]> Git Repo - J-u-boot.git/blob - drivers/tpm/tpm-uclass.c
Merge branch '2021-03-03-assorted-improvements' into next
[J-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 <[email protected]>
5  */
6
7 #define LOG_CATEGORY UCLASS_TPM
8
9 #include <common.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <linux/delay.h>
13 #include <linux/unaligned/be_byteshift.h>
14 #include <tpm-v1.h>
15 #include <tpm-v2.h>
16 #include "tpm_internal.h"
17
18 int tpm_open(struct udevice *dev)
19 {
20         struct tpm_ops *ops = tpm_get_ops(dev);
21
22         if (!ops->open)
23                 return -ENOSYS;
24
25         return ops->open(dev);
26 }
27
28 int tpm_close(struct udevice *dev)
29 {
30         struct tpm_ops *ops = tpm_get_ops(dev);
31
32         if (!ops->close)
33                 return -ENOSYS;
34
35         return ops->close(dev);
36 }
37
38 int tpm_get_desc(struct udevice *dev, char *buf, int size)
39 {
40         struct tpm_ops *ops = tpm_get_ops(dev);
41
42         if (!ops->get_desc)
43                 return -ENOSYS;
44
45         return ops->get_desc(dev, buf, size);
46 }
47
48 /* Returns max number of milliseconds to wait */
49 static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
50                                                u32 ordinal)
51 {
52         int duration_idx = TPM_UNDEFINED;
53         int duration = 0;
54
55         if (ordinal < TPM_MAX_ORDINAL) {
56                 duration_idx = tpm_ordinal_duration[ordinal];
57         } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
58                         TPM_MAX_PROTECTED_ORDINAL) {
59                 duration_idx = tpm_protected_ordinal_duration[
60                                 ordinal & TPM_PROTECTED_ORDINAL_MASK];
61         }
62
63         if (duration_idx != TPM_UNDEFINED)
64                 duration = priv->duration_ms[duration_idx];
65
66         if (duration <= 0)
67                 return 2 * 60 * 1000; /* Two minutes timeout */
68         else
69                 return duration;
70 }
71
72 int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
73         uint8_t *recvbuf, size_t *recv_size)
74 {
75         struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
76         struct tpm_ops *ops = tpm_get_ops(dev);
77         ulong start, stop;
78         uint count, ordinal;
79         int ret, ret2 = 0;
80
81         if (ops->xfer)
82                 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
83
84         if (!ops->send || !ops->recv)
85                 return -ENOSYS;
86
87         /* switch endianess: big->little */
88         count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
89         ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
90
91         if (count == 0) {
92                 log_debug("no data\n");
93                 return -ENODATA;
94         }
95         if (count > send_size) {
96                 log_debug("invalid count value %x %zx\n", count, send_size);
97                 return -E2BIG;
98         }
99
100         log_debug("%s: Calling send\n", __func__);
101         ret = ops->send(dev, sendbuf, send_size);
102         if (ret < 0)
103                 return ret;
104
105         start = get_timer(0);
106         stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
107         do {
108                 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
109                 if (ret >= 0) {
110                         if (ret > *recv_size)
111                                 return -ENOSPC;
112                         memcpy(recvbuf, priv->buf, ret);
113                         *recv_size = ret;
114                         ret = 0;
115                         break;
116                 } else if (ret != -EAGAIN) {
117                         return ret;
118                 }
119
120                 mdelay(priv->retry_time_ms);
121                 if (get_timer(start) > stop) {
122                         ret = -ETIMEDOUT;
123                         break;
124                 }
125         } while (ret);
126
127         if (ret) {
128                 if (ops->cleanup) {
129                         ret2 = ops->cleanup(dev);
130                         if (ret2)
131                                 return log_msg_ret("cleanup", ret2);
132                 }
133                 return log_msg_ret("xfer", ret);
134         }
135
136         return 0;
137 }
138
139 UCLASS_DRIVER(tpm) = {
140         .id             = UCLASS_TPM,
141         .name           = "tpm",
142         .flags          = DM_UC_FLAG_SEQ_ALIAS,
143 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
144         .post_bind      = dm_scan_fdt_dev,
145 #endif
146         .per_device_auto        = sizeof(struct tpm_chip_priv),
147 };
This page took 0.034308 seconds and 4 git commands to generate.