board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 <stdio_dev.h>
  34. #include <timestamp.h>
  35. #include <version.h>
  36. #include <malloc.h>
  37. #include <net.h>
  38. #include <ide.h>
  39. #include <asm/u-boot-i386.h>
  40. #include <elf.h>
  41. #ifdef CONFIG_BITBANGMII
  42. #include <miiphy.h>
  43. #endif
  44. DECLARE_GLOBAL_DATA_PTR;
  45. /* Exports from the Linker Script */
  46. extern ulong _i386boot_text_start;
  47. extern ulong _i386boot_rel_dyn_start;
  48. extern ulong _i386boot_rel_dyn_end;
  49. extern ulong _i386boot_bss_start;
  50. extern ulong _i386boot_bss_size;
  51. void ram_bootstrap (void *);
  52. const char version_string[] =
  53. U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
  54. /************************************************************************
  55. * Init Utilities *
  56. ************************************************************************
  57. * Some of this code should be moved into the core functions,
  58. * or dropped completely,
  59. * but let's get it working (again) first...
  60. */
  61. static int init_baudrate (void)
  62. {
  63. char tmp[64]; /* long enough for environment variables */
  64. int i = getenv_r("baudrate", tmp, 64);
  65. gd->baudrate = (i != 0)
  66. ? (int) simple_strtoul (tmp, NULL, 10)
  67. : CONFIG_BAUDRATE;
  68. return (0);
  69. }
  70. static int display_banner (void)
  71. {
  72. printf ("\n\n%s\n\n", version_string);
  73. /*
  74. printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
  75. " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
  76. i386boot_start, i386boot_romdata_start-1,
  77. i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
  78. i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
  79. i386boot_bss_start+i386boot_bss_size,
  80. i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
  81. */
  82. return (0);
  83. }
  84. /*
  85. * WARNING: this code looks "cleaner" than the PowerPC version, but
  86. * has the disadvantage that you either get nothing, or everything.
  87. * On PowerPC, you might see "DRAM: " before the system hangs - which
  88. * gives a simple yet clear indication which part of the
  89. * initialization if failing.
  90. */
  91. static int display_dram_config (void)
  92. {
  93. int i;
  94. puts ("DRAM Configuration:\n");
  95. for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
  96. printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  97. print_size (gd->bd->bi_dram[i].size, "\n");
  98. }
  99. return (0);
  100. }
  101. static void display_flash_config (ulong size)
  102. {
  103. puts ("Flash: ");
  104. print_size (size, "\n");
  105. }
  106. /*
  107. * Breath some life into the board...
  108. *
  109. * Initialize an SMC for serial comms, 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 dependend #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. init_fnc_t *init_sequence[] = {
  130. serial_init,
  131. cpu_init_r, /* basic cpu dependent setup */
  132. board_early_init_r, /* basic board dependent setup */
  133. dram_init, /* configure available RAM banks */
  134. interrupt_init, /* set up exceptions */
  135. timer_init,
  136. env_init, /* initialize environment */
  137. init_baudrate, /* initialze baudrate settings */
  138. serial_init, /* serial communications setup */
  139. display_banner,
  140. display_dram_config,
  141. NULL,
  142. };
  143. gd_t *gd;
  144. /*
  145. * Load U-Boot into RAM, initialize BSS, perform relocation adjustments
  146. */
  147. void board_init_f (ulong stack_limit)
  148. {
  149. void *text_start = &_i386boot_text_start;
  150. void *u_boot_cmd_end = &__u_boot_cmd_end;
  151. Elf32_Rel *rel_dyn_start = (Elf32_Rel *)&_i386boot_rel_dyn_start;
  152. Elf32_Rel *rel_dyn_end = (Elf32_Rel *)&_i386boot_rel_dyn_end;
  153. void *bss_start = &_i386boot_bss_start;
  154. void *bss_size = &_i386boot_bss_size;
  155. size_t uboot_size;
  156. void *ram_start;
  157. ulong rel_offset;
  158. Elf32_Rel *re;
  159. void (*start_func)(void *);
  160. /* compiler optimization barrier needed for GCC >= 3.4 */
  161. __asm__ __volatile__("": : :"memory");
  162. uboot_size = (size_t)u_boot_cmd_end - (size_t)text_start;
  163. ram_start = (void *)stack_limit - (uboot_size + (ulong)bss_size);
  164. rel_offset = text_start - ram_start;
  165. start_func = ram_bootstrap - rel_offset;
  166. /* First stage CPU initialization */
  167. if (cpu_init_f() != 0)
  168. hang();
  169. /* First stage Board initialization */
  170. if (board_early_init_f() != 0)
  171. hang();
  172. /* Copy U-Boot into RAM */
  173. memcpy(ram_start, text_start, (size_t)uboot_size);
  174. /* Clear BSS */
  175. memset(bss_start - rel_offset, 0, (size_t)bss_size);
  176. /* Perform relocation adjustments */
  177. for (re = rel_dyn_start; re < rel_dyn_end; re++)
  178. {
  179. if (re->r_offset >= TEXT_BASE)
  180. if (*(ulong *)re->r_offset >= TEXT_BASE)
  181. *(ulong *)(re->r_offset - rel_offset) -= (Elf32_Addr)rel_offset;
  182. }
  183. start_func(ram_start);
  184. /* NOTREACHED - relocate_code() does not return */
  185. while(1);
  186. }
  187. /*
  188. * All attempts to jump straight from board_init_f() to board_init_r()
  189. * have failed, hence this special 'bootstrap' function.
  190. */
  191. void ram_bootstrap (void *ram_start)
  192. {
  193. static gd_t gd_data;
  194. /* compiler optimization barrier needed for GCC >= 3.4 */
  195. __asm__ __volatile__("": : :"memory");
  196. board_init_r(&gd_data, (ulong)ram_start);
  197. }
  198. void board_init_r(gd_t *id, ulong ram_start)
  199. {
  200. char *s;
  201. int i;
  202. ulong size;
  203. static bd_t bd_data;
  204. init_fnc_t **init_fnc_ptr;
  205. show_boot_progress(0x21);
  206. gd = id;
  207. /* compiler optimization barrier needed for GCC >= 3.4 */
  208. __asm__ __volatile__("": : :"memory");
  209. memset (gd, 0, sizeof (gd_t));
  210. gd->bd = &bd_data;
  211. memset (gd->bd, 0, sizeof (bd_t));
  212. show_boot_progress(0x22);
  213. gd->baudrate = CONFIG_BAUDRATE;
  214. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  215. mem_malloc_init((((ulong)ram_start - CONFIG_SYS_MALLOC_LEN)+3)&~3,
  216. CONFIG_SYS_MALLOC_LEN);
  217. for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
  218. show_boot_progress(0xa130|i);
  219. if ((*init_fnc_ptr)() != 0) {
  220. hang ();
  221. }
  222. }
  223. show_boot_progress(0x23);
  224. /* configure available FLASH banks */
  225. size = flash_init();
  226. display_flash_config(size);
  227. show_boot_progress(0x24);
  228. show_boot_progress(0x25);
  229. /* initialize environment */
  230. env_relocate ();
  231. show_boot_progress(0x26);
  232. /* IP Address */
  233. bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
  234. #if defined(CONFIG_PCI)
  235. /*
  236. * Do pci configuration
  237. */
  238. pci_init();
  239. #endif
  240. show_boot_progress(0x27);
  241. stdio_init ();
  242. jumptable_init ();
  243. /* Initialize the console (after the relocation and devices init) */
  244. console_init_r();
  245. #ifdef CONFIG_MISC_INIT_R
  246. /* miscellaneous platform dependent initialisations */
  247. misc_init_r();
  248. #endif
  249. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
  250. WATCHDOG_RESET();
  251. puts ("PCMCIA:");
  252. pcmcia_init();
  253. #endif
  254. #if defined(CONFIG_CMD_KGDB)
  255. WATCHDOG_RESET();
  256. puts("KGDB: ");
  257. kgdb_init();
  258. #endif
  259. /* enable exceptions */
  260. enable_interrupts();
  261. show_boot_progress(0x28);
  262. /* Must happen after interrupts are initialized since
  263. * an irq handler gets installed
  264. */
  265. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  266. serial_buffered_init();
  267. #endif
  268. #ifdef CONFIG_STATUS_LED
  269. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  270. #endif
  271. udelay(20);
  272. set_timer (0);
  273. /* Initialize from environment */
  274. if ((s = getenv ("loadaddr")) != NULL) {
  275. load_addr = simple_strtoul (s, NULL, 16);
  276. }
  277. #if defined(CONFIG_CMD_NET)
  278. if ((s = getenv ("bootfile")) != NULL) {
  279. copy_filename (BootFile, s, sizeof (BootFile));
  280. }
  281. #endif
  282. WATCHDOG_RESET();
  283. #if defined(CONFIG_CMD_IDE)
  284. WATCHDOG_RESET();
  285. puts("IDE: ");
  286. ide_init();
  287. #endif
  288. #if defined(CONFIG_CMD_SCSI)
  289. WATCHDOG_RESET();
  290. puts("SCSI: ");
  291. scsi_init();
  292. #endif
  293. #if defined(CONFIG_CMD_DOC)
  294. WATCHDOG_RESET();
  295. puts("DOC: ");
  296. doc_init();
  297. #endif
  298. #ifdef CONFIG_BITBANGMII
  299. bb_miiphy_init();
  300. #endif
  301. #if defined(CONFIG_CMD_NET)
  302. #if defined(CONFIG_NET_MULTI)
  303. WATCHDOG_RESET();
  304. puts("Net: ");
  305. #endif
  306. eth_initialize(gd->bd);
  307. #endif
  308. #if ( defined(CONFIG_CMD_NET)) && (0)
  309. WATCHDOG_RESET();
  310. # ifdef DEBUG
  311. puts ("Reset Ethernet PHY\n");
  312. # endif
  313. reset_phy();
  314. #endif
  315. #ifdef CONFIG_LAST_STAGE_INIT
  316. WATCHDOG_RESET();
  317. /*
  318. * Some parts can be only initialized if all others (like
  319. * Interrupts) are up and running (i.e. the PC-style ISA
  320. * keyboard).
  321. */
  322. last_stage_init();
  323. #endif
  324. #ifdef CONFIG_POST
  325. post_run (NULL, POST_RAM | post_bootmode_get(0));
  326. #endif
  327. show_boot_progress(0x29);
  328. /* main_loop() can return to retry autoboot, if so just run it again. */
  329. for (;;) {
  330. main_loop();
  331. }
  332. /* NOTREACHED - no way out of command loop except booting */
  333. }
  334. void hang (void)
  335. {
  336. puts ("### ERROR ### Please RESET the board ###\n");
  337. for (;;);
  338. }
  339. unsigned long do_go_exec (ulong (*entry)(int, char *[]), int argc, char *argv[])
  340. {
  341. /*
  342. * TODO: Test this function - changed to fix compiler error.
  343. * Original code was:
  344. * return (entry >> 1) (argc, argv);
  345. * with a comment about Nios function pointers are address >> 1
  346. */
  347. return (entry) (argc, argv);
  348. }