blob: f57c32837be3a0cc068b93fc8af2e490323b839c [file] [log] [blame]
Rong Changf6267992013-04-12 10:44:57 +00001/*
2 * Copyright (C) 2011 Infineon Technologies
3 *
4 * Authors:
5 * Peter Huewe <huewe.external@infineon.com>
6 *
7 * Description:
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
10 *
11 * This device driver implements the TPM interface as defined in
12 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
13 * Infineon I2C Protocol Stack Specification v0.20.
14 *
15 * It is based on the Linux kernel driver tpm.c from Leendert van
16 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
17 *
18 * Version: 2.1.1
19 *
Simon Glass07470d62015-08-22 18:31:21 -060020 * SPDX-License-Identifier: GPL-2.0
Rong Changf6267992013-04-12 10:44:57 +000021 */
22
23#include <common.h>
Simon Glassf90acf12015-05-04 11:30:59 -060024#include <dm.h>
Vincent Palatinec34fa52013-04-12 11:04:36 +000025#include <fdtdec.h>
Rong Changf6267992013-04-12 10:44:57 +000026#include <i2c.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000027#include <tpm.h>
28#include <asm-generic/errno.h>
Simon Glassb697e0f2015-08-22 18:31:38 -060029#include <linux/compiler.h>
Rong Changf6267992013-04-12 10:44:57 +000030#include <linux/types.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000031#include <linux/unaligned/be_byteshift.h>
Rong Changf6267992013-04-12 10:44:57 +000032
Christophe Ricard0766ad22015-10-06 22:54:41 +020033#include "tpm_tis_infineon.h"
Simon Glassf255d312015-08-22 18:31:31 -060034#include "tpm_internal.h"
Rong Changf6267992013-04-12 10:44:57 +000035
Vincent Palatinec34fa52013-04-12 11:04:36 +000036DECLARE_GLOBAL_DATA_PTR;
37
Vincent Palatinec34fa52013-04-12 11:04:36 +000038static const char * const chip_name[] = {
39 [SLB9635] = "slb9635tt",
40 [SLB9645] = "slb9645tt",
41 [UNKNOWN] = "unknown/fallback to slb9635",
42};
Rong Changf6267992013-04-12 10:44:57 +000043
Rong Changf6267992013-04-12 10:44:57 +000044/*
Simon Glasse56e20e2015-08-22 18:31:29 -060045 * tpm_tis_i2c_read() - read from TPM register
Rong Changf6267992013-04-12 10:44:57 +000046 * @addr: register address to read from
47 * @buffer: provided by caller
48 * @len: number of bytes to read
49 *
50 * Read len bytes from TPM register and put them into
51 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
52 *
53 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
54 * values have to be swapped.
55 *
56 * Return -EIO on error, 0 on success.
57 */
Simon Glassb697e0f2015-08-22 18:31:38 -060058static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
59 size_t len)
Rong Changf6267992013-04-12 10:44:57 +000060{
Simon Glassb697e0f2015-08-22 18:31:38 -060061 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +000062 int rc;
63 int count;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000064 uint32_t addrbuf = addr;
Rong Changf6267992013-04-12 10:44:57 +000065
Simon Glassb697e0f2015-08-22 18:31:38 -060066 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
Vincent Palatinec34fa52013-04-12 11:04:36 +000067 /* slb9635 protocol should work in both cases */
68 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -060069 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatinec34fa52013-04-12 11:04:36 +000070 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000071 break; /* Success, break to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -060072 udelay(SLEEP_DURATION_US);
Vincent Palatinec34fa52013-04-12 11:04:36 +000073 }
Vincent Palatinec34fa52013-04-12 11:04:36 +000074 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -060075 return rc;
Vincent Palatinec34fa52013-04-12 11:04:36 +000076
77 /* After the TPM has successfully received the register address
78 * it needs some time, thus we're sleeping here again, before
79 * retrieving the data
80 */
81 for (count = 0; count < MAX_COUNT; count++) {
Simon Glass42c8ec52015-08-22 18:31:30 -060082 udelay(SLEEP_DURATION_US);
Simon Glassb697e0f2015-08-22 18:31:38 -060083 rc = dm_i2c_read(dev, 0, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +000084 if (rc == 0)
85 break; /* success, break to skip sleep */
86 }
87 } else {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000088 /*
89 * Use a combined read for newer chips.
90 * Unfortunately the smbus functions are not suitable due to
Vincent Palatinec34fa52013-04-12 11:04:36 +000091 * the 32 byte limit of the smbus.
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000092 * Retries should usually not be needed, but are kept just to
Vincent Palatinec34fa52013-04-12 11:04:36 +000093 * be safe on the safe side.
94 */
95 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -060096 rc = dm_i2c_read(dev, addr, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +000097 if (rc == 0)
98 break; /* break here to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -060099 udelay(SLEEP_DURATION_US);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000100 }
Rong Changf6267992013-04-12 10:44:57 +0000101 }
102
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000103 /* Take care of 'guard time' */
Simon Glass42c8ec52015-08-22 18:31:30 -0600104 udelay(SLEEP_DURATION_US);
Rong Changf6267992013-04-12 10:44:57 +0000105 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600106 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000107
108 return 0;
109}
110
Simon Glassb697e0f2015-08-22 18:31:38 -0600111static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
112 const u8 *buffer, size_t len,
Simon Glass42c8ec52015-08-22 18:31:30 -0600113 unsigned int sleep_time_us, u8 max_count)
Rong Changf6267992013-04-12 10:44:57 +0000114{
Simon Glassb697e0f2015-08-22 18:31:38 -0600115 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
116 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000117 int rc = 0;
118 int count;
119
Simon Glassb697e0f2015-08-22 18:31:38 -0600120 if (chip->chip_type == SLB9635) {
121 /* Prepare send buffer to include the address */
122 priv->buf[0] = addr;
123 memcpy(&(priv->buf[1]), buffer, len);
124 buffer = priv->buf;
125 len++;
126 addr = 0;
127 }
128
Rong Changf6267992013-04-12 10:44:57 +0000129 for (count = 0; count < max_count; count++) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600130 rc = dm_i2c_write(dev, addr, buffer, len);
Rong Changf6267992013-04-12 10:44:57 +0000131 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000132 break; /* Success, break to skip sleep */
Simon Glass42c8ec52015-08-22 18:31:30 -0600133 udelay(sleep_time_us);
Rong Changf6267992013-04-12 10:44:57 +0000134 }
135
Vincent Palatinec34fa52013-04-12 11:04:36 +0000136 /* take care of 'guard time' */
Simon Glass42c8ec52015-08-22 18:31:30 -0600137 udelay(sleep_time_us);
Rong Changf6267992013-04-12 10:44:57 +0000138 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600139 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000140
141 return 0;
142}
143
144/*
Simon Glasse56e20e2015-08-22 18:31:29 -0600145 * tpm_tis_i2c_write() - write to TPM register
Rong Changf6267992013-04-12 10:44:57 +0000146 * @addr: register address to write to
147 * @buffer: containing data to be written
148 * @len: number of bytes to write
149 *
150 * Write len bytes from provided buffer to TPM register (little
151 * endian format, i.e. buffer[0] is written as first byte).
152 *
153 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
154 * values have to be swapped.
155 *
Simon Glasse56e20e2015-08-22 18:31:29 -0600156 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
Rong Changf6267992013-04-12 10:44:57 +0000157 *
158 * Return -EIO on error, 0 on success
159 */
Simon Glassb697e0f2015-08-22 18:31:38 -0600160static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
161 size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000162{
Simon Glassb697e0f2015-08-22 18:31:38 -0600163 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
164 SLEEP_DURATION_US, MAX_COUNT);
Rong Changf6267992013-04-12 10:44:57 +0000165}
166
167/*
168 * This function is needed especially for the cleanup situation after
169 * sending TPM_READY
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000170 */
Simon Glassb697e0f2015-08-22 18:31:38 -0600171static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
172 size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000173{
Simon Glassb697e0f2015-08-22 18:31:38 -0600174 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
Simon Glass42c8ec52015-08-22 18:31:30 -0600175 SLEEP_DURATION_LONG_US,
176 MAX_COUNT_LONG);
Rong Changf6267992013-04-12 10:44:57 +0000177}
178
Simon Glassb697e0f2015-08-22 18:31:38 -0600179static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
Rong Changf6267992013-04-12 10:44:57 +0000180{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000181 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Simon Glassb697e0f2015-08-22 18:31:38 -0600182 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000183 u8 buf;
184 int rc;
185
Simon Glassb697e0f2015-08-22 18:31:38 -0600186 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
Rong Changf6267992013-04-12 10:44:57 +0000187 if (rc < 0)
188 return rc;
189
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000190 if ((buf & mask) == mask) {
Simon Glass7c735372015-08-22 18:31:24 -0600191 chip->locality = loc;
Rong Changf6267992013-04-12 10:44:57 +0000192 return loc;
193 }
194
Simon Glassb697e0f2015-08-22 18:31:38 -0600195 return -ENOENT;
Rong Changf6267992013-04-12 10:44:57 +0000196}
197
Simon Glassb697e0f2015-08-22 18:31:38 -0600198static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
Simon Glasse56e20e2015-08-22 18:31:29 -0600199 int force)
Rong Changf6267992013-04-12 10:44:57 +0000200{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000201 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000202 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000203
Simon Glassb697e0f2015-08-22 18:31:38 -0600204 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000205 return;
206
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000207 if (force || (buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000208 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Simon Glassb697e0f2015-08-22 18:31:38 -0600209 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
Rong Changf6267992013-04-12 10:44:57 +0000210 }
211}
212
Simon Glassb697e0f2015-08-22 18:31:38 -0600213static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
Rong Changf6267992013-04-12 10:44:57 +0000214{
Simon Glassb697e0f2015-08-22 18:31:38 -0600215 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000216 unsigned long start, stop;
217 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassf90acf12015-05-04 11:30:59 -0600218 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000219
Simon Glassb697e0f2015-08-22 18:31:38 -0600220 rc = tpm_tis_i2c_check_locality(dev, loc);
221 if (rc >= 0) {
222 debug("%s: Already have locality\n", __func__);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000223 return loc; /* We already have the locality */
Simon Glassb697e0f2015-08-22 18:31:38 -0600224 } else if (rc != -ENOENT) {
225 debug("%s: Failed to get locality: %d\n", __func__, rc);
Simon Glassf90acf12015-05-04 11:30:59 -0600226 return rc;
Simon Glassb697e0f2015-08-22 18:31:38 -0600227 }
228
229 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
230 if (rc) {
231 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
232 return rc;
233 }
Rong Changf6267992013-04-12 10:44:57 +0000234
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000235 /* Wait for burstcount */
Rong Changf6267992013-04-12 10:44:57 +0000236 start = get_timer(0);
Simon Glass7c735372015-08-22 18:31:24 -0600237 stop = chip->timeout_a;
Rong Changf6267992013-04-12 10:44:57 +0000238 do {
Simon Glassb697e0f2015-08-22 18:31:38 -0600239 rc = tpm_tis_i2c_check_locality(dev, loc);
240 if (rc >= 0) {
241 debug("%s: Have locality\n", __func__);
Rong Changf6267992013-04-12 10:44:57 +0000242 return loc;
Simon Glassb697e0f2015-08-22 18:31:38 -0600243 } else if (rc != -ENOENT) {
244 debug("%s: Failed to get locality: %d\n", __func__, rc);
245 return rc;
246 }
Simon Glass42c8ec52015-08-22 18:31:30 -0600247 mdelay(TPM_TIMEOUT_MS);
Rong Changf6267992013-04-12 10:44:57 +0000248 } while (get_timer(start) < stop);
Simon Glassb697e0f2015-08-22 18:31:38 -0600249 debug("%s: Timeout getting locality: %d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000250
Simon Glassb697e0f2015-08-22 18:31:38 -0600251 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000252}
253
Simon Glassb697e0f2015-08-22 18:31:38 -0600254static u8 tpm_tis_i2c_status(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000255{
Simon Glassb697e0f2015-08-22 18:31:38 -0600256 struct tpm_chip *chip = dev_get_priv(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000257 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Changf6267992013-04-12 10:44:57 +0000258 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000259
Simon Glassb697e0f2015-08-22 18:31:38 -0600260 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000261 return 0;
262 else
263 return buf;
264}
265
Simon Glassb697e0f2015-08-22 18:31:38 -0600266static int tpm_tis_i2c_ready(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);
Simon Glassf90acf12015-05-04 11:30:59 -0600269 int rc;
270
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000271 /* This causes the current command to be aborted */
Rong Changf6267992013-04-12 10:44:57 +0000272 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000273
Simon Glassf90acf12015-05-04 11:30:59 -0600274 debug("%s\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600275 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600276 if (rc)
277 debug("%s: rc=%d\n", __func__, rc);
Simon Glassb697e0f2015-08-22 18:31:38 -0600278
279 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000280}
281
Simon Glassb697e0f2015-08-22 18:31:38 -0600282static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000283{
Simon Glassb697e0f2015-08-22 18:31:38 -0600284 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000285 unsigned long start, stop;
286 ssize_t burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000287 u8 addr, buf[3];
Rong Changf6267992013-04-12 10:44:57 +0000288
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000289 /* Wait for burstcount */
290 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Changf6267992013-04-12 10:44:57 +0000291 start = get_timer(0);
Simon Glass7c735372015-08-22 18:31:24 -0600292 stop = chip->timeout_d;
Rong Changf6267992013-04-12 10:44:57 +0000293 do {
294 /* Note: STS is little endian */
Simon Glass7c735372015-08-22 18:31:24 -0600295 addr = TPM_STS(chip->locality) + 1;
Simon Glassb697e0f2015-08-22 18:31:38 -0600296 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000297 burstcnt = 0;
298 else
299 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
300
301 if (burstcnt)
302 return burstcnt;
Simon Glass42c8ec52015-08-22 18:31:30 -0600303 mdelay(TPM_TIMEOUT_MS);
Rong Changf6267992013-04-12 10:44:57 +0000304 } while (get_timer(start) < stop);
305
306 return -EBUSY;
307}
308
Simon Glassb697e0f2015-08-22 18:31:38 -0600309static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
Simon Glasse56e20e2015-08-22 18:31:29 -0600310 unsigned long timeout, int *status)
Rong Changf6267992013-04-12 10:44:57 +0000311{
312 unsigned long start, stop;
313
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000314 /* Check current status */
Simon Glassb697e0f2015-08-22 18:31:38 -0600315 *status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000316 if ((*status & mask) == mask)
317 return 0;
318
319 start = get_timer(0);
320 stop = timeout;
321 do {
Simon Glass42c8ec52015-08-22 18:31:30 -0600322 mdelay(TPM_TIMEOUT_MS);
Simon Glassb697e0f2015-08-22 18:31:38 -0600323 *status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000324 if ((*status & mask) == mask)
325 return 0;
Rong Changf6267992013-04-12 10:44:57 +0000326 } while (get_timer(start) < stop);
327
Simon Glassb697e0f2015-08-22 18:31:38 -0600328 return -ETIMEDOUT;
Rong Changf6267992013-04-12 10:44:57 +0000329}
330
Simon Glassb697e0f2015-08-22 18:31:38 -0600331static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
Rong Changf6267992013-04-12 10:44:57 +0000332{
Simon Glassb697e0f2015-08-22 18:31:38 -0600333 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000334 size_t size = 0;
335 ssize_t burstcnt;
336 int rc;
337
338 while (size < count) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600339 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Rong Changf6267992013-04-12 10:44:57 +0000340
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000341 /* burstcount < 0 -> tpm is busy */
Rong Changf6267992013-04-12 10:44:57 +0000342 if (burstcnt < 0)
343 return burstcnt;
344
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000345 /* Limit received data to max left */
Rong Changf6267992013-04-12 10:44:57 +0000346 if (burstcnt > (count - size))
347 burstcnt = count - size;
348
Simon Glassb697e0f2015-08-22 18:31:38 -0600349 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
350 &(buf[size]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000351 if (rc == 0)
352 size += burstcnt;
353 }
354
355 return size;
356}
357
Simon Glassb697e0f2015-08-22 18:31:38 -0600358static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
Rong Changf6267992013-04-12 10:44:57 +0000359{
Simon Glassb697e0f2015-08-22 18:31:38 -0600360 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000361 int size = 0;
362 int expected, status;
Simon Glassb697e0f2015-08-22 18:31:38 -0600363 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000364
Simon Glassb697e0f2015-08-22 18:31:38 -0600365 status = tpm_tis_i2c_status(dev);
366 if (status == TPM_STS_COMMAND_READY)
367 return -EINTR;
368 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
369 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
370 return -EAGAIN;
371
372 debug("...got it;\n");
Rong Changf6267992013-04-12 10:44:57 +0000373
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000374 /* Read first 10 bytes, including tag, paramsize, and result */
Simon Glassb697e0f2015-08-22 18:31:38 -0600375 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000376 if (size < TPM_HEADER_SIZE) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600377 debug("Unable to read header\n");
378 return size < 0 ? size : -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000379 }
380
381 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
382 if ((size_t)expected > count) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600383 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
Simon Glassf90acf12015-05-04 11:30:59 -0600384 count);
Simon Glassb697e0f2015-08-22 18:31:38 -0600385 return -ENOSPC;
Rong Changf6267992013-04-12 10:44:57 +0000386 }
387
Simon Glassb697e0f2015-08-22 18:31:38 -0600388 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
Simon Glasse56e20e2015-08-22 18:31:29 -0600389 expected - TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000390 if (size < expected) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600391 debug("Unable to read remainder of result\n");
392 return -ETIMEDOUT;
Rong Changf6267992013-04-12 10:44:57 +0000393 }
394
Simon Glassb697e0f2015-08-22 18:31:38 -0600395 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
396 &status);
397 if (rc)
398 return rc;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000399 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
Simon Glassb697e0f2015-08-22 18:31:38 -0600400 debug("Error left over data\n");
401 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000402 }
403
Rong Changf6267992013-04-12 10:44:57 +0000404 return size;
405}
406
Simon Glassb697e0f2015-08-22 18:31:38 -0600407static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000408{
Simon Glassb697e0f2015-08-22 18:31:38 -0600409 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000410 int rc, status;
Simon Glassf90acf12015-05-04 11:30:59 -0600411 size_t burstcnt;
Rong Changf6267992013-04-12 10:44:57 +0000412 size_t count = 0;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000413 int retry = 0;
Rong Changf6267992013-04-12 10:44:57 +0000414 u8 sts = TPM_STS_GO;
415
Simon Glassf90acf12015-05-04 11:30:59 -0600416 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000417 if (len > TPM_DEV_BUFSIZE)
418 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Changf6267992013-04-12 10:44:57 +0000419
Simon Glassb697e0f2015-08-22 18:31:38 -0600420 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000421 return -EBUSY;
422
Simon Glassb697e0f2015-08-22 18:31:38 -0600423 status = tpm_tis_i2c_status(dev);
Rong Changf6267992013-04-12 10:44:57 +0000424 if ((status & TPM_STS_COMMAND_READY) == 0) {
Simon Glassb697e0f2015-08-22 18:31:38 -0600425 rc = tpm_tis_i2c_ready(dev);
426 if (rc)
427 return rc;
428 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
429 chip->timeout_b, &status);
430 if (rc)
431 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000432 }
433
Simon Glassb697e0f2015-08-22 18:31:38 -0600434 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000435
436 /* burstcount < 0 -> tpm is busy */
437 if (burstcnt < 0)
438 return burstcnt;
439
Simon Glassf90acf12015-05-04 11:30:59 -0600440 while (count < len) {
441 udelay(300);
442 if (burstcnt > len - count)
443 burstcnt = len - count;
Rong Changf6267992013-04-12 10:44:57 +0000444
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000445#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
Simon Glassb697e0f2015-08-22 18:31:38 -0600446 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
447 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000448#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
Rong Changf6267992013-04-12 10:44:57 +0000449
Simon Glassb697e0f2015-08-22 18:31:38 -0600450 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
451 &(buf[count]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000452 if (rc == 0)
453 count += burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000454 else {
Simon Glassf90acf12015-05-04 11:30:59 -0600455 debug("%s: error\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600456 if (retry++ > 10)
457 return -EIO;
458 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
Simon Glasse56e20e2015-08-22 18:31:29 -0600459 chip->timeout_c,
460 &status);
Simon Glassf90acf12015-05-04 11:30:59 -0600461 if (rc)
Simon Glassb697e0f2015-08-22 18:31:38 -0600462 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000463
Simon Glassb697e0f2015-08-22 18:31:38 -0600464 if ((status & TPM_STS_DATA_EXPECT) == 0)
465 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000466 }
467 }
468
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000469 /* Go and do it */
Simon Glassb697e0f2015-08-22 18:31:38 -0600470 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
471 if (rc < 0)
472 return rc;
473 debug("%s: done, rc=%d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000474
475 return len;
Simon Glassb697e0f2015-08-22 18:31:38 -0600476}
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000477
Simon Glassb697e0f2015-08-22 18:31:38 -0600478static int tpm_tis_i2c_cleanup(struct udevice *dev)
479{
480 struct tpm_chip *chip = dev_get_priv(dev);
481
482 tpm_tis_i2c_ready(dev);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000483 /*
484 * The TPM needs some time to clean up here,
Rong Changf6267992013-04-12 10:44:57 +0000485 * so we sleep rather than keeping the bus busy
486 */
Simon Glass42c8ec52015-08-22 18:31:30 -0600487 mdelay(2);
Simon Glassb697e0f2015-08-22 18:31:38 -0600488 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
Rong Changf6267992013-04-12 10:44:57 +0000489
Simon Glassb697e0f2015-08-22 18:31:38 -0600490 return 0;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000491}
492
Simon Glass13932b02015-08-22 18:31:25 -0600493static int tpm_tis_i2c_init(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000494{
Simon Glassb697e0f2015-08-22 18:31:38 -0600495 struct tpm_chip *chip = dev_get_priv(dev);
Rong Changf6267992013-04-12 10:44:57 +0000496 u32 vendor;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000497 u32 expected_did_vid;
Simon Glassb697e0f2015-08-22 18:31:38 -0600498 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000499
Simon Glass7c735372015-08-22 18:31:24 -0600500 chip->is_open = 1;
Rong Changf6267992013-04-12 10:44:57 +0000501
Simon Glasse56e20e2015-08-22 18:31:29 -0600502 /* Default timeouts - these could move to the device tree */
Simon Glass42c8ec52015-08-22 18:31:30 -0600503 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
504 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
505 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
506 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
Rong Changf6267992013-04-12 10:44:57 +0000507
Simon Glassb697e0f2015-08-22 18:31:38 -0600508 rc = tpm_tis_i2c_request_locality(dev, 0);
509 if (rc < 0)
510 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000511
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000512 /* Read four bytes from DID_VID register */
Simon Glassb697e0f2015-08-22 18:31:38 -0600513 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
514 tpm_tis_i2c_release_locality(dev, 0, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600515 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000516 }
517
Simon Glassb697e0f2015-08-22 18:31:38 -0600518 if (chip->chip_type == SLB9635) {
Vincent Palatinec34fa52013-04-12 11:04:36 +0000519 vendor = be32_to_cpu(vendor);
520 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
521 } else {
522 /* device id and byte order has changed for newer i2c tpms */
523 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
524 }
Rong Changf6267992013-04-12 10:44:57 +0000525
Simon Glassb697e0f2015-08-22 18:31:38 -0600526 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000527 error("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassf90acf12015-05-04 11:30:59 -0600528 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000529 }
530
Simon Glassb697e0f2015-08-22 18:31:38 -0600531 chip->vend_dev = vendor;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000532 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
Simon Glassb697e0f2015-08-22 18:31:38 -0600533 chip_name[chip->chip_type], vendor >> 16);
Rong Changf6267992013-04-12 10:44:57 +0000534
535 /*
536 * A timeout query to TPM can be placed here.
537 * Standard timeout values are used so far
538 */
539
540 return 0;
Simon Glassf90acf12015-05-04 11:30:59 -0600541}
Rong Changf6267992013-04-12 10:44:57 +0000542
Simon Glassb697e0f2015-08-22 18:31:38 -0600543static int tpm_tis_i2c_open(struct udevice *dev)
Simon Glass4cd7b782015-08-22 18:31:22 -0600544{
Simon Glassb697e0f2015-08-22 18:31:38 -0600545 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600546 int rc;
Simon Glass4cd7b782015-08-22 18:31:22 -0600547
Simon Glassa53b79a2015-08-22 18:31:28 -0600548 debug("%s: start\n", __func__);
Simon Glassb697e0f2015-08-22 18:31:38 -0600549 if (chip->is_open)
Simon Glassa53b79a2015-08-22 18:31:28 -0600550 return -EBUSY;
Simon Glassb697e0f2015-08-22 18:31:38 -0600551 rc = tpm_tis_i2c_init(dev);
Simon Glassa53b79a2015-08-22 18:31:28 -0600552 if (rc < 0)
Simon Glassb697e0f2015-08-22 18:31:38 -0600553 chip->is_open = 0;
Simon Glass4cd7b782015-08-22 18:31:22 -0600554
555 return rc;
556}
557
Simon Glassb697e0f2015-08-22 18:31:38 -0600558static int tpm_tis_i2c_close(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
Simon Glassb697e0f2015-08-22 18:31:38 -0600562 if (chip->is_open) {
563 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
564 chip->is_open = 0;
565 chip->vend_dev = 0;
Simon Glassa53b79a2015-08-22 18:31:28 -0600566 }
Simon Glass4cd7b782015-08-22 18:31:22 -0600567
568 return 0;
569}
570
Simon Glassb697e0f2015-08-22 18:31:38 -0600571static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
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 (size < 50)
576 return -ENOSPC;
Simon Glass4cd7b782015-08-22 18:31:22 -0600577
Simon Glassb697e0f2015-08-22 18:31:38 -0600578 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
579 chip->is_open ? "open" : "closed",
580 chip_name[chip->chip_type],
581 chip->vend_dev >> 16);
582}
Simon Glass4cd7b782015-08-22 18:31:22 -0600583
Simon Glassb697e0f2015-08-22 18:31:38 -0600584static int tpm_tis_i2c_probe(struct udevice *dev)
585{
586 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
587 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600588
Simon Glassb697e0f2015-08-22 18:31:38 -0600589 chip->chip_type = dev_get_driver_data(dev);
Simon Glass4cd7b782015-08-22 18:31:22 -0600590
Simon Glassb697e0f2015-08-22 18:31:38 -0600591 /* TODO: These need to be checked and tuned */
592 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
593 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
594 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
595 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
Simon Glass4cd7b782015-08-22 18:31:22 -0600596
597 return 0;
598}
Simon Glassb697e0f2015-08-22 18:31:38 -0600599
600static const struct tpm_ops tpm_tis_i2c_ops = {
601 .open = tpm_tis_i2c_open,
602 .close = tpm_tis_i2c_close,
603 .get_desc = tpm_tis_get_desc,
604 .send = tpm_tis_i2c_send,
605 .recv = tpm_tis_i2c_recv,
606 .cleanup = tpm_tis_i2c_cleanup,
607};
608
609static const struct udevice_id tpm_tis_i2c_ids[] = {
610 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
611 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
612 { }
613};
614
615U_BOOT_DRIVER(tpm_tis_i2c) = {
Christophe Ricard0766ad22015-10-06 22:54:41 +0200616 .name = "tpm_tis_infineon",
Simon Glassb697e0f2015-08-22 18:31:38 -0600617 .id = UCLASS_TPM,
618 .of_match = tpm_tis_i2c_ids,
619 .ops = &tpm_tis_i2c_ops,
620 .probe = tpm_tis_i2c_probe,
621 .priv_auto_alloc_size = sizeof(struct tpm_chip),
622};