board_f.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <console.h>
  14. #include <environment.h>
  15. #include <dm.h>
  16. #include <fdtdec.h>
  17. #include <fs.h>
  18. #include <i2c.h>
  19. #include <initcall.h>
  20. #include <malloc.h>
  21. #include <mapmem.h>
  22. #include <os.h>
  23. #include <post.h>
  24. #include <relocate.h>
  25. #include <spi.h>
  26. #include <status_led.h>
  27. #include <timer.h>
  28. #include <trace.h>
  29. #include <video.h>
  30. #include <watchdog.h>
  31. #ifdef CONFIG_MACH_TYPE
  32. #include <asm/mach-types.h>
  33. #endif
  34. #if defined(CONFIG_MP) && defined(CONFIG_PPC)
  35. #include <asm/mp.h>
  36. #endif
  37. #include <asm/io.h>
  38. #include <asm/sections.h>
  39. #include <dm/root.h>
  40. #include <linux/errno.h>
  41. /*
  42. * Pointer to initial global data area
  43. *
  44. * Here we initialize it if needed.
  45. */
  46. #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
  47. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  48. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  49. DECLARE_GLOBAL_DATA_PTR = (gd_t *)(CONFIG_SYS_INIT_GD_ADDR);
  50. #else
  51. DECLARE_GLOBAL_DATA_PTR;
  52. #endif
  53. /*
  54. * TODO(sjg@chromium.org): IMO this code should be
  55. * refactored to a single function, something like:
  56. *
  57. * void led_set_state(enum led_colour_t colour, int on);
  58. */
  59. /************************************************************************
  60. * Coloured LED functionality
  61. ************************************************************************
  62. * May be supplied by boards if desired
  63. */
  64. __weak void coloured_LED_init(void) {}
  65. __weak void red_led_on(void) {}
  66. __weak void red_led_off(void) {}
  67. __weak void green_led_on(void) {}
  68. __weak void green_led_off(void) {}
  69. __weak void yellow_led_on(void) {}
  70. __weak void yellow_led_off(void) {}
  71. __weak void blue_led_on(void) {}
  72. __weak void blue_led_off(void) {}
  73. /*
  74. * Why is gd allocated a register? Prior to reloc it might be better to
  75. * just pass it around to each function in this file?
  76. *
  77. * After reloc one could argue that it is hardly used and doesn't need
  78. * to be in a register. Or if it is it should perhaps hold pointers to all
  79. * global data for all modules, so that post-reloc we can avoid the massive
  80. * literal pool we get on ARM. Or perhaps just encourage each module to use
  81. * a structure...
  82. */
  83. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
  84. static int init_func_watchdog_init(void)
  85. {
  86. # if defined(CONFIG_HW_WATCHDOG) && \
  87. (defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
  88. defined(CONFIG_SH) || defined(CONFIG_AT91SAM9_WATCHDOG) || \
  89. defined(CONFIG_DESIGNWARE_WATCHDOG) || \
  90. defined(CONFIG_IMX_WATCHDOG))
  91. hw_watchdog_init();
  92. puts(" Watchdog enabled\n");
  93. # endif
  94. WATCHDOG_RESET();
  95. return 0;
  96. }
  97. int init_func_watchdog_reset(void)
  98. {
  99. WATCHDOG_RESET();
  100. return 0;
  101. }
  102. #endif /* CONFIG_WATCHDOG */
  103. __weak void board_add_ram_info(int use_default)
  104. {
  105. /* please define platform specific board_add_ram_info() */
  106. }
  107. static int init_baud_rate(void)
  108. {
  109. gd->baudrate = env_get_ulong("baudrate", 10, CONFIG_BAUDRATE);
  110. return 0;
  111. }
  112. static int display_text_info(void)
  113. {
  114. #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
  115. ulong bss_start, bss_end, text_base;
  116. bss_start = (ulong)&__bss_start;
  117. bss_end = (ulong)&__bss_end;
  118. #ifdef CONFIG_SYS_TEXT_BASE
  119. text_base = CONFIG_SYS_TEXT_BASE;
  120. #else
  121. text_base = CONFIG_SYS_MONITOR_BASE;
  122. #endif
  123. debug("U-Boot code: %08lX -> %08lX BSS: -> %08lX\n",
  124. text_base, bss_start, bss_end);
  125. #endif
  126. return 0;
  127. }
  128. static int announce_dram_init(void)
  129. {
  130. puts("DRAM: ");
  131. return 0;
  132. }
  133. static int show_dram_config(void)
  134. {
  135. unsigned long long size;
  136. #ifdef CONFIG_NR_DRAM_BANKS
  137. int i;
  138. debug("\nRAM Configuration:\n");
  139. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  140. size += gd->bd->bi_dram[i].size;
  141. debug("Bank #%d: %llx ", i,
  142. (unsigned long long)(gd->bd->bi_dram[i].start));
  143. #ifdef DEBUG
  144. print_size(gd->bd->bi_dram[i].size, "\n");
  145. #endif
  146. }
  147. debug("\nDRAM: ");
  148. #else
  149. size = gd->ram_size;
  150. #endif
  151. print_size(size, "");
  152. board_add_ram_info(0);
  153. putc('\n');
  154. return 0;
  155. }
  156. __weak int dram_init_banksize(void)
  157. {
  158. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  159. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  160. gd->bd->bi_dram[0].size = get_effective_memsize();
  161. #endif
  162. return 0;
  163. }
  164. #if defined(CONFIG_SYS_I2C)
  165. static int init_func_i2c(void)
  166. {
  167. puts("I2C: ");
  168. #ifdef CONFIG_SYS_I2C
  169. i2c_init_all();
  170. #else
  171. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  172. #endif
  173. puts("ready\n");
  174. return 0;
  175. }
  176. #endif
  177. #if defined(CONFIG_VID)
  178. __weak int init_func_vid(void)
  179. {
  180. return 0;
  181. }
  182. #endif
  183. #if defined(CONFIG_HARD_SPI)
  184. static int init_func_spi(void)
  185. {
  186. puts("SPI: ");
  187. spi_init();
  188. puts("ready\n");
  189. return 0;
  190. }
  191. #endif
  192. static int setup_mon_len(void)
  193. {
  194. #if defined(__ARM__) || defined(__MICROBLAZE__)
  195. gd->mon_len = (ulong)&__bss_end - (ulong)_start;
  196. #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
  197. gd->mon_len = (ulong)&_end - (ulong)_init;
  198. #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
  199. gd->mon_len = CONFIG_SYS_MONITOR_LEN;
  200. #elif defined(CONFIG_NDS32) || defined(CONFIG_SH) || defined(CONFIG_RISCV)
  201. gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
  202. #elif defined(CONFIG_SYS_MONITOR_BASE)
  203. /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
  204. gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  205. #endif
  206. return 0;
  207. }
  208. __weak int arch_cpu_init(void)
  209. {
  210. return 0;
  211. }
  212. __weak int mach_cpu_init(void)
  213. {
  214. return 0;
  215. }
  216. /* Get the top of usable RAM */
  217. __weak ulong board_get_usable_ram_top(ulong total_size)
  218. {
  219. #ifdef CONFIG_SYS_SDRAM_BASE
  220. /*
  221. * Detect whether we have so much RAM that it goes past the end of our
  222. * 32-bit address space. If so, clip the usable RAM so it doesn't.
  223. */
  224. if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
  225. /*
  226. * Will wrap back to top of 32-bit space when reservations
  227. * are made.
  228. */
  229. return 0;
  230. #endif
  231. return gd->ram_top;
  232. }
  233. static int setup_dest_addr(void)
  234. {
  235. debug("Monitor len: %08lX\n", gd->mon_len);
  236. /*
  237. * Ram is setup, size stored in gd !!
  238. */
  239. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  240. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  241. /*
  242. * Subtract specified amount of memory to hide so that it won't
  243. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  244. * the Linux kernel should now get passed the now "corrected"
  245. * memory size and won't touch it either. This should work
  246. * for arch/ppc and arch/powerpc. Only Linux board ports in
  247. * arch/powerpc with bootwrapper support, that recalculate the
  248. * memory size from the SDRAM controller setup will have to
  249. * get fixed.
  250. */
  251. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  252. #endif
  253. #ifdef CONFIG_SYS_SDRAM_BASE
  254. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  255. #endif
  256. gd->ram_top += get_effective_memsize();
  257. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  258. gd->relocaddr = gd->ram_top;
  259. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  260. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  261. /*
  262. * We need to make sure the location we intend to put secondary core
  263. * boot code is reserved and not used by any part of u-boot
  264. */
  265. if (gd->relocaddr > determine_mp_bootpg(NULL)) {
  266. gd->relocaddr = determine_mp_bootpg(NULL);
  267. debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
  268. }
  269. #endif
  270. return 0;
  271. }
  272. #ifdef CONFIG_PRAM
  273. /* reserve protected RAM */
  274. static int reserve_pram(void)
  275. {
  276. ulong reg;
  277. reg = env_get_ulong("pram", 10, CONFIG_PRAM);
  278. gd->relocaddr -= (reg << 10); /* size is in kB */
  279. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  280. gd->relocaddr);
  281. return 0;
  282. }
  283. #endif /* CONFIG_PRAM */
  284. /* Round memory pointer down to next 4 kB limit */
  285. static int reserve_round_4k(void)
  286. {
  287. gd->relocaddr &= ~(4096 - 1);
  288. return 0;
  289. }
  290. #ifdef CONFIG_ARM
  291. __weak int reserve_mmu(void)
  292. {
  293. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  294. /* reserve TLB table */
  295. gd->arch.tlb_size = PGTABLE_SIZE;
  296. gd->relocaddr -= gd->arch.tlb_size;
  297. /* round down to next 64 kB limit */
  298. gd->relocaddr &= ~(0x10000 - 1);
  299. gd->arch.tlb_addr = gd->relocaddr;
  300. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  301. gd->arch.tlb_addr + gd->arch.tlb_size);
  302. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  303. /*
  304. * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
  305. * with location within secure ram.
  306. */
  307. gd->arch.tlb_allocated = gd->arch.tlb_addr;
  308. #endif
  309. #endif
  310. return 0;
  311. }
  312. #endif
  313. static int reserve_video(void)
  314. {
  315. #ifdef CONFIG_DM_VIDEO
  316. ulong addr;
  317. int ret;
  318. addr = gd->relocaddr;
  319. ret = video_reserve(&addr);
  320. if (ret)
  321. return ret;
  322. gd->relocaddr = addr;
  323. #elif defined(CONFIG_LCD)
  324. # ifdef CONFIG_FB_ADDR
  325. gd->fb_base = CONFIG_FB_ADDR;
  326. # else
  327. /* reserve memory for LCD display (always full pages) */
  328. gd->relocaddr = lcd_setmem(gd->relocaddr);
  329. gd->fb_base = gd->relocaddr;
  330. # endif /* CONFIG_FB_ADDR */
  331. #elif defined(CONFIG_VIDEO) && \
  332. (!defined(CONFIG_PPC)) && \
  333. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  334. !defined(CONFIG_M68K)
  335. /* reserve memory for video display (always full pages) */
  336. gd->relocaddr = video_setmem(gd->relocaddr);
  337. gd->fb_base = gd->relocaddr;
  338. #endif
  339. return 0;
  340. }
  341. static int reserve_trace(void)
  342. {
  343. #ifdef CONFIG_TRACE
  344. gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
  345. gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
  346. debug("Reserving %dk for trace data at: %08lx\n",
  347. CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
  348. #endif
  349. return 0;
  350. }
  351. static int reserve_uboot(void)
  352. {
  353. /*
  354. * reserve memory for U-Boot code, data & bss
  355. * round down to next 4 kB limit
  356. */
  357. gd->relocaddr -= gd->mon_len;
  358. gd->relocaddr &= ~(4096 - 1);
  359. #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
  360. /* round down to next 64 kB limit so that IVPR stays aligned */
  361. gd->relocaddr &= ~(65536 - 1);
  362. #endif
  363. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  364. gd->relocaddr);
  365. gd->start_addr_sp = gd->relocaddr;
  366. return 0;
  367. }
  368. /* reserve memory for malloc() area */
  369. static int reserve_malloc(void)
  370. {
  371. gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
  372. debug("Reserving %dk for malloc() at: %08lx\n",
  373. TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  374. return 0;
  375. }
  376. /* (permanently) allocate a Board Info struct */
  377. static int reserve_board(void)
  378. {
  379. if (!gd->bd) {
  380. gd->start_addr_sp -= sizeof(bd_t);
  381. gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  382. memset(gd->bd, '\0', sizeof(bd_t));
  383. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  384. sizeof(bd_t), gd->start_addr_sp);
  385. }
  386. return 0;
  387. }
  388. static int setup_machine(void)
  389. {
  390. #ifdef CONFIG_MACH_TYPE
  391. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  392. #endif
  393. return 0;
  394. }
  395. static int reserve_global_data(void)
  396. {
  397. gd->start_addr_sp -= sizeof(gd_t);
  398. gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  399. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  400. sizeof(gd_t), gd->start_addr_sp);
  401. return 0;
  402. }
  403. static int reserve_fdt(void)
  404. {
  405. #ifndef CONFIG_OF_EMBED
  406. /*
  407. * If the device tree is sitting immediately above our image then we
  408. * must relocate it. If it is embedded in the data section, then it
  409. * will be relocated with other data.
  410. */
  411. if (gd->fdt_blob) {
  412. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  413. gd->start_addr_sp -= gd->fdt_size;
  414. gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  415. debug("Reserving %lu Bytes for FDT at: %08lx\n",
  416. gd->fdt_size, gd->start_addr_sp);
  417. }
  418. #endif
  419. return 0;
  420. }
  421. static int reserve_bootstage(void)
  422. {
  423. #ifdef CONFIG_BOOTSTAGE
  424. int size = bootstage_get_size();
  425. gd->start_addr_sp -= size;
  426. gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
  427. debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
  428. gd->start_addr_sp);
  429. #endif
  430. return 0;
  431. }
  432. __weak int arch_reserve_stacks(void)
  433. {
  434. return 0;
  435. }
  436. static int reserve_stacks(void)
  437. {
  438. /* make stack pointer 16-byte aligned */
  439. gd->start_addr_sp -= 16;
  440. gd->start_addr_sp &= ~0xf;
  441. /*
  442. * let the architecture-specific code tailor gd->start_addr_sp and
  443. * gd->irq_sp
  444. */
  445. return arch_reserve_stacks();
  446. }
  447. static int display_new_sp(void)
  448. {
  449. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  450. return 0;
  451. }
  452. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  453. defined(CONFIG_SH)
  454. static int setup_board_part1(void)
  455. {
  456. bd_t *bd = gd->bd;
  457. /*
  458. * Save local variables to board info struct
  459. */
  460. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  461. bd->bi_memsize = gd->ram_size; /* size in bytes */
  462. #ifdef CONFIG_SYS_SRAM_BASE
  463. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  464. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  465. #endif
  466. #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  467. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  468. #endif
  469. #if defined(CONFIG_M68K)
  470. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  471. #endif
  472. #if defined(CONFIG_MPC83xx)
  473. bd->bi_immrbar = CONFIG_SYS_IMMR;
  474. #endif
  475. return 0;
  476. }
  477. #endif
  478. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  479. static int setup_board_part2(void)
  480. {
  481. bd_t *bd = gd->bd;
  482. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  483. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  484. #if defined(CONFIG_CPM2)
  485. bd->bi_cpmfreq = gd->arch.cpm_clk;
  486. bd->bi_brgfreq = gd->arch.brg_clk;
  487. bd->bi_sccfreq = gd->arch.scc_clk;
  488. bd->bi_vco = gd->arch.vco_out;
  489. #endif /* CONFIG_CPM2 */
  490. #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
  491. bd->bi_pcifreq = gd->pci_clk;
  492. #endif
  493. #if defined(CONFIG_EXTRA_CLOCK)
  494. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  495. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  496. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  497. #endif
  498. return 0;
  499. }
  500. #endif
  501. #ifdef CONFIG_POST
  502. static int init_post(void)
  503. {
  504. post_bootmode_init();
  505. post_run(NULL, POST_ROM | post_bootmode_get(0));
  506. return 0;
  507. }
  508. #endif
  509. static int reloc_fdt(void)
  510. {
  511. #ifndef CONFIG_OF_EMBED
  512. if (gd->flags & GD_FLG_SKIP_RELOC)
  513. return 0;
  514. if (gd->new_fdt) {
  515. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  516. gd->fdt_blob = gd->new_fdt;
  517. }
  518. #endif
  519. return 0;
  520. }
  521. static int reloc_bootstage(void)
  522. {
  523. #ifdef CONFIG_BOOTSTAGE
  524. if (gd->flags & GD_FLG_SKIP_RELOC)
  525. return 0;
  526. if (gd->new_bootstage) {
  527. int size = bootstage_get_size();
  528. debug("Copying bootstage from %p to %p, size %x\n",
  529. gd->bootstage, gd->new_bootstage, size);
  530. memcpy(gd->new_bootstage, gd->bootstage, size);
  531. gd->bootstage = gd->new_bootstage;
  532. }
  533. #endif
  534. return 0;
  535. }
  536. static int setup_reloc(void)
  537. {
  538. if (gd->flags & GD_FLG_SKIP_RELOC) {
  539. debug("Skipping relocation due to flag\n");
  540. return 0;
  541. }
  542. #ifdef CONFIG_SYS_TEXT_BASE
  543. #ifdef ARM
  544. gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
  545. #elif defined(CONFIG_M68K)
  546. /*
  547. * On all ColdFire arch cpu, monitor code starts always
  548. * just after the default vector table location, so at 0x400
  549. */
  550. gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
  551. #else
  552. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  553. #endif
  554. #endif
  555. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  556. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  557. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  558. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  559. gd->start_addr_sp);
  560. return 0;
  561. }
  562. #ifdef CONFIG_OF_BOARD_FIXUP
  563. static int fix_fdt(void)
  564. {
  565. return board_fix_fdt((void *)gd->fdt_blob);
  566. }
  567. #endif
  568. /* ARM calls relocate_code from its crt0.S */
  569. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  570. !CONFIG_IS_ENABLED(X86_64)
  571. static int jump_to_copy(void)
  572. {
  573. if (gd->flags & GD_FLG_SKIP_RELOC)
  574. return 0;
  575. /*
  576. * x86 is special, but in a nice way. It uses a trampoline which
  577. * enables the dcache if possible.
  578. *
  579. * For now, other archs use relocate_code(), which is implemented
  580. * similarly for all archs. When we do generic relocation, hopefully
  581. * we can make all archs enable the dcache prior to relocation.
  582. */
  583. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  584. /*
  585. * SDRAM and console are now initialised. The final stack can now
  586. * be setup in SDRAM. Code execution will continue in Flash, but
  587. * with the stack in SDRAM and Global Data in temporary memory
  588. * (CPU cache)
  589. */
  590. arch_setup_gd(gd->new_gd);
  591. board_init_f_r_trampoline(gd->start_addr_sp);
  592. #else
  593. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  594. #endif
  595. return 0;
  596. }
  597. #endif
  598. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  599. static int initf_bootstage(void)
  600. {
  601. bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
  602. IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
  603. int ret;
  604. ret = bootstage_init(!from_spl);
  605. if (ret)
  606. return ret;
  607. if (from_spl) {
  608. const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
  609. CONFIG_BOOTSTAGE_STASH_SIZE);
  610. ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
  611. if (ret && ret != -ENOENT) {
  612. debug("Failed to unstash bootstage: err=%d\n", ret);
  613. return ret;
  614. }
  615. }
  616. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  617. return 0;
  618. }
  619. static int initf_console_record(void)
  620. {
  621. #if defined(CONFIG_CONSOLE_RECORD) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  622. return console_record_init();
  623. #else
  624. return 0;
  625. #endif
  626. }
  627. static int initf_dm(void)
  628. {
  629. #if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  630. int ret;
  631. bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
  632. ret = dm_init_and_scan(true);
  633. bootstage_accum(BOOTSTATE_ID_ACCUM_DM_F);
  634. if (ret)
  635. return ret;
  636. #endif
  637. #ifdef CONFIG_TIMER_EARLY
  638. ret = dm_timer_init();
  639. if (ret)
  640. return ret;
  641. #endif
  642. return 0;
  643. }
  644. /* Architecture-specific memory reservation */
  645. __weak int reserve_arch(void)
  646. {
  647. return 0;
  648. }
  649. __weak int arch_cpu_init_dm(void)
  650. {
  651. return 0;
  652. }
  653. static const init_fnc_t init_sequence_f[] = {
  654. setup_mon_len,
  655. #ifdef CONFIG_OF_CONTROL
  656. fdtdec_setup,
  657. #endif
  658. #ifdef CONFIG_TRACE
  659. trace_early_init,
  660. #endif
  661. initf_malloc,
  662. log_init,
  663. initf_bootstage, /* uses its own timer, so does not need DM */
  664. initf_console_record,
  665. #if defined(CONFIG_HAVE_FSP)
  666. arch_fsp_init,
  667. #endif
  668. arch_cpu_init, /* basic arch cpu dependent setup */
  669. mach_cpu_init, /* SoC/machine dependent CPU setup */
  670. initf_dm,
  671. arch_cpu_init_dm,
  672. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  673. board_early_init_f,
  674. #endif
  675. #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
  676. /* get CPU and bus clocks according to the environment variable */
  677. get_clocks, /* get CPU and bus clocks (etc.) */
  678. #endif
  679. #if !defined(CONFIG_M68K)
  680. timer_init, /* initialize timer */
  681. #endif
  682. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  683. board_postclk_init,
  684. #endif
  685. env_init, /* initialize environment */
  686. init_baud_rate, /* initialze baudrate settings */
  687. serial_init, /* serial communications setup */
  688. console_init_f, /* stage 1 init of console */
  689. display_options, /* say that we are here */
  690. display_text_info, /* show debugging info if required */
  691. #if defined(CONFIG_PPC) || defined(CONFIG_SH) || defined(CONFIG_X86)
  692. checkcpu,
  693. #endif
  694. #if defined(CONFIG_DISPLAY_CPUINFO)
  695. print_cpuinfo, /* display cpu info (and speed) */
  696. #endif
  697. #if defined(CONFIG_DTB_RESELECT)
  698. embedded_dtb_select,
  699. #endif
  700. #if defined(CONFIG_DISPLAY_BOARDINFO)
  701. show_board_info,
  702. #endif
  703. INIT_FUNC_WATCHDOG_INIT
  704. #if defined(CONFIG_MISC_INIT_F)
  705. misc_init_f,
  706. #endif
  707. INIT_FUNC_WATCHDOG_RESET
  708. #if defined(CONFIG_SYS_I2C)
  709. init_func_i2c,
  710. #endif
  711. #if defined(CONFIG_VID) && !defined(CONFIG_SPL)
  712. init_func_vid,
  713. #endif
  714. #if defined(CONFIG_HARD_SPI)
  715. init_func_spi,
  716. #endif
  717. announce_dram_init,
  718. dram_init, /* configure available RAM banks */
  719. #ifdef CONFIG_POST
  720. post_init_f,
  721. #endif
  722. INIT_FUNC_WATCHDOG_RESET
  723. #if defined(CONFIG_SYS_DRAM_TEST)
  724. testdram,
  725. #endif /* CONFIG_SYS_DRAM_TEST */
  726. INIT_FUNC_WATCHDOG_RESET
  727. #ifdef CONFIG_POST
  728. init_post,
  729. #endif
  730. INIT_FUNC_WATCHDOG_RESET
  731. /*
  732. * Now that we have DRAM mapped and working, we can
  733. * relocate the code and continue running from DRAM.
  734. *
  735. * Reserve memory at end of RAM for (top down in that order):
  736. * - area that won't get touched by U-Boot and Linux (optional)
  737. * - kernel log buffer
  738. * - protected RAM
  739. * - LCD framebuffer
  740. * - monitor code
  741. * - board info struct
  742. */
  743. setup_dest_addr,
  744. #ifdef CONFIG_PRAM
  745. reserve_pram,
  746. #endif
  747. reserve_round_4k,
  748. #ifdef CONFIG_ARM
  749. reserve_mmu,
  750. #endif
  751. reserve_video,
  752. reserve_trace,
  753. reserve_uboot,
  754. reserve_malloc,
  755. reserve_board,
  756. setup_machine,
  757. reserve_global_data,
  758. reserve_fdt,
  759. reserve_bootstage,
  760. reserve_arch,
  761. reserve_stacks,
  762. dram_init_banksize,
  763. show_dram_config,
  764. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  765. defined(CONFIG_SH)
  766. setup_board_part1,
  767. #endif
  768. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  769. INIT_FUNC_WATCHDOG_RESET
  770. setup_board_part2,
  771. #endif
  772. display_new_sp,
  773. #ifdef CONFIG_OF_BOARD_FIXUP
  774. fix_fdt,
  775. #endif
  776. INIT_FUNC_WATCHDOG_RESET
  777. reloc_fdt,
  778. reloc_bootstage,
  779. setup_reloc,
  780. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  781. copy_uboot_to_ram,
  782. do_elf_reloc_fixups,
  783. clear_bss,
  784. #endif
  785. #if defined(CONFIG_XTENSA)
  786. clear_bss,
  787. #endif
  788. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  789. !CONFIG_IS_ENABLED(X86_64)
  790. jump_to_copy,
  791. #endif
  792. NULL,
  793. };
  794. void board_init_f(ulong boot_flags)
  795. {
  796. gd->flags = boot_flags;
  797. gd->have_console = 0;
  798. if (initcall_run_list(init_sequence_f))
  799. hang();
  800. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  801. !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
  802. !defined(CONFIG_ARC)
  803. /* NOTREACHED - jump_to_copy() does not return */
  804. hang();
  805. #endif
  806. }
  807. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  808. /*
  809. * For now this code is only used on x86.
  810. *
  811. * init_sequence_f_r is the list of init functions which are run when
  812. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  813. * The following limitations must be considered when implementing an
  814. * '_f_r' function:
  815. * - 'static' variables are read-only
  816. * - Global Data (gd->xxx) is read/write
  817. *
  818. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  819. * supported). It _should_, if possible, copy global data to RAM and
  820. * initialise the CPU caches (to speed up the relocation process)
  821. *
  822. * NOTE: At present only x86 uses this route, but it is intended that
  823. * all archs will move to this when generic relocation is implemented.
  824. */
  825. static const init_fnc_t init_sequence_f_r[] = {
  826. #if !CONFIG_IS_ENABLED(X86_64)
  827. init_cache_f_r,
  828. #endif
  829. NULL,
  830. };
  831. void board_init_f_r(void)
  832. {
  833. if (initcall_run_list(init_sequence_f_r))
  834. hang();
  835. /*
  836. * The pre-relocation drivers may be using memory that has now gone
  837. * away. Mark serial as unavailable - this will fall back to the debug
  838. * UART if available.
  839. *
  840. * Do the same with log drivers since the memory may not be available.
  841. */
  842. gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
  843. #ifdef CONFIG_TIMER
  844. gd->timer = NULL;
  845. #endif
  846. /*
  847. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  848. * Transfer execution from Flash to RAM by calculating the address
  849. * of the in-RAM copy of board_init_r() and calling it
  850. */
  851. (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
  852. /* NOTREACHED - board_init_r() does not return */
  853. hang();
  854. }
  855. #endif /* CONFIG_X86 */