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 <linux/of_device.h>
29 #include <linux/kernel.h>
31 #include "tpm_tis_core.h"
35 /* irq > 0 means: use irq $irq;
36 * irq = 0 means: autoprobe for an irq;
37 * irq = -1 means: no irq support
42 struct tpm_tis_tcg_phy {
43 struct tpm_tis_data priv;
47 static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
49 return container_of(data, struct tpm_tis_tcg_phy, priv);
52 static bool interrupts = true;
53 module_param(interrupts, bool, 0444);
54 MODULE_PARM_DESC(interrupts, "Enable interrupts");
57 module_param(itpm, bool, 0444);
58 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
62 module_param(force, bool, 0444);
63 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
66 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
67 static int has_hid(struct acpi_device *dev, const char *hid)
69 struct acpi_hardware_id *id;
71 list_for_each_entry(id, &dev->pnp.ids, list)
72 if (!strcmp(hid, id->id))
78 static inline int is_itpm(struct acpi_device *dev)
82 return has_hid(dev, "INTC0102");
85 static inline int is_itpm(struct acpi_device *dev)
91 #if defined(CONFIG_ACPI)
92 #define DEVICE_IS_TPM2 1
94 static const struct acpi_device_id tpm_acpi_tbl[] = {
95 {"MSFT0101", DEVICE_IS_TPM2},
98 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
100 static int check_acpi_tpm2(struct device *dev)
102 const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
103 struct acpi_table_tpm2 *tbl;
106 if (!aid || aid->driver_data != DEVICE_IS_TPM2)
109 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
113 acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
114 if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
115 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
119 /* The tpm2_crb driver handles this device */
120 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
126 static int check_acpi_tpm2(struct device *dev)
132 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
135 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
138 *result++ = ioread8(phy->iobase + addr);
143 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
146 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
149 iowrite8(*value++, phy->iobase + addr);
154 static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
156 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
158 *result = ioread16(phy->iobase + addr);
163 static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
165 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
167 *result = ioread32(phy->iobase + addr);
172 static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
174 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
176 iowrite32(value, phy->iobase + addr);
181 static const struct tpm_tis_phy_ops tpm_tcg = {
182 .read_bytes = tpm_tcg_read_bytes,
183 .write_bytes = tpm_tcg_write_bytes,
184 .read16 = tpm_tcg_read16,
185 .read32 = tpm_tcg_read32,
186 .write32 = tpm_tcg_write32,
189 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
191 struct tpm_tis_tcg_phy *phy;
195 rc = check_acpi_tpm2(dev);
199 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
203 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
204 if (IS_ERR(phy->iobase))
205 return PTR_ERR(phy->iobase);
210 if (itpm || is_itpm(ACPI_COMPANION(dev)))
211 phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
213 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
217 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
219 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
220 const struct pnp_device_id *pnp_id)
222 struct tpm_info tpm_info = {};
223 struct resource *res;
225 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
230 if (pnp_irq_valid(pnp_dev, 0))
231 tpm_info.irq = pnp_irq(pnp_dev, 0);
235 return tpm_tis_init(&pnp_dev->dev, &tpm_info);
238 static struct pnp_device_id tpm_pnp_tbl[] = {
239 {"PNP0C31", 0}, /* TPM */
240 {"ATM1200", 0}, /* Atmel */
241 {"IFX0102", 0}, /* Infineon */
242 {"BCM0101", 0}, /* Broadcom */
243 {"BCM0102", 0}, /* Broadcom */
244 {"NSC1200", 0}, /* National */
245 {"ICO0102", 0}, /* Intel */
247 {"", 0}, /* User Specified */
248 {"", 0} /* Terminator */
250 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
252 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
254 struct tpm_chip *chip = pnp_get_drvdata(dev);
256 tpm_chip_unregister(chip);
257 tpm_tis_remove(chip);
260 static struct pnp_driver tis_pnp_driver = {
262 .id_table = tpm_pnp_tbl,
263 .probe = tpm_tis_pnp_init,
264 .remove = tpm_tis_pnp_remove,
270 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
271 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
272 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
273 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
275 static struct platform_device *force_pdev;
277 static int tpm_tis_plat_probe(struct platform_device *pdev)
279 struct tpm_info tpm_info = {};
280 struct resource *res;
282 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
284 dev_err(&pdev->dev, "no memory resource defined\n");
289 tpm_info.irq = platform_get_irq(pdev, 0);
290 if (tpm_info.irq <= 0) {
291 if (pdev != force_pdev)
294 /* When forcing auto probe the IRQ */
298 return tpm_tis_init(&pdev->dev, &tpm_info);
301 static int tpm_tis_plat_remove(struct platform_device *pdev)
303 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
305 tpm_chip_unregister(chip);
306 tpm_tis_remove(chip);
312 static const struct of_device_id tis_of_platform_match[] = {
313 {.compatible = "tcg,tpm-tis-mmio"},
316 MODULE_DEVICE_TABLE(of, tis_of_platform_match);
319 static struct platform_driver tis_drv = {
320 .probe = tpm_tis_plat_probe,
321 .remove = tpm_tis_plat_remove,
325 .of_match_table = of_match_ptr(tis_of_platform_match),
326 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
330 static int tpm_tis_force_device(void)
332 struct platform_device *pdev;
333 static const struct resource x86_resources[] = {
336 .end = 0xFED40000 + TIS_MEM_LEN - 1,
337 .flags = IORESOURCE_MEM,
344 /* The driver core will match the name tpm_tis of the device to
345 * the tpm_tis platform driver and complete the setup via
348 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
349 ARRAY_SIZE(x86_resources));
351 return PTR_ERR(pdev);
357 static int __init init_tis(void)
361 rc = tpm_tis_force_device();
365 rc = platform_driver_register(&tis_drv);
370 if (IS_ENABLED(CONFIG_PNP)) {
371 rc = pnp_register_driver(&tis_pnp_driver);
379 platform_driver_unregister(&tis_drv);
382 platform_device_unregister(force_pdev);
387 static void __exit cleanup_tis(void)
389 pnp_unregister_driver(&tis_pnp_driver);
390 platform_driver_unregister(&tis_drv);
393 platform_device_unregister(force_pdev);
396 module_init(init_tis);
397 module_exit(cleanup_tis);
399 MODULE_DESCRIPTION("TPM Driver");
400 MODULE_VERSION("2.0");
401 MODULE_LICENSE("GPL");