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