blob: b5fe43ee50f7f68b95bb4b32232b919382000589 [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>
Miquel Raynald677bfe2018-05-15 11:57:06 +020026#include <tpm-v1.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090027#include <linux/errno.h>
Simon Glassb697e0f2015-08-22 18:31:38 -060028#include <linux/compiler.h>
Rong Changf6267992013-04-12 10:44:57 +000029#include <linux/types.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000030#include <linux/unaligned/be_byteshift.h>
Rong Changf6267992013-04-12 10:44:57 +000031
Christophe Ricard1259dcd2016-01-21 23:27:12 +010032#include "tpm_tis.h"
Simon Glassf255d312015-08-22 18:31:31 -060033#include "tpm_internal.h"
Rong Changf6267992013-04-12 10:44:57 +000034
Christophe Ricard1259dcd2016-01-21 23:27:12 +010035enum i2c_chip_type {
36 SLB9635,
37 SLB9645,
38 UNKNOWN,
39};
40
41/* expected value for DIDVID register */
42#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
43#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
44
Vincent Palatinec34fa52013-04-12 11:04:36 +000045static const char * const chip_name[] = {
46 [SLB9635] = "slb9635tt",
47 [SLB9645] = "slb9645tt",
48 [UNKNOWN] = "unknown/fallback to slb9635",
49};
Rong Changf6267992013-04-12 10:44:57 +000050
Christophe Ricard1259dcd2016-01-21 23:27:12 +010051#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
52#define TPM_STS(l) (0x0001 | ((l) << 4))
53#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
54#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
55
Rong Changf6267992013-04-12 10:44:57 +000056/*
Simon Glasse56e20e2015-08-22 18:31:29 -060057 * tpm_tis_i2c_read() - read from TPM register
Rong Changf6267992013-04-12 10:44:57 +000058 * @addr: register address to read from
59 * @buffer: provided by caller
60 * @len: number of bytes to read
61 *
62 * Read len bytes from TPM register and put them into
63 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
64 *
65 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
66 * values have to be swapped.
67 *
68 * Return -EIO on error, 0 on success.
69 */
Simon Glassb697e0f2015-08-22 18:31:38 -060070static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
71 size_t len)
Rong Changf6267992013-04-12 10:44:57 +000072{
Simon Glassb697e0f2015-08-22 18:31:38 -060073 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +000074 int rc;
75 int count;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000076 uint32_t addrbuf = addr;
Rong Changf6267992013-04-12 10:44:57 +000077
Simon Glassb697e0f2015-08-22 18:31:38 -060078 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
Vincent Palatinec34fa52013-04-12 11:04:36 +000079 /* slb9635 protocol should work in both cases */
80 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -060081 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatinec34fa52013-04-12 11:04:36 +000082 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000083 break; /* Success, break to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -060084 udelay(SLEEP_DURATION_US);
Vincent Palatinec34fa52013-04-12 11:04:36 +000085 }
Vincent Palatinec34fa52013-04-12 11:04:36 +000086 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -060087 return rc;
Vincent Palatinec34fa52013-04-12 11:04:36 +000088
89 /* After the TPM has successfully received the register address
90 * it needs some time, thus we're sleeping here again, before
91 * retrieving the data
92 */
93 for (count = 0; count < MAX_COUNT; count++) {
Simon Glass42c8ec52015-08-22 18:31:30 -060094 udelay(SLEEP_DURATION_US);
Simon Glassb697e0f2015-08-22 18:31:38 -060095 rc = dm_i2c_read(dev, 0, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +000096 if (rc == 0)
97 break; /* success, break to skip sleep */
98 }
99 } else {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000100 /*
101 * Use a combined read for newer chips.
102 * Unfortunately the smbus functions are not suitable due to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000103 * the 32 byte limit of the smbus.
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000104 * Retries should usually not be needed, but are kept just to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000105 * be safe on the safe side.
106 */
107 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600108 rc = dm_i2c_read(dev, addr, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000109 if (rc == 0)
110 break; /* break here to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -0600111 udelay(SLEEP_DURATION_US);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000112 }
Rong Changf6267992013-04-12 10:44:57 +0000113 }
114
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000115 /* Take care of 'guard time' */
Simon Glass42c8ec52015-08-22 18:31:30 -0600116 udelay(SLEEP_DURATION_US);
Rong Changf6267992013-04-12 10:44:57 +0000117 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600118 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000119
120 return 0;
121}
122
Simon Glassb697e0f2015-08-22 18:31:38 -0600123static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
124 const u8 *buffer, size_t len,
Simon Glass42c8ec52015-08-22 18:31:30 -0600125 unsigned int sleep_time_us, u8 max_count)
Rong Changf6267992013-04-12 10:44:57 +0000126{
Simon Glassb697e0f2015-08-22 18:31:38 -0600127 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
128 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000129 int rc = 0;
130 int count;
131
Simon Glassb697e0f2015-08-22 18:31:38 -0600132 if (chip->chip_type == SLB9635) {
133 /* Prepare send buffer to include the address */
134 priv->buf[0] = addr;
135 memcpy(&(priv->buf[1]), buffer, len);
136 buffer = priv->buf;
137 len++;
138 addr = 0;
139 }
140
Rong Changf6267992013-04-12 10:44:57 +0000141 for (count = 0; count < max_count; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600142 rc = dm_i2c_write(dev, addr, buffer, len);
Rong Changf6267992013-04-12 10:44:57 +0000143 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000144 break; /* Success, break to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -0600145 udelay(sleep_time_us);
Rong Changf6267992013-04-12 10:44:57 +0000146 }
147
Vincent Palatinec34fa52013-04-12 11:04:36 +0000148 /* take care of 'guard time' */
Simon Glass42c8ec52015-08-22 18:31:30 -0600149 udelay(sleep_time_us);
Rong Changf6267992013-04-12 10:44:57 +0000150 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600151 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000152
153 return 0;
154}
155
156/*
Simon Glasse56e20e2015-08-22 18:31:29 -0600157 * tpm_tis_i2c_write() - write to TPM register
Rong Changf6267992013-04-12 10:44:57 +0000158 * @addr: register address to write to
159 * @buffer: containing data to be written
160 * @len: number of bytes to write
161 *
162 * Write len bytes from provided buffer to TPM register (little
163 * endian format, i.e. buffer[0] is written as first byte).
164 *
165 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
166 * values have to be swapped.
167 *
Simon Glasse56e20e2015-08-22 18:31:29 -0600168 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
Rong Changf6267992013-04-12 10:44:57 +0000169 *
170 * Return -EIO on error, 0 on success
171 */
Simon Glassb697e0f2015-08-22 18:31:38 -0600172static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
173 size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000174{
Simon Glassb697e0f2015-08-22 18:31:38 -0600175 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
176 SLEEP_DURATION_US, MAX_COUNT);
Rong Changf6267992013-04-12 10:44:57 +0000177}
178
179/*
180 * This function is needed especially for the cleanup situation after
181 * sending TPM_READY
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000182 */
Simon Glassb697e0f2015-08-22 18:31:38 -0600183static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
184 size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000185{
Simon Glassb697e0f2015-08-22 18:31:38 -0600186 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
Simon Glass42c8ec52015-08-22 18:31:30 -0600187 SLEEP_DURATION_LONG_US,
188 MAX_COUNT_LONG);
Rong Changf6267992013-04-12 10:44:57 +0000189}
190
Simon Glassb697e0f2015-08-22 18:31:38 -0600191static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
Rong Changf6267992013-04-12 10:44:57 +0000192{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000193 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Simon Glassb697e0f2015-08-22 18:31:38 -0600194 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000195 u8 buf;
196 int rc;
197
Simon Glassb697e0f2015-08-22 18:31:38 -0600198 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
Rong Changf6267992013-04-12 10:44:57 +0000199 if (rc < 0)
200 return rc;
201
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000202 if ((buf & mask) == mask) {
Simon Glass7c735372015-08-22 18:31:24 -0600203 chip->locality = loc;
Rong Changf6267992013-04-12 10:44:57 +0000204 return loc;
205 }
206
Simon Glassb697e0f2015-08-22 18:31:38 -0600207 return -ENOENT;
Rong Changf6267992013-04-12 10:44:57 +0000208}
209
Simon Glassb697e0f2015-08-22 18:31:38 -0600210static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
Simon Glasse56e20e2015-08-22 18:31:29 -0600211 int force)
Rong Changf6267992013-04-12 10:44:57 +0000212{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000213 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000214 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000215
Simon Glassb697e0f2015-08-22 18:31:38 -0600216 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000217 return;
218
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000219 if (force || (buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000220 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Simon Glassb697e0f2015-08-22 18:31:38 -0600221 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
Rong Changf6267992013-04-12 10:44:57 +0000222 }
223}
224
Simon Glassb697e0f2015-08-22 18:31:38 -0600225static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
Rong Changf6267992013-04-12 10:44:57 +0000226{
Simon Glassb697e0f2015-08-22 18:31:38 -0600227 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000228 unsigned long start, stop;
229 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassf90acf12015-05-04 11:30:59 -0600230 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000231
Simon Glassb697e0f2015-08-22 18:31:38 -0600232 rc = tpm_tis_i2c_check_locality(dev, loc);
233 if (rc >= 0) {
234 debug("%s: Already have locality\n", __func__);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000235 return loc; /* We already have the locality */
Simon Glassb697e0f2015-08-22 18:31:38 -0600236 } else if (rc != -ENOENT) {
237 debug("%s: Failed to get locality: %d\n", __func__, rc);
Simon Glassf90acf12015-05-04 11:30:59 -0600238 return rc;
Simon Glassb697e0f2015-08-22 18:31:38 -0600239 }
240
241 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
242 if (rc) {
243 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
244 return rc;
245 }
Rong Changf6267992013-04-12 10:44:57 +0000246
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000247 /* Wait for burstcount */
Rong Changf6267992013-04-12 10:44:57 +0000248 start = get_timer(0);
Simon Glass7c735372015-08-22 18:31:24 -0600249 stop = chip->timeout_a;
Rong Changf6267992013-04-12 10:44:57 +0000250 do {
Simon Glassb697e0f2015-08-22 18:31:38 -0600251 rc = tpm_tis_i2c_check_locality(dev, loc);
252 if (rc >= 0) {
253 debug("%s: Have locality\n", __func__);
Rong Changf6267992013-04-12 10:44:57 +0000254 return loc;
Simon Glassb697e0f2015-08-22 18:31:38 -0600255 } else if (rc != -ENOENT) {
256 debug("%s: Failed to get locality: %d\n", __func__, rc);
257 return rc;
258 }
Simon Glass42c8ec52015-08-22 18:31:30 -0600259 mdelay(TPM_TIMEOUT_MS);
Rong Changf6267992013-04-12 10:44:57 +0000260 } while (get_timer(start) < stop);
Simon Glassb697e0f2015-08-22 18:31:38 -0600261 debug("%s: Timeout getting locality: %d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000262
Simon Glassb697e0f2015-08-22 18:31:38 -0600263 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000264}
265
Simon Glassb697e0f2015-08-22 18:31:38 -0600266static u8 tpm_tis_i2c_status(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000267{
Simon Glassb697e0f2015-08-22 18:31:38 -0600268 struct tpm_chip *chip = dev_get_priv(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000269 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Changf6267992013-04-12 10:44:57 +0000270 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000271
Simon Glassb697e0f2015-08-22 18:31:38 -0600272 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000273 return 0;
274 else
275 return buf;
276}
277
Simon Glassb697e0f2015-08-22 18:31:38 -0600278static int tpm_tis_i2c_ready(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000279{
Simon Glassb697e0f2015-08-22 18:31:38 -0600280 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glassf90acf12015-05-04 11:30:59 -0600281 int rc;
282
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000283 /* This causes the current command to be aborted */
Rong Changf6267992013-04-12 10:44:57 +0000284 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000285
Simon Glassf90acf12015-05-04 11:30:59 -0600286 debug("%s\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600287 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600288 if (rc)
289 debug("%s: rc=%d\n", __func__, rc);
Simon Glassb697e0f2015-08-22 18:31:38 -0600290
291 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000292}
293
Simon Glassb697e0f2015-08-22 18:31:38 -0600294static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000295{
Simon Glassb697e0f2015-08-22 18:31:38 -0600296 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000297 unsigned long start, stop;
298 ssize_t burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000299 u8 addr, buf[3];
Rong Changf6267992013-04-12 10:44:57 +0000300
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000301 /* Wait for burstcount */
302 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Changf6267992013-04-12 10:44:57 +0000303 start = get_timer(0);
Simon Glass7c735372015-08-22 18:31:24 -0600304 stop = chip->timeout_d;
Rong Changf6267992013-04-12 10:44:57 +0000305 do {
306 /* Note: STS is little endian */
Simon Glass7c735372015-08-22 18:31:24 -0600307 addr = TPM_STS(chip->locality) + 1;
Simon Glassb697e0f2015-08-22 18:31:38 -0600308 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000309 burstcnt = 0;
310 else
311 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
312
313 if (burstcnt)
314 return burstcnt;
Simon Glass42c8ec52015-08-22 18:31:30 -0600315 mdelay(TPM_TIMEOUT_MS);
Rong Changf6267992013-04-12 10:44:57 +0000316 } while (get_timer(start) < stop);
317
318 return -EBUSY;
319}
320
Simon Glassb697e0f2015-08-22 18:31:38 -0600321static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
Simon Glasse56e20e2015-08-22 18:31:29 -0600322 unsigned long timeout, int *status)
Rong Changf6267992013-04-12 10:44:57 +0000323{
324 unsigned long start, stop;
325
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000326 /* Check current status */
Simon Glassb697e0f2015-08-22 18:31:38 -0600327 *status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000328 if ((*status & mask) == mask)
329 return 0;
330
331 start = get_timer(0);
332 stop = timeout;
333 do {
Simon Glass42c8ec52015-08-22 18:31:30 -0600334 mdelay(TPM_TIMEOUT_MS);
Simon Glassb697e0f2015-08-22 18:31:38 -0600335 *status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000336 if ((*status & mask) == mask)
337 return 0;
Rong Changf6267992013-04-12 10:44:57 +0000338 } while (get_timer(start) < stop);
339
Simon Glassb697e0f2015-08-22 18:31:38 -0600340 return -ETIMEDOUT;
Rong Changf6267992013-04-12 10:44:57 +0000341}
342
Simon Glassb697e0f2015-08-22 18:31:38 -0600343static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
Rong Changf6267992013-04-12 10:44:57 +0000344{
Simon Glassb697e0f2015-08-22 18:31:38 -0600345 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000346 size_t size = 0;
347 ssize_t burstcnt;
348 int rc;
349
350 while (size < count) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600351 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Rong Changf6267992013-04-12 10:44:57 +0000352
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000353 /* burstcount < 0 -> tpm is busy */
Rong Changf6267992013-04-12 10:44:57 +0000354 if (burstcnt < 0)
355 return burstcnt;
356
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000357 /* Limit received data to max left */
Rong Changf6267992013-04-12 10:44:57 +0000358 if (burstcnt > (count - size))
359 burstcnt = count - size;
360
Simon Glassb697e0f2015-08-22 18:31:38 -0600361 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
362 &(buf[size]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000363 if (rc == 0)
364 size += burstcnt;
365 }
366
367 return size;
368}
369
Simon Glassb697e0f2015-08-22 18:31:38 -0600370static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
Rong Changf6267992013-04-12 10:44:57 +0000371{
Simon Glassb697e0f2015-08-22 18:31:38 -0600372 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000373 int size = 0;
Jeremy Booneafe0e6b2018-02-12 17:56:36 -0500374 int status;
375 unsigned int expected;
Simon Glassb697e0f2015-08-22 18:31:38 -0600376 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000377
Simon Glassb697e0f2015-08-22 18:31:38 -0600378 status = tpm_tis_i2c_status(dev);
379 if (status == TPM_STS_COMMAND_READY)
380 return -EINTR;
381 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
382 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
383 return -EAGAIN;
384
385 debug("...got it;\n");
Rong Changf6267992013-04-12 10:44:57 +0000386
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000387 /* Read first 10 bytes, including tag, paramsize, and result */
Simon Glassb697e0f2015-08-22 18:31:38 -0600388 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000389 if (size < TPM_HEADER_SIZE) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600390 debug("Unable to read header\n");
391 return size < 0 ? size : -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000392 }
393
394 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
Jeremy Booneafe0e6b2018-02-12 17:56:36 -0500395 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600396 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
Simon Glassf90acf12015-05-04 11:30:59 -0600397 count);
Simon Glassb697e0f2015-08-22 18:31:38 -0600398 return -ENOSPC;
Rong Changf6267992013-04-12 10:44:57 +0000399 }
400
Simon Glassb697e0f2015-08-22 18:31:38 -0600401 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
Simon Glasse56e20e2015-08-22 18:31:29 -0600402 expected - TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000403 if (size < expected) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600404 debug("Unable to read remainder of result\n");
405 return -ETIMEDOUT;
Rong Changf6267992013-04-12 10:44:57 +0000406 }
407
Simon Glassb697e0f2015-08-22 18:31:38 -0600408 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
409 &status);
410 if (rc)
411 return rc;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000412 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
Simon Glassb697e0f2015-08-22 18:31:38 -0600413 debug("Error left over data\n");
414 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000415 }
416
Rong Changf6267992013-04-12 10:44:57 +0000417 return size;
418}
419
Simon Glassb697e0f2015-08-22 18:31:38 -0600420static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000421{
Simon Glassb697e0f2015-08-22 18:31:38 -0600422 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000423 int rc, status;
Simon Glassf90acf12015-05-04 11:30:59 -0600424 size_t burstcnt;
Rong Changf6267992013-04-12 10:44:57 +0000425 size_t count = 0;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000426 int retry = 0;
Rong Changf6267992013-04-12 10:44:57 +0000427 u8 sts = TPM_STS_GO;
428
Simon Glassf90acf12015-05-04 11:30:59 -0600429 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000430 if (len > TPM_DEV_BUFSIZE)
431 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Changf6267992013-04-12 10:44:57 +0000432
Simon Glassb697e0f2015-08-22 18:31:38 -0600433 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000434 return -EBUSY;
435
Simon Glassb697e0f2015-08-22 18:31:38 -0600436 status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000437 if ((status & TPM_STS_COMMAND_READY) == 0) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600438 rc = tpm_tis_i2c_ready(dev);
439 if (rc)
440 return rc;
441 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
442 chip->timeout_b, &status);
443 if (rc)
444 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000445 }
446
Simon Glassb697e0f2015-08-22 18:31:38 -0600447 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000448
449 /* burstcount < 0 -> tpm is busy */
450 if (burstcnt < 0)
451 return burstcnt;
452
Simon Glassf90acf12015-05-04 11:30:59 -0600453 while (count < len) {
454 udelay(300);
455 if (burstcnt > len - count)
456 burstcnt = len - count;
Rong Changf6267992013-04-12 10:44:57 +0000457
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000458#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
Simon Glassb697e0f2015-08-22 18:31:38 -0600459 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
460 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000461#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
Rong Changf6267992013-04-12 10:44:57 +0000462
Simon Glassb697e0f2015-08-22 18:31:38 -0600463 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
464 &(buf[count]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000465 if (rc == 0)
466 count += burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000467 else {
Simon Glassf90acf12015-05-04 11:30:59 -0600468 debug("%s: error\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600469 if (retry++ > 10)
470 return -EIO;
471 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
Simon Glasse56e20e2015-08-22 18:31:29 -0600472 chip->timeout_c,
473 &status);
Simon Glassf90acf12015-05-04 11:30:59 -0600474 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600475 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000476
Simon Glassb697e0f2015-08-22 18:31:38 -0600477 if ((status & TPM_STS_DATA_EXPECT) == 0)
478 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000479 }
480 }
481
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000482 /* Go and do it */
Simon Glassb697e0f2015-08-22 18:31:38 -0600483 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
484 if (rc < 0)
485 return rc;
486 debug("%s: done, rc=%d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000487
488 return len;
Simon Glassb697e0f2015-08-22 18:31:38 -0600489}
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000490
Simon Glassb697e0f2015-08-22 18:31:38 -0600491static int tpm_tis_i2c_cleanup(struct udevice *dev)
492{
493 struct tpm_chip *chip = dev_get_priv(dev);
494
495 tpm_tis_i2c_ready(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000496 /*
497 * The TPM needs some time to clean up here,
Rong Changf6267992013-04-12 10:44:57 +0000498 * so we sleep rather than keeping the bus busy
499 */
Simon Glass42c8ec52015-08-22 18:31:30 -0600500 mdelay(2);
Simon Glassb697e0f2015-08-22 18:31:38 -0600501 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
Rong Changf6267992013-04-12 10:44:57 +0000502
Simon Glassb697e0f2015-08-22 18:31:38 -0600503 return 0;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000504}
505
Simon Glass13932b02015-08-22 18:31:25 -0600506static int tpm_tis_i2c_init(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000507{
Simon Glassb697e0f2015-08-22 18:31:38 -0600508 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000509 u32 vendor;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000510 u32 expected_did_vid;
Simon Glassb697e0f2015-08-22 18:31:38 -0600511 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000512
Simon Glass7c735372015-08-22 18:31:24 -0600513 chip->is_open = 1;
Rong Changf6267992013-04-12 10:44:57 +0000514
Simon Glasse56e20e2015-08-22 18:31:29 -0600515 /* Default timeouts - these could move to the device tree */
Simon Glass42c8ec52015-08-22 18:31:30 -0600516 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
517 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
518 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
519 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
Rong Changf6267992013-04-12 10:44:57 +0000520
Simon Glassb697e0f2015-08-22 18:31:38 -0600521 rc = tpm_tis_i2c_request_locality(dev, 0);
522 if (rc < 0)
523 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000524
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000525 /* Read four bytes from DID_VID register */
Simon Glassb697e0f2015-08-22 18:31:38 -0600526 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
527 tpm_tis_i2c_release_locality(dev, 0, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600528 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000529 }
530
Simon Glassb697e0f2015-08-22 18:31:38 -0600531 if (chip->chip_type == SLB9635) {
Vincent Palatinec34fa52013-04-12 11:04:36 +0000532 vendor = be32_to_cpu(vendor);
533 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
534 } else {
535 /* device id and byte order has changed for newer i2c tpms */
536 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
537 }
Rong Changf6267992013-04-12 10:44:57 +0000538
Simon Glassb697e0f2015-08-22 18:31:38 -0600539 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900540 pr_err("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassf90acf12015-05-04 11:30:59 -0600541 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000542 }
543
Simon Glassb697e0f2015-08-22 18:31:38 -0600544 chip->vend_dev = vendor;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000545 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
Simon Glassb697e0f2015-08-22 18:31:38 -0600546 chip_name[chip->chip_type], vendor >> 16);
Rong Changf6267992013-04-12 10:44:57 +0000547
548 /*
549 * A timeout query to TPM can be placed here.
550 * Standard timeout values are used so far
551 */
552
553 return 0;
Simon Glassf90acf12015-05-04 11:30:59 -0600554}
Rong Changf6267992013-04-12 10:44:57 +0000555
Simon Glassb697e0f2015-08-22 18:31:38 -0600556static int tpm_tis_i2c_open(struct udevice *dev)
Simon Glass4cd7b782015-08-22 18:31:22 -0600557{
Simon Glassb697e0f2015-08-22 18:31:38 -0600558 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600559 int rc;
Simon Glass4cd7b782015-08-22 18:31:22 -0600560
Simon Glassa53b79a2015-08-22 18:31:28 -0600561 debug("%s: start\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600562 if (chip->is_open)
Simon Glassa53b79a2015-08-22 18:31:28 -0600563 return -EBUSY;
Simon Glassb697e0f2015-08-22 18:31:38 -0600564 rc = tpm_tis_i2c_init(dev);
Simon Glassa53b79a2015-08-22 18:31:28 -0600565 if (rc < 0)
Simon Glassb697e0f2015-08-22 18:31:38 -0600566 chip->is_open = 0;
Simon Glass4cd7b782015-08-22 18:31:22 -0600567
568 return rc;
569}
570
Simon Glassb697e0f2015-08-22 18:31:38 -0600571static int tpm_tis_i2c_close(struct udevice *dev)
Simon Glass4cd7b782015-08-22 18:31:22 -0600572{
Simon Glassb697e0f2015-08-22 18:31:38 -0600573 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600574
Simon Glassb697e0f2015-08-22 18:31:38 -0600575 if (chip->is_open) {
576 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
577 chip->is_open = 0;
578 chip->vend_dev = 0;
Simon Glassa53b79a2015-08-22 18:31:28 -0600579 }
Simon Glass4cd7b782015-08-22 18:31:22 -0600580
581 return 0;
582}
583
Simon Glassb697e0f2015-08-22 18:31:38 -0600584static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
Simon Glass4cd7b782015-08-22 18:31:22 -0600585{
Simon Glassb697e0f2015-08-22 18:31:38 -0600586 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600587
Simon Glassb697e0f2015-08-22 18:31:38 -0600588 if (size < 50)
589 return -ENOSPC;
Simon Glass4cd7b782015-08-22 18:31:22 -0600590
Simon Glassb697e0f2015-08-22 18:31:38 -0600591 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
592 chip->is_open ? "open" : "closed",
593 chip_name[chip->chip_type],
594 chip->vend_dev >> 16);
595}
Simon Glass4cd7b782015-08-22 18:31:22 -0600596
Simon Glassb697e0f2015-08-22 18:31:38 -0600597static int tpm_tis_i2c_probe(struct udevice *dev)
598{
599 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
600 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600601
Simon Glassb697e0f2015-08-22 18:31:38 -0600602 chip->chip_type = dev_get_driver_data(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600603
Simon Glassb697e0f2015-08-22 18:31:38 -0600604 /* TODO: These need to be checked and tuned */
605 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
606 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
607 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
608 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
Simon Glass4cd7b782015-08-22 18:31:22 -0600609
610 return 0;
611}
Simon Glassb697e0f2015-08-22 18:31:38 -0600612
613static const struct tpm_ops tpm_tis_i2c_ops = {
614 .open = tpm_tis_i2c_open,
615 .close = tpm_tis_i2c_close,
616 .get_desc = tpm_tis_get_desc,
617 .send = tpm_tis_i2c_send,
618 .recv = tpm_tis_i2c_recv,
619 .cleanup = tpm_tis_i2c_cleanup,
620};
621
622static const struct udevice_id tpm_tis_i2c_ids[] = {
623 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
624 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
625 { }
626};
627
628U_BOOT_DRIVER(tpm_tis_i2c) = {
Christophe Ricard0766ad22015-10-06 22:54:41 +0200629 .name = "tpm_tis_infineon",
Simon Glassb697e0f2015-08-22 18:31:38 -0600630 .id = UCLASS_TPM,
631 .of_match = tpm_tis_i2c_ids,
632 .ops = &tpm_tis_i2c_ops,
633 .probe = tpm_tis_i2c_probe,
634 .priv_auto_alloc_size = sizeof(struct tpm_chip),
635};