board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * (C) Copyright 2002-2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright (C) 2011 Andes Technology Corporation
  6. * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
  7. * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <malloc.h>
  30. #include <stdio_dev.h>
  31. #include <timestamp.h>
  32. #include <version.h>
  33. #include <net.h>
  34. #include <serial.h>
  35. #include <nand.h>
  36. #include <onenand_uboot.h>
  37. #include <mmc.h>
  38. #include <asm/sections.h>
  39. DECLARE_GLOBAL_DATA_PTR;
  40. ulong monitor_flash_len;
  41. /*
  42. * Init Utilities
  43. */
  44. #if !defined(CONFIG_BAUDRATE)
  45. #define CONFIG_BAUDRATE 38400
  46. #endif
  47. static int init_baudrate(void)
  48. {
  49. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  50. return 0;
  51. }
  52. /*
  53. * WARNING: this code looks "cleaner" than the PowerPC version, but
  54. * has the disadvantage that you either get nothing, or everything.
  55. * On PowerPC, you might see "DRAM: " before the system hangs - which
  56. * gives a simple yet clear indication which part of the
  57. * initialization if failing.
  58. */
  59. static int display_dram_config(void)
  60. {
  61. int i;
  62. #ifdef DEBUG
  63. puts("RAM Configuration:\n");
  64. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  65. printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  66. print_size(gd->bd->bi_dram[i].size, "\n");
  67. }
  68. #else
  69. ulong size = 0;
  70. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
  71. size += gd->bd->bi_dram[i].size;
  72. puts("DRAM: ");
  73. print_size(size, "\n");
  74. #endif
  75. return 0;
  76. }
  77. #ifndef CONFIG_SYS_NO_FLASH
  78. static void display_flash_config(ulong size)
  79. {
  80. puts("Flash: ");
  81. print_size(size, "\n");
  82. }
  83. #endif /* CONFIG_SYS_NO_FLASH */
  84. #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
  85. #include <pci.h>
  86. static int nds32_pci_init(void)
  87. {
  88. pci_init();
  89. return 0;
  90. }
  91. #endif /* CONFIG_CMD_PCI || CONFIG_PCI */
  92. #if defined(CONFIG_PMU) || defined(CONFIG_PCU)
  93. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  94. static int pmu_init(void)
  95. {
  96. #if defined(CONFIG_FTPMU010_POWER)
  97. #ifdef __NDS32_N1213_43U1H__ /* AG101: internal definition in toolchain */
  98. ftpmu010_sdram_clk_disable(CONFIG_SYS_FTPMU010_PDLLCR0_HCLKOUTDIS);
  99. ftpmu010_mfpsr_select_dev(FTPMU010_MFPSR_AC97CLKSEL);
  100. ftpmu010_sdramhtc_set(CONFIG_SYS_FTPMU010_SDRAMHTC);
  101. #endif /* __NDS32_N1213_43U1H__ */
  102. #endif
  103. return 0;
  104. }
  105. #endif
  106. #endif
  107. /*
  108. * Breathe some life into the board...
  109. *
  110. * Initialize a serial port as console, and carry out some hardware
  111. * tests.
  112. *
  113. * The first part of initialization is running from Flash memory;
  114. * its main purpose is to initialize the RAM so that we
  115. * can relocate the monitor code to RAM.
  116. */
  117. /*
  118. * All attempts to come up with a "common" initialization sequence
  119. * that works for all boards and architectures failed: some of the
  120. * requirements are just _too_ different. To get rid of the resulting
  121. * mess of board dependent #ifdef'ed code we now make the whole
  122. * initialization sequence configurable to the user.
  123. *
  124. * The requirements for any new initalization function is simple: it
  125. * receives a pointer to the "global data" structure as it's only
  126. * argument, and returns an integer return code, where 0 means
  127. * "continue" and != 0 means "fatal error, hang the system".
  128. */
  129. typedef int (init_fnc_t)(void);
  130. void __dram_init_banksize(void)
  131. {
  132. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  133. gd->bd->bi_dram[0].size = gd->ram_size;
  134. }
  135. void dram_init_banksize(void)
  136. __attribute__((weak, alias("__dram_init_banksize")));
  137. init_fnc_t *init_sequence[] = {
  138. #if defined(CONFIG_ARCH_CPU_INIT)
  139. arch_cpu_init, /* basic arch cpu dependent setup */
  140. #endif
  141. #if defined(CONFIG_PMU) || defined(CONFIG_PCU)
  142. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  143. pmu_init,
  144. #endif
  145. #endif
  146. board_init, /* basic board dependent setup */
  147. #if defined(CONFIG_USE_IRQ)
  148. interrupt_init, /* set up exceptions */
  149. #endif
  150. timer_init, /* initialize timer */
  151. env_init, /* initialize environment */
  152. init_baudrate, /* initialze baudrate settings */
  153. serial_init, /* serial communications setup */
  154. console_init_f, /* stage 1 init of console */
  155. #if defined(CONFIG_DISPLAY_BOARDINFO)
  156. checkboard, /* display board info */
  157. #endif
  158. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  159. init_func_i2c,
  160. #endif
  161. dram_init, /* configure available RAM banks */
  162. display_dram_config,
  163. NULL,
  164. };
  165. void board_init_f(ulong bootflag)
  166. {
  167. bd_t *bd;
  168. init_fnc_t **init_fnc_ptr;
  169. gd_t *id;
  170. ulong addr, addr_sp;
  171. /* Pointer is writable since we allocated a register for it */
  172. gd = (gd_t *) ((CONFIG_SYS_INIT_SP_ADDR) & ~0x07);
  173. memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
  174. gd->mon_len = (unsigned int)(&__bss_end) - (unsigned int)(&_start);
  175. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  176. if ((*init_fnc_ptr)() != 0)
  177. hang();
  178. }
  179. debug("monitor len: %08lX\n", gd->mon_len);
  180. /*
  181. * Ram is setup, size stored in gd !!
  182. */
  183. debug("ramsize: %08lX\n", gd->ram_size);
  184. addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
  185. /* round down to next 4 kB limit */
  186. addr &= ~(4096 - 1);
  187. debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
  188. #ifdef CONFIG_LCD
  189. #ifdef CONFIG_FB_ADDR
  190. gd->fb_base = CONFIG_FB_ADDR;
  191. #else
  192. /* reserve memory for LCD display (always full pages) */
  193. addr = lcd_setmem(addr);
  194. gd->fb_base = addr;
  195. #endif /* CONFIG_FB_ADDR */
  196. #endif /* CONFIG_LCD */
  197. /*
  198. * reserve memory for U-Boot code, data & bss
  199. * round down to next 4 kB limit
  200. */
  201. addr -= gd->mon_len;
  202. addr &= ~(4096 - 1);
  203. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr);
  204. /*
  205. * reserve memory for malloc() arena
  206. */
  207. addr_sp = addr - TOTAL_MALLOC_LEN;
  208. debug("Reserving %dk for malloc() at: %08lx\n",
  209. TOTAL_MALLOC_LEN >> 10, addr_sp);
  210. /*
  211. * (permanently) allocate a Board Info struct
  212. * and a permanent copy of the "global" data
  213. */
  214. addr_sp -= GENERATED_BD_INFO_SIZE;
  215. bd = (bd_t *) addr_sp;
  216. gd->bd = bd;
  217. memset((void *)bd, 0, GENERATED_BD_INFO_SIZE);
  218. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  219. GENERATED_BD_INFO_SIZE, addr_sp);
  220. addr_sp -= GENERATED_GBL_DATA_SIZE;
  221. id = (gd_t *) addr_sp;
  222. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  223. GENERATED_GBL_DATA_SIZE, addr_sp);
  224. /* setup stackpointer for exeptions */
  225. gd->irq_sp = addr_sp;
  226. #ifdef CONFIG_USE_IRQ
  227. addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
  228. debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
  229. CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ, addr_sp);
  230. #endif
  231. /* leave 3 words for abort-stack */
  232. addr_sp -= 12;
  233. /* 8-byte alignment for ABI compliance */
  234. addr_sp &= ~0x07;
  235. debug("New Stack Pointer is: %08lx\n", addr_sp);
  236. gd->bd->bi_baudrate = gd->baudrate;
  237. /* Ram isn't board specific, so move it to board code ... */
  238. dram_init_banksize();
  239. display_dram_config(); /* and display it */
  240. gd->relocaddr = addr;
  241. gd->start_addr_sp = addr_sp;
  242. gd->reloc_off = addr - _TEXT_BASE;
  243. debug("relocation Offset is: %08lx\n", gd->reloc_off);
  244. memcpy(id, (void *)gd, GENERATED_GBL_DATA_SIZE);
  245. relocate_code(addr_sp, id, addr);
  246. /* NOTREACHED - relocate_code() does not return */
  247. }
  248. /*
  249. * This is the next part if the initialization sequence: we are now
  250. * running from RAM and have a "normal" C environment, i. e. global
  251. * data can be written, BSS has been cleared, the stack size in not
  252. * that critical any more, etc.
  253. */
  254. void board_init_r(gd_t *id, ulong dest_addr)
  255. {
  256. bd_t *bd;
  257. ulong malloc_start;
  258. gd = id;
  259. bd = gd->bd;
  260. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  261. monitor_flash_len = (ulong)&_end - (ulong)&_start;
  262. debug("monitor flash len: %08lX\n", monitor_flash_len);
  263. board_init(); /* Setup chipselects */
  264. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  265. /*
  266. * We have to relocate the command table manually
  267. */
  268. fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
  269. ll_entry_count(cmd_tbl_t, cmd));
  270. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  271. serial_initialize();
  272. debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  273. /* The Malloc area is immediately below the monitor copy in DRAM */
  274. malloc_start = dest_addr - TOTAL_MALLOC_LEN;
  275. mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
  276. #ifndef CONFIG_SYS_NO_FLASH
  277. /* configure available FLASH banks */
  278. gd->bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  279. gd->bd->bi_flashsize = flash_init();
  280. gd->bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + gd->bd->bi_flashsize;
  281. if (gd->bd->bi_flashsize)
  282. display_flash_config(gd->bd->bi_flashsize);
  283. #endif /* CONFIG_SYS_NO_FLASH */
  284. #if defined(CONFIG_CMD_NAND)
  285. puts("NAND: ");
  286. nand_init(); /* go init the NAND */
  287. #endif
  288. #if defined(CONFIG_CMD_IDE)
  289. puts("IDE: ");
  290. ide_init();
  291. #endif
  292. #ifdef CONFIG_GENERIC_MMC
  293. puts("MMC: ");
  294. mmc_initialize(gd->bd);
  295. #endif
  296. /* initialize environment */
  297. env_relocate();
  298. #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
  299. puts("PCI: ");
  300. nds32_pci_init();
  301. #endif
  302. stdio_init(); /* get the devices list going. */
  303. jumptable_init();
  304. #if defined(CONFIG_API)
  305. /* Initialize API */
  306. api_init();
  307. #endif
  308. console_init_r(); /* fully init console as a device */
  309. #if defined(CONFIG_ARCH_MISC_INIT)
  310. /* miscellaneous arch dependent initialisations */
  311. arch_misc_init();
  312. #endif
  313. #if defined(CONFIG_MISC_INIT_R)
  314. /* miscellaneous platform dependent initialisations */
  315. misc_init_r();
  316. #endif
  317. #if defined(CONFIG_USE_IRQ)
  318. /* set up exceptions */
  319. interrupt_init();
  320. /* enable exceptions */
  321. enable_interrupts();
  322. #endif
  323. /* Initialize from environment */
  324. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  325. #ifdef CONFIG_BOARD_LATE_INIT
  326. board_late_init();
  327. #endif
  328. #if defined(CONFIG_CMD_NET)
  329. puts("Net: ");
  330. eth_initialize(gd->bd);
  331. #if defined(CONFIG_RESET_PHY_R)
  332. debug("Reset Ethernet PHY\n");
  333. reset_phy();
  334. #endif
  335. #endif
  336. /* main_loop() can return to retry autoboot, if so just run it again. */
  337. for (;;)
  338. main_loop();
  339. /* NOTREACHED - no way out of command loop except booting */
  340. }