apple_spi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
  4. * Copyright The Asahi Linux Contributors
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <clk.h>
  9. #include <spi.h>
  10. #include <asm/io.h>
  11. #include <linux/bitfield.h>
  12. #include <linux/delay.h>
  13. #define APPLE_SPI_CTRL 0x000
  14. #define APPLE_SPI_CTRL_RUN BIT(0)
  15. #define APPLE_SPI_CTRL_TX_RESET BIT(2)
  16. #define APPLE_SPI_CTRL_RX_RESET BIT(3)
  17. #define APPLE_SPI_CFG 0x004
  18. #define APPLE_SPI_CFG_CPHA BIT(1)
  19. #define APPLE_SPI_CFG_CPOL BIT(2)
  20. #define APPLE_SPI_CFG_MODE GENMASK(6, 5)
  21. #define APPLE_SPI_CFG_MODE_POLLED 0
  22. #define APPLE_SPI_CFG_MODE_IRQ 1
  23. #define APPLE_SPI_CFG_MODE_DMA 2
  24. #define APPLE_SPI_CFG_IE_RXCOMPLETE BIT(7)
  25. #define APPLE_SPI_CFG_IE_TXRXTHRESH BIT(8)
  26. #define APPLE_SPI_CFG_LSB_FIRST BIT(13)
  27. #define APPLE_SPI_CFG_WORD_SIZE GENMASK(16, 15)
  28. #define APPLE_SPI_CFG_WORD_SIZE_8B 0
  29. #define APPLE_SPI_CFG_WORD_SIZE_16B 1
  30. #define APPLE_SPI_CFG_WORD_SIZE_32B 2
  31. #define APPLE_SPI_CFG_FIFO_THRESH GENMASK(18, 17)
  32. #define APPLE_SPI_CFG_FIFO_THRESH_8B 0
  33. #define APPLE_SPI_CFG_FIFO_THRESH_4B 1
  34. #define APPLE_SPI_CFG_FIFO_THRESH_1B 2
  35. #define APPLE_SPI_CFG_IE_TXCOMPLETE BIT(21)
  36. #define APPLE_SPI_STATUS 0x008
  37. #define APPLE_SPI_STATUS_RXCOMPLETE BIT(0)
  38. #define APPLE_SPI_STATUS_TXRXTHRESH BIT(1)
  39. #define APPLE_SPI_STATUS_TXCOMPLETE BIT(2)
  40. #define APPLE_SPI_PIN 0x00c
  41. #define APPLE_SPI_PIN_KEEP_MOSI BIT(0)
  42. #define APPLE_SPI_PIN_CS BIT(1)
  43. #define APPLE_SPI_TXDATA 0x010
  44. #define APPLE_SPI_RXDATA 0x020
  45. #define APPLE_SPI_CLKDIV 0x030
  46. #define APPLE_SPI_CLKDIV_MIN 0x002
  47. #define APPLE_SPI_CLKDIV_MAX 0x7ff
  48. #define APPLE_SPI_RXCNT 0x034
  49. #define APPLE_SPI_WORD_DELAY 0x038
  50. #define APPLE_SPI_TXCNT 0x04c
  51. #define APPLE_SPI_FIFOSTAT 0x10c
  52. #define APPLE_SPI_FIFOSTAT_TXFULL BIT(4)
  53. #define APPLE_SPI_FIFOSTAT_LEVEL_TX GENMASK(15, 8)
  54. #define APPLE_SPI_FIFOSTAT_RXEMPTY BIT(20)
  55. #define APPLE_SPI_FIFOSTAT_LEVEL_RX GENMASK(31, 24)
  56. #define APPLE_SPI_IE_XFER 0x130
  57. #define APPLE_SPI_IF_XFER 0x134
  58. #define APPLE_SPI_XFER_RXCOMPLETE BIT(0)
  59. #define APPLE_SPI_XFER_TXCOMPLETE BIT(1)
  60. #define APPLE_SPI_IE_FIFO 0x138
  61. #define APPLE_SPI_IF_FIFO 0x13c
  62. #define APPLE_SPI_FIFO_RXTHRESH BIT(4)
  63. #define APPLE_SPI_FIFO_TXTHRESH BIT(5)
  64. #define APPLE_SPI_FIFO_RXFULL BIT(8)
  65. #define APPLE_SPI_FIFO_TXEMPTY BIT(9)
  66. #define APPLE_SPI_FIFO_RXUNDERRUN BIT(16)
  67. #define APPLE_SPI_FIFO_TXOVERFLOW BIT(17)
  68. #define APPLE_SPI_SHIFTCFG 0x150
  69. #define APPLE_SPI_SHIFTCFG_CLK_ENABLE BIT(0)
  70. #define APPLE_SPI_SHIFTCFG_CS_ENABLE BIT(1)
  71. #define APPLE_SPI_SHIFTCFG_AND_CLK_DATA BIT(8)
  72. #define APPLE_SPI_SHIFTCFG_CS_AS_DATA BIT(9)
  73. #define APPLE_SPI_SHIFTCFG_TX_ENABLE BIT(10)
  74. #define APPLE_SPI_SHIFTCFG_RX_ENABLE BIT(11)
  75. #define APPLE_SPI_SHIFTCFG_BITS GENMASK(21, 16)
  76. #define APPLE_SPI_SHIFTCFG_OVERRIDE_CS BIT(24)
  77. #define APPLE_SPI_PINCFG 0x154
  78. #define APPLE_SPI_PINCFG_KEEP_CLK BIT(0)
  79. #define APPLE_SPI_PINCFG_KEEP_CS BIT(1)
  80. #define APPLE_SPI_PINCFG_KEEP_MOSI BIT(2)
  81. #define APPLE_SPI_PINCFG_CLK_IDLE_VAL BIT(8)
  82. #define APPLE_SPI_PINCFG_CS_IDLE_VAL BIT(9)
  83. #define APPLE_SPI_PINCFG_MOSI_IDLE_VAL BIT(10)
  84. #define APPLE_SPI_DELAY_PRE 0x160
  85. #define APPLE_SPI_DELAY_POST 0x168
  86. #define APPLE_SPI_DELAY_ENABLE BIT(0)
  87. #define APPLE_SPI_DELAY_NO_INTERBYTE BIT(1)
  88. #define APPLE_SPI_DELAY_SET_SCK BIT(4)
  89. #define APPLE_SPI_DELAY_SET_MOSI BIT(6)
  90. #define APPLE_SPI_DELAY_SCK_VAL BIT(8)
  91. #define APPLE_SPI_DELAY_MOSI_VAL BIT(12)
  92. #define APPLE_SPI_FIFO_DEPTH 16
  93. #define APPLE_SPI_TIMEOUT_MS 200
  94. struct apple_spi_priv {
  95. void *base;
  96. u32 clkfreq; /* Input clock frequency */
  97. };
  98. static void apple_spi_set_cs(struct apple_spi_priv *priv, int on)
  99. {
  100. writel(on ? 0 : APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
  101. }
  102. /* Fill Tx FIFO. */
  103. static void apple_spi_tx(struct apple_spi_priv *priv, uint *len,
  104. const void **dout)
  105. {
  106. const u8 *out = *dout;
  107. u32 data, fifostat;
  108. uint count;
  109. fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
  110. count = APPLE_SPI_FIFO_DEPTH -
  111. FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_TX, fifostat);
  112. while (*len > 0 && count > 0) {
  113. data = out ? *out++ : 0;
  114. writel(data, priv->base + APPLE_SPI_TXDATA);
  115. (*len)--;
  116. count--;
  117. }
  118. *dout = out;
  119. }
  120. /* Empty Rx FIFO. */
  121. static void apple_spi_rx(struct apple_spi_priv *priv, uint *len,
  122. void **din)
  123. {
  124. u8 *in = *din;
  125. u32 data, fifostat;
  126. uint count;
  127. fifostat = readl(priv->base + APPLE_SPI_FIFOSTAT);
  128. count = FIELD_GET(APPLE_SPI_FIFOSTAT_LEVEL_RX, fifostat);
  129. while (*len > 0 && count > 0) {
  130. data = readl(priv->base + APPLE_SPI_RXDATA);
  131. if (in)
  132. *in++ = data;
  133. (*len)--;
  134. count--;
  135. }
  136. *din = in;
  137. }
  138. static int apple_spi_xfer(struct udevice *dev, unsigned int bitlen,
  139. const void *dout, void *din, unsigned long flags)
  140. {
  141. struct apple_spi_priv *priv = dev_get_priv(dev->parent);
  142. unsigned long start = get_timer(0);
  143. uint txlen, rxlen;
  144. int ret = 0;
  145. if ((bitlen % 8) != 0)
  146. return -EINVAL;
  147. txlen = rxlen = bitlen / 8;
  148. if (flags & SPI_XFER_BEGIN)
  149. apple_spi_set_cs(priv, 1);
  150. if (txlen > 0) {
  151. /* Reset FIFOs */
  152. writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
  153. priv->base + APPLE_SPI_CTRL);
  154. /* Set the transfer length */
  155. writel(txlen, priv->base + APPLE_SPI_TXCNT);
  156. writel(rxlen, priv->base + APPLE_SPI_RXCNT);
  157. /* Prime transmit FIFO */
  158. apple_spi_tx(priv, &txlen, &dout);
  159. /* Start transfer */
  160. writel(APPLE_SPI_CTRL_RUN, priv->base + APPLE_SPI_CTRL);
  161. while ((txlen > 0 || rxlen > 0)) {
  162. apple_spi_rx(priv, &rxlen, &din);
  163. apple_spi_tx(priv, &txlen, &dout);
  164. if (get_timer(start) > APPLE_SPI_TIMEOUT_MS) {
  165. ret = -ETIMEDOUT;
  166. break;
  167. }
  168. }
  169. /* Stop transfer. */
  170. writel(0, priv->base + APPLE_SPI_CTRL);
  171. }
  172. if (flags & SPI_XFER_END)
  173. apple_spi_set_cs(priv, 0);
  174. return ret;
  175. }
  176. static int apple_spi_set_speed(struct udevice *dev, uint speed)
  177. {
  178. struct apple_spi_priv *priv = dev_get_priv(dev);
  179. u32 div;
  180. div = DIV_ROUND_UP(priv->clkfreq, speed);
  181. if (div < APPLE_SPI_CLKDIV_MIN)
  182. div = APPLE_SPI_CLKDIV_MIN;
  183. if (div > APPLE_SPI_CLKDIV_MAX)
  184. div = APPLE_SPI_CLKDIV_MAX;
  185. writel(div, priv->base + APPLE_SPI_CLKDIV);
  186. return 0;
  187. }
  188. static int apple_spi_set_mode(struct udevice *bus, uint mode)
  189. {
  190. return 0;
  191. }
  192. struct dm_spi_ops apple_spi_ops = {
  193. .xfer = apple_spi_xfer,
  194. .set_speed = apple_spi_set_speed,
  195. .set_mode = apple_spi_set_mode,
  196. };
  197. static int apple_spi_probe(struct udevice *dev)
  198. {
  199. struct apple_spi_priv *priv = dev_get_priv(dev);
  200. struct clk clkdev;
  201. int ret;
  202. priv->base = dev_read_addr_ptr(dev);
  203. if (!priv->base)
  204. return -EINVAL;
  205. ret = clk_get_by_index(dev, 0, &clkdev);
  206. if (ret)
  207. return ret;
  208. priv->clkfreq = clk_get_rate(&clkdev);
  209. /* Set CS high (inactive) and disable override and auto-CS */
  210. writel(APPLE_SPI_PIN_CS, priv->base + APPLE_SPI_PIN);
  211. writel(readl(priv->base + APPLE_SPI_SHIFTCFG) & ~APPLE_SPI_SHIFTCFG_OVERRIDE_CS,
  212. priv->base + APPLE_SPI_SHIFTCFG);
  213. writel((readl(priv->base + APPLE_SPI_PINCFG) & ~APPLE_SPI_PINCFG_CS_IDLE_VAL) |
  214. APPLE_SPI_PINCFG_KEEP_CS, priv->base + APPLE_SPI_PINCFG);
  215. /* Reset FIFOs */
  216. writel(APPLE_SPI_CTRL_RX_RESET | APPLE_SPI_CTRL_TX_RESET,
  217. priv->base + APPLE_SPI_CTRL);
  218. /* Configure defaults */
  219. writel(FIELD_PREP(APPLE_SPI_CFG_MODE, APPLE_SPI_CFG_MODE_IRQ) |
  220. FIELD_PREP(APPLE_SPI_CFG_WORD_SIZE, APPLE_SPI_CFG_WORD_SIZE_8B) |
  221. FIELD_PREP(APPLE_SPI_CFG_FIFO_THRESH, APPLE_SPI_CFG_FIFO_THRESH_8B),
  222. priv->base + APPLE_SPI_CFG);
  223. return 0;
  224. }
  225. static const struct udevice_id apple_spi_of_match[] = {
  226. { .compatible = "apple,spi" },
  227. { /* sentinel */ }
  228. };
  229. U_BOOT_DRIVER(apple_spi) = {
  230. .name = "apple_spi",
  231. .id = UCLASS_SPI,
  232. .of_match = apple_spi_of_match,
  233. .probe = apple_spi_probe,
  234. .priv_auto = sizeof(struct apple_spi_priv),
  235. .ops = &apple_spi_ops,
  236. };