blob: 0fade2dcc0ad8b8ce35f5f831fe7b03c9ef96808 [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
Simon Glass2797ebf2021-02-06 14:23:33 -07007#define LOG_CATEGORY UCLASS_TPM
8
Simon Glass6c7a2322015-08-22 18:31:31 -06009#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <time.h>
Sughosh Ganu5b60b462022-07-22 21:32:05 +053012#include <tpm_api.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020013#include <tpm-v1.h>
Miquel Raynalf3b43502018-05-15 11:57:08 +020014#include <tpm-v2.h>
Sughosh Ganu5b60b462022-07-22 21:32:05 +053015#include <dm/lists.h>
16#include <linux/delay.h>
17#include <linux/unaligned/be_byteshift.h>
Simon Glass6c7a2322015-08-22 18:31:31 -060018#include "tpm_internal.h"
19
Sughosh Ganu5b60b462022-07-22 21:32:05 +053020#define TPM_RNG_DRV_NAME "tpm-rng"
21
Simon Glass6c7a2322015-08-22 18:31:31 -060022int tpm_open(struct udevice *dev)
23{
24 struct tpm_ops *ops = tpm_get_ops(dev);
25
26 if (!ops->open)
27 return -ENOSYS;
28
29 return ops->open(dev);
30}
31
32int tpm_close(struct udevice *dev)
33{
34 struct tpm_ops *ops = tpm_get_ops(dev);
35
36 if (!ops->close)
37 return -ENOSYS;
38
39 return ops->close(dev);
40}
41
42int tpm_get_desc(struct udevice *dev, char *buf, int size)
43{
44 struct tpm_ops *ops = tpm_get_ops(dev);
45
46 if (!ops->get_desc)
47 return -ENOSYS;
48
49 return ops->get_desc(dev, buf, size);
50}
51
Simon Glass11a075c2022-08-30 21:05:36 -060052int tpm_report_state(struct udevice *dev, char *buf, int size)
53{
54 struct tpm_ops *ops = tpm_get_ops(dev);
55
56 if (!ops->report_state)
57 return -ENOSYS;
58
59 return ops->report_state(dev, buf, size);
60}
61
Simon Glass6c7a2322015-08-22 18:31:31 -060062/* Returns max number of milliseconds to wait */
63static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
64 u32 ordinal)
65{
66 int duration_idx = TPM_UNDEFINED;
67 int duration = 0;
68
69 if (ordinal < TPM_MAX_ORDINAL) {
70 duration_idx = tpm_ordinal_duration[ordinal];
71 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
72 TPM_MAX_PROTECTED_ORDINAL) {
73 duration_idx = tpm_protected_ordinal_duration[
74 ordinal & TPM_PROTECTED_ORDINAL_MASK];
75 }
76
77 if (duration_idx != TPM_UNDEFINED)
78 duration = priv->duration_ms[duration_idx];
79
80 if (duration <= 0)
81 return 2 * 60 * 1000; /* Two minutes timeout */
82 else
83 return duration;
84}
85
86int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
87 uint8_t *recvbuf, size_t *recv_size)
88{
89 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
90 struct tpm_ops *ops = tpm_get_ops(dev);
91 ulong start, stop;
92 uint count, ordinal;
Simon Glassbfb409b2020-04-08 16:57:25 -060093 int ret, ret2 = 0;
Simon Glass6c7a2322015-08-22 18:31:31 -060094
95 if (ops->xfer)
96 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
97
98 if (!ops->send || !ops->recv)
99 return -ENOSYS;
100
101 /* switch endianess: big->little */
102 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
103 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
104
105 if (count == 0) {
Simon Glass2797ebf2021-02-06 14:23:33 -0700106 log_debug("no data\n");
Simon Glass6c7a2322015-08-22 18:31:31 -0600107 return -ENODATA;
108 }
109 if (count > send_size) {
Simon Glass2797ebf2021-02-06 14:23:33 -0700110 log_debug("invalid count value %x %zx\n", count, send_size);
Simon Glass6c7a2322015-08-22 18:31:31 -0600111 return -E2BIG;
112 }
113
Simon Glass2797ebf2021-02-06 14:23:33 -0700114 log_debug("%s: Calling send\n", __func__);
Simon Glass6c7a2322015-08-22 18:31:31 -0600115 ret = ops->send(dev, sendbuf, send_size);
116 if (ret < 0)
117 return ret;
118
119 start = get_timer(0);
120 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
121 do {
122 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
123 if (ret >= 0) {
124 if (ret > *recv_size)
125 return -ENOSPC;
126 memcpy(recvbuf, priv->buf, ret);
127 *recv_size = ret;
128 ret = 0;
129 break;
130 } else if (ret != -EAGAIN) {
131 return ret;
132 }
133
134 mdelay(priv->retry_time_ms);
135 if (get_timer(start) > stop) {
136 ret = -ETIMEDOUT;
137 break;
138 }
139 } while (ret);
140
Simon Glassbfb409b2020-04-08 16:57:25 -0600141 if (ret) {
142 if (ops->cleanup) {
143 ret2 = ops->cleanup(dev);
144 if (ret2)
145 return log_msg_ret("cleanup", ret2);
146 }
147 return log_msg_ret("xfer", ret);
148 }
Simon Glass6c7a2322015-08-22 18:31:31 -0600149
Simon Glassbfb409b2020-04-08 16:57:25 -0600150 return 0;
Simon Glass6c7a2322015-08-22 18:31:31 -0600151}
152
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530153static int tpm_uclass_post_probe(struct udevice *dev)
154{
155 int ret;
156 const char *drv = TPM_RNG_DRV_NAME;
157 struct udevice *child;
158
Simon Glass4863b6b2023-02-05 15:44:19 -0700159 if (IS_ENABLED(CONFIG_TPM_RNG)) {
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530160 ret = device_find_first_child_by_uclass(dev, UCLASS_RNG,
161 &child);
162
163 if (ret != -ENODEV) {
164 log_debug("RNG child already added to the TPM device\n");
165 return ret;
166 }
167
168 ret = device_bind_driver(dev, drv, TPM_RNG_DRV_NAME, &child);
169 if (ret)
170 return log_msg_ret("bind", ret);
171 }
172
173 return 0;
174}
175
Simon Glass6c7a2322015-08-22 18:31:31 -0600176UCLASS_DRIVER(tpm) = {
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530177 .id = UCLASS_TPM,
178 .name = "tpm",
179 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass3580f6d2021-08-07 07:24:03 -0600180#if CONFIG_IS_ENABLED(OF_REAL)
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530181 .post_bind = dm_scan_fdt_dev,
Simon Glass1a825422018-10-01 12:22:27 -0600182#endif
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530183 .post_probe = tpm_uclass_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700184 .per_device_auto = sizeof(struct tpm_chip_priv),
Simon Glass6c7a2322015-08-22 18:31:31 -0600185};