blob: f08949a5f678508ce864a00df46fe172cb112bba [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>
Jérémy Lefaure33957a12017-10-01 15:30:51 -040033#include <linux/kernel.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070034#include "tpm.h"
Christophe Ricard57dacc22016-05-19 00:35:48 +020035#include "tpm_tis_core.h"
Leendert van Doorn27084ef2006-04-22 02:38:03 -070036
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030037struct tpm_info {
Jason Gunthorpe51dd43d2016-01-07 17:36:23 -070038 struct resource res;
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -070039 /* irq > 0 means: use irq $irq;
40 * irq = 0 means: autoprobe for an irq;
41 * irq = -1 means: no irq support
42 */
43 int irq;
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030044};
45
Christophe Ricard57dacc22016-05-19 00:35:48 +020046struct tpm_tis_tcg_phy {
47 struct tpm_tis_data priv;
Christophe Ricard4eea7032016-03-31 22:56:55 +020048 void __iomem *iobase;
Scot Doyle448e9c52014-09-24 22:41:10 +000049};
50
Christophe Ricard57dacc22016-05-19 00:35:48 +020051static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
52{
53 return container_of(data, struct tpm_tis_tcg_phy, priv);
54}
55
Christophe Ricard41a5e1c2016-05-19 00:35:52 +020056static bool interrupts = true;
57module_param(interrupts, bool, 0444);
58MODULE_PARM_DESC(interrupts, "Enable interrupts");
59
60static bool itpm;
61module_param(itpm, bool, 0444);
62MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
63
64static bool force;
65#ifdef CONFIG_X86
66module_param(force, bool, 0444);
67MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
68#endif
69
Randy Dunlap1560ffe62011-08-03 16:21:11 -070070#if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030071static int has_hid(struct acpi_device *dev, const char *hid)
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040072{
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040073 struct acpi_hardware_id *id;
74
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030075 list_for_each_entry(id, &dev->pnp.ids, list)
76 if (!strcmp(hid, id->id))
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040077 return 1;
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040078
79 return 0;
80}
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030081
82static inline int is_itpm(struct acpi_device *dev)
83{
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -060084 if (!dev)
85 return 0;
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030086 return has_hid(dev, "INTC0102");
87}
Randy Dunlap1560ffe62011-08-03 16:21:11 -070088#else
Jarkko Sakkinen399235d2015-09-29 00:32:19 +030089static inline int is_itpm(struct acpi_device *dev)
Randy Dunlap1560ffe62011-08-03 16:21:11 -070090{
91 return 0;
92}
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040093#endif
94
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -060095#if defined(CONFIG_ACPI)
96#define DEVICE_IS_TPM2 1
97
98static const struct acpi_device_id tpm_acpi_tbl[] = {
99 {"MSFT0101", DEVICE_IS_TPM2},
100 {},
101};
102MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
103
104static int check_acpi_tpm2(struct device *dev)
105{
106 const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
107 struct acpi_table_tpm2 *tbl;
108 acpi_status st;
109
110 if (!aid || aid->driver_data != DEVICE_IS_TPM2)
111 return 0;
112
113 /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
114 * table is mandatory
115 */
116 st =
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");
120 return -EINVAL;
121 }
122
123 /* The tpm2_crb driver handles this device */
124 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
125 return -ENODEV;
126
127 return 0;
128}
129#else
130static int check_acpi_tpm2(struct device *dev)
131{
132 return 0;
133}
134#endif
135
Christophe Ricard1107d062016-05-19 00:35:49 +0200136static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
137 u8 *result)
138{
139 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
140
141 while (len--)
142 *result++ = ioread8(phy->iobase + addr);
Azhar Shaikh5e572ca2017-06-18 19:17:59 -0700143
Christophe Ricard1107d062016-05-19 00:35:49 +0200144 return 0;
145}
146
147static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
Arnd Bergmannc37fbc02017-09-07 15:30:45 +0200148 const u8 *value)
Christophe Ricard1107d062016-05-19 00:35:49 +0200149{
150 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
151
152 while (len--)
153 iowrite8(*value++, phy->iobase + addr);
Azhar Shaikh5e572ca2017-06-18 19:17:59 -0700154
Christophe Ricard1107d062016-05-19 00:35:49 +0200155 return 0;
156}
157
158static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
159{
160 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
161
162 *result = ioread16(phy->iobase + addr);
Azhar Shaikh5e572ca2017-06-18 19:17:59 -0700163
Christophe Ricard1107d062016-05-19 00:35:49 +0200164 return 0;
165}
166
167static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
168{
169 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
170
171 *result = ioread32(phy->iobase + addr);
Azhar Shaikh5e572ca2017-06-18 19:17:59 -0700172
Christophe Ricard1107d062016-05-19 00:35:49 +0200173 return 0;
174}
175
176static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
177{
178 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
179
180 iowrite32(value, phy->iobase + addr);
Azhar Shaikh5e572ca2017-06-18 19:17:59 -0700181
Christophe Ricard1107d062016-05-19 00:35:49 +0200182 return 0;
183}
184
185static 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,
191};
192
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600193static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700194{
Christophe Ricard57dacc22016-05-19 00:35:48 +0200195 struct tpm_tis_tcg_phy *phy;
Christophe Ricard41a5e1c2016-05-19 00:35:52 +0200196 int irq = -1;
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600197 int rc;
198
199 rc = check_acpi_tpm2(dev);
200 if (rc)
201 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700202
Christophe Ricard57dacc22016-05-19 00:35:48 +0200203 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
204 if (phy == NULL)
Scot Doyle448e9c52014-09-24 22:41:10 +0000205 return -ENOMEM;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800206
Christophe Ricard57dacc22016-05-19 00:35:48 +0200207 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
208 if (IS_ERR(phy->iobase))
209 return PTR_ERR(phy->iobase);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700210
Christophe Ricard41a5e1c2016-05-19 00:35:52 +0200211 if (interrupts)
212 irq = tpm_info->irq;
Stefan Berger9519de32011-03-30 12:13:33 -0400213
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600214 if (itpm || is_itpm(ACPI_COMPANION(dev)))
Maciej S. Szmigiero1d70fe92017-01-13 22:37:00 +0100215 phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
Rajiv Andrade3507d612009-09-10 17:09:35 -0300216
Christophe Ricard41a5e1c2016-05-19 00:35:52 +0200217 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600218 ACPI_HANDLE(dev));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700219}
Stefan Berger96854312011-07-17 01:04:24 -0400220
Shuah Khana2fa3fb2013-09-11 14:23:13 -0700221static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
222
Bill Pembertonafc6d362012-11-19 13:22:42 -0500223static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -0700224 const struct pnp_device_id *pnp_id)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700225{
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -0700226 struct tpm_info tpm_info = {};
Jason Gunthorpe51dd43d2016-01-07 17:36:23 -0700227 struct resource *res;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700228
Jason Gunthorpe51dd43d2016-01-07 17:36:23 -0700229 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
230 if (!res)
231 return -ENODEV;
232 tpm_info.res = *res;
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700233
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700234 if (pnp_irq_valid(pnp_dev, 0))
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300235 tpm_info.irq = pnp_irq(pnp_dev, 0);
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700236 else
Jason Gunthorpeef7b81d2016-01-07 17:36:21 -0700237 tpm_info.irq = -1;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700238
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600239 return tpm_tis_init(&pnp_dev->dev, &tpm_info);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700240}
241
Bill Pemberton0bbed202012-11-19 13:24:36 -0500242static struct pnp_device_id tpm_pnp_tbl[] = {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700243 {"PNP0C31", 0}, /* TPM */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700244 {"ATM1200", 0}, /* Atmel */
245 {"IFX0102", 0}, /* Infineon */
246 {"BCM0101", 0}, /* Broadcom */
LE DISEZ Erwan061991e2008-07-25 19:44:56 -0700247 {"BCM0102", 0}, /* Broadcom */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700248 {"NSC1200", 0}, /* National */
Marcin Obarafb0e7e12008-07-10 17:30:42 -0700249 {"ICO0102", 0}, /* Intel */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700250 /* Add new here */
251 {"", 0}, /* User Specified */
252 {"", 0} /* Terminator */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700253};
Matt Domsch31bde712009-11-03 12:05:50 +1100254MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700255
Bill Pemberton39af33f2012-11-19 13:26:26 -0500256static void tpm_tis_pnp_remove(struct pnp_dev *dev)
Rajiv Andrade253115b2008-10-11 09:04:39 +1100257{
258 struct tpm_chip *chip = pnp_get_drvdata(dev);
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300259
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800260 tpm_chip_unregister(chip);
261 tpm_tis_remove(chip);
Rajiv Andrade253115b2008-10-11 09:04:39 +1100262}
263
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700264static struct pnp_driver tis_pnp_driver = {
265 .name = "tpm_tis",
266 .id_table = tpm_pnp_tbl,
267 .probe = tpm_tis_pnp_init,
Rajiv Andrade253115b2008-10-11 09:04:39 +1100268 .remove = tpm_tis_pnp_remove,
Shuah Khana2fa3fb2013-09-11 14:23:13 -0700269 .driver = {
270 .pm = &tpm_tis_pm,
271 },
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700272};
273
Jérémy Lefaure33957a12017-10-01 15:30:51 -0400274#define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700275module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
276 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
277MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
Ming Lei7a192ec2009-02-06 23:40:12 +0800278
Jason Gunthorpe00194822016-01-07 17:36:24 -0700279static struct platform_device *force_pdev;
280
281static int tpm_tis_plat_probe(struct platform_device *pdev)
282{
283 struct tpm_info tpm_info = {};
284 struct resource *res;
285
286 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
287 if (res == NULL) {
288 dev_err(&pdev->dev, "no memory resource defined\n");
289 return -ENODEV;
290 }
291 tpm_info.res = *res;
292
Jason Gunthorpefc0e1322017-05-04 09:53:24 -0600293 tpm_info.irq = platform_get_irq(pdev, 0);
294 if (tpm_info.irq <= 0) {
Jason Gunthorped27f81f2017-05-04 09:53:23 -0600295 if (pdev != force_pdev)
Jason Gunthorpe00194822016-01-07 17:36:24 -0700296 tpm_info.irq = -1;
297 else
298 /* When forcing auto probe the IRQ */
299 tpm_info.irq = 0;
300 }
301
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600302 return tpm_tis_init(&pdev->dev, &tpm_info);
Jason Gunthorpe00194822016-01-07 17:36:24 -0700303}
304
305static int tpm_tis_plat_remove(struct platform_device *pdev)
306{
307 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
308
309 tpm_chip_unregister(chip);
310 tpm_tis_remove(chip);
311
312 return 0;
313}
314
Jason Gunthorpe420d4392016-11-07 15:44:31 -0700315#ifdef CONFIG_OF
316static const struct of_device_id tis_of_platform_match[] = {
317 {.compatible = "tcg,tpm-tis-mmio"},
318 {},
319};
320MODULE_DEVICE_TABLE(of, tis_of_platform_match);
321#endif
322
Ming Lei7a192ec2009-02-06 23:40:12 +0800323static struct platform_driver tis_drv = {
Jason Gunthorpe00194822016-01-07 17:36:24 -0700324 .probe = tpm_tis_plat_probe,
325 .remove = tpm_tis_plat_remove,
Ming Lei7a192ec2009-02-06 23:40:12 +0800326 .driver = {
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800327 .name = "tpm_tis",
Rafael J. Wysockib633f052012-07-06 19:09:20 +0200328 .pm = &tpm_tis_pm,
Jason Gunthorpe420d4392016-11-07 15:44:31 -0700329 .of_match_table = of_match_ptr(tis_of_platform_match),
Jason Gunthorpe4cb586a2017-05-04 09:53:25 -0600330 .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
Ming Lei7a192ec2009-02-06 23:40:12 +0800331 },
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700332};
333
Jason Gunthorpe00194822016-01-07 17:36:24 -0700334static int tpm_tis_force_device(void)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700335{
Jason Gunthorpe00194822016-01-07 17:36:24 -0700336 struct platform_device *pdev;
337 static const struct resource x86_resources[] = {
338 {
339 .start = 0xFED40000,
340 .end = 0xFED40000 + TIS_MEM_LEN - 1,
341 .flags = IORESOURCE_MEM,
342 },
343 };
344
Jarkko Sakkinen399235d2015-09-29 00:32:19 +0300345 if (!force)
346 return 0;
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700347
Jason Gunthorpe00194822016-01-07 17:36:24 -0700348 /* The driver core will match the name tpm_tis of the device to
349 * the tpm_tis platform driver and complete the setup via
350 * tpm_tis_plat_probe
351 */
352 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
353 ARRAY_SIZE(x86_resources));
354 if (IS_ERR(pdev))
355 return PTR_ERR(pdev);
356 force_pdev = pdev;
357
Wei Yongjun4fba3c32013-04-25 15:07:47 +0800358 return 0;
Jason Gunthorpe00194822016-01-07 17:36:24 -0700359}
360
361static int __init init_tis(void)
362{
363 int rc;
364
365 rc = tpm_tis_force_device();
366 if (rc)
367 goto err_force;
368
369 rc = platform_driver_register(&tis_drv);
370 if (rc)
371 goto err_platform;
372
Jason Gunthorpe00194822016-01-07 17:36:24 -0700373
374 if (IS_ENABLED(CONFIG_PNP)) {
375 rc = pnp_register_driver(&tis_pnp_driver);
376 if (rc)
377 goto err_pnp;
378 }
379
380 return 0;
381
382err_pnp:
Wei Yongjun5939eaf2017-02-07 15:51:47 +0000383 platform_driver_unregister(&tis_drv);
Jason Gunthorpe00194822016-01-07 17:36:24 -0700384err_platform:
385 if (force_pdev)
386 platform_device_unregister(force_pdev);
387err_force:
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300388 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700389}
390
391static void __exit cleanup_tis(void)
392{
Jason Gunthorpe00194822016-01-07 17:36:24 -0700393 pnp_unregister_driver(&tis_pnp_driver);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300394 platform_driver_unregister(&tis_drv);
Jason Gunthorpe00194822016-01-07 17:36:24 -0700395
396 if (force_pdev)
397 platform_device_unregister(force_pdev);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700398}
399
400module_init(init_tis);
401module_exit(cleanup_tis);
402MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
403MODULE_DESCRIPTION("TPM Driver");
404MODULE_VERSION("2.0");
405MODULE_LICENSE("GPL");