tpm_tis_spi_cr50.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Google, Inc
  4. *
  5. * This device driver implements a TCG PTP FIFO interface over SPI for chips
  6. * with Cr50 firmware.
  7. * It is based on tpm_tis_spi driver by Peter Huewe and Christophe Ricard.
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/pm.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/wait.h>
  16. #include "tpm_tis_core.h"
  17. #include "tpm_tis_spi.h"
  18. /*
  19. * Cr50 timing constants:
  20. * - can go to sleep not earlier than after CR50_SLEEP_DELAY_MSEC.
  21. * - needs up to CR50_WAKE_START_DELAY_USEC to wake after sleep.
  22. * - requires waiting for "ready" IRQ, if supported; or waiting for at least
  23. * CR50_NOIRQ_ACCESS_DELAY_MSEC between transactions, if IRQ is not supported.
  24. * - waits for up to CR50_FLOW_CONTROL for flow control 'ready' indication.
  25. */
  26. #define CR50_SLEEP_DELAY_MSEC 1000
  27. #define CR50_WAKE_START_DELAY_USEC 1000
  28. #define CR50_NOIRQ_ACCESS_DELAY msecs_to_jiffies(2)
  29. #define CR50_READY_IRQ_TIMEOUT msecs_to_jiffies(TPM2_TIMEOUT_A)
  30. #define CR50_FLOW_CONTROL msecs_to_jiffies(TPM2_TIMEOUT_A)
  31. #define MAX_IRQ_CONFIRMATION_ATTEMPTS 3
  32. #define TPM_CR50_FW_VER(l) (0x0f90 | ((l) << 12))
  33. #define TPM_CR50_MAX_FW_VER_LEN 64
  34. /* Default quality for hwrng. */
  35. #define TPM_CR50_DEFAULT_RNG_QUALITY 700
  36. struct cr50_spi_phy {
  37. struct tpm_tis_spi_phy spi_phy;
  38. struct mutex time_track_mutex;
  39. unsigned long last_access;
  40. unsigned long access_delay;
  41. unsigned int irq_confirmation_attempt;
  42. bool irq_needs_confirmation;
  43. bool irq_confirmed;
  44. };
  45. static inline struct cr50_spi_phy *to_cr50_spi_phy(struct tpm_tis_spi_phy *phy)
  46. {
  47. return container_of(phy, struct cr50_spi_phy, spi_phy);
  48. }
  49. /*
  50. * The cr50 interrupt handler just signals waiting threads that the
  51. * interrupt was asserted. It does not do any processing triggered
  52. * by interrupts but is instead used to avoid fixed delays.
  53. */
  54. static irqreturn_t cr50_spi_irq_handler(int dummy, void *dev_id)
  55. {
  56. struct cr50_spi_phy *cr50_phy = dev_id;
  57. cr50_phy->irq_confirmed = true;
  58. complete(&cr50_phy->spi_phy.ready);
  59. return IRQ_HANDLED;
  60. }
  61. /*
  62. * Cr50 needs to have at least some delay between consecutive
  63. * transactions. Make sure we wait.
  64. */
  65. static void cr50_ensure_access_delay(struct cr50_spi_phy *phy)
  66. {
  67. unsigned long allowed_access = phy->last_access + phy->access_delay;
  68. unsigned long time_now = jiffies;
  69. struct device *dev = &phy->spi_phy.spi_device->dev;
  70. /*
  71. * Note: There is a small chance, if Cr50 is not accessed in a few days,
  72. * that time_in_range will not provide the correct result after the wrap
  73. * around for jiffies. In this case, we'll have an unneeded short delay,
  74. * which is fine.
  75. */
  76. if (time_in_range_open(time_now, phy->last_access, allowed_access)) {
  77. unsigned long remaining, timeout = allowed_access - time_now;
  78. remaining = wait_for_completion_timeout(&phy->spi_phy.ready,
  79. timeout);
  80. if (!remaining && phy->irq_confirmed)
  81. dev_warn(dev, "Timeout waiting for TPM ready IRQ\n");
  82. }
  83. if (phy->irq_needs_confirmation) {
  84. unsigned int attempt = ++phy->irq_confirmation_attempt;
  85. if (phy->irq_confirmed) {
  86. phy->irq_needs_confirmation = false;
  87. phy->access_delay = CR50_READY_IRQ_TIMEOUT;
  88. dev_info(dev, "TPM ready IRQ confirmed on attempt %u\n",
  89. attempt);
  90. } else if (attempt > MAX_IRQ_CONFIRMATION_ATTEMPTS) {
  91. phy->irq_needs_confirmation = false;
  92. dev_warn(dev, "IRQ not confirmed - will use delays\n");
  93. }
  94. }
  95. }
  96. /*
  97. * Cr50 might go to sleep if there is no SPI activity for some time and
  98. * miss the first few bits/bytes on the bus. In such case, wake it up
  99. * by asserting CS and give it time to start up.
  100. */
  101. static bool cr50_needs_waking(struct cr50_spi_phy *phy)
  102. {
  103. /*
  104. * Note: There is a small chance, if Cr50 is not accessed in a few days,
  105. * that time_in_range will not provide the correct result after the wrap
  106. * around for jiffies. In this case, we'll probably timeout or read
  107. * incorrect value from TPM_STS and just retry the operation.
  108. */
  109. return !time_in_range_open(jiffies, phy->last_access,
  110. phy->spi_phy.wake_after);
  111. }
  112. static void cr50_wake_if_needed(struct cr50_spi_phy *cr50_phy)
  113. {
  114. struct tpm_tis_spi_phy *phy = &cr50_phy->spi_phy;
  115. if (cr50_needs_waking(cr50_phy)) {
  116. /* Assert CS, wait 1 msec, deassert CS */
  117. struct spi_transfer spi_cs_wake = {
  118. .delay = {
  119. .value = 1000,
  120. .unit = SPI_DELAY_UNIT_USECS
  121. }
  122. };
  123. spi_sync_transfer(phy->spi_device, &spi_cs_wake, 1);
  124. /* Wait for it to fully wake */
  125. usleep_range(CR50_WAKE_START_DELAY_USEC,
  126. CR50_WAKE_START_DELAY_USEC * 2);
  127. }
  128. /* Reset the time when we need to wake Cr50 again */
  129. phy->wake_after = jiffies + msecs_to_jiffies(CR50_SLEEP_DELAY_MSEC);
  130. }
  131. /*
  132. * Flow control: clock the bus and wait for cr50 to set LSB before
  133. * sending/receiving data. TCG PTP spec allows it to happen during
  134. * the last byte of header, but cr50 never does that in practice,
  135. * and earlier versions had a bug when it was set too early, so don't
  136. * check for it during header transfer.
  137. */
  138. static int cr50_spi_flow_control(struct tpm_tis_spi_phy *phy,
  139. struct spi_transfer *spi_xfer)
  140. {
  141. struct device *dev = &phy->spi_device->dev;
  142. unsigned long timeout = jiffies + CR50_FLOW_CONTROL;
  143. struct spi_message m;
  144. int ret;
  145. spi_xfer->len = 1;
  146. do {
  147. spi_message_init(&m);
  148. spi_message_add_tail(spi_xfer, &m);
  149. ret = spi_sync_locked(phy->spi_device, &m);
  150. if (ret < 0)
  151. return ret;
  152. if (time_after(jiffies, timeout)) {
  153. dev_warn(dev, "Timeout during flow control\n");
  154. return -EBUSY;
  155. }
  156. } while (!(phy->iobuf[0] & 0x01));
  157. return 0;
  158. }
  159. static bool tpm_cr50_spi_is_firmware_power_managed(struct device *dev)
  160. {
  161. u8 val;
  162. int ret;
  163. /* This flag should default true when the device property is not present */
  164. ret = device_property_read_u8(dev, "firmware-power-managed", &val);
  165. if (ret)
  166. return true;
  167. return val;
  168. }
  169. static int tpm_tis_spi_cr50_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
  170. u8 *in, const u8 *out)
  171. {
  172. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  173. struct cr50_spi_phy *cr50_phy = to_cr50_spi_phy(phy);
  174. int ret;
  175. mutex_lock(&cr50_phy->time_track_mutex);
  176. /*
  177. * Do this outside of spi_bus_lock in case cr50 is not the
  178. * only device on that spi bus.
  179. */
  180. cr50_ensure_access_delay(cr50_phy);
  181. cr50_wake_if_needed(cr50_phy);
  182. ret = tpm_tis_spi_transfer(data, addr, len, in, out);
  183. cr50_phy->last_access = jiffies;
  184. mutex_unlock(&cr50_phy->time_track_mutex);
  185. return ret;
  186. }
  187. static int tpm_tis_spi_cr50_read_bytes(struct tpm_tis_data *data, u32 addr,
  188. u16 len, u8 *result, enum tpm_tis_io_mode io_mode)
  189. {
  190. return tpm_tis_spi_cr50_transfer(data, addr, len, result, NULL);
  191. }
  192. static int tpm_tis_spi_cr50_write_bytes(struct tpm_tis_data *data, u32 addr,
  193. u16 len, const u8 *value, enum tpm_tis_io_mode io_mode)
  194. {
  195. return tpm_tis_spi_cr50_transfer(data, addr, len, NULL, value);
  196. }
  197. static const struct tpm_tis_phy_ops tpm_spi_cr50_phy_ops = {
  198. .read_bytes = tpm_tis_spi_cr50_read_bytes,
  199. .write_bytes = tpm_tis_spi_cr50_write_bytes,
  200. };
  201. static void cr50_print_fw_version(struct tpm_tis_data *data)
  202. {
  203. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  204. int i, len = 0;
  205. char fw_ver[TPM_CR50_MAX_FW_VER_LEN + 1];
  206. char fw_ver_block[4];
  207. /*
  208. * Write anything to TPM_CR50_FW_VER to start from the beginning
  209. * of the version string
  210. */
  211. tpm_tis_write8(data, TPM_CR50_FW_VER(data->locality), 0);
  212. /* Read the string, 4 bytes at a time, until we get '\0' */
  213. do {
  214. tpm_tis_read_bytes(data, TPM_CR50_FW_VER(data->locality), 4,
  215. fw_ver_block);
  216. for (i = 0; i < 4 && fw_ver_block[i]; ++len, ++i)
  217. fw_ver[len] = fw_ver_block[i];
  218. } while (i == 4 && len < TPM_CR50_MAX_FW_VER_LEN);
  219. fw_ver[len] = '\0';
  220. dev_info(&phy->spi_device->dev, "Cr50 firmware version: %s\n", fw_ver);
  221. }
  222. int cr50_spi_probe(struct spi_device *spi)
  223. {
  224. struct tpm_tis_spi_phy *phy;
  225. struct cr50_spi_phy *cr50_phy;
  226. int ret;
  227. struct tpm_chip *chip;
  228. cr50_phy = devm_kzalloc(&spi->dev, sizeof(*cr50_phy), GFP_KERNEL);
  229. if (!cr50_phy)
  230. return -ENOMEM;
  231. phy = &cr50_phy->spi_phy;
  232. phy->flow_control = cr50_spi_flow_control;
  233. phy->wake_after = jiffies;
  234. phy->priv.rng_quality = TPM_CR50_DEFAULT_RNG_QUALITY;
  235. init_completion(&phy->ready);
  236. cr50_phy->access_delay = CR50_NOIRQ_ACCESS_DELAY;
  237. cr50_phy->last_access = jiffies;
  238. mutex_init(&cr50_phy->time_track_mutex);
  239. if (spi->irq > 0) {
  240. ret = devm_request_irq(&spi->dev, spi->irq,
  241. cr50_spi_irq_handler,
  242. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  243. "cr50_spi", cr50_phy);
  244. if (ret < 0) {
  245. if (ret == -EPROBE_DEFER)
  246. return ret;
  247. dev_warn(&spi->dev, "Requesting IRQ %d failed: %d\n",
  248. spi->irq, ret);
  249. /*
  250. * This is not fatal, the driver will fall back to
  251. * delays automatically, since ready will never
  252. * be completed without a registered irq handler.
  253. * So, just fall through.
  254. */
  255. } else {
  256. /*
  257. * IRQ requested, let's verify that it is actually
  258. * triggered, before relying on it.
  259. */
  260. cr50_phy->irq_needs_confirmation = true;
  261. }
  262. } else {
  263. dev_warn(&spi->dev,
  264. "No IRQ - will use delays between transactions.\n");
  265. }
  266. ret = tpm_tis_spi_init(spi, phy, -1, &tpm_spi_cr50_phy_ops);
  267. if (ret)
  268. return ret;
  269. cr50_print_fw_version(&phy->priv);
  270. chip = dev_get_drvdata(&spi->dev);
  271. if (tpm_cr50_spi_is_firmware_power_managed(&spi->dev))
  272. chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED;
  273. return 0;
  274. }
  275. #ifdef CONFIG_PM_SLEEP
  276. int tpm_tis_spi_resume(struct device *dev)
  277. {
  278. struct tpm_chip *chip = dev_get_drvdata(dev);
  279. struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
  280. struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
  281. /*
  282. * Jiffies not increased during suspend, so we need to reset
  283. * the time to wake Cr50 after resume.
  284. */
  285. phy->wake_after = jiffies;
  286. return tpm_tis_resume(dev);
  287. }
  288. #endif