board.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
  3. *
  4. * (C) Copyright 2007-2011
  5. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  6. * Tom Cubie <tangliang@allwinnertech.com>
  7. *
  8. * Some init for sunxi platform.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <i2c.h>
  14. #include <serial.h>
  15. #ifdef CONFIG_SPL_BUILD
  16. #include <spl.h>
  17. #endif
  18. #include <asm/gpio.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/clock.h>
  21. #include <asm/arch/gpio.h>
  22. #include <asm/arch/sys_proto.h>
  23. #include <asm/arch/timer.h>
  24. #include <linux/compiler.h>
  25. struct fel_stash {
  26. uint32_t sp;
  27. uint32_t lr;
  28. uint32_t cpsr;
  29. uint32_t sctlr;
  30. uint32_t vbar;
  31. uint32_t cr;
  32. };
  33. struct fel_stash fel_stash __attribute__((section(".data")));
  34. static int gpio_init(void)
  35. {
  36. #if CONFIG_CONS_INDEX == 1 && defined(CONFIG_UART0_PORT_F)
  37. #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
  38. /* disable GPB22,23 as uart0 tx,rx to avoid conflict */
  39. sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUNXI_GPIO_INPUT);
  40. sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUNXI_GPIO_INPUT);
  41. #endif
  42. #if defined(CONFIG_MACH_SUN8I)
  43. sunxi_gpio_set_cfgpin(SUNXI_GPF(2), SUN8I_GPF_UART0_TX);
  44. sunxi_gpio_set_cfgpin(SUNXI_GPF(4), SUN8I_GPF_UART0_RX);
  45. #else
  46. sunxi_gpio_set_cfgpin(SUNXI_GPF(2), SUNXI_GPF_UART0_TX);
  47. sunxi_gpio_set_cfgpin(SUNXI_GPF(4), SUNXI_GPF_UART0_RX);
  48. #endif
  49. sunxi_gpio_set_pull(SUNXI_GPF(4), 1);
  50. #elif CONFIG_CONS_INDEX == 1 && (defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I))
  51. sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUN4I_GPB_UART0);
  52. sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUN4I_GPB_UART0);
  53. sunxi_gpio_set_pull(SUNXI_GPB(23), SUNXI_GPIO_PULL_UP);
  54. #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN5I)
  55. sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN5I_GPB_UART0);
  56. sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN5I_GPB_UART0);
  57. sunxi_gpio_set_pull(SUNXI_GPB(20), SUNXI_GPIO_PULL_UP);
  58. #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN6I)
  59. sunxi_gpio_set_cfgpin(SUNXI_GPH(20), SUN6I_GPH_UART0);
  60. sunxi_gpio_set_cfgpin(SUNXI_GPH(21), SUN6I_GPH_UART0);
  61. sunxi_gpio_set_pull(SUNXI_GPH(21), SUNXI_GPIO_PULL_UP);
  62. #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN9I)
  63. sunxi_gpio_set_cfgpin(SUNXI_GPH(12), SUN9I_GPH_UART0);
  64. sunxi_gpio_set_cfgpin(SUNXI_GPH(13), SUN9I_GPH_UART0);
  65. sunxi_gpio_set_pull(SUNXI_GPH(13), SUNXI_GPIO_PULL_UP);
  66. #elif CONFIG_CONS_INDEX == 2 && defined(CONFIG_MACH_SUN5I)
  67. sunxi_gpio_set_cfgpin(SUNXI_GPG(3), SUN5I_GPG_UART1);
  68. sunxi_gpio_set_cfgpin(SUNXI_GPG(4), SUN5I_GPG_UART1);
  69. sunxi_gpio_set_pull(SUNXI_GPG(4), SUNXI_GPIO_PULL_UP);
  70. #elif CONFIG_CONS_INDEX == 3 && defined(CONFIG_MACH_SUN8I)
  71. sunxi_gpio_set_cfgpin(SUNXI_GPB(0), SUN8I_GPB_UART2);
  72. sunxi_gpio_set_cfgpin(SUNXI_GPB(1), SUN8I_GPB_UART2);
  73. sunxi_gpio_set_pull(SUNXI_GPB(1), SUNXI_GPIO_PULL_UP);
  74. #elif CONFIG_CONS_INDEX == 5 && defined(CONFIG_MACH_SUN8I)
  75. sunxi_gpio_set_cfgpin(SUNXI_GPL(2), SUN8I_GPL_R_UART);
  76. sunxi_gpio_set_cfgpin(SUNXI_GPL(3), SUN8I_GPL_R_UART);
  77. sunxi_gpio_set_pull(SUNXI_GPL(3), SUNXI_GPIO_PULL_UP);
  78. #else
  79. #error Unsupported console port number. Please fix pin mux settings in board.c
  80. #endif
  81. return 0;
  82. }
  83. void spl_board_load_image(void)
  84. {
  85. debug("Returning to FEL sp=%x, lr=%x\n", fel_stash.sp, fel_stash.lr);
  86. return_to_fel(fel_stash.sp, fel_stash.lr);
  87. }
  88. void s_init(void)
  89. {
  90. #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I_A23
  91. /* Magic (undocmented) value taken from boot0, without this DRAM
  92. * access gets messed up (seems cache related) */
  93. setbits_le32(SUNXI_SRAMC_BASE + 0x44, 0x1800);
  94. #endif
  95. #if defined CONFIG_MACH_SUN6I || \
  96. defined CONFIG_MACH_SUN7I || \
  97. defined CONFIG_MACH_SUN8I
  98. /* Enable SMP mode for CPU0, by setting bit 6 of Auxiliary Ctl reg */
  99. asm volatile(
  100. "mrc p15, 0, r0, c1, c0, 1\n"
  101. "orr r0, r0, #1 << 6\n"
  102. "mcr p15, 0, r0, c1, c0, 1\n");
  103. #endif
  104. clock_init();
  105. timer_init();
  106. gpio_init();
  107. i2c_init_board();
  108. }
  109. #ifdef CONFIG_SPL_BUILD
  110. /* The sunxi internal brom will try to loader external bootloader
  111. * from mmc0, nand flash, mmc2.
  112. *
  113. * Unfortunately we can't check how SPL was loaded so assume it's
  114. * always the first SD/MMC controller, unless it was explicitly
  115. * stated that SPL is on nand flash.
  116. */
  117. u32 spl_boot_device(void)
  118. {
  119. #if defined(CONFIG_SPL_NAND_SUPPORT)
  120. /*
  121. * This is compile time configuration informing SPL, that it
  122. * was loaded from nand flash.
  123. */
  124. return BOOT_DEVICE_NAND;
  125. #else
  126. /*
  127. * When booting from the SD card, the "eGON.BT0" signature is expected
  128. * to be found in memory at the address 0x0004 (see the "mksunxiboot"
  129. * tool, which generates this header).
  130. *
  131. * When booting in the FEL mode over USB, this signature is patched in
  132. * memory and replaced with something else by the 'fel' tool. This other
  133. * signature is selected in such a way, that it can't be present in a
  134. * valid bootable SD card image (because the BROM would refuse to
  135. * execute the SPL in this case).
  136. *
  137. * This branch is just making a decision at runtime whether to load
  138. * the main u-boot binary from the SD card (if the "eGON.BT0" signature
  139. * is found) or return to the FEL code in the BROM to wait and receive
  140. * the main u-boot binary over USB.
  141. */
  142. if (readl(4) == 0x4E4F4765 && readl(8) == 0x3054422E) /* eGON.BT0 */
  143. return BOOT_DEVICE_MMC1;
  144. else
  145. return BOOT_DEVICE_BOARD;
  146. #endif
  147. }
  148. /* No confirmation data available in SPL yet. Hardcode bootmode */
  149. u32 spl_boot_mode(void)
  150. {
  151. return MMCSD_MODE_RAW;
  152. }
  153. void board_init_f(ulong dummy)
  154. {
  155. preloader_console_init();
  156. #ifdef CONFIG_SPL_I2C_SUPPORT
  157. /* Needed early by sunxi_board_init if PMU is enabled */
  158. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  159. #endif
  160. sunxi_board_init();
  161. /* Clear the BSS. */
  162. memset(__bss_start, 0, __bss_end - __bss_start);
  163. board_init_r(NULL, 0);
  164. }
  165. #endif
  166. void reset_cpu(ulong addr)
  167. {
  168. #ifdef CONFIG_SUNXI_GEN_SUN4I
  169. static const struct sunxi_wdog *wdog =
  170. &((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog;
  171. /* Set the watchdog for its shortest interval (.5s) and wait */
  172. writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode);
  173. writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl);
  174. while (1) {
  175. /* sun5i sometimes gets stuck without this */
  176. writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode);
  177. }
  178. #endif
  179. #ifdef CONFIG_SUNXI_GEN_SUN6I
  180. static const struct sunxi_wdog *wdog =
  181. ((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog;
  182. /* Set the watchdog for its shortest interval (.5s) and wait */
  183. writel(WDT_CFG_RESET, &wdog->cfg);
  184. writel(WDT_MODE_EN, &wdog->mode);
  185. writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl);
  186. #endif
  187. }
  188. #ifndef CONFIG_SYS_DCACHE_OFF
  189. void enable_caches(void)
  190. {
  191. /* Enable D-cache. I-cache is already enabled in start.S */
  192. dcache_enable();
  193. }
  194. #endif
  195. #ifdef CONFIG_CMD_NET
  196. /*
  197. * Initializes on-chip ethernet controllers.
  198. * to override, implement board_eth_init()
  199. */
  200. int cpu_eth_init(bd_t *bis)
  201. {
  202. __maybe_unused int rc;
  203. #ifdef CONFIG_MACPWR
  204. gpio_request(CONFIG_MACPWR, "macpwr");
  205. gpio_direction_output(CONFIG_MACPWR, 1);
  206. mdelay(200);
  207. #endif
  208. #ifdef CONFIG_SUNXI_GMAC
  209. rc = sunxi_gmac_initialize(bis);
  210. if (rc < 0) {
  211. printf("sunxi: failed to initialize gmac\n");
  212. return rc;
  213. }
  214. #endif
  215. return 0;
  216. }
  217. #endif