board.c 10 KB

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