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