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);
294 if (count < TPM_HEADER_SIZE) {
299 size = recv_data(chip, buf, TPM_HEADER_SIZE);
300 /* read first 10 bytes, including tag, paramsize, and result */
301 if (size < TPM_HEADER_SIZE) {
302 dev_err(&chip->dev, "Unable to read header\n");
306 expected = be32_to_cpu(*(__be32 *) (buf + 2));
307 if (expected > count || expected < TPM_HEADER_SIZE) {
312 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
313 expected - TPM_HEADER_SIZE);
314 if (size < expected) {
315 dev_err(&chip->dev, "Unable to read remainder of result\n");
320 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
321 &priv->int_queue, false) < 0) {
325 status = tpm_tis_status(chip);
326 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
327 dev_err(&chip->dev, "Error left over data\n");
332 rc = tpm_tis_verify_crc(priv, (size_t)size, buf);
334 dev_err(&chip->dev, "CRC mismatch for response.\n");
345 * If interrupts are used (signaled by an irq set in the vendor structure)
346 * tpm.c can skip polling for the data to be available as the interrupt is
349 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
351 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
352 int rc, status, burstcnt;
354 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
356 status = tpm_tis_status(chip);
357 if ((status & TPM_STS_COMMAND_READY) == 0) {
359 if (wait_for_tpm_stat
360 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
361 &priv->int_queue, false) < 0) {
367 while (count < len - 1) {
368 burstcnt = get_burstcount(chip);
370 dev_err(&chip->dev, "Unable to read burstcount\n");
374 burstcnt = min_t(int, burstcnt, len - count - 1);
375 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
376 burstcnt, buf + count);
382 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
383 &priv->int_queue, false) < 0) {
387 status = tpm_tis_status(chip);
388 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
394 /* write last byte */
395 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
399 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
400 &priv->int_queue, false) < 0) {
404 status = tpm_tis_status(chip);
405 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
417 static void disable_interrupts(struct tpm_chip *chip)
419 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
426 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
430 intmask &= ~TPM_GLOBAL_INT_ENABLE;
431 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
433 devm_free_irq(chip->dev.parent, priv->irq, chip);
435 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
439 * If interrupts are used (signaled by an irq set in the vendor structure)
440 * tpm.c can skip polling for the data to be available as the interrupt is
443 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
445 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
450 rc = tpm_tis_send_data(chip, buf, len);
454 rc = tpm_tis_verify_crc(priv, len, buf);
456 dev_err(&chip->dev, "CRC mismatch for command.\n");
461 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
465 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
466 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
468 dur = tpm_calc_ordinal_duration(chip, ordinal);
469 if (wait_for_tpm_stat
470 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
471 &priv->read_queue, false) < 0) {
482 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
485 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
487 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
488 return tpm_tis_send_main(chip, buf, len);
490 /* Verify receipt of the expected IRQ */
493 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
494 rc = tpm_tis_send_main(chip, buf, len);
496 chip->flags |= TPM_CHIP_FLAG_IRQ;
497 if (!priv->irq_tested)
499 if (!priv->irq_tested)
500 disable_interrupts(chip);
501 priv->irq_tested = true;
505 struct tis_vendor_durations_override {
507 struct tpm1_version version;
508 unsigned long durations[3];
511 static const struct tis_vendor_durations_override vendor_dur_overrides[] = {
512 /* STMicroelectronics 0x104a */
515 { (2 * 60 * HZ), (2 * 60 * HZ), (2 * 60 * HZ) } },
518 static void tpm_tis_update_durations(struct tpm_chip *chip,
519 unsigned long *duration_cap)
521 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
522 struct tpm1_version *version;
527 chip->duration_adjusted = false;
529 if (chip->ops->clk_enable != NULL)
530 chip->ops->clk_enable(chip, true);
532 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
534 dev_warn(&chip->dev, "%s: failed to read did_vid. %d\n",
539 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
540 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
541 "attempting to determine the 1.2 version",
542 sizeof(cap.version2));
544 version = &cap.version2.version;
546 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
547 "attempting to determine the 1.1 version",
548 sizeof(cap.version1));
553 version = &cap.version1;
556 for (i = 0; i != ARRAY_SIZE(vendor_dur_overrides); i++) {
557 if (vendor_dur_overrides[i].did_vid != did_vid)
560 if ((version->major ==
561 vendor_dur_overrides[i].version.major) &&
563 vendor_dur_overrides[i].version.minor) &&
564 (version->rev_major ==
565 vendor_dur_overrides[i].version.rev_major) &&
566 (version->rev_minor ==
567 vendor_dur_overrides[i].version.rev_minor)) {
570 vendor_dur_overrides[i].durations,
571 sizeof(vendor_dur_overrides[i].durations));
573 chip->duration_adjusted = true;
579 if (chip->ops->clk_enable != NULL)
580 chip->ops->clk_enable(chip, false);
583 struct tis_vendor_timeout_override {
585 unsigned long timeout_us[4];
588 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
590 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
591 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
594 static void tpm_tis_update_timeouts(struct tpm_chip *chip,
595 unsigned long *timeout_cap)
597 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
601 chip->timeout_adjusted = false;
603 if (chip->ops->clk_enable != NULL)
604 chip->ops->clk_enable(chip, true);
606 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
608 dev_warn(&chip->dev, "%s: failed to read did_vid: %d\n",
613 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
614 if (vendor_timeout_overrides[i].did_vid != did_vid)
616 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
617 sizeof(vendor_timeout_overrides[i].timeout_us));
618 chip->timeout_adjusted = true;
622 if (chip->ops->clk_enable != NULL)
623 chip->ops->clk_enable(chip, false);
629 * Early probing for iTPM with STS_DATA_EXPECT flaw.
630 * Try sending command without itpm flag set and if that
631 * fails, repeat with itpm flag set.
633 static int probe_itpm(struct tpm_chip *chip)
635 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
637 static const u8 cmd_getticks[] = {
638 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
639 0x00, 0x00, 0x00, 0xf1
641 size_t len = sizeof(cmd_getticks);
644 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
647 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
651 /* probe only iTPMS */
652 if (vendor != TPM_VID_INTEL)
655 if (request_locality(chip, 0) != 0)
658 rc = tpm_tis_send_data(chip, cmd_getticks, len);
664 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
666 rc = tpm_tis_send_data(chip, cmd_getticks, len);
668 dev_info(&chip->dev, "Detected an iTPM.\n");
670 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
676 release_locality(chip, priv->locality);
681 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
683 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
685 switch (priv->manufacturer_id) {
686 case TPM_VID_WINBOND:
687 return ((status == TPM_STS_VALID) ||
688 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
690 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
692 return (status == TPM_STS_COMMAND_READY);
696 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
698 struct tpm_chip *chip = dev_id;
699 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
703 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
710 priv->irq_tested = true;
711 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
712 wake_up_interruptible(&priv->read_queue);
713 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
714 for (i = 0; i < 5; i++)
715 if (check_locality(chip, i))
718 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
719 TPM_INTF_CMD_READY_INT))
720 wake_up_interruptible(&priv->int_queue);
722 /* Clear interrupts handled with TPM_EOI */
723 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
727 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
731 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
733 const char *desc = "attempting to generate an interrupt";
738 ret = request_locality(chip, 0);
742 if (chip->flags & TPM_CHIP_FLAG_TPM2)
743 ret = tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
745 ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0);
747 release_locality(chip, 0);
752 /* Register the IRQ and issue a command that will cause an interrupt. If an
753 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
754 * everything and leave in polling mode. Returns 0 on success.
756 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
759 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
764 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
765 dev_name(&chip->dev), chip) != 0) {
766 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
772 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
777 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
781 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
785 /* Clear all existing */
786 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
791 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
792 intmask | TPM_GLOBAL_INT_ENABLE);
796 priv->irq_tested = false;
798 /* Generate an interrupt by having the core call through to
801 rc = tpm_tis_gen_interrupt(chip);
805 /* tpm_tis_send will either confirm the interrupt is working or it
806 * will call disable_irq which undoes all of the above.
808 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
809 rc = tpm_tis_write8(priv, original_int_vec,
810 TPM_INT_VECTOR(priv->locality));
820 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
821 * do not have ACPI/etc. We typically expect the interrupt to be declared if
824 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
826 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
830 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
835 if (!original_int_vec) {
836 if (IS_ENABLED(CONFIG_X86))
837 for (i = 3; i <= 15; i++)
838 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
841 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
846 void tpm_tis_remove(struct tpm_chip *chip)
848 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
849 u32 reg = TPM_INT_ENABLE(priv->locality);
853 tpm_tis_clkrun_enable(chip, true);
855 rc = tpm_tis_read32(priv, reg, &interrupt);
859 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
861 tpm_tis_clkrun_enable(chip, false);
863 if (priv->ilb_base_addr)
864 iounmap(priv->ilb_base_addr);
866 EXPORT_SYMBOL_GPL(tpm_tis_remove);
869 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
870 * of a single TPM command
871 * @chip: TPM chip to use
872 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
873 * 0 - Enable CLKRUN protocol
874 * Call this function directly in tpm_tis_remove() in error or driver removal
875 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
877 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
879 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
882 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
883 !data->ilb_base_addr)
887 data->clkrun_enabled++;
888 if (data->clkrun_enabled > 1)
890 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
892 /* Disable LPC CLKRUN# */
893 clkrun_val &= ~LPC_CLKRUN_EN;
894 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
897 * Write any random value on port 0x80 which is on LPC, to make
898 * sure LPC clock is running before sending any TPM command.
902 data->clkrun_enabled--;
903 if (data->clkrun_enabled)
906 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
908 /* Enable LPC CLKRUN# */
909 clkrun_val |= LPC_CLKRUN_EN;
910 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
913 * Write any random value on port 0x80 which is on LPC, to make
914 * sure LPC clock is running before sending any TPM command.
920 static const struct tpm_class_ops tpm_tis = {
921 .flags = TPM_OPS_AUTO_STARTUP,
922 .status = tpm_tis_status,
923 .recv = tpm_tis_recv,
924 .send = tpm_tis_send,
925 .cancel = tpm_tis_ready,
926 .update_timeouts = tpm_tis_update_timeouts,
927 .update_durations = tpm_tis_update_durations,
928 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
929 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
930 .req_canceled = tpm_tis_req_canceled,
931 .request_locality = request_locality,
932 .relinquish_locality = release_locality,
933 .clk_enable = tpm_tis_clkrun_enable,
936 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
937 const struct tpm_tis_phy_ops *phy_ops,
938 acpi_handle acpi_dev_handle)
946 struct tpm_chip *chip;
948 chip = tpmm_chip_alloc(dev, &tpm_tis);
950 return PTR_ERR(chip);
953 chip->acpi_dev_handle = acpi_dev_handle;
956 chip->hwrng.quality = priv->rng_quality;
958 /* Maximum timeouts */
959 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
960 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
961 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
962 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
963 priv->timeout_min = TPM_TIMEOUT_USECS_MIN;
964 priv->timeout_max = TPM_TIMEOUT_USECS_MAX;
965 priv->phy_ops = phy_ops;
967 dev_set_drvdata(&chip->dev, priv);
969 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
973 priv->manufacturer_id = vendor;
975 if (priv->manufacturer_id == TPM_VID_ATML &&
976 !(chip->flags & TPM_CHIP_FLAG_TPM2)) {
977 priv->timeout_min = TIS_TIMEOUT_MIN_ATML;
978 priv->timeout_max = TIS_TIMEOUT_MAX_ATML;
982 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
984 if (!priv->ilb_base_addr)
987 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
988 /* Check if CLKRUN# is already not enabled in the LPC bus */
989 if (!(clkrun_val & LPC_CLKRUN_EN)) {
990 iounmap(priv->ilb_base_addr);
991 priv->ilb_base_addr = NULL;
995 if (chip->ops->clk_enable != NULL)
996 chip->ops->clk_enable(chip, true);
998 if (wait_startup(chip, 0) != 0) {
1003 /* Take control of the TPM's interrupt hardware and shut it off */
1004 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1008 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
1009 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
1010 intmask &= ~TPM_GLOBAL_INT_ENABLE;
1012 rc = request_locality(chip, 0);
1018 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1019 release_locality(chip, 0);
1021 rc = tpm_chip_start(chip);
1024 rc = tpm2_probe(chip);
1025 tpm_chip_stop(chip);
1029 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
1033 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
1034 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
1037 probe = probe_itpm(chip);
1043 /* Figure out the capabilities */
1044 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
1048 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
1050 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
1051 dev_dbg(dev, "\tBurst Count Static\n");
1052 if (intfcaps & TPM_INTF_CMD_READY_INT)
1053 dev_dbg(dev, "\tCommand Ready Int Support\n");
1054 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
1055 dev_dbg(dev, "\tInterrupt Edge Falling\n");
1056 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
1057 dev_dbg(dev, "\tInterrupt Edge Rising\n");
1058 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
1059 dev_dbg(dev, "\tInterrupt Level Low\n");
1060 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
1061 dev_dbg(dev, "\tInterrupt Level High\n");
1062 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
1063 dev_dbg(dev, "\tLocality Change Int Support\n");
1064 if (intfcaps & TPM_INTF_STS_VALID_INT)
1065 dev_dbg(dev, "\tSts Valid Int Support\n");
1066 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
1067 dev_dbg(dev, "\tData Avail Int Support\n");
1069 /* INTERRUPT Setup */
1070 init_waitqueue_head(&priv->read_queue);
1071 init_waitqueue_head(&priv->int_queue);
1074 * Before doing irq testing issue a command to the TPM in polling mode
1075 * to make sure it works. May as well use that command to set the
1076 * proper timeouts for the driver.
1079 rc = request_locality(chip, 0);
1083 rc = tpm_get_timeouts(chip);
1085 release_locality(chip, 0);
1088 dev_err(dev, "Could not get TPM timeouts and durations\n");
1094 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
1096 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
1097 dev_err(&chip->dev, FW_BUG
1098 "TPM interrupt not working, polling instead\n");
1100 disable_interrupts(chip);
1103 tpm_tis_probe_irq(chip, intmask);
1107 rc = tpm_chip_register(chip);
1111 if (chip->ops->clk_enable != NULL)
1112 chip->ops->clk_enable(chip, false);
1116 if (chip->ops->clk_enable != NULL)
1117 chip->ops->clk_enable(chip, false);
1119 tpm_tis_remove(chip);
1123 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
1125 #ifdef CONFIG_PM_SLEEP
1126 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
1128 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
1132 if (chip->ops->clk_enable != NULL)
1133 chip->ops->clk_enable(chip, true);
1135 /* reenable interrupts that device may have lost or
1136 * BIOS/firmware may have disabled
1138 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
1142 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1146 intmask |= TPM_INTF_CMD_READY_INT
1147 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
1148 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
1150 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1153 if (chip->ops->clk_enable != NULL)
1154 chip->ops->clk_enable(chip, false);
1159 int tpm_tis_resume(struct device *dev)
1161 struct tpm_chip *chip = dev_get_drvdata(dev);
1164 if (chip->flags & TPM_CHIP_FLAG_IRQ)
1165 tpm_tis_reenable_interrupts(chip);
1167 ret = tpm_pm_resume(dev);
1172 * TPM 1.2 requires self-test on resume. This function actually returns
1173 * an error code but for unknown reason it isn't handled.
1175 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
1176 ret = request_locality(chip, 0);
1180 tpm1_do_selftest(chip);
1182 release_locality(chip, 0);
1187 EXPORT_SYMBOL_GPL(tpm_tis_resume);
1191 MODULE_DESCRIPTION("TPM Driver");
1192 MODULE_VERSION("2.0");
1193 MODULE_LICENSE("GPL");