spi.c 11 KB

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