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