spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * STMicroelectronics TPM SPI 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. #include <linux/module.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/acpi.h>
  25. #include <linux/tpm.h>
  26. #include <linux/platform_data/st33zp24.h>
  27. #include "../tpm.h"
  28. #include "st33zp24.h"
  29. #define TPM_DATA_FIFO 0x24
  30. #define TPM_INTF_CAPABILITY 0x14
  31. #define TPM_DUMMY_BYTE 0x00
  32. #define MAX_SPI_LATENCY 15
  33. #define LOCALITY0 0
  34. #define ST33ZP24_OK 0x5A
  35. #define ST33ZP24_UNDEFINED_ERR 0x80
  36. #define ST33ZP24_BADLOCALITY 0x81
  37. #define ST33ZP24_TISREGISTER_UNKNOWN 0x82
  38. #define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83
  39. #define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84
  40. #define ST33ZP24_BAD_COMMAND_ORDER 0x85
  41. #define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86
  42. #define ST33ZP24_TPM_FIFO_OVERFLOW 0x89
  43. #define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A
  44. #define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B
  45. #define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90
  46. #define ST33ZP24_DUMMY_BYTES 0x00
  47. /*
  48. * TPM command can be up to 2048 byte, A TPM response can be up to
  49. * 1024 byte.
  50. * Between command and response, there are latency byte (up to 15
  51. * usually on st33zp24 2 are enough).
  52. *
  53. * Overall when sending a command and expecting an answer we need if
  54. * worst case:
  55. * 2048 (for the TPM command) + 1024 (for the TPM answer). We need
  56. * some latency byte before the answer is available (max 15).
  57. * We have 2048 + 1024 + 15.
  58. */
  59. #define ST33ZP24_SPI_BUFFER_SIZE (ST33ZP24_BUFSIZE + (ST33ZP24_BUFSIZE / 2) +\
  60. MAX_SPI_LATENCY)
  61. struct st33zp24_spi_phy {
  62. struct spi_device *spi_device;
  63. u8 tx_buf[ST33ZP24_SPI_BUFFER_SIZE];
  64. u8 rx_buf[ST33ZP24_SPI_BUFFER_SIZE];
  65. int io_lpcpd;
  66. int latency;
  67. };
  68. static int st33zp24_status_to_errno(u8 code)
  69. {
  70. switch (code) {
  71. case ST33ZP24_OK:
  72. return 0;
  73. case ST33ZP24_UNDEFINED_ERR:
  74. case ST33ZP24_BADLOCALITY:
  75. case ST33ZP24_TISREGISTER_UNKNOWN:
  76. case ST33ZP24_LOCALITY_NOT_ACTIVATED:
  77. case ST33ZP24_HASH_END_BEFORE_HASH_START:
  78. case ST33ZP24_BAD_COMMAND_ORDER:
  79. case ST33ZP24_UNEXPECTED_READ_FIFO:
  80. case ST33ZP24_UNEXPECTED_WRITE_FIFO:
  81. case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END:
  82. return -EPROTO;
  83. case ST33ZP24_INCORECT_RECEIVED_LENGTH:
  84. case ST33ZP24_TPM_FIFO_OVERFLOW:
  85. return -EMSGSIZE;
  86. case ST33ZP24_DUMMY_BYTES:
  87. return -ENOSYS;
  88. }
  89. return code;
  90. }
  91. /*
  92. * st33zp24_spi_send
  93. * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
  94. * @param: phy_id, the phy description
  95. * @param: tpm_register, the tpm tis register where the data should be written
  96. * @param: tpm_data, the tpm_data to write inside the tpm_register
  97. * @param: tpm_size, The length of the data
  98. * @return: should be zero if success else a negative error code.
  99. */
  100. static int st33zp24_spi_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
  101. int tpm_size)
  102. {
  103. int total_length = 0, ret = 0;
  104. struct st33zp24_spi_phy *phy = phy_id;
  105. struct spi_device *dev = phy->spi_device;
  106. struct spi_transfer spi_xfer = {
  107. .tx_buf = phy->tx_buf,
  108. .rx_buf = phy->rx_buf,
  109. };
  110. /* Pre-Header */
  111. phy->tx_buf[total_length++] = TPM_WRITE_DIRECTION | LOCALITY0;
  112. phy->tx_buf[total_length++] = tpm_register;
  113. if (tpm_size > 0 && tpm_register == TPM_DATA_FIFO) {
  114. phy->tx_buf[total_length++] = tpm_size >> 8;
  115. phy->tx_buf[total_length++] = tpm_size;
  116. }
  117. memcpy(&phy->tx_buf[total_length], tpm_data, tpm_size);
  118. total_length += tpm_size;
  119. memset(&phy->tx_buf[total_length], TPM_DUMMY_BYTE, phy->latency);
  120. spi_xfer.len = total_length + phy->latency;
  121. ret = spi_sync_transfer(dev, &spi_xfer, 1);
  122. if (ret == 0)
  123. ret = phy->rx_buf[total_length + phy->latency - 1];
  124. return st33zp24_status_to_errno(ret);
  125. } /* st33zp24_spi_send() */
  126. /*
  127. * st33zp24_spi_read8_recv
  128. * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
  129. * @param: phy_id, the phy description
  130. * @param: tpm_register, the tpm tis register where the data should be read
  131. * @param: tpm_data, the TPM response
  132. * @param: tpm_size, tpm TPM response size to read.
  133. * @return: should be zero if success else a negative error code.
  134. */
  135. static int st33zp24_spi_read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data,
  136. int tpm_size)
  137. {
  138. int total_length = 0, ret;
  139. struct st33zp24_spi_phy *phy = phy_id;
  140. struct spi_device *dev = phy->spi_device;
  141. struct spi_transfer spi_xfer = {
  142. .tx_buf = phy->tx_buf,
  143. .rx_buf = phy->rx_buf,
  144. };
  145. /* Pre-Header */
  146. phy->tx_buf[total_length++] = LOCALITY0;
  147. phy->tx_buf[total_length++] = tpm_register;
  148. memset(&phy->tx_buf[total_length], TPM_DUMMY_BYTE,
  149. phy->latency + tpm_size);
  150. spi_xfer.len = total_length + phy->latency + tpm_size;
  151. /* header + status byte + size of the data + status byte */
  152. ret = spi_sync_transfer(dev, &spi_xfer, 1);
  153. if (tpm_size > 0 && ret == 0) {
  154. ret = phy->rx_buf[total_length + phy->latency - 1];
  155. memcpy(tpm_data, phy->rx_buf + total_length + phy->latency,
  156. tpm_size);
  157. }
  158. return ret;
  159. } /* st33zp24_spi_read8_reg() */
  160. /*
  161. * st33zp24_spi_recv
  162. * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
  163. * @param: phy_id, the phy description
  164. * @param: tpm_register, the tpm tis register where the data should be read
  165. * @param: tpm_data, the TPM response
  166. * @param: tpm_size, tpm TPM response size to read.
  167. * @return: number of byte read successfully: should be one if success.
  168. */
  169. static int st33zp24_spi_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
  170. int tpm_size)
  171. {
  172. int ret;
  173. ret = st33zp24_spi_read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
  174. if (!st33zp24_status_to_errno(ret))
  175. return tpm_size;
  176. return ret;
  177. } /* st33zp24_spi_recv() */
  178. static int st33zp24_spi_evaluate_latency(void *phy_id)
  179. {
  180. struct st33zp24_spi_phy *phy = phy_id;
  181. int latency = 1, status = 0;
  182. u8 data = 0;
  183. while (!status && latency < MAX_SPI_LATENCY) {
  184. phy->latency = latency;
  185. status = st33zp24_spi_read8_reg(phy_id, TPM_INTF_CAPABILITY,
  186. &data, 1);
  187. latency++;
  188. }
  189. if (status < 0)
  190. return status;
  191. if (latency == MAX_SPI_LATENCY)
  192. return -ENODEV;
  193. return latency - 1;
  194. } /* evaluate_latency() */
  195. static const struct st33zp24_phy_ops spi_phy_ops = {
  196. .send = st33zp24_spi_send,
  197. .recv = st33zp24_spi_recv,
  198. };
  199. static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
  200. static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
  201. { "lpcpd-gpios", &lpcpd_gpios, 1 },
  202. {},
  203. };
  204. static int st33zp24_spi_acpi_request_resources(struct spi_device *spi_dev)
  205. {
  206. struct tpm_chip *chip = spi_get_drvdata(spi_dev);
  207. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  208. struct st33zp24_spi_phy *phy = tpm_dev->phy_id;
  209. struct gpio_desc *gpiod_lpcpd;
  210. struct device *dev = &spi_dev->dev;
  211. int ret;
  212. ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
  213. if (ret)
  214. return ret;
  215. /* Get LPCPD GPIO from ACPI */
  216. gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
  217. if (IS_ERR(gpiod_lpcpd)) {
  218. dev_err(dev, "Failed to retrieve lpcpd-gpios from acpi.\n");
  219. phy->io_lpcpd = -1;
  220. /*
  221. * lpcpd pin is not specified. This is not an issue as
  222. * power management can be also managed by TPM specific
  223. * commands. So leave with a success status code.
  224. */
  225. return 0;
  226. }
  227. phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
  228. return 0;
  229. }
  230. static int st33zp24_spi_of_request_resources(struct spi_device *spi_dev)
  231. {
  232. struct tpm_chip *chip = spi_get_drvdata(spi_dev);
  233. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  234. struct st33zp24_spi_phy *phy = tpm_dev->phy_id;
  235. struct device_node *pp;
  236. int gpio;
  237. int ret;
  238. pp = spi_dev->dev.of_node;
  239. if (!pp) {
  240. dev_err(&spi_dev->dev, "No platform data\n");
  241. return -ENODEV;
  242. }
  243. /* Get GPIO from device tree */
  244. gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
  245. if (gpio < 0) {
  246. dev_err(&spi_dev->dev,
  247. "Failed to retrieve lpcpd-gpios from dts.\n");
  248. phy->io_lpcpd = -1;
  249. /*
  250. * lpcpd pin is not specified. This is not an issue as
  251. * power management can be also managed by TPM specific
  252. * commands. So leave with a success status code.
  253. */
  254. return 0;
  255. }
  256. /* GPIO request and configuration */
  257. ret = devm_gpio_request_one(&spi_dev->dev, gpio,
  258. GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
  259. if (ret) {
  260. dev_err(&spi_dev->dev, "Failed to request lpcpd pin\n");
  261. return -ENODEV;
  262. }
  263. phy->io_lpcpd = gpio;
  264. return 0;
  265. }
  266. static int st33zp24_spi_request_resources(struct spi_device *dev)
  267. {
  268. struct tpm_chip *chip = spi_get_drvdata(dev);
  269. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  270. struct st33zp24_spi_phy *phy = tpm_dev->phy_id;
  271. struct st33zp24_platform_data *pdata;
  272. int ret;
  273. pdata = dev->dev.platform_data;
  274. if (!pdata) {
  275. dev_err(&dev->dev, "No platform data\n");
  276. return -ENODEV;
  277. }
  278. /* store for late use */
  279. phy->io_lpcpd = pdata->io_lpcpd;
  280. if (gpio_is_valid(pdata->io_lpcpd)) {
  281. ret = devm_gpio_request_one(&dev->dev,
  282. pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
  283. "TPM IO_LPCPD");
  284. if (ret) {
  285. dev_err(&dev->dev, "%s : reset gpio_request failed\n",
  286. __FILE__);
  287. return ret;
  288. }
  289. }
  290. return 0;
  291. }
  292. /*
  293. * st33zp24_spi_probe initialize the TPM device
  294. * @param: dev, the spi_device drescription (TPM SPI description).
  295. * @return: 0 in case of success.
  296. * or a negative value describing the error.
  297. */
  298. static int st33zp24_spi_probe(struct spi_device *dev)
  299. {
  300. int ret;
  301. struct st33zp24_platform_data *pdata;
  302. struct st33zp24_spi_phy *phy;
  303. /* Check SPI platform functionnalities */
  304. if (!dev) {
  305. pr_info("%s: dev is NULL. Device is not accessible.\n",
  306. __func__);
  307. return -ENODEV;
  308. }
  309. phy = devm_kzalloc(&dev->dev, sizeof(struct st33zp24_spi_phy),
  310. GFP_KERNEL);
  311. if (!phy)
  312. return -ENOMEM;
  313. phy->spi_device = dev;
  314. pdata = dev->dev.platform_data;
  315. if (!pdata && dev->dev.of_node) {
  316. ret = st33zp24_spi_of_request_resources(dev);
  317. if (ret)
  318. return ret;
  319. } else if (pdata) {
  320. ret = st33zp24_spi_request_resources(dev);
  321. if (ret)
  322. return ret;
  323. } else if (ACPI_HANDLE(&dev->dev)) {
  324. ret = st33zp24_spi_acpi_request_resources(dev);
  325. if (ret)
  326. return ret;
  327. }
  328. phy->latency = st33zp24_spi_evaluate_latency(phy);
  329. if (phy->latency <= 0)
  330. return -ENODEV;
  331. return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq,
  332. phy->io_lpcpd);
  333. }
  334. /*
  335. * st33zp24_spi_remove remove the TPM device
  336. * @param: client, the spi_device drescription (TPM SPI description).
  337. * @return: 0 in case of success.
  338. */
  339. static int st33zp24_spi_remove(struct spi_device *dev)
  340. {
  341. struct tpm_chip *chip = spi_get_drvdata(dev);
  342. int ret;
  343. ret = st33zp24_remove(chip);
  344. if (ret)
  345. return ret;
  346. return 0;
  347. }
  348. static const struct spi_device_id st33zp24_spi_id[] = {
  349. {TPM_ST33_SPI, 0},
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(spi, st33zp24_spi_id);
  353. static const struct of_device_id of_st33zp24_spi_match[] = {
  354. { .compatible = "st,st33zp24-spi", },
  355. {}
  356. };
  357. MODULE_DEVICE_TABLE(of, of_st33zp24_spi_match);
  358. static const struct acpi_device_id st33zp24_spi_acpi_match[] = {
  359. {"SMO3324"},
  360. {}
  361. };
  362. MODULE_DEVICE_TABLE(acpi, st33zp24_spi_acpi_match);
  363. static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops, st33zp24_pm_suspend,
  364. st33zp24_pm_resume);
  365. static struct spi_driver st33zp24_spi_driver = {
  366. .driver = {
  367. .name = TPM_ST33_SPI,
  368. .pm = &st33zp24_spi_ops,
  369. .of_match_table = of_match_ptr(of_st33zp24_spi_match),
  370. .acpi_match_table = ACPI_PTR(st33zp24_spi_acpi_match),
  371. },
  372. .probe = st33zp24_spi_probe,
  373. .remove = st33zp24_spi_remove,
  374. .id_table = st33zp24_spi_id,
  375. };
  376. module_spi_driver(st33zp24_spi_driver);
  377. MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
  378. MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver");
  379. MODULE_VERSION("1.3.0");
  380. MODULE_LICENSE("GPL");