blob: 47517e49d2beb8f6592b49332ff1ad8f782800c3 [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
32#define TPM_HEADER_SIZE 10
33
34enum tis_access {
35 TPM_ACCESS_VALID = 0x80,
36 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
37 TPM_ACCESS_REQUEST_PENDING = 0x04,
38 TPM_ACCESS_REQUEST_USE = 0x02,
39};
40
41enum tis_status {
42 TPM_STS_VALID = 0x80,
43 TPM_STS_COMMAND_READY = 0x40,
44 TPM_STS_GO = 0x20,
45 TPM_STS_DATA_AVAIL = 0x10,
46 TPM_STS_DATA_EXPECT = 0x08,
47};
48
49enum tis_int_flags {
50 TPM_GLOBAL_INT_ENABLE = 0x80000000,
51 TPM_INTF_BURST_COUNT_STATIC = 0x100,
52 TPM_INTF_CMD_READY_INT = 0x080,
53 TPM_INTF_INT_EDGE_FALLING = 0x040,
54 TPM_INTF_INT_EDGE_RISING = 0x020,
55 TPM_INTF_INT_LEVEL_LOW = 0x010,
56 TPM_INTF_INT_LEVEL_HIGH = 0x008,
57 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
58 TPM_INTF_STS_VALID_INT = 0x002,
59 TPM_INTF_DATA_AVAIL_INT = 0x001,
60};
61
Kylene Jo Hall36b20022006-04-22 02:38:19 -070062enum tis_defaults {
Kylene Jo Hall2a7362f2006-05-15 09:44:25 -070063 TIS_MEM_BASE = 0xFED40000,
Kylene Jo Hallb09d5302006-04-22 02:38:55 -070064 TIS_MEM_LEN = 0x5000,
Kylene Jo Hallcb535422006-04-22 02:39:31 -070065 TIS_SHORT_TIMEOUT = 750, /* ms */
66 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
Kylene Jo Hall36b20022006-04-22 02:38:19 -070067};
68
Leendert van Doorn27084ef2006-04-22 02:38:03 -070069#define TPM_ACCESS(l) (0x0000 | ((l) << 12))
70#define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
71#define TPM_INT_VECTOR(l) (0x000C | ((l) << 12))
72#define TPM_INT_STATUS(l) (0x0010 | ((l) << 12))
73#define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
74#define TPM_STS(l) (0x0018 | ((l) << 12))
75#define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
76
77#define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
78#define TPM_RID(l) (0x0F04 | ((l) << 12))
79
80static LIST_HEAD(tis_chips);
81static DEFINE_SPINLOCK(tis_lock);
82
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040083#ifdef CONFIG_ACPI
84static int is_itpm(struct pnp_dev *dev)
85{
86 struct acpi_device *acpi = pnp_acpi_device(dev);
87 struct acpi_hardware_id *id;
88
89 list_for_each_entry(id, &acpi->pnp.ids, list) {
90 if (!strcmp("INTC0102", id->id))
91 return 1;
92 }
93
94 return 0;
95}
96#else
97static int is_itpm(struct pnp_dev *dev)
98{
99 return 0;
100}
101#endif
102
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700103static int check_locality(struct tpm_chip *chip, int l)
104{
105 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
106 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
107 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
108 return chip->vendor.locality = l;
109
110 return -1;
111}
112
113static void release_locality(struct tpm_chip *chip, int l, int force)
114{
115 if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
116 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
117 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
118 iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
119 chip->vendor.iobase + TPM_ACCESS(l));
120}
121
122static int request_locality(struct tpm_chip *chip, int l)
123{
Stefan Berger20b87bb2011-03-30 12:13:31 -0400124 unsigned long stop, timeout;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700125 long rc;
126
127 if (check_locality(chip, l) >= 0)
128 return l;
129
130 iowrite8(TPM_ACCESS_REQUEST_USE,
131 chip->vendor.iobase + TPM_ACCESS(l));
132
Stefan Berger20b87bb2011-03-30 12:13:31 -0400133 stop = jiffies + chip->vendor.timeout_a;
134
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700135 if (chip->vendor.irq) {
Stefan Berger20b87bb2011-03-30 12:13:31 -0400136again:
137 timeout = stop - jiffies;
138 if ((long)timeout <= 0)
139 return -1;
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700140 rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700141 (check_locality
142 (chip, l) >= 0),
Stefan Berger20b87bb2011-03-30 12:13:31 -0400143 timeout);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700144 if (rc > 0)
145 return l;
Stefan Berger20b87bb2011-03-30 12:13:31 -0400146 if (rc == -ERESTARTSYS && freezing(current)) {
147 clear_thread_flag(TIF_SIGPENDING);
148 goto again;
149 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700150 } else {
151 /* wait for burstcount */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700152 do {
153 if (check_locality(chip, l) >= 0)
154 return l;
155 msleep(TPM_TIMEOUT);
156 }
157 while (time_before(jiffies, stop));
158 }
159 return -1;
160}
161
162static u8 tpm_tis_status(struct tpm_chip *chip)
163{
164 return ioread8(chip->vendor.iobase +
165 TPM_STS(chip->vendor.locality));
166}
167
168static void tpm_tis_ready(struct tpm_chip *chip)
169{
170 /* this causes the current command to be aborted */
171 iowrite8(TPM_STS_COMMAND_READY,
172 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
173}
174
175static int get_burstcount(struct tpm_chip *chip)
176{
177 unsigned long stop;
178 int burstcnt;
179
180 /* wait for burstcount */
181 /* which timeout value, spec has 2 answers (c & d) */
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700182 stop = jiffies + chip->vendor.timeout_d;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700183 do {
184 burstcnt = ioread8(chip->vendor.iobase +
185 TPM_STS(chip->vendor.locality) + 1);
186 burstcnt += ioread8(chip->vendor.iobase +
187 TPM_STS(chip->vendor.locality) +
188 2) << 8;
189 if (burstcnt)
190 return burstcnt;
191 msleep(TPM_TIMEOUT);
192 } while (time_before(jiffies, stop));
193 return -EBUSY;
194}
195
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700196static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700197 wait_queue_head_t *queue)
198{
199 unsigned long stop;
200 long rc;
201 u8 status;
202
203 /* check current status */
204 status = tpm_tis_status(chip);
205 if ((status & mask) == mask)
206 return 0;
207
Stefan Berger20b87bb2011-03-30 12:13:31 -0400208 stop = jiffies + timeout;
209
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700210 if (chip->vendor.irq) {
Stefan Berger20b87bb2011-03-30 12:13:31 -0400211again:
212 timeout = stop - jiffies;
213 if ((long)timeout <= 0)
214 return -ETIME;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700215 rc = wait_event_interruptible_timeout(*queue,
216 ((tpm_tis_status
217 (chip) & mask) ==
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700218 mask), timeout);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700219 if (rc > 0)
220 return 0;
Stefan Berger20b87bb2011-03-30 12:13:31 -0400221 if (rc == -ERESTARTSYS && freezing(current)) {
222 clear_thread_flag(TIF_SIGPENDING);
223 goto again;
224 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700225 } else {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700226 do {
227 msleep(TPM_TIMEOUT);
228 status = tpm_tis_status(chip);
229 if ((status & mask) == mask)
230 return 0;
231 } while (time_before(jiffies, stop));
232 }
233 return -ETIME;
234}
235
Kylene Jo Hallcb535422006-04-22 02:39:31 -0700236static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700237{
238 int size = 0, burstcnt;
239 while (size < count &&
240 wait_for_stat(chip,
241 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
242 chip->vendor.timeout_c,
243 &chip->vendor.read_queue)
244 == 0) {
245 burstcnt = get_burstcount(chip);
246 for (; burstcnt > 0 && size < count; burstcnt--)
247 buf[size++] = ioread8(chip->vendor.iobase +
248 TPM_DATA_FIFO(chip->vendor.
249 locality));
250 }
251 return size;
252}
253
Kylene Jo Hallcb535422006-04-22 02:39:31 -0700254static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700255{
256 int size = 0;
257 int expected, status;
258
259 if (count < TPM_HEADER_SIZE) {
260 size = -EIO;
261 goto out;
262 }
263
264 /* read first 10 bytes, including tag, paramsize, and result */
265 if ((size =
266 recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
267 dev_err(chip->dev, "Unable to read header\n");
268 goto out;
269 }
270
271 expected = be32_to_cpu(*(__be32 *) (buf + 2));
272 if (expected > count) {
273 size = -EIO;
274 goto out;
275 }
276
277 if ((size +=
278 recv_data(chip, &buf[TPM_HEADER_SIZE],
279 expected - TPM_HEADER_SIZE)) < expected) {
280 dev_err(chip->dev, "Unable to read remainder of result\n");
281 size = -ETIME;
282 goto out;
283 }
284
285 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
286 &chip->vendor.int_queue);
287 status = tpm_tis_status(chip);
288 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
289 dev_err(chip->dev, "Error left over data\n");
290 size = -EIO;
291 goto out;
292 }
293
294out:
295 tpm_tis_ready(chip);
296 release_locality(chip, chip->vendor.locality, 0);
297 return size;
298}
299
Rajiv Andrade3507d612009-09-10 17:09:35 -0300300static int itpm;
301module_param(itpm, bool, 0444);
302MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
303
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700304/*
305 * If interrupts are used (signaled by an irq set in the vendor structure)
306 * tpm.c can skip polling for the data to be available as the interrupt is
307 * waited for here
308 */
Kylene Jo Hallcb535422006-04-22 02:39:31 -0700309static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700310{
311 int rc, status, burstcnt;
312 size_t count = 0;
313 u32 ordinal;
314
315 if (request_locality(chip, 0) < 0)
316 return -EBUSY;
317
318 status = tpm_tis_status(chip);
319 if ((status & TPM_STS_COMMAND_READY) == 0) {
320 tpm_tis_ready(chip);
321 if (wait_for_stat
322 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
323 &chip->vendor.int_queue) < 0) {
324 rc = -ETIME;
325 goto out_err;
326 }
327 }
328
329 while (count < len - 1) {
330 burstcnt = get_burstcount(chip);
331 for (; burstcnt > 0 && count < len - 1; burstcnt--) {
332 iowrite8(buf[count], chip->vendor.iobase +
333 TPM_DATA_FIFO(chip->vendor.locality));
334 count++;
335 }
336
337 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
338 &chip->vendor.int_queue);
339 status = tpm_tis_status(chip);
Rajiv Andrade3507d612009-09-10 17:09:35 -0300340 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700341 rc = -EIO;
342 goto out_err;
343 }
344 }
345
346 /* write last byte */
347 iowrite8(buf[count],
348 chip->vendor.iobase +
349 TPM_DATA_FIFO(chip->vendor.locality));
350 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
351 &chip->vendor.int_queue);
352 status = tpm_tis_status(chip);
353 if ((status & TPM_STS_DATA_EXPECT) != 0) {
354 rc = -EIO;
355 goto out_err;
356 }
357
358 /* go and do it */
359 iowrite8(TPM_STS_GO,
360 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
361
362 if (chip->vendor.irq) {
363 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
364 if (wait_for_stat
365 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
366 tpm_calc_ordinal_duration(chip, ordinal),
367 &chip->vendor.read_queue) < 0) {
368 rc = -ETIME;
369 goto out_err;
370 }
371 }
372 return len;
373out_err:
374 tpm_tis_ready(chip);
375 release_locality(chip, chip->vendor.locality, 0);
376 return rc;
377}
378
Arjan van de Ven62322d22006-07-03 00:24:21 -0700379static const struct file_operations tis_ops = {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700380 .owner = THIS_MODULE,
381 .llseek = no_llseek,
382 .open = tpm_open,
383 .read = tpm_read,
384 .write = tpm_write,
385 .release = tpm_release,
386};
387
388static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
389static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
390static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
391static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
392static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
393static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
394 NULL);
395static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
396static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
Stefan Berger04ab2292011-03-30 12:13:25 -0400397static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
Stefan Berger62592102011-03-30 12:13:28 -0400398static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700399
400static struct attribute *tis_attrs[] = {
401 &dev_attr_pubek.attr,
402 &dev_attr_pcrs.attr,
403 &dev_attr_enabled.attr,
404 &dev_attr_active.attr,
405 &dev_attr_owned.attr,
406 &dev_attr_temp_deactivated.attr,
407 &dev_attr_caps.attr,
Stefan Berger04ab2292011-03-30 12:13:25 -0400408 &dev_attr_cancel.attr,
Stefan Berger62592102011-03-30 12:13:28 -0400409 &dev_attr_durations.attr,
410 &dev_attr_timeouts.attr, NULL,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700411};
412
413static struct attribute_group tis_attr_grp = {
414 .attrs = tis_attrs
415};
416
417static struct tpm_vendor_specific tpm_tis = {
418 .status = tpm_tis_status,
419 .recv = tpm_tis_recv,
420 .send = tpm_tis_send,
421 .cancel = tpm_tis_ready,
422 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
423 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
424 .req_canceled = TPM_STS_COMMAND_READY,
425 .attr_group = &tis_attr_grp,
426 .miscdev = {
427 .fops = &tis_ops,},
428};
429
David Howells7d12e782006-10-05 14:55:46 +0100430static irqreturn_t tis_int_probe(int irq, void *dev_id)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700431{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400432 struct tpm_chip *chip = dev_id;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700433 u32 interrupt;
434
435 interrupt = ioread32(chip->vendor.iobase +
436 TPM_INT_STATUS(chip->vendor.locality));
437
438 if (interrupt == 0)
439 return IRQ_NONE;
440
Stefan Bergera7b66822011-03-30 12:13:32 -0400441 chip->vendor.probed_irq = irq;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700442
443 /* Clear interrupts handled with TPM_EOI */
444 iowrite32(interrupt,
445 chip->vendor.iobase +
446 TPM_INT_STATUS(chip->vendor.locality));
447 return IRQ_HANDLED;
448}
449
Jeff Garzika6f97b22007-10-31 05:20:49 -0400450static irqreturn_t tis_int_handler(int dummy, void *dev_id)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700451{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400452 struct tpm_chip *chip = dev_id;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700453 u32 interrupt;
454 int i;
455
456 interrupt = ioread32(chip->vendor.iobase +
457 TPM_INT_STATUS(chip->vendor.locality));
458
459 if (interrupt == 0)
460 return IRQ_NONE;
461
462 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
463 wake_up_interruptible(&chip->vendor.read_queue);
464 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
465 for (i = 0; i < 5; i++)
466 if (check_locality(chip, i) >= 0)
467 break;
468 if (interrupt &
469 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
470 TPM_INTF_CMD_READY_INT))
471 wake_up_interruptible(&chip->vendor.int_queue);
472
473 /* Clear interrupts handled with TPM_EOI */
474 iowrite32(interrupt,
475 chip->vendor.iobase +
476 TPM_INT_STATUS(chip->vendor.locality));
Kylene Jo Hallcab091e2006-07-14 00:24:30 -0700477 ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700478 return IRQ_HANDLED;
479}
480
Kylene Jo Hall57135562006-04-22 02:39:44 -0700481static int interrupts = 1;
482module_param(interrupts, bool, 0444);
483MODULE_PARM_DESC(interrupts, "Enable interrupts");
484
Kylene Jo Hallc3c36aa2006-07-14 00:24:31 -0700485static int tpm_tis_init(struct device *dev, resource_size_t start,
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700486 resource_size_t len, unsigned int irq)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700487{
488 u32 vendor, intfcaps, intmask;
Stefan Bergera7b66822011-03-30 12:13:32 -0400489 int rc, i, irq_s, irq_e;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700490 struct tpm_chip *chip;
491
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700492 if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700493 return -ENODEV;
494
495 chip->vendor.iobase = ioremap(start, len);
496 if (!chip->vendor.iobase) {
497 rc = -EIO;
498 goto out_err;
499 }
500
Jason Gunthorpeec579352009-09-09 17:22:18 -0600501 /* Default timeouts */
502 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
503 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
504 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
505 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
506
Marcel Selhorst05a462a2007-11-28 16:21:27 -0800507 if (request_locality(chip, 0) != 0) {
508 rc = -ENODEV;
509 goto out_err;
510 }
511
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700512 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700513
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700514 dev_info(dev,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700515 "1.2 TPM (device-id 0x%X, rev-id %d)\n",
516 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
517
Rajiv Andrade3507d612009-09-10 17:09:35 -0300518 if (itpm)
519 dev_info(dev, "Intel iTPM workaround enabled\n");
520
521
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700522 /* Figure out the capabilities */
523 intfcaps =
524 ioread32(chip->vendor.iobase +
525 TPM_INTF_CAPS(chip->vendor.locality));
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700526 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700527 intfcaps);
528 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700529 dev_dbg(dev, "\tBurst Count Static\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700530 if (intfcaps & TPM_INTF_CMD_READY_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700531 dev_dbg(dev, "\tCommand Ready Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700532 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700533 dev_dbg(dev, "\tInterrupt Edge Falling\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700534 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700535 dev_dbg(dev, "\tInterrupt Edge Rising\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700536 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700537 dev_dbg(dev, "\tInterrupt Level Low\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700538 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700539 dev_dbg(dev, "\tInterrupt Level High\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700540 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700541 dev_dbg(dev, "\tLocality Change Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700542 if (intfcaps & TPM_INTF_STS_VALID_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700543 dev_dbg(dev, "\tSts Valid Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700544 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700545 dev_dbg(dev, "\tData Avail Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700546
Stefan Bergera7b66822011-03-30 12:13:32 -0400547 /* get the timeouts before testing for irqs */
548 tpm_get_timeouts(chip);
549
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700550 /* INTERRUPT Setup */
551 init_waitqueue_head(&chip->vendor.read_queue);
552 init_waitqueue_head(&chip->vendor.int_queue);
553
554 intmask =
555 ioread32(chip->vendor.iobase +
556 TPM_INT_ENABLE(chip->vendor.locality));
557
558 intmask |= TPM_INTF_CMD_READY_INT
559 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
560 | TPM_INTF_STS_VALID_INT;
561
562 iowrite32(intmask,
563 chip->vendor.iobase +
564 TPM_INT_ENABLE(chip->vendor.locality));
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700565 if (interrupts)
566 chip->vendor.irq = irq;
567 if (interrupts && !chip->vendor.irq) {
Stefan Bergera7b66822011-03-30 12:13:32 -0400568 irq_s =
Kylene Jo Hall57135562006-04-22 02:39:44 -0700569 ioread8(chip->vendor.iobase +
570 TPM_INT_VECTOR(chip->vendor.locality));
Stefan Bergera7b66822011-03-30 12:13:32 -0400571 if (irq_s) {
572 irq_e = irq_s;
573 } else {
574 irq_s = 3;
575 irq_e = 15;
576 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700577
Stefan Bergera7b66822011-03-30 12:13:32 -0400578 for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
Kylene Jo Hall57135562006-04-22 02:39:44 -0700579 iowrite8(i, chip->vendor.iobase +
Stefan Bergera7b66822011-03-30 12:13:32 -0400580 TPM_INT_VECTOR(chip->vendor.locality));
Kylene Jo Hall57135562006-04-22 02:39:44 -0700581 if (request_irq
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700582 (i, tis_int_probe, IRQF_SHARED,
Kylene Jo Hall57135562006-04-22 02:39:44 -0700583 chip->vendor.miscdev.name, chip) != 0) {
584 dev_info(chip->dev,
585 "Unable to request irq: %d for probe\n",
586 i);
587 continue;
588 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700589
Kylene Jo Hall57135562006-04-22 02:39:44 -0700590 /* Clear all existing */
591 iowrite32(ioread32
592 (chip->vendor.iobase +
593 TPM_INT_STATUS(chip->vendor.locality)),
594 chip->vendor.iobase +
595 TPM_INT_STATUS(chip->vendor.locality));
596
597 /* Turn on */
598 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
599 chip->vendor.iobase +
600 TPM_INT_ENABLE(chip->vendor.locality));
601
Stefan Bergera7b66822011-03-30 12:13:32 -0400602 chip->vendor.probed_irq = 0;
603
Kylene Jo Hall57135562006-04-22 02:39:44 -0700604 /* Generate Interrupts */
605 tpm_gen_interrupt(chip);
606
Stefan Bergera7b66822011-03-30 12:13:32 -0400607 chip->vendor.irq = chip->vendor.probed_irq;
608
609 /* free_irq will call into tis_int_probe;
610 clear all irqs we haven't seen while doing
611 tpm_gen_interrupt */
612 iowrite32(ioread32
613 (chip->vendor.iobase +
614 TPM_INT_STATUS(chip->vendor.locality)),
615 chip->vendor.iobase +
616 TPM_INT_STATUS(chip->vendor.locality));
617
Kylene Jo Hall57135562006-04-22 02:39:44 -0700618 /* Turn off */
619 iowrite32(intmask,
620 chip->vendor.iobase +
621 TPM_INT_ENABLE(chip->vendor.locality));
622 free_irq(i, chip);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700623 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700624 }
625 if (chip->vendor.irq) {
626 iowrite8(chip->vendor.irq,
627 chip->vendor.iobase +
628 TPM_INT_VECTOR(chip->vendor.locality));
629 if (request_irq
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700630 (chip->vendor.irq, tis_int_handler, IRQF_SHARED,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700631 chip->vendor.miscdev.name, chip) != 0) {
632 dev_info(chip->dev,
Kylene Jo Hall57135562006-04-22 02:39:44 -0700633 "Unable to request irq: %d for use\n",
634 chip->vendor.irq);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700635 chip->vendor.irq = 0;
636 } else {
637 /* Clear all existing */
638 iowrite32(ioread32
639 (chip->vendor.iobase +
640 TPM_INT_STATUS(chip->vendor.locality)),
641 chip->vendor.iobase +
642 TPM_INT_STATUS(chip->vendor.locality));
643
644 /* Turn on */
645 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
646 chip->vendor.iobase +
647 TPM_INT_ENABLE(chip->vendor.locality));
648 }
649 }
650
651 INIT_LIST_HEAD(&chip->vendor.list);
652 spin_lock(&tis_lock);
653 list_add(&chip->vendor.list, &tis_chips);
654 spin_unlock(&tis_lock);
655
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700656 tpm_continue_selftest(chip);
657
658 return 0;
659out_err:
660 if (chip->vendor.iobase)
661 iounmap(chip->vendor.iobase);
662 tpm_remove_hardware(chip->dev);
663 return rc;
664}
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300665#ifdef CONFIG_PNP
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700666static int __devinit tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
667 const struct pnp_device_id *pnp_id)
668{
Kylene Jo Hallc3c36aa2006-07-14 00:24:31 -0700669 resource_size_t start, len;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700670 unsigned int irq = 0;
671
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700672 start = pnp_mem_start(pnp_dev, 0);
673 len = pnp_mem_len(pnp_dev, 0);
674
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700675 if (pnp_irq_valid(pnp_dev, 0))
676 irq = pnp_irq(pnp_dev, 0);
677 else
678 interrupts = 0;
679
Olof Johanssone5cce6c2011-01-06 21:24:01 -0600680 if (is_itpm(pnp_dev))
681 itpm = 1;
682
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700683 return tpm_tis_init(&pnp_dev->dev, start, len, irq);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700684}
685
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700686static int tpm_tis_pnp_suspend(struct pnp_dev *dev, pm_message_t msg)
687{
688 return tpm_pm_suspend(&dev->dev, msg);
689}
690
Stefan Berger45baa1d2011-03-30 12:13:30 -0400691static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
692{
693 u32 intmask;
694
695 /* reenable interrupts that device may have lost or
696 BIOS/firmware may have disabled */
697 iowrite8(chip->vendor.irq, chip->vendor.iobase +
698 TPM_INT_VECTOR(chip->vendor.locality));
699
700 intmask =
701 ioread32(chip->vendor.iobase +
702 TPM_INT_ENABLE(chip->vendor.locality));
703
704 intmask |= TPM_INTF_CMD_READY_INT
705 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
706 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
707
708 iowrite32(intmask,
709 chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
710}
711
712
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700713static int tpm_tis_pnp_resume(struct pnp_dev *dev)
714{
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700715 struct tpm_chip *chip = pnp_get_drvdata(dev);
716 int ret;
717
Stefan Berger45baa1d2011-03-30 12:13:30 -0400718 if (chip->vendor.irq)
719 tpm_tis_reenable_interrupts(chip);
720
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700721 ret = tpm_pm_resume(&dev->dev);
722 if (!ret)
723 tpm_continue_selftest(chip);
724
725 return ret;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700726}
727
728static struct pnp_device_id tpm_pnp_tbl[] __devinitdata = {
729 {"PNP0C31", 0}, /* TPM */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700730 {"ATM1200", 0}, /* Atmel */
731 {"IFX0102", 0}, /* Infineon */
732 {"BCM0101", 0}, /* Broadcom */
LE DISEZ Erwan061991e2008-07-25 19:44:56 -0700733 {"BCM0102", 0}, /* Broadcom */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700734 {"NSC1200", 0}, /* National */
Marcin Obarafb0e7e12008-07-10 17:30:42 -0700735 {"ICO0102", 0}, /* Intel */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700736 /* Add new here */
737 {"", 0}, /* User Specified */
738 {"", 0} /* Terminator */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700739};
Matt Domsch31bde712009-11-03 12:05:50 +1100740MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700741
Rajiv Andrade253115b2008-10-11 09:04:39 +1100742static __devexit void tpm_tis_pnp_remove(struct pnp_dev *dev)
743{
744 struct tpm_chip *chip = pnp_get_drvdata(dev);
745
746 tpm_dev_vendor_release(chip);
747
748 kfree(chip);
749}
750
751
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700752static struct pnp_driver tis_pnp_driver = {
753 .name = "tpm_tis",
754 .id_table = tpm_pnp_tbl,
755 .probe = tpm_tis_pnp_init,
756 .suspend = tpm_tis_pnp_suspend,
757 .resume = tpm_tis_pnp_resume,
Rajiv Andrade253115b2008-10-11 09:04:39 +1100758 .remove = tpm_tis_pnp_remove,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700759};
760
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700761#define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
762module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
763 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
764MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300765#endif
Ming Lei7a192ec2009-02-06 23:40:12 +0800766static int tpm_tis_suspend(struct platform_device *dev, pm_message_t msg)
767{
768 return tpm_pm_suspend(&dev->dev, msg);
769}
770
771static int tpm_tis_resume(struct platform_device *dev)
772{
Stefan Berger45baa1d2011-03-30 12:13:30 -0400773 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
774
775 if (chip->vendor.irq)
776 tpm_tis_reenable_interrupts(chip);
777
Ming Lei7a192ec2009-02-06 23:40:12 +0800778 return tpm_pm_resume(&dev->dev);
779}
780static struct platform_driver tis_drv = {
781 .driver = {
782 .name = "tpm_tis",
783 .owner = THIS_MODULE,
784 },
785 .suspend = tpm_tis_suspend,
786 .resume = tpm_tis_resume,
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700787};
788
789static struct platform_device *pdev;
790
791static int force;
792module_param(force, bool, 0444);
793MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700794static int __init init_tis(void)
795{
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700796 int rc;
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300797#ifdef CONFIG_PNP
798 if (!force)
799 return pnp_register_driver(&tis_pnp_driver);
800#endif
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700801
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300802 rc = platform_driver_register(&tis_drv);
803 if (rc < 0)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700804 return rc;
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300805 if (IS_ERR(pdev=platform_device_register_simple("tpm_tis", -1, NULL, 0)))
806 return PTR_ERR(pdev);
807 if((rc=tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0)) != 0) {
808 platform_device_unregister(pdev);
809 platform_driver_unregister(&tis_drv);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700810 }
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300811 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700812}
813
814static void __exit cleanup_tis(void)
815{
816 struct tpm_vendor_specific *i, *j;
817 struct tpm_chip *chip;
818 spin_lock(&tis_lock);
819 list_for_each_entry_safe(i, j, &tis_chips, list) {
820 chip = to_tpm_chip(i);
Rajiv Andrade253115b2008-10-11 09:04:39 +1100821 tpm_remove_hardware(chip->dev);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700822 iowrite32(~TPM_GLOBAL_INT_ENABLE &
823 ioread32(chip->vendor.iobase +
824 TPM_INT_ENABLE(chip->vendor.
825 locality)),
826 chip->vendor.iobase +
827 TPM_INT_ENABLE(chip->vendor.locality));
828 release_locality(chip, chip->vendor.locality, 1);
829 if (chip->vendor.irq)
830 free_irq(chip->vendor.irq, chip);
831 iounmap(i->iobase);
832 list_del(&i->list);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700833 }
834 spin_unlock(&tis_lock);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300835#ifdef CONFIG_PNP
836 if (!force) {
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700837 pnp_unregister_driver(&tis_pnp_driver);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300838 return;
839 }
840#endif
841 platform_device_unregister(pdev);
842 platform_driver_unregister(&tis_drv);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700843}
844
845module_init(init_tis);
846module_exit(cleanup_tis);
847MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
848MODULE_DESCRIPTION("TPM Driver");
849MODULE_VERSION("2.0");
850MODULE_LICENSE("GPL");