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