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