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 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
);
36 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
37 bool check_cancel
, bool *canceled
)
39 u8 status
= chip
->ops
->status(chip
);
42 if ((status
& mask
) == mask
)
44 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
51 static int wait_for_tpm_stat(struct tpm_chip
*chip
, u8 mask
,
52 unsigned long timeout
, wait_queue_head_t
*queue
,
58 bool canceled
= false;
60 /* check current status */
61 status
= chip
->ops
->status(chip
);
62 if ((status
& mask
) == mask
)
65 stop
= jiffies
+ timeout
;
67 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
69 timeout
= stop
- jiffies
;
70 if ((long)timeout
<= 0)
72 rc
= wait_event_interruptible_timeout(*queue
,
73 wait_for_tpm_stat_cond(chip
, mask
, check_cancel
,
81 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
82 clear_thread_flag(TIF_SIGPENDING
);
87 usleep_range(TPM_TIMEOUT_USECS_MIN
,
88 TPM_TIMEOUT_USECS_MAX
);
89 status
= chip
->ops
->status(chip
);
90 if ((status
& mask
) == mask
)
92 } while (time_before(jiffies
, stop
));
97 /* Before we attempt to access the TPM we must see that the valid bit is set.
98 * The specification says that this bit is 0 at reset and remains 0 until the
99 * 'TPM has gone through its self test and initialization and has established
100 * correct values in the other bits.'
102 static int wait_startup(struct tpm_chip
*chip
, int l
)
104 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
105 unsigned long stop
= jiffies
+ chip
->timeout_a
;
111 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
115 if (access
& TPM_ACCESS_VALID
)
117 tpm_msleep(TPM_TIMEOUT
);
118 } while (time_before(jiffies
, stop
));
122 static bool check_locality(struct tpm_chip
*chip
, int l
)
124 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
128 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
132 if ((access
& (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
133 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) {
141 static bool locality_inactive(struct tpm_chip
*chip
, int l
)
143 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
147 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
151 if ((access
& (TPM_ACCESS_VALID
| TPM_ACCESS_ACTIVE_LOCALITY
))
158 static int release_locality(struct tpm_chip
*chip
, int l
)
160 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
161 unsigned long stop
, timeout
;
164 tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_ACTIVE_LOCALITY
);
166 stop
= jiffies
+ chip
->timeout_a
;
168 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
170 timeout
= stop
- jiffies
;
171 if ((long)timeout
<= 0)
174 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
175 (locality_inactive(chip
, l
)),
181 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
182 clear_thread_flag(TIF_SIGPENDING
);
187 if (locality_inactive(chip
, l
))
189 tpm_msleep(TPM_TIMEOUT
);
190 } while (time_before(jiffies
, stop
));
195 static int request_locality(struct tpm_chip
*chip
, int l
)
197 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
198 unsigned long stop
, timeout
;
201 if (check_locality(chip
, l
))
204 rc
= tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_REQUEST_USE
);
208 stop
= jiffies
+ chip
->timeout_a
;
210 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
212 timeout
= stop
- jiffies
;
213 if ((long)timeout
<= 0)
215 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
221 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
222 clear_thread_flag(TIF_SIGPENDING
);
226 /* wait for burstcount */
228 if (check_locality(chip
, l
))
230 tpm_msleep(TPM_TIMEOUT
);
231 } while (time_before(jiffies
, stop
));
236 static u8
tpm_tis_status(struct tpm_chip
*chip
)
238 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
242 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
), &status
);
249 static void tpm_tis_ready(struct tpm_chip
*chip
)
251 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
253 /* this causes the current command to be aborted */
254 tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_COMMAND_READY
);
257 static int get_burstcount(struct tpm_chip
*chip
)
259 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
264 /* wait for burstcount */
265 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
266 stop
= jiffies
+ chip
->timeout_a
;
268 stop
= jiffies
+ chip
->timeout_d
;
270 rc
= tpm_tis_read32(priv
, TPM_STS(priv
->locality
), &value
);
274 burstcnt
= (value
>> 8) & 0xFFFF;
277 usleep_range(TPM_TIMEOUT_USECS_MIN
, TPM_TIMEOUT_USECS_MAX
);
278 } while (time_before(jiffies
, stop
));
282 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
284 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
285 int size
= 0, burstcnt
, rc
;
287 while (size
< count
) {
288 rc
= wait_for_tpm_stat(chip
,
289 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
291 &priv
->read_queue
, true);
294 burstcnt
= get_burstcount(chip
);
296 dev_err(&chip
->dev
, "Unable to read burstcount\n");
299 burstcnt
= min_t(int, burstcnt
, count
- size
);
301 rc
= tpm_tis_read_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
302 burstcnt
, buf
+ size
);
311 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
313 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
318 if (count
< TPM_HEADER_SIZE
) {
323 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
324 /* read first 10 bytes, including tag, paramsize, and result */
325 if (size
< TPM_HEADER_SIZE
) {
326 dev_err(&chip
->dev
, "Unable to read header\n");
330 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
331 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
336 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
337 expected
- TPM_HEADER_SIZE
);
338 if (size
< expected
) {
339 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
344 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
345 &priv
->int_queue
, false) < 0) {
349 status
= tpm_tis_status(chip
);
350 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
351 dev_err(&chip
->dev
, "Error left over data\n");
362 * If interrupts are used (signaled by an irq set in the vendor structure)
363 * tpm.c can skip polling for the data to be available as the interrupt is
366 static int tpm_tis_send_data(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
368 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
369 int rc
, status
, burstcnt
;
371 bool itpm
= priv
->flags
& TPM_TIS_ITPM_WORKAROUND
;
373 status
= tpm_tis_status(chip
);
374 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
376 if (wait_for_tpm_stat
377 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
378 &priv
->int_queue
, false) < 0) {
384 while (count
< len
- 1) {
385 burstcnt
= get_burstcount(chip
);
387 dev_err(&chip
->dev
, "Unable to read burstcount\n");
391 burstcnt
= min_t(int, burstcnt
, len
- count
- 1);
392 rc
= tpm_tis_write_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
393 burstcnt
, buf
+ count
);
399 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
400 &priv
->int_queue
, false) < 0) {
404 status
= tpm_tis_status(chip
);
405 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) == 0) {
411 /* write last byte */
412 rc
= tpm_tis_write8(priv
, TPM_DATA_FIFO(priv
->locality
), buf
[count
]);
416 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
417 &priv
->int_queue
, false) < 0) {
421 status
= tpm_tis_status(chip
);
422 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) != 0) {
434 static void disable_interrupts(struct tpm_chip
*chip
)
436 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
440 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
444 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
445 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
447 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
449 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
453 * If interrupts are used (signaled by an irq set in the vendor structure)
454 * tpm.c can skip polling for the data to be available as the interrupt is
457 static int tpm_tis_send_main(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
459 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
464 rc
= tpm_tis_send_data(chip
, buf
, len
);
469 rc
= tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_GO
);
473 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
474 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
476 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
477 dur
= tpm2_calc_ordinal_duration(chip
, ordinal
);
479 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
481 if (wait_for_tpm_stat
482 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
483 &priv
->read_queue
, false) < 0) {
494 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
497 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
499 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
) || priv
->irq_tested
)
500 return tpm_tis_send_main(chip
, buf
, len
);
502 /* Verify receipt of the expected IRQ */
505 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
506 rc
= tpm_tis_send_main(chip
, buf
, len
);
508 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
509 if (!priv
->irq_tested
)
511 if (!priv
->irq_tested
)
512 disable_interrupts(chip
);
513 priv
->irq_tested
= true;
517 struct tis_vendor_timeout_override
{
519 unsigned long timeout_us
[4];
522 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
524 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
525 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
528 static bool tpm_tis_update_timeouts(struct tpm_chip
*chip
,
529 unsigned long *timeout_cap
)
531 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
535 if (chip
->ops
->clk_enable
!= NULL
)
536 chip
->ops
->clk_enable(chip
, true);
538 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
542 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
543 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
545 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
546 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
553 if (chip
->ops
->clk_enable
!= NULL
)
554 chip
->ops
->clk_enable(chip
, false);
560 * Early probing for iTPM with STS_DATA_EXPECT flaw.
561 * Try sending command without itpm flag set and if that
562 * fails, repeat with itpm flag set.
564 static int probe_itpm(struct tpm_chip
*chip
)
566 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
568 static const u8 cmd_getticks
[] = {
569 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
570 0x00, 0x00, 0x00, 0xf1
572 size_t len
= sizeof(cmd_getticks
);
575 if (priv
->flags
& TPM_TIS_ITPM_WORKAROUND
)
578 rc
= tpm_tis_read16(priv
, TPM_DID_VID(0), &vendor
);
582 /* probe only iTPMS */
583 if (vendor
!= TPM_VID_INTEL
)
586 if (request_locality(chip
, 0) != 0)
589 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
595 priv
->flags
|= TPM_TIS_ITPM_WORKAROUND
;
597 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
599 dev_info(&chip
->dev
, "Detected an iTPM.\n");
601 priv
->flags
&= ~TPM_TIS_ITPM_WORKAROUND
;
607 release_locality(chip
, priv
->locality
);
612 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
614 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
616 switch (priv
->manufacturer_id
) {
617 case TPM_VID_WINBOND
:
618 return ((status
== TPM_STS_VALID
) ||
619 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
621 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
623 return (status
== TPM_STS_COMMAND_READY
);
627 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
629 struct tpm_chip
*chip
= dev_id
;
630 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
634 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
641 priv
->irq_tested
= true;
642 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
643 wake_up_interruptible(&priv
->read_queue
);
644 if (interrupt
& TPM_INTF_LOCALITY_CHANGE_INT
)
645 for (i
= 0; i
< 5; i
++)
646 if (check_locality(chip
, i
))
649 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
650 TPM_INTF_CMD_READY_INT
))
651 wake_up_interruptible(&priv
->int_queue
);
653 /* Clear interrupts handled with TPM_EOI */
654 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), interrupt
);
658 tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
662 static int tpm_tis_gen_interrupt(struct tpm_chip
*chip
)
664 const char *desc
= "attempting to generate an interrupt";
668 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
669 return tpm2_get_tpm_pt(chip
, 0x100, &cap2
, desc
);
671 return tpm_getcap(chip
, TPM_CAP_PROP_TIS_TIMEOUT
, &cap
, desc
,
675 /* Register the IRQ and issue a command that will cause an interrupt. If an
676 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
677 * everything and leave in polling mode. Returns 0 on success.
679 static int tpm_tis_probe_irq_single(struct tpm_chip
*chip
, u32 intmask
,
682 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
687 if (devm_request_irq(chip
->dev
.parent
, irq
, tis_int_handler
, flags
,
688 dev_name(&chip
->dev
), chip
) != 0) {
689 dev_info(&chip
->dev
, "Unable to request irq: %d for probe\n",
695 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
700 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), irq
);
704 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &int_status
);
708 /* Clear all existing */
709 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), int_status
);
714 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
),
715 intmask
| TPM_GLOBAL_INT_ENABLE
);
719 priv
->irq_tested
= false;
721 /* Generate an interrupt by having the core call through to
724 rc
= tpm_tis_gen_interrupt(chip
);
728 /* tpm_tis_send will either confirm the interrupt is working or it
729 * will call disable_irq which undoes all of the above.
731 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
732 rc
= tpm_tis_write8(priv
, original_int_vec
,
733 TPM_INT_VECTOR(priv
->locality
));
743 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
744 * do not have ACPI/etc. We typically expect the interrupt to be declared if
747 static void tpm_tis_probe_irq(struct tpm_chip
*chip
, u32 intmask
)
749 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
753 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
758 if (!original_int_vec
) {
759 if (IS_ENABLED(CONFIG_X86
))
760 for (i
= 3; i
<= 15; i
++)
761 if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
764 } else if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
769 void tpm_tis_remove(struct tpm_chip
*chip
)
771 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
772 u32 reg
= TPM_INT_ENABLE(priv
->locality
);
776 tpm_tis_clkrun_enable(chip
, true);
778 rc
= tpm_tis_read32(priv
, reg
, &interrupt
);
782 tpm_tis_write32(priv
, reg
, ~TPM_GLOBAL_INT_ENABLE
& interrupt
);
784 tpm_tis_clkrun_enable(chip
, false);
786 if (priv
->ilb_base_addr
)
787 iounmap(priv
->ilb_base_addr
);
789 EXPORT_SYMBOL_GPL(tpm_tis_remove
);
792 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
793 * of a single TPM command
794 * @chip: TPM chip to use
795 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
796 * 0 - Enable CLKRUN protocol
797 * Call this function directly in tpm_tis_remove() in error or driver removal
798 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
800 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
)
802 struct tpm_tis_data
*data
= dev_get_drvdata(&chip
->dev
);
805 if (!IS_ENABLED(CONFIG_X86
) || !is_bsw() ||
806 !data
->ilb_base_addr
)
810 data
->clkrun_enabled
++;
811 if (data
->clkrun_enabled
> 1)
813 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
815 /* Disable LPC CLKRUN# */
816 clkrun_val
&= ~LPC_CLKRUN_EN
;
817 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
820 * Write any random value on port 0x80 which is on LPC, to make
821 * sure LPC clock is running before sending any TPM command.
825 data
->clkrun_enabled
--;
826 if (data
->clkrun_enabled
)
829 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
831 /* Enable LPC CLKRUN# */
832 clkrun_val
|= LPC_CLKRUN_EN
;
833 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
836 * Write any random value on port 0x80 which is on LPC, to make
837 * sure LPC clock is running before sending any TPM command.
843 static const struct tpm_class_ops tpm_tis
= {
844 .flags
= TPM_OPS_AUTO_STARTUP
,
845 .status
= tpm_tis_status
,
846 .recv
= tpm_tis_recv
,
847 .send
= tpm_tis_send
,
848 .cancel
= tpm_tis_ready
,
849 .update_timeouts
= tpm_tis_update_timeouts
,
850 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
851 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
852 .req_canceled
= tpm_tis_req_canceled
,
853 .request_locality
= request_locality
,
854 .relinquish_locality
= release_locality
,
855 .clk_enable
= tpm_tis_clkrun_enable
,
858 int tpm_tis_core_init(struct device
*dev
, struct tpm_tis_data
*priv
, int irq
,
859 const struct tpm_tis_phy_ops
*phy_ops
,
860 acpi_handle acpi_dev_handle
)
868 struct tpm_chip
*chip
;
870 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
872 return PTR_ERR(chip
);
875 chip
->acpi_dev_handle
= acpi_dev_handle
;
878 /* Maximum timeouts */
879 chip
->timeout_a
= msecs_to_jiffies(TIS_TIMEOUT_A_MAX
);
880 chip
->timeout_b
= msecs_to_jiffies(TIS_TIMEOUT_B_MAX
);
881 chip
->timeout_c
= msecs_to_jiffies(TIS_TIMEOUT_C_MAX
);
882 chip
->timeout_d
= msecs_to_jiffies(TIS_TIMEOUT_D_MAX
);
883 priv
->phy_ops
= phy_ops
;
884 dev_set_drvdata(&chip
->dev
, priv
);
887 priv
->ilb_base_addr
= ioremap(INTEL_LEGACY_BLK_BASE_ADDR
,
889 if (!priv
->ilb_base_addr
)
892 clkrun_val
= ioread32(priv
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
893 /* Check if CLKRUN# is already not enabled in the LPC bus */
894 if (!(clkrun_val
& LPC_CLKRUN_EN
)) {
895 iounmap(priv
->ilb_base_addr
);
896 priv
->ilb_base_addr
= NULL
;
900 if (chip
->ops
->clk_enable
!= NULL
)
901 chip
->ops
->clk_enable(chip
, true);
903 if (wait_startup(chip
, 0) != 0) {
908 /* Take control of the TPM's interrupt hardware and shut it off */
909 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
913 intmask
|= TPM_INTF_CMD_READY_INT
| TPM_INTF_LOCALITY_CHANGE_INT
|
914 TPM_INTF_DATA_AVAIL_INT
| TPM_INTF_STS_VALID_INT
;
915 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
916 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
918 rc
= tpm2_probe(chip
);
922 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &vendor
);
926 priv
->manufacturer_id
= vendor
;
928 rc
= tpm_tis_read8(priv
, TPM_RID(0), &rid
);
932 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
933 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
936 probe
= probe_itpm(chip
);
942 /* Figure out the capabilities */
943 rc
= tpm_tis_read32(priv
, TPM_INTF_CAPS(priv
->locality
), &intfcaps
);
947 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
949 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
950 dev_dbg(dev
, "\tBurst Count Static\n");
951 if (intfcaps
& TPM_INTF_CMD_READY_INT
)
952 dev_dbg(dev
, "\tCommand Ready Int Support\n");
953 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
954 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
955 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
956 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
957 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
958 dev_dbg(dev
, "\tInterrupt Level Low\n");
959 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
960 dev_dbg(dev
, "\tInterrupt Level High\n");
961 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
)
962 dev_dbg(dev
, "\tLocality Change Int Support\n");
963 if (intfcaps
& TPM_INTF_STS_VALID_INT
)
964 dev_dbg(dev
, "\tSts Valid Int Support\n");
965 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
)
966 dev_dbg(dev
, "\tData Avail Int Support\n");
968 /* INTERRUPT Setup */
969 init_waitqueue_head(&priv
->read_queue
);
970 init_waitqueue_head(&priv
->int_queue
);
972 /* Before doing irq testing issue a command to the TPM in polling mode
973 * to make sure it works. May as well use that command to set the
974 * proper timeouts for the driver.
976 if (tpm_get_timeouts(chip
)) {
977 dev_err(dev
, "Could not get TPM timeouts and durations\n");
983 tpm_tis_probe_irq_single(chip
, intmask
, IRQF_SHARED
,
985 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
))
986 dev_err(&chip
->dev
, FW_BUG
987 "TPM interrupt not working, polling instead\n");
989 tpm_tis_probe_irq(chip
, intmask
);
993 rc
= tpm_chip_register(chip
);
997 if (chip
->ops
->clk_enable
!= NULL
)
998 chip
->ops
->clk_enable(chip
, false);
1002 if ((chip
->ops
!= NULL
) && (chip
->ops
->clk_enable
!= NULL
))
1003 chip
->ops
->clk_enable(chip
, false);
1005 tpm_tis_remove(chip
);
1009 EXPORT_SYMBOL_GPL(tpm_tis_core_init
);
1011 #ifdef CONFIG_PM_SLEEP
1012 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
1014 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
1018 if (chip
->ops
->clk_enable
!= NULL
)
1019 chip
->ops
->clk_enable(chip
, true);
1021 /* reenable interrupts that device may have lost or
1022 * BIOS/firmware may have disabled
1024 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), priv
->irq
);
1028 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
1032 intmask
|= TPM_INTF_CMD_READY_INT
1033 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
1034 | TPM_INTF_STS_VALID_INT
| TPM_GLOBAL_INT_ENABLE
;
1036 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1039 if (chip
->ops
->clk_enable
!= NULL
)
1040 chip
->ops
->clk_enable(chip
, false);
1045 int tpm_tis_resume(struct device
*dev
)
1047 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1050 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
)
1051 tpm_tis_reenable_interrupts(chip
);
1053 ret
= tpm_pm_resume(dev
);
1057 /* TPM 1.2 requires self-test on resume. This function actually returns
1058 * an error code but for unknown reason it isn't handled.
1060 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
1061 tpm_do_selftest(chip
);
1065 EXPORT_SYMBOL_GPL(tpm_tis_resume
);
1068 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1069 MODULE_DESCRIPTION("TPM Driver");
1070 MODULE_VERSION("2.0");
1071 MODULE_LICENSE("GPL");