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(u8 locality)
188 return 0x0 | (locality << 4);
191 static inline u8 tpm_sts(u8 locality)
193 return 0x1 | (locality << 4);
196 static inline u8 tpm_data_fifo(u8 locality)
198 return 0x5 | (locality << 4);
201 static inline u8 tpm_did_vid(u8 locality)
203 return 0x6 | (locality << 4);
206 static int release_locality(struct udevice *dev, int force)
208 struct cr50_priv *priv = dev_get_priv(dev);
209 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
210 u8 addr = tpm_access(priv->locality);
214 ret = cr50_i2c_read(dev, addr, &buf, 1);
218 if (force || (buf & mask) == mask) {
219 buf = TPM_ACCESS_ACTIVE_LOCALITY;
220 cr50_i2c_write(dev, addr, &buf, 1);
228 /* cr50 requires all 4 bytes of status register to be read */
229 static int cr50_i2c_status(struct udevice *dev)
231 struct cr50_priv *priv = dev_get_priv(dev);
235 ret = cr50_i2c_read(dev, tpm_sts(priv->locality), buf, sizeof(buf));
237 log_warning("%s: Failed to read status\n", __func__);
244 /* cr50 requires all 4 bytes of status register to be written */
245 static int cr50_i2c_ready(struct udevice *dev)
247 struct cr50_priv *priv = dev_get_priv(dev);
248 u8 buf[4] = { TPM_STS_COMMAND_READY };
251 ret = cr50_i2c_write(dev, tpm_sts(priv->locality), buf, sizeof(buf));
255 udelay(TIMEOUT_SHORT_US);
260 static int cr50_i2c_wait_burststs(struct udevice *dev, u8 mask,
261 size_t *burst, int *status)
263 struct cr50_priv *priv = dev_get_priv(dev);
268 * cr50 uses bytes 3:2 of status register for burst count and all 4
271 timeout = timer_get_us() + TIMEOUT_LONG_US;
272 while (timer_get_us() < timeout) {
273 if (cr50_i2c_read(dev, tpm_sts(priv->locality),
274 (u8 *)&buf, sizeof(buf)) < 0) {
275 udelay(TIMEOUT_SHORT_US);
279 *status = buf & 0xff;
280 *burst = le16_to_cpu((buf >> 8) & 0xffff);
282 if ((*status & mask) == mask &&
283 *burst > 0 && *burst <= CR50_MAX_BUF_SIZE)
286 udelay(TIMEOUT_SHORT_US);
289 log_warning("Timeout reading burst and status\n");
294 static int cr50_i2c_recv(struct udevice *dev, u8 *buf, size_t buf_len)
296 struct cr50_priv *priv = dev_get_priv(dev);
297 size_t burstcnt, expected, current, len;
298 u8 addr = tpm_data_fifo(priv->locality);
299 u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
304 log_debug("%s: len=%x\n", __func__, buf_len);
305 if (buf_len < TPM_HEADER_SIZE)
308 ret = cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status);
310 log_warning("First chunk not available\n");
314 /* Read first chunk of burstcnt bytes */
315 if (cr50_i2c_read(dev, addr, buf, burstcnt) < 0) {
316 log_warning("Read failed\n");
320 /* Determine expected data in the return buffer */
321 memcpy(&expected_buf, buf + TPM_CMD_COUNT_OFFSET, sizeof(expected_buf));
322 expected = be32_to_cpu(expected_buf);
323 if (expected > buf_len) {
324 log_warning("Too much data: %zu > %zu\n", expected, buf_len);
328 /* Now read the rest of the data */
330 while (current < expected) {
331 /* Read updated burst count and check status */
332 if (cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status) < 0) {
333 log_warning("- burst failure1\n");
337 len = min(burstcnt, expected - current);
338 if (cr50_i2c_read(dev, addr, buf + current, len) != 0) {
339 log_warning("Read failed\n");
346 if (cr50_i2c_wait_burststs(dev, TPM_STS_VALID, &burstcnt,
348 log_warning("- burst failure2\n");
351 if (status & TPM_STS_DATA_AVAIL) {
352 log_warning("Data still available\n");
359 /* Abort current transaction if still pending */
360 ret = cr50_i2c_status(dev);
363 if (ret & TPM_STS_COMMAND_READY) {
364 ret = cr50_i2c_ready(dev);
372 static int cr50_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
374 struct cr50_priv *priv = dev_get_priv(dev);
377 size_t burstcnt, limit, sent = 0;
378 u8 tpm_go[4] = { TPM_STS_GO };
382 log_debug("%s: len=%x\n", __func__, len);
383 timeout = timer_get_us() + TIMEOUT_LONG_US;
385 ret = cr50_i2c_status(dev);
388 if (ret & TPM_STS_COMMAND_READY)
391 if (timer_get_us() > timeout)
394 ret = cr50_i2c_ready(dev);
400 u8 mask = TPM_STS_VALID;
402 /* Wait for data if this is not the first chunk */
404 mask |= TPM_STS_DATA_EXPECT;
406 if (cr50_i2c_wait_burststs(dev, mask, &burstcnt, &status) < 0)
410 * Use burstcnt - 1 to account for the address byte
411 * that is inserted by cr50_i2c_write()
413 limit = min(burstcnt - 1, len);
414 if (cr50_i2c_write(dev, tpm_data_fifo(priv->locality),
415 &buf[sent], limit) != 0) {
416 log_warning("Write failed\n");
424 /* Ensure TPM is not expecting more data */
425 if (cr50_i2c_wait_burststs(dev, TPM_STS_VALID, &burstcnt, &status) < 0)
427 if (status & TPM_STS_DATA_EXPECT) {
428 log_warning("Data still expected\n");
432 /* Start the TPM command */
433 ret = cr50_i2c_write(dev, tpm_sts(priv->locality), tpm_go,
436 log_warning("Start command failed\n");
443 /* Abort current transaction if still pending */
444 ret = cr50_i2c_status(dev);
446 if (ret < 0 || (ret & TPM_STS_COMMAND_READY)) {
447 ret = cr50_i2c_ready(dev);
456 * process_reset() - Wait for the Cr50 to reset
458 * Cr50 processes reset requests asynchronously and conceivably could be busy
459 * executing a long command and not reacting to the reset pulse for a while.
461 * This function will make sure that the AP does not proceed with boot until
462 * TPM finished reset processing.
465 * @return 0 if OK, -EPERM if locality could not be taken
467 static int process_reset(struct udevice *dev)
474 * Locality is released by TPM reset.
476 * If locality is taken at this point, this could be due to the fact
477 * that the TPM is performing a long operation and has not processed
478 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
479 * it releases locality when reset is processed.
481 start = get_timer(0);
483 const u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
486 ret = cr50_i2c_read(dev, tpm_access(loc),
487 &access, sizeof(access));
488 if (ret || ((access & mask) == mask)) {
490 * Don't bombard the chip with traffic; let it keep
491 * processing the command.
497 log_debug("TPM ready after %ld ms\n", get_timer(start));
500 } while (get_timer(start) < TIMEOUT_INIT_MS);
502 log_err("TPM failed to reset after %ld ms, status: %#x\n",
503 get_timer(start), access);
509 * Locality could be already claimed (if this is a later U-Boot phase and the
510 * read-only U-Boot did not release it), or not yet claimed, if this is TPL or
511 * the older read-only U-Boot did release it.
513 static int claim_locality(struct udevice *dev, int loc)
515 const u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
516 struct cr50_priv *priv = dev_get_priv(dev);
520 ret = cr50_i2c_read(dev, tpm_access(loc), &access, sizeof(access));
522 return log_msg_ret("read1", ret);
524 if ((access & mask) == mask) {
525 log_warning("Locality already claimed\n");
529 access = TPM_ACCESS_REQUEST_USE;
530 ret = cr50_i2c_write(dev, tpm_access(loc), &access, sizeof(access));
532 return log_msg_ret("write", ret);
534 ret = cr50_i2c_read(dev, tpm_access(loc), &access, sizeof(access));
536 return log_msg_ret("read2", ret);
538 if ((access & mask) != mask) {
539 log_err("Failed to claim locality\n");
542 log_debug("Claimed locality %d\n", loc);
543 priv->locality = loc;
548 static int cr50_i2c_get_desc(struct udevice *dev, char *buf, int size)
550 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
551 struct cr50_priv *priv = dev_get_priv(dev);
553 return snprintf(buf, size, "cr50 TPM 2.0 (i2c %02x id %x) irq=%d",
554 chip->chip_addr, priv->vendor >> 16, priv->use_irq);
557 static int cr50_i2c_open(struct udevice *dev)
562 ret = process_reset(dev);
564 return log_msg_ret("reset", ret);
566 ret = claim_locality(dev, 0);
568 return log_msg_ret("claim", ret);
570 cr50_i2c_get_desc(dev, buf, sizeof(buf));
571 log_debug("%s\n", buf);
576 static int cr50_i2c_cleanup(struct udevice *dev)
578 struct cr50_priv *priv = dev_get_priv(dev);
580 log_debug("cleanup %d\n", priv->locality);
581 if (priv->locality != -1)
582 release_locality(dev, 1);
587 static int cr50_acpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
589 char scope[ACPI_PATH_MAX];
590 char name[ACPI_NAME_MAX];
594 ret = acpi_device_scope(dev, scope, sizeof(scope));
596 return log_msg_ret("scope", ret);
597 ret = acpi_get_name(dev, name);
599 return log_msg_ret("name", ret);
601 hid = dev_read_string(dev, "acpi,hid");
603 return log_msg_ret("hid", ret);
606 acpigen_write_scope(ctx, scope);
607 acpigen_write_device(ctx, name);
608 acpigen_write_name_string(ctx, "_HID", hid);
609 acpigen_write_name_integer(ctx, "_UID",
610 dev_read_u32_default(dev, "acpi,uid", 0));
611 acpigen_write_name_string(ctx, "_DDN",
612 dev_read_string(dev, "acpi,ddn"));
613 acpigen_write_sta(ctx, acpi_device_status(dev));
616 acpigen_write_name(ctx, "_CRS");
617 acpigen_write_resourcetemplate_header(ctx);
618 ret = acpi_device_write_i2c_dev(ctx, dev);
620 return log_msg_ret("i2c", ret);
621 ret = acpi_device_write_interrupt_or_gpio(ctx, (struct udevice *)dev,
624 return log_msg_ret("irq_gpio", ret);
626 acpigen_write_resourcetemplate_footer(ctx);
628 acpigen_pop_len(ctx); /* Device */
629 acpigen_pop_len(ctx); /* Scope */
636 SHORT_TIMEOUT_MS = 750,
637 LONG_TIMEOUT_MS = 2000,
640 static int cr50_i2c_ofdata_to_platdata(struct udevice *dev)
642 struct tpm_chip_priv *upriv = dev_get_uclass_priv(dev);
643 struct cr50_priv *priv = dev_get_priv(dev);
647 upriv->version = TPM_V2;
648 upriv->duration_ms[TPM_SHORT] = SHORT_TIMEOUT_MS;
649 upriv->duration_ms[TPM_MEDIUM] = LONG_TIMEOUT_MS;
650 upriv->duration_ms[TPM_LONG] = LONG_TIMEOUT_MS;
651 upriv->retry_time_ms = TPM_TIMEOUT_MS;
653 upriv->pcr_count = 32;
654 upriv->pcr_select_min = 2;
656 /* Optional GPIO to track when cr50 is ready */
657 ret = irq_get_by_index(dev, 0, &irq);
660 priv->use_irq = true;
662 ret = gpio_request_by_name(dev, "ready-gpios", 0,
663 &priv->ready_gpio, GPIOD_IS_IN);
665 log_warning("Cr50 does not have an ready GPIO/interrupt (err=%d)\n",
673 static int cr50_i2c_probe(struct udevice *dev)
675 struct cr50_priv *priv = dev_get_priv(dev);
680 * 150ms should be enough to synchronise with the TPM even under the
681 * worst nested-reset-request conditions. In the vast majority of cases
682 * there will be no wait at all.
684 start = get_timer(0);
685 while (get_timer(start) < 150) {
688 /* Exit once DID and VID verified */
689 ret = cr50_i2c_read(dev, tpm_did_vid(0), (u8 *)&vendor, 4);
690 if (!ret && vendor == CR50_DID_VID)
693 /* TPM might be resetting; let's retry in a bit */
696 if (vendor != CR50_DID_VID) {
697 log_debug("DID_VID %08x not recognised\n", vendor);
698 return log_msg_ret("vendor-id", -EXDEV);
700 priv->vendor = vendor;
706 struct acpi_ops cr50_acpi_ops = {
707 .fill_ssdt = cr50_acpi_fill_ssdt,
710 static const struct tpm_ops cr50_i2c_ops = {
711 .open = cr50_i2c_open,
712 .get_desc = cr50_i2c_get_desc,
713 .send = cr50_i2c_send,
714 .recv = cr50_i2c_recv,
715 .cleanup = cr50_i2c_cleanup,
718 static const struct udevice_id cr50_i2c_ids[] = {
719 { .compatible = "google,cr50" },
723 U_BOOT_DRIVER(cr50_i2c) = {
726 .of_match = cr50_i2c_ids,
727 .ops = &cr50_i2c_ops,
728 .ofdata_to_platdata = cr50_i2c_ofdata_to_platdata,
729 .probe = cr50_i2c_probe,
730 .remove = cr50_i2c_cleanup,
731 .priv_auto_alloc_size = sizeof(struct cr50_priv),
732 ACPI_OPS_PTR(&cr50_acpi_ops)
733 .flags = DM_FLAG_OS_PREPARE,