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