tpm_tis_spi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2015 Infineon Technologies AG
  3. * Copyright (C) 2016 STMicroelectronics SAS
  4. *
  5. * Authors:
  6. * Peter Huewe <peter.huewe@infineon.com>
  7. * Christophe Ricard <christophe-h.ricard@st.com>
  8. *
  9. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  10. *
  11. * Device driver for TCG/TCPA TPM (trusted platform module).
  12. * Specifications at www.trustedcomputinggroup.org
  13. *
  14. * This device driver implements the TPM interface as defined in
  15. * the TCG TPM Interface Spec version 1.3, revision 27 via _raw/native
  16. * SPI access_.
  17. *
  18. * It is based on the original tpm_tis device driver from Leendert van
  19. * Dorn and Kyleen Hall and Jarko Sakkinnen.
  20. *
  21. * This program is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation, version 2 of the
  24. * License.
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/wait.h>
  32. #include <linux/acpi.h>
  33. #include <linux/freezer.h>
  34. #include <linux/spi/spi.h>
  35. #include <linux/gpio.h>
  36. #include <linux/of_irq.h>
  37. #include <linux/of_gpio.h>
  38. #include <linux/tpm.h>
  39. #include "tpm.h"
  40. #include "tpm_tis_core.h"
  41. #define MAX_SPI_FRAMESIZE 64
  42. struct tpm_tis_spi_phy {
  43. struct tpm_tis_data priv;
  44. struct spi_device *spi_device;
  45. u8 *iobuf;
  46. };
  47. static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
  48. {
  49. return container_of(data, struct tpm_tis_spi_phy, priv);
  50. }
  51. static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
  52. u8 *in, const u8 *out)
  53. {
  54. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  55. int ret = 0;
  56. int i;
  57. struct spi_message m;
  58. struct spi_transfer spi_xfer;
  59. u8 transfer_len;
  60. spi_bus_lock(phy->spi_device->master);
  61. while (len) {
  62. transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
  63. phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
  64. phy->iobuf[1] = 0xd4;
  65. phy->iobuf[2] = addr >> 8;
  66. phy->iobuf[3] = addr;
  67. memset(&spi_xfer, 0, sizeof(spi_xfer));
  68. spi_xfer.tx_buf = phy->iobuf;
  69. spi_xfer.rx_buf = phy->iobuf;
  70. spi_xfer.len = 4;
  71. spi_xfer.cs_change = 1;
  72. spi_message_init(&m);
  73. spi_message_add_tail(&spi_xfer, &m);
  74. ret = spi_sync_locked(phy->spi_device, &m);
  75. if (ret < 0)
  76. goto exit;
  77. if ((phy->iobuf[3] & 0x01) == 0) {
  78. // handle SPI wait states
  79. phy->iobuf[0] = 0;
  80. for (i = 0; i < TPM_RETRY; i++) {
  81. spi_xfer.len = 1;
  82. spi_message_init(&m);
  83. spi_message_add_tail(&spi_xfer, &m);
  84. ret = spi_sync_locked(phy->spi_device, &m);
  85. if (ret < 0)
  86. goto exit;
  87. if (phy->iobuf[0] & 0x01)
  88. break;
  89. }
  90. if (i == TPM_RETRY) {
  91. ret = -ETIMEDOUT;
  92. goto exit;
  93. }
  94. }
  95. spi_xfer.cs_change = 0;
  96. spi_xfer.len = transfer_len;
  97. spi_xfer.delay_usecs = 5;
  98. if (in) {
  99. spi_xfer.tx_buf = NULL;
  100. } else if (out) {
  101. spi_xfer.rx_buf = NULL;
  102. memcpy(phy->iobuf, out, transfer_len);
  103. out += transfer_len;
  104. }
  105. spi_message_init(&m);
  106. spi_message_add_tail(&spi_xfer, &m);
  107. ret = spi_sync_locked(phy->spi_device, &m);
  108. if (ret < 0)
  109. goto exit;
  110. if (in) {
  111. memcpy(in, phy->iobuf, transfer_len);
  112. in += transfer_len;
  113. }
  114. len -= transfer_len;
  115. }
  116. exit:
  117. spi_bus_unlock(phy->spi_device->master);
  118. return ret;
  119. }
  120. static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
  121. u16 len, u8 *result)
  122. {
  123. return tpm_tis_spi_transfer(data, addr, len, result, NULL);
  124. }
  125. static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  126. u16 len, const u8 *value)
  127. {
  128. return tpm_tis_spi_transfer(data, addr, len, NULL, value);
  129. }
  130. static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  131. {
  132. __le16 result_le;
  133. int rc;
  134. rc = data->phy_ops->read_bytes(data, addr, sizeof(u16),
  135. (u8 *)&result_le);
  136. if (!rc)
  137. *result = le16_to_cpu(result_le);
  138. return rc;
  139. }
  140. static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  141. {
  142. __le32 result_le;
  143. int rc;
  144. rc = data->phy_ops->read_bytes(data, addr, sizeof(u32),
  145. (u8 *)&result_le);
  146. if (!rc)
  147. *result = le32_to_cpu(result_le);
  148. return rc;
  149. }
  150. static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  151. {
  152. __le32 value_le;
  153. int rc;
  154. value_le = cpu_to_le32(value);
  155. rc = data->phy_ops->write_bytes(data, addr, sizeof(u32),
  156. (u8 *)&value_le);
  157. return rc;
  158. }
  159. static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
  160. .read_bytes = tpm_tis_spi_read_bytes,
  161. .write_bytes = tpm_tis_spi_write_bytes,
  162. .read16 = tpm_tis_spi_read16,
  163. .read32 = tpm_tis_spi_read32,
  164. .write32 = tpm_tis_spi_write32,
  165. };
  166. static int tpm_tis_spi_probe(struct spi_device *dev)
  167. {
  168. struct tpm_tis_spi_phy *phy;
  169. int irq;
  170. phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
  171. GFP_KERNEL);
  172. if (!phy)
  173. return -ENOMEM;
  174. phy->spi_device = dev;
  175. phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
  176. if (!phy->iobuf)
  177. return -ENOMEM;
  178. /* If the SPI device has an IRQ then use that */
  179. if (dev->irq > 0)
  180. irq = dev->irq;
  181. else
  182. irq = -1;
  183. return tpm_tis_core_init(&dev->dev, &phy->priv, irq, &tpm_spi_phy_ops,
  184. NULL);
  185. }
  186. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  187. static int tpm_tis_spi_remove(struct spi_device *dev)
  188. {
  189. struct tpm_chip *chip = spi_get_drvdata(dev);
  190. tpm_chip_unregister(chip);
  191. tpm_tis_remove(chip);
  192. return 0;
  193. }
  194. static const struct spi_device_id tpm_tis_spi_id[] = {
  195. {"tpm_tis_spi", 0},
  196. {}
  197. };
  198. MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
  199. static const struct of_device_id of_tis_spi_match[] = {
  200. { .compatible = "st,st33htpm-spi", },
  201. { .compatible = "infineon,slb9670", },
  202. { .compatible = "tcg,tpm_tis-spi", },
  203. {}
  204. };
  205. MODULE_DEVICE_TABLE(of, of_tis_spi_match);
  206. static const struct acpi_device_id acpi_tis_spi_match[] = {
  207. {"SMO0768", 0},
  208. {}
  209. };
  210. MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
  211. static struct spi_driver tpm_tis_spi_driver = {
  212. .driver = {
  213. .owner = THIS_MODULE,
  214. .name = "tpm_tis_spi",
  215. .pm = &tpm_tis_pm,
  216. .of_match_table = of_match_ptr(of_tis_spi_match),
  217. .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
  218. },
  219. .probe = tpm_tis_spi_probe,
  220. .remove = tpm_tis_spi_remove,
  221. .id_table = tpm_tis_spi_id,
  222. };
  223. module_spi_driver(tpm_tis_spi_driver);
  224. MODULE_DESCRIPTION("TPM Driver for native SPI access");
  225. MODULE_LICENSE("GPL");