board.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <malloc.h>
  10. #include <serial.h>
  11. #include <stdio_dev.h>
  12. #include <version.h>
  13. #include <net.h>
  14. #include <environment.h>
  15. #include <nand.h>
  16. #include <onenand_uboot.h>
  17. #include <spi.h>
  18. #ifdef CONFIG_BITBANGMII
  19. #include <miiphy.h>
  20. #endif
  21. DECLARE_GLOBAL_DATA_PTR;
  22. ulong monitor_flash_len;
  23. static char *failed = "*** failed ***\n";
  24. int __board_early_init_f(void)
  25. {
  26. /*
  27. * Nothing to do in this dummy implementation
  28. */
  29. return 0;
  30. }
  31. int board_early_init_f(void)
  32. __attribute__((weak, alias("__board_early_init_f")));
  33. static int init_func_ram(void)
  34. {
  35. #ifdef CONFIG_BOARD_TYPES
  36. int board_type = gd->board_type;
  37. #else
  38. int board_type = 0; /* use dummy arg */
  39. #endif
  40. puts("DRAM: ");
  41. gd->ram_size = initdram(board_type);
  42. if (gd->ram_size > 0) {
  43. print_size(gd->ram_size, "\n");
  44. return 0;
  45. }
  46. puts(failed);
  47. return 1;
  48. }
  49. static int display_banner(void)
  50. {
  51. printf("\n\n%s\n\n", version_string);
  52. return 0;
  53. }
  54. #ifndef CONFIG_SYS_NO_FLASH
  55. static void display_flash_config(ulong size)
  56. {
  57. puts("Flash: ");
  58. print_size(size, "\n");
  59. }
  60. #endif
  61. static int init_baudrate(void)
  62. {
  63. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  64. return 0;
  65. }
  66. /*
  67. * Breath some life into the board...
  68. *
  69. * The first part of initialization is running from Flash memory;
  70. * its main purpose is to initialize the RAM so that we
  71. * can relocate the monitor code to RAM.
  72. */
  73. /*
  74. * All attempts to come up with a "common" initialization sequence
  75. * that works for all boards and architectures failed: some of the
  76. * requirements are just _too_ different. To get rid of the resulting
  77. * mess of board dependend #ifdef'ed code we now make the whole
  78. * initialization sequence configurable to the user.
  79. *
  80. * The requirements for any new initalization function is simple: it
  81. * receives a pointer to the "global data" structure as it's only
  82. * argument, and returns an integer return code, where 0 means
  83. * "continue" and != 0 means "fatal error, hang the system".
  84. */
  85. typedef int (init_fnc_t)(void);
  86. init_fnc_t *init_sequence[] = {
  87. board_early_init_f,
  88. timer_init,
  89. env_init, /* initialize environment */
  90. init_baudrate, /* initialize baudrate settings */
  91. serial_init, /* serial communications setup */
  92. console_init_f,
  93. display_banner, /* say that we are here */
  94. checkboard,
  95. init_func_ram,
  96. NULL,
  97. };
  98. void board_init_f(ulong bootflag)
  99. {
  100. gd_t gd_data, *id;
  101. bd_t *bd;
  102. init_fnc_t **init_fnc_ptr;
  103. ulong addr, addr_sp, len;
  104. ulong *s;
  105. /* Pointer is writable since we allocated a register for it.
  106. */
  107. gd = &gd_data;
  108. /* compiler optimization barrier needed for GCC >= 3.4 */
  109. __asm__ __volatile__("" : : : "memory");
  110. memset((void *)gd, 0, sizeof(gd_t));
  111. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  112. if ((*init_fnc_ptr)() != 0)
  113. hang();
  114. }
  115. /*
  116. * Now that we have DRAM mapped and working, we can
  117. * relocate the code and continue running from DRAM.
  118. */
  119. addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
  120. /* We can reserve some RAM "on top" here.
  121. */
  122. /* round down to next 4 kB limit.
  123. */
  124. addr &= ~(4096 - 1);
  125. debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
  126. /* Reserve memory for U-Boot code, data & bss
  127. * round down to next 16 kB limit
  128. */
  129. len = bss_end() - CONFIG_SYS_MONITOR_BASE;
  130. addr -= len;
  131. addr &= ~(16 * 1024 - 1);
  132. debug("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
  133. /* Reserve memory for malloc() arena.
  134. */
  135. addr_sp = addr - TOTAL_MALLOC_LEN;
  136. debug("Reserving %dk for malloc() at: %08lx\n",
  137. TOTAL_MALLOC_LEN >> 10, addr_sp);
  138. /*
  139. * (permanently) allocate a Board Info struct
  140. * and a permanent copy of the "global" data
  141. */
  142. addr_sp -= sizeof(bd_t);
  143. bd = (bd_t *)addr_sp;
  144. gd->bd = bd;
  145. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  146. sizeof(bd_t), addr_sp);
  147. addr_sp -= sizeof(gd_t);
  148. id = (gd_t *)addr_sp;
  149. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  150. sizeof(gd_t), addr_sp);
  151. /* Reserve memory for boot params.
  152. */
  153. addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
  154. bd->bi_boot_params = addr_sp;
  155. debug("Reserving %dk for boot params() at: %08lx\n",
  156. CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
  157. /*
  158. * Finally, we set up a new (bigger) stack.
  159. *
  160. * Leave some safety gap for SP, force alignment on 16 byte boundary
  161. * Clear initial stack frame
  162. */
  163. addr_sp -= 16;
  164. addr_sp &= ~0xF;
  165. s = (ulong *)addr_sp;
  166. *s-- = 0;
  167. *s-- = 0;
  168. addr_sp = (ulong)s;
  169. debug("Stack Pointer at: %08lx\n", addr_sp);
  170. /*
  171. * Save local variables to board info struct
  172. */
  173. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM */
  174. bd->bi_memsize = gd->ram_size; /* size of DRAM in bytes */
  175. memcpy(id, (void *)gd, sizeof(gd_t));
  176. relocate_code(addr_sp, id, addr);
  177. /* NOTREACHED - relocate_code() does not return */
  178. }
  179. /*
  180. * This is the next part if the initialization sequence: we are now
  181. * running from RAM and have a "normal" C environment, i. e. global
  182. * data can be written, BSS has been cleared, the stack size in not
  183. * that critical any more, etc.
  184. */
  185. void board_init_r(gd_t *id, ulong dest_addr)
  186. {
  187. #ifndef CONFIG_SYS_NO_FLASH
  188. ulong size;
  189. #endif
  190. bd_t *bd;
  191. gd = id;
  192. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  193. debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  194. gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
  195. monitor_flash_len = image_copy_end() - dest_addr;
  196. serial_initialize();
  197. bd = gd->bd;
  198. /* The Malloc area is immediately below the monitor copy in DRAM */
  199. mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
  200. TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
  201. #ifndef CONFIG_SYS_NO_FLASH
  202. /* configure available FLASH banks */
  203. size = flash_init();
  204. display_flash_config(size);
  205. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  206. bd->bi_flashsize = size;
  207. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  208. bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
  209. #else
  210. bd->bi_flashoffset = 0;
  211. #endif
  212. #else
  213. bd->bi_flashstart = 0;
  214. bd->bi_flashsize = 0;
  215. bd->bi_flashoffset = 0;
  216. #endif
  217. #ifdef CONFIG_CMD_NAND
  218. puts("NAND: ");
  219. nand_init(); /* go init the NAND */
  220. #endif
  221. #if defined(CONFIG_CMD_ONENAND)
  222. onenand_init();
  223. #endif
  224. /* relocate environment function pointers etc. */
  225. env_relocate();
  226. #if defined(CONFIG_PCI)
  227. /*
  228. * Do pci configuration
  229. */
  230. pci_init();
  231. #endif
  232. /** leave this here (after malloc(), environment and PCI are working) **/
  233. /* Initialize stdio devices */
  234. stdio_init();
  235. jumptable_init();
  236. /* Initialize the console (after the relocation and devices init) */
  237. console_init_r();
  238. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  239. /* Initialize from environment */
  240. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  241. #ifdef CONFIG_CMD_SPI
  242. puts("SPI: ");
  243. spi_init(); /* go init the SPI */
  244. puts("ready\n");
  245. #endif
  246. #if defined(CONFIG_MISC_INIT_R)
  247. /* miscellaneous platform dependent initialisations */
  248. misc_init_r();
  249. #endif
  250. #ifdef CONFIG_BITBANGMII
  251. bb_miiphy_init();
  252. #endif
  253. #if defined(CONFIG_CMD_NET)
  254. puts("Net: ");
  255. eth_initialize(gd->bd);
  256. #endif
  257. /* main_loop() can return to retry autoboot, if so just run it again. */
  258. for (;;)
  259. main_loop();
  260. /* NOTREACHED - no way out of command loop except booting */
  261. }