blob: e547f0ad9f311db9074e16b145244ee242562bbb [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>
Masahiro Yamadaafc366f2014-11-26 16:00:58 +090026#include <linux/compiler.h>
Rong Changf6267992013-04-12 10:44:57 +000027#include <i2c.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000028#include <tpm.h>
29#include <asm-generic/errno.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
Simon Glass4cd7b782015-08-22 18:31:22 -060033#include "tpm_tis_i2c.h"
Rong Changf6267992013-04-12 10:44:57 +000034
Vincent Palatinec34fa52013-04-12 11:04:36 +000035DECLARE_GLOBAL_DATA_PTR;
36
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000037/* Max buffer size supported by our tpm */
38#define TPM_DEV_BUFSIZE 1260
Rong Changf6267992013-04-12 10:44:57 +000039
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000040/* Max number of iterations after i2c NAK */
41#define MAX_COUNT 3
42
43/*
44 * Max number of iterations after i2c NAK for 'long' commands
45 *
46 * We need this especially for sending TPM_READY, since the cleanup after the
Rong Changf6267992013-04-12 10:44:57 +000047 * transtion to the ready state may take some time, but it is unpredictable
48 * how long it will take.
49 */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000050#define MAX_COUNT_LONG 50
Rong Changf6267992013-04-12 10:44:57 +000051
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000052#define SLEEP_DURATION 60 /* in usec */
53#define SLEEP_DURATION_LONG 210 /* in usec */
54
55#define TPM_HEADER_SIZE 10
56
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000057enum tis_access {
58 TPM_ACCESS_VALID = 0x80,
59 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
60 TPM_ACCESS_REQUEST_PENDING = 0x04,
61 TPM_ACCESS_REQUEST_USE = 0x02,
62};
63
64enum tis_status {
65 TPM_STS_VALID = 0x80,
66 TPM_STS_COMMAND_READY = 0x40,
67 TPM_STS_GO = 0x20,
68 TPM_STS_DATA_AVAIL = 0x10,
69 TPM_STS_DATA_EXPECT = 0x08,
70};
71
72enum tis_defaults {
73 TIS_SHORT_TIMEOUT = 750, /* ms */
74 TIS_LONG_TIMEOUT = 2000, /* ms */
75};
Rong Changf6267992013-04-12 10:44:57 +000076
77/* expected value for DIDVID register */
Vincent Palatinec34fa52013-04-12 11:04:36 +000078#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
79#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
80
81enum i2c_chip_type {
82 SLB9635,
83 SLB9645,
84 UNKNOWN,
85};
86
87static const char * const chip_name[] = {
88 [SLB9635] = "slb9635tt",
89 [SLB9645] = "slb9645tt",
90 [UNKNOWN] = "unknown/fallback to slb9635",
91};
Rong Changf6267992013-04-12 10:44:57 +000092
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000093#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
94#define TPM_STS(l) (0x0001 | ((l) << 4))
95#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
96#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
97
Simon Glass4cd7b782015-08-22 18:31:22 -060098enum tpm_duration {
99 TPM_SHORT = 0,
100 TPM_MEDIUM = 1,
101 TPM_LONG = 2,
102 TPM_UNDEFINED,
103};
104
105/* Extended error numbers from linux (see errno.h) */
106#define ECANCELED 125 /* Operation Canceled */
107
108/* Timer frequency. Corresponds to msec timer resolution*/
109#define HZ 1000
110
111#define TPM_MAX_ORDINAL 243
112#define TPM_MAX_PROTECTED_ORDINAL 12
113#define TPM_PROTECTED_ORDINAL_MASK 0xFF
114
115#define TPM_CMD_COUNT_BYTE 2
116#define TPM_CMD_ORDINAL_BYTE 6
117
118/*
119 * Array with one entry per ordinal defining the maximum amount
120 * of time the chip could take to return the result. The ordinal
121 * designation of short, medium or long is defined in a table in
122 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
123 * values of the SHORT, MEDIUM, and LONG durations are retrieved
124 * from the chip during initialization with a call to tpm_get_timeouts.
125 */
126static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
127 TPM_UNDEFINED, /* 0 */
128 TPM_UNDEFINED,
129 TPM_UNDEFINED,
130 TPM_UNDEFINED,
131 TPM_UNDEFINED,
132 TPM_UNDEFINED, /* 5 */
133 TPM_UNDEFINED,
134 TPM_UNDEFINED,
135 TPM_UNDEFINED,
136 TPM_UNDEFINED,
137 TPM_SHORT, /* 10 */
138 TPM_SHORT,
139};
140
141static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
142 TPM_UNDEFINED, /* 0 */
143 TPM_UNDEFINED,
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_UNDEFINED,
147 TPM_UNDEFINED, /* 5 */
148 TPM_UNDEFINED,
149 TPM_UNDEFINED,
150 TPM_UNDEFINED,
151 TPM_UNDEFINED,
152 TPM_SHORT, /* 10 */
153 TPM_SHORT,
154 TPM_MEDIUM,
155 TPM_LONG,
156 TPM_LONG,
157 TPM_MEDIUM, /* 15 */
158 TPM_SHORT,
159 TPM_SHORT,
160 TPM_MEDIUM,
161 TPM_LONG,
162 TPM_SHORT, /* 20 */
163 TPM_SHORT,
164 TPM_MEDIUM,
165 TPM_MEDIUM,
166 TPM_MEDIUM,
167 TPM_SHORT, /* 25 */
168 TPM_SHORT,
169 TPM_MEDIUM,
170 TPM_SHORT,
171 TPM_SHORT,
172 TPM_MEDIUM, /* 30 */
173 TPM_LONG,
174 TPM_MEDIUM,
175 TPM_SHORT,
176 TPM_SHORT,
177 TPM_SHORT, /* 35 */
178 TPM_MEDIUM,
179 TPM_MEDIUM,
180 TPM_UNDEFINED,
181 TPM_UNDEFINED,
182 TPM_MEDIUM, /* 40 */
183 TPM_LONG,
184 TPM_MEDIUM,
185 TPM_SHORT,
186 TPM_SHORT,
187 TPM_SHORT, /* 45 */
188 TPM_SHORT,
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_LONG,
192 TPM_MEDIUM, /* 50 */
193 TPM_MEDIUM,
194 TPM_UNDEFINED,
195 TPM_UNDEFINED,
196 TPM_UNDEFINED,
197 TPM_UNDEFINED, /* 55 */
198 TPM_UNDEFINED,
199 TPM_UNDEFINED,
200 TPM_UNDEFINED,
201 TPM_UNDEFINED,
202 TPM_MEDIUM, /* 60 */
203 TPM_MEDIUM,
204 TPM_MEDIUM,
205 TPM_SHORT,
206 TPM_SHORT,
207 TPM_MEDIUM, /* 65 */
208 TPM_UNDEFINED,
209 TPM_UNDEFINED,
210 TPM_UNDEFINED,
211 TPM_UNDEFINED,
212 TPM_SHORT, /* 70 */
213 TPM_SHORT,
214 TPM_UNDEFINED,
215 TPM_UNDEFINED,
216 TPM_UNDEFINED,
217 TPM_UNDEFINED, /* 75 */
218 TPM_UNDEFINED,
219 TPM_UNDEFINED,
220 TPM_UNDEFINED,
221 TPM_UNDEFINED,
222 TPM_LONG, /* 80 */
223 TPM_UNDEFINED,
224 TPM_MEDIUM,
225 TPM_LONG,
226 TPM_SHORT,
227 TPM_UNDEFINED, /* 85 */
228 TPM_UNDEFINED,
229 TPM_UNDEFINED,
230 TPM_UNDEFINED,
231 TPM_UNDEFINED,
232 TPM_SHORT, /* 90 */
233 TPM_SHORT,
234 TPM_SHORT,
235 TPM_SHORT,
236 TPM_SHORT,
237 TPM_UNDEFINED, /* 95 */
238 TPM_UNDEFINED,
239 TPM_UNDEFINED,
240 TPM_UNDEFINED,
241 TPM_UNDEFINED,
242 TPM_MEDIUM, /* 100 */
243 TPM_SHORT,
244 TPM_SHORT,
245 TPM_UNDEFINED,
246 TPM_UNDEFINED,
247 TPM_UNDEFINED, /* 105 */
248 TPM_UNDEFINED,
249 TPM_UNDEFINED,
250 TPM_UNDEFINED,
251 TPM_UNDEFINED,
252 TPM_SHORT, /* 110 */
253 TPM_SHORT,
254 TPM_SHORT,
255 TPM_SHORT,
256 TPM_SHORT,
257 TPM_SHORT, /* 115 */
258 TPM_SHORT,
259 TPM_SHORT,
260 TPM_UNDEFINED,
261 TPM_UNDEFINED,
262 TPM_LONG, /* 120 */
263 TPM_LONG,
264 TPM_MEDIUM,
265 TPM_UNDEFINED,
266 TPM_SHORT,
267 TPM_SHORT, /* 125 */
268 TPM_SHORT,
269 TPM_LONG,
270 TPM_SHORT,
271 TPM_SHORT,
272 TPM_SHORT, /* 130 */
273 TPM_MEDIUM,
274 TPM_UNDEFINED,
275 TPM_SHORT,
276 TPM_MEDIUM,
277 TPM_UNDEFINED, /* 135 */
278 TPM_UNDEFINED,
279 TPM_UNDEFINED,
280 TPM_UNDEFINED,
281 TPM_UNDEFINED,
282 TPM_SHORT, /* 140 */
283 TPM_SHORT,
284 TPM_UNDEFINED,
285 TPM_UNDEFINED,
286 TPM_UNDEFINED,
287 TPM_UNDEFINED, /* 145 */
288 TPM_UNDEFINED,
289 TPM_UNDEFINED,
290 TPM_UNDEFINED,
291 TPM_UNDEFINED,
292 TPM_SHORT, /* 150 */
293 TPM_MEDIUM,
294 TPM_MEDIUM,
295 TPM_SHORT,
296 TPM_SHORT,
297 TPM_UNDEFINED, /* 155 */
298 TPM_UNDEFINED,
299 TPM_UNDEFINED,
300 TPM_UNDEFINED,
301 TPM_UNDEFINED,
302 TPM_SHORT, /* 160 */
303 TPM_SHORT,
304 TPM_SHORT,
305 TPM_SHORT,
306 TPM_UNDEFINED,
307 TPM_UNDEFINED, /* 165 */
308 TPM_UNDEFINED,
309 TPM_UNDEFINED,
310 TPM_UNDEFINED,
311 TPM_UNDEFINED,
312 TPM_LONG, /* 170 */
313 TPM_UNDEFINED,
314 TPM_UNDEFINED,
315 TPM_UNDEFINED,
316 TPM_UNDEFINED,
317 TPM_UNDEFINED, /* 175 */
318 TPM_UNDEFINED,
319 TPM_UNDEFINED,
320 TPM_UNDEFINED,
321 TPM_UNDEFINED,
322 TPM_MEDIUM, /* 180 */
323 TPM_SHORT,
324 TPM_MEDIUM,
325 TPM_MEDIUM,
326 TPM_MEDIUM,
327 TPM_MEDIUM, /* 185 */
328 TPM_SHORT,
329 TPM_UNDEFINED,
330 TPM_UNDEFINED,
331 TPM_UNDEFINED,
332 TPM_UNDEFINED, /* 190 */
333 TPM_UNDEFINED,
334 TPM_UNDEFINED,
335 TPM_UNDEFINED,
336 TPM_UNDEFINED,
337 TPM_UNDEFINED, /* 195 */
338 TPM_UNDEFINED,
339 TPM_UNDEFINED,
340 TPM_UNDEFINED,
341 TPM_UNDEFINED,
342 TPM_SHORT, /* 200 */
343 TPM_UNDEFINED,
344 TPM_UNDEFINED,
345 TPM_UNDEFINED,
346 TPM_SHORT,
347 TPM_SHORT, /* 205 */
348 TPM_SHORT,
349 TPM_SHORT,
350 TPM_SHORT,
351 TPM_SHORT,
352 TPM_MEDIUM, /* 210 */
353 TPM_UNDEFINED,
354 TPM_MEDIUM,
355 TPM_MEDIUM,
356 TPM_MEDIUM,
357 TPM_UNDEFINED, /* 215 */
358 TPM_MEDIUM,
359 TPM_UNDEFINED,
360 TPM_UNDEFINED,
361 TPM_SHORT,
362 TPM_SHORT, /* 220 */
363 TPM_SHORT,
364 TPM_SHORT,
365 TPM_SHORT,
366 TPM_SHORT,
367 TPM_UNDEFINED, /* 225 */
368 TPM_UNDEFINED,
369 TPM_UNDEFINED,
370 TPM_UNDEFINED,
371 TPM_UNDEFINED,
372 TPM_SHORT, /* 230 */
373 TPM_LONG,
374 TPM_MEDIUM,
375 TPM_UNDEFINED,
376 TPM_UNDEFINED,
377 TPM_UNDEFINED, /* 235 */
378 TPM_UNDEFINED,
379 TPM_UNDEFINED,
380 TPM_UNDEFINED,
381 TPM_UNDEFINED,
382 TPM_SHORT, /* 240 */
383 TPM_UNDEFINED,
384 TPM_MEDIUM,
385};
386
387/* TPM configuration */
388struct tpm {
389 struct udevice *dev;
390 char inited;
391} tpm;
392
393/* Global structure for tpm chip data */
394static struct tpm_chip g_chip;
395
Rong Changf6267992013-04-12 10:44:57 +0000396/* Structure to store I2C TPM specific stuff */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000397struct tpm_dev {
Simon Glassf90acf12015-05-04 11:30:59 -0600398 struct udevice *dev;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000399 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000400 enum i2c_chip_type chip_type;
Rong Changf6267992013-04-12 10:44:57 +0000401};
402
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000403static struct tpm_dev tpm_dev;
404
Rong Changf6267992013-04-12 10:44:57 +0000405/*
406 * iic_tpm_read() - read from TPM register
407 * @addr: register address to read from
408 * @buffer: provided by caller
409 * @len: number of bytes to read
410 *
411 * Read len bytes from TPM register and put them into
412 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
413 *
414 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
415 * values have to be swapped.
416 *
417 * Return -EIO on error, 0 on success.
418 */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000419static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
Rong Changf6267992013-04-12 10:44:57 +0000420{
421 int rc;
422 int count;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000423 uint32_t addrbuf = addr;
Rong Changf6267992013-04-12 10:44:57 +0000424
Vincent Palatinec34fa52013-04-12 11:04:36 +0000425 if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
426 /* slb9635 protocol should work in both cases */
427 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassf90acf12015-05-04 11:30:59 -0600428 rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000429 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000430 break; /* Success, break to skip sleep */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000431 udelay(SLEEP_DURATION);
432 }
Vincent Palatinec34fa52013-04-12 11:04:36 +0000433 if (rc)
434 return -rc;
435
436 /* After the TPM has successfully received the register address
437 * it needs some time, thus we're sleeping here again, before
438 * retrieving the data
439 */
440 for (count = 0; count < MAX_COUNT; count++) {
441 udelay(SLEEP_DURATION);
Simon Glassf90acf12015-05-04 11:30:59 -0600442 rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000443 if (rc == 0)
444 break; /* success, break to skip sleep */
445 }
446 } else {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000447 /*
448 * Use a combined read for newer chips.
449 * Unfortunately the smbus functions are not suitable due to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000450 * the 32 byte limit of the smbus.
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000451 * Retries should usually not be needed, but are kept just to
Vincent Palatinec34fa52013-04-12 11:04:36 +0000452 * be safe on the safe side.
453 */
454 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassf90acf12015-05-04 11:30:59 -0600455 rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
Vincent Palatinec34fa52013-04-12 11:04:36 +0000456 if (rc == 0)
457 break; /* break here to skip sleep */
458 udelay(SLEEP_DURATION);
459 }
Rong Changf6267992013-04-12 10:44:57 +0000460 }
461
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000462 /* Take care of 'guard time' */
Vincent Palatinec34fa52013-04-12 11:04:36 +0000463 udelay(SLEEP_DURATION);
Rong Changf6267992013-04-12 10:44:57 +0000464 if (rc)
465 return -rc;
466
467 return 0;
468}
469
470static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000471 unsigned int sleep_time, u8 max_count)
Rong Changf6267992013-04-12 10:44:57 +0000472{
473 int rc = 0;
474 int count;
475
Rong Changf6267992013-04-12 10:44:57 +0000476 for (count = 0; count < max_count; count++) {
Simon Glassf90acf12015-05-04 11:30:59 -0600477 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
Rong Changf6267992013-04-12 10:44:57 +0000478 if (rc == 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000479 break; /* Success, break to skip sleep */
Rong Changf6267992013-04-12 10:44:57 +0000480 udelay(sleep_time);
481 }
482
Vincent Palatinec34fa52013-04-12 11:04:36 +0000483 /* take care of 'guard time' */
Simon Glassf90acf12015-05-04 11:30:59 -0600484 udelay(sleep_time);
Rong Changf6267992013-04-12 10:44:57 +0000485 if (rc)
486 return -rc;
487
488 return 0;
489}
490
491/*
492 * iic_tpm_write() - write to TPM register
493 * @addr: register address to write to
494 * @buffer: containing data to be written
495 * @len: number of bytes to write
496 *
497 * Write len bytes from provided buffer to TPM register (little
498 * endian format, i.e. buffer[0] is written as first byte).
499 *
500 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
501 * values have to be swapped.
502 *
503 * NOTE: use this function instead of the iic_tpm_write_generic function.
504 *
505 * Return -EIO on error, 0 on success
506 */
507static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
508{
509 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
510 MAX_COUNT);
511}
512
513/*
514 * This function is needed especially for the cleanup situation after
515 * sending TPM_READY
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000516 */
Rong Changf6267992013-04-12 10:44:57 +0000517static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
518{
519 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
520 MAX_COUNT_LONG);
521}
522
Rong Changf6267992013-04-12 10:44:57 +0000523static int check_locality(struct tpm_chip *chip, int loc)
524{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000525 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000526 u8 buf;
527 int rc;
528
529 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
530 if (rc < 0)
531 return rc;
532
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000533 if ((buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000534 chip->vendor.locality = loc;
535 return loc;
536 }
537
538 return -1;
539}
540
541static void release_locality(struct tpm_chip *chip, int loc, int force)
542{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000543 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Changf6267992013-04-12 10:44:57 +0000544 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000545
Rong Changf6267992013-04-12 10:44:57 +0000546 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
547 return;
548
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000549 if (force || (buf & mask) == mask) {
Rong Changf6267992013-04-12 10:44:57 +0000550 buf = TPM_ACCESS_ACTIVE_LOCALITY;
551 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
552 }
553}
554
555static int request_locality(struct tpm_chip *chip, int loc)
556{
557 unsigned long start, stop;
558 u8 buf = TPM_ACCESS_REQUEST_USE;
Simon Glassf90acf12015-05-04 11:30:59 -0600559 int rc;
Rong Changf6267992013-04-12 10:44:57 +0000560
561 if (check_locality(chip, loc) >= 0)
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000562 return loc; /* We already have the locality */
Rong Changf6267992013-04-12 10:44:57 +0000563
Simon Glassf90acf12015-05-04 11:30:59 -0600564 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
565 if (rc)
566 return rc;
Rong Changf6267992013-04-12 10:44:57 +0000567
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000568 /* Wait for burstcount */
Rong Changf6267992013-04-12 10:44:57 +0000569 start = get_timer(0);
570 stop = chip->vendor.timeout_a;
571 do {
572 if (check_locality(chip, loc) >= 0)
573 return loc;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000574 udelay(TPM_TIMEOUT * 1000);
Rong Changf6267992013-04-12 10:44:57 +0000575 } while (get_timer(start) < stop);
576
577 return -1;
578}
579
580static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
581{
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000582 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Changf6267992013-04-12 10:44:57 +0000583 u8 buf;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000584
Rong Changf6267992013-04-12 10:44:57 +0000585 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
586 return 0;
587 else
588 return buf;
589}
590
591static void tpm_tis_i2c_ready(struct tpm_chip *chip)
592{
Simon Glassf90acf12015-05-04 11:30:59 -0600593 int rc;
594
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000595 /* This causes the current command to be aborted */
Rong Changf6267992013-04-12 10:44:57 +0000596 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000597
Simon Glassf90acf12015-05-04 11:30:59 -0600598 debug("%s\n", __func__);
599 rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
600 if (rc)
601 debug("%s: rc=%d\n", __func__, rc);
Rong Changf6267992013-04-12 10:44:57 +0000602}
603
604static ssize_t get_burstcount(struct tpm_chip *chip)
605{
606 unsigned long start, stop;
607 ssize_t burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000608 u8 addr, buf[3];
Rong Changf6267992013-04-12 10:44:57 +0000609
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000610 /* Wait for burstcount */
611 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Changf6267992013-04-12 10:44:57 +0000612 start = get_timer(0);
613 stop = chip->vendor.timeout_d;
614 do {
615 /* Note: STS is little endian */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000616 addr = TPM_STS(chip->vendor.locality) + 1;
617 if (iic_tpm_read(addr, buf, 3) < 0)
Rong Changf6267992013-04-12 10:44:57 +0000618 burstcnt = 0;
619 else
620 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
621
622 if (burstcnt)
623 return burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000624 udelay(TPM_TIMEOUT * 1000);
Rong Changf6267992013-04-12 10:44:57 +0000625 } while (get_timer(start) < stop);
626
627 return -EBUSY;
628}
629
630static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000631 int *status)
Rong Changf6267992013-04-12 10:44:57 +0000632{
633 unsigned long start, stop;
634
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000635 /* Check current status */
Rong Changf6267992013-04-12 10:44:57 +0000636 *status = tpm_tis_i2c_status(chip);
637 if ((*status & mask) == mask)
638 return 0;
639
640 start = get_timer(0);
641 stop = timeout;
642 do {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000643 udelay(TPM_TIMEOUT * 1000);
Rong Changf6267992013-04-12 10:44:57 +0000644 *status = tpm_tis_i2c_status(chip);
645 if ((*status & mask) == mask)
646 return 0;
Rong Changf6267992013-04-12 10:44:57 +0000647 } while (get_timer(start) < stop);
648
649 return -ETIME;
650}
651
652static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
653{
654 size_t size = 0;
655 ssize_t burstcnt;
656 int rc;
657
658 while (size < count) {
659 burstcnt = get_burstcount(chip);
660
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000661 /* burstcount < 0 -> tpm is busy */
Rong Changf6267992013-04-12 10:44:57 +0000662 if (burstcnt < 0)
663 return burstcnt;
664
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000665 /* Limit received data to max left */
Rong Changf6267992013-04-12 10:44:57 +0000666 if (burstcnt > (count - size))
667 burstcnt = count - size;
668
669 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000670 &(buf[size]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000671 if (rc == 0)
672 size += burstcnt;
673 }
674
675 return size;
676}
677
678static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
679{
680 int size = 0;
681 int expected, status;
682
683 if (count < TPM_HEADER_SIZE) {
684 size = -EIO;
685 goto out;
686 }
687
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000688 /* Read first 10 bytes, including tag, paramsize, and result */
Rong Changf6267992013-04-12 10:44:57 +0000689 size = recv_data(chip, buf, TPM_HEADER_SIZE);
690 if (size < TPM_HEADER_SIZE) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000691 error("Unable to read header\n");
Rong Changf6267992013-04-12 10:44:57 +0000692 goto out;
693 }
694
695 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
696 if ((size_t)expected > count) {
Simon Glassf90acf12015-05-04 11:30:59 -0600697 error("Error size=%x, expected=%x, count=%x\n", size, expected,
698 count);
Rong Changf6267992013-04-12 10:44:57 +0000699 size = -EIO;
700 goto out;
701 }
702
703 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000704 expected - TPM_HEADER_SIZE);
Rong Changf6267992013-04-12 10:44:57 +0000705 if (size < expected) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000706 error("Unable to read remainder of result\n");
Rong Changf6267992013-04-12 10:44:57 +0000707 size = -ETIME;
708 goto out;
709 }
710
711 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000712 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
713 error("Error left over data\n");
Rong Changf6267992013-04-12 10:44:57 +0000714 size = -EIO;
715 goto out;
716 }
717
718out:
719 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000720 /*
721 * The TPM needs some time to clean up here,
Rong Changf6267992013-04-12 10:44:57 +0000722 * so we sleep rather than keeping the bus busy
723 */
724 udelay(2000);
725 release_locality(chip, chip->vendor.locality, 0);
726
727 return size;
728}
729
730static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
731{
732 int rc, status;
Simon Glassf90acf12015-05-04 11:30:59 -0600733 size_t burstcnt;
Rong Changf6267992013-04-12 10:44:57 +0000734 size_t count = 0;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000735 int retry = 0;
Rong Changf6267992013-04-12 10:44:57 +0000736 u8 sts = TPM_STS_GO;
737
Simon Glassf90acf12015-05-04 11:30:59 -0600738 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000739 if (len > TPM_DEV_BUFSIZE)
740 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Changf6267992013-04-12 10:44:57 +0000741
742 if (request_locality(chip, 0) < 0)
743 return -EBUSY;
744
745 status = tpm_tis_i2c_status(chip);
746 if ((status & TPM_STS_COMMAND_READY) == 0) {
747 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000748 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
749 chip->vendor.timeout_b, &status) < 0) {
Rong Changf6267992013-04-12 10:44:57 +0000750 rc = -ETIME;
751 goto out_err;
752 }
753 }
754
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000755 burstcnt = get_burstcount(chip);
756
757 /* burstcount < 0 -> tpm is busy */
758 if (burstcnt < 0)
759 return burstcnt;
760
Simon Glassf90acf12015-05-04 11:30:59 -0600761 while (count < len) {
762 udelay(300);
763 if (burstcnt > len - count)
764 burstcnt = len - count;
Rong Changf6267992013-04-12 10:44:57 +0000765
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000766#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
767 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
768 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
769#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
Rong Changf6267992013-04-12 10:44:57 +0000770
771 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000772 &(buf[count]), burstcnt);
Rong Changf6267992013-04-12 10:44:57 +0000773 if (rc == 0)
774 count += burstcnt;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000775 else {
Simon Glassf90acf12015-05-04 11:30:59 -0600776 debug("%s: error\n", __func__);
777 if (retry++ > 10) {
778 rc = -EIO;
779 goto out_err;
780 }
781 rc = wait_for_stat(chip, TPM_STS_VALID,
782 chip->vendor.timeout_c, &status);
783 if (rc)
784 goto out_err;
Rong Changf6267992013-04-12 10:44:57 +0000785
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000786 if ((status & TPM_STS_DATA_EXPECT) == 0) {
787 rc = -EIO;
788 goto out_err;
789 }
Rong Changf6267992013-04-12 10:44:57 +0000790 }
791 }
792
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000793 /* Go and do it */
Rong Changf6267992013-04-12 10:44:57 +0000794 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
Simon Glassf90acf12015-05-04 11:30:59 -0600795 debug("done\n");
Rong Changf6267992013-04-12 10:44:57 +0000796
797 return len;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000798
Rong Changf6267992013-04-12 10:44:57 +0000799out_err:
Simon Glassf90acf12015-05-04 11:30:59 -0600800 debug("%s: out_err\n", __func__);
Rong Changf6267992013-04-12 10:44:57 +0000801 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000802 /*
803 * The TPM needs some time to clean up here,
Rong Changf6267992013-04-12 10:44:57 +0000804 * so we sleep rather than keeping the bus busy
805 */
806 udelay(2000);
807 release_locality(chip, chip->vendor.locality, 0);
808
809 return rc;
810}
811
812static struct tpm_vendor_specific tpm_tis_i2c = {
813 .status = tpm_tis_i2c_status,
814 .recv = tpm_tis_i2c_recv,
815 .send = tpm_tis_i2c_send,
816 .cancel = tpm_tis_i2c_ready,
817 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
818 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
819 .req_canceled = TPM_STS_COMMAND_READY,
820};
821
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000822
Vincent Palatinec34fa52013-04-12 11:04:36 +0000823static enum i2c_chip_type tpm_vendor_chip_type(void)
824{
Masahiro Yamada0f925822015-08-12 07:31:55 +0900825#if CONFIG_IS_ENABLED(OF_CONTROL)
Vincent Palatinec34fa52013-04-12 11:04:36 +0000826 const void *blob = gd->fdt_blob;
827
828 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
829 return SLB9645;
830
831 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
832 return SLB9635;
833#endif
834 return UNKNOWN;
835}
836
Simon Glass3a3f8e92015-08-22 18:31:17 -0600837int tpm_vendor_init(struct udevice *dev)
Rong Changf6267992013-04-12 10:44:57 +0000838{
Simon Glassf90acf12015-05-04 11:30:59 -0600839 struct tpm_chip *chip;
Rong Changf6267992013-04-12 10:44:57 +0000840 u32 vendor;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000841 u32 expected_did_vid;
Rong Changf6267992013-04-12 10:44:57 +0000842
Simon Glass3a3f8e92015-08-22 18:31:17 -0600843 tpm_dev.dev = dev;
Vincent Palatinec34fa52013-04-12 11:04:36 +0000844 tpm_dev.chip_type = tpm_vendor_chip_type();
845
Rong Changf6267992013-04-12 10:44:57 +0000846 chip = tpm_register_hardware(&tpm_tis_i2c);
Simon Glassf90acf12015-05-04 11:30:59 -0600847 if (chip < 0)
848 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000849
850 /* Disable interrupts (not supported) */
851 chip->vendor.irq = 0;
852
853 /* Default timeouts */
854 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
855 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
856 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
857 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
858
Simon Glassf90acf12015-05-04 11:30:59 -0600859 if (request_locality(chip, 0) < 0)
860 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000861
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000862 /* Read four bytes from DID_VID register */
Rong Changf6267992013-04-12 10:44:57 +0000863 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
Simon Glassf90acf12015-05-04 11:30:59 -0600864 release_locality(chip, 0, 1);
865 return -EIO;
Rong Changf6267992013-04-12 10:44:57 +0000866 }
867
Vincent Palatinec34fa52013-04-12 11:04:36 +0000868 if (tpm_dev.chip_type == SLB9635) {
869 vendor = be32_to_cpu(vendor);
870 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
871 } else {
872 /* device id and byte order has changed for newer i2c tpms */
873 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
874 }
Rong Changf6267992013-04-12 10:44:57 +0000875
Vincent Palatinec34fa52013-04-12 11:04:36 +0000876 if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000877 error("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassf90acf12015-05-04 11:30:59 -0600878 return -ENODEV;
Rong Changf6267992013-04-12 10:44:57 +0000879 }
880
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000881 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
882 chip_name[tpm_dev.chip_type], vendor >> 16);
Rong Changf6267992013-04-12 10:44:57 +0000883
884 /*
885 * A timeout query to TPM can be placed here.
886 * Standard timeout values are used so far
887 */
888
889 return 0;
Simon Glassf90acf12015-05-04 11:30:59 -0600890}
Rong Changf6267992013-04-12 10:44:57 +0000891
Rong Changf6267992013-04-12 10:44:57 +0000892void tpm_vendor_cleanup(struct tpm_chip *chip)
893{
894 release_locality(chip, chip->vendor.locality, 1);
895}
Simon Glass4cd7b782015-08-22 18:31:22 -0600896
897/* Returns max number of milliseconds to wait */
898static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
899 u32 ordinal)
900{
901 int duration_idx = TPM_UNDEFINED;
902 int duration = 0;
903
904 if (ordinal < TPM_MAX_ORDINAL) {
905 duration_idx = tpm_ordinal_duration[ordinal];
906 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
907 TPM_MAX_PROTECTED_ORDINAL) {
908 duration_idx = tpm_protected_ordinal_duration[
909 ordinal & TPM_PROTECTED_ORDINAL_MASK];
910 }
911
912 if (duration_idx != TPM_UNDEFINED)
913 duration = chip->vendor.duration[duration_idx];
914
915 if (duration <= 0)
916 return 2 * 60 * HZ; /* Two minutes timeout */
917 else
918 return duration;
919}
920
921static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
922{
923 int rc;
924 u32 count, ordinal;
925 unsigned long start, stop;
926
927 struct tpm_chip *chip = &g_chip;
928
929 /* switch endianess: big->little */
930 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
931 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
932
933 if (count == 0) {
934 error("no data\n");
935 return -ENODATA;
936 }
937 if (count > bufsiz) {
938 error("invalid count value %x %zx\n", count, bufsiz);
939 return -E2BIG;
940 }
941
942 debug("Calling send\n");
943 rc = chip->vendor.send(chip, (u8 *)buf, count);
944 debug(" ... done calling send\n");
945 if (rc < 0) {
946 error("tpm_transmit: tpm_send: error %d\n", rc);
947 goto out;
948 }
949
950 if (chip->vendor.irq)
951 goto out_recv;
952
953 start = get_timer(0);
954 stop = tpm_calc_ordinal_duration(chip, ordinal);
955 do {
956 debug("waiting for status... %ld %ld\n", start, stop);
957 u8 status = chip->vendor.status(chip);
958 if ((status & chip->vendor.req_complete_mask) ==
959 chip->vendor.req_complete_val) {
960 debug("...got it;\n");
961 goto out_recv;
962 }
963
964 if (status == chip->vendor.req_canceled) {
965 error("Operation Canceled\n");
966 rc = -ECANCELED;
967 goto out;
968 }
969 udelay(TPM_TIMEOUT * 1000);
970 } while (get_timer(start) < stop);
971
972 chip->vendor.cancel(chip);
973 error("Operation Timed out\n");
974 rc = -ETIME;
975 goto out;
976
977out_recv:
978 debug("out_recv: reading response...\n");
979 rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
980 if (rc < 0)
981 error("tpm_transmit: tpm_recv: error %d\n", rc);
982
983out:
984 return rc;
985}
986
987static int tpm_open_dev(struct udevice *dev)
988{
989 int rc;
990
991 debug("%s: start\n", __func__);
992 if (g_chip.is_open)
993 return -EBUSY;
994 rc = tpm_vendor_init(dev);
995 if (rc < 0)
996 g_chip.is_open = 0;
997 return rc;
998}
999
1000static void tpm_close(void)
1001{
1002 if (g_chip.is_open) {
1003 tpm_vendor_cleanup(&g_chip);
1004 g_chip.is_open = 0;
1005 }
1006}
1007
1008/**
1009 * Decode TPM configuration.
1010 *
1011 * @param dev Returns a configuration of TPM device
1012 * @return 0 if ok, -1 on error
1013 */
1014static int tpm_decode_config(struct tpm *dev)
1015{
1016 const void *blob = gd->fdt_blob;
1017 struct udevice *bus;
1018 int chip_addr;
1019 int parent;
1020 int node;
1021 int ret;
1022
1023 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
1024 if (node < 0) {
1025 node = fdtdec_next_compatible(blob, 0,
1026 COMPAT_INFINEON_SLB9645_TPM);
1027 }
1028 if (node < 0) {
1029 debug("%s: Node not found\n", __func__);
1030 return -1;
1031 }
1032 parent = fdt_parent_offset(blob, node);
1033 if (parent < 0) {
1034 debug("%s: Cannot find node parent\n", __func__);
1035 return -1;
1036 }
1037
1038 /*
1039 * TODO(sjg@chromium.org): Remove this when driver model supports
1040 * TPMs
1041 */
1042 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1043 if (ret) {
1044 debug("Cannot find bus for node '%s: ret=%d'\n",
1045 fdt_get_name(blob, parent, NULL), ret);
1046 return ret;
1047 }
1048
1049 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1050 if (chip_addr == -1) {
1051 debug("Cannot find reg property for node '%s: ret=%d'\n",
1052 fdt_get_name(blob, node, NULL), ret);
1053 return ret;
1054 }
1055 /*
1056 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1057 * in iic_tpm_read() so the offset length needs to be 0 here.
1058 */
1059 ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
1060 if (ret) {
1061 debug("Cannot find device for node '%s: ret=%d'\n",
1062 fdt_get_name(blob, node, NULL), ret);
1063 return ret;
1064 }
1065
1066 return 0;
1067}
1068
1069struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
1070{
1071 struct tpm_chip *chip;
1072
1073 /* Driver specific per-device data */
1074 chip = &g_chip;
1075 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1076 chip->is_open = 1;
1077
1078 return chip;
1079}
1080
1081int tis_init(void)
1082{
1083 if (tpm.inited)
1084 return 0;
1085
1086 if (tpm_decode_config(&tpm))
1087 return -1;
1088
1089 debug("%s: done\n", __func__);
1090
1091 tpm.inited = 1;
1092
1093 return 0;
1094}
1095
1096int tis_open(void)
1097{
1098 int rc;
1099
1100 if (!tpm.inited)
1101 return -1;
1102
1103 rc = tpm_open_dev(tpm.dev);
1104
1105 return rc;
1106}
1107
1108int tis_close(void)
1109{
1110 if (!tpm.inited)
1111 return -1;
1112
1113 tpm_close();
1114
1115 return 0;
1116}
1117
1118int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1119 uint8_t *recvbuf, size_t *rbuf_len)
1120{
1121 int len;
1122 uint8_t buf[4096];
1123
1124 if (!tpm.inited)
1125 return -1;
1126
1127 if (sizeof(buf) < sbuf_size)
1128 return -1;
1129
1130 memcpy(buf, sendbuf, sbuf_size);
1131
1132 len = tpm_transmit(buf, sbuf_size);
1133
1134 if (len < 10) {
1135 *rbuf_len = 0;
1136 return -1;
1137 }
1138
1139 memcpy(recvbuf, buf, len);
1140 *rbuf_len = len;
1141
1142 return 0;
1143}