1 /* 2 * STMicroelectronics TPM Linux driver for TPM ST33ZP24 3 * Copyright (C) 2009 - 2016 STMicroelectronics 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include <linux/module.h> 20 #include <linux/fs.h> 21 #include <linux/miscdevice.h> 22 #include <linux/kernel.h> 23 #include <linux/delay.h> 24 #include <linux/wait.h> 25 #include <linux/freezer.h> 26 #include <linux/string.h> 27 #include <linux/interrupt.h> 28 #include <linux/gpio.h> 29 #include <linux/sched.h> 30 #include <linux/uaccess.h> 31 #include <linux/io.h> 32 #include <linux/slab.h> 33 34 #include "../tpm.h" 35 #include "st33zp24.h" 36 37 #define TPM_ACCESS 0x0 38 #define TPM_STS 0x18 39 #define TPM_DATA_FIFO 0x24 40 #define TPM_INTF_CAPABILITY 0x14 41 #define TPM_INT_STATUS 0x10 42 #define TPM_INT_ENABLE 0x08 43 44 #define LOCALITY0 0 45 46 enum st33zp24_access { 47 TPM_ACCESS_VALID = 0x80, 48 TPM_ACCESS_ACTIVE_LOCALITY = 0x20, 49 TPM_ACCESS_REQUEST_PENDING = 0x04, 50 TPM_ACCESS_REQUEST_USE = 0x02, 51 }; 52 53 enum st33zp24_status { 54 TPM_STS_VALID = 0x80, 55 TPM_STS_COMMAND_READY = 0x40, 56 TPM_STS_GO = 0x20, 57 TPM_STS_DATA_AVAIL = 0x10, 58 TPM_STS_DATA_EXPECT = 0x08, 59 }; 60 61 enum st33zp24_int_flags { 62 TPM_GLOBAL_INT_ENABLE = 0x80, 63 TPM_INTF_CMD_READY_INT = 0x080, 64 TPM_INTF_FIFO_AVALAIBLE_INT = 0x040, 65 TPM_INTF_WAKE_UP_READY_INT = 0x020, 66 TPM_INTF_LOCALITY_CHANGE_INT = 0x004, 67 TPM_INTF_STS_VALID_INT = 0x002, 68 TPM_INTF_DATA_AVAIL_INT = 0x001, 69 }; 70 71 enum tis_defaults { 72 TIS_SHORT_TIMEOUT = 750, 73 TIS_LONG_TIMEOUT = 2000, 74 }; 75 76 /* 77 * clear_interruption clear the pending interrupt. 78 * @param: tpm_dev, the tpm device device. 79 * @return: the interrupt status value. 80 */ 81 static u8 clear_interruption(struct st33zp24_dev *tpm_dev) 82 { 83 u8 interrupt; 84 85 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1); 86 tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1); 87 return interrupt; 88 } /* clear_interruption() */ 89 90 /* 91 * st33zp24_cancel, cancel the current command execution or 92 * set STS to COMMAND READY. 93 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h 94 */ 95 static void st33zp24_cancel(struct tpm_chip *chip) 96 { 97 struct st33zp24_dev *tpm_dev; 98 u8 data; 99 100 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 101 102 data = TPM_STS_COMMAND_READY; 103 tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1); 104 } /* st33zp24_cancel() */ 105 106 /* 107 * st33zp24_status return the TPM_STS register 108 * @param: chip, the tpm chip description 109 * @return: the TPM_STS register value. 110 */ 111 static u8 st33zp24_status(struct tpm_chip *chip) 112 { 113 struct st33zp24_dev *tpm_dev; 114 u8 data; 115 116 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 117 118 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1); 119 return data; 120 } /* st33zp24_status() */ 121 122 /* 123 * check_locality if the locality is active 124 * @param: chip, the tpm chip description 125 * @return: the active locality or -EACCESS. 126 */ 127 static int check_locality(struct tpm_chip *chip) 128 { 129 struct st33zp24_dev *tpm_dev; 130 u8 data; 131 u8 status; 132 133 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 134 135 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1); 136 if (status && (data & 137 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == 138 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) 139 return tpm_dev->locality; 140 141 return -EACCES; 142 } /* check_locality() */ 143 144 /* 145 * request_locality request the TPM locality 146 * @param: chip, the chip description 147 * @return: the active locality or negative value. 148 */ 149 static int request_locality(struct tpm_chip *chip) 150 { 151 unsigned long stop; 152 long ret; 153 struct st33zp24_dev *tpm_dev; 154 u8 data; 155 156 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 157 158 if (check_locality(chip) == tpm_dev->locality) 159 return tpm_dev->locality; 160 161 data = TPM_ACCESS_REQUEST_USE; 162 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1); 163 if (ret < 0) 164 return ret; 165 166 stop = jiffies + chip->timeout_a; 167 168 /* Request locality is usually effective after the request */ 169 do { 170 if (check_locality(chip) >= 0) 171 return tpm_dev->locality; 172 msleep(TPM_TIMEOUT); 173 } while (time_before(jiffies, stop)); 174 175 /* could not get locality */ 176 return -EACCES; 177 } /* request_locality() */ 178 179 /* 180 * release_locality release the active locality 181 * @param: chip, the tpm chip description. 182 */ 183 static void release_locality(struct tpm_chip *chip) 184 { 185 struct st33zp24_dev *tpm_dev; 186 u8 data; 187 188 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 189 data = TPM_ACCESS_ACTIVE_LOCALITY; 190 191 tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1); 192 } 193 194 /* 195 * get_burstcount return the burstcount value 196 * @param: chip, the chip description 197 * return: the burstcount or negative value. 198 */ 199 static int get_burstcount(struct tpm_chip *chip) 200 { 201 unsigned long stop; 202 int burstcnt, status; 203 u8 temp; 204 struct st33zp24_dev *tpm_dev; 205 206 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 207 208 stop = jiffies + chip->timeout_d; 209 do { 210 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1, 211 &temp, 1); 212 if (status < 0) 213 return -EBUSY; 214 215 burstcnt = temp; 216 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2, 217 &temp, 1); 218 if (status < 0) 219 return -EBUSY; 220 221 burstcnt |= temp << 8; 222 if (burstcnt) 223 return burstcnt; 224 msleep(TPM_TIMEOUT); 225 } while (time_before(jiffies, stop)); 226 return -EBUSY; 227 } /* get_burstcount() */ 228 229 230 /* 231 * wait_for_tpm_stat_cond 232 * @param: chip, chip description 233 * @param: mask, expected mask value 234 * @param: check_cancel, does the command expected to be canceled ? 235 * @param: canceled, did we received a cancel request ? 236 * @return: true if status == mask or if the command is canceled. 237 * false in other cases. 238 */ 239 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, 240 bool check_cancel, bool *canceled) 241 { 242 u8 status = chip->ops->status(chip); 243 244 *canceled = false; 245 if ((status & mask) == mask) 246 return true; 247 if (check_cancel && chip->ops->req_canceled(chip, status)) { 248 *canceled = true; 249 return true; 250 } 251 return false; 252 } 253 254 /* 255 * wait_for_stat wait for a TPM_STS value 256 * @param: chip, the tpm chip description 257 * @param: mask, the value mask to wait 258 * @param: timeout, the timeout 259 * @param: queue, the wait queue. 260 * @param: check_cancel, does the command can be cancelled ? 261 * @return: the tpm status, 0 if success, -ETIME if timeout is reached. 262 */ 263 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 264 wait_queue_head_t *queue, bool check_cancel) 265 { 266 unsigned long stop; 267 int ret = 0; 268 bool canceled = false; 269 bool condition; 270 u32 cur_intrs; 271 u8 status; 272 struct st33zp24_dev *tpm_dev; 273 274 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 275 276 /* check current status */ 277 status = st33zp24_status(chip); 278 if ((status & mask) == mask) 279 return 0; 280 281 stop = jiffies + timeout; 282 283 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 284 cur_intrs = tpm_dev->intrs; 285 clear_interruption(tpm_dev); 286 enable_irq(tpm_dev->irq); 287 288 do { 289 if (ret == -ERESTARTSYS && freezing(current)) 290 clear_thread_flag(TIF_SIGPENDING); 291 292 timeout = stop - jiffies; 293 if ((long) timeout <= 0) 294 return -1; 295 296 ret = wait_event_interruptible_timeout(*queue, 297 cur_intrs != tpm_dev->intrs, 298 timeout); 299 clear_interruption(tpm_dev); 300 condition = wait_for_tpm_stat_cond(chip, mask, 301 check_cancel, &canceled); 302 if (ret >= 0 && condition) { 303 if (canceled) 304 return -ECANCELED; 305 return 0; 306 } 307 } while (ret == -ERESTARTSYS && freezing(current)); 308 309 disable_irq_nosync(tpm_dev->irq); 310 311 } else { 312 do { 313 msleep(TPM_TIMEOUT); 314 status = chip->ops->status(chip); 315 if ((status & mask) == mask) 316 return 0; 317 } while (time_before(jiffies, stop)); 318 } 319 320 return -ETIME; 321 } /* wait_for_stat() */ 322 323 /* 324 * recv_data receive data 325 * @param: chip, the tpm chip description 326 * @param: buf, the buffer where the data are received 327 * @param: count, the number of data to receive 328 * @return: the number of bytes read from TPM FIFO. 329 */ 330 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) 331 { 332 int size = 0, burstcnt, len, ret; 333 struct st33zp24_dev *tpm_dev; 334 335 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 336 337 while (size < count && 338 wait_for_stat(chip, 339 TPM_STS_DATA_AVAIL | TPM_STS_VALID, 340 chip->timeout_c, 341 &tpm_dev->read_queue, true) == 0) { 342 burstcnt = get_burstcount(chip); 343 if (burstcnt < 0) 344 return burstcnt; 345 len = min_t(int, burstcnt, count - size); 346 ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO, 347 buf + size, len); 348 if (ret < 0) 349 return ret; 350 351 size += len; 352 } 353 return size; 354 } 355 356 /* 357 * tpm_ioserirq_handler the serirq irq handler 358 * @param: irq, the tpm chip description 359 * @param: dev_id, the description of the chip 360 * @return: the status of the handler. 361 */ 362 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) 363 { 364 struct tpm_chip *chip = dev_id; 365 struct st33zp24_dev *tpm_dev; 366 367 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 368 369 tpm_dev->intrs++; 370 wake_up_interruptible(&tpm_dev->read_queue); 371 disable_irq_nosync(tpm_dev->irq); 372 373 return IRQ_HANDLED; 374 } /* tpm_ioserirq_handler() */ 375 376 /* 377 * st33zp24_send send TPM commands through the I2C bus. 378 * 379 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h 380 * @param: buf, the buffer to send. 381 * @param: count, the number of bytes to send. 382 * @return: In case of success the number of bytes sent. 383 * In other case, a < 0 value describing the issue. 384 */ 385 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf, 386 size_t len) 387 { 388 u32 status, i, size, ordinal; 389 int burstcnt = 0; 390 int ret; 391 u8 data; 392 struct st33zp24_dev *tpm_dev; 393 394 if (!chip) 395 return -EBUSY; 396 if (len < TPM_HEADER_SIZE) 397 return -EBUSY; 398 399 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 400 401 ret = request_locality(chip); 402 if (ret < 0) 403 return ret; 404 405 status = st33zp24_status(chip); 406 if ((status & TPM_STS_COMMAND_READY) == 0) { 407 st33zp24_cancel(chip); 408 if (wait_for_stat 409 (chip, TPM_STS_COMMAND_READY, chip->timeout_b, 410 &tpm_dev->read_queue, false) < 0) { 411 ret = -ETIME; 412 goto out_err; 413 } 414 } 415 416 for (i = 0; i < len - 1;) { 417 burstcnt = get_burstcount(chip); 418 if (burstcnt < 0) 419 return burstcnt; 420 size = min_t(int, len - i - 1, burstcnt); 421 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO, 422 buf + i, size); 423 if (ret < 0) 424 goto out_err; 425 426 i += size; 427 } 428 429 status = st33zp24_status(chip); 430 if ((status & TPM_STS_DATA_EXPECT) == 0) { 431 ret = -EIO; 432 goto out_err; 433 } 434 435 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO, 436 buf + len - 1, 1); 437 if (ret < 0) 438 goto out_err; 439 440 status = st33zp24_status(chip); 441 if ((status & TPM_STS_DATA_EXPECT) != 0) { 442 ret = -EIO; 443 goto out_err; 444 } 445 446 data = TPM_STS_GO; 447 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1); 448 if (ret < 0) 449 goto out_err; 450 451 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 452 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 453 454 ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, 455 tpm_calc_ordinal_duration(chip, ordinal), 456 &tpm_dev->read_queue, false); 457 if (ret < 0) 458 goto out_err; 459 } 460 461 return len; 462 out_err: 463 st33zp24_cancel(chip); 464 release_locality(chip); 465 return ret; 466 } 467 468 /* 469 * st33zp24_recv received TPM response through TPM phy. 470 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. 471 * @param: buf, the buffer to store datas. 472 * @param: count, the number of bytes to send. 473 * @return: In case of success the number of bytes received. 474 * In other case, a < 0 value describing the issue. 475 */ 476 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf, 477 size_t count) 478 { 479 int size = 0; 480 int expected; 481 482 if (!chip) 483 return -EBUSY; 484 485 if (count < TPM_HEADER_SIZE) { 486 size = -EIO; 487 goto out; 488 } 489 490 size = recv_data(chip, buf, TPM_HEADER_SIZE); 491 if (size < TPM_HEADER_SIZE) { 492 dev_err(&chip->dev, "Unable to read header\n"); 493 goto out; 494 } 495 496 expected = be32_to_cpu(*(__be32 *)(buf + 2)); 497 if (expected > count) { 498 size = -EIO; 499 goto out; 500 } 501 502 size += recv_data(chip, &buf[TPM_HEADER_SIZE], 503 expected - TPM_HEADER_SIZE); 504 if (size < expected) { 505 dev_err(&chip->dev, "Unable to read remainder of result\n"); 506 size = -ETIME; 507 } 508 509 out: 510 st33zp24_cancel(chip); 511 release_locality(chip); 512 return size; 513 } 514 515 /* 516 * st33zp24_req_canceled 517 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. 518 * @param: status, the TPM status. 519 * @return: Does TPM ready to compute a new command ? true. 520 */ 521 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status) 522 { 523 return (status == TPM_STS_COMMAND_READY); 524 } 525 526 static const struct tpm_class_ops st33zp24_tpm = { 527 .send = st33zp24_send, 528 .recv = st33zp24_recv, 529 .cancel = st33zp24_cancel, 530 .status = st33zp24_status, 531 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 532 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 533 .req_canceled = st33zp24_req_canceled, 534 }; 535 536 /* 537 * st33zp24_probe initialize the TPM device 538 * @param: client, the i2c_client drescription (TPM I2C description). 539 * @param: id, the i2c_device_id struct. 540 * @return: 0 in case of success. 541 * -1 in other case. 542 */ 543 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops, 544 struct device *dev, int irq, int io_lpcpd) 545 { 546 int ret; 547 u8 intmask = 0; 548 struct tpm_chip *chip; 549 struct st33zp24_dev *tpm_dev; 550 551 chip = tpmm_chip_alloc(dev, &st33zp24_tpm); 552 if (IS_ERR(chip)) 553 return PTR_ERR(chip); 554 555 tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev), 556 GFP_KERNEL); 557 if (!tpm_dev) 558 return -ENOMEM; 559 560 TPM_VPRIV(chip) = tpm_dev; 561 tpm_dev->phy_id = phy_id; 562 tpm_dev->ops = ops; 563 564 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 565 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); 566 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 567 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 568 569 tpm_dev->locality = LOCALITY0; 570 571 if (irq) { 572 /* INTERRUPT Setup */ 573 init_waitqueue_head(&tpm_dev->read_queue); 574 tpm_dev->intrs = 0; 575 576 if (request_locality(chip) != LOCALITY0) { 577 ret = -ENODEV; 578 goto _tpm_clean_answer; 579 } 580 581 clear_interruption(tpm_dev); 582 ret = devm_request_irq(dev, irq, tpm_ioserirq_handler, 583 IRQF_TRIGGER_HIGH, "TPM SERIRQ management", 584 chip); 585 if (ret < 0) { 586 dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n", 587 irq); 588 goto _tpm_clean_answer; 589 } 590 591 intmask |= TPM_INTF_CMD_READY_INT 592 | TPM_INTF_STS_VALID_INT 593 | TPM_INTF_DATA_AVAIL_INT; 594 595 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE, 596 &intmask, 1); 597 if (ret < 0) 598 goto _tpm_clean_answer; 599 600 intmask = TPM_GLOBAL_INT_ENABLE; 601 ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3), 602 &intmask, 1); 603 if (ret < 0) 604 goto _tpm_clean_answer; 605 606 tpm_dev->irq = irq; 607 chip->flags |= TPM_CHIP_FLAG_IRQ; 608 609 disable_irq_nosync(tpm_dev->irq); 610 611 tpm_gen_interrupt(chip); 612 } 613 614 tpm_get_timeouts(chip); 615 tpm_do_selftest(chip); 616 617 return tpm_chip_register(chip); 618 _tpm_clean_answer: 619 dev_info(&chip->dev, "TPM initialization fail\n"); 620 return ret; 621 } 622 EXPORT_SYMBOL(st33zp24_probe); 623 624 /* 625 * st33zp24_remove remove the TPM device 626 * @param: tpm_data, the tpm phy. 627 * @return: 0 in case of success. 628 */ 629 int st33zp24_remove(struct tpm_chip *chip) 630 { 631 tpm_chip_unregister(chip); 632 return 0; 633 } 634 EXPORT_SYMBOL(st33zp24_remove); 635 636 #ifdef CONFIG_PM_SLEEP 637 /* 638 * st33zp24_pm_suspend suspend the TPM device 639 * @param: tpm_data, the tpm phy. 640 * @param: mesg, the power management message. 641 * @return: 0 in case of success. 642 */ 643 int st33zp24_pm_suspend(struct device *dev) 644 { 645 struct tpm_chip *chip = dev_get_drvdata(dev); 646 struct st33zp24_dev *tpm_dev; 647 int ret = 0; 648 649 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 650 651 if (gpio_is_valid(tpm_dev->io_lpcpd)) 652 gpio_set_value(tpm_dev->io_lpcpd, 0); 653 else 654 ret = tpm_pm_suspend(dev); 655 656 return ret; 657 } /* st33zp24_pm_suspend() */ 658 EXPORT_SYMBOL(st33zp24_pm_suspend); 659 660 /* 661 * st33zp24_pm_resume resume the TPM device 662 * @param: tpm_data, the tpm phy. 663 * @return: 0 in case of success. 664 */ 665 int st33zp24_pm_resume(struct device *dev) 666 { 667 struct tpm_chip *chip = dev_get_drvdata(dev); 668 struct st33zp24_dev *tpm_dev; 669 int ret = 0; 670 671 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip); 672 673 if (gpio_is_valid(tpm_dev->io_lpcpd)) { 674 gpio_set_value(tpm_dev->io_lpcpd, 1); 675 ret = wait_for_stat(chip, 676 TPM_STS_VALID, chip->timeout_b, 677 &tpm_dev->read_queue, false); 678 } else { 679 ret = tpm_pm_resume(dev); 680 if (!ret) 681 tpm_do_selftest(chip); 682 } 683 return ret; 684 } /* st33zp24_pm_resume() */ 685 EXPORT_SYMBOL(st33zp24_pm_resume); 686 #endif 687 688 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)"); 689 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver"); 690 MODULE_VERSION("1.3.0"); 691 MODULE_LICENSE("GPL"); 692