]> git.sur5r.net Git - u-boot/blob - drivers/tpm/tpm_tis_infineon.c
Merge branch 'master' of git://git.denx.de/u-boot-fsl-qoriq
[u-boot] / drivers / tpm / tpm_tis_infineon.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 <i2c.h>
27 #include <tpm.h>
28 #include <asm-generic/errno.h>
29 #include <linux/compiler.h>
30 #include <linux/types.h>
31 #include <linux/unaligned/be_byteshift.h>
32
33 #include "tpm_tis_infineon.h"
34 #include "tpm_internal.h"
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 static const char * const chip_name[] = {
39         [SLB9635] = "slb9635tt",
40         [SLB9645] = "slb9645tt",
41         [UNKNOWN] = "unknown/fallback to slb9635",
42 };
43
44 /*
45  * tpm_tis_i2c_read() - read from TPM register
46  * @addr: register address to read from
47  * @buffer: provided by caller
48  * @len: number of bytes to read
49  *
50  * Read len bytes from TPM register and put them into
51  * buffer (little-endian format, i.e. first byte is put into buffer[0]).
52  *
53  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
54  * values have to be swapped.
55  *
56  * Return -EIO on error, 0 on success.
57  */
58 static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
59                             size_t len)
60 {
61         struct tpm_chip *chip = dev_get_priv(dev);
62         int rc;
63         int count;
64         uint32_t addrbuf = addr;
65
66         if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
67                 /* slb9635 protocol should work in both cases */
68                 for (count = 0; count < MAX_COUNT; count++) {
69                         rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
70                         if (rc == 0)
71                                 break;  /* Success, break to skip sleep */
72                         udelay(SLEEP_DURATION_US);
73                 }
74                 if (rc)
75                         return rc;
76
77                 /* After the TPM has successfully received the register address
78                  * it needs some time, thus we're sleeping here again, before
79                  * retrieving the data
80                  */
81                 for (count = 0; count < MAX_COUNT; count++) {
82                         udelay(SLEEP_DURATION_US);
83                         rc = dm_i2c_read(dev, 0, buffer, len);
84                         if (rc == 0)
85                                 break;  /* success, break to skip sleep */
86                 }
87         } else {
88                 /*
89                  * Use a combined read for newer chips.
90                  * Unfortunately the smbus functions are not suitable due to
91                  * the 32 byte limit of the smbus.
92                  * Retries should usually not be needed, but are kept just to
93                  * be safe on the safe side.
94                  */
95                 for (count = 0; count < MAX_COUNT; count++) {
96                         rc = dm_i2c_read(dev, addr, buffer, len);
97                         if (rc == 0)
98                                 break;  /* break here to skip sleep */
99                         udelay(SLEEP_DURATION_US);
100                 }
101         }
102
103         /* Take care of 'guard time' */
104         udelay(SLEEP_DURATION_US);
105         if (rc)
106                 return rc;
107
108         return 0;
109 }
110
111 static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
112                                      const u8 *buffer, size_t len,
113                                      unsigned int sleep_time_us, u8 max_count)
114 {
115         struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
116         struct tpm_chip *chip = dev_get_priv(dev);
117         int rc = 0;
118         int count;
119
120         if (chip->chip_type == SLB9635) {
121                 /* Prepare send buffer to include the address */
122                 priv->buf[0] = addr;
123                 memcpy(&(priv->buf[1]), buffer, len);
124                 buffer = priv->buf;
125                 len++;
126                 addr = 0;
127         }
128
129         for (count = 0; count < max_count; count++) {
130                 rc = dm_i2c_write(dev, addr, buffer, len);
131                 if (rc == 0)
132                         break;  /* Success, break to skip sleep */
133                 udelay(sleep_time_us);
134         }
135
136         /* take care of 'guard time' */
137         udelay(sleep_time_us);
138         if (rc)
139                 return rc;
140
141         return 0;
142 }
143
144 /*
145  * tpm_tis_i2c_write() - write to TPM register
146  * @addr: register address to write to
147  * @buffer: containing data to be written
148  * @len: number of bytes to write
149  *
150  * Write len bytes from provided buffer to TPM register (little
151  * endian format, i.e. buffer[0] is written as first byte).
152  *
153  * NOTE: TPM is big-endian for multi-byte values. Multi-byte
154  * values have to be swapped.
155  *
156  * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
157  *
158  * Return -EIO on error, 0 on success
159  */
160 static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
161                              size_t len)
162 {
163         return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
164                                          SLEEP_DURATION_US, MAX_COUNT);
165 }
166
167 /*
168  * This function is needed especially for the cleanup situation after
169  * sending TPM_READY
170  */
171 static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
172                                   size_t len)
173 {
174         return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
175                                          SLEEP_DURATION_LONG_US,
176                                          MAX_COUNT_LONG);
177 }
178
179 static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
180 {
181         const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
182         struct tpm_chip *chip = dev_get_priv(dev);
183         u8 buf;
184         int rc;
185
186         rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
187         if (rc < 0)
188                 return rc;
189
190         if ((buf & mask) == mask) {
191                 chip->locality = loc;
192                 return loc;
193         }
194
195         return -ENOENT;
196 }
197
198 static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
199                                          int force)
200 {
201         const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
202         u8 buf;
203
204         if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
205                 return;
206
207         if (force || (buf & mask) == mask) {
208                 buf = TPM_ACCESS_ACTIVE_LOCALITY;
209                 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
210         }
211 }
212
213 static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
214 {
215         struct tpm_chip *chip = dev_get_priv(dev);
216         unsigned long start, stop;
217         u8 buf = TPM_ACCESS_REQUEST_USE;
218         int rc;
219
220         rc = tpm_tis_i2c_check_locality(dev, loc);
221         if (rc >= 0) {
222                 debug("%s: Already have locality\n", __func__);
223                 return loc;  /* We already have the locality */
224         } else if (rc != -ENOENT) {
225                 debug("%s: Failed to get locality: %d\n", __func__, rc);
226                 return rc;
227         }
228
229         rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
230         if (rc) {
231                 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
232                 return rc;
233         }
234
235         /* Wait for burstcount */
236         start = get_timer(0);
237         stop = chip->timeout_a;
238         do {
239                 rc = tpm_tis_i2c_check_locality(dev, loc);
240                 if (rc >= 0) {
241                         debug("%s: Have locality\n", __func__);
242                         return loc;
243                 } else if (rc != -ENOENT) {
244                         debug("%s: Failed to get locality: %d\n", __func__, rc);
245                         return rc;
246                 }
247                 mdelay(TPM_TIMEOUT_MS);
248         } while (get_timer(start) < stop);
249         debug("%s: Timeout getting locality: %d\n", __func__, rc);
250
251         return rc;
252 }
253
254 static u8 tpm_tis_i2c_status(struct udevice *dev)
255 {
256         struct tpm_chip *chip = dev_get_priv(dev);
257         /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
258         u8 buf;
259
260         if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
261                 return 0;
262         else
263                 return buf;
264 }
265
266 static int tpm_tis_i2c_ready(struct udevice *dev)
267 {
268         struct tpm_chip *chip = dev_get_priv(dev);
269         int rc;
270
271         /* This causes the current command to be aborted */
272         u8 buf = TPM_STS_COMMAND_READY;
273
274         debug("%s\n", __func__);
275         rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
276         if (rc)
277                 debug("%s: rc=%d\n", __func__, rc);
278
279         return rc;
280 }
281
282 static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
283 {
284         struct tpm_chip *chip = dev_get_priv(dev);
285         unsigned long start, stop;
286         ssize_t burstcnt;
287         u8 addr, buf[3];
288
289         /* Wait for burstcount */
290         /* XXX: Which timeout value? Spec has 2 answers (c & d) */
291         start = get_timer(0);
292         stop = chip->timeout_d;
293         do {
294                 /* Note: STS is little endian */
295                 addr = TPM_STS(chip->locality) + 1;
296                 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
297                         burstcnt = 0;
298                 else
299                         burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
300
301                 if (burstcnt)
302                         return burstcnt;
303                 mdelay(TPM_TIMEOUT_MS);
304         } while (get_timer(start) < stop);
305
306         return -EBUSY;
307 }
308
309 static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
310                                      unsigned long timeout, int *status)
311 {
312         unsigned long start, stop;
313
314         /* Check current status */
315         *status = tpm_tis_i2c_status(dev);
316         if ((*status & mask) == mask)
317                 return 0;
318
319         start = get_timer(0);
320         stop = timeout;
321         do {
322                 mdelay(TPM_TIMEOUT_MS);
323                 *status = tpm_tis_i2c_status(dev);
324                 if ((*status & mask) == mask)
325                         return 0;
326         } while (get_timer(start) < stop);
327
328         return -ETIMEDOUT;
329 }
330
331 static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
332 {
333         struct tpm_chip *chip = dev_get_priv(dev);
334         size_t size = 0;
335         ssize_t burstcnt;
336         int rc;
337
338         while (size < count) {
339                 burstcnt = tpm_tis_i2c_get_burstcount(dev);
340
341                 /* burstcount < 0 -> tpm is busy */
342                 if (burstcnt < 0)
343                         return burstcnt;
344
345                 /* Limit received data to max left */
346                 if (burstcnt > (count - size))
347                         burstcnt = count - size;
348
349                 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
350                                       &(buf[size]), burstcnt);
351                 if (rc == 0)
352                         size += burstcnt;
353         }
354
355         return size;
356 }
357
358 static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
359 {
360         struct tpm_chip *chip = dev_get_priv(dev);
361         int size = 0;
362         int expected, status;
363         int rc;
364
365         status = tpm_tis_i2c_status(dev);
366         if (status == TPM_STS_COMMAND_READY)
367                 return -EINTR;
368         if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
369             (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
370                 return -EAGAIN;
371
372         debug("...got it;\n");
373
374         /* Read first 10 bytes, including tag, paramsize, and result */
375         size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
376         if (size < TPM_HEADER_SIZE) {
377                 debug("Unable to read header\n");
378                 return size < 0 ? size : -EIO;
379         }
380
381         expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
382         if ((size_t)expected > count) {
383                 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
384                       count);
385                 return -ENOSPC;
386         }
387
388         size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
389                                       expected - TPM_HEADER_SIZE);
390         if (size < expected) {
391                 debug("Unable to read remainder of result\n");
392                 return -ETIMEDOUT;
393         }
394
395         rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
396                                        &status);
397         if (rc)
398                 return rc;
399         if (status & TPM_STS_DATA_AVAIL) {  /* Retry? */
400                 debug("Error left over data\n");
401                 return -EIO;
402         }
403
404         return size;
405 }
406
407 static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
408 {
409         struct tpm_chip *chip = dev_get_priv(dev);
410         int rc, status;
411         size_t burstcnt;
412         size_t count = 0;
413         int retry = 0;
414         u8 sts = TPM_STS_GO;
415
416         debug("%s: len=%d\n", __func__, len);
417         if (len > TPM_DEV_BUFSIZE)
418                 return -E2BIG;  /* Command is too long for our tpm, sorry */
419
420         if (tpm_tis_i2c_request_locality(dev, 0) < 0)
421                 return -EBUSY;
422
423         status = tpm_tis_i2c_status(dev);
424         if ((status & TPM_STS_COMMAND_READY) == 0) {
425                 rc = tpm_tis_i2c_ready(dev);
426                 if (rc)
427                         return rc;
428                 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
429                                                chip->timeout_b, &status);
430                 if (rc)
431                         return rc;
432         }
433
434         burstcnt = tpm_tis_i2c_get_burstcount(dev);
435
436         /* burstcount < 0 -> tpm is busy */
437         if (burstcnt < 0)
438                 return burstcnt;
439
440         while (count < len) {
441                 udelay(300);
442                 if (burstcnt > len - count)
443                         burstcnt = len - count;
444
445 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
446                 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
447                         burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
448 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
449
450                 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
451                                        &(buf[count]), burstcnt);
452                 if (rc == 0)
453                         count += burstcnt;
454                 else {
455                         debug("%s: error\n", __func__);
456                         if (retry++ > 10)
457                                 return -EIO;
458                         rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
459                                                        chip->timeout_c,
460                                                        &status);
461                         if (rc)
462                                 return rc;
463
464                         if ((status & TPM_STS_DATA_EXPECT) == 0)
465                                 return -EIO;
466                 }
467         }
468
469         /* Go and do it */
470         rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
471         if (rc < 0)
472                 return rc;
473         debug("%s: done, rc=%d\n", __func__, rc);
474
475         return len;
476 }
477
478 static int tpm_tis_i2c_cleanup(struct udevice *dev)
479 {
480         struct tpm_chip *chip = dev_get_priv(dev);
481
482         tpm_tis_i2c_ready(dev);
483         /*
484          * The TPM needs some time to clean up here,
485          * so we sleep rather than keeping the bus busy
486          */
487         mdelay(2);
488         tpm_tis_i2c_release_locality(dev, chip->locality, 0);
489
490         return 0;
491 }
492
493 static int tpm_tis_i2c_init(struct udevice *dev)
494 {
495         struct tpm_chip *chip = dev_get_priv(dev);
496         u32 vendor;
497         u32 expected_did_vid;
498         int rc;
499
500         chip->is_open = 1;
501
502         /* Default timeouts - these could move to the device tree */
503         chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
504         chip->timeout_b = TIS_LONG_TIMEOUT_MS;
505         chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
506         chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
507
508         rc = tpm_tis_i2c_request_locality(dev, 0);
509         if (rc < 0)
510                 return rc;
511
512         /* Read four bytes from DID_VID register */
513         if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
514                 tpm_tis_i2c_release_locality(dev, 0, 1);
515                 return -EIO;
516         }
517
518         if (chip->chip_type == SLB9635) {
519                 vendor = be32_to_cpu(vendor);
520                 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
521         } else {
522                 /* device id and byte order has changed for newer i2c tpms */
523                 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
524         }
525
526         if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
527                 error("Vendor id did not match! ID was %08x\n", vendor);
528                 return -ENODEV;
529         }
530
531         chip->vend_dev = vendor;
532         debug("1.2 TPM (chip type %s device-id 0x%X)\n",
533               chip_name[chip->chip_type], vendor >> 16);
534
535         /*
536          * A timeout query to TPM can be placed here.
537          * Standard timeout values are used so far
538          */
539
540         return 0;
541 }
542
543 static int tpm_tis_i2c_open(struct udevice *dev)
544 {
545         struct tpm_chip *chip = dev_get_priv(dev);
546         int rc;
547
548         debug("%s: start\n", __func__);
549         if (chip->is_open)
550                 return -EBUSY;
551         rc = tpm_tis_i2c_init(dev);
552         if (rc < 0)
553                 chip->is_open = 0;
554
555         return rc;
556 }
557
558 static int tpm_tis_i2c_close(struct udevice *dev)
559 {
560         struct tpm_chip *chip = dev_get_priv(dev);
561
562         if (chip->is_open) {
563                 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
564                 chip->is_open = 0;
565                 chip->vend_dev = 0;
566         }
567
568         return 0;
569 }
570
571 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
572 {
573         struct tpm_chip *chip = dev_get_priv(dev);
574
575         if (size < 50)
576                 return -ENOSPC;
577
578         return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
579                         chip->is_open ? "open" : "closed",
580                         chip_name[chip->chip_type],
581                         chip->vend_dev >> 16);
582 }
583
584 static int tpm_tis_i2c_probe(struct udevice *dev)
585 {
586         struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
587         struct tpm_chip *chip = dev_get_priv(dev);
588
589         chip->chip_type = dev_get_driver_data(dev);
590
591         /* TODO: These need to be checked and tuned */
592         uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
593         uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
594         uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
595         uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
596
597         return 0;
598 }
599
600 static const struct tpm_ops tpm_tis_i2c_ops = {
601         .open           = tpm_tis_i2c_open,
602         .close          = tpm_tis_i2c_close,
603         .get_desc       = tpm_tis_get_desc,
604         .send           = tpm_tis_i2c_send,
605         .recv           = tpm_tis_i2c_recv,
606         .cleanup        = tpm_tis_i2c_cleanup,
607 };
608
609 static const struct udevice_id tpm_tis_i2c_ids[] = {
610         { .compatible = "infineon,slb9635tt", .data = SLB9635 },
611         { .compatible = "infineon,slb9645tt", .data = SLB9645 },
612         { }
613 };
614
615 U_BOOT_DRIVER(tpm_tis_i2c) = {
616         .name   = "tpm_tis_infineon",
617         .id     = UCLASS_TPM,
618         .of_match = tpm_tis_i2c_ids,
619         .ops    = &tpm_tis_i2c_ops,
620         .probe  = tpm_tis_i2c_probe,
621         .priv_auto_alloc_size = sizeof(struct tpm_chip),
622 };