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 #include <linux/delay.h>
23 #define PREFIX "lpc_tpm: "
30 static const char * const chip_name[] = {
31 [SLB9635] = "Infineon SLB9635 TT 1.2",
32 [AT97SC3204] = "Atmel AT97SC3204",
35 static const u32 chip_didvid[] = {
37 [AT97SC3204] = 0x32041114,
57 struct tpm_tis_lpc_priv {
58 struct tpm_locality *regs;
62 * This pointer refers to the TPM chip, 5 of its localities are mapped as an
65 #define TPM_TOTAL_LOCALITIES 5
67 /* Some registers' bit field definitions */
68 #define TIS_STS_VALID (1 << 7) /* 0x80 */
69 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
70 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
71 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
72 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
73 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
75 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
76 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
77 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
78 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
79 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
80 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
81 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
83 #define TIS_STS_BURST_COUNT_MASK (0xffff)
84 #define TIS_STS_BURST_COUNT_SHIFT (8)
86 /* 1 second is plenty for anything TPM does. */
87 #define MAX_DELAY_US (1000 * 1000)
89 /* Retrieve burst count value out of the status register contents. */
90 static u16 burst_count(u32 status)
92 return (status >> TIS_STS_BURST_COUNT_SHIFT) &
93 TIS_STS_BURST_COUNT_MASK;
96 /* TPM access wrappers to support tracing */
97 static u8 tpm_read_byte(struct tpm_tis_lpc_priv *priv, const u8 *ptr)
100 debug(PREFIX "Read reg 0x%4.4x returns 0x%2.2x\n",
101 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, ret);
105 static u32 tpm_read_word(struct tpm_tis_lpc_priv *priv, const u32 *ptr)
107 u32 ret = readl(ptr);
108 debug(PREFIX "Read reg 0x%4.4x returns 0x%8.8x\n",
109 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, ret);
113 static void tpm_write_byte(struct tpm_tis_lpc_priv *priv, u8 value, u8 *ptr)
115 debug(PREFIX "Write reg 0x%4.4x with 0x%2.2x\n",
116 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, value);
120 static void tpm_write_word(struct tpm_tis_lpc_priv *priv, u32 value,
123 debug(PREFIX "Write reg 0x%4.4x with 0x%8.8x\n",
124 (u32)(uintptr_t)ptr - (u32)(uintptr_t)priv->regs, value);
131 * Wait for at least a second for a register to change its state to match the
132 * expected state. Normally the transition happens within microseconds.
134 * @reg - pointer to the TPM register
135 * @mask - bitmask for the bitfield(s) to watch
136 * @expected - value the field(s) are supposed to be set to
138 * Returns the register contents in case the expected value was found in the
139 * appropriate register bits, or -ETIMEDOUT on timeout.
141 static int tis_wait_reg(struct tpm_tis_lpc_priv *priv, u32 *reg, u8 mask,
144 u32 time_us = MAX_DELAY_US;
146 while (time_us > 0) {
147 u32 value = tpm_read_word(priv, reg);
148 if ((value & mask) == expected)
150 udelay(1); /* 1 us */
158 * Probe the TPM device and try determining its manufacturer/device name.
160 * Returns 0 on success, -ve on error
162 static int tpm_tis_lpc_probe(struct udevice *dev)
164 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
167 ulong chip_type = dev_get_driver_data(dev);
169 addr = dev_read_addr(dev);
170 if (addr == FDT_ADDR_T_NONE)
172 priv->regs = map_sysmem(addr, 0);
173 didvid = tpm_read_word(priv, &priv->regs[0].did_vid);
175 if (didvid != chip_didvid[chip_type]) {
177 vid = didvid & 0xffff;
178 did = (didvid >> 16) & 0xffff;
179 debug("Invalid vendor/device ID %04x/%04x\n", vid, did);
183 debug("Found TPM: %s\n", chip_name[chip_type]);
191 * send the passed in data to the TPM device.
193 * @data - address of the data to send, byte by byte
194 * @len - length of the data to send
196 * Returns 0 on success, -ve on error (in case the device does not accept
197 * the entire command).
199 static int tis_senddata(struct udevice *dev, const u8 *data, size_t len)
201 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
202 struct tpm_locality *regs = priv->regs;
209 value = tis_wait_reg(priv, ®s[locality].tpm_status,
210 TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY);
211 if (value == -ETIMEDOUT) {
212 printf("%s:%d - failed to get 'command_ready' status\n",
216 burst = burst_count(value);
221 /* Wait till the device is ready to accept more data. */
223 if (max_cycles++ == MAX_DELAY_US) {
224 printf("%s:%d failed to feed %zd bytes of %zd\n",
225 __FILE__, __LINE__, len - offset, len);
229 burst = burst_count(tpm_read_word(priv,
230 ®s[locality].tpm_status));
236 * Calculate number of bytes the TPM is ready to accept in one
239 * We want to send the last byte outside of the loop (hence
240 * the -1 below) to make sure that the 'expected' status bit
241 * changes to zero exactly after the last byte is fed into the
244 count = min((size_t)burst, len - offset - 1);
246 tpm_write_byte(priv, data[offset++],
247 ®s[locality].data);
249 value = tis_wait_reg(priv, ®s[locality].tpm_status,
250 TIS_STS_VALID, TIS_STS_VALID);
252 if ((value == -ETIMEDOUT) || !(value & TIS_STS_EXPECT)) {
253 printf("%s:%d TPM command feed overflow\n",
255 return value == -ETIMEDOUT ? value : -EIO;
258 burst = burst_count(value);
259 if ((offset == (len - 1)) && burst) {
261 * We need to be able to send the last byte to the
262 * device, so burst size must be nonzero before we
269 /* Send the last byte. */
270 tpm_write_byte(priv, data[offset++], ®s[locality].data);
272 * Verify that TPM does not expect any more data as part of this
275 value = tis_wait_reg(priv, ®s[locality].tpm_status,
276 TIS_STS_VALID, TIS_STS_VALID);
277 if ((value == -ETIMEDOUT) || (value & TIS_STS_EXPECT)) {
278 printf("%s:%d unexpected TPM status 0x%x\n",
279 __FILE__, __LINE__, value);
280 return value == -ETIMEDOUT ? value : -EIO;
283 /* OK, sitting pretty, let's start the command execution. */
284 tpm_write_word(priv, TIS_STS_TPM_GO, ®s[locality].tpm_status);
291 * read the TPM device response after a command was issued.
293 * @buffer - address where to read the response, byte by byte.
294 * @len - pointer to the size of buffer
296 * On success stores the number of received bytes to len and returns 0. On
297 * errors (misformatted TPM data or synchronization problems) returns
300 static int tis_readresponse(struct udevice *dev, u8 *buffer, size_t len)
302 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
303 struct tpm_locality *regs = priv->regs;
308 const u32 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
309 u32 expected_count = len;
312 /* Wait for the TPM to process the command. */
313 value = tis_wait_reg(priv, ®s[locality].tpm_status,
315 if (value == -ETIMEDOUT) {
316 printf("%s:%d failed processing command\n",
322 while ((burst = burst_count(value)) == 0) {
323 if (max_cycles++ == MAX_DELAY_US) {
324 printf("%s:%d TPM stuck on read\n",
329 value = tpm_read_word(priv, ®s[locality].tpm_status);
334 while (burst-- && (offset < expected_count)) {
335 buffer[offset++] = tpm_read_byte(priv,
336 ®s[locality].data);
340 * We got the first six bytes of the reply,
341 * let's figure out how many bytes to expect
342 * total - it is stored as a 4 byte number in
343 * network order, starting with offset 2 into
344 * the body of the reply.
349 sizeof(real_length));
350 expected_count = be32_to_cpu(real_length);
352 if ((expected_count < offset) ||
353 (expected_count > len)) {
354 printf("%s:%d bad response size %d\n",
362 /* Wait for the next portion. */
363 value = tis_wait_reg(priv, ®s[locality].tpm_status,
364 TIS_STS_VALID, TIS_STS_VALID);
365 if (value == -ETIMEDOUT) {
366 printf("%s:%d failed to read response\n",
371 if (offset == expected_count)
372 break; /* We got all we needed. */
374 } while ((value & has_data) == has_data);
377 * Make sure we indeed read all there was. The TIS_STS_VALID bit is
380 if (value & TIS_STS_DATA_AVAILABLE) {
381 printf("%s:%d wrong receive status %x\n",
382 __FILE__, __LINE__, value);
386 /* Tell the TPM that we are done. */
387 tpm_write_word(priv, TIS_STS_COMMAND_READY,
388 ®s[locality].tpm_status);
393 static int tpm_tis_lpc_close(struct udevice *dev)
395 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
396 struct tpm_locality *regs = priv->regs;
399 if (tpm_read_word(priv, ®s[locality].access) &
400 TIS_ACCESS_ACTIVE_LOCALITY) {
401 tpm_write_word(priv, TIS_ACCESS_ACTIVE_LOCALITY,
402 ®s[locality].access);
404 if (tis_wait_reg(priv, ®s[locality].access,
405 TIS_ACCESS_ACTIVE_LOCALITY, 0) == -ETIMEDOUT) {
406 printf("%s:%d - failed to release locality %d\n",
407 __FILE__, __LINE__, locality);
414 static int tpm_tis_lpc_open(struct udevice *dev)
416 struct tpm_tis_lpc_priv *priv = dev_get_priv(dev);
417 struct tpm_locality *regs = priv->regs;
418 u8 locality = 0; /* we use locality zero for everything. */
421 ret = tpm_tis_lpc_close(dev);
423 printf("%s: Failed to close TPM\n", __func__);
427 /* now request access to locality. */
428 tpm_write_word(priv, TIS_ACCESS_REQUEST_USE, ®s[locality].access);
430 /* did we get a lock? */
431 ret = tis_wait_reg(priv, ®s[locality].access,
432 TIS_ACCESS_ACTIVE_LOCALITY,
433 TIS_ACCESS_ACTIVE_LOCALITY);
434 if (ret == -ETIMEDOUT) {
435 printf("%s:%d - failed to lock locality %d\n",
436 __FILE__, __LINE__, locality);
440 tpm_write_word(priv, TIS_STS_COMMAND_READY,
441 ®s[locality].tpm_status);
446 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
448 ulong chip_type = dev_get_driver_data(dev);
453 return snprintf(buf, size, "1.2 TPM (%s)",
454 chip_name[chip_type]);
458 static const struct tpm_ops tpm_tis_lpc_ops = {
459 .open = tpm_tis_lpc_open,
460 .close = tpm_tis_lpc_close,
461 .get_desc = tpm_tis_get_desc,
462 .send = tis_senddata,
463 .recv = tis_readresponse,
466 static const struct udevice_id tpm_tis_lpc_ids[] = {
467 { .compatible = "infineon,slb9635lpc", .data = SLB9635 },
468 { .compatible = "atmel,at97sc3204", .data = AT97SC3204 },
472 U_BOOT_DRIVER(tpm_tis_lpc) = {
473 .name = "tpm_tis_lpc",
475 .of_match = tpm_tis_lpc_ids,
476 .ops = &tpm_tis_lpc_ops,
477 .probe = tpm_tis_lpc_probe,
478 .priv_auto = sizeof(struct tpm_tis_lpc_priv),