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