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