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