board_f.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #ifdef CONFIG_SYS_SYM_OFFSETS
  103. bss_start = _bss_start_ofs + _TEXT_BASE;
  104. bss_end = _bss_end_ofs + _TEXT_BASE;
  105. #else
  106. bss_start = (ulong)&__bss_start;
  107. bss_end = (ulong)&__bss_end;
  108. #endif
  109. debug("U-Boot code: %08X -> %08lX BSS: -> %08lX\n",
  110. CONFIG_SYS_TEXT_BASE, bss_start, bss_end);
  111. #ifdef CONFIG_MODEM_SUPPORT
  112. debug("Modem Support enabled\n");
  113. #endif
  114. #ifdef CONFIG_USE_IRQ
  115. debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
  116. debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
  117. #endif
  118. return 0;
  119. }
  120. static int announce_dram_init(void)
  121. {
  122. puts("DRAM: ");
  123. return 0;
  124. }
  125. static int show_dram_config(void)
  126. {
  127. ulong size;
  128. #ifdef CONFIG_NR_DRAM_BANKS
  129. int i;
  130. debug("\nRAM Configuration:\n");
  131. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  132. size += gd->bd->bi_dram[i].size;
  133. debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  134. #ifdef DEBUG
  135. print_size(gd->bd->bi_dram[i].size, "\n");
  136. #endif
  137. }
  138. debug("\nDRAM: ");
  139. #else
  140. size = gd->ram_size;
  141. #endif
  142. print_size(size, "\n");
  143. return 0;
  144. }
  145. void __dram_init_banksize(void)
  146. {
  147. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  148. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  149. gd->bd->bi_dram[0].size = get_effective_memsize();
  150. #endif
  151. }
  152. void dram_init_banksize(void)
  153. __attribute__((weak, alias("__dram_init_banksize")));
  154. static int zero_global_data(void)
  155. {
  156. memset((void *)gd, '\0', sizeof(gd_t));
  157. return 0;
  158. }
  159. static int setup_mon_len(void)
  160. {
  161. #ifdef CONFIG_SYS_SYM_OFFSETS
  162. gd->mon_len = _bss_end_ofs;
  163. #else
  164. gd->mon_len = (ulong)&__bss_end - (ulong)&__text_start;
  165. #endif
  166. return 0;
  167. }
  168. __weak int arch_cpu_init(void)
  169. {
  170. return 0;
  171. }
  172. static int setup_fdt(void)
  173. {
  174. #ifdef CONFIG_OF_EMBED
  175. /* Get a pointer to the FDT */
  176. gd->fdt_blob = _binary_dt_dtb_start;
  177. #elif defined CONFIG_OF_SEPARATE
  178. /* FDT is at end of image */
  179. # ifdef CONFIG_SYS_SYM_OFFSETS
  180. gd->fdt_blob = (void *)(_end_ofs + CONFIG_SYS_TEXT_BASE);
  181. # else
  182. gd->fdt_blob = (ulong *)&_end;
  183. # endif
  184. #endif
  185. /* Allow the early environment to override the fdt address */
  186. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  187. (uintptr_t)gd->fdt_blob);
  188. return 0;
  189. }
  190. /* Get the top of usable RAM */
  191. __weak ulong board_get_usable_ram_top(ulong total_size)
  192. {
  193. return gd->ram_top;
  194. }
  195. static int setup_dest_addr(void)
  196. {
  197. debug("Monitor len: %08lX\n", gd->mon_len);
  198. /*
  199. * Ram is setup, size stored in gd !!
  200. */
  201. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  202. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  203. /*
  204. * Subtract specified amount of memory to hide so that it won't
  205. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  206. * the Linux kernel should now get passed the now "corrected"
  207. * memory size and won't touch it either. This should work
  208. * for arch/ppc and arch/powerpc. Only Linux board ports in
  209. * arch/powerpc with bootwrapper support, that recalculate the
  210. * memory size from the SDRAM controller setup will have to
  211. * get fixed.
  212. */
  213. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  214. #endif
  215. #ifdef CONFIG_SYS_SDRAM_BASE
  216. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  217. #endif
  218. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  219. gd->dest_addr = gd->ram_top;
  220. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  221. gd->dest_addr_sp = gd->dest_addr;
  222. return 0;
  223. }
  224. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  225. static int reserve_logbuffer(void)
  226. {
  227. /* reserve kernel log buffer */
  228. gd->dest_addr -= LOGBUFF_RESERVE;
  229. debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
  230. gd->dest_addr);
  231. return 0;
  232. }
  233. #endif
  234. #ifdef CONFIG_PRAM
  235. /* reserve protected RAM */
  236. static int reserve_pram(void)
  237. {
  238. ulong reg;
  239. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  240. gd->dest_addr -= (reg << 10); /* size is in kB */
  241. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  242. gd->dest_addr);
  243. return 0;
  244. }
  245. #endif /* CONFIG_PRAM */
  246. /* Round memory pointer down to next 4 kB limit */
  247. static int reserve_round_4k(void)
  248. {
  249. gd->dest_addr &= ~(4096 - 1);
  250. return 0;
  251. }
  252. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  253. defined(CONFIG_ARM)
  254. static int reserve_mmu(void)
  255. {
  256. /* reserve TLB table */
  257. gd->arch.tlb_size = 4096 * 4;
  258. gd->dest_addr -= gd->arch.tlb_size;
  259. /* round down to next 64 kB limit */
  260. gd->dest_addr &= ~(0x10000 - 1);
  261. gd->arch.tlb_addr = gd->dest_addr;
  262. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  263. gd->arch.tlb_addr + gd->arch.tlb_size);
  264. return 0;
  265. }
  266. #endif
  267. #ifdef CONFIG_LCD
  268. static int reserve_lcd(void)
  269. {
  270. #ifdef CONFIG_FB_ADDR
  271. gd->fb_base = CONFIG_FB_ADDR;
  272. #else
  273. /* reserve memory for LCD display (always full pages) */
  274. gd->dest_addr = lcd_setmem(gd->dest_addr);
  275. gd->fb_base = gd->dest_addr;
  276. #endif /* CONFIG_FB_ADDR */
  277. return 0;
  278. }
  279. #endif /* CONFIG_LCD */
  280. static int reserve_uboot(void)
  281. {
  282. /*
  283. * reserve memory for U-Boot code, data & bss
  284. * round down to next 4 kB limit
  285. */
  286. gd->dest_addr -= gd->mon_len;
  287. gd->dest_addr &= ~(4096 - 1);
  288. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  289. gd->dest_addr);
  290. return 0;
  291. }
  292. #ifndef CONFIG_SPL_BUILD
  293. /* reserve memory for malloc() area */
  294. static int reserve_malloc(void)
  295. {
  296. gd->dest_addr_sp = gd->dest_addr - TOTAL_MALLOC_LEN;
  297. debug("Reserving %dk for malloc() at: %08lx\n",
  298. TOTAL_MALLOC_LEN >> 10, gd->dest_addr_sp);
  299. return 0;
  300. }
  301. /* (permanently) allocate a Board Info struct */
  302. static int reserve_board(void)
  303. {
  304. gd->dest_addr_sp -= sizeof(bd_t);
  305. gd->bd = (bd_t *)gd->dest_addr_sp;
  306. memset(gd->bd, '\0', sizeof(bd_t));
  307. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  308. sizeof(bd_t), gd->dest_addr_sp);
  309. return 0;
  310. }
  311. #endif
  312. static int setup_machine(void)
  313. {
  314. #ifdef CONFIG_MACH_TYPE
  315. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  316. #endif
  317. return 0;
  318. }
  319. static int reserve_global_data(void)
  320. {
  321. gd->dest_addr_sp -= sizeof(gd_t);
  322. gd->new_gd = (gd_t *)gd->dest_addr_sp;
  323. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  324. sizeof(gd_t), gd->dest_addr_sp);
  325. return 0;
  326. }
  327. static int reserve_fdt(void)
  328. {
  329. /*
  330. * If the device tree is sitting immediate above our image then we
  331. * must relocate it. If it is embedded in the data section, then it
  332. * will be relocated with other data.
  333. */
  334. if (gd->fdt_blob) {
  335. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  336. gd->dest_addr_sp -= gd->fdt_size;
  337. gd->new_fdt = (void *)gd->dest_addr_sp;
  338. debug("Reserving %lu Bytes for FDT at: %p\n",
  339. gd->fdt_size, gd->new_fdt);
  340. }
  341. return 0;
  342. }
  343. static int reserve_stacks(void)
  344. {
  345. #ifdef CONFIG_SPL_BUILD
  346. # ifdef CONFIG_ARM
  347. gd->dest_addr_sp -= 128; /* leave 32 words for abort-stack */
  348. gd->irq_sp = gd->dest_addr_sp;
  349. # endif
  350. #else
  351. /* setup stack pointer for exceptions */
  352. gd->dest_addr_sp -= 16;
  353. gd->dest_addr_sp &= ~0xf;
  354. gd->irq_sp = gd->dest_addr_sp;
  355. /*
  356. * Handle architecture-specific things here
  357. * TODO(sjg@chromium.org): Perhaps create arch_reserve_stack()
  358. * to handle this and put in arch/xxx/lib/stack.c
  359. */
  360. # ifdef CONFIG_ARM
  361. # ifdef CONFIG_USE_IRQ
  362. gd->dest_addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
  363. debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
  364. CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ, gd->dest_addr_sp);
  365. /* 8-byte alignment for ARM ABI compliance */
  366. gd->dest_addr_sp &= ~0x07;
  367. # endif
  368. /* leave 3 words for abort-stack, plus 1 for alignment */
  369. gd->dest_addr_sp -= 16;
  370. # endif /* Architecture specific code */
  371. return 0;
  372. #endif
  373. }
  374. static int display_new_sp(void)
  375. {
  376. debug("New Stack Pointer is: %08lx\n", gd->dest_addr_sp);
  377. return 0;
  378. }
  379. #ifdef CONFIG_POST
  380. static int init_post(void)
  381. {
  382. post_bootmode_init();
  383. post_run(NULL, POST_ROM | post_bootmode_get(0));
  384. return 0;
  385. }
  386. #endif
  387. static int setup_baud_rate(void)
  388. {
  389. /* Ick, can we get rid of this line? */
  390. gd->bd->bi_baudrate = gd->baudrate;
  391. return 0;
  392. }
  393. static int setup_dram_config(void)
  394. {
  395. /* Ram is board specific, so move it to board code ... */
  396. dram_init_banksize();
  397. return 0;
  398. }
  399. static int reloc_fdt(void)
  400. {
  401. if (gd->new_fdt) {
  402. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  403. gd->fdt_blob = gd->new_fdt;
  404. }
  405. return 0;
  406. }
  407. static int setup_reloc(void)
  408. {
  409. gd->relocaddr = gd->dest_addr;
  410. gd->start_addr_sp = gd->dest_addr_sp;
  411. gd->reloc_off = gd->dest_addr - CONFIG_SYS_TEXT_BASE;
  412. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  413. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  414. debug("Relocating to %08lx, new gd at %p, sp at %08lx\n",
  415. gd->dest_addr, gd->new_gd, gd->dest_addr_sp);
  416. return 0;
  417. }
  418. /* ARM calls relocate_code from its crt0.S */
  419. #if !defined(CONFIG_ARM)
  420. static int jump_to_copy(void)
  421. {
  422. relocate_code(gd->dest_addr_sp, gd->new_gd, gd->dest_addr);
  423. return 0;
  424. }
  425. #endif
  426. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  427. static int mark_bootstage(void)
  428. {
  429. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  430. return 0;
  431. }
  432. static init_fnc_t init_sequence_f[] = {
  433. zero_global_data,
  434. setup_fdt,
  435. setup_mon_len,
  436. arch_cpu_init, /* basic arch cpu dependent setup */
  437. mark_bootstage,
  438. #ifdef CONFIG_OF_CONTROL
  439. fdtdec_check_fdt,
  440. #endif
  441. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  442. board_early_init_f,
  443. #endif
  444. timer_init, /* initialize timer */
  445. #ifdef CONFIG_BOARD_POSTCLK_INIT
  446. board_postclk_init,
  447. #endif
  448. #ifdef CONFIG_FSL_ESDHC
  449. get_clocks,
  450. #endif
  451. env_init, /* initialize environment */
  452. init_baud_rate, /* initialze baudrate settings */
  453. serial_init, /* serial communications setup */
  454. console_init_f, /* stage 1 init of console */
  455. display_options, /* say that we are here */
  456. display_text_info, /* show debugging info if required */
  457. #if defined(CONFIG_DISPLAY_CPUINFO)
  458. print_cpuinfo, /* display cpu info (and speed) */
  459. #endif
  460. #if defined(CONFIG_DISPLAY_BOARDINFO)
  461. checkboard, /* display board info */
  462. #endif
  463. announce_dram_init,
  464. /* TODO: unify all these dram functions? */
  465. #ifdef CONFIG_ARM
  466. dram_init, /* configure available RAM banks */
  467. #endif
  468. #ifdef CONFIG_POST
  469. init_post,
  470. #endif
  471. /*
  472. * Now that we have DRAM mapped and working, we can
  473. * relocate the code and continue running from DRAM.
  474. *
  475. * Reserve memory at end of RAM for (top down in that order):
  476. * - area that won't get touched by U-Boot and Linux (optional)
  477. * - kernel log buffer
  478. * - protected RAM
  479. * - LCD framebuffer
  480. * - monitor code
  481. * - board info struct
  482. */
  483. setup_dest_addr,
  484. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  485. reserve_logbuffer,
  486. #endif
  487. #ifdef CONFIG_PRAM
  488. reserve_pram,
  489. #endif
  490. reserve_round_4k,
  491. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  492. defined(CONFIG_ARM)
  493. reserve_mmu,
  494. #endif
  495. #ifdef CONFIG_LCD
  496. reserve_lcd,
  497. #endif
  498. reserve_uboot,
  499. #ifndef CONFIG_SPL_BUILD
  500. reserve_malloc,
  501. reserve_board,
  502. #endif
  503. setup_machine,
  504. reserve_global_data,
  505. reserve_fdt,
  506. reserve_stacks,
  507. setup_dram_config,
  508. show_dram_config,
  509. setup_baud_rate,
  510. display_new_sp,
  511. reloc_fdt,
  512. setup_reloc,
  513. #ifndef CONFIG_ARM
  514. jump_to_copy,
  515. #endif
  516. NULL,
  517. };
  518. void board_init_f(ulong boot_flags)
  519. {
  520. gd_t data;
  521. gd = &data;
  522. gd->flags = boot_flags;
  523. if (initcall_run_list(init_sequence_f))
  524. hang();
  525. #ifndef CONFIG_ARM
  526. /* NOTREACHED - jump_to_copy() does not return */
  527. hang();
  528. #endif
  529. }
  530. void hang(void)
  531. {
  532. puts("### ERROR ### Please RESET the board ###\n");
  533. for (;;);
  534. }