blob: ee4dfeae445bd7d9b398ce59086e54ead4ec20ff [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 *
20 * See file CREDITS for list of people who contributed to this
21 * project.
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation, version 2 of the
26 * License.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 * MA 02111-1307 USA
37 */
38
39#include <common.h>
Simon Glassf90acf12015-05-04 11:30:59 -060040#include <dm.h>
Vincent Palatinec34fa52013-04-12 11:04:36 +000041#include <fdtdec.h>
Masahiro Yamadaafc366f2014-11-26 16:00:58 +090042#include <linux/compiler.h>
Rong Changf6267992013-04-12 10:44:57 +000043#include <i2c.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000044#include <tpm.h>
45#include <asm-generic/errno.h>
Rong Changf6267992013-04-12 10:44:57 +000046#include <linux/types.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000047#include <linux/unaligned/be_byteshift.h>
Rong Changf6267992013-04-12 10:44:57 +000048
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000049#include "tpm_private.h"
Rong Changf6267992013-04-12 10:44:57 +000050
Vincent Palatinec34fa52013-04-12 11:04:36 +000051DECLARE_GLOBAL_DATA_PTR;
52
Rong Changf6267992013-04-12 10:44:57 +000053/* Address of the TPM on the I2C bus */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000054#define TPM_I2C_ADDR 0x20
Rong Changf6267992013-04-12 10:44:57 +000055
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000056/* Max buffer size supported by our tpm */
57#define TPM_DEV_BUFSIZE 1260
Rong Changf6267992013-04-12 10:44:57 +000058
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000059/* Max number of iterations after i2c NAK */
60#define MAX_COUNT 3
61
62/*
63 * Max number of iterations after i2c NAK for 'long' commands
64 *
65 * We need this especially for sending TPM_READY, since the cleanup after the
Rong Changf6267992013-04-12 10:44:57 +000066 * transtion to the ready state may take some time, but it is unpredictable
67 * how long it will take.
68 */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000069#define MAX_COUNT_LONG 50
Rong Changf6267992013-04-12 10:44:57 +000070
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000071#define SLEEP_DURATION 60 /* in usec */
72#define SLEEP_DURATION_LONG 210 /* in usec */
73
74#define TPM_HEADER_SIZE 10
75
76/*
77 * Expected value for DIDVID register
78 *
79 * The only device the system knows about at this moment is Infineon slb9635.
80 */
81#define TPM_TIS_I2C_DID_VID 0x000b15d1L
82
83enum tis_access {
84 TPM_ACCESS_VALID = 0x80,
85 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
86 TPM_ACCESS_REQUEST_PENDING = 0x04,
87 TPM_ACCESS_REQUEST_USE = 0x02,
88};
89
90enum tis_status {
91 TPM_STS_VALID = 0x80,
92 TPM_STS_COMMAND_READY = 0x40,
93 TPM_STS_GO = 0x20,
94 TPM_STS_DATA_AVAIL = 0x10,
95 TPM_STS_DATA_EXPECT = 0x08,
96};
97
98enum tis_defaults {
99 TIS_SHORT_TIMEOUT = 750, /* ms */
100 TIS_LONG_TIMEOUT = 2000, /* ms */
101};
Rong Changf6267992013-04-12 10:44:57 +0000102
103/* expected value for DIDVID register */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000104#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
105#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
106
107enum i2c_chip_type {
108 SLB9635,
109 SLB9645,
110 UNKNOWN,
111};
112
113static const char * const chip_name[] = {
114 [SLB9635] = "slb9635tt",
115 [SLB9645] = "slb9645tt",
116 [UNKNOWN] = "unknown/fallback to slb9635",
117};
Rong Changf6267992013-04-12 10:44:57 +0000118
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000119#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
120#define TPM_STS(l) (0x0001 | ((l) << 4))
121#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
122#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
123
Rong Changf6267992013-04-12 10:44:57 +0000124/* Structure to store I2C TPM specific stuff */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000125struct tpm_dev {
Simon Glassf90acf12015-05-04 11:30:59 -0600126#ifdef CONFIG_DM_I2C
127 struct udevice *dev;
128#else
Rong Changf6267992013-04-12 10:44:57 +0000129 uint addr;
Simon Glassf90acf12015-05-04 11:30:59 -0600130#endif
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000131 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000132 enum i2c_chip_type chip_type;
Rong Changf6267992013-04-12 10:44:57 +0000133};
134
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000135static struct tpm_dev tpm_dev = {
Simon Glassf90acf12015-05-04 11:30:59 -0600136#ifndef CONFIG_DM_I2C
Rong Changf6267992013-04-12 10:44:57 +0000137 .addr = TPM_I2C_ADDR
Simon Glassf90acf12015-05-04 11:30:59 -0600138#endif
Rong Changf6267992013-04-12 10:44:57 +0000139};
140
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000141static struct tpm_dev tpm_dev;
142
Rong Changf6267992013-04-12 10:44:57 +0000143/*
144 * iic_tpm_read() - read from TPM register
145 * @addr: register address to read from
146 * @buffer: provided by caller
147 * @len: number of bytes to read
148 *
149 * Read len bytes from TPM register and put them into
150 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
151 *
152 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
153 * values have to be swapped.
154 *
155 * Return -EIO on error, 0 on success.
156 */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000157static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000158{
159 int rc;
160 int count;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000161 uint32_t addrbuf = addr;
Rong Changf6267992013-04-12 10:44:57 +0000162
Vincent Palatinec34fa52013-04-12 11:04:36 +0000163 if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
164 /* slb9635 protocol should work in both cases */
165 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassf90acf12015-05-04 11:30:59 -0600166#ifdef CONFIG_DM_I2C
167 rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
168#else
Vincent Palatinec34fa52013-04-12 11:04:36 +0000169 rc = i2c_write(tpm_dev.addr, 0, 0,
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000170 (uchar *)&addrbuf, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600171#endif
Vincent Palatinec34fa52013-04-12 11:04:36 +0000172 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000173 break; /* Success, break to skip sleep */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000174 udelay(SLEEP_DURATION);
175 }
Vincent Palatinec34fa52013-04-12 11:04:36 +0000176 if (rc)
177 return -rc;
178
179 /* After the TPM has successfully received the register address
180 * it needs some time, thus we're sleeping here again, before
181 * retrieving the data
182 */
183 for (count = 0; count < MAX_COUNT; count++) {
184 udelay(SLEEP_DURATION);
Simon Glassf90acf12015-05-04 11:30:59 -0600185#ifdef CONFIG_DM_I2C
186 rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
187#else
Vincent Palatinec34fa52013-04-12 11:04:36 +0000188 rc = i2c_read(tpm_dev.addr, 0, 0, buffer, len);
Simon Glassf90acf12015-05-04 11:30:59 -0600189#endif
Vincent Palatinec34fa52013-04-12 11:04:36 +0000190 if (rc == 0)
191 break; /* success, break to skip sleep */
192 }
193 } else {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000194 /*
195 * Use a combined read for newer chips.
196 * Unfortunately the smbus functions are not suitable due to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000197 * the 32 byte limit of the smbus.
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000198 * Retries should usually not be needed, but are kept just to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000199 * be safe on the safe side.
200 */
201 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassf90acf12015-05-04 11:30:59 -0600202#ifdef CONFIG_DM_I2C
203 rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
204#else
Vincent Palatinec34fa52013-04-12 11:04:36 +0000205 rc = i2c_read(tpm_dev.addr, addr, 1, buffer, len);
Simon Glassf90acf12015-05-04 11:30:59 -0600206#endif
Vincent Palatinec34fa52013-04-12 11:04:36 +0000207 if (rc == 0)
208 break; /* break here to skip sleep */
209 udelay(SLEEP_DURATION);
210 }
Rong Changf6267992013-04-12 10:44:57 +0000211 }
212
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000213 /* Take care of 'guard time' */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000214 udelay(SLEEP_DURATION);
Rong Changf6267992013-04-12 10:44:57 +0000215 if (rc)
216 return -rc;
217
218 return 0;
219}
220
221static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000222 unsigned int sleep_time, u8 max_count)
Rong Changf6267992013-04-12 10:44:57 +0000223{
224 int rc = 0;
225 int count;
226
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000227 /* Prepare send buffer */
Simon Glassf90acf12015-05-04 11:30:59 -0600228#ifndef CONFIG_DM_I2C
Rong Changf6267992013-04-12 10:44:57 +0000229 tpm_dev.buf[0] = addr;
230 memcpy(&(tpm_dev.buf[1]), buffer, len);
Simon Glassf90acf12015-05-04 11:30:59 -0600231 buffer = tpm_dev.buf;
232 len++;
233#endif
Rong Changf6267992013-04-12 10:44:57 +0000234
235 for (count = 0; count < max_count; count++) {
Simon Glassf90acf12015-05-04 11:30:59 -0600236#ifdef CONFIG_DM_I2C
237 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
238#else
239 rc = i2c_write(tpm_dev.addr, 0, 0, buffer, len);
240#endif
Rong Changf6267992013-04-12 10:44:57 +0000241 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000242 break; /* Success, break to skip sleep */
Rong Changf6267992013-04-12 10:44:57 +0000243 udelay(sleep_time);
244 }
245
Vincent Palatinec34fa52013-04-12 11:04:36 +0000246 /* take care of 'guard time' */
Simon Glassf90acf12015-05-04 11:30:59 -0600247 udelay(sleep_time);
Rong Changf6267992013-04-12 10:44:57 +0000248 if (rc)
249 return -rc;
250
251 return 0;
252}
253
254/*
255 * iic_tpm_write() - write to TPM register
256 * @addr: register address to write to
257 * @buffer: containing data to be written
258 * @len: number of bytes to write
259 *
260 * Write len bytes from provided buffer to TPM register (little
261 * endian format, i.e. buffer[0] is written as first byte).
262 *
263 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
264 * values have to be swapped.
265 *
266 * NOTE: use this function instead of the iic_tpm_write_generic function.
267 *
268 * Return -EIO on error, 0 on success
269 */
270static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
271{
272 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
273 MAX_COUNT);
274}
275
276/*
277 * This function is needed especially for the cleanup situation after
278 * sending TPM_READY
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000279 */
Rong Changf6267992013-04-12 10:44:57 +0000280static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
281{
282 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
283 MAX_COUNT_LONG);
284}
285
Rong Changf6267992013-04-12 10:44:57 +0000286static int check_locality(struct tpm_chip *chip, int loc)
287{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000288 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000289 u8 buf;
290 int rc;
291
292 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
293 if (rc < 0)
294 return rc;
295
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000296 if ((buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000297 chip->vendor.locality = loc;
298 return loc;
299 }
300
301 return -1;
302}
303
304static void release_locality(struct tpm_chip *chip, int loc, int force)
305{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000306 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000307 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000308
Rong Changf6267992013-04-12 10:44:57 +0000309 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
310 return;
311
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000312 if (force || (buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000313 buf = TPM_ACCESS_ACTIVE_LOCALITY;
314 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
315 }
316}
317
318static int request_locality(struct tpm_chip *chip, int loc)
319{
320 unsigned long start, stop;
321 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassf90acf12015-05-04 11:30:59 -0600322 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000323
324 if (check_locality(chip, loc) >= 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000325 return loc; /* We already have the locality */
Rong Changf6267992013-04-12 10:44:57 +0000326
Simon Glassf90acf12015-05-04 11:30:59 -0600327 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
328 if (rc)
329 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000330
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000331 /* Wait for burstcount */
Rong Changf6267992013-04-12 10:44:57 +0000332 start = get_timer(0);
333 stop = chip->vendor.timeout_a;
334 do {
335 if (check_locality(chip, loc) >= 0)
336 return loc;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000337 udelay(TPM_TIMEOUT * 1000);
Rong Changf6267992013-04-12 10:44:57 +0000338 } while (get_timer(start) < stop);
339
340 return -1;
341}
342
343static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
344{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000345 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Changf6267992013-04-12 10:44:57 +0000346 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000347
Rong Changf6267992013-04-12 10:44:57 +0000348 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
349 return 0;
350 else
351 return buf;
352}
353
354static void tpm_tis_i2c_ready(struct tpm_chip *chip)
355{
Simon Glassf90acf12015-05-04 11:30:59 -0600356 int rc;
357
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000358 /* This causes the current command to be aborted */
Rong Changf6267992013-04-12 10:44:57 +0000359 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000360
Simon Glassf90acf12015-05-04 11:30:59 -0600361 debug("%s\n", __func__);
362 rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
363 if (rc)
364 debug("%s: rc=%d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000365}
366
367static ssize_t get_burstcount(struct tpm_chip *chip)
368{
369 unsigned long start, stop;
370 ssize_t burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000371 u8 addr, buf[3];
Rong Changf6267992013-04-12 10:44:57 +0000372
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000373 /* Wait for burstcount */
374 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Changf6267992013-04-12 10:44:57 +0000375 start = get_timer(0);
376 stop = chip->vendor.timeout_d;
377 do {
378 /* Note: STS is little endian */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000379 addr = TPM_STS(chip->vendor.locality) + 1;
380 if (iic_tpm_read(addr, buf, 3) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000381 burstcnt = 0;
382 else
383 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
384
385 if (burstcnt)
386 return burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000387 udelay(TPM_TIMEOUT * 1000);
Rong Changf6267992013-04-12 10:44:57 +0000388 } while (get_timer(start) < stop);
389
390 return -EBUSY;
391}
392
393static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000394 int *status)
Rong Changf6267992013-04-12 10:44:57 +0000395{
396 unsigned long start, stop;
397
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000398 /* Check current status */
Rong Changf6267992013-04-12 10:44:57 +0000399 *status = tpm_tis_i2c_status(chip);
400 if ((*status & mask) == mask)
401 return 0;
402
403 start = get_timer(0);
404 stop = timeout;
405 do {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000406 udelay(TPM_TIMEOUT * 1000);
Rong Changf6267992013-04-12 10:44:57 +0000407 *status = tpm_tis_i2c_status(chip);
408 if ((*status & mask) == mask)
409 return 0;
Rong Changf6267992013-04-12 10:44:57 +0000410 } while (get_timer(start) < stop);
411
412 return -ETIME;
413}
414
415static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
416{
417 size_t size = 0;
418 ssize_t burstcnt;
419 int rc;
420
421 while (size < count) {
422 burstcnt = get_burstcount(chip);
423
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000424 /* burstcount < 0 -> tpm is busy */
Rong Changf6267992013-04-12 10:44:57 +0000425 if (burstcnt < 0)
426 return burstcnt;
427
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000428 /* Limit received data to max left */
Rong Changf6267992013-04-12 10:44:57 +0000429 if (burstcnt > (count - size))
430 burstcnt = count - size;
431
432 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000433 &(buf[size]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000434 if (rc == 0)
435 size += burstcnt;
436 }
437
438 return size;
439}
440
441static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
442{
443 int size = 0;
444 int expected, status;
445
446 if (count < TPM_HEADER_SIZE) {
447 size = -EIO;
448 goto out;
449 }
450
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000451 /* Read first 10 bytes, including tag, paramsize, and result */
Rong Changf6267992013-04-12 10:44:57 +0000452 size = recv_data(chip, buf, TPM_HEADER_SIZE);
453 if (size < TPM_HEADER_SIZE) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000454 error("Unable to read header\n");
Rong Changf6267992013-04-12 10:44:57 +0000455 goto out;
456 }
457
458 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
459 if ((size_t)expected > count) {
Simon Glassf90acf12015-05-04 11:30:59 -0600460 error("Error size=%x, expected=%x, count=%x\n", size, expected,
461 count);
Rong Changf6267992013-04-12 10:44:57 +0000462 size = -EIO;
463 goto out;
464 }
465
466 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000467 expected - TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000468 if (size < expected) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000469 error("Unable to read remainder of result\n");
Rong Changf6267992013-04-12 10:44:57 +0000470 size = -ETIME;
471 goto out;
472 }
473
474 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000475 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
476 error("Error left over data\n");
Rong Changf6267992013-04-12 10:44:57 +0000477 size = -EIO;
478 goto out;
479 }
480
481out:
482 tpm_tis_i2c_ready(chip);
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 */
487 udelay(2000);
488 release_locality(chip, chip->vendor.locality, 0);
489
490 return size;
491}
492
493static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
494{
495 int rc, status;
Simon Glassf90acf12015-05-04 11:30:59 -0600496 size_t burstcnt;
Rong Changf6267992013-04-12 10:44:57 +0000497 size_t count = 0;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000498 int retry = 0;
Rong Changf6267992013-04-12 10:44:57 +0000499 u8 sts = TPM_STS_GO;
500
Simon Glassf90acf12015-05-04 11:30:59 -0600501 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000502 if (len > TPM_DEV_BUFSIZE)
503 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Changf6267992013-04-12 10:44:57 +0000504
505 if (request_locality(chip, 0) < 0)
506 return -EBUSY;
507
508 status = tpm_tis_i2c_status(chip);
509 if ((status & TPM_STS_COMMAND_READY) == 0) {
510 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000511 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
512 chip->vendor.timeout_b, &status) < 0) {
Rong Changf6267992013-04-12 10:44:57 +0000513 rc = -ETIME;
514 goto out_err;
515 }
516 }
517
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000518 burstcnt = get_burstcount(chip);
519
520 /* burstcount < 0 -> tpm is busy */
521 if (burstcnt < 0)
522 return burstcnt;
523
Simon Glassf90acf12015-05-04 11:30:59 -0600524 while (count < len) {
525 udelay(300);
526 if (burstcnt > len - count)
527 burstcnt = len - count;
Rong Changf6267992013-04-12 10:44:57 +0000528
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000529#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
530 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
531 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
532#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
Rong Changf6267992013-04-12 10:44:57 +0000533
534 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000535 &(buf[count]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000536 if (rc == 0)
537 count += burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000538 else {
Simon Glassf90acf12015-05-04 11:30:59 -0600539 debug("%s: error\n", __func__);
540 if (retry++ > 10) {
541 rc = -EIO;
542 goto out_err;
543 }
544 rc = wait_for_stat(chip, TPM_STS_VALID,
545 chip->vendor.timeout_c, &status);
546 if (rc)
547 goto out_err;
Rong Changf6267992013-04-12 10:44:57 +0000548
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000549 if ((status & TPM_STS_DATA_EXPECT) == 0) {
550 rc = -EIO;
551 goto out_err;
552 }
Rong Changf6267992013-04-12 10:44:57 +0000553 }
554 }
555
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000556 /* Go and do it */
Rong Changf6267992013-04-12 10:44:57 +0000557 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600558 debug("done\n");
Rong Changf6267992013-04-12 10:44:57 +0000559
560 return len;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000561
Rong Changf6267992013-04-12 10:44:57 +0000562out_err:
Simon Glassf90acf12015-05-04 11:30:59 -0600563 debug("%s: out_err\n", __func__);
Rong Changf6267992013-04-12 10:44:57 +0000564 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000565 /*
566 * The TPM needs some time to clean up here,
Rong Changf6267992013-04-12 10:44:57 +0000567 * so we sleep rather than keeping the bus busy
568 */
569 udelay(2000);
570 release_locality(chip, chip->vendor.locality, 0);
571
572 return rc;
573}
574
575static struct tpm_vendor_specific tpm_tis_i2c = {
576 .status = tpm_tis_i2c_status,
577 .recv = tpm_tis_i2c_recv,
578 .send = tpm_tis_i2c_send,
579 .cancel = tpm_tis_i2c_ready,
580 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
581 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
582 .req_canceled = TPM_STS_COMMAND_READY,
583};
584
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000585
Vincent Palatinec34fa52013-04-12 11:04:36 +0000586static enum i2c_chip_type tpm_vendor_chip_type(void)
587{
588#ifdef CONFIG_OF_CONTROL
589 const void *blob = gd->fdt_blob;
590
591 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
592 return SLB9645;
593
594 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
595 return SLB9635;
596#endif
597 return UNKNOWN;
598}
599
Simon Glassf90acf12015-05-04 11:30:59 -0600600static int tpm_vendor_init_common(void)
Rong Changf6267992013-04-12 10:44:57 +0000601{
Simon Glassf90acf12015-05-04 11:30:59 -0600602 struct tpm_chip *chip;
Rong Changf6267992013-04-12 10:44:57 +0000603 u32 vendor;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000604 u32 expected_did_vid;
Rong Changf6267992013-04-12 10:44:57 +0000605
Vincent Palatinec34fa52013-04-12 11:04:36 +0000606 tpm_dev.chip_type = tpm_vendor_chip_type();
607
Rong Changf6267992013-04-12 10:44:57 +0000608 chip = tpm_register_hardware(&tpm_tis_i2c);
Simon Glassf90acf12015-05-04 11:30:59 -0600609 if (chip < 0)
610 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000611
612 /* Disable interrupts (not supported) */
613 chip->vendor.irq = 0;
614
615 /* Default timeouts */
616 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
617 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
618 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
619 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
620
Simon Glassf90acf12015-05-04 11:30:59 -0600621 if (request_locality(chip, 0) < 0)
622 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000623
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000624 /* Read four bytes from DID_VID register */
Rong Changf6267992013-04-12 10:44:57 +0000625 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
Simon Glassf90acf12015-05-04 11:30:59 -0600626 release_locality(chip, 0, 1);
627 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000628 }
629
Vincent Palatinec34fa52013-04-12 11:04:36 +0000630 if (tpm_dev.chip_type == SLB9635) {
631 vendor = be32_to_cpu(vendor);
632 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
633 } else {
634 /* device id and byte order has changed for newer i2c tpms */
635 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
636 }
Rong Changf6267992013-04-12 10:44:57 +0000637
Vincent Palatinec34fa52013-04-12 11:04:36 +0000638 if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000639 error("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassf90acf12015-05-04 11:30:59 -0600640 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000641 }
642
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000643 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
644 chip_name[tpm_dev.chip_type], vendor >> 16);
Rong Changf6267992013-04-12 10:44:57 +0000645
646 /*
647 * A timeout query to TPM can be placed here.
648 * Standard timeout values are used so far
649 */
650
651 return 0;
Simon Glassf90acf12015-05-04 11:30:59 -0600652}
Rong Changf6267992013-04-12 10:44:57 +0000653
Simon Glassf90acf12015-05-04 11:30:59 -0600654#ifdef CONFIG_DM_I2C
655/* Initialisation of i2c tpm */
656int tpm_vendor_init_dev(struct udevice *dev)
657{
658 tpm_dev.dev = dev;
659 return tpm_vendor_init_common();
660}
661#else
662/* Initialisation of i2c tpm */
663int tpm_vendor_init(uint32_t dev_addr)
664{
665 uint old_addr;
666 int rc = 0;
Rong Changf6267992013-04-12 10:44:57 +0000667
Simon Glassf90acf12015-05-04 11:30:59 -0600668 old_addr = tpm_dev.addr;
669 if (dev_addr != 0)
670 tpm_dev.addr = dev_addr;
671
672 rc = tpm_vendor_init_common();
673 if (rc)
674 tpm_dev.addr = old_addr;
675
Rong Changf6267992013-04-12 10:44:57 +0000676 return rc;
677}
Simon Glassf90acf12015-05-04 11:30:59 -0600678#endif
Rong Changf6267992013-04-12 10:44:57 +0000679
680void tpm_vendor_cleanup(struct tpm_chip *chip)
681{
682 release_locality(chip, chip->vendor.locality, 1);
683}