Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005, 2006 IBM Corporation |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 3 | * Copyright (C) 2014, 2015 Intel Corporation |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
| 6 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 7 | * Kylene Hall <kjhall@us.ibm.com> |
| 8 | * |
Kent Yoder | 8e81cc1 | 2007-08-22 14:01:04 -0700 | [diff] [blame] | 9 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
| 10 | * |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 11 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 12 | * Specifications at www.trustedcomputinggroup.org |
| 13 | * |
| 14 | * This device driver implements the TPM interface as defined in |
| 15 | * the TCG TPM Interface Spec version 1.2, revision 1.0. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation, version 2 of the |
| 20 | * License. |
| 21 | */ |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 25 | #include <linux/pnp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/wait.h> |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 29 | #include <linux/acpi.h> |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 30 | #include <linux/freezer.h> |
Jason Gunthorpe | 420d439 | 2016-11-07 15:44:31 -0700 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_device.h> |
Jérémy Lefaure | 33957a1 | 2017-10-01 15:30:51 -0400 | [diff] [blame] | 33 | #include <linux/kernel.h> |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 34 | #include "tpm.h" |
Christophe Ricard | 57dacc2 | 2016-05-19 00:35:48 +0200 | [diff] [blame] | 35 | #include "tpm_tis_core.h" |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 36 | |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 37 | struct tpm_info { |
Jason Gunthorpe | 51dd43d | 2016-01-07 17:36:23 -0700 | [diff] [blame] | 38 | struct resource res; |
Jason Gunthorpe | ef7b81d | 2016-01-07 17:36:21 -0700 | [diff] [blame] | 39 | /* irq > 0 means: use irq $irq; |
| 40 | * irq = 0 means: autoprobe for an irq; |
| 41 | * irq = -1 means: no irq support |
| 42 | */ |
| 43 | int irq; |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 44 | }; |
| 45 | |
Christophe Ricard | 57dacc2 | 2016-05-19 00:35:48 +0200 | [diff] [blame] | 46 | struct tpm_tis_tcg_phy { |
| 47 | struct tpm_tis_data priv; |
Christophe Ricard | 4eea703 | 2016-03-31 22:56:55 +0200 | [diff] [blame] | 48 | void __iomem *iobase; |
Scot Doyle | 448e9c5 | 2014-09-24 22:41:10 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Christophe Ricard | 57dacc2 | 2016-05-19 00:35:48 +0200 | [diff] [blame] | 51 | static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data) |
| 52 | { |
| 53 | return container_of(data, struct tpm_tis_tcg_phy, priv); |
| 54 | } |
| 55 | |
Christophe Ricard | 41a5e1c | 2016-05-19 00:35:52 +0200 | [diff] [blame] | 56 | static bool interrupts = true; |
| 57 | module_param(interrupts, bool, 0444); |
| 58 | MODULE_PARM_DESC(interrupts, "Enable interrupts"); |
| 59 | |
| 60 | static bool itpm; |
| 61 | module_param(itpm, bool, 0444); |
| 62 | MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)"); |
| 63 | |
| 64 | static bool force; |
| 65 | #ifdef CONFIG_X86 |
| 66 | module_param(force, bool, 0444); |
| 67 | MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry"); |
| 68 | #endif |
| 69 | |
Randy Dunlap | 1560ffe6 | 2011-08-03 16:21:11 -0700 | [diff] [blame] | 70 | #if defined(CONFIG_PNP) && defined(CONFIG_ACPI) |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 71 | static int has_hid(struct acpi_device *dev, const char *hid) |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 72 | { |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 73 | struct acpi_hardware_id *id; |
| 74 | |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 75 | list_for_each_entry(id, &dev->pnp.ids, list) |
| 76 | if (!strcmp(hid, id->id)) |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 77 | return 1; |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 81 | |
| 82 | static inline int is_itpm(struct acpi_device *dev) |
| 83 | { |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 84 | if (!dev) |
| 85 | return 0; |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 86 | return has_hid(dev, "INTC0102"); |
| 87 | } |
Randy Dunlap | 1560ffe6 | 2011-08-03 16:21:11 -0700 | [diff] [blame] | 88 | #else |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 89 | static inline int is_itpm(struct acpi_device *dev) |
Randy Dunlap | 1560ffe6 | 2011-08-03 16:21:11 -0700 | [diff] [blame] | 90 | { |
| 91 | return 0; |
| 92 | } |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 93 | #endif |
| 94 | |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 95 | #if defined(CONFIG_ACPI) |
| 96 | #define DEVICE_IS_TPM2 1 |
| 97 | |
| 98 | static const struct acpi_device_id tpm_acpi_tbl[] = { |
| 99 | {"MSFT0101", DEVICE_IS_TPM2}, |
| 100 | {}, |
| 101 | }; |
| 102 | MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl); |
| 103 | |
| 104 | static int check_acpi_tpm2(struct device *dev) |
| 105 | { |
| 106 | const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev); |
| 107 | struct acpi_table_tpm2 *tbl; |
| 108 | acpi_status st; |
| 109 | |
| 110 | if (!aid || aid->driver_data != DEVICE_IS_TPM2) |
| 111 | return 0; |
| 112 | |
| 113 | /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2 |
| 114 | * table is mandatory |
| 115 | */ |
| 116 | st = |
| 117 | acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl); |
| 118 | if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) { |
| 119 | dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n"); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
| 123 | /* The tpm2_crb driver handles this device */ |
| 124 | if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED) |
| 125 | return -ENODEV; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | #else |
| 130 | static int check_acpi_tpm2(struct device *dev) |
| 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | #endif |
| 135 | |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 136 | static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len, |
| 137 | u8 *result) |
| 138 | { |
| 139 | struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); |
| 140 | |
| 141 | while (len--) |
| 142 | *result++ = ioread8(phy->iobase + addr); |
Azhar Shaikh | 5e572ca | 2017-06-18 19:17:59 -0700 | [diff] [blame] | 143 | |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len, |
Arnd Bergmann | c37fbc0 | 2017-09-07 15:30:45 +0200 | [diff] [blame] | 148 | const u8 *value) |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 149 | { |
| 150 | struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); |
| 151 | |
| 152 | while (len--) |
| 153 | iowrite8(*value++, phy->iobase + addr); |
Azhar Shaikh | 5e572ca | 2017-06-18 19:17:59 -0700 | [diff] [blame] | 154 | |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result) |
| 159 | { |
| 160 | struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); |
| 161 | |
| 162 | *result = ioread16(phy->iobase + addr); |
Azhar Shaikh | 5e572ca | 2017-06-18 19:17:59 -0700 | [diff] [blame] | 163 | |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result) |
| 168 | { |
| 169 | struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); |
| 170 | |
| 171 | *result = ioread32(phy->iobase + addr); |
Azhar Shaikh | 5e572ca | 2017-06-18 19:17:59 -0700 | [diff] [blame] | 172 | |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value) |
| 177 | { |
| 178 | struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data); |
| 179 | |
| 180 | iowrite32(value, phy->iobase + addr); |
Azhar Shaikh | 5e572ca | 2017-06-18 19:17:59 -0700 | [diff] [blame] | 181 | |
Christophe Ricard | 1107d06 | 2016-05-19 00:35:49 +0200 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static const struct tpm_tis_phy_ops tpm_tcg = { |
| 186 | .read_bytes = tpm_tcg_read_bytes, |
| 187 | .write_bytes = tpm_tcg_write_bytes, |
| 188 | .read16 = tpm_tcg_read16, |
| 189 | .read32 = tpm_tcg_read32, |
| 190 | .write32 = tpm_tcg_write32, |
| 191 | }; |
| 192 | |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 193 | static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 194 | { |
Christophe Ricard | 57dacc2 | 2016-05-19 00:35:48 +0200 | [diff] [blame] | 195 | struct tpm_tis_tcg_phy *phy; |
Christophe Ricard | 41a5e1c | 2016-05-19 00:35:52 +0200 | [diff] [blame] | 196 | int irq = -1; |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 197 | int rc; |
| 198 | |
| 199 | rc = check_acpi_tpm2(dev); |
| 200 | if (rc) |
| 201 | return rc; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 202 | |
Christophe Ricard | 57dacc2 | 2016-05-19 00:35:48 +0200 | [diff] [blame] | 203 | phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL); |
| 204 | if (phy == NULL) |
Scot Doyle | 448e9c5 | 2014-09-24 22:41:10 +0000 | [diff] [blame] | 205 | return -ENOMEM; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 206 | |
Christophe Ricard | 57dacc2 | 2016-05-19 00:35:48 +0200 | [diff] [blame] | 207 | phy->iobase = devm_ioremap_resource(dev, &tpm_info->res); |
| 208 | if (IS_ERR(phy->iobase)) |
| 209 | return PTR_ERR(phy->iobase); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 210 | |
Christophe Ricard | 41a5e1c | 2016-05-19 00:35:52 +0200 | [diff] [blame] | 211 | if (interrupts) |
| 212 | irq = tpm_info->irq; |
Stefan Berger | 9519de3 | 2011-03-30 12:13:33 -0400 | [diff] [blame] | 213 | |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 214 | if (itpm || is_itpm(ACPI_COMPANION(dev))) |
Maciej S. Szmigiero | 1d70fe9 | 2017-01-13 22:37:00 +0100 | [diff] [blame] | 215 | phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND; |
Rajiv Andrade | 3507d61 | 2009-09-10 17:09:35 -0300 | [diff] [blame] | 216 | |
Christophe Ricard | 41a5e1c | 2016-05-19 00:35:52 +0200 | [diff] [blame] | 217 | return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg, |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 218 | ACPI_HANDLE(dev)); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 219 | } |
Stefan Berger | 9685431 | 2011-07-17 01:04:24 -0400 | [diff] [blame] | 220 | |
Shuah Khan | a2fa3fb | 2013-09-11 14:23:13 -0700 | [diff] [blame] | 221 | static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume); |
| 222 | |
Bill Pemberton | afc6d36 | 2012-11-19 13:22:42 -0500 | [diff] [blame] | 223 | static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev, |
Jason Gunthorpe | ef7b81d | 2016-01-07 17:36:21 -0700 | [diff] [blame] | 224 | const struct pnp_device_id *pnp_id) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 225 | { |
Jason Gunthorpe | ef7b81d | 2016-01-07 17:36:21 -0700 | [diff] [blame] | 226 | struct tpm_info tpm_info = {}; |
Jason Gunthorpe | 51dd43d | 2016-01-07 17:36:23 -0700 | [diff] [blame] | 227 | struct resource *res; |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 228 | |
Jason Gunthorpe | 51dd43d | 2016-01-07 17:36:23 -0700 | [diff] [blame] | 229 | res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0); |
| 230 | if (!res) |
| 231 | return -ENODEV; |
| 232 | tpm_info.res = *res; |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 233 | |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 234 | if (pnp_irq_valid(pnp_dev, 0)) |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 235 | tpm_info.irq = pnp_irq(pnp_dev, 0); |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 236 | else |
Jason Gunthorpe | ef7b81d | 2016-01-07 17:36:21 -0700 | [diff] [blame] | 237 | tpm_info.irq = -1; |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 238 | |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 239 | return tpm_tis_init(&pnp_dev->dev, &tpm_info); |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Bill Pemberton | 0bbed20 | 2012-11-19 13:24:36 -0500 | [diff] [blame] | 242 | static struct pnp_device_id tpm_pnp_tbl[] = { |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 243 | {"PNP0C31", 0}, /* TPM */ |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 244 | {"ATM1200", 0}, /* Atmel */ |
| 245 | {"IFX0102", 0}, /* Infineon */ |
| 246 | {"BCM0101", 0}, /* Broadcom */ |
LE DISEZ Erwan | 061991e | 2008-07-25 19:44:56 -0700 | [diff] [blame] | 247 | {"BCM0102", 0}, /* Broadcom */ |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 248 | {"NSC1200", 0}, /* National */ |
Marcin Obara | fb0e7e1 | 2008-07-10 17:30:42 -0700 | [diff] [blame] | 249 | {"ICO0102", 0}, /* Intel */ |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 250 | /* Add new here */ |
| 251 | {"", 0}, /* User Specified */ |
| 252 | {"", 0} /* Terminator */ |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 253 | }; |
Matt Domsch | 31bde71 | 2009-11-03 12:05:50 +1100 | [diff] [blame] | 254 | MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 255 | |
Bill Pemberton | 39af33f | 2012-11-19 13:26:26 -0500 | [diff] [blame] | 256 | static void tpm_tis_pnp_remove(struct pnp_dev *dev) |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 257 | { |
| 258 | struct tpm_chip *chip = pnp_get_drvdata(dev); |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 259 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 260 | tpm_chip_unregister(chip); |
| 261 | tpm_tis_remove(chip); |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 262 | } |
| 263 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 264 | static struct pnp_driver tis_pnp_driver = { |
| 265 | .name = "tpm_tis", |
| 266 | .id_table = tpm_pnp_tbl, |
| 267 | .probe = tpm_tis_pnp_init, |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 268 | .remove = tpm_tis_pnp_remove, |
Shuah Khan | a2fa3fb | 2013-09-11 14:23:13 -0700 | [diff] [blame] | 269 | .driver = { |
| 270 | .pm = &tpm_tis_pm, |
| 271 | }, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
Jérémy Lefaure | 33957a1 | 2017-10-01 15:30:51 -0400 | [diff] [blame] | 274 | #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2) |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 275 | module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id, |
| 276 | sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444); |
| 277 | MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe"); |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 278 | |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 279 | static struct platform_device *force_pdev; |
| 280 | |
| 281 | static int tpm_tis_plat_probe(struct platform_device *pdev) |
| 282 | { |
| 283 | struct tpm_info tpm_info = {}; |
| 284 | struct resource *res; |
| 285 | |
| 286 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 287 | if (res == NULL) { |
| 288 | dev_err(&pdev->dev, "no memory resource defined\n"); |
| 289 | return -ENODEV; |
| 290 | } |
| 291 | tpm_info.res = *res; |
| 292 | |
Jason Gunthorpe | fc0e132 | 2017-05-04 09:53:24 -0600 | [diff] [blame] | 293 | tpm_info.irq = platform_get_irq(pdev, 0); |
| 294 | if (tpm_info.irq <= 0) { |
Jason Gunthorpe | d27f81f | 2017-05-04 09:53:23 -0600 | [diff] [blame] | 295 | if (pdev != force_pdev) |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 296 | tpm_info.irq = -1; |
| 297 | else |
| 298 | /* When forcing auto probe the IRQ */ |
| 299 | tpm_info.irq = 0; |
| 300 | } |
| 301 | |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 302 | return tpm_tis_init(&pdev->dev, &tpm_info); |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static int tpm_tis_plat_remove(struct platform_device *pdev) |
| 306 | { |
| 307 | struct tpm_chip *chip = dev_get_drvdata(&pdev->dev); |
| 308 | |
| 309 | tpm_chip_unregister(chip); |
| 310 | tpm_tis_remove(chip); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Jason Gunthorpe | 420d439 | 2016-11-07 15:44:31 -0700 | [diff] [blame] | 315 | #ifdef CONFIG_OF |
| 316 | static const struct of_device_id tis_of_platform_match[] = { |
| 317 | {.compatible = "tcg,tpm-tis-mmio"}, |
| 318 | {}, |
| 319 | }; |
| 320 | MODULE_DEVICE_TABLE(of, tis_of_platform_match); |
| 321 | #endif |
| 322 | |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 323 | static struct platform_driver tis_drv = { |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 324 | .probe = tpm_tis_plat_probe, |
| 325 | .remove = tpm_tis_plat_remove, |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 326 | .driver = { |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 327 | .name = "tpm_tis", |
Rafael J. Wysocki | b633f05 | 2012-07-06 19:09:20 +0200 | [diff] [blame] | 328 | .pm = &tpm_tis_pm, |
Jason Gunthorpe | 420d439 | 2016-11-07 15:44:31 -0700 | [diff] [blame] | 329 | .of_match_table = of_match_ptr(tis_of_platform_match), |
Jason Gunthorpe | 4cb586a | 2017-05-04 09:53:25 -0600 | [diff] [blame] | 330 | .acpi_match_table = ACPI_PTR(tpm_acpi_tbl), |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 331 | }, |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 334 | static int tpm_tis_force_device(void) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 335 | { |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 336 | struct platform_device *pdev; |
| 337 | static const struct resource x86_resources[] = { |
| 338 | { |
| 339 | .start = 0xFED40000, |
| 340 | .end = 0xFED40000 + TIS_MEM_LEN - 1, |
| 341 | .flags = IORESOURCE_MEM, |
| 342 | }, |
| 343 | }; |
| 344 | |
Jarkko Sakkinen | 399235d | 2015-09-29 00:32:19 +0300 | [diff] [blame] | 345 | if (!force) |
| 346 | return 0; |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 347 | |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 348 | /* The driver core will match the name tpm_tis of the device to |
| 349 | * the tpm_tis platform driver and complete the setup via |
| 350 | * tpm_tis_plat_probe |
| 351 | */ |
| 352 | pdev = platform_device_register_simple("tpm_tis", -1, x86_resources, |
| 353 | ARRAY_SIZE(x86_resources)); |
| 354 | if (IS_ERR(pdev)) |
| 355 | return PTR_ERR(pdev); |
| 356 | force_pdev = pdev; |
| 357 | |
Wei Yongjun | 4fba3c3 | 2013-04-25 15:07:47 +0800 | [diff] [blame] | 358 | return 0; |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static int __init init_tis(void) |
| 362 | { |
| 363 | int rc; |
| 364 | |
| 365 | rc = tpm_tis_force_device(); |
| 366 | if (rc) |
| 367 | goto err_force; |
| 368 | |
| 369 | rc = platform_driver_register(&tis_drv); |
| 370 | if (rc) |
| 371 | goto err_platform; |
| 372 | |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 373 | |
| 374 | if (IS_ENABLED(CONFIG_PNP)) { |
| 375 | rc = pnp_register_driver(&tis_pnp_driver); |
| 376 | if (rc) |
| 377 | goto err_pnp; |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | |
| 382 | err_pnp: |
Wei Yongjun | 5939eaf | 2017-02-07 15:51:47 +0000 | [diff] [blame] | 383 | platform_driver_unregister(&tis_drv); |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 384 | err_platform: |
| 385 | if (force_pdev) |
| 386 | platform_device_unregister(force_pdev); |
| 387 | err_force: |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 388 | return rc; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static void __exit cleanup_tis(void) |
| 392 | { |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 393 | pnp_unregister_driver(&tis_pnp_driver); |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 394 | platform_driver_unregister(&tis_drv); |
Jason Gunthorpe | 0019482 | 2016-01-07 17:36:24 -0700 | [diff] [blame] | 395 | |
| 396 | if (force_pdev) |
| 397 | platform_device_unregister(force_pdev); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | module_init(init_tis); |
| 401 | module_exit(cleanup_tis); |
| 402 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 403 | MODULE_DESCRIPTION("TPM Driver"); |
| 404 | MODULE_VERSION("2.0"); |
| 405 | MODULE_LICENSE("GPL"); |