1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
7 * The code in this file is based on the article "Writing a TPM Device Driver"
8 * published on http://ptgmedia.pearsoncmg.com.
10 * One principal difference is that in the simplest config the other than 0
11 * TPM localities do not get mapped by some devices (for instance, by Infineon
12 * slb9635), so this driver provides access to locality 0 only.
21 #define PREFIX "lpc_tpm: "
28 static const char * const chip_name[] = {
29 [SLB9635] = "Infineon SLB9635 TT 1.2",
30 [AT97SC3204] = "Atmel AT97SC3204",
33 static const u32 chip_didvid[] = {
35 [AT97SC3204] = 0x32041114,
55 struct tpm_tis_lpc_priv {
56 struct tpm_locality *regs;
60 * This pointer refers to the TPM chip, 5 of its localities are mapped as an
63 #define TPM_TOTAL_LOCALITIES 5
65 /* Some registers' bit field definitions */
66 #define TIS_STS_VALID (1 << 7) /* 0x80 */
67 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
68 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
69 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
70 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
71 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
73 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
74 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
75 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
76 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
77 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
78 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
79 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
81 #define TIS_STS_BURST_COUNT_MASK (0xffff)
82 #define TIS_STS_BURST_COUNT_SHIFT (8)
84 /* 1 second is plenty for anything TPM does. */
85 #define MAX_DELAY_US (1000 * 1000)
87 /* Retrieve burst count value out of the status register contents. */
88 static u16 burst_count(u32 status)
90 return (status >> TIS_STS_BURST_COUNT_SHIFT) &
91 TIS_STS_BURST_COUNT_MASK;
94 /* TPM access wrappers to support tracing */
95 static u8 tpm_read_byte(struct tpm_tis_lpc_priv *priv, const u8 *ptr)
98 debug(PREFIX "Read reg 0x%4.4x returns 0x%2.2x\n",
99 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, ret);
103 static u32 tpm_read_word(struct tpm_tis_lpc_priv *priv, const u32 *ptr)
105 u32 ret = readl(ptr);
106 debug(PREFIX "Read reg 0x%4.4x returns 0x%8.8x\n",
107 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, ret);
111 static void tpm_write_byte(struct tpm_tis_lpc_priv *priv, u8 value, u8 *ptr)
113 debug(PREFIX "Write reg 0x%4.4x with 0x%2.2x\n",
114 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, value);
118 static void tpm_write_word(struct tpm_tis_lpc_priv *priv, u32 value,
121 debug(PREFIX "Write reg 0x%4.4x with 0x%8.8x\n",
122 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, value);
129 * Wait for at least a second for a register to change its state to match the
130 * expected state. Normally the transition happens within microseconds.
132 * @reg - pointer to the TPM register
133 * @mask - bitmask for the bitfield(s) to watch
134 * @expected - value the field(s) are supposed to be set to
136 * Returns the register contents in case the expected value was found in the
137 * appropriate register bits, or -ETIMEDOUT on timeout.
139 static int tis_wait_reg(struct tpm_tis_lpc_priv *priv, u32 *reg, u8 mask,
142 u32 time_us = MAX_DELAY_US;
144 while (time_us > 0) {
145 u32 value = tpm_read_word(priv, reg);
146 if ((value & mask) == expected)
148 udelay(1); /* 1 us */
156 * Probe the TPM device and try determining its manufacturer/device name.
158 * Returns 0 on success, -ve on error
160 static int tpm_tis_lpc_probe(struct udevice *dev)
162 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
165 ulong chip_type = dev_get_driver_data(dev);
167 addr = dev_read_addr(dev);
168 if (addr == FDT_ADDR_T_NONE)
170 priv->regs = map_sysmem(addr, 0);
171 didvid = tpm_read_word(priv, &priv->regs[0].did_vid);
173 if (didvid != chip_didvid[chip_type]) {
175 vid = didvid & 0xffff;
176 did = (didvid >> 16) & 0xffff;
177 debug("Invalid vendor/device ID %04x/%04x\n", vid, did);
181 debug("Found TPM: %s\n", chip_name[chip_type]);
189 * send the passed in data to the TPM device.
191 * @data - address of the data to send, byte by byte
192 * @len - length of the data to send
194 * Returns 0 on success, -ve on error (in case the device does not accept
195 * the entire command).
197 static int tis_senddata(struct udevice *dev, const u8 *data, size_t len)
199 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
200 struct tpm_locality *regs = priv->regs;
207 value = tis_wait_reg(priv, ®s[locality].tpm_status,
208 TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY);
209 if (value == -ETIMEDOUT) {
210 printf("%s:%d - failed to get 'command_ready' status\n",
214 burst = burst_count(value);
219 /* Wait till the device is ready to accept more data. */
221 if (max_cycles++ == MAX_DELAY_US) {
222 printf("%s:%d failed to feed %zd bytes of %zd\n",
223 __FILE__, __LINE__, len - offset, len);
227 burst = burst_count(tpm_read_word(priv,
228 ®s[locality].tpm_status));
234 * Calculate number of bytes the TPM is ready to accept in one
237 * We want to send the last byte outside of the loop (hence
238 * the -1 below) to make sure that the 'expected' status bit
239 * changes to zero exactly after the last byte is fed into the
242 count = min((size_t)burst, len - offset - 1);
244 tpm_write_byte(priv, data[offset++],
245 ®s[locality].data);
247 value = tis_wait_reg(priv, ®s[locality].tpm_status,
248 TIS_STS_VALID, TIS_STS_VALID);
250 if ((value == -ETIMEDOUT) || !(value & TIS_STS_EXPECT)) {
251 printf("%s:%d TPM command feed overflow\n",
253 return value == -ETIMEDOUT ? value : -EIO;
256 burst = burst_count(value);
257 if ((offset == (len - 1)) && burst) {
259 * We need to be able to send the last byte to the
260 * device, so burst size must be nonzero before we
267 /* Send the last byte. */
268 tpm_write_byte(priv, data[offset++], ®s[locality].data);
270 * Verify that TPM does not expect any more data as part of this
273 value = tis_wait_reg(priv, ®s[locality].tpm_status,
274 TIS_STS_VALID, TIS_STS_VALID);
275 if ((value == -ETIMEDOUT) || (value & TIS_STS_EXPECT)) {
276 printf("%s:%d unexpected TPM status 0x%x\n",
277 __FILE__, __LINE__, value);
278 return value == -ETIMEDOUT ? value : -EIO;
281 /* OK, sitting pretty, let's start the command execution. */
282 tpm_write_word(priv, TIS_STS_TPM_GO, ®s[locality].tpm_status);
289 * read the TPM device response after a command was issued.
291 * @buffer - address where to read the response, byte by byte.
292 * @len - pointer to the size of buffer
294 * On success stores the number of received bytes to len and returns 0. On
295 * errors (misformatted TPM data or synchronization problems) returns
298 static int tis_readresponse(struct udevice *dev, u8 *buffer, size_t len)
300 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
301 struct tpm_locality *regs = priv->regs;
306 const u32 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
307 u32 expected_count = len;
310 /* Wait for the TPM to process the command. */
311 value = tis_wait_reg(priv, ®s[locality].tpm_status,
313 if (value == -ETIMEDOUT) {
314 printf("%s:%d failed processing command\n",
320 while ((burst = burst_count(value)) == 0) {
321 if (max_cycles++ == MAX_DELAY_US) {
322 printf("%s:%d TPM stuck on read\n",
327 value = tpm_read_word(priv, ®s[locality].tpm_status);
332 while (burst-- && (offset < expected_count)) {
333 buffer[offset++] = tpm_read_byte(priv,
334 ®s[locality].data);
338 * We got the first six bytes of the reply,
339 * let's figure out how many bytes to expect
340 * total - it is stored as a 4 byte number in
341 * network order, starting with offset 2 into
342 * the body of the reply.
347 sizeof(real_length));
348 expected_count = be32_to_cpu(real_length);
350 if ((expected_count < offset) ||
351 (expected_count > len)) {
352 printf("%s:%d bad response size %d\n",
360 /* Wait for the next portion. */
361 value = tis_wait_reg(priv, ®s[locality].tpm_status,
362 TIS_STS_VALID, TIS_STS_VALID);
363 if (value == -ETIMEDOUT) {
364 printf("%s:%d failed to read response\n",
369 if (offset == expected_count)
370 break; /* We got all we needed. */
372 } while ((value & has_data) == has_data);
375 * Make sure we indeed read all there was. The TIS_STS_VALID bit is
378 if (value & TIS_STS_DATA_AVAILABLE) {
379 printf("%s:%d wrong receive status %x\n",
380 __FILE__, __LINE__, value);
384 /* Tell the TPM that we are done. */
385 tpm_write_word(priv, TIS_STS_COMMAND_READY,
386 ®s[locality].tpm_status);
391 static int tpm_tis_lpc_close(struct udevice *dev)
393 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
394 struct tpm_locality *regs = priv->regs;
397 if (tpm_read_word(priv, ®s[locality].access) &
398 TIS_ACCESS_ACTIVE_LOCALITY) {
399 tpm_write_word(priv, TIS_ACCESS_ACTIVE_LOCALITY,
400 ®s[locality].access);
402 if (tis_wait_reg(priv, ®s[locality].access,
403 TIS_ACCESS_ACTIVE_LOCALITY, 0) == -ETIMEDOUT) {
404 printf("%s:%d - failed to release locality %d\n",
405 __FILE__, __LINE__, locality);
412 static int tpm_tis_lpc_open(struct udevice *dev)
414 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
415 struct tpm_locality *regs = priv->regs;
416 u8 locality = 0; /* we use locality zero for everything. */
419 ret = tpm_tis_lpc_close(dev);
421 printf("%s: Failed to close TPM\n", __func__);
425 /* now request access to locality. */
426 tpm_write_word(priv, TIS_ACCESS_REQUEST_USE, ®s[locality].access);
428 /* did we get a lock? */
429 ret = tis_wait_reg(priv, ®s[locality].access,
430 TIS_ACCESS_ACTIVE_LOCALITY,
431 TIS_ACCESS_ACTIVE_LOCALITY);
432 if (ret == -ETIMEDOUT) {
433 printf("%s:%d - failed to lock locality %d\n",
434 __FILE__, __LINE__, locality);
438 tpm_write_word(priv, TIS_STS_COMMAND_READY,
439 ®s[locality].tpm_status);
444 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
446 ulong chip_type = dev_get_driver_data(dev);
451 return snprintf(buf, size, "1.2 TPM (%s)",
452 chip_name[chip_type]);
456 static const struct tpm_ops tpm_tis_lpc_ops = {
457 .open = tpm_tis_lpc_open,
458 .close = tpm_tis_lpc_close,
459 .get_desc = tpm_tis_get_desc,
460 .send = tis_senddata,
461 .recv = tis_readresponse,
464 static const struct udevice_id tpm_tis_lpc_ids[] = {
465 { .compatible = "infineon,slb9635lpc", .data = SLB9635 },
466 { .compatible = "atmel,at97sc3204", .data = AT97SC3204 },
470 U_BOOT_DRIVER(tpm_tis_lpc) = {
471 .name = "tpm_tis_lpc",
473 .of_match = tpm_tis_lpc_ids,
474 .ops = &tpm_tis_lpc_ops,
475 .probe = tpm_tis_lpc_probe,
476 .priv_auto_alloc_size = sizeof(struct tpm_tis_lpc_priv),