board_f.c 23 KB

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