1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2020, Linaro Limited
4 *
5 * Based on the Linux TIS core interface and U-Boot original SPI TPM driver
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <tpm-v2.h>
11#include <linux/delay.h>
12#include <linux/unaligned/be_byteshift.h>
13#include "tpm_tis.h"
14
15int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
16{
17	struct tpm_chip *chip = dev_get_priv(dev);
18
19	if (size < 80)
20		return -ENOSPC;
21
22	return snprintf(buf, size,
23			"%s v2.0: VendorID 0x%04x, DeviceID 0x%04x, RevisionID 0x%02x [%s]",
24			dev->name, chip->vend_dev & 0xFFFF,
25			chip->vend_dev >> 16, chip->rid,
26			(chip->is_open ? "open" : "closed"));
27}
28
29/**
30 * tpm_tis_check_locality - Check the current TPM locality
31 *
32 * @dev: TPM device
33 * @loc:  locality
34 *
35 * Return: True if the tested locality matches
36 */
37static bool tpm_tis_check_locality(struct udevice *dev, int loc)
38{
39	struct tpm_chip *chip = dev_get_priv(dev);
40	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
41	u8 locality;
42
43	phy_ops->read_bytes(dev, TPM_ACCESS(loc), 1, &locality);
44	if ((locality & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID |
45	    TPM_ACCESS_REQUEST_USE)) ==
46	    (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
47		chip->locality = loc;
48		return true;
49	}
50
51	return false;
52}
53
54/**
55 * tpm_tis_request_locality - Request a locality from the TPM
56 *
57 * @dev:  TPM device
58 * @loc:  requested locality
59 *
60 * Return: 0 on success -1 on failure
61 */
62int tpm_tis_request_locality(struct udevice *dev, int loc)
63{
64	struct tpm_chip *chip = dev_get_priv(dev);
65	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
66	u8 buf = TPM_ACCESS_REQUEST_USE;
67	unsigned long start, stop;
68
69	if (tpm_tis_check_locality(dev, loc))
70		return 0;
71
72	phy_ops->write_bytes(dev, TPM_ACCESS(loc), 1, &buf);
73	start = get_timer(0);
74	stop = chip->timeout_a;
75	do {
76		if (tpm_tis_check_locality(dev, loc))
77			return 0;
78		mdelay(TPM_TIMEOUT_MS);
79	} while (get_timer(start) < stop);
80
81	return -1;
82}
83
84/**
85 * tpm_tis_status - Check the current device status
86 *
87 * @dev:   TPM device
88 * @status: return value of status
89 *
90 * Return: 0 on success, negative on failure
91 */
92static int tpm_tis_status(struct udevice *dev, u8 *status)
93{
94	struct tpm_chip *chip = dev_get_priv(dev);
95	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
96
97	if (chip->locality < 0)
98		return -EINVAL;
99
100	phy_ops->read_bytes(dev, TPM_STS(chip->locality), 1, status);
101
102	if ((*status & TPM_STS_READ_ZERO)) {
103		log_err("TPM returned invalid status\n");
104		return -EINVAL;
105	}
106
107	return 0;
108}
109
110/**
111 * tpm_tis_release_locality - Release the requested locality
112 *
113 * @dev: TPM device
114 * @loc:  requested locality
115 *
116 * Return: 0 on success, negative on failure
117 */
118int tpm_tis_release_locality(struct udevice *dev, int loc)
119{
120	struct tpm_chip *chip = dev_get_priv(dev);
121	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
122	u8 buf = TPM_ACCESS_ACTIVE_LOCALITY;
123	int ret;
124
125	if (chip->locality < 0)
126		return 0;
127
128	ret = phy_ops->write_bytes(dev, TPM_ACCESS(loc), 1, &buf);
129	chip->locality = -1;
130
131	return ret;
132}
133
134/**
135 * tpm_tis_wait_for_stat - Wait for TPM to become ready
136 *
137 * @dev:     TPM device
138 * @mask:    mask to match
139 * @timeout: timeout for retries
140 * @status:  current status
141 *
142 * Return: 0 on success, negative on failure
143 */
144static int tpm_tis_wait_for_stat(struct udevice *dev, u8 mask,
145				 unsigned long timeout, u8 *status)
146{
147	unsigned long start = get_timer(0);
148	unsigned long stop = timeout;
149	int ret;
150
151	do {
152		mdelay(TPM_TIMEOUT_MS);
153		ret = tpm_tis_status(dev, status);
154		if (ret)
155			return ret;
156
157		if ((*status & mask) == mask)
158			return 0;
159	} while (get_timer(start) < stop);
160
161	return -ETIMEDOUT;
162}
163
164/**
165 * tpm_tis_get_burstcount - Get the burstcount for the data FIFO
166 *
167 * @dev:        TPM device
168 * @burstcount: current burstcount
169 *
170 * Return: 0 on success, negative on failure
171 */
172static int tpm_tis_get_burstcount(struct udevice *dev, size_t *burstcount)
173{
174	struct tpm_chip *chip = dev_get_priv(dev);
175	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
176	unsigned long start, stop;
177	u32 burst;
178
179	if (chip->locality < 0)
180		return -EINVAL;
181
182	/* wait for burstcount */
183	start = get_timer(0);
184	/*
185	 * This is the TPMv2 defined timeout. Change this in case you want to
186	 * make the driver compatile to TPMv1
187	 */
188	stop = chip->timeout_a;
189	do {
190		phy_ops->read32(dev, TPM_STS(chip->locality), &burst);
191		*burstcount = (burst >> 8) & 0xFFFF;
192		if (*burstcount)
193			return 0;
194
195		mdelay(TPM_TIMEOUT_MS);
196	} while (get_timer(start) < stop);
197
198	return -ETIMEDOUT;
199}
200
201/**
202 * tpm_tis_ready - Cancel pending comands and get the device on a ready state
203 *
204 * @dev: TPM device
205 *
206 * Return: 0 on success, negative on failure
207 */
208static int tpm_tis_ready(struct udevice *dev)
209{
210	struct tpm_chip *chip = dev_get_priv(dev);
211	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
212	u8 data = TPM_STS_COMMAND_READY;
213
214	/* This will cancel any pending commands */
215	return phy_ops->write_bytes(dev, TPM_STS(chip->locality), 1, &data);
216}
217
218int tpm_tis_send(struct udevice *dev, const u8 *buf, size_t len)
219{
220	struct tpm_chip *chip = dev_get_priv(dev);
221	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
222	size_t burstcnt, wr_size, sent = 0;
223	u8 data = TPM_STS_GO;
224	u8 status;
225	int ret;
226
227	ret = tpm_tis_request_locality(dev, 0);
228	if (ret < 0)
229		return -EBUSY;
230
231	ret = tpm_tis_status(dev, &status);
232	if (ret)
233		goto release_locality;
234
235	if (!(status & TPM_STS_COMMAND_READY)) {
236		ret = tpm_tis_ready(dev);
237		if (ret) {
238			log_err("Can't cancel previous TPM operation\n");
239			goto release_locality;
240		}
241		ret = tpm_tis_wait_for_stat(dev, TPM_STS_COMMAND_READY,
242					    chip->timeout_b, &status);
243		if (ret) {
244			log_err("TPM not ready\n");
245			goto release_locality;
246		}
247	}
248
249	while (len > 0) {
250		ret = tpm_tis_get_burstcount(dev, &burstcnt);
251		if (ret)
252			goto release_locality;
253
254		wr_size = min(len, burstcnt);
255		ret = phy_ops->write_bytes(dev, TPM_DATA_FIFO(chip->locality),
256					   wr_size, buf + sent);
257		if (ret < 0)
258			goto release_locality;
259
260		ret = tpm_tis_wait_for_stat(dev, TPM_STS_VALID,
261					    chip->timeout_c, &status);
262		if (ret)
263			goto release_locality;
264
265		sent += wr_size;
266		len -= wr_size;
267		/* make sure the TPM expects more data */
268		if (len && !(status & TPM_STS_DATA_EXPECT)) {
269			ret = -EIO;
270			goto release_locality;
271		}
272	}
273
274	/*
275	 * Make a final check ensuring everything is ok and the TPM expects no
276	 * more data
277	 */
278	ret = tpm_tis_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
279				    &status);
280	if (ret)
281		goto release_locality;
282
283	if (status & TPM_STS_DATA_EXPECT) {
284		ret = -EIO;
285		goto release_locality;
286	}
287
288	ret = phy_ops->write_bytes(dev, TPM_STS(chip->locality), 1, &data);
289	if (ret)
290		goto release_locality;
291
292	return sent;
293
294release_locality:
295	tpm_tis_ready(dev);
296	tpm_tis_release_locality(dev, chip->locality);
297
298	return ret;
299}
300
301static int tpm_tis_recv_data(struct udevice *dev, u8 *buf, size_t count)
302{
303	struct tpm_chip *chip = dev_get_priv(dev);
304	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
305	int size = 0, len, ret;
306	size_t burstcnt;
307	u8 status;
308
309	while (size < count &&
310	       tpm_tis_wait_for_stat(dev, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
311				     chip->timeout_c, &status) == 0) {
312		ret = tpm_tis_get_burstcount(dev, &burstcnt);
313		if (ret)
314			return ret;
315
316		len = min_t(int, burstcnt, count - size);
317		ret = phy_ops->read_bytes(dev, TPM_DATA_FIFO(chip->locality),
318					  len, buf + size);
319		if (ret < 0)
320			return ret;
321
322		size += len;
323	}
324
325	return size;
326}
327
328/**
329 * tpm_tis_recv - Receive data from a device
330 *
331 * @dev:  TPM device
332 * @buf:  buffer to copy data
333 * @size: buffer size
334 *
335 * Return: bytes read or negative on failure
336 */
337int tpm_tis_recv(struct udevice *dev, u8 *buf, size_t count)
338{
339	struct tpm_chip *chip = dev_get_priv(dev);
340	int size, expected;
341
342	if (count < TPM_HEADER_SIZE)
343		return -E2BIG;
344
345	size = tpm_tis_recv_data(dev, buf, TPM_HEADER_SIZE);
346	if (size < TPM_HEADER_SIZE) {
347		log_err("TPM error, unable to read header\n");
348		goto out;
349	}
350
351	expected = get_unaligned_be32(buf + TPM_CMD_COUNT_OFFSET);
352	if (expected > count) {
353		size = -EIO;
354		log_warning("Too much data: %d > %zu\n", expected, count);
355		goto out;
356	}
357
358	size += tpm_tis_recv_data(dev, &buf[TPM_HEADER_SIZE],
359				   expected - TPM_HEADER_SIZE);
360	if (size < expected) {
361		log(LOGC_NONE, LOGL_ERR,
362		    "TPM error, unable to read remaining bytes of result\n");
363		size = -EIO;
364		goto out;
365	}
366
367out:
368	tpm_tis_ready(dev);
369	/* acquired in tpm_tis_send */
370	tpm_tis_release_locality(dev, chip->locality);
371
372	return size;
373}
374
375int tpm_tis_cleanup(struct udevice *dev)
376{
377	struct tpm_chip *chip = dev_get_priv(dev);
378	int ret;
379
380	ret = tpm_tis_request_locality(dev, 0);
381	if (ret)
382		return ret;
383
384	tpm_tis_ready(dev);
385
386	tpm_tis_release_locality(dev, chip->locality);
387
388	return 0;
389}
390
391int tpm_tis_open(struct udevice *dev)
392{
393	struct tpm_chip *chip = dev_get_priv(dev);
394	int ret;
395
396	if (chip->is_open)
397		return -EBUSY;
398
399	ret = tpm_tis_request_locality(dev, 0);
400	if (!ret)
401		chip->is_open = 1;
402
403	return ret;
404}
405
406void tpm_tis_ops_register(struct udevice *dev, struct tpm_tis_phy_ops *ops)
407{
408	struct tpm_chip *chip = dev_get_priv(dev);
409
410	chip->phy_ops = ops;
411}
412
413static bool tis_check_ops(struct tpm_tis_phy_ops *phy_ops)
414{
415	if (!phy_ops || !phy_ops->read_bytes || !phy_ops->write_bytes ||
416	    !phy_ops->read32 || !phy_ops->write32)
417		return false;
418
419	return true;
420}
421
422int tpm_tis_init(struct udevice *dev)
423{
424	struct tpm_chip *chip = dev_get_priv(dev);
425	struct tpm_tis_phy_ops *phy_ops = chip->phy_ops;
426	int ret;
427	u32 tmp;
428
429	if (!tis_check_ops(phy_ops)) {
430		log_err("Driver bug. No bus ops defined\n");
431		return -1;
432	}
433
434	chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
435	chip->timeout_b = TIS_LONG_TIMEOUT_MS;
436	chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
437	chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
438
439	ret = tpm_tis_request_locality(dev, 0);
440	if (ret)
441		return ret;
442
443	/* Disable interrupts */
444	phy_ops->read32(dev, TPM_INT_ENABLE(chip->locality), &tmp);
445	tmp |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
446	       TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
447	tmp &= ~TPM_GLOBAL_INT_ENABLE;
448	phy_ops->write32(dev, TPM_INT_ENABLE(chip->locality), tmp);
449
450	phy_ops->read_bytes(dev, TPM_RID(chip->locality), 1, &chip->rid);
451	phy_ops->read32(dev, TPM_DID_VID(chip->locality), &chip->vend_dev);
452
453	return tpm_tis_release_locality(dev, chip->locality);
454}
455
456int tpm_tis_close(struct udevice *dev)
457{
458	struct tpm_chip *chip = dev_get_priv(dev);
459	int ret = 0;
460
461	if (chip->is_open) {
462		ret = tpm_tis_release_locality(dev, chip->locality);
463		chip->is_open = 0;
464	}
465
466	return ret;
467}
468