board_f.c 26 KB

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