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