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