]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/char/tpm/tpm_tis.c
e12b3ab215cf97133488b69f038f07064b4dbde1
[mirror_ubuntu-zesty-kernel.git] / drivers / char / tpm / tpm_tis.c
1 /*
2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
4 *
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 *
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 */
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>
31 #include "tpm.h"
32
33 enum tis_access {
34 TPM_ACCESS_VALID = 0x80,
35 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
36 TPM_ACCESS_REQUEST_PENDING = 0x04,
37 TPM_ACCESS_REQUEST_USE = 0x02,
38 };
39
40 enum tis_status {
41 TPM_STS_VALID = 0x80,
42 TPM_STS_COMMAND_READY = 0x40,
43 TPM_STS_GO = 0x20,
44 TPM_STS_DATA_AVAIL = 0x10,
45 TPM_STS_DATA_EXPECT = 0x08,
46 };
47
48 enum tis_int_flags {
49 TPM_GLOBAL_INT_ENABLE = 0x80000000,
50 TPM_INTF_BURST_COUNT_STATIC = 0x100,
51 TPM_INTF_CMD_READY_INT = 0x080,
52 TPM_INTF_INT_EDGE_FALLING = 0x040,
53 TPM_INTF_INT_EDGE_RISING = 0x020,
54 TPM_INTF_INT_LEVEL_LOW = 0x010,
55 TPM_INTF_INT_LEVEL_HIGH = 0x008,
56 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
57 TPM_INTF_STS_VALID_INT = 0x002,
58 TPM_INTF_DATA_AVAIL_INT = 0x001,
59 };
60
61 enum tis_defaults {
62 TIS_MEM_BASE = 0xFED40000,
63 TIS_MEM_LEN = 0x5000,
64 TIS_SHORT_TIMEOUT = 750, /* ms */
65 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
66 };
67
68
69 /* Some timeout values are needed before it is known whether the chip is
70 * TPM 1.0 or TPM 2.0.
71 */
72 #define TIS_TIMEOUT_A_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
73 #define TIS_TIMEOUT_B_MAX max(TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
74 #define TIS_TIMEOUT_C_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
75 #define TIS_TIMEOUT_D_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
76
77 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
78 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
79 #define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
80 #define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
81 #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
82 #define TPM_STS(l) (0x0018 | ((l) << 12))
83 #define TPM_STS3(l) (0x001b | ((l) << 12))
84 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
85
86 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
87 #define TPM_RID(l) (0x0F04 | ((l) << 12))
88
89 struct priv_data {
90 bool irq_tested;
91 };
92
93 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
94 static int is_itpm(struct pnp_dev *dev)
95 {
96 struct acpi_device *acpi = pnp_acpi_device(dev);
97 struct acpi_hardware_id *id;
98
99 if (!acpi)
100 return 0;
101
102 list_for_each_entry(id, &acpi->pnp.ids, list) {
103 if (!strcmp("INTC0102", id->id))
104 return 1;
105 }
106
107 return 0;
108 }
109 #else
110 static inline int is_itpm(struct pnp_dev *dev)
111 {
112 return 0;
113 }
114 #endif
115
116 /* Before we attempt to access the TPM we must see that the valid bit is set.
117 * The specification says that this bit is 0 at reset and remains 0 until the
118 * 'TPM has gone through its self test and initialization and has established
119 * correct values in the other bits.' */
120 static int wait_startup(struct tpm_chip *chip, int l)
121 {
122 unsigned long stop = jiffies + chip->vendor.timeout_a;
123 do {
124 if (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
125 TPM_ACCESS_VALID)
126 return 0;
127 msleep(TPM_TIMEOUT);
128 } while (time_before(jiffies, stop));
129 return -1;
130 }
131
132 static int check_locality(struct tpm_chip *chip, int l)
133 {
134 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
135 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
136 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
137 return chip->vendor.locality = l;
138
139 return -1;
140 }
141
142 static void release_locality(struct tpm_chip *chip, int l, int force)
143 {
144 if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
145 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
146 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
147 iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
148 chip->vendor.iobase + TPM_ACCESS(l));
149 }
150
151 static int request_locality(struct tpm_chip *chip, int l)
152 {
153 unsigned long stop, timeout;
154 long rc;
155
156 if (check_locality(chip, l) >= 0)
157 return l;
158
159 iowrite8(TPM_ACCESS_REQUEST_USE,
160 chip->vendor.iobase + TPM_ACCESS(l));
161
162 stop = jiffies + chip->vendor.timeout_a;
163
164 if (chip->vendor.irq) {
165 again:
166 timeout = stop - jiffies;
167 if ((long)timeout <= 0)
168 return -1;
169 rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
170 (check_locality
171 (chip, l) >= 0),
172 timeout);
173 if (rc > 0)
174 return l;
175 if (rc == -ERESTARTSYS && freezing(current)) {
176 clear_thread_flag(TIF_SIGPENDING);
177 goto again;
178 }
179 } else {
180 /* wait for burstcount */
181 do {
182 if (check_locality(chip, l) >= 0)
183 return l;
184 msleep(TPM_TIMEOUT);
185 }
186 while (time_before(jiffies, stop));
187 }
188 return -1;
189 }
190
191 static u8 tpm_tis_status(struct tpm_chip *chip)
192 {
193 return ioread8(chip->vendor.iobase +
194 TPM_STS(chip->vendor.locality));
195 }
196
197 static void tpm_tis_ready(struct tpm_chip *chip)
198 {
199 /* this causes the current command to be aborted */
200 iowrite8(TPM_STS_COMMAND_READY,
201 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
202 }
203
204 static int get_burstcount(struct tpm_chip *chip)
205 {
206 unsigned long stop;
207 int burstcnt;
208
209 /* wait for burstcount */
210 /* which timeout value, spec has 2 answers (c & d) */
211 stop = jiffies + chip->vendor.timeout_d;
212 do {
213 burstcnt = ioread8(chip->vendor.iobase +
214 TPM_STS(chip->vendor.locality) + 1);
215 burstcnt += ioread8(chip->vendor.iobase +
216 TPM_STS(chip->vendor.locality) +
217 2) << 8;
218 if (burstcnt)
219 return burstcnt;
220 msleep(TPM_TIMEOUT);
221 } while (time_before(jiffies, stop));
222 return -EBUSY;
223 }
224
225 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
226 {
227 int size = 0, burstcnt;
228 while (size < count &&
229 wait_for_tpm_stat(chip,
230 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
231 chip->vendor.timeout_c,
232 &chip->vendor.read_queue, true)
233 == 0) {
234 burstcnt = get_burstcount(chip);
235 for (; burstcnt > 0 && size < count; burstcnt--)
236 buf[size++] = ioread8(chip->vendor.iobase +
237 TPM_DATA_FIFO(chip->vendor.
238 locality));
239 }
240 return size;
241 }
242
243 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
244 {
245 int size = 0;
246 int expected, status;
247
248 if (count < TPM_HEADER_SIZE) {
249 size = -EIO;
250 goto out;
251 }
252
253 /* read first 10 bytes, including tag, paramsize, and result */
254 if ((size =
255 recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
256 dev_err(chip->pdev, "Unable to read header\n");
257 goto out;
258 }
259
260 expected = be32_to_cpu(*(__be32 *) (buf + 2));
261 if (expected > count) {
262 size = -EIO;
263 goto out;
264 }
265
266 if ((size +=
267 recv_data(chip, &buf[TPM_HEADER_SIZE],
268 expected - TPM_HEADER_SIZE)) < expected) {
269 dev_err(chip->pdev, "Unable to read remainder of result\n");
270 size = -ETIME;
271 goto out;
272 }
273
274 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
275 &chip->vendor.int_queue, false);
276 status = tpm_tis_status(chip);
277 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
278 dev_err(chip->pdev, "Error left over data\n");
279 size = -EIO;
280 goto out;
281 }
282
283 out:
284 tpm_tis_ready(chip);
285 release_locality(chip, chip->vendor.locality, 0);
286 return size;
287 }
288
289 static bool itpm;
290 module_param(itpm, bool, 0444);
291 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
292
293 /*
294 * If interrupts are used (signaled by an irq set in the vendor structure)
295 * tpm.c can skip polling for the data to be available as the interrupt is
296 * waited for here
297 */
298 static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
299 {
300 int rc, status, burstcnt;
301 size_t count = 0;
302
303 if (request_locality(chip, 0) < 0)
304 return -EBUSY;
305
306 status = tpm_tis_status(chip);
307 if ((status & TPM_STS_COMMAND_READY) == 0) {
308 tpm_tis_ready(chip);
309 if (wait_for_tpm_stat
310 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
311 &chip->vendor.int_queue, false) < 0) {
312 rc = -ETIME;
313 goto out_err;
314 }
315 }
316
317 while (count < len - 1) {
318 burstcnt = get_burstcount(chip);
319 for (; burstcnt > 0 && count < len - 1; burstcnt--) {
320 iowrite8(buf[count], chip->vendor.iobase +
321 TPM_DATA_FIFO(chip->vendor.locality));
322 count++;
323 }
324
325 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
326 &chip->vendor.int_queue, false);
327 status = tpm_tis_status(chip);
328 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
329 rc = -EIO;
330 goto out_err;
331 }
332 }
333
334 /* write last byte */
335 iowrite8(buf[count],
336 chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
337 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
338 &chip->vendor.int_queue, false);
339 status = tpm_tis_status(chip);
340 if ((status & TPM_STS_DATA_EXPECT) != 0) {
341 rc = -EIO;
342 goto out_err;
343 }
344
345 return 0;
346
347 out_err:
348 tpm_tis_ready(chip);
349 release_locality(chip, chip->vendor.locality, 0);
350 return rc;
351 }
352
353 static void disable_interrupts(struct tpm_chip *chip)
354 {
355 u32 intmask;
356
357 intmask =
358 ioread32(chip->vendor.iobase +
359 TPM_INT_ENABLE(chip->vendor.locality));
360 intmask &= ~TPM_GLOBAL_INT_ENABLE;
361 iowrite32(intmask,
362 chip->vendor.iobase +
363 TPM_INT_ENABLE(chip->vendor.locality));
364 free_irq(chip->vendor.irq, chip);
365 chip->vendor.irq = 0;
366 }
367
368 /*
369 * If interrupts are used (signaled by an irq set in the vendor structure)
370 * tpm.c can skip polling for the data to be available as the interrupt is
371 * waited for here
372 */
373 static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
374 {
375 int rc;
376 u32 ordinal;
377 unsigned long dur;
378
379 rc = tpm_tis_send_data(chip, buf, len);
380 if (rc < 0)
381 return rc;
382
383 /* go and do it */
384 iowrite8(TPM_STS_GO,
385 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
386
387 if (chip->vendor.irq) {
388 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
389
390 if (chip->flags & TPM_CHIP_FLAG_TPM2)
391 dur = tpm2_calc_ordinal_duration(chip, ordinal);
392 else
393 dur = tpm_calc_ordinal_duration(chip, ordinal);
394
395 if (wait_for_tpm_stat
396 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
397 &chip->vendor.read_queue, false) < 0) {
398 rc = -ETIME;
399 goto out_err;
400 }
401 }
402 return len;
403 out_err:
404 tpm_tis_ready(chip);
405 release_locality(chip, chip->vendor.locality, 0);
406 return rc;
407 }
408
409 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
410 {
411 int rc, irq;
412 struct priv_data *priv = chip->vendor.priv;
413
414 if (!chip->vendor.irq || priv->irq_tested)
415 return tpm_tis_send_main(chip, buf, len);
416
417 /* Verify receipt of the expected IRQ */
418 irq = chip->vendor.irq;
419 chip->vendor.irq = 0;
420 rc = tpm_tis_send_main(chip, buf, len);
421 chip->vendor.irq = irq;
422 if (!priv->irq_tested)
423 msleep(1);
424 if (!priv->irq_tested) {
425 disable_interrupts(chip);
426 dev_err(chip->pdev,
427 FW_BUG "TPM interrupt not working, polling instead\n");
428 }
429 priv->irq_tested = true;
430 return rc;
431 }
432
433 struct tis_vendor_timeout_override {
434 u32 did_vid;
435 unsigned long timeout_us[4];
436 };
437
438 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
439 /* Atmel 3204 */
440 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
441 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
442 };
443
444 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
445 unsigned long *timeout_cap)
446 {
447 int i;
448 u32 did_vid;
449
450 did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
451
452 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
453 if (vendor_timeout_overrides[i].did_vid != did_vid)
454 continue;
455 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
456 sizeof(vendor_timeout_overrides[i].timeout_us));
457 return true;
458 }
459
460 return false;
461 }
462
463 /*
464 * Early probing for iTPM with STS_DATA_EXPECT flaw.
465 * Try sending command without itpm flag set and if that
466 * fails, repeat with itpm flag set.
467 */
468 static int probe_itpm(struct tpm_chip *chip)
469 {
470 int rc = 0;
471 u8 cmd_getticks[] = {
472 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
473 0x00, 0x00, 0x00, 0xf1
474 };
475 size_t len = sizeof(cmd_getticks);
476 bool rem_itpm = itpm;
477 u16 vendor = ioread16(chip->vendor.iobase + TPM_DID_VID(0));
478
479 /* probe only iTPMS */
480 if (vendor != TPM_VID_INTEL)
481 return 0;
482
483 itpm = false;
484
485 rc = tpm_tis_send_data(chip, cmd_getticks, len);
486 if (rc == 0)
487 goto out;
488
489 tpm_tis_ready(chip);
490 release_locality(chip, chip->vendor.locality, 0);
491
492 itpm = true;
493
494 rc = tpm_tis_send_data(chip, cmd_getticks, len);
495 if (rc == 0) {
496 dev_info(chip->pdev, "Detected an iTPM.\n");
497 rc = 1;
498 } else
499 rc = -EFAULT;
500
501 out:
502 itpm = rem_itpm;
503 tpm_tis_ready(chip);
504 release_locality(chip, chip->vendor.locality, 0);
505
506 return rc;
507 }
508
509 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
510 {
511 switch (chip->vendor.manufacturer_id) {
512 case TPM_VID_WINBOND:
513 return ((status == TPM_STS_VALID) ||
514 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
515 case TPM_VID_STM:
516 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
517 default:
518 return (status == TPM_STS_COMMAND_READY);
519 }
520 }
521
522 static const struct tpm_class_ops tpm_tis = {
523 .status = tpm_tis_status,
524 .recv = tpm_tis_recv,
525 .send = tpm_tis_send,
526 .cancel = tpm_tis_ready,
527 .update_timeouts = tpm_tis_update_timeouts,
528 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
529 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
530 .req_canceled = tpm_tis_req_canceled,
531 };
532
533 static irqreturn_t tis_int_probe(int irq, void *dev_id)
534 {
535 struct tpm_chip *chip = dev_id;
536 u32 interrupt;
537
538 interrupt = ioread32(chip->vendor.iobase +
539 TPM_INT_STATUS(chip->vendor.locality));
540
541 if (interrupt == 0)
542 return IRQ_NONE;
543
544 chip->vendor.probed_irq = irq;
545
546 /* Clear interrupts handled with TPM_EOI */
547 iowrite32(interrupt,
548 chip->vendor.iobase +
549 TPM_INT_STATUS(chip->vendor.locality));
550 return IRQ_HANDLED;
551 }
552
553 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
554 {
555 struct tpm_chip *chip = dev_id;
556 u32 interrupt;
557 int i;
558
559 interrupt = ioread32(chip->vendor.iobase +
560 TPM_INT_STATUS(chip->vendor.locality));
561
562 if (interrupt == 0)
563 return IRQ_NONE;
564
565 ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
566 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
567 wake_up_interruptible(&chip->vendor.read_queue);
568 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
569 for (i = 0; i < 5; i++)
570 if (check_locality(chip, i) >= 0)
571 break;
572 if (interrupt &
573 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
574 TPM_INTF_CMD_READY_INT))
575 wake_up_interruptible(&chip->vendor.int_queue);
576
577 /* Clear interrupts handled with TPM_EOI */
578 iowrite32(interrupt,
579 chip->vendor.iobase +
580 TPM_INT_STATUS(chip->vendor.locality));
581 ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
582 return IRQ_HANDLED;
583 }
584
585 static bool interrupts = true;
586 module_param(interrupts, bool, 0444);
587 MODULE_PARM_DESC(interrupts, "Enable interrupts");
588
589 static void tpm_tis_remove(struct tpm_chip *chip)
590 {
591 if (chip->flags & TPM_CHIP_FLAG_TPM2)
592 tpm2_shutdown(chip, TPM2_SU_CLEAR);
593
594 iowrite32(~TPM_GLOBAL_INT_ENABLE &
595 ioread32(chip->vendor.iobase +
596 TPM_INT_ENABLE(chip->vendor.
597 locality)),
598 chip->vendor.iobase +
599 TPM_INT_ENABLE(chip->vendor.locality));
600 release_locality(chip, chip->vendor.locality, 1);
601 }
602
603 static int tpm_tis_init(struct device *dev, acpi_handle acpi_dev_handle,
604 resource_size_t start, resource_size_t len,
605 unsigned int irq)
606 {
607 u32 vendor, intfcaps, intmask;
608 int rc, i, irq_s, irq_e, probe;
609 struct tpm_chip *chip;
610 struct priv_data *priv;
611
612 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
613 if (priv == NULL)
614 return -ENOMEM;
615
616 chip = tpmm_chip_alloc(dev, &tpm_tis);
617 if (IS_ERR(chip))
618 return PTR_ERR(chip);
619
620 chip->vendor.priv = priv;
621 #ifdef CONFIG_ACPI
622 chip->acpi_dev_handle = acpi_dev_handle;
623 #endif
624
625 chip->vendor.iobase = devm_ioremap(dev, start, len);
626 if (!chip->vendor.iobase)
627 return -EIO;
628
629 /* Maximum timeouts */
630 chip->vendor.timeout_a = TIS_TIMEOUT_A_MAX;
631 chip->vendor.timeout_b = TIS_TIMEOUT_B_MAX;
632 chip->vendor.timeout_c = TIS_TIMEOUT_C_MAX;
633 chip->vendor.timeout_d = TIS_TIMEOUT_D_MAX;
634
635 if (wait_startup(chip, 0) != 0) {
636 rc = -ENODEV;
637 goto out_err;
638 }
639
640 if (request_locality(chip, 0) != 0) {
641 rc = -ENODEV;
642 goto out_err;
643 }
644
645 /* Every TPM 2.x command has a higher ordinal than TPM 1.x commands.
646 * Therefore, we can use an idempotent TPM 2.x command to probe TPM 2.x.
647 */
648 rc = tpm2_gen_interrupt(chip, true);
649 if (rc == 0 || rc == TPM2_RC_INITIALIZE)
650 chip->flags |= TPM_CHIP_FLAG_TPM2;
651
652 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
653 chip->vendor.manufacturer_id = vendor;
654
655 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
656 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
657 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
658
659 if (!itpm) {
660 probe = probe_itpm(chip);
661 if (probe < 0) {
662 rc = -ENODEV;
663 goto out_err;
664 }
665 itpm = !!probe;
666 }
667
668 if (itpm)
669 dev_info(dev, "Intel iTPM workaround enabled\n");
670
671
672 /* Figure out the capabilities */
673 intfcaps =
674 ioread32(chip->vendor.iobase +
675 TPM_INTF_CAPS(chip->vendor.locality));
676 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
677 intfcaps);
678 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
679 dev_dbg(dev, "\tBurst Count Static\n");
680 if (intfcaps & TPM_INTF_CMD_READY_INT)
681 dev_dbg(dev, "\tCommand Ready Int Support\n");
682 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
683 dev_dbg(dev, "\tInterrupt Edge Falling\n");
684 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
685 dev_dbg(dev, "\tInterrupt Edge Rising\n");
686 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
687 dev_dbg(dev, "\tInterrupt Level Low\n");
688 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
689 dev_dbg(dev, "\tInterrupt Level High\n");
690 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
691 dev_dbg(dev, "\tLocality Change Int Support\n");
692 if (intfcaps & TPM_INTF_STS_VALID_INT)
693 dev_dbg(dev, "\tSts Valid Int Support\n");
694 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
695 dev_dbg(dev, "\tData Avail Int Support\n");
696
697 /* INTERRUPT Setup */
698 init_waitqueue_head(&chip->vendor.read_queue);
699 init_waitqueue_head(&chip->vendor.int_queue);
700
701 intmask =
702 ioread32(chip->vendor.iobase +
703 TPM_INT_ENABLE(chip->vendor.locality));
704
705 intmask |= TPM_INTF_CMD_READY_INT
706 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
707 | TPM_INTF_STS_VALID_INT;
708
709 iowrite32(intmask,
710 chip->vendor.iobase +
711 TPM_INT_ENABLE(chip->vendor.locality));
712 if (interrupts)
713 chip->vendor.irq = irq;
714 if (interrupts && !chip->vendor.irq) {
715 irq_s =
716 ioread8(chip->vendor.iobase +
717 TPM_INT_VECTOR(chip->vendor.locality));
718 if (irq_s) {
719 irq_e = irq_s;
720 } else {
721 irq_s = 3;
722 irq_e = 15;
723 }
724
725 for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
726 iowrite8(i, chip->vendor.iobase +
727 TPM_INT_VECTOR(chip->vendor.locality));
728 if (devm_request_irq
729 (dev, i, tis_int_probe, IRQF_SHARED,
730 chip->devname, chip) != 0) {
731 dev_info(chip->pdev,
732 "Unable to request irq: %d for probe\n",
733 i);
734 continue;
735 }
736
737 /* Clear all existing */
738 iowrite32(ioread32
739 (chip->vendor.iobase +
740 TPM_INT_STATUS(chip->vendor.locality)),
741 chip->vendor.iobase +
742 TPM_INT_STATUS(chip->vendor.locality));
743
744 /* Turn on */
745 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
746 chip->vendor.iobase +
747 TPM_INT_ENABLE(chip->vendor.locality));
748
749 chip->vendor.probed_irq = 0;
750
751 /* Generate Interrupts */
752 if (chip->flags & TPM_CHIP_FLAG_TPM2)
753 tpm2_gen_interrupt(chip, false);
754 else
755 tpm_gen_interrupt(chip);
756
757 chip->vendor.irq = chip->vendor.probed_irq;
758
759 /* free_irq will call into tis_int_probe;
760 clear all irqs we haven't seen while doing
761 tpm_gen_interrupt */
762 iowrite32(ioread32
763 (chip->vendor.iobase +
764 TPM_INT_STATUS(chip->vendor.locality)),
765 chip->vendor.iobase +
766 TPM_INT_STATUS(chip->vendor.locality));
767
768 /* Turn off */
769 iowrite32(intmask,
770 chip->vendor.iobase +
771 TPM_INT_ENABLE(chip->vendor.locality));
772 }
773 }
774 if (chip->vendor.irq) {
775 iowrite8(chip->vendor.irq,
776 chip->vendor.iobase +
777 TPM_INT_VECTOR(chip->vendor.locality));
778 if (devm_request_irq
779 (dev, chip->vendor.irq, tis_int_handler, IRQF_SHARED,
780 chip->devname, chip) != 0) {
781 dev_info(chip->pdev,
782 "Unable to request irq: %d for use\n",
783 chip->vendor.irq);
784 chip->vendor.irq = 0;
785 } else {
786 /* Clear all existing */
787 iowrite32(ioread32
788 (chip->vendor.iobase +
789 TPM_INT_STATUS(chip->vendor.locality)),
790 chip->vendor.iobase +
791 TPM_INT_STATUS(chip->vendor.locality));
792
793 /* Turn on */
794 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
795 chip->vendor.iobase +
796 TPM_INT_ENABLE(chip->vendor.locality));
797 }
798 }
799
800 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
801 chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
802 chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
803 chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
804 chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
805 chip->vendor.duration[TPM_SHORT] =
806 msecs_to_jiffies(TPM2_DURATION_SHORT);
807 chip->vendor.duration[TPM_MEDIUM] =
808 msecs_to_jiffies(TPM2_DURATION_MEDIUM);
809 chip->vendor.duration[TPM_LONG] =
810 msecs_to_jiffies(TPM2_DURATION_LONG);
811
812 rc = tpm2_do_selftest(chip);
813 if (rc == TPM2_RC_INITIALIZE) {
814 dev_warn(dev, "Firmware has not started TPM\n");
815 rc = tpm2_startup(chip, TPM2_SU_CLEAR);
816 if (!rc)
817 rc = tpm2_do_selftest(chip);
818 }
819
820 if (rc) {
821 dev_err(dev, "TPM self test failed\n");
822 if (rc > 0)
823 rc = -ENODEV;
824 goto out_err;
825 }
826 } else {
827 if (tpm_get_timeouts(chip)) {
828 dev_err(dev, "Could not get TPM timeouts and durations\n");
829 rc = -ENODEV;
830 goto out_err;
831 }
832
833 if (tpm_do_selftest(chip)) {
834 dev_err(dev, "TPM self test failed\n");
835 rc = -ENODEV;
836 goto out_err;
837 }
838 }
839
840 return tpm_chip_register(chip);
841 out_err:
842 tpm_tis_remove(chip);
843 return rc;
844 }
845
846 #ifdef CONFIG_PM_SLEEP
847 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
848 {
849 u32 intmask;
850
851 /* reenable interrupts that device may have lost or
852 BIOS/firmware may have disabled */
853 iowrite8(chip->vendor.irq, chip->vendor.iobase +
854 TPM_INT_VECTOR(chip->vendor.locality));
855
856 intmask =
857 ioread32(chip->vendor.iobase +
858 TPM_INT_ENABLE(chip->vendor.locality));
859
860 intmask |= TPM_INTF_CMD_READY_INT
861 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
862 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
863
864 iowrite32(intmask,
865 chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
866 }
867
868 static int tpm_tis_resume(struct device *dev)
869 {
870 struct tpm_chip *chip = dev_get_drvdata(dev);
871 int ret;
872
873 if (chip->vendor.irq)
874 tpm_tis_reenable_interrupts(chip);
875
876 ret = tpm_pm_resume(dev);
877 if (ret)
878 return ret;
879
880 /* TPM 1.2 requires self-test on resume. This function actually returns
881 * an error code but for unknown reason it isn't handled.
882 */
883 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
884 tpm_do_selftest(chip);
885
886 return 0;
887 }
888 #endif
889
890 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
891
892 #ifdef CONFIG_PNP
893 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
894 const struct pnp_device_id *pnp_id)
895 {
896 resource_size_t start, len;
897 unsigned int irq = 0;
898 acpi_handle acpi_dev_handle = NULL;
899
900 start = pnp_mem_start(pnp_dev, 0);
901 len = pnp_mem_len(pnp_dev, 0);
902
903 if (pnp_irq_valid(pnp_dev, 0))
904 irq = pnp_irq(pnp_dev, 0);
905 else
906 interrupts = false;
907
908 if (is_itpm(pnp_dev))
909 itpm = true;
910
911 #ifdef CONFIG_ACPI
912 if (pnp_acpi_device(pnp_dev))
913 acpi_dev_handle = pnp_acpi_device(pnp_dev)->handle;
914 #endif
915
916 return tpm_tis_init(&pnp_dev->dev, acpi_dev_handle, start, len, irq);
917 }
918
919 static struct pnp_device_id tpm_pnp_tbl[] = {
920 {"PNP0C31", 0}, /* TPM */
921 {"ATM1200", 0}, /* Atmel */
922 {"IFX0102", 0}, /* Infineon */
923 {"BCM0101", 0}, /* Broadcom */
924 {"BCM0102", 0}, /* Broadcom */
925 {"NSC1200", 0}, /* National */
926 {"ICO0102", 0}, /* Intel */
927 /* Add new here */
928 {"", 0}, /* User Specified */
929 {"", 0} /* Terminator */
930 };
931 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
932
933 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
934 {
935 struct tpm_chip *chip = pnp_get_drvdata(dev);
936 tpm_chip_unregister(chip);
937 tpm_tis_remove(chip);
938 }
939
940 static struct pnp_driver tis_pnp_driver = {
941 .name = "tpm_tis",
942 .id_table = tpm_pnp_tbl,
943 .probe = tpm_tis_pnp_init,
944 .remove = tpm_tis_pnp_remove,
945 .driver = {
946 .pm = &tpm_tis_pm,
947 },
948 };
949
950 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
951 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
952 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
953 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
954 #endif
955
956 static struct platform_driver tis_drv = {
957 .driver = {
958 .name = "tpm_tis",
959 .pm = &tpm_tis_pm,
960 },
961 };
962
963 static struct platform_device *pdev;
964
965 static bool force;
966 module_param(force, bool, 0444);
967 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
968 static int __init init_tis(void)
969 {
970 int rc;
971 #ifdef CONFIG_PNP
972 if (!force)
973 return pnp_register_driver(&tis_pnp_driver);
974 #endif
975
976 rc = platform_driver_register(&tis_drv);
977 if (rc < 0)
978 return rc;
979 pdev = platform_device_register_simple("tpm_tis", -1, NULL, 0);
980 if (IS_ERR(pdev)) {
981 rc = PTR_ERR(pdev);
982 goto err_dev;
983 }
984 rc = tpm_tis_init(&pdev->dev, NULL, TIS_MEM_BASE, TIS_MEM_LEN, 0);
985 if (rc)
986 goto err_init;
987 return 0;
988 err_init:
989 platform_device_unregister(pdev);
990 err_dev:
991 platform_driver_unregister(&tis_drv);
992 return rc;
993 }
994
995 static void __exit cleanup_tis(void)
996 {
997 struct tpm_chip *chip;
998 #ifdef CONFIG_PNP
999 if (!force) {
1000 pnp_unregister_driver(&tis_pnp_driver);
1001 return;
1002 }
1003 #endif
1004 chip = dev_get_drvdata(&pdev->dev);
1005 tpm_chip_unregister(chip);
1006 tpm_tis_remove(chip);
1007 platform_device_unregister(pdev);
1008 platform_driver_unregister(&tis_drv);
1009 }
1010
1011 module_init(init_tis);
1012 module_exit(cleanup_tis);
1013 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1014 MODULE_DESCRIPTION("TPM Driver");
1015 MODULE_VERSION("2.0");
1016 MODULE_LICENSE("GPL");