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