chromeec/ec_acpi: Define ACPI devices for USB-C ports using UCSI
[coreboot.git] / src / drivers / pc80 / tpm / tis.c
blob3f863a36da7a48a979baaba822c5de112963de29
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;
289 return (tpm_read_status(locality) & has_data) == has_data;
292 static inline int tis_expect_data(int locality)
294 return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
298 * tis_wait_access()
300 * Wait for at most a second for a access to change its state to match the
301 * expected state. Normally the transition happens within microseconds.
303 * @locality - locality
304 * @mask - bitmask for the bitfield(s) to watch
305 * @expected - value the field(s) are supposed to be set to
307 * Returns TPM_SUCCESS on success or TPM_CB_TIMEOUT on timeout.
309 static tpm_result_t tis_wait_access(int locality, u8 mask, u8 expected)
311 struct stopwatch sw;
313 stopwatch_init_usecs_expire(&sw, MAX_DELAY_US);
314 do {
315 u8 value = tpm_read_access(locality);
316 if ((value & mask) == expected)
317 return TPM_SUCCESS;
318 udelay(1);
319 } while (!stopwatch_expired(&sw));
320 return TPM_CB_TIMEOUT;
323 static inline tpm_result_t tis_wait_received_access(int locality)
325 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY,
326 TIS_ACCESS_ACTIVE_LOCALITY);
329 static inline int tis_has_access(int locality)
331 return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
334 static inline void tis_request_access(int locality)
336 tpm_write_access(TIS_ACCESS_REQUEST_USE, locality);
340 * PC Client Specific TPM Interface Specification section 11.2.12:
342 * Software must be prepared to send two writes of a "1" to command ready
343 * field: the first to indicate successful read of all the data, thus
344 * clearing the data from the ReadFIFO and freeing the TPM's resources,
345 * and the second to indicate to the TPM it is about to send a new command.
347 * In practice not all TPMs behave the same so it is necessary to be
348 * flexible when trying to set command ready.
351 static tpm_result_t tis_command_ready(u8 locality)
353 u32 status;
355 /* 1st attempt to set command ready */
356 tpm_write_status(TIS_STS_COMMAND_READY, locality);
358 /* Wait for response */
359 status = tpm_read_status(locality);
361 /* Check if command ready is set yet */
362 if (status & TIS_STS_COMMAND_READY)
363 return TPM_SUCCESS;
365 /* 2nd attempt to set command ready */
366 tpm_write_status(TIS_STS_COMMAND_READY, locality);
368 return tis_wait_ready(locality);
372 * pc80_tis_probe()
374 * Probe the TPM device and try determining its manufacturer/device name.
376 * Returns TPM_SUCCESS on success (the device is found or was found during
377 * an earlier invocation) or TPM_CB_FAIL if the device is not found.
379 static tpm_result_t pc80_tpm_probe(enum tpm_family *family)
381 static enum tpm_family tpm_family;
383 const char *device_name = NULL;
384 const char *vendor_name = NULL;
385 const struct device_name *dev;
386 u32 didvid, intf_id;
387 u16 vid, did;
388 u8 locality = 0, intf_type;
389 int i;
390 const char *family_str;
392 if (vendor_dev_id) {
393 if (family != NULL)
394 *family = tpm_family;
395 return TPM_SUCCESS; /* Already probed. */
398 didvid = tpm_read_did_vid(0);
399 if (!didvid || (didvid == 0xffffffff)) {
400 printf("%s: No TPM device found\n", __func__);
401 return TPM_CB_FAIL;
404 intf_id = tpm_read_intf_id(locality);
405 intf_type = (intf_id & 0xf);
406 if (intf_type == 0xf) {
407 u32 intf_cap = tpm_read_intf_cap(locality);
408 u8 intf_version = (intf_cap >> 28) & 0x7;
409 switch (intf_version) {
410 case 0:
411 case 2:
412 tpm_family = TPM_1;
413 break;
414 case 3:
415 tpm_family = TPM_2;
416 break;
417 default:
418 printf("%s: Unexpected TPM interface version: %d\n", __func__,
419 intf_version);
420 return TPM_CB_PROBE_FAILURE;
422 } else if (intf_type == 0) {
423 tpm_family = TPM_2;
424 } else {
425 printf("%s: Unexpected TPM interface type: %d\n", __func__, intf_type);
426 return TPM_CB_PROBE_FAILURE;
429 vendor_dev_id = didvid;
431 vid = didvid & 0xffff;
432 did = (didvid >> 16) & 0xffff;
433 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
434 int j = 0;
435 if (vid == vendor_names[i].vendor_id) {
436 vendor_name = vendor_names[i].vendor_name;
437 } else {
438 continue;
440 dev = &vendor_names[i].dev_names[j];
441 while (dev->dev_id != 0xffff) {
442 if (dev->dev_id == did && dev->family == tpm_family) {
443 device_name = dev->dev_name;
444 break;
446 j++;
447 dev = &vendor_names[i].dev_names[j];
449 break;
452 family_str = (tpm_family == TPM_1 ? "TPM 1.2" : "TPM 2.0");
453 if (vendor_name == NULL) {
454 printk(BIOS_INFO, "Found %s 0x%04x by 0x%04x\n", family_str, did, vid);
455 } else if (device_name == NULL) {
456 printk(BIOS_INFO, "Found %s 0x%04x by %s (0x%04x)\n", family_str, did,
457 vendor_name, vid);
458 } else {
459 printk(BIOS_INFO, "Found %s %s (0x%04x) by %s (0x%04x)\n", family_str,
460 device_name, did, vendor_name, vid);
463 if (family != NULL)
464 *family = tpm_family;
465 return TPM_SUCCESS;
469 * tis_senddata()
471 * send the passed in data to the TPM device.
473 * @data - address of the data to send, byte by byte
474 * @len - length of the data to send
476 * Returns TPM_SUCCESS on success, TPM_CB_FAIL on error (in case the device does
477 * not accept the entire command).
479 static tpm_result_t tis_senddata(const u8 *const data, u32 len)
481 u32 offset = 0;
482 u16 burst = 0;
483 u8 locality = 0;
484 tpm_result_t rc = TPM_SUCCESS;
486 rc = tis_wait_ready(locality);
487 if (rc) {
488 printf("%s:%d - failed to get 'command_ready' status with error %#x\n",
489 __FILE__, __LINE__, rc);
490 return rc;
492 burst = tpm_read_burst_count(locality);
494 while (1) {
495 unsigned int count;
496 struct stopwatch sw;
498 /* Wait till the device is ready to accept more data. */
499 stopwatch_init_usecs_expire(&sw, MAX_DELAY_US);
500 while (!burst) {
501 if (stopwatch_expired(&sw)) {
502 printf("%s:%d failed to feed %u bytes of %u\n",
503 __FILE__, __LINE__, len - offset, len);
504 return TPM_CB_TIMEOUT;
506 udelay(1);
507 burst = tpm_read_burst_count(locality);
511 * Calculate number of bytes the TPM is ready to accept in one
512 * shot.
514 * We want to send the last byte outside of the loop (hence
515 * the -1 below) to make sure that the 'expected' status bit
516 * changes to zero exactly after the last byte is fed into the
517 * FIFO.
519 count = MIN(burst, len - offset - 1);
520 while (count--)
521 tpm_write_data(data[offset++], locality);
523 rc = tis_wait_valid(locality);
524 if (rc || !tis_expect_data(locality)) {
525 printf("%s:%d TPM command feed overflow with error %#x\n",
526 __FILE__, __LINE__, rc);
527 return rc ? rc : TPM_CB_FAIL;
530 burst = tpm_read_burst_count(locality);
531 if ((offset == (len - 1)) && burst)
533 * We need to be able to send the last byte to the
534 * device, so burst size must be nonzero before we
535 * break out.
537 break;
540 /* Send the last byte. */
541 tpm_write_data(data[offset++], locality);
544 * Verify that TPM does not expect any more data as part of this
545 * command.
547 rc = tis_wait_valid(locality);
548 if (rc || tis_expect_data(locality)) {
549 printf("%s:%d unexpected TPM error %#x with status %#x\n",
550 __FILE__, __LINE__, rc, tpm_read_status(locality));
551 return rc ? rc : TPM_CB_FAIL;
554 /* OK, sitting pretty, let's start the command execution. */
555 tpm_write_status(TIS_STS_TPM_GO, locality);
557 return TPM_SUCCESS;
561 * tis_readresponse()
563 * read the TPM device response after a command was issued.
565 * @buffer - address where to read the response, byte by byte.
566 * @len - pointer to the size of buffer
568 * On success stores the number of received bytes to len and returns
569 * TPM_SUCCESS. On errors (misformatted TPM data or synchronization
570 * problems) returns TPM_CB_FAIL.
572 static tpm_result_t tis_readresponse(u8 *buffer, size_t *len)
574 u16 burst_count;
575 u32 offset = 0;
576 u8 locality = 0;
577 u32 expected_count = *len;
578 int max_cycles = 0;
579 tpm_result_t rc = TPM_SUCCESS;
581 /* Wait for the TPM to process the command */
582 rc = tis_wait_valid_data(locality);
583 if (rc) {
584 printf("%s:%d failed processing command with error %#x\n",
585 __FILE__, __LINE__, rc);
586 return rc;
589 do {
590 while ((burst_count = tpm_read_burst_count(locality)) == 0) {
591 if (max_cycles++ == MAX_DELAY_US) {
592 printf("%s:%d TPM stuck on read\n",
593 __FILE__, __LINE__);
594 return TPM_CB_FAIL;
596 udelay(1);
599 max_cycles = 0;
601 while (burst_count-- && (offset < expected_count)) {
602 buffer[offset++] = tpm_read_data(locality);
603 if (offset == 6) {
605 * We got the first six bytes of the reply,
606 * let's figure out how many bytes to expect
607 * total - it is stored as a 4 byte number in
608 * network order, starting with offset 2 into
609 * the body of the reply.
611 u32 real_length;
612 memcpy(&real_length,
613 buffer + 2,
614 sizeof(real_length));
615 expected_count = be32_to_cpu(real_length);
617 if ((expected_count < offset) ||
618 (expected_count > *len)) {
619 printf("%s:%d bad response size %u\n",
620 __FILE__, __LINE__,
621 expected_count);
622 return TPM_CB_FAIL;
627 /* Wait for the next portion */
628 rc = tis_wait_valid(locality);
629 if (rc) {
630 printf("%s:%d failed to read response with error %#x\n",
631 __FILE__, __LINE__, rc);
632 return rc;
635 if (offset == expected_count)
636 break; /* We got all we need */
639 * Certain TPMs seem to need some delay between tis_wait_valid()
640 * and tis_has_valid_data(), or some race-condition-related
641 * issue will occur.
643 if (CONFIG(TPM_RDRESP_NEED_DELAY))
644 udelay(10);
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