blob: ef3ff0dbf67eb6705bf51e444661f8963a7ca4a7 [file] [log] [blame]
Rong Chang8faa9452013-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 Glassc98f7fe2015-08-22 18:31:21 -060020 * SPDX-License-Identifier: GPL-2.0
Rong Chang8faa9452013-04-12 10:44:57 +000021 */
22
23#include <common.h>
Simon Glassc9a3e842015-05-04 11:30:59 -060024#include <dm.h>
Vincent Palatin06883eb2013-04-12 11:04:36 +000025#include <fdtdec.h>
Rong Chang8faa9452013-04-12 10:44:57 +000026#include <i2c.h>
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000027#include <tpm.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090028#include <linux/errno.h>
Simon Glasscd8cc672015-08-22 18:31:38 -060029#include <linux/compiler.h>
Rong Chang8faa9452013-04-12 10:44:57 +000030#include <linux/types.h>
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000031#include <linux/unaligned/be_byteshift.h>
Rong Chang8faa9452013-04-12 10:44:57 +000032
Christophe Ricard26549522016-01-21 23:27:12 +010033#include "tpm_tis.h"
Simon Glass6c7a2322015-08-22 18:31:31 -060034#include "tpm_internal.h"
Rong Chang8faa9452013-04-12 10:44:57 +000035
Vincent Palatin06883eb2013-04-12 11:04:36 +000036DECLARE_GLOBAL_DATA_PTR;
37
Christophe Ricard26549522016-01-21 23:27:12 +010038enum i2c_chip_type {
39 SLB9635,
40 SLB9645,
41 UNKNOWN,
42};
43
44/* expected value for DIDVID register */
45#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
46#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
47
Vincent Palatin06883eb2013-04-12 11:04:36 +000048static const char * const chip_name[] = {
49 [SLB9635] = "slb9635tt",
50 [SLB9645] = "slb9645tt",
51 [UNKNOWN] = "unknown/fallback to slb9635",
52};
Rong Chang8faa9452013-04-12 10:44:57 +000053
Christophe Ricard26549522016-01-21 23:27:12 +010054#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
55#define TPM_STS(l) (0x0001 | ((l) << 4))
56#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
57#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
58
Rong Chang8faa9452013-04-12 10:44:57 +000059/*
Simon Glass7ccebaf2015-08-22 18:31:29 -060060 * tpm_tis_i2c_read() - read from TPM register
Rong Chang8faa9452013-04-12 10:44:57 +000061 * @addr: register address to read from
62 * @buffer: provided by caller
63 * @len: number of bytes to read
64 *
65 * Read len bytes from TPM register and put them into
66 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
67 *
68 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
69 * values have to be swapped.
70 *
71 * Return -EIO on error, 0 on success.
72 */
Simon Glasscd8cc672015-08-22 18:31:38 -060073static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
74 size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +000075{
Simon Glasscd8cc672015-08-22 18:31:38 -060076 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +000077 int rc;
78 int count;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000079 uint32_t addrbuf = addr;
Rong Chang8faa9452013-04-12 10:44:57 +000080
Simon Glasscd8cc672015-08-22 18:31:38 -060081 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
Vincent Palatin06883eb2013-04-12 11:04:36 +000082 /* slb9635 protocol should work in both cases */
83 for (count = 0; count < MAX_COUNT; count++) {
Simon Glasscd8cc672015-08-22 18:31:38 -060084 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatin06883eb2013-04-12 11:04:36 +000085 if (rc == 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000086 break; /* Success, break to skip sleep */
Simon Glassa57bcee2015-08-22 18:31:30 -060087 udelay(SLEEP_DURATION_US);
Vincent Palatin06883eb2013-04-12 11:04:36 +000088 }
Vincent Palatin06883eb2013-04-12 11:04:36 +000089 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -060090 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +000091
Vincent Palatin06883eb2013-04-12 11:04:36 +000092 /* After the TPM has successfully received the register address
93 * it needs some time, thus we're sleeping here again, before
94 * retrieving the data
95 */
96 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassa57bcee2015-08-22 18:31:30 -060097 udelay(SLEEP_DURATION_US);
Simon Glasscd8cc672015-08-22 18:31:38 -060098 rc = dm_i2c_read(dev, 0, buffer, len);
Vincent Palatin06883eb2013-04-12 11:04:36 +000099 if (rc == 0)
100 break; /* success, break to skip sleep */
101 }
102 } else {
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000103 /*
104 * Use a combined read for newer chips.
105 * Unfortunately the smbus functions are not suitable due to
Vincent Palatin06883eb2013-04-12 11:04:36 +0000106 * the 32 byte limit of the smbus.
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000107 * Retries should usually not be needed, but are kept just to
Vincent Palatin06883eb2013-04-12 11:04:36 +0000108 * be safe on the safe side.
109 */
110 for (count = 0; count < MAX_COUNT; count++) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600111 rc = dm_i2c_read(dev, addr, buffer, len);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000112 if (rc == 0)
113 break; /* break here to skip sleep */
Simon Glassa57bcee2015-08-22 18:31:30 -0600114 udelay(SLEEP_DURATION_US);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000115 }
Rong Chang8faa9452013-04-12 10:44:57 +0000116 }
117
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000118 /* Take care of 'guard time' */
Simon Glassa57bcee2015-08-22 18:31:30 -0600119 udelay(SLEEP_DURATION_US);
Rong Chang8faa9452013-04-12 10:44:57 +0000120 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -0600121 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000122
123 return 0;
124}
125
Simon Glasscd8cc672015-08-22 18:31:38 -0600126static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
127 const u8 *buffer, size_t len,
Simon Glassa57bcee2015-08-22 18:31:30 -0600128 unsigned int sleep_time_us, u8 max_count)
Rong Chang8faa9452013-04-12 10:44:57 +0000129{
Simon Glasscd8cc672015-08-22 18:31:38 -0600130 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
131 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000132 int rc = 0;
133 int count;
134
Simon Glasscd8cc672015-08-22 18:31:38 -0600135 if (chip->chip_type == SLB9635) {
136 /* Prepare send buffer to include the address */
137 priv->buf[0] = addr;
138 memcpy(&(priv->buf[1]), buffer, len);
139 buffer = priv->buf;
140 len++;
141 addr = 0;
142 }
143
Rong Chang8faa9452013-04-12 10:44:57 +0000144 for (count = 0; count < max_count; count++) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600145 rc = dm_i2c_write(dev, addr, buffer, len);
Rong Chang8faa9452013-04-12 10:44:57 +0000146 if (rc == 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000147 break; /* Success, break to skip sleep */
Simon Glassa57bcee2015-08-22 18:31:30 -0600148 udelay(sleep_time_us);
Rong Chang8faa9452013-04-12 10:44:57 +0000149 }
150
Vincent Palatin06883eb2013-04-12 11:04:36 +0000151 /* take care of 'guard time' */
Simon Glassa57bcee2015-08-22 18:31:30 -0600152 udelay(sleep_time_us);
Rong Chang8faa9452013-04-12 10:44:57 +0000153 if (rc)
Simon Glasscd8cc672015-08-22 18:31:38 -0600154 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000155
156 return 0;
157}
158
159/*
Simon Glass7ccebaf2015-08-22 18:31:29 -0600160 * tpm_tis_i2c_write() - write to TPM register
Rong Chang8faa9452013-04-12 10:44:57 +0000161 * @addr: register address to write to
162 * @buffer: containing data to be written
163 * @len: number of bytes to write
164 *
165 * Write len bytes from provided buffer to TPM register (little
166 * endian format, i.e. buffer[0] is written as first byte).
167 *
168 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
169 * values have to be swapped.
170 *
Simon Glass7ccebaf2015-08-22 18:31:29 -0600171 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
Rong Chang8faa9452013-04-12 10:44:57 +0000172 *
173 * Return -EIO on error, 0 on success
174 */
Simon Glasscd8cc672015-08-22 18:31:38 -0600175static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
176 size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +0000177{
Simon Glasscd8cc672015-08-22 18:31:38 -0600178 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
179 SLEEP_DURATION_US, MAX_COUNT);
Rong Chang8faa9452013-04-12 10:44:57 +0000180}
181
182/*
183 * This function is needed especially for the cleanup situation after
184 * sending TPM_READY
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000185 */
Simon Glasscd8cc672015-08-22 18:31:38 -0600186static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
187 size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +0000188{
Simon Glasscd8cc672015-08-22 18:31:38 -0600189 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
Simon Glassa57bcee2015-08-22 18:31:30 -0600190 SLEEP_DURATION_LONG_US,
191 MAX_COUNT_LONG);
Rong Chang8faa9452013-04-12 10:44:57 +0000192}
193
Simon Glasscd8cc672015-08-22 18:31:38 -0600194static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
Rong Chang8faa9452013-04-12 10:44:57 +0000195{
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000196 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Simon Glasscd8cc672015-08-22 18:31:38 -0600197 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000198 u8 buf;
199 int rc;
200
Simon Glasscd8cc672015-08-22 18:31:38 -0600201 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
Rong Chang8faa9452013-04-12 10:44:57 +0000202 if (rc < 0)
203 return rc;
204
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000205 if ((buf & mask) == mask) {
Simon Glassab79b002015-08-22 18:31:24 -0600206 chip->locality = loc;
Rong Chang8faa9452013-04-12 10:44:57 +0000207 return loc;
208 }
209
Simon Glasscd8cc672015-08-22 18:31:38 -0600210 return -ENOENT;
Rong Chang8faa9452013-04-12 10:44:57 +0000211}
212
Simon Glasscd8cc672015-08-22 18:31:38 -0600213static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
Simon Glass7ccebaf2015-08-22 18:31:29 -0600214 int force)
Rong Chang8faa9452013-04-12 10:44:57 +0000215{
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000216 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Chang8faa9452013-04-12 10:44:57 +0000217 u8 buf;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000218
Simon Glasscd8cc672015-08-22 18:31:38 -0600219 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
Rong Chang8faa9452013-04-12 10:44:57 +0000220 return;
221
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000222 if (force || (buf & mask) == mask) {
Rong Chang8faa9452013-04-12 10:44:57 +0000223 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Simon Glasscd8cc672015-08-22 18:31:38 -0600224 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
Rong Chang8faa9452013-04-12 10:44:57 +0000225 }
226}
227
Simon Glasscd8cc672015-08-22 18:31:38 -0600228static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
Rong Chang8faa9452013-04-12 10:44:57 +0000229{
Simon Glasscd8cc672015-08-22 18:31:38 -0600230 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000231 unsigned long start, stop;
232 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassc9a3e842015-05-04 11:30:59 -0600233 int rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000234
Simon Glasscd8cc672015-08-22 18:31:38 -0600235 rc = tpm_tis_i2c_check_locality(dev, loc);
236 if (rc >= 0) {
237 debug("%s: Already have locality\n", __func__);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000238 return loc; /* We already have the locality */
Simon Glasscd8cc672015-08-22 18:31:38 -0600239 } else if (rc != -ENOENT) {
240 debug("%s: Failed to get locality: %d\n", __func__, rc);
241 return rc;
242 }
Rong Chang8faa9452013-04-12 10:44:57 +0000243
Simon Glasscd8cc672015-08-22 18:31:38 -0600244 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
245 if (rc) {
246 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
Simon Glassc9a3e842015-05-04 11:30:59 -0600247 return rc;
Simon Glasscd8cc672015-08-22 18:31:38 -0600248 }
Rong Chang8faa9452013-04-12 10:44:57 +0000249
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000250 /* Wait for burstcount */
Rong Chang8faa9452013-04-12 10:44:57 +0000251 start = get_timer(0);
Simon Glassab79b002015-08-22 18:31:24 -0600252 stop = chip->timeout_a;
Rong Chang8faa9452013-04-12 10:44:57 +0000253 do {
Simon Glasscd8cc672015-08-22 18:31:38 -0600254 rc = tpm_tis_i2c_check_locality(dev, loc);
255 if (rc >= 0) {
256 debug("%s: Have locality\n", __func__);
Rong Chang8faa9452013-04-12 10:44:57 +0000257 return loc;
Simon Glasscd8cc672015-08-22 18:31:38 -0600258 } else if (rc != -ENOENT) {
259 debug("%s: Failed to get locality: %d\n", __func__, rc);
260 return rc;
261 }
Simon Glassa57bcee2015-08-22 18:31:30 -0600262 mdelay(TPM_TIMEOUT_MS);
Rong Chang8faa9452013-04-12 10:44:57 +0000263 } while (get_timer(start) < stop);
Simon Glasscd8cc672015-08-22 18:31:38 -0600264 debug("%s: Timeout getting locality: %d\n", __func__, rc);
Rong Chang8faa9452013-04-12 10:44:57 +0000265
Simon Glasscd8cc672015-08-22 18:31:38 -0600266 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000267}
268
Simon Glasscd8cc672015-08-22 18:31:38 -0600269static u8 tpm_tis_i2c_status(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000270{
Simon Glasscd8cc672015-08-22 18:31:38 -0600271 struct tpm_chip *chip = dev_get_priv(dev);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000272 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Chang8faa9452013-04-12 10:44:57 +0000273 u8 buf;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000274
Simon Glasscd8cc672015-08-22 18:31:38 -0600275 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
Rong Chang8faa9452013-04-12 10:44:57 +0000276 return 0;
277 else
278 return buf;
279}
280
Simon Glasscd8cc672015-08-22 18:31:38 -0600281static int tpm_tis_i2c_ready(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000282{
Simon Glasscd8cc672015-08-22 18:31:38 -0600283 struct tpm_chip *chip = dev_get_priv(dev);
Simon Glassc9a3e842015-05-04 11:30:59 -0600284 int rc;
285
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000286 /* This causes the current command to be aborted */
Rong Chang8faa9452013-04-12 10:44:57 +0000287 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000288
Simon Glassc9a3e842015-05-04 11:30:59 -0600289 debug("%s\n", __func__);
Simon Glasscd8cc672015-08-22 18:31:38 -0600290 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
Simon Glassc9a3e842015-05-04 11:30:59 -0600291 if (rc)
292 debug("%s: rc=%d\n", __func__, rc);
Simon Glasscd8cc672015-08-22 18:31:38 -0600293
294 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000295}
296
Simon Glasscd8cc672015-08-22 18:31:38 -0600297static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000298{
Simon Glasscd8cc672015-08-22 18:31:38 -0600299 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000300 unsigned long start, stop;
301 ssize_t burstcnt;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000302 u8 addr, buf[3];
Rong Chang8faa9452013-04-12 10:44:57 +0000303
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000304 /* Wait for burstcount */
305 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Chang8faa9452013-04-12 10:44:57 +0000306 start = get_timer(0);
Simon Glassab79b002015-08-22 18:31:24 -0600307 stop = chip->timeout_d;
Rong Chang8faa9452013-04-12 10:44:57 +0000308 do {
309 /* Note: STS is little endian */
Simon Glassab79b002015-08-22 18:31:24 -0600310 addr = TPM_STS(chip->locality) + 1;
Simon Glasscd8cc672015-08-22 18:31:38 -0600311 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
Rong Chang8faa9452013-04-12 10:44:57 +0000312 burstcnt = 0;
313 else
314 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
315
316 if (burstcnt)
317 return burstcnt;
Simon Glassa57bcee2015-08-22 18:31:30 -0600318 mdelay(TPM_TIMEOUT_MS);
Rong Chang8faa9452013-04-12 10:44:57 +0000319 } while (get_timer(start) < stop);
320
321 return -EBUSY;
322}
323
Simon Glasscd8cc672015-08-22 18:31:38 -0600324static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
Simon Glass7ccebaf2015-08-22 18:31:29 -0600325 unsigned long timeout, int *status)
Rong Chang8faa9452013-04-12 10:44:57 +0000326{
327 unsigned long start, stop;
328
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000329 /* Check current status */
Simon Glasscd8cc672015-08-22 18:31:38 -0600330 *status = tpm_tis_i2c_status(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000331 if ((*status & mask) == mask)
332 return 0;
333
334 start = get_timer(0);
335 stop = timeout;
336 do {
Simon Glassa57bcee2015-08-22 18:31:30 -0600337 mdelay(TPM_TIMEOUT_MS);
Simon Glasscd8cc672015-08-22 18:31:38 -0600338 *status = tpm_tis_i2c_status(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000339 if ((*status & mask) == mask)
340 return 0;
Rong Chang8faa9452013-04-12 10:44:57 +0000341 } while (get_timer(start) < stop);
342
Simon Glasscd8cc672015-08-22 18:31:38 -0600343 return -ETIMEDOUT;
Rong Chang8faa9452013-04-12 10:44:57 +0000344}
345
Simon Glasscd8cc672015-08-22 18:31:38 -0600346static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
Rong Chang8faa9452013-04-12 10:44:57 +0000347{
Simon Glasscd8cc672015-08-22 18:31:38 -0600348 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000349 size_t size = 0;
350 ssize_t burstcnt;
351 int rc;
352
353 while (size < count) {
Simon Glasscd8cc672015-08-22 18:31:38 -0600354 burstcnt = tpm_tis_i2c_get_burstcount(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000355
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000356 /* burstcount < 0 -> tpm is busy */
Rong Chang8faa9452013-04-12 10:44:57 +0000357 if (burstcnt < 0)
358 return burstcnt;
359
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000360 /* Limit received data to max left */
Rong Chang8faa9452013-04-12 10:44:57 +0000361 if (burstcnt > (count - size))
362 burstcnt = count - size;
363
Simon Glasscd8cc672015-08-22 18:31:38 -0600364 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
365 &(buf[size]), burstcnt);
Rong Chang8faa9452013-04-12 10:44:57 +0000366 if (rc == 0)
367 size += burstcnt;
368 }
369
370 return size;
371}
372
Simon Glasscd8cc672015-08-22 18:31:38 -0600373static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
Rong Chang8faa9452013-04-12 10:44:57 +0000374{
Simon Glasscd8cc672015-08-22 18:31:38 -0600375 struct tpm_chip *chip = dev_get_priv(dev);
Rong Chang8faa9452013-04-12 10:44:57 +0000376 int size = 0;
377 int expected, status;
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);
397 if ((size_t)expected > count) {
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) {
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000542 error("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,
636 .priv_auto_alloc_size = sizeof(struct tpm_chip),
637};