board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  185. /* reserve TLB table */
  186. addr -= (4096 * 4);
  187. /* round down to next 64 kB limit */
  188. addr &= ~(0x10000 - 1);
  189. gd->tlb_addr = addr;
  190. debug("TLB table at: %08lx\n", addr);
  191. #endif
  192. /* round down to next 4 kB limit */
  193. addr &= ~(4096 - 1);
  194. debug("Top of RAM usable for U-Boot at: %08lx\n", addr);
  195. #ifdef CONFIG_LCD
  196. #ifdef CONFIG_FB_ADDR
  197. gd->fb_base = CONFIG_FB_ADDR;
  198. #else
  199. /* reserve memory for LCD display (always full pages) */
  200. addr = lcd_setmem(addr);
  201. gd->fb_base = addr;
  202. #endif /* CONFIG_FB_ADDR */
  203. #endif /* CONFIG_LCD */
  204. /*
  205. * reserve memory for U-Boot code, data & bss
  206. * round down to next 4 kB limit
  207. */
  208. addr -= gd->mon_len;
  209. addr &= ~(4096 - 1);
  210. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr);
  211. /*
  212. * reserve memory for malloc() arena
  213. */
  214. addr_sp = addr - TOTAL_MALLOC_LEN;
  215. debug("Reserving %dk for malloc() at: %08lx\n",
  216. TOTAL_MALLOC_LEN >> 10, addr_sp);
  217. /*
  218. * (permanently) allocate a Board Info struct
  219. * and a permanent copy of the "global" data
  220. */
  221. addr_sp -= GENERATED_BD_INFO_SIZE;
  222. bd = (bd_t *) addr_sp;
  223. gd->bd = bd;
  224. memset((void *)bd, 0, GENERATED_BD_INFO_SIZE);
  225. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  226. GENERATED_BD_INFO_SIZE, addr_sp);
  227. addr_sp -= GENERATED_GBL_DATA_SIZE;
  228. id = (gd_t *) addr_sp;
  229. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  230. GENERATED_GBL_DATA_SIZE, addr_sp);
  231. /* setup stackpointer for exeptions */
  232. gd->irq_sp = addr_sp;
  233. #ifdef CONFIG_USE_IRQ
  234. addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
  235. debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
  236. CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ, addr_sp);
  237. #endif
  238. /* leave 3 words for abort-stack */
  239. addr_sp -= 12;
  240. /* 8-byte alignment for ABI compliance */
  241. addr_sp &= ~0x07;
  242. debug("New Stack Pointer is: %08lx\n", addr_sp);
  243. gd->bd->bi_baudrate = gd->baudrate;
  244. /* Ram isn't board specific, so move it to board code ... */
  245. dram_init_banksize();
  246. display_dram_config(); /* and display it */
  247. gd->relocaddr = addr;
  248. gd->start_addr_sp = addr_sp;
  249. gd->reloc_off = addr - _TEXT_BASE;
  250. debug("relocation Offset is: %08lx\n", gd->reloc_off);
  251. memcpy(id, (void *)gd, GENERATED_GBL_DATA_SIZE);
  252. relocate_code(addr_sp, id, addr);
  253. /* NOTREACHED - relocate_code() does not return */
  254. }
  255. /*
  256. * This is the next part if the initialization sequence: we are now
  257. * running from RAM and have a "normal" C environment, i. e. global
  258. * data can be written, BSS has been cleared, the stack size in not
  259. * that critical any more, etc.
  260. */
  261. void board_init_r(gd_t *id, ulong dest_addr)
  262. {
  263. bd_t *bd;
  264. ulong malloc_start;
  265. gd = id;
  266. bd = gd->bd;
  267. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  268. monitor_flash_len = &_end - &_start;
  269. debug("monitor flash len: %08lX\n", monitor_flash_len);
  270. board_init(); /* Setup chipselects */
  271. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  272. /*
  273. * We have to relocate the command table manually
  274. */
  275. fixup_cmdtable(&__u_boot_cmd_start,
  276. (ulong)(&__u_boot_cmd_end - &__u_boot_cmd_start));
  277. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  278. #ifdef CONFIG_SERIAL_MULTI
  279. serial_initialize();
  280. #endif
  281. debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  282. /* The Malloc area is immediately below the monitor copy in DRAM */
  283. malloc_start = dest_addr - TOTAL_MALLOC_LEN;
  284. mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN);
  285. malloc_bin_reloc();
  286. #ifndef CONFIG_SYS_NO_FLASH
  287. /* configure available FLASH banks */
  288. gd->bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  289. gd->bd->bi_flashsize = flash_init();
  290. gd->bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + gd->bd->bi_flashsize;
  291. if (gd->bd->bi_flashsize)
  292. display_flash_config(gd->bd->bi_flashsize);
  293. #endif /* CONFIG_SYS_NO_FLASH */
  294. #if defined(CONFIG_CMD_NAND)
  295. puts("NAND: ");
  296. nand_init(); /* go init the NAND */
  297. #endif
  298. #if defined(CONFIG_CMD_IDE)
  299. puts("IDE: ");
  300. ide_init();
  301. #endif
  302. #ifdef CONFIG_GENERIC_MMC
  303. puts("MMC: ");
  304. mmc_initialize(gd->bd);
  305. #endif
  306. /* initialize environment */
  307. env_relocate();
  308. #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI)
  309. puts("PCI: ");
  310. nds32_pci_init();
  311. #endif
  312. stdio_init(); /* get the devices list going. */
  313. jumptable_init();
  314. #if defined(CONFIG_API)
  315. /* Initialize API */
  316. api_init();
  317. #endif
  318. console_init_r(); /* fully init console as a device */
  319. #if defined(CONFIG_ARCH_MISC_INIT)
  320. /* miscellaneous arch dependent initialisations */
  321. arch_misc_init();
  322. #endif
  323. #if defined(CONFIG_MISC_INIT_R)
  324. /* miscellaneous platform dependent initialisations */
  325. misc_init_r();
  326. #endif
  327. #if defined(CONFIG_USE_IRQ)
  328. /* set up exceptions */
  329. interrupt_init();
  330. /* enable exceptions */
  331. enable_interrupts();
  332. #endif
  333. /* Initialize from environment */
  334. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  335. #ifdef BOARD_LATE_INIT
  336. board_late_init();
  337. #endif
  338. #if defined(CONFIG_CMD_NET)
  339. puts("Net: ");
  340. eth_initialize(gd->bd);
  341. #if defined(CONFIG_RESET_PHY_R)
  342. debug("Reset Ethernet PHY\n");
  343. reset_phy();
  344. #endif
  345. #endif
  346. /* main_loop() can return to retry autoboot, if so just run it again. */
  347. for (;;)
  348. main_loop();
  349. /* NOTREACHED - no way out of command loop except booting */
  350. }
  351. void hang(void)
  352. {
  353. puts("### ERROR ### Please RESET the board ###\n");
  354. for (;;)
  355. ;
  356. }