blob: b14d4aa97af87afebcf9f84ed9941f8b993fa77c [file] [log] [blame]
Leendert van Doorn27084ef2006-04-22 02:38:03 -07001/*
2 * Copyright (C) 2005, 2006 IBM Corporation
Jarkko Sakkinen399235d2015-09-29 00:32:19 +03003 * Copyright (C) 2014, 2015 Intel Corporation
Leendert van Doorn27084ef2006-04-22 02:38:03 -07004 *
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
Kent Yoder8e81cc12007-08-22 14:01:04 -07009 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 *
Leendert van Doorn27084ef2006-04-22 02:38:03 -070011 * 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 Hall57135562006-04-22 02:39:44 -070022#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070025#include <linux/pnp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070027#include <linux/interrupt.h>
28#include <linux/wait.h>
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040029#include <linux/acpi.h>
Stefan Berger20b87bb2011-03-30 12:13:31 -040030#include <linux/freezer.h>
Jason Gunthorpe420d4392016-11-07 15:44:31 -070031#include <linux/of.h>
32#include <linux/of_device.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070033#include "tpm.h"
Christophe Ricard57dacc22016-05-19 00:35:48 +020034#include "tpm_tis_core.h"
Leendert van Doorn27084ef2006-04-22 02:38:03 -070035
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030036struct tpm_info {
Jason Gunthorpe51dd43d2016-01-07 17:36:23 -070037 struct resource res;
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -070038 /* irq > 0 means: use irq $irq;
39 * irq = 0 means: autoprobe for an irq;
40 * irq = -1 means: no irq support
41 */
42 int irq;
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030043};
44
Christophe Ricard57dacc22016-05-19 00:35:48 +020045struct tpm_tis_tcg_phy {
46 struct tpm_tis_data priv;
Christophe Ricard4eea7032016-03-31 22:56:55 +020047 void __iomem *iobase;
Scot Doyle448e9c52014-09-24 22:41:10 +000048};
49
Christophe Ricard57dacc22016-05-19 00:35:48 +020050static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
51{
52 return container_of(data, struct tpm_tis_tcg_phy, priv);
53}
54
Christophe Ricard41a5e1c2016-05-19 00:35:52 +020055static bool interrupts = true;
56module_param(interrupts, bool, 0444);
57MODULE_PARM_DESC(interrupts, "Enable interrupts");
58
59static bool itpm;
60module_param(itpm, bool, 0444);
61MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
62
63static bool force;
64#ifdef CONFIG_X86
65module_param(force, bool, 0444);
66MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
67#endif
68
Randy Dunlap1560ffe62011-08-03 16:21:11 -070069#if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030070static int has_hid(struct acpi_device *dev, const char *hid)
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040071{
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040072 struct acpi_hardware_id *id;
73
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030074 list_for_each_entry(id, &dev->pnp.ids, list)
75 if (!strcmp(hid, id->id))
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040076 return 1;
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040077
78 return 0;
79}
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030080
81static inline int is_itpm(struct acpi_device *dev)
82{
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -060083 if (!dev)
84 return 0;
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030085 return has_hid(dev, "INTC0102");
86}
Randy Dunlap1560ffe62011-08-03 16:21:11 -070087#else
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030088static inline int is_itpm(struct acpi_device *dev)
Randy Dunlap1560ffe62011-08-03 16:21:11 -070089{
90 return 0;
91}
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040092#endif
93
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -060094#if defined(CONFIG_ACPI)
95#define DEVICE_IS_TPM2 1
96
97static const struct acpi_device_id tpm_acpi_tbl[] = {
98 {"MSFT0101", DEVICE_IS_TPM2},
99 {},
100};
101MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
102
103static int check_acpi_tpm2(struct device *dev)
104{
105 const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
106 struct acpi_table_tpm2 *tbl;
107 acpi_status st;
108
109 if (!aid || aid->driver_data != DEVICE_IS_TPM2)
110 return 0;
111
112 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
113 * table is mandatory
114 */
115 st =
116 acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
117 if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
118 dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
119 return -EINVAL;
120 }
121
122 /* The tpm2_crb driver handles this device */
123 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
124 return -ENODEV;
125
126 return 0;
127}
128#else
129static int check_acpi_tpm2(struct device *dev)
130{
131 return 0;
132}
133#endif
134
Christophe Ricard1107d062016-05-19 00:35:49 +0200135static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
136 u8 *result)
137{
138 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
139
140 while (len--)
141 *result++ = ioread8(phy->iobase + addr);
142 return 0;
143}
144
145static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
146 u8 *value)
147{
148 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
149
150 while (len--)
151 iowrite8(*value++, phy->iobase + addr);
152 return 0;
153}
154
155static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
156{
157 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
158
159 *result = ioread16(phy->iobase + addr);
160 return 0;
161}
162
163static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
164{
165 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
166
167 *result = ioread32(phy->iobase + addr);
168 return 0;
169}
170
171static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
172{
173 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
174
175 iowrite32(value, phy->iobase + addr);
176 return 0;
177}
178
179static const struct tpm_tis_phy_ops tpm_tcg = {
180 .read_bytes = tpm_tcg_read_bytes,
181 .write_bytes = tpm_tcg_write_bytes,
182 .read16 = tpm_tcg_read16,
183 .read32 = tpm_tcg_read32,
184 .write32 = tpm_tcg_write32,
185};
186
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600187static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700188{
Christophe Ricard57dacc22016-05-19 00:35:48 +0200189 struct tpm_tis_tcg_phy *phy;
Christophe Ricard41a5e1c2016-05-19 00:35:52 +0200190 int irq = -1;
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600191 int rc;
192
193 rc = check_acpi_tpm2(dev);
194 if (rc)
195 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700196
Christophe Ricard57dacc22016-05-19 00:35:48 +0200197 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
198 if (phy == NULL)
Scot Doyle448e9c52014-09-24 22:41:10 +0000199 return -ENOMEM;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800200
Christophe Ricard57dacc22016-05-19 00:35:48 +0200201 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
202 if (IS_ERR(phy->iobase))
203 return PTR_ERR(phy->iobase);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700204
Christophe Ricard41a5e1c2016-05-19 00:35:52 +0200205 if (interrupts)
206 irq = tpm_info->irq;
Stefan Berger9519de32011-03-30 12:13:33 -0400207
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600208 if (itpm || is_itpm(ACPI_COMPANION(dev)))
Maciej S. Szmigiero1d70fe92017-01-13 22:37:00 +0100209 phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
Rajiv Andrade3507d612009-09-10 17:09:35 -0300210
Christophe Ricard41a5e1c2016-05-19 00:35:52 +0200211 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600212 ACPI_HANDLE(dev));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700213}
Stefan Berger96854312011-07-17 01:04:24 -0400214
Shuah Khana2fa3fb2013-09-11 14:23:13 -0700215static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
216
Bill Pembertonafc6d362012-11-19 13:22:42 -0500217static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -0700218 const struct pnp_device_id *pnp_id)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700219{
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -0700220 struct tpm_info tpm_info = {};
Jason Gunthorpe51dd43d2016-01-07 17:36:23 -0700221 struct resource *res;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700222
Jason Gunthorpe51dd43d2016-01-07 17:36:23 -0700223 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
224 if (!res)
225 return -ENODEV;
226 tpm_info.res = *res;
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700227
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700228 if (pnp_irq_valid(pnp_dev, 0))
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300229 tpm_info.irq = pnp_irq(pnp_dev, 0);
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700230 else
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -0700231 tpm_info.irq = -1;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700232
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600233 return tpm_tis_init(&pnp_dev->dev, &tpm_info);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700234}
235
Bill Pemberton0bbed202012-11-19 13:24:36 -0500236static struct pnp_device_id tpm_pnp_tbl[] = {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700237 {"PNP0C31", 0}, /* TPM */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700238 {"ATM1200", 0}, /* Atmel */
239 {"IFX0102", 0}, /* Infineon */
240 {"BCM0101", 0}, /* Broadcom */
LE DISEZ Erwan061991e2008-07-25 19:44:56 -0700241 {"BCM0102", 0}, /* Broadcom */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700242 {"NSC1200", 0}, /* National */
Marcin Obarafb0e7e12008-07-10 17:30:42 -0700243 {"ICO0102", 0}, /* Intel */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700244 /* Add new here */
245 {"", 0}, /* User Specified */
246 {"", 0} /* Terminator */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700247};
Matt Domsch31bde712009-11-03 12:05:50 +1100248MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700249
Bill Pemberton39af33f2012-11-19 13:26:26 -0500250static void tpm_tis_pnp_remove(struct pnp_dev *dev)
Rajiv Andrade253115b2008-10-11 09:04:39 +1100251{
252 struct tpm_chip *chip = pnp_get_drvdata(dev);
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300253
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800254 tpm_chip_unregister(chip);
255 tpm_tis_remove(chip);
Rajiv Andrade253115b2008-10-11 09:04:39 +1100256}
257
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700258static struct pnp_driver tis_pnp_driver = {
259 .name = "tpm_tis",
260 .id_table = tpm_pnp_tbl,
261 .probe = tpm_tis_pnp_init,
Rajiv Andrade253115b2008-10-11 09:04:39 +1100262 .remove = tpm_tis_pnp_remove,
Shuah Khana2fa3fb2013-09-11 14:23:13 -0700263 .driver = {
264 .pm = &tpm_tis_pm,
265 },
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700266};
267
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700268#define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
269module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
270 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
271MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
Ming Lei7a192ec2009-02-06 23:40:12 +0800272
Jason Gunthorpe00194822016-01-07 17:36:24 -0700273static struct platform_device *force_pdev;
274
275static int tpm_tis_plat_probe(struct platform_device *pdev)
276{
277 struct tpm_info tpm_info = {};
278 struct resource *res;
279
280 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281 if (res == NULL) {
282 dev_err(&pdev->dev, "no memory resource defined\n");
283 return -ENODEV;
284 }
285 tpm_info.res = *res;
286
Jason Gunthorpefc0e1322017-05-04 09:53:24 -0600287 tpm_info.irq = platform_get_irq(pdev, 0);
288 if (tpm_info.irq <= 0) {
Jason Gunthorped27f81f2017-05-04 09:53:23 -0600289 if (pdev != force_pdev)
Jason Gunthorpe00194822016-01-07 17:36:24 -0700290 tpm_info.irq = -1;
291 else
292 /* When forcing auto probe the IRQ */
293 tpm_info.irq = 0;
294 }
295
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600296 return tpm_tis_init(&pdev->dev, &tpm_info);
Jason Gunthorpe00194822016-01-07 17:36:24 -0700297}
298
299static int tpm_tis_plat_remove(struct platform_device *pdev)
300{
301 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
302
303 tpm_chip_unregister(chip);
304 tpm_tis_remove(chip);
305
306 return 0;
307}
308
Jason Gunthorpe420d4392016-11-07 15:44:31 -0700309#ifdef CONFIG_OF
310static const struct of_device_id tis_of_platform_match[] = {
311 {.compatible = "tcg,tpm-tis-mmio"},
312 {},
313};
314MODULE_DEVICE_TABLE(of, tis_of_platform_match);
315#endif
316
Ming Lei7a192ec2009-02-06 23:40:12 +0800317static struct platform_driver tis_drv = {
Jason Gunthorpe00194822016-01-07 17:36:24 -0700318 .probe = tpm_tis_plat_probe,
319 .remove = tpm_tis_plat_remove,
Ming Lei7a192ec2009-02-06 23:40:12 +0800320 .driver = {
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800321 .name = "tpm_tis",
Rafael J. Wysockib633f052012-07-06 19:09:20 +0200322 .pm = &tpm_tis_pm,
Jason Gunthorpe420d4392016-11-07 15:44:31 -0700323 .of_match_table = of_match_ptr(tis_of_platform_match),
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600324 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
Ming Lei7a192ec2009-02-06 23:40:12 +0800325 },
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700326};
327
Jason Gunthorpe00194822016-01-07 17:36:24 -0700328static int tpm_tis_force_device(void)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700329{
Jason Gunthorpe00194822016-01-07 17:36:24 -0700330 struct platform_device *pdev;
331 static const struct resource x86_resources[] = {
332 {
333 .start = 0xFED40000,
334 .end = 0xFED40000 + TIS_MEM_LEN - 1,
335 .flags = IORESOURCE_MEM,
336 },
337 };
338
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300339 if (!force)
340 return 0;
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700341
Jason Gunthorpe00194822016-01-07 17:36:24 -0700342 /* The driver core will match the name tpm_tis of the device to
343 * the tpm_tis platform driver and complete the setup via
344 * tpm_tis_plat_probe
345 */
346 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
347 ARRAY_SIZE(x86_resources));
348 if (IS_ERR(pdev))
349 return PTR_ERR(pdev);
350 force_pdev = pdev;
351
Wei Yongjun4fba3c32013-04-25 15:07:47 +0800352 return 0;
Jason Gunthorpe00194822016-01-07 17:36:24 -0700353}
354
355static int __init init_tis(void)
356{
357 int rc;
358
359 rc = tpm_tis_force_device();
360 if (rc)
361 goto err_force;
362
363 rc = platform_driver_register(&tis_drv);
364 if (rc)
365 goto err_platform;
366
Jason Gunthorpe00194822016-01-07 17:36:24 -0700367
368 if (IS_ENABLED(CONFIG_PNP)) {
369 rc = pnp_register_driver(&tis_pnp_driver);
370 if (rc)
371 goto err_pnp;
372 }
373
374 return 0;
375
376err_pnp:
Wei Yongjun5939eaf2017-02-07 15:51:47 +0000377 platform_driver_unregister(&tis_drv);
Jason Gunthorpe00194822016-01-07 17:36:24 -0700378err_platform:
379 if (force_pdev)
380 platform_device_unregister(force_pdev);
381err_force:
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300382 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700383}
384
385static void __exit cleanup_tis(void)
386{
Jason Gunthorpe00194822016-01-07 17:36:24 -0700387 pnp_unregister_driver(&tis_pnp_driver);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300388 platform_driver_unregister(&tis_drv);
Jason Gunthorpe00194822016-01-07 17:36:24 -0700389
390 if (force_pdev)
391 platform_device_unregister(force_pdev);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700392}
393
394module_init(init_tis);
395module_exit(cleanup_tis);
396MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
397MODULE_DESCRIPTION("TPM Driver");
398MODULE_VERSION("2.0");
399MODULE_LICENSE("GPL");