Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / tpm / tpm-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #define LOG_CATEGORY UCLASS_TPM
8
9 #include <common.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <tpm_api.h>
13 #include <tpm-v1.h>
14 #include <tpm-v2.h>
15 #include <dm/lists.h>
16 #include <linux/delay.h>
17 #include <linux/unaligned/be_byteshift.h>
18 #include "tpm_internal.h"
19
20 #define TPM_RNG_DRV_NAME        "tpm-rng"
21
22 int 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
32 int 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
42 int 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
52 int 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
62 /* Returns max number of milliseconds to wait */
63 static 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
86 int 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;
93         int ret, ret2 = 0;
94
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) {
106                 log_debug("no data\n");
107                 return -ENODATA;
108         }
109         if (count > send_size) {
110                 log_debug("invalid count value %x %zx\n", count, send_size);
111                 return -E2BIG;
112         }
113
114         log_debug("%s: Calling send\n", __func__);
115         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
141         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         }
149
150         return 0;
151 }
152
153 static 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
159         if (IS_ENABLED(CONFIG_TPM_RNG)) {
160                 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
176 UCLASS_DRIVER(tpm) = {
177         .id                     = UCLASS_TPM,
178         .name                   = "tpm",
179         .flags                  = DM_UC_FLAG_SEQ_ALIAS,
180 #if CONFIG_IS_ENABLED(OF_REAL)
181         .post_bind              = dm_scan_fdt_dev,
182 #endif
183         .post_probe             = tpm_uclass_post_probe,
184         .per_device_auto        = sizeof(struct tpm_chip_priv),
185 };