1 // SPDX-License-Identifier: GPL-2.0
3 * Cr50 / H1 TPM support
5 * Copyright 2018 Google LLC
8 #define LOG_CATEGORY UCLASS_TPM
18 #include <asm/arch/iomap.h>
19 #include <asm/arch/pm.h>
22 TIMEOUT_INIT_MS = 30000, /* Very long timeout for TPM init */
23 TIMEOUT_LONG_US = 2 * 1000 * 1000,
24 TIMEOUT_SHORT_US = 2 * 1000,
25 TIMEOUT_NO_IRQ_US = 20 * 1000,
26 TIMEOUT_IRQ_US = 100 * 1000,
30 CR50_DID_VID = 0x00281ae0L
34 CR50_MAX_BUF_SIZE = 63,
38 * struct cr50_priv - Private driver data
40 * @ready_gpio: GPIO to use to check if the TPM is ready
41 * @irq: IRQ to use check if the TPM is ready (has priority over @ready_gpio)
42 * @locality: Currenttly claimed locality (-1 if none)
43 * @vendor: vendor: Vendor ID for TPM
44 * @use_irq: true to use @irq, false to use @ready if available
47 struct gpio_desc ready_gpio;
54 /* Wait for interrupt to indicate TPM is ready */
55 static int cr50_i2c_wait_tpm_ready(struct udevice *dev)
57 struct cr50_priv *priv = dev_get_priv(dev);
61 if (!priv->use_irq && !dm_gpio_is_valid(&priv->ready_gpio)) {
62 /* Fixed delay if interrupt not supported */
63 udelay(TIMEOUT_NO_IRQ_US);
67 base = timer_get_us();
68 timeout = base + TIMEOUT_IRQ_US;
71 while (priv->use_irq ? !irq_read_and_clear(&priv->irq) :
72 !dm_gpio_get_value(&priv->ready_gpio)) {
74 if ((int)(timer_get_us() - timeout) >= 0) {
75 log_warning("Timeout\n");
76 /* Use this instead of the -ETIMEDOUT used by i2c */
80 log_debug("i=%d\n", i);
85 /* Clear pending interrupts */
86 static void cr50_i2c_clear_tpm_irq(struct udevice *dev)
88 struct cr50_priv *priv = dev_get_priv(dev);
91 irq_read_and_clear(&priv->irq);
95 * cr50_i2c_read() - read from TPM register
97 * @dev: TPM chip information
98 * @addr: register address to read from
99 * @buffer: provided by caller
100 * @len: number of bytes to read
102 * 1) send register address byte 'addr' to the TPM
103 * 2) wait for TPM to indicate it is ready
104 * 3) read 'len' bytes of TPM response into the provided 'buffer'
106 * Return 0 on success. -ve on error
108 static int cr50_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
113 /* Clear interrupt before starting transaction */
114 cr50_i2c_clear_tpm_irq(dev);
116 /* Send the register address byte to the TPM */
117 ret = dm_i2c_write(dev, 0, &addr, 1);
119 log_err("Address write failed (err=%d)\n", ret);
123 /* Wait for TPM to be ready with response data */
124 ret = cr50_i2c_wait_tpm_ready(dev);
128 /* Read response data frrom the TPM */
129 ret = dm_i2c_read(dev, 0, buffer, len);
131 log_err("Read response failed (err=%d)\n", ret);
139 * cr50_i2c_write() - write to TPM register
141 * @dev: TPM chip information
142 * @addr: register address to write to
143 * @buffer: data to write
144 * @len: number of bytes to write
146 * 1) prepend the provided address to the provided data
147 * 2) send the address+data to the TPM
148 * 3) wait for TPM to indicate it is done writing
150 * Returns -1 on error, 0 on success.
152 static int cr50_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
158 if (len > CR50_MAX_BUF_SIZE) {
159 log_err("Length %zd is too large\n", len);
163 /* Prepend the 'register address' to the buffer */
165 memcpy(buf + 1, buffer, len);
167 /* Clear interrupt before starting transaction */
168 cr50_i2c_clear_tpm_irq(dev);
170 /* Send write request buffer with address */
171 ret = dm_i2c_write(dev, 0, buf, len + 1);
173 log_err("Error writing to TPM (err=%d)\n", ret);
177 /* Wait for TPM to be ready */
178 return cr50_i2c_wait_tpm_ready(dev);
181 static inline u8 tpm_access(u8 locality)
183 return 0x0 | (locality << 4);
186 static inline u8 tpm_sts(u8 locality)
188 return 0x1 | (locality << 4);
191 static inline u8 tpm_data_fifo(u8 locality)
193 return 0x5 | (locality << 4);
196 static inline u8 tpm_did_vid(u8 locality)
198 return 0x6 | (locality << 4);
201 static int release_locality(struct udevice *dev, int force)
203 struct cr50_priv *priv = dev_get_priv(dev);
204 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
205 u8 addr = tpm_access(priv->locality);
209 ret = cr50_i2c_read(dev, addr, &buf, 1);
213 if (force || (buf & mask) == mask) {
214 buf = TPM_ACCESS_ACTIVE_LOCALITY;
215 cr50_i2c_write(dev, addr, &buf, 1);
223 /* cr50 requires all 4 bytes of status register to be read */
224 static int cr50_i2c_status(struct udevice *dev)
226 struct cr50_priv *priv = dev_get_priv(dev);
230 ret = cr50_i2c_read(dev, tpm_sts(priv->locality), buf, sizeof(buf));
232 log_warning("%s: Failed to read status\n", __func__);
239 /* cr50 requires all 4 bytes of status register to be written */
240 static int cr50_i2c_ready(struct udevice *dev)
242 struct cr50_priv *priv = dev_get_priv(dev);
243 u8 buf[4] = { TPM_STS_COMMAND_READY };
246 ret = cr50_i2c_write(dev, tpm_sts(priv->locality), buf, sizeof(buf));
250 udelay(TIMEOUT_SHORT_US);
255 static int cr50_i2c_wait_burststs(struct udevice *dev, u8 mask,
256 size_t *burst, int *status)
258 struct cr50_priv *priv = dev_get_priv(dev);
263 * cr50 uses bytes 3:2 of status register for burst count and all 4
266 timeout = timer_get_us() + TIMEOUT_LONG_US;
267 while (timer_get_us() < timeout) {
268 if (cr50_i2c_read(dev, tpm_sts(priv->locality),
269 (u8 *)&buf, sizeof(buf)) < 0) {
270 udelay(TIMEOUT_SHORT_US);
274 *status = buf & 0xff;
275 *burst = le16_to_cpu((buf >> 8) & 0xffff);
277 if ((*status & mask) == mask &&
278 *burst > 0 && *burst <= CR50_MAX_BUF_SIZE)
281 udelay(TIMEOUT_SHORT_US);
284 log_warning("Timeout reading burst and status\n");
289 static int cr50_i2c_recv(struct udevice *dev, u8 *buf, size_t buf_len)
291 struct cr50_priv *priv = dev_get_priv(dev);
292 size_t burstcnt, expected, current, len;
293 u8 addr = tpm_data_fifo(priv->locality);
294 u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
299 log_debug("%s: len=%x\n", __func__, buf_len);
300 if (buf_len < TPM_HEADER_SIZE)
303 ret = cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status);
305 log_warning("First chunk not available\n");
309 /* Read first chunk of burstcnt bytes */
310 if (cr50_i2c_read(dev, addr, buf, burstcnt) < 0) {
311 log_warning("Read failed\n");
315 /* Determine expected data in the return buffer */
316 memcpy(&expected_buf, buf + TPM_CMD_COUNT_OFFSET, sizeof(expected_buf));
317 expected = be32_to_cpu(expected_buf);
318 if (expected > buf_len) {
319 log_warning("Too much data: %zu > %zu\n", expected, buf_len);
323 /* Now read the rest of the data */
325 while (current < expected) {
326 /* Read updated burst count and check status */
327 if (cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status) < 0) {
328 log_warning("- burst failure1\n");
332 len = min(burstcnt, expected - current);
333 if (cr50_i2c_read(dev, addr, buf + current, len) != 0) {
334 log_warning("Read failed\n");
341 if (cr50_i2c_wait_burststs(dev, TPM_STS_VALID, &burstcnt,
343 log_warning("- burst failure2\n");
346 if (status & TPM_STS_DATA_AVAIL) {
347 log_warning("Data still available\n");
354 /* Abort current transaction if still pending */
355 ret = cr50_i2c_status(dev);
358 if (ret & TPM_STS_COMMAND_READY) {
359 ret = cr50_i2c_ready(dev);
367 static int cr50_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
369 struct cr50_priv *priv = dev_get_priv(dev);
372 size_t burstcnt, limit, sent = 0;
373 u8 tpm_go[4] = { TPM_STS_GO };
377 log_debug("%s: len=%x\n", __func__, len);
378 timeout = timer_get_us() + TIMEOUT_LONG_US;
380 ret = cr50_i2c_status(dev);
383 if (ret & TPM_STS_COMMAND_READY)
386 if (timer_get_us() > timeout)
389 ret = cr50_i2c_ready(dev);
395 u8 mask = TPM_STS_VALID;
397 /* Wait for data if this is not the first chunk */
399 mask |= TPM_STS_DATA_EXPECT;
401 if (cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status) < 0)
405 * Use burstcnt - 1 to account for the address byte
406 * that is inserted by cr50_i2c_write()
408 limit = min(burstcnt - 1, len);
409 if (cr50_i2c_write(dev, tpm_data_fifo(priv->locality),
410 &buf[sent], limit) != 0) {
411 log_warning("Write failed\n");
419 /* Ensure TPM is not expecting more data */
420 if (cr50_i2c_wait_burststs(dev, TPM_STS_VALID, &burstcnt, &status) < 0)
422 if (status & TPM_STS_DATA_EXPECT) {
423 log_warning("Data still expected\n");
427 /* Start the TPM command */
428 ret = cr50_i2c_write(dev, tpm_sts(priv->locality), tpm_go,
431 log_warning("Start command failed\n");
438 /* Abort current transaction if still pending */
439 ret = cr50_i2c_status(dev);
441 if (ret < 0 || (ret & TPM_STS_COMMAND_READY)) {
442 ret = cr50_i2c_ready(dev);
451 * process_reset() - Wait for the Cr50 to reset
453 * Cr50 processes reset requests asynchronously and conceivably could be busy
454 * executing a long command and not reacting to the reset pulse for a while.
456 * This function will make sure that the AP does not proceed with boot until
457 * TPM finished reset processing.
460 * @return 0 if OK, -EPERM if locality could not be taken
462 static int process_reset(struct udevice *dev)
469 * Locality is released by TPM reset.
471 * If locality is taken at this point, this could be due to the fact
472 * that the TPM is performing a long operation and has not processed
473 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
474 * it releases locality when reset is processed.
476 start = get_timer(0);
478 const u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
481 ret = cr50_i2c_read(dev, tpm_access(loc),
482 &access, sizeof(access));
483 if (ret || ((access & mask) == mask)) {
485 * Don't bombard the chip with traffic; let it keep
486 * processing the command.
492 log_warning("TPM ready after %ld ms\n", get_timer(start));
495 } while (get_timer(start) < TIMEOUT_INIT_MS);
497 log_warning("TPM failed to reset after %ld ms, status: %#x\n",
498 get_timer(start), access);
504 * Locality could be already claimed (if this is a later U-Boot phase and the
505 * read-only U-Boot did not release it), or not yet claimed, if this is TPL or
506 * the older read-only U-Boot did release it.
508 static int claim_locality(struct udevice *dev, int loc)
510 const u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
511 struct cr50_priv *priv = dev_get_priv(dev);
515 ret = cr50_i2c_read(dev, tpm_access(loc), &access, sizeof(access));
517 return log_msg_ret("read1", ret);
519 if ((access & mask) == mask) {
520 log_warning("Locality already claimed\n");
524 access = TPM_ACCESS_REQUEST_USE;
525 ret = cr50_i2c_write(dev, tpm_access(loc), &access, sizeof(access));
527 return log_msg_ret("write", ret);
529 ret = cr50_i2c_read(dev, tpm_access(loc), &access, sizeof(access));
531 return log_msg_ret("read2", ret);
533 if ((access & mask) != mask) {
534 log_err("Failed to claim locality\n");
537 log_info("Claimed locality %d\n", loc);
538 priv->locality = loc;
543 static int cr50_i2c_get_desc(struct udevice *dev, char *buf, int size)
545 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
546 struct cr50_priv *priv = dev_get_priv(dev);
548 return snprintf(buf, size, "cr50 TPM 2.0 (i2c %02x id %x) irq=%d",
549 chip->chip_addr, priv->vendor >> 16, priv->use_irq);
552 static int cr50_i2c_open(struct udevice *dev)
557 ret = process_reset(dev);
559 return log_msg_ret("reset", ret);
561 ret = claim_locality(dev, 0);
563 return log_msg_ret("claim", ret);
565 cr50_i2c_get_desc(dev, buf, sizeof(buf));
566 log_debug("%s\n", buf);
571 static int cr50_i2c_cleanup(struct udevice *dev)
573 struct cr50_priv *priv = dev_get_priv(dev);
575 printf("%s: cleanup %d\n", __func__, priv->locality);
576 if (priv->locality != -1)
577 release_locality(dev, 1);
584 SHORT_TIMEOUT_MS = 750,
585 LONG_TIMEOUT_MS = 2000,
588 static int cr50_i2c_ofdata_to_platdata(struct udevice *dev)
590 struct tpm_chip_priv *upriv = dev_get_uclass_priv(dev);
591 struct cr50_priv *priv = dev_get_priv(dev);
595 upriv->version = TPM_V2;
596 upriv->duration_ms[TPM_SHORT] = SHORT_TIMEOUT_MS;
597 upriv->duration_ms[TPM_MEDIUM] = LONG_TIMEOUT_MS;
598 upriv->duration_ms[TPM_LONG] = LONG_TIMEOUT_MS;
599 upriv->retry_time_ms = TPM_TIMEOUT_MS;
601 upriv->pcr_count = 32;
602 upriv->pcr_select_min = 2;
604 /* Optional GPIO to track when cr50 is ready */
605 ret = irq_get_by_index(dev, 0, &irq);
608 priv->use_irq = true;
610 ret = gpio_request_by_name(dev, "ready-gpios", 0,
611 &priv->ready_gpio, GPIOD_IS_IN);
613 log_warning("Cr50 does not have an ready GPIO/interrupt (err=%d)\n",
621 static int cr50_i2c_probe(struct udevice *dev)
623 struct cr50_priv *priv = dev_get_priv(dev);
628 * 150ms should be enough to synchronise with the TPM even under the
629 * worst nested-reset-request conditions. In the vast majority of cases
630 * there will be no wait at all.
632 start = get_timer(0);
633 while (get_timer(start) < 150) {
636 /* Exit once DID and VID verified */
637 ret = cr50_i2c_read(dev, tpm_did_vid(0), (u8 *)&vendor, 4);
638 if (!ret && vendor == CR50_DID_VID)
641 /* TPM might be resetting; let's retry in a bit */
644 if (vendor != CR50_DID_VID) {
645 log_debug("DID_VID %08x not recognised\n", vendor);
646 return log_msg_ret("vendor-id", -EXDEV);
648 priv->vendor = vendor;
654 static const struct tpm_ops cr50_i2c_ops = {
655 .open = cr50_i2c_open,
656 .get_desc = cr50_i2c_get_desc,
657 .send = cr50_i2c_send,
658 .recv = cr50_i2c_recv,
659 .cleanup = cr50_i2c_cleanup,
662 static const struct udevice_id cr50_i2c_ids[] = {
663 { .compatible = "google,cr50" },
667 U_BOOT_DRIVER(cr50_i2c) = {
670 .of_match = cr50_i2c_ids,
671 .ops = &cr50_i2c_ops,
672 .ofdata_to_platdata = cr50_i2c_ofdata_to_platdata,
673 .probe = cr50_i2c_probe,
674 .remove = cr50_i2c_cleanup,
675 .priv_auto_alloc_size = sizeof(struct cr50_priv),
676 .flags = DM_FLAG_OS_PREPARE,