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> |
| 34 | #include <stdint.h> |
| 35 | #include <string.h> |
| 36 | #include <types.h> |
| 37 | #include <delay.h> |
| 38 | #include <console/console.h> |
| 39 | #include <device/i2c.h> |
| 40 | #include <endian.h> |
| 41 | #include <timer.h> |
Furquan Shaikh | 260b297 | 2017-04-07 13:26:01 -0700 | [diff] [blame^] | 42 | #include <tpm.h> |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 43 | #include "tpm.h" |
| 44 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 45 | #if IS_ENABLED(CONFIG_ARCH_X86) |
| 46 | #include <arch/acpi.h> |
| 47 | #endif |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 48 | |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 49 | #define CR50_MAX_BUFSIZE 63 |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 50 | #define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */ |
| 51 | #define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */ |
Duncan Laurie | ed4fa09 | 2016-11-01 15:03:13 -0700 | [diff] [blame] | 52 | #define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */ |
| 53 | #define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */ |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 54 | #define CR50_DID_VID 0x00281ae0L |
| 55 | |
| 56 | struct tpm_inf_dev { |
| 57 | int bus; |
| 58 | unsigned int addr; |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 59 | uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)]; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL; |
| 63 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 64 | /* Wait for interrupt to indicate the TPM is ready */ |
| 65 | static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip) |
| 66 | { |
| 67 | struct stopwatch sw; |
| 68 | |
| 69 | if (!chip->vendor.irq_status) { |
| 70 | /* Fixed delay if interrupt not supported */ |
Duncan Laurie | ed4fa09 | 2016-11-01 15:03:13 -0700 | [diff] [blame] | 71 | mdelay(CR50_TIMEOUT_NOIRQ_MS); |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Duncan Laurie | ed4fa09 | 2016-11-01 15:03:13 -0700 | [diff] [blame] | 75 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS); |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 76 | |
| 77 | while (!chip->vendor.irq_status(chip->vendor.irq)) |
| 78 | if (stopwatch_expired(&sw)) |
| 79 | return -1; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* Clear pending interrupts */ |
| 85 | static void cr50_i2c_clear_tpm_irq(struct tpm_chip *chip) |
| 86 | { |
| 87 | if (chip->vendor.irq_status) |
| 88 | chip->vendor.irq_status(chip->vendor.irq); |
| 89 | } |
| 90 | |
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 | * cr50_i2c_read() - read from TPM register |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 93 | * |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 94 | * @chip: TPM chip information |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 95 | * @addr: register address to read from |
| 96 | * @buffer: provided by caller |
| 97 | * @len: number of bytes to read |
| 98 | * |
| 99 | * 1) send register address byte 'addr' to the TPM |
| 100 | * 2) wait for TPM to indicate it is ready |
| 101 | * 3) read 'len' bytes of TPM response into the provided 'buffer' |
| 102 | * |
| 103 | * Return -1 on error, 0 on success. |
| 104 | */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 105 | static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr, |
| 106 | uint8_t *buffer, size_t len) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 107 | { |
| 108 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 109 | |
| 110 | if (tpm_dev->addr == 0) |
| 111 | return -1; |
| 112 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 113 | /* Clear interrupt before starting transaction */ |
| 114 | cr50_i2c_clear_tpm_irq(chip); |
| 115 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 116 | /* Send the register address byte to the TPM */ |
| 117 | if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, &addr, 1)) { |
| 118 | printk(BIOS_ERR, "%s: Address write failed\n", __func__); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | /* Wait for TPM to be ready with response data */ |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 123 | if (cr50_i2c_wait_tpm_ready(chip) < 0) |
| 124 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 125 | |
| 126 | /* Read response data from the TPM */ |
| 127 | if (i2c_read_raw(tpm_dev->bus, tpm_dev->addr, buffer, len)) { |
| 128 | printk(BIOS_ERR, "%s: Read response failed\n", __func__); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 136 | * cr50_i2c_write() - write to TPM register |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 137 | * |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 138 | * @chip: TPM chip information |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 139 | * @addr: register address to write to |
| 140 | * @buffer: data to write |
| 141 | * @len: number of bytes to write |
| 142 | * |
| 143 | * 1) prepend the provided address to the provided data |
| 144 | * 2) send the address+data to the TPM |
| 145 | * 3) wait for TPM to indicate it is done writing |
| 146 | * |
| 147 | * Returns -1 on error, 0 on success. |
| 148 | */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 149 | static int cr50_i2c_write(struct tpm_chip *chip, |
| 150 | uint8_t addr, uint8_t *buffer, size_t len) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 151 | { |
| 152 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 153 | |
| 154 | if (tpm_dev->addr == 0) |
| 155 | return -1; |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 156 | if (len > CR50_MAX_BUFSIZE) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 157 | return -1; |
| 158 | |
| 159 | /* Prepend the 'register address' to the buffer */ |
| 160 | tpm_dev->buf[0] = addr; |
| 161 | memcpy(tpm_dev->buf + 1, buffer, len); |
| 162 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 163 | /* Clear interrupt before starting transaction */ |
| 164 | cr50_i2c_clear_tpm_irq(chip); |
| 165 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 166 | /* Send write request buffer with address */ |
| 167 | if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, tpm_dev->buf, len + 1)) { |
| 168 | printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | /* Wait for TPM to be ready */ |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 173 | return cr50_i2c_wait_tpm_ready(chip); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static int check_locality(struct tpm_chip *chip, int loc) |
| 177 | { |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 178 | uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 179 | uint8_t buf; |
| 180 | |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 181 | if (cr50_i2c_read(chip, TPM_ACCESS(loc), &buf, 1) < 0) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 182 | return -1; |
| 183 | |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 184 | if ((buf & mask) == mask) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 185 | chip->vendor.locality = loc; |
| 186 | return loc; |
| 187 | } |
| 188 | |
| 189 | return -1; |
| 190 | } |
| 191 | |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 192 | static void release_locality(struct tpm_chip *chip, int force) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 193 | { |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 194 | uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING; |
| 195 | uint8_t addr = TPM_ACCESS(chip->vendor.locality); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 196 | uint8_t buf; |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 197 | |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 198 | if (cr50_i2c_read(chip, addr, &buf, 1) < 0) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 199 | return; |
| 200 | |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 201 | if (force || (buf & mask) == mask) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 202 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 203 | cr50_i2c_write(chip, addr, &buf, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 204 | } |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 205 | |
| 206 | chip->vendor.locality = 0; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int request_locality(struct tpm_chip *chip, int loc) |
| 210 | { |
| 211 | uint8_t buf = TPM_ACCESS_REQUEST_USE; |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 212 | struct stopwatch sw; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 213 | |
| 214 | if (check_locality(chip, loc) >= 0) |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 215 | return loc; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 216 | |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 217 | if (cr50_i2c_write(chip, TPM_ACCESS(loc), &buf, 1) < 0) |
| 218 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 219 | |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 220 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
| 221 | while (check_locality(chip, loc) < 0) { |
| 222 | if (stopwatch_expired(&sw)) |
| 223 | return -1; |
| 224 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 225 | } |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 226 | return loc; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* cr50 requires all 4 bytes of status register to be read */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 230 | static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 231 | { |
| 232 | uint8_t buf[4]; |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 233 | if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality), |
| 234 | buf, sizeof(buf)) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 235 | printk(BIOS_ERR, "%s: Failed to read status\n", __func__); |
| 236 | return 0; |
| 237 | } |
| 238 | return buf[0]; |
| 239 | } |
| 240 | |
| 241 | /* cr50 requires all 4 bytes of status register to be written */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 242 | static void cr50_i2c_tis_ready(struct tpm_chip *chip) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 243 | { |
| 244 | uint8_t buf[4] = { TPM_STS_COMMAND_READY }; |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 245 | cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf)); |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 246 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* cr50 uses bytes 3:2 of status register for burst count and |
| 250 | * all 4 bytes must be read */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 251 | 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] | 252 | size_t *burst, int *status) |
| 253 | { |
| 254 | uint8_t buf[4]; |
| 255 | struct stopwatch sw; |
| 256 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 257 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 258 | |
| 259 | while (!stopwatch_expired(&sw)) { |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 260 | if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality), |
| 261 | buf, sizeof(buf)) != 0) { |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 262 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
| 266 | *status = buf[0]; |
| 267 | *burst = read_le16(&buf[1]); |
| 268 | |
| 269 | /* Check if mask matches and burst is valid */ |
| 270 | if ((*status & mask) == mask && |
Duncan Laurie | 3727a8d | 2016-09-19 16:37:46 -0700 | [diff] [blame] | 271 | *burst > 0 && *burst <= CR50_MAX_BUFSIZE) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 274 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__); |
| 278 | return -1; |
| 279 | } |
| 280 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 281 | 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] | 282 | size_t buf_len) |
| 283 | { |
| 284 | size_t burstcnt, current, len, expected; |
| 285 | uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 286 | uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 287 | int status; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 288 | |
| 289 | if (buf_len < TPM_HEADER_SIZE) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 290 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 291 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 292 | if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 293 | printk(BIOS_ERR, "%s: First chunk not available\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 294 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Read first chunk of burstcnt bytes */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 298 | if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 299 | printk(BIOS_ERR, "%s: Read failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 300 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Determine expected data in the return buffer */ |
| 304 | expected = read_be32(buf + TPM_RSP_SIZE_BYTE); |
| 305 | if (expected > buf_len) { |
| 306 | printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n", |
| 307 | __func__, expected, buf_len); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 308 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Now read the rest of the data */ |
| 312 | current = burstcnt; |
| 313 | while (current < expected) { |
| 314 | /* Read updated burst count and check status */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 315 | if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) |
| 316 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 317 | |
| 318 | len = min(burstcnt, expected - current); |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 319 | if (cr50_i2c_read(chip, addr, buf + current, len) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 320 | printk(BIOS_ERR, "%s: Read failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 321 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | current += len; |
| 325 | } |
| 326 | |
| 327 | /* Ensure TPM is done reading data */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 328 | if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0) |
| 329 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 330 | if (status & TPM_STS_DATA_AVAIL) { |
| 331 | printk(BIOS_ERR, "%s: Data still available\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 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 335 | return current; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 336 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 337 | out_err: |
| 338 | /* Abort current transaction if still pending */ |
| 339 | if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY) |
| 340 | cr50_i2c_tis_ready(chip); |
| 341 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 344 | 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] | 345 | { |
| 346 | int status; |
| 347 | size_t burstcnt, limit, sent = 0; |
| 348 | uint8_t tpm_go[4] = { TPM_STS_GO }; |
| 349 | struct stopwatch sw; |
| 350 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 351 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 352 | |
| 353 | /* Wait until TPM is ready for a command */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 354 | while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 355 | if (stopwatch_expired(&sw)) { |
| 356 | printk(BIOS_ERR, "%s: Command ready timeout\n", |
| 357 | __func__); |
| 358 | return -1; |
| 359 | } |
| 360 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 361 | cr50_i2c_tis_ready(chip); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | while (len > 0) { |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 365 | uint8_t mask = TPM_STS_VALID; |
| 366 | |
| 367 | /* Wait for data if this is not the first chunk */ |
| 368 | if (sent > 0) |
| 369 | mask |= TPM_STS_DATA_EXPECT; |
| 370 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 371 | /* Read burst count and check status */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 372 | if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) |
| 373 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 374 | |
| 375 | /* Use burstcnt - 1 to account for the address byte |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 376 | * that is inserted by cr50_i2c_write() */ |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 377 | limit = min(burstcnt - 1, len); |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 378 | if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality), |
| 379 | &buf[sent], limit) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 380 | printk(BIOS_ERR, "%s: Write failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 381 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | sent += limit; |
| 385 | len -= limit; |
| 386 | } |
| 387 | |
| 388 | /* Ensure TPM is not expecting more data */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 389 | if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0) |
| 390 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 391 | if (status & TPM_STS_DATA_EXPECT) { |
| 392 | printk(BIOS_ERR, "%s: Data still expected\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 393 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* Start the TPM command */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 397 | if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go, |
| 398 | sizeof(tpm_go)) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 399 | printk(BIOS_ERR, "%s: Start command failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 400 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 401 | } |
| 402 | return sent; |
| 403 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 404 | out_err: |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 405 | /* Abort current transaction if still pending */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 406 | if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY) |
| 407 | cr50_i2c_tis_ready(chip); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | static void cr50_vendor_init(struct tpm_chip *chip) |
| 412 | { |
| 413 | memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific)); |
| 414 | chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 415 | chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 416 | chip->vendor.req_canceled = TPM_STS_COMMAND_READY; |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame] | 417 | chip->vendor.status = &cr50_i2c_tis_status; |
| 418 | chip->vendor.recv = &cr50_i2c_tis_recv; |
| 419 | chip->vendor.send = &cr50_i2c_tis_send; |
| 420 | chip->vendor.cancel = &cr50_i2c_tis_ready; |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 421 | chip->vendor.irq = CONFIG_DRIVER_TPM_I2C_IRQ; |
Duncan Laurie | 48f708d | 2016-10-03 12:43:14 -0700 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Interrupts are not supported this early in firmware, |
| 425 | * use use an arch-specific method to query for interrupt status. |
| 426 | */ |
| 427 | if (chip->vendor.irq > 0) { |
| 428 | #if IS_ENABLED(CONFIG_ARCH_X86) |
| 429 | /* Query GPE status for interrupt */ |
| 430 | chip->vendor.irq_status = &acpi_get_gpe; |
| 431 | #else |
| 432 | chip->vendor.irq = -1; |
| 433 | #endif |
| 434 | } |
Duncan Laurie | ed4fa09 | 2016-11-01 15:03:13 -0700 | [diff] [blame] | 435 | |
| 436 | if (chip->vendor.irq <= 0) |
| 437 | printk(BIOS_WARNING, |
| 438 | "%s: No IRQ, will use %ums delay for TPM ready\n", |
| 439 | __func__, CR50_TIMEOUT_NOIRQ_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 442 | int tpm_vendor_probe(unsigned int bus, uint32_t addr) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 443 | { |
| 444 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 445 | struct tpm_chip probe_chip; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 446 | struct stopwatch sw; |
| 447 | uint8_t buf = 0; |
| 448 | int ret; |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 449 | long sw_run_duration = CR50_TIMEOUT_LONG_MS; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 450 | |
| 451 | tpm_dev->bus = bus; |
| 452 | tpm_dev->addr = addr; |
| 453 | |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 454 | cr50_vendor_init(&probe_chip); |
| 455 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 456 | /* Wait for TPM_ACCESS register ValidSts bit to be set */ |
| 457 | stopwatch_init_msecs_expire(&sw, sw_run_duration); |
| 458 | do { |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 459 | ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 460 | if (!ret && (buf & TPM_STS_VALID)) { |
| 461 | sw_run_duration = stopwatch_duration_msecs(&sw); |
| 462 | break; |
| 463 | } |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 464 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 465 | } while (!stopwatch_expired(&sw)); |
| 466 | |
| 467 | printk(BIOS_INFO, |
| 468 | "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n", |
| 469 | __func__, (buf & TPM_STS_VALID) ? "set" : "clear", |
| 470 | (buf & TPM_STS_VALID) >> 7, sw_run_duration); |
| 471 | |
| 472 | /* Claim failure if the ValidSts (bit 7) is clear */ |
| 473 | if (!(buf & TPM_STS_VALID)) |
| 474 | return -1; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Lee Leahy | 52ab30b | 2017-03-15 09:22:11 -0700 | [diff] [blame] | 479 | 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] | 480 | { |
| 481 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 482 | uint32_t vendor; |
| 483 | |
| 484 | if (dev_addr == 0) { |
| 485 | printk(BIOS_ERR, "%s: missing device address\n", __func__); |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | tpm_dev->bus = bus; |
| 490 | tpm_dev->addr = dev_addr; |
| 491 | |
| 492 | cr50_vendor_init(chip); |
| 493 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 494 | if (request_locality(chip, 0) != 0) |
| 495 | return -1; |
| 496 | |
| 497 | /* Read four bytes from DID_VID register */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 498 | if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 499 | goto out_err; |
| 500 | |
| 501 | if (vendor != CR50_DID_VID) { |
| 502 | printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor); |
| 503 | goto out_err; |
| 504 | } |
| 505 | |
Duncan Laurie | 94cc485 | 2016-09-19 17:22:10 -0700 | [diff] [blame] | 506 | printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x irq %d id 0x%x)\n", |
| 507 | bus, dev_addr, chip->vendor.irq, vendor >> 16); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 508 | |
| 509 | chip->is_open = 1; |
| 510 | return 0; |
| 511 | |
| 512 | out_err: |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 513 | release_locality(chip, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | void tpm_vendor_cleanup(struct tpm_chip *chip) |
| 518 | { |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 519 | release_locality(chip, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 520 | } |