tpm_tis_spi.c 6.7 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/module.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/gpio.h>
  37. #include <linux/of_irq.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/tpm.h>
  40. #include "tpm.h"
  41. #include "tpm_tis_core.h"
  42. #define MAX_SPI_FRAMESIZE 64
  43. struct tpm_tis_spi_phy {
  44. struct tpm_tis_data priv;
  45. struct spi_device *spi_device;
  46. u8 tx_buf[MAX_SPI_FRAMESIZE + 4];
  47. u8 rx_buf[MAX_SPI_FRAMESIZE + 4];
  48. };
  49. static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
  50. {
  51. return container_of(data, struct tpm_tis_spi_phy, priv);
  52. }
  53. static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
  54. u16 len, u8 *result)
  55. {
  56. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  57. int ret, i;
  58. struct spi_message m;
  59. struct spi_transfer spi_xfer = {
  60. .tx_buf = phy->tx_buf,
  61. .rx_buf = phy->rx_buf,
  62. .len = 4,
  63. };
  64. if (len > MAX_SPI_FRAMESIZE)
  65. return -ENOMEM;
  66. phy->tx_buf[0] = 0x80 | (len - 1);
  67. phy->tx_buf[1] = 0xd4;
  68. phy->tx_buf[2] = (addr >> 8) & 0xFF;
  69. phy->tx_buf[3] = addr & 0xFF;
  70. spi_xfer.cs_change = 1;
  71. spi_message_init(&m);
  72. spi_message_add_tail(&spi_xfer, &m);
  73. spi_bus_lock(phy->spi_device->master);
  74. ret = spi_sync_locked(phy->spi_device, &m);
  75. if (ret < 0)
  76. goto exit;
  77. memset(phy->tx_buf, 0, len);
  78. /* According to TCG PTP specification, if there is no TPM present at
  79. * all, then the design has a weak pull-up on MISO. If a TPM is not
  80. * present, a pull-up on MISO means that the SB controller sees a 1,
  81. * and will latch in 0xFF on the read.
  82. */
  83. for (i = 0; (phy->rx_buf[0] & 0x01) == 0 && i < TPM_RETRY; i++) {
  84. spi_xfer.len = 1;
  85. spi_message_init(&m);
  86. spi_message_add_tail(&spi_xfer, &m);
  87. ret = spi_sync_locked(phy->spi_device, &m);
  88. if (ret < 0)
  89. goto exit;
  90. }
  91. spi_xfer.cs_change = 0;
  92. spi_xfer.len = len;
  93. spi_xfer.rx_buf = result;
  94. spi_message_init(&m);
  95. spi_message_add_tail(&spi_xfer, &m);
  96. ret = spi_sync_locked(phy->spi_device, &m);
  97. exit:
  98. spi_bus_unlock(phy->spi_device->master);
  99. return ret;
  100. }
  101. static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  102. u16 len, u8 *value)
  103. {
  104. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  105. int ret, i;
  106. struct spi_message m;
  107. struct spi_transfer spi_xfer = {
  108. .tx_buf = phy->tx_buf,
  109. .rx_buf = phy->rx_buf,
  110. .len = 4,
  111. };
  112. if (len > MAX_SPI_FRAMESIZE)
  113. return -ENOMEM;
  114. phy->tx_buf[0] = len - 1;
  115. phy->tx_buf[1] = 0xd4;
  116. phy->tx_buf[2] = (addr >> 8) & 0xFF;
  117. phy->tx_buf[3] = addr & 0xFF;
  118. spi_xfer.cs_change = 1;
  119. spi_message_init(&m);
  120. spi_message_add_tail(&spi_xfer, &m);
  121. spi_bus_lock(phy->spi_device->master);
  122. ret = spi_sync_locked(phy->spi_device, &m);
  123. if (ret < 0)
  124. goto exit;
  125. memset(phy->tx_buf, 0, len);
  126. /* According to TCG PTP specification, if there is no TPM present at
  127. * all, then the design has a weak pull-up on MISO. If a TPM is not
  128. * present, a pull-up on MISO means that the SB controller sees a 1,
  129. * and will latch in 0xFF on the read.
  130. */
  131. for (i = 0; (phy->rx_buf[0] & 0x01) == 0 && i < TPM_RETRY; i++) {
  132. spi_xfer.len = 1;
  133. spi_message_init(&m);
  134. spi_message_add_tail(&spi_xfer, &m);
  135. ret = spi_sync_locked(phy->spi_device, &m);
  136. if (ret < 0)
  137. goto exit;
  138. }
  139. spi_xfer.len = len;
  140. spi_xfer.tx_buf = value;
  141. spi_xfer.cs_change = 0;
  142. spi_xfer.tx_buf = value;
  143. spi_message_init(&m);
  144. spi_message_add_tail(&spi_xfer, &m);
  145. ret = spi_sync_locked(phy->spi_device, &m);
  146. exit:
  147. spi_bus_unlock(phy->spi_device->master);
  148. return ret;
  149. }
  150. static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  151. {
  152. int rc;
  153. rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), (u8 *)result);
  154. if (!rc)
  155. *result = le16_to_cpu(*result);
  156. return rc;
  157. }
  158. static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  159. {
  160. int rc;
  161. rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), (u8 *)result);
  162. if (!rc)
  163. *result = le32_to_cpu(*result);
  164. return rc;
  165. }
  166. static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  167. {
  168. value = cpu_to_le32(value);
  169. return data->phy_ops->write_bytes(data, addr, sizeof(u32),
  170. (u8 *)&value);
  171. }
  172. static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
  173. .read_bytes = tpm_tis_spi_read_bytes,
  174. .write_bytes = tpm_tis_spi_write_bytes,
  175. .read16 = tpm_tis_spi_read16,
  176. .read32 = tpm_tis_spi_read32,
  177. .write32 = tpm_tis_spi_write32,
  178. };
  179. static int tpm_tis_spi_probe(struct spi_device *dev)
  180. {
  181. struct tpm_tis_spi_phy *phy;
  182. phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
  183. GFP_KERNEL);
  184. if (!phy)
  185. return -ENOMEM;
  186. phy->spi_device = dev;
  187. return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_spi_phy_ops,
  188. NULL);
  189. }
  190. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  191. static int tpm_tis_spi_remove(struct spi_device *dev)
  192. {
  193. struct tpm_chip *chip = spi_get_drvdata(dev);
  194. tpm_chip_unregister(chip);
  195. tpm_tis_remove(chip);
  196. return 0;
  197. }
  198. static const struct spi_device_id tpm_tis_spi_id[] = {
  199. {"tpm_tis_spi", 0},
  200. {}
  201. };
  202. MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
  203. static const struct of_device_id of_tis_spi_match[] = {
  204. { .compatible = "st,st33htpm-spi", },
  205. { .compatible = "infineon,slb9670", },
  206. { .compatible = "tcg,tpm_tis-spi", },
  207. {}
  208. };
  209. MODULE_DEVICE_TABLE(of, of_tis_spi_match);
  210. static const struct acpi_device_id acpi_tis_spi_match[] = {
  211. {"SMO0768", 0},
  212. {}
  213. };
  214. MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
  215. static struct spi_driver tpm_tis_spi_driver = {
  216. .driver = {
  217. .owner = THIS_MODULE,
  218. .name = "tpm_tis_spi",
  219. .pm = &tpm_tis_pm,
  220. .of_match_table = of_match_ptr(of_tis_spi_match),
  221. .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
  222. },
  223. .probe = tpm_tis_spi_probe,
  224. .remove = tpm_tis_spi_remove,
  225. .id_table = tpm_tis_spi_id,
  226. };
  227. module_spi_driver(tpm_tis_spi_driver);
  228. MODULE_DESCRIPTION("TPM Driver for native SPI access");
  229. MODULE_LICENSE("GPL");