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