1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <linux/unaligned/be_byteshift.h>
11 #include "tpm_internal.h"
13 int tpm_open(struct udevice *dev)
15 struct tpm_ops *ops = tpm_get_ops(dev);
20 return ops->open(dev);
23 int tpm_close(struct udevice *dev)
25 struct tpm_ops *ops = tpm_get_ops(dev);
30 return ops->close(dev);
33 int tpm_get_desc(struct udevice *dev, char *buf, int size)
35 struct tpm_ops *ops = tpm_get_ops(dev);
40 return ops->get_desc(dev, buf, size);
43 /* Returns max number of milliseconds to wait */
44 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
47 int duration_idx = TPM_UNDEFINED;
50 if (ordinal < TPM_MAX_ORDINAL) {
51 duration_idx = tpm_ordinal_duration[ordinal];
52 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
53 TPM_MAX_PROTECTED_ORDINAL) {
54 duration_idx = tpm_protected_ordinal_duration[
55 ordinal & TPM_PROTECTED_ORDINAL_MASK];
58 if (duration_idx != TPM_UNDEFINED)
59 duration = priv->duration_ms[duration_idx];
62 return 2 * 60 * 1000; /* Two minutes timeout */
67 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
68 uint8_t *recvbuf, size_t *recv_size)
70 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
71 struct tpm_ops *ops = tpm_get_ops(dev);
77 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
79 if (!ops->send || !ops->recv)
82 /* switch endianess: big->little */
83 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
84 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
90 if (count > send_size) {
91 debug("invalid count value %x %zx\n", count, send_size);
95 debug("%s: Calling send\n", __func__);
96 ret = ops->send(dev, sendbuf, send_size);
100 start = get_timer(0);
101 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
103 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
105 if (ret > *recv_size)
107 memcpy(recvbuf, priv->buf, ret);
111 } else if (ret != -EAGAIN) {
115 mdelay(priv->retry_time_ms);
116 if (get_timer(start) > stop) {
122 ret2 = ops->cleanup ? ops->cleanup(dev) : 0;
124 return ret2 ? ret2 : ret;
127 UCLASS_DRIVER(tpm) = {
130 .flags = DM_UC_FLAG_SEQ_ALIAS,
131 .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv),