1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
10 #include <linux/delay.h>
11 #include <linux/unaligned/be_byteshift.h>
14 #include "tpm_internal.h"
16 int tpm_open(struct udevice *dev)
18 struct tpm_ops *ops = tpm_get_ops(dev);
23 return ops->open(dev);
26 int tpm_close(struct udevice *dev)
28 struct tpm_ops *ops = tpm_get_ops(dev);
33 return ops->close(dev);
36 int tpm_get_desc(struct udevice *dev, char *buf, int size)
38 struct tpm_ops *ops = tpm_get_ops(dev);
43 return ops->get_desc(dev, buf, size);
46 /* Returns max number of milliseconds to wait */
47 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
50 int duration_idx = TPM_UNDEFINED;
53 if (ordinal < TPM_MAX_ORDINAL) {
54 duration_idx = tpm_ordinal_duration[ordinal];
55 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
56 TPM_MAX_PROTECTED_ORDINAL) {
57 duration_idx = tpm_protected_ordinal_duration[
58 ordinal & TPM_PROTECTED_ORDINAL_MASK];
61 if (duration_idx != TPM_UNDEFINED)
62 duration = priv->duration_ms[duration_idx];
65 return 2 * 60 * 1000; /* Two minutes timeout */
70 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
71 uint8_t *recvbuf, size_t *recv_size)
73 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
74 struct tpm_ops *ops = tpm_get_ops(dev);
80 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
82 if (!ops->send || !ops->recv)
85 /* switch endianess: big->little */
86 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
87 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
93 if (count > send_size) {
94 debug("invalid count value %x %zx\n", count, send_size);
98 debug("%s: Calling send\n", __func__);
99 ret = ops->send(dev, sendbuf, send_size);
103 start = get_timer(0);
104 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
106 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
108 if (ret > *recv_size)
110 memcpy(recvbuf, priv->buf, ret);
114 } else if (ret != -EAGAIN) {
118 mdelay(priv->retry_time_ms);
119 if (get_timer(start) > stop) {
127 ret2 = ops->cleanup(dev);
129 return log_msg_ret("cleanup", ret2);
131 return log_msg_ret("xfer", ret);
137 UCLASS_DRIVER(tpm) = {
140 .flags = DM_UC_FLAG_SEQ_ALIAS,
141 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
142 .post_bind = dm_scan_fdt_dev,
144 .per_device_auto = sizeof(struct tpm_chip_priv),