board_f.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2002-2006
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <linux/compiler.h>
  14. #include <version.h>
  15. #include <environment.h>
  16. #include <dm.h>
  17. #include <fdtdec.h>
  18. #include <fs.h>
  19. #if defined(CONFIG_CMD_IDE)
  20. #include <ide.h>
  21. #endif
  22. #include <i2c.h>
  23. #include <initcall.h>
  24. #include <logbuff.h>
  25. /* TODO: Can we move these into arch/ headers? */
  26. #ifdef CONFIG_8xx
  27. #include <mpc8xx.h>
  28. #endif
  29. #ifdef CONFIG_5xx
  30. #include <mpc5xx.h>
  31. #endif
  32. #ifdef CONFIG_MPC5xxx
  33. #include <mpc5xxx.h>
  34. #endif
  35. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  36. #include <asm/mp.h>
  37. #endif
  38. #include <os.h>
  39. #include <post.h>
  40. #include <spi.h>
  41. #include <status_led.h>
  42. #include <trace.h>
  43. #include <watchdog.h>
  44. #include <asm/errno.h>
  45. #include <asm/io.h>
  46. #include <asm/sections.h>
  47. #ifdef CONFIG_X86
  48. #include <asm/init_helpers.h>
  49. #include <asm/relocate.h>
  50. #endif
  51. #ifdef CONFIG_SANDBOX
  52. #include <asm/state.h>
  53. #endif
  54. #include <dm/root.h>
  55. #include <linux/compiler.h>
  56. /*
  57. * Pointer to initial global data area
  58. *
  59. * Here we initialize it if needed.
  60. */
  61. #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
  62. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  63. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  64. DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
  65. #else
  66. DECLARE_GLOBAL_DATA_PTR;
  67. #endif
  68. /*
  69. * sjg: IMO this code should be
  70. * refactored to a single function, something like:
  71. *
  72. * void led_set_state(enum led_colour_t colour, int on);
  73. */
  74. /************************************************************************
  75. * Coloured LED functionality
  76. ************************************************************************
  77. * May be supplied by boards if desired
  78. */
  79. __weak void coloured_LED_init(void) {}
  80. __weak void red_led_on(void) {}
  81. __weak void red_led_off(void) {}
  82. __weak void green_led_on(void) {}
  83. __weak void green_led_off(void) {}
  84. __weak void yellow_led_on(void) {}
  85. __weak void yellow_led_off(void) {}
  86. __weak void blue_led_on(void) {}
  87. __weak void blue_led_off(void) {}
  88. /*
  89. * Why is gd allocated a register? Prior to reloc it might be better to
  90. * just pass it around to each function in this file?
  91. *
  92. * After reloc one could argue that it is hardly used and doesn't need
  93. * to be in a register. Or if it is it should perhaps hold pointers to all
  94. * global data for all modules, so that post-reloc we can avoid the massive
  95. * literal pool we get on ARM. Or perhaps just encourage each module to use
  96. * a structure...
  97. */
  98. /*
  99. * Could the CONFIG_SPL_BUILD infection become a flag in gd?
  100. */
  101. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
  102. static int init_func_watchdog_init(void)
  103. {
  104. # if defined(CONFIG_HW_WATCHDOG) && (defined(CONFIG_BLACKFIN) || \
  105. defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
  106. defined(CONFIG_SH))
  107. hw_watchdog_init();
  108. # endif
  109. puts(" Watchdog enabled\n");
  110. WATCHDOG_RESET();
  111. return 0;
  112. }
  113. int init_func_watchdog_reset(void)
  114. {
  115. WATCHDOG_RESET();
  116. return 0;
  117. }
  118. #endif /* CONFIG_WATCHDOG */
  119. __weak void board_add_ram_info(int use_default)
  120. {
  121. /* please define platform specific board_add_ram_info() */
  122. }
  123. static int init_baud_rate(void)
  124. {
  125. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  126. return 0;
  127. }
  128. static int display_text_info(void)
  129. {
  130. #ifndef CONFIG_SANDBOX
  131. ulong bss_start, bss_end;
  132. bss_start = (ulong)&__bss_start;
  133. bss_end = (ulong)&__bss_end;
  134. debug("U-Boot code: %08X -> %08lX BSS: -> %08lX\n",
  135. #ifdef CONFIG_SYS_TEXT_BASE
  136. CONFIG_SYS_TEXT_BASE, bss_start, bss_end);
  137. #else
  138. CONFIG_SYS_MONITOR_BASE, bss_start, bss_end);
  139. #endif
  140. #endif
  141. #ifdef CONFIG_MODEM_SUPPORT
  142. debug("Modem Support enabled\n");
  143. #endif
  144. #ifdef CONFIG_USE_IRQ
  145. debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
  146. debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
  147. #endif
  148. return 0;
  149. }
  150. static int announce_dram_init(void)
  151. {
  152. puts("DRAM: ");
  153. return 0;
  154. }
  155. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC)
  156. static int init_func_ram(void)
  157. {
  158. #ifdef CONFIG_BOARD_TYPES
  159. int board_type = gd->board_type;
  160. #else
  161. int board_type = 0; /* use dummy arg */
  162. #endif
  163. gd->ram_size = initdram(board_type);
  164. if (gd->ram_size > 0)
  165. return 0;
  166. puts("*** failed ***\n");
  167. return 1;
  168. }
  169. #endif
  170. static int show_dram_config(void)
  171. {
  172. unsigned long long size;
  173. #ifdef CONFIG_NR_DRAM_BANKS
  174. int i;
  175. debug("\nRAM Configuration:\n");
  176. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  177. size += gd->bd->bi_dram[i].size;
  178. debug("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  179. #ifdef DEBUG
  180. print_size(gd->bd->bi_dram[i].size, "\n");
  181. #endif
  182. }
  183. debug("\nDRAM: ");
  184. #else
  185. size = gd->ram_size;
  186. #endif
  187. print_size(size, "");
  188. board_add_ram_info(0);
  189. putc('\n');
  190. return 0;
  191. }
  192. __weak void dram_init_banksize(void)
  193. {
  194. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  195. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  196. gd->bd->bi_dram[0].size = get_effective_memsize();
  197. #endif
  198. }
  199. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  200. static int init_func_i2c(void)
  201. {
  202. puts("I2C: ");
  203. #ifdef CONFIG_SYS_I2C
  204. i2c_init_all();
  205. #else
  206. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  207. #endif
  208. puts("ready\n");
  209. return 0;
  210. }
  211. #endif
  212. #if defined(CONFIG_HARD_SPI)
  213. static int init_func_spi(void)
  214. {
  215. puts("SPI: ");
  216. spi_init();
  217. puts("ready\n");
  218. return 0;
  219. }
  220. #endif
  221. __maybe_unused
  222. static int zero_global_data(void)
  223. {
  224. memset((void *)gd, '\0', sizeof(gd_t));
  225. return 0;
  226. }
  227. static int setup_mon_len(void)
  228. {
  229. #ifdef __ARM__
  230. gd->mon_len = (ulong)&__bss_end - (ulong)_start;
  231. #elif defined(CONFIG_SANDBOX)
  232. gd->mon_len = (ulong)&_end - (ulong)_init;
  233. #elif defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
  234. gd->mon_len = CONFIG_SYS_MONITOR_LEN;
  235. #else
  236. /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
  237. gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  238. #endif
  239. return 0;
  240. }
  241. __weak int arch_cpu_init(void)
  242. {
  243. return 0;
  244. }
  245. #ifdef CONFIG_OF_HOSTFILE
  246. static int read_fdt_from_file(void)
  247. {
  248. struct sandbox_state *state = state_get_current();
  249. const char *fname = state->fdt_fname;
  250. void *blob;
  251. ssize_t size;
  252. int err;
  253. int fd;
  254. blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0);
  255. if (!state->fdt_fname) {
  256. err = fdt_create_empty_tree(blob, 256);
  257. if (!err)
  258. goto done;
  259. printf("Unable to create empty FDT: %s\n", fdt_strerror(err));
  260. return -EINVAL;
  261. }
  262. size = os_get_filesize(fname);
  263. if (size < 0) {
  264. printf("Failed to file FDT file '%s'\n", fname);
  265. return -ENOENT;
  266. }
  267. fd = os_open(fname, OS_O_RDONLY);
  268. if (fd < 0) {
  269. printf("Failed to open FDT file '%s'\n", fname);
  270. return -EACCES;
  271. }
  272. if (os_read(fd, blob, size) != size) {
  273. os_close(fd);
  274. return -EIO;
  275. }
  276. os_close(fd);
  277. done:
  278. gd->fdt_blob = blob;
  279. return 0;
  280. }
  281. #endif
  282. #ifdef CONFIG_SANDBOX
  283. static int setup_ram_buf(void)
  284. {
  285. struct sandbox_state *state = state_get_current();
  286. gd->arch.ram_buf = state->ram_buf;
  287. gd->ram_size = state->ram_size;
  288. return 0;
  289. }
  290. #endif
  291. static int setup_fdt(void)
  292. {
  293. #ifdef CONFIG_OF_CONTROL
  294. # ifdef CONFIG_OF_EMBED
  295. /* Get a pointer to the FDT */
  296. gd->fdt_blob = __dtb_dt_begin;
  297. # elif defined CONFIG_OF_SEPARATE
  298. /* FDT is at end of image */
  299. gd->fdt_blob = (ulong *)&_end;
  300. # elif defined(CONFIG_OF_HOSTFILE)
  301. if (read_fdt_from_file()) {
  302. puts("Failed to read control FDT\n");
  303. return -1;
  304. }
  305. # endif
  306. /* Allow the early environment to override the fdt address */
  307. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  308. (uintptr_t)gd->fdt_blob);
  309. #endif
  310. return 0;
  311. }
  312. /* Get the top of usable RAM */
  313. __weak ulong board_get_usable_ram_top(ulong total_size)
  314. {
  315. return gd->ram_top;
  316. }
  317. static int setup_dest_addr(void)
  318. {
  319. debug("Monitor len: %08lX\n", gd->mon_len);
  320. /*
  321. * Ram is setup, size stored in gd !!
  322. */
  323. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  324. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  325. /*
  326. * Subtract specified amount of memory to hide so that it won't
  327. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  328. * the Linux kernel should now get passed the now "corrected"
  329. * memory size and won't touch it either. This should work
  330. * for arch/ppc and arch/powerpc. Only Linux board ports in
  331. * arch/powerpc with bootwrapper support, that recalculate the
  332. * memory size from the SDRAM controller setup will have to
  333. * get fixed.
  334. */
  335. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  336. #endif
  337. #ifdef CONFIG_SYS_SDRAM_BASE
  338. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  339. #endif
  340. gd->ram_top += get_effective_memsize();
  341. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  342. gd->relocaddr = gd->ram_top;
  343. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  344. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  345. /*
  346. * We need to make sure the location we intend to put secondary core
  347. * boot code is reserved and not used by any part of u-boot
  348. */
  349. if (gd->relocaddr > determine_mp_bootpg(NULL)) {
  350. gd->relocaddr = determine_mp_bootpg(NULL);
  351. debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
  352. }
  353. #endif
  354. return 0;
  355. }
  356. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  357. static int reserve_logbuffer(void)
  358. {
  359. /* reserve kernel log buffer */
  360. gd->relocaddr -= LOGBUFF_RESERVE;
  361. debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
  362. gd->relocaddr);
  363. return 0;
  364. }
  365. #endif
  366. #ifdef CONFIG_PRAM
  367. /* reserve protected RAM */
  368. static int reserve_pram(void)
  369. {
  370. ulong reg;
  371. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  372. gd->relocaddr -= (reg << 10); /* size is in kB */
  373. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  374. gd->relocaddr);
  375. return 0;
  376. }
  377. #endif /* CONFIG_PRAM */
  378. /* Round memory pointer down to next 4 kB limit */
  379. static int reserve_round_4k(void)
  380. {
  381. gd->relocaddr &= ~(4096 - 1);
  382. return 0;
  383. }
  384. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  385. defined(CONFIG_ARM)
  386. static int reserve_mmu(void)
  387. {
  388. /* reserve TLB table */
  389. gd->arch.tlb_size = PGTABLE_SIZE;
  390. gd->relocaddr -= gd->arch.tlb_size;
  391. /* round down to next 64 kB limit */
  392. gd->relocaddr &= ~(0x10000 - 1);
  393. gd->arch.tlb_addr = gd->relocaddr;
  394. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  395. gd->arch.tlb_addr + gd->arch.tlb_size);
  396. return 0;
  397. }
  398. #endif
  399. #ifdef CONFIG_LCD
  400. static int reserve_lcd(void)
  401. {
  402. #ifdef CONFIG_FB_ADDR
  403. gd->fb_base = CONFIG_FB_ADDR;
  404. #else
  405. /* reserve memory for LCD display (always full pages) */
  406. gd->relocaddr = lcd_setmem(gd->relocaddr);
  407. gd->fb_base = gd->relocaddr;
  408. #endif /* CONFIG_FB_ADDR */
  409. return 0;
  410. }
  411. #endif /* CONFIG_LCD */
  412. static int reserve_trace(void)
  413. {
  414. #ifdef CONFIG_TRACE
  415. gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
  416. gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
  417. debug("Reserving %dk for trace data at: %08lx\n",
  418. CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
  419. #endif
  420. return 0;
  421. }
  422. #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
  423. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  424. !defined(CONFIG_BLACKFIN)
  425. static int reserve_video(void)
  426. {
  427. /* reserve memory for video display (always full pages) */
  428. gd->relocaddr = video_setmem(gd->relocaddr);
  429. gd->fb_base = gd->relocaddr;
  430. return 0;
  431. }
  432. #endif
  433. static int reserve_uboot(void)
  434. {
  435. /*
  436. * reserve memory for U-Boot code, data & bss
  437. * round down to next 4 kB limit
  438. */
  439. gd->relocaddr -= gd->mon_len;
  440. gd->relocaddr &= ~(4096 - 1);
  441. #ifdef CONFIG_E500
  442. /* round down to next 64 kB limit so that IVPR stays aligned */
  443. gd->relocaddr &= ~(65536 - 1);
  444. #endif
  445. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  446. gd->relocaddr);
  447. gd->start_addr_sp = gd->relocaddr;
  448. return 0;
  449. }
  450. #ifndef CONFIG_SPL_BUILD
  451. /* reserve memory for malloc() area */
  452. static int reserve_malloc(void)
  453. {
  454. gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
  455. debug("Reserving %dk for malloc() at: %08lx\n",
  456. TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  457. return 0;
  458. }
  459. /* (permanently) allocate a Board Info struct */
  460. static int reserve_board(void)
  461. {
  462. if (!gd->bd) {
  463. gd->start_addr_sp -= sizeof(bd_t);
  464. gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  465. memset(gd->bd, '\0', sizeof(bd_t));
  466. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  467. sizeof(bd_t), gd->start_addr_sp);
  468. }
  469. return 0;
  470. }
  471. #endif
  472. static int setup_machine(void)
  473. {
  474. #ifdef CONFIG_MACH_TYPE
  475. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  476. #endif
  477. return 0;
  478. }
  479. static int reserve_global_data(void)
  480. {
  481. gd->start_addr_sp -= sizeof(gd_t);
  482. gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  483. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  484. sizeof(gd_t), gd->start_addr_sp);
  485. return 0;
  486. }
  487. static int reserve_fdt(void)
  488. {
  489. /*
  490. * If the device tree is sitting immediate above our image then we
  491. * must relocate it. If it is embedded in the data section, then it
  492. * will be relocated with other data.
  493. */
  494. if (gd->fdt_blob) {
  495. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  496. gd->start_addr_sp -= gd->fdt_size;
  497. gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  498. debug("Reserving %lu Bytes for FDT at: %08lx\n",
  499. gd->fdt_size, gd->start_addr_sp);
  500. }
  501. return 0;
  502. }
  503. static int reserve_stacks(void)
  504. {
  505. #ifdef CONFIG_SPL_BUILD
  506. # ifdef CONFIG_ARM
  507. gd->start_addr_sp -= 128; /* leave 32 words for abort-stack */
  508. gd->irq_sp = gd->start_addr_sp;
  509. # endif
  510. #else
  511. # ifdef CONFIG_PPC
  512. ulong *s;
  513. # endif
  514. /* setup stack pointer for exceptions */
  515. gd->start_addr_sp -= 16;
  516. gd->start_addr_sp &= ~0xf;
  517. gd->irq_sp = gd->start_addr_sp;
  518. /*
  519. * Handle architecture-specific things here
  520. * TODO(sjg@chromium.org): Perhaps create arch_reserve_stack()
  521. * to handle this and put in arch/xxx/lib/stack.c
  522. */
  523. # if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  524. # ifdef CONFIG_USE_IRQ
  525. gd->start_addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ);
  526. debug("Reserving %zu Bytes for IRQ stack at: %08lx\n",
  527. CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ, gd->start_addr_sp);
  528. /* 8-byte alignment for ARM ABI compliance */
  529. gd->start_addr_sp &= ~0x07;
  530. # endif
  531. /* leave 3 words for abort-stack, plus 1 for alignment */
  532. gd->start_addr_sp -= 16;
  533. # elif defined(CONFIG_PPC)
  534. /* Clear initial stack frame */
  535. s = (ulong *) gd->start_addr_sp;
  536. *s = 0; /* Terminate back chain */
  537. *++s = 0; /* NULL return address */
  538. # endif /* Architecture specific code */
  539. return 0;
  540. #endif
  541. }
  542. static int display_new_sp(void)
  543. {
  544. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  545. return 0;
  546. }
  547. #ifdef CONFIG_PPC
  548. static int setup_board_part1(void)
  549. {
  550. bd_t *bd = gd->bd;
  551. /*
  552. * Save local variables to board info struct
  553. */
  554. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  555. bd->bi_memsize = gd->ram_size; /* size in bytes */
  556. #ifdef CONFIG_SYS_SRAM_BASE
  557. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  558. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  559. #endif
  560. #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
  561. defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  562. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  563. #endif
  564. #if defined(CONFIG_MPC5xxx)
  565. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  566. #endif
  567. #if defined(CONFIG_MPC83xx)
  568. bd->bi_immrbar = CONFIG_SYS_IMMR;
  569. #endif
  570. return 0;
  571. }
  572. static int setup_board_part2(void)
  573. {
  574. bd_t *bd = gd->bd;
  575. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  576. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  577. #if defined(CONFIG_CPM2)
  578. bd->bi_cpmfreq = gd->arch.cpm_clk;
  579. bd->bi_brgfreq = gd->arch.brg_clk;
  580. bd->bi_sccfreq = gd->arch.scc_clk;
  581. bd->bi_vco = gd->arch.vco_out;
  582. #endif /* CONFIG_CPM2 */
  583. #if defined(CONFIG_MPC512X)
  584. bd->bi_ipsfreq = gd->arch.ips_clk;
  585. #endif /* CONFIG_MPC512X */
  586. #if defined(CONFIG_MPC5xxx)
  587. bd->bi_ipbfreq = gd->arch.ipb_clk;
  588. bd->bi_pcifreq = gd->pci_clk;
  589. #endif /* CONFIG_MPC5xxx */
  590. return 0;
  591. }
  592. #endif
  593. #ifdef CONFIG_SYS_EXTBDINFO
  594. static int setup_board_extra(void)
  595. {
  596. bd_t *bd = gd->bd;
  597. strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
  598. strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
  599. sizeof(bd->bi_r_version));
  600. bd->bi_procfreq = gd->cpu_clk; /* Processor Speed, In Hz */
  601. bd->bi_plb_busfreq = gd->bus_clk;
  602. #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
  603. defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  604. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  605. bd->bi_pci_busfreq = get_PCI_freq();
  606. bd->bi_opbfreq = get_OPB_freq();
  607. #elif defined(CONFIG_XILINX_405)
  608. bd->bi_pci_busfreq = get_PCI_freq();
  609. #endif
  610. return 0;
  611. }
  612. #endif
  613. #ifdef CONFIG_POST
  614. static int init_post(void)
  615. {
  616. post_bootmode_init();
  617. post_run(NULL, POST_ROM | post_bootmode_get(0));
  618. return 0;
  619. }
  620. #endif
  621. static int setup_dram_config(void)
  622. {
  623. /* Ram is board specific, so move it to board code ... */
  624. dram_init_banksize();
  625. return 0;
  626. }
  627. static int reloc_fdt(void)
  628. {
  629. if (gd->new_fdt) {
  630. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  631. gd->fdt_blob = gd->new_fdt;
  632. }
  633. return 0;
  634. }
  635. static int setup_reloc(void)
  636. {
  637. #ifdef CONFIG_SYS_TEXT_BASE
  638. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  639. #endif
  640. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  641. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  642. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  643. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  644. gd->start_addr_sp);
  645. return 0;
  646. }
  647. /* ARM calls relocate_code from its crt0.S */
  648. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  649. static int jump_to_copy(void)
  650. {
  651. /*
  652. * x86 is special, but in a nice way. It uses a trampoline which
  653. * enables the dcache if possible.
  654. *
  655. * For now, other archs use relocate_code(), which is implemented
  656. * similarly for all archs. When we do generic relocation, hopefully
  657. * we can make all archs enable the dcache prior to relocation.
  658. */
  659. #ifdef CONFIG_X86
  660. /*
  661. * SDRAM and console are now initialised. The final stack can now
  662. * be setup in SDRAM. Code execution will continue in Flash, but
  663. * with the stack in SDRAM and Global Data in temporary memory
  664. * (CPU cache)
  665. */
  666. board_init_f_r_trampoline(gd->start_addr_sp);
  667. #else
  668. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  669. #endif
  670. return 0;
  671. }
  672. #endif
  673. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  674. static int mark_bootstage(void)
  675. {
  676. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  677. return 0;
  678. }
  679. static int initf_malloc(void)
  680. {
  681. #ifdef CONFIG_SYS_MALLOC_F_LEN
  682. assert(gd->malloc_base); /* Set up by crt0.S */
  683. gd->malloc_limit = gd->malloc_base + CONFIG_SYS_MALLOC_F_LEN;
  684. gd->malloc_ptr = 0;
  685. #endif
  686. return 0;
  687. }
  688. static int initf_dm(void)
  689. {
  690. #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
  691. int ret;
  692. ret = dm_init_and_scan(true);
  693. if (ret)
  694. return ret;
  695. #endif
  696. return 0;
  697. }
  698. static init_fnc_t init_sequence_f[] = {
  699. #ifdef CONFIG_SANDBOX
  700. setup_ram_buf,
  701. #endif
  702. setup_mon_len,
  703. setup_fdt,
  704. trace_early_init,
  705. #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
  706. /* TODO: can this go into arch_cpu_init()? */
  707. probecpu,
  708. #endif
  709. arch_cpu_init, /* basic arch cpu dependent setup */
  710. #ifdef CONFIG_X86
  711. cpu_init_f, /* TODO(sjg@chromium.org): remove */
  712. # ifdef CONFIG_OF_CONTROL
  713. find_fdt, /* TODO(sjg@chromium.org): remove */
  714. # endif
  715. #endif
  716. mark_bootstage,
  717. #ifdef CONFIG_OF_CONTROL
  718. fdtdec_check_fdt,
  719. #endif
  720. initf_malloc,
  721. initf_dm,
  722. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  723. board_early_init_f,
  724. #endif
  725. /* TODO: can any of this go into arch_cpu_init()? */
  726. #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
  727. get_clocks, /* get CPU and bus clocks (etc.) */
  728. #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
  729. && !defined(CONFIG_TQM885D)
  730. adjust_sdram_tbs_8xx,
  731. #endif
  732. /* TODO: can we rename this to timer_init()? */
  733. init_timebase,
  734. #endif
  735. #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_BLACKFIN)
  736. timer_init, /* initialize timer */
  737. #endif
  738. #ifdef CONFIG_SYS_ALLOC_DPRAM
  739. #if !defined(CONFIG_CPM2)
  740. dpram_init,
  741. #endif
  742. #endif
  743. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  744. board_postclk_init,
  745. #endif
  746. #ifdef CONFIG_FSL_ESDHC
  747. get_clocks,
  748. #endif
  749. env_init, /* initialize environment */
  750. #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
  751. /* get CPU and bus clocks according to the environment variable */
  752. get_clocks_866,
  753. /* adjust sdram refresh rate according to the new clock */
  754. sdram_adjust_866,
  755. init_timebase,
  756. #endif
  757. init_baud_rate, /* initialze baudrate settings */
  758. serial_init, /* serial communications setup */
  759. console_init_f, /* stage 1 init of console */
  760. #ifdef CONFIG_SANDBOX
  761. sandbox_early_getopt_check,
  762. #endif
  763. #ifdef CONFIG_OF_CONTROL
  764. fdtdec_prepare_fdt,
  765. #endif
  766. display_options, /* say that we are here */
  767. display_text_info, /* show debugging info if required */
  768. #if defined(CONFIG_MPC8260)
  769. prt_8260_rsr,
  770. prt_8260_clks,
  771. #endif /* CONFIG_MPC8260 */
  772. #if defined(CONFIG_MPC83xx)
  773. prt_83xx_rsr,
  774. #endif
  775. #ifdef CONFIG_PPC
  776. checkcpu,
  777. #endif
  778. print_cpuinfo, /* display cpu info (and speed) */
  779. #if defined(CONFIG_MPC5xxx)
  780. prt_mpc5xxx_clks,
  781. #endif /* CONFIG_MPC5xxx */
  782. #if defined(CONFIG_DISPLAY_BOARDINFO)
  783. checkboard, /* display board info */
  784. #endif
  785. INIT_FUNC_WATCHDOG_INIT
  786. #if defined(CONFIG_MISC_INIT_F)
  787. misc_init_f,
  788. #endif
  789. INIT_FUNC_WATCHDOG_RESET
  790. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  791. init_func_i2c,
  792. #endif
  793. #if defined(CONFIG_HARD_SPI)
  794. init_func_spi,
  795. #endif
  796. #ifdef CONFIG_X86
  797. dram_init_f, /* configure available RAM banks */
  798. calculate_relocation_address,
  799. #endif
  800. announce_dram_init,
  801. /* TODO: unify all these dram functions? */
  802. #ifdef CONFIG_ARM
  803. dram_init, /* configure available RAM banks */
  804. #endif
  805. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC)
  806. init_func_ram,
  807. #endif
  808. #ifdef CONFIG_POST
  809. post_init_f,
  810. #endif
  811. INIT_FUNC_WATCHDOG_RESET
  812. #if defined(CONFIG_SYS_DRAM_TEST)
  813. testdram,
  814. #endif /* CONFIG_SYS_DRAM_TEST */
  815. INIT_FUNC_WATCHDOG_RESET
  816. #ifdef CONFIG_POST
  817. init_post,
  818. #endif
  819. INIT_FUNC_WATCHDOG_RESET
  820. /*
  821. * Now that we have DRAM mapped and working, we can
  822. * relocate the code and continue running from DRAM.
  823. *
  824. * Reserve memory at end of RAM for (top down in that order):
  825. * - area that won't get touched by U-Boot and Linux (optional)
  826. * - kernel log buffer
  827. * - protected RAM
  828. * - LCD framebuffer
  829. * - monitor code
  830. * - board info struct
  831. */
  832. setup_dest_addr,
  833. #if defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
  834. /* Blackfin u-boot monitor should be on top of the ram */
  835. reserve_uboot,
  836. #endif
  837. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  838. reserve_logbuffer,
  839. #endif
  840. #ifdef CONFIG_PRAM
  841. reserve_pram,
  842. #endif
  843. reserve_round_4k,
  844. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  845. defined(CONFIG_ARM)
  846. reserve_mmu,
  847. #endif
  848. #ifdef CONFIG_LCD
  849. reserve_lcd,
  850. #endif
  851. reserve_trace,
  852. /* TODO: Why the dependency on CONFIG_8xx? */
  853. #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
  854. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  855. !defined(CONFIG_BLACKFIN)
  856. reserve_video,
  857. #endif
  858. #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_NIOS2)
  859. reserve_uboot,
  860. #endif
  861. #ifndef CONFIG_SPL_BUILD
  862. reserve_malloc,
  863. reserve_board,
  864. #endif
  865. setup_machine,
  866. reserve_global_data,
  867. reserve_fdt,
  868. reserve_stacks,
  869. setup_dram_config,
  870. show_dram_config,
  871. #ifdef CONFIG_PPC
  872. setup_board_part1,
  873. INIT_FUNC_WATCHDOG_RESET
  874. setup_board_part2,
  875. #endif
  876. display_new_sp,
  877. #ifdef CONFIG_SYS_EXTBDINFO
  878. setup_board_extra,
  879. #endif
  880. INIT_FUNC_WATCHDOG_RESET
  881. reloc_fdt,
  882. setup_reloc,
  883. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  884. jump_to_copy,
  885. #endif
  886. NULL,
  887. };
  888. void board_init_f(ulong boot_flags)
  889. {
  890. #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
  891. /*
  892. * For some archtectures, global data is initialized and used before
  893. * calling this function. The data should be preserved. For others,
  894. * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
  895. * here to host global data until relocation.
  896. */
  897. gd_t data;
  898. gd = &data;
  899. /*
  900. * Clear global data before it is accessed at debug print
  901. * in initcall_run_list. Otherwise the debug print probably
  902. * get the wrong vaule of gd->have_console.
  903. */
  904. zero_global_data();
  905. #endif
  906. gd->flags = boot_flags;
  907. gd->have_console = 0;
  908. if (initcall_run_list(init_sequence_f))
  909. hang();
  910. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  911. /* NOTREACHED - jump_to_copy() does not return */
  912. hang();
  913. #endif
  914. }
  915. #ifdef CONFIG_X86
  916. /*
  917. * For now this code is only used on x86.
  918. *
  919. * init_sequence_f_r is the list of init functions which are run when
  920. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  921. * The following limitations must be considered when implementing an
  922. * '_f_r' function:
  923. * - 'static' variables are read-only
  924. * - Global Data (gd->xxx) is read/write
  925. *
  926. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  927. * supported). It _should_, if possible, copy global data to RAM and
  928. * initialise the CPU caches (to speed up the relocation process)
  929. *
  930. * NOTE: At present only x86 uses this route, but it is intended that
  931. * all archs will move to this when generic relocation is implemented.
  932. */
  933. static init_fnc_t init_sequence_f_r[] = {
  934. init_cache_f_r,
  935. copy_uboot_to_ram,
  936. clear_bss,
  937. do_elf_reloc_fixups,
  938. NULL,
  939. };
  940. void board_init_f_r(void)
  941. {
  942. if (initcall_run_list(init_sequence_f_r))
  943. hang();
  944. /*
  945. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  946. * Transfer execution from Flash to RAM by calculating the address
  947. * of the in-RAM copy of board_init_r() and calling it
  948. */
  949. (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
  950. /* NOTREACHED - board_init_r() does not return */
  951. hang();
  952. }
  953. #endif /* CONFIG_X86 */