1 // SPDX-License-Identifier: GPL-2.0
3 * Cr50 / H1 TPM support
5 * Copyright 2018 Google LLC
8 #define LOG_CATEGORY UCLASS_TPM
17 #include <acpi/acpigen.h>
18 #include <acpi/acpi_device.h>
21 #include <asm/arch/iomap.h>
22 #include <asm/arch/pm.h>
23 #include <linux/delay.h>
27 TIMEOUT_INIT_MS = 30000, /* Very long timeout for TPM init */
28 TIMEOUT_LONG_US = 2 * 1000 * 1000,
29 TIMEOUT_SHORT_US = 2 * 1000,
30 TIMEOUT_NO_IRQ_US = 20 * 1000,
31 TIMEOUT_IRQ_US = 100 * 1000,
35 CR50_DID_VID = 0x00281ae0L
39 CR50_MAX_BUF_SIZE = 63,
43 * struct cr50_priv - Private driver data
45 * @ready_gpio: GPIO to use to check if the TPM is ready
46 * @irq: IRQ to use check if the TPM is ready (has priority over @ready_gpio)
47 * @locality: Currenttly claimed locality (-1 if none)
48 * @vendor: vendor: Vendor ID for TPM
49 * @use_irq: true to use @irq, false to use @ready if available
52 struct gpio_desc ready_gpio;
59 /* Wait for interrupt to indicate TPM is ready */
60 static int cr50_i2c_wait_tpm_ready(struct udevice *dev)
62 struct cr50_priv *priv = dev_get_priv(dev);
66 if (!priv->use_irq && !dm_gpio_is_valid(&priv->ready_gpio)) {
67 /* Fixed delay if interrupt not supported */
68 udelay(TIMEOUT_NO_IRQ_US);
72 base = timer_get_us();
73 timeout = base + TIMEOUT_IRQ_US;
76 while (priv->use_irq ? !irq_read_and_clear(&priv->irq) :
77 !dm_gpio_get_value(&priv->ready_gpio)) {
79 if ((int)(timer_get_us() - timeout) >= 0) {
80 log_warning("Timeout\n");
81 /* Use this instead of the -ETIMEDOUT used by i2c */
85 log_debug("i=%d\n", i);
90 /* Clear pending interrupts */
91 static void cr50_i2c_clear_tpm_irq(struct udevice *dev)
93 struct cr50_priv *priv = dev_get_priv(dev);
96 irq_read_and_clear(&priv->irq);
100 * cr50_i2c_read() - read from TPM register
102 * @dev: TPM chip information
103 * @addr: register address to read from
104 * @buffer: provided by caller
105 * @len: number of bytes to read
107 * 1) send register address byte 'addr' to the TPM
108 * 2) wait for TPM to indicate it is ready
109 * 3) read 'len' bytes of TPM response into the provided 'buffer'
111 * Return 0 on success. -ve on error
113 static int cr50_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
118 /* Clear interrupt before starting transaction */
119 cr50_i2c_clear_tpm_irq(dev);
121 /* Send the register address byte to the TPM */
122 ret = dm_i2c_write(dev, 0, &addr, 1);
124 log_err("Address write failed (err=%d)\n", ret);
128 /* Wait for TPM to be ready with response data */
129 ret = cr50_i2c_wait_tpm_ready(dev);
133 /* Read response data frrom the TPM */
134 ret = dm_i2c_read(dev, 0, buffer, len);
136 log_err("Read response failed (err=%d)\n", ret);
144 * cr50_i2c_write() - write to TPM register
146 * @dev: TPM chip information
147 * @addr: register address to write to
148 * @buffer: data to write
149 * @len: number of bytes to write
151 * 1) prepend the provided address to the provided data
152 * 2) send the address+data to the TPM
153 * 3) wait for TPM to indicate it is done writing
155 * Returns -1 on error, 0 on success.
157 static int cr50_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
163 if (len > CR50_MAX_BUF_SIZE) {
164 log_err("Length %zd is too large\n", len);
168 /* Prepend the 'register address' to the buffer */
170 memcpy(buf + 1, buffer, len);
172 /* Clear interrupt before starting transaction */
173 cr50_i2c_clear_tpm_irq(dev);
175 /* Send write request buffer with address */
176 ret = dm_i2c_write(dev, 0, buf, len + 1);
178 log_err("Error writing to TPM (err=%d)\n", ret);
182 /* Wait for TPM to be ready */
183 return cr50_i2c_wait_tpm_ready(dev);
186 static inline u8 tpm_access(int locality)
190 return 0x0 | (locality << 4);
193 static inline u8 tpm_sts(int locality)
197 return 0x1 | (locality << 4);
200 static inline u8 tpm_data_fifo(int locality)
204 return 0x5 | (locality << 4);
207 static inline u8 tpm_did_vid(int locality)
211 return 0x6 | (locality << 4);
214 static int release_locality(struct udevice *dev, int force)
216 struct cr50_priv *priv = dev_get_priv(dev);
217 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
218 u8 addr = tpm_access(priv->locality);
222 ret = cr50_i2c_read(dev, addr, &buf, 1);
226 if (force || (buf & mask) == mask) {
227 buf = TPM_ACCESS_ACTIVE_LOCALITY;
228 cr50_i2c_write(dev, addr, &buf, 1);
236 /* cr50 requires all 4 bytes of status register to be read */
237 static int cr50_i2c_status(struct udevice *dev)
239 struct cr50_priv *priv = dev_get_priv(dev);
243 ret = cr50_i2c_read(dev, tpm_sts(priv->locality), buf, sizeof(buf));
245 log_warning("%s: Failed to read status\n", __func__);
252 /* cr50 requires all 4 bytes of status register to be written */
253 static int cr50_i2c_ready(struct udevice *dev)
255 struct cr50_priv *priv = dev_get_priv(dev);
256 u8 buf[4] = { TPM_STS_COMMAND_READY };
259 ret = cr50_i2c_write(dev, tpm_sts(priv->locality), buf, sizeof(buf));
263 udelay(TIMEOUT_SHORT_US);
268 static int cr50_i2c_wait_burststs(struct udevice *dev, u8 mask,
269 size_t *burst, int *status)
271 struct cr50_priv *priv = dev_get_priv(dev);
276 * cr50 uses bytes 3:2 of status register for burst count and all 4
279 timeout = timer_get_us() + TIMEOUT_LONG_US;
280 while (timer_get_us() < timeout) {
281 if (cr50_i2c_read(dev, tpm_sts(priv->locality),
282 (u8 *)&buf, sizeof(buf)) < 0) {
283 udelay(TIMEOUT_SHORT_US);
287 *status = buf & 0xff;
288 *burst = le16_to_cpu((buf >> 8) & 0xffff);
290 if ((*status & mask) == mask &&
291 *burst > 0 && *burst <= CR50_MAX_BUF_SIZE)
294 udelay(TIMEOUT_SHORT_US);
297 log_warning("Timeout reading burst and status\n");
302 static int cr50_i2c_recv(struct udevice *dev, u8 *buf, size_t buf_len)
304 struct cr50_priv *priv = dev_get_priv(dev);
305 size_t burstcnt, expected, current, len;
306 u8 addr = tpm_data_fifo(priv->locality);
307 u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
312 log_debug("%s: len=%x\n", __func__, buf_len);
313 if (buf_len < TPM_HEADER_SIZE)
316 ret = cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status);
318 log_warning("First chunk not available\n");
322 /* Read first chunk of burstcnt bytes */
323 if (cr50_i2c_read(dev, addr, buf, burstcnt) < 0) {
324 log_warning("Read failed\n");
328 /* Determine expected data in the return buffer */
329 memcpy(&expected_buf, buf + TPM_CMD_COUNT_OFFSET, sizeof(expected_buf));
330 expected = be32_to_cpu(expected_buf);
331 if (expected > buf_len) {
332 log_warning("Too much data: %zu > %zu\n", expected, buf_len);
336 /* Now read the rest of the data */
338 while (current < expected) {
339 /* Read updated burst count and check status */
340 if (cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status) < 0) {
341 log_warning("- burst failure1\n");
345 len = min(burstcnt, expected - current);
346 if (cr50_i2c_read(dev, addr, buf + current, len) != 0) {
347 log_warning("Read failed\n");
354 if (cr50_i2c_wait_burststs(dev, TPM_STS_VALID, &burstcnt,
356 log_warning("- burst failure2\n");
359 if (status & TPM_STS_DATA_AVAIL) {
360 log_warning("Data still available\n");
367 /* Abort current transaction if still pending */
368 ret = cr50_i2c_status(dev);
371 if (ret & TPM_STS_COMMAND_READY) {
372 ret = cr50_i2c_ready(dev);
380 static int cr50_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
382 struct cr50_priv *priv = dev_get_priv(dev);
384 size_t burstcnt, limit, sent = 0;
385 u8 tpm_go[4] = { TPM_STS_GO };
389 log_debug("%s: len=%x\n", __func__, len);
390 timeout = timer_get_us() + TIMEOUT_LONG_US;
392 ret = cr50_i2c_status(dev);
395 if (ret & TPM_STS_COMMAND_READY)
398 if (timer_get_us() > timeout)
401 ret = cr50_i2c_ready(dev);
407 u8 mask = TPM_STS_VALID;
409 /* Wait for data if this is not the first chunk */
411 mask |= TPM_STS_DATA_EXPECT;
413 if (cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status) < 0)
417 * Use burstcnt - 1 to account for the address byte
418 * that is inserted by cr50_i2c_write()
420 limit = min(burstcnt - 1, len);
421 if (cr50_i2c_write(dev, tpm_data_fifo(priv->locality),
422 &buf[sent], limit) != 0) {
423 log_warning("Write failed\n");
431 /* Ensure TPM is not expecting more data */
432 if (cr50_i2c_wait_burststs(dev, TPM_STS_VALID, &burstcnt, &status) < 0)
434 if (status & TPM_STS_DATA_EXPECT) {
435 log_warning("Data still expected\n");
439 /* Start the TPM command */
440 ret = cr50_i2c_write(dev, tpm_sts(priv->locality), tpm_go,
443 log_warning("Start command failed\n");
450 /* Abort current transaction if still pending */
451 ret = cr50_i2c_status(dev);
453 if (ret < 0 || (ret & TPM_STS_COMMAND_READY)) {
454 ret = cr50_i2c_ready(dev);
463 * process_reset() - Wait for the Cr50 to reset
465 * Cr50 processes reset requests asynchronously and conceivably could be busy
466 * executing a long command and not reacting to the reset pulse for a while.
468 * This function will make sure that the AP does not proceed with boot until
469 * TPM finished reset processing.
472 * @return 0 if OK, -EPERM if locality could not be taken
474 static int process_reset(struct udevice *dev)
481 * Locality is released by TPM reset.
483 * If locality is taken at this point, this could be due to the fact
484 * that the TPM is performing a long operation and has not processed
485 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
486 * it releases locality when reset is processed.
488 start = get_timer(0);
490 const u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
493 ret = cr50_i2c_read(dev, tpm_access(loc),
494 &access, sizeof(access));
495 if (ret || ((access & mask) == mask)) {
497 * Don't bombard the chip with traffic; let it keep
498 * processing the command.
504 log_debug("TPM ready after %ld ms\n", get_timer(start));
507 } while (get_timer(start) < TIMEOUT_INIT_MS);
509 log_err("TPM failed to reset after %ld ms, status: %#x\n",
510 get_timer(start), access);
516 * Locality could be already claimed (if this is a later U-Boot phase and the
517 * read-only U-Boot did not release it), or not yet claimed, if this is TPL or
518 * the older read-only U-Boot did release it.
520 static int claim_locality(struct udevice *dev, int loc)
522 const u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
523 struct cr50_priv *priv = dev_get_priv(dev);
527 ret = cr50_i2c_read(dev, tpm_access(loc), &access, sizeof(access));
529 return log_msg_ret("read1", ret);
531 if ((access & mask) == mask) {
532 log_warning("Locality already claimed\n");
536 access = TPM_ACCESS_REQUEST_USE;
537 ret = cr50_i2c_write(dev, tpm_access(loc), &access, sizeof(access));
539 return log_msg_ret("write", ret);
541 ret = cr50_i2c_read(dev, tpm_access(loc), &access, sizeof(access));
543 return log_msg_ret("read2", ret);
545 if ((access & mask) != mask) {
546 log_err("Failed to claim locality\n");
549 log_debug("Claimed locality %d\n", loc);
550 priv->locality = loc;
555 static int cr50_i2c_get_desc(struct udevice *dev, char *buf, int size)
557 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
558 struct cr50_priv *priv = dev_get_priv(dev);
561 len = snprintf(buf, size, "cr50 TPM 2.0 (i2c %02x id %x), ",
562 chip->chip_addr, priv->vendor >> 16);
564 len += snprintf(buf + len, size - len, "irq=%s/%ld",
565 priv->irq.dev->name, priv->irq.id);
566 } else if (dm_gpio_is_valid(&priv->ready_gpio)) {
567 len += snprintf(buf + len, size - len, "gpio=%s/%u",
568 priv->ready_gpio.dev->name,
569 priv->ready_gpio.offset);
571 len += snprintf(buf + len, size - len, "delay=%d",
578 static int cr50_i2c_open(struct udevice *dev)
583 ret = process_reset(dev);
585 return log_msg_ret("reset", ret);
587 ret = claim_locality(dev, 0);
589 return log_msg_ret("claim", ret);
591 cr50_i2c_get_desc(dev, buf, sizeof(buf));
592 log_debug("%s\n", buf);
597 static int cr50_i2c_cleanup(struct udevice *dev)
599 struct cr50_priv *priv = dev_get_priv(dev);
601 log_debug("cleanup %d\n", priv->locality);
602 if (priv->locality != -1)
603 release_locality(dev, 1);
608 static int cr50_acpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
610 char scope[ACPI_PATH_MAX];
611 char name[ACPI_NAME_MAX];
615 ret = acpi_device_scope(dev, scope, sizeof(scope));
617 return log_msg_ret("scope", ret);
618 ret = acpi_get_name(dev, name);
620 return log_msg_ret("name", ret);
622 hid = dev_read_string(dev, "acpi,hid");
624 return log_msg_ret("hid", ret);
627 acpigen_write_scope(ctx, scope);
628 acpigen_write_device(ctx, name);
629 acpigen_write_name_string(ctx, "_HID", hid);
630 acpigen_write_name_integer(ctx, "_UID",
631 dev_read_u32_default(dev, "acpi,uid", 0));
632 acpigen_write_name_string(ctx, "_DDN",
633 dev_read_string(dev, "acpi,ddn"));
634 acpigen_write_sta(ctx, acpi_device_status(dev));
637 acpigen_write_name(ctx, "_CRS");
638 acpigen_write_resourcetemplate_header(ctx);
639 ret = acpi_device_write_i2c_dev(ctx, dev);
641 return log_msg_ret("i2c", ret);
642 ret = acpi_device_write_interrupt_or_gpio(ctx, (struct udevice *)dev,
645 return log_msg_ret("irq_gpio", ret);
647 acpigen_write_resourcetemplate_footer(ctx);
649 acpigen_pop_len(ctx); /* Device */
650 acpigen_pop_len(ctx); /* Scope */
657 SHORT_TIMEOUT_MS = 750,
658 LONG_TIMEOUT_MS = 2000,
661 static int cr50_i2c_of_to_plat(struct udevice *dev)
663 struct tpm_chip_priv *upriv = dev_get_uclass_priv(dev);
664 struct cr50_priv *priv = dev_get_priv(dev);
668 upriv->version = TPM_V2;
669 upriv->duration_ms[TPM_SHORT] = SHORT_TIMEOUT_MS;
670 upriv->duration_ms[TPM_MEDIUM] = LONG_TIMEOUT_MS;
671 upriv->duration_ms[TPM_LONG] = LONG_TIMEOUT_MS;
672 upriv->retry_time_ms = TPM_TIMEOUT_MS;
674 upriv->pcr_count = 32;
675 upriv->pcr_select_min = 2;
677 /* Optional GPIO to track when cr50 is ready */
678 ret = irq_get_by_index(dev, 0, &irq);
681 priv->use_irq = true;
683 ret = gpio_request_by_name(dev, "ready-gpios", 0,
684 &priv->ready_gpio, GPIOD_IS_IN);
686 log_warning("Cr50 does not have an ready GPIO/interrupt (err=%d)\n",
694 static int cr50_i2c_probe(struct udevice *dev)
696 struct cr50_priv *priv = dev_get_priv(dev);
701 * 150ms should be enough to synchronise with the TPM even under the
702 * worst nested-reset-request conditions. In the vast majority of cases
703 * there will be no wait at all.
705 start = get_timer(0);
706 while (get_timer(start) < 150) {
709 /* Exit once DID and VID verified */
710 ret = cr50_i2c_read(dev, tpm_did_vid(0), (u8 *)&vendor, 4);
711 if (!ret && vendor == CR50_DID_VID)
714 /* TPM might be resetting; let's retry in a bit */
717 if (vendor != CR50_DID_VID) {
718 log_warning("DID_VID %08x not recognised\n", vendor);
719 return log_msg_ret("vendor-id", -EXDEV);
721 priv->vendor = vendor;
723 log_debug("Cr50 ready\n");
728 struct acpi_ops cr50_acpi_ops = {
729 .fill_ssdt = cr50_acpi_fill_ssdt,
732 static const struct tpm_ops cr50_i2c_ops = {
733 .open = cr50_i2c_open,
734 .get_desc = cr50_i2c_get_desc,
735 .send = cr50_i2c_send,
736 .recv = cr50_i2c_recv,
737 .cleanup = cr50_i2c_cleanup,
740 static const struct udevice_id cr50_i2c_ids[] = {
741 { .compatible = "google,cr50" },
745 U_BOOT_DRIVER(google_cr50) = {
746 .name = "google_cr50",
748 .of_match = cr50_i2c_ids,
749 .ops = &cr50_i2c_ops,
750 .of_to_plat = cr50_i2c_of_to_plat,
751 .probe = cr50_i2c_probe,
752 .remove = cr50_i2c_cleanup,
753 .priv_auto = sizeof(struct cr50_priv),
754 ACPI_OPS_PTR(&cr50_acpi_ops)
755 .flags = DM_FLAG_OS_PREPARE,