tpm2_tis_spi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author:
  4. * Miquel Raynal <miquel.raynal@bootlin.com>
  5. *
  6. * Description:
  7. * SPI-level driver for TCG/TIS TPM (trusted platform module).
  8. * Specifications at www.trustedcomputinggroup.org
  9. *
  10. * This device driver implements the TPM interface as defined in
  11. * the TCG SPI protocol stack version 2.0.
  12. *
  13. * It is based on the U-Boot driver tpm_tis_infineon_i2c.c.
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <fdtdec.h>
  18. #include <log.h>
  19. #include <spi.h>
  20. #include <tpm-v2.h>
  21. #include <linux/bitops.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/compiler.h>
  25. #include <linux/types.h>
  26. #include <linux/unaligned/be_byteshift.h>
  27. #include <asm-generic/gpio.h>
  28. #include "tpm_tis.h"
  29. #include "tpm_internal.h"
  30. #define MAX_SPI_FRAMESIZE 64
  31. /* Number of wait states to wait for */
  32. #define TPM_WAIT_STATES 100
  33. /**
  34. * struct tpm_tis_chip_data - Non-discoverable TPM information
  35. *
  36. * @pcr_count: Number of PCR per bank
  37. * @pcr_select_min: Size in octets of the pcrSelect array
  38. */
  39. struct tpm_tis_chip_data {
  40. unsigned int pcr_count;
  41. unsigned int pcr_select_min;
  42. unsigned int time_before_first_cmd_ms;
  43. };
  44. /**
  45. * tpm_tis_spi_read() - Read from TPM register
  46. *
  47. * @addr: register address to read from
  48. * @buffer: provided by caller
  49. * @len: number of bytes to read
  50. *
  51. * Read len bytes from TPM register and put them into
  52. * buffer (little-endian format, i.e. first byte is put into buffer[0]).
  53. *
  54. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  55. * values have to be swapped.
  56. *
  57. * Return: -EIO on error, 0 on success.
  58. */
  59. static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out,
  60. u8 *in, u16 len)
  61. {
  62. struct spi_slave *slave = dev_get_parent_priv(dev);
  63. int transfer_len, ret;
  64. u8 tx_buf[MAX_SPI_FRAMESIZE];
  65. u8 rx_buf[MAX_SPI_FRAMESIZE];
  66. if (in && out) {
  67. log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n",
  68. __func__);
  69. return -EINVAL;
  70. }
  71. ret = spi_claim_bus(slave);
  72. if (ret < 0) {
  73. log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__);
  74. return ret;
  75. }
  76. while (len) {
  77. /* Request */
  78. transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
  79. tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1);
  80. tx_buf[1] = 0xD4;
  81. tx_buf[2] = addr >> 8;
  82. tx_buf[3] = addr;
  83. ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN);
  84. if (ret < 0) {
  85. log(LOGC_NONE, LOGL_ERR,
  86. "%s: spi request transfer failed (err: %d)\n",
  87. __func__, ret);
  88. goto release_bus;
  89. }
  90. /* Wait state */
  91. if (!(rx_buf[3] & 0x1)) {
  92. int i;
  93. for (i = 0; i < TPM_WAIT_STATES; i++) {
  94. ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0);
  95. if (ret) {
  96. log(LOGC_NONE, LOGL_ERR,
  97. "%s: wait state failed: %d\n",
  98. __func__, ret);
  99. goto release_bus;
  100. }
  101. if (rx_buf[0] & 0x1)
  102. break;
  103. }
  104. if (i == TPM_WAIT_STATES) {
  105. log(LOGC_NONE, LOGL_ERR,
  106. "%s: timeout on wait state\n", __func__);
  107. ret = -ETIMEDOUT;
  108. goto release_bus;
  109. }
  110. }
  111. /* Read/Write */
  112. if (out) {
  113. memcpy(tx_buf, out, transfer_len);
  114. out += transfer_len;
  115. }
  116. ret = spi_xfer(slave, transfer_len * 8,
  117. out ? tx_buf : NULL,
  118. in ? rx_buf : NULL,
  119. SPI_XFER_END);
  120. if (ret) {
  121. log(LOGC_NONE, LOGL_ERR,
  122. "%s: spi read transfer failed (err: %d)\n",
  123. __func__, ret);
  124. goto release_bus;
  125. }
  126. if (in) {
  127. memcpy(in, rx_buf, transfer_len);
  128. in += transfer_len;
  129. }
  130. len -= transfer_len;
  131. }
  132. release_bus:
  133. /* If an error occurred, release the chip by deasserting the CS */
  134. if (ret < 0)
  135. spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
  136. spi_release_bus(slave);
  137. return ret;
  138. }
  139. static int tpm_tis_spi_read(struct udevice *dev, u32 addr, u16 len, u8 *in)
  140. {
  141. return tpm_tis_spi_xfer(dev, addr, NULL, in, len);
  142. }
  143. static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result)
  144. {
  145. __le32 result_le;
  146. int ret;
  147. ret = tpm_tis_spi_read(dev, addr, sizeof(u32), (u8 *)&result_le);
  148. if (!ret)
  149. *result = le32_to_cpu(result_le);
  150. return ret;
  151. }
  152. static int tpm_tis_spi_write(struct udevice *dev, u32 addr, u16 len, const u8 *out)
  153. {
  154. return tpm_tis_spi_xfer(dev, addr, out, NULL, len);
  155. }
  156. static int tpm_tis_spi_write32(struct udevice *dev, u32 addr, u32 value)
  157. {
  158. __le32 value_le = cpu_to_le32(value);
  159. return tpm_tis_spi_write(dev, addr, sizeof(value), (u8 *)&value_le);
  160. }
  161. static int tpm_tis_wait_init(struct udevice *dev, int loc)
  162. {
  163. struct tpm_chip *chip = dev_get_priv(dev);
  164. unsigned long start, stop;
  165. u8 status;
  166. int ret;
  167. start = get_timer(0);
  168. stop = chip->timeout_b;
  169. do {
  170. mdelay(TPM_TIMEOUT_MS);
  171. ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), 1, &status);
  172. if (ret)
  173. break;
  174. if (status & TPM_ACCESS_VALID)
  175. return 0;
  176. } while (get_timer(start) < stop);
  177. return -EIO;
  178. }
  179. static struct tpm_tis_phy_ops phy_ops = {
  180. .read_bytes = tpm_tis_spi_read,
  181. .write_bytes = tpm_tis_spi_write,
  182. .read32 = tpm_tis_spi_read32,
  183. .write32 = tpm_tis_spi_write32,
  184. };
  185. static int tpm_tis_spi_probe(struct udevice *dev)
  186. {
  187. struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
  188. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  189. struct tpm_chip *chip = dev_get_priv(dev);
  190. int ret;
  191. /* Use the TPM v2 stack */
  192. priv->version = TPM_V2;
  193. if (CONFIG_IS_ENABLED(DM_GPIO)) {
  194. struct gpio_desc reset_gpio;
  195. ret = gpio_request_by_name(dev, "reset-gpios", 0,
  196. &reset_gpio, GPIOD_IS_OUT);
  197. if (ret) {
  198. /* legacy reset */
  199. ret = gpio_request_by_name(dev, "gpio-reset", 0,
  200. &reset_gpio, GPIOD_IS_OUT);
  201. if (ret) {
  202. log(LOGC_NONE, LOGL_NOTICE,
  203. "%s: missing reset GPIO\n", __func__);
  204. goto init;
  205. }
  206. log(LOGC_NONE, LOGL_NOTICE,
  207. "%s: gpio-reset is deprecated\n", __func__);
  208. }
  209. dm_gpio_set_value(&reset_gpio, 1);
  210. mdelay(1);
  211. dm_gpio_set_value(&reset_gpio, 0);
  212. }
  213. init:
  214. /* Ensure a minimum amount of time elapsed since reset of the TPM */
  215. mdelay(drv_data->time_before_first_cmd_ms);
  216. ret = tpm_tis_wait_init(dev, chip->locality);
  217. if (ret) {
  218. log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__);
  219. return ret;
  220. }
  221. tpm_tis_ops_register(dev, &phy_ops);
  222. ret = tpm_tis_init(dev);
  223. if (ret)
  224. goto err;
  225. priv->pcr_count = drv_data->pcr_count;
  226. priv->pcr_select_min = drv_data->pcr_select_min;
  227. priv->version = TPM_V2;
  228. return 0;
  229. err:
  230. return -EINVAL;
  231. }
  232. static int tpm_tis_spi_remove(struct udevice *udev)
  233. {
  234. return tpm_tis_cleanup(udev);
  235. }
  236. static const struct tpm_ops tpm_tis_spi_ops = {
  237. .open = tpm_tis_open,
  238. .close = tpm_tis_close,
  239. .get_desc = tpm_tis_get_desc,
  240. .send = tpm_tis_send,
  241. .recv = tpm_tis_recv,
  242. .cleanup = tpm_tis_cleanup,
  243. };
  244. static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
  245. .pcr_count = 24,
  246. .pcr_select_min = 3,
  247. .time_before_first_cmd_ms = 30,
  248. };
  249. static const struct udevice_id tpm_tis_spi_ids[] = {
  250. {
  251. .compatible = "tcg,tpm_tis-spi",
  252. .data = (ulong)&tpm_tis_std_chip_data,
  253. },
  254. { }
  255. };
  256. U_BOOT_DRIVER(tpm_tis_spi) = {
  257. .name = "tpm_tis_spi",
  258. .id = UCLASS_TPM,
  259. .of_match = tpm_tis_spi_ids,
  260. .ops = &tpm_tis_spi_ops,
  261. .probe = tpm_tis_spi_probe,
  262. .remove = tpm_tis_spi_remove,
  263. .priv_auto = sizeof(struct tpm_chip),
  264. };