1 // SPDX-License-Identifier: GPL-2.0+
3 * STMicroelectronics TPM ST33ZP24 I2C UBOOT driver
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author(s): Christophe Ricard <christophe-h.ricard@st.com> for STMicroelectronics.
8 * Description: Device driver for ST33ZP24 I2C TPM TCG.
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.
22 #include <linux/delay.h>
23 #include <linux/types.h>
24 #include <asm/unaligned.h>
27 #include "tpm_internal.h"
29 #define TPM_ACCESS 0x0
31 #define TPM_DATA_FIFO 0x24
35 #define TPM_DUMMY_BYTE 0xAA
36 #define TPM_ST33ZP24_I2C_SLAVE_ADDR 0x13
38 #define TPM_WRITE_DIRECTION 0x80
41 * st33zp24_i2c_write8_reg
42 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
43 * @param: tpm_register, the tpm tis register where the data should be written
44 * @param: tpm_data, the tpm_data to write inside the tpm_register
45 * @param: tpm_size, The length of the data
46 * @return: Number of byte written successfully else an error code.
48 static int st33zp24_i2c_write8_reg(struct udevice *dev, u8 tpm_register,
49 const u8 *tpm_data, size_t tpm_size)
51 struct tpm_chip_priv *chip_priv = dev_get_uclass_priv(dev);
53 chip_priv->buf[0] = tpm_register;
54 memcpy(chip_priv->buf + 1, tpm_data, tpm_size);
56 return dm_i2c_write(dev, 0, chip_priv->buf, tpm_size + 1);
60 * st33zp24_i2c_read8_reg
61 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
62 * @param: tpm_register, the tpm tis register where the data should be read
63 * @param: tpm_data, the TPM response
64 * @param: tpm_size, tpm TPM response size to read.
65 * @return: Number of byte read successfully else an error code.
67 static int st33zp24_i2c_read8_reg(struct udevice *dev, u8 tpm_register,
68 u8 *tpm_data, size_t tpm_size)
73 data = TPM_DUMMY_BYTE;
74 status = st33zp24_i2c_write8_reg(dev, tpm_register, &data, 1);
78 return dm_i2c_read(dev, 0, tpm_data, tpm_size);
83 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
84 * @param: phy_id, the phy description
85 * @param: tpm_register, the tpm tis register where the data should be written
86 * @param: tpm_data, the tpm_data to write inside the tpm_register
87 * @param: tpm_size, the length of the data
88 * @return: number of byte written successfully: should be one if success.
90 static int st33zp24_i2c_write(struct udevice *dev, u8 tpm_register,
91 const u8 *tpm_data, size_t tpm_size)
93 return st33zp24_i2c_write8_reg(dev, tpm_register | TPM_WRITE_DIRECTION,
99 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
100 * @param: phy_id, the phy description
101 * @param: tpm_register, the tpm tis register where the data should be read
102 * @param: tpm_data, the TPM response
103 * @param: tpm_size, tpm TPM response size to read.
104 * @return: number of byte read successfully: should be one if success.
106 static int st33zp24_i2c_read(struct udevice *dev, u8 tpm_register,
107 u8 *tpm_data, size_t tpm_size)
109 return st33zp24_i2c_read8_reg(dev, tpm_register, tpm_data, tpm_size);
113 * st33zp24_i2c_release_locality release the active locality
114 * @param: chip, the tpm chip description.
116 static void st33zp24_i2c_release_locality(struct udevice *dev)
118 u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
120 st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
124 * st33zp24_i2c_check_locality if the locality is active
125 * @param: chip, the tpm chip description
126 * @return: the active locality or -EACCES.
128 static int st33zp24_i2c_check_locality(struct udevice *dev)
130 struct tpm_chip *chip = dev_get_priv(dev);
134 status = st33zp24_i2c_read(dev, TPM_ACCESS, &data, 1);
135 if (!status && (data &
136 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
137 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
138 return chip->locality;
144 * st33zp24_i2c_request_locality request the TPM locality
145 * @param: chip, the chip description
146 * @return: the active locality or negative value.
148 static int st33zp24_i2c_request_locality(struct udevice *dev)
150 struct tpm_chip *chip = dev_get_priv(dev);
151 unsigned long start, stop;
155 if (st33zp24_i2c_check_locality(dev) == chip->locality)
156 return chip->locality;
158 data = TPM_ACCESS_REQUEST_USE;
159 ret = st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
163 /* wait for locality activated */
164 start = get_timer(0);
165 stop = chip->timeout_a;
167 if (st33zp24_i2c_check_locality(dev) >= 0)
168 return chip->locality;
169 udelay(TPM_TIMEOUT_MS * 1000);
170 } while (get_timer(start) < stop);
176 * st33zp24_i2c_status return the TPM_STS register
177 * @param: chip, the tpm chip description
178 * @return: the TPM_STS register value.
180 static u8 st33zp24_i2c_status(struct udevice *dev)
184 st33zp24_i2c_read(dev, TPM_STS, &data, 1);
190 * st33zp24_i2c_get_burstcount return the burstcount address 0x19 0x1A
191 * @param: chip, the chip description
192 * return: the burstcount or -TPM_DRIVER_ERR in case of error.
194 static int st33zp24_i2c_get_burstcount(struct udevice *dev)
196 struct tpm_chip *chip = dev_get_priv(dev);
197 unsigned long start, stop;
198 int burstcnt, status;
201 /* wait for burstcount */
202 start = get_timer(0);
203 stop = chip->timeout_d;
205 tpm_reg = TPM_STS + 1;
206 status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
210 tpm_reg = TPM_STS + 2;
212 status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
216 burstcnt |= temp << 8;
219 udelay(TIS_SHORT_TIMEOUT_MS * 1000);
220 } while (get_timer(start) < stop);
226 * st33zp24_i2c_cancel, cancel the current command execution or
227 * set STS to COMMAND READY.
228 * @param: chip, tpm_chip description.
230 static void st33zp24_i2c_cancel(struct udevice *dev)
234 data = TPM_STS_COMMAND_READY;
235 st33zp24_i2c_write(dev, TPM_STS, &data, 1);
239 * st33zp24_i2c_wait_for_stat wait for a TPM_STS value
240 * @param: chip, the tpm chip description
241 * @param: mask, the value mask to wait
242 * @param: timeout, the timeout
244 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
246 static int st33zp24_i2c_wait_for_stat(struct udevice *dev, u8 mask,
247 unsigned long timeout, int *status)
249 unsigned long start, stop;
251 /* Check current status */
252 *status = st33zp24_i2c_status(dev);
253 if ((*status & mask) == mask)
256 start = get_timer(0);
259 udelay(TPM_TIMEOUT_MS * 1000);
260 *status = st33zp24_i2c_status(dev);
261 if ((*status & mask) == mask)
263 } while (get_timer(start) < stop);
269 * st33zp24_i2c_recv_data receive data
270 * @param: chip, the tpm chip description
271 * @param: buf, the buffer where the data are received
272 * @param: count, the number of data to receive
273 * @return: the number of bytes read from TPM FIFO.
275 static int st33zp24_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
277 struct tpm_chip *chip = dev_get_priv(dev);
278 int size = 0, burstcnt, len, ret, status;
280 while (size < count &&
281 st33zp24_i2c_wait_for_stat(dev, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
282 chip->timeout_c, &status) == 0) {
283 burstcnt = st33zp24_i2c_get_burstcount(dev);
286 len = min_t(int, burstcnt, count - size);
287 ret = st33zp24_i2c_read(dev, TPM_DATA_FIFO, buf + size, len);
298 * st33zp24_i2c_recv received TPM response through TPM phy.
299 * @param: chip, tpm_chip description.
300 * @param: buf, the buffer to store data.
301 * @param: count, the number of bytes that can received (sizeof buf).
302 * @return: Returns zero in case of success else -EIO.
304 static int st33zp24_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
306 struct tpm_chip *chip = dev_get_priv(dev);
308 unsigned int expected;
313 if (count < TPM_HEADER_SIZE) {
318 size = st33zp24_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
319 if (size < TPM_HEADER_SIZE) {
320 debug("TPM error, unable to read header\n");
324 expected = get_unaligned_be32(buf + 2);
325 if (expected > count || expected < TPM_HEADER_SIZE) {
330 size += st33zp24_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
331 expected - TPM_HEADER_SIZE);
332 if (size < expected) {
333 debug("TPM error, unable to read remaining bytes of result\n");
339 st33zp24_i2c_cancel(dev);
340 st33zp24_i2c_release_locality(dev);
346 * st33zp24_i2c_send send TPM commands through TPM phy.
347 * @param: chip, tpm_chip description.
348 * @param: buf, the buffer to send.
349 * @param: len, the number of bytes to send.
350 * @return: Returns zero in case of success else the negative error code.
352 static int st33zp24_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
354 struct tpm_chip *chip = dev_get_priv(dev);
356 int burstcnt, ret, status;
361 if (len < TPM_HEADER_SIZE)
364 ret = st33zp24_i2c_request_locality(dev);
368 tpm_stat = st33zp24_i2c_status(dev);
369 if ((tpm_stat & TPM_STS_COMMAND_READY) == 0) {
370 st33zp24_i2c_cancel(dev);
371 if (st33zp24_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
372 chip->timeout_b, &status) < 0) {
378 for (i = 0; i < len - 1;) {
379 burstcnt = st33zp24_i2c_get_burstcount(dev);
383 size = min_t(int, len - i - 1, burstcnt);
384 ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + i, size);
391 tpm_stat = st33zp24_i2c_status(dev);
392 if ((tpm_stat & TPM_STS_DATA_EXPECT) == 0) {
397 ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + len - 1, 1);
401 tpm_stat = st33zp24_i2c_status(dev);
402 if ((tpm_stat & TPM_STS_DATA_EXPECT) != 0) {
408 ret = st33zp24_i2c_write(dev, TPM_STS, &data, 1);
415 st33zp24_i2c_cancel(dev);
416 st33zp24_i2c_release_locality(dev);
421 static int st33zp24_i2c_cleanup(struct udevice *dev)
423 st33zp24_i2c_cancel(dev);
425 * The TPM needs some time to clean up here,
426 * so we sleep rather than keeping the bus busy
429 st33zp24_i2c_release_locality(dev);
434 static int st33zp24_i2c_init(struct udevice *dev)
436 struct tpm_chip *chip = dev_get_priv(dev);
440 /* Default timeouts - these could move to the device tree */
441 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
442 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
443 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
444 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
446 chip->locality = LOCALITY0;
449 * A timeout query to TPM can be placed here.
450 * Standard timeout values are used so far
456 static int st33zp24_i2c_open(struct udevice *dev)
458 struct tpm_chip *chip = dev_get_priv(dev);
461 debug("%s: start\n", __func__);
465 rc = st33zp24_i2c_init(dev);
472 static int st33zp24_i2c_close(struct udevice *dev)
474 struct tpm_chip *chip = dev_get_priv(dev);
477 st33zp24_i2c_release_locality(dev);
485 static int st33zp24_i2c_get_desc(struct udevice *dev, char *buf, int size)
487 struct tpm_chip *chip = dev_get_priv(dev);
492 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
493 chip->is_open ? "open" : "closed",
495 chip->vend_dev >> 16);
498 static const struct tpm_ops st33zp24_i2c_tpm_ops = {
499 .open = st33zp24_i2c_open,
500 .close = st33zp24_i2c_close,
501 .recv = st33zp24_i2c_recv,
502 .send = st33zp24_i2c_send,
503 .cleanup = st33zp24_i2c_cleanup,
504 .get_desc = st33zp24_i2c_get_desc,
507 static int st33zp24_i2c_probe(struct udevice *dev)
509 struct tpm_chip *chip = dev_get_priv(dev);
511 /* Default timeouts */
512 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
513 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
514 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
515 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
517 chip->locality = LOCALITY0;
519 i2c_set_chip_offset_len(dev, 0);
521 debug("ST33ZP24 I2C TPM from STMicroelectronics found\n");
526 static int st33zp24_i2c_remove(struct udevice *dev)
528 st33zp24_i2c_release_locality(dev);
533 static const struct udevice_id st33zp24_i2c_ids[] = {
534 { .compatible = "st,st33zp24-i2c" },
538 U_BOOT_DRIVER(st33zp24_i2c) = {
539 .name = "st33zp24-i2c",
541 .of_match = of_match_ptr(st33zp24_i2c_ids),
542 .probe = st33zp24_i2c_probe,
543 .remove = st33zp24_i2c_remove,
544 .ops = &st33zp24_i2c_tpm_ops,
545 .priv_auto_alloc_size = sizeof(struct tpm_chip),