1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
9 #include <linux/unaligned/be_byteshift.h>
10 #if defined(CONFIG_TPM_V1)
13 #include "tpm_internal.h"
15 int tpm_open(struct udevice *dev)
17 struct tpm_ops *ops = tpm_get_ops(dev);
22 return ops->open(dev);
25 int tpm_close(struct udevice *dev)
27 struct tpm_ops *ops = tpm_get_ops(dev);
32 return ops->close(dev);
35 int tpm_get_desc(struct udevice *dev, char *buf, int size)
37 struct tpm_ops *ops = tpm_get_ops(dev);
42 return ops->get_desc(dev, buf, size);
45 /* Returns max number of milliseconds to wait */
46 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
49 int duration_idx = TPM_UNDEFINED;
52 if (ordinal < TPM_MAX_ORDINAL) {
53 duration_idx = tpm_ordinal_duration[ordinal];
54 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
55 TPM_MAX_PROTECTED_ORDINAL) {
56 duration_idx = tpm_protected_ordinal_duration[
57 ordinal & TPM_PROTECTED_ORDINAL_MASK];
60 if (duration_idx != TPM_UNDEFINED)
61 duration = priv->duration_ms[duration_idx];
64 return 2 * 60 * 1000; /* Two minutes timeout */
69 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
70 uint8_t *recvbuf, size_t *recv_size)
72 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
73 struct tpm_ops *ops = tpm_get_ops(dev);
79 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
81 if (!ops->send || !ops->recv)
84 /* switch endianess: big->little */
85 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
86 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
92 if (count > send_size) {
93 debug("invalid count value %x %zx\n", count, send_size);
97 debug("%s: Calling send\n", __func__);
98 ret = ops->send(dev, sendbuf, send_size);
102 start = get_timer(0);
103 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
105 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
107 if (ret > *recv_size)
109 memcpy(recvbuf, priv->buf, ret);
113 } else if (ret != -EAGAIN) {
117 mdelay(priv->retry_time_ms);
118 if (get_timer(start) > stop) {
124 ret2 = ops->cleanup ? ops->cleanup(dev) : 0;
126 return ret2 ? ret2 : ret;
129 UCLASS_DRIVER(tpm) = {
132 .flags = DM_UC_FLAG_SEQ_ALIAS,
133 .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv),