board_f.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. # if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
  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. # elif defined(CONFIG_MIPS)
  540. /* Clear initial stack frame */
  541. s = (ulong *) gd->start_addr_sp;
  542. *s-- = 0;
  543. *s-- = 0;
  544. gd->start_addr_sp = (ulong) s;
  545. # endif /* Architecture specific code */
  546. return 0;
  547. #endif
  548. }
  549. static int display_new_sp(void)
  550. {
  551. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  552. return 0;
  553. }
  554. #ifdef CONFIG_PPC
  555. static int setup_board_part1(void)
  556. {
  557. bd_t *bd = gd->bd;
  558. /*
  559. * Save local variables to board info struct
  560. */
  561. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  562. bd->bi_memsize = gd->ram_size; /* size in bytes */
  563. #ifdef CONFIG_SYS_SRAM_BASE
  564. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  565. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  566. #endif
  567. #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
  568. defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  569. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  570. #endif
  571. #if defined(CONFIG_MPC5xxx)
  572. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  573. #endif
  574. #if defined(CONFIG_MPC83xx)
  575. bd->bi_immrbar = CONFIG_SYS_IMMR;
  576. #endif
  577. return 0;
  578. }
  579. static int setup_board_part2(void)
  580. {
  581. bd_t *bd = gd->bd;
  582. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  583. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  584. #if defined(CONFIG_CPM2)
  585. bd->bi_cpmfreq = gd->arch.cpm_clk;
  586. bd->bi_brgfreq = gd->arch.brg_clk;
  587. bd->bi_sccfreq = gd->arch.scc_clk;
  588. bd->bi_vco = gd->arch.vco_out;
  589. #endif /* CONFIG_CPM2 */
  590. #if defined(CONFIG_MPC512X)
  591. bd->bi_ipsfreq = gd->arch.ips_clk;
  592. #endif /* CONFIG_MPC512X */
  593. #if defined(CONFIG_MPC5xxx)
  594. bd->bi_ipbfreq = gd->arch.ipb_clk;
  595. bd->bi_pcifreq = gd->pci_clk;
  596. #endif /* CONFIG_MPC5xxx */
  597. return 0;
  598. }
  599. #endif
  600. #ifdef CONFIG_SYS_EXTBDINFO
  601. static int setup_board_extra(void)
  602. {
  603. bd_t *bd = gd->bd;
  604. strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
  605. strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
  606. sizeof(bd->bi_r_version));
  607. bd->bi_procfreq = gd->cpu_clk; /* Processor Speed, In Hz */
  608. bd->bi_plb_busfreq = gd->bus_clk;
  609. #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
  610. defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  611. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  612. bd->bi_pci_busfreq = get_PCI_freq();
  613. bd->bi_opbfreq = get_OPB_freq();
  614. #elif defined(CONFIG_XILINX_405)
  615. bd->bi_pci_busfreq = get_PCI_freq();
  616. #endif
  617. return 0;
  618. }
  619. #endif
  620. #ifdef CONFIG_POST
  621. static int init_post(void)
  622. {
  623. post_bootmode_init();
  624. post_run(NULL, POST_ROM | post_bootmode_get(0));
  625. return 0;
  626. }
  627. #endif
  628. static int setup_dram_config(void)
  629. {
  630. /* Ram is board specific, so move it to board code ... */
  631. dram_init_banksize();
  632. return 0;
  633. }
  634. static int reloc_fdt(void)
  635. {
  636. if (gd->new_fdt) {
  637. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  638. gd->fdt_blob = gd->new_fdt;
  639. }
  640. return 0;
  641. }
  642. static int setup_reloc(void)
  643. {
  644. #ifdef CONFIG_SYS_TEXT_BASE
  645. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  646. #endif
  647. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  648. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  649. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  650. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  651. gd->start_addr_sp);
  652. return 0;
  653. }
  654. /* ARM calls relocate_code from its crt0.S */
  655. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  656. static int jump_to_copy(void)
  657. {
  658. /*
  659. * x86 is special, but in a nice way. It uses a trampoline which
  660. * enables the dcache if possible.
  661. *
  662. * For now, other archs use relocate_code(), which is implemented
  663. * similarly for all archs. When we do generic relocation, hopefully
  664. * we can make all archs enable the dcache prior to relocation.
  665. */
  666. #ifdef CONFIG_X86
  667. /*
  668. * SDRAM and console are now initialised. The final stack can now
  669. * be setup in SDRAM. Code execution will continue in Flash, but
  670. * with the stack in SDRAM and Global Data in temporary memory
  671. * (CPU cache)
  672. */
  673. board_init_f_r_trampoline(gd->start_addr_sp);
  674. #else
  675. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  676. #endif
  677. return 0;
  678. }
  679. #endif
  680. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  681. static int mark_bootstage(void)
  682. {
  683. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  684. return 0;
  685. }
  686. static int initf_malloc(void)
  687. {
  688. #ifdef CONFIG_SYS_MALLOC_F_LEN
  689. assert(gd->malloc_base); /* Set up by crt0.S */
  690. gd->malloc_limit = gd->malloc_base + CONFIG_SYS_MALLOC_F_LEN;
  691. gd->malloc_ptr = 0;
  692. #endif
  693. return 0;
  694. }
  695. static int initf_dm(void)
  696. {
  697. #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
  698. int ret;
  699. ret = dm_init_and_scan(true);
  700. if (ret)
  701. return ret;
  702. #endif
  703. return 0;
  704. }
  705. static init_fnc_t init_sequence_f[] = {
  706. #ifdef CONFIG_SANDBOX
  707. setup_ram_buf,
  708. #endif
  709. setup_mon_len,
  710. setup_fdt,
  711. trace_early_init,
  712. initf_malloc,
  713. #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
  714. /* TODO: can this go into arch_cpu_init()? */
  715. probecpu,
  716. #endif
  717. arch_cpu_init, /* basic arch cpu dependent setup */
  718. mark_bootstage,
  719. #ifdef CONFIG_OF_CONTROL
  720. fdtdec_check_fdt,
  721. #endif
  722. initf_dm,
  723. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  724. board_early_init_f,
  725. #endif
  726. /* TODO: can any of this go into arch_cpu_init()? */
  727. #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
  728. get_clocks, /* get CPU and bus clocks (etc.) */
  729. #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
  730. && !defined(CONFIG_TQM885D)
  731. adjust_sdram_tbs_8xx,
  732. #endif
  733. /* TODO: can we rename this to timer_init()? */
  734. init_timebase,
  735. #endif
  736. #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || defined(CONFIG_BLACKFIN)
  737. timer_init, /* initialize timer */
  738. #endif
  739. #ifdef CONFIG_SYS_ALLOC_DPRAM
  740. #if !defined(CONFIG_CPM2)
  741. dpram_init,
  742. #endif
  743. #endif
  744. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  745. board_postclk_init,
  746. #endif
  747. #ifdef CONFIG_FSL_ESDHC
  748. get_clocks,
  749. #endif
  750. env_init, /* initialize environment */
  751. #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
  752. /* get CPU and bus clocks according to the environment variable */
  753. get_clocks_866,
  754. /* adjust sdram refresh rate according to the new clock */
  755. sdram_adjust_866,
  756. init_timebase,
  757. #endif
  758. init_baud_rate, /* initialze baudrate settings */
  759. serial_init, /* serial communications setup */
  760. console_init_f, /* stage 1 init of console */
  761. #ifdef CONFIG_SANDBOX
  762. sandbox_early_getopt_check,
  763. #endif
  764. #ifdef CONFIG_OF_CONTROL
  765. fdtdec_prepare_fdt,
  766. #endif
  767. display_options, /* say that we are here */
  768. display_text_info, /* show debugging info if required */
  769. #if defined(CONFIG_MPC8260)
  770. prt_8260_rsr,
  771. prt_8260_clks,
  772. #endif /* CONFIG_MPC8260 */
  773. #if defined(CONFIG_MPC83xx)
  774. prt_83xx_rsr,
  775. #endif
  776. #ifdef CONFIG_PPC
  777. checkcpu,
  778. #endif
  779. print_cpuinfo, /* display cpu info (and speed) */
  780. #if defined(CONFIG_MPC5xxx)
  781. prt_mpc5xxx_clks,
  782. #endif /* CONFIG_MPC5xxx */
  783. #if defined(CONFIG_DISPLAY_BOARDINFO)
  784. checkboard, /* display board info */
  785. #endif
  786. INIT_FUNC_WATCHDOG_INIT
  787. #if defined(CONFIG_MISC_INIT_F)
  788. misc_init_f,
  789. #endif
  790. INIT_FUNC_WATCHDOG_RESET
  791. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  792. init_func_i2c,
  793. #endif
  794. #if defined(CONFIG_HARD_SPI)
  795. init_func_spi,
  796. #endif
  797. announce_dram_init,
  798. /* TODO: unify all these dram functions? */
  799. #if defined(CONFIG_ARM) || defined(CONFIG_X86)
  800. dram_init, /* configure available RAM banks */
  801. #endif
  802. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC)
  803. init_func_ram,
  804. #endif
  805. #ifdef CONFIG_POST
  806. post_init_f,
  807. #endif
  808. INIT_FUNC_WATCHDOG_RESET
  809. #if defined(CONFIG_SYS_DRAM_TEST)
  810. testdram,
  811. #endif /* CONFIG_SYS_DRAM_TEST */
  812. INIT_FUNC_WATCHDOG_RESET
  813. #ifdef CONFIG_POST
  814. init_post,
  815. #endif
  816. INIT_FUNC_WATCHDOG_RESET
  817. /*
  818. * Now that we have DRAM mapped and working, we can
  819. * relocate the code and continue running from DRAM.
  820. *
  821. * Reserve memory at end of RAM for (top down in that order):
  822. * - area that won't get touched by U-Boot and Linux (optional)
  823. * - kernel log buffer
  824. * - protected RAM
  825. * - LCD framebuffer
  826. * - monitor code
  827. * - board info struct
  828. */
  829. setup_dest_addr,
  830. #if defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
  831. /* Blackfin u-boot monitor should be on top of the ram */
  832. reserve_uboot,
  833. #endif
  834. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  835. reserve_logbuffer,
  836. #endif
  837. #ifdef CONFIG_PRAM
  838. reserve_pram,
  839. #endif
  840. reserve_round_4k,
  841. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  842. defined(CONFIG_ARM)
  843. reserve_mmu,
  844. #endif
  845. #ifdef CONFIG_LCD
  846. reserve_lcd,
  847. #endif
  848. reserve_trace,
  849. /* TODO: Why the dependency on CONFIG_8xx? */
  850. #if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
  851. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  852. !defined(CONFIG_BLACKFIN)
  853. reserve_video,
  854. #endif
  855. #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_NIOS2)
  856. reserve_uboot,
  857. #endif
  858. #ifndef CONFIG_SPL_BUILD
  859. reserve_malloc,
  860. reserve_board,
  861. #endif
  862. setup_machine,
  863. reserve_global_data,
  864. reserve_fdt,
  865. reserve_stacks,
  866. setup_dram_config,
  867. show_dram_config,
  868. #ifdef CONFIG_PPC
  869. setup_board_part1,
  870. INIT_FUNC_WATCHDOG_RESET
  871. setup_board_part2,
  872. #endif
  873. display_new_sp,
  874. #ifdef CONFIG_SYS_EXTBDINFO
  875. setup_board_extra,
  876. #endif
  877. INIT_FUNC_WATCHDOG_RESET
  878. reloc_fdt,
  879. setup_reloc,
  880. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  881. jump_to_copy,
  882. #endif
  883. NULL,
  884. };
  885. void board_init_f(ulong boot_flags)
  886. {
  887. #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
  888. /*
  889. * For some archtectures, global data is initialized and used before
  890. * calling this function. The data should be preserved. For others,
  891. * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
  892. * here to host global data until relocation.
  893. */
  894. gd_t data;
  895. gd = &data;
  896. /*
  897. * Clear global data before it is accessed at debug print
  898. * in initcall_run_list. Otherwise the debug print probably
  899. * get the wrong vaule of gd->have_console.
  900. */
  901. zero_global_data();
  902. #endif
  903. gd->flags = boot_flags;
  904. gd->have_console = 0;
  905. if (initcall_run_list(init_sequence_f))
  906. hang();
  907. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  908. /* NOTREACHED - jump_to_copy() does not return */
  909. hang();
  910. #endif
  911. }
  912. #ifdef CONFIG_X86
  913. /*
  914. * For now this code is only used on x86.
  915. *
  916. * init_sequence_f_r is the list of init functions which are run when
  917. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  918. * The following limitations must be considered when implementing an
  919. * '_f_r' function:
  920. * - 'static' variables are read-only
  921. * - Global Data (gd->xxx) is read/write
  922. *
  923. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  924. * supported). It _should_, if possible, copy global data to RAM and
  925. * initialise the CPU caches (to speed up the relocation process)
  926. *
  927. * NOTE: At present only x86 uses this route, but it is intended that
  928. * all archs will move to this when generic relocation is implemented.
  929. */
  930. static init_fnc_t init_sequence_f_r[] = {
  931. init_cache_f_r,
  932. copy_uboot_to_ram,
  933. clear_bss,
  934. do_elf_reloc_fixups,
  935. NULL,
  936. };
  937. void board_init_f_r(void)
  938. {
  939. if (initcall_run_list(init_sequence_f_r))
  940. hang();
  941. /*
  942. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  943. * Transfer execution from Flash to RAM by calculating the address
  944. * of the in-RAM copy of board_init_r() and calling it
  945. */
  946. (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
  947. /* NOTREACHED - board_init_r() does not return */
  948. hang();
  949. }
  950. #endif /* CONFIG_X86 */