1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005, 2006 IBM Corporation
4 * Copyright (C) 2014, 2015 Intel Corporation
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,
51 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
55 bool canceled = false;
57 /* check current status */
58 status = chip->ops->status(chip);
59 if ((status & mask) == mask)
62 stop = jiffies + timeout;
64 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
66 timeout = stop - jiffies;
67 if ((long)timeout <= 0)
69 rc = wait_event_interruptible_timeout(*queue,
70 wait_for_tpm_stat_cond(chip, mask, check_cancel,
78 if (rc == -ERESTARTSYS && freezing(current)) {
79 clear_thread_flag(TIF_SIGPENDING);
84 usleep_range(priv->timeout_min,
86 status = chip->ops->status(chip);
87 if ((status & mask) == mask)
89 } while (time_before(jiffies, stop));
94 /* Before we attempt to access the TPM we must see that the valid bit is set.
95 * The specification says that this bit is 0 at reset and remains 0 until the
96 * 'TPM has gone through its self test and initialization and has established
97 * correct values in the other bits.'
99 static int wait_startup(struct tpm_chip *chip, int l)
101 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
102 unsigned long stop = jiffies + chip->timeout_a;
108 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
112 if (access & TPM_ACCESS_VALID)
114 tpm_msleep(TPM_TIMEOUT);
115 } while (time_before(jiffies, stop));
119 static bool check_locality(struct tpm_chip *chip, int l)
121 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
125 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
129 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID
130 | TPM_ACCESS_REQUEST_USE)) ==
131 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
139 static int release_locality(struct tpm_chip *chip, int l)
141 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
143 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
148 static int request_locality(struct tpm_chip *chip, int l)
150 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
151 unsigned long stop, timeout;
154 if (check_locality(chip, l))
157 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
161 stop = jiffies + chip->timeout_a;
163 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
165 timeout = stop - jiffies;
166 if ((long)timeout <= 0)
168 rc = wait_event_interruptible_timeout(priv->int_queue,
174 if (rc == -ERESTARTSYS && freezing(current)) {
175 clear_thread_flag(TIF_SIGPENDING);
179 /* wait for burstcount */
181 if (check_locality(chip, l))
183 tpm_msleep(TPM_TIMEOUT);
184 } while (time_before(jiffies, stop));
189 static u8 tpm_tis_status(struct tpm_chip *chip)
191 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
195 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
199 if (unlikely((status & TPM_STS_READ_ZERO) != 0)) {
200 if (!test_and_set_bit(TPM_TIS_INVALID_STATUS, &priv->flags)) {
202 * If this trips, the chances are the read is
203 * returning 0xff because the locality hasn't been
204 * acquired. Usually because tpm_try_get_ops() hasn't
205 * been called before doing a TPM operation.
207 dev_err(&chip->dev, "invalid TPM_STS.x 0x%02x, dumping stack for forensics\n",
211 * Dump stack for forensics, as invalid TPM_STS.x could be
212 * potentially triggered by impaired tpm_try_get_ops() or
213 * tpm_find_get_ops().
224 static void tpm_tis_ready(struct tpm_chip *chip)
226 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
228 /* this causes the current command to be aborted */
229 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
232 static int get_burstcount(struct tpm_chip *chip)
234 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
239 /* wait for burstcount */
240 if (chip->flags & TPM_CHIP_FLAG_TPM2)
241 stop = jiffies + chip->timeout_a;
243 stop = jiffies + chip->timeout_d;
245 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
249 burstcnt = (value >> 8) & 0xFFFF;
252 usleep_range(TPM_TIMEOUT_USECS_MIN, TPM_TIMEOUT_USECS_MAX);
253 } while (time_before(jiffies, stop));
257 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
259 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
260 int size = 0, burstcnt, rc;
262 while (size < count) {
263 rc = wait_for_tpm_stat(chip,
264 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
266 &priv->read_queue, true);
269 burstcnt = get_burstcount(chip);
271 dev_err(&chip->dev, "Unable to read burstcount\n");
274 burstcnt = min_t(int, burstcnt, count - size);
276 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
277 burstcnt, buf + size);
286 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
288 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
293 if (count < TPM_HEADER_SIZE) {
298 size = recv_data(chip, buf, TPM_HEADER_SIZE);
299 /* read first 10 bytes, including tag, paramsize, and result */
300 if (size < TPM_HEADER_SIZE) {
301 dev_err(&chip->dev, "Unable to read header\n");
305 expected = be32_to_cpu(*(__be32 *) (buf + 2));
306 if (expected > count || expected < TPM_HEADER_SIZE) {
311 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
312 expected - TPM_HEADER_SIZE);
313 if (size < expected) {
314 dev_err(&chip->dev, "Unable to read remainder of result\n");
319 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
320 &priv->int_queue, false) < 0) {
324 status = tpm_tis_status(chip);
325 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
326 dev_err(&chip->dev, "Error left over data\n");
337 * If interrupts are used (signaled by an irq set in the vendor structure)
338 * tpm.c can skip polling for the data to be available as the interrupt is
341 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
343 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
344 int rc, status, burstcnt;
346 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
348 status = tpm_tis_status(chip);
349 if ((status & TPM_STS_COMMAND_READY) == 0) {
351 if (wait_for_tpm_stat
352 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
353 &priv->int_queue, false) < 0) {
359 while (count < len - 1) {
360 burstcnt = get_burstcount(chip);
362 dev_err(&chip->dev, "Unable to read burstcount\n");
366 burstcnt = min_t(int, burstcnt, len - count - 1);
367 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
368 burstcnt, buf + count);
374 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
375 &priv->int_queue, false) < 0) {
379 status = tpm_tis_status(chip);
380 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
386 /* write last byte */
387 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
391 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
392 &priv->int_queue, false) < 0) {
396 status = tpm_tis_status(chip);
397 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
409 static void disable_interrupts(struct tpm_chip *chip)
411 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
418 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
422 intmask &= ~TPM_GLOBAL_INT_ENABLE;
423 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
425 devm_free_irq(chip->dev.parent, priv->irq, chip);
427 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
431 * If interrupts are used (signaled by an irq set in the vendor structure)
432 * tpm.c can skip polling for the data to be available as the interrupt is
435 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
437 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
442 rc = tpm_tis_send_data(chip, buf, len);
447 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
451 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
452 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
454 dur = tpm_calc_ordinal_duration(chip, ordinal);
455 if (wait_for_tpm_stat
456 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
457 &priv->read_queue, false) < 0) {
468 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
471 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
473 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
474 return tpm_tis_send_main(chip, buf, len);
476 /* Verify receipt of the expected IRQ */
479 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
480 rc = tpm_tis_send_main(chip, buf, len);
482 chip->flags |= TPM_CHIP_FLAG_IRQ;
483 if (!priv->irq_tested)
485 if (!priv->irq_tested)
486 disable_interrupts(chip);
487 priv->irq_tested = true;
491 struct tis_vendor_durations_override {
493 struct tpm1_version version;
494 unsigned long durations[3];
497 static const struct tis_vendor_durations_override vendor_dur_overrides[] = {
498 /* STMicroelectronics 0x104a */
501 { (2 * 60 * HZ), (2 * 60 * HZ), (2 * 60 * HZ) } },
504 static void tpm_tis_update_durations(struct tpm_chip *chip,
505 unsigned long *duration_cap)
507 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
508 struct tpm1_version *version;
513 chip->duration_adjusted = false;
515 if (chip->ops->clk_enable != NULL)
516 chip->ops->clk_enable(chip, true);
518 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
520 dev_warn(&chip->dev, "%s: failed to read did_vid. %d\n",
525 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
526 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
527 "attempting to determine the 1.2 version",
528 sizeof(cap.version2));
530 version = &cap.version2.version;
532 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
533 "attempting to determine the 1.1 version",
534 sizeof(cap.version1));
539 version = &cap.version1;
542 for (i = 0; i != ARRAY_SIZE(vendor_dur_overrides); i++) {
543 if (vendor_dur_overrides[i].did_vid != did_vid)
546 if ((version->major ==
547 vendor_dur_overrides[i].version.major) &&
549 vendor_dur_overrides[i].version.minor) &&
550 (version->rev_major ==
551 vendor_dur_overrides[i].version.rev_major) &&
552 (version->rev_minor ==
553 vendor_dur_overrides[i].version.rev_minor)) {
556 vendor_dur_overrides[i].durations,
557 sizeof(vendor_dur_overrides[i].durations));
559 chip->duration_adjusted = true;
565 if (chip->ops->clk_enable != NULL)
566 chip->ops->clk_enable(chip, false);
569 struct tis_vendor_timeout_override {
571 unsigned long timeout_us[4];
574 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
576 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
577 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
580 static void tpm_tis_update_timeouts(struct tpm_chip *chip,
581 unsigned long *timeout_cap)
583 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
587 chip->timeout_adjusted = false;
589 if (chip->ops->clk_enable != NULL)
590 chip->ops->clk_enable(chip, true);
592 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
594 dev_warn(&chip->dev, "%s: failed to read did_vid: %d\n",
599 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
600 if (vendor_timeout_overrides[i].did_vid != did_vid)
602 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
603 sizeof(vendor_timeout_overrides[i].timeout_us));
604 chip->timeout_adjusted = true;
608 if (chip->ops->clk_enable != NULL)
609 chip->ops->clk_enable(chip, false);
615 * Early probing for iTPM with STS_DATA_EXPECT flaw.
616 * Try sending command without itpm flag set and if that
617 * fails, repeat with itpm flag set.
619 static int probe_itpm(struct tpm_chip *chip)
621 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
623 static const u8 cmd_getticks[] = {
624 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
625 0x00, 0x00, 0x00, 0xf1
627 size_t len = sizeof(cmd_getticks);
630 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
633 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
637 /* probe only iTPMS */
638 if (vendor != TPM_VID_INTEL)
641 if (request_locality(chip, 0) != 0)
644 rc = tpm_tis_send_data(chip, cmd_getticks, len);
650 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
652 rc = tpm_tis_send_data(chip, cmd_getticks, len);
654 dev_info(&chip->dev, "Detected an iTPM.\n");
656 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
662 release_locality(chip, priv->locality);
667 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
669 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
671 switch (priv->manufacturer_id) {
672 case TPM_VID_WINBOND:
673 return ((status == TPM_STS_VALID) ||
674 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
676 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
678 return (status == TPM_STS_COMMAND_READY);
682 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
684 struct tpm_chip *chip = dev_id;
685 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
689 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
696 priv->irq_tested = true;
697 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
698 wake_up_interruptible(&priv->read_queue);
699 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
700 for (i = 0; i < 5; i++)
701 if (check_locality(chip, i))
704 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
705 TPM_INTF_CMD_READY_INT))
706 wake_up_interruptible(&priv->int_queue);
708 /* Clear interrupts handled with TPM_EOI */
709 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
713 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
717 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
719 const char *desc = "attempting to generate an interrupt";
724 ret = request_locality(chip, 0);
728 if (chip->flags & TPM_CHIP_FLAG_TPM2)
729 ret = tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
731 ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0);
733 release_locality(chip, 0);
738 /* Register the IRQ and issue a command that will cause an interrupt. If an
739 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
740 * everything and leave in polling mode. Returns 0 on success.
742 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
745 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
750 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
751 dev_name(&chip->dev), chip) != 0) {
752 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
758 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
763 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
767 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
771 /* Clear all existing */
772 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
777 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
778 intmask | TPM_GLOBAL_INT_ENABLE);
782 priv->irq_tested = false;
784 /* Generate an interrupt by having the core call through to
787 rc = tpm_tis_gen_interrupt(chip);
791 /* tpm_tis_send will either confirm the interrupt is working or it
792 * will call disable_irq which undoes all of the above.
794 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
795 rc = tpm_tis_write8(priv, original_int_vec,
796 TPM_INT_VECTOR(priv->locality));
806 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
807 * do not have ACPI/etc. We typically expect the interrupt to be declared if
810 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
812 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
816 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
821 if (!original_int_vec) {
822 if (IS_ENABLED(CONFIG_X86))
823 for (i = 3; i <= 15; i++)
824 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
827 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
832 void tpm_tis_remove(struct tpm_chip *chip)
834 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
835 u32 reg = TPM_INT_ENABLE(priv->locality);
839 tpm_tis_clkrun_enable(chip, true);
841 rc = tpm_tis_read32(priv, reg, &interrupt);
845 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
847 tpm_tis_clkrun_enable(chip, false);
849 if (priv->ilb_base_addr)
850 iounmap(priv->ilb_base_addr);
852 EXPORT_SYMBOL_GPL(tpm_tis_remove);
855 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
856 * of a single TPM command
857 * @chip: TPM chip to use
858 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
859 * 0 - Enable CLKRUN protocol
860 * Call this function directly in tpm_tis_remove() in error or driver removal
861 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
863 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
865 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
868 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
869 !data->ilb_base_addr)
873 data->clkrun_enabled++;
874 if (data->clkrun_enabled > 1)
876 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
878 /* Disable LPC CLKRUN# */
879 clkrun_val &= ~LPC_CLKRUN_EN;
880 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
883 * Write any random value on port 0x80 which is on LPC, to make
884 * sure LPC clock is running before sending any TPM command.
888 data->clkrun_enabled--;
889 if (data->clkrun_enabled)
892 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
894 /* Enable LPC CLKRUN# */
895 clkrun_val |= LPC_CLKRUN_EN;
896 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
899 * Write any random value on port 0x80 which is on LPC, to make
900 * sure LPC clock is running before sending any TPM command.
906 static const struct tpm_class_ops tpm_tis = {
907 .flags = TPM_OPS_AUTO_STARTUP,
908 .status = tpm_tis_status,
909 .recv = tpm_tis_recv,
910 .send = tpm_tis_send,
911 .cancel = tpm_tis_ready,
912 .update_timeouts = tpm_tis_update_timeouts,
913 .update_durations = tpm_tis_update_durations,
914 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
915 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
916 .req_canceled = tpm_tis_req_canceled,
917 .request_locality = request_locality,
918 .relinquish_locality = release_locality,
919 .clk_enable = tpm_tis_clkrun_enable,
922 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
923 const struct tpm_tis_phy_ops *phy_ops,
924 acpi_handle acpi_dev_handle)
932 struct tpm_chip *chip;
934 chip = tpmm_chip_alloc(dev, &tpm_tis);
936 return PTR_ERR(chip);
939 chip->acpi_dev_handle = acpi_dev_handle;
942 chip->hwrng.quality = priv->rng_quality;
944 /* Maximum timeouts */
945 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
946 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
947 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
948 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
949 priv->timeout_min = TPM_TIMEOUT_USECS_MIN;
950 priv->timeout_max = TPM_TIMEOUT_USECS_MAX;
951 priv->phy_ops = phy_ops;
953 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
957 priv->manufacturer_id = vendor;
959 if (priv->manufacturer_id == TPM_VID_ATML &&
960 !(chip->flags & TPM_CHIP_FLAG_TPM2)) {
961 priv->timeout_min = TIS_TIMEOUT_MIN_ATML;
962 priv->timeout_max = TIS_TIMEOUT_MAX_ATML;
965 dev_set_drvdata(&chip->dev, priv);
968 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
970 if (!priv->ilb_base_addr)
973 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
974 /* Check if CLKRUN# is already not enabled in the LPC bus */
975 if (!(clkrun_val & LPC_CLKRUN_EN)) {
976 iounmap(priv->ilb_base_addr);
977 priv->ilb_base_addr = NULL;
981 if (chip->ops->clk_enable != NULL)
982 chip->ops->clk_enable(chip, true);
984 if (wait_startup(chip, 0) != 0) {
989 /* Take control of the TPM's interrupt hardware and shut it off */
990 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
994 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
995 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
996 intmask &= ~TPM_GLOBAL_INT_ENABLE;
997 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
999 rc = tpm_chip_start(chip);
1002 rc = tpm2_probe(chip);
1003 tpm_chip_stop(chip);
1007 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
1011 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
1012 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
1015 probe = probe_itpm(chip);
1021 /* Figure out the capabilities */
1022 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
1026 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
1028 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
1029 dev_dbg(dev, "\tBurst Count Static\n");
1030 if (intfcaps & TPM_INTF_CMD_READY_INT)
1031 dev_dbg(dev, "\tCommand Ready Int Support\n");
1032 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
1033 dev_dbg(dev, "\tInterrupt Edge Falling\n");
1034 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
1035 dev_dbg(dev, "\tInterrupt Edge Rising\n");
1036 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
1037 dev_dbg(dev, "\tInterrupt Level Low\n");
1038 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
1039 dev_dbg(dev, "\tInterrupt Level High\n");
1040 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
1041 dev_dbg(dev, "\tLocality Change Int Support\n");
1042 if (intfcaps & TPM_INTF_STS_VALID_INT)
1043 dev_dbg(dev, "\tSts Valid Int Support\n");
1044 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
1045 dev_dbg(dev, "\tData Avail Int Support\n");
1047 /* INTERRUPT Setup */
1048 init_waitqueue_head(&priv->read_queue);
1049 init_waitqueue_head(&priv->int_queue);
1052 * Before doing irq testing issue a command to the TPM in polling mode
1053 * to make sure it works. May as well use that command to set the
1054 * proper timeouts for the driver.
1057 rc = request_locality(chip, 0);
1061 rc = tpm_get_timeouts(chip);
1063 release_locality(chip, 0);
1066 dev_err(dev, "Could not get TPM timeouts and durations\n");
1072 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
1074 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
1075 dev_err(&chip->dev, FW_BUG
1076 "TPM interrupt not working, polling instead\n");
1078 disable_interrupts(chip);
1081 tpm_tis_probe_irq(chip, intmask);
1085 rc = tpm_chip_register(chip);
1089 if (chip->ops->clk_enable != NULL)
1090 chip->ops->clk_enable(chip, false);
1094 if (chip->ops->clk_enable != NULL)
1095 chip->ops->clk_enable(chip, false);
1097 tpm_tis_remove(chip);
1101 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
1103 #ifdef CONFIG_PM_SLEEP
1104 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
1106 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
1110 if (chip->ops->clk_enable != NULL)
1111 chip->ops->clk_enable(chip, true);
1113 /* reenable interrupts that device may have lost or
1114 * BIOS/firmware may have disabled
1116 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
1120 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1124 intmask |= TPM_INTF_CMD_READY_INT
1125 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
1126 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
1128 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1131 if (chip->ops->clk_enable != NULL)
1132 chip->ops->clk_enable(chip, false);
1137 int tpm_tis_resume(struct device *dev)
1139 struct tpm_chip *chip = dev_get_drvdata(dev);
1142 if (chip->flags & TPM_CHIP_FLAG_IRQ)
1143 tpm_tis_reenable_interrupts(chip);
1145 ret = tpm_pm_resume(dev);
1150 * TPM 1.2 requires self-test on resume. This function actually returns
1151 * an error code but for unknown reason it isn't handled.
1153 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
1154 ret = request_locality(chip, 0);
1158 tpm1_do_selftest(chip);
1160 release_locality(chip, 0);
1165 EXPORT_SYMBOL_GPL(tpm_tis_resume);
1169 MODULE_DESCRIPTION("TPM Driver");
1170 MODULE_VERSION("2.0");
1171 MODULE_LICENSE("GPL");