1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #define LOG_CATEGORY UCLASS_TPM
16 #include <linux/delay.h>
17 #include <linux/unaligned/be_byteshift.h>
18 #include "tpm_internal.h"
20 #define TPM_RNG_DRV_NAME "tpm-rng"
22 int tpm_open(struct udevice *dev)
24 struct tpm_ops *ops = tpm_get_ops(dev);
29 return ops->open(dev);
32 int tpm_close(struct udevice *dev)
34 struct tpm_ops *ops = tpm_get_ops(dev);
39 return ops->close(dev);
42 int tpm_get_desc(struct udevice *dev, char *buf, int size)
44 struct tpm_ops *ops = tpm_get_ops(dev);
49 return ops->get_desc(dev, buf, size);
52 int tpm_report_state(struct udevice *dev, char *buf, int size)
54 struct tpm_ops *ops = tpm_get_ops(dev);
56 if (!ops->report_state)
59 return ops->report_state(dev, buf, size);
62 /* Returns max number of milliseconds to wait */
63 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
66 int duration_idx = TPM_UNDEFINED;
69 if (ordinal < TPM_MAX_ORDINAL) {
70 duration_idx = tpm_ordinal_duration[ordinal];
71 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
72 TPM_MAX_PROTECTED_ORDINAL) {
73 duration_idx = tpm_protected_ordinal_duration[
74 ordinal & TPM_PROTECTED_ORDINAL_MASK];
77 if (duration_idx != TPM_UNDEFINED)
78 duration = priv->duration_ms[duration_idx];
81 return 2 * 60 * 1000; /* Two minutes timeout */
86 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
87 uint8_t *recvbuf, size_t *recv_size)
89 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
90 struct tpm_ops *ops = tpm_get_ops(dev);
96 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
98 if (!ops->send || !ops->recv)
101 /* switch endianess: big->little */
102 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
103 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
106 log_debug("no data\n");
109 if (count > send_size) {
110 log_debug("invalid count value %x %zx\n", count, send_size);
114 log_debug("%s: Calling send\n", __func__);
115 ret = ops->send(dev, sendbuf, send_size);
119 start = get_timer(0);
120 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
122 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
124 if (ret > *recv_size)
126 memcpy(recvbuf, priv->buf, ret);
130 } else if (ret != -EAGAIN) {
134 mdelay(priv->retry_time_ms);
135 if (get_timer(start) > stop) {
143 ret2 = ops->cleanup(dev);
145 return log_msg_ret("cleanup", ret2);
147 return log_msg_ret("xfer", ret);
153 static int tpm_uclass_post_probe(struct udevice *dev)
156 const char *drv = TPM_RNG_DRV_NAME;
157 struct udevice *child;
159 if (CONFIG_IS_ENABLED(TPM_RNG)) {
160 ret = device_find_first_child_by_uclass(dev, UCLASS_RNG,
163 if (ret != -ENODEV) {
164 log_debug("RNG child already added to the TPM device\n");
168 ret = device_bind_driver(dev, drv, TPM_RNG_DRV_NAME, &child);
170 return log_msg_ret("bind", ret);
176 UCLASS_DRIVER(tpm) = {
179 .flags = DM_UC_FLAG_SEQ_ALIAS,
180 #if CONFIG_IS_ENABLED(OF_REAL)
181 .post_bind = dm_scan_fdt_dev,
183 .post_probe = tpm_uclass_post_probe,
184 .per_device_auto = sizeof(struct tpm_chip_priv),