blob: 92f9f34e88f70ecf5508b0ceead3849277935195 [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
Randy Dunlap1560ffe62011-08-03 16:21:11 -070083#if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
Matthew Garrett3f0d3d02010-10-21 17:42:40 -040084static 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}
Randy Dunlap1560ffe62011-08-03 16:21:11 -070096#else
97static inline int is_itpm(struct pnp_dev *dev)
98{
99 return 0;
100}
Matthew Garrett3f0d3d02010-10-21 17:42:40 -0400101#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 Hallcb535422006-04-22 02:39:31 -0700196static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700197{
198 int size = 0, burstcnt;
199 while (size < count &&
Rajiv Andradefd048862011-09-16 14:39:40 -0300200 wait_for_tpm_stat(chip,
201 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
202 chip->vendor.timeout_c,
203 &chip->vendor.read_queue)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700204 == 0) {
205 burstcnt = get_burstcount(chip);
206 for (; burstcnt > 0 && size < count; burstcnt--)
207 buf[size++] = ioread8(chip->vendor.iobase +
208 TPM_DATA_FIFO(chip->vendor.
209 locality));
210 }
211 return size;
212}
213
Kylene Jo Hallcb535422006-04-22 02:39:31 -0700214static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700215{
216 int size = 0;
217 int expected, status;
218
219 if (count < TPM_HEADER_SIZE) {
220 size = -EIO;
221 goto out;
222 }
223
224 /* read first 10 bytes, including tag, paramsize, and result */
225 if ((size =
226 recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
227 dev_err(chip->dev, "Unable to read header\n");
228 goto out;
229 }
230
231 expected = be32_to_cpu(*(__be32 *) (buf + 2));
232 if (expected > count) {
233 size = -EIO;
234 goto out;
235 }
236
237 if ((size +=
238 recv_data(chip, &buf[TPM_HEADER_SIZE],
239 expected - TPM_HEADER_SIZE)) < expected) {
240 dev_err(chip->dev, "Unable to read remainder of result\n");
241 size = -ETIME;
242 goto out;
243 }
244
Rajiv Andradefd048862011-09-16 14:39:40 -0300245 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
246 &chip->vendor.int_queue);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700247 status = tpm_tis_status(chip);
248 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
249 dev_err(chip->dev, "Error left over data\n");
250 size = -EIO;
251 goto out;
252 }
253
254out:
255 tpm_tis_ready(chip);
256 release_locality(chip, chip->vendor.locality, 0);
257 return size;
258}
259
Rajiv Andrade3507d612009-09-10 17:09:35 -0300260static int itpm;
261module_param(itpm, bool, 0444);
262MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
263
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700264/*
265 * If interrupts are used (signaled by an irq set in the vendor structure)
266 * tpm.c can skip polling for the data to be available as the interrupt is
267 * waited for here
268 */
Stefan Berger9519de32011-03-30 12:13:33 -0400269static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700270{
271 int rc, status, burstcnt;
272 size_t count = 0;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700273
274 if (request_locality(chip, 0) < 0)
275 return -EBUSY;
276
277 status = tpm_tis_status(chip);
278 if ((status & TPM_STS_COMMAND_READY) == 0) {
279 tpm_tis_ready(chip);
Rajiv Andradefd048862011-09-16 14:39:40 -0300280 if (wait_for_tpm_stat
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700281 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
282 &chip->vendor.int_queue) < 0) {
283 rc = -ETIME;
284 goto out_err;
285 }
286 }
287
288 while (count < len - 1) {
289 burstcnt = get_burstcount(chip);
290 for (; burstcnt > 0 && count < len - 1; burstcnt--) {
291 iowrite8(buf[count], chip->vendor.iobase +
292 TPM_DATA_FIFO(chip->vendor.locality));
293 count++;
294 }
295
Rajiv Andradefd048862011-09-16 14:39:40 -0300296 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
297 &chip->vendor.int_queue);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700298 status = tpm_tis_status(chip);
Rajiv Andrade3507d612009-09-10 17:09:35 -0300299 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700300 rc = -EIO;
301 goto out_err;
302 }
303 }
304
305 /* write last byte */
306 iowrite8(buf[count],
Stefan Berger9519de32011-03-30 12:13:33 -0400307 chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
Rajiv Andradefd048862011-09-16 14:39:40 -0300308 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
309 &chip->vendor.int_queue);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700310 status = tpm_tis_status(chip);
311 if ((status & TPM_STS_DATA_EXPECT) != 0) {
312 rc = -EIO;
313 goto out_err;
314 }
315
Stefan Berger9519de32011-03-30 12:13:33 -0400316 return 0;
317
318out_err:
319 tpm_tis_ready(chip);
320 release_locality(chip, chip->vendor.locality, 0);
321 return rc;
322}
323
324/*
325 * If interrupts are used (signaled by an irq set in the vendor structure)
326 * tpm.c can skip polling for the data to be available as the interrupt is
327 * waited for here
328 */
329static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
330{
331 int rc;
332 u32 ordinal;
333
334 rc = tpm_tis_send_data(chip, buf, len);
335 if (rc < 0)
336 return rc;
337
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700338 /* go and do it */
339 iowrite8(TPM_STS_GO,
340 chip->vendor.iobase + TPM_STS(chip->vendor.locality));
341
342 if (chip->vendor.irq) {
343 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Rajiv Andradefd048862011-09-16 14:39:40 -0300344 if (wait_for_tpm_stat
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700345 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
346 tpm_calc_ordinal_duration(chip, ordinal),
347 &chip->vendor.read_queue) < 0) {
348 rc = -ETIME;
349 goto out_err;
350 }
351 }
352 return len;
353out_err:
354 tpm_tis_ready(chip);
355 release_locality(chip, chip->vendor.locality, 0);
356 return rc;
357}
358
Stefan Berger9519de32011-03-30 12:13:33 -0400359/*
360 * Early probing for iTPM with STS_DATA_EXPECT flaw.
361 * Try sending command without itpm flag set and if that
362 * fails, repeat with itpm flag set.
363 */
364static int probe_itpm(struct tpm_chip *chip)
365{
366 int rc = 0;
367 u8 cmd_getticks[] = {
368 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
369 0x00, 0x00, 0x00, 0xf1
370 };
371 size_t len = sizeof(cmd_getticks);
372 int rem_itpm = itpm;
373
374 itpm = 0;
375
376 rc = tpm_tis_send_data(chip, cmd_getticks, len);
377 if (rc == 0)
378 goto out;
379
380 tpm_tis_ready(chip);
381 release_locality(chip, chip->vendor.locality, 0);
382
383 itpm = 1;
384
385 rc = tpm_tis_send_data(chip, cmd_getticks, len);
386 if (rc == 0) {
387 dev_info(chip->dev, "Detected an iTPM.\n");
388 rc = 1;
389 } else
390 rc = -EFAULT;
391
392out:
393 itpm = rem_itpm;
394 tpm_tis_ready(chip);
Stefan Bergera927b812011-11-11 12:57:06 -0500395 /* some TPMs need a break here otherwise they will not work
396 * correctly on the immediately subsequent command */
397 msleep(chip->vendor.timeout_b);
Stefan Berger9519de32011-03-30 12:13:33 -0400398 release_locality(chip, chip->vendor.locality, 0);
399
400 return rc;
401}
402
Arjan van de Ven62322d22006-07-03 00:24:21 -0700403static const struct file_operations tis_ops = {
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700404 .owner = THIS_MODULE,
405 .llseek = no_llseek,
406 .open = tpm_open,
407 .read = tpm_read,
408 .write = tpm_write,
409 .release = tpm_release,
410};
411
412static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
413static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
414static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
415static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
416static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
417static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
418 NULL);
419static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
420static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
Stefan Berger04ab2292011-03-30 12:13:25 -0400421static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
Stefan Berger62592102011-03-30 12:13:28 -0400422static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700423
424static struct attribute *tis_attrs[] = {
425 &dev_attr_pubek.attr,
426 &dev_attr_pcrs.attr,
427 &dev_attr_enabled.attr,
428 &dev_attr_active.attr,
429 &dev_attr_owned.attr,
430 &dev_attr_temp_deactivated.attr,
431 &dev_attr_caps.attr,
Stefan Berger04ab2292011-03-30 12:13:25 -0400432 &dev_attr_cancel.attr,
Stefan Berger62592102011-03-30 12:13:28 -0400433 &dev_attr_durations.attr,
434 &dev_attr_timeouts.attr, NULL,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700435};
436
437static struct attribute_group tis_attr_grp = {
438 .attrs = tis_attrs
439};
440
441static struct tpm_vendor_specific tpm_tis = {
442 .status = tpm_tis_status,
443 .recv = tpm_tis_recv,
444 .send = tpm_tis_send,
445 .cancel = tpm_tis_ready,
446 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
447 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
448 .req_canceled = TPM_STS_COMMAND_READY,
449 .attr_group = &tis_attr_grp,
450 .miscdev = {
451 .fops = &tis_ops,},
452};
453
David Howells7d12e782006-10-05 14:55:46 +0100454static irqreturn_t tis_int_probe(int irq, void *dev_id)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700455{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400456 struct tpm_chip *chip = dev_id;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700457 u32 interrupt;
458
459 interrupt = ioread32(chip->vendor.iobase +
460 TPM_INT_STATUS(chip->vendor.locality));
461
462 if (interrupt == 0)
463 return IRQ_NONE;
464
Stefan Bergera7b66822011-03-30 12:13:32 -0400465 chip->vendor.probed_irq = irq;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700466
467 /* Clear interrupts handled with TPM_EOI */
468 iowrite32(interrupt,
469 chip->vendor.iobase +
470 TPM_INT_STATUS(chip->vendor.locality));
471 return IRQ_HANDLED;
472}
473
Jeff Garzika6f97b22007-10-31 05:20:49 -0400474static irqreturn_t tis_int_handler(int dummy, void *dev_id)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700475{
Jeff Garzik06efcad2007-10-19 03:10:11 -0400476 struct tpm_chip *chip = dev_id;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700477 u32 interrupt;
478 int i;
479
480 interrupt = ioread32(chip->vendor.iobase +
481 TPM_INT_STATUS(chip->vendor.locality));
482
483 if (interrupt == 0)
484 return IRQ_NONE;
485
486 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
487 wake_up_interruptible(&chip->vendor.read_queue);
488 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
489 for (i = 0; i < 5; i++)
490 if (check_locality(chip, i) >= 0)
491 break;
492 if (interrupt &
493 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
494 TPM_INTF_CMD_READY_INT))
495 wake_up_interruptible(&chip->vendor.int_queue);
496
497 /* Clear interrupts handled with TPM_EOI */
498 iowrite32(interrupt,
499 chip->vendor.iobase +
500 TPM_INT_STATUS(chip->vendor.locality));
Kylene Jo Hallcab091e2006-07-14 00:24:30 -0700501 ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700502 return IRQ_HANDLED;
503}
504
Kylene Jo Hall57135562006-04-22 02:39:44 -0700505static int interrupts = 1;
506module_param(interrupts, bool, 0444);
507MODULE_PARM_DESC(interrupts, "Enable interrupts");
508
Kylene Jo Hallc3c36aa2006-07-14 00:24:31 -0700509static int tpm_tis_init(struct device *dev, resource_size_t start,
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700510 resource_size_t len, unsigned int irq)
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700511{
512 u32 vendor, intfcaps, intmask;
Stefan Bergera7b66822011-03-30 12:13:32 -0400513 int rc, i, irq_s, irq_e;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700514 struct tpm_chip *chip;
515
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700516 if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700517 return -ENODEV;
518
519 chip->vendor.iobase = ioremap(start, len);
520 if (!chip->vendor.iobase) {
521 rc = -EIO;
522 goto out_err;
523 }
524
Jason Gunthorpeec579352009-09-09 17:22:18 -0600525 /* Default timeouts */
526 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
527 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
528 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
529 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
530
Marcel Selhorst05a462a2007-11-28 16:21:27 -0800531 if (request_locality(chip, 0) != 0) {
532 rc = -ENODEV;
533 goto out_err;
534 }
535
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700536 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700537
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700538 dev_info(dev,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700539 "1.2 TPM (device-id 0x%X, rev-id %d)\n",
540 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
541
Stefan Berger9519de32011-03-30 12:13:33 -0400542 if (!itpm) {
543 itpm = probe_itpm(chip);
544 if (itpm < 0) {
545 rc = -ENODEV;
546 goto out_err;
547 }
548 }
549
Rajiv Andrade3507d612009-09-10 17:09:35 -0300550 if (itpm)
551 dev_info(dev, "Intel iTPM workaround enabled\n");
552
553
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700554 /* Figure out the capabilities */
555 intfcaps =
556 ioread32(chip->vendor.iobase +
557 TPM_INTF_CAPS(chip->vendor.locality));
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700558 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700559 intfcaps);
560 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700561 dev_dbg(dev, "\tBurst Count Static\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700562 if (intfcaps & TPM_INTF_CMD_READY_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700563 dev_dbg(dev, "\tCommand Ready Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700564 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700565 dev_dbg(dev, "\tInterrupt Edge Falling\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700566 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700567 dev_dbg(dev, "\tInterrupt Edge Rising\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700568 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700569 dev_dbg(dev, "\tInterrupt Level Low\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700570 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700571 dev_dbg(dev, "\tInterrupt Level High\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700572 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700573 dev_dbg(dev, "\tLocality Change Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700574 if (intfcaps & TPM_INTF_STS_VALID_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700575 dev_dbg(dev, "\tSts Valid Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700576 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700577 dev_dbg(dev, "\tData Avail Int Support\n");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700578
Stefan Bergera7b66822011-03-30 12:13:32 -0400579 /* get the timeouts before testing for irqs */
Stefan Berger7f326ed2011-11-11 12:57:05 -0500580 if (tpm_get_timeouts(chip)) {
581 dev_err(dev, "Could not get TPM timeouts and durations\n");
582 rc = -ENODEV;
583 goto out_err;
584 }
Stefan Bergera7b66822011-03-30 12:13:32 -0400585
Stefan Berger68d6e672011-11-11 12:57:04 -0500586 if (tpm_do_selftest(chip)) {
587 dev_err(dev, "TPM self test failed\n");
588 rc = -ENODEV;
589 goto out_err;
590 }
591
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700592 /* INTERRUPT Setup */
593 init_waitqueue_head(&chip->vendor.read_queue);
594 init_waitqueue_head(&chip->vendor.int_queue);
595
596 intmask =
597 ioread32(chip->vendor.iobase +
598 TPM_INT_ENABLE(chip->vendor.locality));
599
600 intmask |= TPM_INTF_CMD_READY_INT
601 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
602 | TPM_INTF_STS_VALID_INT;
603
604 iowrite32(intmask,
605 chip->vendor.iobase +
606 TPM_INT_ENABLE(chip->vendor.locality));
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700607 if (interrupts)
608 chip->vendor.irq = irq;
609 if (interrupts && !chip->vendor.irq) {
Stefan Bergera7b66822011-03-30 12:13:32 -0400610 irq_s =
Kylene Jo Hall57135562006-04-22 02:39:44 -0700611 ioread8(chip->vendor.iobase +
612 TPM_INT_VECTOR(chip->vendor.locality));
Stefan Bergera7b66822011-03-30 12:13:32 -0400613 if (irq_s) {
614 irq_e = irq_s;
615 } else {
616 irq_s = 3;
617 irq_e = 15;
618 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700619
Stefan Bergera7b66822011-03-30 12:13:32 -0400620 for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) {
Kylene Jo Hall57135562006-04-22 02:39:44 -0700621 iowrite8(i, chip->vendor.iobase +
Stefan Bergera7b66822011-03-30 12:13:32 -0400622 TPM_INT_VECTOR(chip->vendor.locality));
Kylene Jo Hall57135562006-04-22 02:39:44 -0700623 if (request_irq
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700624 (i, tis_int_probe, IRQF_SHARED,
Kylene Jo Hall57135562006-04-22 02:39:44 -0700625 chip->vendor.miscdev.name, chip) != 0) {
626 dev_info(chip->dev,
627 "Unable to request irq: %d for probe\n",
628 i);
629 continue;
630 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700631
Kylene Jo Hall57135562006-04-22 02:39:44 -0700632 /* Clear all existing */
633 iowrite32(ioread32
634 (chip->vendor.iobase +
635 TPM_INT_STATUS(chip->vendor.locality)),
636 chip->vendor.iobase +
637 TPM_INT_STATUS(chip->vendor.locality));
638
639 /* Turn on */
640 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
641 chip->vendor.iobase +
642 TPM_INT_ENABLE(chip->vendor.locality));
643
Stefan Bergera7b66822011-03-30 12:13:32 -0400644 chip->vendor.probed_irq = 0;
645
Kylene Jo Hall57135562006-04-22 02:39:44 -0700646 /* Generate Interrupts */
647 tpm_gen_interrupt(chip);
648
Stefan Bergera7b66822011-03-30 12:13:32 -0400649 chip->vendor.irq = chip->vendor.probed_irq;
650
651 /* free_irq will call into tis_int_probe;
652 clear all irqs we haven't seen while doing
653 tpm_gen_interrupt */
654 iowrite32(ioread32
655 (chip->vendor.iobase +
656 TPM_INT_STATUS(chip->vendor.locality)),
657 chip->vendor.iobase +
658 TPM_INT_STATUS(chip->vendor.locality));
659
Kylene Jo Hall57135562006-04-22 02:39:44 -0700660 /* Turn off */
661 iowrite32(intmask,
662 chip->vendor.iobase +
663 TPM_INT_ENABLE(chip->vendor.locality));
664 free_irq(i, chip);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700665 }
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700666 }
667 if (chip->vendor.irq) {
668 iowrite8(chip->vendor.irq,
669 chip->vendor.iobase +
670 TPM_INT_VECTOR(chip->vendor.locality));
671 if (request_irq
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700672 (chip->vendor.irq, tis_int_handler, IRQF_SHARED,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700673 chip->vendor.miscdev.name, chip) != 0) {
674 dev_info(chip->dev,
Kylene Jo Hall57135562006-04-22 02:39:44 -0700675 "Unable to request irq: %d for use\n",
676 chip->vendor.irq);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700677 chip->vendor.irq = 0;
678 } else {
679 /* Clear all existing */
680 iowrite32(ioread32
681 (chip->vendor.iobase +
682 TPM_INT_STATUS(chip->vendor.locality)),
683 chip->vendor.iobase +
684 TPM_INT_STATUS(chip->vendor.locality));
685
686 /* Turn on */
687 iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
688 chip->vendor.iobase +
689 TPM_INT_ENABLE(chip->vendor.locality));
690 }
691 }
692
693 INIT_LIST_HEAD(&chip->vendor.list);
694 spin_lock(&tis_lock);
695 list_add(&chip->vendor.list, &tis_chips);
696 spin_unlock(&tis_lock);
697
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700698
699 return 0;
700out_err:
701 if (chip->vendor.iobase)
702 iounmap(chip->vendor.iobase);
703 tpm_remove_hardware(chip->dev);
704 return rc;
705}
Stefan Berger96854312011-07-17 01:04:24 -0400706
707static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
708{
709 u32 intmask;
710
711 /* reenable interrupts that device may have lost or
712 BIOS/firmware may have disabled */
713 iowrite8(chip->vendor.irq, chip->vendor.iobase +
714 TPM_INT_VECTOR(chip->vendor.locality));
715
716 intmask =
717 ioread32(chip->vendor.iobase +
718 TPM_INT_ENABLE(chip->vendor.locality));
719
720 intmask |= TPM_INTF_CMD_READY_INT
721 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
722 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
723
724 iowrite32(intmask,
725 chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
726}
727
728
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300729#ifdef CONFIG_PNP
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700730static int __devinit tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
731 const struct pnp_device_id *pnp_id)
732{
Kylene Jo Hallc3c36aa2006-07-14 00:24:31 -0700733 resource_size_t start, len;
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700734 unsigned int irq = 0;
735
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700736 start = pnp_mem_start(pnp_dev, 0);
737 len = pnp_mem_len(pnp_dev, 0);
738
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700739 if (pnp_irq_valid(pnp_dev, 0))
740 irq = pnp_irq(pnp_dev, 0);
741 else
742 interrupts = 0;
743
Olof Johanssone5cce6c2011-01-06 21:24:01 -0600744 if (is_itpm(pnp_dev))
745 itpm = 1;
746
Bjorn Helgaas7917ff92007-10-16 23:26:46 -0700747 return tpm_tis_init(&pnp_dev->dev, start, len, irq);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700748}
749
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700750static int tpm_tis_pnp_suspend(struct pnp_dev *dev, pm_message_t msg)
751{
752 return tpm_pm_suspend(&dev->dev, msg);
753}
754
755static int tpm_tis_pnp_resume(struct pnp_dev *dev)
756{
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700757 struct tpm_chip *chip = pnp_get_drvdata(dev);
758 int ret;
759
Stefan Berger45baa1d2011-03-30 12:13:30 -0400760 if (chip->vendor.irq)
761 tpm_tis_reenable_interrupts(chip);
762
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700763 ret = tpm_pm_resume(&dev->dev);
764 if (!ret)
Stefan Berger68d6e672011-11-11 12:57:04 -0500765 tpm_do_selftest(chip);
Rajiv Andrade59f6fbe2010-06-23 12:18:56 -0700766
767 return ret;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700768}
769
770static struct pnp_device_id tpm_pnp_tbl[] __devinitdata = {
771 {"PNP0C31", 0}, /* TPM */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700772 {"ATM1200", 0}, /* Atmel */
773 {"IFX0102", 0}, /* Infineon */
774 {"BCM0101", 0}, /* Broadcom */
LE DISEZ Erwan061991e2008-07-25 19:44:56 -0700775 {"BCM0102", 0}, /* Broadcom */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700776 {"NSC1200", 0}, /* National */
Marcin Obarafb0e7e12008-07-10 17:30:42 -0700777 {"ICO0102", 0}, /* Intel */
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700778 /* Add new here */
779 {"", 0}, /* User Specified */
780 {"", 0} /* Terminator */
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700781};
Matt Domsch31bde712009-11-03 12:05:50 +1100782MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700783
Rajiv Andrade253115b2008-10-11 09:04:39 +1100784static __devexit void tpm_tis_pnp_remove(struct pnp_dev *dev)
785{
786 struct tpm_chip *chip = pnp_get_drvdata(dev);
787
788 tpm_dev_vendor_release(chip);
789
790 kfree(chip);
791}
792
793
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700794static struct pnp_driver tis_pnp_driver = {
795 .name = "tpm_tis",
796 .id_table = tpm_pnp_tbl,
797 .probe = tpm_tis_pnp_init,
798 .suspend = tpm_tis_pnp_suspend,
799 .resume = tpm_tis_pnp_resume,
Rajiv Andrade253115b2008-10-11 09:04:39 +1100800 .remove = tpm_tis_pnp_remove,
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700801};
802
Kylene Jo Hall93e1b7d2006-04-22 02:39:52 -0700803#define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
804module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
805 sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
806MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300807#endif
Ming Lei7a192ec2009-02-06 23:40:12 +0800808static int tpm_tis_suspend(struct platform_device *dev, pm_message_t msg)
809{
810 return tpm_pm_suspend(&dev->dev, msg);
811}
812
813static int tpm_tis_resume(struct platform_device *dev)
814{
Stefan Berger45baa1d2011-03-30 12:13:30 -0400815 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
816
817 if (chip->vendor.irq)
818 tpm_tis_reenable_interrupts(chip);
819
Ming Lei7a192ec2009-02-06 23:40:12 +0800820 return tpm_pm_resume(&dev->dev);
821}
822static struct platform_driver tis_drv = {
823 .driver = {
824 .name = "tpm_tis",
825 .owner = THIS_MODULE,
826 },
827 .suspend = tpm_tis_suspend,
828 .resume = tpm_tis_resume,
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700829};
830
831static struct platform_device *pdev;
832
833static int force;
834module_param(force, bool, 0444);
835MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700836static int __init init_tis(void)
837{
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700838 int rc;
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300839#ifdef CONFIG_PNP
840 if (!force)
841 return pnp_register_driver(&tis_pnp_driver);
842#endif
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700843
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300844 rc = platform_driver_register(&tis_drv);
845 if (rc < 0)
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700846 return rc;
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300847 if (IS_ERR(pdev=platform_device_register_simple("tpm_tis", -1, NULL, 0)))
848 return PTR_ERR(pdev);
849 if((rc=tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0)) != 0) {
850 platform_device_unregister(pdev);
851 platform_driver_unregister(&tis_drv);
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700852 }
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300853 return rc;
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700854}
855
856static void __exit cleanup_tis(void)
857{
858 struct tpm_vendor_specific *i, *j;
859 struct tpm_chip *chip;
860 spin_lock(&tis_lock);
861 list_for_each_entry_safe(i, j, &tis_chips, list) {
862 chip = to_tpm_chip(i);
Rajiv Andrade253115b2008-10-11 09:04:39 +1100863 tpm_remove_hardware(chip->dev);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700864 iowrite32(~TPM_GLOBAL_INT_ENABLE &
865 ioread32(chip->vendor.iobase +
866 TPM_INT_ENABLE(chip->vendor.
867 locality)),
868 chip->vendor.iobase +
869 TPM_INT_ENABLE(chip->vendor.locality));
870 release_locality(chip, chip->vendor.locality, 1);
871 if (chip->vendor.irq)
872 free_irq(chip->vendor.irq, chip);
873 iounmap(i->iobase);
874 list_del(&i->list);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700875 }
876 spin_unlock(&tis_lock);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300877#ifdef CONFIG_PNP
878 if (!force) {
Kylene Jo Hall9e323d32006-07-14 00:24:31 -0700879 pnp_unregister_driver(&tis_pnp_driver);
Rajiv Andrade7f2ab002010-05-13 17:37:54 -0300880 return;
881 }
882#endif
883 platform_device_unregister(pdev);
884 platform_driver_unregister(&tis_drv);
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700885}
886
887module_init(init_tis);
888module_exit(cleanup_tis);
889MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
890MODULE_DESCRIPTION("TPM Driver");
891MODULE_VERSION("2.0");
892MODULE_LICENSE("GPL");