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 "tpm_tis_core.h"
36 /* irq > 0 means: use irq $irq;
37 * irq = 0 means: autoprobe for an irq;
38 * irq = -1 means: no irq support
43 struct tpm_tis_tcg_phy
{
44 struct tpm_tis_data priv
;
48 static inline struct tpm_tis_tcg_phy
*to_tpm_tis_tcg_phy(struct tpm_tis_data
*data
)
50 return container_of(data
, struct tpm_tis_tcg_phy
, priv
);
53 static bool interrupts
= true;
54 module_param(interrupts
, bool, 0444);
55 MODULE_PARM_DESC(interrupts
, "Enable interrupts");
58 module_param(itpm
, bool, 0444);
59 MODULE_PARM_DESC(itpm
, "Force iTPM workarounds (found on some Lenovo laptops)");
63 module_param(force
, bool, 0444);
64 MODULE_PARM_DESC(force
, "Force device probe rather than using ACPI entry");
67 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
68 static int has_hid(struct acpi_device
*dev
, const char *hid
)
70 struct acpi_hardware_id
*id
;
72 list_for_each_entry(id
, &dev
->pnp
.ids
, list
)
73 if (!strcmp(hid
, id
->id
))
79 static inline int is_itpm(struct acpi_device
*dev
)
81 return has_hid(dev
, "INTC0102");
84 static inline int is_itpm(struct acpi_device
*dev
)
90 static int tpm_tcg_read_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
93 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
96 *result
++ = ioread8(phy
->iobase
+ addr
);
100 static int tpm_tcg_write_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
103 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
106 iowrite8(*value
++, phy
->iobase
+ addr
);
110 static int tpm_tcg_read16(struct tpm_tis_data
*data
, u32 addr
, u16
*result
)
112 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
114 *result
= ioread16(phy
->iobase
+ addr
);
118 static int tpm_tcg_read32(struct tpm_tis_data
*data
, u32 addr
, u32
*result
)
120 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
122 *result
= ioread32(phy
->iobase
+ addr
);
126 static int tpm_tcg_write32(struct tpm_tis_data
*data
, u32 addr
, u32 value
)
128 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
130 iowrite32(value
, phy
->iobase
+ addr
);
134 static const struct tpm_tis_phy_ops tpm_tcg
= {
135 .read_bytes
= tpm_tcg_read_bytes
,
136 .write_bytes
= tpm_tcg_write_bytes
,
137 .read16
= tpm_tcg_read16
,
138 .read32
= tpm_tcg_read32
,
139 .write32
= tpm_tcg_write32
,
142 static int tpm_tis_init(struct device
*dev
, struct tpm_info
*tpm_info
,
143 acpi_handle acpi_dev_handle
)
145 struct tpm_tis_tcg_phy
*phy
;
148 phy
= devm_kzalloc(dev
, sizeof(struct tpm_tis_tcg_phy
), GFP_KERNEL
);
152 phy
->iobase
= devm_ioremap_resource(dev
, &tpm_info
->res
);
153 if (IS_ERR(phy
->iobase
))
154 return PTR_ERR(phy
->iobase
);
160 phy
->priv
.flags
|= TPM_TIS_ITPM_POSSIBLE
;
162 return tpm_tis_core_init(dev
, &phy
->priv
, irq
, &tpm_tcg
,
166 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
168 static int tpm_tis_pnp_init(struct pnp_dev
*pnp_dev
,
169 const struct pnp_device_id
*pnp_id
)
171 struct tpm_info tpm_info
= {};
172 acpi_handle acpi_dev_handle
= NULL
;
173 struct resource
*res
;
175 res
= pnp_get_resource(pnp_dev
, IORESOURCE_MEM
, 0);
180 if (pnp_irq_valid(pnp_dev
, 0))
181 tpm_info
.irq
= pnp_irq(pnp_dev
, 0);
185 if (pnp_acpi_device(pnp_dev
)) {
186 if (is_itpm(pnp_acpi_device(pnp_dev
)))
189 acpi_dev_handle
= ACPI_HANDLE(&pnp_dev
->dev
);
192 return tpm_tis_init(&pnp_dev
->dev
, &tpm_info
, acpi_dev_handle
);
195 static struct pnp_device_id tpm_pnp_tbl
[] = {
196 {"PNP0C31", 0}, /* TPM */
197 {"ATM1200", 0}, /* Atmel */
198 {"IFX0102", 0}, /* Infineon */
199 {"BCM0101", 0}, /* Broadcom */
200 {"BCM0102", 0}, /* Broadcom */
201 {"NSC1200", 0}, /* National */
202 {"ICO0102", 0}, /* Intel */
204 {"", 0}, /* User Specified */
205 {"", 0} /* Terminator */
207 MODULE_DEVICE_TABLE(pnp
, tpm_pnp_tbl
);
209 static void tpm_tis_pnp_remove(struct pnp_dev
*dev
)
211 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
213 tpm_chip_unregister(chip
);
214 tpm_tis_remove(chip
);
217 static struct pnp_driver tis_pnp_driver
= {
219 .id_table
= tpm_pnp_tbl
,
220 .probe
= tpm_tis_pnp_init
,
221 .remove
= tpm_tis_pnp_remove
,
227 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
228 module_param_string(hid
, tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
,
229 sizeof(tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
), 0444);
230 MODULE_PARM_DESC(hid
, "Set additional specific HID for this driver to probe");
233 static int tpm_check_resource(struct acpi_resource
*ares
, void *data
)
235 struct tpm_info
*tpm_info
= (struct tpm_info
*) data
;
238 if (acpi_dev_resource_interrupt(ares
, 0, &res
))
239 tpm_info
->irq
= res
.start
;
240 else if (acpi_dev_resource_memory(ares
, &res
)) {
242 tpm_info
->res
.name
= NULL
;
248 static int tpm_tis_acpi_init(struct acpi_device
*acpi_dev
)
250 struct acpi_table_tpm2
*tbl
;
252 struct list_head resources
;
253 struct tpm_info tpm_info
= {};
256 st
= acpi_get_table(ACPI_SIG_TPM2
, 1,
257 (struct acpi_table_header
**) &tbl
);
258 if (ACPI_FAILURE(st
) || tbl
->header
.length
< sizeof(*tbl
)) {
259 dev_err(&acpi_dev
->dev
,
260 FW_BUG
"failed to get TPM2 ACPI table\n");
264 if (tbl
->start_method
!= ACPI_TPM2_MEMORY_MAPPED
)
267 INIT_LIST_HEAD(&resources
);
269 ret
= acpi_dev_get_resources(acpi_dev
, &resources
, tpm_check_resource
,
274 acpi_dev_free_resource_list(&resources
);
276 if (resource_type(&tpm_info
.res
) != IORESOURCE_MEM
) {
277 dev_err(&acpi_dev
->dev
,
278 FW_BUG
"TPM2 ACPI table does not define a memory resource\n");
282 if (is_itpm(acpi_dev
))
285 return tpm_tis_init(&acpi_dev
->dev
, &tpm_info
, acpi_dev
->handle
);
288 static int tpm_tis_acpi_remove(struct acpi_device
*dev
)
290 struct tpm_chip
*chip
= dev_get_drvdata(&dev
->dev
);
292 tpm_chip_unregister(chip
);
293 tpm_tis_remove(chip
);
298 static struct acpi_device_id tpm_acpi_tbl
[] = {
299 {"MSFT0101", 0}, /* TPM 2.0 */
301 {"", 0}, /* User Specified */
302 {"", 0} /* Terminator */
304 MODULE_DEVICE_TABLE(acpi
, tpm_acpi_tbl
);
306 static struct acpi_driver tis_acpi_driver
= {
310 .add
= tpm_tis_acpi_init
,
311 .remove
= tpm_tis_acpi_remove
,
319 static struct platform_device
*force_pdev
;
321 static int tpm_tis_plat_probe(struct platform_device
*pdev
)
323 struct tpm_info tpm_info
= {};
324 struct resource
*res
;
326 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
328 dev_err(&pdev
->dev
, "no memory resource defined\n");
333 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
335 tpm_info
.irq
= res
->start
;
337 if (pdev
== force_pdev
)
340 /* When forcing auto probe the IRQ */
344 return tpm_tis_init(&pdev
->dev
, &tpm_info
, NULL
);
347 static int tpm_tis_plat_remove(struct platform_device
*pdev
)
349 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
351 tpm_chip_unregister(chip
);
352 tpm_tis_remove(chip
);
357 static struct platform_driver tis_drv
= {
358 .probe
= tpm_tis_plat_probe
,
359 .remove
= tpm_tis_plat_remove
,
366 static int tpm_tis_force_device(void)
368 struct platform_device
*pdev
;
369 static const struct resource x86_resources
[] = {
372 .end
= 0xFED40000 + TIS_MEM_LEN
- 1,
373 .flags
= IORESOURCE_MEM
,
380 /* The driver core will match the name tpm_tis of the device to
381 * the tpm_tis platform driver and complete the setup via
384 pdev
= platform_device_register_simple("tpm_tis", -1, x86_resources
,
385 ARRAY_SIZE(x86_resources
));
387 return PTR_ERR(pdev
);
393 static int __init
init_tis(void)
397 rc
= tpm_tis_force_device();
401 rc
= platform_driver_register(&tis_drv
);
406 rc
= acpi_bus_register_driver(&tis_acpi_driver
);
411 if (IS_ENABLED(CONFIG_PNP
)) {
412 rc
= pnp_register_driver(&tis_pnp_driver
);
421 acpi_bus_unregister_driver(&tis_acpi_driver
);
424 platform_device_unregister(force_pdev
);
427 platform_device_unregister(force_pdev
);
432 static void __exit
cleanup_tis(void)
434 pnp_unregister_driver(&tis_pnp_driver
);
436 acpi_bus_unregister_driver(&tis_acpi_driver
);
438 platform_driver_unregister(&tis_drv
);
441 platform_device_unregister(force_pdev
);
444 module_init(init_tis
);
445 module_exit(cleanup_tis
);
446 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
447 MODULE_DESCRIPTION("TPM Driver");
448 MODULE_VERSION("2.0");
449 MODULE_LICENSE("GPL");