OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2011 Infineon Technologies |
| 3 * |
| 4 * Authors: |
| 5 * Peter Huewe <huewe.external@infineon.com> |
| 6 * |
| 7 * Description: |
| 8 * Device driver for TCG/TCPA TPM (trusted platform module). |
| 9 * Specifications at www.trustedcomputinggroup.org |
| 10 * |
| 11 * This device driver implements the TPM interface as defined in |
| 12 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the |
| 13 * Infineon I2C Protocol Stack Specification v0.20. |
| 14 * |
| 15 * It is based on the Linux kernel driver tpm.c from Leendert van |
| 16 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. |
| 17 * |
| 18 * Version: 2.1.1 |
| 19 * |
| 20 * See file CREDITS for list of people who contributed to this |
| 21 * project. |
| 22 * |
| 23 * This program is free software; you can redistribute it and/or |
| 24 * modify it under the terms of the GNU General Public License as |
| 25 * published by the Free Software Foundation, version 2 of the |
| 26 * License. |
| 27 * |
| 28 * This program is distributed in the hope that it will be useful, |
| 29 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 * GNU General Public License for more details. |
| 32 * |
| 33 * You should have received a copy of the GNU General Public License |
| 34 * along with this program; if not, write to the Free Software |
| 35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 36 * MA 02111-1307 USA |
| 37 */ |
| 38 |
| 39 #include <common.h> |
| 40 #include <i2c.h> |
| 41 #include <linux/types.h> |
| 42 |
| 43 #include "compatibility.h" |
| 44 #include "tpm.h" |
| 45 |
| 46 /* max. buffer size supported by our tpm */ |
| 47 #ifdef TPM_BUFSIZE |
| 48 #undef TPM_BUFSIZE |
| 49 #endif |
| 50 #define TPM_BUFSIZE 1260 |
| 51 /* Address of the TPM on the I2C bus */ |
| 52 #define TPM_I2C_ADDR 0x20 |
| 53 /* max. number of iterations after i2c NAK */ |
| 54 #define MAX_COUNT 3 |
| 55 |
| 56 #define SLEEP_DURATION 60 /*in usec*/ |
| 57 |
| 58 /* max. number of iterations after i2c NAK for 'long' commands |
| 59 * we need this especially for sending TPM_READY, since the cleanup after the |
| 60 * transtion to the ready state may take some time, but it is unpredictable |
| 61 * how long it will take. |
| 62 */ |
| 63 #define MAX_COUNT_LONG 50 |
| 64 |
| 65 #define SLEEP_DURATION_LONG 210 /* in usec */ |
| 66 |
| 67 /* expected value for DIDVID register */ |
| 68 #define TPM_TIS_I2C_DID_VID 0x000b15d1L |
| 69 |
| 70 /* Structure to store I2C TPM specific stuff */ |
| 71 struct tpm_inf_dev { |
| 72 uint addr; |
| 73 u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */ |
| 74 }; |
| 75 |
| 76 static struct tpm_inf_dev tpm_dev = { |
| 77 .addr = TPM_I2C_ADDR |
| 78 }; |
| 79 |
| 80 /* I2C Read/Write Functions from U-Boot |
| 81 * Unfortunately we have to use these functions directly, due to the wakeup |
| 82 * behaviour of the tpm |
| 83 */ |
| 84 extern int i2c_read_data(uchar chip, uchar *buffer, int len); |
| 85 extern int i2c_write_data(uchar chip, uchar *buffer, int len); |
| 86 |
| 87 /* |
| 88 * iic_tpm_read() - read from TPM register |
| 89 * @addr: register address to read from |
| 90 * @buffer: provided by caller |
| 91 * @len: number of bytes to read |
| 92 * |
| 93 * Read len bytes from TPM register and put them into |
| 94 * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 95 * |
| 96 * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 97 * values have to be swapped. |
| 98 * |
| 99 * Return -EIO on error, 0 on success. |
| 100 */ |
| 101 int iic_tpm_read(u8 addr, u8 *buffer, size_t len) |
| 102 { |
| 103 int rc; |
| 104 int count; |
| 105 uint myaddr = addr; |
| 106 /* we have to use uint here, uchar hangs the board */ |
| 107 |
| 108 for (count = 0; count < MAX_COUNT; count++) { |
| 109 rc = i2c_write_data(tpm_dev.addr, (uchar *) &myaddr, 1); |
| 110 if (rc == 0) |
| 111 break; /*success, break to skip sleep*/ |
| 112 |
| 113 udelay(SLEEP_DURATION); |
| 114 } |
| 115 |
| 116 if (rc) |
| 117 return -rc; |
| 118 |
| 119 /* After the TPM has successfully received the register address it needs |
| 120 * some time, thus we're sleeping here again, before retrieving the data |
| 121 */ |
| 122 for (count = 0; count < MAX_COUNT; count++) { |
| 123 udelay(SLEEP_DURATION); |
| 124 rc = i2c_read_data(tpm_dev.addr, buffer, len); |
| 125 if (rc == 0) |
| 126 break; /*success, break to skip sleep*/ |
| 127 |
| 128 } |
| 129 |
| 130 if (rc) |
| 131 return -rc; |
| 132 |
| 133 return 0; |
| 134 } |
| 135 |
| 136 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, |
| 137 unsigned int sleep_time, |
| 138 u8 max_count) |
| 139 { |
| 140 int rc = 0; |
| 141 int count; |
| 142 |
| 143 /* prepare send buffer */ |
| 144 tpm_dev.buf[0] = addr; |
| 145 memcpy(&(tpm_dev.buf[1]), buffer, len); |
| 146 |
| 147 for (count = 0; count < max_count; count++) { |
| 148 rc = i2c_write_data(tpm_dev.addr, tpm_dev.buf, len + 1); |
| 149 if (rc == 0) |
| 150 break; /*success, break to skip sleep*/ |
| 151 |
| 152 udelay(sleep_time); |
| 153 } |
| 154 |
| 155 if (rc) |
| 156 return -rc; |
| 157 |
| 158 return 0; |
| 159 } |
| 160 |
| 161 /* |
| 162 * iic_tpm_write() - write to TPM register |
| 163 * @addr: register address to write to |
| 164 * @buffer: containing data to be written |
| 165 * @len: number of bytes to write |
| 166 * |
| 167 * Write len bytes from provided buffer to TPM register (little |
| 168 * endian format, i.e. buffer[0] is written as first byte). |
| 169 * |
| 170 * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 171 * values have to be swapped. |
| 172 * |
| 173 * NOTE: use this function instead of the iic_tpm_write_generic function. |
| 174 * |
| 175 * Return -EIO on error, 0 on success |
| 176 */ |
| 177 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) |
| 178 { |
| 179 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION, |
| 180 MAX_COUNT); |
| 181 } |
| 182 |
| 183 /* |
| 184 * This function is needed especially for the cleanup situation after |
| 185 * sending TPM_READY |
| 186 * */ |
| 187 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) |
| 188 { |
| 189 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG, |
| 190 MAX_COUNT_LONG); |
| 191 } |
| 192 |
| 193 #define TPM_HEADER_SIZE 10 |
| 194 |
| 195 enum tis_access { |
| 196 TPM_ACCESS_VALID = 0x80, |
| 197 TPM_ACCESS_ACTIVE_LOCALITY = 0x20, |
| 198 TPM_ACCESS_REQUEST_PENDING = 0x04, |
| 199 TPM_ACCESS_REQUEST_USE = 0x02, |
| 200 }; |
| 201 |
| 202 enum tis_status { |
| 203 TPM_STS_VALID = 0x80, |
| 204 TPM_STS_COMMAND_READY = 0x40, |
| 205 TPM_STS_GO = 0x20, |
| 206 TPM_STS_DATA_AVAIL = 0x10, |
| 207 TPM_STS_DATA_EXPECT = 0x08, |
| 208 }; |
| 209 |
| 210 enum tis_defaults { |
| 211 TIS_SHORT_TIMEOUT = 750, /* ms */ |
| 212 TIS_LONG_TIMEOUT = 2000, /* 2 sec */ |
| 213 }; |
| 214 |
| 215 #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) |
| 216 #define TPM_STS(l) (0x0001 | ((l) << 4)) |
| 217 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) |
| 218 #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) |
| 219 |
| 220 static int check_locality(struct tpm_chip *chip, int loc) |
| 221 { |
| 222 u8 buf; |
| 223 int rc; |
| 224 |
| 225 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); |
| 226 if (rc < 0) |
| 227 return rc; |
| 228 |
| 229 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == |
| 230 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { |
| 231 chip->vendor.locality = loc; |
| 232 return loc; |
| 233 } |
| 234 |
| 235 return -1; |
| 236 } |
| 237 |
| 238 static void release_locality(struct tpm_chip *chip, int loc, int force) |
| 239 { |
| 240 u8 buf; |
| 241 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) |
| 242 return; |
| 243 |
| 244 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == |
| 245 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { |
| 246 buf = TPM_ACCESS_ACTIVE_LOCALITY; |
| 247 iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 248 } |
| 249 } |
| 250 |
| 251 static int request_locality(struct tpm_chip *chip, int loc) |
| 252 { |
| 253 unsigned long start, stop; |
| 254 u8 buf = TPM_ACCESS_REQUEST_USE; |
| 255 |
| 256 if (check_locality(chip, loc) >= 0) |
| 257 return loc; /* we already have the locality */ |
| 258 |
| 259 iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 260 |
| 261 /* wait for burstcount */ |
| 262 start = get_timer(0); |
| 263 stop = chip->vendor.timeout_a; |
| 264 do { |
| 265 if (check_locality(chip, loc) >= 0) |
| 266 return loc; |
| 267 msleep(TPM_TIMEOUT); |
| 268 } while (get_timer(start) < stop); |
| 269 |
| 270 return -1; |
| 271 } |
| 272 |
| 273 static u8 tpm_tis_i2c_status(struct tpm_chip *chip) |
| 274 { |
| 275 /* NOTE: since i2c read may fail, return 0 in this case --> time-out */ |
| 276 u8 buf; |
| 277 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0) |
| 278 return 0; |
| 279 else |
| 280 return buf; |
| 281 } |
| 282 |
| 283 static void tpm_tis_i2c_ready(struct tpm_chip *chip) |
| 284 { |
| 285 /* this causes the current command to be aborted */ |
| 286 u8 buf = TPM_STS_COMMAND_READY; |
| 287 iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1); |
| 288 } |
| 289 |
| 290 static ssize_t get_burstcount(struct tpm_chip *chip) |
| 291 { |
| 292 unsigned long start, stop; |
| 293 ssize_t burstcnt; |
| 294 u8 buf[3]; |
| 295 |
| 296 /* wait for burstcount */ |
| 297 /* which timeout value, spec has 2 answers (c & d) */ |
| 298 start = get_timer(0); |
| 299 stop = chip->vendor.timeout_d; |
| 300 do { |
| 301 /* Note: STS is little endian */ |
| 302 if (iic_tpm_read(TPM_STS(chip->vendor.locality) + 1, buf, 3) < 0
) |
| 303 burstcnt = 0; |
| 304 else |
| 305 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 306 |
| 307 if (burstcnt) |
| 308 return burstcnt; |
| 309 msleep(TPM_TIMEOUT); |
| 310 } while (get_timer(start) < stop); |
| 311 return -EBUSY; |
| 312 } |
| 313 |
| 314 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
| 315 int *status) |
| 316 { |
| 317 unsigned long start, stop; |
| 318 |
| 319 /* check current status */ |
| 320 *status = tpm_tis_i2c_status(chip); |
| 321 if ((*status & mask) == mask) |
| 322 return 0; |
| 323 |
| 324 start = get_timer(0); |
| 325 stop = timeout; |
| 326 do { |
| 327 msleep(TPM_TIMEOUT); |
| 328 *status = tpm_tis_i2c_status(chip); |
| 329 if ((*status & mask) == mask) |
| 330 return 0; |
| 331 |
| 332 } while (get_timer(start) < stop); |
| 333 |
| 334 return -ETIME; |
| 335 } |
| 336 |
| 337 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
| 338 { |
| 339 size_t size = 0; |
| 340 ssize_t burstcnt; |
| 341 int rc; |
| 342 |
| 343 while (size < count) { |
| 344 burstcnt = get_burstcount(chip); |
| 345 |
| 346 /* burstcount < 0 = tpm is busy */ |
| 347 if (burstcnt < 0) |
| 348 return burstcnt; |
| 349 |
| 350 /* limit received data to max. left */ |
| 351 if (burstcnt > (count - size)) |
| 352 burstcnt = count - size; |
| 353 |
| 354 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality), |
| 355 &(buf[size]), |
| 356 burstcnt); |
| 357 if (rc == 0) |
| 358 size += burstcnt; |
| 359 |
| 360 } |
| 361 return size; |
| 362 } |
| 363 |
| 364 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 365 { |
| 366 int size = 0; |
| 367 int expected, status; |
| 368 |
| 369 if (count < TPM_HEADER_SIZE) { |
| 370 size = -EIO; |
| 371 goto out; |
| 372 } |
| 373 |
| 374 /* read first 10 bytes, including tag, paramsize, and result */ |
| 375 size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 376 if (size < TPM_HEADER_SIZE) { |
| 377 dev_err(chip->dev, "Unable to read header\n"); |
| 378 goto out; |
| 379 } |
| 380 |
| 381 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE); |
| 382 if ((size_t)expected > count) { |
| 383 size = -EIO; |
| 384 goto out; |
| 385 } |
| 386 |
| 387 size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
| 388 expected - TPM_HEADER_SIZE); |
| 389 if (size < expected) { |
| 390 dev_err(chip->dev, "Unable to read remainder of result\n"); |
| 391 size = -ETIME; |
| 392 goto out; |
| 393 } |
| 394 |
| 395 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); |
| 396 if (status & TPM_STS_DATA_AVAIL) { /* retry? */ |
| 397 dev_err(chip->dev, "Error left over data\n"); |
| 398 size = -EIO; |
| 399 goto out; |
| 400 } |
| 401 |
| 402 out: |
| 403 tpm_tis_i2c_ready(chip); |
| 404 /* The TPM needs some time to clean up here, |
| 405 * so we sleep rather than keeping the bus busy |
| 406 */ |
| 407 msleep(2); |
| 408 release_locality(chip, chip->vendor.locality, 0); |
| 409 |
| 410 return size; |
| 411 } |
| 412 |
| 413 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 414 { |
| 415 int rc, status; |
| 416 ssize_t burstcnt; |
| 417 size_t count = 0; |
| 418 u8 sts = TPM_STS_GO; |
| 419 |
| 420 if (len > TPM_BUFSIZE) |
| 421 return -E2BIG; /* command is too long for our tpm, sorry */ |
| 422 |
| 423 if (request_locality(chip, 0) < 0) |
| 424 return -EBUSY; |
| 425 |
| 426 status = tpm_tis_i2c_status(chip); |
| 427 if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 428 tpm_tis_i2c_ready(chip); |
| 429 if (wait_for_stat |
| 430 (chip, TPM_STS_COMMAND_READY, |
| 431 chip->vendor.timeout_b, &status) < 0) { |
| 432 rc = -ETIME; |
| 433 goto out_err; |
| 434 } |
| 435 } |
| 436 |
| 437 while (count < len - 1) { |
| 438 burstcnt = get_burstcount(chip); |
| 439 |
| 440 /* burstcount < 0 = tpm is busy */ |
| 441 if (burstcnt < 0) |
| 442 return burstcnt; |
| 443 |
| 444 if (burstcnt > (len-1-count)) |
| 445 burstcnt = len-1-count; |
| 446 |
| 447 #ifdef CONFIG_TPM_I2C_BURST_LIMITATION |
| 448 if (burstcnt > CONFIG_TPM_I2C_BURST_LIMITATION) |
| 449 burstcnt = CONFIG_TPM_I2C_BURST_LIMITATION; |
| 450 #endif /* CONFIG_TPM_I2C_BURST_LIMITATION */ |
| 451 |
| 452 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), |
| 453 &(buf[count]), burstcnt); |
| 454 if (rc == 0) |
| 455 count += burstcnt; |
| 456 |
| 457 wait_for_stat(chip, TPM_STS_VALID, |
| 458 chip->vendor.timeout_c, &status); |
| 459 |
| 460 if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 461 rc = -EIO; |
| 462 goto out_err; |
| 463 } |
| 464 |
| 465 } |
| 466 |
| 467 /* write last byte */ |
| 468 iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1); |
| 469 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); |
| 470 if ((status & TPM_STS_DATA_EXPECT) != 0) { |
| 471 rc = -EIO; |
| 472 goto out_err; |
| 473 } |
| 474 |
| 475 /* go and do it */ |
| 476 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1); |
| 477 |
| 478 return len; |
| 479 out_err: |
| 480 tpm_tis_i2c_ready(chip); |
| 481 /* The TPM needs some time to clean up here, |
| 482 * so we sleep rather than keeping the bus busy |
| 483 */ |
| 484 msleep(2); |
| 485 release_locality(chip, chip->vendor.locality, 0); |
| 486 |
| 487 return rc; |
| 488 } |
| 489 |
| 490 static struct tpm_vendor_specific tpm_tis_i2c = { |
| 491 .status = tpm_tis_i2c_status, |
| 492 .recv = tpm_tis_i2c_recv, |
| 493 .send = tpm_tis_i2c_send, |
| 494 .cancel = tpm_tis_i2c_ready, |
| 495 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 496 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 497 .req_canceled = TPM_STS_COMMAND_READY, |
| 498 }; |
| 499 |
| 500 /* initialisation of i2c tpm */ |
| 501 |
| 502 |
| 503 int tpm_vendor_init(uint32_t dev_addr) |
| 504 { |
| 505 u32 vendor; |
| 506 uint old_addr; |
| 507 int rc = 0; |
| 508 struct tpm_chip *chip; |
| 509 |
| 510 old_addr = tpm_dev.addr; |
| 511 if (dev_addr != 0) |
| 512 tpm_dev.addr = dev_addr; |
| 513 |
| 514 chip = tpm_register_hardware(&tpm_tis_i2c); |
| 515 if (chip < 0) { |
| 516 rc = -ENODEV; |
| 517 goto out_err; |
| 518 } |
| 519 |
| 520 /* Disable interrupts (not supported) */ |
| 521 chip->vendor.irq = 0; |
| 522 |
| 523 /* Default timeouts */ |
| 524 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT; |
| 525 chip->vendor.timeout_b = TIS_LONG_TIMEOUT; |
| 526 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT; |
| 527 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT; |
| 528 |
| 529 if (request_locality(chip, 0) != 0) { |
| 530 rc = -ENODEV; |
| 531 goto out_err; |
| 532 } |
| 533 |
| 534 /* read four bytes from DID_VID register */ |
| 535 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) { |
| 536 rc = -EIO; |
| 537 goto out_release; |
| 538 } |
| 539 |
| 540 /* create DID_VID register value, after swapping to little-endian */ |
| 541 vendor = be32_to_cpu(vendor); |
| 542 |
| 543 if (vendor != TPM_TIS_I2C_DID_VID) { |
| 544 rc = -ENODEV; |
| 545 goto out_release; |
| 546 } |
| 547 |
| 548 dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); |
| 549 |
| 550 /* |
| 551 * A timeout query to TPM can be placed here. |
| 552 * Standard timeout values are used so far |
| 553 */ |
| 554 |
| 555 return 0; |
| 556 |
| 557 out_release: |
| 558 release_locality(chip, 0, 1); |
| 559 |
| 560 out_err: |
| 561 tpm_dev.addr = old_addr; |
| 562 return rc; |
| 563 } |
| 564 |
| 565 void tpm_vendor_cleanup(struct tpm_chip *chip) |
| 566 { |
| 567 release_locality(chip, chip->vendor.locality, 1); |
| 568 } |
OLD | NEW |