drivers/uart: Replace 'unsigned long int' by 'unsigned long'
[coreboot2.git] / src / drivers / pc80 / tpm / tis.c
blob93cb2a6163a4b77396a08904b8d41b4b1f470a60
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * The code in this file has been heavily based on the article "Writing a TPM
5 * Device Driver" published on http://ptgmedia.pearsoncmg.com and the
6 * submission by Stefan Berger on Qemu-devel mailing list.
8 * One principal difference is that in the simplest config the other than 0
9 * TPM localities do not get mapped by some devices (for instance, by
10 * Infineon slb9635), so this driver provides access to locality 0 only.
13 #include <commonlib/helpers.h>
14 #include <string.h>
15 #include <delay.h>
16 #include <device/mmio.h>
17 #include <acpi/acpi.h>
18 #include <acpi/acpigen.h>
19 #include <acpi/acpi_device.h>
20 #include <device/device.h>
21 #include <console/console.h>
22 #include <security/tpm/tis.h>
23 #include <security/tpm/tss.h>
24 #include <device/pnp.h>
25 #include <drivers/tpm/tpm_ppi.h>
26 #include <timer.h>
28 #include "chip.h"
29 #include "tpm.h"
31 #define PREFIX "lpc_tpm: "
33 /* coreboot wrapper for TPM driver (start) */
34 #define TPM_DEBUG(fmt, args...) \
35 if (CONFIG(DEBUG_TPM)) { \
36 printk(BIOS_DEBUG, PREFIX); \
37 printk(BIOS_DEBUG, fmt, ##args); \
39 #define TPM_DEBUG_IO_READ(reg_, val_) \
40 TPM_DEBUG("Read reg %#x returns %#x\n", (reg_), (val_))
41 #define TPM_DEBUG_IO_WRITE(reg_, val_) \
42 TPM_DEBUG("Write reg %#x with %#x\n", (reg_), (val_))
43 #define printf(x...) printk(BIOS_ERR, x)
45 /* coreboot wrapper for TPM driver (end) */
47 /* the macro accepts the locality value, but only locality 0 is operational */
48 #define TIS_REG(LOCALITY, REG) \
49 (void *)(uintptr_t)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
51 /* hardware registers' offsets */
52 #define TIS_REG_ACCESS 0x0
53 #define TIS_REG_INT_ENABLE 0x8
54 #define TIS_REG_INT_VECTOR 0xc
55 #define TIS_REG_INT_STATUS 0x10
56 #define TIS_REG_INTF_CAPABILITY 0x14
57 #define TIS_REG_STS 0x18
58 #define TIS_REG_BURST_COUNT 0x19
59 #define TIS_REG_DATA_FIFO 0x24
60 #define TIS_REG_INTF_ID 0x30
61 #define TIS_REG_DID_VID 0xf00
62 #define TIS_REG_RID 0xf04
64 /* Some registers' bit field definitions */
65 #define TIS_STS_VALID (1 << 7) /* 0x80 */
66 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
67 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
68 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
69 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
70 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
72 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
73 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
74 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
75 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
76 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
77 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
78 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
80 /* 1 second is plenty for anything TPM does.*/
81 #define MAX_DELAY_US USECS_PER_SEC
84 * Structures defined below allow creating descriptions of TPM vendor/device
85 * ID information for run time discovery.
87 struct device_name {
88 u16 dev_id;
89 enum tpm_family family;
90 const char *const dev_name;
93 struct vendor_name {
94 u16 vendor_id;
95 const char *vendor_name;
96 const struct device_name *dev_names;
99 static const struct device_name atmel_devices[] = {
100 {0x3204, TPM_1, "AT97SC3204"},
101 {0xffff}
104 static const struct device_name infineon_devices[] = {
105 {0x000b, TPM_1, "SLB9635 TT 1.2"},
106 {0x001a, TPM_1, "SLB9660 TT 1.2"},
107 {0x001b, TPM_1, "SLB9670 TT 1.2"},
108 {0x001a, TPM_2, "SLB9665 TT 2.0"},
109 {0x001b, TPM_2, "SLB9670 TT 2.0"},
110 {0x001d, TPM_2, "SLB9672 TT 2.0"},
111 {0xffff}
114 static const struct device_name nuvoton_devices[] = {
115 {0x00fe, TPM_1, "NPCT420AA V2"},
116 {0xffff}
119 static const struct device_name stmicro_devices[] = {
120 {0x0000, TPM_1, "ST33ZP24" },
121 {0xffff}
124 static const struct device_name swtpm_devices[] = {
125 {0x0001, TPM_1, "SwTPM 1.2" },
126 {0x0001, TPM_2, "SwTPM 2.0" },
127 {0xffff}
130 static const struct vendor_name vendor_names[] = {
131 {0x1114, "Atmel", atmel_devices},
132 {0x15d1, "Infineon", infineon_devices},
133 {0x1050, "Nuvoton", nuvoton_devices},
134 {0x1014, "TPM Emulator", swtpm_devices},
135 {0x104a, "ST Microelectronics", stmicro_devices},
139 * Cached vendor/device ID pair to indicate that the device has been already
140 * discovered
142 static u32 vendor_dev_id;
144 static inline u8 tpm_read_status(int locality)
146 u8 value = read8(TIS_REG(locality, TIS_REG_STS));
147 TPM_DEBUG_IO_READ(TIS_REG_STS, value);
148 return value;
151 static inline void tpm_write_status(u8 sts, int locality)
153 TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts);
154 write8(TIS_REG(locality, TIS_REG_STS), sts);
157 static inline u8 tpm_read_data(int locality)
159 u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO));
160 TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value);
161 return value;
164 static inline void tpm_write_data(u8 data, int locality)
166 TPM_DEBUG_IO_WRITE(TIS_REG_DATA_FIFO, data);
167 write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data);
170 static inline u16 tpm_read_burst_count(int locality)
172 u16 count;
173 count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT));
174 count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8;
175 TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count);
176 return count;
179 static inline u8 tpm_read_access(int locality)
181 u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS));
182 TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value);
183 return value;
186 static inline void tpm_write_access(u8 data, int locality)
188 TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data);
189 write8(TIS_REG(locality, TIS_REG_ACCESS), data);
192 static inline u32 tpm_read_intf_cap(int locality)
194 u32 value = read32(TIS_REG(locality, TIS_REG_INTF_CAPABILITY));
195 TPM_DEBUG_IO_READ(TIS_REG_INTF_CAPABILITY, value);
196 return value;
199 static inline u32 tpm_read_intf_id(int locality)
201 u32 value = read32(TIS_REG(locality, TIS_REG_INTF_ID));
202 TPM_DEBUG_IO_READ(TIS_REG_INTF_ID, value);
203 return value;
206 static inline u32 tpm_read_did_vid(int locality)
208 u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID));
209 TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value);
210 return value;
213 static inline void tpm_write_int_vector(int vector, int locality)
215 TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector);
216 write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf);
219 static inline u8 tpm_read_int_vector(int locality)
221 u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR));
222 TPM_DEBUG_IO_READ(TIS_REG_INT_VECTOR, value);
223 return value;
226 static inline void tpm_write_int_polarity(int polarity, int locality)
228 /* Set polarity and leave all other bits at 0 */
229 u32 value = (polarity & 0x3) << 3;
230 TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value);
231 write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value);
234 static inline u32 tpm_read_int_polarity(int locality)
236 /* Get polarity and leave all other bits */
237 u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE));
238 value = (value >> 3) & 0x3;
239 TPM_DEBUG_IO_READ(TIS_REG_INT_ENABLE, value);
240 return value;
244 * tis_wait_sts()
246 * Wait for at most a second for a status to change its state to match the
247 * expected state. Normally the transition happens within microseconds.
249 * @locality - locality
250 * @mask - bitmask for the bitfield(s) to watch
251 * @expected - value the field(s) are supposed to be set to
253 * Returns TPM_SUCCESS on success or TPM_CB_TIMEOUT on timeout.
255 static tpm_result_t tis_wait_sts(int locality, u8 mask, u8 expected)
257 struct stopwatch sw;
259 stopwatch_init_usecs_expire(&sw, MAX_DELAY_US);
260 do {
261 u8 value = tpm_read_status(locality);
262 if ((value & mask) == expected)
263 return TPM_SUCCESS;
264 udelay(1);
265 } while (!stopwatch_expired(&sw));
266 return TPM_CB_TIMEOUT;
269 static inline tpm_result_t tis_wait_ready(int locality)
271 return tis_wait_sts(locality, TIS_STS_COMMAND_READY,
272 TIS_STS_COMMAND_READY);
275 static inline tpm_result_t tis_wait_valid(int locality)
277 return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID);
280 static inline tpm_result_t tis_wait_valid_data(int locality)
282 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
283 return tis_wait_sts(locality, has_data, has_data);
286 static inline int tis_has_valid_data(int locality)
288 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
291 * Certain TPMs require a small delay here, as they have set
292 * TIS_STS_VALID first and TIS_STS_DATA_AVAILABLE few clocks later.
294 if ((tpm_read_status(locality) & has_data) == has_data)
295 return 1;
297 return (tpm_read_status(locality) & has_data) == has_data;
300 static inline int tis_expect_data(int locality)
302 return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
306 * tis_wait_access()
308 * Wait for at most a second for a access to change its state to match the
309 * expected state. Normally the transition happens within microseconds.
311 * @locality - locality
312 * @mask - bitmask for the bitfield(s) to watch
313 * @expected - value the field(s) are supposed to be set to
315 * Returns TPM_SUCCESS on success or TPM_CB_TIMEOUT on timeout.
317 static tpm_result_t tis_wait_access(int locality, u8 mask, u8 expected)
319 struct stopwatch sw;
321 stopwatch_init_usecs_expire(&sw, MAX_DELAY_US);
322 do {
323 u8 value = tpm_read_access(locality);
324 if ((value & mask) == expected)
325 return TPM_SUCCESS;
326 udelay(1);
327 } while (!stopwatch_expired(&sw));
328 return TPM_CB_TIMEOUT;
331 static inline tpm_result_t tis_wait_received_access(int locality)
333 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY,
334 TIS_ACCESS_ACTIVE_LOCALITY);
337 static inline int tis_has_access(int locality)
339 return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
342 static inline void tis_request_access(int locality)
344 tpm_write_access(TIS_ACCESS_REQUEST_USE, locality);
348 * PC Client Specific TPM Interface Specification section 11.2.12:
350 * Software must be prepared to send two writes of a "1" to command ready
351 * field: the first to indicate successful read of all the data, thus
352 * clearing the data from the ReadFIFO and freeing the TPM's resources,
353 * and the second to indicate to the TPM it is about to send a new command.
355 * In practice not all TPMs behave the same so it is necessary to be
356 * flexible when trying to set command ready.
359 static tpm_result_t tis_command_ready(u8 locality)
361 u32 status;
363 /* 1st attempt to set command ready */
364 tpm_write_status(TIS_STS_COMMAND_READY, locality);
366 /* Wait for response */
367 status = tpm_read_status(locality);
369 /* Check if command ready is set yet */
370 if (status & TIS_STS_COMMAND_READY)
371 return TPM_SUCCESS;
373 /* 2nd attempt to set command ready */
374 tpm_write_status(TIS_STS_COMMAND_READY, locality);
376 return tis_wait_ready(locality);
380 * pc80_tis_probe()
382 * Probe the TPM device and try determining its manufacturer/device name.
384 * Returns TPM_SUCCESS on success (the device is found or was found during
385 * an earlier invocation) or TPM_CB_FAIL if the device is not found.
387 static tpm_result_t pc80_tpm_probe(enum tpm_family *family)
389 static enum tpm_family tpm_family;
391 const char *device_name = NULL;
392 const char *vendor_name = NULL;
393 const struct device_name *dev;
394 u32 didvid, intf_id;
395 u16 vid, did;
396 u8 locality = 0, intf_type;
397 int i;
398 const char *family_str;
400 if (vendor_dev_id) {
401 if (family != NULL)
402 *family = tpm_family;
403 return TPM_SUCCESS; /* Already probed. */
406 didvid = tpm_read_did_vid(0);
407 if (!didvid || (didvid == 0xffffffff)) {
408 printf("%s: No TPM device found\n", __func__);
409 return TPM_CB_FAIL;
412 intf_id = tpm_read_intf_id(locality);
413 intf_type = (intf_id & 0xf);
414 if (intf_type == 0xf) {
415 u32 intf_cap = tpm_read_intf_cap(locality);
416 u8 intf_version = (intf_cap >> 28) & 0x7;
417 switch (intf_version) {
418 case 0:
419 case 2:
420 tpm_family = TPM_1;
421 break;
422 case 3:
423 tpm_family = TPM_2;
424 break;
425 default:
426 printf("%s: Unexpected TPM interface version: %d\n", __func__,
427 intf_version);
428 return TPM_CB_PROBE_FAILURE;
430 } else if (intf_type == 0) {
431 tpm_family = TPM_2;
432 } else {
433 printf("%s: Unexpected TPM interface type: %d\n", __func__, intf_type);
434 return TPM_CB_PROBE_FAILURE;
437 vendor_dev_id = didvid;
439 vid = didvid & 0xffff;
440 did = (didvid >> 16) & 0xffff;
441 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
442 int j = 0;
443 if (vid == vendor_names[i].vendor_id) {
444 vendor_name = vendor_names[i].vendor_name;
445 } else {
446 continue;
448 dev = &vendor_names[i].dev_names[j];
449 while (dev->dev_id != 0xffff) {
450 if (dev->dev_id == did && dev->family == tpm_family) {
451 device_name = dev->dev_name;
452 break;
454 j++;
455 dev = &vendor_names[i].dev_names[j];
457 break;
460 family_str = (tpm_family == TPM_1 ? "TPM 1.2" : "TPM 2.0");
461 if (vendor_name == NULL) {
462 printk(BIOS_INFO, "Found %s 0x%04x by 0x%04x\n", family_str, did, vid);
463 } else if (device_name == NULL) {
464 printk(BIOS_INFO, "Found %s 0x%04x by %s (0x%04x)\n", family_str, did,
465 vendor_name, vid);
466 } else {
467 printk(BIOS_INFO, "Found %s %s (0x%04x) by %s (0x%04x)\n", family_str,
468 device_name, did, vendor_name, vid);
471 if (family != NULL)
472 *family = tpm_family;
473 return TPM_SUCCESS;
477 * tis_senddata()
479 * send the passed in data to the TPM device.
481 * @data - address of the data to send, byte by byte
482 * @len - length of the data to send
484 * Returns TPM_SUCCESS on success, TPM_CB_FAIL on error (in case the device does
485 * not accept the entire command).
487 static tpm_result_t tis_senddata(const u8 *const data, u32 len)
489 u32 offset = 0;
490 u16 burst = 0;
491 u8 locality = 0;
492 tpm_result_t rc = TPM_SUCCESS;
494 rc = tis_wait_ready(locality);
495 if (rc) {
496 printf("%s:%d - failed to get 'command_ready' status with error %#x\n",
497 __FILE__, __LINE__, rc);
498 return rc;
500 burst = tpm_read_burst_count(locality);
502 while (1) {
503 unsigned int count;
504 struct stopwatch sw;
506 /* Wait till the device is ready to accept more data. */
507 stopwatch_init_usecs_expire(&sw, MAX_DELAY_US);
508 while (!burst) {
509 if (stopwatch_expired(&sw)) {
510 printf("%s:%d failed to feed %u bytes of %u\n",
511 __FILE__, __LINE__, len - offset, len);
512 return TPM_CB_TIMEOUT;
514 udelay(1);
515 burst = tpm_read_burst_count(locality);
519 * Calculate number of bytes the TPM is ready to accept in one
520 * shot.
522 * We want to send the last byte outside of the loop (hence
523 * the -1 below) to make sure that the 'expected' status bit
524 * changes to zero exactly after the last byte is fed into the
525 * FIFO.
527 count = MIN(burst, len - offset - 1);
528 while (count--)
529 tpm_write_data(data[offset++], locality);
531 rc = tis_wait_valid(locality);
532 if (rc || !tis_expect_data(locality)) {
533 printf("%s:%d TPM command feed overflow with error %#x\n",
534 __FILE__, __LINE__, rc);
535 return rc ? rc : TPM_CB_FAIL;
538 burst = tpm_read_burst_count(locality);
539 if ((offset == (len - 1)) && burst)
541 * We need to be able to send the last byte to the
542 * device, so burst size must be nonzero before we
543 * break out.
545 break;
548 /* Send the last byte. */
549 tpm_write_data(data[offset++], locality);
552 * Verify that TPM does not expect any more data as part of this
553 * command.
555 rc = tis_wait_valid(locality);
556 if (rc || tis_expect_data(locality)) {
557 printf("%s:%d unexpected TPM error %#x with status %#x\n",
558 __FILE__, __LINE__, rc, tpm_read_status(locality));
559 return rc ? rc : TPM_CB_FAIL;
562 /* OK, sitting pretty, let's start the command execution. */
563 tpm_write_status(TIS_STS_TPM_GO, locality);
565 return TPM_SUCCESS;
569 * tis_readresponse()
571 * read the TPM device response after a command was issued.
573 * @buffer - address where to read the response, byte by byte.
574 * @len - pointer to the size of buffer
576 * On success stores the number of received bytes to len and returns
577 * TPM_SUCCESS. On errors (misformatted TPM data or synchronization
578 * problems) returns TPM_CB_FAIL.
580 static tpm_result_t tis_readresponse(u8 *buffer, size_t *len)
582 u16 burst_count;
583 u32 offset = 0;
584 u8 locality = 0;
585 u32 expected_count = *len;
586 int max_cycles = 0;
587 tpm_result_t rc = TPM_SUCCESS;
589 /* Wait for the TPM to process the command */
590 rc = tis_wait_valid_data(locality);
591 if (rc) {
592 printf("%s:%d failed processing command with error %#x\n",
593 __FILE__, __LINE__, rc);
594 return rc;
597 do {
598 while ((burst_count = tpm_read_burst_count(locality)) == 0) {
599 if (max_cycles++ == MAX_DELAY_US) {
600 printf("%s:%d TPM stuck on read\n",
601 __FILE__, __LINE__);
602 return TPM_CB_FAIL;
604 udelay(1);
607 max_cycles = 0;
609 while (burst_count-- && (offset < expected_count)) {
610 buffer[offset++] = tpm_read_data(locality);
611 if (offset == 6) {
613 * We got the first six bytes of the reply,
614 * let's figure out how many bytes to expect
615 * total - it is stored as a 4 byte number in
616 * network order, starting with offset 2 into
617 * the body of the reply.
619 u32 real_length;
620 memcpy(&real_length,
621 buffer + 2,
622 sizeof(real_length));
623 expected_count = be32_to_cpu(real_length);
625 if ((expected_count < offset) ||
626 (expected_count > *len)) {
627 printf("%s:%d bad response size %u\n",
628 __FILE__, __LINE__,
629 expected_count);
630 return TPM_CB_FAIL;
635 /* Wait for the next portion */
636 rc = tis_wait_valid(locality);
637 if (rc) {
638 printf("%s:%d failed to read response with error %#x\n",
639 __FILE__, __LINE__, rc);
640 return rc;
643 if (offset == expected_count)
644 break; /* We got all we need */
646 } while (tis_has_valid_data(locality));
648 /* * Make sure we indeed read all there was. */
649 if (tis_has_valid_data(locality)) {
650 printf("%s:%d wrong receive status: %#x %u bytes left\n",
651 __FILE__, __LINE__, tpm_read_status(locality),
652 tpm_read_burst_count(locality));
653 return TPM_CB_FAIL;
656 /* Tell the TPM that we are done. */
657 rc = tis_command_ready(locality);
658 if (rc)
659 return rc;
661 *len = offset;
662 return TPM_SUCCESS;
666 * pc80_tis_open()
668 * Requests access to locality 0 for the caller.
670 * Returns TPM_SUCCESS on success, TSS Error on failure.
672 static tpm_result_t pc80_tis_open(void)
674 u8 locality = 0; /* we use locality zero for everything */
675 tpm_result_t rc = TPM_SUCCESS;
677 if (!tis_has_access(locality)) {
678 /* request access to locality */
679 tis_request_access(locality);
681 /* did we get a lock? */
682 rc = tis_wait_received_access(locality);
683 if (rc) {
684 printf("%s:%d - failed to lock locality %u with error %#x\n",
685 __FILE__, __LINE__, locality, rc);
686 return rc;
689 /* Certain TPMs seem to need some delay here or they hang... */
690 udelay(10);
693 return tis_command_ready(locality);
697 * tis_sendrecv()
699 * Send the requested data to the TPM and then try to get its response
701 * @sendbuf - buffer of the data to send
702 * @send_size size of the data to send
703 * @recvbuf - memory to save the response to
704 * @recv_len - pointer to the size of the response buffer
706 * Returns TPM_SUCCESS on success (and places the number of response bytes
707 * at recv_len) or TPM_CB_FAIL on failure.
709 static tpm_result_t pc80_tpm_sendrecv(const uint8_t *sendbuf, size_t send_size,
710 uint8_t *recvbuf, size_t *recv_len)
712 tpm_result_t rc = tis_senddata(sendbuf, send_size);
713 if (rc) {
714 printf("%s:%d failed sending data to TPM with error %#x\n",
715 __FILE__, __LINE__, rc);
716 return rc;
719 return tis_readresponse(recvbuf, recv_len);
723 * pc80_tis_probe()
725 * Probe for the TPM device and set it up for use within locality 0.
727 * @tpm_family - pointer to tpm_family which is set to TPM family of the device.
729 * Returns pointer to send-receive function on success or NULL on failure.
731 tis_sendrecv_fn pc80_tis_probe(enum tpm_family *family)
733 if (pc80_tpm_probe(family))
734 return NULL;
736 if (pc80_tis_open())
737 return NULL;
739 return &pc80_tpm_sendrecv;
743 * tis_setup_interrupt()
745 * Set up the interrupt vector and polarity for locality 0 and
746 * disable all interrupts so they are unused in firmware but can
747 * be enabled by the OS.
749 * The values used here must match what is passed in the TPM ACPI
750 * device if ACPI is used on the platform.
752 * @vector - TPM interrupt vector
753 * @polarity - TPM interrupt polarity
755 * Returns TPM_SUCCESS on success, TPM_CB_FAIL on failure.
757 static tpm_result_t tis_setup_interrupt(int vector, int polarity)
759 u8 locality = 0;
760 tpm_result_t rc = tlcl_lib_init();
762 if (rc)
763 return rc;
765 /* Set TPM interrupt vector */
766 tpm_write_int_vector(vector, locality);
768 /* Set TPM interrupt polarity and disable interrupts */
769 tpm_write_int_polarity(polarity, locality);
771 return TPM_SUCCESS;
774 static void lpc_tpm_read_resources(struct device *dev)
776 /* Static 5K memory region specified in Kconfig */
777 mmio_range(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000);
780 static void lpc_tpm_set_resources(struct device *dev)
782 struct drivers_pc80_tpm_config *config;
783 DEVTREE_CONST struct resource *res;
785 config = (struct drivers_pc80_tpm_config *)dev->chip_info;
787 for (res = dev->resource_list; res; res = res->next) {
788 if (!(res->flags & IORESOURCE_ASSIGNED))
789 continue;
791 if (res->flags & IORESOURCE_IRQ) {
792 /* Set interrupt vector */
793 tis_setup_interrupt((int)res->base,
794 config->irq_polarity);
795 } else {
796 continue;
799 #if !DEVTREE_EARLY
800 res->flags |= IORESOURCE_STORED;
801 report_resource_stored(dev, res, " <tpm>");
802 #endif
806 #if CONFIG(HAVE_ACPI_TABLES)
807 static void lpc_tpm_fill_ssdt(const struct device *dev)
809 /* Windows 11 requires the following path for TPM to be detected */
810 const char *path = "\\_SB_.PCI0";
812 /* Device */
813 acpigen_write_scope(path);
814 acpigen_write_device(acpi_device_name(dev));
816 if (tlcl_get_family() == TPM_2) {
817 acpigen_write_name_string("_HID", "MSFT0101");
818 acpigen_write_name_string("_CID", "MSFT0101");
819 } else {
820 acpigen_write_name("_HID");
821 acpigen_emit_eisaid("PNP0C31");
823 acpigen_write_name("_CID");
824 acpigen_emit_eisaid("PNP0C31");
827 acpi_device_write_uid(dev);
829 u32 did_vid = tpm_read_did_vid(0);
830 if (did_vid > 0 && did_vid < 0xffffffff)
831 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
832 else
833 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF);
835 u16 port = dev->path.pnp.port;
837 /* Resources */
838 acpigen_write_name("_CRS");
839 acpigen_write_resourcetemplate_header();
840 acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000);
841 if (port)
842 acpigen_write_io16(port, port, 1, 2, 1);
844 if (CONFIG_TPM_PIRQ) {
846 * PIRQ: Update interrupt vector with configured PIRQ
847 * Active-Low Level-Triggered Shared
849 struct acpi_irq tpm_irq_a = ACPI_IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ);
850 acpi_device_write_interrupt(&tpm_irq_a);
851 } else if (tpm_read_int_vector(0) > 0) {
852 u8 int_vec = tpm_read_int_vector(0);
853 u8 int_pol = tpm_read_int_polarity(0);
854 struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec);
856 if (int_pol & 1)
857 tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW;
858 else
859 tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH;
861 if (int_pol & 2)
862 tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED;
863 else
864 tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED;
866 acpi_device_write_interrupt(&tpm_irq);
870 acpigen_write_resourcetemplate_footer();
872 if (!CONFIG(CHROMEOS))
873 tpm_ppi_acpi_fill_ssdt(dev);
875 acpigen_pop_len(); /* Device */
876 acpigen_pop_len(); /* Scope */
878 #if !DEVTREE_EARLY
879 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
880 dev->chip_ops->name, dev_path(dev));
881 #endif
884 static const char *lpc_tpm_acpi_name(const struct device *dev)
886 return "TPM";
888 #endif
890 static struct device_operations lpc_tpm_ops = {
891 .read_resources = lpc_tpm_read_resources,
892 .set_resources = lpc_tpm_set_resources,
893 #if CONFIG(HAVE_ACPI_TABLES)
894 .acpi_name = lpc_tpm_acpi_name,
895 .acpi_fill_ssdt = lpc_tpm_fill_ssdt,
896 #endif
899 static struct device_operations noop_tpm_ops = {
900 .read_resources = noop_read_resources,
901 .set_resources = noop_set_resources,
904 static struct pnp_info pnp_dev_info[] = {
905 { .flags = PNP_IRQ0 }
908 static void enable_dev(struct device *dev)
910 if (CONFIG(TPM)) {
911 if (pc80_tis_probe(NULL) == NULL) {
912 dev->enabled = 0;
913 return;
916 pnp_enable_devices(dev, &lpc_tpm_ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
917 } else {
918 pnp_enable_devices(dev, &noop_tpm_ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
922 struct chip_operations drivers_pc80_tpm_ops = {
923 .name = "LPC TPM",
924 .enable_dev = enable_dev