2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
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>
34 TPM_ACCESS_VALID = 0x80,
35 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
36 TPM_ACCESS_REQUEST_PENDING = 0x04,
37 TPM_ACCESS_REQUEST_USE = 0x02,
42 TPM_STS_COMMAND_READY = 0x40,
44 TPM_STS_DATA_AVAIL = 0x10,
45 TPM_STS_DATA_EXPECT = 0x08,
49 TPM_GLOBAL_INT_ENABLE = 0x80000000,
50 TPM_INTF_BURST_COUNT_STATIC = 0x100,
51 TPM_INTF_CMD_READY_INT = 0x080,
52 TPM_INTF_INT_EDGE_FALLING = 0x040,
53 TPM_INTF_INT_EDGE_RISING = 0x020,
54 TPM_INTF_INT_LEVEL_LOW = 0x010,
55 TPM_INTF_INT_LEVEL_HIGH = 0x008,
56 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
57 TPM_INTF_STS_VALID_INT = 0x002,
58 TPM_INTF_DATA_AVAIL_INT = 0x001,
62 TIS_MEM_BASE = 0xFED40000,
64 TIS_SHORT_TIMEOUT = 750, /* ms */
65 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
69 /* Some timeout values are needed before it is known whether the chip is
72 #define TIS_TIMEOUT_A_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
73 #define TIS_TIMEOUT_B_MAX max(TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
74 #define TIS_TIMEOUT_C_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
75 #define TIS_TIMEOUT_D_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
77 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
78 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
79 #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
80 #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
81 #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
82 #define TPM_STS(l) (0x0018 | ((l) << 12))
83 #define TPM_STS3(l) (0x001b | ((l) << 12))
84 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
86 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
87 #define TPM_RID(l) (0x0F04 | ((l) << 12))
93 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
94 static int is_itpm(struct pnp_dev *dev)
96 struct acpi_device *acpi = pnp_acpi_device(dev);
97 struct acpi_hardware_id *id;
102 list_for_each_entry(id, &acpi->pnp.ids, list) {
103 if (!strcmp("INTC0102", id->id))
110 static inline int is_itpm(struct pnp_dev *dev)
116 /* Before we attempt to access the TPM we must see that the valid bit is set.
117 * The specification says that this bit is 0 at reset and remains 0 until the
118 * 'TPM has gone through its self test and initialization and has established
119 * correct values in the other bits.' */
120 static int wait_startup(struct tpm_chip *chip, int l)
122 unsigned long stop = jiffies + chip->vendor.timeout_a;
124 if (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
128 } while (time_before(jiffies, stop));
132 static int check_locality(struct tpm_chip *chip, int l)
134 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
135 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
136 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
137 return chip->vendor.locality = l;
142 static void release_locality(struct tpm_chip *chip, int l, int force)
144 if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
145 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
146 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
147 iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
148 chip->vendor.iobase + TPM_ACCESS(l));
151 static int request_locality(struct tpm_chip *chip, int l)
153 unsigned long stop, timeout;
156 if (check_locality(chip, l) >= 0)
159 iowrite8(TPM_ACCESS_REQUEST_USE,
160 chip->vendor.iobase + TPM_ACCESS(l));
162 stop = jiffies + chip->vendor.timeout_a;
164 if (chip->vendor.irq) {
166 timeout = stop - jiffies;
167 if ((long)timeout <= 0)
169 rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
175 if (rc == -ERESTARTSYS && freezing(current)) {
176 clear_thread_flag(TIF_SIGPENDING);
180 /* wait for burstcount */
182 if (check_locality(chip, l) >= 0)
186 while (time_before(jiffies, stop));
191 static u8 tpm_tis_status(struct tpm_chip *chip)
193 return ioread8(chip->vendor.iobase +
194 TPM_STS(chip->vendor.locality));
197 static void tpm_tis_ready(struct tpm_chip *chip)
199 /* this causes the current command to be aborted */
200 iowrite8(TPM_STS_COMMAND_READY,
201 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
204 static int get_burstcount(struct tpm_chip *chip)
209 /* wait for burstcount */
210 /* which timeout value, spec has 2 answers (c & d) */
211 stop = jiffies + chip->vendor.timeout_d;
213 burstcnt = ioread8(chip->vendor.iobase +
214 TPM_STS(chip->vendor.locality) + 1);
215 burstcnt += ioread8(chip->vendor.iobase +
216 TPM_STS(chip->vendor.locality) +
221 } while (time_before(jiffies, stop));
225 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
227 int size = 0, burstcnt;
228 while (size < count &&
229 wait_for_tpm_stat(chip,
230 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
231 chip->vendor.timeout_c,
232 &chip->vendor.read_queue, true)
234 burstcnt = get_burstcount(chip);
235 for (; burstcnt > 0 && size < count; burstcnt--)
236 buf[size++] = ioread8(chip->vendor.iobase +
237 TPM_DATA_FIFO(chip->vendor.
243 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
246 int expected, status;
248 if (count < TPM_HEADER_SIZE) {
253 /* read first 10 bytes, including tag, paramsize, and result */
255 recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
256 dev_err(chip->pdev, "Unable to read header\n");
260 expected = be32_to_cpu(*(__be32 *) (buf + 2));
261 if (expected > count) {
267 recv_data(chip, &buf[TPM_HEADER_SIZE],
268 expected - TPM_HEADER_SIZE)) < expected) {
269 dev_err(chip->pdev, "Unable to read remainder of result\n");
274 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
275 &chip->vendor.int_queue, false);
276 status = tpm_tis_status(chip);
277 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
278 dev_err(chip->pdev, "Error left over data\n");
285 release_locality(chip, chip->vendor.locality, 0);
290 module_param(itpm, bool, 0444);
291 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
294 * If interrupts are used (signaled by an irq set in the vendor structure)
295 * tpm.c can skip polling for the data to be available as the interrupt is
298 static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
300 int rc, status, burstcnt;
303 if (request_locality(chip, 0) < 0)
306 status = tpm_tis_status(chip);
307 if ((status & TPM_STS_COMMAND_READY) == 0) {
309 if (wait_for_tpm_stat
310 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
311 &chip->vendor.int_queue, false) < 0) {
317 while (count < len - 1) {
318 burstcnt = get_burstcount(chip);
319 for (; burstcnt > 0 && count < len - 1; burstcnt--) {
320 iowrite8(buf[count], chip->vendor.iobase +
321 TPM_DATA_FIFO(chip->vendor.locality));
325 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
326 &chip->vendor.int_queue, false);
327 status = tpm_tis_status(chip);
328 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
334 /* write last byte */
336 chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
337 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
338 &chip->vendor.int_queue, false);
339 status = tpm_tis_status(chip);
340 if ((status & TPM_STS_DATA_EXPECT) != 0) {
349 release_locality(chip, chip->vendor.locality, 0);
353 static void disable_interrupts(struct tpm_chip *chip)
358 ioread32(chip->vendor.iobase +
359 TPM_INT_ENABLE(chip->vendor.locality));
360 intmask &= ~TPM_GLOBAL_INT_ENABLE;
362 chip->vendor.iobase +
363 TPM_INT_ENABLE(chip->vendor.locality));
364 free_irq(chip->vendor.irq, chip);
365 chip->vendor.irq = 0;
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_main(struct tpm_chip *chip, u8 *buf, size_t len)
379 rc = tpm_tis_send_data(chip, buf, len);
385 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
387 if (chip->vendor.irq) {
388 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
390 if (chip->flags & TPM_CHIP_FLAG_TPM2)
391 dur = tpm2_calc_ordinal_duration(chip, ordinal);
393 dur = tpm_calc_ordinal_duration(chip, ordinal);
395 if (wait_for_tpm_stat
396 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
397 &chip->vendor.read_queue, false) < 0) {
405 release_locality(chip, chip->vendor.locality, 0);
409 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
412 struct priv_data *priv = chip->vendor.priv;
414 if (!chip->vendor.irq || priv->irq_tested)
415 return tpm_tis_send_main(chip, buf, len);
417 /* Verify receipt of the expected IRQ */
418 irq = chip->vendor.irq;
419 chip->vendor.irq = 0;
420 rc = tpm_tis_send_main(chip, buf, len);
421 chip->vendor.irq = irq;
422 if (!priv->irq_tested)
424 if (!priv->irq_tested) {
425 disable_interrupts(chip);
427 FW_BUG "TPM interrupt not working, polling instead\n");
429 priv->irq_tested = true;
433 struct tis_vendor_timeout_override {
435 unsigned long timeout_us[4];
438 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
440 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
441 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
444 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
445 unsigned long *timeout_cap)
450 did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
452 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
453 if (vendor_timeout_overrides[i].did_vid != did_vid)
455 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
456 sizeof(vendor_timeout_overrides[i].timeout_us));
464 * Early probing for iTPM with STS_DATA_EXPECT flaw.
465 * Try sending command without itpm flag set and if that
466 * fails, repeat with itpm flag set.
468 static int probe_itpm(struct tpm_chip *chip)
471 u8 cmd_getticks[] = {
472 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
473 0x00, 0x00, 0x00, 0xf1
475 size_t len = sizeof(cmd_getticks);
476 bool rem_itpm = itpm;
477 u16 vendor = ioread16(chip->vendor.iobase + TPM_DID_VID(0));
479 /* probe only iTPMS */
480 if (vendor != TPM_VID_INTEL)
485 rc = tpm_tis_send_data(chip, cmd_getticks, len);
490 release_locality(chip, chip->vendor.locality, 0);
494 rc = tpm_tis_send_data(chip, cmd_getticks, len);
496 dev_info(chip->pdev, "Detected an iTPM.\n");
504 release_locality(chip, chip->vendor.locality, 0);
509 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
511 switch (chip->vendor.manufacturer_id) {
512 case TPM_VID_WINBOND:
513 return ((status == TPM_STS_VALID) ||
514 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
516 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
518 return (status == TPM_STS_COMMAND_READY);
522 static const struct tpm_class_ops tpm_tis = {
523 .status = tpm_tis_status,
524 .recv = tpm_tis_recv,
525 .send = tpm_tis_send,
526 .cancel = tpm_tis_ready,
527 .update_timeouts = tpm_tis_update_timeouts,
528 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
529 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
530 .req_canceled = tpm_tis_req_canceled,
533 static irqreturn_t tis_int_probe(int irq, void *dev_id)
535 struct tpm_chip *chip = dev_id;
538 interrupt = ioread32(chip->vendor.iobase +
539 TPM_INT_STATUS(chip->vendor.locality));
544 chip->vendor.probed_irq = irq;
546 /* Clear interrupts handled with TPM_EOI */
548 chip->vendor.iobase +
549 TPM_INT_STATUS(chip->vendor.locality));
553 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
555 struct tpm_chip *chip = dev_id;
559 interrupt = ioread32(chip->vendor.iobase +
560 TPM_INT_STATUS(chip->vendor.locality));
565 ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
566 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
567 wake_up_interruptible(&chip->vendor.read_queue);
568 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
569 for (i = 0; i < 5; i++)
570 if (check_locality(chip, i) >= 0)
573 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
574 TPM_INTF_CMD_READY_INT))
575 wake_up_interruptible(&chip->vendor.int_queue);
577 /* Clear interrupts handled with TPM_EOI */
579 chip->vendor.iobase +
580 TPM_INT_STATUS(chip->vendor.locality));
581 ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
585 static bool interrupts = true;
586 module_param(interrupts, bool, 0444);
587 MODULE_PARM_DESC(interrupts, "Enable interrupts");
589 static void tpm_tis_remove(struct tpm_chip *chip)
591 if (chip->flags & TPM_CHIP_FLAG_TPM2)
592 tpm2_shutdown(chip, TPM2_SU_CLEAR);
594 iowrite32(~TPM_GLOBAL_INT_ENABLE &
595 ioread32(chip->vendor.iobase +
596 TPM_INT_ENABLE(chip->vendor.
598 chip->vendor.iobase +
599 TPM_INT_ENABLE(chip->vendor.locality));
600 release_locality(chip, chip->vendor.locality, 1);
603 static int tpm_tis_init(struct device *dev, acpi_handle acpi_dev_handle,
604 resource_size_t start, resource_size_t len,
607 u32 vendor, intfcaps, intmask;
608 int rc, i, irq_s, irq_e, probe;
609 struct tpm_chip *chip;
610 struct priv_data *priv;
612 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
616 chip = tpmm_chip_alloc(dev, &tpm_tis);
618 return PTR_ERR(chip);
620 chip->vendor.priv = priv;
622 chip->acpi_dev_handle = acpi_dev_handle;
625 chip->vendor.iobase = devm_ioremap(dev, start, len);
626 if (!chip->vendor.iobase)
629 /* Maximum timeouts */
630 chip->vendor.timeout_a = TIS_TIMEOUT_A_MAX;
631 chip->vendor.timeout_b = TIS_TIMEOUT_B_MAX;
632 chip->vendor.timeout_c = TIS_TIMEOUT_C_MAX;
633 chip->vendor.timeout_d = TIS_TIMEOUT_D_MAX;
635 if (wait_startup(chip, 0) != 0) {
640 if (request_locality(chip, 0) != 0) {
645 rc = tpm2_probe(chip);
649 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
650 chip->vendor.manufacturer_id = vendor;
652 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
653 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
654 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
657 probe = probe_itpm(chip);
666 dev_info(dev, "Intel iTPM workaround enabled\n");
669 /* Figure out the capabilities */
671 ioread32(chip->vendor.iobase +
672 TPM_INTF_CAPS(chip->vendor.locality));
673 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
675 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
676 dev_dbg(dev, "\tBurst Count Static\n");
677 if (intfcaps & TPM_INTF_CMD_READY_INT)
678 dev_dbg(dev, "\tCommand Ready Int Support\n");
679 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
680 dev_dbg(dev, "\tInterrupt Edge Falling\n");
681 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
682 dev_dbg(dev, "\tInterrupt Edge Rising\n");
683 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
684 dev_dbg(dev, "\tInterrupt Level Low\n");
685 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
686 dev_dbg(dev, "\tInterrupt Level High\n");
687 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
688 dev_dbg(dev, "\tLocality Change Int Support\n");
689 if (intfcaps & TPM_INTF_STS_VALID_INT)
690 dev_dbg(dev, "\tSts Valid Int Support\n");
691 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
692 dev_dbg(dev, "\tData Avail Int Support\n");
694 /* INTERRUPT Setup */
695 init_waitqueue_head(&chip->vendor.read_queue);
696 init_waitqueue_head(&chip->vendor.int_queue);
699 ioread32(chip->vendor.iobase +
700 TPM_INT_ENABLE(chip->vendor.locality));
702 intmask |= TPM_INTF_CMD_READY_INT
703 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
704 | TPM_INTF_STS_VALID_INT;
707 chip->vendor.iobase +
708 TPM_INT_ENABLE(chip->vendor.locality));
710 chip->vendor.irq = irq;
711 if (interrupts && !chip->vendor.irq) {
713 ioread8(chip->vendor.iobase +
714 TPM_INT_VECTOR(chip->vendor.locality));
722 for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
723 iowrite8(i, chip->vendor.iobase +
724 TPM_INT_VECTOR(chip->vendor.locality));
726 (dev, i, tis_int_probe, IRQF_SHARED,
727 chip->devname, chip) != 0) {
729 "Unable to request irq: %d for probe\n",
734 /* Clear all existing */
736 (chip->vendor.iobase +
737 TPM_INT_STATUS(chip->vendor.locality)),
738 chip->vendor.iobase +
739 TPM_INT_STATUS(chip->vendor.locality));
742 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
743 chip->vendor.iobase +
744 TPM_INT_ENABLE(chip->vendor.locality));
746 chip->vendor.probed_irq = 0;
748 /* Generate Interrupts */
749 if (chip->flags & TPM_CHIP_FLAG_TPM2)
750 tpm2_gen_interrupt(chip);
752 tpm_gen_interrupt(chip);
754 chip->vendor.irq = chip->vendor.probed_irq;
756 /* free_irq will call into tis_int_probe;
757 clear all irqs we haven't seen while doing
760 (chip->vendor.iobase +
761 TPM_INT_STATUS(chip->vendor.locality)),
762 chip->vendor.iobase +
763 TPM_INT_STATUS(chip->vendor.locality));
767 chip->vendor.iobase +
768 TPM_INT_ENABLE(chip->vendor.locality));
771 if (chip->vendor.irq) {
772 iowrite8(chip->vendor.irq,
773 chip->vendor.iobase +
774 TPM_INT_VECTOR(chip->vendor.locality));
776 (dev, chip->vendor.irq, tis_int_handler, IRQF_SHARED,
777 chip->devname, chip) != 0) {
779 "Unable to request irq: %d for use\n",
781 chip->vendor.irq = 0;
783 /* Clear all existing */
785 (chip->vendor.iobase +
786 TPM_INT_STATUS(chip->vendor.locality)),
787 chip->vendor.iobase +
788 TPM_INT_STATUS(chip->vendor.locality));
791 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
792 chip->vendor.iobase +
793 TPM_INT_ENABLE(chip->vendor.locality));
797 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
798 chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
799 chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
800 chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
801 chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
802 chip->vendor.duration[TPM_SHORT] =
803 msecs_to_jiffies(TPM2_DURATION_SHORT);
804 chip->vendor.duration[TPM_MEDIUM] =
805 msecs_to_jiffies(TPM2_DURATION_MEDIUM);
806 chip->vendor.duration[TPM_LONG] =
807 msecs_to_jiffies(TPM2_DURATION_LONG);
809 rc = tpm2_do_selftest(chip);
810 if (rc == TPM2_RC_INITIALIZE) {
811 dev_warn(dev, "Firmware has not started TPM\n");
812 rc = tpm2_startup(chip, TPM2_SU_CLEAR);
814 rc = tpm2_do_selftest(chip);
818 dev_err(dev, "TPM self test failed\n");
824 if (tpm_get_timeouts(chip)) {
825 dev_err(dev, "Could not get TPM timeouts and durations\n");
830 if (tpm_do_selftest(chip)) {
831 dev_err(dev, "TPM self test failed\n");
837 return tpm_chip_register(chip);
839 tpm_tis_remove(chip);
843 #ifdef CONFIG_PM_SLEEP
844 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
848 /* reenable interrupts that device may have lost or
849 BIOS/firmware may have disabled */
850 iowrite8(chip->vendor.irq, chip->vendor.iobase +
851 TPM_INT_VECTOR(chip->vendor.locality));
854 ioread32(chip->vendor.iobase +
855 TPM_INT_ENABLE(chip->vendor.locality));
857 intmask |= TPM_INTF_CMD_READY_INT
858 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
859 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
862 chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
865 static int tpm_tis_resume(struct device *dev)
867 struct tpm_chip *chip = dev_get_drvdata(dev);
870 if (chip->vendor.irq)
871 tpm_tis_reenable_interrupts(chip);
873 ret = tpm_pm_resume(dev);
877 /* TPM 1.2 requires self-test on resume. This function actually returns
878 * an error code but for unknown reason it isn't handled.
880 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
881 tpm_do_selftest(chip);
887 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
890 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
891 const struct pnp_device_id *pnp_id)
893 resource_size_t start, len;
894 unsigned int irq = 0;
895 acpi_handle acpi_dev_handle = NULL;
897 start = pnp_mem_start(pnp_dev, 0);
898 len = pnp_mem_len(pnp_dev, 0);
900 if (pnp_irq_valid(pnp_dev, 0))
901 irq = pnp_irq(pnp_dev, 0);
905 if (is_itpm(pnp_dev))
909 if (pnp_acpi_device(pnp_dev))
910 acpi_dev_handle = pnp_acpi_device(pnp_dev)->handle;
913 return tpm_tis_init(&pnp_dev->dev, acpi_dev_handle, start, len, irq);
916 static struct pnp_device_id tpm_pnp_tbl[] = {
917 {"PNP0C31", 0}, /* TPM */
918 {"ATM1200", 0}, /* Atmel */
919 {"IFX0102", 0}, /* Infineon */
920 {"BCM0101", 0}, /* Broadcom */
921 {"BCM0102", 0}, /* Broadcom */
922 {"NSC1200", 0}, /* National */
923 {"ICO0102", 0}, /* Intel */
925 {"", 0}, /* User Specified */
926 {"", 0} /* Terminator */
928 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
930 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
932 struct tpm_chip *chip = pnp_get_drvdata(dev);
933 tpm_chip_unregister(chip);
934 tpm_tis_remove(chip);
937 static struct pnp_driver tis_pnp_driver = {
939 .id_table = tpm_pnp_tbl,
940 .probe = tpm_tis_pnp_init,
941 .remove = tpm_tis_pnp_remove,
947 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
948 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
949 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
950 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
953 static struct platform_driver tis_drv = {
960 static struct platform_device *pdev;
963 module_param(force, bool, 0444);
964 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
965 static int __init init_tis(void)
970 return pnp_register_driver(&tis_pnp_driver);
973 rc = platform_driver_register(&tis_drv);
976 pdev = platform_device_register_simple("tpm_tis", -1, NULL, 0);
981 rc = tpm_tis_init(&pdev->dev, NULL, TIS_MEM_BASE, TIS_MEM_LEN, 0);
986 platform_device_unregister(pdev);
988 platform_driver_unregister(&tis_drv);
992 static void __exit cleanup_tis(void)
994 struct tpm_chip *chip;
997 pnp_unregister_driver(&tis_pnp_driver);
1001 chip = dev_get_drvdata(&pdev->dev);
1002 tpm_chip_unregister(chip);
1003 tpm_tis_remove(chip);
1004 platform_device_unregister(pdev);
1005 platform_driver_unregister(&tis_drv);
1008 module_init(init_tis);
1009 module_exit(cleanup_tis);
1011 MODULE_DESCRIPTION("TPM Driver");
1012 MODULE_VERSION("2.0");
1013 MODULE_LICENSE("GPL");