2 * Copyright (c) 2011 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
8 * The code in this file is based on the article "Writing a TPM Device Driver"
9 * published on http://ptgmedia.pearsoncmg.com.
11 * One principal difference is that in the simplest config the other than 0
12 * TPM localities do not get mapped by some devices (for instance, by Infineon
13 * slb9635), so this driver provides access to locality 0 only.
22 #define PREFIX "lpc_tpm: "
29 static const char * const chip_name
[] = {
30 [SLB9635
] = "Infineon SLB9635 TT 1.2",
31 [AT97SC3204
] = "Atmel AT97SC3204",
34 static const u32 chip_didvid
[] = {
36 [AT97SC3204
] = 0x32041114,
56 struct tpm_tis_lpc_priv
{
57 struct tpm_locality
*regs
;
61 * This pointer refers to the TPM chip, 5 of its localities are mapped as an
64 #define TPM_TOTAL_LOCALITIES 5
66 /* Some registers' bit field definitions */
67 #define TIS_STS_VALID (1 << 7) /* 0x80 */
68 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
69 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
70 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
71 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
72 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
74 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
75 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
76 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
77 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
78 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
79 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
80 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
82 #define TIS_STS_BURST_COUNT_MASK (0xffff)
83 #define TIS_STS_BURST_COUNT_SHIFT (8)
85 /* 1 second is plenty for anything TPM does. */
86 #define MAX_DELAY_US (1000 * 1000)
88 /* Retrieve burst count value out of the status register contents. */
89 static u16
burst_count(u32 status
)
91 return (status
>> TIS_STS_BURST_COUNT_SHIFT
) &
92 TIS_STS_BURST_COUNT_MASK
;
95 /* TPM access wrappers to support tracing */
96 static u8
tpm_read_byte(struct tpm_tis_lpc_priv
*priv
, const u8
*ptr
)
99 debug(PREFIX
"Read reg 0x%4.4x returns 0x%2.2x\n",
100 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, ret
);
104 static u32
tpm_read_word(struct tpm_tis_lpc_priv
*priv
, const u32
*ptr
)
106 u32 ret
= readl(ptr
);
107 debug(PREFIX
"Read reg 0x%4.4x returns 0x%8.8x\n",
108 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, ret
);
112 static void tpm_write_byte(struct tpm_tis_lpc_priv
*priv
, u8 value
, u8
*ptr
)
114 debug(PREFIX
"Write reg 0x%4.4x with 0x%2.2x\n",
115 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, value
);
119 static void tpm_write_word(struct tpm_tis_lpc_priv
*priv
, u32 value
,
122 debug(PREFIX
"Write reg 0x%4.4x with 0x%8.8x\n",
123 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, value
);
130 * Wait for at least a second for a register to change its state to match the
131 * expected state. Normally the transition happens within microseconds.
133 * @reg - pointer to the TPM register
134 * @mask - bitmask for the bitfield(s) to watch
135 * @expected - value the field(s) are supposed to be set to
137 * Returns the register contents in case the expected value was found in the
138 * appropriate register bits, or -ETIMEDOUT on timeout.
140 static int tis_wait_reg(struct tpm_tis_lpc_priv
*priv
, u32
*reg
, u8 mask
,
143 u32 time_us
= MAX_DELAY_US
;
145 while (time_us
> 0) {
146 u32 value
= tpm_read_word(priv
, reg
);
147 if ((value
& mask
) == expected
)
149 udelay(1); /* 1 us */
157 * Probe the TPM device and try determining its manufacturer/device name.
159 * Returns 0 on success, -ve on error
161 static int tpm_tis_lpc_probe(struct udevice
*dev
)
163 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
166 ulong chip_type
= dev_get_driver_data(dev
);
168 addr
= devfdt_get_addr(dev
);
169 if (addr
== FDT_ADDR_T_NONE
)
171 priv
->regs
= map_sysmem(addr
, 0);
172 didvid
= tpm_read_word(priv
, &priv
->regs
[0].did_vid
);
174 if (didvid
!= chip_didvid
[chip_type
]) {
176 vid
= didvid
& 0xffff;
177 did
= (didvid
>> 16) & 0xffff;
178 debug("Invalid vendor/device ID %04x/%04x\n", vid
, did
);
182 debug("Found TPM: %s\n", chip_name
[chip_type
]);
190 * send the passed in data to the TPM device.
192 * @data - address of the data to send, byte by byte
193 * @len - length of the data to send
195 * Returns 0 on success, -ve on error (in case the device does not accept
196 * the entire command).
198 static int tis_senddata(struct udevice
*dev
, const u8
*data
, size_t len
)
200 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
201 struct tpm_locality
*regs
= priv
->regs
;
208 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
209 TIS_STS_COMMAND_READY
, TIS_STS_COMMAND_READY
);
210 if (value
== -ETIMEDOUT
) {
211 printf("%s:%d - failed to get 'command_ready' status\n",
215 burst
= burst_count(value
);
220 /* Wait till the device is ready to accept more data. */
222 if (max_cycles
++ == MAX_DELAY_US
) {
223 printf("%s:%d failed to feed %zd bytes of %zd\n",
224 __FILE__
, __LINE__
, len
- offset
, len
);
228 burst
= burst_count(tpm_read_word(priv
,
229 ®s
[locality
].tpm_status
));
235 * Calculate number of bytes the TPM is ready to accept in one
238 * We want to send the last byte outside of the loop (hence
239 * the -1 below) to make sure that the 'expected' status bit
240 * changes to zero exactly after the last byte is fed into the
243 count
= min((size_t)burst
, len
- offset
- 1);
245 tpm_write_byte(priv
, data
[offset
++],
246 ®s
[locality
].data
);
248 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
249 TIS_STS_VALID
, TIS_STS_VALID
);
251 if ((value
== -ETIMEDOUT
) || !(value
& TIS_STS_EXPECT
)) {
252 printf("%s:%d TPM command feed overflow\n",
254 return value
== -ETIMEDOUT
? value
: -EIO
;
257 burst
= burst_count(value
);
258 if ((offset
== (len
- 1)) && burst
) {
260 * We need to be able to send the last byte to the
261 * device, so burst size must be nonzero before we
268 /* Send the last byte. */
269 tpm_write_byte(priv
, data
[offset
++], ®s
[locality
].data
);
271 * Verify that TPM does not expect any more data as part of this
274 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
275 TIS_STS_VALID
, TIS_STS_VALID
);
276 if ((value
== -ETIMEDOUT
) || (value
& TIS_STS_EXPECT
)) {
277 printf("%s:%d unexpected TPM status 0x%x\n",
278 __FILE__
, __LINE__
, value
);
279 return value
== -ETIMEDOUT
? value
: -EIO
;
282 /* OK, sitting pretty, let's start the command execution. */
283 tpm_write_word(priv
, TIS_STS_TPM_GO
, ®s
[locality
].tpm_status
);
290 * read the TPM device response after a command was issued.
292 * @buffer - address where to read the response, byte by byte.
293 * @len - pointer to the size of buffer
295 * On success stores the number of received bytes to len and returns 0. On
296 * errors (misformatted TPM data or synchronization problems) returns
299 static int tis_readresponse(struct udevice
*dev
, u8
*buffer
, size_t len
)
301 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
302 struct tpm_locality
*regs
= priv
->regs
;
307 const u32 has_data
= TIS_STS_DATA_AVAILABLE
| TIS_STS_VALID
;
308 u32 expected_count
= len
;
311 /* Wait for the TPM to process the command. */
312 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
314 if (value
== -ETIMEDOUT
) {
315 printf("%s:%d failed processing command\n",
321 while ((burst
= burst_count(value
)) == 0) {
322 if (max_cycles
++ == MAX_DELAY_US
) {
323 printf("%s:%d TPM stuck on read\n",
328 value
= tpm_read_word(priv
, ®s
[locality
].tpm_status
);
333 while (burst
-- && (offset
< expected_count
)) {
334 buffer
[offset
++] = tpm_read_byte(priv
,
335 ®s
[locality
].data
);
339 * We got the first six bytes of the reply,
340 * let's figure out how many bytes to expect
341 * total - it is stored as a 4 byte number in
342 * network order, starting with offset 2 into
343 * the body of the reply.
348 sizeof(real_length
));
349 expected_count
= be32_to_cpu(real_length
);
351 if ((expected_count
< offset
) ||
352 (expected_count
> len
)) {
353 printf("%s:%d bad response size %d\n",
361 /* Wait for the next portion. */
362 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
363 TIS_STS_VALID
, TIS_STS_VALID
);
364 if (value
== -ETIMEDOUT
) {
365 printf("%s:%d failed to read response\n",
370 if (offset
== expected_count
)
371 break; /* We got all we needed. */
373 } while ((value
& has_data
) == has_data
);
376 * Make sure we indeed read all there was. The TIS_STS_VALID bit is
379 if (value
& TIS_STS_DATA_AVAILABLE
) {
380 printf("%s:%d wrong receive status %x\n",
381 __FILE__
, __LINE__
, value
);
385 /* Tell the TPM that we are done. */
386 tpm_write_word(priv
, TIS_STS_COMMAND_READY
,
387 ®s
[locality
].tpm_status
);
392 static int tpm_tis_lpc_open(struct udevice
*dev
)
394 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
395 struct tpm_locality
*regs
= priv
->regs
;
396 u8 locality
= 0; /* we use locality zero for everything. */
399 /* now request access to locality. */
400 tpm_write_word(priv
, TIS_ACCESS_REQUEST_USE
, ®s
[locality
].access
);
402 /* did we get a lock? */
403 ret
= tis_wait_reg(priv
, ®s
[locality
].access
,
404 TIS_ACCESS_ACTIVE_LOCALITY
,
405 TIS_ACCESS_ACTIVE_LOCALITY
);
406 if (ret
== -ETIMEDOUT
) {
407 printf("%s:%d - failed to lock locality %d\n",
408 __FILE__
, __LINE__
, locality
);
412 tpm_write_word(priv
, TIS_STS_COMMAND_READY
,
413 ®s
[locality
].tpm_status
);
417 static int tpm_tis_lpc_close(struct udevice
*dev
)
419 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
420 struct tpm_locality
*regs
= priv
->regs
;
423 if (tpm_read_word(priv
, ®s
[locality
].access
) &
424 TIS_ACCESS_ACTIVE_LOCALITY
) {
425 tpm_write_word(priv
, TIS_ACCESS_ACTIVE_LOCALITY
,
426 ®s
[locality
].access
);
428 if (tis_wait_reg(priv
, ®s
[locality
].access
,
429 TIS_ACCESS_ACTIVE_LOCALITY
, 0) == -ETIMEDOUT
) {
430 printf("%s:%d - failed to release locality %d\n",
431 __FILE__
, __LINE__
, locality
);
438 static int tpm_tis_get_desc(struct udevice
*dev
, char *buf
, int size
)
440 ulong chip_type
= dev_get_driver_data(dev
);
445 return snprintf(buf
, size
, "1.2 TPM (%s)",
446 chip_name
[chip_type
]);
450 static const struct tpm_ops tpm_tis_lpc_ops
= {
451 .open
= tpm_tis_lpc_open
,
452 .close
= tpm_tis_lpc_close
,
453 .get_desc
= tpm_tis_get_desc
,
454 .send
= tis_senddata
,
455 .recv
= tis_readresponse
,
458 static const struct udevice_id tpm_tis_lpc_ids
[] = {
459 { .compatible
= "infineon,slb9635lpc", .data
= SLB9635
},
460 { .compatible
= "atmel,at97sc3204", .data
= AT97SC3204
},
464 U_BOOT_DRIVER(tpm_tis_lpc
) = {
465 .name
= "tpm_tis_lpc",
467 .of_match
= tpm_tis_lpc_ids
,
468 .ops
= &tpm_tis_lpc_ops
,
469 .probe
= tpm_tis_lpc_probe
,
470 .priv_auto_alloc_size
= sizeof(struct tpm_tis_lpc_priv
),