board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 <serial.h>
  40. #include <asm/u-boot-i386.h>
  41. #include <elf.h>
  42. #ifdef CONFIG_BITBANGMII
  43. #include <miiphy.h>
  44. #endif
  45. DECLARE_GLOBAL_DATA_PTR;
  46. /* Exports from the Linker Script */
  47. extern ulong _i386boot_text_start;
  48. extern ulong _i386boot_rel_dyn_start;
  49. extern ulong _i386boot_rel_dyn_end;
  50. extern ulong _i386boot_bss_start;
  51. extern ulong _i386boot_bss_size;
  52. void ram_bootstrap (void *, ulong);
  53. const char version_string[] =
  54. U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
  55. /************************************************************************
  56. * Init Utilities *
  57. ************************************************************************
  58. * Some of this code should be moved into the core functions,
  59. * or dropped completely,
  60. * but let's get it working (again) first...
  61. */
  62. static int init_baudrate (void)
  63. {
  64. char tmp[64]; /* long enough for environment variables */
  65. int i = getenv_r("baudrate", tmp, 64);
  66. gd->baudrate = (i != 0)
  67. ? (int) simple_strtoul (tmp, NULL, 10)
  68. : CONFIG_BAUDRATE;
  69. return (0);
  70. }
  71. static int display_banner (void)
  72. {
  73. printf ("\n\n%s\n\n", version_string);
  74. /*
  75. printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
  76. " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
  77. i386boot_start, i386boot_romdata_start-1,
  78. i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
  79. i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
  80. i386boot_bss_start+i386boot_bss_size,
  81. i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
  82. */
  83. return (0);
  84. }
  85. /*
  86. * WARNING: this code looks "cleaner" than the PowerPC version, but
  87. * has the disadvantage that you either get nothing, or everything.
  88. * On PowerPC, you might see "DRAM: " before the system hangs - which
  89. * gives a simple yet clear indication which part of the
  90. * initialization if failing.
  91. */
  92. static int display_dram_config (void)
  93. {
  94. int i;
  95. puts ("DRAM Configuration:\n");
  96. for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
  97. printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  98. print_size (gd->bd->bi_dram[i].size, "\n");
  99. }
  100. return (0);
  101. }
  102. static void display_flash_config (ulong size)
  103. {
  104. puts ("Flash: ");
  105. print_size (size, "\n");
  106. }
  107. /*
  108. * Breath some life into the board...
  109. *
  110. * Initialize an SMC for serial comms, and carry out some hardware
  111. * tests.
  112. *
  113. * The first part of initialization is running from Flash memory;
  114. * its main purpose is to initialize the RAM so that we
  115. * can relocate the monitor code to RAM.
  116. */
  117. /*
  118. * All attempts to come up with a "common" initialization sequence
  119. * that works for all boards and architectures failed: some of the
  120. * requirements are just _too_ different. To get rid of the resulting
  121. * mess of board dependend #ifdef'ed code we now make the whole
  122. * initialization sequence configurable to the user.
  123. *
  124. * The requirements for any new initalization function is simple: it
  125. * receives a pointer to the "global data" structure as it's only
  126. * argument, and returns an integer return code, where 0 means
  127. * "continue" and != 0 means "fatal error, hang the system".
  128. */
  129. typedef int (init_fnc_t) (void);
  130. init_fnc_t *init_sequence[] = {
  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. static gd_t gd_data;
  144. gd_t *gd;
  145. /*
  146. * Load U-Boot into RAM, initialize BSS, perform relocation adjustments
  147. */
  148. void board_init_f (ulong stack_limit)
  149. {
  150. void *text_start = &_i386boot_text_start;
  151. void *u_boot_cmd_end = &__u_boot_cmd_end;
  152. Elf32_Rel *rel_dyn_start = (Elf32_Rel *)&_i386boot_rel_dyn_start;
  153. Elf32_Rel *rel_dyn_end = (Elf32_Rel *)&_i386boot_rel_dyn_end;
  154. void *bss_start = &_i386boot_bss_start;
  155. ulong bss_size = (ulong)&_i386boot_bss_size;
  156. ulong uboot_size;
  157. void *dest_addr;
  158. ulong rel_offset;
  159. Elf32_Rel *re;
  160. void (*start_func)(void *, ulong);
  161. uboot_size = (ulong)u_boot_cmd_end - (ulong)text_start;
  162. dest_addr = (void *)stack_limit - (uboot_size + (ulong)bss_size);
  163. rel_offset = text_start - dest_addr;
  164. start_func = ram_bootstrap - rel_offset;
  165. /* First stage CPU initialization */
  166. if (cpu_init_f() != 0)
  167. hang();
  168. /* First stage Board initialization */
  169. if (board_early_init_f() != 0)
  170. hang();
  171. /* Copy U-Boot into RAM */
  172. memcpy(dest_addr, text_start, uboot_size);
  173. /* Clear BSS */
  174. memset(bss_start - rel_offset, 0, bss_size);
  175. /* Perform relocation adjustments */
  176. for (re = rel_dyn_start; re < rel_dyn_end; re++)
  177. {
  178. if (re->r_offset >= TEXT_BASE)
  179. if (*(ulong *)re->r_offset >= TEXT_BASE)
  180. *(ulong *)(re->r_offset - rel_offset) -= (Elf32_Addr)rel_offset;
  181. }
  182. /* Enter the relocated U-Boot! */
  183. start_func(dest_addr, rel_offset);
  184. /* NOTREACHED - board_init_f() does not return */
  185. while(1);
  186. }
  187. /*
  188. * We cannot initialize gd_data in board_init_f() because we would be
  189. * attempting to write to flash (I have even tried using manual relocation
  190. * adjustments on pointers but it just won't work) and board_init_r() does
  191. * not have enough arguments to allow us to pass the relocation offset
  192. * straight up. This bootstrap function (which runs in RAM) is used to
  193. * setup gd_data in order to pass the relocation offset to the rest of
  194. * U-Boot.
  195. *
  196. * TODO: The compiler optimization barrier is intended to stop GCC from
  197. * optimizing this function into board_init_f(). It seems to work without
  198. * it, but I've left it in to be sure. I think also that the barrier in
  199. * board_init_r() is no longer needed, but left it in 'just in case'
  200. */
  201. void ram_bootstrap (void *dest_addr, ulong rel_offset)
  202. {
  203. /* compiler optimization barrier needed for GCC >= 3.4 */
  204. __asm__ __volatile__("": : :"memory");
  205. /* tell others: relocation done */
  206. gd_data.reloc_off = rel_offset;
  207. gd_data.flags |= GD_FLG_RELOC;
  208. board_init_r(&gd_data, (ulong)dest_addr);
  209. }
  210. void board_init_r(gd_t *id, ulong dest_addr)
  211. {
  212. char *s;
  213. int i;
  214. ulong size;
  215. static bd_t bd_data;
  216. init_fnc_t **init_fnc_ptr;
  217. show_boot_progress(0x21);
  218. gd = id;
  219. /* compiler optimization barrier needed for GCC >= 3.4 */
  220. __asm__ __volatile__("": : :"memory");
  221. gd->bd = &bd_data;
  222. memset (gd->bd, 0, sizeof (bd_t));
  223. show_boot_progress(0x22);
  224. gd->baudrate = CONFIG_BAUDRATE;
  225. mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
  226. CONFIG_SYS_MALLOC_LEN);
  227. for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
  228. show_boot_progress(0xa130|i);
  229. if ((*init_fnc_ptr)() != 0) {
  230. hang ();
  231. }
  232. }
  233. show_boot_progress(0x23);
  234. #ifdef CONFIG_SERIAL_MULTI
  235. serial_initialize();
  236. #endif
  237. /* configure available FLASH banks */
  238. size = flash_init();
  239. display_flash_config(size);
  240. show_boot_progress(0x24);
  241. show_boot_progress(0x25);
  242. /* initialize environment */
  243. env_relocate ();
  244. show_boot_progress(0x26);
  245. #ifdef CONFIG_CMD_NET
  246. /* IP Address */
  247. bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
  248. #endif
  249. #if defined(CONFIG_PCI)
  250. /*
  251. * Do pci configuration
  252. */
  253. pci_init();
  254. #endif
  255. show_boot_progress(0x27);
  256. stdio_init ();
  257. jumptable_init ();
  258. /* Initialize the console (after the relocation and devices init) */
  259. console_init_r();
  260. #ifdef CONFIG_MISC_INIT_R
  261. /* miscellaneous platform dependent initialisations */
  262. misc_init_r();
  263. #endif
  264. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
  265. WATCHDOG_RESET();
  266. puts ("PCMCIA:");
  267. pcmcia_init();
  268. #endif
  269. #if defined(CONFIG_CMD_KGDB)
  270. WATCHDOG_RESET();
  271. puts("KGDB: ");
  272. kgdb_init();
  273. #endif
  274. /* enable exceptions */
  275. enable_interrupts();
  276. show_boot_progress(0x28);
  277. /* Must happen after interrupts are initialized since
  278. * an irq handler gets installed
  279. */
  280. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  281. serial_buffered_init();
  282. #endif
  283. #ifdef CONFIG_STATUS_LED
  284. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  285. #endif
  286. udelay(20);
  287. set_timer (0);
  288. /* Initialize from environment */
  289. if ((s = getenv ("loadaddr")) != NULL) {
  290. load_addr = simple_strtoul (s, NULL, 16);
  291. }
  292. #if defined(CONFIG_CMD_NET)
  293. if ((s = getenv ("bootfile")) != NULL) {
  294. copy_filename (BootFile, s, sizeof (BootFile));
  295. }
  296. #endif
  297. WATCHDOG_RESET();
  298. #if defined(CONFIG_CMD_IDE)
  299. WATCHDOG_RESET();
  300. puts("IDE: ");
  301. ide_init();
  302. #endif
  303. #if defined(CONFIG_CMD_SCSI)
  304. WATCHDOG_RESET();
  305. puts("SCSI: ");
  306. scsi_init();
  307. #endif
  308. #if defined(CONFIG_CMD_DOC)
  309. WATCHDOG_RESET();
  310. puts("DOC: ");
  311. doc_init();
  312. #endif
  313. #ifdef CONFIG_BITBANGMII
  314. bb_miiphy_init();
  315. #endif
  316. #if defined(CONFIG_CMD_NET)
  317. #if defined(CONFIG_NET_MULTI)
  318. WATCHDOG_RESET();
  319. puts("Net: ");
  320. #endif
  321. eth_initialize(gd->bd);
  322. #endif
  323. #if ( defined(CONFIG_CMD_NET)) && (0)
  324. WATCHDOG_RESET();
  325. # ifdef DEBUG
  326. puts ("Reset Ethernet PHY\n");
  327. # endif
  328. reset_phy();
  329. #endif
  330. #ifdef CONFIG_LAST_STAGE_INIT
  331. WATCHDOG_RESET();
  332. /*
  333. * Some parts can be only initialized if all others (like
  334. * Interrupts) are up and running (i.e. the PC-style ISA
  335. * keyboard).
  336. */
  337. last_stage_init();
  338. #endif
  339. #ifdef CONFIG_POST
  340. post_run (NULL, POST_RAM | post_bootmode_get(0));
  341. #endif
  342. show_boot_progress(0x29);
  343. /* main_loop() can return to retry autoboot, if so just run it again. */
  344. for (;;) {
  345. main_loop();
  346. }
  347. /* NOTREACHED - no way out of command loop except booting */
  348. }
  349. void hang (void)
  350. {
  351. puts ("### ERROR ### Please RESET the board ###\n");
  352. for (;;);
  353. }
  354. unsigned long do_go_exec (ulong (*entry)(int, char *[]), int argc, char * const argv[])
  355. {
  356. /*
  357. * x86 does not use a dedicated register to pass the pointer
  358. * to the global_data
  359. */
  360. argv[-1] = (char *)gd;
  361. return (entry) (argc, argv);
  362. }
  363. void setup_pcat_compatibility(void)
  364. __attribute__((weak, alias("__setup_pcat_compatibility")));
  365. void __setup_pcat_compatibility(void)
  366. {
  367. }