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 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 __tpm_tis_relinquish_locality(struct tpm_tis_data *priv, int l)
141 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
146 static int tpm_tis_relinquish_locality(struct tpm_chip *chip, int l)
148 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
150 mutex_lock(&priv->locality_count_mutex);
151 priv->locality_count--;
152 if (priv->locality_count == 0)
153 __tpm_tis_relinquish_locality(priv, l);
154 mutex_unlock(&priv->locality_count_mutex);
159 static int __tpm_tis_request_locality(struct tpm_chip *chip, int l)
161 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
162 unsigned long stop, timeout;
165 if (check_locality(chip, l))
168 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
172 stop = jiffies + chip->timeout_a;
174 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
176 timeout = stop - jiffies;
177 if ((long)timeout <= 0)
179 rc = wait_event_interruptible_timeout(priv->int_queue,
185 if (rc == -ERESTARTSYS && freezing(current)) {
186 clear_thread_flag(TIF_SIGPENDING);
190 /* wait for burstcount */
192 if (check_locality(chip, l))
194 tpm_msleep(TPM_TIMEOUT);
195 } while (time_before(jiffies, stop));
200 static int tpm_tis_request_locality(struct tpm_chip *chip, int l)
202 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
205 mutex_lock(&priv->locality_count_mutex);
206 if (priv->locality_count == 0)
207 ret = __tpm_tis_request_locality(chip, l);
209 priv->locality_count++;
210 mutex_unlock(&priv->locality_count_mutex);
214 static u8 tpm_tis_status(struct tpm_chip *chip)
216 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
220 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
224 if (unlikely((status & TPM_STS_READ_ZERO) != 0)) {
225 if (!test_and_set_bit(TPM_TIS_INVALID_STATUS, &priv->flags)) {
227 * If this trips, the chances are the read is
228 * returning 0xff because the locality hasn't been
229 * acquired. Usually because tpm_try_get_ops() hasn't
230 * been called before doing a TPM operation.
232 dev_err(&chip->dev, "invalid TPM_STS.x 0x%02x, dumping stack for forensics\n",
236 * Dump stack for forensics, as invalid TPM_STS.x could be
237 * potentially triggered by impaired tpm_try_get_ops() or
238 * tpm_find_get_ops().
249 static void tpm_tis_ready(struct tpm_chip *chip)
251 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
253 /* this causes the current command to be aborted */
254 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
257 static int get_burstcount(struct tpm_chip *chip)
259 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
264 /* wait for burstcount */
265 if (chip->flags & TPM_CHIP_FLAG_TPM2)
266 stop = jiffies + chip->timeout_a;
268 stop = jiffies + chip->timeout_d;
270 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
274 burstcnt = (value >> 8) & 0xFFFF;
277 usleep_range(TPM_TIMEOUT_USECS_MIN, TPM_TIMEOUT_USECS_MAX);
278 } while (time_before(jiffies, stop));
282 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
284 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
285 int size = 0, burstcnt, rc;
287 while (size < count) {
288 rc = wait_for_tpm_stat(chip,
289 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
291 &priv->read_queue, true);
294 burstcnt = get_burstcount(chip);
296 dev_err(&chip->dev, "Unable to read burstcount\n");
299 burstcnt = min_t(int, burstcnt, count - size);
301 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
302 burstcnt, buf + size);
311 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
313 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
319 if (count < TPM_HEADER_SIZE) {
324 size = recv_data(chip, buf, TPM_HEADER_SIZE);
325 /* read first 10 bytes, including tag, paramsize, and result */
326 if (size < TPM_HEADER_SIZE) {
327 dev_err(&chip->dev, "Unable to read header\n");
331 expected = be32_to_cpu(*(__be32 *) (buf + 2));
332 if (expected > count || expected < TPM_HEADER_SIZE) {
337 rc = recv_data(chip, &buf[TPM_HEADER_SIZE],
338 expected - TPM_HEADER_SIZE);
344 if (size < expected) {
345 dev_err(&chip->dev, "Unable to read remainder of result\n");
350 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
351 &priv->int_queue, false) < 0) {
355 status = tpm_tis_status(chip);
356 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
357 dev_err(&chip->dev, "Error left over data\n");
368 * If interrupts are used (signaled by an irq set in the vendor structure)
369 * tpm.c can skip polling for the data to be available as the interrupt is
372 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
374 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
375 int rc, status, burstcnt;
377 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
379 status = tpm_tis_status(chip);
380 if ((status & TPM_STS_COMMAND_READY) == 0) {
382 if (wait_for_tpm_stat
383 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
384 &priv->int_queue, false) < 0) {
390 while (count < len - 1) {
391 burstcnt = get_burstcount(chip);
393 dev_err(&chip->dev, "Unable to read burstcount\n");
397 burstcnt = min_t(int, burstcnt, len - count - 1);
398 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
399 burstcnt, buf + count);
405 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
406 &priv->int_queue, false) < 0) {
410 status = tpm_tis_status(chip);
411 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
417 /* write last byte */
418 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
422 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
423 &priv->int_queue, false) < 0) {
427 status = tpm_tis_status(chip);
428 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
440 static void disable_interrupts(struct tpm_chip *chip)
442 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
449 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
453 intmask &= ~TPM_GLOBAL_INT_ENABLE;
454 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
456 devm_free_irq(chip->dev.parent, priv->irq, chip);
458 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
462 * If interrupts are used (signaled by an irq set in the vendor structure)
463 * tpm.c can skip polling for the data to be available as the interrupt is
466 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
468 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
474 for (try = 0; try < TPM_RETRY; try++) {
475 rc = tpm_tis_send_data(chip, buf, len);
477 /* Data transfer done successfully */
480 /* Data transfer failed, not recoverable */
485 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
489 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
490 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
492 dur = tpm_calc_ordinal_duration(chip, ordinal);
493 if (wait_for_tpm_stat
494 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
495 &priv->read_queue, false) < 0) {
506 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
509 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
511 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
512 return tpm_tis_send_main(chip, buf, len);
514 /* Verify receipt of the expected IRQ */
517 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
518 rc = tpm_tis_send_main(chip, buf, len);
520 chip->flags |= TPM_CHIP_FLAG_IRQ;
521 if (!priv->irq_tested)
523 if (!priv->irq_tested)
524 disable_interrupts(chip);
525 priv->irq_tested = true;
529 struct tis_vendor_durations_override {
531 struct tpm1_version version;
532 unsigned long durations[3];
535 static const struct tis_vendor_durations_override vendor_dur_overrides[] = {
536 /* STMicroelectronics 0x104a */
539 { (2 * 60 * HZ), (2 * 60 * HZ), (2 * 60 * HZ) } },
542 static void tpm_tis_update_durations(struct tpm_chip *chip,
543 unsigned long *duration_cap)
545 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
546 struct tpm1_version *version;
551 chip->duration_adjusted = false;
553 if (chip->ops->clk_enable != NULL)
554 chip->ops->clk_enable(chip, true);
556 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
558 dev_warn(&chip->dev, "%s: failed to read did_vid. %d\n",
563 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
564 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
565 "attempting to determine the 1.2 version",
566 sizeof(cap.version2));
568 version = &cap.version2.version;
570 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
571 "attempting to determine the 1.1 version",
572 sizeof(cap.version1));
577 version = &cap.version1;
580 for (i = 0; i != ARRAY_SIZE(vendor_dur_overrides); i++) {
581 if (vendor_dur_overrides[i].did_vid != did_vid)
584 if ((version->major ==
585 vendor_dur_overrides[i].version.major) &&
587 vendor_dur_overrides[i].version.minor) &&
588 (version->rev_major ==
589 vendor_dur_overrides[i].version.rev_major) &&
590 (version->rev_minor ==
591 vendor_dur_overrides[i].version.rev_minor)) {
594 vendor_dur_overrides[i].durations,
595 sizeof(vendor_dur_overrides[i].durations));
597 chip->duration_adjusted = true;
603 if (chip->ops->clk_enable != NULL)
604 chip->ops->clk_enable(chip, false);
607 struct tis_vendor_timeout_override {
609 unsigned long timeout_us[4];
612 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
614 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
615 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
618 static void tpm_tis_update_timeouts(struct tpm_chip *chip,
619 unsigned long *timeout_cap)
621 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
625 chip->timeout_adjusted = false;
627 if (chip->ops->clk_enable != NULL)
628 chip->ops->clk_enable(chip, true);
630 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
632 dev_warn(&chip->dev, "%s: failed to read did_vid: %d\n",
637 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
638 if (vendor_timeout_overrides[i].did_vid != did_vid)
640 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
641 sizeof(vendor_timeout_overrides[i].timeout_us));
642 chip->timeout_adjusted = true;
646 if (chip->ops->clk_enable != NULL)
647 chip->ops->clk_enable(chip, false);
653 * Early probing for iTPM with STS_DATA_EXPECT flaw.
654 * Try sending command without itpm flag set and if that
655 * fails, repeat with itpm flag set.
657 static int probe_itpm(struct tpm_chip *chip)
659 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
661 static const u8 cmd_getticks[] = {
662 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
663 0x00, 0x00, 0x00, 0xf1
665 size_t len = sizeof(cmd_getticks);
668 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
671 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
675 /* probe only iTPMS */
676 if (vendor != TPM_VID_INTEL)
679 if (tpm_tis_request_locality(chip, 0) != 0)
682 rc = tpm_tis_send_data(chip, cmd_getticks, len);
688 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
690 rc = tpm_tis_send_data(chip, cmd_getticks, len);
692 dev_info(&chip->dev, "Detected an iTPM.\n");
694 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
700 tpm_tis_relinquish_locality(chip, priv->locality);
705 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
707 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
709 switch (priv->manufacturer_id) {
710 case TPM_VID_WINBOND:
711 return ((status == TPM_STS_VALID) ||
712 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
714 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
716 return (status == TPM_STS_COMMAND_READY);
720 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
722 struct tpm_chip *chip = dev_id;
723 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
727 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
734 priv->irq_tested = true;
735 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
736 wake_up_interruptible(&priv->read_queue);
737 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
738 for (i = 0; i < 5; i++)
739 if (check_locality(chip, i))
742 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
743 TPM_INTF_CMD_READY_INT))
744 wake_up_interruptible(&priv->int_queue);
746 /* Clear interrupts handled with TPM_EOI */
747 tpm_tis_request_locality(chip, 0);
748 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
749 tpm_tis_relinquish_locality(chip, 0);
753 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
757 static void tpm_tis_gen_interrupt(struct tpm_chip *chip)
759 const char *desc = "attempting to generate an interrupt";
764 if (chip->flags & TPM_CHIP_FLAG_TPM2)
765 ret = tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
767 ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0);
770 /* Register the IRQ and issue a command that will cause an interrupt. If an
771 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
772 * everything and leave in polling mode. Returns 0 on success.
774 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
777 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
783 rc = devm_request_threaded_irq(chip->dev.parent, irq, NULL,
784 tis_int_handler, IRQF_ONESHOT | flags,
785 dev_name(&chip->dev), chip);
787 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
793 rc = tpm_tis_request_locality(chip, 0);
797 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
800 tpm_tis_relinquish_locality(chip, priv->locality);
804 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
808 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
812 /* Clear all existing */
813 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
817 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
818 intmask | TPM_GLOBAL_INT_ENABLE);
822 priv->irq_tested = false;
824 /* Generate an interrupt by having the core call through to
827 tpm_tis_gen_interrupt(chip);
830 /* tpm_tis_send will either confirm the interrupt is working or it
831 * will call disable_irq which undoes all of the above.
833 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
834 tpm_tis_write8(priv, original_int_vec,
835 TPM_INT_VECTOR(priv->locality));
839 tpm_tis_relinquish_locality(chip, priv->locality);
844 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
845 * do not have ACPI/etc. We typically expect the interrupt to be declared if
848 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
850 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
854 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
859 if (!original_int_vec) {
860 if (IS_ENABLED(CONFIG_X86))
861 for (i = 3; i <= 15; i++)
862 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
865 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
870 void tpm_tis_remove(struct tpm_chip *chip)
872 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
873 u32 reg = TPM_INT_ENABLE(priv->locality);
877 tpm_tis_clkrun_enable(chip, true);
879 rc = tpm_tis_read32(priv, reg, &interrupt);
883 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
885 tpm_tis_clkrun_enable(chip, false);
887 if (priv->ilb_base_addr)
888 iounmap(priv->ilb_base_addr);
890 EXPORT_SYMBOL_GPL(tpm_tis_remove);
893 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
894 * of a single TPM command
895 * @chip: TPM chip to use
896 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
897 * 0 - Enable CLKRUN protocol
898 * Call this function directly in tpm_tis_remove() in error or driver removal
899 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
901 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
903 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
906 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
907 !data->ilb_base_addr)
911 data->clkrun_enabled++;
912 if (data->clkrun_enabled > 1)
914 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
916 /* Disable LPC CLKRUN# */
917 clkrun_val &= ~LPC_CLKRUN_EN;
918 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
921 * Write any random value on port 0x80 which is on LPC, to make
922 * sure LPC clock is running before sending any TPM command.
926 data->clkrun_enabled--;
927 if (data->clkrun_enabled)
930 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
932 /* Enable LPC CLKRUN# */
933 clkrun_val |= LPC_CLKRUN_EN;
934 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
937 * Write any random value on port 0x80 which is on LPC, to make
938 * sure LPC clock is running before sending any TPM command.
944 static const struct tpm_class_ops tpm_tis = {
945 .flags = TPM_OPS_AUTO_STARTUP,
946 .status = tpm_tis_status,
947 .recv = tpm_tis_recv,
948 .send = tpm_tis_send,
949 .cancel = tpm_tis_ready,
950 .update_timeouts = tpm_tis_update_timeouts,
951 .update_durations = tpm_tis_update_durations,
952 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
953 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
954 .req_canceled = tpm_tis_req_canceled,
955 .request_locality = tpm_tis_request_locality,
956 .relinquish_locality = tpm_tis_relinquish_locality,
957 .clk_enable = tpm_tis_clkrun_enable,
960 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
961 const struct tpm_tis_phy_ops *phy_ops,
962 acpi_handle acpi_dev_handle)
970 struct tpm_chip *chip;
972 chip = tpmm_chip_alloc(dev, &tpm_tis);
974 return PTR_ERR(chip);
977 chip->acpi_dev_handle = acpi_dev_handle;
980 chip->hwrng.quality = priv->rng_quality;
982 /* Maximum timeouts */
983 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
984 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
985 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
986 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
987 priv->timeout_min = TPM_TIMEOUT_USECS_MIN;
988 priv->timeout_max = TPM_TIMEOUT_USECS_MAX;
989 priv->phy_ops = phy_ops;
990 priv->locality_count = 0;
991 mutex_init(&priv->locality_count_mutex);
993 dev_set_drvdata(&chip->dev, priv);
995 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
999 priv->manufacturer_id = vendor;
1001 if (priv->manufacturer_id == TPM_VID_ATML &&
1002 !(chip->flags & TPM_CHIP_FLAG_TPM2)) {
1003 priv->timeout_min = TIS_TIMEOUT_MIN_ATML;
1004 priv->timeout_max = TIS_TIMEOUT_MAX_ATML;
1008 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
1010 if (!priv->ilb_base_addr)
1013 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
1014 /* Check if CLKRUN# is already not enabled in the LPC bus */
1015 if (!(clkrun_val & LPC_CLKRUN_EN)) {
1016 iounmap(priv->ilb_base_addr);
1017 priv->ilb_base_addr = NULL;
1021 if (chip->ops->clk_enable != NULL)
1022 chip->ops->clk_enable(chip, true);
1024 if (wait_startup(chip, 0) != 0) {
1029 /* Take control of the TPM's interrupt hardware and shut it off */
1030 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1034 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
1035 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
1036 intmask &= ~TPM_GLOBAL_INT_ENABLE;
1038 rc = tpm_tis_request_locality(chip, 0);
1044 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1045 tpm_tis_relinquish_locality(chip, 0);
1047 rc = tpm_chip_start(chip);
1050 rc = tpm2_probe(chip);
1051 tpm_chip_stop(chip);
1055 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
1059 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
1060 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
1063 probe = probe_itpm(chip);
1069 /* Figure out the capabilities */
1070 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
1074 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
1076 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
1077 dev_dbg(dev, "\tBurst Count Static\n");
1078 if (intfcaps & TPM_INTF_CMD_READY_INT)
1079 dev_dbg(dev, "\tCommand Ready Int Support\n");
1080 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
1081 dev_dbg(dev, "\tInterrupt Edge Falling\n");
1082 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
1083 dev_dbg(dev, "\tInterrupt Edge Rising\n");
1084 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
1085 dev_dbg(dev, "\tInterrupt Level Low\n");
1086 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
1087 dev_dbg(dev, "\tInterrupt Level High\n");
1088 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
1089 dev_dbg(dev, "\tLocality Change Int Support\n");
1090 if (intfcaps & TPM_INTF_STS_VALID_INT)
1091 dev_dbg(dev, "\tSts Valid Int Support\n");
1092 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
1093 dev_dbg(dev, "\tData Avail Int Support\n");
1095 /* INTERRUPT Setup */
1096 init_waitqueue_head(&priv->read_queue);
1097 init_waitqueue_head(&priv->int_queue);
1100 * Before doing irq testing issue a command to the TPM in polling mode
1101 * to make sure it works. May as well use that command to set the
1102 * proper timeouts for the driver.
1105 rc = tpm_tis_request_locality(chip, 0);
1109 rc = tpm_get_timeouts(chip);
1111 tpm_tis_relinquish_locality(chip, 0);
1114 dev_err(dev, "Could not get TPM timeouts and durations\n");
1120 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
1123 tpm_tis_probe_irq(chip, intmask);
1125 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
1126 dev_err(&chip->dev, FW_BUG
1127 "TPM interrupt not working, polling instead\n");
1129 rc = tpm_tis_request_locality(chip, 0);
1132 disable_interrupts(chip);
1133 tpm_tis_relinquish_locality(chip, 0);
1137 rc = tpm_chip_register(chip);
1141 if (chip->ops->clk_enable != NULL)
1142 chip->ops->clk_enable(chip, false);
1146 if (chip->ops->clk_enable != NULL)
1147 chip->ops->clk_enable(chip, false);
1149 tpm_tis_remove(chip);
1153 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
1155 #ifdef CONFIG_PM_SLEEP
1156 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
1158 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
1162 if (chip->ops->clk_enable != NULL)
1163 chip->ops->clk_enable(chip, true);
1165 /* reenable interrupts that device may have lost or
1166 * BIOS/firmware may have disabled
1168 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
1172 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
1176 intmask |= TPM_INTF_CMD_READY_INT
1177 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
1178 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
1180 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
1183 if (chip->ops->clk_enable != NULL)
1184 chip->ops->clk_enable(chip, false);
1189 int tpm_tis_resume(struct device *dev)
1191 struct tpm_chip *chip = dev_get_drvdata(dev);
1194 ret = tpm_tis_request_locality(chip, 0);
1198 if (chip->flags & TPM_CHIP_FLAG_IRQ)
1199 tpm_tis_reenable_interrupts(chip);
1201 ret = tpm_pm_resume(dev);
1206 * TPM 1.2 requires self-test on resume. This function actually returns
1207 * an error code but for unknown reason it isn't handled.
1209 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1210 tpm1_do_selftest(chip);
1212 tpm_tis_relinquish_locality(chip, 0);
1216 EXPORT_SYMBOL_GPL(tpm_tis_resume);
1219 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1220 MODULE_DESCRIPTION("TPM Driver");
1221 MODULE_VERSION("2.0");
1222 MODULE_LICENSE("GPL");