tpm_tis_spi_main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Infineon Technologies AG
  4. * Copyright (C) 2016 STMicroelectronics SAS
  5. *
  6. * Authors:
  7. * Peter Huewe <peter.huewe@infineon.com>
  8. * Christophe Ricard <christophe-h.ricard@st.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This device driver implements the TPM interface as defined in
  16. * the TCG TPM Interface Spec version 1.3, revision 27 via _raw/native
  17. * SPI access_.
  18. *
  19. * It is based on the original tpm_tis device driver from Leendert van
  20. * Dorn and Kyleen Hall and Jarko Sakkinnen.
  21. */
  22. #include <linux/acpi.h>
  23. #include <linux/completion.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/of.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/tpm.h>
  32. #include "tpm.h"
  33. #include "tpm_tis_core.h"
  34. #include "tpm_tis_spi.h"
  35. #define MAX_SPI_FRAMESIZE 64
  36. #define SPI_HDRSIZE 4
  37. /*
  38. * TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,
  39. * keep trying to read from the device until MISO goes high indicating the
  40. * wait state has ended.
  41. *
  42. * [1] https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
  43. */
  44. static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
  45. struct spi_transfer *spi_xfer)
  46. {
  47. struct spi_message m;
  48. int ret, i;
  49. if ((phy->iobuf[3] & 0x01) == 0) {
  50. // handle SPI wait states
  51. for (i = 0; i < TPM_RETRY; i++) {
  52. spi_xfer->len = 1;
  53. spi_message_init(&m);
  54. spi_message_add_tail(spi_xfer, &m);
  55. ret = spi_sync_locked(phy->spi_device, &m);
  56. if (ret < 0)
  57. return ret;
  58. if (phy->iobuf[0] & 0x01)
  59. break;
  60. }
  61. if (i == TPM_RETRY)
  62. return -ETIMEDOUT;
  63. }
  64. return 0;
  65. }
  66. /*
  67. * Half duplex controller with support for TPM wait state detection like
  68. * Tegra QSPI need CMD, ADDR & DATA sent in single message to manage HW flow
  69. * control. Each phase sent in different transfer for controller to idenity
  70. * phase.
  71. */
  72. static int tpm_tis_spi_transfer_half(struct tpm_tis_data *data, u32 addr,
  73. u16 len, u8 *in, const u8 *out)
  74. {
  75. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  76. struct spi_transfer spi_xfer[3];
  77. struct spi_message m;
  78. u8 transfer_len;
  79. int ret;
  80. while (len) {
  81. transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
  82. spi_message_init(&m);
  83. phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
  84. phy->iobuf[1] = 0xd4;
  85. phy->iobuf[2] = addr >> 8;
  86. phy->iobuf[3] = addr;
  87. memset(&spi_xfer, 0, sizeof(spi_xfer));
  88. spi_xfer[0].tx_buf = phy->iobuf;
  89. spi_xfer[0].len = 1;
  90. spi_message_add_tail(&spi_xfer[0], &m);
  91. spi_xfer[1].tx_buf = phy->iobuf + 1;
  92. spi_xfer[1].len = 3;
  93. spi_message_add_tail(&spi_xfer[1], &m);
  94. if (out) {
  95. spi_xfer[2].tx_buf = &phy->iobuf[4];
  96. spi_xfer[2].rx_buf = NULL;
  97. memcpy(&phy->iobuf[4], out, transfer_len);
  98. out += transfer_len;
  99. }
  100. if (in) {
  101. spi_xfer[2].tx_buf = NULL;
  102. spi_xfer[2].rx_buf = &phy->iobuf[4];
  103. }
  104. spi_xfer[2].len = transfer_len;
  105. spi_message_add_tail(&spi_xfer[2], &m);
  106. reinit_completion(&phy->ready);
  107. ret = spi_sync(phy->spi_device, &m);
  108. if (ret < 0)
  109. return ret;
  110. if (in) {
  111. memcpy(in, &phy->iobuf[4], transfer_len);
  112. in += transfer_len;
  113. }
  114. len -= transfer_len;
  115. }
  116. return ret;
  117. }
  118. static int tpm_tis_spi_transfer_full(struct tpm_tis_data *data, u32 addr,
  119. u16 len, u8 *in, const u8 *out)
  120. {
  121. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  122. int ret = 0;
  123. struct spi_message m;
  124. struct spi_transfer spi_xfer;
  125. u8 transfer_len;
  126. spi_bus_lock(phy->spi_device->controller);
  127. while (len) {
  128. transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
  129. phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
  130. phy->iobuf[1] = 0xd4;
  131. phy->iobuf[2] = addr >> 8;
  132. phy->iobuf[3] = addr;
  133. memset(&spi_xfer, 0, sizeof(spi_xfer));
  134. spi_xfer.tx_buf = phy->iobuf;
  135. spi_xfer.rx_buf = phy->iobuf;
  136. spi_xfer.len = 4;
  137. spi_xfer.cs_change = 1;
  138. spi_message_init(&m);
  139. spi_message_add_tail(&spi_xfer, &m);
  140. ret = spi_sync_locked(phy->spi_device, &m);
  141. if (ret < 0)
  142. goto exit;
  143. /* Flow control transfers are receive only */
  144. spi_xfer.tx_buf = NULL;
  145. ret = phy->flow_control(phy, &spi_xfer);
  146. if (ret < 0)
  147. goto exit;
  148. spi_xfer.cs_change = 0;
  149. spi_xfer.len = transfer_len;
  150. spi_xfer.delay.value = 5;
  151. spi_xfer.delay.unit = SPI_DELAY_UNIT_USECS;
  152. if (out) {
  153. spi_xfer.tx_buf = phy->iobuf;
  154. spi_xfer.rx_buf = NULL;
  155. memcpy(phy->iobuf, out, transfer_len);
  156. out += transfer_len;
  157. }
  158. spi_message_init(&m);
  159. spi_message_add_tail(&spi_xfer, &m);
  160. reinit_completion(&phy->ready);
  161. ret = spi_sync_locked(phy->spi_device, &m);
  162. if (ret < 0)
  163. goto exit;
  164. if (in) {
  165. memcpy(in, phy->iobuf, transfer_len);
  166. in += transfer_len;
  167. }
  168. len -= transfer_len;
  169. }
  170. exit:
  171. if (ret < 0) {
  172. /* Deactivate chip select */
  173. memset(&spi_xfer, 0, sizeof(spi_xfer));
  174. spi_message_init(&m);
  175. spi_message_add_tail(&spi_xfer, &m);
  176. spi_sync_locked(phy->spi_device, &m);
  177. }
  178. spi_bus_unlock(phy->spi_device->controller);
  179. return ret;
  180. }
  181. int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
  182. u8 *in, const u8 *out)
  183. {
  184. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  185. struct spi_controller *ctlr = phy->spi_device->controller;
  186. /*
  187. * TPM flow control over SPI requires full duplex support.
  188. * Send entire message to a half duplex controller to handle
  189. * wait polling in controller.
  190. * Set TPM HW flow control flag..
  191. */
  192. if (ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX)
  193. return tpm_tis_spi_transfer_half(data, addr, len, in, out);
  194. else
  195. return tpm_tis_spi_transfer_full(data, addr, len, in, out);
  196. }
  197. static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
  198. u16 len, u8 *result, enum tpm_tis_io_mode io_mode)
  199. {
  200. return tpm_tis_spi_transfer(data, addr, len, result, NULL);
  201. }
  202. static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  203. u16 len, const u8 *value, enum tpm_tis_io_mode io_mode)
  204. {
  205. return tpm_tis_spi_transfer(data, addr, len, NULL, value);
  206. }
  207. int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
  208. int irq, const struct tpm_tis_phy_ops *phy_ops)
  209. {
  210. phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
  211. if (!phy->iobuf)
  212. return -ENOMEM;
  213. phy->spi_device = spi;
  214. return tpm_tis_core_init(&spi->dev, &phy->priv, irq, phy_ops, NULL);
  215. }
  216. static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
  217. .read_bytes = tpm_tis_spi_read_bytes,
  218. .write_bytes = tpm_tis_spi_write_bytes,
  219. };
  220. static int tpm_tis_spi_probe(struct spi_device *dev)
  221. {
  222. struct tpm_tis_spi_phy *phy;
  223. int irq;
  224. phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
  225. GFP_KERNEL);
  226. if (!phy)
  227. return -ENOMEM;
  228. phy->flow_control = tpm_tis_spi_flow_control;
  229. if (dev->controller->flags & SPI_CONTROLLER_HALF_DUPLEX)
  230. dev->mode |= SPI_TPM_HW_FLOW;
  231. /* If the SPI device has an IRQ then use that */
  232. if (dev->irq > 0)
  233. irq = dev->irq;
  234. else
  235. irq = -1;
  236. init_completion(&phy->ready);
  237. return tpm_tis_spi_init(dev, phy, irq, &tpm_spi_phy_ops);
  238. }
  239. typedef int (*tpm_tis_spi_probe_func)(struct spi_device *);
  240. static int tpm_tis_spi_driver_probe(struct spi_device *spi)
  241. {
  242. const struct spi_device_id *spi_dev_id = spi_get_device_id(spi);
  243. tpm_tis_spi_probe_func probe_func;
  244. probe_func = of_device_get_match_data(&spi->dev);
  245. if (!probe_func) {
  246. if (spi_dev_id) {
  247. probe_func = (tpm_tis_spi_probe_func)spi_dev_id->driver_data;
  248. if (!probe_func)
  249. return -ENODEV;
  250. } else
  251. probe_func = tpm_tis_spi_probe;
  252. }
  253. return probe_func(spi);
  254. }
  255. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_spi_resume);
  256. static void tpm_tis_spi_remove(struct spi_device *dev)
  257. {
  258. struct tpm_chip *chip = spi_get_drvdata(dev);
  259. tpm_chip_unregister(chip);
  260. tpm_tis_remove(chip);
  261. }
  262. static const struct spi_device_id tpm_tis_spi_id[] = {
  263. { "attpm20p", (unsigned long)tpm_tis_spi_probe },
  264. { "st33htpm-spi", (unsigned long)tpm_tis_spi_probe },
  265. { "slb9670", (unsigned long)tpm_tis_spi_probe },
  266. { "tpm_tis_spi", (unsigned long)tpm_tis_spi_probe },
  267. { "tpm_tis-spi", (unsigned long)tpm_tis_spi_probe },
  268. { "cr50", (unsigned long)cr50_spi_probe },
  269. {}
  270. };
  271. MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
  272. static const struct of_device_id of_tis_spi_match[] __maybe_unused = {
  273. { .compatible = "atmel,attpm20p", .data = tpm_tis_spi_probe },
  274. { .compatible = "st,st33htpm-spi", .data = tpm_tis_spi_probe },
  275. { .compatible = "infineon,slb9670", .data = tpm_tis_spi_probe },
  276. { .compatible = "tcg,tpm_tis-spi", .data = tpm_tis_spi_probe },
  277. { .compatible = "google,cr50", .data = cr50_spi_probe },
  278. {}
  279. };
  280. MODULE_DEVICE_TABLE(of, of_tis_spi_match);
  281. static const struct acpi_device_id acpi_tis_spi_match[] __maybe_unused = {
  282. {"SMO0768", 0},
  283. {}
  284. };
  285. MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
  286. static struct spi_driver tpm_tis_spi_driver = {
  287. .driver = {
  288. .name = "tpm_tis_spi",
  289. .pm = &tpm_tis_pm,
  290. .of_match_table = of_match_ptr(of_tis_spi_match),
  291. .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
  292. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  293. },
  294. .probe = tpm_tis_spi_driver_probe,
  295. .remove = tpm_tis_spi_remove,
  296. .id_table = tpm_tis_spi_id,
  297. };
  298. module_spi_driver(tpm_tis_spi_driver);
  299. MODULE_DESCRIPTION("TPM Driver for native SPI access");
  300. MODULE_LICENSE("GPL");