spi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/of.h>
  9. #include <linux/acpi.h>
  10. #include <linux/tpm.h>
  11. #include "../tpm.h"
  12. #include "st33zp24.h"
  13. #define TPM_DATA_FIFO 0x24
  14. #define TPM_INTF_CAPABILITY 0x14
  15. #define TPM_DUMMY_BYTE 0x00
  16. #define MAX_SPI_LATENCY 15
  17. #define LOCALITY0 0
  18. #define ST33ZP24_OK 0x5A
  19. #define ST33ZP24_UNDEFINED_ERR 0x80
  20. #define ST33ZP24_BADLOCALITY 0x81
  21. #define ST33ZP24_TISREGISTER_UNKNOWN 0x82
  22. #define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83
  23. #define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84
  24. #define ST33ZP24_BAD_COMMAND_ORDER 0x85
  25. #define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86
  26. #define ST33ZP24_TPM_FIFO_OVERFLOW 0x89
  27. #define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A
  28. #define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B
  29. #define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90
  30. #define ST33ZP24_DUMMY_BYTES 0x00
  31. /*
  32. * TPM command can be up to 2048 byte, A TPM response can be up to
  33. * 1024 byte.
  34. * Between command and response, there are latency byte (up to 15
  35. * usually on st33zp24 2 are enough).
  36. *
  37. * Overall when sending a command and expecting an answer we need if
  38. * worst case:
  39. * 2048 (for the TPM command) + 1024 (for the TPM answer). We need
  40. * some latency byte before the answer is available (max 15).
  41. * We have 2048 + 1024 + 15.
  42. */
  43. #define ST33ZP24_SPI_BUFFER_SIZE (ST33ZP24_BUFSIZE + (ST33ZP24_BUFSIZE / 2) +\
  44. MAX_SPI_LATENCY)
  45. struct st33zp24_spi_phy {
  46. struct spi_device *spi_device;
  47. u8 tx_buf[ST33ZP24_SPI_BUFFER_SIZE];
  48. u8 rx_buf[ST33ZP24_SPI_BUFFER_SIZE];
  49. int latency;
  50. };
  51. static int st33zp24_status_to_errno(u8 code)
  52. {
  53. switch (code) {
  54. case ST33ZP24_OK:
  55. return 0;
  56. case ST33ZP24_UNDEFINED_ERR:
  57. case ST33ZP24_BADLOCALITY:
  58. case ST33ZP24_TISREGISTER_UNKNOWN:
  59. case ST33ZP24_LOCALITY_NOT_ACTIVATED:
  60. case ST33ZP24_HASH_END_BEFORE_HASH_START:
  61. case ST33ZP24_BAD_COMMAND_ORDER:
  62. case ST33ZP24_UNEXPECTED_READ_FIFO:
  63. case ST33ZP24_UNEXPECTED_WRITE_FIFO:
  64. case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END:
  65. return -EPROTO;
  66. case ST33ZP24_INCORECT_RECEIVED_LENGTH:
  67. case ST33ZP24_TPM_FIFO_OVERFLOW:
  68. return -EMSGSIZE;
  69. case ST33ZP24_DUMMY_BYTES:
  70. return -ENOSYS;
  71. }
  72. return code;
  73. }
  74. /*
  75. * st33zp24_spi_send
  76. * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
  77. * @param: phy_id, the phy description
  78. * @param: tpm_register, the tpm tis register where the data should be written
  79. * @param: tpm_data, the tpm_data to write inside the tpm_register
  80. * @param: tpm_size, The length of the data
  81. * @return: should be zero if success else a negative error code.
  82. */
  83. static int st33zp24_spi_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
  84. int tpm_size)
  85. {
  86. int total_length = 0, ret = 0;
  87. struct st33zp24_spi_phy *phy = phy_id;
  88. struct spi_device *dev = phy->spi_device;
  89. struct spi_transfer spi_xfer = {
  90. .tx_buf = phy->tx_buf,
  91. .rx_buf = phy->rx_buf,
  92. };
  93. /* Pre-Header */
  94. phy->tx_buf[total_length++] = TPM_WRITE_DIRECTION | LOCALITY0;
  95. phy->tx_buf[total_length++] = tpm_register;
  96. if (tpm_size > 0 && tpm_register == TPM_DATA_FIFO) {
  97. phy->tx_buf[total_length++] = tpm_size >> 8;
  98. phy->tx_buf[total_length++] = tpm_size;
  99. }
  100. memcpy(&phy->tx_buf[total_length], tpm_data, tpm_size);
  101. total_length += tpm_size;
  102. memset(&phy->tx_buf[total_length], TPM_DUMMY_BYTE, phy->latency);
  103. spi_xfer.len = total_length + phy->latency;
  104. ret = spi_sync_transfer(dev, &spi_xfer, 1);
  105. if (ret == 0)
  106. ret = phy->rx_buf[total_length + phy->latency - 1];
  107. return st33zp24_status_to_errno(ret);
  108. } /* st33zp24_spi_send() */
  109. /*
  110. * st33zp24_spi_read8_recv
  111. * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
  112. * @param: phy_id, the phy description
  113. * @param: tpm_register, the tpm tis register where the data should be read
  114. * @param: tpm_data, the TPM response
  115. * @param: tpm_size, tpm TPM response size to read.
  116. * @return: should be zero if success else a negative error code.
  117. */
  118. static int st33zp24_spi_read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data,
  119. int tpm_size)
  120. {
  121. int total_length = 0, ret;
  122. struct st33zp24_spi_phy *phy = phy_id;
  123. struct spi_device *dev = phy->spi_device;
  124. struct spi_transfer spi_xfer = {
  125. .tx_buf = phy->tx_buf,
  126. .rx_buf = phy->rx_buf,
  127. };
  128. /* Pre-Header */
  129. phy->tx_buf[total_length++] = LOCALITY0;
  130. phy->tx_buf[total_length++] = tpm_register;
  131. memset(&phy->tx_buf[total_length], TPM_DUMMY_BYTE,
  132. phy->latency + tpm_size);
  133. spi_xfer.len = total_length + phy->latency + tpm_size;
  134. /* header + status byte + size of the data + status byte */
  135. ret = spi_sync_transfer(dev, &spi_xfer, 1);
  136. if (tpm_size > 0 && ret == 0) {
  137. ret = phy->rx_buf[total_length + phy->latency - 1];
  138. memcpy(tpm_data, phy->rx_buf + total_length + phy->latency,
  139. tpm_size);
  140. }
  141. return ret;
  142. } /* st33zp24_spi_read8_reg() */
  143. /*
  144. * st33zp24_spi_recv
  145. * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
  146. * @param: phy_id, the phy description
  147. * @param: tpm_register, the tpm tis register where the data should be read
  148. * @param: tpm_data, the TPM response
  149. * @param: tpm_size, tpm TPM response size to read.
  150. * @return: number of byte read successfully: should be one if success.
  151. */
  152. static int st33zp24_spi_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
  153. int tpm_size)
  154. {
  155. int ret;
  156. ret = st33zp24_spi_read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
  157. if (!st33zp24_status_to_errno(ret))
  158. return tpm_size;
  159. return ret;
  160. } /* st33zp24_spi_recv() */
  161. static int st33zp24_spi_evaluate_latency(void *phy_id)
  162. {
  163. struct st33zp24_spi_phy *phy = phy_id;
  164. int latency = 1, status = 0;
  165. u8 data = 0;
  166. while (!status && latency < MAX_SPI_LATENCY) {
  167. phy->latency = latency;
  168. status = st33zp24_spi_read8_reg(phy_id, TPM_INTF_CAPABILITY,
  169. &data, 1);
  170. latency++;
  171. }
  172. if (status < 0)
  173. return status;
  174. if (latency == MAX_SPI_LATENCY)
  175. return -ENODEV;
  176. return latency - 1;
  177. } /* evaluate_latency() */
  178. static const struct st33zp24_phy_ops spi_phy_ops = {
  179. .send = st33zp24_spi_send,
  180. .recv = st33zp24_spi_recv,
  181. };
  182. /*
  183. * st33zp24_spi_probe initialize the TPM device
  184. * @param: dev, the spi_device description (TPM SPI description).
  185. * @return: 0 in case of success.
  186. * or a negative value describing the error.
  187. */
  188. static int st33zp24_spi_probe(struct spi_device *dev)
  189. {
  190. struct st33zp24_spi_phy *phy;
  191. phy = devm_kzalloc(&dev->dev, sizeof(struct st33zp24_spi_phy),
  192. GFP_KERNEL);
  193. if (!phy)
  194. return -ENOMEM;
  195. phy->spi_device = dev;
  196. phy->latency = st33zp24_spi_evaluate_latency(phy);
  197. if (phy->latency <= 0)
  198. return -ENODEV;
  199. return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq);
  200. }
  201. /*
  202. * st33zp24_spi_remove remove the TPM device
  203. * @param: client, the spi_device description (TPM SPI description).
  204. * @return: 0 in case of success.
  205. */
  206. static void st33zp24_spi_remove(struct spi_device *dev)
  207. {
  208. struct tpm_chip *chip = spi_get_drvdata(dev);
  209. st33zp24_remove(chip);
  210. }
  211. static const struct spi_device_id st33zp24_spi_id[] = {
  212. {TPM_ST33_SPI, 0},
  213. {}
  214. };
  215. MODULE_DEVICE_TABLE(spi, st33zp24_spi_id);
  216. static const struct of_device_id of_st33zp24_spi_match[] __maybe_unused = {
  217. { .compatible = "st,st33zp24-spi", },
  218. {}
  219. };
  220. MODULE_DEVICE_TABLE(of, of_st33zp24_spi_match);
  221. static const struct acpi_device_id st33zp24_spi_acpi_match[] __maybe_unused = {
  222. {"SMO3324"},
  223. {}
  224. };
  225. MODULE_DEVICE_TABLE(acpi, st33zp24_spi_acpi_match);
  226. static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops, st33zp24_pm_suspend,
  227. st33zp24_pm_resume);
  228. static struct spi_driver st33zp24_spi_driver = {
  229. .driver = {
  230. .name = "st33zp24-spi",
  231. .pm = &st33zp24_spi_ops,
  232. .of_match_table = of_match_ptr(of_st33zp24_spi_match),
  233. .acpi_match_table = ACPI_PTR(st33zp24_spi_acpi_match),
  234. },
  235. .probe = st33zp24_spi_probe,
  236. .remove = st33zp24_spi_remove,
  237. .id_table = st33zp24_spi_id,
  238. };
  239. module_spi_driver(st33zp24_spi_driver);
  240. MODULE_AUTHOR("TPM support <TPMsupport@list.st.com>");
  241. MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver");
  242. MODULE_VERSION("1.3.0");
  243. MODULE_LICENSE("GPL");