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,
41 #include <asm-generic/errno.h>
42 #include <linux/types.h>
43 #include <linux/unaligned/be_byteshift.h>
45 #include "tpm_private.h"
47 DECLARE_GLOBAL_DATA_PTR
;
49 /* TPM configuration */
57 /* Global structure for tpm chip data */
58 static struct tpm_chip g_chip
;
67 /* Extended error numbers from linux (see errno.h) */
68 #define ECANCELED 125 /* Operation Canceled */
70 /* Timer frequency. Corresponds to msec timer resolution*/
73 #define TPM_MAX_ORDINAL 243
74 #define TPM_MAX_PROTECTED_ORDINAL 12
75 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
77 #define TPM_CMD_COUNT_BYTE 2
78 #define TPM_CMD_ORDINAL_BYTE 6
81 * Array with one entry per ordinal defining the maximum amount
82 * of time the chip could take to return the result. The ordinal
83 * designation of short, medium or long is defined in a table in
84 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
85 * values of the SHORT, MEDIUM, and LONG durations are retrieved
86 * from the chip during initialization with a call to tpm_get_timeouts.
88 static const u8 tpm_protected_ordinal_duration
[TPM_MAX_PROTECTED_ORDINAL
] = {
89 TPM_UNDEFINED
, /* 0 */
94 TPM_UNDEFINED
, /* 5 */
103 static const u8 tpm_ordinal_duration
[TPM_MAX_ORDINAL
] = {
104 TPM_UNDEFINED
, /* 0 */
109 TPM_UNDEFINED
, /* 5 */
159 TPM_UNDEFINED
, /* 55 */
179 TPM_UNDEFINED
, /* 75 */
189 TPM_UNDEFINED
, /* 85 */
199 TPM_UNDEFINED
, /* 95 */
204 TPM_MEDIUM
, /* 100 */
209 TPM_UNDEFINED
, /* 105 */
239 TPM_UNDEFINED
, /* 135 */
249 TPM_UNDEFINED
, /* 145 */
259 TPM_UNDEFINED
, /* 155 */
269 TPM_UNDEFINED
, /* 165 */
279 TPM_UNDEFINED
, /* 175 */
284 TPM_MEDIUM
, /* 180 */
289 TPM_MEDIUM
, /* 185 */
294 TPM_UNDEFINED
, /* 190 */
299 TPM_UNDEFINED
, /* 195 */
314 TPM_MEDIUM
, /* 210 */
319 TPM_UNDEFINED
, /* 215 */
329 TPM_UNDEFINED
, /* 225 */
339 TPM_UNDEFINED
, /* 235 */
349 /* Returns max number of milliseconds to wait */
350 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip
*chip
,
353 int duration_idx
= TPM_UNDEFINED
;
356 if (ordinal
< TPM_MAX_ORDINAL
) {
357 duration_idx
= tpm_ordinal_duration
[ordinal
];
358 } else if ((ordinal
& TPM_PROTECTED_ORDINAL_MASK
) <
359 TPM_MAX_PROTECTED_ORDINAL
) {
360 duration_idx
= tpm_protected_ordinal_duration
[
361 ordinal
& TPM_PROTECTED_ORDINAL_MASK
];
364 if (duration_idx
!= TPM_UNDEFINED
)
365 duration
= chip
->vendor
.duration
[duration_idx
];
368 return 2 * 60 * HZ
; /* Two minutes timeout */
373 static ssize_t
tpm_transmit(const unsigned char *buf
, size_t bufsiz
)
377 unsigned long start
, stop
;
379 struct tpm_chip
*chip
= &g_chip
;
381 /* switch endianess: big->little */
382 count
= get_unaligned_be32(buf
+ TPM_CMD_COUNT_BYTE
);
383 ordinal
= get_unaligned_be32(buf
+ TPM_CMD_ORDINAL_BYTE
);
389 if (count
> bufsiz
) {
390 error("invalid count value %x %zx\n", count
, bufsiz
);
394 rc
= chip
->vendor
.send(chip
, (u8
*)buf
, count
);
396 error("tpm_transmit: tpm_send: error %zd\n", rc
);
400 if (chip
->vendor
.irq
)
403 start
= get_timer(0);
404 stop
= tpm_calc_ordinal_duration(chip
, ordinal
);
406 debug("waiting for status...\n");
407 u8 status
= chip
->vendor
.status(chip
);
408 if ((status
& chip
->vendor
.req_complete_mask
) ==
409 chip
->vendor
.req_complete_val
) {
410 debug("...got it;\n");
414 if ((status
== chip
->vendor
.req_canceled
)) {
415 error("Operation Canceled\n");
419 udelay(TPM_TIMEOUT
* 1000);
420 } while (get_timer(start
) < stop
);
422 chip
->vendor
.cancel(chip
);
423 error("Operation Timed out\n");
428 debug("out_recv: reading response...\n");
429 rc
= chip
->vendor
.recv(chip
, (u8
*)buf
, TPM_BUFSIZE
);
431 error("tpm_transmit: tpm_recv: error %zd\n", rc
);
437 static int tpm_open(uint32_t dev_addr
)
442 rc
= tpm_vendor_init(dev_addr
);
448 static void tpm_close(void)
450 if (g_chip
.is_open
) {
451 tpm_vendor_cleanup(&g_chip
);
456 static int tpm_select(void)
460 tpm
.old_bus
= i2c_get_bus_num();
461 if (tpm
.old_bus
!= tpm
.i2c_bus
) {
462 ret
= i2c_set_bus_num(tpm
.i2c_bus
);
464 debug("%s: Fail to set i2c bus %d\n", __func__
,
472 static int tpm_deselect(void)
476 if (tpm
.old_bus
!= i2c_get_bus_num()) {
477 ret
= i2c_set_bus_num(tpm
.old_bus
);
479 debug("%s: Fail to restore i2c bus %d\n",
480 __func__
, tpm
.old_bus
);
489 * Decode TPM configuration.
491 * @param dev Returns a configuration of TPM device
492 * @return 0 if ok, -1 on error
494 static int tpm_decode_config(struct tpm
*dev
)
496 #ifdef CONFIG_OF_CONTROL
497 const void *blob
= gd
->fdt_blob
;
501 node
= fdtdec_next_compatible(blob
, 0, COMPAT_INFINEON_SLB9635_TPM
);
503 node
= fdtdec_next_compatible(blob
, 0,
504 COMPAT_INFINEON_SLB9645_TPM
);
507 debug("%s: Node not found\n", __func__
);
510 parent
= fdt_parent_offset(blob
, node
);
512 debug("%s: Cannot find node parent\n", __func__
);
515 i2c_bus
= i2c_get_bus_num_fdt(parent
);
518 dev
->i2c_bus
= i2c_bus
;
519 dev
->slave_addr
= fdtdec_get_addr(blob
, node
, "reg");
521 dev
->i2c_bus
= CONFIG_TPM_TIS_I2C_BUS_NUMBER
;
522 dev
->slave_addr
= CONFIG_TPM_TIS_I2C_SLAVE_ADDRESS
;
527 struct tpm_chip
*tpm_register_hardware(const struct tpm_vendor_specific
*entry
)
529 struct tpm_chip
*chip
;
531 /* Driver specific per-device data */
533 memcpy(&chip
->vendor
, entry
, sizeof(struct tpm_vendor_specific
));
544 if (tpm_decode_config(&tpm
))
551 * Probe TPM twice; the first probing might fail because TPM is asleep,
552 * and the probing can wake up TPM.
554 if (i2c_probe(tpm
.slave_addr
) && i2c_probe(tpm
.slave_addr
)) {
555 debug("%s: fail to probe i2c addr 0x%x\n", __func__
,
577 rc
= tpm_open(tpm
.slave_addr
);
599 int tis_sendrecv(const uint8_t *sendbuf
, size_t sbuf_size
,
600 uint8_t *recvbuf
, size_t *rbuf_len
)
608 if (sizeof(buf
) < sbuf_size
)
611 memcpy(buf
, sendbuf
, sbuf_size
);
616 len
= tpm_transmit(buf
, sbuf_size
);
625 memcpy(recvbuf
, buf
, len
);