blob: 0eb35f50c4e6a5185bb917c25d0b5f2033f90940 [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 <common.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.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
52/* Returns max number of milliseconds to wait */
53static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
54 u32 ordinal)
55{
56 int duration_idx = TPM_UNDEFINED;
57 int duration = 0;
58
59 if (ordinal < TPM_MAX_ORDINAL) {
60 duration_idx = tpm_ordinal_duration[ordinal];
61 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
62 TPM_MAX_PROTECTED_ORDINAL) {
63 duration_idx = tpm_protected_ordinal_duration[
64 ordinal & TPM_PROTECTED_ORDINAL_MASK];
65 }
66
67 if (duration_idx != TPM_UNDEFINED)
68 duration = priv->duration_ms[duration_idx];
69
70 if (duration <= 0)
71 return 2 * 60 * 1000; /* Two minutes timeout */
72 else
73 return duration;
74}
75
76int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
77 uint8_t *recvbuf, size_t *recv_size)
78{
79 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
80 struct tpm_ops *ops = tpm_get_ops(dev);
81 ulong start, stop;
82 uint count, ordinal;
Simon Glassbfb409b2020-04-08 16:57:25 -060083 int ret, ret2 = 0;
Simon Glass6c7a2322015-08-22 18:31:31 -060084
85 if (ops->xfer)
86 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
87
88 if (!ops->send || !ops->recv)
89 return -ENOSYS;
90
91 /* switch endianess: big->little */
92 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
93 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
94
95 if (count == 0) {
Simon Glass2797ebf2021-02-06 14:23:33 -070096 log_debug("no data\n");
Simon Glass6c7a2322015-08-22 18:31:31 -060097 return -ENODATA;
98 }
99 if (count > send_size) {
Simon Glass2797ebf2021-02-06 14:23:33 -0700100 log_debug("invalid count value %x %zx\n", count, send_size);
Simon Glass6c7a2322015-08-22 18:31:31 -0600101 return -E2BIG;
102 }
103
Simon Glass2797ebf2021-02-06 14:23:33 -0700104 log_debug("%s: Calling send\n", __func__);
Simon Glass6c7a2322015-08-22 18:31:31 -0600105 ret = ops->send(dev, sendbuf, send_size);
106 if (ret < 0)
107 return ret;
108
109 start = get_timer(0);
110 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
111 do {
112 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
113 if (ret >= 0) {
114 if (ret > *recv_size)
115 return -ENOSPC;
116 memcpy(recvbuf, priv->buf, ret);
117 *recv_size = ret;
118 ret = 0;
119 break;
120 } else if (ret != -EAGAIN) {
121 return ret;
122 }
123
124 mdelay(priv->retry_time_ms);
125 if (get_timer(start) > stop) {
126 ret = -ETIMEDOUT;
127 break;
128 }
129 } while (ret);
130
Simon Glassbfb409b2020-04-08 16:57:25 -0600131 if (ret) {
132 if (ops->cleanup) {
133 ret2 = ops->cleanup(dev);
134 if (ret2)
135 return log_msg_ret("cleanup", ret2);
136 }
137 return log_msg_ret("xfer", ret);
138 }
Simon Glass6c7a2322015-08-22 18:31:31 -0600139
Simon Glassbfb409b2020-04-08 16:57:25 -0600140 return 0;
Simon Glass6c7a2322015-08-22 18:31:31 -0600141}
142
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530143static int tpm_uclass_post_probe(struct udevice *dev)
144{
145 int ret;
146 const char *drv = TPM_RNG_DRV_NAME;
147 struct udevice *child;
148
149 if (CONFIG_IS_ENABLED(TPM_RNG)) {
150 ret = device_find_first_child_by_uclass(dev, UCLASS_RNG,
151 &child);
152
153 if (ret != -ENODEV) {
154 log_debug("RNG child already added to the TPM device\n");
155 return ret;
156 }
157
158 ret = device_bind_driver(dev, drv, TPM_RNG_DRV_NAME, &child);
159 if (ret)
160 return log_msg_ret("bind", ret);
161 }
162
163 return 0;
164}
165
Simon Glass6c7a2322015-08-22 18:31:31 -0600166UCLASS_DRIVER(tpm) = {
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530167 .id = UCLASS_TPM,
168 .name = "tpm",
169 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass3580f6d2021-08-07 07:24:03 -0600170#if CONFIG_IS_ENABLED(OF_REAL)
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530171 .post_bind = dm_scan_fdt_dev,
Simon Glass1a825422018-10-01 12:22:27 -0600172#endif
Sughosh Ganu5b60b462022-07-22 21:32:05 +0530173 .post_probe = tpm_uclass_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700174 .per_device_auto = sizeof(struct tpm_chip_priv),
Simon Glass6c7a2322015-08-22 18:31:31 -0600175};