board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* SPARC Board initialization
  2. *
  3. * (C) Copyright 2000-2006
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2007
  7. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <malloc.h>
  30. #include <stdio_dev.h>
  31. #include <config.h>
  32. #if defined(CONFIG_CMD_IDE)
  33. #include <ide.h>
  34. #endif
  35. #ifdef CONFIG_STATUS_LED
  36. #include <status_led.h>
  37. #endif
  38. #include <net.h>
  39. #include <serial.h>
  40. #include <version.h>
  41. #if defined(CONFIG_POST)
  42. #include <post.h>
  43. #endif
  44. #ifdef CONFIG_PS2KBD
  45. #include <keyboard.h>
  46. #endif
  47. #ifdef CONFIG_CMD_AMBAPP
  48. #include <ambapp.h>
  49. #endif
  50. #ifdef CONFIG_BITBANGMII
  51. #include <miiphy.h>
  52. #endif
  53. DECLARE_GLOBAL_DATA_PTR;
  54. /* Debug options
  55. #define DEBUG_INIT_SEQUENCE
  56. #define DEBUG_MEM_LAYOUT
  57. #define DEBUG_COMMANDS
  58. */
  59. extern void timer_interrupt_init(void);
  60. extern int do_ambapp_print(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]);
  61. extern int prom_init(void);
  62. #if defined(CONFIG__CMD_DOC)
  63. void doc_init(void);
  64. #endif
  65. #if !defined(CONFIG_SYS_NO_FLASH)
  66. static char *failed = "*** failed ***\n";
  67. #endif
  68. #include <environment.h>
  69. ulong monitor_flash_len;
  70. /************************************************************************
  71. * Init Utilities *
  72. ************************************************************************
  73. * Some of this code should be moved into the core functions,
  74. * but let's get it working (again) first...
  75. */
  76. static int init_baudrate(void)
  77. {
  78. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  79. return 0;
  80. }
  81. /***********************************************************************/
  82. /*
  83. * All attempts to come up with a "common" initialization sequence
  84. * that works for all boards and architectures failed: some of the
  85. * requirements are just _too_ different. To get rid of the resulting
  86. * mess of board dependend #ifdef'ed code we now make the whole
  87. * initialization sequence configurable to the user.
  88. *
  89. * The requirements for any new initalization function is simple: it
  90. * receives a pointer to the "global data" structure as it's only
  91. * argument, and returns an integer return code, where 0 means
  92. * "continue" and != 0 means "fatal error, hang the system".
  93. */
  94. typedef int (init_fnc_t) (void);
  95. #define WATCHDOG_RESET(x)
  96. /************************************************************************
  97. * Initialization sequence *
  98. ************************************************************************
  99. */
  100. init_fnc_t *init_sequence[] = {
  101. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  102. board_early_init_f,
  103. #endif
  104. serial_init,
  105. init_timebase,
  106. #if defined(CONFIG_CMD_AMBAPP)
  107. ambapp_init_reloc,
  108. #endif
  109. env_init,
  110. init_baudrate,
  111. console_init_f,
  112. display_options,
  113. checkcpu,
  114. checkboard,
  115. #if defined(CONFIG_MISC_INIT_F)
  116. misc_init_f,
  117. #endif
  118. #ifdef CONFIG_POST
  119. post_init_f,
  120. #endif
  121. NULL, /* Terminate this list,
  122. * beware: this list will be relocated
  123. * which means that NULL will become
  124. * NULL+RELOC_OFFSET. We simply make
  125. * NULL be -RELOC_OFFSET instead.
  126. */
  127. };
  128. /************************************************************************
  129. *
  130. * This is the SPARC board initialization routine, running from RAM.
  131. *
  132. ************************************************************************
  133. */
  134. #ifdef DEBUG_INIT_SEQUENCE
  135. char *str_init_seq = "INIT_SEQ 00\n";
  136. char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
  137. #endif
  138. void board_init_f(ulong bootflag)
  139. {
  140. bd_t *bd;
  141. init_fnc_t **init_fnc_ptr;
  142. int j;
  143. #ifndef CONFIG_SYS_NO_FLASH
  144. ulong flash_size;
  145. #endif
  146. gd = (gd_t *) (CONFIG_SYS_GBL_DATA_OFFSET);
  147. /* Clear initial global data */
  148. memset((void *)gd, 0, sizeof(gd_t));
  149. gd->bd = (bd_t *) (gd + 1); /* At end of global data */
  150. gd->baudrate = CONFIG_BAUDRATE;
  151. gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
  152. bd = gd->bd;
  153. bd->bi_memstart = CONFIG_SYS_RAM_BASE;
  154. bd->bi_memsize = CONFIG_SYS_RAM_SIZE;
  155. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  156. #if defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
  157. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
  158. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
  159. #endif
  160. bd->bi_baudrate = CONFIG_BAUDRATE;
  161. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  162. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  163. gd->reloc_off = CONFIG_SYS_RELOC_MONITOR_BASE - CONFIG_SYS_MONITOR_BASE;
  164. for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
  165. ++init_fnc_ptr, j++) {
  166. #ifdef DEBUG_INIT_SEQUENCE
  167. if (j > 9)
  168. str_init_seq[9] = '0' + (j / 10);
  169. str_init_seq[10] = '0' + (j - (j / 10) * 10);
  170. serial_puts(str_init_seq);
  171. #endif
  172. if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
  173. hang();
  174. }
  175. }
  176. #ifdef DEBUG_INIT_SEQUENCE
  177. serial_puts(str_init_seq_done);
  178. #endif
  179. /*
  180. * Now that we have DRAM mapped and working, we can
  181. * relocate the code and continue running from DRAM.
  182. *
  183. * Reserve memory at end of RAM for (top down in that order):
  184. * - kernel log buffer
  185. * - protected RAM
  186. * - LCD framebuffer
  187. * - monitor code
  188. * - board info struct
  189. */
  190. #ifdef DEBUG_MEM_LAYOUT
  191. printf("CONFIG_SYS_MONITOR_BASE: 0x%lx\n", CONFIG_SYS_MONITOR_BASE);
  192. printf("CONFIG_ENV_ADDR: 0x%lx\n", CONFIG_ENV_ADDR);
  193. printf("CONFIG_SYS_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CONFIG_SYS_RELOC_MONITOR_BASE,
  194. CONFIG_SYS_MONITOR_LEN);
  195. printf("CONFIG_SYS_MALLOC_BASE: 0x%lx (%d)\n", CONFIG_SYS_MALLOC_BASE,
  196. CONFIG_SYS_MALLOC_LEN);
  197. printf("CONFIG_SYS_INIT_SP_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_INIT_SP_OFFSET,
  198. CONFIG_SYS_STACK_SIZE);
  199. printf("CONFIG_SYS_PROM_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_PROM_OFFSET,
  200. CONFIG_SYS_PROM_SIZE);
  201. printf("CONFIG_SYS_GBL_DATA_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_GBL_DATA_OFFSET,
  202. GENERATED_GBL_DATA_SIZE);
  203. #endif
  204. #ifdef CONFIG_POST
  205. post_bootmode_init();
  206. post_run(NULL, POST_ROM | post_bootmode_get(0));
  207. #endif
  208. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  209. /*
  210. * We have to relocate the command table manually
  211. */
  212. fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
  213. ll_entry_count(cmd_tbl_t, cmd));
  214. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  215. #if defined(CONFIG_CMD_AMBAPP) && defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
  216. puts("AMBA:\n");
  217. do_ambapp_print(NULL, 0, 0, NULL);
  218. #endif
  219. /* initialize higher level parts of CPU like time base and timers */
  220. cpu_init_r();
  221. /* start timer */
  222. timer_interrupt_init();
  223. /*
  224. * Enable Interrupts before any calls to udelay,
  225. * the flash driver may use udelay resulting in
  226. * a hang if not timer0 IRQ is enabled.
  227. */
  228. interrupt_init();
  229. /* The Malloc area is immediately below the monitor copy in RAM */
  230. mem_malloc_init(CONFIG_SYS_MALLOC_BASE,
  231. CONFIG_SYS_MALLOC_END - CONFIG_SYS_MALLOC_BASE);
  232. malloc_bin_reloc();
  233. #if !defined(CONFIG_SYS_NO_FLASH)
  234. puts("Flash: ");
  235. if ((flash_size = flash_init()) > 0) {
  236. # ifdef CONFIG_SYS_FLASH_CHECKSUM
  237. print_size(flash_size, "");
  238. /*
  239. * Compute and print flash CRC if flashchecksum is set to 'y'
  240. *
  241. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  242. */
  243. if (getenv_yesno("flashchecksum") == 1) {
  244. printf(" CRC: %08lX",
  245. crc32(0, (const unsigned char *)CONFIG_SYS_FLASH_BASE,
  246. flash_size)
  247. );
  248. }
  249. putc('\n');
  250. # else /* !CONFIG_SYS_FLASH_CHECKSUM */
  251. print_size(flash_size, "\n");
  252. # endif /* CONFIG_SYS_FLASH_CHECKSUM */
  253. } else {
  254. puts(failed);
  255. hang();
  256. }
  257. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
  258. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  259. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  260. bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor */
  261. #else
  262. bd->bi_flashoffset = 0;
  263. #endif
  264. #else /* CONFIG_SYS_NO_FLASH */
  265. bd->bi_flashsize = 0;
  266. bd->bi_flashstart = 0;
  267. bd->bi_flashoffset = 0;
  268. #endif /* !CONFIG_SYS_NO_FLASH */
  269. #ifdef CONFIG_SPI
  270. # if !defined(CONFIG_ENV_IS_IN_EEPROM)
  271. spi_init_f();
  272. # endif
  273. spi_init_r();
  274. #endif
  275. /* relocate environment function pointers etc. */
  276. env_relocate();
  277. #if defined(CONFIG_BOARD_LATE_INIT)
  278. board_late_init();
  279. #endif
  280. #ifdef CONFIG_ID_EEPROM
  281. mac_read_from_eeprom();
  282. #endif
  283. #if defined(CONFIG_PCI)
  284. /*
  285. * Do pci configuration
  286. */
  287. pci_init();
  288. #endif
  289. /* Initialize stdio devices */
  290. stdio_init();
  291. /* Initialize the jump table for applications */
  292. jumptable_init();
  293. /* Initialize the console (after the relocation and devices init) */
  294. console_init_r();
  295. #ifdef CONFIG_STATUS_LED
  296. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  297. #endif
  298. udelay(20);
  299. /* Initialize from environment */
  300. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  301. WATCHDOG_RESET();
  302. #if defined(CONFIG_CMD_DOC)
  303. WATCHDOG_RESET();
  304. puts("DOC: ");
  305. doc_init();
  306. #endif
  307. #ifdef CONFIG_BITBANGMII
  308. bb_miiphy_init();
  309. #endif
  310. #if defined(CONFIG_CMD_NET)
  311. WATCHDOG_RESET();
  312. puts("Net: ");
  313. eth_initialize(bd);
  314. #endif
  315. #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
  316. WATCHDOG_RESET();
  317. debug("Reset Ethernet PHY\n");
  318. reset_phy();
  319. #endif
  320. #ifdef CONFIG_POST
  321. post_run(NULL, POST_RAM | post_bootmode_get(0));
  322. #endif
  323. #if defined(CONFIG_CMD_IDE)
  324. WATCHDOG_RESET();
  325. puts("IDE: ");
  326. ide_init();
  327. #endif /* CONFIG_CMD_IDE */
  328. #ifdef CONFIG_LAST_STAGE_INIT
  329. WATCHDOG_RESET();
  330. /*
  331. * Some parts can be only initialized if all others (like
  332. * Interrupts) are up and running (i.e. the PC-style ISA
  333. * keyboard).
  334. */
  335. last_stage_init();
  336. #endif
  337. #ifdef CONFIG_PS2KBD
  338. puts("PS/2: ");
  339. kbd_init();
  340. #endif
  341. prom_init();
  342. /* main_loop */
  343. for (;;) {
  344. WATCHDOG_RESET();
  345. main_loop();
  346. }
  347. }
  348. void hang(void)
  349. {
  350. puts("### ERROR ### Please RESET the board ###\n");
  351. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  352. bootstage_error(BOOTSTAGE_ID_NEED_RESET);
  353. #endif
  354. for (;;) ;
  355. }
  356. /************************************************************************/