Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Author: |
| 4 | * Miquel Raynal <miquel.raynal@bootlin.com> |
| 5 | * |
| 6 | * Description: |
| 7 | * SPI-level driver for TCG/TIS TPM (trusted platform module). |
| 8 | * Specifications at www.trustedcomputinggroup.org |
| 9 | * |
| 10 | * This device driver implements the TPM interface as defined in |
| 11 | * the TCG SPI protocol stack version 2.0. |
| 12 | * |
| 13 | * It is based on the U-Boot driver tpm_tis_infineon_i2c.c. |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | #include <dm.h> |
| 18 | #include <fdtdec.h> |
| 19 | #include <log.h> |
| 20 | #include <spi.h> |
| 21 | #include <tpm-v2.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/compiler.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/unaligned/be_byteshift.h> |
Miquel Raynal | 68f34a2 | 2018-05-16 08:59:16 +0200 | [diff] [blame] | 26 | #include <asm-generic/gpio.h> |
Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 27 | |
| 28 | #include "tpm_tis.h" |
| 29 | #include "tpm_internal.h" |
| 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
| 33 | #define TPM_ACCESS(l) (0x0000 | ((l) << 12)) |
| 34 | #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12)) |
| 35 | #define TPM_STS(l) (0x0018 | ((l) << 12)) |
| 36 | #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12)) |
| 37 | #define TPM_DID_VID(l) (0x0F00 | ((l) << 12)) |
| 38 | #define TPM_RID(l) (0x0F04 | ((l) << 12)) |
| 39 | |
| 40 | #define MAX_SPI_FRAMESIZE 64 |
| 41 | |
| 42 | /* Number of wait states to wait for */ |
| 43 | #define TPM_WAIT_STATES 100 |
| 44 | |
| 45 | /** |
| 46 | * struct tpm_tis_chip_data - Non-discoverable TPM information |
| 47 | * |
| 48 | * @pcr_count: Number of PCR per bank |
| 49 | * @pcr_select_min: Size in octets of the pcrSelect array |
| 50 | */ |
| 51 | struct tpm_tis_chip_data { |
| 52 | unsigned int pcr_count; |
| 53 | unsigned int pcr_select_min; |
| 54 | unsigned int time_before_first_cmd_ms; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * tpm_tis_spi_read() - Read from TPM register |
| 59 | * |
| 60 | * @addr: register address to read from |
| 61 | * @buffer: provided by caller |
| 62 | * @len: number of bytes to read |
| 63 | * |
| 64 | * Read len bytes from TPM register and put them into |
| 65 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 66 | * |
| 67 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 68 | * values have to be swapped. |
| 69 | * |
| 70 | * @return -EIO on error, 0 on success. |
| 71 | */ |
| 72 | static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out, |
| 73 | u8 *in, u16 len) |
| 74 | { |
| 75 | struct spi_slave *slave = dev_get_parent_priv(dev); |
| 76 | int transfer_len, ret; |
| 77 | u8 tx_buf[MAX_SPI_FRAMESIZE]; |
| 78 | u8 rx_buf[MAX_SPI_FRAMESIZE]; |
| 79 | |
| 80 | if (in && out) { |
| 81 | log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n", |
| 82 | __func__); |
| 83 | return -EINVAL; |
| 84 | } |
| 85 | |
| 86 | ret = spi_claim_bus(slave); |
| 87 | if (ret < 0) { |
| 88 | log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | while (len) { |
| 93 | /* Request */ |
| 94 | transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE); |
| 95 | tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1); |
| 96 | tx_buf[1] = 0xD4; |
| 97 | tx_buf[2] = addr >> 8; |
| 98 | tx_buf[3] = addr; |
| 99 | |
| 100 | ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN); |
| 101 | if (ret < 0) { |
| 102 | log(LOGC_NONE, LOGL_ERR, |
| 103 | "%s: spi request transfer failed (err: %d)\n", |
| 104 | __func__, ret); |
| 105 | goto release_bus; |
| 106 | } |
| 107 | |
| 108 | /* Wait state */ |
| 109 | if (!(rx_buf[3] & 0x1)) { |
| 110 | int i; |
| 111 | |
| 112 | for (i = 0; i < TPM_WAIT_STATES; i++) { |
| 113 | ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0); |
| 114 | if (ret) { |
| 115 | log(LOGC_NONE, LOGL_ERR, |
| 116 | "%s: wait state failed: %d\n", |
| 117 | __func__, ret); |
| 118 | goto release_bus; |
| 119 | } |
| 120 | |
| 121 | if (rx_buf[0] & 0x1) |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (i == TPM_WAIT_STATES) { |
| 126 | log(LOGC_NONE, LOGL_ERR, |
| 127 | "%s: timeout on wait state\n", __func__); |
| 128 | ret = -ETIMEDOUT; |
| 129 | goto release_bus; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Read/Write */ |
| 134 | if (out) { |
| 135 | memcpy(tx_buf, out, transfer_len); |
| 136 | out += transfer_len; |
| 137 | } |
| 138 | |
| 139 | ret = spi_xfer(slave, transfer_len * 8, |
| 140 | out ? tx_buf : NULL, |
| 141 | in ? rx_buf : NULL, |
| 142 | SPI_XFER_END); |
| 143 | if (ret) { |
| 144 | log(LOGC_NONE, LOGL_ERR, |
| 145 | "%s: spi read transfer failed (err: %d)\n", |
| 146 | __func__, ret); |
| 147 | goto release_bus; |
| 148 | } |
| 149 | |
| 150 | if (in) { |
| 151 | memcpy(in, rx_buf, transfer_len); |
| 152 | in += transfer_len; |
| 153 | } |
| 154 | |
| 155 | len -= transfer_len; |
| 156 | } |
| 157 | |
| 158 | release_bus: |
| 159 | /* If an error occurred, release the chip by deasserting the CS */ |
| 160 | if (ret < 0) |
| 161 | spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END); |
| 162 | |
| 163 | spi_release_bus(slave); |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | static int tpm_tis_spi_read(struct udevice *dev, u16 addr, u8 *in, u16 len) |
| 169 | { |
| 170 | return tpm_tis_spi_xfer(dev, addr, NULL, in, len); |
| 171 | } |
| 172 | |
| 173 | static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result) |
| 174 | { |
| 175 | __le32 result_le; |
| 176 | int ret; |
| 177 | |
| 178 | ret = tpm_tis_spi_read(dev, addr, (u8 *)&result_le, sizeof(u32)); |
| 179 | if (!ret) |
| 180 | *result = le32_to_cpu(result_le); |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static int tpm_tis_spi_write(struct udevice *dev, u16 addr, const u8 *out, |
| 186 | u16 len) |
| 187 | { |
| 188 | return tpm_tis_spi_xfer(dev, addr, out, NULL, len); |
| 189 | } |
| 190 | |
| 191 | static int tpm_tis_spi_check_locality(struct udevice *dev, int loc) |
| 192 | { |
| 193 | const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID; |
| 194 | struct tpm_chip *chip = dev_get_priv(dev); |
| 195 | u8 buf; |
| 196 | int ret; |
| 197 | |
| 198 | ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
| 202 | if ((buf & mask) == mask) { |
| 203 | chip->locality = loc; |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | return -ENOENT; |
| 208 | } |
| 209 | |
| 210 | static void tpm_tis_spi_release_locality(struct udevice *dev, int loc, |
| 211 | bool force) |
| 212 | { |
| 213 | const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID; |
| 214 | u8 buf; |
| 215 | |
| 216 | if (tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1) < 0) |
| 217 | return; |
| 218 | |
| 219 | if (force || (buf & mask) == mask) { |
| 220 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
| 221 | tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static int tpm_tis_spi_request_locality(struct udevice *dev, int loc) |
| 226 | { |
| 227 | struct tpm_chip *chip = dev_get_priv(dev); |
| 228 | unsigned long start, stop; |
| 229 | u8 buf = TPM_ACCESS_REQUEST_USE; |
| 230 | int ret; |
| 231 | |
| 232 | ret = tpm_tis_spi_check_locality(dev, loc); |
| 233 | if (!ret) |
| 234 | return 0; |
| 235 | |
| 236 | if (ret != -ENOENT) { |
| 237 | log(LOGC_NONE, LOGL_ERR, "%s: Failed to get locality: %d\n", |
| 238 | __func__, ret); |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | ret = tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1); |
| 243 | if (ret) { |
| 244 | log(LOGC_NONE, LOGL_ERR, "%s: Failed to write to TPM: %d\n", |
| 245 | __func__, ret); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | start = get_timer(0); |
| 250 | stop = chip->timeout_a; |
| 251 | do { |
| 252 | ret = tpm_tis_spi_check_locality(dev, loc); |
| 253 | if (!ret) |
| 254 | return 0; |
| 255 | |
| 256 | if (ret != -ENOENT) { |
| 257 | log(LOGC_NONE, LOGL_ERR, |
| 258 | "%s: Failed to get locality: %d\n", __func__, ret); |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | mdelay(TPM_TIMEOUT_MS); |
| 263 | } while (get_timer(start) < stop); |
| 264 | |
| 265 | log(LOGC_NONE, LOGL_ERR, "%s: Timeout getting locality: %d\n", __func__, |
| 266 | ret); |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static u8 tpm_tis_spi_status(struct udevice *dev, u8 *status) |
| 272 | { |
| 273 | struct tpm_chip *chip = dev_get_priv(dev); |
| 274 | |
| 275 | return tpm_tis_spi_read(dev, TPM_STS(chip->locality), status, 1); |
| 276 | } |
| 277 | |
| 278 | static int tpm_tis_spi_wait_for_stat(struct udevice *dev, u8 mask, |
| 279 | unsigned long timeout, u8 *status) |
| 280 | { |
| 281 | unsigned long start = get_timer(0); |
| 282 | unsigned long stop = timeout; |
| 283 | int ret; |
| 284 | |
| 285 | do { |
| 286 | mdelay(TPM_TIMEOUT_MS); |
| 287 | ret = tpm_tis_spi_status(dev, status); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
| 291 | if ((*status & mask) == mask) |
| 292 | return 0; |
| 293 | } while (get_timer(start) < stop); |
| 294 | |
| 295 | return -ETIMEDOUT; |
| 296 | } |
| 297 | |
Roman Kapl | c84dd7f | 2019-05-16 18:32:48 +0200 | [diff] [blame] | 298 | static u8 tpm_tis_spi_valid_status(struct udevice *dev, u8 *status) |
| 299 | { |
| 300 | struct tpm_chip *chip = dev_get_priv(dev); |
| 301 | |
| 302 | return tpm_tis_spi_wait_for_stat(dev, TPM_STS_VALID, |
| 303 | chip->timeout_c, status); |
| 304 | } |
| 305 | |
Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 306 | static int tpm_tis_spi_get_burstcount(struct udevice *dev) |
| 307 | { |
| 308 | struct tpm_chip *chip = dev_get_priv(dev); |
| 309 | unsigned long start, stop; |
| 310 | u32 burstcount, ret; |
| 311 | |
| 312 | /* wait for burstcount */ |
| 313 | start = get_timer(0); |
| 314 | stop = chip->timeout_d; |
| 315 | do { |
| 316 | ret = tpm_tis_spi_read32(dev, TPM_STS(chip->locality), |
| 317 | &burstcount); |
| 318 | if (ret) |
| 319 | return -EBUSY; |
| 320 | |
| 321 | burstcount = (burstcount >> 8) & 0xFFFF; |
| 322 | if (burstcount) |
| 323 | return burstcount; |
| 324 | |
| 325 | mdelay(TPM_TIMEOUT_MS); |
| 326 | } while (get_timer(start) < stop); |
| 327 | |
| 328 | return -EBUSY; |
| 329 | } |
| 330 | |
| 331 | static int tpm_tis_spi_cancel(struct udevice *dev) |
| 332 | { |
| 333 | struct tpm_chip *chip = dev_get_priv(dev); |
| 334 | u8 data = TPM_STS_COMMAND_READY; |
| 335 | |
| 336 | return tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1); |
| 337 | } |
| 338 | |
| 339 | static int tpm_tis_spi_recv_data(struct udevice *dev, u8 *buf, size_t count) |
| 340 | { |
| 341 | struct tpm_chip *chip = dev_get_priv(dev); |
| 342 | int size = 0, burstcnt, len, ret; |
| 343 | u8 status; |
| 344 | |
| 345 | while (size < count && |
| 346 | tpm_tis_spi_wait_for_stat(dev, |
| 347 | TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 348 | chip->timeout_c, &status) == 0) { |
| 349 | burstcnt = tpm_tis_spi_get_burstcount(dev); |
| 350 | if (burstcnt < 0) |
| 351 | return burstcnt; |
| 352 | |
| 353 | len = min_t(int, burstcnt, count - size); |
| 354 | ret = tpm_tis_spi_read(dev, TPM_DATA_FIFO(chip->locality), |
| 355 | buf + size, len); |
| 356 | if (ret < 0) |
| 357 | return ret; |
| 358 | |
| 359 | size += len; |
| 360 | } |
| 361 | |
| 362 | return size; |
| 363 | } |
| 364 | |
| 365 | static int tpm_tis_spi_recv(struct udevice *dev, u8 *buf, size_t count) |
| 366 | { |
| 367 | struct tpm_chip *chip = dev_get_priv(dev); |
| 368 | int size, expected; |
| 369 | |
| 370 | if (!chip) |
| 371 | return -ENODEV; |
| 372 | |
| 373 | if (count < TPM_HEADER_SIZE) { |
| 374 | size = -EIO; |
| 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | size = tpm_tis_spi_recv_data(dev, buf, TPM_HEADER_SIZE); |
| 379 | if (size < TPM_HEADER_SIZE) { |
| 380 | log(LOGC_NONE, LOGL_ERR, "TPM error, unable to read header\n"); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | expected = get_unaligned_be32(buf + 2); |
| 385 | if (expected > count) { |
| 386 | size = -EIO; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | size += tpm_tis_spi_recv_data(dev, &buf[TPM_HEADER_SIZE], |
| 391 | expected - TPM_HEADER_SIZE); |
| 392 | if (size < expected) { |
| 393 | log(LOGC_NONE, LOGL_ERR, |
| 394 | "TPM error, unable to read remaining bytes of result\n"); |
| 395 | size = -EIO; |
| 396 | goto out; |
| 397 | } |
| 398 | |
| 399 | out: |
| 400 | tpm_tis_spi_cancel(dev); |
| 401 | tpm_tis_spi_release_locality(dev, chip->locality, false); |
| 402 | |
| 403 | return size; |
| 404 | } |
| 405 | |
| 406 | static int tpm_tis_spi_send(struct udevice *dev, const u8 *buf, size_t len) |
| 407 | { |
| 408 | struct tpm_chip *chip = dev_get_priv(dev); |
| 409 | u32 i, size; |
| 410 | u8 status; |
| 411 | int burstcnt, ret; |
| 412 | u8 data; |
| 413 | |
| 414 | if (!chip) |
| 415 | return -ENODEV; |
| 416 | |
| 417 | if (len > TPM_DEV_BUFSIZE) |
| 418 | return -E2BIG; /* Command is too long for our tpm, sorry */ |
| 419 | |
| 420 | ret = tpm_tis_spi_request_locality(dev, 0); |
| 421 | if (ret < 0) |
| 422 | return -EBUSY; |
| 423 | |
| 424 | /* |
| 425 | * Check if the TPM is ready. If not, if not, cancel the pending command |
| 426 | * and poll on the status to be finally ready. |
| 427 | */ |
| 428 | ret = tpm_tis_spi_status(dev, &status); |
| 429 | if (ret) |
| 430 | return ret; |
| 431 | |
| 432 | if (!(status & TPM_STS_COMMAND_READY)) { |
| 433 | /* Force the transition, usually this will be done at startup */ |
| 434 | ret = tpm_tis_spi_cancel(dev); |
| 435 | if (ret) { |
| 436 | log(LOGC_NONE, LOGL_ERR, |
| 437 | "%s: Could not cancel previous operation\n", |
| 438 | __func__); |
| 439 | goto out_err; |
| 440 | } |
| 441 | |
| 442 | ret = tpm_tis_spi_wait_for_stat(dev, TPM_STS_COMMAND_READY, |
| 443 | chip->timeout_b, &status); |
| 444 | if (ret < 0 || !(status & TPM_STS_COMMAND_READY)) { |
| 445 | log(LOGC_NONE, LOGL_ERR, |
| 446 | "status %d after wait for stat returned %d\n", |
| 447 | status, ret); |
| 448 | goto out_err; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | for (i = 0; i < len - 1;) { |
| 453 | burstcnt = tpm_tis_spi_get_burstcount(dev); |
| 454 | if (burstcnt < 0) |
| 455 | return burstcnt; |
| 456 | |
| 457 | size = min_t(int, len - i - 1, burstcnt); |
| 458 | ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality), |
| 459 | buf + i, size); |
| 460 | if (ret < 0) |
| 461 | goto out_err; |
| 462 | |
| 463 | i += size; |
| 464 | } |
| 465 | |
Roman Kapl | c84dd7f | 2019-05-16 18:32:48 +0200 | [diff] [blame] | 466 | ret = tpm_tis_spi_valid_status(dev, &status); |
Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 467 | if (ret) |
| 468 | goto out_err; |
| 469 | |
| 470 | if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 471 | ret = -EIO; |
| 472 | goto out_err; |
| 473 | } |
| 474 | |
| 475 | ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality), |
| 476 | buf + len - 1, 1); |
| 477 | if (ret) |
| 478 | goto out_err; |
| 479 | |
Roman Kapl | c84dd7f | 2019-05-16 18:32:48 +0200 | [diff] [blame] | 480 | ret = tpm_tis_spi_valid_status(dev, &status); |
Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 481 | if (ret) |
| 482 | goto out_err; |
| 483 | |
| 484 | if ((status & TPM_STS_DATA_EXPECT) != 0) { |
| 485 | ret = -EIO; |
| 486 | goto out_err; |
| 487 | } |
| 488 | |
| 489 | data = TPM_STS_GO; |
| 490 | ret = tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1); |
| 491 | if (ret) |
| 492 | goto out_err; |
| 493 | |
| 494 | return len; |
| 495 | |
| 496 | out_err: |
| 497 | tpm_tis_spi_cancel(dev); |
| 498 | tpm_tis_spi_release_locality(dev, chip->locality, false); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | static int tpm_tis_spi_cleanup(struct udevice *dev) |
| 504 | { |
| 505 | struct tpm_chip *chip = dev_get_priv(dev); |
| 506 | |
| 507 | tpm_tis_spi_cancel(dev); |
| 508 | /* |
| 509 | * The TPM needs some time to clean up here, |
| 510 | * so we sleep rather than keeping the bus busy |
| 511 | */ |
| 512 | mdelay(2); |
| 513 | tpm_tis_spi_release_locality(dev, chip->locality, false); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int tpm_tis_spi_open(struct udevice *dev) |
| 519 | { |
| 520 | struct tpm_chip *chip = dev_get_priv(dev); |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 521 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 522 | |
| 523 | if (chip->is_open) |
| 524 | return -EBUSY; |
| 525 | |
| 526 | chip->is_open = 1; |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int tpm_tis_spi_close(struct udevice *dev) |
| 532 | { |
| 533 | struct tpm_chip *chip = dev_get_priv(dev); |
| 534 | |
| 535 | if (chip->is_open) { |
| 536 | tpm_tis_spi_release_locality(dev, chip->locality, true); |
| 537 | chip->is_open = 0; |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size) |
| 544 | { |
| 545 | struct tpm_chip *chip = dev_get_priv(dev); |
| 546 | |
| 547 | if (size < 80) |
| 548 | return -ENOSPC; |
| 549 | |
| 550 | return snprintf(buf, size, |
| 551 | "%s v2.0: VendorID 0x%04x, DeviceID 0x%04x, RevisionID 0x%02x [%s]", |
| 552 | dev->name, chip->vend_dev & 0xFFFF, |
| 553 | chip->vend_dev >> 16, chip->rid, |
| 554 | (chip->is_open ? "open" : "closed")); |
| 555 | } |
| 556 | |
| 557 | static int tpm_tis_wait_init(struct udevice *dev, int loc) |
| 558 | { |
| 559 | struct tpm_chip *chip = dev_get_priv(dev); |
| 560 | unsigned long start, stop; |
| 561 | u8 status; |
| 562 | int ret; |
| 563 | |
| 564 | start = get_timer(0); |
| 565 | stop = chip->timeout_b; |
| 566 | do { |
| 567 | mdelay(TPM_TIMEOUT_MS); |
| 568 | |
| 569 | ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &status, 1); |
| 570 | if (ret) |
| 571 | break; |
| 572 | |
| 573 | if (status & TPM_ACCESS_VALID) |
| 574 | return 0; |
| 575 | } while (get_timer(start) < stop); |
| 576 | |
| 577 | return -EIO; |
| 578 | } |
| 579 | |
| 580 | static int tpm_tis_spi_probe(struct udevice *dev) |
| 581 | { |
| 582 | struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev); |
| 583 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
| 584 | struct tpm_chip *chip = dev_get_priv(dev); |
| 585 | int ret; |
| 586 | |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 587 | /* Use the TPM v2 stack */ |
| 588 | priv->version = TPM_V2; |
| 589 | |
Miquel Raynal | 68f34a2 | 2018-05-16 08:59:16 +0200 | [diff] [blame] | 590 | if (IS_ENABLED(CONFIG_DM_GPIO)) { |
| 591 | struct gpio_desc reset_gpio; |
| 592 | |
| 593 | ret = gpio_request_by_name(dev, "gpio-reset", 0, |
| 594 | &reset_gpio, GPIOD_IS_OUT); |
| 595 | if (ret) { |
| 596 | log(LOGC_NONE, LOGL_NOTICE, "%s: missing reset GPIO\n", |
| 597 | __func__); |
| 598 | } else { |
Miquel Raynal | 68f34a2 | 2018-05-16 08:59:16 +0200 | [diff] [blame] | 599 | dm_gpio_set_value(&reset_gpio, 1); |
Kayla Theil | 7ef21ad | 2019-09-06 12:40:45 +0200 | [diff] [blame] | 600 | mdelay(1); |
| 601 | dm_gpio_set_value(&reset_gpio, 0); |
Miquel Raynal | 68f34a2 | 2018-05-16 08:59:16 +0200 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Miquel Raynal | c1f6d6c | 2018-05-15 11:57:21 +0200 | [diff] [blame] | 605 | /* Ensure a minimum amount of time elapsed since reset of the TPM */ |
| 606 | mdelay(drv_data->time_before_first_cmd_ms); |
| 607 | |
| 608 | chip->locality = 0; |
| 609 | chip->timeout_a = TIS_SHORT_TIMEOUT_MS; |
| 610 | chip->timeout_b = TIS_LONG_TIMEOUT_MS; |
| 611 | chip->timeout_c = TIS_SHORT_TIMEOUT_MS; |
| 612 | chip->timeout_d = TIS_SHORT_TIMEOUT_MS; |
| 613 | priv->pcr_count = drv_data->pcr_count; |
| 614 | priv->pcr_select_min = drv_data->pcr_select_min; |
| 615 | |
| 616 | ret = tpm_tis_wait_init(dev, chip->locality); |
| 617 | if (ret) { |
| 618 | log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__); |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | ret = tpm_tis_spi_request_locality(dev, chip->locality); |
| 623 | if (ret) { |
| 624 | log(LOGC_NONE, LOGL_ERR, "%s: could not request locality %d\n", |
| 625 | __func__, chip->locality); |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | ret = tpm_tis_spi_read32(dev, TPM_DID_VID(chip->locality), |
| 630 | &chip->vend_dev); |
| 631 | if (ret) { |
| 632 | log(LOGC_NONE, LOGL_ERR, |
| 633 | "%s: could not retrieve VendorID/DeviceID\n", __func__); |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | ret = tpm_tis_spi_read(dev, TPM_RID(chip->locality), &chip->rid, 1); |
| 638 | if (ret) { |
| 639 | log(LOGC_NONE, LOGL_ERR, "%s: could not retrieve RevisionID\n", |
| 640 | __func__); |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | log(LOGC_NONE, LOGL_ERR, |
| 645 | "SPI TPMv2.0 found (vid:%04x, did:%04x, rid:%02x)\n", |
| 646 | chip->vend_dev & 0xFFFF, chip->vend_dev >> 16, chip->rid); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int tpm_tis_spi_remove(struct udevice *dev) |
| 652 | { |
| 653 | struct tpm_chip *chip = dev_get_priv(dev); |
| 654 | |
| 655 | tpm_tis_spi_release_locality(dev, chip->locality, true); |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static const struct tpm_ops tpm_tis_spi_ops = { |
| 661 | .open = tpm_tis_spi_open, |
| 662 | .close = tpm_tis_spi_close, |
| 663 | .get_desc = tpm_tis_get_desc, |
| 664 | .send = tpm_tis_spi_send, |
| 665 | .recv = tpm_tis_spi_recv, |
| 666 | .cleanup = tpm_tis_spi_cleanup, |
| 667 | }; |
| 668 | |
| 669 | static const struct tpm_tis_chip_data tpm_tis_std_chip_data = { |
| 670 | .pcr_count = 24, |
| 671 | .pcr_select_min = 3, |
| 672 | .time_before_first_cmd_ms = 30, |
| 673 | }; |
| 674 | |
| 675 | static const struct udevice_id tpm_tis_spi_ids[] = { |
| 676 | { |
| 677 | .compatible = "tis,tpm2-spi", |
| 678 | .data = (ulong)&tpm_tis_std_chip_data, |
| 679 | }, |
| 680 | { } |
| 681 | }; |
| 682 | |
| 683 | U_BOOT_DRIVER(tpm_tis_spi) = { |
| 684 | .name = "tpm_tis_spi", |
| 685 | .id = UCLASS_TPM, |
| 686 | .of_match = tpm_tis_spi_ids, |
| 687 | .ops = &tpm_tis_spi_ops, |
| 688 | .probe = tpm_tis_spi_probe, |
| 689 | .remove = tpm_tis_spi_remove, |
| 690 | .priv_auto_alloc_size = sizeof(struct tpm_chip), |
| 691 | }; |