blob: f47813e76d097b0279b8d433b285a1b55fea5dd6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Rong Changf6267992013-04-12 10:44:57 +00002/*
3 * Copyright (C) 2011 Infineon Technologies
4 *
5 * Authors:
6 * Peter Huewe <huewe.external@infineon.com>
7 *
8 * Description:
9 * Device driver for TCG/TCPA TPM (trusted platform module).
10 * Specifications at www.trustedcomputinggroup.org
11 *
12 * This device driver implements the TPM interface as defined in
13 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
14 * Infineon I2C Protocol Stack Specification v0.20.
15 *
16 * It is based on the Linux kernel driver tpm.c from Leendert van
17 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
18 *
19 * Version: 2.1.1
Rong Changf6267992013-04-12 10:44:57 +000020 */
21
22#include <common.h>
Simon Glassf90acf12015-05-04 11:30:59 -060023#include <dm.h>
Vincent Palatinec34fa52013-04-12 11:04:36 +000024#include <fdtdec.h>
Rong Changf6267992013-04-12 10:44:57 +000025#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060026#include <log.h>
Miquel Raynald677bfe2018-05-15 11:57:06 +020027#include <tpm-v1.h>
Simon Glassc05ed002020-05-10 11:40:11 -060028#include <linux/delay.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090029#include <linux/errno.h>
Simon Glassb697e0f2015-08-22 18:31:38 -060030#include <linux/compiler.h>
Rong Changf6267992013-04-12 10:44:57 +000031#include <linux/types.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000032#include <linux/unaligned/be_byteshift.h>
Rong Changf6267992013-04-12 10:44:57 +000033
Christophe Ricard1259dcd2016-01-21 23:27:12 +010034#include "tpm_tis.h"
Simon Glassf255d312015-08-22 18:31:31 -060035#include "tpm_internal.h"
Rong Changf6267992013-04-12 10:44:57 +000036
Christophe Ricard1259dcd2016-01-21 23:27:12 +010037enum i2c_chip_type {
38 SLB9635,
39 SLB9645,
40 UNKNOWN,
41};
42
43/* expected value for DIDVID register */
44#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
45#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
46
Vincent Palatinec34fa52013-04-12 11:04:36 +000047static const char * const chip_name[] = {
48 [SLB9635] = "slb9635tt",
49 [SLB9645] = "slb9645tt",
50 [UNKNOWN] = "unknown/fallback to slb9635",
51};
Rong Changf6267992013-04-12 10:44:57 +000052
Christophe Ricard1259dcd2016-01-21 23:27:12 +010053#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
54#define TPM_STS(l) (0x0001 | ((l) << 4))
55#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
56#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
57
Rong Changf6267992013-04-12 10:44:57 +000058/*
Simon Glasse56e20e2015-08-22 18:31:29 -060059 * tpm_tis_i2c_read() - read from TPM register
Rong Changf6267992013-04-12 10:44:57 +000060 * @addr: register address to read from
61 * @buffer: provided by caller
62 * @len: number of bytes to read
63 *
64 * Read len bytes from TPM register and put them into
65 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
66 *
67 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
68 * values have to be swapped.
69 *
70 * Return -EIO on error, 0 on success.
71 */
Simon Glassb697e0f2015-08-22 18:31:38 -060072static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
73 size_t len)
Rong Changf6267992013-04-12 10:44:57 +000074{
Simon Glassb697e0f2015-08-22 18:31:38 -060075 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +000076 int rc;
77 int count;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000078 uint32_t addrbuf = addr;
Rong Changf6267992013-04-12 10:44:57 +000079
Simon Glassb697e0f2015-08-22 18:31:38 -060080 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
Vincent Palatinec34fa52013-04-12 11:04:36 +000081 /* slb9635 protocol should work in both cases */
82 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -060083 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatinec34fa52013-04-12 11:04:36 +000084 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000085 break; /* Success, break to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -060086 udelay(SLEEP_DURATION_US);
Vincent Palatinec34fa52013-04-12 11:04:36 +000087 }
Vincent Palatinec34fa52013-04-12 11:04:36 +000088 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -060089 return rc;
Vincent Palatinec34fa52013-04-12 11:04:36 +000090
91 /* After the TPM has successfully received the register address
92 * it needs some time, thus we're sleeping here again, before
93 * retrieving the data
94 */
95 for (count = 0; count < MAX_COUNT; count++) {
Simon Glass42c8ec52015-08-22 18:31:30 -060096 udelay(SLEEP_DURATION_US);
Simon Glassb697e0f2015-08-22 18:31:38 -060097 rc = dm_i2c_read(dev, 0, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +000098 if (rc == 0)
99 break; /* success, break to skip sleep */
100 }
101 } else {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000102 /*
103 * Use a combined read for newer chips.
104 * Unfortunately the smbus functions are not suitable due to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000105 * the 32 byte limit of the smbus.
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000106 * Retries should usually not be needed, but are kept just to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000107 * be safe on the safe side.
108 */
109 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600110 rc = dm_i2c_read(dev, addr, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000111 if (rc == 0)
112 break; /* break here to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -0600113 udelay(SLEEP_DURATION_US);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000114 }
Rong Changf6267992013-04-12 10:44:57 +0000115 }
116
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000117 /* Take care of 'guard time' */
Simon Glass42c8ec52015-08-22 18:31:30 -0600118 udelay(SLEEP_DURATION_US);
Rong Changf6267992013-04-12 10:44:57 +0000119 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600120 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000121
122 return 0;
123}
124
Simon Glassb697e0f2015-08-22 18:31:38 -0600125static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
126 const u8 *buffer, size_t len,
Simon Glass42c8ec52015-08-22 18:31:30 -0600127 unsigned int sleep_time_us, u8 max_count)
Rong Changf6267992013-04-12 10:44:57 +0000128{
Simon Glassb697e0f2015-08-22 18:31:38 -0600129 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
130 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000131 int rc = 0;
132 int count;
133
Simon Glassb697e0f2015-08-22 18:31:38 -0600134 if (chip->chip_type == SLB9635) {
135 /* Prepare send buffer to include the address */
136 priv->buf[0] = addr;
137 memcpy(&(priv->buf[1]), buffer, len);
138 buffer = priv->buf;
139 len++;
140 addr = 0;
141 }
142
Rong Changf6267992013-04-12 10:44:57 +0000143 for (count = 0; count < max_count; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600144 rc = dm_i2c_write(dev, addr, buffer, len);
Rong Changf6267992013-04-12 10:44:57 +0000145 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000146 break; /* Success, break to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -0600147 udelay(sleep_time_us);
Rong Changf6267992013-04-12 10:44:57 +0000148 }
149
Vincent Palatinec34fa52013-04-12 11:04:36 +0000150 /* take care of 'guard time' */
Simon Glass42c8ec52015-08-22 18:31:30 -0600151 udelay(sleep_time_us);
Rong Changf6267992013-04-12 10:44:57 +0000152 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600153 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000154
155 return 0;
156}
157
158/*
Simon Glasse56e20e2015-08-22 18:31:29 -0600159 * tpm_tis_i2c_write() - write to TPM register
Rong Changf6267992013-04-12 10:44:57 +0000160 * @addr: register address to write to
161 * @buffer: containing data to be written
162 * @len: number of bytes to write
163 *
164 * Write len bytes from provided buffer to TPM register (little
165 * endian format, i.e. buffer[0] is written as first byte).
166 *
167 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
168 * values have to be swapped.
169 *
Simon Glasse56e20e2015-08-22 18:31:29 -0600170 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
Rong Changf6267992013-04-12 10:44:57 +0000171 *
172 * Return -EIO on error, 0 on success
173 */
Simon Glassb697e0f2015-08-22 18:31:38 -0600174static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
175 size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000176{
Simon Glassb697e0f2015-08-22 18:31:38 -0600177 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
178 SLEEP_DURATION_US, MAX_COUNT);
Rong Changf6267992013-04-12 10:44:57 +0000179}
180
181/*
182 * This function is needed especially for the cleanup situation after
183 * sending TPM_READY
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000184 */
Simon Glassb697e0f2015-08-22 18:31:38 -0600185static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
186 size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000187{
Simon Glassb697e0f2015-08-22 18:31:38 -0600188 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
Simon Glass42c8ec52015-08-22 18:31:30 -0600189 SLEEP_DURATION_LONG_US,
190 MAX_COUNT_LONG);
Rong Changf6267992013-04-12 10:44:57 +0000191}
192
Simon Glassb697e0f2015-08-22 18:31:38 -0600193static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
Rong Changf6267992013-04-12 10:44:57 +0000194{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000195 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Simon Glassb697e0f2015-08-22 18:31:38 -0600196 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000197 u8 buf;
198 int rc;
199
Simon Glassb697e0f2015-08-22 18:31:38 -0600200 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
Rong Changf6267992013-04-12 10:44:57 +0000201 if (rc < 0)
202 return rc;
203
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000204 if ((buf & mask) == mask) {
Simon Glass7c735372015-08-22 18:31:24 -0600205 chip->locality = loc;
Rong Changf6267992013-04-12 10:44:57 +0000206 return loc;
207 }
208
Simon Glassb697e0f2015-08-22 18:31:38 -0600209 return -ENOENT;
Rong Changf6267992013-04-12 10:44:57 +0000210}
211
Simon Glassb697e0f2015-08-22 18:31:38 -0600212static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
Simon Glasse56e20e2015-08-22 18:31:29 -0600213 int force)
Rong Changf6267992013-04-12 10:44:57 +0000214{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000215 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000216 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000217
Simon Glassb697e0f2015-08-22 18:31:38 -0600218 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000219 return;
220
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000221 if (force || (buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000222 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Simon Glassb697e0f2015-08-22 18:31:38 -0600223 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
Rong Changf6267992013-04-12 10:44:57 +0000224 }
225}
226
Simon Glassb697e0f2015-08-22 18:31:38 -0600227static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
Rong Changf6267992013-04-12 10:44:57 +0000228{
Simon Glassb697e0f2015-08-22 18:31:38 -0600229 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000230 unsigned long start, stop;
231 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassf90acf12015-05-04 11:30:59 -0600232 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000233
Simon Glassb697e0f2015-08-22 18:31:38 -0600234 rc = tpm_tis_i2c_check_locality(dev, loc);
235 if (rc >= 0) {
236 debug("%s: Already have locality\n", __func__);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000237 return loc; /* We already have the locality */
Simon Glassb697e0f2015-08-22 18:31:38 -0600238 } else if (rc != -ENOENT) {
239 debug("%s: Failed to get locality: %d\n", __func__, rc);
Simon Glassf90acf12015-05-04 11:30:59 -0600240 return rc;
Simon Glassb697e0f2015-08-22 18:31:38 -0600241 }
242
243 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
244 if (rc) {
245 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
246 return rc;
247 }
Rong Changf6267992013-04-12 10:44:57 +0000248
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000249 /* Wait for burstcount */
Rong Changf6267992013-04-12 10:44:57 +0000250 start = get_timer(0);
Simon Glass7c735372015-08-22 18:31:24 -0600251 stop = chip->timeout_a;
Rong Changf6267992013-04-12 10:44:57 +0000252 do {
Simon Glassb697e0f2015-08-22 18:31:38 -0600253 rc = tpm_tis_i2c_check_locality(dev, loc);
254 if (rc >= 0) {
255 debug("%s: Have locality\n", __func__);
Rong Changf6267992013-04-12 10:44:57 +0000256 return loc;
Simon Glassb697e0f2015-08-22 18:31:38 -0600257 } else if (rc != -ENOENT) {
258 debug("%s: Failed to get locality: %d\n", __func__, rc);
259 return rc;
260 }
Simon Glass42c8ec52015-08-22 18:31:30 -0600261 mdelay(TPM_TIMEOUT_MS);
Rong Changf6267992013-04-12 10:44:57 +0000262 } while (get_timer(start) < stop);
Simon Glassb697e0f2015-08-22 18:31:38 -0600263 debug("%s: Timeout getting locality: %d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000264
Simon Glassb697e0f2015-08-22 18:31:38 -0600265 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000266}
267
Simon Glassb697e0f2015-08-22 18:31:38 -0600268static u8 tpm_tis_i2c_status(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000269{
Simon Glassb697e0f2015-08-22 18:31:38 -0600270 struct tpm_chip *chip = dev_get_priv(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000271 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Changf6267992013-04-12 10:44:57 +0000272 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000273
Simon Glassb697e0f2015-08-22 18:31:38 -0600274 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000275 return 0;
276 else
277 return buf;
278}
279
Simon Glassb697e0f2015-08-22 18:31:38 -0600280static int tpm_tis_i2c_ready(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000281{
Simon Glassb697e0f2015-08-22 18:31:38 -0600282 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glassf90acf12015-05-04 11:30:59 -0600283 int rc;
284
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000285 /* This causes the current command to be aborted */
Rong Changf6267992013-04-12 10:44:57 +0000286 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000287
Simon Glassf90acf12015-05-04 11:30:59 -0600288 debug("%s\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600289 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600290 if (rc)
291 debug("%s: rc=%d\n", __func__, rc);
Simon Glassb697e0f2015-08-22 18:31:38 -0600292
293 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000294}
295
Simon Glassb697e0f2015-08-22 18:31:38 -0600296static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000297{
Simon Glassb697e0f2015-08-22 18:31:38 -0600298 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000299 unsigned long start, stop;
300 ssize_t burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000301 u8 addr, buf[3];
Rong Changf6267992013-04-12 10:44:57 +0000302
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000303 /* Wait for burstcount */
304 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Changf6267992013-04-12 10:44:57 +0000305 start = get_timer(0);
Simon Glass7c735372015-08-22 18:31:24 -0600306 stop = chip->timeout_d;
Rong Changf6267992013-04-12 10:44:57 +0000307 do {
308 /* Note: STS is little endian */
Simon Glass7c735372015-08-22 18:31:24 -0600309 addr = TPM_STS(chip->locality) + 1;
Simon Glassb697e0f2015-08-22 18:31:38 -0600310 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000311 burstcnt = 0;
312 else
313 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
314
315 if (burstcnt)
316 return burstcnt;
Simon Glass42c8ec52015-08-22 18:31:30 -0600317 mdelay(TPM_TIMEOUT_MS);
Rong Changf6267992013-04-12 10:44:57 +0000318 } while (get_timer(start) < stop);
319
320 return -EBUSY;
321}
322
Simon Glassb697e0f2015-08-22 18:31:38 -0600323static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
Simon Glasse56e20e2015-08-22 18:31:29 -0600324 unsigned long timeout, int *status)
Rong Changf6267992013-04-12 10:44:57 +0000325{
326 unsigned long start, stop;
327
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000328 /* Check current status */
Simon Glassb697e0f2015-08-22 18:31:38 -0600329 *status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000330 if ((*status & mask) == mask)
331 return 0;
332
333 start = get_timer(0);
334 stop = timeout;
335 do {
Simon Glass42c8ec52015-08-22 18:31:30 -0600336 mdelay(TPM_TIMEOUT_MS);
Simon Glassb697e0f2015-08-22 18:31:38 -0600337 *status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000338 if ((*status & mask) == mask)
339 return 0;
Rong Changf6267992013-04-12 10:44:57 +0000340 } while (get_timer(start) < stop);
341
Simon Glassb697e0f2015-08-22 18:31:38 -0600342 return -ETIMEDOUT;
Rong Changf6267992013-04-12 10:44:57 +0000343}
344
Simon Glassb697e0f2015-08-22 18:31:38 -0600345static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
Rong Changf6267992013-04-12 10:44:57 +0000346{
Simon Glassb697e0f2015-08-22 18:31:38 -0600347 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000348 size_t size = 0;
349 ssize_t burstcnt;
350 int rc;
351
352 while (size < count) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600353 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Rong Changf6267992013-04-12 10:44:57 +0000354
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000355 /* burstcount < 0 -> tpm is busy */
Rong Changf6267992013-04-12 10:44:57 +0000356 if (burstcnt < 0)
357 return burstcnt;
358
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000359 /* Limit received data to max left */
Rong Changf6267992013-04-12 10:44:57 +0000360 if (burstcnt > (count - size))
361 burstcnt = count - size;
362
Simon Glassb697e0f2015-08-22 18:31:38 -0600363 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
364 &(buf[size]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000365 if (rc == 0)
366 size += burstcnt;
367 }
368
369 return size;
370}
371
Simon Glassb697e0f2015-08-22 18:31:38 -0600372static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
Rong Changf6267992013-04-12 10:44:57 +0000373{
Simon Glassb697e0f2015-08-22 18:31:38 -0600374 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000375 int size = 0;
Jeremy Booneafe0e6b2018-02-12 17:56:36 -0500376 int status;
377 unsigned int expected;
Simon Glassb697e0f2015-08-22 18:31:38 -0600378 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000379
Simon Glassb697e0f2015-08-22 18:31:38 -0600380 status = tpm_tis_i2c_status(dev);
381 if (status == TPM_STS_COMMAND_READY)
382 return -EINTR;
383 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
384 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
385 return -EAGAIN;
386
387 debug("...got it;\n");
Rong Changf6267992013-04-12 10:44:57 +0000388
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000389 /* Read first 10 bytes, including tag, paramsize, and result */
Simon Glassb697e0f2015-08-22 18:31:38 -0600390 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000391 if (size < TPM_HEADER_SIZE) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600392 debug("Unable to read header\n");
393 return size < 0 ? size : -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000394 }
395
396 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
Jeremy Booneafe0e6b2018-02-12 17:56:36 -0500397 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600398 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
Simon Glassf90acf12015-05-04 11:30:59 -0600399 count);
Simon Glassb697e0f2015-08-22 18:31:38 -0600400 return -ENOSPC;
Rong Changf6267992013-04-12 10:44:57 +0000401 }
402
Simon Glassb697e0f2015-08-22 18:31:38 -0600403 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
Simon Glasse56e20e2015-08-22 18:31:29 -0600404 expected - TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000405 if (size < expected) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600406 debug("Unable to read remainder of result\n");
407 return -ETIMEDOUT;
Rong Changf6267992013-04-12 10:44:57 +0000408 }
409
Simon Glassb697e0f2015-08-22 18:31:38 -0600410 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
411 &status);
412 if (rc)
413 return rc;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000414 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
Simon Glassb697e0f2015-08-22 18:31:38 -0600415 debug("Error left over data\n");
416 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000417 }
418
Rong Changf6267992013-04-12 10:44:57 +0000419 return size;
420}
421
Simon Glassb697e0f2015-08-22 18:31:38 -0600422static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000423{
Simon Glassb697e0f2015-08-22 18:31:38 -0600424 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000425 int rc, status;
Simon Glassf90acf12015-05-04 11:30:59 -0600426 size_t burstcnt;
Rong Changf6267992013-04-12 10:44:57 +0000427 size_t count = 0;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000428 int retry = 0;
Rong Changf6267992013-04-12 10:44:57 +0000429 u8 sts = TPM_STS_GO;
430
Simon Glassf90acf12015-05-04 11:30:59 -0600431 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000432 if (len > TPM_DEV_BUFSIZE)
433 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Changf6267992013-04-12 10:44:57 +0000434
Simon Glassb697e0f2015-08-22 18:31:38 -0600435 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000436 return -EBUSY;
437
Simon Glassb697e0f2015-08-22 18:31:38 -0600438 status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000439 if ((status & TPM_STS_COMMAND_READY) == 0) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600440 rc = tpm_tis_i2c_ready(dev);
441 if (rc)
442 return rc;
443 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
444 chip->timeout_b, &status);
445 if (rc)
446 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000447 }
448
Simon Glassb697e0f2015-08-22 18:31:38 -0600449 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000450
451 /* burstcount < 0 -> tpm is busy */
452 if (burstcnt < 0)
453 return burstcnt;
454
Simon Glassf90acf12015-05-04 11:30:59 -0600455 while (count < len) {
456 udelay(300);
457 if (burstcnt > len - count)
458 burstcnt = len - count;
Rong Changf6267992013-04-12 10:44:57 +0000459
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000460#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
Simon Glassb697e0f2015-08-22 18:31:38 -0600461 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
462 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000463#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
Rong Changf6267992013-04-12 10:44:57 +0000464
Simon Glassb697e0f2015-08-22 18:31:38 -0600465 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
466 &(buf[count]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000467 if (rc == 0)
468 count += burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000469 else {
Simon Glassf90acf12015-05-04 11:30:59 -0600470 debug("%s: error\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600471 if (retry++ > 10)
472 return -EIO;
473 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
Simon Glasse56e20e2015-08-22 18:31:29 -0600474 chip->timeout_c,
475 &status);
Simon Glassf90acf12015-05-04 11:30:59 -0600476 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600477 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000478
Simon Glassb697e0f2015-08-22 18:31:38 -0600479 if ((status & TPM_STS_DATA_EXPECT) == 0)
480 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000481 }
482 }
483
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000484 /* Go and do it */
Simon Glassb697e0f2015-08-22 18:31:38 -0600485 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
486 if (rc < 0)
487 return rc;
488 debug("%s: done, rc=%d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000489
490 return len;
Simon Glassb697e0f2015-08-22 18:31:38 -0600491}
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000492
Simon Glassb697e0f2015-08-22 18:31:38 -0600493static int tpm_tis_i2c_cleanup(struct udevice *dev)
494{
495 struct tpm_chip *chip = dev_get_priv(dev);
496
497 tpm_tis_i2c_ready(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000498 /*
499 * The TPM needs some time to clean up here,
Rong Changf6267992013-04-12 10:44:57 +0000500 * so we sleep rather than keeping the bus busy
501 */
Simon Glass42c8ec52015-08-22 18:31:30 -0600502 mdelay(2);
Simon Glassb697e0f2015-08-22 18:31:38 -0600503 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
Rong Changf6267992013-04-12 10:44:57 +0000504
Simon Glassb697e0f2015-08-22 18:31:38 -0600505 return 0;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000506}
507
Simon Glass13932b02015-08-22 18:31:25 -0600508static int tpm_tis_i2c_init(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000509{
Simon Glassb697e0f2015-08-22 18:31:38 -0600510 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000511 u32 vendor;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000512 u32 expected_did_vid;
Simon Glassb697e0f2015-08-22 18:31:38 -0600513 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000514
Simon Glass7c735372015-08-22 18:31:24 -0600515 chip->is_open = 1;
Rong Changf6267992013-04-12 10:44:57 +0000516
Simon Glasse56e20e2015-08-22 18:31:29 -0600517 /* Default timeouts - these could move to the device tree */
Simon Glass42c8ec52015-08-22 18:31:30 -0600518 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
519 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
520 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
521 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
Rong Changf6267992013-04-12 10:44:57 +0000522
Simon Glassb697e0f2015-08-22 18:31:38 -0600523 rc = tpm_tis_i2c_request_locality(dev, 0);
524 if (rc < 0)
525 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000526
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000527 /* Read four bytes from DID_VID register */
Simon Glassb697e0f2015-08-22 18:31:38 -0600528 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
529 tpm_tis_i2c_release_locality(dev, 0, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600530 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000531 }
532
Simon Glassb697e0f2015-08-22 18:31:38 -0600533 if (chip->chip_type == SLB9635) {
Vincent Palatinec34fa52013-04-12 11:04:36 +0000534 vendor = be32_to_cpu(vendor);
535 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
536 } else {
537 /* device id and byte order has changed for newer i2c tpms */
538 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
539 }
Rong Changf6267992013-04-12 10:44:57 +0000540
Simon Glassb697e0f2015-08-22 18:31:38 -0600541 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900542 pr_err("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassf90acf12015-05-04 11:30:59 -0600543 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000544 }
545
Simon Glassb697e0f2015-08-22 18:31:38 -0600546 chip->vend_dev = vendor;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000547 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
Simon Glassb697e0f2015-08-22 18:31:38 -0600548 chip_name[chip->chip_type], vendor >> 16);
Rong Changf6267992013-04-12 10:44:57 +0000549
550 /*
551 * A timeout query to TPM can be placed here.
552 * Standard timeout values are used so far
553 */
554
555 return 0;
Simon Glassf90acf12015-05-04 11:30:59 -0600556}
Rong Changf6267992013-04-12 10:44:57 +0000557
Simon Glassb697e0f2015-08-22 18:31:38 -0600558static int tpm_tis_i2c_open(struct udevice *dev)
Simon Glass4cd7b782015-08-22 18:31:22 -0600559{
Simon Glassb697e0f2015-08-22 18:31:38 -0600560 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600561 int rc;
Simon Glass4cd7b782015-08-22 18:31:22 -0600562
Simon Glassa53b79a2015-08-22 18:31:28 -0600563 debug("%s: start\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600564 if (chip->is_open)
Simon Glassa53b79a2015-08-22 18:31:28 -0600565 return -EBUSY;
Simon Glassb697e0f2015-08-22 18:31:38 -0600566 rc = tpm_tis_i2c_init(dev);
Simon Glassa53b79a2015-08-22 18:31:28 -0600567 if (rc < 0)
Simon Glassb697e0f2015-08-22 18:31:38 -0600568 chip->is_open = 0;
Simon Glass4cd7b782015-08-22 18:31:22 -0600569
570 return rc;
571}
572
Simon Glassb697e0f2015-08-22 18:31:38 -0600573static int tpm_tis_i2c_close(struct udevice *dev)
Simon Glass4cd7b782015-08-22 18:31:22 -0600574{
Simon Glassb697e0f2015-08-22 18:31:38 -0600575 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600576
Simon Glassb697e0f2015-08-22 18:31:38 -0600577 if (chip->is_open) {
578 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
579 chip->is_open = 0;
580 chip->vend_dev = 0;
Simon Glassa53b79a2015-08-22 18:31:28 -0600581 }
Simon Glass4cd7b782015-08-22 18:31:22 -0600582
583 return 0;
584}
585
Simon Glassb697e0f2015-08-22 18:31:38 -0600586static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
Simon Glass4cd7b782015-08-22 18:31:22 -0600587{
Simon Glassb697e0f2015-08-22 18:31:38 -0600588 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600589
Simon Glassb697e0f2015-08-22 18:31:38 -0600590 if (size < 50)
591 return -ENOSPC;
Simon Glass4cd7b782015-08-22 18:31:22 -0600592
Simon Glassb697e0f2015-08-22 18:31:38 -0600593 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
594 chip->is_open ? "open" : "closed",
595 chip_name[chip->chip_type],
596 chip->vend_dev >> 16);
597}
Simon Glass4cd7b782015-08-22 18:31:22 -0600598
Simon Glassb697e0f2015-08-22 18:31:38 -0600599static int tpm_tis_i2c_probe(struct udevice *dev)
600{
601 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
602 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600603
Simon Glassb697e0f2015-08-22 18:31:38 -0600604 chip->chip_type = dev_get_driver_data(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600605
Simon Glassb697e0f2015-08-22 18:31:38 -0600606 /* TODO: These need to be checked and tuned */
607 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
608 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
609 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
610 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
Simon Glass4cd7b782015-08-22 18:31:22 -0600611
612 return 0;
613}
Simon Glassb697e0f2015-08-22 18:31:38 -0600614
615static const struct tpm_ops tpm_tis_i2c_ops = {
616 .open = tpm_tis_i2c_open,
617 .close = tpm_tis_i2c_close,
618 .get_desc = tpm_tis_get_desc,
619 .send = tpm_tis_i2c_send,
620 .recv = tpm_tis_i2c_recv,
621 .cleanup = tpm_tis_i2c_cleanup,
622};
623
624static const struct udevice_id tpm_tis_i2c_ids[] = {
625 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
626 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
627 { }
628};
629
630U_BOOT_DRIVER(tpm_tis_i2c) = {
Christophe Ricard0766ad22015-10-06 22:54:41 +0200631 .name = "tpm_tis_infineon",
Simon Glassb697e0f2015-08-22 18:31:38 -0600632 .id = UCLASS_TPM,
633 .of_match = tpm_tis_i2c_ids,
634 .ops = &tpm_tis_i2c_ops,
635 .probe = tpm_tis_i2c_probe,
636 .priv_auto_alloc_size = sizeof(struct tpm_chip),
637};