Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 2 | |
| 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. |
| 7 | * |
| 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. |
| 11 | */ |
| 12 | |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 13 | #include <commonlib/helpers.h> |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #include <delay.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 16 | #include <device/mmio.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 17 | #include <acpi/acpi.h> |
| 18 | #include <acpi/acpigen.h> |
| 19 | #include <acpi/acpi_device.h> |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 20 | #include <device/device.h> |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 21 | #include <console/console.h> |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 22 | #include <security/tpm/tis.h> |
Sergii Dmytruk | 0a89d52 | 2022-10-29 16:57:07 +0300 | [diff] [blame] | 23 | #include <security/tpm/tss.h> |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 24 | #include <device/pnp.h> |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 25 | #include <drivers/tpm/tpm_ppi.h> |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 26 | #include <timer.h> |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 27 | #include "chip.h" |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 28 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 29 | #define PREFIX "lpc_tpm: " |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 30 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 31 | /* coreboot wrapper for TPM driver (start) */ |
| 32 | #define TPM_DEBUG(fmt, args...) \ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 33 | if (CONFIG(DEBUG_TPM)) { \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 34 | printk(BIOS_DEBUG, PREFIX); \ |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 35 | printk(BIOS_DEBUG, fmt, ##args); \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 36 | } |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 37 | #define TPM_DEBUG_IO_READ(reg_, val_) \ |
Jon Murphy | 53fc667 | 2023-09-26 21:05:37 -0600 | [diff] [blame] | 38 | TPM_DEBUG("Read reg %#x returns %#x\n", (reg_), (val_)) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 39 | #define TPM_DEBUG_IO_WRITE(reg_, val_) \ |
Jon Murphy | 53fc667 | 2023-09-26 21:05:37 -0600 | [diff] [blame] | 40 | TPM_DEBUG("Write reg %#x with %#x\n", (reg_), (val_)) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 41 | #define printf(x...) printk(BIOS_ERR, x) |
| 42 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 43 | /* coreboot wrapper for TPM driver (end) */ |
| 44 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 45 | /* the macro accepts the locality value, but only locality 0 is operational */ |
| 46 | #define TIS_REG(LOCALITY, REG) \ |
Patrick Rudolph | 56fdafb | 2020-07-01 20:07:08 +0200 | [diff] [blame] | 47 | (void *)(uintptr_t)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 48 | |
| 49 | /* hardware registers' offsets */ |
| 50 | #define TIS_REG_ACCESS 0x0 |
| 51 | #define TIS_REG_INT_ENABLE 0x8 |
| 52 | #define TIS_REG_INT_VECTOR 0xc |
| 53 | #define TIS_REG_INT_STATUS 0x10 |
| 54 | #define TIS_REG_INTF_CAPABILITY 0x14 |
| 55 | #define TIS_REG_STS 0x18 |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 56 | #define TIS_REG_BURST_COUNT 0x19 |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 57 | #define TIS_REG_DATA_FIFO 0x24 |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 58 | #define TIS_REG_INTF_ID 0x30 |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 59 | #define TIS_REG_DID_VID 0xf00 |
| 60 | #define TIS_REG_RID 0xf04 |
| 61 | |
| 62 | /* Some registers' bit field definitions */ |
| 63 | #define TIS_STS_VALID (1 << 7) /* 0x80 */ |
| 64 | #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */ |
| 65 | #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */ |
| 66 | #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */ |
| 67 | #define TIS_STS_EXPECT (1 << 3) /* 0x08 */ |
| 68 | #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */ |
| 69 | |
| 70 | #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */ |
| 71 | #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */ |
| 72 | #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */ |
| 73 | #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */ |
| 74 | #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */ |
| 75 | #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */ |
| 76 | #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */ |
| 77 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 78 | /* 1 second is plenty for anything TPM does.*/ |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 79 | #define MAX_DELAY_US USECS_PER_SEC |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 80 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Structures defined below allow creating descriptions of TPM vendor/device |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 83 | * ID information for run time discovery. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 84 | */ |
| 85 | struct device_name { |
| 86 | u16 dev_id; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 87 | enum tpm_family family; |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 88 | const char *const dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct vendor_name { |
| 92 | u16 vendor_id; |
Elyes HAOUAS | b0b0c8c | 2018-07-08 12:33:47 +0200 | [diff] [blame] | 93 | const char *vendor_name; |
| 94 | const struct device_name *dev_names; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 97 | static const struct device_name atmel_devices[] = { |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 98 | {0x3204, TPM_1, "AT97SC3204"}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 99 | {0xffff} |
| 100 | }; |
| 101 | |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 102 | static const struct device_name infineon_devices[] = { |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 103 | {0x000b, TPM_1, "SLB9635 TT 1.2"}, |
| 104 | {0x001a, TPM_1, "SLB9660 TT 1.2"}, |
| 105 | {0x001b, TPM_1, "SLB9670 TT 1.2"}, |
| 106 | {0x001a, TPM_2, "SLB9665 TT 2.0"}, |
| 107 | {0x001b, TPM_2, "SLB9670 TT 2.0"}, |
| 108 | {0x001d, TPM_2, "SLB9672 TT 2.0"}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 109 | {0xffff} |
| 110 | }; |
| 111 | |
| 112 | static const struct device_name nuvoton_devices[] = { |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 113 | {0x00fe, TPM_1, "NPCT420AA V2"}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 114 | {0xffff} |
| 115 | }; |
| 116 | |
| 117 | static const struct device_name stmicro_devices[] = { |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 118 | {0x0000, TPM_1, "ST33ZP24" }, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 119 | {0xffff} |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 122 | static const struct device_name swtpm_devices[] = { |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 123 | {0x0001, TPM_1, "SwTPM 1.2" }, |
| 124 | {0x0001, TPM_2, "SwTPM 2.0" }, |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 125 | {0xffff} |
| 126 | }; |
| 127 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 128 | static const struct vendor_name vendor_names[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 129 | {0x1114, "Atmel", atmel_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 130 | {0x15d1, "Infineon", infineon_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 131 | {0x1050, "Nuvoton", nuvoton_devices}, |
Tsung Ho Wu | 804a043 | 2019-06-07 15:03:49 -0700 | [diff] [blame] | 132 | {0x1014, "TPM Emulator", swtpm_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 133 | {0x104a, "ST Microelectronics", stmicro_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * Cached vendor/device ID pair to indicate that the device has been already |
| 138 | * discovered |
| 139 | */ |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 140 | static u32 vendor_dev_id; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 141 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 142 | static inline u8 tpm_read_status(int locality) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 143 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 144 | u8 value = read8(TIS_REG(locality, TIS_REG_STS)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 145 | TPM_DEBUG_IO_READ(TIS_REG_STS, value); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 146 | return value; |
| 147 | } |
| 148 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 149 | static inline void tpm_write_status(u8 sts, int locality) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 150 | { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 151 | TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 152 | write8(TIS_REG(locality, TIS_REG_STS), sts); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 153 | } |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 154 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 155 | static inline u8 tpm_read_data(int locality) |
| 156 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 157 | u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 158 | TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value); |
| 159 | return value; |
| 160 | } |
| 161 | |
| 162 | static inline void tpm_write_data(u8 data, int locality) |
| 163 | { |
Werner Zeh | a31d6cd | 2021-10-07 07:15:38 +0200 | [diff] [blame] | 164 | TPM_DEBUG_IO_WRITE(TIS_REG_DATA_FIFO, data); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 165 | write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static inline u16 tpm_read_burst_count(int locality) |
| 169 | { |
| 170 | u16 count; |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 171 | count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT)); |
| 172 | count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8; |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 173 | TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count); |
| 174 | return count; |
| 175 | } |
| 176 | |
| 177 | static inline u8 tpm_read_access(int locality) |
| 178 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 179 | u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 180 | TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value); |
| 181 | return value; |
| 182 | } |
| 183 | |
| 184 | static inline void tpm_write_access(u8 data, int locality) |
| 185 | { |
| 186 | TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 187 | write8(TIS_REG(locality, TIS_REG_ACCESS), data); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 190 | static inline u32 tpm_read_intf_cap(int locality) |
| 191 | { |
| 192 | u32 value = read32(TIS_REG(locality, TIS_REG_INTF_CAPABILITY)); |
| 193 | TPM_DEBUG_IO_READ(TIS_REG_INTF_CAPABILITY, value); |
| 194 | return value; |
| 195 | } |
| 196 | |
| 197 | static inline u32 tpm_read_intf_id(int locality) |
| 198 | { |
| 199 | u32 value = read32(TIS_REG(locality, TIS_REG_INTF_ID)); |
| 200 | TPM_DEBUG_IO_READ(TIS_REG_INTF_ID, value); |
| 201 | return value; |
| 202 | } |
| 203 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 204 | static inline u32 tpm_read_did_vid(int locality) |
| 205 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 206 | u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID)); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 207 | TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value); |
| 208 | return value; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 211 | static inline void tpm_write_int_vector(int vector, int locality) |
| 212 | { |
| 213 | TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 214 | write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 217 | static inline u8 tpm_read_int_vector(int locality) |
| 218 | { |
| 219 | u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR)); |
| 220 | TPM_DEBUG_IO_READ(TIS_REG_INT_VECTOR, value); |
| 221 | return value; |
| 222 | } |
| 223 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 224 | static inline void tpm_write_int_polarity(int polarity, int locality) |
| 225 | { |
| 226 | /* Set polarity and leave all other bits at 0 */ |
| 227 | u32 value = (polarity & 0x3) << 3; |
| 228 | TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value); |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 229 | write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 232 | static inline u32 tpm_read_int_polarity(int locality) |
| 233 | { |
| 234 | /* Get polarity and leave all other bits */ |
| 235 | u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE)); |
| 236 | value = (value >> 3) & 0x3; |
| 237 | TPM_DEBUG_IO_READ(TIS_REG_INT_ENABLE, value); |
| 238 | return value; |
| 239 | } |
| 240 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 241 | /* |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 242 | * tis_wait_sts() |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 243 | * |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 244 | * Wait for at most a second for a status to change its state to match the |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 245 | * expected state. Normally the transition happens within microseconds. |
| 246 | * |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 247 | * @locality - locality |
| 248 | * @mask - bitmask for the bitfield(s) to watch |
| 249 | * @expected - value the field(s) are supposed to be set to |
| 250 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 251 | * Returns TPM_SUCCESS on success or TPM_CB_TIMEOUT on timeout. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 252 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 253 | static tpm_result_t tis_wait_sts(int locality, u8 mask, u8 expected) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 254 | { |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 255 | struct stopwatch sw; |
| 256 | |
| 257 | stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 258 | do { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 259 | u8 value = tpm_read_status(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 260 | if ((value & mask) == expected) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 261 | return TPM_SUCCESS; |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 262 | udelay(1); |
| 263 | } while (!stopwatch_expired(&sw)); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 264 | return TPM_CB_TIMEOUT; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 267 | static inline tpm_result_t tis_wait_ready(int locality) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 268 | { |
| 269 | return tis_wait_sts(locality, TIS_STS_COMMAND_READY, |
| 270 | TIS_STS_COMMAND_READY); |
| 271 | } |
| 272 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 273 | static inline tpm_result_t tis_wait_valid(int locality) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 274 | { |
| 275 | return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID); |
| 276 | } |
| 277 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 278 | static inline tpm_result_t tis_wait_valid_data(int locality) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 279 | { |
| 280 | const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 281 | return tis_wait_sts(locality, has_data, has_data); |
| 282 | } |
| 283 | |
| 284 | static inline int tis_has_valid_data(int locality) |
| 285 | { |
| 286 | const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 287 | return (tpm_read_status(locality) & has_data) == has_data; |
| 288 | } |
| 289 | |
| 290 | static inline int tis_expect_data(int locality) |
| 291 | { |
| 292 | return !!(tpm_read_status(locality) & TIS_STS_EXPECT); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * tis_wait_access() |
| 297 | * |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 298 | * Wait for at most a second for a access to change its state to match the |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 299 | * expected state. Normally the transition happens within microseconds. |
| 300 | * |
| 301 | * @locality - locality |
| 302 | * @mask - bitmask for the bitfield(s) to watch |
| 303 | * @expected - value the field(s) are supposed to be set to |
| 304 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 305 | * Returns TPM_SUCCESS on success or TPM_CB_TIMEOUT on timeout. |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 306 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 307 | static tpm_result_t tis_wait_access(int locality, u8 mask, u8 expected) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 308 | { |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 309 | struct stopwatch sw; |
| 310 | |
| 311 | stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 312 | do { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 313 | u8 value = tpm_read_access(locality); |
| 314 | if ((value & mask) == expected) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 315 | return TPM_SUCCESS; |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 316 | udelay(1); |
| 317 | } while (!stopwatch_expired(&sw)); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 318 | return TPM_CB_TIMEOUT; |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 319 | } |
| 320 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 321 | static inline tpm_result_t tis_wait_received_access(int locality) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 322 | { |
| 323 | return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, |
| 324 | TIS_ACCESS_ACTIVE_LOCALITY); |
| 325 | } |
| 326 | |
| 327 | static inline int tis_has_access(int locality) |
| 328 | { |
| 329 | return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY); |
| 330 | } |
| 331 | |
| 332 | static inline void tis_request_access(int locality) |
| 333 | { |
| 334 | tpm_write_access(TIS_ACCESS_REQUEST_USE, locality); |
| 335 | } |
| 336 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 337 | /* |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 338 | * PC Client Specific TPM Interface Specification section 11.2.12: |
| 339 | * |
| 340 | * Software must be prepared to send two writes of a "1" to command ready |
| 341 | * field: the first to indicate successful read of all the data, thus |
| 342 | * clearing the data from the ReadFIFO and freeing the TPM's resources, |
| 343 | * and the second to indicate to the TPM it is about to send a new command. |
| 344 | * |
| 345 | * In practice not all TPMs behave the same so it is necessary to be |
| 346 | * flexible when trying to set command ready. |
| 347 | * |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 348 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 349 | static tpm_result_t tis_command_ready(u8 locality) |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 350 | { |
| 351 | u32 status; |
| 352 | |
| 353 | /* 1st attempt to set command ready */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 354 | tpm_write_status(TIS_STS_COMMAND_READY, locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 355 | |
| 356 | /* Wait for response */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 357 | status = tpm_read_status(locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 358 | |
| 359 | /* Check if command ready is set yet */ |
| 360 | if (status & TIS_STS_COMMAND_READY) |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 361 | return TPM_SUCCESS; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 362 | |
| 363 | /* 2nd attempt to set command ready */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 364 | tpm_write_status(TIS_STS_COMMAND_READY, locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 365 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 366 | return tis_wait_ready(locality); |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 370 | * pc80_tis_probe() |
Jon Murphy | a7c64d7 | 2023-09-15 07:58:08 -0600 | [diff] [blame] | 371 | * |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 372 | * Probe the TPM device and try determining its manufacturer/device name. |
| 373 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 374 | * Returns TPM_SUCCESS on success (the device is found or was found during |
| 375 | * an earlier invocation) or TPM_CB_FAIL if the device is not found. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 376 | */ |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 377 | static tpm_result_t pc80_tis_probe(enum tpm_family *family) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 378 | { |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 379 | static enum tpm_family tpm_family; |
| 380 | |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 381 | const char *device_name = NULL; |
| 382 | const char *vendor_name = NULL; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 383 | const struct device_name *dev; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 384 | u32 didvid, intf_id; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 385 | u16 vid, did; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 386 | u8 locality = 0, intf_type; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 387 | int i; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 388 | const char *family_str; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 389 | |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 390 | if (vendor_dev_id) { |
| 391 | if (family != NULL) |
| 392 | *family = tpm_family; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 393 | return TPM_SUCCESS; /* Already probed. */ |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 394 | } |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 395 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 396 | didvid = tpm_read_did_vid(0); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 397 | if (!didvid || (didvid == 0xffffffff)) { |
Angel Pons | 08e8cab | 2020-06-18 15:20:37 +0200 | [diff] [blame] | 398 | printf("%s: No TPM device found\n", __func__); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 399 | return TPM_CB_FAIL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 402 | intf_id = tpm_read_intf_id(locality); |
| 403 | intf_type = (intf_id & 0xf); |
| 404 | if (intf_type == 0xf) { |
| 405 | u32 intf_cap = tpm_read_intf_cap(locality); |
| 406 | u8 intf_version = (intf_cap >> 28) & 0x7; |
| 407 | switch (intf_version) { |
| 408 | case 0: |
| 409 | case 2: |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 410 | tpm_family = TPM_1; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 411 | break; |
| 412 | case 3: |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 413 | tpm_family = TPM_2; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | printf("%s: Unexpected TPM interface version: %d\n", __func__, |
| 417 | intf_version); |
| 418 | return TPM_CB_PROBE_FAILURE; |
| 419 | } |
| 420 | } else if (intf_type == 0) { |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 421 | tpm_family = TPM_2; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 422 | } else { |
| 423 | printf("%s: Unexpected TPM interface type: %d\n", __func__, intf_type); |
| 424 | return TPM_CB_PROBE_FAILURE; |
| 425 | } |
| 426 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 427 | vendor_dev_id = didvid; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 428 | |
| 429 | vid = didvid & 0xffff; |
| 430 | did = (didvid >> 16) & 0xffff; |
| 431 | for (i = 0; i < ARRAY_SIZE(vendor_names); i++) { |
| 432 | int j = 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 433 | if (vid == vendor_names[i].vendor_id) { |
| 434 | vendor_name = vendor_names[i].vendor_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 435 | } else { |
| 436 | continue; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 437 | } |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 438 | dev = &vendor_names[i].dev_names[j]; |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 439 | while (dev->dev_id != 0xffff) { |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 440 | if (dev->dev_id == did && dev->family == tpm_family) { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame] | 441 | device_name = dev->dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | j++; |
Subrata | 41b08d9 | 2015-05-14 14:38:07 +0530 | [diff] [blame] | 445 | dev = &vendor_names[i].dev_names[j]; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 446 | } |
| 447 | break; |
| 448 | } |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 449 | |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 450 | family_str = (tpm_family == TPM_1 ? "TPM 1.2" : "TPM 2.0"); |
Sergii Dmytruk | 7608699 | 2022-10-30 17:19:46 +0200 | [diff] [blame] | 451 | if (vendor_name == NULL) { |
| 452 | printk(BIOS_INFO, "Found %s 0x%04x by 0x%04x\n", family_str, did, vid); |
| 453 | } else if (device_name == NULL) { |
| 454 | printk(BIOS_INFO, "Found %s 0x%04x by %s (0x%04x)\n", family_str, did, |
| 455 | vendor_name, vid); |
| 456 | } else { |
| 457 | printk(BIOS_INFO, "Found %s %s (0x%04x) by %s (0x%04x)\n", family_str, |
| 458 | device_name, did, vendor_name, vid); |
| 459 | } |
| 460 | |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 461 | if (family != NULL) |
| 462 | *family = tpm_family; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 463 | return TPM_SUCCESS; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* |
| 467 | * tis_senddata() |
| 468 | * |
| 469 | * send the passed in data to the TPM device. |
| 470 | * |
| 471 | * @data - address of the data to send, byte by byte |
| 472 | * @len - length of the data to send |
| 473 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 474 | * Returns TPM_SUCCESS on success, TPM_CB_FAIL on error (in case the device does |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 475 | * not accept the entire command). |
| 476 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 477 | static tpm_result_t tis_senddata(const u8 *const data, u32 len) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 478 | { |
| 479 | u32 offset = 0; |
| 480 | u16 burst = 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 481 | u8 locality = 0; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 482 | tpm_result_t rc = TPM_SUCCESS; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 483 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 484 | rc = tis_wait_ready(locality); |
| 485 | if (rc) { |
| 486 | printf("%s:%d - failed to get 'command_ready' status with error %#x\n", |
| 487 | __FILE__, __LINE__, rc); |
| 488 | return rc; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 489 | } |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 490 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 491 | |
| 492 | while (1) { |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 493 | unsigned int count; |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 494 | struct stopwatch sw; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 495 | |
| 496 | /* Wait till the device is ready to accept more data. */ |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 497 | stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 498 | while (!burst) { |
Werner Zeh | 92ab611 | 2021-10-11 15:27:12 +0200 | [diff] [blame] | 499 | if (stopwatch_expired(&sw)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame] | 500 | printf("%s:%d failed to feed %u bytes of %u\n", |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 501 | __FILE__, __LINE__, len - offset, len); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 502 | return TPM_CB_TIMEOUT; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 503 | } |
| 504 | udelay(1); |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 505 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 508 | /* |
| 509 | * Calculate number of bytes the TPM is ready to accept in one |
| 510 | * shot. |
| 511 | * |
| 512 | * We want to send the last byte outside of the loop (hence |
| 513 | * the -1 below) to make sure that the 'expected' status bit |
| 514 | * changes to zero exactly after the last byte is fed into the |
| 515 | * FIFO. |
| 516 | */ |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 517 | count = MIN(burst, len - offset - 1); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 518 | while (count--) |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 519 | tpm_write_data(data[offset++], locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 520 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 521 | rc = tis_wait_valid(locality); |
| 522 | if (rc || !tis_expect_data(locality)) { |
| 523 | printf("%s:%d TPM command feed overflow with error %#x\n", |
| 524 | __FILE__, __LINE__, rc); |
| 525 | return rc ? rc : TPM_CB_FAIL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 528 | burst = tpm_read_burst_count(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 529 | if ((offset == (len - 1)) && burst) |
| 530 | /* |
| 531 | * We need to be able to send the last byte to the |
| 532 | * device, so burst size must be nonzero before we |
| 533 | * break out. |
| 534 | */ |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | /* Send the last byte. */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 539 | tpm_write_data(data[offset++], locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 540 | |
| 541 | /* |
| 542 | * Verify that TPM does not expect any more data as part of this |
| 543 | * command. |
| 544 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 545 | rc = tis_wait_valid(locality); |
| 546 | if (rc || tis_expect_data(locality)) { |
| 547 | printf("%s:%d unexpected TPM error %#x with status %#x\n", |
| 548 | __FILE__, __LINE__, rc, tpm_read_status(locality)); |
| 549 | return rc ? rc : TPM_CB_FAIL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* OK, sitting pretty, let's start the command execution. */ |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 553 | tpm_write_status(TIS_STS_TPM_GO, locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 554 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 555 | return TPM_SUCCESS; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
| 559 | * tis_readresponse() |
| 560 | * |
| 561 | * read the TPM device response after a command was issued. |
| 562 | * |
| 563 | * @buffer - address where to read the response, byte by byte. |
| 564 | * @len - pointer to the size of buffer |
| 565 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 566 | * On success stores the number of received bytes to len and returns |
| 567 | * TPM_SUCCESS. On errors (misformatted TPM data or synchronization |
| 568 | * problems) returns TPM_CB_FAIL. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 569 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 570 | static tpm_result_t tis_readresponse(u8 *buffer, size_t *len) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 571 | { |
| 572 | u16 burst_count; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 573 | u32 offset = 0; |
| 574 | u8 locality = 0; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 575 | u32 expected_count = *len; |
| 576 | int max_cycles = 0; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 577 | tpm_result_t rc = TPM_SUCCESS; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 578 | |
| 579 | /* Wait for the TPM to process the command */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 580 | rc = tis_wait_valid_data(locality); |
| 581 | if (rc) { |
| 582 | printf("%s:%d failed processing command with error %#x\n", |
| 583 | __FILE__, __LINE__, rc); |
| 584 | return rc; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | do { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 588 | while ((burst_count = tpm_read_burst_count(locality)) == 0) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 589 | if (max_cycles++ == MAX_DELAY_US) { |
| 590 | printf("%s:%d TPM stuck on read\n", |
| 591 | __FILE__, __LINE__); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 592 | return TPM_CB_FAIL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 593 | } |
| 594 | udelay(1); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | max_cycles = 0; |
| 598 | |
| 599 | while (burst_count-- && (offset < expected_count)) { |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 600 | buffer[offset++] = tpm_read_data(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 601 | if (offset == 6) { |
| 602 | /* |
| 603 | * We got the first six bytes of the reply, |
| 604 | * let's figure out how many bytes to expect |
| 605 | * total - it is stored as a 4 byte number in |
| 606 | * network order, starting with offset 2 into |
| 607 | * the body of the reply. |
| 608 | */ |
| 609 | u32 real_length; |
| 610 | memcpy(&real_length, |
| 611 | buffer + 2, |
| 612 | sizeof(real_length)); |
| 613 | expected_count = be32_to_cpu(real_length); |
| 614 | |
| 615 | if ((expected_count < offset) || |
| 616 | (expected_count > *len)) { |
Werner Zeh | 66b2f20 | 2021-10-12 15:14:22 +0200 | [diff] [blame] | 617 | printf("%s:%d bad response size %u\n", |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 618 | __FILE__, __LINE__, |
| 619 | expected_count); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 620 | return TPM_CB_FAIL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /* Wait for the next portion */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 626 | rc = tis_wait_valid(locality); |
| 627 | if (rc) { |
| 628 | printf("%s:%d failed to read response with error %#x\n", |
| 629 | __FILE__, __LINE__, rc); |
| 630 | return rc; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | if (offset == expected_count) |
| 634 | break; /* We got all we need */ |
| 635 | |
Bill XIE | a4bf0b7 | 2018-03-22 17:07:43 +0800 | [diff] [blame] | 636 | /* |
| 637 | * Certain TPMs seem to need some delay between tis_wait_valid() |
| 638 | * and tis_has_valid_data(), or some race-condition-related |
| 639 | * issue will occur. |
| 640 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 641 | if (CONFIG(TPM_RDRESP_NEED_DELAY)) |
Bill XIE | a4bf0b7 | 2018-03-22 17:07:43 +0800 | [diff] [blame] | 642 | udelay(10); |
| 643 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 644 | } while (tis_has_valid_data(locality)); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 645 | |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 646 | /* * Make sure we indeed read all there was. */ |
| 647 | if (tis_has_valid_data(locality)) { |
Jon Murphy | 53fc667 | 2023-09-26 21:05:37 -0600 | [diff] [blame] | 648 | printf("%s:%d wrong receive status: %#x %u bytes left\n", |
Aaron Durbin | c422002 | 2013-07-24 16:02:14 -0500 | [diff] [blame] | 649 | __FILE__, __LINE__, tpm_read_status(locality), |
| 650 | tpm_read_burst_count(locality)); |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 651 | return TPM_CB_FAIL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* Tell the TPM that we are done. */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 655 | rc = tis_command_ready(locality); |
| 656 | if (rc) |
| 657 | return rc; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 658 | |
| 659 | *len = offset; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 660 | return TPM_SUCCESS; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 664 | * pc80_tis_open() |
Sergii Dmytruk | 4ee0317 | 2022-12-22 19:35:25 +0200 | [diff] [blame] | 665 | * |
Sergii Dmytruk | 0a89d52 | 2022-10-29 16:57:07 +0300 | [diff] [blame] | 666 | * Requests access to locality 0 for the caller. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 667 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 668 | * Returns TPM_SUCCESS on success, TSS Error on failure. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 669 | */ |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 670 | static tpm_result_t pc80_tis_open(void) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 671 | { |
| 672 | u8 locality = 0; /* we use locality zero for everything */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 673 | tpm_result_t rc = TPM_SUCCESS; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 674 | |
Sergii Dmytruk | 0a89d52 | 2022-10-29 16:57:07 +0300 | [diff] [blame] | 675 | if (!tis_has_access(locality)) { |
| 676 | /* request access to locality */ |
| 677 | tis_request_access(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 678 | |
Sergii Dmytruk | 0a89d52 | 2022-10-29 16:57:07 +0300 | [diff] [blame] | 679 | /* did we get a lock? */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 680 | rc = tis_wait_received_access(locality); |
| 681 | if (rc) { |
| 682 | printf("%s:%d - failed to lock locality %u with error %#x\n", |
| 683 | __FILE__, __LINE__, locality, rc); |
| 684 | return rc; |
Sergii Dmytruk | 0a89d52 | 2022-10-29 16:57:07 +0300 | [diff] [blame] | 685 | } |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 686 | |
Sergii Dmytruk | 0a89d52 | 2022-10-29 16:57:07 +0300 | [diff] [blame] | 687 | /* Certain TPMs seem to need some delay here or they hang... */ |
| 688 | udelay(10); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 691 | return tis_command_ready(locality); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
Sergii Dmytruk | 4ee0317 | 2022-12-22 19:35:25 +0200 | [diff] [blame] | 695 | * tis_sendrecv() |
| 696 | * |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 697 | * Send the requested data to the TPM and then try to get its response |
| 698 | * |
| 699 | * @sendbuf - buffer of the data to send |
| 700 | * @send_size size of the data to send |
| 701 | * @recvbuf - memory to save the response to |
| 702 | * @recv_len - pointer to the size of the response buffer |
| 703 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 704 | * Returns TPM_SUCCESS on success (and places the number of response bytes |
| 705 | * at recv_len) or TPM_CB_FAIL on failure. |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 706 | */ |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 707 | static tpm_result_t pc80_tpm_sendrecv(const uint8_t *sendbuf, size_t send_size, |
| 708 | uint8_t *recvbuf, size_t *recv_len) |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 709 | { |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 710 | tpm_result_t rc = tis_senddata(sendbuf, send_size); |
| 711 | if (rc) { |
| 712 | printf("%s:%d failed sending data to TPM with error %#x\n", |
| 713 | __FILE__, __LINE__, rc); |
| 714 | return rc; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | return tis_readresponse(recvbuf, recv_len); |
| 718 | } |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 719 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 720 | /* |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 721 | * tis_probe() |
| 722 | * |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 723 | * Probe for the TPM device and set it up for use within locality 0. |
| 724 | * |
| 725 | * @tpm_family - pointer to tpm_family which is set to TPM family of the device. |
| 726 | * |
| 727 | * Returns pointer to send-receive function on success or NULL on failure. |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 728 | */ |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 729 | tis_sendrecv_fn tis_probe(enum tpm_family *family) |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 730 | { |
Sergii Dmytruk | febf9b9 | 2022-10-31 15:30:15 +0200 | [diff] [blame^] | 731 | if (pc80_tis_probe(family)) |
Sergii Dmytruk | 963f7b9 | 2022-10-29 20:42:28 +0300 | [diff] [blame] | 732 | return NULL; |
| 733 | |
| 734 | if (pc80_tis_open()) |
| 735 | return NULL; |
| 736 | |
| 737 | return &pc80_tpm_sendrecv; |
| 738 | } |
| 739 | |
| 740 | /* |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 741 | * tis_setup_interrupt() |
| 742 | * |
| 743 | * Set up the interrupt vector and polarity for locality 0 and |
| 744 | * disable all interrupts so they are unused in firmware but can |
| 745 | * be enabled by the OS. |
| 746 | * |
| 747 | * The values used here must match what is passed in the TPM ACPI |
| 748 | * device if ACPI is used on the platform. |
| 749 | * |
| 750 | * @vector - TPM interrupt vector |
| 751 | * @polarity - TPM interrupt polarity |
| 752 | * |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 753 | * Returns TPM_SUCCESS on success, TPM_CB_FAIL on failure. |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 754 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 755 | static tpm_result_t tis_setup_interrupt(int vector, int polarity) |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 756 | { |
| 757 | u8 locality = 0; |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 758 | tpm_result_t rc = tlcl_lib_init(); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 759 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 760 | if (rc) |
| 761 | return rc; |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 762 | |
| 763 | /* Set TPM interrupt vector */ |
| 764 | tpm_write_int_vector(vector, locality); |
| 765 | |
Elyes HAOUAS | 1895838 | 2018-08-07 12:23:16 +0200 | [diff] [blame] | 766 | /* Set TPM interrupt polarity and disable interrupts */ |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 767 | tpm_write_int_polarity(polarity, locality); |
| 768 | |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 769 | return TPM_SUCCESS; |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | static void lpc_tpm_read_resources(struct device *dev) |
| 773 | { |
| 774 | /* Static 5K memory region specified in Kconfig */ |
Arthur Heymans | e05693e | 2023-07-05 11:43:09 +0200 | [diff] [blame] | 775 | mmio_range(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void lpc_tpm_set_resources(struct device *dev) |
| 779 | { |
| 780 | tpm_config_t *config = (tpm_config_t *)dev->chip_info; |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 781 | DEVTREE_CONST struct resource *res; |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 782 | |
| 783 | for (res = dev->resource_list; res; res = res->next) { |
| 784 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 785 | continue; |
| 786 | |
| 787 | if (res->flags & IORESOURCE_IRQ) { |
| 788 | /* Set interrupt vector */ |
| 789 | tis_setup_interrupt((int)res->base, |
| 790 | config->irq_polarity); |
| 791 | } else { |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 792 | continue; |
| 793 | } |
| 794 | |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 795 | #if !DEVTREE_EARLY |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 796 | res->flags |= IORESOURCE_STORED; |
| 797 | report_resource_stored(dev, res, " <tpm>"); |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 798 | #endif |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 802 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 803 | static void lpc_tpm_fill_ssdt(const struct device *dev) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 804 | { |
Michał Kopeć | a691cbd | 2022-02-22 12:30:22 +0100 | [diff] [blame] | 805 | /* Windows 11 requires the following path for TPM to be detected */ |
| 806 | const char *path = "\\_SB_.PCI0"; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 807 | |
| 808 | /* Device */ |
| 809 | acpigen_write_scope(path); |
| 810 | acpigen_write_device(acpi_device_name(dev)); |
| 811 | |
Michał Żygowski | 7b28801 | 2020-03-20 15:41:44 +0100 | [diff] [blame] | 812 | if (CONFIG(TPM2)) { |
| 813 | acpigen_write_name_string("_HID", "MSFT0101"); |
| 814 | acpigen_write_name_string("_CID", "MSFT0101"); |
| 815 | } else { |
| 816 | acpigen_write_name("_HID"); |
| 817 | acpigen_emit_eisaid("PNP0C31"); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 818 | |
Michał Żygowski | 7b28801 | 2020-03-20 15:41:44 +0100 | [diff] [blame] | 819 | acpigen_write_name("_CID"); |
| 820 | acpigen_emit_eisaid("PNP0C31"); |
| 821 | } |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 822 | |
Patrick Rudolph | c83bab6 | 2019-12-13 12:16:06 +0100 | [diff] [blame] | 823 | acpi_device_write_uid(dev); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 824 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 825 | u32 did_vid = tpm_read_did_vid(0); |
| 826 | if (did_vid > 0 && did_vid < 0xffffffff) |
| 827 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 828 | else |
| 829 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF); |
| 830 | |
Kevin Cody-Little | c97b5af | 2018-05-09 14:14:59 -0400 | [diff] [blame] | 831 | u16 port = dev->path.pnp.port; |
| 832 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 833 | /* Resources */ |
| 834 | acpigen_write_name("_CRS"); |
| 835 | acpigen_write_resourcetemplate_header(); |
| 836 | acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
Frans Hendriks | 6cc937e | 2018-10-29 14:30:58 +0100 | [diff] [blame] | 837 | if (port) |
| 838 | acpigen_write_io16(port, port, 1, 2, 1); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 839 | |
| 840 | if (CONFIG_TPM_PIRQ) { |
| 841 | /* |
| 842 | * PIRQ: Update interrupt vector with configured PIRQ |
| 843 | * Active-Low Level-Triggered Shared |
| 844 | */ |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 845 | struct acpi_irq tpm_irq_a = ACPI_IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 846 | acpi_device_write_interrupt(&tpm_irq_a); |
| 847 | } else if (tpm_read_int_vector(0) > 0) { |
| 848 | u8 int_vec = tpm_read_int_vector(0); |
| 849 | u8 int_pol = tpm_read_int_polarity(0); |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 850 | struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 851 | |
| 852 | if (int_pol & 1) |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 853 | tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 854 | else |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 855 | tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 856 | |
| 857 | if (int_pol & 2) |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 858 | tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 859 | else |
Furquan Shaikh | 5b9b593 | 2017-02-21 13:16:30 -0800 | [diff] [blame] | 860 | tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED; |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 861 | |
| 862 | acpi_device_write_interrupt(&tpm_irq); |
| 863 | } |
| 864 | |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 865 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 866 | acpigen_write_resourcetemplate_footer(); |
| 867 | |
Patrick Rudolph | d8d8be1 | 2020-09-21 09:48:53 +0200 | [diff] [blame] | 868 | if (!CONFIG(CHROMEOS)) |
| 869 | tpm_ppi_acpi_fill_ssdt(dev); |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 870 | |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 871 | acpigen_pop_len(); /* Device */ |
| 872 | acpigen_pop_len(); /* Scope */ |
| 873 | |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 874 | #if !DEVTREE_EARLY |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 875 | printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev), |
| 876 | dev->chip_ops->name, dev_path(dev)); |
Kyösti Mälkki | baa16e9 | 2019-11-05 18:38:00 +0200 | [diff] [blame] | 877 | #endif |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 878 | } |
| 879 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 880 | static const char *lpc_tpm_acpi_name(const struct device *dev) |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 881 | { |
| 882 | return "TPM"; |
| 883 | } |
| 884 | #endif |
| 885 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 886 | static struct device_operations lpc_tpm_ops = { |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 887 | .read_resources = lpc_tpm_read_resources, |
| 888 | .set_resources = lpc_tpm_set_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 889 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 890 | .acpi_name = lpc_tpm_acpi_name, |
| 891 | .acpi_fill_ssdt = lpc_tpm_fill_ssdt, |
Naresh G Solanki | 80ff038 | 2016-11-15 11:01:33 +0530 | [diff] [blame] | 892 | #endif |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 893 | }; |
| 894 | |
Arthur Heymans | 04c49a5 | 2023-08-22 13:16:40 +0200 | [diff] [blame] | 895 | static struct device_operations noop_tpm_ops = { |
| 896 | .read_resources = noop_read_resources, |
| 897 | .set_resources = noop_set_resources, |
| 898 | }; |
| 899 | |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 900 | static struct pnp_info pnp_dev_info[] = { |
| 901 | { .flags = PNP_IRQ0 } |
| 902 | }; |
| 903 | |
| 904 | static void enable_dev(struct device *dev) |
| 905 | { |
Kyösti Mälkki | d2b2a18 | 2021-04-29 15:33:07 +0300 | [diff] [blame] | 906 | if (CONFIG(TPM)) |
Kyösti Mälkki | 9cc6493 | 2020-05-29 19:42:07 +0300 | [diff] [blame] | 907 | pnp_enable_devices(dev, &lpc_tpm_ops, |
| 908 | ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
Arthur Heymans | 04c49a5 | 2023-08-22 13:16:40 +0200 | [diff] [blame] | 909 | else |
| 910 | pnp_enable_devices(dev, &noop_tpm_ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | struct chip_operations drivers_pc80_tpm_ops = { |
Nicholas Sudsgaard | bfb11be | 2024-01-30 09:53:46 +0900 | [diff] [blame] | 914 | .name = "LPC TPM", |
Duncan Laurie | dd281ed | 2014-10-30 15:20:19 -0700 | [diff] [blame] | 915 | .enable_dev = enable_dev |
| 916 | }; |