1 // SPDX-License-Identifier: GPL-2.0-only
4 * Device Driver for the Infineon Technologies
5 * SLD 9630 TT 1.1 and SLB 9635 TT 1.2 Trusted Platform Module
6 * Specifications at www.trustedcomputinggroup.org
8 * Copyright (C) 2005, Marcel Selhorst <tpmdd@selhorst.net>
9 * Sirrix AG - security technologies <tpmdd@sirrix.com> and
10 * Applied Data Security Group, Ruhr-University Bochum, Germany
11 * Project-Homepage: http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
14 #include <linux/init.h>
15 #include <linux/pnp.h>
18 /* Infineon specific definitions */
19 /* maximum number of WTX-packages */
20 #define TPM_MAX_WTX_PACKAGES 50
21 /* msleep-Time for WTX-packages */
22 #define TPM_WTX_MSLEEP_TIME 20
23 /* msleep-Time --> Interval to check status register */
24 #define TPM_MSLEEP_TIME 3
25 /* gives number of max. msleep()-calls before throwing timeout */
26 #define TPM_MAX_TRIES 5000
27 #define TPM_INFINEON_DEV_VEN_VALUE 0x15D1
29 #define TPM_INF_IO_PORT 0x0
30 #define TPM_INF_IO_MEM 0x1
32 #define TPM_INF_ADDR 0x0
33 #define TPM_INF_DATA 0x1
38 void __iomem
*mem_base
; /* MMIO ioremap'd addr */
39 unsigned long map_base
; /* phys MMIO base */
40 unsigned long map_size
; /* MMIO region size */
41 unsigned int index_off
; /* index register offset */
43 unsigned int data_regs
; /* Data registers */
44 unsigned int data_size
;
46 unsigned int config_port
; /* IO Port config index reg */
47 unsigned int config_size
;
50 static struct tpm_inf_dev tpm_dev
;
52 static inline void tpm_data_out(unsigned char data
, unsigned char offset
)
54 if (tpm_dev
.iotype
== TPM_INF_IO_PORT
)
55 outb(data
, tpm_dev
.data_regs
+ offset
);
57 writeb(data
, tpm_dev
.mem_base
+ tpm_dev
.data_regs
+ offset
);
60 static inline unsigned char tpm_data_in(unsigned char offset
)
62 if (tpm_dev
.iotype
== TPM_INF_IO_PORT
)
63 return inb(tpm_dev
.data_regs
+ offset
);
65 return readb(tpm_dev
.mem_base
+ tpm_dev
.data_regs
+ offset
);
68 static inline void tpm_config_out(unsigned char data
, unsigned char offset
)
70 if (tpm_dev
.iotype
== TPM_INF_IO_PORT
)
71 outb(data
, tpm_dev
.config_port
+ offset
);
73 writeb(data
, tpm_dev
.mem_base
+ tpm_dev
.index_off
+ offset
);
76 static inline unsigned char tpm_config_in(unsigned char offset
)
78 if (tpm_dev
.iotype
== TPM_INF_IO_PORT
)
79 return inb(tpm_dev
.config_port
+ offset
);
81 return readb(tpm_dev
.mem_base
+ tpm_dev
.index_off
+ offset
);
84 /* TPM header definitions */
85 enum infineon_tpm_header
{
87 TPM_VL_CHANNEL_CONTROL
= 0x07,
88 TPM_VL_CHANNEL_PERSONALISATION
= 0x0A,
89 TPM_VL_CHANNEL_TPM
= 0x0B,
90 TPM_VL_CONTROL
= 0x00,
93 TPM_CTRL_WTX_ABORT
= 0x18,
94 TPM_CTRL_WTX_ABORT_ACK
= 0x18,
95 TPM_CTRL_ERROR
= 0x20,
96 TPM_CTRL_CHAININGACK
= 0x40,
97 TPM_CTRL_CHAINING
= 0x80,
99 TPM_CTRL_DATA_CHA
= 0x84,
100 TPM_CTRL_DATA_CHA_ACK
= 0xC4
103 enum infineon_tpm_register
{
110 enum infineon_tpm_command_bits
{
117 enum infineon_tpm_status_bits
{
126 /* some outgoing values */
127 enum infineon_tpm_values
{
131 RESET_LP_IRQC_DISABLE
= 0x41,
132 ENABLE_REGISTER_PAIR
= 0x55,
135 DISABLE_REGISTER_PAIR
= 0xAA,
142 static int number_of_wtx
;
144 static int empty_fifo(struct tpm_chip
*chip
, int clear_wrfifo
)
151 for (i
= 0; i
< 4096; i
++) {
152 status
= tpm_data_in(WRFIFO
);
153 if (status
== 0xff) {
161 /* Note: The values which are currently in the FIFO of the TPM
162 are thrown away since there is no usage for them. Usually,
163 this has nothing to say, since the TPM will give its answer
164 immediately or will be aborted anyway, so the data here is
165 usually garbage and useless.
166 We have to clean this, because the next communication with
167 the TPM would be rubbish, if there is still some old data
172 status
= tpm_data_in(RDFIFO
);
173 status
= tpm_data_in(STAT
);
175 if (i
== TPM_MAX_TRIES
)
177 } while ((status
& (1 << STAT_RDA
)) != 0);
181 static int wait(struct tpm_chip
*chip
, int wait_for_bit
)
185 for (i
= 0; i
< TPM_MAX_TRIES
; i
++) {
186 status
= tpm_data_in(STAT
);
187 /* check the status-register if wait_for_bit is set */
188 if (status
& 1 << wait_for_bit
)
190 tpm_msleep(TPM_MSLEEP_TIME
);
192 if (i
== TPM_MAX_TRIES
) { /* timeout occurs */
193 if (wait_for_bit
== STAT_XFE
)
194 dev_err(&chip
->dev
, "Timeout in wait(STAT_XFE)\n");
195 if (wait_for_bit
== STAT_RDA
)
196 dev_err(&chip
->dev
, "Timeout in wait(STAT_RDA)\n");
202 static void wait_and_send(struct tpm_chip
*chip
, u8 sendbyte
)
204 wait(chip
, STAT_XFE
);
205 tpm_data_out(sendbyte
, WRFIFO
);
208 /* Note: WTX means Waiting-Time-Extension. Whenever the TPM needs more
209 calculation time, it sends a WTX-package, which has to be acknowledged
210 or aborted. This usually occurs if you are hammering the TPM with key
211 creation. Set the maximum number of WTX-packages in the definitions
212 above, if the number is reached, the waiting-time will be denied
213 and the TPM command has to be resend.
216 static void tpm_wtx(struct tpm_chip
*chip
)
219 dev_info(&chip
->dev
, "Granting WTX (%02d / %02d)\n",
220 number_of_wtx
, TPM_MAX_WTX_PACKAGES
);
221 wait_and_send(chip
, TPM_VL_VER
);
222 wait_and_send(chip
, TPM_CTRL_WTX
);
223 wait_and_send(chip
, 0x00);
224 wait_and_send(chip
, 0x00);
225 tpm_msleep(TPM_WTX_MSLEEP_TIME
);
228 static void tpm_wtx_abort(struct tpm_chip
*chip
)
230 dev_info(&chip
->dev
, "Aborting WTX\n");
231 wait_and_send(chip
, TPM_VL_VER
);
232 wait_and_send(chip
, TPM_CTRL_WTX_ABORT
);
233 wait_and_send(chip
, 0x00);
234 wait_and_send(chip
, 0x00);
236 tpm_msleep(TPM_WTX_MSLEEP_TIME
);
239 static int tpm_inf_recv(struct tpm_chip
*chip
, u8
* buf
, size_t count
)
247 /* start receiving header */
248 for (i
= 0; i
< 4; i
++) {
249 ret
= wait(chip
, STAT_RDA
);
252 buf
[i
] = tpm_data_in(RDFIFO
);
255 if (buf
[0] != TPM_VL_VER
) {
257 "Wrong transport protocol implementation!\n");
261 if (buf
[1] == TPM_CTRL_DATA
) {
262 /* size of the data received */
263 size
= ((buf
[2] << 8) | buf
[3]);
265 for (i
= 0; i
< size
; i
++) {
266 wait(chip
, STAT_RDA
);
267 buf
[i
] = tpm_data_in(RDFIFO
);
270 if ((size
== 0x6D00) && (buf
[1] == 0x80)) {
271 dev_err(&chip
->dev
, "Error handling on vendor layer!\n");
275 for (i
= 0; i
< size
; i
++)
282 if (buf
[1] == TPM_CTRL_WTX
) {
283 dev_info(&chip
->dev
, "WTX-package received\n");
284 if (number_of_wtx
< TPM_MAX_WTX_PACKAGES
) {
293 if (buf
[1] == TPM_CTRL_WTX_ABORT_ACK
) {
294 dev_info(&chip
->dev
, "WTX-abort acknowledged\n");
298 if (buf
[1] == TPM_CTRL_ERROR
) {
299 dev_err(&chip
->dev
, "ERROR-package received:\n");
300 if (buf
[4] == TPM_INF_NAK
)
302 "-> Negative acknowledgement"
303 " - retransmit command!\n");
309 static int tpm_inf_send(struct tpm_chip
*chip
, u8
* buf
, size_t count
)
313 u8 count_high
, count_low
, count_4
, count_3
, count_2
, count_1
;
315 /* Disabling Reset, LP and IRQC */
316 tpm_data_out(RESET_LP_IRQC_DISABLE
, CMD
);
318 ret
= empty_fifo(chip
, 1);
320 dev_err(&chip
->dev
, "Timeout while clearing FIFO\n");
324 ret
= wait(chip
, STAT_XFE
);
328 count_4
= (count
& 0xff000000) >> 24;
329 count_3
= (count
& 0x00ff0000) >> 16;
330 count_2
= (count
& 0x0000ff00) >> 8;
331 count_1
= (count
& 0x000000ff);
332 count_high
= ((count
+ 6) & 0xffffff00) >> 8;
333 count_low
= ((count
+ 6) & 0x000000ff);
336 wait_and_send(chip
, TPM_VL_VER
);
337 wait_and_send(chip
, TPM_CTRL_DATA
);
338 wait_and_send(chip
, count_high
);
339 wait_and_send(chip
, count_low
);
341 /* Sending Data Header */
342 wait_and_send(chip
, TPM_VL_VER
);
343 wait_and_send(chip
, TPM_VL_CHANNEL_TPM
);
344 wait_and_send(chip
, count_4
);
345 wait_and_send(chip
, count_3
);
346 wait_and_send(chip
, count_2
);
347 wait_and_send(chip
, count_1
);
350 for (i
= 0; i
< count
; i
++) {
351 wait_and_send(chip
, buf
[i
]);
356 static void tpm_inf_cancel(struct tpm_chip
*chip
)
359 Since we are using the legacy mode to communicate
360 with the TPM, we have no cancel functions, but have
361 a workaround for interrupting the TPM through WTX.
365 static u8
tpm_inf_status(struct tpm_chip
*chip
)
367 return tpm_data_in(STAT
);
370 static const struct tpm_class_ops tpm_inf
= {
371 .recv
= tpm_inf_recv
,
372 .send
= tpm_inf_send
,
373 .cancel
= tpm_inf_cancel
,
374 .status
= tpm_inf_status
,
375 .req_complete_mask
= 0,
376 .req_complete_val
= 0,
379 static const struct pnp_device_id tpm_inf_pnp_tbl
[] = {
386 MODULE_DEVICE_TABLE(pnp
, tpm_inf_pnp_tbl
);
388 static int tpm_inf_pnp_probe(struct pnp_dev
*dev
,
389 const struct pnp_device_id
*dev_id
)
396 const char *chipname
;
397 struct tpm_chip
*chip
;
399 /* read IO-ports through PnP */
400 if (pnp_port_valid(dev
, 0) && pnp_port_valid(dev
, 1) &&
401 !(pnp_port_flags(dev
, 0) & IORESOURCE_DISABLED
)) {
403 tpm_dev
.iotype
= TPM_INF_IO_PORT
;
405 tpm_dev
.config_port
= pnp_port_start(dev
, 0);
406 tpm_dev
.config_size
= pnp_port_len(dev
, 0);
407 tpm_dev
.data_regs
= pnp_port_start(dev
, 1);
408 tpm_dev
.data_size
= pnp_port_len(dev
, 1);
409 if ((tpm_dev
.data_size
< 4) || (tpm_dev
.config_size
< 2)) {
413 dev_info(&dev
->dev
, "Found %s with ID %s\n",
414 dev
->name
, dev_id
->id
);
415 if (!((tpm_dev
.data_regs
>> 8) & 0xff)) {
419 /* publish my base address and request region */
420 if (request_region(tpm_dev
.data_regs
, tpm_dev
.data_size
,
421 "tpm_infineon0") == NULL
) {
425 if (request_region(tpm_dev
.config_port
, tpm_dev
.config_size
,
426 "tpm_infineon0") == NULL
) {
427 release_region(tpm_dev
.data_regs
, tpm_dev
.data_size
);
431 } else if (pnp_mem_valid(dev
, 0) &&
432 !(pnp_mem_flags(dev
, 0) & IORESOURCE_DISABLED
)) {
434 tpm_dev
.iotype
= TPM_INF_IO_MEM
;
436 tpm_dev
.map_base
= pnp_mem_start(dev
, 0);
437 tpm_dev
.map_size
= pnp_mem_len(dev
, 0);
439 dev_info(&dev
->dev
, "Found %s with ID %s\n",
440 dev
->name
, dev_id
->id
);
442 /* publish my base address and request region */
443 if (request_mem_region(tpm_dev
.map_base
, tpm_dev
.map_size
,
444 "tpm_infineon0") == NULL
) {
449 tpm_dev
.mem_base
= ioremap(tpm_dev
.map_base
, tpm_dev
.map_size
);
450 if (tpm_dev
.mem_base
== NULL
) {
451 release_mem_region(tpm_dev
.map_base
, tpm_dev
.map_size
);
457 * The only known MMIO based Infineon TPM system provides
458 * a single large mem region with the device config
459 * registers at the default TPM_ADDR. The data registers
460 * seem like they could be placed anywhere within the MMIO
461 * region, but lets just put them at zero offset.
463 tpm_dev
.index_off
= TPM_ADDR
;
464 tpm_dev
.data_regs
= 0x0;
470 /* query chip for its vendor, its version number a.s.o. */
471 tpm_config_out(ENABLE_REGISTER_PAIR
, TPM_INF_ADDR
);
472 tpm_config_out(IDVENL
, TPM_INF_ADDR
);
473 vendorid
[1] = tpm_config_in(TPM_INF_DATA
);
474 tpm_config_out(IDVENH
, TPM_INF_ADDR
);
475 vendorid
[0] = tpm_config_in(TPM_INF_DATA
);
476 tpm_config_out(IDPDL
, TPM_INF_ADDR
);
477 productid
[1] = tpm_config_in(TPM_INF_DATA
);
478 tpm_config_out(IDPDH
, TPM_INF_ADDR
);
479 productid
[0] = tpm_config_in(TPM_INF_DATA
);
480 tpm_config_out(CHIP_ID1
, TPM_INF_ADDR
);
481 version
[1] = tpm_config_in(TPM_INF_DATA
);
482 tpm_config_out(CHIP_ID2
, TPM_INF_ADDR
);
483 version
[0] = tpm_config_in(TPM_INF_DATA
);
485 switch ((productid
[0] << 8) | productid
[1]) {
487 chipname
= " (SLD 9630 TT 1.1)";
490 chipname
= " (SLB 9635 TT 1.2)";
493 chipname
= " (unknown chip)";
497 if ((vendorid
[0] << 8 | vendorid
[1]) == (TPM_INFINEON_DEV_VEN_VALUE
)) {
499 /* configure TPM with IO-ports */
500 tpm_config_out(IOLIMH
, TPM_INF_ADDR
);
501 tpm_config_out((tpm_dev
.data_regs
>> 8) & 0xff, TPM_INF_DATA
);
502 tpm_config_out(IOLIML
, TPM_INF_ADDR
);
503 tpm_config_out((tpm_dev
.data_regs
& 0xff), TPM_INF_DATA
);
505 /* control if IO-ports are set correctly */
506 tpm_config_out(IOLIMH
, TPM_INF_ADDR
);
507 ioh
= tpm_config_in(TPM_INF_DATA
);
508 tpm_config_out(IOLIML
, TPM_INF_ADDR
);
509 iol
= tpm_config_in(TPM_INF_DATA
);
511 if ((ioh
<< 8 | iol
) != tpm_dev
.data_regs
) {
513 "Could not set IO-data registers to 0x%x\n",
516 goto err_release_region
;
519 /* activate register */
520 tpm_config_out(TPM_DAR
, TPM_INF_ADDR
);
521 tpm_config_out(0x01, TPM_INF_DATA
);
522 tpm_config_out(DISABLE_REGISTER_PAIR
, TPM_INF_ADDR
);
524 /* disable RESET, LP and IRQC */
525 tpm_data_out(RESET_LP_IRQC_DISABLE
, CMD
);
527 /* Finally, we're done, print some infos */
528 dev_info(&dev
->dev
, "TPM found: "
529 "config base 0x%lx, "
531 "chip version 0x%02x%02x, "
532 "vendor id 0x%x%x (Infineon), "
533 "product id 0x%02x%02x"
535 tpm_dev
.iotype
== TPM_INF_IO_PORT
?
536 tpm_dev
.config_port
:
537 tpm_dev
.map_base
+ tpm_dev
.index_off
,
538 tpm_dev
.iotype
== TPM_INF_IO_PORT
?
540 tpm_dev
.map_base
+ tpm_dev
.data_regs
,
541 version
[0], version
[1],
542 vendorid
[0], vendorid
[1],
543 productid
[0], productid
[1], chipname
);
545 chip
= tpmm_chip_alloc(&dev
->dev
, &tpm_inf
);
548 goto err_release_region
;
551 rc
= tpm_chip_register(chip
);
553 goto err_release_region
;
558 goto err_release_region
;
562 if (tpm_dev
.iotype
== TPM_INF_IO_PORT
) {
563 release_region(tpm_dev
.data_regs
, tpm_dev
.data_size
);
564 release_region(tpm_dev
.config_port
, tpm_dev
.config_size
);
566 iounmap(tpm_dev
.mem_base
);
567 release_mem_region(tpm_dev
.map_base
, tpm_dev
.map_size
);
574 static void tpm_inf_pnp_remove(struct pnp_dev
*dev
)
576 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
578 tpm_chip_unregister(chip
);
580 if (tpm_dev
.iotype
== TPM_INF_IO_PORT
) {
581 release_region(tpm_dev
.data_regs
, tpm_dev
.data_size
);
582 release_region(tpm_dev
.config_port
,
583 tpm_dev
.config_size
);
585 iounmap(tpm_dev
.mem_base
);
586 release_mem_region(tpm_dev
.map_base
, tpm_dev
.map_size
);
590 #ifdef CONFIG_PM_SLEEP
591 static int tpm_inf_resume(struct device
*dev
)
593 /* Re-configure TPM after suspending */
594 tpm_config_out(ENABLE_REGISTER_PAIR
, TPM_INF_ADDR
);
595 tpm_config_out(IOLIMH
, TPM_INF_ADDR
);
596 tpm_config_out((tpm_dev
.data_regs
>> 8) & 0xff, TPM_INF_DATA
);
597 tpm_config_out(IOLIML
, TPM_INF_ADDR
);
598 tpm_config_out((tpm_dev
.data_regs
& 0xff), TPM_INF_DATA
);
599 /* activate register */
600 tpm_config_out(TPM_DAR
, TPM_INF_ADDR
);
601 tpm_config_out(0x01, TPM_INF_DATA
);
602 tpm_config_out(DISABLE_REGISTER_PAIR
, TPM_INF_ADDR
);
603 /* disable RESET, LP and IRQC */
604 tpm_data_out(RESET_LP_IRQC_DISABLE
, CMD
);
605 return tpm_pm_resume(dev
);
608 static SIMPLE_DEV_PM_OPS(tpm_inf_pm
, tpm_pm_suspend
, tpm_inf_resume
);
610 static struct pnp_driver tpm_inf_pnp_driver
= {
611 .name
= "tpm_inf_pnp",
612 .id_table
= tpm_inf_pnp_tbl
,
613 .probe
= tpm_inf_pnp_probe
,
614 .remove
= tpm_inf_pnp_remove
,
620 module_pnp_driver(tpm_inf_pnp_driver
);
622 MODULE_AUTHOR("Marcel Selhorst <tpmdd@sirrix.com>");
623 MODULE_DESCRIPTION("Driver for Infineon TPM SLD 9630 TT 1.1 / SLB 9635 TT 1.2");
624 MODULE_VERSION("1.9.2");
625 MODULE_LICENSE("GPL");