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 /* Before we attempt to access the TPM we must see that the valid bit is set.
37 * The specification says that this bit is 0 at reset and remains 0 until the
38 * 'TPM has gone through its self test and initialization and has established
39 * correct values in the other bits.'
41 static int wait_startup(struct tpm_chip *chip, int l)
43 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
44 unsigned long stop = jiffies + chip->timeout_a;
50 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
54 if (access & TPM_ACCESS_VALID)
56 tpm_msleep(TPM_TIMEOUT);
57 } while (time_before(jiffies, stop));
61 static bool check_locality(struct tpm_chip *chip, int l)
63 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
67 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
71 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID
72 | TPM_ACCESS_REQUEST_USE)) ==
73 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
81 static int release_locality(struct tpm_chip *chip, int l)
83 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
85 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
90 static int request_locality(struct tpm_chip *chip, int l)
92 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
93 unsigned long stop, timeout;
96 if (check_locality(chip, l))
99 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
103 stop = jiffies + chip->timeout_a;
105 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
107 timeout = stop - jiffies;
108 if ((long)timeout <= 0)
110 rc = wait_event_interruptible_timeout(priv->int_queue,
116 if (rc == -ERESTARTSYS && freezing(current)) {
117 clear_thread_flag(TIF_SIGPENDING);
121 /* wait for burstcount */
123 if (check_locality(chip, l))
125 tpm_msleep(TPM_TIMEOUT);
126 } while (time_before(jiffies, stop));
131 static u8 tpm_tis_status(struct tpm_chip *chip)
133 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
137 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
144 static void tpm_tis_ready(struct tpm_chip *chip)
146 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
148 /* this causes the current command to be aborted */
149 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
152 static int get_burstcount(struct tpm_chip *chip)
154 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
159 /* wait for burstcount */
160 if (chip->flags & TPM_CHIP_FLAG_TPM2)
161 stop = jiffies + chip->timeout_a;
163 stop = jiffies + chip->timeout_d;
165 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
169 burstcnt = (value >> 8) & 0xFFFF;
172 tpm_msleep(TPM_TIMEOUT);
173 } while (time_before(jiffies, stop));
177 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
179 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
180 int size = 0, burstcnt, rc;
182 while (size < count) {
183 rc = wait_for_tpm_stat(chip,
184 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
186 &priv->read_queue, true);
189 burstcnt = get_burstcount(chip);
191 dev_err(&chip->dev, "Unable to read burstcount\n");
194 burstcnt = min_t(int, burstcnt, count - size);
196 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
197 burstcnt, buf + size);
206 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
208 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
213 if (count < TPM_HEADER_SIZE) {
218 size = recv_data(chip, buf, TPM_HEADER_SIZE);
219 /* read first 10 bytes, including tag, paramsize, and result */
220 if (size < TPM_HEADER_SIZE) {
221 dev_err(&chip->dev, "Unable to read header\n");
225 expected = be32_to_cpu(*(__be32 *) (buf + 2));
226 if (expected > count || expected < TPM_HEADER_SIZE) {
231 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
232 expected - TPM_HEADER_SIZE);
233 if (size < expected) {
234 dev_err(&chip->dev, "Unable to read remainder of result\n");
239 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
240 &priv->int_queue, false) < 0) {
244 status = tpm_tis_status(chip);
245 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
246 dev_err(&chip->dev, "Error left over data\n");
257 * If interrupts are used (signaled by an irq set in the vendor structure)
258 * tpm.c can skip polling for the data to be available as the interrupt is
261 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
263 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
264 int rc, status, burstcnt;
266 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
268 status = tpm_tis_status(chip);
269 if ((status & TPM_STS_COMMAND_READY) == 0) {
271 if (wait_for_tpm_stat
272 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
273 &priv->int_queue, false) < 0) {
279 while (count < len - 1) {
280 burstcnt = get_burstcount(chip);
282 dev_err(&chip->dev, "Unable to read burstcount\n");
286 burstcnt = min_t(int, burstcnt, len - count - 1);
287 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
288 burstcnt, buf + count);
294 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
295 &priv->int_queue, false) < 0) {
299 status = tpm_tis_status(chip);
300 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
306 /* write last byte */
307 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
311 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
312 &priv->int_queue, false) < 0) {
316 status = tpm_tis_status(chip);
317 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
329 static void disable_interrupts(struct tpm_chip *chip)
331 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
338 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
342 intmask &= ~TPM_GLOBAL_INT_ENABLE;
343 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
345 devm_free_irq(chip->dev.parent, priv->irq, chip);
347 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
351 * If interrupts are used (signaled by an irq set in the vendor structure)
352 * tpm.c can skip polling for the data to be available as the interrupt is
355 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
357 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
362 rc = tpm_tis_send_data(chip, buf, len);
367 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
371 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
372 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
374 if (chip->flags & TPM_CHIP_FLAG_TPM2)
375 dur = tpm2_calc_ordinal_duration(chip, ordinal);
377 dur = tpm_calc_ordinal_duration(chip, ordinal);
379 if (wait_for_tpm_stat
380 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
381 &priv->read_queue, false) < 0) {
392 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
395 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
397 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
398 return tpm_tis_send_main(chip, buf, len);
400 /* Verify receipt of the expected IRQ */
403 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
404 rc = tpm_tis_send_main(chip, buf, len);
406 chip->flags |= TPM_CHIP_FLAG_IRQ;
407 if (!priv->irq_tested)
409 if (!priv->irq_tested)
410 disable_interrupts(chip);
411 priv->irq_tested = true;
415 struct tis_vendor_timeout_override {
417 unsigned long timeout_us[4];
420 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
422 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
423 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
426 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
427 unsigned long *timeout_cap)
429 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
433 if (chip->ops->clk_enable != NULL)
434 chip->ops->clk_enable(chip, true);
436 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
440 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
441 if (vendor_timeout_overrides[i].did_vid != did_vid)
443 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
444 sizeof(vendor_timeout_overrides[i].timeout_us));
451 if (chip->ops->clk_enable != NULL)
452 chip->ops->clk_enable(chip, false);
458 * Early probing for iTPM with STS_DATA_EXPECT flaw.
459 * Try sending command without itpm flag set and if that
460 * fails, repeat with itpm flag set.
462 static int probe_itpm(struct tpm_chip *chip)
464 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
466 u8 cmd_getticks[] = {
467 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
468 0x00, 0x00, 0x00, 0xf1
470 size_t len = sizeof(cmd_getticks);
473 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
476 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
480 /* probe only iTPMS */
481 if (vendor != TPM_VID_INTEL)
484 if (request_locality(chip, 0) != 0)
487 rc = tpm_tis_send_data(chip, cmd_getticks, len);
493 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
495 rc = tpm_tis_send_data(chip, cmd_getticks, len);
497 dev_info(&chip->dev, "Detected an iTPM.\n");
499 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
505 release_locality(chip, priv->locality);
510 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
512 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
514 switch (priv->manufacturer_id) {
515 case TPM_VID_WINBOND:
516 return ((status == TPM_STS_VALID) ||
517 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
519 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
521 return (status == TPM_STS_COMMAND_READY);
525 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
527 struct tpm_chip *chip = dev_id;
528 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
532 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
539 priv->irq_tested = true;
540 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
541 wake_up_interruptible(&priv->read_queue);
542 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
543 for (i = 0; i < 5; i++)
544 if (check_locality(chip, i))
547 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
548 TPM_INTF_CMD_READY_INT))
549 wake_up_interruptible(&priv->int_queue);
551 /* Clear interrupts handled with TPM_EOI */
552 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
556 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
560 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
562 const char *desc = "attempting to generate an interrupt";
566 if (chip->flags & TPM_CHIP_FLAG_TPM2)
567 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
569 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
573 /* Register the IRQ and issue a command that will cause an interrupt. If an
574 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
575 * everything and leave in polling mode. Returns 0 on success.
577 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
580 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
585 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
586 dev_name(&chip->dev), chip) != 0) {
587 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
593 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
598 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
602 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
606 /* Clear all existing */
607 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
612 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
613 intmask | TPM_GLOBAL_INT_ENABLE);
617 priv->irq_tested = false;
619 /* Generate an interrupt by having the core call through to
622 rc = tpm_tis_gen_interrupt(chip);
626 /* tpm_tis_send will either confirm the interrupt is working or it
627 * will call disable_irq which undoes all of the above.
629 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
630 rc = tpm_tis_write8(priv, original_int_vec,
631 TPM_INT_VECTOR(priv->locality));
641 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
642 * do not have ACPI/etc. We typically expect the interrupt to be declared if
645 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
647 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
651 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
656 if (!original_int_vec) {
657 if (IS_ENABLED(CONFIG_X86))
658 for (i = 3; i <= 15; i++)
659 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
662 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
667 void tpm_tis_remove(struct tpm_chip *chip)
669 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
670 u32 reg = TPM_INT_ENABLE(priv->locality);
674 tpm_tis_clkrun_enable(chip, true);
676 rc = tpm_tis_read32(priv, reg, &interrupt);
680 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
682 tpm_tis_clkrun_enable(chip, false);
684 if (priv->ilb_base_addr)
685 iounmap(priv->ilb_base_addr);
687 EXPORT_SYMBOL_GPL(tpm_tis_remove);
690 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
691 * of a single TPM command
692 * @chip: TPM chip to use
693 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
694 * 0 - Enable CLKRUN protocol
695 * Call this function directly in tpm_tis_remove() in error or driver removal
696 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
698 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
700 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
703 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
704 !data->ilb_base_addr)
708 data->clkrun_enabled++;
709 if (data->clkrun_enabled > 1)
711 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
713 /* Disable LPC CLKRUN# */
714 clkrun_val &= ~LPC_CLKRUN_EN;
715 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
718 * Write any random value on port 0x80 which is on LPC, to make
719 * sure LPC clock is running before sending any TPM command.
723 data->clkrun_enabled--;
724 if (data->clkrun_enabled)
727 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
729 /* Enable LPC CLKRUN# */
730 clkrun_val |= LPC_CLKRUN_EN;
731 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
734 * Write any random value on port 0x80 which is on LPC, to make
735 * sure LPC clock is running before sending any TPM command.
741 static const struct tpm_class_ops tpm_tis = {
742 .flags = TPM_OPS_AUTO_STARTUP,
743 .status = tpm_tis_status,
744 .recv = tpm_tis_recv,
745 .send = tpm_tis_send,
746 .cancel = tpm_tis_ready,
747 .update_timeouts = tpm_tis_update_timeouts,
748 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
749 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
750 .req_canceled = tpm_tis_req_canceled,
751 .request_locality = request_locality,
752 .relinquish_locality = release_locality,
753 .clk_enable = tpm_tis_clkrun_enable,
756 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
757 const struct tpm_tis_phy_ops *phy_ops,
758 acpi_handle acpi_dev_handle)
760 u32 vendor, intfcaps, intmask;
764 struct tpm_chip *chip;
766 chip = tpmm_chip_alloc(dev, &tpm_tis);
768 return PTR_ERR(chip);
771 chip->acpi_dev_handle = acpi_dev_handle;
774 /* Maximum timeouts */
775 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
776 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
777 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
778 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
779 priv->phy_ops = phy_ops;
780 dev_set_drvdata(&chip->dev, priv);
783 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
785 if (!priv->ilb_base_addr)
788 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
789 /* Check if CLKRUN# is already not enabled in the LPC bus */
790 if (!(clkrun_val & LPC_CLKRUN_EN)) {
791 iounmap(priv->ilb_base_addr);
792 priv->ilb_base_addr = NULL;
796 if (chip->ops->clk_enable != NULL)
797 chip->ops->clk_enable(chip, true);
799 if (wait_startup(chip, 0) != 0) {
804 /* Take control of the TPM's interrupt hardware and shut it off */
805 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
809 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
810 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
811 intmask &= ~TPM_GLOBAL_INT_ENABLE;
813 rc = request_locality(chip, 0);
819 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
820 release_locality(chip, 0);
822 rc = tpm2_probe(chip);
826 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
830 priv->manufacturer_id = vendor;
832 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
836 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
837 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
840 probe = probe_itpm(chip);
846 /* Figure out the capabilities */
847 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
851 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
853 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
854 dev_dbg(dev, "\tBurst Count Static\n");
855 if (intfcaps & TPM_INTF_CMD_READY_INT)
856 dev_dbg(dev, "\tCommand Ready Int Support\n");
857 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
858 dev_dbg(dev, "\tInterrupt Edge Falling\n");
859 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
860 dev_dbg(dev, "\tInterrupt Edge Rising\n");
861 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
862 dev_dbg(dev, "\tInterrupt Level Low\n");
863 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
864 dev_dbg(dev, "\tInterrupt Level High\n");
865 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
866 dev_dbg(dev, "\tLocality Change Int Support\n");
867 if (intfcaps & TPM_INTF_STS_VALID_INT)
868 dev_dbg(dev, "\tSts Valid Int Support\n");
869 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
870 dev_dbg(dev, "\tData Avail Int Support\n");
872 /* INTERRUPT Setup */
873 init_waitqueue_head(&priv->read_queue);
874 init_waitqueue_head(&priv->int_queue);
876 /* Before doing irq testing issue a command to the TPM in polling mode
877 * to make sure it works. May as well use that command to set the
878 * proper timeouts for the driver.
880 if (tpm_get_timeouts(chip)) {
881 dev_err(dev, "Could not get TPM timeouts and durations\n");
887 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
889 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
890 dev_err(&chip->dev, FW_BUG
891 "TPM interrupt not working, polling instead\n");
893 disable_interrupts(chip);
896 tpm_tis_probe_irq(chip, intmask);
900 rc = tpm_chip_register(chip);
904 if (chip->ops->clk_enable != NULL)
905 chip->ops->clk_enable(chip, false);
909 if (chip->ops->clk_enable != NULL)
910 chip->ops->clk_enable(chip, false);
912 tpm_tis_remove(chip);
916 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
918 #ifdef CONFIG_PM_SLEEP
919 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
921 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
925 if (chip->ops->clk_enable != NULL)
926 chip->ops->clk_enable(chip, true);
928 /* reenable interrupts that device may have lost or
929 * BIOS/firmware may have disabled
931 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
935 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
939 intmask |= TPM_INTF_CMD_READY_INT
940 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
941 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
943 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
946 if (chip->ops->clk_enable != NULL)
947 chip->ops->clk_enable(chip, false);
952 int tpm_tis_resume(struct device *dev)
954 struct tpm_chip *chip = dev_get_drvdata(dev);
957 if (chip->flags & TPM_CHIP_FLAG_IRQ)
958 tpm_tis_reenable_interrupts(chip);
960 ret = tpm_pm_resume(dev);
964 /* TPM 1.2 requires self-test on resume. This function actually returns
965 * an error code but for unknown reason it isn't handled.
967 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
968 tpm_do_selftest(chip);
972 EXPORT_SYMBOL_GPL(tpm_tis_resume);
975 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
976 MODULE_DESCRIPTION("TPM Driver");
977 MODULE_VERSION("2.0");
978 MODULE_LICENSE("GPL");