board_f.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2002-2006
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <linux/compiler.h>
  30. #include <version.h>
  31. #include <environment.h>
  32. #include <fdtdec.h>
  33. #include <initcall.h>
  34. #include <logbuff.h>
  35. #include <post.h>
  36. #include <asm/io.h>
  37. #include <asm/sections.h>
  38. #include <linux/compiler.h>
  39. /*
  40. * Pointer to initial global data area
  41. *
  42. * Here we initialize it if needed.
  43. */
  44. #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
  45. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  46. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  47. DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
  48. #else
  49. DECLARE_GLOBAL_DATA_PTR;
  50. #endif
  51. /*
  52. * sjg: IMO this code should be
  53. * refactored to a single function, something like:
  54. *
  55. * void led_set_state(enum led_colour_t colour, int on);
  56. */
  57. /************************************************************************
  58. * Coloured LED functionality
  59. ************************************************************************
  60. * May be supplied by boards if desired
  61. */
  62. inline void __coloured_LED_init(void) {}
  63. void coloured_LED_init(void)
  64. __attribute__((weak, alias("__coloured_LED_init")));
  65. inline void __red_led_on(void) {}
  66. void red_led_on(void) __attribute__((weak, alias("__red_led_on")));
  67. inline void __red_led_off(void) {}
  68. void red_led_off(void) __attribute__((weak, alias("__red_led_off")));
  69. inline void __green_led_on(void) {}
  70. void green_led_on(void) __attribute__((weak, alias("__green_led_on")));
  71. inline void __green_led_off(void) {}
  72. void green_led_off(void) __attribute__((weak, alias("__green_led_off")));
  73. inline void __yellow_led_on(void) {}
  74. void yellow_led_on(void) __attribute__((weak, alias("__yellow_led_on")));
  75. inline void __yellow_led_off(void) {}
  76. void yellow_led_off(void) __attribute__((weak, alias("__yellow_led_off")));
  77. inline void __blue_led_on(void) {}
  78. void blue_led_on(void) __attribute__((weak, alias("__blue_led_on")));
  79. inline void __blue_led_off(void) {}
  80. void blue_led_off(void) __attribute__((weak, alias("__blue_led_off")));
  81. /*
  82. * Why is gd allocated a register? Prior to reloc it might be better to
  83. * just pass it around to each function in this file?
  84. *
  85. * After reloc one could argue that it is hardly used and doesn't need
  86. * to be in a register. Or if it is it should perhaps hold pointers to all
  87. * global data for all modules, so that post-reloc we can avoid the massive
  88. * literal pool we get on ARM. Or perhaps just encourage each module to use
  89. * a structure...
  90. */
  91. /*
  92. * Could the CONFIG_SPL_BUILD infection become a flag in gd?
  93. */
  94. static int init_baud_rate(void)
  95. {
  96. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  97. return 0;
  98. }
  99. static int display_text_info(void)
  100. {
  101. ulong bss_start, bss_end;
  102. bss_start = _bss_start_ofs + _TEXT_BASE;
  103. bss_end = _bss_end_ofs + _TEXT_BASE;
  104. debug("U-Boot code: %08X -> %08lX BSS: -> %08lX\n",
  105. CONFIG_SYS_TEXT_BASE, bss_start, bss_end);
  106. #ifdef CONFIG_MODEM_SUPPORT
  107. debug("Modem Support enabled\n");
  108. #endif
  109. #ifdef CONFIG_USE_IRQ
  110. debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
  111. debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
  112. #endif
  113. return 0;
  114. }
  115. static int announce_dram_init(void)
  116. {
  117. puts("DRAM: ");
  118. return 0;
  119. }
  120. static int show_dram_config(void)
  121. {
  122. ulong size;
  123. #ifdef CONFIG_NR_DRAM_BANKS
  124. int i;
  125. debug("\nRAM Configuration:\n");
  126. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  127. size += gd->bd->bi_dram[i].size;
  128. debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  129. #ifdef DEBUG
  130. print_size(gd->bd->bi_dram[i].size, "\n");
  131. #endif
  132. }
  133. debug("\nDRAM: ");
  134. #else
  135. size = gd->ram_size;
  136. #endif
  137. print_size(size, "\n");
  138. return 0;
  139. }
  140. void __dram_init_banksize(void)
  141. {
  142. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  143. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  144. gd->bd->bi_dram[0].size = get_effective_memsize();
  145. #endif
  146. }
  147. void dram_init_banksize(void)
  148. __attribute__((weak, alias("__dram_init_banksize")));
  149. static int zero_global_data(void)
  150. {
  151. memset((void *)gd, '\0', sizeof(gd_t));
  152. return 0;
  153. }
  154. static int setup_mon_len(void)
  155. {
  156. gd->mon_len = _bss_end_ofs;
  157. return 0;
  158. }
  159. __weak int arch_cpu_init(void)
  160. {
  161. return 0;
  162. }
  163. static int setup_fdt(void)
  164. {
  165. #ifdef CONFIG_OF_EMBED
  166. /* Get a pointer to the FDT */
  167. gd->fdt_blob = _binary_dt_dtb_start;
  168. #elif defined CONFIG_OF_SEPARATE
  169. /* FDT is at end of image */
  170. gd->fdt_blob = (void *)(_end_ofs + CONFIG_SYS_TEXT_BASE);
  171. #endif
  172. /* Allow the early environment to override the fdt address */
  173. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  174. (uintptr_t)gd->fdt_blob);
  175. return 0;
  176. }
  177. /* Get the top of usable RAM */
  178. __weak ulong board_get_usable_ram_top(ulong total_size)
  179. {
  180. return gd->ram_top;
  181. }
  182. static int setup_dest_addr(void)
  183. {
  184. debug("Monitor len: %08lX\n", gd->mon_len);
  185. /*
  186. * Ram is setup, size stored in gd !!
  187. */
  188. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  189. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  190. /*
  191. * Subtract specified amount of memory to hide so that it won't
  192. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  193. * the Linux kernel should now get passed the now "corrected"
  194. * memory size and won't touch it either. This should work
  195. * for arch/ppc and arch/powerpc. Only Linux board ports in
  196. * arch/powerpc with bootwrapper support, that recalculate the
  197. * memory size from the SDRAM controller setup will have to
  198. * get fixed.
  199. */
  200. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  201. #endif
  202. #ifdef CONFIG_SYS_SDRAM_BASE
  203. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  204. #endif
  205. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  206. gd->dest_addr = gd->ram_top;
  207. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  208. gd->dest_addr_sp = gd->dest_addr;
  209. return 0;
  210. }
  211. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  212. static int reserve_logbuffer(void)
  213. {
  214. /* reserve kernel log buffer */
  215. gd->dest_addr -= LOGBUFF_RESERVE;
  216. debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
  217. gd->dest_addr);
  218. return 0;
  219. }
  220. #endif
  221. #ifdef CONFIG_PRAM
  222. /* reserve protected RAM */
  223. static int reserve_pram(void)
  224. {
  225. ulong reg;
  226. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  227. gd->dest_addr -= (reg << 10); /* size is in kB */
  228. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  229. gd->dest_addr);
  230. return 0;
  231. }
  232. #endif /* CONFIG_PRAM */
  233. /* Round memory pointer down to next 4 kB limit */
  234. static int reserve_round_4k(void)
  235. {
  236. gd->dest_addr &= ~(4096 - 1);
  237. return 0;
  238. }
  239. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  240. defined(CONFIG_ARM)
  241. static int reserve_mmu(void)
  242. {
  243. /* reserve TLB table */
  244. gd->arch.tlb_size = 4096 * 4;
  245. gd->dest_addr -= gd->arch.tlb_size;
  246. /* round down to next 64 kB limit */
  247. gd->dest_addr &= ~(0x10000 - 1);
  248. gd->arch.tlb_addr = gd->dest_addr;
  249. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  250. gd->arch.tlb_addr + gd->arch.tlb_size);
  251. return 0;
  252. }
  253. #endif
  254. #ifdef CONFIG_LCD
  255. static int reserve_lcd(void)
  256. {
  257. #ifdef CONFIG_FB_ADDR
  258. gd->fb_base = CONFIG_FB_ADDR;
  259. #else
  260. /* reserve memory for LCD display (always full pages) */
  261. gd->dest_addr = lcd_setmem(gd->dest_addr);
  262. gd->fb_base = gd->dest_addr;
  263. #endif /* CONFIG_FB_ADDR */
  264. return 0;
  265. }
  266. #endif /* CONFIG_LCD */
  267. static int reserve_uboot(void)
  268. {
  269. /*
  270. * reserve memory for U-Boot code, data & bss
  271. * round down to next 4 kB limit
  272. */
  273. gd->dest_addr -= gd->mon_len;
  274. gd->dest_addr &= ~(4096 - 1);
  275. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  276. gd->dest_addr);
  277. return 0;
  278. }
  279. /* reserve memory for malloc() area */
  280. static int reserve_malloc(void)
  281. {
  282. gd->dest_addr_sp = gd->dest_addr - TOTAL_MALLOC_LEN;
  283. debug("Reserving %dk for malloc() at: %08lx\n",
  284. TOTAL_MALLOC_LEN >> 10, gd->dest_addr_sp);
  285. return 0;
  286. }
  287. /* (permanently) allocate a Board Info struct */
  288. static int reserve_board(void)
  289. {
  290. gd->dest_addr_sp -= sizeof(bd_t);
  291. gd->bd = (bd_t *)gd->dest_addr_sp;
  292. memset(gd->bd, '\0', sizeof(bd_t));
  293. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  294. sizeof(bd_t), gd->dest_addr_sp);
  295. return 0;
  296. }
  297. static int setup_machine(void)
  298. {
  299. #ifdef CONFIG_MACH_TYPE
  300. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  301. #endif
  302. return 0;
  303. }
  304. static int reserve_global_data(void)
  305. {
  306. gd->dest_addr_sp -= sizeof(gd_t);
  307. gd->new_gd = (gd_t *)gd->dest_addr_sp;
  308. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  309. sizeof(gd_t), gd->dest_addr_sp);
  310. return 0;
  311. }
  312. static int reserve_fdt(void)
  313. {
  314. /*
  315. * If the device tree is sitting immediate above our image then we
  316. * must relocate it. If it is embedded in the data section, then it
  317. * will be relocated with other data.
  318. */
  319. if (gd->fdt_blob) {
  320. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  321. gd->dest_addr_sp -= gd->fdt_size;
  322. gd->new_fdt = (void *)gd->dest_addr_sp;
  323. debug("Reserving %lu Bytes for FDT at: %p\n",
  324. gd->fdt_size, gd->new_fdt);
  325. }
  326. return 0;
  327. }
  328. static int reserve_stacks(void)
  329. {
  330. /* setup stack pointer for exceptions */
  331. gd->dest_addr_sp -= 16;
  332. gd->dest_addr_sp &= ~0xf;
  333. gd->irq_sp = gd->dest_addr_sp;
  334. /*
  335. * Handle architecture-specific things here
  336. * TODO(sjg@chromium.org): Perhaps create arch_reserve_stack()
  337. * to handle this and put in arch/xxx/lib/stack.c
  338. */
  339. # ifdef CONFIG_ARM
  340. # ifdef CONFIG_USE_IRQ
  341. gd->dest_addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
  342. debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
  343. CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ, gd->dest_addr_sp);
  344. /* 8-byte alignment for ARM ABI compliance */
  345. gd->dest_addr_sp &= ~0x07;
  346. # endif
  347. /* leave 3 words for abort-stack, plus 1 for alignment */
  348. gd->dest_addr_sp -= 16;
  349. return 0;
  350. }
  351. static int display_new_sp(void)
  352. {
  353. debug("New Stack Pointer is: %08lx\n", gd->dest_addr_sp);
  354. return 0;
  355. }
  356. #ifdef CONFIG_POST
  357. static int init_post(void)
  358. {
  359. post_bootmode_init();
  360. post_run(NULL, POST_ROM | post_bootmode_get(0));
  361. return 0;
  362. }
  363. #endif
  364. static int setup_baud_rate(void)
  365. {
  366. /* Ick, can we get rid of this line? */
  367. gd->bd->bi_baudrate = gd->baudrate;
  368. return 0;
  369. }
  370. static int setup_dram_config(void)
  371. {
  372. /* Ram is board specific, so move it to board code ... */
  373. dram_init_banksize();
  374. return 0;
  375. }
  376. static int reloc_fdt(void)
  377. {
  378. if (gd->new_fdt) {
  379. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  380. gd->fdt_blob = gd->new_fdt;
  381. }
  382. return 0;
  383. }
  384. static int setup_reloc(void)
  385. {
  386. gd->relocaddr = gd->dest_addr;
  387. gd->start_addr_sp = gd->dest_addr_sp;
  388. gd->reloc_off = gd->dest_addr - CONFIG_SYS_TEXT_BASE;
  389. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  390. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  391. debug("Relocating to %08lx, new gd at %p, sp at %08lx\n",
  392. gd->dest_addr, gd->new_gd, gd->dest_addr_sp);
  393. return 0;
  394. }
  395. /* ARM calls relocate_code from its crt0.S */
  396. #if !defined(CONFIG_ARM)
  397. static int jump_to_copy(void)
  398. {
  399. relocate_code(gd->dest_addr_sp, gd->new_gd, gd->dest_addr);
  400. return 0;
  401. }
  402. #endif
  403. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  404. static int mark_bootstage(void)
  405. {
  406. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  407. return 0;
  408. }
  409. static init_fnc_t init_sequence_f[] = {
  410. setup_fdt,
  411. setup_mon_len,
  412. arch_cpu_init, /* basic arch cpu dependent setup */
  413. mark_bootstage,
  414. #ifdef CONFIG_OF_CONTROL
  415. fdtdec_check_fdt,
  416. #endif
  417. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  418. board_early_init_f,
  419. #endif
  420. timer_init, /* initialize timer */
  421. #ifdef CONFIG_BOARD_POSTCLK_INIT
  422. board_postclk_init,
  423. #endif
  424. #ifdef CONFIG_FSL_ESDHC
  425. get_clocks,
  426. #endif
  427. env_init, /* initialize environment */
  428. init_baud_rate, /* initialze baudrate settings */
  429. serial_init, /* serial communications setup */
  430. console_init_f, /* stage 1 init of console */
  431. display_options, /* say that we are here */
  432. display_text_info, /* show debugging info if required */
  433. #if defined(CONFIG_DISPLAY_CPUINFO)
  434. print_cpuinfo, /* display cpu info (and speed) */
  435. #endif
  436. #if defined(CONFIG_DISPLAY_BOARDINFO)
  437. checkboard, /* display board info */
  438. #endif
  439. announce_dram_init,
  440. /* TODO: unify all these dram functions? */
  441. #ifdef CONFIG_ARM
  442. dram_init, /* configure available RAM banks */
  443. #endif
  444. #ifdef CONFIG_POST
  445. init_post,
  446. #endif
  447. /*
  448. * Now that we have DRAM mapped and working, we can
  449. * relocate the code and continue running from DRAM.
  450. *
  451. * Reserve memory at end of RAM for (top down in that order):
  452. * - area that won't get touched by U-Boot and Linux (optional)
  453. * - kernel log buffer
  454. * - protected RAM
  455. * - LCD framebuffer
  456. * - monitor code
  457. * - board info struct
  458. */
  459. setup_dest_addr,
  460. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  461. reserve_logbuffer,
  462. #endif
  463. #ifdef CONFIG_PRAM
  464. reserve_pram,
  465. #endif
  466. reserve_round_4k,
  467. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  468. defined(CONFIG_ARM)
  469. reserve_mmu,
  470. #endif
  471. #ifdef CONFIG_LCD
  472. reserve_lcd,
  473. #endif
  474. reserve_uboot,
  475. reserve_malloc,
  476. reserve_board,
  477. setup_machine,
  478. reserve_global_data,
  479. reserve_fdt,
  480. reserve_stacks,
  481. setup_dram_config,
  482. show_dram_config,
  483. setup_baud_rate,
  484. display_new_sp,
  485. reloc_fdt,
  486. setup_reloc,
  487. #ifndef CONFIG_ARM
  488. jump_to_copy,
  489. #endif
  490. NULL,
  491. };
  492. void board_init_f(ulong boot_flags)
  493. {
  494. gd_t data;
  495. gd = &data;
  496. gd->flags = boot_flags;
  497. if (initcall_run_list(init_sequence_f))
  498. hang();
  499. #ifndef CONFIG_ARM
  500. /* NOTREACHED - jump_to_copy() does not return */
  501. hang();
  502. #endif
  503. }
  504. void hang(void)
  505. {
  506. puts("### ERROR ### Please RESET the board ###\n");
  507. for (;;);
  508. }