Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 2 | |
Martin Roth | cddd600 | 2019-09-23 17:38:27 -0600 | [diff] [blame] | 3 | /* Based on Linux Kernel TPM driver */ |
| 4 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 5 | /* |
Martin Roth | 0949e73 | 2021-10-01 14:28:22 -0600 | [diff] [blame] | 6 | * cr50 is a TPM 2.0 capable device that requires special |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 7 | * handling for the I2C interface. |
| 8 | * |
| 9 | * - Use an interrupt for transaction status instead of hardcoded delays |
| 10 | * - Must use write+wait+read read protocol |
| 11 | * - All 4 bytes of status register must be read/written at once |
| 12 | * - Burst count max is 63 bytes, and burst count behaves |
| 13 | * slightly differently than other I2C TPMs |
| 14 | * - When reading from FIFO the full burstcnt must be read |
| 15 | * instead of just reading header and determining the remainder |
| 16 | */ |
| 17 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 18 | #include <commonlib/endian.h> |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 19 | #include <commonlib/helpers.h> |
Tim Wawrzynczak | 1e50dfb | 2022-02-16 13:48:07 -0700 | [diff] [blame] | 20 | #include <console/console.h> |
| 21 | #include <delay.h> |
| 22 | #include <device/i2c_simple.h> |
| 23 | #include <drivers/tpm/cr50.h> |
| 24 | #include <endian.h> |
| 25 | #include <security/tpm/tis.h> |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <types.h> |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 28 | #include <timer.h> |
Elyes HAOUAS | ede8dd0 | 2019-06-23 06:57:53 +0200 | [diff] [blame] | 29 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 30 | #include "tpm.h" |
| 31 | |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 32 | #define CR50_MAX_BUFSIZE 63 |
Duncan Laurie | 469af7b | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 33 | #define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */ |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 34 | #define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */ |
| 35 | #define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */ |
Duncan Laurie | ed4fa09 | 2016-11-01 15:03:13 -0700 | [diff] [blame] | 36 | #define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */ |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 37 | #define CR50_DID_VID 0x00281ae0L |
Jes Klinke | 1430b04 | 2022-03-28 14:22:24 -0700 | [diff] [blame] | 38 | #define TI50_DID_VID 0x504a6666L |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 39 | |
| 40 | struct tpm_inf_dev { |
| 41 | int bus; |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 42 | int locality; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 43 | unsigned int addr; |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 44 | uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)]; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 47 | static struct tpm_inf_dev tpm_dev; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 48 | |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 49 | __weak int tis_plat_irq_status(void) |
Daniel Kurtz | c4852e7 | 2017-04-21 14:11:51 +0800 | [diff] [blame] | 50 | { |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 51 | static int warning_displayed; |
Daniel Kurtz | c4852e7 | 2017-04-21 14:11:51 +0800 | [diff] [blame] | 52 | |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 53 | if (!warning_displayed) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 54 | printk(BIOS_WARNING, "%s() not implemented, wasting 20ms to wait on" |
Elyes HAOUAS | 5201665 | 2021-01-16 17:29:49 +0100 | [diff] [blame] | 55 | " Cr50!\n", __func__); |
Arthur Heymans | 0ca944b | 2019-11-20 19:51:06 +0100 | [diff] [blame] | 56 | warning_displayed = 1; |
Daniel Kurtz | c4852e7 | 2017-04-21 14:11:51 +0800 | [diff] [blame] | 57 | } |
| 58 | mdelay(CR50_TIMEOUT_NOIRQ_MS); |
| 59 | |
| 60 | return 1; |
| 61 | } |
| 62 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 63 | /* |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 64 | * cr50_i2c_read() - read from TPM register |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 65 | * |
| 66 | * @addr: register address to read from |
| 67 | * @buffer: provided by caller |
| 68 | * @len: number of bytes to read |
| 69 | * |
| 70 | * 1) send register address byte 'addr' to the TPM |
| 71 | * 2) wait for TPM to indicate it is ready |
| 72 | * 3) read 'len' bytes of TPM response into the provided 'buffer' |
| 73 | * |
| 74 | * Return -1 on error, 0 on success. |
| 75 | */ |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 76 | static int cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 77 | { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 78 | if (tpm_dev.addr == 0) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 79 | return -1; |
| 80 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 81 | /* Clear interrupt before starting transaction */ |
Daniel Kurtz | c4852e7 | 2017-04-21 14:11:51 +0800 | [diff] [blame] | 82 | tis_plat_irq_status(); |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 83 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 84 | /* Send the register address byte to the TPM */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 85 | if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 86 | printk(BIOS_ERR, "%s: Address write failed\n", __func__); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | /* Wait for TPM to be ready with response data */ |
Yu-Ping Wu | ae1e702 | 2022-05-17 09:33:18 +0800 | [diff] [blame] | 91 | if (cr50_wait_tpm_ready() != CB_SUCCESS) |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 92 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 93 | |
| 94 | /* Read response data from the TPM */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 95 | if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 96 | printk(BIOS_ERR, "%s: Read response failed\n", __func__); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 104 | * cr50_i2c_write() - write to TPM register |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 105 | * |
| 106 | * @addr: register address to write to |
| 107 | * @buffer: data to write |
| 108 | * @len: number of bytes to write |
| 109 | * |
| 110 | * 1) prepend the provided address to the provided data |
| 111 | * 2) send the address+data to the TPM |
| 112 | * 3) wait for TPM to indicate it is done writing |
| 113 | * |
| 114 | * Returns -1 on error, 0 on success. |
| 115 | */ |
Tim Wawrzynczak | 1e50dfb | 2022-02-16 13:48:07 -0700 | [diff] [blame] | 116 | static int cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 117 | { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 118 | if (tpm_dev.addr == 0) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 119 | return -1; |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 120 | if (len > CR50_MAX_BUFSIZE) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 121 | return -1; |
| 122 | |
| 123 | /* Prepend the 'register address' to the buffer */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 124 | tpm_dev.buf[0] = addr; |
| 125 | memcpy(tpm_dev.buf + 1, buffer, len); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 126 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 127 | /* Clear interrupt before starting transaction */ |
Daniel Kurtz | c4852e7 | 2017-04-21 14:11:51 +0800 | [diff] [blame] | 128 | tis_plat_irq_status(); |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 129 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 130 | /* Send write request buffer with address */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 131 | if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 132 | printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | /* Wait for TPM to be ready */ |
Yu-Ping Wu | ae1e702 | 2022-05-17 09:33:18 +0800 | [diff] [blame] | 137 | return cr50_wait_tpm_ready() == CB_SUCCESS ? 0 : -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 140 | /* |
| 141 | * Cr50 processes reset requests asynchronously and consceivably could be busy |
| 142 | * executing a long command and not reacting to the reset pulse for a while. |
| 143 | * |
| 144 | * This function will make sure that the AP does not proceed with boot until |
| 145 | * TPM finished reset processing. |
| 146 | */ |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 147 | static int process_reset(void) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 148 | { |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 149 | struct stopwatch sw; |
Richard Spiegel | 7c1e959 | 2018-08-09 14:41:17 -0700 | [diff] [blame] | 150 | int rv = 0; |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 151 | uint8_t access; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 152 | |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 153 | /* |
| 154 | * Locality is released by TPM reset. |
| 155 | * |
| 156 | * If locality is taken at this point, this could be due to the fact |
| 157 | * that the TPM is performing a long operation and has not processed |
| 158 | * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if |
| 159 | * it releases locality when reset is processed. |
| 160 | */ |
| 161 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS); |
| 162 | do { |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 163 | const uint8_t mask = |
| 164 | TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 165 | |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 166 | rv = cr50_i2c_read(TPM_ACCESS(0), |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 167 | &access, sizeof(access)); |
| 168 | if (rv || ((access & mask) == mask)) { |
| 169 | /* |
| 170 | * Don't bombard the chip with traffic, let it keep |
| 171 | * processing the command. |
| 172 | */ |
| 173 | mdelay(2); |
| 174 | continue; |
| 175 | } |
| 176 | |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 177 | printk(BIOS_INFO, "TPM ready after %lld ms\n", |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 178 | stopwatch_duration_msecs(&sw)); |
| 179 | |
| 180 | return 0; |
| 181 | } while (!stopwatch_expired(&sw)); |
| 182 | |
Richard Spiegel | 7c1e959 | 2018-08-09 14:41:17 -0700 | [diff] [blame] | 183 | if (rv) |
| 184 | printk(BIOS_ERR, "Failed to read TPM\n"); |
| 185 | else |
| 186 | printk(BIOS_ERR, |
Rob Barnes | d522f38 | 2022-09-12 06:31:47 -0600 | [diff] [blame] | 187 | "TPM failed to reset after %lld ms, status: %#x\n", |
Richard Spiegel | 7c1e959 | 2018-08-09 14:41:17 -0700 | [diff] [blame] | 188 | stopwatch_duration_msecs(&sw), access); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 189 | |
| 190 | return -1; |
| 191 | } |
| 192 | |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 193 | /* |
| 194 | * Locality could be already claimed (if this is a later coreboot stage and |
| 195 | * the RO did not release it), or not yet claimed, if this is verstage or the |
| 196 | * older RO did release it. |
| 197 | */ |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 198 | static int claim_locality(void) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 199 | { |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 200 | uint8_t access; |
| 201 | const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY; |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 202 | |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 203 | if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access))) |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 204 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 205 | |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 206 | if ((access & mask) == mask) { |
| 207 | printk(BIOS_INFO, "Locality already claimed\n"); |
| 208 | return 0; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 209 | } |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 210 | |
| 211 | access = TPM_ACCESS_REQUEST_USE; |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 212 | if (cr50_i2c_write(TPM_ACCESS(0), |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 213 | &access, sizeof(access))) |
| 214 | return -1; |
| 215 | |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 216 | if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access))) |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 217 | return -1; |
| 218 | |
| 219 | if ((access & mask) != mask) { |
| 220 | printk(BIOS_INFO, "Failed to claim locality.\n"); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | return 0; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* cr50 requires all 4 bytes of status register to be read */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 228 | static uint8_t cr50_i2c_tis_status(void) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 229 | { |
| 230 | uint8_t buf[4]; |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 231 | if (cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 232 | printk(BIOS_ERR, "%s: Failed to read status\n", __func__); |
| 233 | return 0; |
| 234 | } |
| 235 | return buf[0]; |
| 236 | } |
| 237 | |
| 238 | /* cr50 requires all 4 bytes of status register to be written */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 239 | static void cr50_i2c_tis_ready(void) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 240 | { |
| 241 | uint8_t buf[4] = { TPM_STS_COMMAND_READY }; |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 242 | cr50_i2c_write(TPM_STS(tpm_dev.locality), buf, sizeof(buf)); |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 243 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* cr50 uses bytes 3:2 of status register for burst count and |
| 247 | * all 4 bytes must be read */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 248 | static int cr50_i2c_wait_burststs(uint8_t mask, size_t *burst, int *status) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 249 | { |
| 250 | uint8_t buf[4]; |
| 251 | struct stopwatch sw; |
| 252 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 253 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 254 | |
| 255 | while (!stopwatch_expired(&sw)) { |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 256 | if (cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)) != 0) { |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 257 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | |
| 261 | *status = buf[0]; |
| 262 | *burst = read_le16(&buf[1]); |
| 263 | |
| 264 | /* Check if mask matches and burst is valid */ |
| 265 | if ((*status & mask) == mask && |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 266 | *burst > 0 && *burst <= CR50_MAX_BUFSIZE) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 269 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__); |
| 273 | return -1; |
| 274 | } |
| 275 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 276 | static int cr50_i2c_tis_recv(uint8_t *buf, size_t buf_len) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 277 | { |
| 278 | size_t burstcnt, current, len, expected; |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 279 | uint8_t addr = TPM_DATA_FIFO(tpm_dev.locality); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 280 | uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 281 | int status; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 282 | |
| 283 | if (buf_len < TPM_HEADER_SIZE) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 284 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 285 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 286 | if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 287 | printk(BIOS_ERR, "%s: First chunk not available\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 288 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Read first chunk of burstcnt bytes */ |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 292 | if (cr50_i2c_read(addr, buf, burstcnt) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 293 | printk(BIOS_ERR, "%s: Read failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 294 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Determine expected data in the return buffer */ |
| 298 | expected = read_be32(buf + TPM_RSP_SIZE_BYTE); |
| 299 | if (expected > buf_len) { |
| 300 | printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n", |
| 301 | __func__, expected, buf_len); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 302 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* Now read the rest of the data */ |
| 306 | current = burstcnt; |
| 307 | while (current < expected) { |
| 308 | /* Read updated burst count and check status */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 309 | if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 310 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 311 | |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 312 | len = MIN(burstcnt, expected - current); |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 313 | if (cr50_i2c_read(addr, buf + current, len) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 314 | printk(BIOS_ERR, "%s: Read failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 315 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | current += len; |
| 319 | } |
| 320 | |
| 321 | /* Ensure TPM is done reading data */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 322 | if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status) < 0) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 323 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 324 | if (status & TPM_STS_DATA_AVAIL) { |
| 325 | printk(BIOS_ERR, "%s: Data still available\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 326 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 329 | return current; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 330 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 331 | out_err: |
| 332 | /* Abort current transaction if still pending */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 333 | if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY) |
| 334 | cr50_i2c_tis_ready(); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 335 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 338 | static int cr50_i2c_tis_send(uint8_t *buf, size_t len) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 339 | { |
| 340 | int status; |
| 341 | size_t burstcnt, limit, sent = 0; |
| 342 | uint8_t tpm_go[4] = { TPM_STS_GO }; |
| 343 | struct stopwatch sw; |
| 344 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 345 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 346 | |
| 347 | /* Wait until TPM is ready for a command */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 348 | while (!(cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 349 | if (stopwatch_expired(&sw)) { |
| 350 | printk(BIOS_ERR, "%s: Command ready timeout\n", |
| 351 | __func__); |
| 352 | return -1; |
| 353 | } |
| 354 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 355 | cr50_i2c_tis_ready(); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | while (len > 0) { |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 359 | uint8_t mask = TPM_STS_VALID; |
| 360 | |
| 361 | /* Wait for data if this is not the first chunk */ |
| 362 | if (sent > 0) |
| 363 | mask |= TPM_STS_DATA_EXPECT; |
| 364 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 365 | /* Read burst count and check status */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 366 | if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 367 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 368 | |
| 369 | /* Use burstcnt - 1 to account for the address byte |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 370 | * that is inserted by cr50_i2c_write() */ |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 371 | limit = MIN(burstcnt - 1, len); |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 372 | if (cr50_i2c_write(TPM_DATA_FIFO(tpm_dev.locality), &buf[sent], limit) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 373 | printk(BIOS_ERR, "%s: Write failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 374 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | sent += limit; |
| 378 | len -= limit; |
| 379 | } |
| 380 | |
| 381 | /* Ensure TPM is not expecting more data */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 382 | if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status) < 0) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 383 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 384 | if (status & TPM_STS_DATA_EXPECT) { |
| 385 | printk(BIOS_ERR, "%s: Data still expected\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 386 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* Start the TPM command */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 390 | if (cr50_i2c_write(TPM_STS(tpm_dev.locality), tpm_go, sizeof(tpm_go)) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 391 | printk(BIOS_ERR, "%s: Start command failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 392 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 393 | } |
| 394 | return sent; |
| 395 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 396 | out_err: |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 397 | /* Abort current transaction if still pending */ |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 398 | if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY) |
| 399 | cr50_i2c_tis_ready(); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | static void cr50_vendor_init(struct tpm_chip *chip) |
| 404 | { |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 405 | chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 406 | chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 407 | chip->req_canceled = TPM_STS_COMMAND_READY; |
| 408 | chip->status = &cr50_i2c_tis_status; |
| 409 | chip->recv = &cr50_i2c_tis_recv; |
| 410 | chip->send = &cr50_i2c_tis_send; |
| 411 | chip->cancel = &cr50_i2c_tis_ready; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 414 | int tpm_vendor_probe(unsigned int bus, uint32_t addr) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 415 | { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 419 | static int cr50_i2c_probe(uint32_t *did_vid) |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 420 | { |
| 421 | int retries; |
| 422 | |
| 423 | /* |
Rob Barnes | 22372f4 | 2022-02-11 07:59:21 -0700 | [diff] [blame] | 424 | * 1s should be enough to synchronize with the TPM even under the |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 425 | * worst nested reset request conditions. In vast majority of cases |
Rob Barnes | 22372f4 | 2022-02-11 07:59:21 -0700 | [diff] [blame] | 426 | * there would be no wait at all. If this probe fails, boot likely |
| 427 | * cannot proceed, so an extra long timeout is appropriate. |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 428 | */ |
| 429 | printk(BIOS_INFO, "Probing TPM I2C: "); |
| 430 | |
Rob Barnes | 22372f4 | 2022-02-11 07:59:21 -0700 | [diff] [blame] | 431 | for (retries = 100; retries > 0; retries--) { |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 432 | int rc; |
| 433 | |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 434 | rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4); |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 435 | |
| 436 | /* Exit once DID and VID verified */ |
Jes Klinke | 1430b04 | 2022-03-28 14:22:24 -0700 | [diff] [blame] | 437 | if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DID_VID)) { |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 438 | printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /* TPM might be resetting, let's retry in a bit. */ |
| 443 | mdelay(10); |
| 444 | printk(BIOS_INFO, "."); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * I2C reads failed, or the DID and VID didn't match |
| 449 | */ |
| 450 | printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid); |
| 451 | return -1; |
| 452 | } |
| 453 | |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 454 | int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 455 | { |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 456 | uint32_t did_vid = 0; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 457 | |
| 458 | if (dev_addr == 0) { |
| 459 | printk(BIOS_ERR, "%s: missing device address\n", __func__); |
| 460 | return -1; |
| 461 | } |
| 462 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 463 | tpm_dev.bus = bus; |
| 464 | tpm_dev.addr = dev_addr; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 465 | |
| 466 | cr50_vendor_init(chip); |
| 467 | |
Sergii Dmytruk | 86f845a | 2022-10-29 18:55:24 +0300 | [diff] [blame^] | 468 | if (cr50_i2c_probe(&did_vid)) |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 469 | return -1; |
| 470 | |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame] | 471 | if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK) |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 472 | if (process_reset()) |
Duncan Laurie | 2bc6ad3 | 2017-11-07 09:13:19 -0800 | [diff] [blame] | 473 | return -1; |
| 474 | |
Tim Wawrzynczak | eb1891a | 2022-02-08 12:49:31 -0700 | [diff] [blame] | 475 | if (claim_locality()) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 476 | return -1; |
| 477 | |
Daniel Kurtz | c4852e7 | 2017-04-21 14:11:51 +0800 | [diff] [blame] | 478 | printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n", |
Keith Short | 5143635 | 2018-12-17 14:21:46 -0700 | [diff] [blame] | 479 | bus, dev_addr, did_vid >> 16); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 480 | |
Jes Klinke | 1430b04 | 2022-03-28 14:22:24 -0700 | [diff] [blame] | 481 | if (tpm_first_access_this_boot()) { |
Tim Wawrzynczak | 1e50dfb | 2022-02-16 13:48:07 -0700 | [diff] [blame] | 482 | /* This is called for the side-effect of printing the version string. */ |
Jes Klinke | 1430b04 | 2022-03-28 14:22:24 -0700 | [diff] [blame] | 483 | cr50_get_firmware_version(NULL); |
| 484 | cr50_set_board_cfg(); |
Tim Wawrzynczak | 1e50dfb | 2022-02-16 13:48:07 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 487 | chip->is_open = 1; |
| 488 | return 0; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Subrata Banik | 60b2ab8 | 2022-03-09 12:55:34 +0530 | [diff] [blame] | 491 | enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes) |
Tim Wawrzynczak | 1e50dfb | 2022-02-16 13:48:07 -0700 | [diff] [blame] | 492 | { |
| 493 | return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS; |
| 494 | } |
| 495 | |
Subrata Banik | 60b2ab8 | 2022-03-09 12:55:34 +0530 | [diff] [blame] | 496 | enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes) |
Tim Wawrzynczak | 1e50dfb | 2022-02-16 13:48:07 -0700 | [diff] [blame] | 497 | { |
| 498 | return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS; |
| 499 | } |