sunxi_spi_spl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2016 Siarhei Siamashka <siarhei.siamashka@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <spl.h>
  8. #include <asm/gpio.h>
  9. #include <asm/io.h>
  10. #ifdef CONFIG_SPL_OS_BOOT
  11. #error CONFIG_SPL_OS_BOOT is not supported yet
  12. #endif
  13. /*
  14. * This is a very simple U-Boot image loading implementation, trying to
  15. * replicate what the boot ROM is doing when loading the SPL. Because we
  16. * know the exact pins where the SPI Flash is connected and also know
  17. * that the Read Data Bytes (03h) command is supported, the hardware
  18. * configuration is very simple and we don't need the extra flexibility
  19. * of the SPI framework. Moreover, we rely on the default settings of
  20. * the SPI controler hardware registers and only adjust what needs to
  21. * be changed. This is good for the code size and this implementation
  22. * adds less than 400 bytes to the SPL.
  23. *
  24. * There are two variants of the SPI controller in Allwinner SoCs:
  25. * A10/A13/A20 (sun4i variant) and everything else (sun6i variant).
  26. * Both of them are supported.
  27. *
  28. * The pin mixing part is SoC specific and only A10/A13/A20/H3/A64 are
  29. * supported at the moment.
  30. */
  31. /*****************************************************************************/
  32. /* SUN4I variant of the SPI controller */
  33. /*****************************************************************************/
  34. #define SUN4I_SPI0_CCTL (0x01C05000 + 0x1C)
  35. #define SUN4I_SPI0_CTL (0x01C05000 + 0x08)
  36. #define SUN4I_SPI0_RX (0x01C05000 + 0x00)
  37. #define SUN4I_SPI0_TX (0x01C05000 + 0x04)
  38. #define SUN4I_SPI0_FIFO_STA (0x01C05000 + 0x28)
  39. #define SUN4I_SPI0_BC (0x01C05000 + 0x20)
  40. #define SUN4I_SPI0_TC (0x01C05000 + 0x24)
  41. #define SUN4I_CTL_ENABLE BIT(0)
  42. #define SUN4I_CTL_MASTER BIT(1)
  43. #define SUN4I_CTL_TF_RST BIT(8)
  44. #define SUN4I_CTL_RF_RST BIT(9)
  45. #define SUN4I_CTL_XCH BIT(10)
  46. /*****************************************************************************/
  47. /* SUN6I variant of the SPI controller */
  48. /*****************************************************************************/
  49. #define SUN6I_SPI0_CCTL (0x01C68000 + 0x24)
  50. #define SUN6I_SPI0_GCR (0x01C68000 + 0x04)
  51. #define SUN6I_SPI0_TCR (0x01C68000 + 0x08)
  52. #define SUN6I_SPI0_FIFO_STA (0x01C68000 + 0x1C)
  53. #define SUN6I_SPI0_MBC (0x01C68000 + 0x30)
  54. #define SUN6I_SPI0_MTC (0x01C68000 + 0x34)
  55. #define SUN6I_SPI0_BCC (0x01C68000 + 0x38)
  56. #define SUN6I_SPI0_TXD (0x01C68000 + 0x200)
  57. #define SUN6I_SPI0_RXD (0x01C68000 + 0x300)
  58. #define SUN6I_CTL_ENABLE BIT(0)
  59. #define SUN6I_CTL_MASTER BIT(1)
  60. #define SUN6I_CTL_SRST BIT(31)
  61. #define SUN6I_TCR_XCH BIT(31)
  62. /*****************************************************************************/
  63. #define CCM_AHB_GATING0 (0x01C20000 + 0x60)
  64. #define CCM_SPI0_CLK (0x01C20000 + 0xA0)
  65. #define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0)
  66. #define AHB_RESET_SPI0_SHIFT 20
  67. #define AHB_GATE_OFFSET_SPI0 20
  68. #define SPI0_CLK_DIV_BY_2 0x1000
  69. #define SPI0_CLK_DIV_BY_4 0x1001
  70. /*****************************************************************************/
  71. /*
  72. * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting
  73. * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3.
  74. */
  75. static void spi0_pinmux_setup(unsigned int pin_function)
  76. {
  77. unsigned int pin;
  78. for (pin = SUNXI_GPC(0); pin <= SUNXI_GPC(2); pin++)
  79. sunxi_gpio_set_cfgpin(pin, pin_function);
  80. if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I))
  81. sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function);
  82. else
  83. sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function);
  84. }
  85. /*
  86. * Setup 6 MHz from OSC24M (because the BROM is doing the same).
  87. */
  88. static void spi0_enable_clock(void)
  89. {
  90. /* Deassert SPI0 reset on SUN6I */
  91. if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
  92. setbits_le32(SUN6I_BUS_SOFT_RST_REG0,
  93. (1 << AHB_RESET_SPI0_SHIFT));
  94. /* Open the SPI0 gate */
  95. setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
  96. /* Divide by 4 */
  97. writel(SPI0_CLK_DIV_BY_4, IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) ?
  98. SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL);
  99. /* 24MHz from OSC24M */
  100. writel((1 << 31), CCM_SPI0_CLK);
  101. if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I)) {
  102. /* Enable SPI in the master mode and do a soft reset */
  103. setbits_le32(SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
  104. SUN6I_CTL_ENABLE |
  105. SUN6I_CTL_SRST);
  106. /* Wait for completion */
  107. while (readl(SUN6I_SPI0_GCR) & SUN6I_CTL_SRST)
  108. ;
  109. } else {
  110. /* Enable SPI in the master mode and reset FIFO */
  111. setbits_le32(SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
  112. SUN4I_CTL_ENABLE |
  113. SUN4I_CTL_TF_RST |
  114. SUN4I_CTL_RF_RST);
  115. }
  116. }
  117. static void spi0_disable_clock(void)
  118. {
  119. /* Disable the SPI0 controller */
  120. if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
  121. clrbits_le32(SUN6I_SPI0_GCR, SUN6I_CTL_MASTER |
  122. SUN6I_CTL_ENABLE);
  123. else
  124. clrbits_le32(SUN4I_SPI0_CTL, SUN4I_CTL_MASTER |
  125. SUN4I_CTL_ENABLE);
  126. /* Disable the SPI0 clock */
  127. writel(0, CCM_SPI0_CLK);
  128. /* Close the SPI0 gate */
  129. clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0));
  130. /* Assert SPI0 reset on SUN6I */
  131. if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
  132. clrbits_le32(SUN6I_BUS_SOFT_RST_REG0,
  133. (1 << AHB_RESET_SPI0_SHIFT));
  134. }
  135. static int spi0_init(void)
  136. {
  137. unsigned int pin_function = SUNXI_GPC_SPI0;
  138. if (IS_ENABLED(CONFIG_MACH_SUN50I))
  139. pin_function = SUN50I_GPC_SPI0;
  140. spi0_pinmux_setup(pin_function);
  141. spi0_enable_clock();
  142. }
  143. static void spi0_deinit(void)
  144. {
  145. /* New SoCs can disable pins, older could only set them as input */
  146. unsigned int pin_function = SUNXI_GPIO_INPUT;
  147. if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I))
  148. pin_function = SUNXI_GPIO_DISABLE;
  149. spi0_disable_clock();
  150. spi0_pinmux_setup(pin_function);
  151. }
  152. /*****************************************************************************/
  153. #define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */
  154. static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize,
  155. u32 spi_ctl_reg,
  156. u32 spi_ctl_xch_bitmask,
  157. u32 spi_fifo_reg,
  158. u32 spi_tx_reg,
  159. u32 spi_rx_reg,
  160. u32 spi_bc_reg,
  161. u32 spi_tc_reg,
  162. u32 spi_bcc_reg)
  163. {
  164. writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */
  165. writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */
  166. if (spi_bcc_reg)
  167. writel(4, spi_bcc_reg); /* SUN6I also needs this */
  168. /* Send the Read Data Bytes (03h) command header */
  169. writeb(0x03, spi_tx_reg);
  170. writeb((u8)(addr >> 16), spi_tx_reg);
  171. writeb((u8)(addr >> 8), spi_tx_reg);
  172. writeb((u8)(addr), spi_tx_reg);
  173. /* Start the data transfer */
  174. setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask);
  175. /* Wait until everything is received in the RX FIFO */
  176. while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize)
  177. ;
  178. /* Skip 4 bytes */
  179. readl(spi_rx_reg);
  180. /* Read the data */
  181. while (bufsize-- > 0)
  182. *buf++ = readb(spi_rx_reg);
  183. /* tSHSL time is up to 100 ns in various SPI flash datasheets */
  184. udelay(1);
  185. }
  186. static void spi0_read_data(void *buf, u32 addr, u32 len)
  187. {
  188. u8 *buf8 = buf;
  189. u32 chunk_len;
  190. while (len > 0) {
  191. chunk_len = len;
  192. if (chunk_len > SPI_READ_MAX_SIZE)
  193. chunk_len = SPI_READ_MAX_SIZE;
  194. if (IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I)) {
  195. sunxi_spi0_read_data(buf8, addr, chunk_len,
  196. SUN6I_SPI0_TCR,
  197. SUN6I_TCR_XCH,
  198. SUN6I_SPI0_FIFO_STA,
  199. SUN6I_SPI0_TXD,
  200. SUN6I_SPI0_RXD,
  201. SUN6I_SPI0_MBC,
  202. SUN6I_SPI0_MTC,
  203. SUN6I_SPI0_BCC);
  204. } else {
  205. sunxi_spi0_read_data(buf8, addr, chunk_len,
  206. SUN4I_SPI0_CTL,
  207. SUN4I_CTL_XCH,
  208. SUN4I_SPI0_FIFO_STA,
  209. SUN4I_SPI0_TX,
  210. SUN4I_SPI0_RX,
  211. SUN4I_SPI0_BC,
  212. SUN4I_SPI0_TC,
  213. 0);
  214. }
  215. len -= chunk_len;
  216. buf8 += chunk_len;
  217. addr += chunk_len;
  218. }
  219. }
  220. /*****************************************************************************/
  221. int spl_spi_load_image(void)
  222. {
  223. int err;
  224. struct image_header *header;
  225. header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
  226. spi0_init();
  227. spi0_read_data((void *)header, CONFIG_SYS_SPI_U_BOOT_OFFS, 0x40);
  228. err = spl_parse_image_header(&spl_image, header);
  229. if (err)
  230. return err;
  231. spi0_read_data((void *)spl_image.load_addr, CONFIG_SYS_SPI_U_BOOT_OFFS,
  232. spl_image.size);
  233. spi0_deinit();
  234. return 0;
  235. }