board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
  4. *
  5. * (C) Copyright 2002
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * (C) Copyright 2002
  9. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  10. * Marius Groeger <mgroeger@sysgo.de>
  11. *
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. */
  30. #include <common.h>
  31. #include <watchdog.h>
  32. #include <command.h>
  33. #include <devices.h>
  34. #include <version.h>
  35. #include <malloc.h>
  36. #include <syscall.h>
  37. #include <net.h>
  38. #include <ide.h>
  39. extern long _i386boot_start;
  40. extern long _i386boot_end;
  41. extern long _i386boot_romdata_start;
  42. extern long _i386boot_romdata_dest;
  43. extern long _i386boot_romdata_size;
  44. extern long _i386boot_bss_start;
  45. extern long _i386boot_bss_size;
  46. extern long _i386boot_realmode;
  47. extern long _i386boot_realmode_size;
  48. extern long _i386boot_bios;
  49. extern long _i386boot_bios_size;
  50. /* The symbols defined by the linker script becomes pointers
  51. * which is somewhat inconveient ... */
  52. ulong i386boot_start = (ulong)&_i386boot_start; /* code start (in flash) defined in start.S */
  53. ulong i386boot_end = (ulong)&_i386boot_end; /* code end (in flash) */
  54. ulong i386boot_romdata_start = (ulong)&_i386boot_romdata_start; /* datasegment in flash (also code+rodata end) */
  55. ulong i386boot_romdata_dest = (ulong)&_i386boot_romdata_dest; /* data location segment in ram */
  56. ulong i386boot_romdata_size = (ulong)&_i386boot_romdata_size; /* size of data segment */
  57. ulong i386boot_bss_start = (ulong)&_i386boot_bss_start; /* bss start */
  58. ulong i386boot_bss_size = (ulong)&_i386boot_bss_size; /* bss size */
  59. ulong i386boot_realmode = (ulong)&_i386boot_realmode; /* start of realmode entry code */
  60. ulong i386boot_realmode_size = (ulong)&_i386boot_realmode_size; /* size of realmode entry code */
  61. ulong i386boot_bios = (ulong)&_i386boot_bios; /* start of BIOS emulation code */
  62. ulong i386boot_bios_size = (ulong)&_i386boot_bios_size; /* size of BIOS emulation code */
  63. const char version_string[] =
  64. U_BOOT_VERSION" (" __DATE__ " - " __TIME__ ")";
  65. /*
  66. * Begin and End of memory area for malloc(), and current "brk"
  67. */
  68. static ulong mem_malloc_start = 0;
  69. static ulong mem_malloc_end = 0;
  70. static ulong mem_malloc_brk = 0;
  71. static int mem_malloc_init(void)
  72. {
  73. DECLARE_GLOBAL_DATA_PTR;
  74. #if 1
  75. /* start malloc area right after the stack */
  76. mem_malloc_start = i386boot_bss_start +
  77. i386boot_bss_size + CFG_STACK_SIZE;
  78. mem_malloc_start = (mem_malloc_start+3)&~3;
  79. #else
  80. mem_malloc_start = 0x400000;
  81. #endif
  82. #if 1
  83. /* Use all available RAM for malloc() */
  84. mem_malloc_end = gd->ram_size;
  85. #else
  86. /* Use only CONFIG_MALLOC_SIZE bytes of RAM for malloc() */
  87. mem_malloc_end = mem_malloc_start + CONFIG_MALLOC_SIZE;
  88. #endif
  89. mem_malloc_brk = mem_malloc_start;
  90. return 0;
  91. }
  92. void *sbrk (ptrdiff_t increment)
  93. {
  94. ulong old = mem_malloc_brk;
  95. ulong new = old + increment;
  96. if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
  97. return (NULL);
  98. }
  99. mem_malloc_brk = new;
  100. return ((void *) old);
  101. }
  102. char *strmhz (char *buf, long hz)
  103. {
  104. long l, n;
  105. long m;
  106. n = hz / 1000000L;
  107. l = sprintf (buf, "%ld", n);
  108. m = (hz % 1000000L) / 1000L;
  109. if (m != 0)
  110. sprintf (buf + l, ".%03ld", m);
  111. return (buf);
  112. }
  113. /************************************************************************
  114. * Init Utilities *
  115. ************************************************************************
  116. * Some of this code should be moved into the core functions,
  117. * or dropped completely,
  118. * but let's get it working (again) first...
  119. */
  120. static void syscalls_init (void)
  121. {
  122. syscall_tbl[SYSCALL_MALLOC] = (void *) malloc;
  123. syscall_tbl[SYSCALL_FREE] = (void *) free;
  124. syscall_tbl[SYSCALL_INSTALL_HDLR] = (void *) irq_install_handler;
  125. syscall_tbl[SYSCALL_FREE_HDLR] = (void *) irq_free_handler;
  126. }
  127. static int init_baudrate (void)
  128. {
  129. DECLARE_GLOBAL_DATA_PTR;
  130. uchar tmp[64]; /* long enough for environment variables */
  131. int i = getenv_r ("baudrate", tmp, sizeof (tmp));
  132. gd->baudrate = (i > 0)
  133. ? (int) simple_strtoul (tmp, NULL, 10)
  134. : CONFIG_BAUDRATE;
  135. return (0);
  136. }
  137. static int display_banner (void)
  138. {
  139. printf ("\n\n%s\n\n", version_string);
  140. printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
  141. " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
  142. i386boot_start, i386boot_romdata_start-1,
  143. i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
  144. i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
  145. i386boot_bss_start+i386boot_bss_size,
  146. i386boot_bss_start+i386boot_bss_size+CFG_STACK_SIZE-1);
  147. return (0);
  148. }
  149. /*
  150. * WARNING: this code looks "cleaner" than the PowerPC version, but
  151. * has the disadvantage that you either get nothing, or everything.
  152. * On PowerPC, you might see "DRAM: " before the system hangs - which
  153. * gives a simple yet clear indication which part of the
  154. * initialization if failing.
  155. */
  156. static int display_dram_config (void)
  157. {
  158. DECLARE_GLOBAL_DATA_PTR;
  159. int i;
  160. puts ("DRAM Configuration:\n");
  161. for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
  162. printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  163. print_size (gd->bd->bi_dram[i].size, "\n");
  164. }
  165. return (0);
  166. }
  167. static void display_flash_config (ulong size)
  168. {
  169. puts ("Flash: ");
  170. print_size (size, "\n");
  171. }
  172. /*
  173. * Breath some life into the board...
  174. *
  175. * Initialize an SMC for serial comms, and carry out some hardware
  176. * tests.
  177. *
  178. * The first part of initialization is running from Flash memory;
  179. * its main purpose is to initialize the RAM so that we
  180. * can relocate the monitor code to RAM.
  181. */
  182. /*
  183. * All attempts to come up with a "common" initialization sequence
  184. * that works for all boards and architectures failed: some of the
  185. * requirements are just _too_ different. To get rid of the resulting
  186. * mess of board dependend #ifdef'ed code we now make the whole
  187. * initialization sequence configurable to the user.
  188. *
  189. * The requirements for any new initalization function is simple: it
  190. * receives a pointer to the "global data" structure as it's only
  191. * argument, and returns an integer return code, where 0 means
  192. * "continue" and != 0 means "fatal error, hang the system".
  193. */
  194. typedef int (init_fnc_t) (void);
  195. init_fnc_t *init_sequence[] = {
  196. cpu_init, /* basic cpu dependent setup */
  197. board_init, /* basic board dependent setup */
  198. dram_init, /* configure available RAM banks */
  199. mem_malloc_init, /* dependant on dram_init */
  200. interrupt_init, /* set up exceptions */
  201. timer_init,
  202. env_init, /* initialize environment */
  203. init_baudrate, /* initialze baudrate settings */
  204. serial_init, /* serial communications setup */
  205. display_banner,
  206. display_dram_config,
  207. NULL,
  208. };
  209. gd_t *global_data;
  210. void start_i386boot (void)
  211. {
  212. DECLARE_GLOBAL_DATA_PTR;
  213. char *s;
  214. int i;
  215. ulong size;
  216. static gd_t gd_data;
  217. static bd_t bd_data;
  218. init_fnc_t **init_fnc_ptr;
  219. show_boot_progress(0x21);
  220. gd = global_data = &gd_data;
  221. memset (gd, 0, sizeof (gd_t));
  222. gd->bd = &bd_data;
  223. memset (gd->bd, 0, sizeof (bd_t));
  224. show_boot_progress(0x22);
  225. for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
  226. show_boot_progress(0xa130|i);
  227. if ((*init_fnc_ptr)() != 0) {
  228. hang ();
  229. }
  230. }
  231. show_boot_progress(0x23);
  232. /* configure available FLASH banks */
  233. size = flash_init();
  234. display_flash_config(size);
  235. show_boot_progress(0x24);
  236. show_boot_progress(0x25);
  237. /* initialize environment */
  238. env_relocate ();
  239. show_boot_progress(0x26);
  240. /* IP Address */
  241. bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
  242. /* MAC Address */
  243. {
  244. int i;
  245. ulong reg;
  246. char *s, *e;
  247. uchar tmp[64];
  248. i = getenv_r ("ethaddr", tmp, sizeof (tmp));
  249. s = (i > 0) ? tmp : NULL;
  250. for (reg = 0; reg < 6; ++reg) {
  251. bd_data.bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  252. if (s)
  253. s = (*e) ? e + 1 : e;
  254. }
  255. }
  256. #if defined(CONFIG_PCI)
  257. /*
  258. * Do pci configuration
  259. */
  260. pci_init();
  261. #endif
  262. show_boot_progress(0x27);
  263. devices_init ();
  264. /* allocate syscalls table (console_init_r will fill it in */
  265. syscall_tbl = (void **) malloc (NR_SYSCALLS * sizeof (void *));
  266. /* Initialize the console (after the relocation and devices init) */
  267. console_init_r();
  268. syscalls_init();
  269. #ifdef CONFIG_MISC_INIT_R
  270. /* miscellaneous platform dependent initialisations */
  271. misc_init_r();
  272. #endif
  273. #if (CONFIG_COMMANDS & CFG_CMD_NET) && (0)
  274. WATCHDOG_RESET();
  275. # ifdef DEBUG
  276. puts ("Reset Ethernet PHY\n");
  277. # endif
  278. reset_phy();
  279. #endif
  280. #if (CONFIG_COMMANDS & CFG_CMD_PCMCIA) && !(CONFIG_COMMANDS & CFG_CMD_IDE)
  281. WATCHDOG_RESET();
  282. puts ("PCMCIA:");
  283. pcmcia_init();
  284. #endif
  285. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  286. WATCHDOG_RESET();
  287. puts("KGDB: ");
  288. kgdb_init();
  289. #endif
  290. /* enable exceptions */
  291. enable_interrupts ();
  292. show_boot_progress(0x28);
  293. /* Must happen after interrupts are initialized since
  294. * an irq handler gets installed
  295. */
  296. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  297. serial_buffered_init();
  298. #endif
  299. #ifdef CONFIG_STATUS_LED
  300. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  301. #endif
  302. udelay (20);
  303. set_timer (0);
  304. /* Initialize from environment */
  305. if ((s = getenv ("loadaddr")) != NULL) {
  306. load_addr = simple_strtoul (s, NULL, 16);
  307. }
  308. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  309. if ((s = getenv ("bootfile")) != NULL) {
  310. copy_filename (BootFile, s, sizeof (BootFile));
  311. }
  312. #endif /* CFG_CMD_NET */
  313. WATCHDOG_RESET();
  314. #if (CONFIG_COMMANDS & CFG_CMD_IDE)
  315. WATCHDOG_RESET();
  316. puts("IDE: ");
  317. ide_init();
  318. #endif /* CFG_CMD_IDE */
  319. #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
  320. WATCHDOG_RESET();
  321. puts("SCSI: ");
  322. scsi_init();
  323. #endif
  324. #if (CONFIG_COMMANDS & CFG_CMD_DOC)
  325. WATCHDOG_RESET();
  326. puts ("DOC: ");
  327. doc_init();
  328. #endif
  329. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
  330. WATCHDOG_RESET();
  331. puts("Net: ");
  332. eth_initialize(gd->bd);
  333. #endif
  334. #ifdef CONFIG_LAST_STAGE_INIT
  335. WATCHDOG_RESET();
  336. /*
  337. * Some parts can be only initialized if all others (like
  338. * Interrupts) are up and running (i.e. the PC-style ISA
  339. * keyboard).
  340. */
  341. last_stage_init();
  342. #endif
  343. #ifdef CONFIG_POST
  344. post_run (NULL, POST_RAM | post_bootmode_get(0));
  345. if (post_bootmode_get(0) & POST_POWERFAIL) {
  346. post_bootmode_clear();
  347. board_poweroff();
  348. }
  349. #endif
  350. show_boot_progress(0x29);
  351. /* main_loop() can return to retry autoboot, if so just run it again. */
  352. for (;;) {
  353. main_loop ();
  354. }
  355. /* NOTREACHED - no way out of command loop except booting */
  356. }
  357. void hang (void)
  358. {
  359. puts ("### ERROR ### Please RESET the board ###\n");
  360. for (;;);
  361. }