board.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <netdev.h>
  15. #include <miiphy.h>
  16. #include <serial.h>
  17. #ifdef CONFIG_SPL_BUILD
  18. #include <spl.h>
  19. #endif
  20. #include <asm/gpio.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/clock.h>
  23. #include <asm/arch/gpio.h>
  24. #include <asm/arch/sys_proto.h>
  25. #include <asm/arch/timer.h>
  26. #include <linux/compiler.h>
  27. struct fel_stash {
  28. uint32_t sp;
  29. uint32_t lr;
  30. };
  31. struct fel_stash fel_stash __attribute__((section(".data")));
  32. static int gpio_init(void)
  33. {
  34. #if CONFIG_CONS_INDEX == 1 && defined(CONFIG_UART0_PORT_F)
  35. #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
  36. /* disable GPB22,23 as uart0 tx,rx to avoid conflict */
  37. sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUNXI_GPIO_INPUT);
  38. sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUNXI_GPIO_INPUT);
  39. #endif
  40. sunxi_gpio_set_cfgpin(SUNXI_GPF(2), SUNXI_GPF2_UART0_TX);
  41. sunxi_gpio_set_cfgpin(SUNXI_GPF(4), SUNXI_GPF4_UART0_RX);
  42. sunxi_gpio_set_pull(SUNXI_GPF(4), 1);
  43. #elif CONFIG_CONS_INDEX == 1 && (defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I))
  44. sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUN4I_GPB22_UART0_TX);
  45. sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUN4I_GPB23_UART0_RX);
  46. sunxi_gpio_set_pull(SUNXI_GPB(23), SUNXI_GPIO_PULL_UP);
  47. #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN5I)
  48. sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN5I_GPB19_UART0_TX);
  49. sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN5I_GPB20_UART0_RX);
  50. sunxi_gpio_set_pull(SUNXI_GPB(20), SUNXI_GPIO_PULL_UP);
  51. #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN6I)
  52. sunxi_gpio_set_cfgpin(SUNXI_GPH(20), SUN6I_GPH20_UART0_TX);
  53. sunxi_gpio_set_cfgpin(SUNXI_GPH(21), SUN6I_GPH21_UART0_RX);
  54. sunxi_gpio_set_pull(SUNXI_GPH(21), SUNXI_GPIO_PULL_UP);
  55. #elif CONFIG_CONS_INDEX == 2 && defined(CONFIG_MACH_SUN5I)
  56. sunxi_gpio_set_cfgpin(SUNXI_GPG(3), SUN5I_GPG3_UART1_TX);
  57. sunxi_gpio_set_cfgpin(SUNXI_GPG(4), SUN5I_GPG4_UART1_RX);
  58. sunxi_gpio_set_pull(SUNXI_GPG(4), SUNXI_GPIO_PULL_UP);
  59. #elif CONFIG_CONS_INDEX == 5 && defined(CONFIG_MACH_SUN8I)
  60. sunxi_gpio_set_cfgpin(SUNXI_GPL(2), SUN8I_GPL2_R_UART_TX);
  61. sunxi_gpio_set_cfgpin(SUNXI_GPL(3), SUN8I_GPL3_R_UART_RX);
  62. sunxi_gpio_set_pull(SUNXI_GPL(3), SUNXI_GPIO_PULL_UP);
  63. #else
  64. #error Unsupported console port number. Please fix pin mux settings in board.c
  65. #endif
  66. return 0;
  67. }
  68. void spl_board_load_image(void)
  69. {
  70. debug("Returning to FEL sp=%x, lr=%x\n", fel_stash.sp, fel_stash.lr);
  71. return_to_fel(fel_stash.sp, fel_stash.lr);
  72. }
  73. void s_init(void)
  74. {
  75. #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I
  76. /* Magic (undocmented) value taken from boot0, without this DRAM
  77. * access gets messed up (seems cache related) */
  78. setbits_le32(SUNXI_SRAMC_BASE + 0x44, 0x1800);
  79. #endif
  80. #if !defined CONFIG_SPL_BUILD && (defined CONFIG_MACH_SUN7I || \
  81. defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I)
  82. /* Enable SMP mode for CPU0, by setting bit 6 of Auxiliary Ctl reg */
  83. asm volatile(
  84. "mrc p15, 0, r0, c1, c0, 1\n"
  85. "orr r0, r0, #1 << 6\n"
  86. "mcr p15, 0, r0, c1, c0, 1\n");
  87. #endif
  88. clock_init();
  89. timer_init();
  90. gpio_init();
  91. i2c_init_board();
  92. }
  93. #ifdef CONFIG_SPL_BUILD
  94. /* The sunxi internal brom will try to loader external bootloader
  95. * from mmc0, nand flash, mmc2.
  96. * Unfortunately we can't check how SPL was loaded so assume
  97. * it's always the first SD/MMC controller
  98. */
  99. u32 spl_boot_device(void)
  100. {
  101. /*
  102. * Have we been asked to return to the FEL portion of the boot ROM?
  103. * TODO: We need a more robust test here, or bracket this with
  104. * #ifdef CONFIG_SPL_FEL.
  105. */
  106. if (fel_stash.lr >= 0xffff0000 && fel_stash.lr < 0xffff4000)
  107. return BOOT_DEVICE_BOARD;
  108. return BOOT_DEVICE_MMC1;
  109. }
  110. /* No confirmation data available in SPL yet. Hardcode bootmode */
  111. u32 spl_boot_mode(void)
  112. {
  113. return MMCSD_MODE_RAW;
  114. }
  115. void board_init_f(ulong dummy)
  116. {
  117. preloader_console_init();
  118. #ifdef CONFIG_SPL_I2C_SUPPORT
  119. /* Needed early by sunxi_board_init if PMU is enabled */
  120. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  121. #endif
  122. sunxi_board_init();
  123. /* Clear the BSS. */
  124. memset(__bss_start, 0, __bss_end - __bss_start);
  125. board_init_r(NULL, 0);
  126. }
  127. #endif
  128. void reset_cpu(ulong addr)
  129. {
  130. #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
  131. static const struct sunxi_wdog *wdog =
  132. &((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog;
  133. /* Set the watchdog for its shortest interval (.5s) and wait */
  134. writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode);
  135. writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl);
  136. while (1) {
  137. /* sun5i sometimes gets stuck without this */
  138. writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode);
  139. }
  140. #else /* CONFIG_MACH_SUN6I || CONFIG_MACH_SUN8I || .. */
  141. static const struct sunxi_wdog *wdog =
  142. ((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog;
  143. /* Set the watchdog for its shortest interval (.5s) and wait */
  144. writel(WDT_CFG_RESET, &wdog->cfg);
  145. writel(WDT_MODE_EN, &wdog->mode);
  146. writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl);
  147. #endif
  148. }
  149. #ifndef CONFIG_SYS_DCACHE_OFF
  150. void enable_caches(void)
  151. {
  152. /* Enable D-cache. I-cache is already enabled in start.S */
  153. dcache_enable();
  154. }
  155. #endif
  156. #ifdef CONFIG_CMD_NET
  157. /*
  158. * Initializes on-chip ethernet controllers.
  159. * to override, implement board_eth_init()
  160. */
  161. int cpu_eth_init(bd_t *bis)
  162. {
  163. __maybe_unused int rc;
  164. #ifdef CONFIG_MACPWR
  165. gpio_direction_output(CONFIG_MACPWR, 1);
  166. mdelay(200);
  167. #endif
  168. #ifdef CONFIG_SUNXI_EMAC
  169. rc = sunxi_emac_initialize(bis);
  170. if (rc < 0) {
  171. printf("sunxi: failed to initialize emac\n");
  172. return rc;
  173. }
  174. #endif
  175. #ifdef CONFIG_SUNXI_GMAC
  176. rc = sunxi_gmac_initialize(bis);
  177. if (rc < 0) {
  178. printf("sunxi: failed to initialize gmac\n");
  179. return rc;
  180. }
  181. #endif
  182. return 0;
  183. }
  184. #endif