2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014, 2015 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>
32 #include "tpm_tis_core.h"
35 TPM_ACCESS_VALID
= 0x80,
36 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
37 TPM_ACCESS_REQUEST_PENDING
= 0x04,
38 TPM_ACCESS_REQUEST_USE
= 0x02,
43 TPM_STS_COMMAND_READY
= 0x40,
45 TPM_STS_DATA_AVAIL
= 0x10,
46 TPM_STS_DATA_EXPECT
= 0x08,
50 TPM_GLOBAL_INT_ENABLE
= 0x80000000,
51 TPM_INTF_BURST_COUNT_STATIC
= 0x100,
52 TPM_INTF_CMD_READY_INT
= 0x080,
53 TPM_INTF_INT_EDGE_FALLING
= 0x040,
54 TPM_INTF_INT_EDGE_RISING
= 0x020,
55 TPM_INTF_INT_LEVEL_LOW
= 0x010,
56 TPM_INTF_INT_LEVEL_HIGH
= 0x008,
57 TPM_INTF_LOCALITY_CHANGE_INT
= 0x004,
58 TPM_INTF_STS_VALID_INT
= 0x002,
59 TPM_INTF_DATA_AVAIL_INT
= 0x001,
64 TIS_SHORT_TIMEOUT
= 750, /* ms */
65 TIS_LONG_TIMEOUT
= 2000, /* 2 sec */
70 /* irq > 0 means: use irq $irq;
71 * irq = 0 means: autoprobe for an irq;
72 * irq = -1 means: no irq support
77 /* Some timeout values are needed before it is known whether the chip is
80 #define TIS_TIMEOUT_A_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
81 #define TIS_TIMEOUT_B_MAX max(TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
82 #define TIS_TIMEOUT_C_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
83 #define TIS_TIMEOUT_D_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
85 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
86 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
87 #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
88 #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
89 #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
90 #define TPM_STS(l) (0x0018 | ((l) << 12))
91 #define TPM_STS3(l) (0x001b | ((l) << 12))
92 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
94 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
95 #define TPM_RID(l) (0x0F04 | ((l) << 12))
97 struct tpm_tis_tcg_phy
{
98 struct tpm_tis_data priv
;
102 static inline struct tpm_tis_tcg_phy
*to_tpm_tis_tcg_phy(struct tpm_tis_data
*data
)
104 return container_of(data
, struct tpm_tis_tcg_phy
, priv
);
107 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
108 static int has_hid(struct acpi_device
*dev
, const char *hid
)
110 struct acpi_hardware_id
*id
;
112 list_for_each_entry(id
, &dev
->pnp
.ids
, list
)
113 if (!strcmp(hid
, id
->id
))
119 static inline int is_itpm(struct acpi_device
*dev
)
121 return has_hid(dev
, "INTC0102");
124 static inline int is_itpm(struct acpi_device
*dev
)
130 /* Before we attempt to access the TPM we must see that the valid bit is set.
131 * The specification says that this bit is 0 at reset and remains 0 until the
132 * 'TPM has gone through its self test and initialization and has established
133 * correct values in the other bits.' */
134 static int wait_startup(struct tpm_chip
*chip
, int l
)
136 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
137 unsigned long stop
= jiffies
+ chip
->timeout_a
;
142 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
146 if (access
& TPM_ACCESS_VALID
)
149 } while (time_before(jiffies
, stop
));
153 static int check_locality(struct tpm_chip
*chip
, int l
)
155 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
159 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
163 if ((access
& (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
164 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
))
165 return priv
->locality
= l
;
170 static void release_locality(struct tpm_chip
*chip
, int l
, int force
)
172 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
176 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
180 if (force
|| (access
&
181 (TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
)) ==
182 (TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
))
183 tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_ACTIVE_LOCALITY
);
187 static int request_locality(struct tpm_chip
*chip
, int l
)
189 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
190 unsigned long stop
, timeout
;
193 if (check_locality(chip
, l
) >= 0)
196 rc
= tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_REQUEST_USE
);
200 stop
= jiffies
+ chip
->timeout_a
;
202 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
204 timeout
= stop
- jiffies
;
205 if ((long)timeout
<= 0)
207 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
213 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
214 clear_thread_flag(TIF_SIGPENDING
);
218 /* wait for burstcount */
220 if (check_locality(chip
, l
) >= 0)
224 while (time_before(jiffies
, stop
));
229 static u8
tpm_tis_status(struct tpm_chip
*chip
)
231 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
235 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
), &status
);
242 static void tpm_tis_ready(struct tpm_chip
*chip
)
244 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
246 /* this causes the current command to be aborted */
247 tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_COMMAND_READY
);
250 static int get_burstcount(struct tpm_chip
*chip
)
252 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
257 /* wait for burstcount */
258 /* which timeout value, spec has 2 answers (c & d) */
259 stop
= jiffies
+ chip
->timeout_d
;
261 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
) + 1, &value
);
266 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
) + 2, &value
);
270 burstcnt
+= value
<< 8;
274 } while (time_before(jiffies
, stop
));
278 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
280 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
281 int size
= 0, burstcnt
, rc
;
283 while (size
< count
&&
284 wait_for_tpm_stat(chip
,
285 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
287 &priv
->read_queue
, true) == 0) {
288 burstcnt
= min_t(int, get_burstcount(chip
), count
- size
);
289 rc
= tpm_tis_read_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
290 burstcnt
, buf
+ size
);
299 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
301 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
303 int expected
, status
;
305 if (count
< TPM_HEADER_SIZE
) {
310 /* read first 10 bytes, including tag, paramsize, and result */
312 recv_data(chip
, buf
, TPM_HEADER_SIZE
)) < TPM_HEADER_SIZE
) {
313 dev_err(&chip
->dev
, "Unable to read header\n");
317 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
318 if (expected
> count
) {
324 recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
325 expected
- TPM_HEADER_SIZE
)) < expected
) {
326 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
331 wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
332 &priv
->int_queue
, false);
333 status
= tpm_tis_status(chip
);
334 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
335 dev_err(&chip
->dev
, "Error left over data\n");
342 release_locality(chip
, priv
->locality
, 0);
347 module_param(itpm
, bool, 0444);
348 MODULE_PARM_DESC(itpm
, "Force iTPM workarounds (found on some Lenovo laptops)");
351 * If interrupts are used (signaled by an irq set in the vendor structure)
352 * tpm.c can skip polling for the data to be available as the interrupt is
355 static int tpm_tis_send_data(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
357 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
358 int rc
, status
, burstcnt
;
361 if (request_locality(chip
, 0) < 0)
364 status
= tpm_tis_status(chip
);
365 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
367 if (wait_for_tpm_stat
368 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
369 &priv
->int_queue
, false) < 0) {
375 while (count
< len
- 1) {
376 burstcnt
= min_t(int, get_burstcount(chip
), len
- count
- 1);
377 rc
= tpm_tis_write_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
378 burstcnt
, buf
+ count
);
384 wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
385 &priv
->int_queue
, false);
386 status
= tpm_tis_status(chip
);
387 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) == 0) {
393 /* write last byte */
394 rc
= tpm_tis_write8(priv
, TPM_DATA_FIFO(priv
->locality
), buf
[count
]);
398 wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
399 &priv
->int_queue
, false);
400 status
= tpm_tis_status(chip
);
401 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
410 release_locality(chip
, priv
->locality
, 0);
414 static void disable_interrupts(struct tpm_chip
*chip
)
416 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
420 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
424 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
425 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
427 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
429 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
433 * If interrupts are used (signaled by an irq set in the vendor structure)
434 * tpm.c can skip polling for the data to be available as the interrupt is
437 static int tpm_tis_send_main(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
439 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
444 rc
= tpm_tis_send_data(chip
, buf
, len
);
449 rc
= tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_GO
);
453 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
454 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
456 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
457 dur
= tpm2_calc_ordinal_duration(chip
, ordinal
);
459 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
461 if (wait_for_tpm_stat
462 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
463 &priv
->read_queue
, false) < 0) {
471 release_locality(chip
, priv
->locality
, 0);
475 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
477 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
480 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
) || priv
->irq_tested
)
481 return tpm_tis_send_main(chip
, buf
, len
);
483 /* Verify receipt of the expected IRQ */
486 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
487 rc
= tpm_tis_send_main(chip
, buf
, len
);
489 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
490 if (!priv
->irq_tested
)
492 if (!priv
->irq_tested
)
493 disable_interrupts(chip
);
494 priv
->irq_tested
= true;
498 struct tis_vendor_timeout_override
{
500 unsigned long timeout_us
[4];
503 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
505 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
506 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
509 static bool tpm_tis_update_timeouts(struct tpm_chip
*chip
,
510 unsigned long *timeout_cap
)
512 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
516 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
520 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
521 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
523 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
524 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
532 * Early probing for iTPM with STS_DATA_EXPECT flaw.
533 * Try sending command without itpm flag set and if that
534 * fails, repeat with itpm flag set.
536 static int probe_itpm(struct tpm_chip
*chip
)
538 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
540 u8 cmd_getticks
[] = {
541 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
542 0x00, 0x00, 0x00, 0xf1
544 size_t len
= sizeof(cmd_getticks
);
545 bool rem_itpm
= itpm
;
548 rc
= tpm_tis_read16(priv
, TPM_DID_VID(0), &vendor
);
552 /* probe only iTPMS */
553 if (vendor
!= TPM_VID_INTEL
)
558 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
563 release_locality(chip
, priv
->locality
, 0);
567 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
569 dev_info(&chip
->dev
, "Detected an iTPM.\n");
577 release_locality(chip
, priv
->locality
, 0);
582 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
584 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
586 switch (priv
->manufacturer_id
) {
587 case TPM_VID_WINBOND
:
588 return ((status
== TPM_STS_VALID
) ||
589 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
591 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
593 return (status
== TPM_STS_COMMAND_READY
);
597 static const struct tpm_class_ops tpm_tis
= {
598 .status
= tpm_tis_status
,
599 .recv
= tpm_tis_recv
,
600 .send
= tpm_tis_send
,
601 .cancel
= tpm_tis_ready
,
602 .update_timeouts
= tpm_tis_update_timeouts
,
603 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
604 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
605 .req_canceled
= tpm_tis_req_canceled
,
608 static int tpm_tcg_read_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
611 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
614 *result
++ = ioread8(phy
->iobase
+ addr
);
618 static int tpm_tcg_write_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
621 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
624 iowrite8(*value
++, phy
->iobase
+ addr
);
628 static int tpm_tcg_read16(struct tpm_tis_data
*data
, u32 addr
, u16
*result
)
630 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
632 *result
= ioread16(phy
->iobase
+ addr
);
636 static int tpm_tcg_read32(struct tpm_tis_data
*data
, u32 addr
, u32
*result
)
638 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
640 *result
= ioread32(phy
->iobase
+ addr
);
644 static int tpm_tcg_write32(struct tpm_tis_data
*data
, u32 addr
, u32 value
)
646 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
648 iowrite32(value
, phy
->iobase
+ addr
);
652 static const struct tpm_tis_phy_ops tpm_tcg
= {
653 .read_bytes
= tpm_tcg_read_bytes
,
654 .write_bytes
= tpm_tcg_write_bytes
,
655 .read16
= tpm_tcg_read16
,
656 .read32
= tpm_tcg_read32
,
657 .write32
= tpm_tcg_write32
,
660 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
662 struct tpm_chip
*chip
= dev_id
;
663 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
667 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
674 priv
->irq_tested
= true;
675 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
676 wake_up_interruptible(&priv
->read_queue
);
677 if (interrupt
& TPM_INTF_LOCALITY_CHANGE_INT
)
678 for (i
= 0; i
< 5; i
++)
679 if (check_locality(chip
, i
) >= 0)
682 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
683 TPM_INTF_CMD_READY_INT
))
684 wake_up_interruptible(&priv
->int_queue
);
686 /* Clear interrupts handled with TPM_EOI */
687 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), interrupt
);
691 tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
695 /* Register the IRQ and issue a command that will cause an interrupt. If an
696 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
697 * everything and leave in polling mode. Returns 0 on success.
699 static int tpm_tis_probe_irq_single(struct tpm_chip
*chip
, u32 intmask
,
702 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
707 if (devm_request_irq(chip
->dev
.parent
, irq
, tis_int_handler
, flags
,
708 dev_name(&chip
->dev
), chip
) != 0) {
709 dev_info(&chip
->dev
, "Unable to request irq: %d for probe\n",
715 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
720 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), irq
);
724 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &int_status
);
728 /* Clear all existing */
729 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), int_status
);
734 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
),
735 intmask
| TPM_GLOBAL_INT_ENABLE
);
739 priv
->irq_tested
= false;
741 /* Generate an interrupt by having the core call through to
744 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
745 tpm2_gen_interrupt(chip
);
747 tpm_gen_interrupt(chip
);
749 /* tpm_tis_send will either confirm the interrupt is working or it
750 * will call disable_irq which undoes all of the above.
752 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
753 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
),
764 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
765 * do not have ACPI/etc. We typically expect the interrupt to be declared if
768 static void tpm_tis_probe_irq(struct tpm_chip
*chip
, u32 intmask
)
770 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
774 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
779 if (!original_int_vec
) {
780 if (IS_ENABLED(CONFIG_X86
))
781 for (i
= 3; i
<= 15; i
++)
782 if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
785 } else if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
790 static bool interrupts
= true;
791 module_param(interrupts
, bool, 0444);
792 MODULE_PARM_DESC(interrupts
, "Enable interrupts");
794 static void tpm_tis_remove(struct tpm_chip
*chip
)
796 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
797 u32 reg
= TPM_INT_ENABLE(priv
->locality
);
801 rc
= tpm_tis_read32(priv
, reg
, &interrupt
);
805 tpm_tis_write32(priv
, reg
, ~TPM_GLOBAL_INT_ENABLE
& interrupt
);
806 release_locality(chip
, priv
->locality
, 1);
809 static int tpm_tis_init(struct device
*dev
, struct tpm_info
*tpm_info
,
810 acpi_handle acpi_dev_handle
)
812 u32 vendor
, intfcaps
, intmask
;
815 struct tpm_chip
*chip
;
816 struct tpm_tis_tcg_phy
*phy
;
818 phy
= devm_kzalloc(dev
, sizeof(struct tpm_tis_tcg_phy
), GFP_KERNEL
);
822 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
824 return PTR_ERR(chip
);
827 chip
->acpi_dev_handle
= acpi_dev_handle
;
830 phy
->iobase
= devm_ioremap_resource(dev
, &tpm_info
->res
);
831 if (IS_ERR(phy
->iobase
))
832 return PTR_ERR(phy
->iobase
);
834 phy
->priv
.phy_ops
= &tpm_tcg
;
836 /* Maximum timeouts */
837 chip
->timeout_a
= TIS_TIMEOUT_A_MAX
;
838 chip
->timeout_b
= TIS_TIMEOUT_B_MAX
;
839 chip
->timeout_c
= TIS_TIMEOUT_C_MAX
;
840 chip
->timeout_d
= TIS_TIMEOUT_D_MAX
;
842 dev_set_drvdata(&chip
->dev
, &phy
->priv
);
844 if (wait_startup(chip
, 0) != 0) {
849 /* Take control of the TPM's interrupt hardware and shut it off */
850 rc
= tpm_tis_read32(&phy
->priv
, TPM_INT_ENABLE(phy
->priv
.locality
),
855 intmask
|= TPM_INTF_CMD_READY_INT
| TPM_INTF_LOCALITY_CHANGE_INT
|
856 TPM_INTF_DATA_AVAIL_INT
| TPM_INTF_STS_VALID_INT
;
857 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
858 tpm_tis_write32(&phy
->priv
, TPM_INT_ENABLE(phy
->priv
.locality
),
861 if (request_locality(chip
, 0) != 0) {
866 rc
= tpm2_probe(chip
);
870 rc
= tpm_tis_read32(&phy
->priv
, TPM_DID_VID(0), &vendor
);
874 phy
->priv
.manufacturer_id
= vendor
;
876 rc
= tpm_tis_read8(&phy
->priv
, TPM_RID(0), &rid
);
880 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
881 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
885 probe
= probe_itpm(chip
);
894 dev_info(dev
, "Intel iTPM workaround enabled\n");
896 /* Figure out the capabilities */
897 rc
= tpm_tis_read32(&phy
->priv
, TPM_INTF_CAPS(phy
->priv
.locality
),
902 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
904 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
905 dev_dbg(dev
, "\tBurst Count Static\n");
906 if (intfcaps
& TPM_INTF_CMD_READY_INT
)
907 dev_dbg(dev
, "\tCommand Ready Int Support\n");
908 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
909 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
910 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
911 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
912 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
913 dev_dbg(dev
, "\tInterrupt Level Low\n");
914 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
915 dev_dbg(dev
, "\tInterrupt Level High\n");
916 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
)
917 dev_dbg(dev
, "\tLocality Change Int Support\n");
918 if (intfcaps
& TPM_INTF_STS_VALID_INT
)
919 dev_dbg(dev
, "\tSts Valid Int Support\n");
920 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
)
921 dev_dbg(dev
, "\tData Avail Int Support\n");
923 /* Very early on issue a command to the TPM in polling mode to make
924 * sure it works. May as well use that command to set the proper
925 * timeouts for the driver.
927 if (tpm_get_timeouts(chip
)) {
928 dev_err(dev
, "Could not get TPM timeouts and durations\n");
933 /* INTERRUPT Setup */
934 init_waitqueue_head(&phy
->priv
.read_queue
);
935 init_waitqueue_head(&phy
->priv
.int_queue
);
936 if (interrupts
&& tpm_info
->irq
!= -1) {
938 tpm_tis_probe_irq_single(chip
, intmask
, IRQF_SHARED
,
940 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
))
941 dev_err(&chip
->dev
, FW_BUG
942 "TPM interrupt not working, polling instead\n");
944 tpm_tis_probe_irq(chip
, intmask
);
947 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
) {
948 rc
= tpm2_do_selftest(chip
);
949 if (rc
== TPM2_RC_INITIALIZE
) {
950 dev_warn(dev
, "Firmware has not started TPM\n");
951 rc
= tpm2_startup(chip
, TPM2_SU_CLEAR
);
953 rc
= tpm2_do_selftest(chip
);
957 dev_err(dev
, "TPM self test failed\n");
963 if (tpm_do_selftest(chip
)) {
964 dev_err(dev
, "TPM self test failed\n");
970 return tpm_chip_register(chip
);
972 tpm_tis_remove(chip
);
976 #ifdef CONFIG_PM_SLEEP
977 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
979 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
983 /* reenable interrupts that device may have lost or
984 BIOS/firmware may have disabled */
985 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), priv
->irq
);
989 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
993 intmask
|= TPM_INTF_CMD_READY_INT
994 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
995 | TPM_INTF_STS_VALID_INT
| TPM_GLOBAL_INT_ENABLE
;
997 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1000 static int tpm_tis_resume(struct device
*dev
)
1002 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1005 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
)
1006 tpm_tis_reenable_interrupts(chip
);
1008 ret
= tpm_pm_resume(dev
);
1012 /* TPM 1.2 requires self-test on resume. This function actually returns
1013 * an error code but for unknown reason it isn't handled.
1015 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
1016 tpm_do_selftest(chip
);
1022 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
1024 static int tpm_tis_pnp_init(struct pnp_dev
*pnp_dev
,
1025 const struct pnp_device_id
*pnp_id
)
1027 struct tpm_info tpm_info
= {};
1028 acpi_handle acpi_dev_handle
= NULL
;
1029 struct resource
*res
;
1031 res
= pnp_get_resource(pnp_dev
, IORESOURCE_MEM
, 0);
1034 tpm_info
.res
= *res
;
1036 if (pnp_irq_valid(pnp_dev
, 0))
1037 tpm_info
.irq
= pnp_irq(pnp_dev
, 0);
1041 if (pnp_acpi_device(pnp_dev
)) {
1042 if (is_itpm(pnp_acpi_device(pnp_dev
)))
1045 acpi_dev_handle
= ACPI_HANDLE(&pnp_dev
->dev
);
1048 return tpm_tis_init(&pnp_dev
->dev
, &tpm_info
, acpi_dev_handle
);
1051 static struct pnp_device_id tpm_pnp_tbl
[] = {
1052 {"PNP0C31", 0}, /* TPM */
1053 {"ATM1200", 0}, /* Atmel */
1054 {"IFX0102", 0}, /* Infineon */
1055 {"BCM0101", 0}, /* Broadcom */
1056 {"BCM0102", 0}, /* Broadcom */
1057 {"NSC1200", 0}, /* National */
1058 {"ICO0102", 0}, /* Intel */
1060 {"", 0}, /* User Specified */
1061 {"", 0} /* Terminator */
1063 MODULE_DEVICE_TABLE(pnp
, tpm_pnp_tbl
);
1065 static void tpm_tis_pnp_remove(struct pnp_dev
*dev
)
1067 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
1069 tpm_chip_unregister(chip
);
1070 tpm_tis_remove(chip
);
1073 static struct pnp_driver tis_pnp_driver
= {
1075 .id_table
= tpm_pnp_tbl
,
1076 .probe
= tpm_tis_pnp_init
,
1077 .remove
= tpm_tis_pnp_remove
,
1083 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
1084 module_param_string(hid
, tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
,
1085 sizeof(tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
), 0444);
1086 MODULE_PARM_DESC(hid
, "Set additional specific HID for this driver to probe");
1089 static int tpm_check_resource(struct acpi_resource
*ares
, void *data
)
1091 struct tpm_info
*tpm_info
= (struct tpm_info
*) data
;
1092 struct resource res
;
1094 if (acpi_dev_resource_interrupt(ares
, 0, &res
))
1095 tpm_info
->irq
= res
.start
;
1096 else if (acpi_dev_resource_memory(ares
, &res
)) {
1097 tpm_info
->res
= res
;
1098 tpm_info
->res
.name
= NULL
;
1104 static int tpm_tis_acpi_init(struct acpi_device
*acpi_dev
)
1106 struct acpi_table_tpm2
*tbl
;
1108 struct list_head resources
;
1109 struct tpm_info tpm_info
= {};
1112 st
= acpi_get_table(ACPI_SIG_TPM2
, 1,
1113 (struct acpi_table_header
**) &tbl
);
1114 if (ACPI_FAILURE(st
) || tbl
->header
.length
< sizeof(*tbl
)) {
1115 dev_err(&acpi_dev
->dev
,
1116 FW_BUG
"failed to get TPM2 ACPI table\n");
1120 if (tbl
->start_method
!= ACPI_TPM2_MEMORY_MAPPED
)
1123 INIT_LIST_HEAD(&resources
);
1125 ret
= acpi_dev_get_resources(acpi_dev
, &resources
, tpm_check_resource
,
1130 acpi_dev_free_resource_list(&resources
);
1132 if (resource_type(&tpm_info
.res
) != IORESOURCE_MEM
) {
1133 dev_err(&acpi_dev
->dev
,
1134 FW_BUG
"TPM2 ACPI table does not define a memory resource\n");
1138 if (is_itpm(acpi_dev
))
1141 return tpm_tis_init(&acpi_dev
->dev
, &tpm_info
, acpi_dev
->handle
);
1144 static int tpm_tis_acpi_remove(struct acpi_device
*dev
)
1146 struct tpm_chip
*chip
= dev_get_drvdata(&dev
->dev
);
1148 tpm_chip_unregister(chip
);
1149 tpm_tis_remove(chip
);
1154 static struct acpi_device_id tpm_acpi_tbl
[] = {
1155 {"MSFT0101", 0}, /* TPM 2.0 */
1157 {"", 0}, /* User Specified */
1158 {"", 0} /* Terminator */
1160 MODULE_DEVICE_TABLE(acpi
, tpm_acpi_tbl
);
1162 static struct acpi_driver tis_acpi_driver
= {
1164 .ids
= tpm_acpi_tbl
,
1166 .add
= tpm_tis_acpi_init
,
1167 .remove
= tpm_tis_acpi_remove
,
1175 static struct platform_device
*force_pdev
;
1177 static int tpm_tis_plat_probe(struct platform_device
*pdev
)
1179 struct tpm_info tpm_info
= {};
1180 struct resource
*res
;
1182 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1184 dev_err(&pdev
->dev
, "no memory resource defined\n");
1187 tpm_info
.res
= *res
;
1189 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1191 tpm_info
.irq
= res
->start
;
1193 if (pdev
== force_pdev
)
1196 /* When forcing auto probe the IRQ */
1200 return tpm_tis_init(&pdev
->dev
, &tpm_info
, NULL
);
1203 static int tpm_tis_plat_remove(struct platform_device
*pdev
)
1205 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
1207 tpm_chip_unregister(chip
);
1208 tpm_tis_remove(chip
);
1213 static struct platform_driver tis_drv
= {
1214 .probe
= tpm_tis_plat_probe
,
1215 .remove
= tpm_tis_plat_remove
,
1224 module_param(force
, bool, 0444);
1225 MODULE_PARM_DESC(force
, "Force device probe rather than using ACPI entry");
1228 static int tpm_tis_force_device(void)
1230 struct platform_device
*pdev
;
1231 static const struct resource x86_resources
[] = {
1233 .start
= 0xFED40000,
1234 .end
= 0xFED40000 + TIS_MEM_LEN
- 1,
1235 .flags
= IORESOURCE_MEM
,
1242 /* The driver core will match the name tpm_tis of the device to
1243 * the tpm_tis platform driver and complete the setup via
1244 * tpm_tis_plat_probe
1246 pdev
= platform_device_register_simple("tpm_tis", -1, x86_resources
,
1247 ARRAY_SIZE(x86_resources
));
1249 return PTR_ERR(pdev
);
1255 static int __init
init_tis(void)
1259 rc
= tpm_tis_force_device();
1263 rc
= platform_driver_register(&tis_drv
);
1268 rc
= acpi_bus_register_driver(&tis_acpi_driver
);
1273 if (IS_ENABLED(CONFIG_PNP
)) {
1274 rc
= pnp_register_driver(&tis_pnp_driver
);
1283 acpi_bus_unregister_driver(&tis_acpi_driver
);
1286 platform_device_unregister(force_pdev
);
1289 platform_device_unregister(force_pdev
);
1294 static void __exit
cleanup_tis(void)
1296 pnp_unregister_driver(&tis_pnp_driver
);
1298 acpi_bus_unregister_driver(&tis_acpi_driver
);
1300 platform_driver_unregister(&tis_drv
);
1303 platform_device_unregister(force_pdev
);
1306 module_init(init_tis
);
1307 module_exit(cleanup_tis
);
1308 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1309 MODULE_DESCRIPTION("TPM Driver");
1310 MODULE_VERSION("2.0");
1311 MODULE_LICENSE("GPL");