Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Infineon Technologies |
| 3 | * |
| 4 | * Authors: |
| 5 | * Peter Huewe <peter.huewe@infineon.com> |
| 6 | * |
| 7 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 8 | * Specifications at www.trustedcomputinggroup.org |
| 9 | * |
| 10 | * This device driver implements the TPM interface as defined in |
| 11 | * the TCG TPM Interface Spec version 1.2, revision 1.0 and the |
| 12 | * Infineon I2C Protocol Stack Specification v0.20. |
| 13 | * |
| 14 | * It is based on the original tpm_tis device driver from Leendert van |
| 15 | * Dorn and Kyleen Hall. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation, version 2 of the |
| 20 | * License. |
| 21 | * |
| 22 | * |
| 23 | */ |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/wait.h> |
| 29 | #include "tpm.h" |
| 30 | |
| 31 | /* max. buffer size supported by our TPM */ |
| 32 | #define TPM_BUFSIZE 1260 |
| 33 | |
| 34 | /* max. number of iterations after I2C NAK */ |
| 35 | #define MAX_COUNT 3 |
| 36 | |
| 37 | #define SLEEP_DURATION_LOW 55 |
| 38 | #define SLEEP_DURATION_HI 65 |
| 39 | |
| 40 | /* max. number of iterations after I2C NAK for 'long' commands |
| 41 | * we need this especially for sending TPM_READY, since the cleanup after the |
| 42 | * transtion to the ready state may take some time, but it is unpredictable |
| 43 | * how long it will take. |
| 44 | */ |
| 45 | #define MAX_COUNT_LONG 50 |
| 46 | |
| 47 | #define SLEEP_DURATION_LONG_LOW 200 |
| 48 | #define SLEEP_DURATION_LONG_HI 220 |
| 49 | |
| 50 | /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */ |
| 51 | #define SLEEP_DURATION_RESET_LOW 2400 |
| 52 | #define SLEEP_DURATION_RESET_HI 2600 |
| 53 | |
| 54 | /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */ |
| 55 | #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000) |
| 56 | #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000) |
| 57 | |
| 58 | /* expected value for DIDVID register */ |
| 59 | #define TPM_TIS_I2C_DID_VID 0x000b15d1L |
| 60 | |
| 61 | /* Structure to store I2C TPM specific stuff */ |
| 62 | struct tpm_inf_dev { |
| 63 | struct i2c_client *client; |
| 64 | u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */ |
| 65 | struct tpm_chip *chip; |
| 66 | }; |
| 67 | |
| 68 | static struct tpm_inf_dev tpm_dev; |
| 69 | static struct i2c_driver tpm_tis_i2c_driver; |
| 70 | |
| 71 | /* |
| 72 | * iic_tpm_read() - read from TPM register |
| 73 | * @addr: register address to read from |
| 74 | * @buffer: provided by caller |
| 75 | * @len: number of bytes to read |
| 76 | * |
| 77 | * Read len bytes from TPM register and put them into |
| 78 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 79 | * |
| 80 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 81 | * values have to be swapped. |
| 82 | * |
| 83 | * NOTE: We can't unfortunately use the combined read/write functions |
| 84 | * provided by the i2c core as the TPM currently does not support the |
| 85 | * repeated start condition and due to it's special requirements. |
| 86 | * The i2c_smbus* functions do not work for this chip. |
| 87 | * |
| 88 | * Return -EIO on error, 0 on success. |
| 89 | */ |
| 90 | static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) |
| 91 | { |
| 92 | |
Shubhrajyoti Datta | eef8b62 | 2013-02-28 11:06:11 +0100 | [diff] [blame^] | 93 | struct i2c_msg msg1 = { |
| 94 | .addr = tpm_dev.client->addr, |
| 95 | .len = 1, |
| 96 | .buf = &addr |
| 97 | }; |
| 98 | struct i2c_msg msg2 = { |
| 99 | .addr = tpm_dev.client->addr, |
| 100 | .flags = I2C_M_RD, |
| 101 | .len = len, |
| 102 | .buf = buffer |
| 103 | }; |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 104 | |
| 105 | int rc; |
| 106 | int count; |
| 107 | |
| 108 | /* Lock the adapter for the duration of the whole sequence. */ |
| 109 | if (!tpm_dev.client->adapter->algo->master_xfer) |
| 110 | return -EOPNOTSUPP; |
| 111 | i2c_lock_adapter(tpm_dev.client->adapter); |
| 112 | |
| 113 | for (count = 0; count < MAX_COUNT; count++) { |
| 114 | rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); |
| 115 | if (rc > 0) |
| 116 | break; /* break here to skip sleep */ |
| 117 | |
| 118 | usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); |
| 119 | } |
| 120 | |
| 121 | if (rc <= 0) |
| 122 | goto out; |
| 123 | |
| 124 | /* After the TPM has successfully received the register address it needs |
| 125 | * some time, thus we're sleeping here again, before retrieving the data |
| 126 | */ |
| 127 | for (count = 0; count < MAX_COUNT; count++) { |
| 128 | usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); |
| 129 | rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1); |
| 130 | if (rc > 0) |
| 131 | break; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | out: |
| 136 | i2c_unlock_adapter(tpm_dev.client->adapter); |
| 137 | if (rc <= 0) |
| 138 | return -EIO; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, |
| 144 | unsigned int sleep_low, |
| 145 | unsigned int sleep_hi, u8 max_count) |
| 146 | { |
| 147 | int rc = -EIO; |
| 148 | int count; |
| 149 | |
Shubhrajyoti Datta | eef8b62 | 2013-02-28 11:06:11 +0100 | [diff] [blame^] | 150 | struct i2c_msg msg1 = { |
| 151 | .addr = tpm_dev.client->addr, |
| 152 | .len = len + 1, |
| 153 | .buf = tpm_dev.buf |
| 154 | }; |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 155 | |
| 156 | if (len > TPM_BUFSIZE) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | if (!tpm_dev.client->adapter->algo->master_xfer) |
| 160 | return -EOPNOTSUPP; |
| 161 | i2c_lock_adapter(tpm_dev.client->adapter); |
| 162 | |
| 163 | /* prepend the 'register address' to the buffer */ |
| 164 | tpm_dev.buf[0] = addr; |
| 165 | memcpy(&(tpm_dev.buf[1]), buffer, len); |
| 166 | |
| 167 | /* |
| 168 | * NOTE: We have to use these special mechanisms here and unfortunately |
| 169 | * cannot rely on the standard behavior of i2c_transfer. |
| 170 | */ |
| 171 | for (count = 0; count < max_count; count++) { |
| 172 | rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); |
| 173 | if (rc > 0) |
| 174 | break; |
| 175 | |
| 176 | usleep_range(sleep_low, sleep_hi); |
| 177 | } |
| 178 | |
| 179 | i2c_unlock_adapter(tpm_dev.client->adapter); |
| 180 | if (rc <= 0) |
| 181 | return -EIO; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * iic_tpm_write() - write to TPM register |
| 188 | * @addr: register address to write to |
| 189 | * @buffer: containing data to be written |
| 190 | * @len: number of bytes to write |
| 191 | * |
| 192 | * Write len bytes from provided buffer to TPM register (little |
| 193 | * endian format, i.e. buffer[0] is written as first byte). |
| 194 | * |
| 195 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 196 | * values have to be swapped. |
| 197 | * |
| 198 | * NOTE: use this function instead of the iic_tpm_write_generic function. |
| 199 | * |
| 200 | * Return -EIO on error, 0 on success |
| 201 | */ |
| 202 | static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) |
| 203 | { |
| 204 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW, |
| 205 | SLEEP_DURATION_HI, MAX_COUNT); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * This function is needed especially for the cleanup situation after |
| 210 | * sending TPM_READY |
| 211 | * */ |
| 212 | static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) |
| 213 | { |
| 214 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW, |
| 215 | SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG); |
| 216 | } |
| 217 | |
| 218 | enum tis_access { |
| 219 | TPM_ACCESS_VALID = 0x80, |
| 220 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, |
| 221 | TPM_ACCESS_REQUEST_PENDING = 0x04, |
| 222 | TPM_ACCESS_REQUEST_USE = 0x02, |
| 223 | }; |
| 224 | |
| 225 | enum tis_status { |
| 226 | TPM_STS_VALID = 0x80, |
| 227 | TPM_STS_COMMAND_READY = 0x40, |
| 228 | TPM_STS_GO = 0x20, |
| 229 | TPM_STS_DATA_AVAIL = 0x10, |
| 230 | TPM_STS_DATA_EXPECT = 0x08, |
| 231 | }; |
| 232 | |
| 233 | enum tis_defaults { |
| 234 | TIS_SHORT_TIMEOUT = 750, /* ms */ |
| 235 | TIS_LONG_TIMEOUT = 2000, /* 2 sec */ |
| 236 | }; |
| 237 | |
| 238 | #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) |
| 239 | #define TPM_STS(l) (0x0001 | ((l) << 4)) |
| 240 | #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) |
| 241 | #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) |
| 242 | |
| 243 | static int check_locality(struct tpm_chip *chip, int loc) |
| 244 | { |
| 245 | u8 buf; |
| 246 | int rc; |
| 247 | |
| 248 | rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); |
| 249 | if (rc < 0) |
| 250 | return rc; |
| 251 | |
| 252 | if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == |
| 253 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { |
| 254 | chip->vendor.locality = loc; |
| 255 | return loc; |
| 256 | } |
| 257 | |
| 258 | return -EIO; |
| 259 | } |
| 260 | |
| 261 | /* implementation similar to tpm_tis */ |
| 262 | static void release_locality(struct tpm_chip *chip, int loc, int force) |
| 263 | { |
| 264 | u8 buf; |
| 265 | if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) |
| 266 | return; |
| 267 | |
| 268 | if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == |
| 269 | (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { |
| 270 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
| 271 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static int request_locality(struct tpm_chip *chip, int loc) |
| 276 | { |
| 277 | unsigned long stop; |
| 278 | u8 buf = TPM_ACCESS_REQUEST_USE; |
| 279 | |
| 280 | if (check_locality(chip, loc) >= 0) |
| 281 | return loc; |
| 282 | |
| 283 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 284 | |
| 285 | /* wait for burstcount */ |
| 286 | stop = jiffies + chip->vendor.timeout_a; |
| 287 | do { |
| 288 | if (check_locality(chip, loc) >= 0) |
| 289 | return loc; |
| 290 | usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); |
| 291 | } while (time_before(jiffies, stop)); |
| 292 | |
| 293 | return -ETIME; |
| 294 | } |
| 295 | |
| 296 | static u8 tpm_tis_i2c_status(struct tpm_chip *chip) |
| 297 | { |
| 298 | /* NOTE: since I2C read may fail, return 0 in this case --> time-out */ |
| 299 | u8 buf; |
| 300 | if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0) |
| 301 | return 0; |
| 302 | else |
| 303 | return buf; |
| 304 | } |
| 305 | |
| 306 | static void tpm_tis_i2c_ready(struct tpm_chip *chip) |
| 307 | { |
| 308 | /* this causes the current command to be aborted */ |
| 309 | u8 buf = TPM_STS_COMMAND_READY; |
| 310 | iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1); |
| 311 | } |
| 312 | |
| 313 | static ssize_t get_burstcount(struct tpm_chip *chip) |
| 314 | { |
| 315 | unsigned long stop; |
| 316 | ssize_t burstcnt; |
| 317 | u8 buf[3]; |
| 318 | |
| 319 | /* wait for burstcount */ |
| 320 | /* which timeout value, spec has 2 answers (c & d) */ |
| 321 | stop = jiffies + chip->vendor.timeout_d; |
| 322 | do { |
| 323 | /* Note: STS is little endian */ |
| 324 | if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0) |
| 325 | burstcnt = 0; |
| 326 | else |
| 327 | burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 328 | |
| 329 | if (burstcnt) |
| 330 | return burstcnt; |
| 331 | |
| 332 | usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); |
| 333 | } while (time_before(jiffies, stop)); |
| 334 | return -EBUSY; |
| 335 | } |
| 336 | |
| 337 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
| 338 | int *status) |
| 339 | { |
| 340 | unsigned long stop; |
| 341 | |
| 342 | /* check current status */ |
| 343 | *status = tpm_tis_i2c_status(chip); |
| 344 | if ((*status & mask) == mask) |
| 345 | return 0; |
| 346 | |
| 347 | stop = jiffies + timeout; |
| 348 | do { |
| 349 | /* since we just checked the status, give the TPM some time */ |
| 350 | usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); |
| 351 | *status = tpm_tis_i2c_status(chip); |
| 352 | if ((*status & mask) == mask) |
| 353 | return 0; |
| 354 | |
| 355 | } while (time_before(jiffies, stop)); |
| 356 | |
| 357 | return -ETIME; |
| 358 | } |
| 359 | |
| 360 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
| 361 | { |
| 362 | size_t size = 0; |
| 363 | ssize_t burstcnt; |
| 364 | u8 retries = 0; |
| 365 | int rc; |
| 366 | |
| 367 | while (size < count) { |
| 368 | burstcnt = get_burstcount(chip); |
| 369 | |
| 370 | /* burstcnt < 0 = TPM is busy */ |
| 371 | if (burstcnt < 0) |
| 372 | return burstcnt; |
| 373 | |
| 374 | /* limit received data to max. left */ |
| 375 | if (burstcnt > (count - size)) |
| 376 | burstcnt = count - size; |
| 377 | |
| 378 | rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality), |
| 379 | &(buf[size]), burstcnt); |
| 380 | if (rc == 0) |
| 381 | size += burstcnt; |
| 382 | else if (rc < 0) |
| 383 | retries++; |
| 384 | |
| 385 | /* avoid endless loop in case of broken HW */ |
| 386 | if (retries > MAX_COUNT_LONG) |
| 387 | return -EIO; |
| 388 | |
| 389 | } |
| 390 | return size; |
| 391 | } |
| 392 | |
| 393 | static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 394 | { |
| 395 | int size = 0; |
| 396 | int expected, status; |
| 397 | |
| 398 | if (count < TPM_HEADER_SIZE) { |
| 399 | size = -EIO; |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | /* read first 10 bytes, including tag, paramsize, and result */ |
| 404 | size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 405 | if (size < TPM_HEADER_SIZE) { |
| 406 | dev_err(chip->dev, "Unable to read header\n"); |
| 407 | goto out; |
| 408 | } |
| 409 | |
| 410 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); |
| 411 | if ((size_t) expected > count) { |
| 412 | size = -EIO; |
| 413 | goto out; |
| 414 | } |
| 415 | |
| 416 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
| 417 | expected - TPM_HEADER_SIZE); |
| 418 | if (size < expected) { |
| 419 | dev_err(chip->dev, "Unable to read remainder of result\n"); |
| 420 | size = -ETIME; |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); |
| 425 | if (status & TPM_STS_DATA_AVAIL) { /* retry? */ |
| 426 | dev_err(chip->dev, "Error left over data\n"); |
| 427 | size = -EIO; |
| 428 | goto out; |
| 429 | } |
| 430 | |
| 431 | out: |
| 432 | tpm_tis_i2c_ready(chip); |
| 433 | /* The TPM needs some time to clean up here, |
| 434 | * so we sleep rather than keeping the bus busy |
| 435 | */ |
| 436 | usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); |
| 437 | release_locality(chip, chip->vendor.locality, 0); |
| 438 | return size; |
| 439 | } |
| 440 | |
| 441 | static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 442 | { |
| 443 | int rc, status; |
| 444 | ssize_t burstcnt; |
| 445 | size_t count = 0; |
| 446 | u8 retries = 0; |
| 447 | u8 sts = TPM_STS_GO; |
| 448 | |
| 449 | if (len > TPM_BUFSIZE) |
| 450 | return -E2BIG; /* command is too long for our tpm, sorry */ |
| 451 | |
| 452 | if (request_locality(chip, 0) < 0) |
| 453 | return -EBUSY; |
| 454 | |
| 455 | status = tpm_tis_i2c_status(chip); |
| 456 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 457 | tpm_tis_i2c_ready(chip); |
| 458 | if (wait_for_stat |
| 459 | (chip, TPM_STS_COMMAND_READY, |
| 460 | chip->vendor.timeout_b, &status) < 0) { |
| 461 | rc = -ETIME; |
| 462 | goto out_err; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | while (count < len - 1) { |
| 467 | burstcnt = get_burstcount(chip); |
| 468 | |
| 469 | /* burstcnt < 0 = TPM is busy */ |
| 470 | if (burstcnt < 0) |
| 471 | return burstcnt; |
| 472 | |
| 473 | if (burstcnt > (len - 1 - count)) |
| 474 | burstcnt = len - 1 - count; |
| 475 | |
| 476 | rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), |
| 477 | &(buf[count]), burstcnt); |
| 478 | if (rc == 0) |
| 479 | count += burstcnt; |
| 480 | else if (rc < 0) |
| 481 | retries++; |
| 482 | |
| 483 | /* avoid endless loop in case of broken HW */ |
| 484 | if (retries > MAX_COUNT_LONG) { |
| 485 | rc = -EIO; |
| 486 | goto out_err; |
| 487 | } |
| 488 | |
| 489 | wait_for_stat(chip, TPM_STS_VALID, |
| 490 | chip->vendor.timeout_c, &status); |
| 491 | |
| 492 | if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 493 | rc = -EIO; |
| 494 | goto out_err; |
| 495 | } |
| 496 | |
| 497 | } |
| 498 | |
| 499 | /* write last byte */ |
| 500 | iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1); |
| 501 | wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); |
| 502 | if ((status & TPM_STS_DATA_EXPECT) != 0) { |
| 503 | rc = -EIO; |
| 504 | goto out_err; |
| 505 | } |
| 506 | |
| 507 | /* go and do it */ |
| 508 | iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1); |
| 509 | |
| 510 | return len; |
| 511 | out_err: |
| 512 | tpm_tis_i2c_ready(chip); |
| 513 | /* The TPM needs some time to clean up here, |
| 514 | * so we sleep rather than keeping the bus busy |
| 515 | */ |
| 516 | usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); |
| 517 | release_locality(chip, chip->vendor.locality, 0); |
| 518 | return rc; |
| 519 | } |
| 520 | |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 521 | static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status) |
| 522 | { |
| 523 | return (status == TPM_STS_COMMAND_READY); |
| 524 | } |
| 525 | |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 526 | static const struct file_operations tis_ops = { |
| 527 | .owner = THIS_MODULE, |
| 528 | .llseek = no_llseek, |
| 529 | .open = tpm_open, |
| 530 | .read = tpm_read, |
| 531 | .write = tpm_write, |
| 532 | .release = tpm_release, |
| 533 | }; |
| 534 | |
| 535 | static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); |
| 536 | static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); |
| 537 | static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); |
| 538 | static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); |
| 539 | static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); |
| 540 | static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL); |
| 541 | static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); |
| 542 | static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); |
| 543 | static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); |
| 544 | static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); |
| 545 | |
| 546 | static struct attribute *tis_attrs[] = { |
| 547 | &dev_attr_pubek.attr, |
| 548 | &dev_attr_pcrs.attr, |
| 549 | &dev_attr_enabled.attr, |
| 550 | &dev_attr_active.attr, |
| 551 | &dev_attr_owned.attr, |
| 552 | &dev_attr_temp_deactivated.attr, |
| 553 | &dev_attr_caps.attr, |
| 554 | &dev_attr_cancel.attr, |
| 555 | &dev_attr_durations.attr, |
| 556 | &dev_attr_timeouts.attr, |
| 557 | NULL, |
| 558 | }; |
| 559 | |
| 560 | static struct attribute_group tis_attr_grp = { |
| 561 | .attrs = tis_attrs |
| 562 | }; |
| 563 | |
| 564 | static struct tpm_vendor_specific tpm_tis_i2c = { |
| 565 | .status = tpm_tis_i2c_status, |
| 566 | .recv = tpm_tis_i2c_recv, |
| 567 | .send = tpm_tis_i2c_send, |
| 568 | .cancel = tpm_tis_i2c_ready, |
| 569 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 570 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 571 | .req_canceled = tpm_tis_i2c_req_canceled, |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 572 | .attr_group = &tis_attr_grp, |
| 573 | .miscdev.fops = &tis_ops, |
| 574 | }; |
| 575 | |
Bill Pemberton | afc6d36 | 2012-11-19 13:22:42 -0500 | [diff] [blame] | 576 | static int tpm_tis_i2c_init(struct device *dev) |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 577 | { |
| 578 | u32 vendor; |
| 579 | int rc = 0; |
| 580 | struct tpm_chip *chip; |
| 581 | |
| 582 | chip = tpm_register_hardware(dev, &tpm_tis_i2c); |
| 583 | if (!chip) { |
| 584 | rc = -ENODEV; |
| 585 | goto out_err; |
| 586 | } |
| 587 | |
| 588 | /* Disable interrupts */ |
| 589 | chip->vendor.irq = 0; |
| 590 | |
| 591 | /* Default timeouts */ |
| 592 | chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 593 | chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); |
| 594 | chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 595 | chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 596 | |
| 597 | if (request_locality(chip, 0) != 0) { |
| 598 | rc = -ENODEV; |
| 599 | goto out_vendor; |
| 600 | } |
| 601 | |
| 602 | /* read four bytes from DID_VID register */ |
| 603 | if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) { |
| 604 | rc = -EIO; |
| 605 | goto out_release; |
| 606 | } |
| 607 | |
| 608 | /* create DID_VID register value, after swapping to little-endian */ |
| 609 | vendor = be32_to_cpu((__be32) vendor); |
| 610 | |
| 611 | if (vendor != TPM_TIS_I2C_DID_VID) { |
| 612 | rc = -ENODEV; |
| 613 | goto out_release; |
| 614 | } |
| 615 | |
| 616 | dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); |
| 617 | |
| 618 | INIT_LIST_HEAD(&chip->vendor.list); |
| 619 | tpm_dev.chip = chip; |
| 620 | |
| 621 | tpm_get_timeouts(chip); |
| 622 | tpm_do_selftest(chip); |
| 623 | |
| 624 | return 0; |
| 625 | |
| 626 | out_release: |
| 627 | release_locality(chip, chip->vendor.locality, 1); |
| 628 | |
| 629 | out_vendor: |
| 630 | /* close file handles */ |
| 631 | tpm_dev_vendor_release(chip); |
| 632 | |
| 633 | /* remove hardware */ |
| 634 | tpm_remove_hardware(chip->dev); |
| 635 | |
| 636 | /* reset these pointers, otherwise we oops */ |
| 637 | chip->dev->release = NULL; |
| 638 | chip->release = NULL; |
| 639 | tpm_dev.client = NULL; |
| 640 | dev_set_drvdata(chip->dev, chip); |
| 641 | out_err: |
| 642 | return rc; |
| 643 | } |
| 644 | |
| 645 | static const struct i2c_device_id tpm_tis_i2c_table[] = { |
| 646 | {"tpm_i2c_infineon", 0}, |
| 647 | {}, |
| 648 | }; |
| 649 | |
| 650 | MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table); |
| 651 | static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume); |
| 652 | |
Bill Pemberton | afc6d36 | 2012-11-19 13:22:42 -0500 | [diff] [blame] | 653 | static int tpm_tis_i2c_probe(struct i2c_client *client, |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 654 | const struct i2c_device_id *id) |
| 655 | { |
| 656 | int rc; |
| 657 | if (tpm_dev.client != NULL) |
| 658 | return -EBUSY; /* We only support one client */ |
| 659 | |
| 660 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 661 | dev_err(&client->dev, |
| 662 | "no algorithms associated to the i2c bus\n"); |
| 663 | return -ENODEV; |
| 664 | } |
| 665 | |
| 666 | client->driver = &tpm_tis_i2c_driver; |
| 667 | tpm_dev.client = client; |
| 668 | rc = tpm_tis_i2c_init(&client->dev); |
| 669 | if (rc != 0) { |
| 670 | client->driver = NULL; |
| 671 | tpm_dev.client = NULL; |
| 672 | rc = -ENODEV; |
| 673 | } |
| 674 | return rc; |
| 675 | } |
| 676 | |
Bill Pemberton | 39af33f | 2012-11-19 13:26:26 -0500 | [diff] [blame] | 677 | static int tpm_tis_i2c_remove(struct i2c_client *client) |
Peter Huewe | aad628c | 2012-08-07 11:42:32 +0200 | [diff] [blame] | 678 | { |
| 679 | struct tpm_chip *chip = tpm_dev.chip; |
| 680 | release_locality(chip, chip->vendor.locality, 1); |
| 681 | |
| 682 | /* close file handles */ |
| 683 | tpm_dev_vendor_release(chip); |
| 684 | |
| 685 | /* remove hardware */ |
| 686 | tpm_remove_hardware(chip->dev); |
| 687 | |
| 688 | /* reset these pointers, otherwise we oops */ |
| 689 | chip->dev->release = NULL; |
| 690 | chip->release = NULL; |
| 691 | tpm_dev.client = NULL; |
| 692 | dev_set_drvdata(chip->dev, chip); |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static struct i2c_driver tpm_tis_i2c_driver = { |
| 698 | |
| 699 | .id_table = tpm_tis_i2c_table, |
| 700 | .probe = tpm_tis_i2c_probe, |
| 701 | .remove = tpm_tis_i2c_remove, |
| 702 | .driver = { |
| 703 | .name = "tpm_i2c_infineon", |
| 704 | .owner = THIS_MODULE, |
| 705 | .pm = &tpm_tis_i2c_ops, |
| 706 | }, |
| 707 | }; |
| 708 | |
| 709 | module_i2c_driver(tpm_tis_i2c_driver); |
| 710 | MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>"); |
| 711 | MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver"); |
| 712 | MODULE_VERSION("2.1.5"); |
| 713 | MODULE_LICENSE("GPL"); |