2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
14 * This device driver implements the TPM interface as defined in
15 * the TCG TPM Interface Spec version 1.2, revision 1.0.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation, version 2 of the
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/pnp.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/acpi.h>
30 #include <linux/freezer.h>
34 TPM_ACCESS_VALID
= 0x80,
35 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
36 TPM_ACCESS_REQUEST_PENDING
= 0x04,
37 TPM_ACCESS_REQUEST_USE
= 0x02,
42 TPM_STS_COMMAND_READY
= 0x40,
44 TPM_STS_DATA_AVAIL
= 0x10,
45 TPM_STS_DATA_EXPECT
= 0x08,
49 TPM_GLOBAL_INT_ENABLE
= 0x80000000,
50 TPM_INTF_BURST_COUNT_STATIC
= 0x100,
51 TPM_INTF_CMD_READY_INT
= 0x080,
52 TPM_INTF_INT_EDGE_FALLING
= 0x040,
53 TPM_INTF_INT_EDGE_RISING
= 0x020,
54 TPM_INTF_INT_LEVEL_LOW
= 0x010,
55 TPM_INTF_INT_LEVEL_HIGH
= 0x008,
56 TPM_INTF_LOCALITY_CHANGE_INT
= 0x004,
57 TPM_INTF_STS_VALID_INT
= 0x002,
58 TPM_INTF_DATA_AVAIL_INT
= 0x001,
62 TIS_MEM_BASE
= 0xFED40000,
64 TIS_SHORT_TIMEOUT
= 750, /* ms */
65 TIS_LONG_TIMEOUT
= 2000, /* 2 sec */
69 /* Some timeout values are needed before it is known whether the chip is
72 #define TIS_TIMEOUT_A_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
73 #define TIS_TIMEOUT_B_MAX max(TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
74 #define TIS_TIMEOUT_C_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
75 #define TIS_TIMEOUT_D_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
77 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
78 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
79 #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
80 #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
81 #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
82 #define TPM_STS(l) (0x0018 | ((l) << 12))
83 #define TPM_STS3(l) (0x001b | ((l) << 12))
84 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
86 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
87 #define TPM_RID(l) (0x0F04 | ((l) << 12))
93 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
94 static int is_itpm(struct pnp_dev
*dev
)
96 struct acpi_device
*acpi
= pnp_acpi_device(dev
);
97 struct acpi_hardware_id
*id
;
102 list_for_each_entry(id
, &acpi
->pnp
.ids
, list
) {
103 if (!strcmp("INTC0102", id
->id
))
110 static inline int is_itpm(struct pnp_dev
*dev
)
116 /* Before we attempt to access the TPM we must see that the valid bit is set.
117 * The specification says that this bit is 0 at reset and remains 0 until the
118 * 'TPM has gone through its self test and initialization and has established
119 * correct values in the other bits.' */
120 static int wait_startup(struct tpm_chip
*chip
, int l
)
122 unsigned long stop
= jiffies
+ chip
->vendor
.timeout_a
;
124 if (ioread8(chip
->vendor
.iobase
+ TPM_ACCESS(l
)) &
128 } while (time_before(jiffies
, stop
));
132 static int check_locality(struct tpm_chip
*chip
, int l
)
134 if ((ioread8(chip
->vendor
.iobase
+ TPM_ACCESS(l
)) &
135 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
136 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
))
137 return chip
->vendor
.locality
= l
;
142 static void release_locality(struct tpm_chip
*chip
, int l
, int force
)
144 if (force
|| (ioread8(chip
->vendor
.iobase
+ TPM_ACCESS(l
)) &
145 (TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
)) ==
146 (TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
))
147 iowrite8(TPM_ACCESS_ACTIVE_LOCALITY
,
148 chip
->vendor
.iobase
+ TPM_ACCESS(l
));
151 static int request_locality(struct tpm_chip
*chip
, int l
)
153 unsigned long stop
, timeout
;
156 if (check_locality(chip
, l
) >= 0)
159 iowrite8(TPM_ACCESS_REQUEST_USE
,
160 chip
->vendor
.iobase
+ TPM_ACCESS(l
));
162 stop
= jiffies
+ chip
->vendor
.timeout_a
;
164 if (chip
->vendor
.irq
) {
166 timeout
= stop
- jiffies
;
167 if ((long)timeout
<= 0)
169 rc
= wait_event_interruptible_timeout(chip
->vendor
.int_queue
,
175 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
176 clear_thread_flag(TIF_SIGPENDING
);
180 /* wait for burstcount */
182 if (check_locality(chip
, l
) >= 0)
186 while (time_before(jiffies
, stop
));
191 static u8
tpm_tis_status(struct tpm_chip
*chip
)
193 return ioread8(chip
->vendor
.iobase
+
194 TPM_STS(chip
->vendor
.locality
));
197 static void tpm_tis_ready(struct tpm_chip
*chip
)
199 /* this causes the current command to be aborted */
200 iowrite8(TPM_STS_COMMAND_READY
,
201 chip
->vendor
.iobase
+ TPM_STS(chip
->vendor
.locality
));
204 static int get_burstcount(struct tpm_chip
*chip
)
209 /* wait for burstcount */
210 /* which timeout value, spec has 2 answers (c & d) */
211 stop
= jiffies
+ chip
->vendor
.timeout_d
;
213 burstcnt
= ioread8(chip
->vendor
.iobase
+
214 TPM_STS(chip
->vendor
.locality
) + 1);
215 burstcnt
+= ioread8(chip
->vendor
.iobase
+
216 TPM_STS(chip
->vendor
.locality
) +
221 } while (time_before(jiffies
, stop
));
225 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
227 int size
= 0, burstcnt
;
228 while (size
< count
&&
229 wait_for_tpm_stat(chip
,
230 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
231 chip
->vendor
.timeout_c
,
232 &chip
->vendor
.read_queue
, true)
234 burstcnt
= get_burstcount(chip
);
235 for (; burstcnt
> 0 && size
< count
; burstcnt
--)
236 buf
[size
++] = ioread8(chip
->vendor
.iobase
+
237 TPM_DATA_FIFO(chip
->vendor
.
243 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
246 int expected
, status
;
248 if (count
< TPM_HEADER_SIZE
) {
253 /* read first 10 bytes, including tag, paramsize, and result */
255 recv_data(chip
, buf
, TPM_HEADER_SIZE
)) < TPM_HEADER_SIZE
) {
256 dev_err(chip
->pdev
, "Unable to read header\n");
260 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
261 if (expected
> count
) {
267 recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
268 expected
- TPM_HEADER_SIZE
)) < expected
) {
269 dev_err(chip
->pdev
, "Unable to read remainder of result\n");
274 wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->vendor
.timeout_c
,
275 &chip
->vendor
.int_queue
, false);
276 status
= tpm_tis_status(chip
);
277 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
278 dev_err(chip
->pdev
, "Error left over data\n");
285 release_locality(chip
, chip
->vendor
.locality
, 0);
290 module_param(itpm
, bool, 0444);
291 MODULE_PARM_DESC(itpm
, "Force iTPM workarounds (found on some Lenovo laptops)");
294 * If interrupts are used (signaled by an irq set in the vendor structure)
295 * tpm.c can skip polling for the data to be available as the interrupt is
298 static int tpm_tis_send_data(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
300 int rc
, status
, burstcnt
;
303 if (request_locality(chip
, 0) < 0)
306 status
= tpm_tis_status(chip
);
307 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
309 if (wait_for_tpm_stat
310 (chip
, TPM_STS_COMMAND_READY
, chip
->vendor
.timeout_b
,
311 &chip
->vendor
.int_queue
, false) < 0) {
317 while (count
< len
- 1) {
318 burstcnt
= get_burstcount(chip
);
319 for (; burstcnt
> 0 && count
< len
- 1; burstcnt
--) {
320 iowrite8(buf
[count
], chip
->vendor
.iobase
+
321 TPM_DATA_FIFO(chip
->vendor
.locality
));
325 wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->vendor
.timeout_c
,
326 &chip
->vendor
.int_queue
, false);
327 status
= tpm_tis_status(chip
);
328 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) == 0) {
334 /* write last byte */
336 chip
->vendor
.iobase
+ TPM_DATA_FIFO(chip
->vendor
.locality
));
337 wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->vendor
.timeout_c
,
338 &chip
->vendor
.int_queue
, false);
339 status
= tpm_tis_status(chip
);
340 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
349 release_locality(chip
, chip
->vendor
.locality
, 0);
353 static void disable_interrupts(struct tpm_chip
*chip
)
358 ioread32(chip
->vendor
.iobase
+
359 TPM_INT_ENABLE(chip
->vendor
.locality
));
360 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
362 chip
->vendor
.iobase
+
363 TPM_INT_ENABLE(chip
->vendor
.locality
));
364 free_irq(chip
->vendor
.irq
, chip
);
365 chip
->vendor
.irq
= 0;
369 * If interrupts are used (signaled by an irq set in the vendor structure)
370 * tpm.c can skip polling for the data to be available as the interrupt is
373 static int tpm_tis_send_main(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
379 rc
= tpm_tis_send_data(chip
, buf
, len
);
385 chip
->vendor
.iobase
+ TPM_STS(chip
->vendor
.locality
));
387 if (chip
->vendor
.irq
) {
388 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
390 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
391 dur
= tpm2_calc_ordinal_duration(chip
, ordinal
);
393 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
395 if (wait_for_tpm_stat
396 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
397 &chip
->vendor
.read_queue
, false) < 0) {
405 release_locality(chip
, chip
->vendor
.locality
, 0);
409 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
412 struct priv_data
*priv
= chip
->vendor
.priv
;
414 if (!chip
->vendor
.irq
|| priv
->irq_tested
)
415 return tpm_tis_send_main(chip
, buf
, len
);
417 /* Verify receipt of the expected IRQ */
418 irq
= chip
->vendor
.irq
;
419 chip
->vendor
.irq
= 0;
420 rc
= tpm_tis_send_main(chip
, buf
, len
);
421 chip
->vendor
.irq
= irq
;
422 if (!priv
->irq_tested
)
424 if (!priv
->irq_tested
) {
425 disable_interrupts(chip
);
427 FW_BUG
"TPM interrupt not working, polling instead\n");
429 priv
->irq_tested
= true;
433 struct tis_vendor_timeout_override
{
435 unsigned long timeout_us
[4];
438 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
440 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
441 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
444 static bool tpm_tis_update_timeouts(struct tpm_chip
*chip
,
445 unsigned long *timeout_cap
)
450 did_vid
= ioread32(chip
->vendor
.iobase
+ TPM_DID_VID(0));
452 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
453 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
455 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
456 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
464 * Early probing for iTPM with STS_DATA_EXPECT flaw.
465 * Try sending command without itpm flag set and if that
466 * fails, repeat with itpm flag set.
468 static int probe_itpm(struct tpm_chip
*chip
)
471 u8 cmd_getticks
[] = {
472 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
473 0x00, 0x00, 0x00, 0xf1
475 size_t len
= sizeof(cmd_getticks
);
476 bool rem_itpm
= itpm
;
477 u16 vendor
= ioread16(chip
->vendor
.iobase
+ TPM_DID_VID(0));
479 /* probe only iTPMS */
480 if (vendor
!= TPM_VID_INTEL
)
485 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
490 release_locality(chip
, chip
->vendor
.locality
, 0);
494 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
496 dev_info(chip
->pdev
, "Detected an iTPM.\n");
504 release_locality(chip
, chip
->vendor
.locality
, 0);
509 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
511 switch (chip
->vendor
.manufacturer_id
) {
512 case TPM_VID_WINBOND
:
513 return ((status
== TPM_STS_VALID
) ||
514 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
516 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
518 return (status
== TPM_STS_COMMAND_READY
);
522 static const struct tpm_class_ops tpm_tis
= {
523 .status
= tpm_tis_status
,
524 .recv
= tpm_tis_recv
,
525 .send
= tpm_tis_send
,
526 .cancel
= tpm_tis_ready
,
527 .update_timeouts
= tpm_tis_update_timeouts
,
528 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
529 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
530 .req_canceled
= tpm_tis_req_canceled
,
533 static irqreturn_t
tis_int_probe(int irq
, void *dev_id
)
535 struct tpm_chip
*chip
= dev_id
;
538 interrupt
= ioread32(chip
->vendor
.iobase
+
539 TPM_INT_STATUS(chip
->vendor
.locality
));
544 chip
->vendor
.probed_irq
= irq
;
546 /* Clear interrupts handled with TPM_EOI */
548 chip
->vendor
.iobase
+
549 TPM_INT_STATUS(chip
->vendor
.locality
));
553 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
555 struct tpm_chip
*chip
= dev_id
;
559 interrupt
= ioread32(chip
->vendor
.iobase
+
560 TPM_INT_STATUS(chip
->vendor
.locality
));
565 ((struct priv_data
*)chip
->vendor
.priv
)->irq_tested
= true;
566 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
567 wake_up_interruptible(&chip
->vendor
.read_queue
);
568 if (interrupt
& TPM_INTF_LOCALITY_CHANGE_INT
)
569 for (i
= 0; i
< 5; i
++)
570 if (check_locality(chip
, i
) >= 0)
573 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
574 TPM_INTF_CMD_READY_INT
))
575 wake_up_interruptible(&chip
->vendor
.int_queue
);
577 /* Clear interrupts handled with TPM_EOI */
579 chip
->vendor
.iobase
+
580 TPM_INT_STATUS(chip
->vendor
.locality
));
581 ioread32(chip
->vendor
.iobase
+ TPM_INT_STATUS(chip
->vendor
.locality
));
585 static bool interrupts
= true;
586 module_param(interrupts
, bool, 0444);
587 MODULE_PARM_DESC(interrupts
, "Enable interrupts");
589 static void tpm_tis_remove(struct tpm_chip
*chip
)
591 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
592 tpm2_shutdown(chip
, TPM2_SU_CLEAR
);
594 iowrite32(~TPM_GLOBAL_INT_ENABLE
&
595 ioread32(chip
->vendor
.iobase
+
596 TPM_INT_ENABLE(chip
->vendor
.
598 chip
->vendor
.iobase
+
599 TPM_INT_ENABLE(chip
->vendor
.locality
));
600 release_locality(chip
, chip
->vendor
.locality
, 1);
603 static int tpm_tis_init(struct device
*dev
, acpi_handle acpi_dev_handle
,
604 resource_size_t start
, resource_size_t len
,
607 u32 vendor
, intfcaps
, intmask
;
608 int rc
, i
, irq_s
, irq_e
, probe
;
609 struct tpm_chip
*chip
;
610 struct priv_data
*priv
;
612 priv
= devm_kzalloc(dev
, sizeof(struct priv_data
), GFP_KERNEL
);
616 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
618 return PTR_ERR(chip
);
620 chip
->vendor
.priv
= priv
;
622 chip
->acpi_dev_handle
= acpi_dev_handle
;
625 chip
->vendor
.iobase
= devm_ioremap(dev
, start
, len
);
626 if (!chip
->vendor
.iobase
)
629 /* Maximum timeouts */
630 chip
->vendor
.timeout_a
= TIS_TIMEOUT_A_MAX
;
631 chip
->vendor
.timeout_b
= TIS_TIMEOUT_B_MAX
;
632 chip
->vendor
.timeout_c
= TIS_TIMEOUT_C_MAX
;
633 chip
->vendor
.timeout_d
= TIS_TIMEOUT_D_MAX
;
635 if (wait_startup(chip
, 0) != 0) {
640 if (request_locality(chip
, 0) != 0) {
645 /* Every TPM 2.x command has a higher ordinal than TPM 1.x commands.
646 * Therefore, we can use an idempotent TPM 2.x command to probe TPM 2.x.
648 rc
= tpm2_gen_interrupt(chip
, true);
649 if (rc
== 0 || rc
== TPM2_RC_INITIALIZE
)
650 chip
->flags
|= TPM_CHIP_FLAG_TPM2
;
652 vendor
= ioread32(chip
->vendor
.iobase
+ TPM_DID_VID(0));
653 chip
->vendor
.manufacturer_id
= vendor
;
655 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
656 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
657 vendor
>> 16, ioread8(chip
->vendor
.iobase
+ TPM_RID(0)));
660 probe
= probe_itpm(chip
);
669 dev_info(dev
, "Intel iTPM workaround enabled\n");
672 /* Figure out the capabilities */
674 ioread32(chip
->vendor
.iobase
+
675 TPM_INTF_CAPS(chip
->vendor
.locality
));
676 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
678 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
679 dev_dbg(dev
, "\tBurst Count Static\n");
680 if (intfcaps
& TPM_INTF_CMD_READY_INT
)
681 dev_dbg(dev
, "\tCommand Ready Int Support\n");
682 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
683 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
684 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
685 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
686 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
687 dev_dbg(dev
, "\tInterrupt Level Low\n");
688 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
689 dev_dbg(dev
, "\tInterrupt Level High\n");
690 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
)
691 dev_dbg(dev
, "\tLocality Change Int Support\n");
692 if (intfcaps
& TPM_INTF_STS_VALID_INT
)
693 dev_dbg(dev
, "\tSts Valid Int Support\n");
694 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
)
695 dev_dbg(dev
, "\tData Avail Int Support\n");
697 /* INTERRUPT Setup */
698 init_waitqueue_head(&chip
->vendor
.read_queue
);
699 init_waitqueue_head(&chip
->vendor
.int_queue
);
702 ioread32(chip
->vendor
.iobase
+
703 TPM_INT_ENABLE(chip
->vendor
.locality
));
705 intmask
|= TPM_INTF_CMD_READY_INT
706 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
707 | TPM_INTF_STS_VALID_INT
;
710 chip
->vendor
.iobase
+
711 TPM_INT_ENABLE(chip
->vendor
.locality
));
713 chip
->vendor
.irq
= irq
;
714 if (interrupts
&& !chip
->vendor
.irq
) {
716 ioread8(chip
->vendor
.iobase
+
717 TPM_INT_VECTOR(chip
->vendor
.locality
));
725 for (i
= irq_s
; i
<= irq_e
&& chip
->vendor
.irq
== 0; i
++) {
726 iowrite8(i
, chip
->vendor
.iobase
+
727 TPM_INT_VECTOR(chip
->vendor
.locality
));
729 (dev
, i
, tis_int_probe
, IRQF_SHARED
,
730 chip
->devname
, chip
) != 0) {
732 "Unable to request irq: %d for probe\n",
737 /* Clear all existing */
739 (chip
->vendor
.iobase
+
740 TPM_INT_STATUS(chip
->vendor
.locality
)),
741 chip
->vendor
.iobase
+
742 TPM_INT_STATUS(chip
->vendor
.locality
));
745 iowrite32(intmask
| TPM_GLOBAL_INT_ENABLE
,
746 chip
->vendor
.iobase
+
747 TPM_INT_ENABLE(chip
->vendor
.locality
));
749 chip
->vendor
.probed_irq
= 0;
751 /* Generate Interrupts */
752 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
753 tpm2_gen_interrupt(chip
, false);
755 tpm_gen_interrupt(chip
);
757 chip
->vendor
.irq
= chip
->vendor
.probed_irq
;
759 /* free_irq will call into tis_int_probe;
760 clear all irqs we haven't seen while doing
763 (chip
->vendor
.iobase
+
764 TPM_INT_STATUS(chip
->vendor
.locality
)),
765 chip
->vendor
.iobase
+
766 TPM_INT_STATUS(chip
->vendor
.locality
));
770 chip
->vendor
.iobase
+
771 TPM_INT_ENABLE(chip
->vendor
.locality
));
774 if (chip
->vendor
.irq
) {
775 iowrite8(chip
->vendor
.irq
,
776 chip
->vendor
.iobase
+
777 TPM_INT_VECTOR(chip
->vendor
.locality
));
779 (dev
, chip
->vendor
.irq
, tis_int_handler
, IRQF_SHARED
,
780 chip
->devname
, chip
) != 0) {
782 "Unable to request irq: %d for use\n",
784 chip
->vendor
.irq
= 0;
786 /* Clear all existing */
788 (chip
->vendor
.iobase
+
789 TPM_INT_STATUS(chip
->vendor
.locality
)),
790 chip
->vendor
.iobase
+
791 TPM_INT_STATUS(chip
->vendor
.locality
));
794 iowrite32(intmask
| TPM_GLOBAL_INT_ENABLE
,
795 chip
->vendor
.iobase
+
796 TPM_INT_ENABLE(chip
->vendor
.locality
));
800 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
) {
801 chip
->vendor
.timeout_a
= msecs_to_jiffies(TPM2_TIMEOUT_A
);
802 chip
->vendor
.timeout_b
= msecs_to_jiffies(TPM2_TIMEOUT_B
);
803 chip
->vendor
.timeout_c
= msecs_to_jiffies(TPM2_TIMEOUT_C
);
804 chip
->vendor
.timeout_d
= msecs_to_jiffies(TPM2_TIMEOUT_D
);
805 chip
->vendor
.duration
[TPM_SHORT
] =
806 msecs_to_jiffies(TPM2_DURATION_SHORT
);
807 chip
->vendor
.duration
[TPM_MEDIUM
] =
808 msecs_to_jiffies(TPM2_DURATION_MEDIUM
);
809 chip
->vendor
.duration
[TPM_LONG
] =
810 msecs_to_jiffies(TPM2_DURATION_LONG
);
812 rc
= tpm2_do_selftest(chip
);
813 if (rc
== TPM2_RC_INITIALIZE
) {
814 dev_warn(dev
, "Firmware has not started TPM\n");
815 rc
= tpm2_startup(chip
, TPM2_SU_CLEAR
);
817 rc
= tpm2_do_selftest(chip
);
821 dev_err(dev
, "TPM self test failed\n");
827 if (tpm_get_timeouts(chip
)) {
828 dev_err(dev
, "Could not get TPM timeouts and durations\n");
833 if (tpm_do_selftest(chip
)) {
834 dev_err(dev
, "TPM self test failed\n");
840 return tpm_chip_register(chip
);
842 tpm_tis_remove(chip
);
846 #ifdef CONFIG_PM_SLEEP
847 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
851 /* reenable interrupts that device may have lost or
852 BIOS/firmware may have disabled */
853 iowrite8(chip
->vendor
.irq
, chip
->vendor
.iobase
+
854 TPM_INT_VECTOR(chip
->vendor
.locality
));
857 ioread32(chip
->vendor
.iobase
+
858 TPM_INT_ENABLE(chip
->vendor
.locality
));
860 intmask
|= TPM_INTF_CMD_READY_INT
861 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
862 | TPM_INTF_STS_VALID_INT
| TPM_GLOBAL_INT_ENABLE
;
865 chip
->vendor
.iobase
+ TPM_INT_ENABLE(chip
->vendor
.locality
));
868 static int tpm_tis_resume(struct device
*dev
)
870 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
873 if (chip
->vendor
.irq
)
874 tpm_tis_reenable_interrupts(chip
);
876 ret
= tpm_pm_resume(dev
);
880 /* TPM 1.2 requires self-test on resume. This function actually returns
881 * an error code but for unknown reason it isn't handled.
883 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
884 tpm_do_selftest(chip
);
890 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
893 static int tpm_tis_pnp_init(struct pnp_dev
*pnp_dev
,
894 const struct pnp_device_id
*pnp_id
)
896 resource_size_t start
, len
;
897 unsigned int irq
= 0;
898 acpi_handle acpi_dev_handle
= NULL
;
900 start
= pnp_mem_start(pnp_dev
, 0);
901 len
= pnp_mem_len(pnp_dev
, 0);
903 if (pnp_irq_valid(pnp_dev
, 0))
904 irq
= pnp_irq(pnp_dev
, 0);
908 if (is_itpm(pnp_dev
))
912 if (pnp_acpi_device(pnp_dev
))
913 acpi_dev_handle
= pnp_acpi_device(pnp_dev
)->handle
;
916 return tpm_tis_init(&pnp_dev
->dev
, acpi_dev_handle
, start
, len
, irq
);
919 static struct pnp_device_id tpm_pnp_tbl
[] = {
920 {"PNP0C31", 0}, /* TPM */
921 {"ATM1200", 0}, /* Atmel */
922 {"IFX0102", 0}, /* Infineon */
923 {"BCM0101", 0}, /* Broadcom */
924 {"BCM0102", 0}, /* Broadcom */
925 {"NSC1200", 0}, /* National */
926 {"ICO0102", 0}, /* Intel */
928 {"", 0}, /* User Specified */
929 {"", 0} /* Terminator */
931 MODULE_DEVICE_TABLE(pnp
, tpm_pnp_tbl
);
933 static void tpm_tis_pnp_remove(struct pnp_dev
*dev
)
935 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
936 tpm_chip_unregister(chip
);
937 tpm_tis_remove(chip
);
940 static struct pnp_driver tis_pnp_driver
= {
942 .id_table
= tpm_pnp_tbl
,
943 .probe
= tpm_tis_pnp_init
,
944 .remove
= tpm_tis_pnp_remove
,
950 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
951 module_param_string(hid
, tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
,
952 sizeof(tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
), 0444);
953 MODULE_PARM_DESC(hid
, "Set additional specific HID for this driver to probe");
956 static struct platform_driver tis_drv
= {
963 static struct platform_device
*pdev
;
966 module_param(force
, bool, 0444);
967 MODULE_PARM_DESC(force
, "Force device probe rather than using ACPI entry");
968 static int __init
init_tis(void)
973 return pnp_register_driver(&tis_pnp_driver
);
976 rc
= platform_driver_register(&tis_drv
);
979 pdev
= platform_device_register_simple("tpm_tis", -1, NULL
, 0);
984 rc
= tpm_tis_init(&pdev
->dev
, NULL
, TIS_MEM_BASE
, TIS_MEM_LEN
, 0);
989 platform_device_unregister(pdev
);
991 platform_driver_unregister(&tis_drv
);
995 static void __exit
cleanup_tis(void)
997 struct tpm_chip
*chip
;
1000 pnp_unregister_driver(&tis_pnp_driver
);
1004 chip
= dev_get_drvdata(&pdev
->dev
);
1005 tpm_chip_unregister(chip
);
1006 tpm_tis_remove(chip
);
1007 platform_device_unregister(pdev
);
1008 platform_driver_unregister(&tis_drv
);
1011 module_init(init_tis
);
1012 module_exit(cleanup_tis
);
1013 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1014 MODULE_DESCRIPTION("TPM Driver");
1015 MODULE_VERSION("2.0");
1016 MODULE_LICENSE("GPL");