board.c 7.5 KB

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