blob: 6ea795bc0c982b42ec5ceaf8dedb60bdc23b7030 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6c7a2322015-08-22 18:31:31 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass6c7a2322015-08-22 18:31:31 -06005 */
6
7#include <common.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Simon Glass6c7a2322015-08-22 18:31:31 -060011#include <linux/unaligned/be_byteshift.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020012#include <tpm-v1.h>
Miquel Raynalf3b43502018-05-15 11:57:08 +020013#include <tpm-v2.h>
Simon Glass6c7a2322015-08-22 18:31:31 -060014#include "tpm_internal.h"
15
16int tpm_open(struct udevice *dev)
17{
18 struct tpm_ops *ops = tpm_get_ops(dev);
19
20 if (!ops->open)
21 return -ENOSYS;
22
23 return ops->open(dev);
24}
25
26int tpm_close(struct udevice *dev)
27{
28 struct tpm_ops *ops = tpm_get_ops(dev);
29
30 if (!ops->close)
31 return -ENOSYS;
32
33 return ops->close(dev);
34}
35
36int tpm_get_desc(struct udevice *dev, char *buf, int size)
37{
38 struct tpm_ops *ops = tpm_get_ops(dev);
39
40 if (!ops->get_desc)
41 return -ENOSYS;
42
43 return ops->get_desc(dev, buf, size);
44}
45
46/* Returns max number of milliseconds to wait */
47static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
48 u32 ordinal)
49{
50 int duration_idx = TPM_UNDEFINED;
51 int duration = 0;
52
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];
59 }
60
61 if (duration_idx != TPM_UNDEFINED)
62 duration = priv->duration_ms[duration_idx];
63
64 if (duration <= 0)
65 return 2 * 60 * 1000; /* Two minutes timeout */
66 else
67 return duration;
68}
69
70int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
71 uint8_t *recvbuf, size_t *recv_size)
72{
73 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
74 struct tpm_ops *ops = tpm_get_ops(dev);
75 ulong start, stop;
76 uint count, ordinal;
Simon Glassbfb409b2020-04-08 16:57:25 -060077 int ret, ret2 = 0;
Simon Glass6c7a2322015-08-22 18:31:31 -060078
79 if (ops->xfer)
80 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
81
82 if (!ops->send || !ops->recv)
83 return -ENOSYS;
84
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);
88
89 if (count == 0) {
90 debug("no data\n");
91 return -ENODATA;
92 }
93 if (count > send_size) {
94 debug("invalid count value %x %zx\n", count, send_size);
95 return -E2BIG;
96 }
97
98 debug("%s: Calling send\n", __func__);
99 ret = ops->send(dev, sendbuf, send_size);
100 if (ret < 0)
101 return ret;
102
103 start = get_timer(0);
104 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
105 do {
106 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
107 if (ret >= 0) {
108 if (ret > *recv_size)
109 return -ENOSPC;
110 memcpy(recvbuf, priv->buf, ret);
111 *recv_size = ret;
112 ret = 0;
113 break;
114 } else if (ret != -EAGAIN) {
115 return ret;
116 }
117
118 mdelay(priv->retry_time_ms);
119 if (get_timer(start) > stop) {
120 ret = -ETIMEDOUT;
121 break;
122 }
123 } while (ret);
124
Simon Glassbfb409b2020-04-08 16:57:25 -0600125 if (ret) {
126 if (ops->cleanup) {
127 ret2 = ops->cleanup(dev);
128 if (ret2)
129 return log_msg_ret("cleanup", ret2);
130 }
131 return log_msg_ret("xfer", ret);
132 }
Simon Glass6c7a2322015-08-22 18:31:31 -0600133
Simon Glassbfb409b2020-04-08 16:57:25 -0600134 return 0;
Simon Glass6c7a2322015-08-22 18:31:31 -0600135}
136
137UCLASS_DRIVER(tpm) = {
Simon Glassbed43522018-10-01 12:22:22 -0600138 .id = UCLASS_TPM,
139 .name = "tpm",
140 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass1a825422018-10-01 12:22:27 -0600141#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
142 .post_bind = dm_scan_fdt_dev,
143#endif
Simon Glass6c7a2322015-08-22 18:31:31 -0600144 .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv),
145};