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_REQUEST_USE
)) ==
130 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) {
138 static int release_locality(struct tpm_chip
*chip
, int l
)
140 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
142 tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_ACTIVE_LOCALITY
);
147 static int request_locality(struct tpm_chip
*chip
, int l
)
149 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
150 unsigned long stop
, timeout
;
153 if (check_locality(chip
, l
))
156 rc
= tpm_tis_write8(priv
, TPM_ACCESS(l
), TPM_ACCESS_REQUEST_USE
);
160 stop
= jiffies
+ chip
->timeout_a
;
162 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
164 timeout
= stop
- jiffies
;
165 if ((long)timeout
<= 0)
167 rc
= wait_event_interruptible_timeout(priv
->int_queue
,
173 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
174 clear_thread_flag(TIF_SIGPENDING
);
178 /* wait for burstcount */
180 if (check_locality(chip
, l
))
182 tpm_msleep(TPM_TIMEOUT
);
183 } while (time_before(jiffies
, stop
));
188 static u8
tpm_tis_status(struct tpm_chip
*chip
)
190 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
194 rc
= tpm_tis_read8(priv
, TPM_STS(priv
->locality
), &status
);
198 if (unlikely((status
& TPM_STS_READ_ZERO
) != 0)) {
199 if (!test_and_set_bit(TPM_TIS_INVALID_STATUS
, &priv
->flags
)) {
201 * If this trips, the chances are the read is
202 * returning 0xff because the locality hasn't been
203 * acquired. Usually because tpm_try_get_ops() hasn't
204 * been called before doing a TPM operation.
206 dev_err(&chip
->dev
, "invalid TPM_STS.x 0x%02x, dumping stack for forensics\n",
210 * Dump stack for forensics, as invalid TPM_STS.x could be
211 * potentially triggered by impaired tpm_try_get_ops() or
212 * tpm_find_get_ops().
223 static void tpm_tis_ready(struct tpm_chip
*chip
)
225 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
227 /* this causes the current command to be aborted */
228 tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_COMMAND_READY
);
231 static int get_burstcount(struct tpm_chip
*chip
)
233 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
238 /* wait for burstcount */
239 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
240 stop
= jiffies
+ chip
->timeout_a
;
242 stop
= jiffies
+ chip
->timeout_d
;
244 rc
= tpm_tis_read32(priv
, TPM_STS(priv
->locality
), &value
);
248 burstcnt
= (value
>> 8) & 0xFFFF;
251 usleep_range(TPM_TIMEOUT_USECS_MIN
, TPM_TIMEOUT_USECS_MAX
);
252 } while (time_before(jiffies
, stop
));
256 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
258 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
259 int size
= 0, burstcnt
, rc
;
261 while (size
< count
) {
262 rc
= wait_for_tpm_stat(chip
,
263 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
265 &priv
->read_queue
, true);
268 burstcnt
= get_burstcount(chip
);
270 dev_err(&chip
->dev
, "Unable to read burstcount\n");
273 burstcnt
= min_t(int, burstcnt
, count
- size
);
275 rc
= tpm_tis_read_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
276 burstcnt
, buf
+ size
);
285 static int tpm_tis_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
287 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
292 if (count
< TPM_HEADER_SIZE
) {
297 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
298 /* read first 10 bytes, including tag, paramsize, and result */
299 if (size
< TPM_HEADER_SIZE
) {
300 dev_err(&chip
->dev
, "Unable to read header\n");
304 expected
= be32_to_cpu(*(__be32
*) (buf
+ 2));
305 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
310 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
311 expected
- TPM_HEADER_SIZE
);
312 if (size
< expected
) {
313 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
318 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
319 &priv
->int_queue
, false) < 0) {
323 status
= tpm_tis_status(chip
);
324 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
325 dev_err(&chip
->dev
, "Error left over data\n");
336 * If interrupts are used (signaled by an irq set in the vendor structure)
337 * tpm.c can skip polling for the data to be available as the interrupt is
340 static int tpm_tis_send_data(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
342 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
343 int rc
, status
, burstcnt
;
345 bool itpm
= priv
->flags
& TPM_TIS_ITPM_WORKAROUND
;
347 status
= tpm_tis_status(chip
);
348 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
350 if (wait_for_tpm_stat
351 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
352 &priv
->int_queue
, false) < 0) {
358 while (count
< len
- 1) {
359 burstcnt
= get_burstcount(chip
);
361 dev_err(&chip
->dev
, "Unable to read burstcount\n");
365 burstcnt
= min_t(int, burstcnt
, len
- count
- 1);
366 rc
= tpm_tis_write_bytes(priv
, TPM_DATA_FIFO(priv
->locality
),
367 burstcnt
, 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) {
385 /* write last byte */
386 rc
= tpm_tis_write8(priv
, TPM_DATA_FIFO(priv
->locality
), buf
[count
]);
390 if (wait_for_tpm_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
,
391 &priv
->int_queue
, false) < 0) {
395 status
= tpm_tis_status(chip
);
396 if (!itpm
&& (status
& TPM_STS_DATA_EXPECT
) != 0) {
408 static void disable_interrupts(struct tpm_chip
*chip
)
410 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
417 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
421 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
422 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
424 devm_free_irq(chip
->dev
.parent
, priv
->irq
, chip
);
426 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
430 * If interrupts are used (signaled by an irq set in the vendor structure)
431 * tpm.c can skip polling for the data to be available as the interrupt is
434 static int tpm_tis_send_main(struct tpm_chip
*chip
, const u8
*buf
, size_t len
)
436 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
441 rc
= tpm_tis_send_data(chip
, buf
, len
);
446 rc
= tpm_tis_write8(priv
, TPM_STS(priv
->locality
), TPM_STS_GO
);
450 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
451 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
453 dur
= tpm_calc_ordinal_duration(chip
, ordinal
);
454 if (wait_for_tpm_stat
455 (chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
, dur
,
456 &priv
->read_queue
, false) < 0) {
467 static int tpm_tis_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
470 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
472 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
) || priv
->irq_tested
)
473 return tpm_tis_send_main(chip
, buf
, len
);
475 /* Verify receipt of the expected IRQ */
478 chip
->flags
&= ~TPM_CHIP_FLAG_IRQ
;
479 rc
= tpm_tis_send_main(chip
, buf
, len
);
481 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
482 if (!priv
->irq_tested
)
484 if (!priv
->irq_tested
)
485 disable_interrupts(chip
);
486 priv
->irq_tested
= true;
490 struct tis_vendor_durations_override
{
492 struct tpm1_version version
;
493 unsigned long durations
[3];
496 static const struct tis_vendor_durations_override vendor_dur_overrides
[] = {
497 /* STMicroelectronics 0x104a */
500 { (2 * 60 * HZ
), (2 * 60 * HZ
), (2 * 60 * HZ
) } },
503 static void tpm_tis_update_durations(struct tpm_chip
*chip
,
504 unsigned long *duration_cap
)
506 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
507 struct tpm1_version
*version
;
512 chip
->duration_adjusted
= false;
514 if (chip
->ops
->clk_enable
!= NULL
)
515 chip
->ops
->clk_enable(chip
, true);
517 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
519 dev_warn(&chip
->dev
, "%s: failed to read did_vid. %d\n",
524 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
525 rc
= tpm1_getcap(chip
, TPM_CAP_VERSION_1_2
, &cap
,
526 "attempting to determine the 1.2 version",
527 sizeof(cap
.version2
));
529 version
= &cap
.version2
.version
;
531 rc
= tpm1_getcap(chip
, TPM_CAP_VERSION_1_1
, &cap
,
532 "attempting to determine the 1.1 version",
533 sizeof(cap
.version1
));
538 version
= &cap
.version1
;
541 for (i
= 0; i
!= ARRAY_SIZE(vendor_dur_overrides
); i
++) {
542 if (vendor_dur_overrides
[i
].did_vid
!= did_vid
)
545 if ((version
->major
==
546 vendor_dur_overrides
[i
].version
.major
) &&
548 vendor_dur_overrides
[i
].version
.minor
) &&
549 (version
->rev_major
==
550 vendor_dur_overrides
[i
].version
.rev_major
) &&
551 (version
->rev_minor
==
552 vendor_dur_overrides
[i
].version
.rev_minor
)) {
555 vendor_dur_overrides
[i
].durations
,
556 sizeof(vendor_dur_overrides
[i
].durations
));
558 chip
->duration_adjusted
= true;
564 if (chip
->ops
->clk_enable
!= NULL
)
565 chip
->ops
->clk_enable(chip
, false);
568 struct tis_vendor_timeout_override
{
570 unsigned long timeout_us
[4];
573 static const struct tis_vendor_timeout_override vendor_timeout_overrides
[] = {
575 { 0x32041114, { (TIS_SHORT_TIMEOUT
*1000), (TIS_LONG_TIMEOUT
*1000),
576 (TIS_SHORT_TIMEOUT
*1000), (TIS_SHORT_TIMEOUT
*1000) } },
579 static void tpm_tis_update_timeouts(struct tpm_chip
*chip
,
580 unsigned long *timeout_cap
)
582 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
586 chip
->timeout_adjusted
= false;
588 if (chip
->ops
->clk_enable
!= NULL
)
589 chip
->ops
->clk_enable(chip
, true);
591 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &did_vid
);
593 dev_warn(&chip
->dev
, "%s: failed to read did_vid: %d\n",
598 for (i
= 0; i
!= ARRAY_SIZE(vendor_timeout_overrides
); i
++) {
599 if (vendor_timeout_overrides
[i
].did_vid
!= did_vid
)
601 memcpy(timeout_cap
, vendor_timeout_overrides
[i
].timeout_us
,
602 sizeof(vendor_timeout_overrides
[i
].timeout_us
));
603 chip
->timeout_adjusted
= true;
607 if (chip
->ops
->clk_enable
!= NULL
)
608 chip
->ops
->clk_enable(chip
, false);
614 * Early probing for iTPM with STS_DATA_EXPECT flaw.
615 * Try sending command without itpm flag set and if that
616 * fails, repeat with itpm flag set.
618 static int probe_itpm(struct tpm_chip
*chip
)
620 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
622 static const u8 cmd_getticks
[] = {
623 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
624 0x00, 0x00, 0x00, 0xf1
626 size_t len
= sizeof(cmd_getticks
);
629 if (priv
->flags
& TPM_TIS_ITPM_WORKAROUND
)
632 rc
= tpm_tis_read16(priv
, TPM_DID_VID(0), &vendor
);
636 /* probe only iTPMS */
637 if (vendor
!= TPM_VID_INTEL
)
640 if (request_locality(chip
, 0) != 0)
643 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
649 priv
->flags
|= TPM_TIS_ITPM_WORKAROUND
;
651 rc
= tpm_tis_send_data(chip
, cmd_getticks
, len
);
653 dev_info(&chip
->dev
, "Detected an iTPM.\n");
655 priv
->flags
&= ~TPM_TIS_ITPM_WORKAROUND
;
661 release_locality(chip
, priv
->locality
);
666 static bool tpm_tis_req_canceled(struct tpm_chip
*chip
, u8 status
)
668 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
670 switch (priv
->manufacturer_id
) {
671 case TPM_VID_WINBOND
:
672 return ((status
== TPM_STS_VALID
) ||
673 (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
)));
675 return (status
== (TPM_STS_VALID
| TPM_STS_COMMAND_READY
));
677 return (status
== TPM_STS_COMMAND_READY
);
681 static irqreturn_t
tis_int_handler(int dummy
, void *dev_id
)
683 struct tpm_chip
*chip
= dev_id
;
684 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
688 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
695 priv
->irq_tested
= true;
696 if (interrupt
& TPM_INTF_DATA_AVAIL_INT
)
697 wake_up_interruptible(&priv
->read_queue
);
698 if (interrupt
& TPM_INTF_LOCALITY_CHANGE_INT
)
699 for (i
= 0; i
< 5; i
++)
700 if (check_locality(chip
, i
))
703 (TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_STS_VALID_INT
|
704 TPM_INTF_CMD_READY_INT
))
705 wake_up_interruptible(&priv
->int_queue
);
707 /* Clear interrupts handled with TPM_EOI */
708 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), interrupt
);
712 tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &interrupt
);
716 static int tpm_tis_gen_interrupt(struct tpm_chip
*chip
)
718 const char *desc
= "attempting to generate an interrupt";
723 ret
= request_locality(chip
, 0);
727 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
728 ret
= tpm2_get_tpm_pt(chip
, 0x100, &cap2
, desc
);
730 ret
= tpm1_getcap(chip
, TPM_CAP_PROP_TIS_TIMEOUT
, &cap
, desc
, 0);
732 release_locality(chip
, 0);
737 /* Register the IRQ and issue a command that will cause an interrupt. If an
738 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
739 * everything and leave in polling mode. Returns 0 on success.
741 static int tpm_tis_probe_irq_single(struct tpm_chip
*chip
, u32 intmask
,
744 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
749 if (devm_request_irq(chip
->dev
.parent
, irq
, tis_int_handler
, flags
,
750 dev_name(&chip
->dev
), chip
) != 0) {
751 dev_info(&chip
->dev
, "Unable to request irq: %d for probe\n",
757 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
762 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), irq
);
766 rc
= tpm_tis_read32(priv
, TPM_INT_STATUS(priv
->locality
), &int_status
);
770 /* Clear all existing */
771 rc
= tpm_tis_write32(priv
, TPM_INT_STATUS(priv
->locality
), int_status
);
776 rc
= tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
),
777 intmask
| TPM_GLOBAL_INT_ENABLE
);
781 priv
->irq_tested
= false;
783 /* Generate an interrupt by having the core call through to
786 rc
= tpm_tis_gen_interrupt(chip
);
790 /* tpm_tis_send will either confirm the interrupt is working or it
791 * will call disable_irq which undoes all of the above.
793 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
794 rc
= tpm_tis_write8(priv
, original_int_vec
,
795 TPM_INT_VECTOR(priv
->locality
));
805 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
806 * do not have ACPI/etc. We typically expect the interrupt to be declared if
809 static void tpm_tis_probe_irq(struct tpm_chip
*chip
, u32 intmask
)
811 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
815 rc
= tpm_tis_read8(priv
, TPM_INT_VECTOR(priv
->locality
),
820 if (!original_int_vec
) {
821 if (IS_ENABLED(CONFIG_X86
))
822 for (i
= 3; i
<= 15; i
++)
823 if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
826 } else if (!tpm_tis_probe_irq_single(chip
, intmask
, 0,
831 void tpm_tis_remove(struct tpm_chip
*chip
)
833 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
834 u32 reg
= TPM_INT_ENABLE(priv
->locality
);
838 tpm_tis_clkrun_enable(chip
, true);
840 rc
= tpm_tis_read32(priv
, reg
, &interrupt
);
844 tpm_tis_write32(priv
, reg
, ~TPM_GLOBAL_INT_ENABLE
& interrupt
);
846 tpm_tis_clkrun_enable(chip
, false);
848 if (priv
->ilb_base_addr
)
849 iounmap(priv
->ilb_base_addr
);
851 EXPORT_SYMBOL_GPL(tpm_tis_remove
);
854 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
855 * of a single TPM command
856 * @chip: TPM chip to use
857 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
858 * 0 - Enable CLKRUN protocol
859 * Call this function directly in tpm_tis_remove() in error or driver removal
860 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
862 static void tpm_tis_clkrun_enable(struct tpm_chip
*chip
, bool value
)
864 struct tpm_tis_data
*data
= dev_get_drvdata(&chip
->dev
);
867 if (!IS_ENABLED(CONFIG_X86
) || !is_bsw() ||
868 !data
->ilb_base_addr
)
872 data
->clkrun_enabled
++;
873 if (data
->clkrun_enabled
> 1)
875 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
877 /* Disable LPC CLKRUN# */
878 clkrun_val
&= ~LPC_CLKRUN_EN
;
879 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
882 * Write any random value on port 0x80 which is on LPC, to make
883 * sure LPC clock is running before sending any TPM command.
887 data
->clkrun_enabled
--;
888 if (data
->clkrun_enabled
)
891 clkrun_val
= ioread32(data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
893 /* Enable LPC CLKRUN# */
894 clkrun_val
|= LPC_CLKRUN_EN
;
895 iowrite32(clkrun_val
, data
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
898 * Write any random value on port 0x80 which is on LPC, to make
899 * sure LPC clock is running before sending any TPM command.
905 static const struct tpm_class_ops tpm_tis
= {
906 .flags
= TPM_OPS_AUTO_STARTUP
,
907 .status
= tpm_tis_status
,
908 .recv
= tpm_tis_recv
,
909 .send
= tpm_tis_send
,
910 .cancel
= tpm_tis_ready
,
911 .update_timeouts
= tpm_tis_update_timeouts
,
912 .update_durations
= tpm_tis_update_durations
,
913 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
914 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
915 .req_canceled
= tpm_tis_req_canceled
,
916 .request_locality
= request_locality
,
917 .relinquish_locality
= release_locality
,
918 .clk_enable
= tpm_tis_clkrun_enable
,
921 int tpm_tis_core_init(struct device
*dev
, struct tpm_tis_data
*priv
, int irq
,
922 const struct tpm_tis_phy_ops
*phy_ops
,
923 acpi_handle acpi_dev_handle
)
931 struct tpm_chip
*chip
;
933 chip
= tpmm_chip_alloc(dev
, &tpm_tis
);
935 return PTR_ERR(chip
);
938 chip
->acpi_dev_handle
= acpi_dev_handle
;
941 chip
->hwrng
.quality
= priv
->rng_quality
;
943 /* Maximum timeouts */
944 chip
->timeout_a
= msecs_to_jiffies(TIS_TIMEOUT_A_MAX
);
945 chip
->timeout_b
= msecs_to_jiffies(TIS_TIMEOUT_B_MAX
);
946 chip
->timeout_c
= msecs_to_jiffies(TIS_TIMEOUT_C_MAX
);
947 chip
->timeout_d
= msecs_to_jiffies(TIS_TIMEOUT_D_MAX
);
948 priv
->phy_ops
= phy_ops
;
949 dev_set_drvdata(&chip
->dev
, priv
);
952 priv
->ilb_base_addr
= ioremap(INTEL_LEGACY_BLK_BASE_ADDR
,
954 if (!priv
->ilb_base_addr
)
957 clkrun_val
= ioread32(priv
->ilb_base_addr
+ LPC_CNTRL_OFFSET
);
958 /* Check if CLKRUN# is already not enabled in the LPC bus */
959 if (!(clkrun_val
& LPC_CLKRUN_EN
)) {
960 iounmap(priv
->ilb_base_addr
);
961 priv
->ilb_base_addr
= NULL
;
965 if (chip
->ops
->clk_enable
!= NULL
)
966 chip
->ops
->clk_enable(chip
, true);
968 if (wait_startup(chip
, 0) != 0) {
973 /* Take control of the TPM's interrupt hardware and shut it off */
974 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
978 intmask
|= TPM_INTF_CMD_READY_INT
| TPM_INTF_LOCALITY_CHANGE_INT
|
979 TPM_INTF_DATA_AVAIL_INT
| TPM_INTF_STS_VALID_INT
;
980 intmask
&= ~TPM_GLOBAL_INT_ENABLE
;
981 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
983 rc
= tpm_chip_start(chip
);
986 rc
= tpm2_probe(chip
);
991 rc
= tpm_tis_read32(priv
, TPM_DID_VID(0), &vendor
);
995 priv
->manufacturer_id
= vendor
;
997 rc
= tpm_tis_read8(priv
, TPM_RID(0), &rid
);
1001 dev_info(dev
, "%s TPM (device-id 0x%X, rev-id %d)\n",
1002 (chip
->flags
& TPM_CHIP_FLAG_TPM2
) ? "2.0" : "1.2",
1005 probe
= probe_itpm(chip
);
1011 /* Figure out the capabilities */
1012 rc
= tpm_tis_read32(priv
, TPM_INTF_CAPS(priv
->locality
), &intfcaps
);
1016 dev_dbg(dev
, "TPM interface capabilities (0x%x):\n",
1018 if (intfcaps
& TPM_INTF_BURST_COUNT_STATIC
)
1019 dev_dbg(dev
, "\tBurst Count Static\n");
1020 if (intfcaps
& TPM_INTF_CMD_READY_INT
)
1021 dev_dbg(dev
, "\tCommand Ready Int Support\n");
1022 if (intfcaps
& TPM_INTF_INT_EDGE_FALLING
)
1023 dev_dbg(dev
, "\tInterrupt Edge Falling\n");
1024 if (intfcaps
& TPM_INTF_INT_EDGE_RISING
)
1025 dev_dbg(dev
, "\tInterrupt Edge Rising\n");
1026 if (intfcaps
& TPM_INTF_INT_LEVEL_LOW
)
1027 dev_dbg(dev
, "\tInterrupt Level Low\n");
1028 if (intfcaps
& TPM_INTF_INT_LEVEL_HIGH
)
1029 dev_dbg(dev
, "\tInterrupt Level High\n");
1030 if (intfcaps
& TPM_INTF_LOCALITY_CHANGE_INT
)
1031 dev_dbg(dev
, "\tLocality Change Int Support\n");
1032 if (intfcaps
& TPM_INTF_STS_VALID_INT
)
1033 dev_dbg(dev
, "\tSts Valid Int Support\n");
1034 if (intfcaps
& TPM_INTF_DATA_AVAIL_INT
)
1035 dev_dbg(dev
, "\tData Avail Int Support\n");
1037 /* INTERRUPT Setup */
1038 init_waitqueue_head(&priv
->read_queue
);
1039 init_waitqueue_head(&priv
->int_queue
);
1042 * Before doing irq testing issue a command to the TPM in polling mode
1043 * to make sure it works. May as well use that command to set the
1044 * proper timeouts for the driver.
1047 rc
= request_locality(chip
, 0);
1051 rc
= tpm_get_timeouts(chip
);
1053 release_locality(chip
, 0);
1056 dev_err(dev
, "Could not get TPM timeouts and durations\n");
1062 tpm_tis_probe_irq_single(chip
, intmask
, IRQF_SHARED
,
1064 if (!(chip
->flags
& TPM_CHIP_FLAG_IRQ
)) {
1065 dev_err(&chip
->dev
, FW_BUG
1066 "TPM interrupt not working, polling instead\n");
1068 disable_interrupts(chip
);
1071 tpm_tis_probe_irq(chip
, intmask
);
1075 rc
= tpm_chip_register(chip
);
1079 if (chip
->ops
->clk_enable
!= NULL
)
1080 chip
->ops
->clk_enable(chip
, false);
1084 if (chip
->ops
->clk_enable
!= NULL
)
1085 chip
->ops
->clk_enable(chip
, false);
1087 tpm_tis_remove(chip
);
1091 EXPORT_SYMBOL_GPL(tpm_tis_core_init
);
1093 #ifdef CONFIG_PM_SLEEP
1094 static void tpm_tis_reenable_interrupts(struct tpm_chip
*chip
)
1096 struct tpm_tis_data
*priv
= dev_get_drvdata(&chip
->dev
);
1100 if (chip
->ops
->clk_enable
!= NULL
)
1101 chip
->ops
->clk_enable(chip
, true);
1103 /* reenable interrupts that device may have lost or
1104 * BIOS/firmware may have disabled
1106 rc
= tpm_tis_write8(priv
, TPM_INT_VECTOR(priv
->locality
), priv
->irq
);
1110 rc
= tpm_tis_read32(priv
, TPM_INT_ENABLE(priv
->locality
), &intmask
);
1114 intmask
|= TPM_INTF_CMD_READY_INT
1115 | TPM_INTF_LOCALITY_CHANGE_INT
| TPM_INTF_DATA_AVAIL_INT
1116 | TPM_INTF_STS_VALID_INT
| TPM_GLOBAL_INT_ENABLE
;
1118 tpm_tis_write32(priv
, TPM_INT_ENABLE(priv
->locality
), intmask
);
1121 if (chip
->ops
->clk_enable
!= NULL
)
1122 chip
->ops
->clk_enable(chip
, false);
1127 int tpm_tis_resume(struct device
*dev
)
1129 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
1132 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
)
1133 tpm_tis_reenable_interrupts(chip
);
1135 ret
= tpm_pm_resume(dev
);
1140 * TPM 1.2 requires self-test on resume. This function actually returns
1141 * an error code but for unknown reason it isn't handled.
1143 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
)) {
1144 ret
= request_locality(chip
, 0);
1148 tpm1_do_selftest(chip
);
1150 release_locality(chip
, 0);
1155 EXPORT_SYMBOL_GPL(tpm_tis_resume
);
1158 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1159 MODULE_DESCRIPTION("TPM Driver");
1160 MODULE_VERSION("2.0");
1161 MODULE_LICENSE("GPL");