board_f.c 25 KB

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