1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * STMicroelectronics TPM ST33ZP24 I2C UBOOT driver
4  *
5  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6  * Author(s): Christophe Ricard <christophe-h.ricard@st.com> for STMicroelectronics.
7  *
8  * Description: Device driver for ST33ZP24 I2C TPM TCG.
9  *
10  * This device driver implements the TPM interface as defined in
11  * the TCG TPM Interface Spec version 1.21, revision 1.0 and the
12  * STMicroelectronics Protocol Stack Specification version 1.2.0.
13  */
14 
15 #include <common.h>
16 #include <dm.h>
17 #include <fdtdec.h>
18 #include <i2c.h>
19 #include <tpm-v1.h>
20 #include <errno.h>
21 #include <linux/types.h>
22 #include <asm/unaligned.h>
23 
24 #include "tpm_tis.h"
25 #include "tpm_internal.h"
26 
27 #define TPM_ACCESS			0x0
28 #define TPM_STS				0x18
29 #define TPM_DATA_FIFO			0x24
30 
31 #define LOCALITY0			0
32 
33 #define TPM_DUMMY_BYTE			0xAA
34 #define TPM_ST33ZP24_I2C_SLAVE_ADDR	0x13
35 
36 #define TPM_WRITE_DIRECTION             0x80
37 
38 /*
39  * st33zp24_i2c_write8_reg
40  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
41  * @param: tpm_register, the tpm tis register where the data should be written
42  * @param: tpm_data, the tpm_data to write inside the tpm_register
43  * @param: tpm_size, The length of the data
44  * @return: Number of byte written successfully else an error code.
45  */
46 static int st33zp24_i2c_write8_reg(struct udevice *dev, u8 tpm_register,
47 				   const u8 *tpm_data, size_t tpm_size)
48 {
49 	struct tpm_chip_priv *chip_priv = dev_get_uclass_priv(dev);
50 
51 	chip_priv->buf[0] = tpm_register;
52 	memcpy(chip_priv->buf + 1, tpm_data, tpm_size);
53 
54 	return dm_i2c_write(dev, 0, chip_priv->buf, tpm_size + 1);
55 }
56 
57 /*
58 * st33zp24_i2c_read8_reg
59 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
60 * @param: tpm_register, the tpm tis register where the data should be read
61 * @param: tpm_data, the TPM response
62 * @param: tpm_size, tpm TPM response size to read.
63 * @return: Number of byte read successfully else an error code.
64 */
65 static int st33zp24_i2c_read8_reg(struct udevice *dev, u8 tpm_register,
66 				  u8 *tpm_data, size_t tpm_size)
67 {
68 	int status;
69 	u8 data;
70 
71 	data = TPM_DUMMY_BYTE;
72 	status = st33zp24_i2c_write8_reg(dev, tpm_register, &data, 1);
73 	if (status < 0)
74 		return status;
75 
76 	return dm_i2c_read(dev, 0, tpm_data, tpm_size);
77 }
78 
79 /*
80  * st33zp24_i2c_write
81  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
82  * @param: phy_id, the phy description
83  * @param: tpm_register, the tpm tis register where the data should be written
84  * @param: tpm_data, the tpm_data to write inside the tpm_register
85  * @param: tpm_size, the length of the data
86  * @return: number of byte written successfully: should be one if success.
87  */
88 static int st33zp24_i2c_write(struct udevice *dev, u8 tpm_register,
89 			      const u8 *tpm_data, size_t tpm_size)
90 {
91 	return st33zp24_i2c_write8_reg(dev, tpm_register | TPM_WRITE_DIRECTION,
92 				       tpm_data, tpm_size);
93 }
94 
95 /*
96  * st33zp24_i2c_read
97  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
98  * @param: phy_id, the phy description
99  * @param: tpm_register, the tpm tis register where the data should be read
100  * @param: tpm_data, the TPM response
101  * @param: tpm_size, tpm TPM response size to read.
102  * @return: number of byte read successfully: should be one if success.
103  */
104 static int st33zp24_i2c_read(struct udevice *dev, u8 tpm_register,
105 			     u8 *tpm_data, size_t tpm_size)
106 {
107 	return st33zp24_i2c_read8_reg(dev, tpm_register, tpm_data, tpm_size);
108 }
109 
110 /*
111  * st33zp24_i2c_release_locality release the active locality
112  * @param: chip, the tpm chip description.
113  */
114 static void st33zp24_i2c_release_locality(struct udevice *dev)
115 {
116 	u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
117 
118 	st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
119 }
120 
121 /*
122  * st33zp24_i2c_check_locality if the locality is active
123  * @param: chip, the tpm chip description
124  * @return: the active locality or -EACCES.
125  */
126 static int st33zp24_i2c_check_locality(struct udevice *dev)
127 {
128 	struct tpm_chip *chip = dev_get_priv(dev);
129 	u8 data;
130 	u8 status;
131 
132 	status = st33zp24_i2c_read(dev, TPM_ACCESS, &data, 1);
133 	if (!status && (data &
134 		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
135 		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
136 		return chip->locality;
137 
138 	return -EACCES;
139 }
140 
141 /*
142  * st33zp24_i2c_request_locality request the TPM locality
143  * @param: chip, the chip description
144  * @return: the active locality or negative value.
145  */
146 static int st33zp24_i2c_request_locality(struct udevice *dev)
147 {
148 	struct tpm_chip *chip = dev_get_priv(dev);
149 	unsigned long start, stop;
150 	long ret;
151 	u8 data;
152 
153 	if (st33zp24_i2c_check_locality(dev) == chip->locality)
154 		return chip->locality;
155 
156 	data = TPM_ACCESS_REQUEST_USE;
157 	ret = st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
158 	if (ret < 0)
159 		return ret;
160 
161 	/* wait for locality activated */
162 	start = get_timer(0);
163 	stop = chip->timeout_a;
164 	do {
165 		if (st33zp24_i2c_check_locality(dev) >= 0)
166 			return chip->locality;
167 		udelay(TPM_TIMEOUT_MS * 1000);
168 	} while	 (get_timer(start) < stop);
169 
170 	return -EACCES;
171 }
172 
173 /*
174  * st33zp24_i2c_status return the TPM_STS register
175  * @param: chip, the tpm chip description
176  * @return: the TPM_STS register value.
177  */
178 static u8 st33zp24_i2c_status(struct udevice *dev)
179 {
180 	u8 data;
181 
182 	st33zp24_i2c_read(dev, TPM_STS, &data, 1);
183 
184 	return data;
185 }
186 
187 /*
188  * st33zp24_i2c_get_burstcount return the burstcount address 0x19 0x1A
189  * @param: chip, the chip description
190  * return: the burstcount or -TPM_DRIVER_ERR in case of error.
191  */
192 static int st33zp24_i2c_get_burstcount(struct udevice *dev)
193 {
194 	struct tpm_chip *chip = dev_get_priv(dev);
195 	unsigned long start, stop;
196 	int burstcnt, status;
197 	u8 tpm_reg, temp;
198 
199 	/* wait for burstcount */
200 	start = get_timer(0);
201 	stop = chip->timeout_d;
202 	do {
203 		tpm_reg = TPM_STS + 1;
204 		status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
205 		if (status < 0)
206 			return -EBUSY;
207 
208 		tpm_reg = TPM_STS + 2;
209 		burstcnt = temp;
210 		status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
211 		if (status < 0)
212 			return -EBUSY;
213 
214 		burstcnt |= temp << 8;
215 		if (burstcnt)
216 			return burstcnt;
217 		udelay(TIS_SHORT_TIMEOUT_MS * 1000);
218 	} while (get_timer(start) < stop);
219 
220 	return -EBUSY;
221 }
222 
223 /*
224  * st33zp24_i2c_cancel, cancel the current command execution or
225  * set STS to COMMAND READY.
226  * @param: chip, tpm_chip description.
227  */
228 static void st33zp24_i2c_cancel(struct udevice *dev)
229 {
230 	u8 data;
231 
232 	data = TPM_STS_COMMAND_READY;
233 	st33zp24_i2c_write(dev, TPM_STS, &data, 1);
234 }
235 
236 /*
237  * st33zp24_i2c_wait_for_stat wait for a TPM_STS value
238  * @param: chip, the tpm chip description
239  * @param: mask, the value mask to wait
240  * @param: timeout, the timeout
241  * @param: status,
242  * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
243  */
244 static int st33zp24_i2c_wait_for_stat(struct udevice *dev, u8 mask,
245 				      unsigned long timeout, int *status)
246 {
247 	unsigned long start, stop;
248 
249 	/* Check current status */
250 	*status = st33zp24_i2c_status(dev);
251 	if ((*status & mask) == mask)
252 		return 0;
253 
254 	start = get_timer(0);
255 	stop = timeout;
256 	do {
257 		udelay(TPM_TIMEOUT_MS * 1000);
258 		*status = st33zp24_i2c_status(dev);
259 		if ((*status & mask) == mask)
260 			return 0;
261 	} while (get_timer(start) < stop);
262 
263 	return -ETIME;
264 }
265 
266 /*
267  * st33zp24_i2c_recv_data receive data
268  * @param: chip, the tpm chip description
269  * @param: buf, the buffer where the data are received
270  * @param: count, the number of data to receive
271  * @return: the number of bytes read from TPM FIFO.
272  */
273 static int st33zp24_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
274 {
275 	struct tpm_chip *chip = dev_get_priv(dev);
276 	int size = 0, burstcnt, len, ret, status;
277 
278 	while (size < count &&
279 	       st33zp24_i2c_wait_for_stat(dev, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
280 				chip->timeout_c, &status) == 0) {
281 		burstcnt = st33zp24_i2c_get_burstcount(dev);
282 		if (burstcnt < 0)
283 			return burstcnt;
284 		len = min_t(int, burstcnt, count - size);
285 		ret = st33zp24_i2c_read(dev, TPM_DATA_FIFO, buf + size, len);
286 		if (ret < 0)
287 			return ret;
288 
289 		size += len;
290 	}
291 
292 	return size;
293 }
294 
295 /*
296  * st33zp24_i2c_recv received TPM response through TPM phy.
297  * @param: chip, tpm_chip description.
298  * @param: buf,	the buffer to store data.
299  * @param: count, the number of bytes that can received (sizeof buf).
300  * @return: Returns zero in case of success else -EIO.
301  */
302 static int st33zp24_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
303 {
304 	struct tpm_chip *chip = dev_get_priv(dev);
305 	int size;
306 	unsigned int expected;
307 
308 	if (!chip)
309 		return -ENODEV;
310 
311 	if (count < TPM_HEADER_SIZE) {
312 		size = -EIO;
313 		goto out;
314 	}
315 
316 	size = st33zp24_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
317 	if (size < TPM_HEADER_SIZE) {
318 		debug("TPM error, unable to read header\n");
319 		goto out;
320 	}
321 
322 	expected = get_unaligned_be32(buf + 2);
323 	if (expected > count || expected < TPM_HEADER_SIZE) {
324 		size = -EIO;
325 		goto out;
326 	}
327 
328 	size += st33zp24_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
329 				   expected - TPM_HEADER_SIZE);
330 	if (size < expected) {
331 		debug("TPM error, unable to read remaining bytes of result\n");
332 		size = -EIO;
333 		goto out;
334 	}
335 
336 out:
337 	st33zp24_i2c_cancel(dev);
338 	st33zp24_i2c_release_locality(dev);
339 
340 	return size;
341 }
342 
343 /*
344  * st33zp24_i2c_send send TPM commands through TPM phy.
345  * @param: chip, tpm_chip description.
346  * @param: buf,	the buffer to send.
347  * @param: len, the number of bytes to send.
348  * @return: Returns zero in case of success else the negative error code.
349  */
350 static int st33zp24_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
351 {
352 	struct tpm_chip *chip = dev_get_priv(dev);
353 	u32 i, size;
354 	int burstcnt, ret, status;
355 	u8 data, tpm_stat;
356 
357 	if (!chip)
358 		return -ENODEV;
359 	if (len < TPM_HEADER_SIZE)
360 		return -EIO;
361 
362 	ret = st33zp24_i2c_request_locality(dev);
363 	if (ret < 0)
364 		return ret;
365 
366 	tpm_stat = st33zp24_i2c_status(dev);
367 	if ((tpm_stat & TPM_STS_COMMAND_READY) == 0) {
368 		st33zp24_i2c_cancel(dev);
369 		if (st33zp24_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
370 					       chip->timeout_b, &status) < 0) {
371 			ret = -ETIME;
372 			goto out_err;
373 		}
374 	}
375 
376 	for (i = 0; i < len - 1;) {
377 		burstcnt = st33zp24_i2c_get_burstcount(dev);
378 		if (burstcnt < 0)
379 			return burstcnt;
380 
381 		size = min_t(int, len - i - 1, burstcnt);
382 		ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + i, size);
383 		if (ret < 0)
384 			goto out_err;
385 
386 		i += size;
387 	}
388 
389 	tpm_stat = st33zp24_i2c_status(dev);
390 	if ((tpm_stat & TPM_STS_DATA_EXPECT) == 0) {
391 		ret = -EIO;
392 		goto out_err;
393 	}
394 
395 	ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + len - 1, 1);
396 	if (ret < 0)
397 		goto out_err;
398 
399 	tpm_stat = st33zp24_i2c_status(dev);
400 	if ((tpm_stat & TPM_STS_DATA_EXPECT) != 0) {
401 		ret = -EIO;
402 		goto out_err;
403 	}
404 
405 	data = TPM_STS_GO;
406 	ret = st33zp24_i2c_write(dev, TPM_STS, &data, 1);
407 	if (ret < 0)
408 		goto out_err;
409 
410 	return len;
411 
412 out_err:
413 	st33zp24_i2c_cancel(dev);
414 	st33zp24_i2c_release_locality(dev);
415 
416 	return ret;
417 }
418 
419 static int st33zp24_i2c_cleanup(struct udevice *dev)
420 {
421 	st33zp24_i2c_cancel(dev);
422 	/*
423 	 * The TPM needs some time to clean up here,
424 	 * so we sleep rather than keeping the bus busy
425 	 */
426 	mdelay(2);
427 	st33zp24_i2c_release_locality(dev);
428 
429 	return 0;
430 }
431 
432 static int st33zp24_i2c_init(struct udevice *dev)
433 {
434 	struct tpm_chip *chip = dev_get_priv(dev);
435 
436 	chip->is_open = 1;
437 
438 	/* Default timeouts - these could move to the device tree */
439 	chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
440 	chip->timeout_b = TIS_LONG_TIMEOUT_MS;
441 	chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
442 	chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
443 
444 	chip->locality = LOCALITY0;
445 
446 	/*
447 	 * A timeout query to TPM can be placed here.
448 	 * Standard timeout values are used so far
449 	 */
450 
451 	return 0;
452 }
453 
454 static int st33zp24_i2c_open(struct udevice *dev)
455 {
456 	struct tpm_chip *chip = dev_get_priv(dev);
457 	int rc;
458 
459 	debug("%s: start\n", __func__);
460 	if (chip->is_open)
461 		return -EBUSY;
462 
463 	rc = st33zp24_i2c_init(dev);
464 	if (rc < 0)
465 		chip->is_open = 0;
466 
467 	return rc;
468 }
469 
470 static int st33zp24_i2c_close(struct udevice *dev)
471 {
472 	struct tpm_chip *chip = dev_get_priv(dev);
473 
474 	if (chip->is_open) {
475 		st33zp24_i2c_release_locality(dev);
476 		chip->is_open = 0;
477 		chip->vend_dev = 0;
478 	}
479 
480 	return 0;
481 }
482 
483 static int st33zp24_i2c_get_desc(struct udevice *dev, char *buf, int size)
484 {
485 	struct tpm_chip *chip = dev_get_priv(dev);
486 
487 	if (size < 50)
488 		return -ENOSPC;
489 
490 	return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
491 			chip->is_open ? "open" : "closed",
492 			dev->name,
493 			chip->vend_dev >> 16);
494 }
495 
496 static const struct tpm_ops st33zp24_i2c_tpm_ops = {
497 	.open = st33zp24_i2c_open,
498 	.close = st33zp24_i2c_close,
499 	.recv = st33zp24_i2c_recv,
500 	.send = st33zp24_i2c_send,
501 	.cleanup = st33zp24_i2c_cleanup,
502 	.get_desc = st33zp24_i2c_get_desc,
503 };
504 
505 static int st33zp24_i2c_probe(struct udevice *dev)
506 {
507 	struct tpm_chip *chip = dev_get_priv(dev);
508 
509 	/* Default timeouts */
510 	chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
511 	chip->timeout_b = TIS_LONG_TIMEOUT_MS;
512 	chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
513 	chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
514 
515 	chip->locality = LOCALITY0;
516 
517 	i2c_set_chip_offset_len(dev, 0);
518 
519 	debug("ST33ZP24 I2C TPM from STMicroelectronics found\n");
520 
521 	return 0;
522 }
523 
524 static int st33zp24_i2c_remove(struct udevice *dev)
525 {
526 	st33zp24_i2c_release_locality(dev);
527 
528 	return 0;
529 }
530 
531 static const struct udevice_id st33zp24_i2c_ids[] = {
532 	{ .compatible = "st,st33zp24-i2c" },
533 	{ }
534 };
535 
536 U_BOOT_DRIVER(st33zp24_i2c) = {
537 	.name   = "st33zp24-i2c",
538 	.id     = UCLASS_TPM,
539 	.of_match = of_match_ptr(st33zp24_i2c_ids),
540 	.probe  = st33zp24_i2c_probe,
541 	.remove = st33zp24_i2c_remove,
542 	.ops = &st33zp24_i2c_tpm_ops,
543 	.priv_auto_alloc_size = sizeof(struct tpm_chip),
544 };
545