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