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>
34 #include "tpm_tis_core.h"
38 /* irq > 0 means: use irq $irq;
39 * irq = 0 means: autoprobe for an irq;
40 * irq = -1 means: no irq support
45 struct tpm_tis_tcg_phy
{
46 struct tpm_tis_data priv
;
50 static inline struct tpm_tis_tcg_phy
*to_tpm_tis_tcg_phy(struct tpm_tis_data
*data
)
52 return container_of(data
, struct tpm_tis_tcg_phy
, priv
);
55 static bool interrupts
= true;
56 module_param(interrupts
, bool, 0444);
57 MODULE_PARM_DESC(interrupts
, "Enable interrupts");
60 module_param(itpm
, bool, 0444);
61 MODULE_PARM_DESC(itpm
, "Force iTPM workarounds (found on some Lenovo laptops)");
65 module_param(force
, bool, 0444);
66 MODULE_PARM_DESC(force
, "Force device probe rather than using ACPI entry");
69 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
70 static int has_hid(struct acpi_device
*dev
, const char *hid
)
72 struct acpi_hardware_id
*id
;
74 list_for_each_entry(id
, &dev
->pnp
.ids
, list
)
75 if (!strcmp(hid
, id
->id
))
81 static inline int is_itpm(struct acpi_device
*dev
)
83 return has_hid(dev
, "INTC0102");
86 static inline int is_itpm(struct acpi_device
*dev
)
92 static int tpm_tcg_read_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
95 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
98 *result
++ = ioread8(phy
->iobase
+ addr
);
102 static int tpm_tcg_write_bytes(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
105 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
108 iowrite8(*value
++, phy
->iobase
+ addr
);
112 static int tpm_tcg_read16(struct tpm_tis_data
*data
, u32 addr
, u16
*result
)
114 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
116 *result
= ioread16(phy
->iobase
+ addr
);
120 static int tpm_tcg_read32(struct tpm_tis_data
*data
, u32 addr
, u32
*result
)
122 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
124 *result
= ioread32(phy
->iobase
+ addr
);
128 static int tpm_tcg_write32(struct tpm_tis_data
*data
, u32 addr
, u32 value
)
130 struct tpm_tis_tcg_phy
*phy
= to_tpm_tis_tcg_phy(data
);
132 iowrite32(value
, phy
->iobase
+ addr
);
136 static const struct tpm_tis_phy_ops tpm_tcg
= {
137 .read_bytes
= tpm_tcg_read_bytes
,
138 .write_bytes
= tpm_tcg_write_bytes
,
139 .read16
= tpm_tcg_read16
,
140 .read32
= tpm_tcg_read32
,
141 .write32
= tpm_tcg_write32
,
144 static int tpm_tis_init(struct device
*dev
, struct tpm_info
*tpm_info
,
145 acpi_handle acpi_dev_handle
)
147 struct tpm_tis_tcg_phy
*phy
;
150 phy
= devm_kzalloc(dev
, sizeof(struct tpm_tis_tcg_phy
), GFP_KERNEL
);
154 phy
->iobase
= devm_ioremap_resource(dev
, &tpm_info
->res
);
155 if (IS_ERR(phy
->iobase
))
156 return PTR_ERR(phy
->iobase
);
162 phy
->priv
.flags
|= TPM_TIS_ITPM_WORKAROUND
;
164 return tpm_tis_core_init(dev
, &phy
->priv
, irq
, &tpm_tcg
,
168 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
170 static int tpm_tis_pnp_init(struct pnp_dev
*pnp_dev
,
171 const struct pnp_device_id
*pnp_id
)
173 struct tpm_info tpm_info
= {};
174 acpi_handle acpi_dev_handle
= NULL
;
175 struct resource
*res
;
177 res
= pnp_get_resource(pnp_dev
, IORESOURCE_MEM
, 0);
182 if (pnp_irq_valid(pnp_dev
, 0))
183 tpm_info
.irq
= pnp_irq(pnp_dev
, 0);
187 if (pnp_acpi_device(pnp_dev
)) {
188 if (is_itpm(pnp_acpi_device(pnp_dev
)))
191 acpi_dev_handle
= ACPI_HANDLE(&pnp_dev
->dev
);
194 return tpm_tis_init(&pnp_dev
->dev
, &tpm_info
, acpi_dev_handle
);
197 static struct pnp_device_id tpm_pnp_tbl
[] = {
198 {"PNP0C31", 0}, /* TPM */
199 {"ATM1200", 0}, /* Atmel */
200 {"IFX0102", 0}, /* Infineon */
201 {"BCM0101", 0}, /* Broadcom */
202 {"BCM0102", 0}, /* Broadcom */
203 {"NSC1200", 0}, /* National */
204 {"ICO0102", 0}, /* Intel */
206 {"", 0}, /* User Specified */
207 {"", 0} /* Terminator */
209 MODULE_DEVICE_TABLE(pnp
, tpm_pnp_tbl
);
211 static void tpm_tis_pnp_remove(struct pnp_dev
*dev
)
213 struct tpm_chip
*chip
= pnp_get_drvdata(dev
);
215 tpm_chip_unregister(chip
);
216 tpm_tis_remove(chip
);
219 static struct pnp_driver tis_pnp_driver
= {
221 .id_table
= tpm_pnp_tbl
,
222 .probe
= tpm_tis_pnp_init
,
223 .remove
= tpm_tis_pnp_remove
,
229 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
230 module_param_string(hid
, tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
,
231 sizeof(tpm_pnp_tbl
[TIS_HID_USR_IDX
].id
), 0444);
232 MODULE_PARM_DESC(hid
, "Set additional specific HID for this driver to probe");
235 static int tpm_check_resource(struct acpi_resource
*ares
, void *data
)
237 struct tpm_info
*tpm_info
= (struct tpm_info
*) data
;
240 if (acpi_dev_resource_interrupt(ares
, 0, &res
))
241 tpm_info
->irq
= res
.start
;
242 else if (acpi_dev_resource_memory(ares
, &res
)) {
244 tpm_info
->res
.name
= NULL
;
250 static int tpm_tis_acpi_init(struct acpi_device
*acpi_dev
)
252 struct acpi_table_tpm2
*tbl
;
254 struct list_head resources
;
255 struct tpm_info tpm_info
= {};
258 st
= acpi_get_table(ACPI_SIG_TPM2
, 1,
259 (struct acpi_table_header
**) &tbl
);
260 if (ACPI_FAILURE(st
) || tbl
->header
.length
< sizeof(*tbl
)) {
261 dev_err(&acpi_dev
->dev
,
262 FW_BUG
"failed to get TPM2 ACPI table\n");
266 if (tbl
->start_method
!= ACPI_TPM2_MEMORY_MAPPED
)
269 INIT_LIST_HEAD(&resources
);
271 ret
= acpi_dev_get_resources(acpi_dev
, &resources
, tpm_check_resource
,
276 acpi_dev_free_resource_list(&resources
);
278 if (resource_type(&tpm_info
.res
) != IORESOURCE_MEM
) {
279 dev_err(&acpi_dev
->dev
,
280 FW_BUG
"TPM2 ACPI table does not define a memory resource\n");
284 if (is_itpm(acpi_dev
))
287 return tpm_tis_init(&acpi_dev
->dev
, &tpm_info
, acpi_dev
->handle
);
290 static int tpm_tis_acpi_remove(struct acpi_device
*dev
)
292 struct tpm_chip
*chip
= dev_get_drvdata(&dev
->dev
);
294 tpm_chip_unregister(chip
);
295 tpm_tis_remove(chip
);
300 static struct acpi_device_id tpm_acpi_tbl
[] = {
301 {"MSFT0101", 0}, /* TPM 2.0 */
303 {"", 0}, /* User Specified */
304 {"", 0} /* Terminator */
306 MODULE_DEVICE_TABLE(acpi
, tpm_acpi_tbl
);
308 static struct acpi_driver tis_acpi_driver
= {
312 .add
= tpm_tis_acpi_init
,
313 .remove
= tpm_tis_acpi_remove
,
321 static struct platform_device
*force_pdev
;
323 static int tpm_tis_plat_probe(struct platform_device
*pdev
)
325 struct tpm_info tpm_info
= {};
326 struct resource
*res
;
328 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
330 dev_err(&pdev
->dev
, "no memory resource defined\n");
335 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
337 tpm_info
.irq
= res
->start
;
339 if (pdev
== force_pdev
)
342 /* When forcing auto probe the IRQ */
346 return tpm_tis_init(&pdev
->dev
, &tpm_info
, NULL
);
349 static int tpm_tis_plat_remove(struct platform_device
*pdev
)
351 struct tpm_chip
*chip
= dev_get_drvdata(&pdev
->dev
);
353 tpm_chip_unregister(chip
);
354 tpm_tis_remove(chip
);
360 static const struct of_device_id tis_of_platform_match
[] = {
361 {.compatible
= "tcg,tpm-tis-mmio"},
364 MODULE_DEVICE_TABLE(of
, tis_of_platform_match
);
367 static struct platform_driver tis_drv
= {
368 .probe
= tpm_tis_plat_probe
,
369 .remove
= tpm_tis_plat_remove
,
373 .of_match_table
= of_match_ptr(tis_of_platform_match
),
377 static int tpm_tis_force_device(void)
379 struct platform_device
*pdev
;
380 static const struct resource x86_resources
[] = {
383 .end
= 0xFED40000 + TIS_MEM_LEN
- 1,
384 .flags
= IORESOURCE_MEM
,
391 /* The driver core will match the name tpm_tis of the device to
392 * the tpm_tis platform driver and complete the setup via
395 pdev
= platform_device_register_simple("tpm_tis", -1, x86_resources
,
396 ARRAY_SIZE(x86_resources
));
398 return PTR_ERR(pdev
);
404 static int __init
init_tis(void)
408 rc
= tpm_tis_force_device();
412 rc
= platform_driver_register(&tis_drv
);
417 rc
= acpi_bus_register_driver(&tis_acpi_driver
);
422 if (IS_ENABLED(CONFIG_PNP
)) {
423 rc
= pnp_register_driver(&tis_pnp_driver
);
432 acpi_bus_unregister_driver(&tis_acpi_driver
);
435 platform_driver_unregister(&tis_drv
);
438 platform_device_unregister(force_pdev
);
443 static void __exit
cleanup_tis(void)
445 pnp_unregister_driver(&tis_pnp_driver
);
447 acpi_bus_unregister_driver(&tis_acpi_driver
);
449 platform_driver_unregister(&tis_drv
);
452 platform_device_unregister(force_pdev
);
455 module_init(init_tis
);
456 module_exit(cleanup_tis
);
457 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
458 MODULE_DESCRIPTION("TPM Driver");
459 MODULE_VERSION("2.0");
460 MODULE_LICENSE("GPL");