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)
12 #elif defined(CONFIG_TPM_V2)
15 #include "tpm_internal.h"
17 int tpm_open(struct udevice *dev)
19 struct tpm_ops *ops = tpm_get_ops(dev);
24 return ops->open(dev);
27 int tpm_close(struct udevice *dev)
29 struct tpm_ops *ops = tpm_get_ops(dev);
34 return ops->close(dev);
37 int tpm_get_desc(struct udevice *dev, char *buf, int size)
39 struct tpm_ops *ops = tpm_get_ops(dev);
44 return ops->get_desc(dev, buf, size);
47 /* Returns max number of milliseconds to wait */
48 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
51 int duration_idx = TPM_UNDEFINED;
54 if (ordinal < TPM_MAX_ORDINAL) {
55 duration_idx = tpm_ordinal_duration[ordinal];
56 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
57 TPM_MAX_PROTECTED_ORDINAL) {
58 duration_idx = tpm_protected_ordinal_duration[
59 ordinal & TPM_PROTECTED_ORDINAL_MASK];
62 if (duration_idx != TPM_UNDEFINED)
63 duration = priv->duration_ms[duration_idx];
66 return 2 * 60 * 1000; /* Two minutes timeout */
71 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
72 uint8_t *recvbuf, size_t *recv_size)
74 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
75 struct tpm_ops *ops = tpm_get_ops(dev);
81 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
83 if (!ops->send || !ops->recv)
86 /* switch endianess: big->little */
87 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
88 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
94 if (count > send_size) {
95 debug("invalid count value %x %zx\n", count, send_size);
99 debug("%s: Calling send\n", __func__);
100 ret = ops->send(dev, sendbuf, send_size);
104 start = get_timer(0);
105 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
107 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
109 if (ret > *recv_size)
111 memcpy(recvbuf, priv->buf, ret);
115 } else if (ret != -EAGAIN) {
119 mdelay(priv->retry_time_ms);
120 if (get_timer(start) > stop) {
126 ret2 = ops->cleanup ? ops->cleanup(dev) : 0;
128 return ret2 ? ret2 : ret;
131 UCLASS_DRIVER(tpm) = {
134 .flags = DM_UC_FLAG_SEQ_ALIAS,
135 .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv),