st33zp24.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * STMicroelectronics TPM Linux driver for TPM ST33ZP24
  4. * Copyright (C) 2009 - 2016 STMicroelectronics
  5. */
  6. #include <linux/module.h>
  7. #include <linux/fs.h>
  8. #include <linux/kernel.h>
  9. #include <linux/delay.h>
  10. #include <linux/wait.h>
  11. #include <linux/freezer.h>
  12. #include <linux/string.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/gpio.h>
  15. #include <linux/sched.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #include "../tpm.h"
  20. #include "st33zp24.h"
  21. #define TPM_ACCESS 0x0
  22. #define TPM_STS 0x18
  23. #define TPM_DATA_FIFO 0x24
  24. #define TPM_INTF_CAPABILITY 0x14
  25. #define TPM_INT_STATUS 0x10
  26. #define TPM_INT_ENABLE 0x08
  27. #define LOCALITY0 0
  28. enum st33zp24_access {
  29. TPM_ACCESS_VALID = 0x80,
  30. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  31. TPM_ACCESS_REQUEST_PENDING = 0x04,
  32. TPM_ACCESS_REQUEST_USE = 0x02,
  33. };
  34. enum st33zp24_status {
  35. TPM_STS_VALID = 0x80,
  36. TPM_STS_COMMAND_READY = 0x40,
  37. TPM_STS_GO = 0x20,
  38. TPM_STS_DATA_AVAIL = 0x10,
  39. TPM_STS_DATA_EXPECT = 0x08,
  40. };
  41. enum st33zp24_int_flags {
  42. TPM_GLOBAL_INT_ENABLE = 0x80,
  43. TPM_INTF_CMD_READY_INT = 0x080,
  44. TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
  45. TPM_INTF_WAKE_UP_READY_INT = 0x020,
  46. TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
  47. TPM_INTF_STS_VALID_INT = 0x002,
  48. TPM_INTF_DATA_AVAIL_INT = 0x001,
  49. };
  50. enum tis_defaults {
  51. TIS_SHORT_TIMEOUT = 750,
  52. TIS_LONG_TIMEOUT = 2000,
  53. };
  54. /*
  55. * clear the pending interrupt.
  56. */
  57. static u8 clear_interruption(struct st33zp24_dev *tpm_dev)
  58. {
  59. u8 interrupt;
  60. tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
  61. tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
  62. return interrupt;
  63. }
  64. /*
  65. * cancel the current command execution or set STS to COMMAND READY.
  66. */
  67. static void st33zp24_cancel(struct tpm_chip *chip)
  68. {
  69. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  70. u8 data;
  71. data = TPM_STS_COMMAND_READY;
  72. tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
  73. }
  74. /*
  75. * return the TPM_STS register
  76. */
  77. static u8 st33zp24_status(struct tpm_chip *chip)
  78. {
  79. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  80. u8 data;
  81. tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
  82. return data;
  83. }
  84. /*
  85. * if the locality is active
  86. */
  87. static bool check_locality(struct tpm_chip *chip)
  88. {
  89. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  90. u8 data;
  91. u8 status;
  92. status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
  93. if (status && (data &
  94. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  95. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  96. return true;
  97. return false;
  98. }
  99. static int request_locality(struct tpm_chip *chip)
  100. {
  101. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  102. unsigned long stop;
  103. long ret;
  104. u8 data;
  105. if (check_locality(chip))
  106. return tpm_dev->locality;
  107. data = TPM_ACCESS_REQUEST_USE;
  108. ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
  109. if (ret < 0)
  110. return ret;
  111. stop = jiffies + chip->timeout_a;
  112. /* Request locality is usually effective after the request */
  113. do {
  114. if (check_locality(chip))
  115. return tpm_dev->locality;
  116. msleep(TPM_TIMEOUT);
  117. } while (time_before(jiffies, stop));
  118. /* could not get locality */
  119. return -EACCES;
  120. }
  121. static void release_locality(struct tpm_chip *chip)
  122. {
  123. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  124. u8 data;
  125. data = TPM_ACCESS_ACTIVE_LOCALITY;
  126. tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
  127. }
  128. /*
  129. * get_burstcount return the burstcount value
  130. */
  131. static int get_burstcount(struct tpm_chip *chip)
  132. {
  133. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  134. unsigned long stop;
  135. int burstcnt, status;
  136. u8 temp;
  137. stop = jiffies + chip->timeout_d;
  138. do {
  139. status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1,
  140. &temp, 1);
  141. if (status < 0)
  142. return -EBUSY;
  143. burstcnt = temp;
  144. status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2,
  145. &temp, 1);
  146. if (status < 0)
  147. return -EBUSY;
  148. burstcnt |= temp << 8;
  149. if (burstcnt)
  150. return burstcnt;
  151. msleep(TPM_TIMEOUT);
  152. } while (time_before(jiffies, stop));
  153. return -EBUSY;
  154. }
  155. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  156. bool check_cancel, bool *canceled)
  157. {
  158. u8 status = chip->ops->status(chip);
  159. *canceled = false;
  160. if ((status & mask) == mask)
  161. return true;
  162. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  163. *canceled = true;
  164. return true;
  165. }
  166. return false;
  167. }
  168. /*
  169. * wait for a TPM_STS value
  170. */
  171. static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  172. wait_queue_head_t *queue, bool check_cancel)
  173. {
  174. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  175. unsigned long stop;
  176. int ret = 0;
  177. bool canceled = false;
  178. bool condition;
  179. u32 cur_intrs;
  180. u8 status;
  181. /* check current status */
  182. status = st33zp24_status(chip);
  183. if ((status & mask) == mask)
  184. return 0;
  185. stop = jiffies + timeout;
  186. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  187. cur_intrs = tpm_dev->intrs;
  188. clear_interruption(tpm_dev);
  189. enable_irq(tpm_dev->irq);
  190. do {
  191. if (ret == -ERESTARTSYS && freezing(current))
  192. clear_thread_flag(TIF_SIGPENDING);
  193. timeout = stop - jiffies;
  194. if ((long) timeout <= 0)
  195. return -1;
  196. ret = wait_event_interruptible_timeout(*queue,
  197. cur_intrs != tpm_dev->intrs,
  198. timeout);
  199. clear_interruption(tpm_dev);
  200. condition = wait_for_tpm_stat_cond(chip, mask,
  201. check_cancel, &canceled);
  202. if (ret >= 0 && condition) {
  203. if (canceled)
  204. return -ECANCELED;
  205. return 0;
  206. }
  207. } while (ret == -ERESTARTSYS && freezing(current));
  208. disable_irq_nosync(tpm_dev->irq);
  209. } else {
  210. do {
  211. msleep(TPM_TIMEOUT);
  212. status = chip->ops->status(chip);
  213. if ((status & mask) == mask)
  214. return 0;
  215. } while (time_before(jiffies, stop));
  216. }
  217. return -ETIME;
  218. }
  219. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  220. {
  221. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  222. int size = 0, burstcnt, len, ret;
  223. while (size < count &&
  224. wait_for_stat(chip,
  225. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  226. chip->timeout_c,
  227. &tpm_dev->read_queue, true) == 0) {
  228. burstcnt = get_burstcount(chip);
  229. if (burstcnt < 0)
  230. return burstcnt;
  231. len = min_t(int, burstcnt, count - size);
  232. ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
  233. buf + size, len);
  234. if (ret < 0)
  235. return ret;
  236. size += len;
  237. }
  238. return size;
  239. }
  240. static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
  241. {
  242. struct tpm_chip *chip = dev_id;
  243. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  244. tpm_dev->intrs++;
  245. wake_up_interruptible(&tpm_dev->read_queue);
  246. disable_irq_nosync(tpm_dev->irq);
  247. return IRQ_HANDLED;
  248. }
  249. /*
  250. * send TPM commands through the I2C bus.
  251. */
  252. static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
  253. size_t len)
  254. {
  255. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  256. u32 status, i, size, ordinal;
  257. int burstcnt = 0;
  258. int ret;
  259. u8 data;
  260. if (len < TPM_HEADER_SIZE)
  261. return -EBUSY;
  262. ret = request_locality(chip);
  263. if (ret < 0)
  264. return ret;
  265. status = st33zp24_status(chip);
  266. if ((status & TPM_STS_COMMAND_READY) == 0) {
  267. st33zp24_cancel(chip);
  268. if (wait_for_stat
  269. (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
  270. &tpm_dev->read_queue, false) < 0) {
  271. ret = -ETIME;
  272. goto out_err;
  273. }
  274. }
  275. for (i = 0; i < len - 1;) {
  276. burstcnt = get_burstcount(chip);
  277. if (burstcnt < 0)
  278. return burstcnt;
  279. size = min_t(int, len - i - 1, burstcnt);
  280. ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
  281. buf + i, size);
  282. if (ret < 0)
  283. goto out_err;
  284. i += size;
  285. }
  286. status = st33zp24_status(chip);
  287. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  288. ret = -EIO;
  289. goto out_err;
  290. }
  291. ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
  292. buf + len - 1, 1);
  293. if (ret < 0)
  294. goto out_err;
  295. status = st33zp24_status(chip);
  296. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  297. ret = -EIO;
  298. goto out_err;
  299. }
  300. data = TPM_STS_GO;
  301. ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
  302. if (ret < 0)
  303. goto out_err;
  304. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  305. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  306. ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  307. tpm_calc_ordinal_duration(chip, ordinal),
  308. &tpm_dev->read_queue, false);
  309. if (ret < 0)
  310. goto out_err;
  311. }
  312. return 0;
  313. out_err:
  314. st33zp24_cancel(chip);
  315. release_locality(chip);
  316. return ret;
  317. }
  318. static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
  319. size_t count)
  320. {
  321. int size = 0;
  322. u32 expected;
  323. if (!chip)
  324. return -EBUSY;
  325. if (count < TPM_HEADER_SIZE) {
  326. size = -EIO;
  327. goto out;
  328. }
  329. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  330. if (size < TPM_HEADER_SIZE) {
  331. dev_err(&chip->dev, "Unable to read header\n");
  332. goto out;
  333. }
  334. expected = be32_to_cpu(*(__be32 *)(buf + 2));
  335. if (expected > count || expected < TPM_HEADER_SIZE) {
  336. size = -EIO;
  337. goto out;
  338. }
  339. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  340. expected - TPM_HEADER_SIZE);
  341. if (size < expected) {
  342. dev_err(&chip->dev, "Unable to read remainder of result\n");
  343. size = -ETIME;
  344. }
  345. out:
  346. st33zp24_cancel(chip);
  347. release_locality(chip);
  348. return size;
  349. }
  350. static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
  351. {
  352. return (status == TPM_STS_COMMAND_READY);
  353. }
  354. static const struct tpm_class_ops st33zp24_tpm = {
  355. .flags = TPM_OPS_AUTO_STARTUP,
  356. .send = st33zp24_send,
  357. .recv = st33zp24_recv,
  358. .cancel = st33zp24_cancel,
  359. .status = st33zp24_status,
  360. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  361. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  362. .req_canceled = st33zp24_req_canceled,
  363. };
  364. /*
  365. * initialize the TPM device
  366. */
  367. int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
  368. struct device *dev, int irq, int io_lpcpd)
  369. {
  370. int ret;
  371. u8 intmask = 0;
  372. struct tpm_chip *chip;
  373. struct st33zp24_dev *tpm_dev;
  374. chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
  375. if (IS_ERR(chip))
  376. return PTR_ERR(chip);
  377. tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
  378. GFP_KERNEL);
  379. if (!tpm_dev)
  380. return -ENOMEM;
  381. tpm_dev->phy_id = phy_id;
  382. tpm_dev->ops = ops;
  383. dev_set_drvdata(&chip->dev, tpm_dev);
  384. chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  385. chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  386. chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  387. chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  388. tpm_dev->locality = LOCALITY0;
  389. if (irq) {
  390. /* INTERRUPT Setup */
  391. init_waitqueue_head(&tpm_dev->read_queue);
  392. tpm_dev->intrs = 0;
  393. if (request_locality(chip) != LOCALITY0) {
  394. ret = -ENODEV;
  395. goto _tpm_clean_answer;
  396. }
  397. clear_interruption(tpm_dev);
  398. ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
  399. IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
  400. chip);
  401. if (ret < 0) {
  402. dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
  403. irq);
  404. goto _tpm_clean_answer;
  405. }
  406. intmask |= TPM_INTF_CMD_READY_INT
  407. | TPM_INTF_STS_VALID_INT
  408. | TPM_INTF_DATA_AVAIL_INT;
  409. ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
  410. &intmask, 1);
  411. if (ret < 0)
  412. goto _tpm_clean_answer;
  413. intmask = TPM_GLOBAL_INT_ENABLE;
  414. ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
  415. &intmask, 1);
  416. if (ret < 0)
  417. goto _tpm_clean_answer;
  418. tpm_dev->irq = irq;
  419. chip->flags |= TPM_CHIP_FLAG_IRQ;
  420. disable_irq_nosync(tpm_dev->irq);
  421. }
  422. return tpm_chip_register(chip);
  423. _tpm_clean_answer:
  424. dev_info(&chip->dev, "TPM initialization fail\n");
  425. return ret;
  426. }
  427. EXPORT_SYMBOL(st33zp24_probe);
  428. void st33zp24_remove(struct tpm_chip *chip)
  429. {
  430. tpm_chip_unregister(chip);
  431. }
  432. EXPORT_SYMBOL(st33zp24_remove);
  433. #ifdef CONFIG_PM_SLEEP
  434. int st33zp24_pm_suspend(struct device *dev)
  435. {
  436. struct tpm_chip *chip = dev_get_drvdata(dev);
  437. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  438. int ret = 0;
  439. if (gpio_is_valid(tpm_dev->io_lpcpd))
  440. gpio_set_value(tpm_dev->io_lpcpd, 0);
  441. else
  442. ret = tpm_pm_suspend(dev);
  443. return ret;
  444. }
  445. EXPORT_SYMBOL(st33zp24_pm_suspend);
  446. int st33zp24_pm_resume(struct device *dev)
  447. {
  448. struct tpm_chip *chip = dev_get_drvdata(dev);
  449. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  450. int ret = 0;
  451. if (gpio_is_valid(tpm_dev->io_lpcpd)) {
  452. gpio_set_value(tpm_dev->io_lpcpd, 1);
  453. ret = wait_for_stat(chip,
  454. TPM_STS_VALID, chip->timeout_b,
  455. &tpm_dev->read_queue, false);
  456. } else {
  457. ret = tpm_pm_resume(dev);
  458. if (!ret)
  459. tpm1_do_selftest(chip);
  460. }
  461. return ret;
  462. }
  463. EXPORT_SYMBOL(st33zp24_pm_resume);
  464. #endif
  465. MODULE_AUTHOR("TPM support ([email protected])");
  466. MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
  467. MODULE_VERSION("1.3.0");
  468. MODULE_LICENSE("GPL");