tpm_tis_spi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 tx_buf[4];
  46. u8 rx_buf[4];
  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 *buffer, u8 direction)
  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->tx_buf[0] = direction | (transfer_len - 1);
  65. phy->tx_buf[1] = 0xd4;
  66. phy->tx_buf[2] = addr >> 8;
  67. phy->tx_buf[3] = addr;
  68. memset(&spi_xfer, 0, sizeof(spi_xfer));
  69. spi_xfer.tx_buf = phy->tx_buf;
  70. spi_xfer.rx_buf = phy->rx_buf;
  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->rx_buf[3] & 0x01) == 0) {
  79. // handle SPI wait states
  80. phy->tx_buf[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->rx_buf[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 (direction) {
  100. spi_xfer.tx_buf = NULL;
  101. spi_xfer.rx_buf = buffer;
  102. } else {
  103. spi_xfer.tx_buf = buffer;
  104. spi_xfer.rx_buf = NULL;
  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. len -= transfer_len;
  112. buffer += transfer_len;
  113. }
  114. exit:
  115. spi_bus_unlock(phy->spi_device->master);
  116. return ret;
  117. }
  118. static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
  119. u16 len, u8 *result)
  120. {
  121. return tpm_tis_spi_transfer(data, addr, len, result, 0x80);
  122. }
  123. static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  124. u16 len, u8 *value)
  125. {
  126. return tpm_tis_spi_transfer(data, addr, len, value, 0);
  127. }
  128. static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  129. {
  130. int rc;
  131. rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), (u8 *)result);
  132. if (!rc)
  133. *result = le16_to_cpu(*result);
  134. return rc;
  135. }
  136. static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  137. {
  138. int rc;
  139. rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), (u8 *)result);
  140. if (!rc)
  141. *result = le32_to_cpu(*result);
  142. return rc;
  143. }
  144. static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  145. {
  146. value = cpu_to_le32(value);
  147. return data->phy_ops->write_bytes(data, addr, sizeof(u32),
  148. (u8 *)&value);
  149. }
  150. static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
  151. .read_bytes = tpm_tis_spi_read_bytes,
  152. .write_bytes = tpm_tis_spi_write_bytes,
  153. .read16 = tpm_tis_spi_read16,
  154. .read32 = tpm_tis_spi_read32,
  155. .write32 = tpm_tis_spi_write32,
  156. };
  157. static int tpm_tis_spi_probe(struct spi_device *dev)
  158. {
  159. struct tpm_tis_spi_phy *phy;
  160. phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
  161. GFP_KERNEL);
  162. if (!phy)
  163. return -ENOMEM;
  164. phy->spi_device = dev;
  165. return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_spi_phy_ops,
  166. NULL);
  167. }
  168. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  169. static int tpm_tis_spi_remove(struct spi_device *dev)
  170. {
  171. struct tpm_chip *chip = spi_get_drvdata(dev);
  172. tpm_chip_unregister(chip);
  173. tpm_tis_remove(chip);
  174. return 0;
  175. }
  176. static const struct spi_device_id tpm_tis_spi_id[] = {
  177. {"tpm_tis_spi", 0},
  178. {}
  179. };
  180. MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
  181. static const struct of_device_id of_tis_spi_match[] = {
  182. { .compatible = "st,st33htpm-spi", },
  183. { .compatible = "infineon,slb9670", },
  184. { .compatible = "tcg,tpm_tis-spi", },
  185. {}
  186. };
  187. MODULE_DEVICE_TABLE(of, of_tis_spi_match);
  188. static const struct acpi_device_id acpi_tis_spi_match[] = {
  189. {"SMO0768", 0},
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
  193. static struct spi_driver tpm_tis_spi_driver = {
  194. .driver = {
  195. .owner = THIS_MODULE,
  196. .name = "tpm_tis_spi",
  197. .pm = &tpm_tis_pm,
  198. .of_match_table = of_match_ptr(of_tis_spi_match),
  199. .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
  200. },
  201. .probe = tpm_tis_spi_probe,
  202. .remove = tpm_tis_spi_remove,
  203. .id_table = tpm_tis_spi_id,
  204. };
  205. module_spi_driver(tpm_tis_spi_driver);
  206. MODULE_DESCRIPTION("TPM Driver for native SPI access");
  207. MODULE_LICENSE("GPL");