board_f.c 25 KB

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