2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014, 2015 Intel Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
14 * This device driver implements the TPM interface as defined in
15 * the TCG TPM Interface Spec version 1.2, revision 1.0.
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
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/pnp.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/acpi.h>
30 #include <linux/freezer.h>
32 #include <linux/of_device.h>
33 #include <linux/kernel.h>
35 #include "tpm_tis_core.h"
39 /* irq > 0 means: use irq $irq;
40 * irq = 0 means: autoprobe for an irq;
41 * irq = -1 means: no irq support
46 struct tpm_tis_tcg_phy
{
47 struct tpm_tis_data priv
;
51 static inline struct tpm_tis_tcg_phy
*to_tpm_tis_tcg_phy(struct tpm_tis_data
*data
)
53 return container_of(data
, struct tpm_tis_tcg_phy
, priv
);
56 static bool interrupts
= true;
57 module_param(interrupts
, bool, 0444);
58 MODULE_PARM_DESC(interrupts
, "Enable interrupts");
61 module_param(itpm
, bool, 0444);
62 MODULE_PARM_DESC(itpm
, "Force iTPM workarounds (found on some Lenovo laptops)");
66 module_param(force
, bool, 0444);
67 MODULE_PARM_DESC(force
, "Force device probe rather than using ACPI entry");
70 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
71 static int has_hid(struct acpi_device
*dev
, const char *hid
)
73 struct acpi_hardware_id
*id
;
75 list_for_each_entry(id
, &dev
->pnp
.ids
, list
)
76 if (!strcmp(hid
, id
->id
))
82 static inline int is_itpm(struct acpi_device
*dev
)
86 return has_hid(dev
, "INTC0102");
89 static inline int is_itpm(struct acpi_device
*dev
)
95 #if defined(CONFIG_ACPI)
96 #define DEVICE_IS_TPM2 1
98 static const struct acpi_device_id tpm_acpi_tbl
[] = {
99 {"MSFT0101", DEVICE_IS_TPM2
},
102 MODULE_DEVICE_TABLE(acpi
, tpm_acpi_tbl
);
104 static int check_acpi_tpm2(struct device
*dev
)
106 const struct acpi_device_id
*aid
= acpi_match_device(tpm_acpi_tbl
, dev
);
107 struct acpi_table_tpm2
*tbl
;
110 if (!aid
|| aid
->driver_data
!= DEVICE_IS_TPM2
)
113 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
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");
123 /* The tpm2_crb driver handles this device */
124 if (tbl
->start_method
!= ACPI_TPM2_MEMORY_MAPPED
)
130 static int check_acpi_tpm2(struct device
*dev
)
136 static int tpm_tcg_read_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
139 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
142 *result
++ = ioread8(phy
->iobase
+ addr
);
147 static int tpm_tcg_write_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
150 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
153 iowrite8(*value
++, phy
->iobase
+ addr
);
158 static int tpm_tcg_read16(struct tpm_tis_data
*data
, u32 addr
, u16
*result
)
160 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
162 *result
= ioread16(phy
->iobase
+ addr
);
167 static int tpm_tcg_read32(struct tpm_tis_data
*data
, u32 addr
, u32
*result
)
169 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
171 *result
= ioread32(phy
->iobase
+ addr
);
176 static int tpm_tcg_write32(struct tpm_tis_data
*data
, u32 addr
, u32 value
)
178 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
180 iowrite32(value
, phy
->iobase
+ addr
);
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
,
193 static int tpm_tis_init(struct device
*dev
, struct tpm_info
*tpm_info
)
195 struct tpm_tis_tcg_phy
*phy
;
199 rc
= check_acpi_tpm2(dev
);
203 phy
= devm_kzalloc(dev
, sizeof(struct tpm_tis_tcg_phy
), GFP_KERNEL
);
207 phy
->iobase
= devm_ioremap_resource(dev
, &tpm_info
->res
);
208 if (IS_ERR(phy
->iobase
))
209 return PTR_ERR(phy
->iobase
);
214 if (itpm
|| is_itpm(ACPI_COMPANION(dev
)))
215 phy
->priv
.flags
|= TPM_TIS_ITPM_WORKAROUND
;
217 return tpm_tis_core_init(dev
, &phy
->priv
, irq
, &tpm_tcg
,
221 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
223 static int tpm_tis_pnp_init(struct pnp_dev
*pnp_dev
,
224 const struct pnp_device_id
*pnp_id
)
226 struct tpm_info tpm_info
= {};
227 struct resource
*res
;
229 res
= pnp_get_resource(pnp_dev
, IORESOURCE_MEM
, 0);
234 if (pnp_irq_valid(pnp_dev
, 0))
235 tpm_info
.irq
= pnp_irq(pnp_dev
, 0);
239 return tpm_tis_init(&pnp_dev
->dev
, &tpm_info
);
242 static struct pnp_device_id tpm_pnp_tbl
[] = {
243 {"PNP0C31", 0}, /* TPM */
244 {"ATM1200", 0}, /* Atmel */
245 {"IFX0102", 0}, /* Infineon */
246 {"BCM0101", 0}, /* Broadcom */
247 {"BCM0102", 0}, /* Broadcom */
248 {"NSC1200", 0}, /* National */
249 {"ICO0102", 0}, /* Intel */
251 {"", 0}, /* User Specified */
252 {"", 0} /* Terminator */
254 MODULE_DEVICE_TABLE(pnp
, tpm_pnp_tbl
);
256 static void tpm_tis_pnp_remove(struct pnp_dev
*dev
)
258 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
260 tpm_chip_unregister(chip
);
261 tpm_tis_remove(chip
);
264 static struct pnp_driver tis_pnp_driver
= {
266 .id_table
= tpm_pnp_tbl
,
267 .probe
= tpm_tis_pnp_init
,
268 .remove
= tpm_tis_pnp_remove
,
274 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
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");
279 static struct platform_device
*force_pdev
;
281 static int tpm_tis_plat_probe(struct platform_device
*pdev
)
283 struct tpm_info tpm_info
= {};
284 struct resource
*res
;
286 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
288 dev_err(&pdev
->dev
, "no memory resource defined\n");
293 tpm_info
.irq
= platform_get_irq(pdev
, 0);
294 if (tpm_info
.irq
<= 0) {
295 if (pdev
!= force_pdev
)
298 /* When forcing auto probe the IRQ */
302 return tpm_tis_init(&pdev
->dev
, &tpm_info
);
305 static int tpm_tis_plat_remove(struct platform_device
*pdev
)
307 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
309 tpm_chip_unregister(chip
);
310 tpm_tis_remove(chip
);
316 static const struct of_device_id tis_of_platform_match
[] = {
317 {.compatible
= "tcg,tpm-tis-mmio"},
320 MODULE_DEVICE_TABLE(of
, tis_of_platform_match
);
323 static struct platform_driver tis_drv
= {
324 .probe
= tpm_tis_plat_probe
,
325 .remove
= tpm_tis_plat_remove
,
329 .of_match_table
= of_match_ptr(tis_of_platform_match
),
330 .acpi_match_table
= ACPI_PTR(tpm_acpi_tbl
),
334 static int tpm_tis_force_device(void)
336 struct platform_device
*pdev
;
337 static const struct resource x86_resources
[] = {
340 .end
= 0xFED40000 + TIS_MEM_LEN
- 1,
341 .flags
= IORESOURCE_MEM
,
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
352 pdev
= platform_device_register_simple("tpm_tis", -1, x86_resources
,
353 ARRAY_SIZE(x86_resources
));
355 return PTR_ERR(pdev
);
361 static int __init
init_tis(void)
365 rc
= tpm_tis_force_device();
369 rc
= platform_driver_register(&tis_drv
);
374 if (IS_ENABLED(CONFIG_PNP
)) {
375 rc
= pnp_register_driver(&tis_pnp_driver
);
383 platform_driver_unregister(&tis_drv
);
386 platform_device_unregister(force_pdev
);
391 static void __exit
cleanup_tis(void)
393 pnp_unregister_driver(&tis_pnp_driver
);
394 platform_driver_unregister(&tis_drv
);
397 platform_device_unregister(force_pdev
);
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");