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