blob: 972b17de42909a2b168a33c9c4943890b595305c [file] [log] [blame]
Stefan Reinauer7cb01e02013-08-29 16:05:02 -07001/*
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 *
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080011 * 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 *
Stefan Reinauer7cb01e02013-08-29 16:05:02 -070015 * 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 *
Stefan Reinauer7cb01e02013-08-29 16:05:02 -070020 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation, version 2 of the
23 * License.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
Stefan Reinauer7cb01e02013-08-29 16:05:02 -070029 */
30
Duncan Laurie40ae1702016-08-31 13:51:14 -070031#include <arch/early_variables.h>
Duncan Laurieffa765f2016-09-01 15:50:22 -070032#include <commonlib/endian.h>
Stefan Reinauer7cb01e02013-08-29 16:05:02 -070033#include <stdint.h>
34#include <string.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080035#include <types.h>
Stefan Reinauer7cb01e02013-08-29 16:05:02 -070036#include <delay.h>
37#include <console/console.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080038#include <device/i2c.h>
Julius Werner9ff8f6f2015-02-23 14:31:09 -080039#include <endian.h>
Duncan Laurieffa765f2016-09-01 15:50:22 -070040#include <timer.h>
Stefan Reinauer7cb01e02013-08-29 16:05:02 -070041#include "tpm.h"
42
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080043/* max. number of iterations after I2C NAK */
44#define MAX_COUNT 3
45
46#define SLEEP_DURATION 60 /* in usec */
Duncan Laurieefa579f2016-08-31 14:48:12 -070047#define SLEEP_DURATION_LONG 210 /* in usec */
Duncan Laurie1ca19682016-09-07 10:52:12 -070048#define SLEEP_DURATION_PROBE_MS 1000 /* in msec */
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080049
50/* max. number of iterations after I2C NAK for 'long' commands
51 * we need this especially for sending TPM_READY, since the cleanup after the
52 * transtion to the ready state may take some time, but it is unpredictable
53 * how long it will take.
54 */
55#define MAX_COUNT_LONG 50
56
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080057/* expected value for DIDVID register */
58#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
59#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
60
61enum i2c_chip_type {
62 SLB9635,
63 SLB9645,
64 UNKNOWN,
65};
66
67static const char * const chip_name[] = {
68 [SLB9635] = "slb9635tt",
69 [SLB9645] = "slb9645tt",
70 [UNKNOWN] = "unknown/fallback to slb9635",
71};
72
73/* Structure to store I2C TPM specific stuff */
74struct tpm_inf_dev {
Daisuke Nojiri1fd91a12014-10-29 11:14:53 -070075 int bus;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080076 unsigned int addr;
Duncan Laurieefa579f2016-08-31 14:48:12 -070077 unsigned int sleep_short; /* Short sleep duration in usec */
78 unsigned int sleep_long; /* Long sleep duration in usec */
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080079 uint8_t buf[TPM_BUFSIZE + sizeof(uint8_t)]; // max. buffer size + addr
80 enum i2c_chip_type chip_type;
81};
82
Duncan Laurie40ae1702016-08-31 13:51:14 -070083static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080084
85/*
86 * iic_tpm_read() - read from TPM register
87 * @addr: register address to read from
88 * @buffer: provided by caller
89 * @len: number of bytes to read
90 *
91 * Read len bytes from TPM register and put them into
92 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
93 *
94 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
95 * values have to be swapped.
96 *
97 * Return -1 on error, 0 on success.
98 */
99static int iic_tpm_read(uint8_t addr, uint8_t *buffer, size_t len)
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700100{
Duncan Laurie40ae1702016-08-31 13:51:14 -0700101 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800102 int rc;
103 int count;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700104
Duncan Laurie40ae1702016-08-31 13:51:14 -0700105 if (tpm_dev->addr == 0)
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800106 return -1;
Duncan Laurieffa765f2016-09-01 15:50:22 -0700107
108 switch (tpm_dev->chip_type) {
109 case SLB9635:
Duncan Laurieffa765f2016-09-01 15:50:22 -0700110 case UNKNOWN:
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800111 /* slb9635 protocol should work in both cases */
112 for (count = 0; count < MAX_COUNT; count++) {
Duncan Laurie40ae1702016-08-31 13:51:14 -0700113 rc = i2c_write_raw(tpm_dev->bus, tpm_dev->addr,
114 &addr, 1);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800115 if (rc == 0)
116 break; /* success, break to skip sleep */
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700117
Duncan Laurieefa579f2016-08-31 14:48:12 -0700118 udelay(tpm_dev->sleep_short);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800119 }
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700120
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800121 if (rc)
122 return -1;
123
124 /* After the TPM has successfully received the register address
125 * it needs some time, thus we're sleeping here again, before
126 * retrieving the data
127 */
128 for (count = 0; count < MAX_COUNT; count++) {
Duncan Laurieefa579f2016-08-31 14:48:12 -0700129 udelay(tpm_dev->sleep_short);
Duncan Laurie40ae1702016-08-31 13:51:14 -0700130 rc = i2c_read_raw(tpm_dev->bus, tpm_dev->addr,
Gabe Blackcdb61a62014-04-07 18:45:14 -0700131 buffer, len);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800132 if (rc == 0)
133 break; /* success, break to skip sleep */
134
135 }
Duncan Laurieffa765f2016-09-01 15:50:22 -0700136 break;
137
138 default:
139 {
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800140 /* use a combined read for newer chips
141 * unfortunately the smbus functions are not suitable due to
142 * the 32 byte limit of the smbus.
143 * retries should usually not be needed, but are kept just to
144 * be safe on the safe side.
145 */
Duncan Laurie40ae1702016-08-31 13:51:14 -0700146 struct i2c_seg aseg = { .read = 0, .chip = tpm_dev->addr,
Gabe Blackcdb61a62014-04-07 18:45:14 -0700147 .buf = &addr, .len = 1 };
Duncan Laurie40ae1702016-08-31 13:51:14 -0700148 struct i2c_seg dseg = { .read = 1, .chip = tpm_dev->addr,
Gabe Blackcdb61a62014-04-07 18:45:14 -0700149 .buf = buffer, .len = len };
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800150 for (count = 0; count < MAX_COUNT; count++) {
Duncan Laurie40ae1702016-08-31 13:51:14 -0700151 rc = i2c_transfer(tpm_dev->bus, &aseg, 1) ||
152 i2c_transfer(tpm_dev->bus, &dseg, 1);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800153 if (rc == 0)
154 break; /* break here to skip sleep */
Duncan Laurieefa579f2016-08-31 14:48:12 -0700155 udelay(tpm_dev->sleep_short);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800156 }
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700157 }
Duncan Laurieffa765f2016-09-01 15:50:22 -0700158 }
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700159
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800160 /* take care of 'guard time' */
Duncan Laurieefa579f2016-08-31 14:48:12 -0700161 udelay(tpm_dev->sleep_short);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800162 if (rc)
163 return -1;
164
165 return 0;
166}
167
168static int iic_tpm_write_generic(uint8_t addr, uint8_t *buffer, size_t len,
169 unsigned int sleep_time,
170 uint8_t max_count)
171{
Duncan Laurie40ae1702016-08-31 13:51:14 -0700172 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800173 int rc = 0;
174 int count;
175
176 if (len > TPM_BUFSIZE) {
Furquan Shaikh251eef12014-07-22 11:12:15 -0700177 printk(BIOS_DEBUG, "%s: Length %zd is too large\n", __func__, len);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800178 return -1;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700179 }
180
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800181 /* prepare send buffer */
Duncan Laurie40ae1702016-08-31 13:51:14 -0700182 tpm_dev->buf[0] = addr;
183 memcpy(&(tpm_dev->buf[1]), buffer, len);
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700184
Duncan Laurie40ae1702016-08-31 13:51:14 -0700185 if (tpm_dev->addr == 0)
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800186 return -1;
187 for (count = 0; count < max_count; count++) {
Duncan Laurie40ae1702016-08-31 13:51:14 -0700188 rc = i2c_write_raw(tpm_dev->bus, tpm_dev->addr,
189 tpm_dev->buf, len + 1);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800190 if (rc == 0)
191 break; /* success, break to skip sleep */
192
193 udelay(sleep_time);
194 }
195
196 /* take care of 'guard time' */
Duncan Laurieefa579f2016-08-31 14:48:12 -0700197 udelay(tpm_dev->sleep_short);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800198 if (rc)
199 return -1;
200
201 return 0;
202}
203
204/*
205 * iic_tpm_write() - write to TPM register
206 * @addr: register address to write to
207 * @buffer: containing data to be written
208 * @len: number of bytes to write
209 *
210 * Write len bytes from provided buffer to TPM register (little
211 * endian format, i.e. buffer[0] is written as first byte).
212 *
213 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
214 * values have to be swapped.
215 *
216 * NOTE: use this function instead of the iic_tpm_write_generic function.
217 *
218 * Return -EIO on error, 0 on success
219 */
220static int iic_tpm_write(uint8_t addr, uint8_t *buffer, size_t len)
221{
Duncan Laurieefa579f2016-08-31 14:48:12 -0700222 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
223 return iic_tpm_write_generic(addr, buffer, len, tpm_dev->sleep_short,
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800224 MAX_COUNT);
225}
226
227/*
228 * This function is needed especially for the cleanup situation after
229 * sending TPM_READY
230 * */
231static int iic_tpm_write_long(uint8_t addr, uint8_t *buffer, size_t len)
232{
Duncan Laurieefa579f2016-08-31 14:48:12 -0700233 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
234 return iic_tpm_write_generic(addr, buffer, len, tpm_dev->sleep_long,
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800235 MAX_COUNT_LONG);
236}
237
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800238static int check_locality(struct tpm_chip *chip, int loc)
239{
240 uint8_t buf;
241
242 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
243 return -1;
244
245 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
246 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
247 chip->vendor.locality = loc;
248 return loc;
249 }
250
251 return -1;
252}
253
254static void release_locality(struct tpm_chip *chip, int loc, int force)
255{
256 uint8_t buf;
257 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
258 return;
259
260 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
261 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
262 buf = TPM_ACCESS_ACTIVE_LOCALITY;
263 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
264 }
265}
266
267static int request_locality(struct tpm_chip *chip, int loc)
268{
269 uint8_t buf = TPM_ACCESS_REQUEST_USE;
270
271 if (check_locality(chip, loc) >= 0)
272 return loc; /* we already have the locality */
273
274 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
275
276 /* wait for burstcount */
277 int timeout = 2 * 1000; /* 2s timeout */
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700278 while (timeout) {
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800279 if (check_locality(chip, loc) >= 0)
280 return loc;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700281 mdelay(TPM_TIMEOUT);
282 timeout--;
283 }
284
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800285 return -1;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700286}
287
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800288static uint8_t tpm_tis_i2c_status(struct tpm_chip *chip)
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700289{
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800290 /* NOTE: Since I2C read may fail, return 0 in this case --> time-out */
291 uint8_t buf;
292 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
293 return 0;
Julius Wernera69ac782016-11-30 17:46:17 -0800294 else if (buf == 0xff) /* Some TPMs sometimes randomly return 0xff. */
295 return 0;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800296 else
297 return buf;
298}
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700299
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800300static void tpm_tis_i2c_ready(struct tpm_chip *chip)
301{
302 /* this causes the current command to be aborted */
303 uint8_t buf = TPM_STS_COMMAND_READY;
304 iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
305}
306
307static ssize_t get_burstcount(struct tpm_chip *chip)
308{
309 ssize_t burstcnt;
310 uint8_t buf[3];
311
312 /* wait for burstcount */
313 int timeout = 2 * 1000; /* 2s timeout */
314 while (timeout) {
315 /* Note: STS is little endian */
316 if (iic_tpm_read(TPM_STS(chip->vendor.locality) + 1, buf, 3) < 0)
317 burstcnt = 0;
318 else
319 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
320
Julius Wernera69ac782016-11-30 17:46:17 -0800321 if (burstcnt && burstcnt != 0xffffff)
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800322 return burstcnt;
323 mdelay(TPM_TIMEOUT);
324 timeout--;
325 }
326 return -1;
327}
328
329static int wait_for_stat(struct tpm_chip *chip, uint8_t mask, int *status)
330{
331 unsigned long timeout = 2 * 1024;
332 while (timeout) {
333 *status = tpm_tis_i2c_status(chip);
334 if ((*status & mask) == mask)
335 return 0;
336 mdelay(TPM_TIMEOUT);
337 timeout--;
338 }
339
340 return -1;
341}
342
343static int recv_data(struct tpm_chip *chip, uint8_t *buf, size_t count)
344{
345 size_t size = 0;
346
347 while (size < count) {
348 ssize_t burstcnt = get_burstcount(chip);
349 int rc;
350
351 /* burstcount < 0 = TPM is busy */
352 if (burstcnt < 0)
353 return burstcnt;
354
355 /* limit received data to max. left */
356 if (burstcnt > (count - size))
357 burstcnt = count - size;
358
359 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
360 &(buf[size]),
361 burstcnt);
362 if (rc == 0)
363 size += burstcnt;
364
365 }
366 return size;
367}
368
369static int tpm_tis_i2c_recv(struct tpm_chip *chip, uint8_t *buf, size_t count)
370{
371 int size = 0;
372 uint32_t expected;
373 int status;
374
375 if (count < TPM_HEADER_SIZE) {
376 size = -1;
377 goto out;
378 }
379
380 /* read first 10 bytes, including tag, paramsize, and result */
381 size = recv_data(chip, buf, TPM_HEADER_SIZE);
382 if (size < TPM_HEADER_SIZE) {
383 printk(BIOS_DEBUG, "tpm_tis_i2c_recv: Unable to read header\n");
384 goto out;
385 }
386
387 memcpy(&expected, buf + TPM_RSP_SIZE_BYTE, sizeof(expected));
388 expected = be32_to_cpu(expected);
389 if ((size_t)expected > count) {
390 size = -1;
391 goto out;
392 }
393
394 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
395 expected - TPM_HEADER_SIZE);
396 if (size < expected) {
397 printk(BIOS_DEBUG, "tpm_tis_i2c_recv: Unable to "
398 "read remainder of result\n");
399 size = -1;
400 goto out;
401 }
402
403 wait_for_stat(chip, TPM_STS_VALID, &status);
404 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
405 printk(BIOS_DEBUG, "tpm_tis_i2c_recv: Error left over data\n");
406 size = -1;
407 goto out;
408 }
409
410out:
411 tpm_tis_i2c_ready(chip);
412
413 return size;
414}
415
416static int tpm_tis_i2c_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
417{
418 int status;
419 size_t count = 0;
420 uint8_t sts = TPM_STS_GO;
421
422 if (len > TPM_BUFSIZE)
423 return -1; /* command is too long for our TPM, sorry */
424
425 status = tpm_tis_i2c_status(chip);
426 if ((status & TPM_STS_COMMAND_READY) == 0) {
427 tpm_tis_i2c_ready(chip);
428 if (wait_for_stat(chip, TPM_STS_COMMAND_READY, &status) < 0)
429 goto out_err;
430 }
431
432 while (count < len - 1) {
433 ssize_t burstcnt = get_burstcount(chip);
434
435 /* burstcount < 0 = TPM is busy */
436 if (burstcnt < 0)
437 return burstcnt;
438
439 if (burstcnt > (len-1-count))
440 burstcnt = len-1-count;
441
442#ifdef CONFIG_TPM_I2C_BURST_LIMITATION
443 if (burstcnt > CONFIG_TPM_I2C_BURST_LIMITATION)
444 burstcnt = CONFIG_TPM_I2C_BURST_LIMITATION;
445#endif /* CONFIG_TPM_I2C_BURST_LIMITATION */
446
447 if (iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
448 &(buf[count]), burstcnt) == 0)
449 count += burstcnt;
450
451 wait_for_stat(chip, TPM_STS_VALID, &status);
452 if ((status & TPM_STS_DATA_EXPECT) == 0)
453 goto out_err;
454 }
455
456 /* write last byte */
457 iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
458
459 wait_for_stat(chip, TPM_STS_VALID, &status);
460 if ((status & TPM_STS_DATA_EXPECT) != 0)
461 goto out_err;
462
463 /* go and do it */
464 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
465
466 return len;
467
468out_err:
469 tpm_tis_i2c_ready(chip);
470
471 return -1;
472}
473
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800474/* Initialization of I2C TPM */
475
Duncan Laurie1ca19682016-09-07 10:52:12 -0700476int tpm_vendor_probe(unsigned bus, uint32_t addr)
477{
478 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
479 struct stopwatch sw;
480 uint8_t buf = 0;
481 int ret;
482 long sw_run_duration = SLEEP_DURATION_PROBE_MS;
483
484 tpm_dev->chip_type = UNKNOWN;
485 tpm_dev->bus = bus;
486 tpm_dev->addr = addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700487 tpm_dev->sleep_short = SLEEP_DURATION;
488 tpm_dev->sleep_long = SLEEP_DURATION_LONG;
Duncan Laurie1ca19682016-09-07 10:52:12 -0700489
490 /*
491 * Probe TPM. Check if the TPM_ACCESS register's ValidSts bit is set(1)
492 * If the bit remains clear(0) then claim that init has failed.
493 */
494 stopwatch_init_msecs_expire(&sw, sw_run_duration);
495 do {
496 ret = iic_tpm_read(TPM_ACCESS(0), &buf, 1);
497 if (!ret && (buf & TPM_STS_VALID)) {
498 sw_run_duration = stopwatch_duration_msecs(&sw);
499 break;
500 }
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700501 udelay(SLEEP_DURATION);
Duncan Laurie1ca19682016-09-07 10:52:12 -0700502 } while (!stopwatch_expired(&sw));
503
504 printk(BIOS_INFO,
505 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
506 __func__, (buf & TPM_STS_VALID) ? "set" : "clear",
507 (buf & TPM_STS_VALID) >> 7, sw_run_duration);
508
509 /*
510 * Claim failure if the ValidSts (bit 7) is clear.
511 */
512 if (!(buf & TPM_STS_VALID))
513 return -1;
514
515 return 0;
516}
517
Duncan Laurie40ae1702016-08-31 13:51:14 -0700518int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr)
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800519{
Duncan Laurie40ae1702016-08-31 13:51:14 -0700520 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800521 uint32_t vendor;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800522
Duncan Laurie40ae1702016-08-31 13:51:14 -0700523 if (dev_addr == 0) {
524 printk(BIOS_ERR, "%s: missing device address\n", __func__);
525 return -1;
526 }
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800527
Duncan Lauriefbce31a2016-09-12 11:26:45 -0700528 tpm_dev->chip_type = UNKNOWN;
Duncan Laurie40ae1702016-08-31 13:51:14 -0700529 tpm_dev->bus = bus;
530 tpm_dev->addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700531 tpm_dev->sleep_short = SLEEP_DURATION;
532 tpm_dev->sleep_long = SLEEP_DURATION_LONG;
Duncan Laurieefa579f2016-08-31 14:48:12 -0700533
Duncan Laurie40ae1702016-08-31 13:51:14 -0700534 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700535 chip->is_open = 1;
536
Duncan Laurie40ae1702016-08-31 13:51:14 -0700537 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
538 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
539 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700540 chip->vendor.status = &tpm_tis_i2c_status;
541 chip->vendor.recv = &tpm_tis_i2c_recv;
542 chip->vendor.send = &tpm_tis_i2c_send;
543 chip->vendor.cancel = &tpm_tis_i2c_ready;
Duncan Laurie40ae1702016-08-31 13:51:14 -0700544
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800545 /* Disable interrupts (not supported) */
546 chip->vendor.irq = 0;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700547
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800548 if (request_locality(chip, 0) != 0)
Duncan Laurie40ae1702016-08-31 13:51:14 -0700549 return -1;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700550
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800551 /* Read four bytes from DID_VID register */
552 if (iic_tpm_read(TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie40ae1702016-08-31 13:51:14 -0700553 goto out_err;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800554
555 if (vendor == TPM_TIS_I2C_DID_VID_9645) {
Duncan Laurie40ae1702016-08-31 13:51:14 -0700556 tpm_dev->chip_type = SLB9645;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800557 } else if (be32_to_cpu(vendor) == TPM_TIS_I2C_DID_VID_9635) {
Duncan Laurie40ae1702016-08-31 13:51:14 -0700558 tpm_dev->chip_type = SLB9635;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800559 } else {
560 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized.\n", vendor);
Duncan Laurie40ae1702016-08-31 13:51:14 -0700561 goto out_err;
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700562 }
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800563
Duncan Laurieffa765f2016-09-01 15:50:22 -0700564 printk(BIOS_DEBUG, "I2C TPM %u:%02x (chip type %s device-id 0x%X)\n",
565 tpm_dev->bus, tpm_dev->addr,
566 chip_name[tpm_dev->chip_type], vendor >> 16);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800567
568 /*
569 * A timeout query to TPM can be placed here.
570 * Standard timeout values are used so far
571 */
572
573 return 0;
574
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800575out_err:
Duncan Laurie40ae1702016-08-31 13:51:14 -0700576 release_locality(chip, 0, 1);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800577 return -1;
578}
579
580void tpm_vendor_cleanup(struct tpm_chip *chip)
581{
582 release_locality(chip, chip->vendor.locality, 1);
Stefan Reinauer7cb01e02013-08-29 16:05:02 -0700583}