2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * The code in this file has been heavily based on the article "Writing a TPM
22 * Device Driver" published on http://ptgmedia.pearsoncmg.com and the
23 * submission by Stefan Berger on Qemu-devel mailing list.
25 * One principal difference is that in the simplest config the other than 0
26 * TPM localities do not get mapped by some devices (for instance, by
27 * Infineon slb9635), so this driver provides access to locality 0 only.
34 #include <arch/byteorder.h>
35 #include <console/console.h>
37 #include <cpu/x86/car.h>
39 #define PREFIX "lpc_tpm: "
41 /* coreboot wrapper for TPM driver (start) */
42 #define TPM_DEBUG(fmt, args...) \
43 if (CONFIG_DEBUG_TPM) { \
44 printk(BIOS_DEBUG, PREFIX); \
45 printk(BIOS_DEBUG, fmt , ##args); \
47 #define printf(x...) printk(BIOS_ERR, x)
49 #define min(a,b) MIN(a,b)
50 #define max(a,b) MAX(a,b)
51 #define readb(_a) (*(volatile unsigned char *) (_a))
52 #define writeb(_v, _a) (*(volatile unsigned char *) (_a) = (_v))
53 #define readl(_a) (*(volatile unsigned long *) (_a))
54 #define writel(_v, _a) (*(volatile unsigned long *) (_a) = (_v))
55 /* coreboot wrapper for TPM driver (end) */
57 #ifndef CONFIG_TPM_TIS_BASE_ADDRESS
58 /* Base TPM address standard for x86 systems */
59 #define CONFIG_TPM_TIS_BASE_ADDRESS 0xfed40000
62 /* the macro accepts the locality value, but only locality 0 is operational */
63 #define TIS_REG(LOCALITY, REG) \
64 (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
66 /* hardware registers' offsets */
67 #define TIS_REG_ACCESS 0x0
68 #define TIS_REG_INT_ENABLE 0x8
69 #define TIS_REG_INT_VECTOR 0xc
70 #define TIS_REG_INT_STATUS 0x10
71 #define TIS_REG_INTF_CAPABILITY 0x14
72 #define TIS_REG_STS 0x18
73 #define TIS_REG_DATA_FIFO 0x24
74 #define TIS_REG_DID_VID 0xf00
75 #define TIS_REG_RID 0xf04
77 /* Some registers' bit field definitions */
78 #define TIS_STS_VALID (1 << 7) /* 0x80 */
79 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
80 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
81 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
82 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
83 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
85 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
86 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
87 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
88 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
89 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
90 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
91 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
93 #define TIS_STS_BURST_COUNT_MASK (0xffff)
94 #define TIS_STS_BURST_COUNT_SHIFT (8)
97 * Error value returned if a tpm register does not enter the expected state
98 * after continuous polling. No actual TPM register reading ever returns ~0,
99 * so this value is a safe error indication to be mixed with possible status
102 #define TPM_TIMEOUT_ERR (~0)
104 /* Error value returned on various TPM driver errors */
105 #define TPM_DRIVER_ERR (~0)
107 /* 1 second is plenty for anything TPM does.*/
108 #define MAX_DELAY_US (1000 * 1000)
110 /* Retrieve burst count value out of the status register contents. */
111 #define BURST_COUNT(status) ((u16)(((status) >> TIS_STS_BURST_COUNT_SHIFT) & \
112 TIS_STS_BURST_COUNT_MASK))
115 * Structures defined below allow creating descriptions of TPM vendor/device
116 * ID information for run time discovery. The only device the system knows
117 * about at this time is Infineon slb9635
121 const char * const dev_name;
126 const char * vendor_name;
127 const struct device_name* dev_names;
130 static const struct device_name infineon_devices[] = {
131 {0xb, "SLB9635 TT 1.2"},
135 static const struct vendor_name vendor_names[] = {
136 {0x15d1, "Infineon", infineon_devices},
140 * Cached vendor/device ID pair to indicate that the device has been already
143 static u32 vendor_dev_id CAR_GLOBAL;
145 static int is_byte_reg(u32 reg)
148 * These TPM registers are 8 bits wide and as such require byte access
149 * on writes and truncated value on reads.
151 return ((reg == TIS_REG_ACCESS) ||
152 (reg == TIS_REG_INT_VECTOR) ||
153 (reg == TIS_REG_DATA_FIFO));
156 /* TPM access functions are carved out to make tracing easier. */
157 static u32 tpm_read(int locality, u32 reg)
161 * Data FIFO register must be read and written in byte access mode,
162 * otherwise the FIFO values are returned 4 bytes at a time.
164 if (is_byte_reg(reg))
165 value = readb(TIS_REG(locality, reg));
167 value = readl(TIS_REG(locality, reg));
169 TPM_DEBUG("Read reg 0x%x returns 0x%x\n", reg, value);
173 static void tpm_write(u32 value, int locality, u32 reg)
175 TPM_DEBUG("Write reg 0x%x with 0x%x\n", reg, value);
177 if (is_byte_reg(reg))
178 writeb(value & 0xff, TIS_REG(locality, reg));
180 writel(value, TIS_REG(locality, reg));
186 * Wait for at least a second for a register to change its state to match the
187 * expected state. Normally the transition happens within microseconds.
189 * @reg - the TPM register offset
190 * @locality - locality
191 * @mask - bitmask for the bitfield(s) to watch
192 * @expected - value the field(s) are supposed to be set to
194 * Returns the register contents in case the expected value was found in the
195 * appropriate register bits, or TPM_TIMEOUT_ERR on timeout.
197 static u32 tis_wait_reg(u8 reg, u8 locality, u8 mask, u8 expected)
199 u32 time_us = MAX_DELAY_US;
200 while (time_us > 0) {
201 u32 value = tpm_read(locality, reg);
202 if ((value & mask) == expected)
204 udelay(1); /* 1 us */
207 return TPM_TIMEOUT_ERR;
211 * Probe the TPM device and try determining its manufacturer/device name.
213 * Returns 0 on success (the device is found or was found during an earlier
214 * invocation) or TPM_DRIVER_ERR if the device is not found.
216 static u32 tis_probe(void)
218 u32 didvid = tpm_read(0, TIS_REG_DID_VID);
220 const char *device_name = "unknown";
221 const char *vendor_name = device_name;
225 return 0; /* Already probed. */
227 if (!didvid || (didvid == 0xffffffff)) {
228 printf("%s: No TPM device found\n", __FUNCTION__);
229 return TPM_DRIVER_ERR;
232 vendor_dev_id = didvid;
234 vid = didvid & 0xffff;
235 did = (didvid >> 16) & 0xffff;
236 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
239 if (vid == vendor_names[i].vendor_id) {
240 vendor_name = vendor_names[i].vendor_name;
242 while ((known_did = vendor_names[i].dev_names[j].dev_id) != 0) {
243 if (known_did == did) {
245 vendor_names[i].dev_names[j].dev_name;
252 /* this will have to be converted into debug printout */
253 TPM_DEBUG("Found TPM %s by %s\n", device_name, vendor_name);
260 * send the passed in data to the TPM device.
262 * @data - address of the data to send, byte by byte
263 * @len - length of the data to send
265 * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
266 * not accept the entire command).
268 static u32 tis_senddata(const u8 * const data, u32 len)
276 value = tis_wait_reg(TIS_REG_STS, locality, TIS_STS_COMMAND_READY,
277 TIS_STS_COMMAND_READY);
278 if (value == TPM_TIMEOUT_ERR) {
279 printf("%s:%d - failed to get 'command_ready' status\n",
281 return TPM_DRIVER_ERR;
283 burst = BURST_COUNT(value);
288 /* Wait till the device is ready to accept more data. */
290 if (max_cycles++ == MAX_DELAY_US) {
291 printf("%s:%d failed to feed %d bytes of %d\n",
292 __FILE__, __LINE__, len - offset, len);
293 return TPM_DRIVER_ERR;
296 burst = BURST_COUNT(tpm_read(locality, TIS_REG_STS));
302 * Calculate number of bytes the TPM is ready to accept in one
305 * We want to send the last byte outside of the loop (hence
306 * the -1 below) to make sure that the 'expected' status bit
307 * changes to zero exactly after the last byte is fed into the
310 count = min(burst, len - offset - 1);
312 tpm_write(data[offset++], locality, TIS_REG_DATA_FIFO);
314 value = tis_wait_reg(TIS_REG_STS, locality,
315 TIS_STS_VALID, TIS_STS_VALID);
317 if ((value == TPM_TIMEOUT_ERR) || !(value & TIS_STS_EXPECT)) {
318 printf("%s:%d TPM command feed overflow\n",
320 return TPM_DRIVER_ERR;
323 burst = BURST_COUNT(value);
324 if ((offset == (len - 1)) && burst)
326 * We need to be able to send the last byte to the
327 * device, so burst size must be nonzero before we
333 /* Send the last byte. */
334 tpm_write(data[offset++], locality, TIS_REG_DATA_FIFO);
337 * Verify that TPM does not expect any more data as part of this
340 value = tis_wait_reg(TIS_REG_STS, locality,
341 TIS_STS_VALID, TIS_STS_VALID);
342 if ((value == TPM_TIMEOUT_ERR) || (value & TIS_STS_EXPECT)) {
343 printf("%s:%d unexpected TPM status 0x%x\n",
344 __FILE__, __LINE__, value);
345 return TPM_DRIVER_ERR;
348 /* OK, sitting pretty, let's start the command execution. */
349 tpm_write(TIS_STS_TPM_GO, locality, TIS_REG_STS);
357 * read the TPM device response after a command was issued.
359 * @buffer - address where to read the response, byte by byte.
360 * @len - pointer to the size of buffer
362 * On success stores the number of received bytes to len and returns 0. On
363 * errors (misformatted TPM data or synchronization problems) returns
366 static u32 tis_readresponse(u8 *buffer, size_t *len)
372 const u32 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
373 u32 expected_count = *len;
376 /* Wait for the TPM to process the command */
377 status = tis_wait_reg(TIS_REG_STS, locality, has_data, has_data);
378 if (status == TPM_TIMEOUT_ERR) {
379 printf("%s:%d failed processing command\n",
381 return TPM_DRIVER_ERR;
385 while ((burst_count = BURST_COUNT(status)) == 0) {
386 if (max_cycles++ == MAX_DELAY_US) {
387 printf("%s:%d TPM stuck on read\n",
389 return TPM_DRIVER_ERR;
392 status = tpm_read(locality, TIS_REG_STS);
397 while (burst_count-- && (offset < expected_count)) {
398 buffer[offset++] = (u8) tpm_read(locality,
402 * We got the first six bytes of the reply,
403 * let's figure out how many bytes to expect
404 * total - it is stored as a 4 byte number in
405 * network order, starting with offset 2 into
406 * the body of the reply.
411 sizeof(real_length));
412 expected_count = be32_to_cpu(real_length);
414 if ((expected_count < offset) ||
415 (expected_count > *len)) {
416 printf("%s:%d bad response size %d\n",
419 return TPM_DRIVER_ERR;
424 /* Wait for the next portion */
425 status = tis_wait_reg(TIS_REG_STS, locality,
426 TIS_STS_VALID, TIS_STS_VALID);
427 if (status == TPM_TIMEOUT_ERR) {
428 printf("%s:%d failed to read response\n",
430 return TPM_DRIVER_ERR;
433 if (offset == expected_count)
434 break; /* We got all we need */
436 } while ((status & has_data) == has_data);
439 * Make sure we indeed read all there was. The TIS_STS_VALID bit is
442 if (status & TIS_STS_DATA_AVAILABLE) {
443 printf("%s:%d wrong receive status %x\n",
444 __FILE__, __LINE__, status);
445 return TPM_DRIVER_ERR;
448 /* Tell the TPM that we are done. */
449 tpm_write(TIS_STS_COMMAND_READY, locality, TIS_REG_STS);
458 * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on
459 * failure (in case device probing did not succeed).
464 return TPM_DRIVER_ERR;
471 * Requests access to locality 0 for the caller. After all commands have been
472 * completed the caller is supposed to call tis_close().
474 * Returns 0 on success, TPM_DRIVER_ERR on failure.
478 u8 locality = 0; /* we use locality zero for everything */
481 return TPM_DRIVER_ERR;
483 /* now request access to locality */
484 tpm_write(TIS_ACCESS_REQUEST_USE, locality, TIS_REG_ACCESS);
486 /* did we get a lock? */
487 if (tis_wait_reg(TIS_REG_ACCESS, locality,
488 TIS_ACCESS_ACTIVE_LOCALITY,
489 TIS_ACCESS_ACTIVE_LOCALITY) == TPM_TIMEOUT_ERR) {
490 printf("%s:%d - failed to lock locality %d\n",
491 __FILE__, __LINE__, locality);
492 return TPM_DRIVER_ERR;
495 tpm_write(TIS_STS_COMMAND_READY, locality, TIS_REG_STS);
503 * terminate the currect session with the TPM by releasing the locked
504 * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock
505 * removal did not succeed).
510 if (tpm_read(locality, TIS_REG_ACCESS) &
511 TIS_ACCESS_ACTIVE_LOCALITY) {
512 tpm_write(TIS_ACCESS_ACTIVE_LOCALITY, locality, TIS_REG_ACCESS);
514 if (tis_wait_reg(TIS_REG_ACCESS, locality,
515 TIS_ACCESS_ACTIVE_LOCALITY, 0) ==
517 printf("%s:%d - failed to release locality %d\n",
518 __FILE__, __LINE__, locality);
519 return TPM_DRIVER_ERR;
528 * Send the requested data to the TPM and then try to get its response
530 * @sendbuf - buffer of the data to send
531 * @send_size size of the data to send
532 * @recvbuf - memory to save the response to
533 * @recv_len - pointer to the size of the response buffer
535 * Returns 0 on success (and places the number of response bytes at recv_len)
536 * or TPM_DRIVER_ERR on failure.
538 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size,
539 uint8_t *recvbuf, size_t *recv_len)
541 if (tis_senddata(sendbuf, send_size)) {
542 printf("%s:%d failed sending data to TPM\n",
544 return TPM_DRIVER_ERR;
547 return tis_readresponse(recvbuf, recv_len);