1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005, 2006 IBM Corporation
4 * Copyright (C) 2014, 2015 Intel Corporation
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
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 u8 tpm_tis_filter_sts_mask(u8 int_mask, u8 sts_mask)
49 if (!(int_mask & TPM_INTF_STS_VALID_INT))
50 sts_mask &= ~TPM_STS_VALID;
52 if (!(int_mask & TPM_INTF_DATA_AVAIL_INT))
53 sts_mask &= ~TPM_STS_DATA_AVAIL;
55 if (!(int_mask & TPM_INTF_CMD_READY_INT))
56 sts_mask &= ~TPM_STS_COMMAND_READY;
61 static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
62 unsigned long timeout, wait_queue_head_t *queue,
65 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
69 bool canceled = false;
73 /* check current status */
74 status = chip->ops->status(chip);
75 if ((status & mask) == mask)
78 sts_mask = mask & (TPM_STS_VALID | TPM_STS_DATA_AVAIL |
79 TPM_STS_COMMAND_READY);
80 /* check what status changes can be handled by irqs */
81 sts_mask = tpm_tis_filter_sts_mask(priv->int_mask, sts_mask);
83 stop = jiffies + timeout;
84 /* process status changes with irq support */
88 timeout = stop - jiffies;
89 if ((long)timeout <= 0)
91 rc = wait_event_interruptible_timeout(*queue,
92 wait_for_tpm_stat_cond(chip, sts_mask, check_cancel,
100 if (rc == -ERESTARTSYS && freezing(current)) {
101 clear_thread_flag(TIF_SIGPENDING);
110 if (!mask) /* all done */
112 /* process status changes without irq support */
114 status = chip->ops->status(chip);
115 if ((status & mask) == mask)
117 usleep_range(priv->timeout_min,
119 } while (time_before(jiffies, stop));
123 /* Before we attempt to access the TPM we must see that the valid bit is set.
124 * The specification says that this bit is 0 at reset and remains 0 until the
125 * 'TPM has gone through its self test and initialization and has established
126 * correct values in the other bits.'
128 static int wait_startup(struct tpm_chip *chip, int l)
130 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
131 unsigned long stop = jiffies + chip->timeout_a;
137 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
141 if (access & TPM_ACCESS_VALID)
143 tpm_msleep(TPM_TIMEOUT);
144 } while (time_before(jiffies, stop));
148 static bool check_locality(struct tpm_chip *chip, int l)
150 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
154 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
158 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID
159 | TPM_ACCESS_REQUEST_USE)) ==
160 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
168 static int __tpm_tis_relinquish_locality(struct tpm_tis_data *priv, int l)
170 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
175 static int tpm_tis_relinquish_locality(struct tpm_chip *chip, int l)
177 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
179 mutex_lock(&priv->locality_count_mutex);
180 priv->locality_count--;
181 if (priv->locality_count == 0)
182 __tpm_tis_relinquish_locality(priv, l);
183 mutex_unlock(&priv->locality_count_mutex);
188 static int __tpm_tis_request_locality(struct tpm_chip *chip, int l)
190 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
191 unsigned long stop, timeout;
194 if (check_locality(chip, l))
197 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
201 stop = jiffies + chip->timeout_a;
203 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
205 timeout = stop - jiffies;
206 if ((long)timeout <= 0)
208 rc = wait_event_interruptible_timeout(priv->int_queue,
214 if (rc == -ERESTARTSYS && freezing(current)) {
215 clear_thread_flag(TIF_SIGPENDING);
219 /* wait for burstcount */
221 if (check_locality(chip, l))
223 tpm_msleep(TPM_TIMEOUT);
224 } while (time_before(jiffies, stop));
229 static int tpm_tis_request_locality(struct tpm_chip *chip, int l)
231 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
234 mutex_lock(&priv->locality_count_mutex);
235 if (priv->locality_count == 0)
236 ret = __tpm_tis_request_locality(chip, l);
238 priv->locality_count++;
239 mutex_unlock(&priv->locality_count_mutex);
243 static u8 tpm_tis_status(struct tpm_chip *chip)
245 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
249 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
253 if (unlikely((status & TPM_STS_READ_ZERO) != 0)) {
254 if (!test_and_set_bit(TPM_TIS_INVALID_STATUS, &priv->flags)) {
256 * If this trips, the chances are the read is
257 * returning 0xff because the locality hasn't been
258 * acquired. Usually because tpm_try_get_ops() hasn't
259 * been called before doing a TPM operation.
261 dev_err(&chip->dev, "invalid TPM_STS.x 0x%02x, dumping stack for forensics\n",
265 * Dump stack for forensics, as invalid TPM_STS.x could be
266 * potentially triggered by impaired tpm_try_get_ops() or
267 * tpm_find_get_ops().
278 static void tpm_tis_ready(struct tpm_chip *chip)
280 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
282 /* this causes the current command to be aborted */
283 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
286 static int get_burstcount(struct tpm_chip *chip)
288 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
293 /* wait for burstcount */
294 if (chip->flags & TPM_CHIP_FLAG_TPM2)
295 stop = jiffies + chip->timeout_a;
297 stop = jiffies + chip->timeout_d;
299 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
303 burstcnt = (value >> 8) & 0xFFFF;
306 usleep_range(TPM_TIMEOUT_USECS_MIN, TPM_TIMEOUT_USECS_MAX);
307 } while (time_before(jiffies, stop));
311 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
313 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
314 int size = 0, burstcnt, rc;
316 while (size < count) {
317 rc = wait_for_tpm_stat(chip,
318 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
320 &priv->read_queue, true);
323 burstcnt = get_burstcount(chip);
325 dev_err(&chip->dev, "Unable to read burstcount\n");
328 burstcnt = min_t(int, burstcnt, count - size);
330 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
331 burstcnt, buf + size);
340 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
342 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
348 if (count < TPM_HEADER_SIZE) {
353 size = recv_data(chip, buf, TPM_HEADER_SIZE);
354 /* read first 10 bytes, including tag, paramsize, and result */
355 if (size < TPM_HEADER_SIZE) {
356 dev_err(&chip->dev, "Unable to read header\n");
360 expected = be32_to_cpu(*(__be32 *) (buf + 2));
361 if (expected > count || expected < TPM_HEADER_SIZE) {
366 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
367 expected - TPM_HEADER_SIZE);
368 if (size < expected) {
369 dev_err(&chip->dev, "Unable to read remainder of result\n");
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 (status & TPM_STS_DATA_AVAIL) { /* retry? */
381 dev_err(&chip->dev, "Error left over data\n");
386 rc = tpm_tis_verify_crc(priv, (size_t)size, buf);
388 dev_err(&chip->dev, "CRC mismatch for response.\n");
399 * If interrupts are used (signaled by an irq set in the vendor structure)
400 * tpm.c can skip polling for the data to be available as the interrupt is
403 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
405 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
406 int rc, status, burstcnt;
408 bool itpm = test_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags);
410 status = tpm_tis_status(chip);
411 if ((status & TPM_STS_COMMAND_READY) == 0) {
413 if (wait_for_tpm_stat
414 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
415 &priv->int_queue, false) < 0) {
421 while (count < len - 1) {
422 burstcnt = get_burstcount(chip);
424 dev_err(&chip->dev, "Unable to read burstcount\n");
428 burstcnt = min_t(int, burstcnt, len - count - 1);
429 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
430 burstcnt, buf + count);
436 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
437 &priv->int_queue, false) < 0) {
441 status = tpm_tis_status(chip);
442 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
448 /* write last byte */
449 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
453 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
454 &priv->int_queue, false) < 0) {
458 status = tpm_tis_status(chip);
459 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
471 static void disable_interrupts(struct tpm_chip *chip)
473 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
480 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
484 intmask &= ~TPM_GLOBAL_INT_ENABLE;
485 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
487 devm_free_irq(chip->dev.parent, priv->irq, chip);
489 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
493 * If interrupts are used (signaled by an irq set in the vendor structure)
494 * tpm.c can skip polling for the data to be available as the interrupt is
497 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
499 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
504 rc = tpm_tis_send_data(chip, buf, len);
508 rc = tpm_tis_verify_crc(priv, len, buf);
510 dev_err(&chip->dev, "CRC mismatch for command.\n");
515 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
519 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
520 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
522 dur = tpm_calc_ordinal_duration(chip, ordinal);
523 if (wait_for_tpm_stat
524 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
525 &priv->read_queue, false) < 0) {
536 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
539 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
541 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) ||
542 test_bit(TPM_TIS_IRQ_TESTED, &priv->flags))
543 return tpm_tis_send_main(chip, buf, len);
545 /* Verify receipt of the expected IRQ */
548 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
549 rc = tpm_tis_send_main(chip, buf, len);
551 chip->flags |= TPM_CHIP_FLAG_IRQ;
552 if (!test_bit(TPM_TIS_IRQ_TESTED, &priv->flags))
554 if (!test_bit(TPM_TIS_IRQ_TESTED, &priv->flags))
555 disable_interrupts(chip);
556 set_bit(TPM_TIS_IRQ_TESTED, &priv->flags);
560 struct tis_vendor_durations_override {
562 struct tpm1_version version;
563 unsigned long durations[3];
566 static const struct tis_vendor_durations_override vendor_dur_overrides[] = {
567 /* STMicroelectronics 0x104a */
570 { (2 * 60 * HZ), (2 * 60 * HZ), (2 * 60 * HZ) } },
573 static void tpm_tis_update_durations(struct tpm_chip *chip,
574 unsigned long *duration_cap)
576 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
577 struct tpm1_version *version;
582 chip->duration_adjusted = false;
584 if (chip->ops->clk_enable != NULL)
585 chip->ops->clk_enable(chip, true);
587 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
589 dev_warn(&chip->dev, "%s: failed to read did_vid. %d\n",
594 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
595 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
596 "attempting to determine the 1.2 version",
597 sizeof(cap.version2));
599 version = &cap.version2.version;
601 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
602 "attempting to determine the 1.1 version",
603 sizeof(cap.version1));
608 version = &cap.version1;
611 for (i = 0; i != ARRAY_SIZE(vendor_dur_overrides); i++) {
612 if (vendor_dur_overrides[i].did_vid != did_vid)
615 if ((version->major ==
616 vendor_dur_overrides[i].version.major) &&
618 vendor_dur_overrides[i].version.minor) &&
619 (version->rev_major ==
620 vendor_dur_overrides[i].version.rev_major) &&
621 (version->rev_minor ==
622 vendor_dur_overrides[i].version.rev_minor)) {
625 vendor_dur_overrides[i].durations,
626 sizeof(vendor_dur_overrides[i].durations));
628 chip->duration_adjusted = true;
634 if (chip->ops->clk_enable != NULL)
635 chip->ops->clk_enable(chip, false);
638 struct tis_vendor_timeout_override {
640 unsigned long timeout_us[4];
643 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
645 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
646 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
649 static void tpm_tis_update_timeouts(struct tpm_chip *chip,
650 unsigned long *timeout_cap)
652 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
656 chip->timeout_adjusted = false;
658 if (chip->ops->clk_enable != NULL)
659 chip->ops->clk_enable(chip, true);
661 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
663 dev_warn(&chip->dev, "%s: failed to read did_vid: %d\n",
668 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
669 if (vendor_timeout_overrides[i].did_vid != did_vid)
671 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
672 sizeof(vendor_timeout_overrides[i].timeout_us));
673 chip->timeout_adjusted = true;
677 if (chip->ops->clk_enable != NULL)
678 chip->ops->clk_enable(chip, false);
684 * Early probing for iTPM with STS_DATA_EXPECT flaw.
685 * Try sending command without itpm flag set and if that
686 * fails, repeat with itpm flag set.
688 static int probe_itpm(struct tpm_chip *chip)
690 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
692 static const u8 cmd_getticks[] = {
693 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
694 0x00, 0x00, 0x00, 0xf1
696 size_t len = sizeof(cmd_getticks);
699 if (test_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags))
702 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
706 /* probe only iTPMS */
707 if (vendor != TPM_VID_INTEL)
710 if (tpm_tis_request_locality(chip, 0) != 0)
713 rc = tpm_tis_send_data(chip, cmd_getticks, len);
719 set_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags);
721 rc = tpm_tis_send_data(chip, cmd_getticks, len);
723 dev_info(&chip->dev, "Detected an iTPM.\n");
725 clear_bit(TPM_TIS_ITPM_WORKAROUND, &priv->flags);
731 tpm_tis_relinquish_locality(chip, priv->locality);
736 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
738 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
740 if (!test_bit(TPM_TIS_DEFAULT_CANCELLATION, &priv->flags)) {
741 switch (priv->manufacturer_id) {
742 case TPM_VID_WINBOND:
743 return ((status == TPM_STS_VALID) ||
744 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
746 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
752 return status == TPM_STS_COMMAND_READY;
755 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
757 struct tpm_chip *chip = dev_id;
758 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
762 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
769 set_bit(TPM_TIS_IRQ_TESTED, &priv->flags);
770 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
771 wake_up_interruptible(&priv->read_queue);
774 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
775 TPM_INTF_CMD_READY_INT))
776 wake_up_interruptible(&priv->int_queue);
778 /* Clear interrupts handled with TPM_EOI */
779 tpm_tis_request_locality(chip, 0);
780 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
781 tpm_tis_relinquish_locality(chip, 0);
785 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
789 static void tpm_tis_gen_interrupt(struct tpm_chip *chip)
791 const char *desc = "attempting to generate an interrupt";
796 chip->flags |= TPM_CHIP_FLAG_IRQ;
798 if (chip->flags & TPM_CHIP_FLAG_TPM2)
799 ret = tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
801 ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0);
804 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
807 /* Register the IRQ and issue a command that will cause an interrupt. If an
808 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
809 * everything and leave in polling mode. Returns 0 on success.
811 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
814 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
820 rc = devm_request_threaded_irq(chip->dev.parent, irq, NULL,
821 tis_int_handler, IRQF_ONESHOT | flags,
822 dev_name(&chip->dev), chip);
824 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
830 rc = tpm_tis_request_locality(chip, 0);
834 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
837 tpm_tis_relinquish_locality(chip, priv->locality);
841 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
845 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
849 /* Clear all existing */
850 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
854 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
855 intmask | TPM_GLOBAL_INT_ENABLE);
859 clear_bit(TPM_TIS_IRQ_TESTED, &priv->flags);
861 /* Generate an interrupt by having the core call through to
864 tpm_tis_gen_interrupt(chip);
867 /* tpm_tis_send will either confirm the interrupt is working or it
868 * will call disable_irq which undoes all of the above.
870 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
871 tpm_tis_write8(priv, original_int_vec,
872 TPM_INT_VECTOR(priv->locality));
876 tpm_tis_relinquish_locality(chip, priv->locality);
881 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
882 * do not have ACPI/etc. We typically expect the interrupt to be declared if
885 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
887 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
891 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
896 if (!original_int_vec) {
897 if (IS_ENABLED(CONFIG_X86))
898 for (i = 3; i <= 15; i++)
899 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
902 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
907 void tpm_tis_remove(struct tpm_chip *chip)
909 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
910 u32 reg = TPM_INT_ENABLE(priv->locality);
914 tpm_tis_clkrun_enable(chip, true);
916 rc = tpm_tis_read32(priv, reg, &interrupt);
920 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
922 tpm_tis_clkrun_enable(chip, false);
924 if (priv->ilb_base_addr)
925 iounmap(priv->ilb_base_addr);
927 EXPORT_SYMBOL_GPL(tpm_tis_remove);
930 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
931 * of a single TPM command
932 * @chip: TPM chip to use
933 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
934 * 0 - Enable CLKRUN protocol
935 * Call this function directly in tpm_tis_remove() in error or driver removal
936 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
938 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
940 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
943 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
944 !data->ilb_base_addr)
948 data->clkrun_enabled++;
949 if (data->clkrun_enabled > 1)
951 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
953 /* Disable LPC CLKRUN# */
954 clkrun_val &= ~LPC_CLKRUN_EN;
955 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
958 * Write any random value on port 0x80 which is on LPC, to make
959 * sure LPC clock is running before sending any TPM command.
963 data->clkrun_enabled--;
964 if (data->clkrun_enabled)
967 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
969 /* Enable LPC CLKRUN# */
970 clkrun_val |= LPC_CLKRUN_EN;
971 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
974 * Write any random value on port 0x80 which is on LPC, to make
975 * sure LPC clock is running before sending any TPM command.
981 static const struct tpm_class_ops tpm_tis = {
982 .flags = TPM_OPS_AUTO_STARTUP,
983 .status = tpm_tis_status,
984 .recv = tpm_tis_recv,
985 .send = tpm_tis_send,
986 .cancel = tpm_tis_ready,
987 .update_timeouts = tpm_tis_update_timeouts,
988 .update_durations = tpm_tis_update_durations,
989 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
990 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
991 .req_canceled = tpm_tis_req_canceled,
992 .request_locality = tpm_tis_request_locality,
993 .relinquish_locality = tpm_tis_relinquish_locality,
994 .clk_enable = tpm_tis_clkrun_enable,
997 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
998 const struct tpm_tis_phy_ops *phy_ops,
999 acpi_handle acpi_dev_handle)
1007 struct tpm_chip *chip;
1009 chip = tpmm_chip_alloc(dev, &tpm_tis);
1011 return PTR_ERR(chip);
1014 chip->acpi_dev_handle = acpi_dev_handle;
1017 chip->hwrng.quality = priv->rng_quality;
1019 /* Maximum timeouts */
1020 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
1021 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
1022 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
1023 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
1024 priv->timeout_min = TPM_TIMEOUT_USECS_MIN;
1025 priv->timeout_max = TPM_TIMEOUT_USECS_MAX;
1026 priv->phy_ops = phy_ops;
1027 priv->locality_count = 0;
1028 mutex_init(&priv->locality_count_mutex);
1030 dev_set_drvdata(&chip->dev, priv);
1032 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
1036 priv->manufacturer_id = vendor;
1038 if (priv->manufacturer_id == TPM_VID_ATML &&
1039 !(chip->flags & TPM_CHIP_FLAG_TPM2)) {
1040 priv->timeout_min = TIS_TIMEOUT_MIN_ATML;
1041 priv->timeout_max = TIS_TIMEOUT_MAX_ATML;
1045 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
1047 if (!priv->ilb_base_addr)
1050 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
1051 /* Check if CLKRUN# is already not enabled in the LPC bus */
1052 if (!(clkrun_val & LPC_CLKRUN_EN)) {
1053 iounmap(priv->ilb_base_addr);
1054 priv->ilb_base_addr = NULL;
1058 if (chip->ops->clk_enable != NULL)
1059 chip->ops->clk_enable(chip, true);
1061 if (wait_startup(chip, 0) != 0) {
1066 /* Take control of the TPM's interrupt hardware and shut it off */
1067 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1071 /* Figure out the capabilities */
1072 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
1076 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
1078 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
1079 dev_dbg(dev, "\tBurst Count Static\n");
1080 if (intfcaps & TPM_INTF_CMD_READY_INT) {
1081 intmask |= TPM_INTF_CMD_READY_INT;
1082 dev_dbg(dev, "\tCommand Ready Int Support\n");
1084 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
1085 dev_dbg(dev, "\tInterrupt Edge Falling\n");
1086 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
1087 dev_dbg(dev, "\tInterrupt Edge Rising\n");
1088 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
1089 dev_dbg(dev, "\tInterrupt Level Low\n");
1090 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
1091 dev_dbg(dev, "\tInterrupt Level High\n");
1092 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT) {
1093 intmask |= TPM_INTF_LOCALITY_CHANGE_INT;
1094 dev_dbg(dev, "\tLocality Change Int Support\n");
1096 if (intfcaps & TPM_INTF_STS_VALID_INT) {
1097 intmask |= TPM_INTF_STS_VALID_INT;
1098 dev_dbg(dev, "\tSts Valid Int Support\n");
1100 if (intfcaps & TPM_INTF_DATA_AVAIL_INT) {
1101 intmask |= TPM_INTF_DATA_AVAIL_INT;
1102 dev_dbg(dev, "\tData Avail Int Support\n");
1105 intmask &= ~TPM_GLOBAL_INT_ENABLE;
1107 rc = tpm_tis_request_locality(chip, 0);
1113 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1114 tpm_tis_relinquish_locality(chip, 0);
1116 rc = tpm_chip_start(chip);
1119 rc = tpm2_probe(chip);
1120 tpm_chip_stop(chip);
1124 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
1128 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
1129 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
1132 probe = probe_itpm(chip);
1138 /* INTERRUPT Setup */
1139 init_waitqueue_head(&priv->read_queue);
1140 init_waitqueue_head(&priv->int_queue);
1142 rc = tpm_chip_bootstrap(chip);
1148 * Before doing irq testing issue a command to the TPM in polling mode
1149 * to make sure it works. May as well use that command to set the
1150 * proper timeouts for the driver.
1153 rc = tpm_tis_request_locality(chip, 0);
1157 rc = tpm_get_timeouts(chip);
1159 tpm_tis_relinquish_locality(chip, 0);
1162 dev_err(dev, "Could not get TPM timeouts and durations\n");
1168 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
1171 tpm_tis_probe_irq(chip, intmask);
1173 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
1174 priv->int_mask = intmask;
1176 dev_err(&chip->dev, FW_BUG
1177 "TPM interrupt not working, polling instead\n");
1179 rc = tpm_tis_request_locality(chip, 0);
1182 disable_interrupts(chip);
1183 tpm_tis_relinquish_locality(chip, 0);
1187 rc = tpm_chip_register(chip);
1191 if (chip->ops->clk_enable != NULL)
1192 chip->ops->clk_enable(chip, false);
1196 if (chip->ops->clk_enable != NULL)
1197 chip->ops->clk_enable(chip, false);
1199 tpm_tis_remove(chip);
1203 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
1205 #ifdef CONFIG_PM_SLEEP
1206 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
1208 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
1213 * Re-enable interrupts that device may have lost or BIOS/firmware may
1216 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
1218 dev_err(&chip->dev, "Setting IRQ failed.\n");
1222 intmask = priv->int_mask | TPM_GLOBAL_INT_ENABLE;
1223 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1225 dev_err(&chip->dev, "Enabling interrupts failed.\n");
1228 int tpm_tis_resume(struct device *dev)
1230 struct tpm_chip *chip = dev_get_drvdata(dev);
1233 ret = tpm_chip_start(chip);
1237 if (chip->flags & TPM_CHIP_FLAG_IRQ)
1238 tpm_tis_reenable_interrupts(chip);
1241 * TPM 1.2 requires self-test on resume. This function actually returns
1242 * an error code but for unknown reason it isn't handled.
1244 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1245 tpm1_do_selftest(chip);
1247 tpm_chip_stop(chip);
1249 ret = tpm_pm_resume(dev);
1255 EXPORT_SYMBOL_GPL(tpm_tis_resume);
1258 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1259 MODULE_DESCRIPTION("TPM Driver");
1260 MODULE_VERSION("2.0");
1261 MODULE_LICENSE("GPL");