tpm_tis_spi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 *iobuf;
  47. };
  48. static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
  49. {
  50. return container_of(data, struct tpm_tis_spi_phy, priv);
  51. }
  52. static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
  53. u8 *in, const u8 *out)
  54. {
  55. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  56. int ret = 0;
  57. int i;
  58. struct spi_message m;
  59. struct spi_transfer spi_xfer;
  60. u8 transfer_len;
  61. spi_bus_lock(phy->spi_device->master);
  62. while (len) {
  63. transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
  64. phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
  65. phy->iobuf[1] = 0xd4;
  66. phy->iobuf[2] = addr >> 8;
  67. phy->iobuf[3] = addr;
  68. memset(&spi_xfer, 0, sizeof(spi_xfer));
  69. spi_xfer.tx_buf = phy->iobuf;
  70. spi_xfer.rx_buf = phy->iobuf;
  71. spi_xfer.len = 4;
  72. spi_xfer.cs_change = 1;
  73. spi_message_init(&m);
  74. spi_message_add_tail(&spi_xfer, &m);
  75. ret = spi_sync_locked(phy->spi_device, &m);
  76. if (ret < 0)
  77. goto exit;
  78. if ((phy->iobuf[3] & 0x01) == 0) {
  79. // handle SPI wait states
  80. phy->iobuf[0] = 0;
  81. for (i = 0; i < TPM_RETRY; i++) {
  82. spi_xfer.len = 1;
  83. spi_message_init(&m);
  84. spi_message_add_tail(&spi_xfer, &m);
  85. ret = spi_sync_locked(phy->spi_device, &m);
  86. if (ret < 0)
  87. goto exit;
  88. if (phy->iobuf[0] & 0x01)
  89. break;
  90. }
  91. if (i == TPM_RETRY) {
  92. ret = -ETIMEDOUT;
  93. goto exit;
  94. }
  95. }
  96. spi_xfer.cs_change = 0;
  97. spi_xfer.len = transfer_len;
  98. spi_xfer.delay_usecs = 5;
  99. if (in) {
  100. spi_xfer.tx_buf = NULL;
  101. } else if (out) {
  102. spi_xfer.rx_buf = NULL;
  103. memcpy(phy->iobuf, out, transfer_len);
  104. out += transfer_len;
  105. }
  106. spi_message_init(&m);
  107. spi_message_add_tail(&spi_xfer, &m);
  108. ret = spi_sync_locked(phy->spi_device, &m);
  109. if (ret < 0)
  110. goto exit;
  111. if (in) {
  112. memcpy(in, phy->iobuf, transfer_len);
  113. in += transfer_len;
  114. }
  115. len -= transfer_len;
  116. }
  117. exit:
  118. spi_bus_unlock(phy->spi_device->master);
  119. return ret;
  120. }
  121. static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
  122. u16 len, u8 *result)
  123. {
  124. return tpm_tis_spi_transfer(data, addr, len, result, NULL);
  125. }
  126. static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  127. u16 len, const u8 *value)
  128. {
  129. return tpm_tis_spi_transfer(data, addr, len, NULL, value);
  130. }
  131. static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  132. {
  133. int rc;
  134. rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), (u8 *)result);
  135. if (!rc)
  136. *result = le16_to_cpu(*result);
  137. return rc;
  138. }
  139. static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  140. {
  141. int rc;
  142. rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), (u8 *)result);
  143. if (!rc)
  144. *result = le32_to_cpu(*result);
  145. return rc;
  146. }
  147. static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  148. {
  149. value = cpu_to_le32(value);
  150. return data->phy_ops->write_bytes(data, addr, sizeof(u32),
  151. (u8 *)&value);
  152. }
  153. static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
  154. .read_bytes = tpm_tis_spi_read_bytes,
  155. .write_bytes = tpm_tis_spi_write_bytes,
  156. .read16 = tpm_tis_spi_read16,
  157. .read32 = tpm_tis_spi_read32,
  158. .write32 = tpm_tis_spi_write32,
  159. };
  160. static int tpm_tis_spi_probe(struct spi_device *dev)
  161. {
  162. struct tpm_tis_spi_phy *phy;
  163. phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
  164. GFP_KERNEL);
  165. if (!phy)
  166. return -ENOMEM;
  167. phy->spi_device = dev;
  168. phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
  169. if (!phy->iobuf)
  170. return -ENOMEM;
  171. return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_spi_phy_ops,
  172. NULL);
  173. }
  174. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  175. static int tpm_tis_spi_remove(struct spi_device *dev)
  176. {
  177. struct tpm_chip *chip = spi_get_drvdata(dev);
  178. tpm_chip_unregister(chip);
  179. tpm_tis_remove(chip);
  180. return 0;
  181. }
  182. static const struct spi_device_id tpm_tis_spi_id[] = {
  183. {"tpm_tis_spi", 0},
  184. {}
  185. };
  186. MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
  187. static const struct of_device_id of_tis_spi_match[] = {
  188. { .compatible = "st,st33htpm-spi", },
  189. { .compatible = "infineon,slb9670", },
  190. { .compatible = "tcg,tpm_tis-spi", },
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(of, of_tis_spi_match);
  194. static const struct acpi_device_id acpi_tis_spi_match[] = {
  195. {"SMO0768", 0},
  196. {}
  197. };
  198. MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
  199. static struct spi_driver tpm_tis_spi_driver = {
  200. .driver = {
  201. .owner = THIS_MODULE,
  202. .name = "tpm_tis_spi",
  203. .pm = &tpm_tis_pm,
  204. .of_match_table = of_match_ptr(of_tis_spi_match),
  205. .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
  206. },
  207. .probe = tpm_tis_spi_probe,
  208. .remove = tpm_tis_spi_remove,
  209. .id_table = tpm_tis_spi_id,
  210. };
  211. module_spi_driver(tpm_tis_spi_driver);
  212. MODULE_DESCRIPTION("TPM Driver for native SPI access");
  213. MODULE_LICENSE("GPL");