2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014, 2015 Intel Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
14 * This device driver implements the TPM interface as defined in
15 * the TCG TPM Interface Spec version 1.2, revision 1.0.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation, version 2 of the
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/pnp.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/acpi.h>
30 #include <linux/freezer.h>
32 #include "tpm_tis_core.h"
34 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value);
36 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
37 bool check_cancel, bool *canceled)
39 u8 status = chip->ops->status(chip);
42 if ((status & mask) == mask)
44 if (check_cancel && chip->ops->req_canceled(chip, status)) {
51 static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
52 unsigned long timeout, wait_queue_head_t *queue,
58 bool canceled = false;
60 /* check current status */
61 status = chip->ops->status(chip);
62 if ((status & mask) == mask)
65 stop = jiffies + timeout;
67 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
69 timeout = stop - jiffies;
70 if ((long)timeout <= 0)
72 rc = wait_event_interruptible_timeout(*queue,
73 wait_for_tpm_stat_cond(chip, mask, check_cancel,
81 if (rc == -ERESTARTSYS && freezing(current)) {
82 clear_thread_flag(TIF_SIGPENDING);
87 usleep_range(TPM_TIMEOUT_USECS_MIN,
88 TPM_TIMEOUT_USECS_MAX);
89 status = chip->ops->status(chip);
90 if ((status & mask) == mask)
92 } while (time_before(jiffies, stop));
97 /* Before we attempt to access the TPM we must see that the valid bit is set.
98 * The specification says that this bit is 0 at reset and remains 0 until the
99 * 'TPM has gone through its self test and initialization and has established
100 * correct values in the other bits.'
102 static int wait_startup(struct tpm_chip *chip, int l)
104 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
105 unsigned long stop = jiffies + chip->timeout_a;
111 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
115 if (access & TPM_ACCESS_VALID)
117 tpm_msleep(TPM_TIMEOUT);
118 } while (time_before(jiffies, stop));
122 static bool check_locality(struct tpm_chip *chip, int l)
124 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
128 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
132 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID
133 | TPM_ACCESS_REQUEST_USE)) ==
134 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
142 static int release_locality(struct tpm_chip *chip, int l)
144 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
146 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
151 static int request_locality(struct tpm_chip *chip, int l)
153 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
154 unsigned long stop, timeout;
157 if (check_locality(chip, l))
160 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
164 stop = jiffies + chip->timeout_a;
166 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
168 timeout = stop - jiffies;
169 if ((long)timeout <= 0)
171 rc = wait_event_interruptible_timeout(priv->int_queue,
177 if (rc == -ERESTARTSYS && freezing(current)) {
178 clear_thread_flag(TIF_SIGPENDING);
182 /* wait for burstcount */
184 if (check_locality(chip, l))
186 tpm_msleep(TPM_TIMEOUT);
187 } while (time_before(jiffies, stop));
192 static u8 tpm_tis_status(struct tpm_chip *chip)
194 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
198 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
205 static void tpm_tis_ready(struct tpm_chip *chip)
207 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
209 /* this causes the current command to be aborted */
210 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
213 static int get_burstcount(struct tpm_chip *chip)
215 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
220 /* wait for burstcount */
221 if (chip->flags & TPM_CHIP_FLAG_TPM2)
222 stop = jiffies + chip->timeout_a;
224 stop = jiffies + chip->timeout_d;
226 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
230 burstcnt = (value >> 8) & 0xFFFF;
233 usleep_range(TPM_TIMEOUT_USECS_MIN, TPM_TIMEOUT_USECS_MAX);
234 } while (time_before(jiffies, stop));
238 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
240 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
241 int size = 0, burstcnt, rc;
243 while (size < count) {
244 rc = wait_for_tpm_stat(chip,
245 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
247 &priv->read_queue, true);
250 burstcnt = get_burstcount(chip);
252 dev_err(&chip->dev, "Unable to read burstcount\n");
255 burstcnt = min_t(int, burstcnt, count - size);
257 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
258 burstcnt, buf + size);
267 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
269 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
275 if (count < TPM_HEADER_SIZE) {
280 size = recv_data(chip, buf, TPM_HEADER_SIZE);
281 /* read first 10 bytes, including tag, paramsize, and result */
282 if (size < TPM_HEADER_SIZE) {
283 dev_err(&chip->dev, "Unable to read header\n");
287 expected = be32_to_cpu(*(__be32 *) (buf + 2));
288 if (expected > count || expected < TPM_HEADER_SIZE) {
293 rc = recv_data(chip, &buf[TPM_HEADER_SIZE],
294 expected - TPM_HEADER_SIZE);
300 if (size < expected) {
301 dev_err(&chip->dev, "Unable to read remainder of result\n");
306 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
307 &priv->int_queue, false) < 0) {
311 status = tpm_tis_status(chip);
312 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
313 dev_err(&chip->dev, "Error left over data\n");
324 * If interrupts are used (signaled by an irq set in the vendor structure)
325 * tpm.c can skip polling for the data to be available as the interrupt is
328 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
330 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
331 int rc, status, burstcnt;
333 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
335 status = tpm_tis_status(chip);
336 if ((status & TPM_STS_COMMAND_READY) == 0) {
338 if (wait_for_tpm_stat
339 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
340 &priv->int_queue, false) < 0) {
346 while (count < len - 1) {
347 burstcnt = get_burstcount(chip);
349 dev_err(&chip->dev, "Unable to read burstcount\n");
353 burstcnt = min_t(int, burstcnt, len - count - 1);
354 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
355 burstcnt, buf + count);
361 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
362 &priv->int_queue, false) < 0) {
366 status = tpm_tis_status(chip);
367 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
373 /* write last byte */
374 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
378 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
379 &priv->int_queue, false) < 0) {
383 status = tpm_tis_status(chip);
384 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
396 static void disable_interrupts(struct tpm_chip *chip)
398 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
405 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
409 intmask &= ~TPM_GLOBAL_INT_ENABLE;
410 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
412 devm_free_irq(chip->dev.parent, priv->irq, chip);
414 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
418 * If interrupts are used (signaled by an irq set in the vendor structure)
419 * tpm.c can skip polling for the data to be available as the interrupt is
422 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
424 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
430 for (try = 0; try < TPM_RETRY; try++) {
431 rc = tpm_tis_send_data(chip, buf, len);
433 /* Data transfer done successfully */
436 /* Data transfer failed, not recoverable */
441 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
445 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
446 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
448 if (chip->flags & TPM_CHIP_FLAG_TPM2)
449 dur = tpm2_calc_ordinal_duration(chip, ordinal);
451 dur = tpm_calc_ordinal_duration(chip, ordinal);
453 if (wait_for_tpm_stat
454 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
455 &priv->read_queue, false) < 0) {
466 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
469 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
471 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
472 return tpm_tis_send_main(chip, buf, len);
474 /* Verify receipt of the expected IRQ */
477 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
478 rc = tpm_tis_send_main(chip, buf, len);
480 chip->flags |= TPM_CHIP_FLAG_IRQ;
481 if (!priv->irq_tested)
483 if (!priv->irq_tested)
484 disable_interrupts(chip);
485 priv->irq_tested = true;
489 struct tis_vendor_timeout_override {
491 unsigned long timeout_us[4];
494 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
496 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
497 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
500 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
501 unsigned long *timeout_cap)
503 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
507 if (chip->ops->clk_enable != NULL)
508 chip->ops->clk_enable(chip, true);
510 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
514 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
515 if (vendor_timeout_overrides[i].did_vid != did_vid)
517 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
518 sizeof(vendor_timeout_overrides[i].timeout_us));
525 if (chip->ops->clk_enable != NULL)
526 chip->ops->clk_enable(chip, false);
532 * Early probing for iTPM with STS_DATA_EXPECT flaw.
533 * Try sending command without itpm flag set and if that
534 * fails, repeat with itpm flag set.
536 static int probe_itpm(struct tpm_chip *chip)
538 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
540 static const u8 cmd_getticks[] = {
541 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
542 0x00, 0x00, 0x00, 0xf1
544 size_t len = sizeof(cmd_getticks);
547 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
550 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
554 /* probe only iTPMS */
555 if (vendor != TPM_VID_INTEL)
558 if (request_locality(chip, 0) != 0)
561 rc = tpm_tis_send_data(chip, cmd_getticks, len);
567 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
569 rc = tpm_tis_send_data(chip, cmd_getticks, len);
571 dev_info(&chip->dev, "Detected an iTPM.\n");
573 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
579 release_locality(chip, priv->locality);
584 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
586 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
588 switch (priv->manufacturer_id) {
589 case TPM_VID_WINBOND:
590 return ((status == TPM_STS_VALID) ||
591 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
593 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
595 return (status == TPM_STS_COMMAND_READY);
599 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
601 struct tpm_chip *chip = dev_id;
602 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
606 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
613 priv->irq_tested = true;
614 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
615 wake_up_interruptible(&priv->read_queue);
616 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
617 for (i = 0; i < 5; i++)
618 if (check_locality(chip, i))
621 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
622 TPM_INTF_CMD_READY_INT))
623 wake_up_interruptible(&priv->int_queue);
625 /* Clear interrupts handled with TPM_EOI */
626 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
630 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
634 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
636 const char *desc = "attempting to generate an interrupt";
640 if (chip->flags & TPM_CHIP_FLAG_TPM2)
641 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
643 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
647 /* Register the IRQ and issue a command that will cause an interrupt. If an
648 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
649 * everything and leave in polling mode. Returns 0 on success.
651 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
654 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
659 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
660 dev_name(&chip->dev), chip) != 0) {
661 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
667 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
672 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
676 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
680 /* Clear all existing */
681 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
686 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
687 intmask | TPM_GLOBAL_INT_ENABLE);
691 priv->irq_tested = false;
693 /* Generate an interrupt by having the core call through to
696 rc = tpm_tis_gen_interrupt(chip);
700 /* tpm_tis_send will either confirm the interrupt is working or it
701 * will call disable_irq which undoes all of the above.
703 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
704 rc = tpm_tis_write8(priv, original_int_vec,
705 TPM_INT_VECTOR(priv->locality));
715 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
716 * do not have ACPI/etc. We typically expect the interrupt to be declared if
719 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
721 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
725 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
730 if (!original_int_vec) {
731 if (IS_ENABLED(CONFIG_X86))
732 for (i = 3; i <= 15; i++)
733 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
736 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
741 void tpm_tis_remove(struct tpm_chip *chip)
743 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
744 u32 reg = TPM_INT_ENABLE(priv->locality);
748 tpm_tis_clkrun_enable(chip, true);
750 rc = tpm_tis_read32(priv, reg, &interrupt);
754 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
756 tpm_tis_clkrun_enable(chip, false);
758 if (priv->ilb_base_addr)
759 iounmap(priv->ilb_base_addr);
761 EXPORT_SYMBOL_GPL(tpm_tis_remove);
764 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
765 * of a single TPM command
766 * @chip: TPM chip to use
767 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
768 * 0 - Enable CLKRUN protocol
769 * Call this function directly in tpm_tis_remove() in error or driver removal
770 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
772 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
774 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
777 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
778 !data->ilb_base_addr)
782 data->clkrun_enabled++;
783 if (data->clkrun_enabled > 1)
785 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
787 /* Disable LPC CLKRUN# */
788 clkrun_val &= ~LPC_CLKRUN_EN;
789 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
792 * Write any random value on port 0x80 which is on LPC, to make
793 * sure LPC clock is running before sending any TPM command.
797 data->clkrun_enabled--;
798 if (data->clkrun_enabled)
801 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
803 /* Enable LPC CLKRUN# */
804 clkrun_val |= LPC_CLKRUN_EN;
805 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
808 * Write any random value on port 0x80 which is on LPC, to make
809 * sure LPC clock is running before sending any TPM command.
815 static const struct tpm_class_ops tpm_tis = {
816 .flags = TPM_OPS_AUTO_STARTUP,
817 .status = tpm_tis_status,
818 .recv = tpm_tis_recv,
819 .send = tpm_tis_send,
820 .cancel = tpm_tis_ready,
821 .update_timeouts = tpm_tis_update_timeouts,
822 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
823 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
824 .req_canceled = tpm_tis_req_canceled,
825 .request_locality = request_locality,
826 .relinquish_locality = release_locality,
827 .clk_enable = tpm_tis_clkrun_enable,
830 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
831 const struct tpm_tis_phy_ops *phy_ops,
832 acpi_handle acpi_dev_handle)
840 struct tpm_chip *chip;
842 chip = tpmm_chip_alloc(dev, &tpm_tis);
844 return PTR_ERR(chip);
847 chip->acpi_dev_handle = acpi_dev_handle;
850 chip->hwrng.quality = priv->rng_quality;
852 /* Maximum timeouts */
853 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
854 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
855 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
856 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
857 priv->phy_ops = phy_ops;
858 dev_set_drvdata(&chip->dev, priv);
861 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
863 if (!priv->ilb_base_addr)
866 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
867 /* Check if CLKRUN# is already not enabled in the LPC bus */
868 if (!(clkrun_val & LPC_CLKRUN_EN)) {
869 iounmap(priv->ilb_base_addr);
870 priv->ilb_base_addr = NULL;
874 if (chip->ops->clk_enable != NULL)
875 chip->ops->clk_enable(chip, true);
877 if (wait_startup(chip, 0) != 0) {
882 /* Take control of the TPM's interrupt hardware and shut it off */
883 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
887 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
888 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
889 intmask &= ~TPM_GLOBAL_INT_ENABLE;
891 rc = request_locality(chip, 0);
897 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
898 release_locality(chip, 0);
900 rc = tpm2_probe(chip);
904 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
908 priv->manufacturer_id = vendor;
910 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
914 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
915 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
918 probe = probe_itpm(chip);
924 /* Figure out the capabilities */
925 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
929 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
931 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
932 dev_dbg(dev, "\tBurst Count Static\n");
933 if (intfcaps & TPM_INTF_CMD_READY_INT)
934 dev_dbg(dev, "\tCommand Ready Int Support\n");
935 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
936 dev_dbg(dev, "\tInterrupt Edge Falling\n");
937 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
938 dev_dbg(dev, "\tInterrupt Edge Rising\n");
939 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
940 dev_dbg(dev, "\tInterrupt Level Low\n");
941 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
942 dev_dbg(dev, "\tInterrupt Level High\n");
943 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
944 dev_dbg(dev, "\tLocality Change Int Support\n");
945 if (intfcaps & TPM_INTF_STS_VALID_INT)
946 dev_dbg(dev, "\tSts Valid Int Support\n");
947 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
948 dev_dbg(dev, "\tData Avail Int Support\n");
950 /* INTERRUPT Setup */
951 init_waitqueue_head(&priv->read_queue);
952 init_waitqueue_head(&priv->int_queue);
954 /* Before doing irq testing issue a command to the TPM in polling mode
955 * to make sure it works. May as well use that command to set the
956 * proper timeouts for the driver.
958 if (tpm_get_timeouts(chip)) {
959 dev_err(dev, "Could not get TPM timeouts and durations\n");
965 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
967 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
968 dev_err(&chip->dev, FW_BUG
969 "TPM interrupt not working, polling instead\n");
971 disable_interrupts(chip);
974 tpm_tis_probe_irq(chip, intmask);
978 rc = tpm_chip_register(chip);
982 if (chip->ops->clk_enable != NULL)
983 chip->ops->clk_enable(chip, false);
987 if (chip->ops->clk_enable != NULL)
988 chip->ops->clk_enable(chip, false);
990 tpm_tis_remove(chip);
994 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
996 #ifdef CONFIG_PM_SLEEP
997 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
999 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
1003 if (chip->ops->clk_enable != NULL)
1004 chip->ops->clk_enable(chip, true);
1006 /* reenable interrupts that device may have lost or
1007 * BIOS/firmware may have disabled
1009 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
1013 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1017 intmask |= TPM_INTF_CMD_READY_INT
1018 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
1019 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
1021 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1024 if (chip->ops->clk_enable != NULL)
1025 chip->ops->clk_enable(chip, false);
1030 int tpm_tis_resume(struct device *dev)
1032 struct tpm_chip *chip = dev_get_drvdata(dev);
1035 if (chip->flags & TPM_CHIP_FLAG_IRQ)
1036 tpm_tis_reenable_interrupts(chip);
1038 ret = tpm_pm_resume(dev);
1042 /* TPM 1.2 requires self-test on resume. This function actually returns
1043 * an error code but for unknown reason it isn't handled.
1045 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1046 tpm_do_selftest(chip);
1050 EXPORT_SYMBOL_GPL(tpm_tis_resume);
1053 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1054 MODULE_DESCRIPTION("TPM Driver");
1055 MODULE_VERSION("2.0");
1056 MODULE_LICENSE("GPL");