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