board.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 <devices.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. DECLARE_GLOBAL_DATA_PTR;
  51. /* Debug options
  52. #define DEBUG_INIT_SEQUENCE
  53. #define DEBUG_MEM_LAYOUT
  54. #define DEBUG_COMMANDS
  55. */
  56. extern void timer_interrupt_init(void);
  57. extern void malloc_bin_reloc(void);
  58. extern int do_ambapp_print(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
  59. extern int prom_init(void);
  60. #if defined(CONFIG__CMD_DOC)
  61. void doc_init(void);
  62. #endif
  63. #if !defined(CFG_NO_FLASH)
  64. static char *failed = "*** failed ***\n";
  65. #endif
  66. #include <environment.h>
  67. ulong monitor_flash_len;
  68. /*
  69. * Begin and End of memory area for malloc(), and current "brk"
  70. */
  71. static ulong mem_malloc_start = 0;
  72. static ulong mem_malloc_end = 0;
  73. static ulong mem_malloc_brk = 0;
  74. /************************************************************************
  75. * Utilities *
  76. ************************************************************************
  77. */
  78. /*
  79. * The Malloc area is immediately below the monitor copy in RAM
  80. */
  81. static void mem_malloc_init(void)
  82. {
  83. mem_malloc_start = CFG_MALLOC_BASE;
  84. mem_malloc_end = CFG_MALLOC_END;
  85. mem_malloc_brk = mem_malloc_start;
  86. memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
  87. }
  88. void *sbrk(ptrdiff_t increment)
  89. {
  90. ulong old = mem_malloc_brk;
  91. ulong new = old + increment;
  92. if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
  93. return (NULL);
  94. }
  95. mem_malloc_brk = new;
  96. return ((void *)old);
  97. }
  98. /***********************************************************************/
  99. /************************************************************************
  100. * Init Utilities *
  101. ************************************************************************
  102. * Some of this code should be moved into the core functions,
  103. * but let's get it working (again) first...
  104. */
  105. static int init_baudrate(void)
  106. {
  107. char tmp[64]; /* long enough for environment variables */
  108. int i = getenv_r("baudrate", tmp, sizeof(tmp));
  109. gd->baudrate = (i > 0)
  110. ? (int)simple_strtoul(tmp, NULL, 10)
  111. : CONFIG_BAUDRATE;
  112. return (0);
  113. }
  114. /***********************************************************************/
  115. /*
  116. * All attempts to come up with a "common" initialization sequence
  117. * that works for all boards and architectures failed: some of the
  118. * requirements are just _too_ different. To get rid of the resulting
  119. * mess of board dependend #ifdef'ed code we now make the whole
  120. * initialization sequence configurable to the user.
  121. *
  122. * The requirements for any new initalization function is simple: it
  123. * receives a pointer to the "global data" structure as it's only
  124. * argument, and returns an integer return code, where 0 means
  125. * "continue" and != 0 means "fatal error, hang the system".
  126. */
  127. typedef int (init_fnc_t) (void);
  128. #define WATCHDOG_RESET(x)
  129. /************************************************************************
  130. * Initialization sequence *
  131. ************************************************************************
  132. */
  133. init_fnc_t *init_sequence[] = {
  134. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  135. board_early_init_f,
  136. #endif
  137. serial_init,
  138. init_timebase,
  139. #if defined(CONFIG_CMD_AMBAPP)
  140. ambapp_init_reloc,
  141. #endif
  142. env_init,
  143. init_baudrate,
  144. console_init_f,
  145. display_options,
  146. checkcpu,
  147. checkboard,
  148. #if defined(CONFIG_MISC_INIT_F)
  149. misc_init_f,
  150. #endif
  151. #ifdef CONFIG_POST
  152. post_init_f,
  153. #endif
  154. NULL, /* Terminate this list,
  155. * beware: this list will be relocated
  156. * which means that NULL will become
  157. * NULL+RELOC_OFFSET. We simply make
  158. * NULL be -RELOC_OFFSET instead.
  159. */
  160. };
  161. /************************************************************************
  162. *
  163. * This is the SPARC board initialization routine, running from RAM.
  164. *
  165. ************************************************************************
  166. */
  167. #ifdef DEBUG_INIT_SEQUENCE
  168. char *str_init_seq = "INIT_SEQ 00\n";
  169. char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
  170. #endif
  171. void board_init_f(ulong bootflag)
  172. {
  173. cmd_tbl_t *cmdtp;
  174. bd_t *bd;
  175. unsigned char *s;
  176. init_fnc_t **init_fnc_ptr;
  177. int j;
  178. int i;
  179. char *e;
  180. #ifndef CFG_NO_FLASH
  181. ulong flash_size;
  182. #endif
  183. gd = (gd_t *) (CFG_GBL_DATA_OFFSET);
  184. /* Clear initial global data */
  185. memset((void *)gd, 0, sizeof(gd_t));
  186. gd->bd = (bd_t *) (gd + 1); /* At end of global data */
  187. gd->baudrate = CONFIG_BAUDRATE;
  188. gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
  189. bd = gd->bd;
  190. bd->bi_memstart = CFG_RAM_BASE;
  191. bd->bi_memsize = CFG_RAM_SIZE;
  192. bd->bi_flashstart = CFG_FLASH_BASE;
  193. #if defined(CFG_SRAM_BASE) && defined(CFG_SRAM_SIZE)
  194. bd->bi_sramstart = CFG_SRAM_BASE;
  195. bd->bi_sramsize = CFG_SRAM_SIZE;
  196. #endif
  197. bd->bi_baudrate = CONFIG_BAUDRATE;
  198. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  199. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  200. gd->reloc_off = CFG_RELOC_MONITOR_BASE - CFG_MONITOR_BASE;
  201. for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
  202. ++init_fnc_ptr, j++) {
  203. #ifdef DEBUG_INIT_SEQUENCE
  204. if (j > 9)
  205. str_init_seq[9] = '0' + (j / 10);
  206. str_init_seq[10] = '0' + (j - (j / 10) * 10);
  207. serial_puts(str_init_seq);
  208. #endif
  209. if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
  210. hang();
  211. }
  212. }
  213. #ifdef DEBUG_INIT_SEQUENCE
  214. serial_puts(str_init_seq_done);
  215. #endif
  216. /*
  217. * Now that we have DRAM mapped and working, we can
  218. * relocate the code and continue running from DRAM.
  219. *
  220. * Reserve memory at end of RAM for (top down in that order):
  221. * - kernel log buffer
  222. * - protected RAM
  223. * - LCD framebuffer
  224. * - monitor code
  225. * - board info struct
  226. */
  227. #ifdef DEBUG_MEM_LAYOUT
  228. printf("CFG_MONITOR_BASE: 0x%lx\n", CFG_MONITOR_BASE);
  229. printf("CFG_ENV_ADDR: 0x%lx\n", CFG_ENV_ADDR);
  230. printf("CFG_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CFG_RELOC_MONITOR_BASE,
  231. CFG_MONITOR_LEN);
  232. printf("CFG_MALLOC_BASE: 0x%lx (%d)\n", CFG_MALLOC_BASE,
  233. CFG_MALLOC_LEN);
  234. printf("CFG_INIT_SP_OFFSET: 0x%lx (%d)\n", CFG_INIT_SP_OFFSET,
  235. CFG_STACK_SIZE);
  236. printf("CFG_PROM_OFFSET: 0x%lx (%d)\n", CFG_PROM_OFFSET,
  237. CFG_PROM_SIZE);
  238. printf("CFG_GBL_DATA_OFFSET: 0x%lx (%d)\n", CFG_GBL_DATA_OFFSET,
  239. CFG_GBL_DATA_SIZE);
  240. #endif
  241. #ifdef CONFIG_POST
  242. post_bootmode_init();
  243. post_run(NULL, POST_ROM | post_bootmode_get(0));
  244. #endif
  245. /*
  246. * We have to relocate the command table manually
  247. */
  248. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  249. ulong addr;
  250. addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
  251. #if DEBUG_COMMANDS
  252. printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
  253. cmdtp->name, (ulong) (cmdtp->cmd), addr);
  254. #endif
  255. cmdtp->cmd =
  256. (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
  257. addr = (ulong) (cmdtp->name) + gd->reloc_off;
  258. cmdtp->name = (char *)addr;
  259. if (cmdtp->usage) {
  260. addr = (ulong) (cmdtp->usage) + gd->reloc_off;
  261. cmdtp->usage = (char *)addr;
  262. }
  263. #ifdef CFG_LONGHELP
  264. if (cmdtp->help) {
  265. addr = (ulong) (cmdtp->help) + gd->reloc_off;
  266. cmdtp->help = (char *)addr;
  267. }
  268. #endif
  269. }
  270. #if defined(CONFIG_CMD_AMBAPP) && defined(CFG_AMBAPP_PRINT_ON_STARTUP)
  271. puts("AMBA:\n");
  272. do_ambapp_print(NULL, 0, 0, NULL);
  273. #endif
  274. /* initialize higher level parts of CPU like time base and timers */
  275. cpu_init_r();
  276. /* start timer */
  277. timer_interrupt_init();
  278. /*
  279. * Enable Interrupts before any calls to udelay,
  280. * the flash driver may use udelay resulting in
  281. * a hang if not timer0 IRQ is enabled.
  282. */
  283. interrupt_init();
  284. #if !defined(CFG_NO_FLASH)
  285. puts("FLASH: ");
  286. if ((flash_size = flash_init()) > 0) {
  287. # ifdef CFG_FLASH_CHECKSUM
  288. print_size(flash_size, "");
  289. /*
  290. * Compute and print flash CRC if flashchecksum is set to 'y'
  291. *
  292. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  293. */
  294. s = getenv("flashchecksum");
  295. if (s && (*s == 'y')) {
  296. printf(" CRC: %08lX",
  297. crc32(0, (const unsigned char *)CFG_FLASH_BASE,
  298. flash_size)
  299. );
  300. }
  301. putc('\n');
  302. # else /* !CFG_FLASH_CHECKSUM */
  303. print_size(flash_size, "\n");
  304. # endif /* CFG_FLASH_CHECKSUM */
  305. } else {
  306. puts(failed);
  307. hang();
  308. }
  309. bd->bi_flashstart = CFG_FLASH_BASE; /* update start of FLASH memory */
  310. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  311. #if CFG_MONITOR_BASE == CFG_FLASH_BASE
  312. bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor */
  313. #else
  314. bd->bi_flashoffset = 0;
  315. #endif
  316. #else /* CFG_NO_FLASH */
  317. bd->bi_flashsize = 0;
  318. bd->bi_flashstart = 0;
  319. bd->bi_flashoffset = 0;
  320. #endif /* !CFG_NO_FLASH */
  321. /* initialize malloc() area */
  322. mem_malloc_init();
  323. malloc_bin_reloc();
  324. #ifdef CONFIG_SPI
  325. # if !defined(CFG_ENV_IS_IN_EEPROM)
  326. spi_init_f();
  327. # endif
  328. spi_init_r();
  329. #endif
  330. /* relocate environment function pointers etc. */
  331. env_relocate();
  332. #if defined(CONFIG_BOARD_LATE_INIT)
  333. board_late_init();
  334. #endif
  335. s = getenv("ethaddr");
  336. for (i = 0; i < 6; ++i) {
  337. bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
  338. if (s)
  339. s = (*e) ? e + 1 : e;
  340. }
  341. #ifdef CONFIG_HAS_ETH1
  342. /* handle the 2nd ethernet address */
  343. s = getenv("eth1addr");
  344. for (i = 0; i < 6; ++i) {
  345. bd->bi_enet1addr[i] = s ? simple_strtoul(s, &e, 16) : 0;
  346. if (s)
  347. s = (*e) ? e + 1 : e;
  348. }
  349. #endif
  350. #ifdef CFG_ID_EEPROM
  351. mac_read_from_eeprom();
  352. #endif
  353. /* IP Address */
  354. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  355. #if defined(CONFIG_PCI)
  356. /*
  357. * Do pci configuration
  358. */
  359. pci_init();
  360. #endif
  361. /* Initialize devices */
  362. devices_init();
  363. /* Initialize the jump table for applications */
  364. jumptable_init();
  365. /* Initialize the console (after the relocation and devices init) */
  366. console_init_r();
  367. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  368. serial_buffered_init();
  369. #endif
  370. #ifdef CONFIG_STATUS_LED
  371. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  372. #endif
  373. udelay(20);
  374. set_timer(0);
  375. /* Initialize from environment */
  376. if ((s = getenv("loadaddr")) != NULL) {
  377. load_addr = simple_strtoul(s, NULL, 16);
  378. }
  379. #if defined(CONFIG_CMD_NET)
  380. if ((s = getenv("bootfile")) != NULL) {
  381. copy_filename(BootFile, s, sizeof(BootFile));
  382. }
  383. #endif /* CFG_CMD_NET */
  384. WATCHDOG_RESET();
  385. #if defined(CONFIG_CMD_DOC)
  386. WATCHDOG_RESET();
  387. puts("DOC: ");
  388. doc_init();
  389. #endif
  390. #if defined(CONFIG_CMD_NET)
  391. #if defined(CONFIG_NET_MULTI)
  392. WATCHDOG_RESET();
  393. puts("Net: ");
  394. #endif
  395. eth_initialize(bd);
  396. #endif
  397. #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
  398. WATCHDOG_RESET();
  399. debug("Reset Ethernet PHY\n");
  400. reset_phy();
  401. #endif
  402. #ifdef CONFIG_POST
  403. post_run(NULL, POST_RAM | post_bootmode_get(0));
  404. #endif
  405. #if defined(CONFIG_CMD_IDE)
  406. WATCHDOG_RESET();
  407. puts("IDE: ");
  408. ide_init();
  409. #endif /* CFG_CMD_IDE */
  410. #ifdef CONFIG_LAST_STAGE_INIT
  411. WATCHDOG_RESET();
  412. /*
  413. * Some parts can be only initialized if all others (like
  414. * Interrupts) are up and running (i.e. the PC-style ISA
  415. * keyboard).
  416. */
  417. last_stage_init();
  418. #endif
  419. #ifdef CONFIG_PS2KBD
  420. puts("PS/2: ");
  421. kbd_init();
  422. #endif
  423. prom_init();
  424. /* main_loop */
  425. for (;;) {
  426. WATCHDOG_RESET();
  427. main_loop();
  428. }
  429. }
  430. void hang(void)
  431. {
  432. puts("### ERROR ### Please RESET the board ###\n");
  433. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  434. show_boot_progress(-30);
  435. #endif
  436. for (;;) ;
  437. }
  438. /************************************************************************/