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 /* global structure for tpm chip data */
39 struct tpm_chip g_chip;
48 #define TPM_MAX_ORDINAL 243
49 #define TPM_MAX_PROTECTED_ORDINAL 12
50 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
53 * Array with one entry per ordinal defining the maximum amount
54 * of time the chip could take to return the result. The ordinal
55 * designation of short, medium or long is defined in a table in
56 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
57 * values of the SHORT, MEDIUM, and LONG durations are retrieved
58 * from the chip during initialization with a call to tpm_get_timeouts.
60 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
61 TPM_UNDEFINED, /* 0 */
66 TPM_UNDEFINED, /* 5 */
75 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
76 TPM_UNDEFINED, /* 0 */
81 TPM_UNDEFINED, /* 5 */
131 TPM_UNDEFINED, /* 55 */
151 TPM_UNDEFINED, /* 75 */
161 TPM_UNDEFINED, /* 85 */
171 TPM_UNDEFINED, /* 95 */
176 TPM_MEDIUM, /* 100 */
181 TPM_UNDEFINED, /* 105 */
211 TPM_UNDEFINED, /* 135 */
221 TPM_UNDEFINED, /* 145 */
231 TPM_UNDEFINED, /* 155 */
241 TPM_UNDEFINED, /* 165 */
251 TPM_UNDEFINED, /* 175 */
256 TPM_MEDIUM, /* 180 */
261 TPM_MEDIUM, /* 185 */
266 TPM_UNDEFINED, /* 190 */
271 TPM_UNDEFINED, /* 195 */
286 TPM_MEDIUM, /* 210 */
291 TPM_UNDEFINED, /* 215 */
301 TPM_UNDEFINED, /* 225 */
311 TPM_UNDEFINED, /* 235 */
322 * Returns max number of milliseconds to wait
324 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
326 int duration_idx = TPM_UNDEFINED;
329 if (ordinal < TPM_MAX_ORDINAL)
330 duration_idx = tpm_ordinal_duration[ordinal];
331 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
332 TPM_MAX_PROTECTED_ORDINAL)
334 tpm_protected_ordinal_duration[ordinal &
335 TPM_PROTECTED_ORDINAL_MASK];
337 if (duration_idx != TPM_UNDEFINED)
338 duration = chip->vendor.duration[duration_idx];
340 return 2 * 60 * HZ; /*two minutes timeout*/
345 #define TPM_CMD_COUNT_BYTE 2
346 #define TPM_CMD_ORDINAL_BYTE 6
348 ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
352 unsigned long start, stop;
354 struct tpm_chip *chip = &g_chip;
356 /* switch endianess: big->little */
357 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
358 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
361 dev_err(chip->dev, "no data\n");
364 if (count > bufsiz) {
366 "invalid count value %x %zx\n", count, bufsiz);
370 rc = chip->vendor.send(chip, (u8 *)buf, count);
372 dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
376 if (chip->vendor.irq)
379 start = get_timer(0);
380 stop = tpm_calc_ordinal_duration(chip, ordinal);
382 dbg_printf("waiting for status...\n");
383 u8 status = chip->vendor.status(chip);
384 if ((status & chip->vendor.req_complete_mask) ==
385 chip->vendor.req_complete_val) {
386 dbg_printf("...got it;\n");
390 if ((status == chip->vendor.req_canceled)) {
391 dev_err(chip->dev, "Operation Canceled\n");
396 } while (get_timer(start) < stop);
398 chip->vendor.cancel(chip);
399 dev_err(chip->dev, "Operation Timed out\n");
405 dbg_printf("out_recv: reading response...\n");
406 rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
408 dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
413 #define TPM_ERROR_SIZE 10
415 enum tpm_capabilities {
416 TPM_CAP_PROP = cpu_to_be32(5),
419 enum tpm_sub_capabilities {
420 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
421 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
424 struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
426 struct tpm_chip *chip;
428 /* Driver specific per-device data */
430 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
436 int tpm_open(uint32_t dev_addr)
441 rc = tpm_vendor_init(dev_addr);
449 if (g_chip.is_open) {
450 tpm_vendor_cleanup(&g_chip);