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