board_f.c 23 KB

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