board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 void malloc_bin_reloc(void);
  61. extern int do_ambapp_print(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]);
  62. extern int prom_init(void);
  63. #if defined(CONFIG__CMD_DOC)
  64. void doc_init(void);
  65. #endif
  66. #if !defined(CONFIG_SYS_NO_FLASH)
  67. static char *failed = "*** failed ***\n";
  68. #endif
  69. #include <environment.h>
  70. ulong monitor_flash_len;
  71. /************************************************************************
  72. * Init Utilities *
  73. ************************************************************************
  74. * Some of this code should be moved into the core functions,
  75. * but let's get it working (again) first...
  76. */
  77. static int init_baudrate(void)
  78. {
  79. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  80. return 0;
  81. }
  82. /***********************************************************************/
  83. /*
  84. * All attempts to come up with a "common" initialization sequence
  85. * that works for all boards and architectures failed: some of the
  86. * requirements are just _too_ different. To get rid of the resulting
  87. * mess of board dependend #ifdef'ed code we now make the whole
  88. * initialization sequence configurable to the user.
  89. *
  90. * The requirements for any new initalization function is simple: it
  91. * receives a pointer to the "global data" structure as it's only
  92. * argument, and returns an integer return code, where 0 means
  93. * "continue" and != 0 means "fatal error, hang the system".
  94. */
  95. typedef int (init_fnc_t) (void);
  96. #define WATCHDOG_RESET(x)
  97. /************************************************************************
  98. * Initialization sequence *
  99. ************************************************************************
  100. */
  101. init_fnc_t *init_sequence[] = {
  102. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  103. board_early_init_f,
  104. #endif
  105. serial_init,
  106. init_timebase,
  107. #if defined(CONFIG_CMD_AMBAPP)
  108. ambapp_init_reloc,
  109. #endif
  110. env_init,
  111. init_baudrate,
  112. console_init_f,
  113. display_options,
  114. checkcpu,
  115. checkboard,
  116. #if defined(CONFIG_MISC_INIT_F)
  117. misc_init_f,
  118. #endif
  119. #ifdef CONFIG_POST
  120. post_init_f,
  121. #endif
  122. NULL, /* Terminate this list,
  123. * beware: this list will be relocated
  124. * which means that NULL will become
  125. * NULL+RELOC_OFFSET. We simply make
  126. * NULL be -RELOC_OFFSET instead.
  127. */
  128. };
  129. /************************************************************************
  130. *
  131. * This is the SPARC board initialization routine, running from RAM.
  132. *
  133. ************************************************************************
  134. */
  135. #ifdef DEBUG_INIT_SEQUENCE
  136. char *str_init_seq = "INIT_SEQ 00\n";
  137. char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
  138. #endif
  139. void board_init_f(ulong bootflag)
  140. {
  141. cmd_tbl_t *cmdtp;
  142. bd_t *bd;
  143. unsigned char *s;
  144. init_fnc_t **init_fnc_ptr;
  145. int j;
  146. int i;
  147. char *e;
  148. #ifndef CONFIG_SYS_NO_FLASH
  149. ulong flash_size;
  150. #endif
  151. gd = (gd_t *) (CONFIG_SYS_GBL_DATA_OFFSET);
  152. /* Clear initial global data */
  153. memset((void *)gd, 0, sizeof(gd_t));
  154. gd->bd = (bd_t *) (gd + 1); /* At end of global data */
  155. gd->baudrate = CONFIG_BAUDRATE;
  156. gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
  157. bd = gd->bd;
  158. bd->bi_memstart = CONFIG_SYS_RAM_BASE;
  159. bd->bi_memsize = CONFIG_SYS_RAM_SIZE;
  160. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  161. #if defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
  162. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
  163. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
  164. #endif
  165. bd->bi_baudrate = CONFIG_BAUDRATE;
  166. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  167. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  168. gd->reloc_off = CONFIG_SYS_RELOC_MONITOR_BASE - CONFIG_SYS_MONITOR_BASE;
  169. for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
  170. ++init_fnc_ptr, j++) {
  171. #ifdef DEBUG_INIT_SEQUENCE
  172. if (j > 9)
  173. str_init_seq[9] = '0' + (j / 10);
  174. str_init_seq[10] = '0' + (j - (j / 10) * 10);
  175. serial_puts(str_init_seq);
  176. #endif
  177. if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
  178. hang();
  179. }
  180. }
  181. #ifdef DEBUG_INIT_SEQUENCE
  182. serial_puts(str_init_seq_done);
  183. #endif
  184. /*
  185. * Now that we have DRAM mapped and working, we can
  186. * relocate the code and continue running from DRAM.
  187. *
  188. * Reserve memory at end of RAM for (top down in that order):
  189. * - kernel log buffer
  190. * - protected RAM
  191. * - LCD framebuffer
  192. * - monitor code
  193. * - board info struct
  194. */
  195. #ifdef DEBUG_MEM_LAYOUT
  196. printf("CONFIG_SYS_MONITOR_BASE: 0x%lx\n", CONFIG_SYS_MONITOR_BASE);
  197. printf("CONFIG_ENV_ADDR: 0x%lx\n", CONFIG_ENV_ADDR);
  198. printf("CONFIG_SYS_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CONFIG_SYS_RELOC_MONITOR_BASE,
  199. CONFIG_SYS_MONITOR_LEN);
  200. printf("CONFIG_SYS_MALLOC_BASE: 0x%lx (%d)\n", CONFIG_SYS_MALLOC_BASE,
  201. CONFIG_SYS_MALLOC_LEN);
  202. printf("CONFIG_SYS_INIT_SP_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_INIT_SP_OFFSET,
  203. CONFIG_SYS_STACK_SIZE);
  204. printf("CONFIG_SYS_PROM_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_PROM_OFFSET,
  205. CONFIG_SYS_PROM_SIZE);
  206. printf("CONFIG_SYS_GBL_DATA_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_GBL_DATA_OFFSET,
  207. GENERATED_GBL_DATA_SIZE);
  208. #endif
  209. #ifdef CONFIG_POST
  210. post_bootmode_init();
  211. post_run(NULL, POST_ROM | post_bootmode_get(0));
  212. #endif
  213. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  214. /*
  215. * We have to relocate the command table manually
  216. */
  217. fixup_cmdtable(&__u_boot_cmd_start,
  218. (ulong)(&__u_boot_cmd_end - &__u_boot_cmd_start));
  219. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  220. #if defined(CONFIG_CMD_AMBAPP) && defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
  221. puts("AMBA:\n");
  222. do_ambapp_print(NULL, 0, 0, NULL);
  223. #endif
  224. /* initialize higher level parts of CPU like time base and timers */
  225. cpu_init_r();
  226. /* start timer */
  227. timer_interrupt_init();
  228. /*
  229. * Enable Interrupts before any calls to udelay,
  230. * the flash driver may use udelay resulting in
  231. * a hang if not timer0 IRQ is enabled.
  232. */
  233. interrupt_init();
  234. /* The Malloc area is immediately below the monitor copy in RAM */
  235. mem_malloc_init(CONFIG_SYS_MALLOC_BASE,
  236. CONFIG_SYS_MALLOC_END - CONFIG_SYS_MALLOC_BASE);
  237. malloc_bin_reloc();
  238. #if !defined(CONFIG_SYS_NO_FLASH)
  239. puts("Flash: ");
  240. if ((flash_size = flash_init()) > 0) {
  241. # ifdef CONFIG_SYS_FLASH_CHECKSUM
  242. print_size(flash_size, "");
  243. /*
  244. * Compute and print flash CRC if flashchecksum is set to 'y'
  245. *
  246. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  247. */
  248. s = getenv("flashchecksum");
  249. if (s && (*s == 'y')) {
  250. printf(" CRC: %08lX",
  251. crc32(0, (const unsigned char *)CONFIG_SYS_FLASH_BASE,
  252. flash_size)
  253. );
  254. }
  255. putc('\n');
  256. # else /* !CONFIG_SYS_FLASH_CHECKSUM */
  257. print_size(flash_size, "\n");
  258. # endif /* CONFIG_SYS_FLASH_CHECKSUM */
  259. } else {
  260. puts(failed);
  261. hang();
  262. }
  263. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
  264. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  265. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  266. bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor */
  267. #else
  268. bd->bi_flashoffset = 0;
  269. #endif
  270. #else /* CONFIG_SYS_NO_FLASH */
  271. bd->bi_flashsize = 0;
  272. bd->bi_flashstart = 0;
  273. bd->bi_flashoffset = 0;
  274. #endif /* !CONFIG_SYS_NO_FLASH */
  275. #ifdef CONFIG_SPI
  276. # if !defined(CONFIG_ENV_IS_IN_EEPROM)
  277. spi_init_f();
  278. # endif
  279. spi_init_r();
  280. #endif
  281. /* relocate environment function pointers etc. */
  282. env_relocate();
  283. #if defined(CONFIG_BOARD_LATE_INIT)
  284. board_late_init();
  285. #endif
  286. #ifdef CONFIG_ID_EEPROM
  287. mac_read_from_eeprom();
  288. #endif
  289. /* IP Address */
  290. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  291. #if defined(CONFIG_PCI)
  292. /*
  293. * Do pci configuration
  294. */
  295. pci_init();
  296. #endif
  297. /* Initialize stdio devices */
  298. stdio_init();
  299. /* Initialize the jump table for applications */
  300. jumptable_init();
  301. /* Initialize the console (after the relocation and devices init) */
  302. console_init_r();
  303. #ifdef CONFIG_STATUS_LED
  304. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  305. #endif
  306. udelay(20);
  307. /* Initialize from environment */
  308. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  309. #if defined(CONFIG_CMD_NET)
  310. if ((s = getenv("bootfile")) != NULL) {
  311. copy_filename(BootFile, s, sizeof(BootFile));
  312. }
  313. #endif /* CONFIG_CMD_NET */
  314. WATCHDOG_RESET();
  315. #if defined(CONFIG_CMD_DOC)
  316. WATCHDOG_RESET();
  317. puts("DOC: ");
  318. doc_init();
  319. #endif
  320. #ifdef CONFIG_BITBANGMII
  321. bb_miiphy_init();
  322. #endif
  323. #if defined(CONFIG_CMD_NET)
  324. WATCHDOG_RESET();
  325. puts("Net: ");
  326. eth_initialize(bd);
  327. #endif
  328. #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
  329. WATCHDOG_RESET();
  330. debug("Reset Ethernet PHY\n");
  331. reset_phy();
  332. #endif
  333. #ifdef CONFIG_POST
  334. post_run(NULL, POST_RAM | post_bootmode_get(0));
  335. #endif
  336. #if defined(CONFIG_CMD_IDE)
  337. WATCHDOG_RESET();
  338. puts("IDE: ");
  339. ide_init();
  340. #endif /* CONFIG_CMD_IDE */
  341. #ifdef CONFIG_LAST_STAGE_INIT
  342. WATCHDOG_RESET();
  343. /*
  344. * Some parts can be only initialized if all others (like
  345. * Interrupts) are up and running (i.e. the PC-style ISA
  346. * keyboard).
  347. */
  348. last_stage_init();
  349. #endif
  350. #ifdef CONFIG_PS2KBD
  351. puts("PS/2: ");
  352. kbd_init();
  353. #endif
  354. prom_init();
  355. /* main_loop */
  356. for (;;) {
  357. WATCHDOG_RESET();
  358. main_loop();
  359. }
  360. }
  361. void hang(void)
  362. {
  363. puts("### ERROR ### Please RESET the board ###\n");
  364. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  365. show_boot_progress(-30);
  366. #endif
  367. for (;;) ;
  368. }
  369. /************************************************************************/