]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/char/tpm/tpm_tis.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / char / tpm / tpm_tis.c
CommitLineData
27084efe
LD
1/*
2 * Copyright (C) 2005, 2006 IBM Corporation
399235dc 3 * Copyright (C) 2014, 2015 Intel Corporation
27084efe
LD
4 *
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
8e81cc13
KY
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 *
27084efe
LD
11 * 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 */
57135568
KJH
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
27084efe 25#include <linux/pnp.h>
5a0e3ad6 26#include <linux/slab.h>
27084efe
LD
27#include <linux/interrupt.h>
28#include <linux/wait.h>
3f0d3d01 29#include <linux/acpi.h>
20b87bbf 30#include <linux/freezer.h>
420d4398
JG
31#include <linux/of.h>
32#include <linux/of_device.h>
27084efe 33#include "tpm.h"
57dacc2b 34#include "tpm_tis_core.h"
27084efe 35
399235dc 36struct tpm_info {
51dd43df 37 struct resource res;
ef7b81dc
JG
38 /* 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;
399235dc
JS
43};
44
57dacc2b
CR
45struct tpm_tis_tcg_phy {
46 struct tpm_tis_data priv;
4eea703c 47 void __iomem *iobase;
448e9c55
SD
48};
49
57dacc2b
CR
50static 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
41a5e1cf
CR
55static 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
1560ffe6 69#if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
399235dc 70static int has_hid(struct acpi_device *dev, const char *hid)
3f0d3d01 71{
3f0d3d01
MG
72 struct acpi_hardware_id *id;
73
399235dc
JS
74 list_for_each_entry(id, &dev->pnp.ids, list)
75 if (!strcmp(hid, id->id))
3f0d3d01 76 return 1;
3f0d3d01
MG
77
78 return 0;
79}
399235dc
JS
80
81static inline int is_itpm(struct acpi_device *dev)
82{
83 return has_hid(dev, "INTC0102");
84}
1560ffe6 85#else
399235dc 86static inline int is_itpm(struct acpi_device *dev)
1560ffe6
RD
87{
88 return 0;
89}
3f0d3d01
MG
90#endif
91
1107d065
CR
92static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
93 u8 *result)
94{
95 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
96
97 while (len--)
98 *result++ = ioread8(phy->iobase + addr);
99 return 0;
100}
101
102static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
103 u8 *value)
104{
105 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
106
107 while (len--)
108 iowrite8(*value++, phy->iobase + addr);
109 return 0;
110}
111
112static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
113{
114 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
115
116 *result = ioread16(phy->iobase + addr);
117 return 0;
118}
119
120static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
121{
122 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
123
124 *result = ioread32(phy->iobase + addr);
125 return 0;
126}
127
128static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
129{
130 struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
131
132 iowrite32(value, phy->iobase + addr);
133 return 0;
134}
135
136static 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,
142};
143
399235dc
JS
144static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info,
145 acpi_handle acpi_dev_handle)
27084efe 146{
57dacc2b 147 struct tpm_tis_tcg_phy *phy;
41a5e1cf 148 int irq = -1;
27084efe 149
57dacc2b
CR
150 phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
151 if (phy == NULL)
448e9c55 152 return -ENOMEM;
afb5abc2 153
57dacc2b
CR
154 phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
155 if (IS_ERR(phy->iobase))
156 return PTR_ERR(phy->iobase);
27084efe 157
41a5e1cf
CR
158 if (interrupts)
159 irq = tpm_info->irq;
9519de3f 160
3507d612 161 if (itpm)
1d70fe9d 162 phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
25112048 163
41a5e1cf
CR
164 return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
165 acpi_dev_handle);
27084efe 166}
96854310 167
a2fa3fb0
SK
168static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
169
afc6d369 170static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
ef7b81dc 171 const struct pnp_device_id *pnp_id)
9e323d3e 172{
ef7b81dc 173 struct tpm_info tpm_info = {};
0dc55365 174 acpi_handle acpi_dev_handle = NULL;
51dd43df 175 struct resource *res;
7917ff9a 176
51dd43df
JG
177 res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
178 if (!res)
179 return -ENODEV;
180 tpm_info.res = *res;
9e323d3e 181
7917ff9a 182 if (pnp_irq_valid(pnp_dev, 0))
399235dc 183 tpm_info.irq = pnp_irq(pnp_dev, 0);
7917ff9a 184 else
ef7b81dc 185 tpm_info.irq = -1;
7917ff9a 186
399235dc
JS
187 if (pnp_acpi_device(pnp_dev)) {
188 if (is_itpm(pnp_acpi_device(pnp_dev)))
189 itpm = true;
190
00194826 191 acpi_dev_handle = ACPI_HANDLE(&pnp_dev->dev);
399235dc 192 }
0dc55365 193
399235dc 194 return tpm_tis_init(&pnp_dev->dev, &tpm_info, acpi_dev_handle);
9e323d3e
KJH
195}
196
0bbed20e 197static struct pnp_device_id tpm_pnp_tbl[] = {
27084efe 198 {"PNP0C31", 0}, /* TPM */
93e1b7d4
KJH
199 {"ATM1200", 0}, /* Atmel */
200 {"IFX0102", 0}, /* Infineon */
201 {"BCM0101", 0}, /* Broadcom */
061991ec 202 {"BCM0102", 0}, /* Broadcom */
93e1b7d4 203 {"NSC1200", 0}, /* National */
fb0e7e11 204 {"ICO0102", 0}, /* Intel */
93e1b7d4
KJH
205 /* Add new here */
206 {"", 0}, /* User Specified */
207 {"", 0} /* Terminator */
27084efe 208};
31bde71c 209MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
27084efe 210
39af33fc 211static void tpm_tis_pnp_remove(struct pnp_dev *dev)
253115b7
RA
212{
213 struct tpm_chip *chip = pnp_get_drvdata(dev);
399235dc 214
afb5abc2
JS
215 tpm_chip_unregister(chip);
216 tpm_tis_remove(chip);
253115b7
RA
217}
218
27084efe
LD
219static struct pnp_driver tis_pnp_driver = {
220 .name = "tpm_tis",
221 .id_table = tpm_pnp_tbl,
222 .probe = tpm_tis_pnp_init,
253115b7 223 .remove = tpm_tis_pnp_remove,
a2fa3fb0
SK
224 .driver = {
225 .pm = &tpm_tis_pm,
226 },
27084efe
LD
227};
228
93e1b7d4
KJH
229#define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
230module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
231 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
232MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
7a192ec3 233
399235dc
JS
234#ifdef CONFIG_ACPI
235static int tpm_check_resource(struct acpi_resource *ares, void *data)
236{
237 struct tpm_info *tpm_info = (struct tpm_info *) data;
238 struct resource res;
239
51dd43df 240 if (acpi_dev_resource_interrupt(ares, 0, &res))
399235dc 241 tpm_info->irq = res.start;
30f9c8c9 242 else if (acpi_dev_resource_memory(ares, &res)) {
51dd43df 243 tpm_info->res = res;
30f9c8c9
JS
244 tpm_info->res.name = NULL;
245 }
399235dc
JS
246
247 return 1;
248}
249
250static int tpm_tis_acpi_init(struct acpi_device *acpi_dev)
251{
4d627e67
JG
252 struct acpi_table_tpm2 *tbl;
253 acpi_status st;
399235dc 254 struct list_head resources;
4d627e67 255 struct tpm_info tpm_info = {};
399235dc
JS
256 int ret;
257
4d627e67
JG
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");
263 return -EINVAL;
264 }
265
266 if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
399235dc
JS
267 return -ENODEV;
268
269 INIT_LIST_HEAD(&resources);
ef7b81dc 270 tpm_info.irq = -1;
399235dc
JS
271 ret = acpi_dev_get_resources(acpi_dev, &resources, tpm_check_resource,
272 &tpm_info);
273 if (ret < 0)
274 return ret;
275
276 acpi_dev_free_resource_list(&resources);
277
51dd43df 278 if (resource_type(&tpm_info.res) != IORESOURCE_MEM) {
4d627e67
JG
279 dev_err(&acpi_dev->dev,
280 FW_BUG "TPM2 ACPI table does not define a memory resource\n");
281 return -EINVAL;
282 }
283
399235dc
JS
284 if (is_itpm(acpi_dev))
285 itpm = true;
286
287 return tpm_tis_init(&acpi_dev->dev, &tpm_info, acpi_dev->handle);
288}
289
290static int tpm_tis_acpi_remove(struct acpi_device *dev)
291{
292 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
293
294 tpm_chip_unregister(chip);
295 tpm_tis_remove(chip);
296
297 return 0;
298}
299
300static struct acpi_device_id tpm_acpi_tbl[] = {
301 {"MSFT0101", 0}, /* TPM 2.0 */
302 /* Add new here */
303 {"", 0}, /* User Specified */
304 {"", 0} /* Terminator */
305};
306MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
307
308static struct acpi_driver tis_acpi_driver = {
309 .name = "tpm_tis",
310 .ids = tpm_acpi_tbl,
311 .ops = {
312 .add = tpm_tis_acpi_init,
313 .remove = tpm_tis_acpi_remove,
314 },
315 .drv = {
316 .pm = &tpm_tis_pm,
317 },
318};
319#endif
320
00194826
JG
321static struct platform_device *force_pdev;
322
323static int tpm_tis_plat_probe(struct platform_device *pdev)
324{
325 struct tpm_info tpm_info = {};
326 struct resource *res;
327
328 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
329 if (res == NULL) {
330 dev_err(&pdev->dev, "no memory resource defined\n");
331 return -ENODEV;
332 }
333 tpm_info.res = *res;
334
335 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
336 if (res) {
337 tpm_info.irq = res->start;
338 } else {
339 if (pdev == force_pdev)
340 tpm_info.irq = -1;
341 else
342 /* When forcing auto probe the IRQ */
343 tpm_info.irq = 0;
344 }
345
346 return tpm_tis_init(&pdev->dev, &tpm_info, NULL);
347}
348
349static int tpm_tis_plat_remove(struct platform_device *pdev)
350{
351 struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
352
353 tpm_chip_unregister(chip);
354 tpm_tis_remove(chip);
355
356 return 0;
357}
358
420d4398
JG
359#ifdef CONFIG_OF
360static const struct of_device_id tis_of_platform_match[] = {
361 {.compatible = "tcg,tpm-tis-mmio"},
362 {},
363};
364MODULE_DEVICE_TABLE(of, tis_of_platform_match);
365#endif
366
7a192ec3 367static struct platform_driver tis_drv = {
00194826
JG
368 .probe = tpm_tis_plat_probe,
369 .remove = tpm_tis_plat_remove,
7a192ec3 370 .driver = {
afb5abc2 371 .name = "tpm_tis",
b633f050 372 .pm = &tpm_tis_pm,
420d4398 373 .of_match_table = of_match_ptr(tis_of_platform_match),
7a192ec3 374 },
9e323d3e
KJH
375};
376
00194826
JG
377static int tpm_tis_force_device(void)
378{
379 struct platform_device *pdev;
380 static const struct resource x86_resources[] = {
381 {
382 .start = 0xFED40000,
383 .end = 0xFED40000 + TIS_MEM_LEN - 1,
384 .flags = IORESOURCE_MEM,
385 },
386 };
387
388 if (!force)
389 return 0;
390
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
393 * tpm_tis_plat_probe
394 */
395 pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
396 ARRAY_SIZE(x86_resources));
397 if (IS_ERR(pdev))
398 return PTR_ERR(pdev);
399 force_pdev = pdev;
400
401 return 0;
402}
403
27084efe
LD
404static int __init init_tis(void)
405{
9e323d3e 406 int rc;
00194826
JG
407
408 rc = tpm_tis_force_device();
409 if (rc)
410 goto err_force;
411
412 rc = platform_driver_register(&tis_drv);
413 if (rc)
414 goto err_platform;
415
399235dc 416#ifdef CONFIG_ACPI
00194826
JG
417 rc = acpi_bus_register_driver(&tis_acpi_driver);
418 if (rc)
419 goto err_acpi;
399235dc 420#endif
9e323d3e 421
00194826
JG
422 if (IS_ENABLED(CONFIG_PNP)) {
423 rc = pnp_register_driver(&tis_pnp_driver);
424 if (rc)
425 goto err_pnp;
9e323d3e 426 }
00194826 427
4fba3c3b 428 return 0;
00194826
JG
429
430err_pnp:
431#ifdef CONFIG_ACPI
432 acpi_bus_unregister_driver(&tis_acpi_driver);
433err_acpi:
434#endif
5939eaf4 435 platform_driver_unregister(&tis_drv);
00194826
JG
436err_platform:
437 if (force_pdev)
438 platform_device_unregister(force_pdev);
439err_force:
7f2ab000 440 return rc;
27084efe
LD
441}
442
443static void __exit cleanup_tis(void)
444{
00194826 445 pnp_unregister_driver(&tis_pnp_driver);
399235dc 446#ifdef CONFIG_ACPI
00194826 447 acpi_bus_unregister_driver(&tis_acpi_driver);
7f2ab000 448#endif
7f2ab000 449 platform_driver_unregister(&tis_drv);
00194826
JG
450
451 if (force_pdev)
452 platform_device_unregister(force_pdev);
27084efe
LD
453}
454
455module_init(init_tis);
456module_exit(cleanup_tis);
457MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
458MODULE_DESCRIPTION("TPM Driver");
459MODULE_VERSION("2.0");
460MODULE_LICENSE("GPL");