board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * (C) Copyright 2008-2011
  3. * Graeme Russ, <graeme.russ@gmail.com>
  4. *
  5. * (C) Copyright 2002
  6. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  7. *
  8. * (C) Copyright 2002
  9. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  10. *
  11. * (C) Copyright 2002
  12. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Marius Groeger <mgroeger@sysgo.de>
  14. *
  15. * See file CREDITS for list of people who contributed to this
  16. * project.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  31. * MA 02111-1307 USA
  32. */
  33. #include <common.h>
  34. #include <watchdog.h>
  35. #include <command.h>
  36. #include <stdio_dev.h>
  37. #include <timestamp.h>
  38. #include <version.h>
  39. #include <malloc.h>
  40. #include <net.h>
  41. #include <ide.h>
  42. #include <serial.h>
  43. #include <asm/u-boot-x86.h>
  44. #include <elf.h>
  45. #ifdef CONFIG_BITBANGMII
  46. #include <miiphy.h>
  47. #endif
  48. /*
  49. * Pointer to initial global data area
  50. *
  51. * Here we initialize it.
  52. */
  53. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  54. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  55. DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
  56. /* Exports from the Linker Script */
  57. extern ulong __text_start;
  58. extern ulong __data_end;
  59. extern ulong __rel_dyn_start;
  60. extern ulong __rel_dyn_end;
  61. extern ulong __bss_start;
  62. extern ulong __bss_end;
  63. const char version_string[] =
  64. U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
  65. /************************************************************************
  66. * Init Utilities *
  67. ************************************************************************
  68. * Some of this code should be moved into the core functions,
  69. * or dropped completely,
  70. * but let's get it working (again) first...
  71. */
  72. static int init_baudrate (void)
  73. {
  74. char tmp[64]; /* long enough for environment variables */
  75. int i = getenv_f("baudrate", tmp, 64);
  76. gd->baudrate = (i != 0)
  77. ? (int) simple_strtoul (tmp, NULL, 10)
  78. : CONFIG_BAUDRATE;
  79. return (0);
  80. }
  81. static int display_banner (void)
  82. {
  83. printf ("\n\n%s\n\n", version_string);
  84. /*
  85. printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
  86. " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
  87. i386boot_start, i386boot_romdata_start-1,
  88. i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
  89. i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
  90. i386boot_bss_start+i386boot_bss_size,
  91. i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
  92. */
  93. return (0);
  94. }
  95. static int display_dram_config (void)
  96. {
  97. int i;
  98. puts ("DRAM Configuration:\n");
  99. for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
  100. printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  101. print_size (gd->bd->bi_dram[i].size, "\n");
  102. }
  103. return (0);
  104. }
  105. static void display_flash_config (ulong size)
  106. {
  107. puts ("Flash: ");
  108. print_size (size, "\n");
  109. }
  110. /*
  111. * Breath some life into the board...
  112. *
  113. * Initialize an SMC for serial comms, and carry out some hardware
  114. * tests.
  115. *
  116. * The first part of initialization is running from Flash memory;
  117. * its main purpose is to initialize the RAM so that we
  118. * can relocate the monitor code to RAM.
  119. */
  120. /*
  121. * All attempts to come up with a "common" initialization sequence
  122. * that works for all boards and architectures failed: some of the
  123. * requirements are just _too_ different. To get rid of the resulting
  124. * mess of board dependend #ifdef'ed code we now make the whole
  125. * initialization sequence configurable to the user.
  126. *
  127. * The requirements for any new initalization function is simple: it
  128. * receives a pointer to the "global data" structure as it's only
  129. * argument, and returns an integer return code, where 0 means
  130. * "continue" and != 0 means "fatal error, hang the system".
  131. */
  132. typedef int (init_fnc_t) (void);
  133. static int calculate_relocation_address(void);
  134. static int copy_uboot_to_ram(void);
  135. static int clear_bss(void);
  136. static int do_elf_reloc_fixups(void);
  137. init_fnc_t *init_sequence_f[] = {
  138. cpu_init_f,
  139. board_early_init_f,
  140. env_init,
  141. init_baudrate,
  142. serial_init,
  143. console_init_f,
  144. dram_init_f,
  145. calculate_relocation_address,
  146. copy_uboot_to_ram,
  147. clear_bss,
  148. do_elf_reloc_fixups,
  149. NULL,
  150. };
  151. init_fnc_t *init_sequence_r[] = {
  152. cpu_init_r, /* basic cpu dependent setup */
  153. board_early_init_r, /* basic board dependent setup */
  154. dram_init, /* configure available RAM banks */
  155. interrupt_init, /* set up exceptions */
  156. timer_init,
  157. display_banner,
  158. display_dram_config,
  159. NULL,
  160. };
  161. gd_t *gd;
  162. static int calculate_relocation_address(void)
  163. {
  164. void *text_start = &__text_start;
  165. void *bss_end = &__bss_end;
  166. void *dest_addr;
  167. ulong rel_offset;
  168. /* Calculate destination RAM Address and relocation offset */
  169. dest_addr = (void *)gd->ram_size;
  170. dest_addr -= CONFIG_SYS_STACK_SIZE;
  171. dest_addr -= (bss_end - text_start);
  172. rel_offset = dest_addr - text_start;
  173. gd->start_addr_sp = gd->ram_size;
  174. gd->relocaddr = (ulong)dest_addr;
  175. gd->reloc_off = rel_offset;
  176. return 0;
  177. }
  178. static int copy_uboot_to_ram(void)
  179. {
  180. ulong *dst_addr = (ulong *)gd->relocaddr;
  181. ulong *src_addr = (ulong *)&__text_start;
  182. ulong *end_addr = (ulong *)&__data_end;
  183. while (src_addr < end_addr)
  184. *dst_addr++ = *src_addr++;
  185. return 0;
  186. }
  187. static int clear_bss(void)
  188. {
  189. void *bss_start = &__bss_start;
  190. void *bss_end = &__bss_end;
  191. ulong *dst_addr = (ulong *)(bss_start + gd->reloc_off);
  192. ulong *end_addr = (ulong *)(bss_end + gd->reloc_off);;
  193. while (dst_addr < end_addr)
  194. *dst_addr++ = 0x00000000;
  195. return 0;
  196. }
  197. static int do_elf_reloc_fixups(void)
  198. {
  199. Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
  200. Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
  201. do {
  202. if (re_src->r_offset >= CONFIG_SYS_TEXT_BASE)
  203. if (*(Elf32_Addr *)(re_src->r_offset + gd->reloc_off) >= CONFIG_SYS_TEXT_BASE)
  204. *(Elf32_Addr *)(re_src->r_offset + gd->reloc_off) += gd->reloc_off;
  205. } while (re_src++ < re_end);
  206. return 0;
  207. }
  208. /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
  209. void board_init_f(ulong boot_flags)
  210. {
  211. init_fnc_t **init_fnc_ptr;
  212. gd->flags = boot_flags;
  213. for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
  214. if ((*init_fnc_ptr)() != 0)
  215. hang();
  216. }
  217. gd->flags |= GD_FLG_RELOC;
  218. /* Enter the relocated U-Boot! */
  219. relocate_code(gd->start_addr_sp, gd, gd->relocaddr);
  220. /* NOTREACHED - relocate_code() does not return */
  221. while(1);
  222. }
  223. void board_init_r(gd_t *id, ulong dest_addr)
  224. {
  225. char *s;
  226. ulong size;
  227. static bd_t bd_data;
  228. static gd_t gd_data;
  229. init_fnc_t **init_fnc_ptr;
  230. show_boot_progress(0x21);
  231. /* Global data pointer is now writable */
  232. gd = &gd_data;
  233. memcpy(gd, id, sizeof(gd_t));
  234. /* compiler optimization barrier needed for GCC >= 3.4 */
  235. __asm__ __volatile__("": : :"memory");
  236. gd->bd = &bd_data;
  237. memset (gd->bd, 0, sizeof (bd_t));
  238. show_boot_progress(0x22);
  239. gd->baudrate = CONFIG_BAUDRATE;
  240. mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
  241. CONFIG_SYS_MALLOC_LEN);
  242. for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
  243. if ((*init_fnc_ptr)() != 0)
  244. hang ();
  245. }
  246. show_boot_progress(0x23);
  247. #ifdef CONFIG_SERIAL_MULTI
  248. serial_initialize();
  249. #endif
  250. /* configure available FLASH banks */
  251. size = flash_init();
  252. display_flash_config(size);
  253. show_boot_progress(0x24);
  254. show_boot_progress(0x25);
  255. /* initialize environment */
  256. env_relocate ();
  257. show_boot_progress(0x26);
  258. #ifdef CONFIG_CMD_NET
  259. /* IP Address */
  260. bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
  261. #endif
  262. #if defined(CONFIG_PCI)
  263. /*
  264. * Do pci configuration
  265. */
  266. pci_init();
  267. #endif
  268. show_boot_progress(0x27);
  269. stdio_init ();
  270. jumptable_init ();
  271. /* Initialize the console (after the relocation and devices init) */
  272. console_init_r();
  273. #ifdef CONFIG_MISC_INIT_R
  274. /* miscellaneous platform dependent initialisations */
  275. misc_init_r();
  276. #endif
  277. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
  278. WATCHDOG_RESET();
  279. puts ("PCMCIA:");
  280. pcmcia_init();
  281. #endif
  282. #if defined(CONFIG_CMD_KGDB)
  283. WATCHDOG_RESET();
  284. puts("KGDB: ");
  285. kgdb_init();
  286. #endif
  287. /* enable exceptions */
  288. enable_interrupts();
  289. show_boot_progress(0x28);
  290. #ifdef CONFIG_STATUS_LED
  291. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  292. #endif
  293. udelay(20);
  294. /* Initialize from environment */
  295. if ((s = getenv ("loadaddr")) != NULL) {
  296. load_addr = simple_strtoul (s, NULL, 16);
  297. }
  298. #if defined(CONFIG_CMD_NET)
  299. if ((s = getenv ("bootfile")) != NULL) {
  300. copy_filename (BootFile, s, sizeof (BootFile));
  301. }
  302. #endif
  303. WATCHDOG_RESET();
  304. #if defined(CONFIG_CMD_IDE)
  305. WATCHDOG_RESET();
  306. puts("IDE: ");
  307. ide_init();
  308. #endif
  309. #if defined(CONFIG_CMD_SCSI)
  310. WATCHDOG_RESET();
  311. puts("SCSI: ");
  312. scsi_init();
  313. #endif
  314. #if defined(CONFIG_CMD_DOC)
  315. WATCHDOG_RESET();
  316. puts("DOC: ");
  317. doc_init();
  318. #endif
  319. #ifdef CONFIG_BITBANGMII
  320. bb_miiphy_init();
  321. #endif
  322. #if defined(CONFIG_CMD_NET)
  323. #if defined(CONFIG_NET_MULTI)
  324. WATCHDOG_RESET();
  325. puts("Net: ");
  326. #endif
  327. eth_initialize(gd->bd);
  328. #endif
  329. #if ( defined(CONFIG_CMD_NET)) && (0)
  330. WATCHDOG_RESET();
  331. # ifdef DEBUG
  332. puts ("Reset Ethernet PHY\n");
  333. # endif
  334. reset_phy();
  335. #endif
  336. #ifdef CONFIG_LAST_STAGE_INIT
  337. WATCHDOG_RESET();
  338. /*
  339. * Some parts can be only initialized if all others (like
  340. * Interrupts) are up and running (i.e. the PC-style ISA
  341. * keyboard).
  342. */
  343. last_stage_init();
  344. #endif
  345. #ifdef CONFIG_POST
  346. post_run (NULL, POST_RAM | post_bootmode_get(0));
  347. #endif
  348. show_boot_progress(0x29);
  349. /* main_loop() can return to retry autoboot, if so just run it again. */
  350. for (;;) {
  351. main_loop();
  352. }
  353. /* NOTREACHED - no way out of command loop except booting */
  354. }
  355. void hang (void)
  356. {
  357. puts ("### ERROR ### Please RESET the board ###\n");
  358. for (;;);
  359. }
  360. unsigned long do_go_exec (ulong (*entry)(int, char * const []), int argc, char * const argv[])
  361. {
  362. unsigned long ret = 0;
  363. char **argv_tmp;
  364. /*
  365. * x86 does not use a dedicated register to pass the pointer to
  366. * the global_data, so it is instead passed as argv[-1]. By using
  367. * argv[-1], the called 'Application' can use the contents of
  368. * argv natively. However, to safely use argv[-1] a new copy of
  369. * argv is needed with the extra element
  370. */
  371. argv_tmp = malloc(sizeof(char *) * (argc + 1));
  372. if (argv_tmp) {
  373. argv_tmp[0] = (char *)gd;
  374. memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
  375. ret = (entry) (argc, &argv_tmp[1]);
  376. free(argv_tmp);
  377. }
  378. return ret;
  379. }
  380. void setup_pcat_compatibility(void)
  381. __attribute__((weak, alias("__setup_pcat_compatibility")));
  382. void __setup_pcat_compatibility(void)
  383. {
  384. }