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"
34 /* This is a polling delay to check for status and burstcount.
35 * As per ddwg input, expectation is that status check and burstcount
36 * check should return within few usecs.
38 #define TPM_POLL_SLEEP 1 /* msec */
40 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
);
42 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
43 bool check_cancel
, bool *canceled
)
45 u8 status
= chip
->ops
->status(chip
);
48 if ((status
& mask
) == mask
)
50 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
57 static int wait_for_tpm_stat(struct tpm_chip
*chip
, u8 mask
,
58 unsigned long timeout
, wait_queue_head_t
*queue
,
64 bool canceled
= false;
66 /* check current status */
67 status
= chip
->ops
->status(chip
);
68 if ((status
& mask
) == mask
)
71 stop
= jiffies
+ timeout
;
73 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
75 timeout
= stop
- jiffies
;
76 if ((long)timeout
<= 0)
78 rc
= wait_event_interruptible_timeout(*queue
,
79 wait_for_tpm_stat_cond(chip
, mask
, check_cancel
,
87 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
88 clear_thread_flag(TIF_SIGPENDING
);
93 tpm_msleep(TPM_POLL_SLEEP
);
94 status
= chip
->ops
->status(chip
);
95 if ((status
& mask
) == mask
)
97 } while (time_before(jiffies
, stop
));
102 /* Before we attempt to access the TPM we must see that the valid bit is set.
103 * The specification says that this bit is 0 at reset and remains 0 until the
104 * 'TPM has gone through its self test and initialization and has established
105 * correct values in the other bits.'
107 static int wait_startup(struct tpm_chip
*chip
, int l
)
109 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
110 unsigned long stop
= jiffies
+ chip
->timeout_a
;
116 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
120 if (access
& TPM_ACCESS_VALID
)
122 tpm_msleep(TPM_TIMEOUT
);
123 } while (time_before(jiffies
, stop
));
127 static bool check_locality(struct tpm_chip
*chip
, int l
)
129 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
133 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
137 if ((access
& (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
138 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) {
146 static void release_locality(struct tpm_chip
*chip
, int l
)
148 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
150 tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_ACTIVE_LOCALITY
);
153 static int request_locality(struct tpm_chip
*chip
, int l
)
155 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
156 unsigned long stop
, timeout
;
159 if (check_locality(chip
, l
))
162 rc
= tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_REQUEST_USE
);
166 stop
= jiffies
+ chip
->timeout_a
;
168 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
170 timeout
= stop
- jiffies
;
171 if ((long)timeout
<= 0)
173 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
179 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
180 clear_thread_flag(TIF_SIGPENDING
);
184 /* wait for burstcount */
186 if (check_locality(chip
, l
))
188 tpm_msleep(TPM_TIMEOUT
);
189 } while (time_before(jiffies
, stop
));
194 static u8
tpm_tis_status(struct tpm_chip
*chip
)
196 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
200 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
), &status
);
207 static void tpm_tis_ready(struct tpm_chip
*chip
)
209 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
211 /* this causes the current command to be aborted */
212 tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_COMMAND_READY
);
215 static int get_burstcount(struct tpm_chip
*chip
)
217 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
222 /* wait for burstcount */
223 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
224 stop
= jiffies
+ chip
->timeout_a
;
226 stop
= jiffies
+ chip
->timeout_d
;
228 rc
= tpm_tis_read32(priv
, TPM_STS(priv
->locality
), &value
);
232 burstcnt
= (value
>> 8) & 0xFFFF;
235 tpm_msleep(TPM_POLL_SLEEP
);
236 } while (time_before(jiffies
, stop
));
240 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
242 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
243 int size
= 0, burstcnt
, rc
;
245 while (size
< count
) {
246 rc
= wait_for_tpm_stat(chip
,
247 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
249 &priv
->read_queue
, true);
252 burstcnt
= get_burstcount(chip
);
254 dev_err(&chip
->dev
, "Unable to read burstcount\n");
257 burstcnt
= min_t(int, burstcnt
, count
- size
);
259 rc
= tpm_tis_read_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
260 burstcnt
, buf
+ size
);
269 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
271 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
273 int expected
, status
;
275 if (count
< TPM_HEADER_SIZE
) {
280 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
281 /* read first 10 bytes, including tag, paramsize, and result */
282 if (size
< TPM_HEADER_SIZE
) {
283 dev_err(&chip
->dev
, "Unable to read header\n");
287 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
288 if (expected
> count
) {
293 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
294 expected
- TPM_HEADER_SIZE
);
295 if (size
< expected
) {
296 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
301 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
302 &priv
->int_queue
, false) < 0) {
306 status
= tpm_tis_status(chip
);
307 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
308 dev_err(&chip
->dev
, "Error left over data\n");
319 * If interrupts are used (signaled by an irq set in the vendor structure)
320 * tpm.c can skip polling for the data to be available as the interrupt is
323 static int tpm_tis_send_data(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
325 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
326 int rc
, status
, burstcnt
;
328 bool itpm
= priv
->flags
& TPM_TIS_ITPM_WORKAROUND
;
330 status
= tpm_tis_status(chip
);
331 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
333 if (wait_for_tpm_stat
334 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
335 &priv
->int_queue
, false) < 0) {
341 while (count
< len
- 1) {
342 burstcnt
= get_burstcount(chip
);
344 dev_err(&chip
->dev
, "Unable to read burstcount\n");
348 burstcnt
= min_t(int, burstcnt
, len
- count
- 1);
349 rc
= tpm_tis_write_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
350 burstcnt
, buf
+ count
);
356 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
357 &priv
->int_queue
, false) < 0) {
361 status
= tpm_tis_status(chip
);
362 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) == 0) {
368 /* write last byte */
369 rc
= tpm_tis_write8(priv
, TPM_DATA_FIFO(priv
->locality
), buf
[count
]);
373 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
374 &priv
->int_queue
, false) < 0) {
378 status
= tpm_tis_status(chip
);
379 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) != 0) {
391 static void disable_interrupts(struct tpm_chip
*chip
)
393 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
397 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
401 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
402 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
404 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
406 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
410 * If interrupts are used (signaled by an irq set in the vendor structure)
411 * tpm.c can skip polling for the data to be available as the interrupt is
414 static int tpm_tis_send_main(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
416 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
421 rc
= tpm_tis_send_data(chip
, buf
, len
);
426 rc
= tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_GO
);
430 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
431 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
433 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
434 dur
= tpm2_calc_ordinal_duration(chip
, ordinal
);
436 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
438 if (wait_for_tpm_stat
439 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
440 &priv
->read_queue
, false) < 0) {
451 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
454 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
456 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
) || priv
->irq_tested
)
457 return tpm_tis_send_main(chip
, buf
, len
);
459 /* Verify receipt of the expected IRQ */
462 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
463 rc
= tpm_tis_send_main(chip
, buf
, len
);
465 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
466 if (!priv
->irq_tested
)
468 if (!priv
->irq_tested
)
469 disable_interrupts(chip
);
470 priv
->irq_tested
= true;
474 struct tis_vendor_timeout_override
{
476 unsigned long timeout_us
[4];
479 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
481 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
482 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
485 static bool tpm_tis_update_timeouts(struct tpm_chip
*chip
,
486 unsigned long *timeout_cap
)
488 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
492 if (chip
->ops
->clk_enable
!= NULL
)
493 chip
->ops
->clk_enable(chip
, true);
495 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
499 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
500 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
502 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
503 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
510 if (chip
->ops
->clk_enable
!= NULL
)
511 chip
->ops
->clk_enable(chip
, false);
517 * Early probing for iTPM with STS_DATA_EXPECT flaw.
518 * Try sending command without itpm flag set and if that
519 * fails, repeat with itpm flag set.
521 static int probe_itpm(struct tpm_chip
*chip
)
523 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
525 static const u8 cmd_getticks
[] = {
526 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
527 0x00, 0x00, 0x00, 0xf1
529 size_t len
= sizeof(cmd_getticks
);
532 if (priv
->flags
& TPM_TIS_ITPM_WORKAROUND
)
535 rc
= tpm_tis_read16(priv
, TPM_DID_VID(0), &vendor
);
539 /* probe only iTPMS */
540 if (vendor
!= TPM_VID_INTEL
)
543 if (request_locality(chip
, 0) != 0)
546 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
552 priv
->flags
|= TPM_TIS_ITPM_WORKAROUND
;
554 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
556 dev_info(&chip
->dev
, "Detected an iTPM.\n");
558 priv
->flags
&= ~TPM_TIS_ITPM_WORKAROUND
;
564 release_locality(chip
, priv
->locality
);
569 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
571 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
573 switch (priv
->manufacturer_id
) {
574 case TPM_VID_WINBOND
:
575 return ((status
== TPM_STS_VALID
) ||
576 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
578 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
580 return (status
== TPM_STS_COMMAND_READY
);
584 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
586 struct tpm_chip
*chip
= dev_id
;
587 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
591 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
598 priv
->irq_tested
= true;
599 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
600 wake_up_interruptible(&priv
->read_queue
);
601 if (interrupt
& TPM_INTF_LOCALITY_CHANGE_INT
)
602 for (i
= 0; i
< 5; i
++)
603 if (check_locality(chip
, i
))
606 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
607 TPM_INTF_CMD_READY_INT
))
608 wake_up_interruptible(&priv
->int_queue
);
610 /* Clear interrupts handled with TPM_EOI */
611 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), interrupt
);
615 tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
619 static int tpm_tis_gen_interrupt(struct tpm_chip
*chip
)
621 const char *desc
= "attempting to generate an interrupt";
625 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
626 return tpm2_get_tpm_pt(chip
, 0x100, &cap2
, desc
);
628 return tpm_getcap(chip
, TPM_CAP_PROP_TIS_TIMEOUT
, &cap
, desc
,
632 /* Register the IRQ and issue a command that will cause an interrupt. If an
633 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
634 * everything and leave in polling mode. Returns 0 on success.
636 static int tpm_tis_probe_irq_single(struct tpm_chip
*chip
, u32 intmask
,
639 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
644 if (devm_request_irq(chip
->dev
.parent
, irq
, tis_int_handler
, flags
,
645 dev_name(&chip
->dev
), chip
) != 0) {
646 dev_info(&chip
->dev
, "Unable to request irq: %d for probe\n",
652 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
657 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), irq
);
661 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &int_status
);
665 /* Clear all existing */
666 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), int_status
);
671 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
),
672 intmask
| TPM_GLOBAL_INT_ENABLE
);
676 priv
->irq_tested
= false;
678 /* Generate an interrupt by having the core call through to
681 rc
= tpm_tis_gen_interrupt(chip
);
685 /* tpm_tis_send will either confirm the interrupt is working or it
686 * will call disable_irq which undoes all of the above.
688 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
689 rc
= tpm_tis_write8(priv
, original_int_vec
,
690 TPM_INT_VECTOR(priv
->locality
));
700 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
701 * do not have ACPI/etc. We typically expect the interrupt to be declared if
704 static void tpm_tis_probe_irq(struct tpm_chip
*chip
, u32 intmask
)
706 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
710 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
715 if (!original_int_vec
) {
716 if (IS_ENABLED(CONFIG_X86
))
717 for (i
= 3; i
<= 15; i
++)
718 if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
721 } else if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
726 void tpm_tis_remove(struct tpm_chip
*chip
)
728 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
729 u32 reg
= TPM_INT_ENABLE(priv
->locality
);
733 tpm_tis_clkrun_enable(chip
, true);
735 rc
= tpm_tis_read32(priv
, reg
, &interrupt
);
739 tpm_tis_write32(priv
, reg
, ~TPM_GLOBAL_INT_ENABLE
& interrupt
);
741 tpm_tis_clkrun_enable(chip
, false);
743 if (priv
->ilb_base_addr
)
744 iounmap(priv
->ilb_base_addr
);
746 EXPORT_SYMBOL_GPL(tpm_tis_remove
);
749 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
750 * of a single TPM command
751 * @chip: TPM chip to use
752 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
753 * 0 - Enable CLKRUN protocol
754 * Call this function directly in tpm_tis_remove() in error or driver removal
755 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
757 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
)
759 struct tpm_tis_data
*data
= dev_get_drvdata(&chip
->dev
);
762 if (!IS_ENABLED(CONFIG_X86
) || !is_bsw() ||
763 !data
->ilb_base_addr
)
767 data
->clkrun_enabled
++;
768 if (data
->clkrun_enabled
> 1)
770 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
772 /* Disable LPC CLKRUN# */
773 clkrun_val
&= ~LPC_CLKRUN_EN
;
774 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
777 * Write any random value on port 0x80 which is on LPC, to make
778 * sure LPC clock is running before sending any TPM command.
782 data
->clkrun_enabled
--;
783 if (data
->clkrun_enabled
)
786 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
788 /* Enable LPC CLKRUN# */
789 clkrun_val
|= LPC_CLKRUN_EN
;
790 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
793 * Write any random value on port 0x80 which is on LPC, to make
794 * sure LPC clock is running before sending any TPM command.
800 static const struct tpm_class_ops tpm_tis
= {
801 .flags
= TPM_OPS_AUTO_STARTUP
,
802 .status
= tpm_tis_status
,
803 .recv
= tpm_tis_recv
,
804 .send
= tpm_tis_send
,
805 .cancel
= tpm_tis_ready
,
806 .update_timeouts
= tpm_tis_update_timeouts
,
807 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
808 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
809 .req_canceled
= tpm_tis_req_canceled
,
810 .request_locality
= request_locality
,
811 .relinquish_locality
= release_locality
,
812 .clk_enable
= tpm_tis_clkrun_enable
,
815 int tpm_tis_core_init(struct device
*dev
, struct tpm_tis_data
*priv
, int irq
,
816 const struct tpm_tis_phy_ops
*phy_ops
,
817 acpi_handle acpi_dev_handle
)
825 struct tpm_chip
*chip
;
827 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
829 return PTR_ERR(chip
);
832 chip
->acpi_dev_handle
= acpi_dev_handle
;
835 /* Maximum timeouts */
836 chip
->timeout_a
= msecs_to_jiffies(TIS_TIMEOUT_A_MAX
);
837 chip
->timeout_b
= msecs_to_jiffies(TIS_TIMEOUT_B_MAX
);
838 chip
->timeout_c
= msecs_to_jiffies(TIS_TIMEOUT_C_MAX
);
839 chip
->timeout_d
= msecs_to_jiffies(TIS_TIMEOUT_D_MAX
);
840 priv
->phy_ops
= phy_ops
;
841 dev_set_drvdata(&chip
->dev
, priv
);
844 priv
->ilb_base_addr
= ioremap(INTEL_LEGACY_BLK_BASE_ADDR
,
846 if (!priv
->ilb_base_addr
)
849 clkrun_val
= ioread32(priv
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
850 /* Check if CLKRUN# is already not enabled in the LPC bus */
851 if (!(clkrun_val
& LPC_CLKRUN_EN
)) {
852 iounmap(priv
->ilb_base_addr
);
853 priv
->ilb_base_addr
= NULL
;
857 if (chip
->ops
->clk_enable
!= NULL
)
858 chip
->ops
->clk_enable(chip
, true);
860 if (wait_startup(chip
, 0) != 0) {
865 /* Take control of the TPM's interrupt hardware and shut it off */
866 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
870 intmask
|= TPM_INTF_CMD_READY_INT
| TPM_INTF_LOCALITY_CHANGE_INT
|
871 TPM_INTF_DATA_AVAIL_INT
| TPM_INTF_STS_VALID_INT
;
872 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
873 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
875 rc
= tpm2_probe(chip
);
879 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &vendor
);
883 priv
->manufacturer_id
= vendor
;
885 rc
= tpm_tis_read8(priv
, TPM_RID(0), &rid
);
889 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
890 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
893 probe
= probe_itpm(chip
);
899 /* Figure out the capabilities */
900 rc
= tpm_tis_read32(priv
, TPM_INTF_CAPS(priv
->locality
), &intfcaps
);
904 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
906 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
907 dev_dbg(dev
, "\tBurst Count Static\n");
908 if (intfcaps
& TPM_INTF_CMD_READY_INT
)
909 dev_dbg(dev
, "\tCommand Ready Int Support\n");
910 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
911 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
912 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
913 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
914 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
915 dev_dbg(dev
, "\tInterrupt Level Low\n");
916 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
917 dev_dbg(dev
, "\tInterrupt Level High\n");
918 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
)
919 dev_dbg(dev
, "\tLocality Change Int Support\n");
920 if (intfcaps
& TPM_INTF_STS_VALID_INT
)
921 dev_dbg(dev
, "\tSts Valid Int Support\n");
922 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
)
923 dev_dbg(dev
, "\tData Avail Int Support\n");
925 /* INTERRUPT Setup */
926 init_waitqueue_head(&priv
->read_queue
);
927 init_waitqueue_head(&priv
->int_queue
);
929 /* Before doing irq testing issue a command to the TPM in polling mode
930 * to make sure it works. May as well use that command to set the
931 * proper timeouts for the driver.
933 if (tpm_get_timeouts(chip
)) {
934 dev_err(dev
, "Could not get TPM timeouts and durations\n");
940 tpm_tis_probe_irq_single(chip
, intmask
, IRQF_SHARED
,
942 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
))
943 dev_err(&chip
->dev
, FW_BUG
944 "TPM interrupt not working, polling instead\n");
946 tpm_tis_probe_irq(chip
, intmask
);
950 rc
= tpm_chip_register(chip
);
954 if (chip
->ops
->clk_enable
!= NULL
)
955 chip
->ops
->clk_enable(chip
, false);
959 if ((chip
->ops
!= NULL
) && (chip
->ops
->clk_enable
!= NULL
))
960 chip
->ops
->clk_enable(chip
, false);
962 tpm_tis_remove(chip
);
966 EXPORT_SYMBOL_GPL(tpm_tis_core_init
);
968 #ifdef CONFIG_PM_SLEEP
969 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
971 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
975 if (chip
->ops
->clk_enable
!= NULL
)
976 chip
->ops
->clk_enable(chip
, true);
978 /* reenable interrupts that device may have lost or
979 * BIOS/firmware may have disabled
981 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), priv
->irq
);
985 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
989 intmask
|= TPM_INTF_CMD_READY_INT
990 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
991 | TPM_INTF_STS_VALID_INT
| TPM_GLOBAL_INT_ENABLE
;
993 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
996 if (chip
->ops
->clk_enable
!= NULL
)
997 chip
->ops
->clk_enable(chip
, false);
1002 int tpm_tis_resume(struct device
*dev
)
1004 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1007 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
)
1008 tpm_tis_reenable_interrupts(chip
);
1010 ret
= tpm_pm_resume(dev
);
1014 /* TPM 1.2 requires self-test on resume. This function actually returns
1015 * an error code but for unknown reason it isn't handled.
1017 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
1018 tpm_do_selftest(chip
);
1022 EXPORT_SYMBOL_GPL(tpm_tis_resume
);
1025 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1026 MODULE_DESCRIPTION("TPM Driver");
1027 MODULE_VERSION("2.0");
1028 MODULE_LICENSE("GPL");