tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / drivers / pc80 / tpm / tpm.c
blobea1109510e65b79b94238f9ef429ce59bcd36e13
1 /*
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.
17 * The code in this file has been heavily based on the article "Writing a TPM
18 * Device Driver" published on http://ptgmedia.pearsoncmg.com and the
19 * submission by Stefan Berger on Qemu-devel mailing list.
21 * One principal difference is that in the simplest config the other than 0
22 * TPM localities do not get mapped by some devices (for instance, by
23 * Infineon slb9635), so this driver provides access to locality 0 only.
26 #include <stdlib.h>
27 #include <string.h>
28 #include <delay.h>
29 #include <arch/io.h>
30 #include <console/console.h>
31 #include <tpm.h>
32 #include <arch/early_variables.h>
33 #include <device/pnp.h>
34 #include "chip.h"
36 #define PREFIX "lpc_tpm: "
38 /* coreboot wrapper for TPM driver (start) */
39 #define TPM_DEBUG(fmt, args...) \
40 if (CONFIG_DEBUG_TPM) { \
41 printk(BIOS_DEBUG, PREFIX); \
42 printk(BIOS_DEBUG, fmt , ##args); \
44 #define TPM_DEBUG_IO_READ(reg_, val_) \
45 TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_))
46 #define TPM_DEBUG_IO_WRITE(reg_, val_) \
47 TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_))
48 #define printf(x...) printk(BIOS_ERR, x)
50 /* coreboot wrapper for TPM driver (end) */
52 #ifndef CONFIG_TPM_TIS_BASE_ADDRESS
53 /* Base TPM address standard for x86 systems */
54 #define CONFIG_TPM_TIS_BASE_ADDRESS 0xfed40000
55 #endif
57 /* the macro accepts the locality value, but only locality 0 is operational */
58 #define TIS_REG(LOCALITY, REG) \
59 (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
61 /* hardware registers' offsets */
62 #define TIS_REG_ACCESS 0x0
63 #define TIS_REG_INT_ENABLE 0x8
64 #define TIS_REG_INT_VECTOR 0xc
65 #define TIS_REG_INT_STATUS 0x10
66 #define TIS_REG_INTF_CAPABILITY 0x14
67 #define TIS_REG_STS 0x18
68 #define TIS_REG_BURST_COUNT 0x19
69 #define TIS_REG_DATA_FIFO 0x24
70 #define TIS_REG_DID_VID 0xf00
71 #define TIS_REG_RID 0xf04
73 /* Some registers' bit field definitions */
74 #define TIS_STS_VALID (1 << 7) /* 0x80 */
75 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
76 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
77 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
78 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
79 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
81 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
82 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
83 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
84 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
85 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
86 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
87 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
90 * Error value returned if a tpm register does not enter the expected state
91 * after continuous polling. No actual TPM register reading ever returns ~0,
92 * so this value is a safe error indication to be mixed with possible status
93 * register values.
95 #define TPM_TIMEOUT_ERR (~0)
97 /* Error value returned on various TPM driver errors */
98 #define TPM_DRIVER_ERR (~0)
100 /* 1 second is plenty for anything TPM does.*/
101 #define MAX_DELAY_US (1000 * 1000)
104 * Structures defined below allow creating descriptions of TPM vendor/device
105 * ID information for run time discovery. The only device the system knows
106 * about at this time is Infineon slb9635
108 struct device_name {
109 u16 dev_id;
110 const char * const dev_name;
113 struct vendor_name {
114 u16 vendor_id;
115 const char * vendor_name;
116 const struct device_name* dev_names;
119 static const struct device_name atmel_devices[] = {
120 {0x3204, "AT97SC3204"},
121 {0xffff}
124 static const struct device_name infineon_devices[] = {
125 {0x000b, "SLB9635 TT 1.2"},
126 {0x001a, "SLB9660 TT 1.2"},
127 {0x001b, "SLB9670 TT 1.2"},
128 {0xffff}
131 static const struct device_name nuvoton_devices[] = {
132 {0x00fe, "NPCT420AA V2"},
133 {0xffff}
136 static const struct device_name stmicro_devices[] = {
137 {0x0000, "ST33ZP24" },
138 {0xffff}
141 static const struct vendor_name vendor_names[] = {
142 {0x1114, "Atmel", atmel_devices},
143 {0x15d1, "Infineon", infineon_devices},
144 {0x1050, "Nuvoton", nuvoton_devices},
145 {0x104a, "ST Microelectronics", stmicro_devices},
149 * Cached vendor/device ID pair to indicate that the device has been already
150 * discovered
152 static u32 vendor_dev_id CAR_GLOBAL;
154 static inline u8 tpm_read_status(int locality)
156 u8 value = read8(TIS_REG(locality, TIS_REG_STS));
157 TPM_DEBUG_IO_READ(TIS_REG_STS, value);
158 return value;
161 static inline void tpm_write_status(u8 sts, int locality)
163 TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts);
164 write8(TIS_REG(locality, TIS_REG_STS), sts);
167 static inline u8 tpm_read_data(int locality)
169 u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO));
170 TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value);
171 return value;
174 static inline void tpm_write_data(u8 data, int locality)
176 TPM_DEBUG_IO_WRITE(TIS_REG_STS, data);
177 write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data);
180 static inline u16 tpm_read_burst_count(int locality)
182 u16 count;
183 count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT));
184 count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8;
185 TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count);
186 return count;
189 static inline u8 tpm_read_access(int locality)
191 u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS));
192 TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value);
193 return value;
196 static inline void tpm_write_access(u8 data, int locality)
198 TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data);
199 write8(TIS_REG(locality, TIS_REG_ACCESS), data);
202 static inline u32 tpm_read_did_vid(int locality)
204 u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID));
205 TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value);
206 return value;
209 static inline void tpm_write_int_vector(int vector, int locality)
211 TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector);
212 write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf);
215 static inline void tpm_write_int_polarity(int polarity, int locality)
217 /* Set polarity and leave all other bits at 0 */
218 u32 value = (polarity & 0x3) << 3;
219 TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value);
220 write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value);
224 * tis_wait_sts()
226 * Wait for at least a second for a status to change its state to match the
227 * expected state. Normally the transition happens within microseconds.
229 * @locality - locality
230 * @mask - bitmask for the bitfield(s) to watch
231 * @expected - value the field(s) are supposed to be set to
233 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
235 static int tis_wait_sts(int locality, u8 mask, u8 expected)
237 u32 time_us = MAX_DELAY_US;
238 while (time_us > 0) {
239 u8 value = tpm_read_status(locality);
240 if ((value & mask) == expected)
241 return 0;
242 udelay(1); /* 1 us */
243 time_us--;
245 return TPM_TIMEOUT_ERR;
248 static inline int tis_wait_ready(int locality)
250 return tis_wait_sts(locality, TIS_STS_COMMAND_READY,
251 TIS_STS_COMMAND_READY);
254 static inline int tis_wait_valid(int locality)
256 return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID);
259 static inline int tis_wait_valid_data(int locality)
261 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
262 return tis_wait_sts(locality, has_data, has_data);
265 static inline int tis_has_valid_data(int locality)
267 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
268 return (tpm_read_status(locality) & has_data) == has_data;
271 static inline int tis_expect_data(int locality)
273 return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
277 * tis_wait_access()
279 * Wait for at least a second for a access to change its state to match the
280 * expected state. Normally the transition happens within microseconds.
282 * @locality - locality
283 * @mask - bitmask for the bitfield(s) to watch
284 * @expected - value the field(s) are supposed to be set to
286 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
288 static int tis_wait_access(int locality, u8 mask, u8 expected)
290 u32 time_us = MAX_DELAY_US;
291 while (time_us > 0) {
292 u8 value = tpm_read_access(locality);
293 if ((value & mask) == expected)
294 return 0;
295 udelay(1); /* 1 us */
296 time_us--;
298 return TPM_TIMEOUT_ERR;
301 static inline int tis_wait_dropped_access(int locality)
303 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0);
306 static inline int tis_wait_received_access(int locality)
308 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY,
309 TIS_ACCESS_ACTIVE_LOCALITY);
312 static inline int tis_has_access(int locality)
314 return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
317 static inline void tis_request_access(int locality)
319 tpm_write_access(TIS_ACCESS_REQUEST_USE, locality);
322 static inline void tis_drop_access(int locality)
324 tpm_write_access(TIS_ACCESS_ACTIVE_LOCALITY, locality);
328 * PC Client Specific TPM Interface Specification section 11.2.12:
330 * Software must be prepared to send two writes of a "1" to command ready
331 * field: the first to indicate successful read of all the data, thus
332 * clearing the data from the ReadFIFO and freeing the TPM's resources,
333 * and the second to indicate to the TPM it is about to send a new command.
335 * In practice not all TPMs behave the same so it is necessary to be
336 * flexible when trying to set command ready.
338 * Returns 0 on success if the TPM is ready for transactions.
339 * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set.
341 static int tis_command_ready(u8 locality)
343 u32 status;
345 /* 1st attempt to set command ready */
346 tpm_write_status(TIS_STS_COMMAND_READY, locality);
348 /* Wait for response */
349 status = tpm_read_status(locality);
351 /* Check if command ready is set yet */
352 if (status & TIS_STS_COMMAND_READY)
353 return 0;
355 /* 2nd attempt to set command ready */
356 tpm_write_status(TIS_STS_COMMAND_READY, locality);
358 return tis_wait_ready(locality);
362 * Probe the TPM device and try determining its manufacturer/device name.
364 * Returns 0 on success (the device is found or was found during an earlier
365 * invocation) or TPM_DRIVER_ERR if the device is not found.
367 static u32 tis_probe(void)
369 const char *device_name = "unknown";
370 const char *vendor_name = device_name;
371 const struct device_name *dev;
372 u32 didvid;
373 u16 vid, did;
374 int i;
376 if (car_get_var(vendor_dev_id))
377 return 0; /* Already probed. */
379 didvid = tpm_read_did_vid(0);
380 if (!didvid || (didvid == 0xffffffff)) {
381 printf("%s: No TPM device found\n", __FUNCTION__);
382 return TPM_DRIVER_ERR;
385 car_set_var(vendor_dev_id, didvid);
387 vid = didvid & 0xffff;
388 did = (didvid >> 16) & 0xffff;
389 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
390 int j = 0;
391 u16 known_did;
392 if (vid == vendor_names[i].vendor_id) {
393 vendor_name = vendor_names[i].vendor_name;
394 } else {
395 continue;
397 dev = &vendor_names[i].dev_names[j];
398 while ((known_did = dev->dev_id) != 0xffff) {
399 if (known_did == did) {
400 device_name = dev->dev_name;
401 break;
403 j++;
404 dev = &vendor_names[i].dev_names[j];
406 break;
408 /* this will have to be converted into debug printout */
409 printk(BIOS_INFO, "Found TPM %s by %s\n", device_name, vendor_name);
410 return 0;
414 * tis_senddata()
416 * send the passed in data to the TPM device.
418 * @data - address of the data to send, byte by byte
419 * @len - length of the data to send
421 * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
422 * not accept the entire command).
424 static u32 tis_senddata(const u8 * const data, u32 len)
426 u32 offset = 0;
427 u16 burst = 0;
428 u32 max_cycles = 0;
429 u8 locality = 0;
431 if (tis_wait_ready(locality)) {
432 printf("%s:%d - failed to get 'command_ready' status\n",
433 __FILE__, __LINE__);
434 return TPM_DRIVER_ERR;
436 burst = tpm_read_burst_count(locality);
438 while (1) {
439 unsigned count;
441 /* Wait till the device is ready to accept more data. */
442 while (!burst) {
443 if (max_cycles++ == MAX_DELAY_US) {
444 printf("%s:%d failed to feed %d bytes of %d\n",
445 __FILE__, __LINE__, len - offset, len);
446 return TPM_DRIVER_ERR;
448 udelay(1);
449 burst = tpm_read_burst_count(locality);
452 max_cycles = 0;
455 * Calculate number of bytes the TPM is ready to accept in one
456 * shot.
458 * We want to send the last byte outside of the loop (hence
459 * the -1 below) to make sure that the 'expected' status bit
460 * changes to zero exactly after the last byte is fed into the
461 * FIFO.
463 count = min(burst, len - offset - 1);
464 while (count--)
465 tpm_write_data(data[offset++], locality);
467 if (tis_wait_valid(locality) || !tis_expect_data(locality)) {
468 printf("%s:%d TPM command feed overflow\n",
469 __FILE__, __LINE__);
470 return TPM_DRIVER_ERR;
473 burst = tpm_read_burst_count(locality);
474 if ((offset == (len - 1)) && burst)
476 * We need to be able to send the last byte to the
477 * device, so burst size must be nonzero before we
478 * break out.
480 break;
483 /* Send the last byte. */
484 tpm_write_data(data[offset++], locality);
487 * Verify that TPM does not expect any more data as part of this
488 * command.
490 if (tis_wait_valid(locality) || tis_expect_data(locality)) {
491 printf("%s:%d unexpected TPM status 0x%x\n",
492 __FILE__, __LINE__, tpm_read_status(locality));
493 return TPM_DRIVER_ERR;
496 /* OK, sitting pretty, let's start the command execution. */
497 tpm_write_status(TIS_STS_TPM_GO, locality);
499 return 0;
503 * tis_readresponse()
505 * read the TPM device response after a command was issued.
507 * @buffer - address where to read the response, byte by byte.
508 * @len - pointer to the size of buffer
510 * On success stores the number of received bytes to len and returns 0. On
511 * errors (misformatted TPM data or synchronization problems) returns
512 * TPM_DRIVER_ERR.
514 static u32 tis_readresponse(u8 *buffer, size_t *len)
516 u16 burst_count;
517 u32 offset = 0;
518 u8 locality = 0;
519 u32 expected_count = *len;
520 int max_cycles = 0;
522 /* Wait for the TPM to process the command */
523 if (tis_wait_valid_data(locality)) {
524 printf("%s:%d failed processing command\n", __FILE__, __LINE__);
525 return TPM_DRIVER_ERR;
528 do {
529 while ((burst_count = tpm_read_burst_count(locality)) == 0) {
530 if (max_cycles++ == MAX_DELAY_US) {
531 printf("%s:%d TPM stuck on read\n",
532 __FILE__, __LINE__);
533 return TPM_DRIVER_ERR;
535 udelay(1);
538 max_cycles = 0;
540 while (burst_count-- && (offset < expected_count)) {
541 buffer[offset++] = tpm_read_data(locality);
542 if (offset == 6) {
544 * We got the first six bytes of the reply,
545 * let's figure out how many bytes to expect
546 * total - it is stored as a 4 byte number in
547 * network order, starting with offset 2 into
548 * the body of the reply.
550 u32 real_length;
551 memcpy(&real_length,
552 buffer + 2,
553 sizeof(real_length));
554 expected_count = be32_to_cpu(real_length);
556 if ((expected_count < offset) ||
557 (expected_count > *len)) {
558 printf("%s:%d bad response size %d\n",
559 __FILE__, __LINE__,
560 expected_count);
561 return TPM_DRIVER_ERR;
566 /* Wait for the next portion */
567 if (tis_wait_valid(locality)) {
568 printf("%s:%d failed to read response\n",
569 __FILE__, __LINE__);
570 return TPM_DRIVER_ERR;
573 if (offset == expected_count)
574 break; /* We got all we need */
576 } while (tis_has_valid_data(locality));
578 /* * Make sure we indeed read all there was. */
579 if (tis_has_valid_data(locality)) {
580 printf("%s:%d wrong receive status: %x %d bytes left\n",
581 __FILE__, __LINE__, tpm_read_status(locality),
582 tpm_read_burst_count(locality));
583 return TPM_DRIVER_ERR;
586 /* Tell the TPM that we are done. */
587 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
588 return TPM_DRIVER_ERR;
590 *len = offset;
591 return 0;
595 * tis_init()
597 * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on
598 * failure (in case device probing did not succeed).
600 int tis_init(void)
602 if (tis_probe())
603 return TPM_DRIVER_ERR;
604 return 0;
608 * tis_open()
610 * Requests access to locality 0 for the caller. After all commands have been
611 * completed the caller is supposed to call tis_close().
613 * Returns 0 on success, TPM_DRIVER_ERR on failure.
615 int tis_open(void)
617 u8 locality = 0; /* we use locality zero for everything */
619 if (tis_close())
620 return TPM_DRIVER_ERR;
622 /* now request access to locality */
623 tis_request_access(locality);
625 /* did we get a lock? */
626 if (tis_wait_received_access(locality)) {
627 printf("%s:%d - failed to lock locality %d\n",
628 __FILE__, __LINE__, locality);
629 return TPM_DRIVER_ERR;
632 /* Certain TPMs seem to need some delay here or they hang... */
633 udelay(10);
635 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
636 return TPM_DRIVER_ERR;
638 return 0;
642 * tis_close()
644 * terminate the current session with the TPM by releasing the locked
645 * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock
646 * removal did not succeed).
648 int tis_close(void)
650 u8 locality = 0;
651 if (tis_has_access(locality)) {
652 tis_drop_access(locality);
653 if (tis_wait_dropped_access(locality)) {
654 printf("%s:%d - failed to release locality %d\n",
655 __FILE__, __LINE__, locality);
656 return TPM_DRIVER_ERR;
659 return 0;
663 * tis_sendrecv()
665 * Send the requested data to the TPM and then try to get its response
667 * @sendbuf - buffer of the data to send
668 * @send_size size of the data to send
669 * @recvbuf - memory to save the response to
670 * @recv_len - pointer to the size of the response buffer
672 * Returns 0 on success (and places the number of response bytes at recv_len)
673 * or TPM_DRIVER_ERR on failure.
675 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size,
676 uint8_t *recvbuf, size_t *recv_len)
678 if (tis_senddata(sendbuf, send_size)) {
679 printf("%s:%d failed sending data to TPM\n",
680 __FILE__, __LINE__);
681 return TPM_DRIVER_ERR;
684 return tis_readresponse(recvbuf, recv_len);
687 #ifdef __RAMSTAGE__
690 * tis_setup_interrupt()
692 * Set up the interrupt vector and polarity for locality 0 and
693 * disable all interrupts so they are unused in firmware but can
694 * be enabled by the OS.
696 * The values used here must match what is passed in the TPM ACPI
697 * device if ACPI is used on the platform.
699 * @vector - TPM interrupt vector
700 * @polarity - TPM interrupt polarity
702 * Returns 0 on success, TPM_DRIVER_ERR on failure.
704 static int tis_setup_interrupt(int vector, int polarity)
706 u8 locality = 0;
707 int has_access = tis_has_access(locality);
709 /* Open connection and request access if not already granted */
710 if (!has_access && tis_open() < 0)
711 return TPM_DRIVER_ERR;
713 /* Set TPM interrupt vector */
714 tpm_write_int_vector(vector, locality);
716 /* Set TPM interupt polarity and disable interrupts */
717 tpm_write_int_polarity(polarity, locality);
719 /* Close connection if it was opened */
720 if (!has_access && tis_close() < 0)
721 return TPM_DRIVER_ERR;
723 return 0;
726 static void lpc_tpm_read_resources(struct device *dev)
728 /* Static 5K memory region specified in Kconfig */
729 mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10);
732 static void lpc_tpm_set_resources(struct device *dev)
734 tpm_config_t *config = (tpm_config_t *)dev->chip_info;
735 struct resource *res;
737 for (res = dev->resource_list; res; res = res->next) {
738 if (!(res->flags & IORESOURCE_ASSIGNED))
739 continue;
741 if (res->flags & IORESOURCE_IRQ) {
742 /* Set interrupt vector */
743 tis_setup_interrupt((int)res->base,
744 config->irq_polarity);
745 } else {
746 continue;
749 res->flags |= IORESOURCE_STORED;
750 report_resource_stored(dev, res, " <tpm>");
754 static struct device_operations lpc_tpm_ops = {
755 .read_resources = &lpc_tpm_read_resources,
756 .set_resources = &lpc_tpm_set_resources,
759 static struct pnp_info pnp_dev_info[] = {
760 { .flags = PNP_IRQ0 }
763 static void enable_dev(struct device *dev)
765 pnp_enable_devices(dev, &lpc_tpm_ops,
766 ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
769 struct chip_operations drivers_pc80_tpm_ops = {
770 CHIP_NAME("LPC TPM")
771 .enable_dev = enable_dev
774 #endif /* __RAMSTAGE__ */