]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/tpm/tpm_tis_i2c.c
438a22160747ee4414384a5d404b0a2223c4ad34
[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_private.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 /* Structure to store I2C TPM specific stuff */
99 struct tpm_dev {
100         struct udevice *dev;
101         u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)];  /* Max buffer size + addr */
102         enum i2c_chip_type chip_type;
103 };
104
105 static struct tpm_dev tpm_dev;
106
107 /*
108  * iic_tpm_read() - read from TPM register
109  * @addr: register address to read from
110  * @buffer: provided by caller
111  * @len: number of bytes to read
112  *
113  * Read len bytes from TPM register and put them into
114  * buffer (little-endian format, i.e. first byte is put into buffer[0]).
115  *
116  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
117  * values have to be swapped.
118  *
119  * Return -EIO on error, 0 on success.
120  */
121 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
122 {
123         int rc;
124         int count;
125         uint32_t addrbuf = addr;
126
127         if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
128                 /* slb9635 protocol should work in both cases */
129                 for (count = 0; count < MAX_COUNT; count++) {
130                         rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
131                         if (rc == 0)
132                                 break;  /* Success, break to skip sleep */
133                         udelay(SLEEP_DURATION);
134                 }
135                 if (rc)
136                         return -rc;
137
138                 /* After the TPM has successfully received the register address
139                  * it needs some time, thus we're sleeping here again, before
140                  * retrieving the data
141                  */
142                 for (count = 0; count < MAX_COUNT; count++) {
143                         udelay(SLEEP_DURATION);
144                         rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
145                         if (rc == 0)
146                                 break;  /* success, break to skip sleep */
147                 }
148         } else {
149                 /*
150                  * Use a combined read for newer chips.
151                  * Unfortunately the smbus functions are not suitable due to
152                  * the 32 byte limit of the smbus.
153                  * Retries should usually not be needed, but are kept just to
154                  * be safe on the safe side.
155                  */
156                 for (count = 0; count < MAX_COUNT; count++) {
157                         rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
158                         if (rc == 0)
159                                 break;  /* break here to skip sleep */
160                         udelay(SLEEP_DURATION);
161                 }
162         }
163
164         /* Take care of 'guard time' */
165         udelay(SLEEP_DURATION);
166         if (rc)
167                 return -rc;
168
169         return 0;
170 }
171
172 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
173                 unsigned int sleep_time, u8 max_count)
174 {
175         int rc = 0;
176         int count;
177
178         for (count = 0; count < max_count; count++) {
179                 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
180                 if (rc == 0)
181                         break;  /* Success, break to skip sleep */
182                 udelay(sleep_time);
183         }
184
185         /* take care of 'guard time' */
186         udelay(sleep_time);
187         if (rc)
188                 return -rc;
189
190         return 0;
191 }
192
193 /*
194  * iic_tpm_write() - write to TPM register
195  * @addr: register address to write to
196  * @buffer: containing data to be written
197  * @len: number of bytes to write
198  *
199  * Write len bytes from provided buffer to TPM register (little
200  * endian format, i.e. buffer[0] is written as first byte).
201  *
202  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
203  * values have to be swapped.
204  *
205  * NOTE: use this function instead of the iic_tpm_write_generic function.
206  *
207  * Return -EIO on error, 0 on success
208  */
209 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
210 {
211         return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
212                         MAX_COUNT);
213 }
214
215 /*
216  * This function is needed especially for the cleanup situation after
217  * sending TPM_READY
218  */
219 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
220 {
221         return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
222                         MAX_COUNT_LONG);
223 }
224
225 static int check_locality(struct tpm_chip *chip, int loc)
226 {
227         const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
228         u8 buf;
229         int rc;
230
231         rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
232         if (rc < 0)
233                 return rc;
234
235         if ((buf & mask) == mask) {
236                 chip->vendor.locality = loc;
237                 return loc;
238         }
239
240         return -1;
241 }
242
243 static void release_locality(struct tpm_chip *chip, int loc, int force)
244 {
245         const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
246         u8 buf;
247
248         if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
249                 return;
250
251         if (force || (buf & mask) == mask) {
252                 buf = TPM_ACCESS_ACTIVE_LOCALITY;
253                 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
254         }
255 }
256
257 static int request_locality(struct tpm_chip *chip, int loc)
258 {
259         unsigned long start, stop;
260         u8 buf = TPM_ACCESS_REQUEST_USE;
261         int rc;
262
263         if (check_locality(chip, loc) >= 0)
264                 return loc;  /* We already have the locality */
265
266         rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
267         if (rc)
268                 return rc;
269
270         /* Wait for burstcount */
271         start = get_timer(0);
272         stop = chip->vendor.timeout_a;
273         do {
274                 if (check_locality(chip, loc) >= 0)
275                         return loc;
276                 udelay(TPM_TIMEOUT * 1000);
277         } while (get_timer(start) < stop);
278
279         return -1;
280 }
281
282 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
283 {
284         /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
285         u8 buf;
286
287         if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
288                 return 0;
289         else
290                 return buf;
291 }
292
293 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
294 {
295         int rc;
296
297         /* This causes the current command to be aborted */
298         u8 buf = TPM_STS_COMMAND_READY;
299
300         debug("%s\n", __func__);
301         rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
302         if (rc)
303                 debug("%s: rc=%d\n", __func__, rc);
304 }
305
306 static ssize_t get_burstcount(struct tpm_chip *chip)
307 {
308         unsigned long start, stop;
309         ssize_t burstcnt;
310         u8 addr, buf[3];
311
312         /* Wait for burstcount */
313         /* XXX: Which timeout value? Spec has 2 answers (c & d) */
314         start = get_timer(0);
315         stop = chip->vendor.timeout_d;
316         do {
317                 /* Note: STS is little endian */
318                 addr = TPM_STS(chip->vendor.locality) + 1;
319                 if (iic_tpm_read(addr, buf, 3) < 0)
320                         burstcnt = 0;
321                 else
322                         burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
323
324                 if (burstcnt)
325                         return burstcnt;
326                 udelay(TPM_TIMEOUT * 1000);
327         } while (get_timer(start) < stop);
328
329         return -EBUSY;
330 }
331
332 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
333                 int *status)
334 {
335         unsigned long start, stop;
336
337         /* Check current status */
338         *status = tpm_tis_i2c_status(chip);
339         if ((*status & mask) == mask)
340                 return 0;
341
342         start = get_timer(0);
343         stop = timeout;
344         do {
345                 udelay(TPM_TIMEOUT * 1000);
346                 *status = tpm_tis_i2c_status(chip);
347                 if ((*status & mask) == mask)
348                         return 0;
349         } while (get_timer(start) < stop);
350
351         return -ETIME;
352 }
353
354 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
355 {
356         size_t size = 0;
357         ssize_t burstcnt;
358         int rc;
359
360         while (size < count) {
361                 burstcnt = get_burstcount(chip);
362
363                 /* burstcount < 0 -> tpm is busy */
364                 if (burstcnt < 0)
365                         return burstcnt;
366
367                 /* Limit received data to max left */
368                 if (burstcnt > (count - size))
369                         burstcnt = count - size;
370
371                 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
372                                 &(buf[size]), burstcnt);
373                 if (rc == 0)
374                         size += burstcnt;
375         }
376
377         return size;
378 }
379
380 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
381 {
382         int size = 0;
383         int expected, status;
384
385         if (count < TPM_HEADER_SIZE) {
386                 size = -EIO;
387                 goto out;
388         }
389
390         /* Read first 10 bytes, including tag, paramsize, and result */
391         size = recv_data(chip, buf, TPM_HEADER_SIZE);
392         if (size < TPM_HEADER_SIZE) {
393                 error("Unable to read header\n");
394                 goto out;
395         }
396
397         expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
398         if ((size_t)expected > count) {
399                 error("Error size=%x, expected=%x, count=%x\n", size, expected,
400                       count);
401                 size = -EIO;
402                 goto out;
403         }
404
405         size += recv_data(chip, &buf[TPM_HEADER_SIZE],
406                         expected - TPM_HEADER_SIZE);
407         if (size < expected) {
408                 error("Unable to read remainder of result\n");
409                 size = -ETIME;
410                 goto out;
411         }
412
413         wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
414         if (status & TPM_STS_DATA_AVAIL) {  /* Retry? */
415                 error("Error left over data\n");
416                 size = -EIO;
417                 goto out;
418         }
419
420 out:
421         tpm_tis_i2c_ready(chip);
422         /*
423          * The TPM needs some time to clean up here,
424          * so we sleep rather than keeping the bus busy
425          */
426         udelay(2000);
427         release_locality(chip, chip->vendor.locality, 0);
428
429         return size;
430 }
431
432 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
433 {
434         int rc, status;
435         size_t burstcnt;
436         size_t count = 0;
437         int retry = 0;
438         u8 sts = TPM_STS_GO;
439
440         debug("%s: len=%d\n", __func__, len);
441         if (len > TPM_DEV_BUFSIZE)
442                 return -E2BIG;  /* Command is too long for our tpm, sorry */
443
444         if (request_locality(chip, 0) < 0)
445                 return -EBUSY;
446
447         status = tpm_tis_i2c_status(chip);
448         if ((status & TPM_STS_COMMAND_READY) == 0) {
449                 tpm_tis_i2c_ready(chip);
450                 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
451                                   chip->vendor.timeout_b, &status) < 0) {
452                         rc = -ETIME;
453                         goto out_err;
454                 }
455         }
456
457         burstcnt = get_burstcount(chip);
458
459         /* burstcount < 0 -> tpm is busy */
460         if (burstcnt < 0)
461                 return burstcnt;
462
463         while (count < len) {
464                 udelay(300);
465                 if (burstcnt > len - count)
466                         burstcnt = len - count;
467
468 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
469                 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
470                         burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
471 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
472
473                 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
474                                 &(buf[count]), burstcnt);
475                 if (rc == 0)
476                         count += burstcnt;
477                 else {
478                         debug("%s: error\n", __func__);
479                         if (retry++ > 10) {
480                                 rc = -EIO;
481                                 goto out_err;
482                         }
483                         rc = wait_for_stat(chip, TPM_STS_VALID,
484                                            chip->vendor.timeout_c, &status);
485                         if (rc)
486                                 goto out_err;
487
488                         if ((status & TPM_STS_DATA_EXPECT) == 0) {
489                                 rc = -EIO;
490                                 goto out_err;
491                         }
492                 }
493         }
494
495         /* Go and do it */
496         iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
497         debug("done\n");
498
499         return len;
500
501 out_err:
502         debug("%s: out_err\n", __func__);
503         tpm_tis_i2c_ready(chip);
504         /*
505          * The TPM needs some time to clean up here,
506          * so we sleep rather than keeping the bus busy
507          */
508         udelay(2000);
509         release_locality(chip, chip->vendor.locality, 0);
510
511         return rc;
512 }
513
514 static struct tpm_vendor_specific tpm_tis_i2c = {
515         .status = tpm_tis_i2c_status,
516         .recv = tpm_tis_i2c_recv,
517         .send = tpm_tis_i2c_send,
518         .cancel = tpm_tis_i2c_ready,
519         .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
520         .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
521         .req_canceled = TPM_STS_COMMAND_READY,
522 };
523
524
525 static enum i2c_chip_type tpm_vendor_chip_type(void)
526 {
527 #if CONFIG_IS_ENABLED(OF_CONTROL)
528         const void *blob = gd->fdt_blob;
529
530         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
531                 return SLB9645;
532
533         if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
534                 return SLB9635;
535 #endif
536         return UNKNOWN;
537 }
538
539 int tpm_vendor_init(struct udevice *dev)
540 {
541         struct tpm_chip *chip;
542         u32 vendor;
543         u32 expected_did_vid;
544
545         tpm_dev.dev = dev;
546         tpm_dev.chip_type = tpm_vendor_chip_type();
547
548         chip = tpm_register_hardware(&tpm_tis_i2c);
549         if (chip < 0)
550                 return -ENODEV;
551
552         /* Disable interrupts (not supported) */
553         chip->vendor.irq = 0;
554
555         /* Default timeouts */
556         chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
557         chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
558         chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
559         chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
560
561         if (request_locality(chip, 0) < 0)
562                 return  -ENODEV;
563
564         /* Read four bytes from DID_VID register */
565         if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
566                 release_locality(chip, 0, 1);
567                 return -EIO;
568         }
569
570         if (tpm_dev.chip_type == SLB9635) {
571                 vendor = be32_to_cpu(vendor);
572                 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
573         } else {
574                 /* device id and byte order has changed for newer i2c tpms */
575                 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
576         }
577
578         if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
579                 error("Vendor id did not match! ID was %08x\n", vendor);
580                 return -ENODEV;
581         }
582
583         debug("1.2 TPM (chip type %s device-id 0x%X)\n",
584               chip_name[tpm_dev.chip_type], vendor >> 16);
585
586         /*
587          * A timeout query to TPM can be placed here.
588          * Standard timeout values are used so far
589          */
590
591         return 0;
592 }
593
594 void tpm_vendor_cleanup(struct tpm_chip *chip)
595 {
596         release_locality(chip, chip->vendor.locality, 1);
597 }