board.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <version.h>
  38. #include <malloc.h>
  39. #include <net.h>
  40. #include <ide.h>
  41. #include <serial.h>
  42. #include <asm/u-boot-x86.h>
  43. #include <elf.h>
  44. #include <asm/processor.h>
  45. #ifdef CONFIG_BITBANGMII
  46. #include <miiphy.h>
  47. #endif
  48. /************************************************************************
  49. * Init Utilities *
  50. ************************************************************************
  51. * Some of this code should be moved into the core functions,
  52. * or dropped completely,
  53. * but let's get it working (again) first...
  54. */
  55. static int init_baudrate(void)
  56. {
  57. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  58. return 0;
  59. }
  60. static int display_banner(void)
  61. {
  62. printf("\n\n%s\n\n", version_string);
  63. return 0;
  64. }
  65. static int display_dram_config(void)
  66. {
  67. int i;
  68. puts("DRAM Configuration:\n");
  69. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  70. printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  71. print_size(gd->bd->bi_dram[i].size, "\n");
  72. }
  73. return 0;
  74. }
  75. #ifndef CONFIG_SYS_NO_FLASH
  76. static void display_flash_config(ulong size)
  77. {
  78. puts("Flash: ");
  79. print_size(size, "\n");
  80. }
  81. #endif
  82. /*
  83. * Breath some life into the board...
  84. *
  85. * Initialize an SMC for serial comms, and carry out some hardware
  86. * tests.
  87. *
  88. * The first part of initialization is running from Flash memory;
  89. * its main purpose is to initialize the RAM so that we
  90. * can relocate the monitor code to RAM.
  91. */
  92. /*
  93. * All attempts to come up with a "common" initialization sequence
  94. * that works for all boards and architectures failed: some of the
  95. * requirements are just _too_ different. To get rid of the resulting
  96. * mess of board dependend #ifdef'ed code we now make the whole
  97. * initialization sequence configurable to the user.
  98. *
  99. * The requirements for any new initalization function is simple: it
  100. * receives a pointer to the "global data" structure as it's only
  101. * argument, and returns an integer return code, where 0 means
  102. * "continue" and != 0 means "fatal error, hang the system".
  103. */
  104. typedef int (init_fnc_t) (void);
  105. static int calculate_relocation_address(void);
  106. static int copy_uboot_to_ram(void);
  107. static int clear_bss(void);
  108. static int do_elf_reloc_fixups(void);
  109. static int copy_gd_to_ram(void);
  110. init_fnc_t *init_sequence_f[] = {
  111. cpu_init_f,
  112. board_early_init_f,
  113. env_init,
  114. init_baudrate,
  115. serial_init,
  116. console_init_f,
  117. dram_init_f,
  118. calculate_relocation_address,
  119. copy_uboot_to_ram,
  120. clear_bss,
  121. do_elf_reloc_fixups,
  122. NULL,
  123. };
  124. init_fnc_t *init_sequence_r[] = {
  125. copy_gd_to_ram,
  126. cpu_init_r, /* basic cpu dependent setup */
  127. board_early_init_r, /* basic board dependent setup */
  128. dram_init, /* configure available RAM banks */
  129. interrupt_init, /* set up exceptions */
  130. timer_init,
  131. display_banner,
  132. display_dram_config,
  133. NULL,
  134. };
  135. static int calculate_relocation_address(void)
  136. {
  137. ulong text_start = (ulong)&__text_start;
  138. ulong bss_end = (ulong)&__bss_end;
  139. ulong dest_addr;
  140. /*
  141. * NOTE: All destination address are rounded down to 16-byte
  142. * boundary to satisfy various worst-case alignment
  143. * requirements
  144. */
  145. /* Global Data is at top of available memory */
  146. dest_addr = gd->ram_size;
  147. dest_addr -= GENERATED_GBL_DATA_SIZE;
  148. dest_addr &= ~15;
  149. gd->new_gd_addr = dest_addr;
  150. /* GDT is below Global Data */
  151. dest_addr -= X86_GDT_SIZE;
  152. dest_addr &= ~15;
  153. gd->gdt_addr = dest_addr;
  154. /* Stack is below GDT */
  155. gd->start_addr_sp = dest_addr;
  156. /* U-Boot is below the stack */
  157. dest_addr -= CONFIG_SYS_STACK_SIZE;
  158. dest_addr -= (bss_end - text_start);
  159. dest_addr &= ~15;
  160. gd->relocaddr = dest_addr;
  161. gd->reloc_off = (dest_addr - text_start);
  162. return 0;
  163. }
  164. static int copy_uboot_to_ram(void)
  165. {
  166. size_t len = (size_t)&__data_end - (size_t)&__text_start;
  167. memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
  168. return 0;
  169. }
  170. static int clear_bss(void)
  171. {
  172. ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
  173. size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
  174. memset((void *)dst_addr, 0x00, len);
  175. return 0;
  176. }
  177. static int do_elf_reloc_fixups(void)
  178. {
  179. Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
  180. Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
  181. Elf32_Addr *offset_ptr_rom;
  182. Elf32_Addr *offset_ptr_ram;
  183. /* The size of the region of u-boot that runs out of RAM. */
  184. uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
  185. do {
  186. /* Get the location from the relocation entry */
  187. offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
  188. /* Check that the location of the relocation is in .text */
  189. if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
  190. /* Switch to the in-RAM version */
  191. offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
  192. gd->reloc_off);
  193. /* Check that the target points into .text */
  194. if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
  195. *offset_ptr_ram <
  196. (CONFIG_SYS_TEXT_BASE + size)) {
  197. *offset_ptr_ram += gd->reloc_off;
  198. }
  199. }
  200. } while (re_src++ < re_end);
  201. return 0;
  202. }
  203. /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
  204. void board_init_f(ulong boot_flags)
  205. {
  206. init_fnc_t **init_fnc_ptr;
  207. gd->flags = boot_flags;
  208. for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
  209. if ((*init_fnc_ptr)() != 0)
  210. hang();
  211. }
  212. /*
  213. * SDRAM is now initialised, U-Boot has been copied into SDRAM,
  214. * the BSS has been cleared etc. The final stack can now be setup
  215. * in SDRAM. Code execution will continue (momentarily) in Flash,
  216. * but with the stack in SDRAM and Global Data in temporary memory
  217. * (CPU cache)
  218. */
  219. board_init_f_r_trampoline(gd->start_addr_sp);
  220. /* NOTREACHED - board_init_f_r_trampoline() does not return */
  221. while (1)
  222. ;
  223. }
  224. void board_init_f_r(void)
  225. {
  226. /*
  227. * Transfer execution from Flash to RAM by calculating the address
  228. * of the in-RAM copy of board_init_r() and calling it
  229. */
  230. (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
  231. /* NOTREACHED - board_init_r() does not return */
  232. while (1)
  233. ;
  234. }
  235. static int copy_gd_to_ram(void)
  236. {
  237. gd_t *ram_gd;
  238. /*
  239. * Global data is still in temporary memory (the CPU cache).
  240. * calculate_relocation_address() has set gd->new_gd_addr to
  241. * where the global data lives in RAM but getting it there
  242. * safely is a bit tricky due to the 'F-Segment Hack' that
  243. * we need to use for x86
  244. */
  245. ram_gd = (gd_t *)gd->new_gd_addr;
  246. memcpy((void *)ram_gd, gd, sizeof(gd_t));
  247. /*
  248. * Reload the Global Descriptor Table so FS points to the
  249. * in-RAM copy of Global Data (calculate_relocation_address()
  250. * has already calculated the in-RAM location of the GDT)
  251. */
  252. ram_gd->gd_addr = (ulong)ram_gd;
  253. init_gd(ram_gd, (u64 *)gd->gdt_addr);
  254. return 0;
  255. }
  256. void board_init_r(gd_t *id, ulong dest_addr)
  257. {
  258. #if defined(CONFIG_CMD_NET)
  259. char *s;
  260. #endif
  261. #ifndef CONFIG_SYS_NO_FLASH
  262. ulong size;
  263. #endif
  264. static bd_t bd_data;
  265. init_fnc_t **init_fnc_ptr;
  266. show_boot_progress(0x21);
  267. /* compiler optimization barrier needed for GCC >= 3.4 */
  268. __asm__ __volatile__("" : : : "memory");
  269. gd->flags |= GD_FLG_RELOC;
  270. gd->bd = &bd_data;
  271. memset(gd->bd, 0, sizeof(bd_t));
  272. show_boot_progress(0x22);
  273. gd->baudrate = CONFIG_BAUDRATE;
  274. mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
  275. CONFIG_SYS_MALLOC_LEN);
  276. for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
  277. if ((*init_fnc_ptr)() != 0)
  278. hang();
  279. }
  280. show_boot_progress(0x23);
  281. #ifdef CONFIG_SERIAL_MULTI
  282. serial_initialize();
  283. #endif
  284. #ifndef CONFIG_SYS_NO_FLASH
  285. /* configure available FLASH banks */
  286. size = flash_init();
  287. display_flash_config(size);
  288. show_boot_progress(0x24);
  289. #endif
  290. show_boot_progress(0x25);
  291. /* initialize environment */
  292. env_relocate();
  293. show_boot_progress(0x26);
  294. #ifdef CONFIG_CMD_NET
  295. /* IP Address */
  296. bd_data.bi_ip_addr = getenv_IPaddr("ipaddr");
  297. #endif
  298. #if defined(CONFIG_PCI)
  299. /*
  300. * Do pci configuration
  301. */
  302. pci_init();
  303. #endif
  304. show_boot_progress(0x27);
  305. stdio_init();
  306. jumptable_init();
  307. /* Initialize the console (after the relocation and devices init) */
  308. console_init_r();
  309. #ifdef CONFIG_MISC_INIT_R
  310. /* miscellaneous platform dependent initialisations */
  311. misc_init_r();
  312. #endif
  313. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
  314. WATCHDOG_RESET();
  315. puts("PCMCIA:");
  316. pcmcia_init();
  317. #endif
  318. #if defined(CONFIG_CMD_KGDB)
  319. WATCHDOG_RESET();
  320. puts("KGDB: ");
  321. kgdb_init();
  322. #endif
  323. /* enable exceptions */
  324. enable_interrupts();
  325. show_boot_progress(0x28);
  326. #ifdef CONFIG_STATUS_LED
  327. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  328. #endif
  329. udelay(20);
  330. /* Initialize from environment */
  331. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  332. #if defined(CONFIG_CMD_NET)
  333. s = getenv("bootfile");
  334. if (s != NULL)
  335. copy_filename(BootFile, s, sizeof(BootFile));
  336. #endif
  337. WATCHDOG_RESET();
  338. #if defined(CONFIG_CMD_IDE)
  339. WATCHDOG_RESET();
  340. puts("IDE: ");
  341. ide_init();
  342. #endif
  343. #if defined(CONFIG_CMD_SCSI)
  344. WATCHDOG_RESET();
  345. puts("SCSI: ");
  346. scsi_init();
  347. #endif
  348. #if defined(CONFIG_CMD_DOC)
  349. WATCHDOG_RESET();
  350. puts("DOC: ");
  351. doc_init();
  352. #endif
  353. #ifdef CONFIG_BITBANGMII
  354. bb_miiphy_init();
  355. #endif
  356. #if defined(CONFIG_CMD_NET)
  357. WATCHDOG_RESET();
  358. puts("Net: ");
  359. eth_initialize(gd->bd);
  360. #endif
  361. #if (defined(CONFIG_CMD_NET)) && (0)
  362. WATCHDOG_RESET();
  363. # ifdef DEBUG
  364. puts("Reset Ethernet PHY\n");
  365. # endif
  366. reset_phy();
  367. #endif
  368. #ifdef CONFIG_LAST_STAGE_INIT
  369. WATCHDOG_RESET();
  370. /*
  371. * Some parts can be only initialized if all others (like
  372. * Interrupts) are up and running (i.e. the PC-style ISA
  373. * keyboard).
  374. */
  375. last_stage_init();
  376. #endif
  377. #ifdef CONFIG_POST
  378. post_run(NULL, POST_RAM | post_bootmode_get(0));
  379. #endif
  380. show_boot_progress(0x29);
  381. /* main_loop() can return to retry autoboot, if so just run it again. */
  382. for (;;)
  383. main_loop();
  384. /* NOTREACHED - no way out of command loop except booting */
  385. }
  386. void hang(void)
  387. {
  388. puts("### ERROR ### Please RESET the board ###\n");
  389. for (;;)
  390. ;
  391. }
  392. unsigned long do_go_exec(ulong (*entry)(int, char * const []),
  393. int argc, char * const argv[])
  394. {
  395. unsigned long ret = 0;
  396. char **argv_tmp;
  397. /*
  398. * x86 does not use a dedicated register to pass the pointer to
  399. * the global_data, so it is instead passed as argv[-1]. By using
  400. * argv[-1], the called 'Application' can use the contents of
  401. * argv natively. However, to safely use argv[-1] a new copy of
  402. * argv is needed with the extra element
  403. */
  404. argv_tmp = malloc(sizeof(char *) * (argc + 1));
  405. if (argv_tmp) {
  406. argv_tmp[0] = (char *)gd;
  407. memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
  408. ret = (entry) (argc, &argv_tmp[1]);
  409. free(argv_tmp);
  410. }
  411. return ret;
  412. }
  413. void setup_pcat_compatibility(void)
  414. __attribute__((weak, alias("__setup_pcat_compatibility")));
  415. void __setup_pcat_compatibility(void)
  416. {
  417. }