1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005, 2006 IBM Corporation
4 * Copyright (C) 2014, 2015 Intel Corporation
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This device driver implements the TPM interface as defined in
16 * the TCG TPM Interface Spec version 1.2, revision 1.0.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/pnp.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/wait.h>
25 #include <linux/acpi.h>
26 #include <linux/freezer.h>
28 #include "tpm_tis_core.h"
30 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
);
32 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
33 bool check_cancel
, bool *canceled
)
35 u8 status
= chip
->ops
->status(chip
);
38 if ((status
& mask
) == mask
)
40 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
47 static int wait_for_tpm_stat(struct tpm_chip
*chip
, u8 mask
,
48 unsigned long timeout
, wait_queue_head_t
*queue
,
54 bool canceled
= false;
56 /* check current status */
57 status
= chip
->ops
->status(chip
);
58 if ((status
& mask
) == mask
)
61 stop
= jiffies
+ timeout
;
63 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
65 timeout
= stop
- jiffies
;
66 if ((long)timeout
<= 0)
68 rc
= wait_event_interruptible_timeout(*queue
,
69 wait_for_tpm_stat_cond(chip
, mask
, check_cancel
,
77 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
78 clear_thread_flag(TIF_SIGPENDING
);
83 usleep_range(TPM_TIMEOUT_USECS_MIN
,
84 TPM_TIMEOUT_USECS_MAX
);
85 status
= chip
->ops
->status(chip
);
86 if ((status
& mask
) == mask
)
88 } while (time_before(jiffies
, stop
));
93 /* Before we attempt to access the TPM we must see that the valid bit is set.
94 * The specification says that this bit is 0 at reset and remains 0 until the
95 * 'TPM has gone through its self test and initialization and has established
96 * correct values in the other bits.'
98 static int wait_startup(struct tpm_chip
*chip
, int l
)
100 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
101 unsigned long stop
= jiffies
+ chip
->timeout_a
;
107 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
111 if (access
& TPM_ACCESS_VALID
)
113 tpm_msleep(TPM_TIMEOUT
);
114 } while (time_before(jiffies
, stop
));
118 static bool check_locality(struct tpm_chip
*chip
, int l
)
120 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
124 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
128 if ((access
& (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
129 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) {
137 static bool locality_inactive(struct tpm_chip
*chip
, int l
)
139 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
143 rc
= tpm_tis_read8(priv
, TPM_ACCESS(l
), &access
);
147 if ((access
& (TPM_ACCESS_VALID
| TPM_ACCESS_ACTIVE_LOCALITY
))
154 static int release_locality(struct tpm_chip
*chip
, int l
)
156 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
157 unsigned long stop
, timeout
;
160 tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_ACTIVE_LOCALITY
);
162 stop
= jiffies
+ chip
->timeout_a
;
164 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
166 timeout
= stop
- jiffies
;
167 if ((long)timeout
<= 0)
170 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
171 (locality_inactive(chip
, l
)),
177 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
178 clear_thread_flag(TIF_SIGPENDING
);
183 if (locality_inactive(chip
, l
))
185 tpm_msleep(TPM_TIMEOUT
);
186 } while (time_before(jiffies
, stop
));
191 static int request_locality(struct tpm_chip
*chip
, int l
)
193 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
194 unsigned long stop
, timeout
;
197 if (check_locality(chip
, l
))
200 rc
= tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_REQUEST_USE
);
204 stop
= jiffies
+ chip
->timeout_a
;
206 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
208 timeout
= stop
- jiffies
;
209 if ((long)timeout
<= 0)
211 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
217 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
218 clear_thread_flag(TIF_SIGPENDING
);
222 /* wait for burstcount */
224 if (check_locality(chip
, l
))
226 tpm_msleep(TPM_TIMEOUT
);
227 } while (time_before(jiffies
, stop
));
232 static u8
tpm_tis_status(struct tpm_chip
*chip
)
234 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
238 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
), &status
);
242 if (unlikely((status
& TPM_STS_READ_ZERO
) != 0)) {
244 * If this trips, the chances are the read is
245 * returning 0xff because the locality hasn't been
246 * acquired. Usually because tpm_try_get_ops() hasn't
247 * been called before doing a TPM operation.
249 WARN_ONCE(1, "TPM returned invalid status\n");
256 static void tpm_tis_ready(struct tpm_chip
*chip
)
258 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
260 /* this causes the current command to be aborted */
261 tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_COMMAND_READY
);
264 static int get_burstcount(struct tpm_chip
*chip
)
266 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
271 /* wait for burstcount */
272 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
273 stop
= jiffies
+ chip
->timeout_a
;
275 stop
= jiffies
+ chip
->timeout_d
;
277 rc
= tpm_tis_read32(priv
, TPM_STS(priv
->locality
), &value
);
281 burstcnt
= (value
>> 8) & 0xFFFF;
284 usleep_range(TPM_TIMEOUT_USECS_MIN
, TPM_TIMEOUT_USECS_MAX
);
285 } while (time_before(jiffies
, stop
));
289 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
291 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
292 int size
= 0, burstcnt
, rc
;
294 while (size
< count
) {
295 rc
= wait_for_tpm_stat(chip
,
296 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
298 &priv
->read_queue
, true);
301 burstcnt
= get_burstcount(chip
);
303 dev_err(&chip
->dev
, "Unable to read burstcount\n");
306 burstcnt
= min_t(int, burstcnt
, count
- size
);
308 rc
= tpm_tis_read_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
309 burstcnt
, buf
+ size
);
318 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
320 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
325 if (count
< TPM_HEADER_SIZE
) {
330 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
331 /* read first 10 bytes, including tag, paramsize, and result */
332 if (size
< TPM_HEADER_SIZE
) {
333 dev_err(&chip
->dev
, "Unable to read header\n");
337 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
338 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
343 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
344 expected
- TPM_HEADER_SIZE
);
345 if (size
< expected
) {
346 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
351 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
352 &priv
->int_queue
, false) < 0) {
356 status
= tpm_tis_status(chip
);
357 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
358 dev_err(&chip
->dev
, "Error left over data\n");
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_data(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
375 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
376 int rc
, status
, burstcnt
;
378 bool itpm
= priv
->flags
& TPM_TIS_ITPM_WORKAROUND
;
380 status
= tpm_tis_status(chip
);
381 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
383 if (wait_for_tpm_stat
384 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
385 &priv
->int_queue
, false) < 0) {
391 while (count
< len
- 1) {
392 burstcnt
= get_burstcount(chip
);
394 dev_err(&chip
->dev
, "Unable to read burstcount\n");
398 burstcnt
= min_t(int, burstcnt
, len
- count
- 1);
399 rc
= tpm_tis_write_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
400 burstcnt
, buf
+ count
);
406 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
407 &priv
->int_queue
, false) < 0) {
411 status
= tpm_tis_status(chip
);
412 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) == 0) {
418 /* write last byte */
419 rc
= tpm_tis_write8(priv
, TPM_DATA_FIFO(priv
->locality
), buf
[count
]);
423 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
424 &priv
->int_queue
, false) < 0) {
428 status
= tpm_tis_status(chip
);
429 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) != 0) {
441 static void disable_interrupts(struct tpm_chip
*chip
)
443 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
450 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
454 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
455 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
457 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
459 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
463 * If interrupts are used (signaled by an irq set in the vendor structure)
464 * tpm.c can skip polling for the data to be available as the interrupt is
467 static int tpm_tis_send_main(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
469 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
474 rc
= tpm_tis_send_data(chip
, buf
, len
);
479 rc
= tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_GO
);
483 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
484 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
486 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
487 if (wait_for_tpm_stat
488 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
489 &priv
->read_queue
, false) < 0) {
500 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
503 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
505 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
) || priv
->irq_tested
)
506 return tpm_tis_send_main(chip
, buf
, len
);
508 /* Verify receipt of the expected IRQ */
511 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
512 rc
= tpm_tis_send_main(chip
, buf
, len
);
514 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
515 if (!priv
->irq_tested
)
517 if (!priv
->irq_tested
)
518 disable_interrupts(chip
);
519 priv
->irq_tested
= true;
523 struct tis_vendor_durations_override
{
525 struct tpm1_version version
;
526 unsigned long durations
[3];
529 static const struct tis_vendor_durations_override vendor_dur_overrides
[] = {
530 /* STMicroelectronics 0x104a */
533 { (2 * 60 * HZ
), (2 * 60 * HZ
), (2 * 60 * HZ
) } },
536 static void tpm_tis_update_durations(struct tpm_chip
*chip
,
537 unsigned long *duration_cap
)
539 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
540 struct tpm1_version
*version
;
545 chip
->duration_adjusted
= false;
547 if (chip
->ops
->clk_enable
!= NULL
)
548 chip
->ops
->clk_enable(chip
, true);
550 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
552 dev_warn(&chip
->dev
, "%s: failed to read did_vid. %d\n",
557 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
558 rc
= tpm1_getcap(chip
, TPM_CAP_VERSION_1_2
, &cap
,
559 "attempting to determine the 1.2 version",
560 sizeof(cap
.version2
));
562 version
= &cap
.version2
.version
;
564 rc
= tpm1_getcap(chip
, TPM_CAP_VERSION_1_1
, &cap
,
565 "attempting to determine the 1.1 version",
566 sizeof(cap
.version1
));
571 version
= &cap
.version1
;
574 for (i
= 0; i
!= ARRAY_SIZE(vendor_dur_overrides
); i
++) {
575 if (vendor_dur_overrides
[i
].did_vid
!= did_vid
)
578 if ((version
->major
==
579 vendor_dur_overrides
[i
].version
.major
) &&
581 vendor_dur_overrides
[i
].version
.minor
) &&
582 (version
->rev_major
==
583 vendor_dur_overrides
[i
].version
.rev_major
) &&
584 (version
->rev_minor
==
585 vendor_dur_overrides
[i
].version
.rev_minor
)) {
588 vendor_dur_overrides
[i
].durations
,
589 sizeof(vendor_dur_overrides
[i
].durations
));
591 chip
->duration_adjusted
= true;
597 if (chip
->ops
->clk_enable
!= NULL
)
598 chip
->ops
->clk_enable(chip
, false);
601 struct tis_vendor_timeout_override
{
603 unsigned long timeout_us
[4];
606 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
608 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
609 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
612 static void tpm_tis_update_timeouts(struct tpm_chip
*chip
,
613 unsigned long *timeout_cap
)
615 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
619 chip
->timeout_adjusted
= false;
621 if (chip
->ops
->clk_enable
!= NULL
)
622 chip
->ops
->clk_enable(chip
, true);
624 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
626 dev_warn(&chip
->dev
, "%s: failed to read did_vid: %d\n",
631 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
632 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
634 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
635 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
636 chip
->timeout_adjusted
= true;
640 if (chip
->ops
->clk_enable
!= NULL
)
641 chip
->ops
->clk_enable(chip
, false);
647 * Early probing for iTPM with STS_DATA_EXPECT flaw.
648 * Try sending command without itpm flag set and if that
649 * fails, repeat with itpm flag set.
651 static int probe_itpm(struct tpm_chip
*chip
)
653 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
655 static const u8 cmd_getticks
[] = {
656 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
657 0x00, 0x00, 0x00, 0xf1
659 size_t len
= sizeof(cmd_getticks
);
662 if (priv
->flags
& TPM_TIS_ITPM_WORKAROUND
)
665 rc
= tpm_tis_read16(priv
, TPM_DID_VID(0), &vendor
);
669 /* probe only iTPMS */
670 if (vendor
!= TPM_VID_INTEL
)
673 if (request_locality(chip
, 0) != 0)
676 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
682 priv
->flags
|= TPM_TIS_ITPM_WORKAROUND
;
684 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
686 dev_info(&chip
->dev
, "Detected an iTPM.\n");
688 priv
->flags
&= ~TPM_TIS_ITPM_WORKAROUND
;
694 release_locality(chip
, priv
->locality
);
699 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
701 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
703 switch (priv
->manufacturer_id
) {
704 case TPM_VID_WINBOND
:
705 return ((status
== TPM_STS_VALID
) ||
706 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
708 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
710 return (status
== TPM_STS_COMMAND_READY
);
714 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
716 struct tpm_chip
*chip
= dev_id
;
717 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
721 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
728 priv
->irq_tested
= true;
729 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
730 wake_up_interruptible(&priv
->read_queue
);
731 if (interrupt
& TPM_INTF_LOCALITY_CHANGE_INT
)
732 for (i
= 0; i
< 5; i
++)
733 if (check_locality(chip
, i
))
736 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
737 TPM_INTF_CMD_READY_INT
))
738 wake_up_interruptible(&priv
->int_queue
);
740 /* Clear interrupts handled with TPM_EOI */
741 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), interrupt
);
745 tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
749 static int tpm_tis_gen_interrupt(struct tpm_chip
*chip
)
751 const char *desc
= "attempting to generate an interrupt";
755 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
756 return tpm2_get_tpm_pt(chip
, 0x100, &cap2
, desc
);
758 return tpm1_getcap(chip
, TPM_CAP_PROP_TIS_TIMEOUT
, &cap
, desc
,
762 /* Register the IRQ and issue a command that will cause an interrupt. If an
763 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
764 * everything and leave in polling mode. Returns 0 on success.
766 static int tpm_tis_probe_irq_single(struct tpm_chip
*chip
, u32 intmask
,
769 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
774 if (devm_request_irq(chip
->dev
.parent
, irq
, tis_int_handler
, flags
,
775 dev_name(&chip
->dev
), chip
) != 0) {
776 dev_info(&chip
->dev
, "Unable to request irq: %d for probe\n",
782 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
787 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), irq
);
791 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &int_status
);
795 /* Clear all existing */
796 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), int_status
);
801 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
),
802 intmask
| TPM_GLOBAL_INT_ENABLE
);
806 priv
->irq_tested
= false;
808 /* Generate an interrupt by having the core call through to
811 rc
= tpm_tis_gen_interrupt(chip
);
815 /* tpm_tis_send will either confirm the interrupt is working or it
816 * will call disable_irq which undoes all of the above.
818 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
819 rc
= tpm_tis_write8(priv
, original_int_vec
,
820 TPM_INT_VECTOR(priv
->locality
));
830 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
831 * do not have ACPI/etc. We typically expect the interrupt to be declared if
834 static void tpm_tis_probe_irq(struct tpm_chip
*chip
, u32 intmask
)
836 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
840 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
845 if (!original_int_vec
) {
846 if (IS_ENABLED(CONFIG_X86
))
847 for (i
= 3; i
<= 15; i
++)
848 if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
851 } else if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
856 void tpm_tis_remove(struct tpm_chip
*chip
)
858 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
859 u32 reg
= TPM_INT_ENABLE(priv
->locality
);
863 tpm_tis_clkrun_enable(chip
, true);
865 rc
= tpm_tis_read32(priv
, reg
, &interrupt
);
869 tpm_tis_write32(priv
, reg
, ~TPM_GLOBAL_INT_ENABLE
& interrupt
);
871 tpm_tis_clkrun_enable(chip
, false);
873 if (priv
->ilb_base_addr
)
874 iounmap(priv
->ilb_base_addr
);
876 EXPORT_SYMBOL_GPL(tpm_tis_remove
);
879 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
880 * of a single TPM command
881 * @chip: TPM chip to use
882 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
883 * 0 - Enable CLKRUN protocol
884 * Call this function directly in tpm_tis_remove() in error or driver removal
885 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
887 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
)
889 struct tpm_tis_data
*data
= dev_get_drvdata(&chip
->dev
);
892 if (!IS_ENABLED(CONFIG_X86
) || !is_bsw() ||
893 !data
->ilb_base_addr
)
897 data
->clkrun_enabled
++;
898 if (data
->clkrun_enabled
> 1)
900 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
902 /* Disable LPC CLKRUN# */
903 clkrun_val
&= ~LPC_CLKRUN_EN
;
904 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
907 * Write any random value on port 0x80 which is on LPC, to make
908 * sure LPC clock is running before sending any TPM command.
912 data
->clkrun_enabled
--;
913 if (data
->clkrun_enabled
)
916 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
918 /* Enable LPC CLKRUN# */
919 clkrun_val
|= LPC_CLKRUN_EN
;
920 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
923 * Write any random value on port 0x80 which is on LPC, to make
924 * sure LPC clock is running before sending any TPM command.
930 static const struct tpm_class_ops tpm_tis
= {
931 .flags
= TPM_OPS_AUTO_STARTUP
,
932 .status
= tpm_tis_status
,
933 .recv
= tpm_tis_recv
,
934 .send
= tpm_tis_send
,
935 .cancel
= tpm_tis_ready
,
936 .update_timeouts
= tpm_tis_update_timeouts
,
937 .update_durations
= tpm_tis_update_durations
,
938 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
939 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
940 .req_canceled
= tpm_tis_req_canceled
,
941 .request_locality
= request_locality
,
942 .relinquish_locality
= release_locality
,
943 .clk_enable
= tpm_tis_clkrun_enable
,
946 int tpm_tis_core_init(struct device
*dev
, struct tpm_tis_data
*priv
, int irq
,
947 const struct tpm_tis_phy_ops
*phy_ops
,
948 acpi_handle acpi_dev_handle
)
956 struct tpm_chip
*chip
;
958 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
960 return PTR_ERR(chip
);
963 chip
->acpi_dev_handle
= acpi_dev_handle
;
966 chip
->hwrng
.quality
= priv
->rng_quality
;
968 /* Maximum timeouts */
969 chip
->timeout_a
= msecs_to_jiffies(TIS_TIMEOUT_A_MAX
);
970 chip
->timeout_b
= msecs_to_jiffies(TIS_TIMEOUT_B_MAX
);
971 chip
->timeout_c
= msecs_to_jiffies(TIS_TIMEOUT_C_MAX
);
972 chip
->timeout_d
= msecs_to_jiffies(TIS_TIMEOUT_D_MAX
);
973 priv
->phy_ops
= phy_ops
;
974 dev_set_drvdata(&chip
->dev
, priv
);
977 priv
->ilb_base_addr
= ioremap(INTEL_LEGACY_BLK_BASE_ADDR
,
979 if (!priv
->ilb_base_addr
)
982 clkrun_val
= ioread32(priv
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
983 /* Check if CLKRUN# is already not enabled in the LPC bus */
984 if (!(clkrun_val
& LPC_CLKRUN_EN
)) {
985 iounmap(priv
->ilb_base_addr
);
986 priv
->ilb_base_addr
= NULL
;
990 if (chip
->ops
->clk_enable
!= NULL
)
991 chip
->ops
->clk_enable(chip
, true);
993 if (wait_startup(chip
, 0) != 0) {
998 /* Take control of the TPM's interrupt hardware and shut it off */
999 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
1003 intmask
|= TPM_INTF_CMD_READY_INT
| TPM_INTF_LOCALITY_CHANGE_INT
|
1004 TPM_INTF_DATA_AVAIL_INT
| TPM_INTF_STS_VALID_INT
;
1005 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
1006 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1008 rc
= tpm_chip_start(chip
);
1011 rc
= tpm2_probe(chip
);
1012 tpm_chip_stop(chip
);
1016 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &vendor
);
1020 priv
->manufacturer_id
= vendor
;
1022 rc
= tpm_tis_read8(priv
, TPM_RID(0), &rid
);
1026 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
1027 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
1030 probe
= probe_itpm(chip
);
1036 /* Figure out the capabilities */
1037 rc
= tpm_tis_read32(priv
, TPM_INTF_CAPS(priv
->locality
), &intfcaps
);
1041 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
1043 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
1044 dev_dbg(dev
, "\tBurst Count Static\n");
1045 if (intfcaps
& TPM_INTF_CMD_READY_INT
)
1046 dev_dbg(dev
, "\tCommand Ready Int Support\n");
1047 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
1048 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
1049 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
1050 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
1051 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
1052 dev_dbg(dev
, "\tInterrupt Level Low\n");
1053 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
1054 dev_dbg(dev
, "\tInterrupt Level High\n");
1055 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
)
1056 dev_dbg(dev
, "\tLocality Change Int Support\n");
1057 if (intfcaps
& TPM_INTF_STS_VALID_INT
)
1058 dev_dbg(dev
, "\tSts Valid Int Support\n");
1059 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
)
1060 dev_dbg(dev
, "\tData Avail Int Support\n");
1062 /* INTERRUPT Setup */
1063 init_waitqueue_head(&priv
->read_queue
);
1064 init_waitqueue_head(&priv
->int_queue
);
1066 /* Before doing irq testing issue a command to the TPM in polling mode
1067 * to make sure it works. May as well use that command to set the
1068 * proper timeouts for the driver.
1070 if (tpm_get_timeouts(chip
)) {
1071 dev_err(dev
, "Could not get TPM timeouts and durations\n");
1077 tpm_tis_probe_irq_single(chip
, intmask
, IRQF_SHARED
,
1079 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
1080 dev_err(&chip
->dev
, FW_BUG
1081 "TPM interrupt not working, polling instead\n");
1083 disable_interrupts(chip
);
1086 tpm_tis_probe_irq(chip
, intmask
);
1090 rc
= tpm_chip_register(chip
);
1094 if (chip
->ops
->clk_enable
!= NULL
)
1095 chip
->ops
->clk_enable(chip
, false);
1099 if (chip
->ops
->clk_enable
!= NULL
)
1100 chip
->ops
->clk_enable(chip
, false);
1102 tpm_tis_remove(chip
);
1106 EXPORT_SYMBOL_GPL(tpm_tis_core_init
);
1108 #ifdef CONFIG_PM_SLEEP
1109 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
1111 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
1115 if (chip
->ops
->clk_enable
!= NULL
)
1116 chip
->ops
->clk_enable(chip
, true);
1118 /* reenable interrupts that device may have lost or
1119 * BIOS/firmware may have disabled
1121 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), priv
->irq
);
1125 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
1129 intmask
|= TPM_INTF_CMD_READY_INT
1130 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
1131 | TPM_INTF_STS_VALID_INT
| TPM_GLOBAL_INT_ENABLE
;
1133 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1136 if (chip
->ops
->clk_enable
!= NULL
)
1137 chip
->ops
->clk_enable(chip
, false);
1142 int tpm_tis_resume(struct device
*dev
)
1144 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1147 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
)
1148 tpm_tis_reenable_interrupts(chip
);
1150 ret
= tpm_pm_resume(dev
);
1154 /* TPM 1.2 requires self-test on resume. This function actually returns
1155 * an error code but for unknown reason it isn't handled.
1157 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
))
1158 tpm1_do_selftest(chip
);
1162 EXPORT_SYMBOL_GPL(tpm_tis_resume
);
1165 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1166 MODULE_DESCRIPTION("TPM Driver");
1167 MODULE_VERSION("2.0");
1168 MODULE_LICENSE("GPL");