board.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * (C) Copyright 2003
  3. * Josef Baumgartner <josef.baumgartner@telex.de>
  4. *
  5. * (C) Copyright 2000-2002
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <watchdog.h>
  12. #include <command.h>
  13. #include <malloc.h>
  14. #include <stdio_dev.h>
  15. #include <linux/compiler.h>
  16. #include <asm/immap.h>
  17. #if defined(CONFIG_CMD_IDE)
  18. #include <ide.h>
  19. #endif
  20. #if defined(CONFIG_CMD_SCSI)
  21. #include <scsi.h>
  22. #endif
  23. #if defined(CONFIG_CMD_KGDB)
  24. #include <kgdb.h>
  25. #endif
  26. #ifdef CONFIG_STATUS_LED
  27. #include <status_led.h>
  28. #endif
  29. #include <net.h>
  30. #include <serial.h>
  31. #if defined(CONFIG_CMD_BEDBUG)
  32. #include <cmd_bedbug.h>
  33. #endif
  34. #ifdef CONFIG_SYS_ALLOC_DPRAM
  35. #include <commproc.h>
  36. #endif
  37. #include <version.h>
  38. #if defined(CONFIG_HARD_I2C) || \
  39. defined(CONFIG_SYS_I2C)
  40. #include <i2c.h>
  41. #endif
  42. #ifdef CONFIG_CMD_SPI
  43. #include <spi.h>
  44. #endif
  45. #ifdef CONFIG_BITBANGMII
  46. #include <miiphy.h>
  47. #endif
  48. #include <nand.h>
  49. DECLARE_GLOBAL_DATA_PTR;
  50. static char *failed = "*** failed ***\n";
  51. #include <environment.h>
  52. extern ulong __init_end;
  53. extern ulong __bss_end;
  54. #if defined(CONFIG_WATCHDOG)
  55. # undef INIT_FUNC_WATCHDOG_INIT
  56. # define INIT_FUNC_WATCHDOG_INIT watchdog_init,
  57. # define WATCHDOG_DISABLE watchdog_disable
  58. extern int watchdog_init(void);
  59. extern int watchdog_disable(void);
  60. #else
  61. # define INIT_FUNC_WATCHDOG_INIT /* undef */
  62. # define WATCHDOG_DISABLE /* undef */
  63. #endif /* CONFIG_WATCHDOG */
  64. ulong monitor_flash_len;
  65. /************************************************************************
  66. * Utilities *
  67. ************************************************************************
  68. */
  69. /*
  70. * All attempts to come up with a "common" initialization sequence
  71. * that works for all boards and architectures failed: some of the
  72. * requirements are just _too_ different. To get rid of the resulting
  73. * mess of board dependend #ifdef'ed code we now make the whole
  74. * initialization sequence configurable to the user.
  75. *
  76. * The requirements for any new initalization function is simple: it
  77. * receives a pointer to the "global data" structure as it's only
  78. * argument, and returns an integer return code, where 0 means
  79. * "continue" and != 0 means "fatal error, hang the system".
  80. */
  81. typedef int (init_fnc_t) (void);
  82. /************************************************************************
  83. * Init Utilities
  84. ************************************************************************
  85. * Some of this code should be moved into the core functions,
  86. * but let's get it working (again) first...
  87. */
  88. static int init_baudrate (void)
  89. {
  90. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  91. return 0;
  92. }
  93. /***********************************************************************/
  94. static int init_func_ram (void)
  95. {
  96. int board_type = 0; /* use dummy arg */
  97. puts ("DRAM: ");
  98. if ((gd->ram_size = initdram (board_type)) > 0) {
  99. print_size (gd->ram_size, "\n");
  100. return (0);
  101. }
  102. puts (failed);
  103. return (1);
  104. }
  105. /***********************************************************************/
  106. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  107. static int init_func_i2c (void)
  108. {
  109. puts ("I2C: ");
  110. #ifdef CONFIG_SYS_I2C
  111. i2c_init_all();
  112. #else
  113. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  114. #endif
  115. puts ("ready\n");
  116. return (0);
  117. }
  118. #endif
  119. #if defined(CONFIG_HARD_SPI)
  120. static int init_func_spi (void)
  121. {
  122. puts ("SPI: ");
  123. spi_init ();
  124. puts ("ready\n");
  125. return (0);
  126. }
  127. #endif
  128. /***********************************************************************/
  129. /************************************************************************
  130. * Initialization sequence *
  131. ************************************************************************
  132. */
  133. init_fnc_t *init_sequence[] = {
  134. get_clocks,
  135. env_init,
  136. init_baudrate,
  137. serial_init,
  138. console_init_f,
  139. display_options,
  140. checkcpu,
  141. checkboard,
  142. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  143. init_func_i2c,
  144. #endif
  145. #if defined(CONFIG_HARD_SPI)
  146. init_func_spi,
  147. #endif
  148. init_func_ram,
  149. #if defined(CONFIG_SYS_DRAM_TEST)
  150. testdram,
  151. #endif /* CONFIG_SYS_DRAM_TEST */
  152. INIT_FUNC_WATCHDOG_INIT
  153. NULL, /* Terminate this list */
  154. };
  155. /************************************************************************
  156. *
  157. * This is the first part of the initialization sequence that is
  158. * implemented in C, but still running from ROM.
  159. *
  160. * The main purpose is to provide a (serial) console interface as
  161. * soon as possible (so we can see any error messages), and to
  162. * initialize the RAM so that we can relocate the monitor code to
  163. * RAM.
  164. *
  165. * Be aware of the restrictions: global data is read-only, BSS is not
  166. * initialized, and stack space is limited to a few kB.
  167. *
  168. ************************************************************************
  169. */
  170. void
  171. board_init_f (ulong bootflag)
  172. {
  173. bd_t *bd;
  174. ulong len, addr, addr_sp;
  175. ulong *paddr;
  176. gd_t *id;
  177. init_fnc_t **init_fnc_ptr;
  178. #ifdef CONFIG_PRAM
  179. ulong reg;
  180. #endif
  181. /* Pointer is writable since we allocated a register for it */
  182. gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
  183. /* compiler optimization barrier needed for GCC >= 3.4 */
  184. __asm__ __volatile__("": : :"memory");
  185. /* Clear initial global data */
  186. memset ((void *) gd, 0, sizeof (gd_t));
  187. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  188. if ((*init_fnc_ptr)() != 0) {
  189. hang ();
  190. }
  191. }
  192. /*
  193. * Now that we have DRAM mapped and working, we can
  194. * relocate the code and continue running from DRAM.
  195. *
  196. * Reserve memory at end of RAM for (top down in that order):
  197. * - protected RAM
  198. * - LCD framebuffer
  199. * - monitor code
  200. * - board info struct
  201. */
  202. len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  203. addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
  204. #ifdef CONFIG_LOGBUFFER
  205. /* reserve kernel log buffer */
  206. addr -= (LOGBUFF_RESERVE);
  207. debug ("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
  208. #endif
  209. #ifdef CONFIG_PRAM
  210. /*
  211. * reserve protected RAM
  212. */
  213. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  214. addr -= (reg << 10); /* size is in kB */
  215. debug ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
  216. #endif /* CONFIG_PRAM */
  217. /* round down to next 4 kB limit */
  218. addr &= ~(4096 - 1);
  219. debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
  220. #ifdef CONFIG_LCD
  221. #ifdef CONFIG_FB_ADDR
  222. gd->fb_base = CONFIG_FB_ADDR;
  223. #else
  224. /* reserve memory for LCD display (always full pages) */
  225. addr = lcd_setmem (addr);
  226. gd->fb_base = addr;
  227. #endif /* CONFIG_FB_ADDR */
  228. #endif /* CONFIG_LCD */
  229. /*
  230. * reserve memory for U-Boot code, data & bss
  231. * round down to next 4 kB limit
  232. */
  233. addr -= len;
  234. addr &= ~(4096 - 1);
  235. debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
  236. /*
  237. * reserve memory for malloc() arena
  238. */
  239. addr_sp = addr - TOTAL_MALLOC_LEN;
  240. debug ("Reserving %dk for malloc() at: %08lx\n",
  241. TOTAL_MALLOC_LEN >> 10, addr_sp);
  242. /*
  243. * (permanently) allocate a Board Info struct
  244. * and a permanent copy of the "global" data
  245. */
  246. addr_sp -= sizeof (bd_t);
  247. bd = (bd_t *) addr_sp;
  248. gd->bd = bd;
  249. debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
  250. sizeof (bd_t), addr_sp);
  251. addr_sp -= sizeof (gd_t);
  252. id = (gd_t *) addr_sp;
  253. debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
  254. sizeof (gd_t), addr_sp);
  255. /* Reserve memory for boot params. */
  256. addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
  257. bd->bi_boot_params = addr_sp;
  258. debug ("Reserving %dk for boot parameters at: %08lx\n",
  259. CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
  260. /*
  261. * Finally, we set up a new (bigger) stack.
  262. *
  263. * Leave some safety gap for SP, force alignment on 16 byte boundary
  264. * Clear initial stack frame
  265. */
  266. addr_sp -= 16;
  267. addr_sp &= ~0xF;
  268. paddr = (ulong *)addr_sp;
  269. *paddr-- = 0;
  270. *paddr-- = 0;
  271. addr_sp = (ulong)paddr;
  272. debug ("Stack Pointer at: %08lx\n", addr_sp);
  273. /*
  274. * Save local variables to board info struct
  275. */
  276. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM memory */
  277. bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
  278. #ifdef CONFIG_SYS_INIT_RAM_ADDR
  279. bd->bi_sramstart = CONFIG_SYS_INIT_RAM_ADDR; /* start of SRAM memory */
  280. bd->bi_sramsize = CONFIG_SYS_INIT_RAM_SIZE; /* size of SRAM memory */
  281. #endif
  282. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  283. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  284. WATCHDOG_RESET ();
  285. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  286. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  287. #ifdef CONFIG_PCI
  288. bd->bi_pcifreq = gd->pci_clk; /* PCI Freq in Hz */
  289. #endif
  290. #ifdef CONFIG_EXTRA_CLOCK
  291. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  292. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  293. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  294. #endif
  295. bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
  296. #ifdef CONFIG_SYS_EXTBDINFO
  297. strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
  298. strncpy (bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
  299. #endif
  300. WATCHDOG_RESET ();
  301. #ifdef CONFIG_POST
  302. post_bootmode_init();
  303. post_run (NULL, POST_ROM | post_bootmode_get(0));
  304. #endif
  305. WATCHDOG_RESET();
  306. memcpy (id, (void *)gd, sizeof (gd_t));
  307. debug ("Start relocate of code from %08x to %08lx\n", CONFIG_SYS_MONITOR_BASE, addr);
  308. relocate_code (addr_sp, id, addr);
  309. /* NOTREACHED - jump_to_ram() does not return */
  310. }
  311. /************************************************************************
  312. *
  313. * This is the next part if the initialization sequence: we are now
  314. * running from RAM and have a "normal" C environment, i. e. global
  315. * data can be written, BSS has been cleared, the stack size in not
  316. * that critical any more, etc.
  317. *
  318. ************************************************************************
  319. */
  320. void board_init_r (gd_t *id, ulong dest_addr)
  321. {
  322. char *s __maybe_unused;
  323. bd_t *bd;
  324. #ifndef CONFIG_ENV_IS_NOWHERE
  325. extern char * env_name_spec;
  326. #endif
  327. #ifndef CONFIG_SYS_NO_FLASH
  328. ulong flash_size;
  329. #endif
  330. gd = id; /* initialize RAM version of global data */
  331. bd = gd->bd;
  332. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  333. WATCHDOG_RESET ();
  334. gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
  335. serial_initialize();
  336. debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  337. monitor_flash_len = (ulong)&__init_end - dest_addr;
  338. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  339. /*
  340. * We have to relocate the command table manually
  341. */
  342. fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
  343. ll_entry_count(cmd_tbl_t, cmd));
  344. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  345. /* there are some other pointer constants we must deal with */
  346. #ifndef CONFIG_ENV_IS_NOWHERE
  347. env_name_spec += gd->reloc_off;
  348. #endif
  349. WATCHDOG_RESET ();
  350. #ifdef CONFIG_LOGBUFFER
  351. logbuff_init_ptrs ();
  352. #endif
  353. #ifdef CONFIG_POST
  354. post_output_backlog ();
  355. post_reloc ();
  356. #endif
  357. WATCHDOG_RESET();
  358. #if 0
  359. /* instruction cache enabled in cpu_init_f() for faster relocation */
  360. icache_enable (); /* it's time to enable the instruction cache */
  361. #endif
  362. /*
  363. * Setup trap handlers
  364. */
  365. trap_init (CONFIG_SYS_SDRAM_BASE);
  366. /* The Malloc area is immediately below the monitor copy in DRAM */
  367. mem_malloc_init (CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
  368. TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
  369. #if !defined(CONFIG_SYS_NO_FLASH)
  370. puts ("Flash: ");
  371. if ((flash_size = flash_init ()) > 0) {
  372. # ifdef CONFIG_SYS_FLASH_CHECKSUM
  373. print_size (flash_size, "");
  374. /*
  375. * Compute and print flash CRC if flashchecksum is set to 'y'
  376. *
  377. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  378. */
  379. if (getenv_yesno("flashchecksum") == 1) {
  380. printf (" CRC: %08X",
  381. crc32 (0,
  382. (const unsigned char *) CONFIG_SYS_FLASH_BASE,
  383. flash_size)
  384. );
  385. }
  386. putc ('\n');
  387. # else /* !CONFIG_SYS_FLASH_CHECKSUM */
  388. print_size (flash_size, "\n");
  389. # endif /* CONFIG_SYS_FLASH_CHECKSUM */
  390. } else {
  391. puts (failed);
  392. hang ();
  393. }
  394. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
  395. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  396. bd->bi_flashoffset = 0;
  397. #else /* CONFIG_SYS_NO_FLASH */
  398. bd->bi_flashsize = 0;
  399. bd->bi_flashstart = 0;
  400. bd->bi_flashoffset = 0;
  401. #endif /* !CONFIG_SYS_NO_FLASH */
  402. WATCHDOG_RESET ();
  403. /* initialize higher level parts of CPU like time base and timers */
  404. cpu_init_r ();
  405. WATCHDOG_RESET ();
  406. #ifdef CONFIG_SPI
  407. # if !defined(CONFIG_ENV_IS_IN_EEPROM)
  408. spi_init_f ();
  409. # endif
  410. spi_init_r ();
  411. #endif
  412. #if defined(CONFIG_SYS_I2C)
  413. /* Adjust I2C subsystem pointers after relocation */
  414. i2c_reloc_fixup();
  415. #endif
  416. /* relocate environment function pointers etc. */
  417. env_relocate ();
  418. WATCHDOG_RESET ();
  419. #if defined(CONFIG_PCI)
  420. /*
  421. * Do pci configuration
  422. */
  423. pci_init ();
  424. #endif
  425. /** leave this here (after malloc(), environment and PCI are working) **/
  426. /* Initialize stdio devices */
  427. stdio_init ();
  428. /* Initialize the jump table for applications */
  429. jumptable_init ();
  430. /* Initialize the console (after the relocation and devices init) */
  431. console_init_r ();
  432. #if defined(CONFIG_MISC_INIT_R)
  433. /* miscellaneous platform dependent initialisations */
  434. misc_init_r ();
  435. #endif
  436. #if defined(CONFIG_CMD_KGDB)
  437. WATCHDOG_RESET ();
  438. puts ("KGDB: ");
  439. kgdb_init ();
  440. #endif
  441. debug ("U-Boot relocated to %08lx\n", dest_addr);
  442. /*
  443. * Enable Interrupts
  444. */
  445. interrupt_init ();
  446. /* Must happen after interrupts are initialized since
  447. * an irq handler gets installed
  448. */
  449. timer_init();
  450. #ifdef CONFIG_STATUS_LED
  451. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  452. #endif
  453. udelay (20);
  454. /* Insert function pointers now that we have relocated the code */
  455. /* Initialize from environment */
  456. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  457. WATCHDOG_RESET ();
  458. #if defined(CONFIG_CMD_DOC)
  459. WATCHDOG_RESET ();
  460. puts ("DOC: ");
  461. doc_init ();
  462. #endif
  463. #if defined(CONFIG_CMD_NAND)
  464. WATCHDOG_RESET ();
  465. puts ("NAND: ");
  466. nand_init(); /* go init the NAND */
  467. #endif
  468. #ifdef CONFIG_BITBANGMII
  469. bb_miiphy_init();
  470. #endif
  471. #if defined(CONFIG_CMD_NET)
  472. WATCHDOG_RESET();
  473. #if defined(FEC_ENET)
  474. eth_init(bd);
  475. #endif
  476. puts ("Net: ");
  477. eth_initialize (bd);
  478. #endif
  479. #ifdef CONFIG_POST
  480. post_run (NULL, POST_RAM | post_bootmode_get(0));
  481. #endif
  482. #if defined(CONFIG_CMD_PCMCIA) \
  483. && !defined(CONFIG_CMD_IDE)
  484. WATCHDOG_RESET ();
  485. puts ("PCMCIA:");
  486. pcmcia_init ();
  487. #endif
  488. #if defined(CONFIG_CMD_IDE)
  489. WATCHDOG_RESET ();
  490. puts ("IDE: ");
  491. ide_init ();
  492. #endif
  493. #ifdef CONFIG_LAST_STAGE_INIT
  494. WATCHDOG_RESET ();
  495. /*
  496. * Some parts can be only initialized if all others (like
  497. * Interrupts) are up and running (i.e. the PC-style ISA
  498. * keyboard).
  499. */
  500. last_stage_init ();
  501. #endif
  502. #if defined(CONFIG_CMD_BEDBUG)
  503. WATCHDOG_RESET ();
  504. bedbug_init ();
  505. #endif
  506. #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
  507. /*
  508. * Export available size of memory for Linux,
  509. * taking into account the protected RAM at top of memory
  510. */
  511. {
  512. ulong pram = 0;
  513. char memsz[32];
  514. #ifdef CONFIG_PRAM
  515. pram = getenv_ulong("pram", 10, CONFIG_PRAM);
  516. #endif
  517. #ifdef CONFIG_LOGBUFFER
  518. /* Also take the logbuffer into account (pram is in kB) */
  519. pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
  520. #endif
  521. sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
  522. setenv ("mem", memsz);
  523. }
  524. #endif
  525. #ifdef CONFIG_MODEM_SUPPORT
  526. {
  527. extern int do_mdm_init;
  528. do_mdm_init = gd->do_mdm_init;
  529. }
  530. #endif
  531. #ifdef CONFIG_WATCHDOG
  532. /* disable watchdog if environment is set */
  533. if ((s = getenv ("watchdog")) != NULL) {
  534. if (strncmp (s, "off", 3) == 0) {
  535. WATCHDOG_DISABLE ();
  536. }
  537. }
  538. #endif /* CONFIG_WATCHDOG*/
  539. /* Initialization complete - start the monitor */
  540. /* main_loop() can return to retry autoboot, if so just run it again. */
  541. for (;;) {
  542. WATCHDOG_RESET ();
  543. main_loop ();
  544. }
  545. /* NOTREACHED - no way out of command loop except booting */
  546. }