mtk_snfi_spi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 MediaTek Inc. All Rights Reserved.
  4. *
  5. * Author: Weijie Gao <weijie.gao@mediatek.com>
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <spi.h>
  12. #include <spi-mem.h>
  13. #include <stdbool.h>
  14. #include <watchdog.h>
  15. #include <dm/pinctrl.h>
  16. #include <linux/bitops.h>
  17. #include <linux/io.h>
  18. #include <linux/iopoll.h>
  19. #define SNFI_MAC_CTL 0x500
  20. #define MAC_XIO_SEL BIT(4)
  21. #define SF_MAC_EN BIT(3)
  22. #define SF_TRIG BIT(2)
  23. #define WIP_READY BIT(1)
  24. #define WIP BIT(0)
  25. #define SNFI_MAC_OUTL 0x504
  26. #define SNFI_MAC_INL 0x508
  27. #define SNFI_MISC_CTL 0x538
  28. #define SW_RST BIT(28)
  29. #define FIFO_RD_LTC_SHIFT 25
  30. #define FIFO_RD_LTC GENMASK(26, 25)
  31. #define LATCH_LAT_SHIFT 8
  32. #define LATCH_LAT GENMASK(9, 8)
  33. #define CS_DESELECT_CYC_SHIFT 0
  34. #define CS_DESELECT_CYC GENMASK(4, 0)
  35. #define SNF_STA_CTL1 0x550
  36. #define SPI_STATE GENMASK(3, 0)
  37. #define SNFI_GPRAM_OFFSET 0x800
  38. #define SNFI_GPRAM_SIZE 0x80
  39. #define SNFI_POLL_INTERVAL 500000
  40. #define SNFI_RST_POLL_INTERVAL 1000000
  41. struct mtk_snfi_priv {
  42. void __iomem *base;
  43. struct clk nfi_clk;
  44. struct clk pad_clk;
  45. };
  46. static int mtk_snfi_adjust_op_size(struct spi_slave *slave,
  47. struct spi_mem_op *op)
  48. {
  49. u32 nbytes;
  50. /*
  51. * When there is input data, it will be appended after the output
  52. * data in the GPRAM. So the total size of either pure output data
  53. * or the output+input data must not exceed the GPRAM size.
  54. */
  55. nbytes = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
  56. if (nbytes + op->data.nbytes <= SNFI_GPRAM_SIZE)
  57. return 0;
  58. if (nbytes >= SNFI_GPRAM_SIZE)
  59. return -ENOTSUPP;
  60. op->data.nbytes = SNFI_GPRAM_SIZE - nbytes;
  61. return 0;
  62. }
  63. static bool mtk_snfi_supports_op(struct spi_slave *slave,
  64. const struct spi_mem_op *op)
  65. {
  66. if (op->cmd.buswidth > 1 || op->addr.buswidth > 1 ||
  67. op->dummy.buswidth > 1 || op->data.buswidth > 1)
  68. return false;
  69. return true;
  70. }
  71. static int mtk_snfi_mac_trigger(struct mtk_snfi_priv *priv,
  72. struct udevice *bus, u32 outlen, u32 inlen)
  73. {
  74. int ret;
  75. u32 val;
  76. #ifdef CONFIG_PINCTRL
  77. pinctrl_select_state(bus, "snfi");
  78. #endif
  79. writel(SF_MAC_EN, priv->base + SNFI_MAC_CTL);
  80. writel(outlen, priv->base + SNFI_MAC_OUTL);
  81. writel(inlen, priv->base + SNFI_MAC_INL);
  82. writel(SF_MAC_EN | SF_TRIG, priv->base + SNFI_MAC_CTL);
  83. ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val,
  84. val & WIP_READY, SNFI_POLL_INTERVAL);
  85. if (ret) {
  86. printf("%s: timed out waiting for WIP_READY\n", __func__);
  87. goto cleanup;
  88. }
  89. ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val,
  90. !(val & WIP), SNFI_POLL_INTERVAL);
  91. if (ret)
  92. printf("%s: timed out waiting for WIP cleared\n", __func__);
  93. writel(0, priv->base + SNFI_MAC_CTL);
  94. cleanup:
  95. #ifdef CONFIG_PINCTRL
  96. pinctrl_select_state(bus, "default");
  97. #endif
  98. return ret;
  99. }
  100. static int mtk_snfi_mac_reset(struct mtk_snfi_priv *priv)
  101. {
  102. int ret;
  103. u32 val;
  104. setbits_32(priv->base + SNFI_MISC_CTL, SW_RST);
  105. ret = readl_poll_timeout(priv->base + SNF_STA_CTL1, val,
  106. !(val & SPI_STATE), SNFI_POLL_INTERVAL);
  107. if (ret)
  108. printf("%s: failed to reset snfi mac\n", __func__);
  109. writel((2 << FIFO_RD_LTC_SHIFT) |
  110. (10 << CS_DESELECT_CYC_SHIFT),
  111. priv->base + SNFI_MISC_CTL);
  112. return ret;
  113. }
  114. static void mtk_snfi_copy_to_gpram(struct mtk_snfi_priv *priv,
  115. const void *data, size_t len)
  116. {
  117. void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET;
  118. size_t i, n = (len + sizeof(u32) - 1) / sizeof(u32);
  119. const u32 *buff = data;
  120. /*
  121. * The output data will always be copied to the beginning of
  122. * the GPRAM. Uses word write for better performance.
  123. *
  124. * Trailing bytes in the last word are not cared.
  125. */
  126. for (i = 0; i < n; i++)
  127. writel(buff[i], gpram + i * sizeof(u32));
  128. }
  129. static void mtk_snfi_copy_from_gpram(struct mtk_snfi_priv *priv, u8 *cache,
  130. void *data, size_t pos, size_t len)
  131. {
  132. void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET;
  133. u32 *buff = (u32 *)cache;
  134. size_t i, off, end;
  135. /* Start position in the buffer */
  136. off = pos & (sizeof(u32) - 1);
  137. /* End position for copy */
  138. end = (len + pos + sizeof(u32) - 1) & (~(sizeof(u32) - 1));
  139. /* Start position for copy */
  140. pos &= ~(sizeof(u32) - 1);
  141. /*
  142. * Read aligned data from GPRAM to buffer first.
  143. * Uses word read for better performance.
  144. */
  145. i = 0;
  146. while (pos < end) {
  147. buff[i++] = readl(gpram + pos);
  148. pos += sizeof(u32);
  149. }
  150. /* Copy rx data */
  151. memcpy(data, cache + off, len);
  152. }
  153. static int mtk_snfi_exec_op(struct spi_slave *slave,
  154. const struct spi_mem_op *op)
  155. {
  156. struct udevice *bus = dev_get_parent(slave->dev);
  157. struct mtk_snfi_priv *priv = dev_get_priv(bus);
  158. u8 gpram_cache[SNFI_GPRAM_SIZE];
  159. u32 i, len = 0, inlen = 0;
  160. int addr_sh;
  161. int ret;
  162. schedule();
  163. ret = mtk_snfi_mac_reset(priv);
  164. if (ret)
  165. return ret;
  166. /* Put opcode */
  167. gpram_cache[len++] = op->cmd.opcode;
  168. /* Put address */
  169. addr_sh = (op->addr.nbytes - 1) * 8;
  170. while (addr_sh >= 0) {
  171. gpram_cache[len++] = (op->addr.val >> addr_sh) & 0xff;
  172. addr_sh -= 8;
  173. }
  174. /* Put dummy bytes */
  175. for (i = 0; i < op->dummy.nbytes; i++)
  176. gpram_cache[len++] = 0;
  177. /* Put output data */
  178. if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) {
  179. memcpy(gpram_cache + len, op->data.buf.out, op->data.nbytes);
  180. len += op->data.nbytes;
  181. }
  182. /* Copy final output data to GPRAM */
  183. mtk_snfi_copy_to_gpram(priv, gpram_cache, len);
  184. /* Start one SPI transaction */
  185. if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
  186. inlen = op->data.nbytes;
  187. ret = mtk_snfi_mac_trigger(priv, bus, len, inlen);
  188. if (ret)
  189. return ret;
  190. /* Copy input data from GPRAM */
  191. if (inlen)
  192. mtk_snfi_copy_from_gpram(priv, gpram_cache, op->data.buf.in,
  193. len, inlen);
  194. return 0;
  195. }
  196. static int mtk_snfi_spi_probe(struct udevice *bus)
  197. {
  198. struct mtk_snfi_priv *priv = dev_get_priv(bus);
  199. int ret;
  200. priv->base = dev_read_addr_ptr(bus);
  201. if (!priv->base)
  202. return -EINVAL;
  203. ret = clk_get_by_name(bus, "nfi_clk", &priv->nfi_clk);
  204. if (ret < 0)
  205. return ret;
  206. ret = clk_get_by_name(bus, "pad_clk", &priv->pad_clk);
  207. if (ret < 0)
  208. return ret;
  209. clk_enable(&priv->nfi_clk);
  210. clk_enable(&priv->pad_clk);
  211. return 0;
  212. }
  213. static int mtk_snfi_set_speed(struct udevice *bus, uint speed)
  214. {
  215. /*
  216. * The SNFI does not have a bus clock divider.
  217. * The bus clock is set in dts (pad_clk, UNIVPLL2_D8 = 50MHz).
  218. */
  219. return 0;
  220. }
  221. static int mtk_snfi_set_mode(struct udevice *bus, uint mode)
  222. {
  223. /* The SNFI supports only mode 0 */
  224. if (mode)
  225. return -EINVAL;
  226. return 0;
  227. }
  228. static const struct spi_controller_mem_ops mtk_snfi_mem_ops = {
  229. .adjust_op_size = mtk_snfi_adjust_op_size,
  230. .supports_op = mtk_snfi_supports_op,
  231. .exec_op = mtk_snfi_exec_op,
  232. };
  233. static const struct dm_spi_ops mtk_snfi_spi_ops = {
  234. .mem_ops = &mtk_snfi_mem_ops,
  235. .set_speed = mtk_snfi_set_speed,
  236. .set_mode = mtk_snfi_set_mode,
  237. };
  238. static const struct udevice_id mtk_snfi_spi_ids[] = {
  239. { .compatible = "mediatek,mtk-snfi-spi" },
  240. { }
  241. };
  242. U_BOOT_DRIVER(mtk_snfi_spi) = {
  243. .name = "mtk_snfi_spi",
  244. .id = UCLASS_SPI,
  245. .of_match = mtk_snfi_spi_ids,
  246. .ops = &mtk_snfi_spi_ops,
  247. .priv_auto = sizeof(struct mtk_snfi_priv),
  248. .probe = mtk_snfi_spi_probe,
  249. };