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