1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
7 #define LOG_CATEGORY UCLASS_TPM
12 #include <linux/delay.h>
13 #include <linux/unaligned/be_byteshift.h>
16 #include "tpm_internal.h"
18 int tpm_open(struct udevice *dev)
20 struct tpm_ops *ops = tpm_get_ops(dev);
25 return ops->open(dev);
28 int tpm_close(struct udevice *dev)
30 struct tpm_ops *ops = tpm_get_ops(dev);
35 return ops->close(dev);
38 int tpm_get_desc(struct udevice *dev, char *buf, int size)
40 struct tpm_ops *ops = tpm_get_ops(dev);
45 return ops->get_desc(dev, buf, size);
48 /* Returns max number of milliseconds to wait */
49 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
52 int duration_idx = TPM_UNDEFINED;
55 if (ordinal < TPM_MAX_ORDINAL) {
56 duration_idx = tpm_ordinal_duration[ordinal];
57 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
58 TPM_MAX_PROTECTED_ORDINAL) {
59 duration_idx = tpm_protected_ordinal_duration[
60 ordinal & TPM_PROTECTED_ORDINAL_MASK];
63 if (duration_idx != TPM_UNDEFINED)
64 duration = priv->duration_ms[duration_idx];
67 return 2 * 60 * 1000; /* Two minutes timeout */
72 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
73 uint8_t *recvbuf, size_t *recv_size)
75 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
76 struct tpm_ops *ops = tpm_get_ops(dev);
82 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
84 if (!ops->send || !ops->recv)
87 /* switch endianess: big->little */
88 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
89 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
92 log_debug("no data\n");
95 if (count > send_size) {
96 log_debug("invalid count value %x %zx\n", count, send_size);
100 log_debug("%s: Calling send\n", __func__);
101 ret = ops->send(dev, sendbuf, send_size);
105 start = get_timer(0);
106 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
108 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
110 if (ret > *recv_size)
112 memcpy(recvbuf, priv->buf, ret);
116 } else if (ret != -EAGAIN) {
120 mdelay(priv->retry_time_ms);
121 if (get_timer(start) > stop) {
129 ret2 = ops->cleanup(dev);
131 return log_msg_ret("cleanup", ret2);
133 return log_msg_ret("xfer", ret);
139 UCLASS_DRIVER(tpm) = {
142 .flags = DM_UC_FLAG_SEQ_ALIAS,
143 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
144 .post_bind = dm_scan_fdt_dev,
146 .per_device_auto = sizeof(struct tpm_chip_priv),