spi.c 11 KB

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