blob: f67fe1019b2ec63670958dde66f2cba6659c36eb [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>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Simon Glass6c7a2322015-08-22 18:31:31 -060013#include <linux/unaligned/be_byteshift.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020014#include <tpm-v1.h>
Miquel Raynalf3b43502018-05-15 11:57:08 +020015#include <tpm-v2.h>
Simon Glass6c7a2322015-08-22 18:31:31 -060016#include "tpm_internal.h"
17
18int 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
28int 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
38int 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 */
49static 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
72int 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;
Simon Glassbfb409b2020-04-08 16:57:25 -060079 int ret, ret2 = 0;
Simon Glass6c7a2322015-08-22 18:31:31 -060080
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) {
Simon Glass2797ebf2021-02-06 14:23:33 -070092 log_debug("no data\n");
Simon Glass6c7a2322015-08-22 18:31:31 -060093 return -ENODATA;
94 }
95 if (count > send_size) {
Simon Glass2797ebf2021-02-06 14:23:33 -070096 log_debug("invalid count value %x %zx\n", count, send_size);
Simon Glass6c7a2322015-08-22 18:31:31 -060097 return -E2BIG;
98 }
99
Simon Glass2797ebf2021-02-06 14:23:33 -0700100 log_debug("%s: Calling send\n", __func__);
Simon Glass6c7a2322015-08-22 18:31:31 -0600101 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
Simon Glassbfb409b2020-04-08 16:57:25 -0600127 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 }
Simon Glass6c7a2322015-08-22 18:31:31 -0600135
Simon Glassbfb409b2020-04-08 16:57:25 -0600136 return 0;
Simon Glass6c7a2322015-08-22 18:31:31 -0600137}
138
139UCLASS_DRIVER(tpm) = {
Simon Glassbed43522018-10-01 12:22:22 -0600140 .id = UCLASS_TPM,
141 .name = "tpm",
142 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass3580f6d2021-08-07 07:24:03 -0600143#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass1a825422018-10-01 12:22:27 -0600144 .post_bind = dm_scan_fdt_dev,
145#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700146 .per_device_auto = sizeof(struct tpm_chip_priv),
Simon Glass6c7a2322015-08-22 18:31:31 -0600147};