2 * Copyright (C) 2011 Infineon Technologies
5 * Peter Huewe <huewe.external@infineon.com>
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
11 * It is based on the Linux kernel driver tpm.c from Leendert van
12 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
16 * See file CREDITS for list of people who contributed to this
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation, version 2 of the
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
38 #include <linux/compiler.h>
42 #include <asm-generic/errno.h>
43 #include <linux/types.h>
44 #include <linux/unaligned/be_byteshift.h>
46 #include "tpm_private.h"
48 DECLARE_GLOBAL_DATA_PTR;
50 /* TPM configuration */
62 /* Global structure for tpm chip data */
63 static struct tpm_chip g_chip;
72 /* Extended error numbers from linux (see errno.h) */
73 #define ECANCELED 125 /* Operation Canceled */
75 /* Timer frequency. Corresponds to msec timer resolution*/
78 #define TPM_MAX_ORDINAL 243
79 #define TPM_MAX_PROTECTED_ORDINAL 12
80 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
82 #define TPM_CMD_COUNT_BYTE 2
83 #define TPM_CMD_ORDINAL_BYTE 6
86 * Array with one entry per ordinal defining the maximum amount
87 * of time the chip could take to return the result. The ordinal
88 * designation of short, medium or long is defined in a table in
89 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
90 * values of the SHORT, MEDIUM, and LONG durations are retrieved
91 * from the chip during initialization with a call to tpm_get_timeouts.
93 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
94 TPM_UNDEFINED, /* 0 */
99 TPM_UNDEFINED, /* 5 */
108 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
109 TPM_UNDEFINED, /* 0 */
114 TPM_UNDEFINED, /* 5 */
164 TPM_UNDEFINED, /* 55 */
184 TPM_UNDEFINED, /* 75 */
194 TPM_UNDEFINED, /* 85 */
204 TPM_UNDEFINED, /* 95 */
209 TPM_MEDIUM, /* 100 */
214 TPM_UNDEFINED, /* 105 */
244 TPM_UNDEFINED, /* 135 */
254 TPM_UNDEFINED, /* 145 */
264 TPM_UNDEFINED, /* 155 */
274 TPM_UNDEFINED, /* 165 */
284 TPM_UNDEFINED, /* 175 */
289 TPM_MEDIUM, /* 180 */
294 TPM_MEDIUM, /* 185 */
299 TPM_UNDEFINED, /* 190 */
304 TPM_UNDEFINED, /* 195 */
319 TPM_MEDIUM, /* 210 */
324 TPM_UNDEFINED, /* 215 */
334 TPM_UNDEFINED, /* 225 */
344 TPM_UNDEFINED, /* 235 */
354 /* Returns max number of milliseconds to wait */
355 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
358 int duration_idx = TPM_UNDEFINED;
361 if (ordinal < TPM_MAX_ORDINAL) {
362 duration_idx = tpm_ordinal_duration[ordinal];
363 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
364 TPM_MAX_PROTECTED_ORDINAL) {
365 duration_idx = tpm_protected_ordinal_duration[
366 ordinal & TPM_PROTECTED_ORDINAL_MASK];
369 if (duration_idx != TPM_UNDEFINED)
370 duration = chip->vendor.duration[duration_idx];
373 return 2 * 60 * HZ; /* Two minutes timeout */
378 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
382 unsigned long start, stop;
384 struct tpm_chip *chip = &g_chip;
386 /* switch endianess: big->little */
387 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
388 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
394 if (count > bufsiz) {
395 error("invalid count value %x %zx\n", count, bufsiz);
399 debug("Calling send\n");
400 rc = chip->vendor.send(chip, (u8 *)buf, count);
401 debug(" ... done calling send\n");
403 error("tpm_transmit: tpm_send: error %d\n", rc);
407 if (chip->vendor.irq)
410 start = get_timer(0);
411 stop = tpm_calc_ordinal_duration(chip, ordinal);
413 debug("waiting for status... %ld %ld\n", start, stop);
414 u8 status = chip->vendor.status(chip);
415 if ((status & chip->vendor.req_complete_mask) ==
416 chip->vendor.req_complete_val) {
417 debug("...got it;\n");
421 if (status == chip->vendor.req_canceled) {
422 error("Operation Canceled\n");
426 udelay(TPM_TIMEOUT * 1000);
427 } while (get_timer(start) < stop);
429 chip->vendor.cancel(chip);
430 error("Operation Timed out\n");
435 debug("out_recv: reading response...\n");
436 rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
438 error("tpm_transmit: tpm_recv: error %d\n", rc);
445 static int tpm_open_dev(struct udevice *dev)
449 debug("%s: start\n", __func__);
452 rc = tpm_vendor_init_dev(dev);
458 static int tpm_open(uint32_t dev_addr)
464 rc = tpm_vendor_init(dev_addr);
470 static void tpm_close(void)
472 if (g_chip.is_open) {
473 tpm_vendor_cleanup(&g_chip);
478 static int tpm_select(void)
480 #ifndef CONFIG_DM_I2C
483 tpm.old_bus = i2c_get_bus_num();
484 if (tpm.old_bus != tpm.i2c_bus) {
485 ret = i2c_set_bus_num(tpm.i2c_bus);
487 debug("%s: Fail to set i2c bus %d\n", __func__,
496 static int tpm_deselect(void)
498 #ifndef CONFIG_DM_I2C
501 if (tpm.old_bus != i2c_get_bus_num()) {
502 ret = i2c_set_bus_num(tpm.old_bus);
504 debug("%s: Fail to restore i2c bus %d\n",
505 __func__, tpm.old_bus);
515 * Decode TPM configuration.
517 * @param dev Returns a configuration of TPM device
518 * @return 0 if ok, -1 on error
520 static int tpm_decode_config(struct tpm *dev)
522 const void *blob = gd->fdt_blob;
526 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
528 node = fdtdec_next_compatible(blob, 0,
529 COMPAT_INFINEON_SLB9645_TPM);
532 debug("%s: Node not found\n", __func__);
535 parent = fdt_parent_offset(blob, node);
537 debug("%s: Cannot find node parent\n", __func__);
546 * TODO(sjg@chromium.org): Remove this when driver model supports
549 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
551 debug("Cannot find bus for node '%s: ret=%d'\n",
552 fdt_get_name(blob, parent, NULL), ret);
556 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
557 if (chip_addr == -1) {
558 debug("Cannot find reg property for node '%s: ret=%d'\n",
559 fdt_get_name(blob, node, NULL), ret);
563 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
564 * in iic_tpm_read() so the offset length needs to be 0 here.
566 ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
568 debug("Cannot find device for node '%s: ret=%d'\n",
569 fdt_get_name(blob, node, NULL), ret);
575 i2c_bus = i2c_get_bus_num_fdt(parent);
578 dev->i2c_bus = i2c_bus;
579 dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
585 struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
587 struct tpm_chip *chip;
589 /* Driver specific per-device data */
591 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
602 if (tpm_decode_config(&tpm))
608 #ifndef CONFIG_DM_I2C
610 * Probe TPM twice; the first probing might fail because TPM is asleep,
611 * and the probing can wake up TPM.
613 if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
614 debug("%s: fail to probe i2c addr 0x%x\n", __func__,
621 debug("%s: done\n", __func__);
639 rc = tpm_open_dev(tpm.dev);
641 rc = tpm_open(tpm.slave_addr);
664 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
665 uint8_t *recvbuf, size_t *rbuf_len)
673 if (sizeof(buf) < sbuf_size)
676 memcpy(buf, sendbuf, sbuf_size);
681 len = tpm_transmit(buf, sbuf_size);
690 memcpy(recvbuf, buf, len);