tpm: Move the I2C TPM code into one file
[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         .status = tpm_tis_i2c_status,
814         .recv = tpm_tis_i2c_recv,
815         .send = tpm_tis_i2c_send,
816         .cancel = tpm_tis_i2c_ready,
817         .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
818         .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
819         .req_canceled = TPM_STS_COMMAND_READY,
820 };
821
822
823 static enum i2c_chip_type tpm_vendor_chip_type(void)
824 {
825 #if CONFIG_IS_ENABLED(OF_CONTROL)
826         const void *blob = gd->fdt_blob;
827
828         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
829                 return SLB9645;
830
831         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
832                 return SLB9635;
833 #endif
834         return UNKNOWN;
835 }
836
837 int tpm_vendor_init(struct udevice *dev)
838 {
839         struct tpm_chip *chip;
840         u32 vendor;
841         u32 expected_did_vid;
842
843         tpm_dev.dev = dev;
844         tpm_dev.chip_type = tpm_vendor_chip_type();
845
846         chip = tpm_register_hardware(&tpm_tis_i2c);
847         if (chip < 0)
848                 return -ENODEV;
849
850         /* Disable interrupts (not supported) */
851         chip->vendor.irq = 0;
852
853         /* Default timeouts */
854         chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
855         chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
856         chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
857         chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
858
859         if (request_locality(chip, 0) < 0)
860                 return  -ENODEV;
861
862         /* Read four bytes from DID_VID register */
863         if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
864                 release_locality(chip, 0, 1);
865                 return -EIO;
866         }
867
868         if (tpm_dev.chip_type == SLB9635) {
869                 vendor = be32_to_cpu(vendor);
870                 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
871         } else {
872                 /* device id and byte order has changed for newer i2c tpms */
873                 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
874         }
875
876         if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
877                 error("Vendor id did not match! ID was %08x\n", vendor);
878                 return -ENODEV;
879         }
880
881         debug("1.2 TPM (chip type %s device-id 0x%X)\n",
882               chip_name[tpm_dev.chip_type], vendor >> 16);
883
884         /*
885          * A timeout query to TPM can be placed here.
886          * Standard timeout values are used so far
887          */
888
889         return 0;
890 }
891
892 void tpm_vendor_cleanup(struct tpm_chip *chip)
893 {
894         release_locality(chip, chip->vendor.locality, 1);
895 }
896
897 /* Returns max number of milliseconds to wait */
898 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
899                 u32 ordinal)
900 {
901         int duration_idx = TPM_UNDEFINED;
902         int duration = 0;
903
904         if (ordinal < TPM_MAX_ORDINAL) {
905                 duration_idx = tpm_ordinal_duration[ordinal];
906         } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
907                         TPM_MAX_PROTECTED_ORDINAL) {
908                 duration_idx = tpm_protected_ordinal_duration[
909                                 ordinal & TPM_PROTECTED_ORDINAL_MASK];
910         }
911
912         if (duration_idx != TPM_UNDEFINED)
913                 duration = chip->vendor.duration[duration_idx];
914
915         if (duration <= 0)
916                 return 2 * 60 * HZ; /* Two minutes timeout */
917         else
918                 return duration;
919 }
920
921 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
922 {
923         int rc;
924         u32 count, ordinal;
925         unsigned long start, stop;
926
927         struct tpm_chip *chip = &g_chip;
928
929         /* switch endianess: big->little */
930         count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
931         ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
932
933         if (count == 0) {
934                 error("no data\n");
935                 return -ENODATA;
936         }
937         if (count > bufsiz) {
938                 error("invalid count value %x %zx\n", count, bufsiz);
939                 return -E2BIG;
940         }
941
942         debug("Calling send\n");
943         rc = chip->vendor.send(chip, (u8 *)buf, count);
944         debug("   ... done calling send\n");
945         if (rc < 0) {
946                 error("tpm_transmit: tpm_send: error %d\n", rc);
947                 goto out;
948         }
949
950         if (chip->vendor.irq)
951                 goto out_recv;
952
953         start = get_timer(0);
954         stop = tpm_calc_ordinal_duration(chip, ordinal);
955         do {
956                 debug("waiting for status... %ld %ld\n", start, stop);
957                 u8 status = chip->vendor.status(chip);
958                 if ((status & chip->vendor.req_complete_mask) ==
959                     chip->vendor.req_complete_val) {
960                         debug("...got it;\n");
961                         goto out_recv;
962                 }
963
964                 if (status == chip->vendor.req_canceled) {
965                         error("Operation Canceled\n");
966                         rc = -ECANCELED;
967                         goto out;
968                 }
969                 udelay(TPM_TIMEOUT * 1000);
970         } while (get_timer(start) < stop);
971
972         chip->vendor.cancel(chip);
973         error("Operation Timed out\n");
974         rc = -ETIME;
975         goto out;
976
977 out_recv:
978         debug("out_recv: reading response...\n");
979         rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
980         if (rc < 0)
981                 error("tpm_transmit: tpm_recv: error %d\n", rc);
982
983 out:
984         return rc;
985 }
986
987 static int tpm_open_dev(struct udevice *dev)
988 {
989         int rc;
990
991         debug("%s: start\n", __func__);
992         if (g_chip.is_open)
993                 return -EBUSY;
994         rc = tpm_vendor_init(dev);
995         if (rc < 0)
996                 g_chip.is_open = 0;
997         return rc;
998 }
999
1000 static void tpm_close(void)
1001 {
1002         if (g_chip.is_open) {
1003                 tpm_vendor_cleanup(&g_chip);
1004                 g_chip.is_open = 0;
1005         }
1006 }
1007
1008 /**
1009  * Decode TPM configuration.
1010  *
1011  * @param dev   Returns a configuration of TPM device
1012  * @return 0 if ok, -1 on error
1013  */
1014 static int tpm_decode_config(struct tpm *dev)
1015 {
1016         const void *blob = gd->fdt_blob;
1017         struct udevice *bus;
1018         int chip_addr;
1019         int parent;
1020         int node;
1021         int ret;
1022
1023         node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
1024         if (node < 0) {
1025                 node = fdtdec_next_compatible(blob, 0,
1026                                 COMPAT_INFINEON_SLB9645_TPM);
1027         }
1028         if (node < 0) {
1029                 debug("%s: Node not found\n", __func__);
1030                 return -1;
1031         }
1032         parent = fdt_parent_offset(blob, node);
1033         if (parent < 0) {
1034                 debug("%s: Cannot find node parent\n", __func__);
1035                 return -1;
1036         }
1037
1038         /*
1039          * TODO(sjg@chromium.org): Remove this when driver model supports
1040          * TPMs
1041          */
1042         ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1043         if (ret) {
1044                 debug("Cannot find bus for node '%s: ret=%d'\n",
1045                       fdt_get_name(blob, parent, NULL), ret);
1046                 return ret;
1047         }
1048
1049         chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1050         if (chip_addr == -1) {
1051                 debug("Cannot find reg property for node '%s: ret=%d'\n",
1052                       fdt_get_name(blob, node, NULL), ret);
1053                 return ret;
1054         }
1055         /*
1056          * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1057          * in iic_tpm_read() so the offset length needs to be 0 here.
1058          */
1059         ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
1060         if (ret) {
1061                 debug("Cannot find device for node '%s: ret=%d'\n",
1062                       fdt_get_name(blob, node, NULL), ret);
1063                 return ret;
1064         }
1065
1066         return 0;
1067 }
1068
1069 struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
1070 {
1071         struct tpm_chip *chip;
1072
1073         /* Driver specific per-device data */
1074         chip = &g_chip;
1075         memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1076         chip->is_open = 1;
1077
1078         return chip;
1079 }
1080
1081 int tis_init(void)
1082 {
1083         if (tpm.inited)
1084                 return 0;
1085
1086         if (tpm_decode_config(&tpm))
1087                 return -1;
1088
1089         debug("%s: done\n", __func__);
1090
1091         tpm.inited = 1;
1092
1093         return 0;
1094 }
1095
1096 int tis_open(void)
1097 {
1098         int rc;
1099
1100         if (!tpm.inited)
1101                 return -1;
1102
1103         rc = tpm_open_dev(tpm.dev);
1104
1105         return rc;
1106 }
1107
1108 int tis_close(void)
1109 {
1110         if (!tpm.inited)
1111                 return -1;
1112
1113         tpm_close();
1114
1115         return 0;
1116 }
1117
1118 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1119                 uint8_t *recvbuf, size_t *rbuf_len)
1120 {
1121         int len;
1122         uint8_t buf[4096];
1123
1124         if (!tpm.inited)
1125                 return -1;
1126
1127         if (sizeof(buf) < sbuf_size)
1128                 return -1;
1129
1130         memcpy(buf, sendbuf, sbuf_size);
1131
1132         len = tpm_transmit(buf, sbuf_size);
1133
1134         if (len < 10) {
1135                 *rbuf_len = 0;
1136                 return -1;
1137         }
1138
1139         memcpy(recvbuf, buf, len);
1140         *rbuf_len = len;
1141
1142         return 0;
1143 }