blob: f414e5657db9e6f17a4e2500758311b819dcb99f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Rong Chang8faa9452013-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 Chang8faa9452013-04-12 10:44:57 +000020 */
21
22#include <common.h>
Simon Glassc9a3e842015-05-04 11:30:59 -060023#include <dm.h>
Vincent Palatin06883eb2013-04-12 11:04:36 +000024#include <fdtdec.h>
Rong Chang8faa9452013-04-12 10:44:57 +000025#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060026#include <log.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020027#include <tpm-v1.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090029#include <linux/errno.h>
Simon Glasscd8cc672015-08-22 18:31:38 -060030#include <linux/compiler.h>
Rong Chang8faa9452013-04-12 10:44:57 +000031#include <linux/types.h>
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000032#include <linux/unaligned/be_byteshift.h>
Rong Chang8faa9452013-04-12 10:44:57 +000033
Christophe Ricard26549522016-01-21 23:27:12 +010034#include "tpm_tis.h"
Simon Glass6c7a2322015-08-22 18:31:31 -060035#include "tpm_internal.h"
Rong Chang8faa9452013-04-12 10:44:57 +000036
Christophe Ricard26549522016-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 Palatin06883eb2013-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 Chang8faa9452013-04-12 10:44:57 +000052
Christophe Ricard26549522016-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 Chang8faa9452013-04-12 10:44:57 +000058/*
Simon Glass7ccebaf2015-08-22 18:31:29 -060059 * tpm_tis_i2c_read() - read from TPM register
Rong Chang8faa9452013-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 Glasscd8cc672015-08-22 18:31:38 -060072static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
73 size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +000074{
Simon Glasscd8cc672015-08-22 18:31:38 -060075 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +000076 int rc;
77 int count;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000078 uint32_t addrbuf = addr;
Rong Chang8faa9452013-04-12 10:44:57 +000079
Simon Glasscd8cc672015-08-22 18:31:38 -060080 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
Vincent Palatin06883eb2013-04-12 11:04:36 +000081 /* slb9635 protocol should work in both cases */
82 for (count = 0; count < MAX_COUNT; count++) {
Simon Glasscd8cc672015-08-22 18:31:38 -060083 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatin06883eb2013-04-12 11:04:36 +000084 if (rc == 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000085 break; /* Success, break to skip sleep */
Simon Glassa57bcee2015-08-22 18:31:30 -060086 udelay(SLEEP_DURATION_US);
Vincent Palatin06883eb2013-04-12 11:04:36 +000087 }
Vincent Palatin06883eb2013-04-12 11:04:36 +000088 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -060089 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +000090
Vincent Palatin06883eb2013-04-12 11:04:36 +000091 /* 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 Glassa57bcee2015-08-22 18:31:30 -060096 udelay(SLEEP_DURATION_US);
Simon Glasscd8cc672015-08-22 18:31:38 -060097 rc = dm_i2c_read(dev, 0, buffer, len);
Vincent Palatin06883eb2013-04-12 11:04:36 +000098 if (rc == 0)
99 break; /* success, break to skip sleep */
100 }
101 } else {
Tom Wai-Hong Tame49fed52013-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 Palatin06883eb2013-04-12 11:04:36 +0000105 * the 32 byte limit of the smbus.
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000106 * Retries should usually not be needed, but are kept just to
Vincent Palatin06883eb2013-04-12 11:04:36 +0000107 * be safe on the safe side.
108 */
109 for (count = 0; count < MAX_COUNT; count++) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600110 rc = dm_i2c_read(dev, addr, buffer, len);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000111 if (rc == 0)
112 break; /* break here to skip sleep */
Simon Glassa57bcee2015-08-22 18:31:30 -0600113 udelay(SLEEP_DURATION_US);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000114 }
Rong Chang8faa9452013-04-12 10:44:57 +0000115 }
116
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000117 /* Take care of 'guard time' */
Simon Glassa57bcee2015-08-22 18:31:30 -0600118 udelay(SLEEP_DURATION_US);
Rong Chang8faa9452013-04-12 10:44:57 +0000119 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -0600120 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000121
122 return 0;
123}
124
Simon Glasscd8cc672015-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 Glassa57bcee2015-08-22 18:31:30 -0600127 unsigned int sleep_time_us, u8 max_count)
Rong Chang8faa9452013-04-12 10:44:57 +0000128{
Simon Glasscd8cc672015-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 Chang8faa9452013-04-12 10:44:57 +0000131 int rc = 0;
132 int count;
133
Simon Glasscd8cc672015-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 Chang8faa9452013-04-12 10:44:57 +0000143 for (count = 0; count < max_count; count++) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600144 rc = dm_i2c_write(dev, addr, buffer, len);
Rong Chang8faa9452013-04-12 10:44:57 +0000145 if (rc == 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000146 break; /* Success, break to skip sleep */
Simon Glassa57bcee2015-08-22 18:31:30 -0600147 udelay(sleep_time_us);
Rong Chang8faa9452013-04-12 10:44:57 +0000148 }
149
Vincent Palatin06883eb2013-04-12 11:04:36 +0000150 /* take care of 'guard time' */
Simon Glassa57bcee2015-08-22 18:31:30 -0600151 udelay(sleep_time_us);
Rong Chang8faa9452013-04-12 10:44:57 +0000152 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -0600153 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000154
155 return 0;
156}
157
158/*
Simon Glass7ccebaf2015-08-22 18:31:29 -0600159 * tpm_tis_i2c_write() - write to TPM register
Rong Chang8faa9452013-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 Glass7ccebaf2015-08-22 18:31:29 -0600170 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
Rong Chang8faa9452013-04-12 10:44:57 +0000171 *
172 * Return -EIO on error, 0 on success
173 */
Simon Glasscd8cc672015-08-22 18:31:38 -0600174static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
175 size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +0000176{
Simon Glasscd8cc672015-08-22 18:31:38 -0600177 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
178 SLEEP_DURATION_US, MAX_COUNT);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000184 */
Simon Glasscd8cc672015-08-22 18:31:38 -0600185static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
186 size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +0000187{
Simon Glasscd8cc672015-08-22 18:31:38 -0600188 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
Simon Glassa57bcee2015-08-22 18:31:30 -0600189 SLEEP_DURATION_LONG_US,
190 MAX_COUNT_LONG);
Rong Chang8faa9452013-04-12 10:44:57 +0000191}
192
Simon Glasscd8cc672015-08-22 18:31:38 -0600193static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
Rong Chang8faa9452013-04-12 10:44:57 +0000194{
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000195 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Simon Glasscd8cc672015-08-22 18:31:38 -0600196 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000197 u8 buf;
198 int rc;
199
Simon Glasscd8cc672015-08-22 18:31:38 -0600200 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
Rong Chang8faa9452013-04-12 10:44:57 +0000201 if (rc < 0)
202 return rc;
203
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000204 if ((buf & mask) == mask) {
Simon Glassab79b002015-08-22 18:31:24 -0600205 chip->locality = loc;
Rong Chang8faa9452013-04-12 10:44:57 +0000206 return loc;
207 }
208
Simon Glasscd8cc672015-08-22 18:31:38 -0600209 return -ENOENT;
Rong Chang8faa9452013-04-12 10:44:57 +0000210}
211
Simon Glasscd8cc672015-08-22 18:31:38 -0600212static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
Simon Glass7ccebaf2015-08-22 18:31:29 -0600213 int force)
Rong Chang8faa9452013-04-12 10:44:57 +0000214{
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000215 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Chang8faa9452013-04-12 10:44:57 +0000216 u8 buf;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000217
Simon Glasscd8cc672015-08-22 18:31:38 -0600218 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
Rong Chang8faa9452013-04-12 10:44:57 +0000219 return;
220
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000221 if (force || (buf & mask) == mask) {
Rong Chang8faa9452013-04-12 10:44:57 +0000222 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Simon Glasscd8cc672015-08-22 18:31:38 -0600223 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
Rong Chang8faa9452013-04-12 10:44:57 +0000224 }
225}
226
Simon Glasscd8cc672015-08-22 18:31:38 -0600227static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
Rong Chang8faa9452013-04-12 10:44:57 +0000228{
Simon Glasscd8cc672015-08-22 18:31:38 -0600229 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000230 unsigned long start, stop;
231 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassc9a3e842015-05-04 11:30:59 -0600232 int rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000233
Simon Glasscd8cc672015-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 Tame49fed52013-04-12 11:04:37 +0000237 return loc; /* We already have the locality */
Simon Glasscd8cc672015-08-22 18:31:38 -0600238 } else if (rc != -ENOENT) {
239 debug("%s: Failed to get locality: %d\n", __func__, rc);
240 return rc;
241 }
Rong Chang8faa9452013-04-12 10:44:57 +0000242
Simon Glasscd8cc672015-08-22 18:31:38 -0600243 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);
Simon Glassc9a3e842015-05-04 11:30:59 -0600246 return rc;
Simon Glasscd8cc672015-08-22 18:31:38 -0600247 }
Rong Chang8faa9452013-04-12 10:44:57 +0000248
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000249 /* Wait for burstcount */
Rong Chang8faa9452013-04-12 10:44:57 +0000250 start = get_timer(0);
Simon Glassab79b002015-08-22 18:31:24 -0600251 stop = chip->timeout_a;
Rong Chang8faa9452013-04-12 10:44:57 +0000252 do {
Simon Glasscd8cc672015-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 Chang8faa9452013-04-12 10:44:57 +0000256 return loc;
Simon Glasscd8cc672015-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 Glassa57bcee2015-08-22 18:31:30 -0600261 mdelay(TPM_TIMEOUT_MS);
Rong Chang8faa9452013-04-12 10:44:57 +0000262 } while (get_timer(start) < stop);
Simon Glasscd8cc672015-08-22 18:31:38 -0600263 debug("%s: Timeout getting locality: %d\n", __func__, rc);
Rong Chang8faa9452013-04-12 10:44:57 +0000264
Simon Glasscd8cc672015-08-22 18:31:38 -0600265 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000266}
267
Simon Glasscd8cc672015-08-22 18:31:38 -0600268static u8 tpm_tis_i2c_status(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000269{
Simon Glasscd8cc672015-08-22 18:31:38 -0600270 struct tpm_chip *chip = dev_get_priv(dev);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000271 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Chang8faa9452013-04-12 10:44:57 +0000272 u8 buf;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000273
Simon Glasscd8cc672015-08-22 18:31:38 -0600274 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
Rong Chang8faa9452013-04-12 10:44:57 +0000275 return 0;
276 else
277 return buf;
278}
279
Simon Glasscd8cc672015-08-22 18:31:38 -0600280static int tpm_tis_i2c_ready(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000281{
Simon Glasscd8cc672015-08-22 18:31:38 -0600282 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glassc9a3e842015-05-04 11:30:59 -0600283 int rc;
284
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000285 /* This causes the current command to be aborted */
Rong Chang8faa9452013-04-12 10:44:57 +0000286 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000287
Simon Glassc9a3e842015-05-04 11:30:59 -0600288 debug("%s\n", __func__);
Simon Glasscd8cc672015-08-22 18:31:38 -0600289 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
Simon Glassc9a3e842015-05-04 11:30:59 -0600290 if (rc)
291 debug("%s: rc=%d\n", __func__, rc);
Simon Glasscd8cc672015-08-22 18:31:38 -0600292
293 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000294}
295
Simon Glasscd8cc672015-08-22 18:31:38 -0600296static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000297{
Simon Glasscd8cc672015-08-22 18:31:38 -0600298 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000299 unsigned long start, stop;
300 ssize_t burstcnt;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000301 u8 addr, buf[3];
Rong Chang8faa9452013-04-12 10:44:57 +0000302
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000303 /* Wait for burstcount */
304 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Chang8faa9452013-04-12 10:44:57 +0000305 start = get_timer(0);
Simon Glassab79b002015-08-22 18:31:24 -0600306 stop = chip->timeout_d;
Rong Chang8faa9452013-04-12 10:44:57 +0000307 do {
308 /* Note: STS is little endian */
Simon Glassab79b002015-08-22 18:31:24 -0600309 addr = TPM_STS(chip->locality) + 1;
Simon Glasscd8cc672015-08-22 18:31:38 -0600310 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
Rong Chang8faa9452013-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 Glassa57bcee2015-08-22 18:31:30 -0600317 mdelay(TPM_TIMEOUT_MS);
Rong Chang8faa9452013-04-12 10:44:57 +0000318 } while (get_timer(start) < stop);
319
320 return -EBUSY;
321}
322
Simon Glasscd8cc672015-08-22 18:31:38 -0600323static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
Simon Glass7ccebaf2015-08-22 18:31:29 -0600324 unsigned long timeout, int *status)
Rong Chang8faa9452013-04-12 10:44:57 +0000325{
326 unsigned long start, stop;
327
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000328 /* Check current status */
Simon Glasscd8cc672015-08-22 18:31:38 -0600329 *status = tpm_tis_i2c_status(dev);
Rong Chang8faa9452013-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 Glassa57bcee2015-08-22 18:31:30 -0600336 mdelay(TPM_TIMEOUT_MS);
Simon Glasscd8cc672015-08-22 18:31:38 -0600337 *status = tpm_tis_i2c_status(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000338 if ((*status & mask) == mask)
339 return 0;
Rong Chang8faa9452013-04-12 10:44:57 +0000340 } while (get_timer(start) < stop);
341
Simon Glasscd8cc672015-08-22 18:31:38 -0600342 return -ETIMEDOUT;
Rong Chang8faa9452013-04-12 10:44:57 +0000343}
344
Simon Glasscd8cc672015-08-22 18:31:38 -0600345static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
Rong Chang8faa9452013-04-12 10:44:57 +0000346{
Simon Glasscd8cc672015-08-22 18:31:38 -0600347 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000348 size_t size = 0;
349 ssize_t burstcnt;
350 int rc;
351
352 while (size < count) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600353 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000354
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000355 /* burstcount < 0 -> tpm is busy */
Rong Chang8faa9452013-04-12 10:44:57 +0000356 if (burstcnt < 0)
357 return burstcnt;
358
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000359 /* Limit received data to max left */
Rong Chang8faa9452013-04-12 10:44:57 +0000360 if (burstcnt > (count - size))
361 burstcnt = count - size;
362
Simon Glasscd8cc672015-08-22 18:31:38 -0600363 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
364 &(buf[size]), burstcnt);
Rong Chang8faa9452013-04-12 10:44:57 +0000365 if (rc == 0)
366 size += burstcnt;
367 }
368
369 return size;
370}
371
Simon Glasscd8cc672015-08-22 18:31:38 -0600372static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
Rong Chang8faa9452013-04-12 10:44:57 +0000373{
Simon Glasscd8cc672015-08-22 18:31:38 -0600374 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000375 int size = 0;
Jeremy Boone4dc0eca2018-02-12 17:56:36 -0500376 int status;
377 unsigned int expected;
Simon Glasscd8cc672015-08-22 18:31:38 -0600378 int rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000379
Simon Glasscd8cc672015-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 Chang8faa9452013-04-12 10:44:57 +0000388
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000389 /* Read first 10 bytes, including tag, paramsize, and result */
Simon Glasscd8cc672015-08-22 18:31:38 -0600390 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
Rong Chang8faa9452013-04-12 10:44:57 +0000391 if (size < TPM_HEADER_SIZE) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600392 debug("Unable to read header\n");
393 return size < 0 ? size : -EIO;
Rong Chang8faa9452013-04-12 10:44:57 +0000394 }
395
396 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
Jeremy Boone4dc0eca2018-02-12 17:56:36 -0500397 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600398 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
Simon Glassc9a3e842015-05-04 11:30:59 -0600399 count);
Simon Glasscd8cc672015-08-22 18:31:38 -0600400 return -ENOSPC;
Rong Chang8faa9452013-04-12 10:44:57 +0000401 }
402
Simon Glasscd8cc672015-08-22 18:31:38 -0600403 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
Simon Glass7ccebaf2015-08-22 18:31:29 -0600404 expected - TPM_HEADER_SIZE);
Rong Chang8faa9452013-04-12 10:44:57 +0000405 if (size < expected) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600406 debug("Unable to read remainder of result\n");
407 return -ETIMEDOUT;
Rong Chang8faa9452013-04-12 10:44:57 +0000408 }
409
Simon Glasscd8cc672015-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 Tame49fed52013-04-12 11:04:37 +0000414 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
Simon Glasscd8cc672015-08-22 18:31:38 -0600415 debug("Error left over data\n");
416 return -EIO;
Rong Chang8faa9452013-04-12 10:44:57 +0000417 }
418
Rong Chang8faa9452013-04-12 10:44:57 +0000419 return size;
420}
421
Simon Glasscd8cc672015-08-22 18:31:38 -0600422static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +0000423{
Simon Glasscd8cc672015-08-22 18:31:38 -0600424 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000425 int rc, status;
Simon Glassc9a3e842015-05-04 11:30:59 -0600426 size_t burstcnt;
Rong Chang8faa9452013-04-12 10:44:57 +0000427 size_t count = 0;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000428 int retry = 0;
Rong Chang8faa9452013-04-12 10:44:57 +0000429 u8 sts = TPM_STS_GO;
430
Simon Glassc9a3e842015-05-04 11:30:59 -0600431 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000432 if (len > TPM_DEV_BUFSIZE)
433 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Chang8faa9452013-04-12 10:44:57 +0000434
Simon Glasscd8cc672015-08-22 18:31:38 -0600435 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
Rong Chang8faa9452013-04-12 10:44:57 +0000436 return -EBUSY;
437
Simon Glasscd8cc672015-08-22 18:31:38 -0600438 status = tpm_tis_i2c_status(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000439 if ((status & TPM_STS_COMMAND_READY) == 0) {
Simon Glasscd8cc672015-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 Chang8faa9452013-04-12 10:44:57 +0000447 }
448
Simon Glasscd8cc672015-08-22 18:31:38 -0600449 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000450
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000451 /* burstcount < 0 -> tpm is busy */
452 if (burstcnt < 0)
453 return burstcnt;
Rong Chang8faa9452013-04-12 10:44:57 +0000454
Simon Glassc9a3e842015-05-04 11:30:59 -0600455 while (count < len) {
456 udelay(300);
457 if (burstcnt > len - count)
458 burstcnt = len - count;
Rong Chang8faa9452013-04-12 10:44:57 +0000459
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000460#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
Simon Glasscd8cc672015-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 Tame49fed52013-04-12 11:04:37 +0000463#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
Rong Chang8faa9452013-04-12 10:44:57 +0000464
Simon Glasscd8cc672015-08-22 18:31:38 -0600465 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
466 &(buf[count]), burstcnt);
Rong Chang8faa9452013-04-12 10:44:57 +0000467 if (rc == 0)
468 count += burstcnt;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000469 else {
Simon Glassc9a3e842015-05-04 11:30:59 -0600470 debug("%s: error\n", __func__);
Simon Glasscd8cc672015-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 Glass7ccebaf2015-08-22 18:31:29 -0600474 chip->timeout_c,
475 &status);
Simon Glassc9a3e842015-05-04 11:30:59 -0600476 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -0600477 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000478
Simon Glasscd8cc672015-08-22 18:31:38 -0600479 if ((status & TPM_STS_DATA_EXPECT) == 0)
480 return -EIO;
Rong Chang8faa9452013-04-12 10:44:57 +0000481 }
482 }
483
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000484 /* Go and do it */
Simon Glasscd8cc672015-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 Chang8faa9452013-04-12 10:44:57 +0000489
490 return len;
Simon Glasscd8cc672015-08-22 18:31:38 -0600491}
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000492
Simon Glasscd8cc672015-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 Tame49fed52013-04-12 11:04:37 +0000498 /*
499 * The TPM needs some time to clean up here,
Rong Chang8faa9452013-04-12 10:44:57 +0000500 * so we sleep rather than keeping the bus busy
501 */
Simon Glassa57bcee2015-08-22 18:31:30 -0600502 mdelay(2);
Simon Glasscd8cc672015-08-22 18:31:38 -0600503 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000504
Simon Glasscd8cc672015-08-22 18:31:38 -0600505 return 0;
Vincent Palatin06883eb2013-04-12 11:04:36 +0000506}
507
Simon Glass1de4d582015-08-22 18:31:25 -0600508static int tpm_tis_i2c_init(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000509{
Simon Glasscd8cc672015-08-22 18:31:38 -0600510 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000511 u32 vendor;
Vincent Palatin06883eb2013-04-12 11:04:36 +0000512 u32 expected_did_vid;
Simon Glasscd8cc672015-08-22 18:31:38 -0600513 int rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000514
Simon Glassab79b002015-08-22 18:31:24 -0600515 chip->is_open = 1;
Rong Chang8faa9452013-04-12 10:44:57 +0000516
Simon Glass7ccebaf2015-08-22 18:31:29 -0600517 /* Default timeouts - these could move to the device tree */
Simon Glassa57bcee2015-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 Chang8faa9452013-04-12 10:44:57 +0000522
Simon Glasscd8cc672015-08-22 18:31:38 -0600523 rc = tpm_tis_i2c_request_locality(dev, 0);
524 if (rc < 0)
525 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000526
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000527 /* Read four bytes from DID_VID register */
Simon Glasscd8cc672015-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 Glassc9a3e842015-05-04 11:30:59 -0600530 return -EIO;
Rong Chang8faa9452013-04-12 10:44:57 +0000531 }
532
Simon Glasscd8cc672015-08-22 18:31:38 -0600533 if (chip->chip_type == SLB9635) {
Vincent Palatin06883eb2013-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 Chang8faa9452013-04-12 10:44:57 +0000540
Simon Glasscd8cc672015-08-22 18:31:38 -0600541 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900542 pr_err("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassc9a3e842015-05-04 11:30:59 -0600543 return -ENODEV;
Rong Chang8faa9452013-04-12 10:44:57 +0000544 }
545
Simon Glasscd8cc672015-08-22 18:31:38 -0600546 chip->vend_dev = vendor;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000547 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
Simon Glasscd8cc672015-08-22 18:31:38 -0600548 chip_name[chip->chip_type], vendor >> 16);
Rong Chang8faa9452013-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 Glassc9a3e842015-05-04 11:30:59 -0600556}
Rong Chang8faa9452013-04-12 10:44:57 +0000557
Simon Glasscd8cc672015-08-22 18:31:38 -0600558static int tpm_tis_i2c_open(struct udevice *dev)
Simon Glass94576272015-08-22 18:31:22 -0600559{
Simon Glasscd8cc672015-08-22 18:31:38 -0600560 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass94576272015-08-22 18:31:22 -0600561 int rc;
Simon Glass94576272015-08-22 18:31:22 -0600562
Simon Glasscd8cc672015-08-22 18:31:38 -0600563 debug("%s: start\n", __func__);
564 if (chip->is_open)
565 return -EBUSY;
566 rc = tpm_tis_i2c_init(dev);
Simon Glass94576272015-08-22 18:31:22 -0600567 if (rc < 0)
Simon Glasscd8cc672015-08-22 18:31:38 -0600568 chip->is_open = 0;
Simon Glass94576272015-08-22 18:31:22 -0600569
Simon Glass94576272015-08-22 18:31:22 -0600570 return rc;
571}
572
Simon Glasscd8cc672015-08-22 18:31:38 -0600573static int tpm_tis_i2c_close(struct udevice *dev)
Simon Glass94576272015-08-22 18:31:22 -0600574{
Simon Glasscd8cc672015-08-22 18:31:38 -0600575 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass94576272015-08-22 18:31:22 -0600576
Simon Glasscd8cc672015-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 Glass94576272015-08-22 18:31:22 -0600581 }
582
Simon Glass94576272015-08-22 18:31:22 -0600583 return 0;
584}
585
Simon Glasscd8cc672015-08-22 18:31:38 -0600586static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
Simon Glass94576272015-08-22 18:31:22 -0600587{
Simon Glasscd8cc672015-08-22 18:31:38 -0600588 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass94576272015-08-22 18:31:22 -0600589
Simon Glasscd8cc672015-08-22 18:31:38 -0600590 if (size < 50)
591 return -ENOSPC;
Simon Glass94576272015-08-22 18:31:22 -0600592
Simon Glasscd8cc672015-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);
Simon Glass94576272015-08-22 18:31:22 -0600597}
598
Simon Glasscd8cc672015-08-22 18:31:38 -0600599static int tpm_tis_i2c_probe(struct udevice *dev)
Simon Glass94576272015-08-22 18:31:22 -0600600{
Simon Glasscd8cc672015-08-22 18:31:38 -0600601 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
602 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass94576272015-08-22 18:31:22 -0600603
Simon Glasscd8cc672015-08-22 18:31:38 -0600604 chip->chip_type = dev_get_driver_data(dev);
Simon Glass94576272015-08-22 18:31:22 -0600605
Simon Glasscd8cc672015-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 Glass94576272015-08-22 18:31:22 -0600611
612 return 0;
613}
614
Simon Glasscd8cc672015-08-22 18:31:38 -0600615static 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};
Simon Glass94576272015-08-22 18:31:22 -0600623
Simon Glasscd8cc672015-08-22 18:31:38 -0600624static const struct udevice_id tpm_tis_i2c_ids[] = {
625 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
626 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
627 { }
628};
Simon Glass94576272015-08-22 18:31:22 -0600629
Simon Glasscd8cc672015-08-22 18:31:38 -0600630U_BOOT_DRIVER(tpm_tis_i2c) = {
Christophe Ricard8759ff82015-10-06 22:54:41 +0200631 .name = "tpm_tis_infineon",
Simon Glasscd8cc672015-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,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700636 .priv_auto = sizeof(struct tpm_chip),
Simon Glasscd8cc672015-08-22 18:31:38 -0600637};