blob: a1748621111be511a8694a4d91ab74094f45c631 [file] [log] [blame]
Leendert van Doorn27084ef2006-04-22 02:38:03 -07001/*
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 *
Kent Yoder8e81cc12007-08-22 14:01:04 -07008 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
9 *
Leendert van Doorn27084ef2006-04-22 02:38:03 -070010 * 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 */
Kylene Jo Hall57135562006-04-22 02:39:44 -070021#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070024#include <linux/pnp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070026#include <linux/interrupt.h>
27#include <linux/wait.h>
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040028#include <linux/acpi.h>
Stefan Berger20b87bb2011-03-30 12:13:31 -040029#include <linux/freezer.h>
Leendert van Doorn27084ef2006-04-22 02:38:03 -070030#include "tpm.h"
31
Leendert van Doorn27084ef2006-04-22 02:38:03 -070032enum 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
39enum 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
47enum 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
Kylene Jo Hall36b20022006-04-22 02:38:19 -070060enum tis_defaults {
Kylene Jo Hall2a7362f2006-05-15 09:44:25 -070061 TIS_MEM_BASE = 0xFED40000,
Kylene Jo Hallb09d5302006-04-22 02:38:55 -070062 TIS_MEM_LEN = 0x5000,
Kylene Jo Hallcb535422006-04-22 02:39:31 -070063 TIS_SHORT_TIMEOUT = 750, /* ms */
64 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
Kylene Jo Hall36b20022006-04-22 02:38:19 -070065};
66
Leendert van Doorn27084ef2006-04-22 02:38:03 -070067#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
78static LIST_HEAD(tis_chips);
79static DEFINE_SPINLOCK(tis_lock);
80
Randy Dunlap1560ffe62011-08-03 16:21:11 -070081#if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040082static 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}
Randy Dunlap1560ffe62011-08-03 16:21:11 -070094#else
95static inline int is_itpm(struct pnp_dev *dev)
96{
97 return 0;
98}
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040099#endif
100
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700101static int check_locality(struct tpm_chip *chip, int l)
102{
103 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
104 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
105 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
106 return chip->vendor.locality = l;
107
108 return -1;
109}
110
111static void release_locality(struct tpm_chip *chip, int l, int force)
112{
113 if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
114 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
115 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
116 iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
117 chip->vendor.iobase + TPM_ACCESS(l));
118}
119
120static int request_locality(struct tpm_chip *chip, int l)
121{
Stefan Berger20b87bb2011-03-30 12:13:31 -0400122 unsigned long stop, timeout;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700123 long rc;
124
125 if (check_locality(chip, l) >= 0)
126 return l;
127
128 iowrite8(TPM_ACCESS_REQUEST_USE,
129 chip->vendor.iobase + TPM_ACCESS(l));
130
Stefan Berger20b87bb2011-03-30 12:13:31 -0400131 stop = jiffies + chip->vendor.timeout_a;
132
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700133 if (chip->vendor.irq) {
Stefan Berger20b87bb2011-03-30 12:13:31 -0400134again:
135 timeout = stop - jiffies;
136 if ((long)timeout <= 0)
137 return -1;
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700138 rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700139 (check_locality
140 (chip, l) >= 0),
Stefan Berger20b87bb2011-03-30 12:13:31 -0400141 timeout);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700142 if (rc > 0)
143 return l;
Stefan Berger20b87bb2011-03-30 12:13:31 -0400144 if (rc == -ERESTARTSYS && freezing(current)) {
145 clear_thread_flag(TIF_SIGPENDING);
146 goto again;
147 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700148 } else {
149 /* wait for burstcount */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700150 do {
151 if (check_locality(chip, l) >= 0)
152 return l;
153 msleep(TPM_TIMEOUT);
154 }
155 while (time_before(jiffies, stop));
156 }
157 return -1;
158}
159
160static u8 tpm_tis_status(struct tpm_chip *chip)
161{
162 return ioread8(chip->vendor.iobase +
163 TPM_STS(chip->vendor.locality));
164}
165
166static void tpm_tis_ready(struct tpm_chip *chip)
167{
168 /* this causes the current command to be aborted */
169 iowrite8(TPM_STS_COMMAND_READY,
170 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
171}
172
173static int get_burstcount(struct tpm_chip *chip)
174{
175 unsigned long stop;
176 int burstcnt;
177
178 /* wait for burstcount */
179 /* which timeout value, spec has 2 answers (c & d) */
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700180 stop = jiffies + chip->vendor.timeout_d;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700181 do {
182 burstcnt = ioread8(chip->vendor.iobase +
183 TPM_STS(chip->vendor.locality) + 1);
184 burstcnt += ioread8(chip->vendor.iobase +
185 TPM_STS(chip->vendor.locality) +
186 2) << 8;
187 if (burstcnt)
188 return burstcnt;
189 msleep(TPM_TIMEOUT);
190 } while (time_before(jiffies, stop));
191 return -EBUSY;
192}
193
Kylene Jo Hallcb535422006-04-22 02:39:31 -0700194static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700195{
196 int size = 0, burstcnt;
197 while (size < count &&
Rajiv Andradefd048862011-09-16 14:39:40 -0300198 wait_for_tpm_stat(chip,
199 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
200 chip->vendor.timeout_c,
201 &chip->vendor.read_queue)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700202 == 0) {
203 burstcnt = get_burstcount(chip);
204 for (; burstcnt > 0 && size < count; burstcnt--)
205 buf[size++] = ioread8(chip->vendor.iobase +
206 TPM_DATA_FIFO(chip->vendor.
207 locality));
208 }
209 return size;
210}
211
Kylene Jo Hallcb535422006-04-22 02:39:31 -0700212static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700213{
214 int size = 0;
215 int expected, status;
216
217 if (count < TPM_HEADER_SIZE) {
218 size = -EIO;
219 goto out;
220 }
221
222 /* read first 10 bytes, including tag, paramsize, and result */
223 if ((size =
224 recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
225 dev_err(chip->dev, "Unable to read header\n");
226 goto out;
227 }
228
229 expected = be32_to_cpu(*(__be32 *) (buf + 2));
230 if (expected > count) {
231 size = -EIO;
232 goto out;
233 }
234
235 if ((size +=
236 recv_data(chip, &buf[TPM_HEADER_SIZE],
237 expected - TPM_HEADER_SIZE)) < expected) {
238 dev_err(chip->dev, "Unable to read remainder of result\n");
239 size = -ETIME;
240 goto out;
241 }
242
Rajiv Andradefd048862011-09-16 14:39:40 -0300243 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
244 &chip->vendor.int_queue);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700245 status = tpm_tis_status(chip);
246 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
247 dev_err(chip->dev, "Error left over data\n");
248 size = -EIO;
249 goto out;
250 }
251
252out:
253 tpm_tis_ready(chip);
254 release_locality(chip, chip->vendor.locality, 0);
255 return size;
256}
257
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030258static bool itpm;
Rajiv Andrade3507d612009-09-10 17:09:35 -0300259module_param(itpm, bool, 0444);
260MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
261
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700262/*
263 * If interrupts are used (signaled by an irq set in the vendor structure)
264 * tpm.c can skip polling for the data to be available as the interrupt is
265 * waited for here
266 */
Stefan Berger9519de32011-03-30 12:13:33 -0400267static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700268{
269 int rc, status, burstcnt;
270 size_t count = 0;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700271
272 if (request_locality(chip, 0) < 0)
273 return -EBUSY;
274
275 status = tpm_tis_status(chip);
276 if ((status & TPM_STS_COMMAND_READY) == 0) {
277 tpm_tis_ready(chip);
Rajiv Andradefd048862011-09-16 14:39:40 -0300278 if (wait_for_tpm_stat
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700279 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
280 &chip->vendor.int_queue) < 0) {
281 rc = -ETIME;
282 goto out_err;
283 }
284 }
285
286 while (count < len - 1) {
287 burstcnt = get_burstcount(chip);
288 for (; burstcnt > 0 && count < len - 1; burstcnt--) {
289 iowrite8(buf[count], chip->vendor.iobase +
290 TPM_DATA_FIFO(chip->vendor.locality));
291 count++;
292 }
293
Rajiv Andradefd048862011-09-16 14:39:40 -0300294 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
295 &chip->vendor.int_queue);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700296 status = tpm_tis_status(chip);
Rajiv Andrade3507d612009-09-10 17:09:35 -0300297 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700298 rc = -EIO;
299 goto out_err;
300 }
301 }
302
303 /* write last byte */
304 iowrite8(buf[count],
Stefan Berger9519de32011-03-30 12:13:33 -0400305 chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
Rajiv Andradefd048862011-09-16 14:39:40 -0300306 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
307 &chip->vendor.int_queue);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700308 status = tpm_tis_status(chip);
309 if ((status & TPM_STS_DATA_EXPECT) != 0) {
310 rc = -EIO;
311 goto out_err;
312 }
313
Stefan Berger9519de32011-03-30 12:13:33 -0400314 return 0;
315
316out_err:
317 tpm_tis_ready(chip);
318 release_locality(chip, chip->vendor.locality, 0);
319 return rc;
320}
321
322/*
323 * If interrupts are used (signaled by an irq set in the vendor structure)
324 * tpm.c can skip polling for the data to be available as the interrupt is
325 * waited for here
326 */
327static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
328{
329 int rc;
330 u32 ordinal;
331
332 rc = tpm_tis_send_data(chip, buf, len);
333 if (rc < 0)
334 return rc;
335
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700336 /* go and do it */
337 iowrite8(TPM_STS_GO,
338 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
339
340 if (chip->vendor.irq) {
341 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Rajiv Andradefd048862011-09-16 14:39:40 -0300342 if (wait_for_tpm_stat
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700343 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
344 tpm_calc_ordinal_duration(chip, ordinal),
345 &chip->vendor.read_queue) < 0) {
346 rc = -ETIME;
347 goto out_err;
348 }
349 }
350 return len;
351out_err:
352 tpm_tis_ready(chip);
353 release_locality(chip, chip->vendor.locality, 0);
354 return rc;
355}
356
Stefan Berger9519de32011-03-30 12:13:33 -0400357/*
358 * Early probing for iTPM with STS_DATA_EXPECT flaw.
359 * Try sending command without itpm flag set and if that
360 * fails, repeat with itpm flag set.
361 */
362static int probe_itpm(struct tpm_chip *chip)
363{
364 int rc = 0;
365 u8 cmd_getticks[] = {
366 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
367 0x00, 0x00, 0x00, 0xf1
368 };
369 size_t len = sizeof(cmd_getticks);
370 int rem_itpm = itpm;
371
372 itpm = 0;
373
374 rc = tpm_tis_send_data(chip, cmd_getticks, len);
375 if (rc == 0)
376 goto out;
377
378 tpm_tis_ready(chip);
379 release_locality(chip, chip->vendor.locality, 0);
380
381 itpm = 1;
382
383 rc = tpm_tis_send_data(chip, cmd_getticks, len);
384 if (rc == 0) {
385 dev_info(chip->dev, "Detected an iTPM.\n");
386 rc = 1;
387 } else
388 rc = -EFAULT;
389
390out:
391 itpm = rem_itpm;
392 tpm_tis_ready(chip);
Stefan Bergera927b812011-11-11 12:57:06 -0500393 /* some TPMs need a break here otherwise they will not work
394 * correctly on the immediately subsequent command */
395 msleep(chip->vendor.timeout_b);
Stefan Berger9519de32011-03-30 12:13:33 -0400396 release_locality(chip, chip->vendor.locality, 0);
397
398 return rc;
399}
400
Arjan van de Ven62322d22006-07-03 00:24:21 -0700401static const struct file_operations tis_ops = {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700402 .owner = THIS_MODULE,
403 .llseek = no_llseek,
404 .open = tpm_open,
405 .read = tpm_read,
406 .write = tpm_write,
407 .release = tpm_release,
408};
409
410static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
411static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
412static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
413static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
414static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
415static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
416 NULL);
417static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
418static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
Stefan Berger04ab2292011-03-30 12:13:25 -0400419static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
Stefan Berger62592102011-03-30 12:13:28 -0400420static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700421
422static struct attribute *tis_attrs[] = {
423 &dev_attr_pubek.attr,
424 &dev_attr_pcrs.attr,
425 &dev_attr_enabled.attr,
426 &dev_attr_active.attr,
427 &dev_attr_owned.attr,
428 &dev_attr_temp_deactivated.attr,
429 &dev_attr_caps.attr,
Stefan Berger04ab2292011-03-30 12:13:25 -0400430 &dev_attr_cancel.attr,
Stefan Berger62592102011-03-30 12:13:28 -0400431 &dev_attr_durations.attr,
432 &dev_attr_timeouts.attr, NULL,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700433};
434
435static struct attribute_group tis_attr_grp = {
436 .attrs = tis_attrs
437};
438
439static struct tpm_vendor_specific tpm_tis = {
440 .status = tpm_tis_status,
441 .recv = tpm_tis_recv,
442 .send = tpm_tis_send,
443 .cancel = tpm_tis_ready,
444 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
445 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
446 .req_canceled = TPM_STS_COMMAND_READY,
447 .attr_group = &tis_attr_grp,
448 .miscdev = {
449 .fops = &tis_ops,},
450};
451
David Howells7d12e782006-10-05 14:55:46 +0100452static irqreturn_t tis_int_probe(int irq, void *dev_id)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700453{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400454 struct tpm_chip *chip = dev_id;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700455 u32 interrupt;
456
457 interrupt = ioread32(chip->vendor.iobase +
458 TPM_INT_STATUS(chip->vendor.locality));
459
460 if (interrupt == 0)
461 return IRQ_NONE;
462
Stefan Bergera7b66822011-03-30 12:13:32 -0400463 chip->vendor.probed_irq = irq;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700464
465 /* Clear interrupts handled with TPM_EOI */
466 iowrite32(interrupt,
467 chip->vendor.iobase +
468 TPM_INT_STATUS(chip->vendor.locality));
469 return IRQ_HANDLED;
470}
471
Jeff Garzika6f97b22007-10-31 05:20:49 -0400472static irqreturn_t tis_int_handler(int dummy, void *dev_id)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700473{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400474 struct tpm_chip *chip = dev_id;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700475 u32 interrupt;
476 int i;
477
478 interrupt = ioread32(chip->vendor.iobase +
479 TPM_INT_STATUS(chip->vendor.locality));
480
481 if (interrupt == 0)
482 return IRQ_NONE;
483
484 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
485 wake_up_interruptible(&chip->vendor.read_queue);
486 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
487 for (i = 0; i < 5; i++)
488 if (check_locality(chip, i) >= 0)
489 break;
490 if (interrupt &
491 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
492 TPM_INTF_CMD_READY_INT))
493 wake_up_interruptible(&chip->vendor.int_queue);
494
495 /* Clear interrupts handled with TPM_EOI */
496 iowrite32(interrupt,
497 chip->vendor.iobase +
498 TPM_INT_STATUS(chip->vendor.locality));
Kylene Jo Hallcab091e2006-07-14 00:24:30 -0700499 ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700500 return IRQ_HANDLED;
501}
502
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030503static bool interrupts = 1;
Kylene Jo Hall57135562006-04-22 02:39:44 -0700504module_param(interrupts, bool, 0444);
505MODULE_PARM_DESC(interrupts, "Enable interrupts");
506
Kylene Jo Hallc3c36aa2006-07-14 00:24:31 -0700507static int tpm_tis_init(struct device *dev, resource_size_t start,
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700508 resource_size_t len, unsigned int irq)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700509{
510 u32 vendor, intfcaps, intmask;
Stefan Bergera7b66822011-03-30 12:13:32 -0400511 int rc, i, irq_s, irq_e;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700512 struct tpm_chip *chip;
513
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700514 if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700515 return -ENODEV;
516
517 chip->vendor.iobase = ioremap(start, len);
518 if (!chip->vendor.iobase) {
519 rc = -EIO;
520 goto out_err;
521 }
522
Jason Gunthorpeec579352009-09-09 17:22:18 -0600523 /* Default timeouts */
524 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
525 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
526 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
527 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
528
Marcel Selhorst05a462a2007-11-28 16:21:27 -0800529 if (request_locality(chip, 0) != 0) {
530 rc = -ENODEV;
531 goto out_err;
532 }
533
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700534 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700535
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700536 dev_info(dev,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700537 "1.2 TPM (device-id 0x%X, rev-id %d)\n",
538 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
539
Stefan Berger9519de32011-03-30 12:13:33 -0400540 if (!itpm) {
541 itpm = probe_itpm(chip);
542 if (itpm < 0) {
543 rc = -ENODEV;
544 goto out_err;
545 }
546 }
547
Rajiv Andrade3507d612009-09-10 17:09:35 -0300548 if (itpm)
549 dev_info(dev, "Intel iTPM workaround enabled\n");
550
551
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700552 /* Figure out the capabilities */
553 intfcaps =
554 ioread32(chip->vendor.iobase +
555 TPM_INTF_CAPS(chip->vendor.locality));
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700556 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700557 intfcaps);
558 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700559 dev_dbg(dev, "\tBurst Count Static\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700560 if (intfcaps & TPM_INTF_CMD_READY_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700561 dev_dbg(dev, "\tCommand Ready Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700562 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700563 dev_dbg(dev, "\tInterrupt Edge Falling\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700564 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700565 dev_dbg(dev, "\tInterrupt Edge Rising\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700566 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700567 dev_dbg(dev, "\tInterrupt Level Low\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700568 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700569 dev_dbg(dev, "\tInterrupt Level High\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700570 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700571 dev_dbg(dev, "\tLocality Change Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700572 if (intfcaps & TPM_INTF_STS_VALID_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700573 dev_dbg(dev, "\tSts Valid Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700574 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700575 dev_dbg(dev, "\tData Avail Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700576
Stefan Bergera7b66822011-03-30 12:13:32 -0400577 /* get the timeouts before testing for irqs */
Stefan Berger7f326ed2011-11-11 12:57:05 -0500578 if (tpm_get_timeouts(chip)) {
579 dev_err(dev, "Could not get TPM timeouts and durations\n");
580 rc = -ENODEV;
581 goto out_err;
582 }
Stefan Bergera7b66822011-03-30 12:13:32 -0400583
Stefan Berger68d6e672011-11-11 12:57:04 -0500584 if (tpm_do_selftest(chip)) {
585 dev_err(dev, "TPM self test failed\n");
586 rc = -ENODEV;
587 goto out_err;
588 }
589
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700590 /* INTERRUPT Setup */
591 init_waitqueue_head(&chip->vendor.read_queue);
592 init_waitqueue_head(&chip->vendor.int_queue);
593
594 intmask =
595 ioread32(chip->vendor.iobase +
596 TPM_INT_ENABLE(chip->vendor.locality));
597
598 intmask |= TPM_INTF_CMD_READY_INT
599 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
600 | TPM_INTF_STS_VALID_INT;
601
602 iowrite32(intmask,
603 chip->vendor.iobase +
604 TPM_INT_ENABLE(chip->vendor.locality));
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700605 if (interrupts)
606 chip->vendor.irq = irq;
607 if (interrupts && !chip->vendor.irq) {
Stefan Bergera7b66822011-03-30 12:13:32 -0400608 irq_s =
Kylene Jo Hall57135562006-04-22 02:39:44 -0700609 ioread8(chip->vendor.iobase +
610 TPM_INT_VECTOR(chip->vendor.locality));
Stefan Bergera7b66822011-03-30 12:13:32 -0400611 if (irq_s) {
612 irq_e = irq_s;
613 } else {
614 irq_s = 3;
615 irq_e = 15;
616 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700617
Stefan Bergera7b66822011-03-30 12:13:32 -0400618 for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
Kylene Jo Hall57135562006-04-22 02:39:44 -0700619 iowrite8(i, chip->vendor.iobase +
Stefan Bergera7b66822011-03-30 12:13:32 -0400620 TPM_INT_VECTOR(chip->vendor.locality));
Kylene Jo Hall57135562006-04-22 02:39:44 -0700621 if (request_irq
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700622 (i, tis_int_probe, IRQF_SHARED,
Kylene Jo Hall57135562006-04-22 02:39:44 -0700623 chip->vendor.miscdev.name, chip) != 0) {
624 dev_info(chip->dev,
625 "Unable to request irq: %d for probe\n",
626 i);
627 continue;
628 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700629
Kylene Jo Hall57135562006-04-22 02:39:44 -0700630 /* Clear all existing */
631 iowrite32(ioread32
632 (chip->vendor.iobase +
633 TPM_INT_STATUS(chip->vendor.locality)),
634 chip->vendor.iobase +
635 TPM_INT_STATUS(chip->vendor.locality));
636
637 /* Turn on */
638 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
639 chip->vendor.iobase +
640 TPM_INT_ENABLE(chip->vendor.locality));
641
Stefan Bergera7b66822011-03-30 12:13:32 -0400642 chip->vendor.probed_irq = 0;
643
Kylene Jo Hall57135562006-04-22 02:39:44 -0700644 /* Generate Interrupts */
645 tpm_gen_interrupt(chip);
646
Stefan Bergera7b66822011-03-30 12:13:32 -0400647 chip->vendor.irq = chip->vendor.probed_irq;
648
649 /* free_irq will call into tis_int_probe;
650 clear all irqs we haven't seen while doing
651 tpm_gen_interrupt */
652 iowrite32(ioread32
653 (chip->vendor.iobase +
654 TPM_INT_STATUS(chip->vendor.locality)),
655 chip->vendor.iobase +
656 TPM_INT_STATUS(chip->vendor.locality));
657
Kylene Jo Hall57135562006-04-22 02:39:44 -0700658 /* Turn off */
659 iowrite32(intmask,
660 chip->vendor.iobase +
661 TPM_INT_ENABLE(chip->vendor.locality));
662 free_irq(i, chip);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700663 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700664 }
665 if (chip->vendor.irq) {
666 iowrite8(chip->vendor.irq,
667 chip->vendor.iobase +
668 TPM_INT_VECTOR(chip->vendor.locality));
669 if (request_irq
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700670 (chip->vendor.irq, tis_int_handler, IRQF_SHARED,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700671 chip->vendor.miscdev.name, chip) != 0) {
672 dev_info(chip->dev,
Kylene Jo Hall57135562006-04-22 02:39:44 -0700673 "Unable to request irq: %d for use\n",
674 chip->vendor.irq);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700675 chip->vendor.irq = 0;
676 } else {
677 /* Clear all existing */
678 iowrite32(ioread32
679 (chip->vendor.iobase +
680 TPM_INT_STATUS(chip->vendor.locality)),
681 chip->vendor.iobase +
682 TPM_INT_STATUS(chip->vendor.locality));
683
684 /* Turn on */
685 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
686 chip->vendor.iobase +
687 TPM_INT_ENABLE(chip->vendor.locality));
688 }
689 }
690
691 INIT_LIST_HEAD(&chip->vendor.list);
692 spin_lock(&tis_lock);
693 list_add(&chip->vendor.list, &tis_chips);
694 spin_unlock(&tis_lock);
695
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700696
697 return 0;
698out_err:
699 if (chip->vendor.iobase)
700 iounmap(chip->vendor.iobase);
701 tpm_remove_hardware(chip->dev);
702 return rc;
703}
Stefan Berger96854312011-07-17 01:04:24 -0400704
705static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
706{
707 u32 intmask;
708
709 /* reenable interrupts that device may have lost or
710 BIOS/firmware may have disabled */
711 iowrite8(chip->vendor.irq, chip->vendor.iobase +
712 TPM_INT_VECTOR(chip->vendor.locality));
713
714 intmask =
715 ioread32(chip->vendor.iobase +
716 TPM_INT_ENABLE(chip->vendor.locality));
717
718 intmask |= TPM_INTF_CMD_READY_INT
719 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
720 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
721
722 iowrite32(intmask,
723 chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
724}
725
726
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300727#ifdef CONFIG_PNP
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700728static int __devinit tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
729 const struct pnp_device_id *pnp_id)
730{
Kylene Jo Hallc3c36aa2006-07-14 00:24:31 -0700731 resource_size_t start, len;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700732 unsigned int irq = 0;
733
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700734 start = pnp_mem_start(pnp_dev, 0);
735 len = pnp_mem_len(pnp_dev, 0);
736
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700737 if (pnp_irq_valid(pnp_dev, 0))
738 irq = pnp_irq(pnp_dev, 0);
739 else
740 interrupts = 0;
741
Olof Johanssone5cce6c2011-01-06 21:24:01 -0600742 if (is_itpm(pnp_dev))
743 itpm = 1;
744
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700745 return tpm_tis_init(&pnp_dev->dev, start, len, irq);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700746}
747
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700748static int tpm_tis_pnp_suspend(struct pnp_dev *dev, pm_message_t msg)
749{
750 return tpm_pm_suspend(&dev->dev, msg);
751}
752
753static int tpm_tis_pnp_resume(struct pnp_dev *dev)
754{
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700755 struct tpm_chip *chip = pnp_get_drvdata(dev);
756 int ret;
757
Stefan Berger45baa1d2011-03-30 12:13:30 -0400758 if (chip->vendor.irq)
759 tpm_tis_reenable_interrupts(chip);
760
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700761 ret = tpm_pm_resume(&dev->dev);
762 if (!ret)
Stefan Berger68d6e672011-11-11 12:57:04 -0500763 tpm_do_selftest(chip);
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700764
765 return ret;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700766}
767
768static struct pnp_device_id tpm_pnp_tbl[] __devinitdata = {
769 {"PNP0C31", 0}, /* TPM */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700770 {"ATM1200", 0}, /* Atmel */
771 {"IFX0102", 0}, /* Infineon */
772 {"BCM0101", 0}, /* Broadcom */
LE DISEZ Erwan061991e2008-07-25 19:44:56 -0700773 {"BCM0102", 0}, /* Broadcom */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700774 {"NSC1200", 0}, /* National */
Marcin Obarafb0e7e12008-07-10 17:30:42 -0700775 {"ICO0102", 0}, /* Intel */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700776 /* Add new here */
777 {"", 0}, /* User Specified */
778 {"", 0} /* Terminator */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700779};
Matt Domsch31bde712009-11-03 12:05:50 +1100780MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700781
Rajiv Andrade253115b2008-10-11 09:04:39 +1100782static __devexit void tpm_tis_pnp_remove(struct pnp_dev *dev)
783{
784 struct tpm_chip *chip = pnp_get_drvdata(dev);
785
786 tpm_dev_vendor_release(chip);
787
788 kfree(chip);
789}
790
791
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700792static struct pnp_driver tis_pnp_driver = {
793 .name = "tpm_tis",
794 .id_table = tpm_pnp_tbl,
795 .probe = tpm_tis_pnp_init,
796 .suspend = tpm_tis_pnp_suspend,
797 .resume = tpm_tis_pnp_resume,
Rajiv Andrade253115b2008-10-11 09:04:39 +1100798 .remove = tpm_tis_pnp_remove,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700799};
800
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700801#define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
802module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
803 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
804MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300805#endif
Ming Lei7a192ec2009-02-06 23:40:12 +0800806static int tpm_tis_suspend(struct platform_device *dev, pm_message_t msg)
807{
808 return tpm_pm_suspend(&dev->dev, msg);
809}
810
811static int tpm_tis_resume(struct platform_device *dev)
812{
Stefan Berger45baa1d2011-03-30 12:13:30 -0400813 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
814
815 if (chip->vendor.irq)
816 tpm_tis_reenable_interrupts(chip);
817
Ming Lei7a192ec2009-02-06 23:40:12 +0800818 return tpm_pm_resume(&dev->dev);
819}
820static struct platform_driver tis_drv = {
821 .driver = {
822 .name = "tpm_tis",
823 .owner = THIS_MODULE,
824 },
825 .suspend = tpm_tis_suspend,
826 .resume = tpm_tis_resume,
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700827};
828
829static struct platform_device *pdev;
830
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030831static bool force;
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700832module_param(force, bool, 0444);
833MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700834static int __init init_tis(void)
835{
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700836 int rc;
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300837#ifdef CONFIG_PNP
838 if (!force)
839 return pnp_register_driver(&tis_pnp_driver);
840#endif
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700841
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300842 rc = platform_driver_register(&tis_drv);
843 if (rc < 0)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700844 return rc;
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300845 if (IS_ERR(pdev=platform_device_register_simple("tpm_tis", -1, NULL, 0)))
846 return PTR_ERR(pdev);
847 if((rc=tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0)) != 0) {
848 platform_device_unregister(pdev);
849 platform_driver_unregister(&tis_drv);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700850 }
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300851 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700852}
853
854static void __exit cleanup_tis(void)
855{
856 struct tpm_vendor_specific *i, *j;
857 struct tpm_chip *chip;
858 spin_lock(&tis_lock);
859 list_for_each_entry_safe(i, j, &tis_chips, list) {
860 chip = to_tpm_chip(i);
Rajiv Andrade253115b2008-10-11 09:04:39 +1100861 tpm_remove_hardware(chip->dev);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700862 iowrite32(~TPM_GLOBAL_INT_ENABLE &
863 ioread32(chip->vendor.iobase +
864 TPM_INT_ENABLE(chip->vendor.
865 locality)),
866 chip->vendor.iobase +
867 TPM_INT_ENABLE(chip->vendor.locality));
868 release_locality(chip, chip->vendor.locality, 1);
869 if (chip->vendor.irq)
870 free_irq(chip->vendor.irq, chip);
871 iounmap(i->iobase);
872 list_del(&i->list);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700873 }
874 spin_unlock(&tis_lock);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300875#ifdef CONFIG_PNP
876 if (!force) {
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700877 pnp_unregister_driver(&tis_pnp_driver);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300878 return;
879 }
880#endif
881 platform_device_unregister(pdev);
882 platform_driver_unregister(&tis_drv);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700883}
884
885module_init(init_tis);
886module_exit(cleanup_tis);
887MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
888MODULE_DESCRIPTION("TPM Driver");
889MODULE_VERSION("2.0");
890MODULE_LICENSE("GPL");