board_f.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 <cpu.h>
  14. #include <dm.h>
  15. #include <environment.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 <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) || \
  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. #if defined(CONFIG_DISPLAY_CPUINFO) && CONFIG_IS_ENABLED(CPU)
  149. static int print_cpuinfo(void)
  150. {
  151. struct udevice *dev;
  152. char desc[512];
  153. int ret;
  154. ret = uclass_first_device_err(UCLASS_CPU, &dev);
  155. if (ret) {
  156. debug("%s: Could not get CPU device (err = %d)\n",
  157. __func__, ret);
  158. return ret;
  159. }
  160. ret = cpu_get_desc(dev, desc, sizeof(desc));
  161. if (ret) {
  162. debug("%s: Could not get CPU description (err = %d)\n",
  163. dev->name, ret);
  164. return ret;
  165. }
  166. printf("CPU: %s\n", desc);
  167. return 0;
  168. }
  169. #endif
  170. static int announce_dram_init(void)
  171. {
  172. puts("DRAM: ");
  173. return 0;
  174. }
  175. static int show_dram_config(void)
  176. {
  177. unsigned long long size;
  178. #ifdef CONFIG_NR_DRAM_BANKS
  179. int i;
  180. debug("\nRAM Configuration:\n");
  181. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  182. size += gd->bd->bi_dram[i].size;
  183. debug("Bank #%d: %llx ", i,
  184. (unsigned long long)(gd->bd->bi_dram[i].start));
  185. #ifdef DEBUG
  186. print_size(gd->bd->bi_dram[i].size, "\n");
  187. #endif
  188. }
  189. debug("\nDRAM: ");
  190. #else
  191. size = gd->ram_size;
  192. #endif
  193. print_size(size, "");
  194. board_add_ram_info(0);
  195. putc('\n');
  196. return 0;
  197. }
  198. __weak int dram_init_banksize(void)
  199. {
  200. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  201. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  202. gd->bd->bi_dram[0].size = get_effective_memsize();
  203. #endif
  204. return 0;
  205. }
  206. #if defined(CONFIG_SYS_I2C)
  207. static int init_func_i2c(void)
  208. {
  209. puts("I2C: ");
  210. #ifdef CONFIG_SYS_I2C
  211. i2c_init_all();
  212. #else
  213. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  214. #endif
  215. puts("ready\n");
  216. return 0;
  217. }
  218. #endif
  219. #if defined(CONFIG_VID)
  220. __weak int init_func_vid(void)
  221. {
  222. return 0;
  223. }
  224. #endif
  225. #if defined(CONFIG_HARD_SPI)
  226. static int init_func_spi(void)
  227. {
  228. puts("SPI: ");
  229. puts("ready\n");
  230. return 0;
  231. }
  232. #endif
  233. static int setup_mon_len(void)
  234. {
  235. #if defined(__ARM__) || defined(__MICROBLAZE__)
  236. gd->mon_len = (ulong)&__bss_end - (ulong)_start;
  237. #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
  238. gd->mon_len = (ulong)&_end - (ulong)_init;
  239. #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
  240. gd->mon_len = CONFIG_SYS_MONITOR_LEN;
  241. #elif defined(CONFIG_NDS32) || defined(CONFIG_SH) || defined(CONFIG_RISCV)
  242. gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
  243. #elif defined(CONFIG_SYS_MONITOR_BASE)
  244. /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
  245. gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  246. #endif
  247. return 0;
  248. }
  249. __weak int arch_cpu_init(void)
  250. {
  251. return 0;
  252. }
  253. __weak int mach_cpu_init(void)
  254. {
  255. return 0;
  256. }
  257. /* Get the top of usable RAM */
  258. __weak ulong board_get_usable_ram_top(ulong total_size)
  259. {
  260. #ifdef CONFIG_SYS_SDRAM_BASE
  261. /*
  262. * Detect whether we have so much RAM that it goes past the end of our
  263. * 32-bit address space. If so, clip the usable RAM so it doesn't.
  264. */
  265. if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
  266. /*
  267. * Will wrap back to top of 32-bit space when reservations
  268. * are made.
  269. */
  270. return 0;
  271. #endif
  272. return gd->ram_top;
  273. }
  274. static int setup_dest_addr(void)
  275. {
  276. debug("Monitor len: %08lX\n", gd->mon_len);
  277. /*
  278. * Ram is setup, size stored in gd !!
  279. */
  280. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  281. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  282. /*
  283. * Subtract specified amount of memory to hide so that it won't
  284. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  285. * the Linux kernel should now get passed the now "corrected"
  286. * memory size and won't touch it either. This should work
  287. * for arch/ppc and arch/powerpc. Only Linux board ports in
  288. * arch/powerpc with bootwrapper support, that recalculate the
  289. * memory size from the SDRAM controller setup will have to
  290. * get fixed.
  291. */
  292. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  293. #endif
  294. #ifdef CONFIG_SYS_SDRAM_BASE
  295. gd->ram_base = CONFIG_SYS_SDRAM_BASE;
  296. #endif
  297. gd->ram_top = gd->ram_base + get_effective_memsize();
  298. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  299. gd->relocaddr = gd->ram_top;
  300. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  301. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  302. /*
  303. * We need to make sure the location we intend to put secondary core
  304. * boot code is reserved and not used by any part of u-boot
  305. */
  306. if (gd->relocaddr > determine_mp_bootpg(NULL)) {
  307. gd->relocaddr = determine_mp_bootpg(NULL);
  308. debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
  309. }
  310. #endif
  311. return 0;
  312. }
  313. #ifdef CONFIG_PRAM
  314. /* reserve protected RAM */
  315. static int reserve_pram(void)
  316. {
  317. ulong reg;
  318. reg = env_get_ulong("pram", 10, CONFIG_PRAM);
  319. gd->relocaddr -= (reg << 10); /* size is in kB */
  320. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  321. gd->relocaddr);
  322. return 0;
  323. }
  324. #endif /* CONFIG_PRAM */
  325. /* Round memory pointer down to next 4 kB limit */
  326. static int reserve_round_4k(void)
  327. {
  328. gd->relocaddr &= ~(4096 - 1);
  329. return 0;
  330. }
  331. #ifdef CONFIG_ARM
  332. __weak int reserve_mmu(void)
  333. {
  334. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  335. /* reserve TLB table */
  336. gd->arch.tlb_size = PGTABLE_SIZE;
  337. gd->relocaddr -= gd->arch.tlb_size;
  338. /* round down to next 64 kB limit */
  339. gd->relocaddr &= ~(0x10000 - 1);
  340. gd->arch.tlb_addr = gd->relocaddr;
  341. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  342. gd->arch.tlb_addr + gd->arch.tlb_size);
  343. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  344. /*
  345. * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
  346. * with location within secure ram.
  347. */
  348. gd->arch.tlb_allocated = gd->arch.tlb_addr;
  349. #endif
  350. #endif
  351. return 0;
  352. }
  353. #endif
  354. static int reserve_video(void)
  355. {
  356. #ifdef CONFIG_DM_VIDEO
  357. ulong addr;
  358. int ret;
  359. addr = gd->relocaddr;
  360. ret = video_reserve(&addr);
  361. if (ret)
  362. return ret;
  363. gd->relocaddr = addr;
  364. #elif defined(CONFIG_LCD)
  365. # ifdef CONFIG_FB_ADDR
  366. gd->fb_base = CONFIG_FB_ADDR;
  367. # else
  368. /* reserve memory for LCD display (always full pages) */
  369. gd->relocaddr = lcd_setmem(gd->relocaddr);
  370. gd->fb_base = gd->relocaddr;
  371. # endif /* CONFIG_FB_ADDR */
  372. #elif defined(CONFIG_VIDEO) && \
  373. (!defined(CONFIG_PPC)) && \
  374. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  375. !defined(CONFIG_M68K)
  376. /* reserve memory for video display (always full pages) */
  377. gd->relocaddr = video_setmem(gd->relocaddr);
  378. gd->fb_base = gd->relocaddr;
  379. #endif
  380. return 0;
  381. }
  382. static int reserve_trace(void)
  383. {
  384. #ifdef CONFIG_TRACE
  385. gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
  386. gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
  387. debug("Reserving %dk for trace data at: %08lx\n",
  388. CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
  389. #endif
  390. return 0;
  391. }
  392. static int reserve_uboot(void)
  393. {
  394. if (!(gd->flags & GD_FLG_SKIP_RELOC)) {
  395. /*
  396. * reserve memory for U-Boot code, data & bss
  397. * round down to next 4 kB limit
  398. */
  399. gd->relocaddr -= gd->mon_len;
  400. gd->relocaddr &= ~(4096 - 1);
  401. #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
  402. /* round down to next 64 kB limit so that IVPR stays aligned */
  403. gd->relocaddr &= ~(65536 - 1);
  404. #endif
  405. debug("Reserving %ldk for U-Boot at: %08lx\n",
  406. gd->mon_len >> 10, gd->relocaddr);
  407. }
  408. gd->start_addr_sp = gd->relocaddr;
  409. return 0;
  410. }
  411. /* reserve memory for malloc() area */
  412. static int reserve_malloc(void)
  413. {
  414. gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
  415. debug("Reserving %dk for malloc() at: %08lx\n",
  416. TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  417. return 0;
  418. }
  419. /* (permanently) allocate a Board Info struct */
  420. static int reserve_board(void)
  421. {
  422. if (!gd->bd) {
  423. gd->start_addr_sp -= sizeof(bd_t);
  424. gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  425. memset(gd->bd, '\0', sizeof(bd_t));
  426. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  427. sizeof(bd_t), gd->start_addr_sp);
  428. }
  429. return 0;
  430. }
  431. static int setup_machine(void)
  432. {
  433. #ifdef CONFIG_MACH_TYPE
  434. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  435. #endif
  436. return 0;
  437. }
  438. static int reserve_global_data(void)
  439. {
  440. gd->start_addr_sp -= sizeof(gd_t);
  441. gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  442. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  443. sizeof(gd_t), gd->start_addr_sp);
  444. return 0;
  445. }
  446. static int reserve_fdt(void)
  447. {
  448. #ifndef CONFIG_OF_EMBED
  449. /*
  450. * If the device tree is sitting immediately above our image then we
  451. * must relocate it. If it is embedded in the data section, then it
  452. * will be relocated with other data.
  453. */
  454. if (gd->fdt_blob) {
  455. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  456. gd->start_addr_sp -= gd->fdt_size;
  457. gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  458. debug("Reserving %lu Bytes for FDT at: %08lx\n",
  459. gd->fdt_size, gd->start_addr_sp);
  460. }
  461. #endif
  462. return 0;
  463. }
  464. static int reserve_bootstage(void)
  465. {
  466. #ifdef CONFIG_BOOTSTAGE
  467. int size = bootstage_get_size();
  468. gd->start_addr_sp -= size;
  469. gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
  470. debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
  471. gd->start_addr_sp);
  472. #endif
  473. return 0;
  474. }
  475. __weak int arch_reserve_stacks(void)
  476. {
  477. return 0;
  478. }
  479. static int reserve_stacks(void)
  480. {
  481. /* make stack pointer 16-byte aligned */
  482. gd->start_addr_sp -= 16;
  483. gd->start_addr_sp &= ~0xf;
  484. /*
  485. * let the architecture-specific code tailor gd->start_addr_sp and
  486. * gd->irq_sp
  487. */
  488. return arch_reserve_stacks();
  489. }
  490. static int display_new_sp(void)
  491. {
  492. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  493. return 0;
  494. }
  495. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  496. defined(CONFIG_SH)
  497. static int setup_board_part1(void)
  498. {
  499. bd_t *bd = gd->bd;
  500. /*
  501. * Save local variables to board info struct
  502. */
  503. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  504. bd->bi_memsize = gd->ram_size; /* size in bytes */
  505. #ifdef CONFIG_SYS_SRAM_BASE
  506. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  507. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  508. #endif
  509. #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  510. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  511. #endif
  512. #if defined(CONFIG_M68K)
  513. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  514. #endif
  515. #if defined(CONFIG_MPC83xx)
  516. bd->bi_immrbar = CONFIG_SYS_IMMR;
  517. #endif
  518. return 0;
  519. }
  520. #endif
  521. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  522. static int setup_board_part2(void)
  523. {
  524. bd_t *bd = gd->bd;
  525. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  526. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  527. #if defined(CONFIG_CPM2)
  528. bd->bi_cpmfreq = gd->arch.cpm_clk;
  529. bd->bi_brgfreq = gd->arch.brg_clk;
  530. bd->bi_sccfreq = gd->arch.scc_clk;
  531. bd->bi_vco = gd->arch.vco_out;
  532. #endif /* CONFIG_CPM2 */
  533. #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
  534. bd->bi_pcifreq = gd->pci_clk;
  535. #endif
  536. #if defined(CONFIG_EXTRA_CLOCK)
  537. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  538. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  539. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  540. #endif
  541. return 0;
  542. }
  543. #endif
  544. #ifdef CONFIG_POST
  545. static int init_post(void)
  546. {
  547. post_bootmode_init();
  548. post_run(NULL, POST_ROM | post_bootmode_get(0));
  549. return 0;
  550. }
  551. #endif
  552. static int reloc_fdt(void)
  553. {
  554. #ifndef CONFIG_OF_EMBED
  555. if (gd->flags & GD_FLG_SKIP_RELOC)
  556. return 0;
  557. if (gd->new_fdt) {
  558. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  559. gd->fdt_blob = gd->new_fdt;
  560. }
  561. #endif
  562. return 0;
  563. }
  564. static int reloc_bootstage(void)
  565. {
  566. #ifdef CONFIG_BOOTSTAGE
  567. if (gd->flags & GD_FLG_SKIP_RELOC)
  568. return 0;
  569. if (gd->new_bootstage) {
  570. int size = bootstage_get_size();
  571. debug("Copying bootstage from %p to %p, size %x\n",
  572. gd->bootstage, gd->new_bootstage, size);
  573. memcpy(gd->new_bootstage, gd->bootstage, size);
  574. gd->bootstage = gd->new_bootstage;
  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. #else
  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(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
  675. ret = dm_init_and_scan(true);
  676. bootstage_accum(BOOTSTATE_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. static const init_fnc_t init_sequence_f[] = {
  697. setup_mon_len,
  698. #ifdef CONFIG_OF_CONTROL
  699. fdtdec_setup,
  700. #endif
  701. #ifdef CONFIG_TRACE
  702. trace_early_init,
  703. #endif
  704. initf_malloc,
  705. log_init,
  706. initf_bootstage, /* uses its own timer, so does not need DM */
  707. initf_console_record,
  708. #if defined(CONFIG_HAVE_FSP)
  709. arch_fsp_init,
  710. #endif
  711. arch_cpu_init, /* basic arch cpu dependent setup */
  712. mach_cpu_init, /* SoC/machine dependent CPU setup */
  713. initf_dm,
  714. arch_cpu_init_dm,
  715. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  716. board_early_init_f,
  717. #endif
  718. #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
  719. /* get CPU and bus clocks according to the environment variable */
  720. get_clocks, /* get CPU and bus clocks (etc.) */
  721. #endif
  722. #if !defined(CONFIG_M68K)
  723. timer_init, /* initialize timer */
  724. #endif
  725. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  726. board_postclk_init,
  727. #endif
  728. env_init, /* initialize environment */
  729. init_baud_rate, /* initialze baudrate settings */
  730. serial_init, /* serial communications setup */
  731. console_init_f, /* stage 1 init of console */
  732. display_options, /* say that we are here */
  733. display_text_info, /* show debugging info if required */
  734. #if defined(CONFIG_PPC) || defined(CONFIG_SH) || defined(CONFIG_X86)
  735. checkcpu,
  736. #endif
  737. #if defined(CONFIG_SYSRESET)
  738. print_resetinfo,
  739. #endif
  740. #if defined(CONFIG_DISPLAY_CPUINFO)
  741. print_cpuinfo, /* display cpu info (and speed) */
  742. #endif
  743. #if defined(CONFIG_DTB_RESELECT)
  744. embedded_dtb_select,
  745. #endif
  746. #if defined(CONFIG_DISPLAY_BOARDINFO)
  747. show_board_info,
  748. #endif
  749. INIT_FUNC_WATCHDOG_INIT
  750. #if defined(CONFIG_MISC_INIT_F)
  751. misc_init_f,
  752. #endif
  753. INIT_FUNC_WATCHDOG_RESET
  754. #if defined(CONFIG_SYS_I2C)
  755. init_func_i2c,
  756. #endif
  757. #if defined(CONFIG_VID) && !defined(CONFIG_SPL)
  758. init_func_vid,
  759. #endif
  760. #if defined(CONFIG_HARD_SPI)
  761. init_func_spi,
  762. #endif
  763. announce_dram_init,
  764. dram_init, /* configure available RAM banks */
  765. #ifdef CONFIG_POST
  766. post_init_f,
  767. #endif
  768. INIT_FUNC_WATCHDOG_RESET
  769. #if defined(CONFIG_SYS_DRAM_TEST)
  770. testdram,
  771. #endif /* CONFIG_SYS_DRAM_TEST */
  772. INIT_FUNC_WATCHDOG_RESET
  773. #ifdef CONFIG_POST
  774. init_post,
  775. #endif
  776. INIT_FUNC_WATCHDOG_RESET
  777. /*
  778. * Now that we have DRAM mapped and working, we can
  779. * relocate the code and continue running from DRAM.
  780. *
  781. * Reserve memory at end of RAM for (top down in that order):
  782. * - area that won't get touched by U-Boot and Linux (optional)
  783. * - kernel log buffer
  784. * - protected RAM
  785. * - LCD framebuffer
  786. * - monitor code
  787. * - board info struct
  788. */
  789. setup_dest_addr,
  790. #ifdef CONFIG_PRAM
  791. reserve_pram,
  792. #endif
  793. reserve_round_4k,
  794. #ifdef CONFIG_ARM
  795. reserve_mmu,
  796. #endif
  797. reserve_video,
  798. reserve_trace,
  799. reserve_uboot,
  800. reserve_malloc,
  801. reserve_board,
  802. setup_machine,
  803. reserve_global_data,
  804. reserve_fdt,
  805. reserve_bootstage,
  806. reserve_arch,
  807. reserve_stacks,
  808. dram_init_banksize,
  809. show_dram_config,
  810. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  811. defined(CONFIG_SH)
  812. setup_board_part1,
  813. #endif
  814. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  815. INIT_FUNC_WATCHDOG_RESET
  816. setup_board_part2,
  817. #endif
  818. display_new_sp,
  819. #ifdef CONFIG_OF_BOARD_FIXUP
  820. fix_fdt,
  821. #endif
  822. INIT_FUNC_WATCHDOG_RESET
  823. reloc_fdt,
  824. reloc_bootstage,
  825. setup_reloc,
  826. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  827. copy_uboot_to_ram,
  828. do_elf_reloc_fixups,
  829. clear_bss,
  830. #endif
  831. #if defined(CONFIG_XTENSA)
  832. clear_bss,
  833. #endif
  834. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  835. !CONFIG_IS_ENABLED(X86_64)
  836. jump_to_copy,
  837. #endif
  838. NULL,
  839. };
  840. void board_init_f(ulong boot_flags)
  841. {
  842. gd->flags = boot_flags;
  843. gd->have_console = 0;
  844. if (initcall_run_list(init_sequence_f))
  845. hang();
  846. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  847. !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64) && \
  848. !defined(CONFIG_ARC)
  849. /* NOTREACHED - jump_to_copy() does not return */
  850. hang();
  851. #endif
  852. }
  853. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  854. /*
  855. * For now this code is only used on x86.
  856. *
  857. * init_sequence_f_r is the list of init functions which are run when
  858. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  859. * The following limitations must be considered when implementing an
  860. * '_f_r' function:
  861. * - 'static' variables are read-only
  862. * - Global Data (gd->xxx) is read/write
  863. *
  864. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  865. * supported). It _should_, if possible, copy global data to RAM and
  866. * initialise the CPU caches (to speed up the relocation process)
  867. *
  868. * NOTE: At present only x86 uses this route, but it is intended that
  869. * all archs will move to this when generic relocation is implemented.
  870. */
  871. static const init_fnc_t init_sequence_f_r[] = {
  872. #if !CONFIG_IS_ENABLED(X86_64)
  873. init_cache_f_r,
  874. #endif
  875. NULL,
  876. };
  877. void board_init_f_r(void)
  878. {
  879. if (initcall_run_list(init_sequence_f_r))
  880. hang();
  881. /*
  882. * The pre-relocation drivers may be using memory that has now gone
  883. * away. Mark serial as unavailable - this will fall back to the debug
  884. * UART if available.
  885. *
  886. * Do the same with log drivers since the memory may not be available.
  887. */
  888. gd->flags &= ~(GD_FLG_SERIAL_READY | GD_FLG_LOG_READY);
  889. #ifdef CONFIG_TIMER
  890. gd->timer = NULL;
  891. #endif
  892. /*
  893. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  894. * Transfer execution from Flash to RAM by calculating the address
  895. * of the in-RAM copy of board_init_r() and calling it
  896. */
  897. (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
  898. /* NOTREACHED - board_init_r() does not return */
  899. hang();
  900. }
  901. #endif /* CONFIG_X86 */