]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/tpm/tpm_tis_i2c.c
tpm: tpm_tis_i2c: Drop unnecessary methods
[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 buffer size supported by our tpm */
38 #define TPM_DEV_BUFSIZE         1260
39
40 /* 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
47  * transtion to the ready state may take some time, but it is unpredictable
48  * how long it will take.
49  */
50 #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
57 enum 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
64 enum 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
72 enum tis_defaults {
73         TIS_SHORT_TIMEOUT               = 750,  /* ms */
74         TIS_LONG_TIMEOUT                = 2000, /* ms */
75 };
76
77 /* expected value for DIDVID register */
78 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
79 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
80
81 enum i2c_chip_type {
82         SLB9635,
83         SLB9645,
84         UNKNOWN,
85 };
86
87 static const char * const chip_name[] = {
88         [SLB9635] = "slb9635tt",
89         [SLB9645] = "slb9645tt",
90         [UNKNOWN] = "unknown/fallback to slb9635",
91 };
92
93 #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
98 enum 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  */
126 static 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
141 static 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 */
388 struct tpm {
389         struct udevice *dev;
390         char inited;
391 } tpm;
392
393 /* Global structure for tpm chip data */
394 static struct tpm_chip g_chip;
395
396 /* Structure to store I2C TPM specific stuff */
397 struct tpm_dev {
398         struct udevice *dev;
399         u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)];  /* Max buffer size + addr */
400         enum i2c_chip_type chip_type;
401 };
402
403 static struct tpm_dev tpm_dev;
404
405 /*
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  */
419 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
420 {
421         int rc;
422         int count;
423         uint32_t addrbuf = addr;
424
425         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++) {
428                         rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
429                         if (rc == 0)
430                                 break;  /* Success, break to skip sleep */
431                         udelay(SLEEP_DURATION);
432                 }
433                 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);
442                         rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
443                         if (rc == 0)
444                                 break;  /* success, break to skip sleep */
445                 }
446         } else {
447                 /*
448                  * Use a combined read for newer chips.
449                  * Unfortunately the smbus functions are not suitable due to
450                  * the 32 byte limit of the smbus.
451                  * Retries should usually not be needed, but are kept just to
452                  * be safe on the safe side.
453                  */
454                 for (count = 0; count < MAX_COUNT; count++) {
455                         rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
456                         if (rc == 0)
457                                 break;  /* break here to skip sleep */
458                         udelay(SLEEP_DURATION);
459                 }
460         }
461
462         /* Take care of 'guard time' */
463         udelay(SLEEP_DURATION);
464         if (rc)
465                 return -rc;
466
467         return 0;
468 }
469
470 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
471                 unsigned int sleep_time, u8 max_count)
472 {
473         int rc = 0;
474         int count;
475
476         for (count = 0; count < max_count; count++) {
477                 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
478                 if (rc == 0)
479                         break;  /* Success, break to skip sleep */
480                 udelay(sleep_time);
481         }
482
483         /* take care of 'guard time' */
484         udelay(sleep_time);
485         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  */
507 static 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
516  */
517 static 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
523 static int check_locality(struct tpm_chip *chip, int loc)
524 {
525         const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
526         u8 buf;
527         int rc;
528
529         rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
530         if (rc < 0)
531                 return rc;
532
533         if ((buf & mask) == mask) {
534                 chip->vendor.locality = loc;
535                 return loc;
536         }
537
538         return -1;
539 }
540
541 static void release_locality(struct tpm_chip *chip, int loc, int force)
542 {
543         const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
544         u8 buf;
545
546         if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
547                 return;
548
549         if (force || (buf & mask) == mask) {
550                 buf = TPM_ACCESS_ACTIVE_LOCALITY;
551                 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
552         }
553 }
554
555 static int request_locality(struct tpm_chip *chip, int loc)
556 {
557         unsigned long start, stop;
558         u8 buf = TPM_ACCESS_REQUEST_USE;
559         int rc;
560
561         if (check_locality(chip, loc) >= 0)
562                 return loc;  /* We already have the locality */
563
564         rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
565         if (rc)
566                 return rc;
567
568         /* Wait for burstcount */
569         start = get_timer(0);
570         stop = chip->vendor.timeout_a;
571         do {
572                 if (check_locality(chip, loc) >= 0)
573                         return loc;
574                 udelay(TPM_TIMEOUT * 1000);
575         } while (get_timer(start) < stop);
576
577         return -1;
578 }
579
580 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
581 {
582         /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
583         u8 buf;
584
585         if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
586                 return 0;
587         else
588                 return buf;
589 }
590
591 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
592 {
593         int rc;
594
595         /* This causes the current command to be aborted */
596         u8 buf = TPM_STS_COMMAND_READY;
597
598         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);
602 }
603
604 static ssize_t get_burstcount(struct tpm_chip *chip)
605 {
606         unsigned long start, stop;
607         ssize_t burstcnt;
608         u8 addr, buf[3];
609
610         /* Wait for burstcount */
611         /* XXX: Which timeout value? Spec has 2 answers (c & d) */
612         start = get_timer(0);
613         stop = chip->vendor.timeout_d;
614         do {
615                 /* Note: STS is little endian */
616                 addr = TPM_STS(chip->vendor.locality) + 1;
617                 if (iic_tpm_read(addr, buf, 3) < 0)
618                         burstcnt = 0;
619                 else
620                         burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
621
622                 if (burstcnt)
623                         return burstcnt;
624                 udelay(TPM_TIMEOUT * 1000);
625         } while (get_timer(start) < stop);
626
627         return -EBUSY;
628 }
629
630 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
631                 int *status)
632 {
633         unsigned long start, stop;
634
635         /* Check current status */
636         *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 {
643                 udelay(TPM_TIMEOUT * 1000);
644                 *status = tpm_tis_i2c_status(chip);
645                 if ((*status & mask) == mask)
646                         return 0;
647         } while (get_timer(start) < stop);
648
649         return -ETIME;
650 }
651
652 static 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
661                 /* burstcount < 0 -> tpm is busy */
662                 if (burstcnt < 0)
663                         return burstcnt;
664
665                 /* Limit received data to max left */
666                 if (burstcnt > (count - size))
667                         burstcnt = count - size;
668
669                 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
670                                 &(buf[size]), burstcnt);
671                 if (rc == 0)
672                         size += burstcnt;
673         }
674
675         return size;
676 }
677
678 static 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
688         /* Read first 10 bytes, including tag, paramsize, and result */
689         size = recv_data(chip, buf, TPM_HEADER_SIZE);
690         if (size < TPM_HEADER_SIZE) {
691                 error("Unable to read header\n");
692                 goto out;
693         }
694
695         expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
696         if ((size_t)expected > count) {
697                 error("Error size=%x, expected=%x, count=%x\n", size, expected,
698                       count);
699                 size = -EIO;
700                 goto out;
701         }
702
703         size += recv_data(chip, &buf[TPM_HEADER_SIZE],
704                         expected - TPM_HEADER_SIZE);
705         if (size < expected) {
706                 error("Unable to read remainder of result\n");
707                 size = -ETIME;
708                 goto out;
709         }
710
711         wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
712         if (status & TPM_STS_DATA_AVAIL) {  /* Retry? */
713                 error("Error left over data\n");
714                 size = -EIO;
715                 goto out;
716         }
717
718 out:
719         tpm_tis_i2c_ready(chip);
720         /*
721          * The TPM needs some time to clean up here,
722          * 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
730 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
731 {
732         int rc, status;
733         size_t burstcnt;
734         size_t count = 0;
735         int retry = 0;
736         u8 sts = TPM_STS_GO;
737
738         debug("%s: len=%d\n", __func__, len);
739         if (len > TPM_DEV_BUFSIZE)
740                 return -E2BIG;  /* Command is too long for our tpm, sorry */
741
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);
748                 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
749                                   chip->vendor.timeout_b, &status) < 0) {
750                         rc = -ETIME;
751                         goto out_err;
752                 }
753         }
754
755         burstcnt = get_burstcount(chip);
756
757         /* burstcount < 0 -> tpm is busy */
758         if (burstcnt < 0)
759                 return burstcnt;
760
761         while (count < len) {
762                 udelay(300);
763                 if (burstcnt > len - count)
764                         burstcnt = len - count;
765
766 #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 */
770
771                 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
772                                 &(buf[count]), burstcnt);
773                 if (rc == 0)
774                         count += burstcnt;
775                 else {
776                         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;
785
786                         if ((status & TPM_STS_DATA_EXPECT) == 0) {
787                                 rc = -EIO;
788                                 goto out_err;
789                         }
790                 }
791         }
792
793         /* Go and do it */
794         iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
795         debug("done\n");
796
797         return len;
798
799 out_err:
800         debug("%s: out_err\n", __func__);
801         tpm_tis_i2c_ready(chip);
802         /*
803          * The TPM needs some time to clean up here,
804          * 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
812 static struct tpm_vendor_specific tpm_tis_i2c = {
813         .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
818
819 static enum i2c_chip_type tpm_vendor_chip_type(void)
820 {
821 #if CONFIG_IS_ENABLED(OF_CONTROL)
822         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
833 int tpm_vendor_init(struct udevice *dev)
834 {
835         struct tpm_chip *chip;
836         u32 vendor;
837         u32 expected_did_vid;
838
839         tpm_dev.dev = dev;
840         tpm_dev.chip_type = tpm_vendor_chip_type();
841
842         chip = tpm_register_hardware(&tpm_tis_i2c);
843         if (chip < 0)
844                 return -ENODEV;
845
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
855         if (request_locality(chip, 0) < 0)
856                 return  -ENODEV;
857
858         /* Read four bytes from DID_VID register */
859         if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
860                 release_locality(chip, 0, 1);
861                 return -EIO;
862         }
863
864         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         }
871
872         if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
873                 error("Vendor id did not match! ID was %08x\n", vendor);
874                 return -ENODEV;
875         }
876
877         debug("1.2 TPM (chip type %s device-id 0x%X)\n",
878               chip_name[tpm_dev.chip_type], vendor >> 16);
879
880         /*
881          * A timeout query to TPM can be placed here.
882          * Standard timeout values are used so far
883          */
884
885         return 0;
886 }
887
888 void tpm_vendor_cleanup(struct tpm_chip *chip)
889 {
890         release_locality(chip, chip->vendor.locality, 1);
891 }
892
893 /* Returns max number of milliseconds to wait */
894 static 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
917 static 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");
939         rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
940         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);
953                 u8 status = tpm_tis_i2c_status(chip);
954                 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
968         tpm_tis_i2c_ready(chip);
969         error("Operation Timed out\n");
970         rc = -ETIME;
971         goto out;
972
973 out_recv:
974         debug("out_recv: reading response...\n");
975         rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
976         if (rc < 0)
977                 error("tpm_transmit: tpm_recv: error %d\n", rc);
978
979 out:
980         return rc;
981 }
982
983 static 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
996 static 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  */
1010 static 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
1065 struct 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
1077 int 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
1092 int 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
1104 int tis_close(void)
1105 {
1106         if (!tpm.inited)
1107                 return -1;
1108
1109         tpm_close();
1110
1111         return 0;
1112 }
1113
1114 int 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 }