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: "
41 struct tpm_tis_lpc_priv
{
42 struct tpm_locality
*regs
;
46 * This pointer refers to the TPM chip, 5 of its localities are mapped as an
49 #define TPM_TOTAL_LOCALITIES 5
51 /* Some registers' bit field definitions */
52 #define TIS_STS_VALID (1 << 7) /* 0x80 */
53 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
54 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
55 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
56 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
57 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
59 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
60 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
61 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
62 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
63 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
64 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
65 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
67 #define TIS_STS_BURST_COUNT_MASK (0xffff)
68 #define TIS_STS_BURST_COUNT_SHIFT (8)
70 /* 1 second is plenty for anything TPM does. */
71 #define MAX_DELAY_US (1000 * 1000)
73 /* Retrieve burst count value out of the status register contents. */
74 static u16
burst_count(u32 status
)
76 return (status
>> TIS_STS_BURST_COUNT_SHIFT
) &
77 TIS_STS_BURST_COUNT_MASK
;
80 /* TPM access wrappers to support tracing */
81 static u8
tpm_read_byte(struct tpm_tis_lpc_priv
*priv
, const u8
*ptr
)
84 debug(PREFIX
"Read reg 0x%4.4x returns 0x%2.2x\n",
85 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, ret
);
89 static u32
tpm_read_word(struct tpm_tis_lpc_priv
*priv
, const u32
*ptr
)
92 debug(PREFIX
"Read reg 0x%4.4x returns 0x%8.8x\n",
93 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, ret
);
97 static void tpm_write_byte(struct tpm_tis_lpc_priv
*priv
, u8 value
, u8
*ptr
)
99 debug(PREFIX
"Write reg 0x%4.4x with 0x%2.2x\n",
100 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, value
);
104 static void tpm_write_word(struct tpm_tis_lpc_priv
*priv
, u32 value
,
107 debug(PREFIX
"Write reg 0x%4.4x with 0x%8.8x\n",
108 (u32
)(uintptr_t)ptr
- (u32
)(uintptr_t)priv
->regs
, value
);
115 * Wait for at least a second for a register to change its state to match the
116 * expected state. Normally the transition happens within microseconds.
118 * @reg - pointer to the TPM register
119 * @mask - bitmask for the bitfield(s) to watch
120 * @expected - value the field(s) are supposed to be set to
122 * Returns the register contents in case the expected value was found in the
123 * appropriate register bits, or -ETIMEDOUT on timeout.
125 static int tis_wait_reg(struct tpm_tis_lpc_priv
*priv
, u32
*reg
, u8 mask
,
128 u32 time_us
= MAX_DELAY_US
;
130 while (time_us
> 0) {
131 u32 value
= tpm_read_word(priv
, reg
);
132 if ((value
& mask
) == expected
)
134 udelay(1); /* 1 us */
142 * Probe the TPM device and try determining its manufacturer/device name.
144 * Returns 0 on success, -ve on error
146 static int tpm_tis_lpc_probe(struct udevice
*dev
)
148 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
153 addr
= dev_get_addr(dev
);
154 if (addr
== FDT_ADDR_T_NONE
)
156 priv
->regs
= map_sysmem(addr
, 0);
157 didvid
= tpm_read_word(priv
, &priv
->regs
[0].did_vid
);
159 vid
= didvid
& 0xffff;
160 did
= (didvid
>> 16) & 0xffff;
161 if (vid
!= 0x15d1 || did
!= 0xb) {
162 debug("Invalid vendor/device ID %04x/%04x\n", vid
, did
);
166 debug("Found TPM %s by %s\n", "SLB9635 TT 1.2", "Infineon");
174 * send the passed in data to the TPM device.
176 * @data - address of the data to send, byte by byte
177 * @len - length of the data to send
179 * Returns 0 on success, -ve on error (in case the device does not accept
180 * the entire command).
182 static int tis_senddata(struct udevice
*dev
, const u8
*data
, size_t len
)
184 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
185 struct tpm_locality
*regs
= priv
->regs
;
192 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
193 TIS_STS_COMMAND_READY
, TIS_STS_COMMAND_READY
);
194 if (value
== -ETIMEDOUT
) {
195 printf("%s:%d - failed to get 'command_ready' status\n",
199 burst
= burst_count(value
);
204 /* Wait till the device is ready to accept more data. */
206 if (max_cycles
++ == MAX_DELAY_US
) {
207 printf("%s:%d failed to feed %d bytes of %d\n",
208 __FILE__
, __LINE__
, len
- offset
, len
);
212 burst
= burst_count(tpm_read_word(priv
,
213 ®s
[locality
].tpm_status
));
219 * Calculate number of bytes the TPM is ready to accept in one
222 * We want to send the last byte outside of the loop (hence
223 * the -1 below) to make sure that the 'expected' status bit
224 * changes to zero exactly after the last byte is fed into the
227 count
= min((u32
)burst
, len
- offset
- 1);
229 tpm_write_byte(priv
, data
[offset
++],
230 ®s
[locality
].data
);
232 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
233 TIS_STS_VALID
, TIS_STS_VALID
);
235 if ((value
== -ETIMEDOUT
) || !(value
& TIS_STS_EXPECT
)) {
236 printf("%s:%d TPM command feed overflow\n",
238 return value
== -ETIMEDOUT
? value
: -EIO
;
241 burst
= burst_count(value
);
242 if ((offset
== (len
- 1)) && burst
) {
244 * We need to be able to send the last byte to the
245 * device, so burst size must be nonzero before we
252 /* Send the last byte. */
253 tpm_write_byte(priv
, data
[offset
++], ®s
[locality
].data
);
255 * Verify that TPM does not expect any more data as part of this
258 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
259 TIS_STS_VALID
, TIS_STS_VALID
);
260 if ((value
== -ETIMEDOUT
) || (value
& TIS_STS_EXPECT
)) {
261 printf("%s:%d unexpected TPM status 0x%x\n",
262 __FILE__
, __LINE__
, value
);
263 return value
== -ETIMEDOUT
? value
: -EIO
;
266 /* OK, sitting pretty, let's start the command execution. */
267 tpm_write_word(priv
, TIS_STS_TPM_GO
, ®s
[locality
].tpm_status
);
274 * read the TPM device response after a command was issued.
276 * @buffer - address where to read the response, byte by byte.
277 * @len - pointer to the size of buffer
279 * On success stores the number of received bytes to len and returns 0. On
280 * errors (misformatted TPM data or synchronization problems) returns
283 static int tis_readresponse(struct udevice
*dev
, u8
*buffer
, size_t len
)
285 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
286 struct tpm_locality
*regs
= priv
->regs
;
291 const u32 has_data
= TIS_STS_DATA_AVAILABLE
| TIS_STS_VALID
;
292 u32 expected_count
= len
;
295 /* Wait for the TPM to process the command. */
296 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
298 if (value
== -ETIMEDOUT
) {
299 printf("%s:%d failed processing command\n",
305 while ((burst
= burst_count(value
)) == 0) {
306 if (max_cycles
++ == MAX_DELAY_US
) {
307 printf("%s:%d TPM stuck on read\n",
312 value
= tpm_read_word(priv
, ®s
[locality
].tpm_status
);
317 while (burst
-- && (offset
< expected_count
)) {
318 buffer
[offset
++] = tpm_read_byte(priv
,
319 ®s
[locality
].data
);
323 * We got the first six bytes of the reply,
324 * let's figure out how many bytes to expect
325 * total - it is stored as a 4 byte number in
326 * network order, starting with offset 2 into
327 * the body of the reply.
332 sizeof(real_length
));
333 expected_count
= be32_to_cpu(real_length
);
335 if ((expected_count
< offset
) ||
336 (expected_count
> len
)) {
337 printf("%s:%d bad response size %d\n",
345 /* Wait for the next portion. */
346 value
= tis_wait_reg(priv
, ®s
[locality
].tpm_status
,
347 TIS_STS_VALID
, TIS_STS_VALID
);
348 if (value
== -ETIMEDOUT
) {
349 printf("%s:%d failed to read response\n",
354 if (offset
== expected_count
)
355 break; /* We got all we needed. */
357 } while ((value
& has_data
) == has_data
);
360 * Make sure we indeed read all there was. The TIS_STS_VALID bit is
363 if (value
& TIS_STS_DATA_AVAILABLE
) {
364 printf("%s:%d wrong receive status %x\n",
365 __FILE__
, __LINE__
, value
);
369 /* Tell the TPM that we are done. */
370 tpm_write_word(priv
, TIS_STS_COMMAND_READY
,
371 ®s
[locality
].tpm_status
);
376 static int tpm_tis_lpc_open(struct udevice
*dev
)
378 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
379 struct tpm_locality
*regs
= priv
->regs
;
380 u8 locality
= 0; /* we use locality zero for everything. */
383 /* now request access to locality. */
384 tpm_write_word(priv
, TIS_ACCESS_REQUEST_USE
, ®s
[locality
].access
);
386 /* did we get a lock? */
387 ret
= tis_wait_reg(priv
, ®s
[locality
].access
,
388 TIS_ACCESS_ACTIVE_LOCALITY
,
389 TIS_ACCESS_ACTIVE_LOCALITY
);
390 if (ret
== -ETIMEDOUT
) {
391 printf("%s:%d - failed to lock locality %d\n",
392 __FILE__
, __LINE__
, locality
);
396 tpm_write_word(priv
, TIS_STS_COMMAND_READY
,
397 ®s
[locality
].tpm_status
);
401 static int tpm_tis_lpc_close(struct udevice
*dev
)
403 struct tpm_tis_lpc_priv
*priv
= dev_get_priv(dev
);
404 struct tpm_locality
*regs
= priv
->regs
;
407 if (tpm_read_word(priv
, ®s
[locality
].access
) &
408 TIS_ACCESS_ACTIVE_LOCALITY
) {
409 tpm_write_word(priv
, TIS_ACCESS_ACTIVE_LOCALITY
,
410 ®s
[locality
].access
);
412 if (tis_wait_reg(priv
, ®s
[locality
].access
,
413 TIS_ACCESS_ACTIVE_LOCALITY
, 0) == -ETIMEDOUT
) {
414 printf("%s:%d - failed to release locality %d\n",
415 __FILE__
, __LINE__
, locality
);
422 static int tpm_tis_get_desc(struct udevice
*dev
, char *buf
, int size
)
427 return snprintf(buf
, size
, "1.2 TPM (vendor %s, chip %s)",
428 "Infineon", "SLB9635 TT 1.2");
432 static const struct tpm_ops tpm_tis_lpc_ops
= {
433 .open
= tpm_tis_lpc_open
,
434 .close
= tpm_tis_lpc_close
,
435 .get_desc
= tpm_tis_get_desc
,
436 .send
= tis_senddata
,
437 .recv
= tis_readresponse
,
440 static const struct udevice_id tpm_tis_lpc_ids
[] = {
441 { .compatible
= "infineon,slb9635lpc" },
445 U_BOOT_DRIVER(tpm_tis_lpc
) = {
446 .name
= "tpm_tis_lpc",
448 .of_match
= tpm_tis_lpc_ids
,
449 .ops
= &tpm_tis_lpc_ops
,
450 .probe
= tpm_tis_lpc_probe
,
451 .priv_auto_alloc_size
= sizeof(struct tpm_tis_lpc_priv
),