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 */
56 /* Global structure for tpm chip data */
57 static struct tpm_chip g_chip;
66 /* Extended error numbers from linux (see errno.h) */
67 #define ECANCELED 125 /* Operation Canceled */
69 /* Timer frequency. Corresponds to msec timer resolution*/
72 #define TPM_MAX_ORDINAL 243
73 #define TPM_MAX_PROTECTED_ORDINAL 12
74 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
76 #define TPM_CMD_COUNT_BYTE 2
77 #define TPM_CMD_ORDINAL_BYTE 6
80 * Array with one entry per ordinal defining the maximum amount
81 * of time the chip could take to return the result. The ordinal
82 * designation of short, medium or long is defined in a table in
83 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
84 * values of the SHORT, MEDIUM, and LONG durations are retrieved
85 * from the chip during initialization with a call to tpm_get_timeouts.
87 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
88 TPM_UNDEFINED, /* 0 */
93 TPM_UNDEFINED, /* 5 */
102 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
103 TPM_UNDEFINED, /* 0 */
108 TPM_UNDEFINED, /* 5 */
158 TPM_UNDEFINED, /* 55 */
178 TPM_UNDEFINED, /* 75 */
188 TPM_UNDEFINED, /* 85 */
198 TPM_UNDEFINED, /* 95 */
203 TPM_MEDIUM, /* 100 */
208 TPM_UNDEFINED, /* 105 */
238 TPM_UNDEFINED, /* 135 */
248 TPM_UNDEFINED, /* 145 */
258 TPM_UNDEFINED, /* 155 */
268 TPM_UNDEFINED, /* 165 */
278 TPM_UNDEFINED, /* 175 */
283 TPM_MEDIUM, /* 180 */
288 TPM_MEDIUM, /* 185 */
293 TPM_UNDEFINED, /* 190 */
298 TPM_UNDEFINED, /* 195 */
313 TPM_MEDIUM, /* 210 */
318 TPM_UNDEFINED, /* 215 */
328 TPM_UNDEFINED, /* 225 */
338 TPM_UNDEFINED, /* 235 */
348 /* Returns max number of milliseconds to wait */
349 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
352 int duration_idx = TPM_UNDEFINED;
355 if (ordinal < TPM_MAX_ORDINAL) {
356 duration_idx = tpm_ordinal_duration[ordinal];
357 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
358 TPM_MAX_PROTECTED_ORDINAL) {
359 duration_idx = tpm_protected_ordinal_duration[
360 ordinal & TPM_PROTECTED_ORDINAL_MASK];
363 if (duration_idx != TPM_UNDEFINED)
364 duration = chip->vendor.duration[duration_idx];
367 return 2 * 60 * HZ; /* Two minutes timeout */
372 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
376 unsigned long start, stop;
378 struct tpm_chip *chip = &g_chip;
380 /* switch endianess: big->little */
381 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
382 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
388 if (count > bufsiz) {
389 error("invalid count value %x %zx\n", count, bufsiz);
393 debug("Calling send\n");
394 rc = chip->vendor.send(chip, (u8 *)buf, count);
395 debug(" ... done calling send\n");
397 error("tpm_transmit: tpm_send: error %d\n", rc);
401 if (chip->vendor.irq)
404 start = get_timer(0);
405 stop = tpm_calc_ordinal_duration(chip, ordinal);
407 debug("waiting for status... %ld %ld\n", start, stop);
408 u8 status = chip->vendor.status(chip);
409 if ((status & chip->vendor.req_complete_mask) ==
410 chip->vendor.req_complete_val) {
411 debug("...got it;\n");
415 if (status == chip->vendor.req_canceled) {
416 error("Operation Canceled\n");
420 udelay(TPM_TIMEOUT * 1000);
421 } while (get_timer(start) < stop);
423 chip->vendor.cancel(chip);
424 error("Operation Timed out\n");
429 debug("out_recv: reading response...\n");
430 rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
432 error("tpm_transmit: tpm_recv: error %d\n", rc);
438 static int tpm_open_dev(struct udevice *dev)
442 debug("%s: start\n", __func__);
445 rc = tpm_vendor_init(dev);
451 static void tpm_close(void)
453 if (g_chip.is_open) {
454 tpm_vendor_cleanup(&g_chip);
460 * Decode TPM configuration.
462 * @param dev Returns a configuration of TPM device
463 * @return 0 if ok, -1 on error
465 static int tpm_decode_config(struct tpm *dev)
467 const void *blob = gd->fdt_blob;
474 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
476 node = fdtdec_next_compatible(blob, 0,
477 COMPAT_INFINEON_SLB9645_TPM);
480 debug("%s: Node not found\n", __func__);
483 parent = fdt_parent_offset(blob, node);
485 debug("%s: Cannot find node parent\n", __func__);
490 * TODO(sjg@chromium.org): Remove this when driver model supports
493 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
495 debug("Cannot find bus for node '%s: ret=%d'\n",
496 fdt_get_name(blob, parent, NULL), ret);
500 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
501 if (chip_addr == -1) {
502 debug("Cannot find reg property for node '%s: ret=%d'\n",
503 fdt_get_name(blob, node, NULL), ret);
507 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
508 * in iic_tpm_read() so the offset length needs to be 0 here.
510 ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
512 debug("Cannot find device for node '%s: ret=%d'\n",
513 fdt_get_name(blob, node, NULL), ret);
520 struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
522 struct tpm_chip *chip;
524 /* Driver specific per-device data */
526 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
537 if (tpm_decode_config(&tpm))
540 debug("%s: done\n", __func__);
554 rc = tpm_open_dev(tpm.dev);
569 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
570 uint8_t *recvbuf, size_t *rbuf_len)
578 if (sizeof(buf) < sbuf_size)
581 memcpy(buf, sendbuf, sbuf_size);
583 len = tpm_transmit(buf, sbuf_size);
590 memcpy(recvbuf, buf, len);