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