Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005, 2006 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 6 | * Kylene Hall <kjhall@us.ibm.com> |
| 7 | * |
Kent Yoder | 8e81cc1 | 2007-08-22 14:01:04 -0700 | [diff] [blame] | 8 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
| 9 | * |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 10 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 11 | * Specifications at www.trustedcomputinggroup.org |
| 12 | * |
| 13 | * This device driver implements the TPM interface as defined in |
| 14 | * the TCG TPM Interface Spec version 1.2, revision 1.0. |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License as |
| 18 | * published by the Free Software Foundation, version 2 of the |
| 19 | * License. |
| 20 | */ |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 24 | #include <linux/pnp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/wait.h> |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 28 | #include <linux/acpi.h> |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 29 | #include <linux/freezer.h> |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 30 | #include "tpm.h" |
| 31 | |
| 32 | #define TPM_HEADER_SIZE 10 |
| 33 | |
| 34 | enum 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 | |
| 41 | enum 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 | |
| 49 | enum 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 Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 62 | enum tis_defaults { |
Kylene Jo Hall | 2a7362f | 2006-05-15 09:44:25 -0700 | [diff] [blame] | 63 | TIS_MEM_BASE = 0xFED40000, |
Kylene Jo Hall | b09d530 | 2006-04-22 02:38:55 -0700 | [diff] [blame] | 64 | TIS_MEM_LEN = 0x5000, |
Kylene Jo Hall | cb53542 | 2006-04-22 02:39:31 -0700 | [diff] [blame] | 65 | TIS_SHORT_TIMEOUT = 750, /* ms */ |
| 66 | TIS_LONG_TIMEOUT = 2000, /* 2 sec */ |
Kylene Jo Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 69 | #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 | |
| 80 | static LIST_HEAD(tis_chips); |
| 81 | static DEFINE_SPINLOCK(tis_lock); |
| 82 | |
Randy Dunlap | 1560ffe6 | 2011-08-03 16:21:11 -0700 | [diff] [blame] | 83 | #if defined(CONFIG_PNP) && defined(CONFIG_ACPI) |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 84 | static 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 Dunlap | 1560ffe6 | 2011-08-03 16:21:11 -0700 | [diff] [blame] | 96 | #else |
| 97 | static inline int is_itpm(struct pnp_dev *dev) |
| 98 | { |
| 99 | return 0; |
| 100 | } |
Matthew Garrett | 3f0d3d0 | 2010-10-21 17:42:40 -0400 | [diff] [blame] | 101 | #endif |
| 102 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 103 | static 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 | |
| 113 | static 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 | |
| 122 | static int request_locality(struct tpm_chip *chip, int l) |
| 123 | { |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 124 | unsigned long stop, timeout; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 125 | 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 Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 133 | stop = jiffies + chip->vendor.timeout_a; |
| 134 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 135 | if (chip->vendor.irq) { |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 136 | again: |
| 137 | timeout = stop - jiffies; |
| 138 | if ((long)timeout <= 0) |
| 139 | return -1; |
Kylene Jo Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 140 | rc = wait_event_interruptible_timeout(chip->vendor.int_queue, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 141 | (check_locality |
| 142 | (chip, l) >= 0), |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 143 | timeout); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 144 | if (rc > 0) |
| 145 | return l; |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 146 | if (rc == -ERESTARTSYS && freezing(current)) { |
| 147 | clear_thread_flag(TIF_SIGPENDING); |
| 148 | goto again; |
| 149 | } |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 150 | } else { |
| 151 | /* wait for burstcount */ |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 152 | 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 | |
| 162 | static u8 tpm_tis_status(struct tpm_chip *chip) |
| 163 | { |
| 164 | return ioread8(chip->vendor.iobase + |
| 165 | TPM_STS(chip->vendor.locality)); |
| 166 | } |
| 167 | |
| 168 | static 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 | |
| 175 | static 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 Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 182 | stop = jiffies + chip->vendor.timeout_d; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 183 | 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 Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 196 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 197 | 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 Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 208 | stop = jiffies + timeout; |
| 209 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 210 | if (chip->vendor.irq) { |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 211 | again: |
| 212 | timeout = stop - jiffies; |
| 213 | if ((long)timeout <= 0) |
| 214 | return -ETIME; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 215 | rc = wait_event_interruptible_timeout(*queue, |
| 216 | ((tpm_tis_status |
| 217 | (chip) & mask) == |
Kylene Jo Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 218 | mask), timeout); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 219 | if (rc > 0) |
| 220 | return 0; |
Stefan Berger | 20b87bb | 2011-03-30 12:13:31 -0400 | [diff] [blame] | 221 | if (rc == -ERESTARTSYS && freezing(current)) { |
| 222 | clear_thread_flag(TIF_SIGPENDING); |
| 223 | goto again; |
| 224 | } |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 225 | } else { |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 226 | 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 Hall | cb53542 | 2006-04-22 02:39:31 -0700 | [diff] [blame] | 236 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 237 | { |
| 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 Hall | cb53542 | 2006-04-22 02:39:31 -0700 | [diff] [blame] | 254 | static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 255 | { |
| 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 | |
| 294 | out: |
| 295 | tpm_tis_ready(chip); |
| 296 | release_locality(chip, chip->vendor.locality, 0); |
| 297 | return size; |
| 298 | } |
| 299 | |
Rajiv Andrade | 3507d61 | 2009-09-10 17:09:35 -0300 | [diff] [blame] | 300 | static int itpm; |
| 301 | module_param(itpm, bool, 0444); |
| 302 | MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)"); |
| 303 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 304 | /* |
| 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 | */ |
Stefan Berger | 9519de3 | 2011-03-30 12:13:33 -0400 | [diff] [blame] | 309 | static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 310 | { |
| 311 | int rc, status, burstcnt; |
| 312 | size_t count = 0; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 313 | |
| 314 | if (request_locality(chip, 0) < 0) |
| 315 | return -EBUSY; |
| 316 | |
| 317 | status = tpm_tis_status(chip); |
| 318 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 319 | tpm_tis_ready(chip); |
| 320 | if (wait_for_stat |
| 321 | (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b, |
| 322 | &chip->vendor.int_queue) < 0) { |
| 323 | rc = -ETIME; |
| 324 | goto out_err; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | while (count < len - 1) { |
| 329 | burstcnt = get_burstcount(chip); |
| 330 | for (; burstcnt > 0 && count < len - 1; burstcnt--) { |
| 331 | iowrite8(buf[count], chip->vendor.iobase + |
| 332 | TPM_DATA_FIFO(chip->vendor.locality)); |
| 333 | count++; |
| 334 | } |
| 335 | |
| 336 | wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, |
| 337 | &chip->vendor.int_queue); |
| 338 | status = tpm_tis_status(chip); |
Rajiv Andrade | 3507d61 | 2009-09-10 17:09:35 -0300 | [diff] [blame] | 339 | if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) { |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 340 | rc = -EIO; |
| 341 | goto out_err; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* write last byte */ |
| 346 | iowrite8(buf[count], |
Stefan Berger | 9519de3 | 2011-03-30 12:13:33 -0400 | [diff] [blame] | 347 | chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality)); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 348 | wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, |
| 349 | &chip->vendor.int_queue); |
| 350 | status = tpm_tis_status(chip); |
| 351 | if ((status & TPM_STS_DATA_EXPECT) != 0) { |
| 352 | rc = -EIO; |
| 353 | goto out_err; |
| 354 | } |
| 355 | |
Stefan Berger | 9519de3 | 2011-03-30 12:13:33 -0400 | [diff] [blame] | 356 | return 0; |
| 357 | |
| 358 | out_err: |
| 359 | tpm_tis_ready(chip); |
| 360 | release_locality(chip, chip->vendor.locality, 0); |
| 361 | return rc; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * If interrupts are used (signaled by an irq set in the vendor structure) |
| 366 | * tpm.c can skip polling for the data to be available as the interrupt is |
| 367 | * waited for here |
| 368 | */ |
| 369 | static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 370 | { |
| 371 | int rc; |
| 372 | u32 ordinal; |
| 373 | |
| 374 | rc = tpm_tis_send_data(chip, buf, len); |
| 375 | if (rc < 0) |
| 376 | return rc; |
| 377 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 378 | /* go and do it */ |
| 379 | iowrite8(TPM_STS_GO, |
| 380 | chip->vendor.iobase + TPM_STS(chip->vendor.locality)); |
| 381 | |
| 382 | if (chip->vendor.irq) { |
| 383 | ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); |
| 384 | if (wait_for_stat |
| 385 | (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 386 | tpm_calc_ordinal_duration(chip, ordinal), |
| 387 | &chip->vendor.read_queue) < 0) { |
| 388 | rc = -ETIME; |
| 389 | goto out_err; |
| 390 | } |
| 391 | } |
| 392 | return len; |
| 393 | out_err: |
| 394 | tpm_tis_ready(chip); |
| 395 | release_locality(chip, chip->vendor.locality, 0); |
| 396 | return rc; |
| 397 | } |
| 398 | |
Stefan Berger | 9519de3 | 2011-03-30 12:13:33 -0400 | [diff] [blame] | 399 | /* |
| 400 | * Early probing for iTPM with STS_DATA_EXPECT flaw. |
| 401 | * Try sending command without itpm flag set and if that |
| 402 | * fails, repeat with itpm flag set. |
| 403 | */ |
| 404 | static int probe_itpm(struct tpm_chip *chip) |
| 405 | { |
| 406 | int rc = 0; |
| 407 | u8 cmd_getticks[] = { |
| 408 | 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, |
| 409 | 0x00, 0x00, 0x00, 0xf1 |
| 410 | }; |
| 411 | size_t len = sizeof(cmd_getticks); |
| 412 | int rem_itpm = itpm; |
| 413 | |
| 414 | itpm = 0; |
| 415 | |
| 416 | rc = tpm_tis_send_data(chip, cmd_getticks, len); |
| 417 | if (rc == 0) |
| 418 | goto out; |
| 419 | |
| 420 | tpm_tis_ready(chip); |
| 421 | release_locality(chip, chip->vendor.locality, 0); |
| 422 | |
| 423 | itpm = 1; |
| 424 | |
| 425 | rc = tpm_tis_send_data(chip, cmd_getticks, len); |
| 426 | if (rc == 0) { |
| 427 | dev_info(chip->dev, "Detected an iTPM.\n"); |
| 428 | rc = 1; |
| 429 | } else |
| 430 | rc = -EFAULT; |
| 431 | |
| 432 | out: |
| 433 | itpm = rem_itpm; |
| 434 | tpm_tis_ready(chip); |
| 435 | release_locality(chip, chip->vendor.locality, 0); |
| 436 | |
| 437 | return rc; |
| 438 | } |
| 439 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 440 | static const struct file_operations tis_ops = { |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 441 | .owner = THIS_MODULE, |
| 442 | .llseek = no_llseek, |
| 443 | .open = tpm_open, |
| 444 | .read = tpm_read, |
| 445 | .write = tpm_write, |
| 446 | .release = tpm_release, |
| 447 | }; |
| 448 | |
| 449 | static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); |
| 450 | static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); |
| 451 | static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); |
| 452 | static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); |
| 453 | static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); |
| 454 | static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, |
| 455 | NULL); |
| 456 | static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); |
| 457 | static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); |
Stefan Berger | 04ab229 | 2011-03-30 12:13:25 -0400 | [diff] [blame] | 458 | static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); |
Stefan Berger | 6259210 | 2011-03-30 12:13:28 -0400 | [diff] [blame] | 459 | static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 460 | |
| 461 | static struct attribute *tis_attrs[] = { |
| 462 | &dev_attr_pubek.attr, |
| 463 | &dev_attr_pcrs.attr, |
| 464 | &dev_attr_enabled.attr, |
| 465 | &dev_attr_active.attr, |
| 466 | &dev_attr_owned.attr, |
| 467 | &dev_attr_temp_deactivated.attr, |
| 468 | &dev_attr_caps.attr, |
Stefan Berger | 04ab229 | 2011-03-30 12:13:25 -0400 | [diff] [blame] | 469 | &dev_attr_cancel.attr, |
Stefan Berger | 6259210 | 2011-03-30 12:13:28 -0400 | [diff] [blame] | 470 | &dev_attr_durations.attr, |
| 471 | &dev_attr_timeouts.attr, NULL, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | static struct attribute_group tis_attr_grp = { |
| 475 | .attrs = tis_attrs |
| 476 | }; |
| 477 | |
| 478 | static struct tpm_vendor_specific tpm_tis = { |
| 479 | .status = tpm_tis_status, |
| 480 | .recv = tpm_tis_recv, |
| 481 | .send = tpm_tis_send, |
| 482 | .cancel = tpm_tis_ready, |
| 483 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 484 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 485 | .req_canceled = TPM_STS_COMMAND_READY, |
| 486 | .attr_group = &tis_attr_grp, |
| 487 | .miscdev = { |
| 488 | .fops = &tis_ops,}, |
| 489 | }; |
| 490 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 491 | static irqreturn_t tis_int_probe(int irq, void *dev_id) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 492 | { |
Jeff Garzik | 06efcad | 2007-10-19 03:10:11 -0400 | [diff] [blame] | 493 | struct tpm_chip *chip = dev_id; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 494 | u32 interrupt; |
| 495 | |
| 496 | interrupt = ioread32(chip->vendor.iobase + |
| 497 | TPM_INT_STATUS(chip->vendor.locality)); |
| 498 | |
| 499 | if (interrupt == 0) |
| 500 | return IRQ_NONE; |
| 501 | |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 502 | chip->vendor.probed_irq = irq; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 503 | |
| 504 | /* Clear interrupts handled with TPM_EOI */ |
| 505 | iowrite32(interrupt, |
| 506 | chip->vendor.iobase + |
| 507 | TPM_INT_STATUS(chip->vendor.locality)); |
| 508 | return IRQ_HANDLED; |
| 509 | } |
| 510 | |
Jeff Garzik | a6f97b2 | 2007-10-31 05:20:49 -0400 | [diff] [blame] | 511 | static irqreturn_t tis_int_handler(int dummy, void *dev_id) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 512 | { |
Jeff Garzik | 06efcad | 2007-10-19 03:10:11 -0400 | [diff] [blame] | 513 | struct tpm_chip *chip = dev_id; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 514 | u32 interrupt; |
| 515 | int i; |
| 516 | |
| 517 | interrupt = ioread32(chip->vendor.iobase + |
| 518 | TPM_INT_STATUS(chip->vendor.locality)); |
| 519 | |
| 520 | if (interrupt == 0) |
| 521 | return IRQ_NONE; |
| 522 | |
| 523 | if (interrupt & TPM_INTF_DATA_AVAIL_INT) |
| 524 | wake_up_interruptible(&chip->vendor.read_queue); |
| 525 | if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT) |
| 526 | for (i = 0; i < 5; i++) |
| 527 | if (check_locality(chip, i) >= 0) |
| 528 | break; |
| 529 | if (interrupt & |
| 530 | (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT | |
| 531 | TPM_INTF_CMD_READY_INT)) |
| 532 | wake_up_interruptible(&chip->vendor.int_queue); |
| 533 | |
| 534 | /* Clear interrupts handled with TPM_EOI */ |
| 535 | iowrite32(interrupt, |
| 536 | chip->vendor.iobase + |
| 537 | TPM_INT_STATUS(chip->vendor.locality)); |
Kylene Jo Hall | cab091e | 2006-07-14 00:24:30 -0700 | [diff] [blame] | 538 | ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality)); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 539 | return IRQ_HANDLED; |
| 540 | } |
| 541 | |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 542 | static int interrupts = 1; |
| 543 | module_param(interrupts, bool, 0444); |
| 544 | MODULE_PARM_DESC(interrupts, "Enable interrupts"); |
| 545 | |
Kylene Jo Hall | c3c36aa | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 546 | static int tpm_tis_init(struct device *dev, resource_size_t start, |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 547 | resource_size_t len, unsigned int irq) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 548 | { |
| 549 | u32 vendor, intfcaps, intmask; |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 550 | int rc, i, irq_s, irq_e; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 551 | struct tpm_chip *chip; |
| 552 | |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 553 | if (!(chip = tpm_register_hardware(dev, &tpm_tis))) |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 554 | return -ENODEV; |
| 555 | |
| 556 | chip->vendor.iobase = ioremap(start, len); |
| 557 | if (!chip->vendor.iobase) { |
| 558 | rc = -EIO; |
| 559 | goto out_err; |
| 560 | } |
| 561 | |
Jason Gunthorpe | ec57935 | 2009-09-09 17:22:18 -0600 | [diff] [blame] | 562 | /* Default timeouts */ |
| 563 | chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 564 | chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); |
| 565 | chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 566 | chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 567 | |
Marcel Selhorst | 05a462a | 2007-11-28 16:21:27 -0800 | [diff] [blame] | 568 | if (request_locality(chip, 0) != 0) { |
| 569 | rc = -ENODEV; |
| 570 | goto out_err; |
| 571 | } |
| 572 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 573 | vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0)); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 574 | |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 575 | dev_info(dev, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 576 | "1.2 TPM (device-id 0x%X, rev-id %d)\n", |
| 577 | vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0))); |
| 578 | |
Stefan Berger | 9519de3 | 2011-03-30 12:13:33 -0400 | [diff] [blame] | 579 | if (!itpm) { |
| 580 | itpm = probe_itpm(chip); |
| 581 | if (itpm < 0) { |
| 582 | rc = -ENODEV; |
| 583 | goto out_err; |
| 584 | } |
| 585 | } |
| 586 | |
Rajiv Andrade | 3507d61 | 2009-09-10 17:09:35 -0300 | [diff] [blame] | 587 | if (itpm) |
| 588 | dev_info(dev, "Intel iTPM workaround enabled\n"); |
| 589 | |
| 590 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 591 | /* Figure out the capabilities */ |
| 592 | intfcaps = |
| 593 | ioread32(chip->vendor.iobase + |
| 594 | TPM_INTF_CAPS(chip->vendor.locality)); |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 595 | dev_dbg(dev, "TPM interface capabilities (0x%x):\n", |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 596 | intfcaps); |
| 597 | if (intfcaps & TPM_INTF_BURST_COUNT_STATIC) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 598 | dev_dbg(dev, "\tBurst Count Static\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 599 | if (intfcaps & TPM_INTF_CMD_READY_INT) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 600 | dev_dbg(dev, "\tCommand Ready Int Support\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 601 | if (intfcaps & TPM_INTF_INT_EDGE_FALLING) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 602 | dev_dbg(dev, "\tInterrupt Edge Falling\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 603 | if (intfcaps & TPM_INTF_INT_EDGE_RISING) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 604 | dev_dbg(dev, "\tInterrupt Edge Rising\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 605 | if (intfcaps & TPM_INTF_INT_LEVEL_LOW) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 606 | dev_dbg(dev, "\tInterrupt Level Low\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 607 | if (intfcaps & TPM_INTF_INT_LEVEL_HIGH) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 608 | dev_dbg(dev, "\tInterrupt Level High\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 609 | if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 610 | dev_dbg(dev, "\tLocality Change Int Support\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 611 | if (intfcaps & TPM_INTF_STS_VALID_INT) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 612 | dev_dbg(dev, "\tSts Valid Int Support\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 613 | if (intfcaps & TPM_INTF_DATA_AVAIL_INT) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 614 | dev_dbg(dev, "\tData Avail Int Support\n"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 615 | |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 616 | /* get the timeouts before testing for irqs */ |
Stefan Berger | 7f326ed | 2011-11-11 12:57:05 -0500 | [diff] [blame^] | 617 | if (tpm_get_timeouts(chip)) { |
| 618 | dev_err(dev, "Could not get TPM timeouts and durations\n"); |
| 619 | rc = -ENODEV; |
| 620 | goto out_err; |
| 621 | } |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 622 | |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 623 | if (tpm_do_selftest(chip)) { |
| 624 | dev_err(dev, "TPM self test failed\n"); |
| 625 | rc = -ENODEV; |
| 626 | goto out_err; |
| 627 | } |
| 628 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 629 | /* INTERRUPT Setup */ |
| 630 | init_waitqueue_head(&chip->vendor.read_queue); |
| 631 | init_waitqueue_head(&chip->vendor.int_queue); |
| 632 | |
| 633 | intmask = |
| 634 | ioread32(chip->vendor.iobase + |
| 635 | TPM_INT_ENABLE(chip->vendor.locality)); |
| 636 | |
| 637 | intmask |= TPM_INTF_CMD_READY_INT |
| 638 | | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT |
| 639 | | TPM_INTF_STS_VALID_INT; |
| 640 | |
| 641 | iowrite32(intmask, |
| 642 | chip->vendor.iobase + |
| 643 | TPM_INT_ENABLE(chip->vendor.locality)); |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 644 | if (interrupts) |
| 645 | chip->vendor.irq = irq; |
| 646 | if (interrupts && !chip->vendor.irq) { |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 647 | irq_s = |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 648 | ioread8(chip->vendor.iobase + |
| 649 | TPM_INT_VECTOR(chip->vendor.locality)); |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 650 | if (irq_s) { |
| 651 | irq_e = irq_s; |
| 652 | } else { |
| 653 | irq_s = 3; |
| 654 | irq_e = 15; |
| 655 | } |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 656 | |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 657 | for (i = irq_s; i <= irq_e && chip->vendor.irq == 0; i++) { |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 658 | iowrite8(i, chip->vendor.iobase + |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 659 | TPM_INT_VECTOR(chip->vendor.locality)); |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 660 | if (request_irq |
Thomas Gleixner | 0f2ed4c | 2006-07-01 19:29:33 -0700 | [diff] [blame] | 661 | (i, tis_int_probe, IRQF_SHARED, |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 662 | chip->vendor.miscdev.name, chip) != 0) { |
| 663 | dev_info(chip->dev, |
| 664 | "Unable to request irq: %d for probe\n", |
| 665 | i); |
| 666 | continue; |
| 667 | } |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 668 | |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 669 | /* Clear all existing */ |
| 670 | iowrite32(ioread32 |
| 671 | (chip->vendor.iobase + |
| 672 | TPM_INT_STATUS(chip->vendor.locality)), |
| 673 | chip->vendor.iobase + |
| 674 | TPM_INT_STATUS(chip->vendor.locality)); |
| 675 | |
| 676 | /* Turn on */ |
| 677 | iowrite32(intmask | TPM_GLOBAL_INT_ENABLE, |
| 678 | chip->vendor.iobase + |
| 679 | TPM_INT_ENABLE(chip->vendor.locality)); |
| 680 | |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 681 | chip->vendor.probed_irq = 0; |
| 682 | |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 683 | /* Generate Interrupts */ |
| 684 | tpm_gen_interrupt(chip); |
| 685 | |
Stefan Berger | a7b6682 | 2011-03-30 12:13:32 -0400 | [diff] [blame] | 686 | chip->vendor.irq = chip->vendor.probed_irq; |
| 687 | |
| 688 | /* free_irq will call into tis_int_probe; |
| 689 | clear all irqs we haven't seen while doing |
| 690 | tpm_gen_interrupt */ |
| 691 | iowrite32(ioread32 |
| 692 | (chip->vendor.iobase + |
| 693 | TPM_INT_STATUS(chip->vendor.locality)), |
| 694 | chip->vendor.iobase + |
| 695 | TPM_INT_STATUS(chip->vendor.locality)); |
| 696 | |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 697 | /* Turn off */ |
| 698 | iowrite32(intmask, |
| 699 | chip->vendor.iobase + |
| 700 | TPM_INT_ENABLE(chip->vendor.locality)); |
| 701 | free_irq(i, chip); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 702 | } |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 703 | } |
| 704 | if (chip->vendor.irq) { |
| 705 | iowrite8(chip->vendor.irq, |
| 706 | chip->vendor.iobase + |
| 707 | TPM_INT_VECTOR(chip->vendor.locality)); |
| 708 | if (request_irq |
Thomas Gleixner | 0f2ed4c | 2006-07-01 19:29:33 -0700 | [diff] [blame] | 709 | (chip->vendor.irq, tis_int_handler, IRQF_SHARED, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 710 | chip->vendor.miscdev.name, chip) != 0) { |
| 711 | dev_info(chip->dev, |
Kylene Jo Hall | 5713556 | 2006-04-22 02:39:44 -0700 | [diff] [blame] | 712 | "Unable to request irq: %d for use\n", |
| 713 | chip->vendor.irq); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 714 | chip->vendor.irq = 0; |
| 715 | } else { |
| 716 | /* Clear all existing */ |
| 717 | iowrite32(ioread32 |
| 718 | (chip->vendor.iobase + |
| 719 | TPM_INT_STATUS(chip->vendor.locality)), |
| 720 | chip->vendor.iobase + |
| 721 | TPM_INT_STATUS(chip->vendor.locality)); |
| 722 | |
| 723 | /* Turn on */ |
| 724 | iowrite32(intmask | TPM_GLOBAL_INT_ENABLE, |
| 725 | chip->vendor.iobase + |
| 726 | TPM_INT_ENABLE(chip->vendor.locality)); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | INIT_LIST_HEAD(&chip->vendor.list); |
| 731 | spin_lock(&tis_lock); |
| 732 | list_add(&chip->vendor.list, &tis_chips); |
| 733 | spin_unlock(&tis_lock); |
| 734 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 735 | |
| 736 | return 0; |
| 737 | out_err: |
| 738 | if (chip->vendor.iobase) |
| 739 | iounmap(chip->vendor.iobase); |
| 740 | tpm_remove_hardware(chip->dev); |
| 741 | return rc; |
| 742 | } |
Stefan Berger | 9685431 | 2011-07-17 01:04:24 -0400 | [diff] [blame] | 743 | |
| 744 | static void tpm_tis_reenable_interrupts(struct tpm_chip *chip) |
| 745 | { |
| 746 | u32 intmask; |
| 747 | |
| 748 | /* reenable interrupts that device may have lost or |
| 749 | BIOS/firmware may have disabled */ |
| 750 | iowrite8(chip->vendor.irq, chip->vendor.iobase + |
| 751 | TPM_INT_VECTOR(chip->vendor.locality)); |
| 752 | |
| 753 | intmask = |
| 754 | ioread32(chip->vendor.iobase + |
| 755 | TPM_INT_ENABLE(chip->vendor.locality)); |
| 756 | |
| 757 | intmask |= TPM_INTF_CMD_READY_INT |
| 758 | | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT |
| 759 | | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE; |
| 760 | |
| 761 | iowrite32(intmask, |
| 762 | chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality)); |
| 763 | } |
| 764 | |
| 765 | |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 766 | #ifdef CONFIG_PNP |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 767 | static int __devinit tpm_tis_pnp_init(struct pnp_dev *pnp_dev, |
| 768 | const struct pnp_device_id *pnp_id) |
| 769 | { |
Kylene Jo Hall | c3c36aa | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 770 | resource_size_t start, len; |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 771 | unsigned int irq = 0; |
| 772 | |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 773 | start = pnp_mem_start(pnp_dev, 0); |
| 774 | len = pnp_mem_len(pnp_dev, 0); |
| 775 | |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 776 | if (pnp_irq_valid(pnp_dev, 0)) |
| 777 | irq = pnp_irq(pnp_dev, 0); |
| 778 | else |
| 779 | interrupts = 0; |
| 780 | |
Olof Johansson | e5cce6c | 2011-01-06 21:24:01 -0600 | [diff] [blame] | 781 | if (is_itpm(pnp_dev)) |
| 782 | itpm = 1; |
| 783 | |
Bjorn Helgaas | 7917ff9 | 2007-10-16 23:26:46 -0700 | [diff] [blame] | 784 | return tpm_tis_init(&pnp_dev->dev, start, len, irq); |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 787 | static int tpm_tis_pnp_suspend(struct pnp_dev *dev, pm_message_t msg) |
| 788 | { |
| 789 | return tpm_pm_suspend(&dev->dev, msg); |
| 790 | } |
| 791 | |
| 792 | static int tpm_tis_pnp_resume(struct pnp_dev *dev) |
| 793 | { |
Rajiv Andrade | 59f6fbe | 2010-06-23 12:18:56 -0700 | [diff] [blame] | 794 | struct tpm_chip *chip = pnp_get_drvdata(dev); |
| 795 | int ret; |
| 796 | |
Stefan Berger | 45baa1d | 2011-03-30 12:13:30 -0400 | [diff] [blame] | 797 | if (chip->vendor.irq) |
| 798 | tpm_tis_reenable_interrupts(chip); |
| 799 | |
Rajiv Andrade | 59f6fbe | 2010-06-23 12:18:56 -0700 | [diff] [blame] | 800 | ret = tpm_pm_resume(&dev->dev); |
| 801 | if (!ret) |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 802 | tpm_do_selftest(chip); |
Rajiv Andrade | 59f6fbe | 2010-06-23 12:18:56 -0700 | [diff] [blame] | 803 | |
| 804 | return ret; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static struct pnp_device_id tpm_pnp_tbl[] __devinitdata = { |
| 808 | {"PNP0C31", 0}, /* TPM */ |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 809 | {"ATM1200", 0}, /* Atmel */ |
| 810 | {"IFX0102", 0}, /* Infineon */ |
| 811 | {"BCM0101", 0}, /* Broadcom */ |
LE DISEZ Erwan | 061991e | 2008-07-25 19:44:56 -0700 | [diff] [blame] | 812 | {"BCM0102", 0}, /* Broadcom */ |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 813 | {"NSC1200", 0}, /* National */ |
Marcin Obara | fb0e7e1 | 2008-07-10 17:30:42 -0700 | [diff] [blame] | 814 | {"ICO0102", 0}, /* Intel */ |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 815 | /* Add new here */ |
| 816 | {"", 0}, /* User Specified */ |
| 817 | {"", 0} /* Terminator */ |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 818 | }; |
Matt Domsch | 31bde71 | 2009-11-03 12:05:50 +1100 | [diff] [blame] | 819 | MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 820 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 821 | static __devexit void tpm_tis_pnp_remove(struct pnp_dev *dev) |
| 822 | { |
| 823 | struct tpm_chip *chip = pnp_get_drvdata(dev); |
| 824 | |
| 825 | tpm_dev_vendor_release(chip); |
| 826 | |
| 827 | kfree(chip); |
| 828 | } |
| 829 | |
| 830 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 831 | static struct pnp_driver tis_pnp_driver = { |
| 832 | .name = "tpm_tis", |
| 833 | .id_table = tpm_pnp_tbl, |
| 834 | .probe = tpm_tis_pnp_init, |
| 835 | .suspend = tpm_tis_pnp_suspend, |
| 836 | .resume = tpm_tis_pnp_resume, |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 837 | .remove = tpm_tis_pnp_remove, |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 838 | }; |
| 839 | |
Kylene Jo Hall | 93e1b7d | 2006-04-22 02:39:52 -0700 | [diff] [blame] | 840 | #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2 |
| 841 | module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id, |
| 842 | sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444); |
| 843 | MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe"); |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 844 | #endif |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 845 | static int tpm_tis_suspend(struct platform_device *dev, pm_message_t msg) |
| 846 | { |
| 847 | return tpm_pm_suspend(&dev->dev, msg); |
| 848 | } |
| 849 | |
| 850 | static int tpm_tis_resume(struct platform_device *dev) |
| 851 | { |
Stefan Berger | 45baa1d | 2011-03-30 12:13:30 -0400 | [diff] [blame] | 852 | struct tpm_chip *chip = dev_get_drvdata(&dev->dev); |
| 853 | |
| 854 | if (chip->vendor.irq) |
| 855 | tpm_tis_reenable_interrupts(chip); |
| 856 | |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 857 | return tpm_pm_resume(&dev->dev); |
| 858 | } |
| 859 | static struct platform_driver tis_drv = { |
| 860 | .driver = { |
| 861 | .name = "tpm_tis", |
| 862 | .owner = THIS_MODULE, |
| 863 | }, |
| 864 | .suspend = tpm_tis_suspend, |
| 865 | .resume = tpm_tis_resume, |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 866 | }; |
| 867 | |
| 868 | static struct platform_device *pdev; |
| 869 | |
| 870 | static int force; |
| 871 | module_param(force, bool, 0444); |
| 872 | MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry"); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 873 | static int __init init_tis(void) |
| 874 | { |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 875 | int rc; |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 876 | #ifdef CONFIG_PNP |
| 877 | if (!force) |
| 878 | return pnp_register_driver(&tis_pnp_driver); |
| 879 | #endif |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 880 | |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 881 | rc = platform_driver_register(&tis_drv); |
| 882 | if (rc < 0) |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 883 | return rc; |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 884 | if (IS_ERR(pdev=platform_device_register_simple("tpm_tis", -1, NULL, 0))) |
| 885 | return PTR_ERR(pdev); |
| 886 | if((rc=tpm_tis_init(&pdev->dev, TIS_MEM_BASE, TIS_MEM_LEN, 0)) != 0) { |
| 887 | platform_device_unregister(pdev); |
| 888 | platform_driver_unregister(&tis_drv); |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 889 | } |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 890 | return rc; |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static void __exit cleanup_tis(void) |
| 894 | { |
| 895 | struct tpm_vendor_specific *i, *j; |
| 896 | struct tpm_chip *chip; |
| 897 | spin_lock(&tis_lock); |
| 898 | list_for_each_entry_safe(i, j, &tis_chips, list) { |
| 899 | chip = to_tpm_chip(i); |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 900 | tpm_remove_hardware(chip->dev); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 901 | iowrite32(~TPM_GLOBAL_INT_ENABLE & |
| 902 | ioread32(chip->vendor.iobase + |
| 903 | TPM_INT_ENABLE(chip->vendor. |
| 904 | locality)), |
| 905 | chip->vendor.iobase + |
| 906 | TPM_INT_ENABLE(chip->vendor.locality)); |
| 907 | release_locality(chip, chip->vendor.locality, 1); |
| 908 | if (chip->vendor.irq) |
| 909 | free_irq(chip->vendor.irq, chip); |
| 910 | iounmap(i->iobase); |
| 911 | list_del(&i->list); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 912 | } |
| 913 | spin_unlock(&tis_lock); |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 914 | #ifdef CONFIG_PNP |
| 915 | if (!force) { |
Kylene Jo Hall | 9e323d3 | 2006-07-14 00:24:31 -0700 | [diff] [blame] | 916 | pnp_unregister_driver(&tis_pnp_driver); |
Rajiv Andrade | 7f2ab00 | 2010-05-13 17:37:54 -0300 | [diff] [blame] | 917 | return; |
| 918 | } |
| 919 | #endif |
| 920 | platform_device_unregister(pdev); |
| 921 | platform_driver_unregister(&tis_drv); |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | module_init(init_tis); |
| 925 | module_exit(cleanup_tis); |
| 926 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 927 | MODULE_DESCRIPTION("TPM Driver"); |
| 928 | MODULE_VERSION("2.0"); |
| 929 | MODULE_LICENSE("GPL"); |