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 */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 192 | static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 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 */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 204 | static void cr50_i2c_tis_ready(struct tpm_chip *chip) |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 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 */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 213 | 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] | 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 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 243 | 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] | 244 | size_t buf_len) |
| 245 | { |
| 246 | size_t burstcnt, current, len, expected; |
| 247 | uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 248 | uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 249 | int status; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 250 | |
| 251 | if (buf_len < TPM_HEADER_SIZE) |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 252 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 253 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 254 | if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 255 | printk(BIOS_ERR, "%s: First chunk not available\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 256 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* Read first chunk of burstcnt bytes */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 260 | if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 261 | printk(BIOS_ERR, "%s: Read failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 262 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* Determine expected data in the return buffer */ |
| 266 | expected = read_be32(buf + TPM_RSP_SIZE_BYTE); |
| 267 | if (expected > buf_len) { |
| 268 | printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n", |
| 269 | __func__, expected, buf_len); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 270 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* Now read the rest of the data */ |
| 274 | current = burstcnt; |
| 275 | while (current < expected) { |
| 276 | /* Read updated burst count and check status */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 277 | if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) |
| 278 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 279 | |
| 280 | len = min(burstcnt, expected - current); |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 281 | if (cr50_i2c_read(chip, addr, buf + current, len) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 282 | printk(BIOS_ERR, "%s: Read failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 283 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | current += len; |
| 287 | } |
| 288 | |
| 289 | /* Ensure TPM is done reading data */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 290 | if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0) |
| 291 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 292 | if (status & TPM_STS_DATA_AVAIL) { |
| 293 | printk(BIOS_ERR, "%s: Data still 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 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 297 | return current; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 298 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 299 | out_err: |
| 300 | /* Abort current transaction if still pending */ |
| 301 | if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY) |
| 302 | cr50_i2c_tis_ready(chip); |
| 303 | return -1; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 306 | 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] | 307 | { |
| 308 | int status; |
| 309 | size_t burstcnt, limit, sent = 0; |
| 310 | uint8_t tpm_go[4] = { TPM_STS_GO }; |
| 311 | struct stopwatch sw; |
| 312 | |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 313 | stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 314 | |
| 315 | /* Wait until TPM is ready for a command */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 316 | while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 317 | if (stopwatch_expired(&sw)) { |
| 318 | printk(BIOS_ERR, "%s: Command ready timeout\n", |
| 319 | __func__); |
| 320 | return -1; |
| 321 | } |
| 322 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 323 | cr50_i2c_tis_ready(chip); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | while (len > 0) { |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 327 | uint8_t mask = TPM_STS_VALID; |
| 328 | |
| 329 | /* Wait for data if this is not the first chunk */ |
| 330 | if (sent > 0) |
| 331 | mask |= TPM_STS_DATA_EXPECT; |
| 332 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 333 | /* Read burst count and check status */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 334 | if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) |
| 335 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 336 | |
| 337 | /* Use burstcnt - 1 to account for the address byte |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 338 | * that is inserted by cr50_i2c_write() */ |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 339 | limit = min(burstcnt - 1, len); |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 340 | if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality), |
| 341 | &buf[sent], limit) != 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 342 | printk(BIOS_ERR, "%s: Write failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 343 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | sent += limit; |
| 347 | len -= limit; |
| 348 | } |
| 349 | |
| 350 | /* Ensure TPM is not expecting more data */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 351 | if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0) |
| 352 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 353 | if (status & TPM_STS_DATA_EXPECT) { |
| 354 | printk(BIOS_ERR, "%s: Data still expected\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 355 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* Start the TPM command */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 359 | if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go, |
| 360 | sizeof(tpm_go)) < 0) { |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 361 | printk(BIOS_ERR, "%s: Start command failed\n", __func__); |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 362 | goto out_err; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 363 | } |
| 364 | return sent; |
| 365 | |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 366 | out_err: |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 367 | /* Abort current transaction if still pending */ |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 368 | if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY) |
| 369 | cr50_i2c_tis_ready(chip); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | static void cr50_vendor_init(struct tpm_chip *chip) |
| 374 | { |
| 375 | memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific)); |
| 376 | chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 377 | chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 378 | chip->vendor.req_canceled = TPM_STS_COMMAND_READY; |
Duncan Laurie | f235a9b | 2016-09-19 17:19:10 -0700 | [diff] [blame^] | 379 | chip->vendor.status = &cr50_i2c_tis_status; |
| 380 | chip->vendor.recv = &cr50_i2c_tis_recv; |
| 381 | chip->vendor.send = &cr50_i2c_tis_send; |
| 382 | chip->vendor.cancel = &cr50_i2c_tis_ready; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | int tpm_vendor_probe(unsigned bus, uint32_t addr) |
| 386 | { |
| 387 | 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] | 388 | struct tpm_chip probe_chip; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 389 | struct stopwatch sw; |
| 390 | uint8_t buf = 0; |
| 391 | int ret; |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 392 | long sw_run_duration = CR50_TIMEOUT_LONG_MS; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 393 | |
| 394 | tpm_dev->bus = bus; |
| 395 | tpm_dev->addr = addr; |
| 396 | |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 397 | cr50_vendor_init(&probe_chip); |
| 398 | |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 399 | /* Wait for TPM_ACCESS register ValidSts bit to be set */ |
| 400 | stopwatch_init_msecs_expire(&sw, sw_run_duration); |
| 401 | do { |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 402 | ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 403 | if (!ret && (buf & TPM_STS_VALID)) { |
| 404 | sw_run_duration = stopwatch_duration_msecs(&sw); |
| 405 | break; |
| 406 | } |
Duncan Laurie | 1dc036c | 2016-09-19 16:49:23 -0700 | [diff] [blame] | 407 | mdelay(CR50_TIMEOUT_SHORT_MS); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 408 | } while (!stopwatch_expired(&sw)); |
| 409 | |
| 410 | printk(BIOS_INFO, |
| 411 | "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n", |
| 412 | __func__, (buf & TPM_STS_VALID) ? "set" : "clear", |
| 413 | (buf & TPM_STS_VALID) >> 7, sw_run_duration); |
| 414 | |
| 415 | /* Claim failure if the ValidSts (bit 7) is clear */ |
| 416 | if (!(buf & TPM_STS_VALID)) |
| 417 | return -1; |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr) |
| 423 | { |
| 424 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 425 | uint32_t vendor; |
| 426 | |
| 427 | if (dev_addr == 0) { |
| 428 | printk(BIOS_ERR, "%s: missing device address\n", __func__); |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | tpm_dev->bus = bus; |
| 433 | tpm_dev->addr = dev_addr; |
| 434 | |
| 435 | cr50_vendor_init(chip); |
| 436 | |
| 437 | /* Disable interrupts (not supported) */ |
| 438 | chip->vendor.irq = 0; |
| 439 | |
| 440 | if (request_locality(chip, 0) != 0) |
| 441 | return -1; |
| 442 | |
| 443 | /* Read four bytes from DID_VID register */ |
Duncan Laurie | 510cb6a | 2016-09-19 17:05:45 -0700 | [diff] [blame] | 444 | 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] | 445 | goto out_err; |
| 446 | |
| 447 | if (vendor != CR50_DID_VID) { |
| 448 | printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor); |
| 449 | goto out_err; |
| 450 | } |
| 451 | |
| 452 | printk(BIOS_DEBUG, "cr50 TPM %u:%02x (device-id 0x%X)\n", |
| 453 | tpm_dev->bus, tpm_dev->addr, vendor >> 16); |
| 454 | |
| 455 | chip->is_open = 1; |
| 456 | return 0; |
| 457 | |
| 458 | out_err: |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 459 | release_locality(chip, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | void tpm_vendor_cleanup(struct tpm_chip *chip) |
| 464 | { |
Duncan Laurie | d3920e7 | 2016-09-19 17:09:01 -0700 | [diff] [blame] | 465 | release_locality(chip, 1); |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 466 | } |