board.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <malloc.h>
  26. #include <stdio_dev.h>
  27. #include <version.h>
  28. #include <net.h>
  29. #include <environment.h>
  30. #include <nand.h>
  31. #include <onenand_uboot.h>
  32. #include <spi.h>
  33. #ifdef CONFIG_BITBANGMII
  34. #include <miiphy.h>
  35. #endif
  36. DECLARE_GLOBAL_DATA_PTR;
  37. #undef DEBUG
  38. extern int timer_init(void);
  39. extern int incaip_set_cpuclk(void);
  40. extern ulong uboot_end_data;
  41. extern ulong uboot_end;
  42. ulong monitor_flash_len;
  43. static char *failed = "*** failed ***\n";
  44. /*
  45. * mips_io_port_base is the begin of the address space to which x86 style
  46. * I/O ports are mapped.
  47. */
  48. unsigned long mips_io_port_base = -1;
  49. int __board_early_init_f(void)
  50. {
  51. /*
  52. * Nothing to do in this dummy implementation
  53. */
  54. return 0;
  55. }
  56. int board_early_init_f(void) __attribute__((weak, alias("__board_early_init_f")));
  57. static int init_func_ram (void)
  58. {
  59. #ifdef CONFIG_BOARD_TYPES
  60. int board_type = gd->board_type;
  61. #else
  62. int board_type = 0; /* use dummy arg */
  63. #endif
  64. puts ("DRAM: ");
  65. if ((gd->ram_size = initdram (board_type)) > 0) {
  66. print_size (gd->ram_size, "\n");
  67. return (0);
  68. }
  69. puts (failed);
  70. return (1);
  71. }
  72. static int display_banner(void)
  73. {
  74. printf ("\n\n%s\n\n", version_string);
  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
  84. static int init_baudrate (void)
  85. {
  86. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  87. return 0;
  88. }
  89. /*
  90. * Breath some life into the board...
  91. *
  92. * The first part of initialization is running from Flash memory;
  93. * its main purpose is to initialize the RAM so that we
  94. * can relocate the monitor code to RAM.
  95. */
  96. /*
  97. * All attempts to come up with a "common" initialization sequence
  98. * that works for all boards and architectures failed: some of the
  99. * requirements are just _too_ different. To get rid of the resulting
  100. * mess of board dependend #ifdef'ed code we now make the whole
  101. * initialization sequence configurable to the user.
  102. *
  103. * The requirements for any new initalization function is simple: it
  104. * receives a pointer to the "global data" structure as it's only
  105. * argument, and returns an integer return code, where 0 means
  106. * "continue" and != 0 means "fatal error, hang the system".
  107. */
  108. typedef int (init_fnc_t) (void);
  109. init_fnc_t *init_sequence[] = {
  110. board_early_init_f,
  111. timer_init,
  112. env_init, /* initialize environment */
  113. #ifdef CONFIG_INCA_IP
  114. incaip_set_cpuclk, /* set cpu clock according to environment variable */
  115. #endif
  116. init_baudrate, /* initialze baudrate settings */
  117. serial_init, /* serial communications setup */
  118. console_init_f,
  119. display_banner, /* say that we are here */
  120. checkboard,
  121. init_func_ram,
  122. NULL,
  123. };
  124. void board_init_f(ulong bootflag)
  125. {
  126. gd_t gd_data, *id;
  127. bd_t *bd;
  128. init_fnc_t **init_fnc_ptr;
  129. ulong addr, addr_sp, len = (ulong)&uboot_end - CONFIG_SYS_MONITOR_BASE;
  130. ulong *s;
  131. /* Pointer is writable since we allocated a register for it.
  132. */
  133. gd = &gd_data;
  134. /* compiler optimization barrier needed for GCC >= 3.4 */
  135. __asm__ __volatile__("": : :"memory");
  136. memset ((void *)gd, 0, sizeof (gd_t));
  137. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  138. if ((*init_fnc_ptr)() != 0) {
  139. hang ();
  140. }
  141. }
  142. /*
  143. * Now that we have DRAM mapped and working, we can
  144. * relocate the code and continue running from DRAM.
  145. */
  146. addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
  147. /* We can reserve some RAM "on top" here.
  148. */
  149. /* round down to next 4 kB limit.
  150. */
  151. addr &= ~(4096 - 1);
  152. debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
  153. /* Reserve memory for U-Boot code, data & bss
  154. * round down to next 16 kB limit
  155. */
  156. addr -= len;
  157. addr &= ~(16 * 1024 - 1);
  158. debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
  159. /* Reserve memory for malloc() arena.
  160. */
  161. addr_sp = addr - TOTAL_MALLOC_LEN;
  162. debug ("Reserving %dk for malloc() at: %08lx\n",
  163. TOTAL_MALLOC_LEN >> 10, addr_sp);
  164. /*
  165. * (permanently) allocate a Board Info struct
  166. * and a permanent copy of the "global" data
  167. */
  168. addr_sp -= sizeof(bd_t);
  169. bd = (bd_t *)addr_sp;
  170. gd->bd = bd;
  171. debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
  172. sizeof(bd_t), addr_sp);
  173. addr_sp -= sizeof(gd_t);
  174. id = (gd_t *)addr_sp;
  175. debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
  176. sizeof (gd_t), addr_sp);
  177. /* Reserve memory for boot params.
  178. */
  179. addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
  180. bd->bi_boot_params = addr_sp;
  181. debug ("Reserving %dk for boot params() at: %08lx\n",
  182. CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
  183. /*
  184. * Finally, we set up a new (bigger) stack.
  185. *
  186. * Leave some safety gap for SP, force alignment on 16 byte boundary
  187. * Clear initial stack frame
  188. */
  189. addr_sp -= 16;
  190. addr_sp &= ~0xF;
  191. s = (ulong *)addr_sp;
  192. *s-- = 0;
  193. *s-- = 0;
  194. addr_sp = (ulong)s;
  195. debug ("Stack Pointer at: %08lx\n", addr_sp);
  196. /*
  197. * Save local variables to board info struct
  198. */
  199. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM memory */
  200. bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
  201. bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
  202. memcpy (id, (void *)gd, sizeof (gd_t));
  203. relocate_code (addr_sp, id, addr);
  204. /* NOTREACHED - relocate_code() does not return */
  205. }
  206. /************************************************************************
  207. *
  208. * This is the next part if the initialization sequence: we are now
  209. * running from RAM and have a "normal" C environment, i. e. global
  210. * data can be written, BSS has been cleared, the stack size in not
  211. * that critical any more, etc.
  212. *
  213. ************************************************************************
  214. */
  215. void board_init_r (gd_t *id, ulong dest_addr)
  216. {
  217. #ifndef CONFIG_SYS_NO_FLASH
  218. ulong size;
  219. #endif
  220. extern void malloc_bin_reloc (void);
  221. #ifndef CONFIG_ENV_IS_NOWHERE
  222. extern char * env_name_spec;
  223. #endif
  224. char *s;
  225. bd_t *bd;
  226. gd = id;
  227. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  228. debug ("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  229. gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
  230. monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
  231. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  232. /*
  233. * We have to relocate the command table manually
  234. */
  235. fixup_cmdtable(&__u_boot_cmd_start,
  236. (ulong)(&__u_boot_cmd_end - &__u_boot_cmd_start));
  237. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  238. /* there are some other pointer constants we must deal with */
  239. #ifndef CONFIG_ENV_IS_NOWHERE
  240. env_name_spec += gd->reloc_off;
  241. #endif
  242. bd = gd->bd;
  243. /* The Malloc area is immediately below the monitor copy in DRAM */
  244. mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
  245. TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
  246. malloc_bin_reloc();
  247. #ifndef CONFIG_SYS_NO_FLASH
  248. /* configure available FLASH banks */
  249. size = flash_init();
  250. display_flash_config (size);
  251. bd->bi_flashsize = size;
  252. #endif
  253. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  254. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  255. bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
  256. #else
  257. bd->bi_flashoffset = 0;
  258. #endif
  259. #ifdef CONFIG_CMD_NAND
  260. puts ("NAND: ");
  261. nand_init (); /* go init the NAND */
  262. #endif
  263. #if defined(CONFIG_CMD_ONENAND)
  264. onenand_init();
  265. #endif
  266. /* relocate environment function pointers etc. */
  267. env_relocate();
  268. /* IP Address */
  269. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  270. #if defined(CONFIG_PCI)
  271. /*
  272. * Do pci configuration
  273. */
  274. pci_init();
  275. #endif
  276. /** leave this here (after malloc(), environment and PCI are working) **/
  277. /* Initialize stdio devices */
  278. stdio_init ();
  279. jumptable_init ();
  280. /* Initialize the console (after the relocation and devices init) */
  281. console_init_r ();
  282. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  283. /* Initialize from environment */
  284. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  285. #if defined(CONFIG_CMD_NET)
  286. if ((s = getenv ("bootfile")) != NULL) {
  287. copy_filename (BootFile, s, sizeof (BootFile));
  288. }
  289. #endif
  290. #ifdef CONFIG_CMD_SPI
  291. puts ("SPI: ");
  292. spi_init (); /* go init the SPI */
  293. puts ("ready\n");
  294. #endif
  295. #if defined(CONFIG_MISC_INIT_R)
  296. /* miscellaneous platform dependent initialisations */
  297. misc_init_r ();
  298. #endif
  299. #ifdef CONFIG_BITBANGMII
  300. bb_miiphy_init();
  301. #endif
  302. #if defined(CONFIG_CMD_NET)
  303. puts ("Net: ");
  304. eth_initialize(gd->bd);
  305. #endif
  306. /* main_loop() can return to retry autoboot, if so just run it again. */
  307. for (;;) {
  308. main_loop ();
  309. }
  310. /* NOTREACHED - no way out of command loop except booting */
  311. }
  312. void hang (void)
  313. {
  314. puts ("### ERROR ### Please RESET the board ###\n");
  315. for (;;);
  316. }