blob: 6a3991f9870ea72f2bf089d8638d7f2e267db731 [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>
Masahiro Yamadae7228522014-11-26 16:00:58 +090026#include <linux/compiler.h>
Rong Chang8faa9452013-04-12 10:44:57 +000027#include <i2c.h>
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000028#include <tpm.h>
29#include <asm-generic/errno.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
Simon Glass94576272015-08-22 18:31:22 -060033#include "tpm_tis_i2c.h"
Rong Chang8faa9452013-04-12 10:44:57 +000034
Vincent Palatin06883eb2013-04-12 11:04:36 +000035DECLARE_GLOBAL_DATA_PTR;
36
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000037/* Max buffer size supported by our tpm */
38#define TPM_DEV_BUFSIZE 1260
Rong Chang8faa9452013-04-12 10:44:57 +000039
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000040/* Max number of iterations after i2c NAK */
41#define MAX_COUNT 3
Rong Chang8faa9452013-04-12 10:44:57 +000042
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000043/*
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 Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +000050#define MAX_COUNT_LONG 50
51
52#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 Tame49fed52013-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};
Rong Chang8faa9452013-04-12 10:44:57 +000071
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +000072enum tis_defaults {
73 TIS_SHORT_TIMEOUT = 750, /* ms */
74 TIS_LONG_TIMEOUT = 2000, /* ms */
75};
Rong Chang8faa9452013-04-12 10:44:57 +000076
77/* expected value for DIDVID register */
Vincent Palatin06883eb2013-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 Chang8faa9452013-04-12 10:44:57 +000092
Tom Wai-Hong Tame49fed52013-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 Glass94576272015-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 Chang8faa9452013-04-12 10:44:57 +0000396/* Structure to store I2C TPM specific stuff */
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000397struct tpm_dev {
Simon Glassc9a3e842015-05-04 11:30:59 -0600398 struct udevice *dev;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000399 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
Vincent Palatin06883eb2013-04-12 11:04:36 +0000400 enum i2c_chip_type chip_type;
Rong Chang8faa9452013-04-12 10:44:57 +0000401};
402
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000403static struct tpm_dev tpm_dev;
404
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000419static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
Rong Chang8faa9452013-04-12 10:44:57 +0000420{
421 int rc;
422 int count;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000423 uint32_t addrbuf = addr;
Rong Chang8faa9452013-04-12 10:44:57 +0000424
Vincent Palatin06883eb2013-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 Glassc9a3e842015-05-04 11:30:59 -0600428 rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000429 if (rc == 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000430 break; /* Success, break to skip sleep */
Vincent Palatin06883eb2013-04-12 11:04:36 +0000431 udelay(SLEEP_DURATION);
432 }
Vincent Palatin06883eb2013-04-12 11:04:36 +0000433 if (rc)
434 return -rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000435
Vincent Palatin06883eb2013-04-12 11:04:36 +0000436 /* 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 Glassc9a3e842015-05-04 11:30:59 -0600442 rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000443 if (rc == 0)
444 break; /* success, break to skip sleep */
445 }
446 } else {
Tom Wai-Hong Tame49fed52013-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 Palatin06883eb2013-04-12 11:04:36 +0000450 * the 32 byte limit of the smbus.
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000451 * Retries should usually not be needed, but are kept just to
Vincent Palatin06883eb2013-04-12 11:04:36 +0000452 * be safe on the safe side.
453 */
454 for (count = 0; count < MAX_COUNT; count++) {
Simon Glassc9a3e842015-05-04 11:30:59 -0600455 rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
Vincent Palatin06883eb2013-04-12 11:04:36 +0000456 if (rc == 0)
457 break; /* break here to skip sleep */
458 udelay(SLEEP_DURATION);
459 }
Rong Chang8faa9452013-04-12 10:44:57 +0000460 }
461
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000462 /* Take care of 'guard time' */
Vincent Palatin06883eb2013-04-12 11:04:36 +0000463 udelay(SLEEP_DURATION);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000471 unsigned int sleep_time, u8 max_count)
Rong Chang8faa9452013-04-12 10:44:57 +0000472{
473 int rc = 0;
474 int count;
475
Rong Chang8faa9452013-04-12 10:44:57 +0000476 for (count = 0; count < max_count; count++) {
Simon Glassc9a3e842015-05-04 11:30:59 -0600477 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
Rong Chang8faa9452013-04-12 10:44:57 +0000478 if (rc == 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000479 break; /* Success, break to skip sleep */
Rong Chang8faa9452013-04-12 10:44:57 +0000480 udelay(sleep_time);
481 }
482
Vincent Palatin06883eb2013-04-12 11:04:36 +0000483 /* take care of 'guard time' */
Simon Glassc9a3e842015-05-04 11:30:59 -0600484 udelay(sleep_time);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000516 */
Rong Chang8faa9452013-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 Chang8faa9452013-04-12 10:44:57 +0000523static int check_locality(struct tpm_chip *chip, int loc)
524{
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000525 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000533 if ((buf & mask) == mask) {
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000543 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
Rong Chang8faa9452013-04-12 10:44:57 +0000544 u8 buf;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000545
Rong Chang8faa9452013-04-12 10:44:57 +0000546 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
547 return;
548
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000549 if (force || (buf & mask) == mask) {
Rong Chang8faa9452013-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 Glassc9a3e842015-05-04 11:30:59 -0600559 int rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000560
561 if (check_locality(chip, loc) >= 0)
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000562 return loc; /* We already have the locality */
Rong Chang8faa9452013-04-12 10:44:57 +0000563
Simon Glassc9a3e842015-05-04 11:30:59 -0600564 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
565 if (rc)
566 return rc;
Rong Chang8faa9452013-04-12 10:44:57 +0000567
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000568 /* Wait for burstcount */
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000574 udelay(TPM_TIMEOUT * 1000);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000582 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
Rong Chang8faa9452013-04-12 10:44:57 +0000583 u8 buf;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000584
Rong Chang8faa9452013-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 Glassc9a3e842015-05-04 11:30:59 -0600593 int rc;
594
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000595 /* This causes the current command to be aborted */
Rong Chang8faa9452013-04-12 10:44:57 +0000596 u8 buf = TPM_STS_COMMAND_READY;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000597
Simon Glassc9a3e842015-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 Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000608 u8 addr, buf[3];
Rong Chang8faa9452013-04-12 10:44:57 +0000609
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000610 /* Wait for burstcount */
611 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000616 addr = TPM_STS(chip->vendor.locality) + 1;
617 if (iic_tpm_read(addr, buf, 3) < 0)
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000624 udelay(TPM_TIMEOUT * 1000);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000631 int *status)
Rong Chang8faa9452013-04-12 10:44:57 +0000632{
633 unsigned long start, stop;
634
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000635 /* Check current status */
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000643 udelay(TPM_TIMEOUT * 1000);
Rong Chang8faa9452013-04-12 10:44:57 +0000644 *status = tpm_tis_i2c_status(chip);
645 if ((*status & mask) == mask)
646 return 0;
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000661 /* burstcount < 0 -> tpm is busy */
Rong Chang8faa9452013-04-12 10:44:57 +0000662 if (burstcnt < 0)
663 return burstcnt;
664
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000665 /* Limit received data to max left */
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000670 &(buf[size]), burstcnt);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000688 /* Read first 10 bytes, including tag, paramsize, and result */
Rong Chang8faa9452013-04-12 10:44:57 +0000689 size = recv_data(chip, buf, TPM_HEADER_SIZE);
690 if (size < TPM_HEADER_SIZE) {
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000691 error("Unable to read header\n");
Rong Chang8faa9452013-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 Glassc9a3e842015-05-04 11:30:59 -0600697 error("Error size=%x, expected=%x, count=%x\n", size, expected,
698 count);
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000704 expected - TPM_HEADER_SIZE);
Rong Chang8faa9452013-04-12 10:44:57 +0000705 if (size < expected) {
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000706 error("Unable to read remainder of result\n");
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000712 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
713 error("Error left over data\n");
Rong Chang8faa9452013-04-12 10:44:57 +0000714 size = -EIO;
715 goto out;
716 }
717
718out:
719 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000720 /*
721 * The TPM needs some time to clean up here,
Rong Chang8faa9452013-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 Glassc9a3e842015-05-04 11:30:59 -0600733 size_t burstcnt;
Rong Chang8faa9452013-04-12 10:44:57 +0000734 size_t count = 0;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000735 int retry = 0;
Rong Chang8faa9452013-04-12 10:44:57 +0000736 u8 sts = TPM_STS_GO;
737
Simon Glassc9a3e842015-05-04 11:30:59 -0600738 debug("%s: len=%d\n", __func__, len);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000739 if (len > TPM_DEV_BUFSIZE)
740 return -E2BIG; /* Command is too long for our tpm, sorry */
Rong Chang8faa9452013-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 Tame49fed52013-04-12 11:04:37 +0000748 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
749 chip->vendor.timeout_b, &status) < 0) {
Rong Chang8faa9452013-04-12 10:44:57 +0000750 rc = -ETIME;
751 goto out_err;
752 }
753 }
754
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000755 burstcnt = get_burstcount(chip);
Rong Chang8faa9452013-04-12 10:44:57 +0000756
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000757 /* burstcount < 0 -> tpm is busy */
758 if (burstcnt < 0)
759 return burstcnt;
Rong Chang8faa9452013-04-12 10:44:57 +0000760
Simon Glassc9a3e842015-05-04 11:30:59 -0600761 while (count < len) {
762 udelay(300);
763 if (burstcnt > len - count)
764 burstcnt = len - count;
Rong Chang8faa9452013-04-12 10:44:57 +0000765
Tom Wai-Hong Tame49fed52013-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 Chang8faa9452013-04-12 10:44:57 +0000770
771 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000772 &(buf[count]), burstcnt);
Rong Chang8faa9452013-04-12 10:44:57 +0000773 if (rc == 0)
774 count += burstcnt;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000775 else {
Simon Glassc9a3e842015-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 Chang8faa9452013-04-12 10:44:57 +0000785
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000786 if ((status & TPM_STS_DATA_EXPECT) == 0) {
787 rc = -EIO;
788 goto out_err;
789 }
Rong Chang8faa9452013-04-12 10:44:57 +0000790 }
791 }
792
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000793 /* Go and do it */
Rong Chang8faa9452013-04-12 10:44:57 +0000794 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
Simon Glassc9a3e842015-05-04 11:30:59 -0600795 debug("done\n");
Rong Chang8faa9452013-04-12 10:44:57 +0000796
797 return len;
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000798
Rong Chang8faa9452013-04-12 10:44:57 +0000799out_err:
Simon Glassc9a3e842015-05-04 11:30:59 -0600800 debug("%s: out_err\n", __func__);
Rong Chang8faa9452013-04-12 10:44:57 +0000801 tpm_tis_i2c_ready(chip);
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000802 /*
803 * The TPM needs some time to clean up here,
Rong Chang8faa9452013-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 = {
Rong Chang8faa9452013-04-12 10:44:57 +0000813 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
814 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
815 .req_canceled = TPM_STS_COMMAND_READY,
816};
817
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000818
Vincent Palatin06883eb2013-04-12 11:04:36 +0000819static enum i2c_chip_type tpm_vendor_chip_type(void)
820{
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900821#if CONFIG_IS_ENABLED(OF_CONTROL)
Vincent Palatin06883eb2013-04-12 11:04:36 +0000822 const void *blob = gd->fdt_blob;
823
824 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
825 return SLB9645;
826
827 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
828 return SLB9635;
829#endif
830 return UNKNOWN;
831}
832
Simon Glassac1f8572015-08-22 18:31:17 -0600833int tpm_vendor_init(struct udevice *dev)
Rong Chang8faa9452013-04-12 10:44:57 +0000834{
Simon Glassc9a3e842015-05-04 11:30:59 -0600835 struct tpm_chip *chip;
Rong Chang8faa9452013-04-12 10:44:57 +0000836 u32 vendor;
Vincent Palatin06883eb2013-04-12 11:04:36 +0000837 u32 expected_did_vid;
Rong Chang8faa9452013-04-12 10:44:57 +0000838
Simon Glassac1f8572015-08-22 18:31:17 -0600839 tpm_dev.dev = dev;
Vincent Palatin06883eb2013-04-12 11:04:36 +0000840 tpm_dev.chip_type = tpm_vendor_chip_type();
841
Rong Chang8faa9452013-04-12 10:44:57 +0000842 chip = tpm_register_hardware(&tpm_tis_i2c);
Simon Glassc9a3e842015-05-04 11:30:59 -0600843 if (chip < 0)
844 return -ENODEV;
Rong Chang8faa9452013-04-12 10:44:57 +0000845
846 /* Disable interrupts (not supported) */
847 chip->vendor.irq = 0;
848
849 /* Default timeouts */
850 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
851 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
852 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
853 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
854
Simon Glassc9a3e842015-05-04 11:30:59 -0600855 if (request_locality(chip, 0) < 0)
856 return -ENODEV;
Rong Chang8faa9452013-04-12 10:44:57 +0000857
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000858 /* Read four bytes from DID_VID register */
Rong Chang8faa9452013-04-12 10:44:57 +0000859 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
Simon Glassc9a3e842015-05-04 11:30:59 -0600860 release_locality(chip, 0, 1);
861 return -EIO;
Rong Chang8faa9452013-04-12 10:44:57 +0000862 }
863
Vincent Palatin06883eb2013-04-12 11:04:36 +0000864 if (tpm_dev.chip_type == SLB9635) {
865 vendor = be32_to_cpu(vendor);
866 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
867 } else {
868 /* device id and byte order has changed for newer i2c tpms */
869 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
870 }
Rong Chang8faa9452013-04-12 10:44:57 +0000871
Vincent Palatin06883eb2013-04-12 11:04:36 +0000872 if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000873 error("Vendor id did not match! ID was %08x\n", vendor);
Simon Glassc9a3e842015-05-04 11:30:59 -0600874 return -ENODEV;
Rong Chang8faa9452013-04-12 10:44:57 +0000875 }
876
Tom Wai-Hong Tame49fed52013-04-12 11:04:37 +0000877 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
878 chip_name[tpm_dev.chip_type], vendor >> 16);
Rong Chang8faa9452013-04-12 10:44:57 +0000879
880 /*
881 * A timeout query to TPM can be placed here.
882 * Standard timeout values are used so far
883 */
884
885 return 0;
Simon Glassc9a3e842015-05-04 11:30:59 -0600886}
Rong Chang8faa9452013-04-12 10:44:57 +0000887
888void tpm_vendor_cleanup(struct tpm_chip *chip)
889{
890 release_locality(chip, chip->vendor.locality, 1);
891}
Simon Glass94576272015-08-22 18:31:22 -0600892
893/* Returns max number of milliseconds to wait */
894static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
895 u32 ordinal)
896{
897 int duration_idx = TPM_UNDEFINED;
898 int duration = 0;
899
900 if (ordinal < TPM_MAX_ORDINAL) {
901 duration_idx = tpm_ordinal_duration[ordinal];
902 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
903 TPM_MAX_PROTECTED_ORDINAL) {
904 duration_idx = tpm_protected_ordinal_duration[
905 ordinal & TPM_PROTECTED_ORDINAL_MASK];
906 }
907
908 if (duration_idx != TPM_UNDEFINED)
909 duration = chip->vendor.duration[duration_idx];
910
911 if (duration <= 0)
912 return 2 * 60 * HZ; /* Two minutes timeout */
913 else
914 return duration;
915}
916
917static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
918{
919 int rc;
920 u32 count, ordinal;
921 unsigned long start, stop;
922
923 struct tpm_chip *chip = &g_chip;
924
925 /* switch endianess: big->little */
926 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
927 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
928
929 if (count == 0) {
930 error("no data\n");
931 return -ENODATA;
932 }
933 if (count > bufsiz) {
934 error("invalid count value %x %zx\n", count, bufsiz);
935 return -E2BIG;
936 }
937
938 debug("Calling send\n");
Simon Glass651d3d12015-08-22 18:31:23 -0600939 rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
Simon Glass94576272015-08-22 18:31:22 -0600940 debug(" ... done calling send\n");
941 if (rc < 0) {
942 error("tpm_transmit: tpm_send: error %d\n", rc);
943 goto out;
944 }
945
946 if (chip->vendor.irq)
947 goto out_recv;
948
949 start = get_timer(0);
950 stop = tpm_calc_ordinal_duration(chip, ordinal);
951 do {
952 debug("waiting for status... %ld %ld\n", start, stop);
Simon Glass651d3d12015-08-22 18:31:23 -0600953 u8 status = tpm_tis_i2c_status(chip);
Simon Glass94576272015-08-22 18:31:22 -0600954 if ((status & chip->vendor.req_complete_mask) ==
955 chip->vendor.req_complete_val) {
956 debug("...got it;\n");
957 goto out_recv;
958 }
959
960 if (status == chip->vendor.req_canceled) {
961 error("Operation Canceled\n");
962 rc = -ECANCELED;
963 goto out;
964 }
965 udelay(TPM_TIMEOUT * 1000);
966 } while (get_timer(start) < stop);
967
Simon Glass651d3d12015-08-22 18:31:23 -0600968 tpm_tis_i2c_ready(chip);
Simon Glass94576272015-08-22 18:31:22 -0600969 error("Operation Timed out\n");
970 rc = -ETIME;
971 goto out;
972
973out_recv:
974 debug("out_recv: reading response...\n");
Simon Glass651d3d12015-08-22 18:31:23 -0600975 rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
Simon Glass94576272015-08-22 18:31:22 -0600976 if (rc < 0)
977 error("tpm_transmit: tpm_recv: error %d\n", rc);
978
979out:
980 return rc;
981}
982
983static int tpm_open_dev(struct udevice *dev)
984{
985 int rc;
986
987 debug("%s: start\n", __func__);
988 if (g_chip.is_open)
989 return -EBUSY;
990 rc = tpm_vendor_init(dev);
991 if (rc < 0)
992 g_chip.is_open = 0;
993 return rc;
994}
995
996static void tpm_close(void)
997{
998 if (g_chip.is_open) {
999 tpm_vendor_cleanup(&g_chip);
1000 g_chip.is_open = 0;
1001 }
1002}
1003
1004/**
1005 * Decode TPM configuration.
1006 *
1007 * @param dev Returns a configuration of TPM device
1008 * @return 0 if ok, -1 on error
1009 */
1010static int tpm_decode_config(struct tpm *dev)
1011{
1012 const void *blob = gd->fdt_blob;
1013 struct udevice *bus;
1014 int chip_addr;
1015 int parent;
1016 int node;
1017 int ret;
1018
1019 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
1020 if (node < 0) {
1021 node = fdtdec_next_compatible(blob, 0,
1022 COMPAT_INFINEON_SLB9645_TPM);
1023 }
1024 if (node < 0) {
1025 debug("%s: Node not found\n", __func__);
1026 return -1;
1027 }
1028 parent = fdt_parent_offset(blob, node);
1029 if (parent < 0) {
1030 debug("%s: Cannot find node parent\n", __func__);
1031 return -1;
1032 }
1033
1034 /*
1035 * TODO(sjg@chromium.org): Remove this when driver model supports
1036 * TPMs
1037 */
1038 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1039 if (ret) {
1040 debug("Cannot find bus for node '%s: ret=%d'\n",
1041 fdt_get_name(blob, parent, NULL), ret);
1042 return ret;
1043 }
1044
1045 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1046 if (chip_addr == -1) {
1047 debug("Cannot find reg property for node '%s: ret=%d'\n",
1048 fdt_get_name(blob, node, NULL), ret);
1049 return ret;
1050 }
1051 /*
1052 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1053 * in iic_tpm_read() so the offset length needs to be 0 here.
1054 */
1055 ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
1056 if (ret) {
1057 debug("Cannot find device for node '%s: ret=%d'\n",
1058 fdt_get_name(blob, node, NULL), ret);
1059 return ret;
1060 }
1061
1062 return 0;
1063}
1064
1065struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
1066{
1067 struct tpm_chip *chip;
1068
1069 /* Driver specific per-device data */
1070 chip = &g_chip;
1071 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1072 chip->is_open = 1;
1073
1074 return chip;
1075}
1076
1077int tis_init(void)
1078{
1079 if (tpm.inited)
1080 return 0;
1081
1082 if (tpm_decode_config(&tpm))
1083 return -1;
1084
1085 debug("%s: done\n", __func__);
1086
1087 tpm.inited = 1;
1088
1089 return 0;
1090}
1091
1092int tis_open(void)
1093{
1094 int rc;
1095
1096 if (!tpm.inited)
1097 return -1;
1098
1099 rc = tpm_open_dev(tpm.dev);
1100
1101 return rc;
1102}
1103
1104int tis_close(void)
1105{
1106 if (!tpm.inited)
1107 return -1;
1108
1109 tpm_close();
1110
1111 return 0;
1112}
1113
1114int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1115 uint8_t *recvbuf, size_t *rbuf_len)
1116{
1117 int len;
1118 uint8_t buf[4096];
1119
1120 if (!tpm.inited)
1121 return -1;
1122
1123 if (sizeof(buf) < sbuf_size)
1124 return -1;
1125
1126 memcpy(buf, sendbuf, sbuf_size);
1127
1128 len = tpm_transmit(buf, sbuf_size);
1129
1130 if (len < 10) {
1131 *rbuf_len = 0;
1132 return -1;
1133 }
1134
1135 memcpy(recvbuf, buf, len);
1136 *rbuf_len = len;
1137
1138 return 0;
1139}