1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <common.h>
23#include <dm.h>
24#include <fdtdec.h>
25#include <i2c.h>
26#include <log.h>
27#include <tpm-v1.h>
28#include <linux/delay.h>
29#include <linux/errno.h>
30#include <linux/compiler.h>
31#include <linux/types.h>
32#include <linux/unaligned/be_byteshift.h>
33
34#include "tpm_tis.h"
35#include "tpm_internal.h"
36
37enum i2c_chip_type {
38 SLB9635,
39 SLB9645,
40 UNKNOWN,
41};
42
43
44#define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
45#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
46
47static const char * const chip_name[] = {
48 [SLB9635] = "slb9635tt",
49 [SLB9645] = "slb9645tt",
50 [UNKNOWN] = "unknown/fallback to slb9635",
51};
52
53#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
54#define TPM_STS(l) (0x0001 | ((l) << 4))
55#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
56#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
73 size_t len)
74{
75 struct tpm_chip *chip = dev_get_priv(dev);
76 int rc;
77 int count;
78 uint32_t addrbuf = addr;
79
80 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
81
82 for (count = 0; count < MAX_COUNT; count++) {
83 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
84 if (rc == 0)
85 break;
86 udelay(SLEEP_DURATION_US);
87 }
88 if (rc)
89 return rc;
90
91
92
93
94
95 for (count = 0; count < MAX_COUNT; count++) {
96 udelay(SLEEP_DURATION_US);
97 rc = dm_i2c_read(dev, 0, buffer, len);
98 if (rc == 0)
99 break;
100 }
101 } else {
102
103
104
105
106
107
108
109 for (count = 0; count < MAX_COUNT; count++) {
110 rc = dm_i2c_read(dev, addr, buffer, len);
111 if (rc == 0)
112 break;
113 udelay(SLEEP_DURATION_US);
114 }
115 }
116
117
118 udelay(SLEEP_DURATION_US);
119 if (rc)
120 return rc;
121
122 return 0;
123}
124
125static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
126 const u8 *buffer, size_t len,
127 unsigned int sleep_time_us, u8 max_count)
128{
129 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
130 struct tpm_chip *chip = dev_get_priv(dev);
131 int rc = 0;
132 int count;
133
134 if (chip->chip_type == SLB9635) {
135
136 priv->buf[0] = addr;
137 memcpy(&(priv->buf[1]), buffer, len);
138 buffer = priv->buf;
139 len++;
140 addr = 0;
141 }
142
143 for (count = 0; count < max_count; count++) {
144 rc = dm_i2c_write(dev, addr, buffer, len);
145 if (rc == 0)
146 break;
147 udelay(sleep_time_us);
148 }
149
150
151 udelay(sleep_time_us);
152 if (rc)
153 return rc;
154
155 return 0;
156}
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
175 size_t len)
176{
177 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
178 SLEEP_DURATION_US, MAX_COUNT);
179}
180
181
182
183
184
185static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
186 size_t len)
187{
188 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
189 SLEEP_DURATION_LONG_US,
190 MAX_COUNT_LONG);
191}
192
193static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
194{
195 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
196 struct tpm_chip *chip = dev_get_priv(dev);
197 u8 buf;
198 int rc;
199
200 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
201 if (rc < 0)
202 return rc;
203
204 if ((buf & mask) == mask) {
205 chip->locality = loc;
206 return loc;
207 }
208
209 return -ENOENT;
210}
211
212static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
213 int force)
214{
215 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
216 u8 buf;
217
218 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
219 return;
220
221 if (force || (buf & mask) == mask) {
222 buf = TPM_ACCESS_ACTIVE_LOCALITY;
223 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
224 }
225}
226
227static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
228{
229 struct tpm_chip *chip = dev_get_priv(dev);
230 unsigned long start, stop;
231 u8 buf = TPM_ACCESS_REQUEST_USE;
232 int rc;
233
234 rc = tpm_tis_i2c_check_locality(dev, loc);
235 if (rc >= 0) {
236 debug("%s: Already have locality\n", __func__);
237 return loc;
238 } else if (rc != -ENOENT) {
239 debug("%s: Failed to get locality: %d\n", __func__, rc);
240 return rc;
241 }
242
243 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
244 if (rc) {
245 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
246 return rc;
247 }
248
249
250 start = get_timer(0);
251 stop = chip->timeout_a;
252 do {
253 rc = tpm_tis_i2c_check_locality(dev, loc);
254 if (rc >= 0) {
255 debug("%s: Have locality\n", __func__);
256 return loc;
257 } else if (rc != -ENOENT) {
258 debug("%s: Failed to get locality: %d\n", __func__, rc);
259 return rc;
260 }
261 mdelay(TPM_TIMEOUT_MS);
262 } while (get_timer(start) < stop);
263 debug("%s: Timeout getting locality: %d\n", __func__, rc);
264
265 return rc;
266}
267
268static u8 tpm_tis_i2c_status(struct udevice *dev)
269{
270 struct tpm_chip *chip = dev_get_priv(dev);
271
272 u8 buf;
273
274 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
275 return 0;
276 else
277 return buf;
278}
279
280static int tpm_tis_i2c_ready(struct udevice *dev)
281{
282 struct tpm_chip *chip = dev_get_priv(dev);
283 int rc;
284
285
286 u8 buf = TPM_STS_COMMAND_READY;
287
288 debug("%s\n", __func__);
289 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
290 if (rc)
291 debug("%s: rc=%d\n", __func__, rc);
292
293 return rc;
294}
295
296static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
297{
298 struct tpm_chip *chip = dev_get_priv(dev);
299 unsigned long start, stop;
300 ssize_t burstcnt;
301 u8 addr, buf[3];
302
303
304
305 start = get_timer(0);
306 stop = chip->timeout_d;
307 do {
308
309 addr = TPM_STS(chip->locality) + 1;
310 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
311 burstcnt = 0;
312 else
313 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
314
315 if (burstcnt)
316 return burstcnt;
317 mdelay(TPM_TIMEOUT_MS);
318 } while (get_timer(start) < stop);
319
320 return -EBUSY;
321}
322
323static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
324 unsigned long timeout, int *status)
325{
326 unsigned long start, stop;
327
328
329 *status = tpm_tis_i2c_status(dev);
330 if ((*status & mask) == mask)
331 return 0;
332
333 start = get_timer(0);
334 stop = timeout;
335 do {
336 mdelay(TPM_TIMEOUT_MS);
337 *status = tpm_tis_i2c_status(dev);
338 if ((*status & mask) == mask)
339 return 0;
340 } while (get_timer(start) < stop);
341
342 return -ETIMEDOUT;
343}
344
345static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
346{
347 struct tpm_chip *chip = dev_get_priv(dev);
348 size_t size = 0;
349 ssize_t burstcnt;
350 int rc;
351
352 while (size < count) {
353 burstcnt = tpm_tis_i2c_get_burstcount(dev);
354
355
356 if (burstcnt < 0)
357 return burstcnt;
358
359
360 if (burstcnt > (count - size))
361 burstcnt = count - size;
362
363 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
364 &(buf[size]), burstcnt);
365 if (rc == 0)
366 size += burstcnt;
367 }
368
369 return size;
370}
371
372static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
373{
374 struct tpm_chip *chip = dev_get_priv(dev);
375 int size = 0;
376 int status;
377 unsigned int expected;
378 int rc;
379
380 status = tpm_tis_i2c_status(dev);
381 if (status == TPM_STS_COMMAND_READY)
382 return -EINTR;
383 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
384 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
385 return -EAGAIN;
386
387 debug("...got it;\n");
388
389
390 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
391 if (size < TPM_HEADER_SIZE) {
392 debug("Unable to read header\n");
393 return size < 0 ? size : -EIO;
394 }
395
396 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
397 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
398 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
399 count);
400 return -ENOSPC;
401 }
402
403 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
404 expected - TPM_HEADER_SIZE);
405 if (size < expected) {
406 debug("Unable to read remainder of result\n");
407 return -ETIMEDOUT;
408 }
409
410 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
411 &status);
412 if (rc)
413 return rc;
414 if (status & TPM_STS_DATA_AVAIL) {
415 debug("Error left over data\n");
416 return -EIO;
417 }
418
419 return size;
420}
421
422static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
423{
424 struct tpm_chip *chip = dev_get_priv(dev);
425 int rc, status;
426 size_t burstcnt;
427 size_t count = 0;
428 int retry = 0;
429 u8 sts = TPM_STS_GO;
430
431 debug("%s: len=%d\n", __func__, len);
432 if (len > TPM_DEV_BUFSIZE)
433 return -E2BIG;
434
435 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
436 return -EBUSY;
437
438 status = tpm_tis_i2c_status(dev);
439 if ((status & TPM_STS_COMMAND_READY) == 0) {
440 rc = tpm_tis_i2c_ready(dev);
441 if (rc)
442 return rc;
443 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
444 chip->timeout_b, &status);
445 if (rc)
446 return rc;
447 }
448
449 burstcnt = tpm_tis_i2c_get_burstcount(dev);
450
451
452 if (burstcnt < 0)
453 return burstcnt;
454
455 while (count < len) {
456 udelay(300);
457 if (burstcnt > len - count)
458 burstcnt = len - count;
459
460#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
461 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
462 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
463#endif
464
465 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
466 &(buf[count]), burstcnt);
467 if (rc == 0)
468 count += burstcnt;
469 else {
470 debug("%s: error\n", __func__);
471 if (retry++ > 10)
472 return -EIO;
473 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
474 chip->timeout_c,
475 &status);
476 if (rc)
477 return rc;
478
479 if ((status & TPM_STS_DATA_EXPECT) == 0)
480 return -EIO;
481 }
482 }
483
484
485 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
486 if (rc < 0)
487 return rc;
488 debug("%s: done, rc=%d\n", __func__, rc);
489
490 return len;
491}
492
493static int tpm_tis_i2c_cleanup(struct udevice *dev)
494{
495 struct tpm_chip *chip = dev_get_priv(dev);
496
497 tpm_tis_i2c_ready(dev);
498
499
500
501
502 mdelay(2);
503 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
504
505 return 0;
506}
507
508static int tpm_tis_i2c_init(struct udevice *dev)
509{
510 struct tpm_chip *chip = dev_get_priv(dev);
511 u32 vendor;
512 u32 expected_did_vid;
513 int rc;
514
515 chip->is_open = 1;
516
517
518 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
519 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
520 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
521 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
522
523 rc = tpm_tis_i2c_request_locality(dev, 0);
524 if (rc < 0)
525 return rc;
526
527
528 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
529 tpm_tis_i2c_release_locality(dev, 0, 1);
530 return -EIO;
531 }
532
533 if (chip->chip_type == SLB9635) {
534 vendor = be32_to_cpu(vendor);
535 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
536 } else {
537
538 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
539 }
540
541 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
542 pr_err("Vendor id did not match! ID was %08x\n", vendor);
543 return -ENODEV;
544 }
545
546 chip->vend_dev = vendor;
547 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
548 chip_name[chip->chip_type], vendor >> 16);
549
550
551
552
553
554
555 return 0;
556}
557
558static int tpm_tis_i2c_open(struct udevice *dev)
559{
560 struct tpm_chip *chip = dev_get_priv(dev);
561 int rc;
562
563 debug("%s: start\n", __func__);
564 if (chip->is_open)
565 return -EBUSY;
566 rc = tpm_tis_i2c_init(dev);
567 if (rc < 0)
568 chip->is_open = 0;
569
570 return rc;
571}
572
573static int tpm_tis_i2c_close(struct udevice *dev)
574{
575 struct tpm_chip *chip = dev_get_priv(dev);
576
577 if (chip->is_open) {
578 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
579 chip->is_open = 0;
580 chip->vend_dev = 0;
581 }
582
583 return 0;
584}
585
586static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
587{
588 struct tpm_chip *chip = dev_get_priv(dev);
589
590 if (size < 50)
591 return -ENOSPC;
592
593 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
594 chip->is_open ? "open" : "closed",
595 chip_name[chip->chip_type],
596 chip->vend_dev >> 16);
597}
598
599static int tpm_tis_i2c_probe(struct udevice *dev)
600{
601 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
602 struct tpm_chip *chip = dev_get_priv(dev);
603
604 chip->chip_type = dev_get_driver_data(dev);
605
606
607 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
608 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
609 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
610 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
611
612 return 0;
613}
614
615static const struct tpm_ops tpm_tis_i2c_ops = {
616 .open = tpm_tis_i2c_open,
617 .close = tpm_tis_i2c_close,
618 .get_desc = tpm_tis_get_desc,
619 .send = tpm_tis_i2c_send,
620 .recv = tpm_tis_i2c_recv,
621 .cleanup = tpm_tis_i2c_cleanup,
622};
623
624static const struct udevice_id tpm_tis_i2c_ids[] = {
625 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
626 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
627 { }
628};
629
630U_BOOT_DRIVER(tpm_tis_i2c) = {
631 .name = "tpm_tis_infineon",
632 .id = UCLASS_TPM,
633 .of_match = tpm_tis_i2c_ids,
634 .ops = &tpm_tis_i2c_ops,
635 .probe = tpm_tis_i2c_probe,
636 .priv_auto_alloc_size = sizeof(struct tpm_chip),
637};
638