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