]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/tpm/tpm_tis_i2c.c
tpm: tpm_tis_i2c: Merge struct tpm_dev into tpm_chip
[karo-tx-uboot.git] / drivers / tpm / tpm_tis_i2c.c
1 /*
2  * Copyright (C) 2011 Infineon Technologies
3  *
4  * Authors:
5  * Peter Huewe <huewe.external@infineon.com>
6  *
7  * Description:
8  * Device driver for TCG/TCPA TPM (trusted platform module).
9  * Specifications at www.trustedcomputinggroup.org
10  *
11  * This device driver implements the TPM interface as defined in
12  * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
13  * Infineon I2C Protocol Stack Specification v0.20.
14  *
15  * It is based on the Linux kernel driver tpm.c from Leendert van
16  * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
17  *
18  * Version: 2.1.1
19  *
20  * SPDX-License-Identifier:     GPL-2.0
21  */
22
23 #include <common.h>
24 #include <dm.h>
25 #include <fdtdec.h>
26 #include <linux/compiler.h>
27 #include <i2c.h>
28 #include <tpm.h>
29 #include <asm-generic/errno.h>
30 #include <linux/types.h>
31 #include <linux/unaligned/be_byteshift.h>
32
33 #include "tpm_tis_i2c.h"
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 /* Max number of iterations after i2c NAK */
38 #define MAX_COUNT               3
39
40 /*
41  * Max number of iterations after i2c NAK for 'long' commands
42  *
43  * We need this especially for sending TPM_READY, since the cleanup after the
44  * transtion to the ready state may take some time, but it is unpredictable
45  * how long it will take.
46  */
47 #define MAX_COUNT_LONG          50
48
49 #define SLEEP_DURATION          60      /* in usec */
50 #define SLEEP_DURATION_LONG     210     /* in usec */
51
52 #define TPM_HEADER_SIZE         10
53
54 enum tis_access {
55         TPM_ACCESS_VALID                = 0x80,
56         TPM_ACCESS_ACTIVE_LOCALITY      = 0x20,
57         TPM_ACCESS_REQUEST_PENDING      = 0x04,
58         TPM_ACCESS_REQUEST_USE          = 0x02,
59 };
60
61 enum tis_status {
62         TPM_STS_VALID                   = 0x80,
63         TPM_STS_COMMAND_READY           = 0x40,
64         TPM_STS_GO                      = 0x20,
65         TPM_STS_DATA_AVAIL              = 0x10,
66         TPM_STS_DATA_EXPECT             = 0x08,
67 };
68
69 enum tis_defaults {
70         TIS_SHORT_TIMEOUT               = 750,  /* ms */
71         TIS_LONG_TIMEOUT                = 2000, /* ms */
72 };
73
74 /* expected value for DIDVID register */
75 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
76 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
77
78 static const char * const chip_name[] = {
79         [SLB9635] = "slb9635tt",
80         [SLB9645] = "slb9645tt",
81         [UNKNOWN] = "unknown/fallback to slb9635",
82 };
83
84 #define TPM_ACCESS(l)                   (0x0000 | ((l) << 4))
85 #define TPM_STS(l)                      (0x0001 | ((l) << 4))
86 #define TPM_DATA_FIFO(l)                (0x0005 | ((l) << 4))
87 #define TPM_DID_VID(l)                  (0x0006 | ((l) << 4))
88
89 enum tpm_duration {
90         TPM_SHORT = 0,
91         TPM_MEDIUM = 1,
92         TPM_LONG = 2,
93         TPM_UNDEFINED,
94 };
95
96 /* Extended error numbers from linux (see errno.h) */
97 #define ECANCELED       125     /* Operation Canceled */
98
99 /* Timer frequency. Corresponds to msec timer resolution*/
100 #define HZ              1000
101
102 #define TPM_MAX_ORDINAL                 243
103 #define TPM_MAX_PROTECTED_ORDINAL       12
104 #define TPM_PROTECTED_ORDINAL_MASK      0xFF
105
106 #define TPM_CMD_COUNT_BYTE      2
107 #define TPM_CMD_ORDINAL_BYTE    6
108
109 /*
110  * Array with one entry per ordinal defining the maximum amount
111  * of time the chip could take to return the result.  The ordinal
112  * designation of short, medium or long is defined in a table in
113  * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
114  * values of the SHORT, MEDIUM, and LONG durations are retrieved
115  * from the chip during initialization with a call to tpm_get_timeouts.
116  */
117 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
118         TPM_UNDEFINED,          /* 0 */
119         TPM_UNDEFINED,
120         TPM_UNDEFINED,
121         TPM_UNDEFINED,
122         TPM_UNDEFINED,
123         TPM_UNDEFINED,          /* 5 */
124         TPM_UNDEFINED,
125         TPM_UNDEFINED,
126         TPM_UNDEFINED,
127         TPM_UNDEFINED,
128         TPM_SHORT,              /* 10 */
129         TPM_SHORT,
130 };
131
132 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
133         TPM_UNDEFINED,          /* 0 */
134         TPM_UNDEFINED,
135         TPM_UNDEFINED,
136         TPM_UNDEFINED,
137         TPM_UNDEFINED,
138         TPM_UNDEFINED,          /* 5 */
139         TPM_UNDEFINED,
140         TPM_UNDEFINED,
141         TPM_UNDEFINED,
142         TPM_UNDEFINED,
143         TPM_SHORT,              /* 10 */
144         TPM_SHORT,
145         TPM_MEDIUM,
146         TPM_LONG,
147         TPM_LONG,
148         TPM_MEDIUM,             /* 15 */
149         TPM_SHORT,
150         TPM_SHORT,
151         TPM_MEDIUM,
152         TPM_LONG,
153         TPM_SHORT,              /* 20 */
154         TPM_SHORT,
155         TPM_MEDIUM,
156         TPM_MEDIUM,
157         TPM_MEDIUM,
158         TPM_SHORT,              /* 25 */
159         TPM_SHORT,
160         TPM_MEDIUM,
161         TPM_SHORT,
162         TPM_SHORT,
163         TPM_MEDIUM,             /* 30 */
164         TPM_LONG,
165         TPM_MEDIUM,
166         TPM_SHORT,
167         TPM_SHORT,
168         TPM_SHORT,              /* 35 */
169         TPM_MEDIUM,
170         TPM_MEDIUM,
171         TPM_UNDEFINED,
172         TPM_UNDEFINED,
173         TPM_MEDIUM,             /* 40 */
174         TPM_LONG,
175         TPM_MEDIUM,
176         TPM_SHORT,
177         TPM_SHORT,
178         TPM_SHORT,              /* 45 */
179         TPM_SHORT,
180         TPM_SHORT,
181         TPM_SHORT,
182         TPM_LONG,
183         TPM_MEDIUM,             /* 50 */
184         TPM_MEDIUM,
185         TPM_UNDEFINED,
186         TPM_UNDEFINED,
187         TPM_UNDEFINED,
188         TPM_UNDEFINED,          /* 55 */
189         TPM_UNDEFINED,
190         TPM_UNDEFINED,
191         TPM_UNDEFINED,
192         TPM_UNDEFINED,
193         TPM_MEDIUM,             /* 60 */
194         TPM_MEDIUM,
195         TPM_MEDIUM,
196         TPM_SHORT,
197         TPM_SHORT,
198         TPM_MEDIUM,             /* 65 */
199         TPM_UNDEFINED,
200         TPM_UNDEFINED,
201         TPM_UNDEFINED,
202         TPM_UNDEFINED,
203         TPM_SHORT,              /* 70 */
204         TPM_SHORT,
205         TPM_UNDEFINED,
206         TPM_UNDEFINED,
207         TPM_UNDEFINED,
208         TPM_UNDEFINED,          /* 75 */
209         TPM_UNDEFINED,
210         TPM_UNDEFINED,
211         TPM_UNDEFINED,
212         TPM_UNDEFINED,
213         TPM_LONG,               /* 80 */
214         TPM_UNDEFINED,
215         TPM_MEDIUM,
216         TPM_LONG,
217         TPM_SHORT,
218         TPM_UNDEFINED,          /* 85 */
219         TPM_UNDEFINED,
220         TPM_UNDEFINED,
221         TPM_UNDEFINED,
222         TPM_UNDEFINED,
223         TPM_SHORT,              /* 90 */
224         TPM_SHORT,
225         TPM_SHORT,
226         TPM_SHORT,
227         TPM_SHORT,
228         TPM_UNDEFINED,          /* 95 */
229         TPM_UNDEFINED,
230         TPM_UNDEFINED,
231         TPM_UNDEFINED,
232         TPM_UNDEFINED,
233         TPM_MEDIUM,             /* 100 */
234         TPM_SHORT,
235         TPM_SHORT,
236         TPM_UNDEFINED,
237         TPM_UNDEFINED,
238         TPM_UNDEFINED,          /* 105 */
239         TPM_UNDEFINED,
240         TPM_UNDEFINED,
241         TPM_UNDEFINED,
242         TPM_UNDEFINED,
243         TPM_SHORT,              /* 110 */
244         TPM_SHORT,
245         TPM_SHORT,
246         TPM_SHORT,
247         TPM_SHORT,
248         TPM_SHORT,              /* 115 */
249         TPM_SHORT,
250         TPM_SHORT,
251         TPM_UNDEFINED,
252         TPM_UNDEFINED,
253         TPM_LONG,               /* 120 */
254         TPM_LONG,
255         TPM_MEDIUM,
256         TPM_UNDEFINED,
257         TPM_SHORT,
258         TPM_SHORT,              /* 125 */
259         TPM_SHORT,
260         TPM_LONG,
261         TPM_SHORT,
262         TPM_SHORT,
263         TPM_SHORT,              /* 130 */
264         TPM_MEDIUM,
265         TPM_UNDEFINED,
266         TPM_SHORT,
267         TPM_MEDIUM,
268         TPM_UNDEFINED,          /* 135 */
269         TPM_UNDEFINED,
270         TPM_UNDEFINED,
271         TPM_UNDEFINED,
272         TPM_UNDEFINED,
273         TPM_SHORT,              /* 140 */
274         TPM_SHORT,
275         TPM_UNDEFINED,
276         TPM_UNDEFINED,
277         TPM_UNDEFINED,
278         TPM_UNDEFINED,          /* 145 */
279         TPM_UNDEFINED,
280         TPM_UNDEFINED,
281         TPM_UNDEFINED,
282         TPM_UNDEFINED,
283         TPM_SHORT,              /* 150 */
284         TPM_MEDIUM,
285         TPM_MEDIUM,
286         TPM_SHORT,
287         TPM_SHORT,
288         TPM_UNDEFINED,          /* 155 */
289         TPM_UNDEFINED,
290         TPM_UNDEFINED,
291         TPM_UNDEFINED,
292         TPM_UNDEFINED,
293         TPM_SHORT,              /* 160 */
294         TPM_SHORT,
295         TPM_SHORT,
296         TPM_SHORT,
297         TPM_UNDEFINED,
298         TPM_UNDEFINED,          /* 165 */
299         TPM_UNDEFINED,
300         TPM_UNDEFINED,
301         TPM_UNDEFINED,
302         TPM_UNDEFINED,
303         TPM_LONG,               /* 170 */
304         TPM_UNDEFINED,
305         TPM_UNDEFINED,
306         TPM_UNDEFINED,
307         TPM_UNDEFINED,
308         TPM_UNDEFINED,          /* 175 */
309         TPM_UNDEFINED,
310         TPM_UNDEFINED,
311         TPM_UNDEFINED,
312         TPM_UNDEFINED,
313         TPM_MEDIUM,             /* 180 */
314         TPM_SHORT,
315         TPM_MEDIUM,
316         TPM_MEDIUM,
317         TPM_MEDIUM,
318         TPM_MEDIUM,             /* 185 */
319         TPM_SHORT,
320         TPM_UNDEFINED,
321         TPM_UNDEFINED,
322         TPM_UNDEFINED,
323         TPM_UNDEFINED,          /* 190 */
324         TPM_UNDEFINED,
325         TPM_UNDEFINED,
326         TPM_UNDEFINED,
327         TPM_UNDEFINED,
328         TPM_UNDEFINED,          /* 195 */
329         TPM_UNDEFINED,
330         TPM_UNDEFINED,
331         TPM_UNDEFINED,
332         TPM_UNDEFINED,
333         TPM_SHORT,              /* 200 */
334         TPM_UNDEFINED,
335         TPM_UNDEFINED,
336         TPM_UNDEFINED,
337         TPM_SHORT,
338         TPM_SHORT,              /* 205 */
339         TPM_SHORT,
340         TPM_SHORT,
341         TPM_SHORT,
342         TPM_SHORT,
343         TPM_MEDIUM,             /* 210 */
344         TPM_UNDEFINED,
345         TPM_MEDIUM,
346         TPM_MEDIUM,
347         TPM_MEDIUM,
348         TPM_UNDEFINED,          /* 215 */
349         TPM_MEDIUM,
350         TPM_UNDEFINED,
351         TPM_UNDEFINED,
352         TPM_SHORT,
353         TPM_SHORT,              /* 220 */
354         TPM_SHORT,
355         TPM_SHORT,
356         TPM_SHORT,
357         TPM_SHORT,
358         TPM_UNDEFINED,          /* 225 */
359         TPM_UNDEFINED,
360         TPM_UNDEFINED,
361         TPM_UNDEFINED,
362         TPM_UNDEFINED,
363         TPM_SHORT,              /* 230 */
364         TPM_LONG,
365         TPM_MEDIUM,
366         TPM_UNDEFINED,
367         TPM_UNDEFINED,
368         TPM_UNDEFINED,          /* 235 */
369         TPM_UNDEFINED,
370         TPM_UNDEFINED,
371         TPM_UNDEFINED,
372         TPM_UNDEFINED,
373         TPM_SHORT,              /* 240 */
374         TPM_UNDEFINED,
375         TPM_MEDIUM,
376 };
377
378 /* TPM configuration */
379 struct tpm {
380         struct udevice *dev;
381         char inited;
382 } tpm;
383
384 static struct tpm_chip g_chip;
385
386 /*
387  * iic_tpm_read() - read from TPM register
388  * @addr: register address to read from
389  * @buffer: provided by caller
390  * @len: number of bytes to read
391  *
392  * Read len bytes from TPM register and put them into
393  * buffer (little-endian format, i.e. first byte is put into buffer[0]).
394  *
395  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
396  * values have to be swapped.
397  *
398  * Return -EIO on error, 0 on success.
399  */
400 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
401 {
402         int rc;
403         int count;
404         uint32_t addrbuf = addr;
405
406         if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
407                 /* slb9635 protocol should work in both cases */
408                 for (count = 0; count < MAX_COUNT; count++) {
409                         rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
410                         if (rc == 0)
411                                 break;  /* Success, break to skip sleep */
412                         udelay(SLEEP_DURATION);
413                 }
414                 if (rc)
415                         return -rc;
416
417                 /* After the TPM has successfully received the register address
418                  * it needs some time, thus we're sleeping here again, before
419                  * retrieving the data
420                  */
421                 for (count = 0; count < MAX_COUNT; count++) {
422                         udelay(SLEEP_DURATION);
423                         rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
424                         if (rc == 0)
425                                 break;  /* success, break to skip sleep */
426                 }
427         } else {
428                 /*
429                  * Use a combined read for newer chips.
430                  * Unfortunately the smbus functions are not suitable due to
431                  * the 32 byte limit of the smbus.
432                  * Retries should usually not be needed, but are kept just to
433                  * be safe on the safe side.
434                  */
435                 for (count = 0; count < MAX_COUNT; count++) {
436                         rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
437                         if (rc == 0)
438                                 break;  /* break here to skip sleep */
439                         udelay(SLEEP_DURATION);
440                 }
441         }
442
443         /* Take care of 'guard time' */
444         udelay(SLEEP_DURATION);
445         if (rc)
446                 return -rc;
447
448         return 0;
449 }
450
451 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
452                 unsigned int sleep_time, u8 max_count)
453 {
454         int rc = 0;
455         int count;
456
457         for (count = 0; count < max_count; count++) {
458                 rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
459                 if (rc == 0)
460                         break;  /* Success, break to skip sleep */
461                 udelay(sleep_time);
462         }
463
464         /* take care of 'guard time' */
465         udelay(sleep_time);
466         if (rc)
467                 return -rc;
468
469         return 0;
470 }
471
472 /*
473  * iic_tpm_write() - write to TPM register
474  * @addr: register address to write to
475  * @buffer: containing data to be written
476  * @len: number of bytes to write
477  *
478  * Write len bytes from provided buffer to TPM register (little
479  * endian format, i.e. buffer[0] is written as first byte).
480  *
481  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
482  * values have to be swapped.
483  *
484  * NOTE: use this function instead of the iic_tpm_write_generic function.
485  *
486  * Return -EIO on error, 0 on success
487  */
488 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
489 {
490         return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
491                         MAX_COUNT);
492 }
493
494 /*
495  * This function is needed especially for the cleanup situation after
496  * sending TPM_READY
497  */
498 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
499 {
500         return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
501                         MAX_COUNT_LONG);
502 }
503
504 static int check_locality(struct tpm_chip *chip, int loc)
505 {
506         const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
507         u8 buf;
508         int rc;
509
510         rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
511         if (rc < 0)
512                 return rc;
513
514         if ((buf & mask) == mask) {
515                 chip->locality = loc;
516                 return loc;
517         }
518
519         return -1;
520 }
521
522 static void release_locality(struct tpm_chip *chip, int loc, int force)
523 {
524         const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
525         u8 buf;
526
527         if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
528                 return;
529
530         if (force || (buf & mask) == mask) {
531                 buf = TPM_ACCESS_ACTIVE_LOCALITY;
532                 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
533         }
534 }
535
536 static int request_locality(struct tpm_chip *chip, int loc)
537 {
538         unsigned long start, stop;
539         u8 buf = TPM_ACCESS_REQUEST_USE;
540         int rc;
541
542         if (check_locality(chip, loc) >= 0)
543                 return loc;  /* We already have the locality */
544
545         rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
546         if (rc)
547                 return rc;
548
549         /* Wait for burstcount */
550         start = get_timer(0);
551         stop = chip->timeout_a;
552         do {
553                 if (check_locality(chip, loc) >= 0)
554                         return loc;
555                 udelay(TPM_TIMEOUT * 1000);
556         } while (get_timer(start) < stop);
557
558         return -1;
559 }
560
561 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
562 {
563         /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
564         u8 buf;
565
566         if (iic_tpm_read(TPM_STS(chip->locality), &buf, 1) < 0)
567                 return 0;
568         else
569                 return buf;
570 }
571
572 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
573 {
574         int rc;
575
576         /* This causes the current command to be aborted */
577         u8 buf = TPM_STS_COMMAND_READY;
578
579         debug("%s\n", __func__);
580         rc = iic_tpm_write_long(TPM_STS(chip->locality), &buf, 1);
581         if (rc)
582                 debug("%s: rc=%d\n", __func__, rc);
583 }
584
585 static ssize_t get_burstcount(struct tpm_chip *chip)
586 {
587         unsigned long start, stop;
588         ssize_t burstcnt;
589         u8 addr, buf[3];
590
591         /* Wait for burstcount */
592         /* XXX: Which timeout value? Spec has 2 answers (c & d) */
593         start = get_timer(0);
594         stop = chip->timeout_d;
595         do {
596                 /* Note: STS is little endian */
597                 addr = TPM_STS(chip->locality) + 1;
598                 if (iic_tpm_read(addr, buf, 3) < 0)
599                         burstcnt = 0;
600                 else
601                         burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
602
603                 if (burstcnt)
604                         return burstcnt;
605                 udelay(TPM_TIMEOUT * 1000);
606         } while (get_timer(start) < stop);
607
608         return -EBUSY;
609 }
610
611 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
612                 int *status)
613 {
614         unsigned long start, stop;
615
616         /* Check current status */
617         *status = tpm_tis_i2c_status(chip);
618         if ((*status & mask) == mask)
619                 return 0;
620
621         start = get_timer(0);
622         stop = timeout;
623         do {
624                 udelay(TPM_TIMEOUT * 1000);
625                 *status = tpm_tis_i2c_status(chip);
626                 if ((*status & mask) == mask)
627                         return 0;
628         } while (get_timer(start) < stop);
629
630         return -ETIME;
631 }
632
633 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
634 {
635         size_t size = 0;
636         ssize_t burstcnt;
637         int rc;
638
639         while (size < count) {
640                 burstcnt = get_burstcount(chip);
641
642                 /* burstcount < 0 -> tpm is busy */
643                 if (burstcnt < 0)
644                         return burstcnt;
645
646                 /* Limit received data to max left */
647                 if (burstcnt > (count - size))
648                         burstcnt = count - size;
649
650                 rc = iic_tpm_read(TPM_DATA_FIFO(chip->locality),
651                                 &(buf[size]), burstcnt);
652                 if (rc == 0)
653                         size += burstcnt;
654         }
655
656         return size;
657 }
658
659 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
660 {
661         int size = 0;
662         int expected, status;
663
664         if (count < TPM_HEADER_SIZE) {
665                 size = -EIO;
666                 goto out;
667         }
668
669         /* Read first 10 bytes, including tag, paramsize, and result */
670         size = recv_data(chip, buf, TPM_HEADER_SIZE);
671         if (size < TPM_HEADER_SIZE) {
672                 error("Unable to read header\n");
673                 goto out;
674         }
675
676         expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
677         if ((size_t)expected > count) {
678                 error("Error size=%x, expected=%x, count=%x\n", size, expected,
679                       count);
680                 size = -EIO;
681                 goto out;
682         }
683
684         size += recv_data(chip, &buf[TPM_HEADER_SIZE],
685                         expected - TPM_HEADER_SIZE);
686         if (size < expected) {
687                 error("Unable to read remainder of result\n");
688                 size = -ETIME;
689                 goto out;
690         }
691
692         wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
693         if (status & TPM_STS_DATA_AVAIL) {  /* Retry? */
694                 error("Error left over data\n");
695                 size = -EIO;
696                 goto out;
697         }
698
699 out:
700         tpm_tis_i2c_ready(chip);
701         /*
702          * The TPM needs some time to clean up here,
703          * so we sleep rather than keeping the bus busy
704          */
705         udelay(2000);
706         release_locality(chip, chip->locality, 0);
707
708         return size;
709 }
710
711 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
712 {
713         int rc, status;
714         size_t burstcnt;
715         size_t count = 0;
716         int retry = 0;
717         u8 sts = TPM_STS_GO;
718
719         debug("%s: len=%d\n", __func__, len);
720         if (len > TPM_DEV_BUFSIZE)
721                 return -E2BIG;  /* Command is too long for our tpm, sorry */
722
723         if (request_locality(chip, 0) < 0)
724                 return -EBUSY;
725
726         status = tpm_tis_i2c_status(chip);
727         if ((status & TPM_STS_COMMAND_READY) == 0) {
728                 tpm_tis_i2c_ready(chip);
729                 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
730                                   chip->timeout_b, &status) < 0) {
731                         rc = -ETIME;
732                         goto out_err;
733                 }
734         }
735
736         burstcnt = get_burstcount(chip);
737
738         /* burstcount < 0 -> tpm is busy */
739         if (burstcnt < 0)
740                 return burstcnt;
741
742         while (count < len) {
743                 udelay(300);
744                 if (burstcnt > len - count)
745                         burstcnt = len - count;
746
747 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
748                 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
749                         burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
750 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
751
752                 rc = iic_tpm_write(TPM_DATA_FIFO(chip->locality),
753                                 &(buf[count]), burstcnt);
754                 if (rc == 0)
755                         count += burstcnt;
756                 else {
757                         debug("%s: error\n", __func__);
758                         if (retry++ > 10) {
759                                 rc = -EIO;
760                                 goto out_err;
761                         }
762                         rc = wait_for_stat(chip, TPM_STS_VALID,
763                                            chip->timeout_c, &status);
764                         if (rc)
765                                 goto out_err;
766
767                         if ((status & TPM_STS_DATA_EXPECT) == 0) {
768                                 rc = -EIO;
769                                 goto out_err;
770                         }
771                 }
772         }
773
774         /* Go and do it */
775         iic_tpm_write(TPM_STS(chip->locality), &sts, 1);
776         debug("done\n");
777
778         return len;
779
780 out_err:
781         debug("%s: out_err\n", __func__);
782         tpm_tis_i2c_ready(chip);
783         /*
784          * The TPM needs some time to clean up here,
785          * so we sleep rather than keeping the bus busy
786          */
787         udelay(2000);
788         release_locality(chip, chip->locality, 0);
789
790         return rc;
791 }
792
793 static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
794 {
795 #if CONFIG_IS_ENABLED(OF_CONTROL)
796         const void *blob = gd->fdt_blob;
797
798         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
799                 return SLB9645;
800
801         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
802                 return SLB9635;
803 #endif
804         return UNKNOWN;
805 }
806
807 static int tpm_tis_i2c_init(struct udevice *dev)
808 {
809         struct tpm_chip *chip = &g_chip;
810         u32 vendor;
811         u32 expected_did_vid;
812
813         g_chip.dev = dev;
814         g_chip.chip_type = tpm_tis_i2c_chip_type();
815         chip->is_open = 1;
816
817         /* Disable interrupts (not supported) */
818         chip->irq = 0;
819
820         /* Default timeouts */
821         chip->timeout_a = TIS_SHORT_TIMEOUT;
822         chip->timeout_b = TIS_LONG_TIMEOUT;
823         chip->timeout_c = TIS_SHORT_TIMEOUT;
824         chip->timeout_d = TIS_SHORT_TIMEOUT;
825         chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
826         chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
827         chip->req_canceled = TPM_STS_COMMAND_READY;
828
829         if (request_locality(chip, 0) < 0)
830                 return  -ENODEV;
831
832         /* Read four bytes from DID_VID register */
833         if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
834                 release_locality(chip, 0, 1);
835                 return -EIO;
836         }
837
838         if (g_chip.chip_type == SLB9635) {
839                 vendor = be32_to_cpu(vendor);
840                 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
841         } else {
842                 /* device id and byte order has changed for newer i2c tpms */
843                 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
844         }
845
846         if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
847                 error("Vendor id did not match! ID was %08x\n", vendor);
848                 return -ENODEV;
849         }
850
851         debug("1.2 TPM (chip type %s device-id 0x%X)\n",
852               chip_name[g_chip.chip_type], vendor >> 16);
853
854         /*
855          * A timeout query to TPM can be placed here.
856          * Standard timeout values are used so far
857          */
858
859         return 0;
860 }
861
862 /* Returns max number of milliseconds to wait */
863 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
864                 u32 ordinal)
865 {
866         int duration_idx = TPM_UNDEFINED;
867         int duration = 0;
868
869         if (ordinal < TPM_MAX_ORDINAL) {
870                 duration_idx = tpm_ordinal_duration[ordinal];
871         } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
872                         TPM_MAX_PROTECTED_ORDINAL) {
873                 duration_idx = tpm_protected_ordinal_duration[
874                                 ordinal & TPM_PROTECTED_ORDINAL_MASK];
875         }
876
877         if (duration_idx != TPM_UNDEFINED)
878                 duration = chip->duration[duration_idx];
879
880         if (duration <= 0)
881                 return 2 * 60 * HZ; /* Two minutes timeout */
882         else
883                 return duration;
884 }
885
886 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
887 {
888         int rc;
889         u32 count, ordinal;
890         unsigned long start, stop;
891
892         struct tpm_chip *chip = &g_chip;
893
894         /* switch endianess: big->little */
895         count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
896         ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
897
898         if (count == 0) {
899                 error("no data\n");
900                 return -ENODATA;
901         }
902         if (count > bufsiz) {
903                 error("invalid count value %x %zx\n", count, bufsiz);
904                 return -E2BIG;
905         }
906
907         debug("Calling send\n");
908         rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
909         debug("   ... done calling send\n");
910         if (rc < 0) {
911                 error("tpm_transmit: tpm_send: error %d\n", rc);
912                 goto out;
913         }
914
915         if (chip->irq)
916                 goto out_recv;
917
918         start = get_timer(0);
919         stop = tpm_calc_ordinal_duration(chip, ordinal);
920         do {
921                 debug("waiting for status... %ld %ld\n", start, stop);
922                 u8 status = tpm_tis_i2c_status(chip);
923                 if ((status & chip->req_complete_mask) ==
924                     chip->req_complete_val) {
925                         debug("...got it;\n");
926                         goto out_recv;
927                 }
928
929                 if (status == chip->req_canceled) {
930                         error("Operation Canceled\n");
931                         rc = -ECANCELED;
932                         goto out;
933                 }
934                 udelay(TPM_TIMEOUT * 1000);
935         } while (get_timer(start) < stop);
936
937         tpm_tis_i2c_ready(chip);
938         error("Operation Timed out\n");
939         rc = -ETIME;
940         goto out;
941
942 out_recv:
943         debug("out_recv: reading response...\n");
944         rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
945         if (rc < 0)
946                 error("tpm_transmit: tpm_recv: error %d\n", rc);
947
948 out:
949         return rc;
950 }
951
952 static int tpm_open_dev(struct udevice *dev)
953 {
954         int rc;
955
956         debug("%s: start\n", __func__);
957         if (g_chip.is_open)
958                 return -EBUSY;
959         rc = tpm_tis_i2c_init(dev);
960         if (rc < 0)
961                 g_chip.is_open = 0;
962         return rc;
963 }
964
965 static void tpm_close(void)
966 {
967         if (g_chip.is_open) {
968                 release_locality(&g_chip, g_chip.locality, 1);
969                 g_chip.is_open = 0;
970         }
971 }
972
973 /**
974  * Decode TPM configuration.
975  *
976  * @param dev   Returns a configuration of TPM device
977  * @return 0 if ok, -1 on error
978  */
979 static int tpm_decode_config(struct tpm *dev)
980 {
981         const void *blob = gd->fdt_blob;
982         struct udevice *bus;
983         int chip_addr;
984         int parent;
985         int node;
986         int ret;
987
988         node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
989         if (node < 0) {
990                 node = fdtdec_next_compatible(blob, 0,
991                                 COMPAT_INFINEON_SLB9645_TPM);
992         }
993         if (node < 0) {
994                 debug("%s: Node not found\n", __func__);
995                 return -1;
996         }
997         parent = fdt_parent_offset(blob, node);
998         if (parent < 0) {
999                 debug("%s: Cannot find node parent\n", __func__);
1000                 return -1;
1001         }
1002
1003         /*
1004          * TODO(sjg@chromium.org): Remove this when driver model supports
1005          * TPMs
1006          */
1007         ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1008         if (ret) {
1009                 debug("Cannot find bus for node '%s: ret=%d'\n",
1010                       fdt_get_name(blob, parent, NULL), ret);
1011                 return ret;
1012         }
1013
1014         chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1015         if (chip_addr == -1) {
1016                 debug("Cannot find reg property for node '%s: ret=%d'\n",
1017                       fdt_get_name(blob, node, NULL), ret);
1018                 return ret;
1019         }
1020         /*
1021          * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1022          * in iic_tpm_read() so the offset length needs to be 0 here.
1023          */
1024         ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
1025         if (ret) {
1026                 debug("Cannot find device for node '%s: ret=%d'\n",
1027                       fdt_get_name(blob, node, NULL), ret);
1028                 return ret;
1029         }
1030
1031         return 0;
1032 }
1033
1034 int tis_init(void)
1035 {
1036         if (tpm.inited)
1037                 return 0;
1038
1039         if (tpm_decode_config(&tpm))
1040                 return -1;
1041
1042         debug("%s: done\n", __func__);
1043
1044         tpm.inited = 1;
1045
1046         return 0;
1047 }
1048
1049 int tis_open(void)
1050 {
1051         int rc;
1052
1053         if (!tpm.inited)
1054                 return -1;
1055
1056         rc = tpm_open_dev(tpm.dev);
1057
1058         return rc;
1059 }
1060
1061 int tis_close(void)
1062 {
1063         if (!tpm.inited)
1064                 return -1;
1065
1066         tpm_close();
1067
1068         return 0;
1069 }
1070
1071 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1072                 uint8_t *recvbuf, size_t *rbuf_len)
1073 {
1074         int len;
1075         uint8_t buf[4096];
1076
1077         if (!tpm.inited)
1078                 return -1;
1079
1080         if (sizeof(buf) < sbuf_size)
1081                 return -1;
1082
1083         memcpy(buf, sendbuf, sbuf_size);
1084
1085         len = tpm_transmit(buf, sbuf_size);
1086
1087         if (len < 10) {
1088                 *rbuf_len = 0;
1089                 return -1;
1090         }
1091
1092         memcpy(recvbuf, buf, len);
1093         *rbuf_len = len;
1094
1095         return 0;
1096 }