board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  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 <version.h>
  32. #include <net.h>
  33. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  34. void nand_init (void);
  35. #endif
  36. ulong monitor_flash_len;
  37. #ifdef CONFIG_HAS_DATAFLASH
  38. extern int AT91F_DataflashInit(void);
  39. extern void dataflash_print_info(void);
  40. #endif
  41. #ifndef CONFIG_IDENT_STRING
  42. #define CONFIG_IDENT_STRING ""
  43. #endif
  44. const char version_string[] =
  45. U_BOOT_VERSION" (" __DATE__ " - " __TIME__ ")"CONFIG_IDENT_STRING;
  46. #ifdef CONFIG_DRIVER_CS8900
  47. extern void cs8900_get_enetaddr (uchar * addr);
  48. #endif
  49. #ifdef CONFIG_DRIVER_CS8900
  50. extern void rtl8019_get_enetaddr (uchar * addr);
  51. #endif
  52. #ifdef CONFIG_DRIVER_LAN91C96
  53. #include "../drivers/lan91c96.h"
  54. #endif
  55. /*
  56. * Begin and End of memory area for malloc(), and current "brk"
  57. */
  58. static ulong mem_malloc_start = 0;
  59. static ulong mem_malloc_end = 0;
  60. static ulong mem_malloc_brk = 0;
  61. static
  62. void mem_malloc_init (ulong dest_addr)
  63. {
  64. mem_malloc_start = dest_addr;
  65. mem_malloc_end = dest_addr + CFG_MALLOC_LEN;
  66. mem_malloc_brk = mem_malloc_start;
  67. memset ((void *) mem_malloc_start, 0,
  68. mem_malloc_end - mem_malloc_start);
  69. }
  70. void *sbrk (ptrdiff_t increment)
  71. {
  72. ulong old = mem_malloc_brk;
  73. ulong new = old + increment;
  74. if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
  75. return (NULL);
  76. }
  77. mem_malloc_brk = new;
  78. return ((void *) old);
  79. }
  80. /************************************************************************
  81. * Init Utilities *
  82. ************************************************************************
  83. * Some of this code should be moved into the core functions,
  84. * or dropped completely,
  85. * but let's get it working (again) first...
  86. */
  87. static int init_baudrate (void)
  88. {
  89. DECLARE_GLOBAL_DATA_PTR;
  90. uchar tmp[64]; /* long enough for environment variables */
  91. int i = getenv_r ("baudrate", tmp, sizeof (tmp));
  92. gd->bd->bi_baudrate = gd->baudrate = (i > 0)
  93. ? (int) simple_strtoul (tmp, NULL, 10)
  94. : CONFIG_BAUDRATE;
  95. return (0);
  96. }
  97. static int display_banner (void)
  98. {
  99. printf ("\n\n%s\n\n", version_string);
  100. printf ("U-Boot code: %08lX -> %08lX BSS: -> %08lX\n",
  101. _armboot_start, _bss_start, _bss_end);
  102. #ifdef CONFIG_MODEM_SUPPORT
  103. puts ("Modem Support enabled\n");
  104. #endif
  105. #ifdef CONFIG_USE_IRQ
  106. printf ("IRQ Stack: %08lx\n", IRQ_STACK_START);
  107. printf ("FIQ Stack: %08lx\n", FIQ_STACK_START);
  108. #endif
  109. return (0);
  110. }
  111. /*
  112. * WARNING: this code looks "cleaner" than the PowerPC version, but
  113. * has the disadvantage that you either get nothing, or everything.
  114. * On PowerPC, you might see "DRAM: " before the system hangs - which
  115. * gives a simple yet clear indication which part of the
  116. * initialization if failing.
  117. */
  118. static int display_dram_config (void)
  119. {
  120. DECLARE_GLOBAL_DATA_PTR;
  121. int i;
  122. puts ("RAM Configuration:\n");
  123. for(i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
  124. printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  125. print_size (gd->bd->bi_dram[i].size, "\n");
  126. }
  127. return (0);
  128. }
  129. static void display_flash_config (ulong size)
  130. {
  131. puts ("Flash: ");
  132. print_size (size, "\n");
  133. }
  134. /*
  135. * Breathe some life into the board...
  136. *
  137. * Initialize a serial port as console, and carry out some hardware
  138. * tests.
  139. *
  140. * The first part of initialization is running from Flash memory;
  141. * its main purpose is to initialize the RAM so that we
  142. * can relocate the monitor code to RAM.
  143. */
  144. /*
  145. * All attempts to come up with a "common" initialization sequence
  146. * that works for all boards and architectures failed: some of the
  147. * requirements are just _too_ different. To get rid of the resulting
  148. * mess of board dependent #ifdef'ed code we now make the whole
  149. * initialization sequence configurable to the user.
  150. *
  151. * The requirements for any new initalization function is simple: it
  152. * receives a pointer to the "global data" structure as it's only
  153. * argument, and returns an integer return code, where 0 means
  154. * "continue" and != 0 means "fatal error, hang the system".
  155. */
  156. typedef int (init_fnc_t) (void);
  157. init_fnc_t *init_sequence[] = {
  158. cpu_init, /* basic cpu dependent setup */
  159. board_init, /* basic board dependent setup */
  160. interrupt_init, /* set up exceptions */
  161. env_init, /* initialize environment */
  162. init_baudrate, /* initialze baudrate settings */
  163. serial_init, /* serial communications setup */
  164. console_init_f, /* stage 1 init of console */
  165. display_banner, /* say that we are here */
  166. dram_init, /* configure available RAM banks */
  167. display_dram_config,
  168. #if defined(CONFIG_VCMA9)
  169. checkboard,
  170. #endif
  171. NULL,
  172. };
  173. void start_armboot (void)
  174. {
  175. DECLARE_GLOBAL_DATA_PTR;
  176. ulong size;
  177. init_fnc_t **init_fnc_ptr;
  178. char *s;
  179. #if defined(CONFIG_VFD)
  180. unsigned long addr;
  181. #endif
  182. /* Pointer is writable since we allocated a register for it */
  183. gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));
  184. memset ((void*)gd, 0, sizeof (gd_t));
  185. gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));
  186. memset (gd->bd, 0, sizeof (bd_t));
  187. monitor_flash_len = _bss_start - _armboot_start;
  188. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  189. if ((*init_fnc_ptr)() != 0) {
  190. hang ();
  191. }
  192. }
  193. /* configure available FLASH banks */
  194. size = flash_init ();
  195. display_flash_config (size);
  196. #ifdef CONFIG_VFD
  197. # ifndef PAGE_SIZE
  198. # define PAGE_SIZE 4096
  199. # endif
  200. /*
  201. * reserve memory for VFD display (always full pages)
  202. */
  203. /* armboot_end is defined in the board-specific linker script */
  204. addr = (_bss_start + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  205. size = vfd_setmem (addr);
  206. gd->fb_base = addr;
  207. #endif /* CONFIG_VFD */
  208. /* armboot_start is defined in the board-specific linker script */
  209. mem_malloc_init (_armboot_start - CFG_MALLOC_LEN);
  210. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  211. puts ("NAND:");
  212. nand_init(); /* go init the NAND */
  213. #endif
  214. #ifdef CONFIG_HAS_DATAFLASH
  215. AT91F_DataflashInit();
  216. dataflash_print_info();
  217. #endif
  218. /* initialize environment */
  219. env_relocate ();
  220. #ifdef CONFIG_VFD
  221. /* must do this after the framebuffer is allocated */
  222. drv_vfd_init();
  223. #endif /* CONFIG_VFD */
  224. /* IP Address */
  225. gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
  226. /* MAC Address */
  227. {
  228. int i;
  229. ulong reg;
  230. char *s, *e;
  231. uchar tmp[64];
  232. i = getenv_r ("ethaddr", tmp, sizeof (tmp));
  233. s = (i > 0) ? tmp : NULL;
  234. for (reg = 0; reg < 6; ++reg) {
  235. gd->bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  236. if (s)
  237. s = (*e) ? e + 1 : e;
  238. }
  239. }
  240. devices_init (); /* get the devices list going. */
  241. jumptable_init ();
  242. console_init_r (); /* fully init console as a device */
  243. #if defined(CONFIG_MISC_INIT_R)
  244. /* miscellaneous platform dependent initialisations */
  245. misc_init_r ();
  246. #endif
  247. /* enable exceptions */
  248. enable_interrupts ();
  249. /* Perform network card initialisation if necessary */
  250. #ifdef CONFIG_DRIVER_CS8900
  251. cs8900_get_enetaddr (gd->bd->bi_enetaddr);
  252. #endif
  253. #ifdef CONFIG_DRIVER_LAN91C96
  254. if (getenv ("ethaddr")) {
  255. smc_set_mac_addr(gd->bd->bi_enetaddr);
  256. }
  257. /* eth_hw_init(); */
  258. #endif /* CONFIG_DRIVER_LAN91C96 */
  259. /* Initialize from environment */
  260. if ((s = getenv ("loadaddr")) != NULL) {
  261. load_addr = simple_strtoul (s, NULL, 16);
  262. }
  263. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  264. if ((s = getenv ("bootfile")) != NULL) {
  265. copy_filename (BootFile, s, sizeof (BootFile));
  266. }
  267. #endif /* CFG_CMD_NET */
  268. #ifdef BOARD_LATE_INIT
  269. board_late_init ();
  270. #endif
  271. /* main_loop() can return to retry autoboot, if so just run it again. */
  272. for (;;) {
  273. main_loop ();
  274. }
  275. /* NOTREACHED - no way out of command loop except booting */
  276. }
  277. void hang (void)
  278. {
  279. puts ("### ERROR ### Please RESET the board ###\n");
  280. for (;;);
  281. }
  282. #ifdef CONFIG_MODEM_SUPPORT
  283. /* called from main loop (common/main.c) */
  284. extern void dbg(const char *fmt, ...);
  285. int mdm_init (void)
  286. {
  287. char env_str[16];
  288. char *init_str;
  289. int i;
  290. extern char console_buffer[];
  291. static inline void mdm_readline(char *buf, int bufsiz);
  292. extern void enable_putc(void);
  293. extern int hwflow_onoff(int);
  294. enable_putc(); /* enable serial_putc() */
  295. #ifdef CONFIG_HWFLOW
  296. init_str = getenv("mdm_flow_control");
  297. if (init_str && (strcmp(init_str, "rts/cts") == 0))
  298. hwflow_onoff (1);
  299. else
  300. hwflow_onoff(-1);
  301. #endif
  302. for (i = 1;;i++) {
  303. sprintf(env_str, "mdm_init%d", i);
  304. if ((init_str = getenv(env_str)) != NULL) {
  305. serial_puts(init_str);
  306. serial_puts("\n");
  307. for(;;) {
  308. mdm_readline(console_buffer, CFG_CBSIZE);
  309. dbg("ini%d: [%s]", i, console_buffer);
  310. if ((strcmp(console_buffer, "OK") == 0) ||
  311. (strcmp(console_buffer, "ERROR") == 0)) {
  312. dbg("ini%d: cmd done", i);
  313. break;
  314. } else /* in case we are originating call ... */
  315. if (strncmp(console_buffer, "CONNECT", 7) == 0) {
  316. dbg("ini%d: connect", i);
  317. return 0;
  318. }
  319. }
  320. } else
  321. break; /* no init string - stop modem init */
  322. udelay(100000);
  323. }
  324. udelay(100000);
  325. /* final stage - wait for connect */
  326. for(;i > 1;) { /* if 'i' > 1 - wait for connection
  327. message from modem */
  328. mdm_readline(console_buffer, CFG_CBSIZE);
  329. dbg("ini_f: [%s]", console_buffer);
  330. if (strncmp(console_buffer, "CONNECT", 7) == 0) {
  331. dbg("ini_f: connected");
  332. return 0;
  333. }
  334. }
  335. return 0;
  336. }
  337. /* 'inline' - We have to do it fast */
  338. static inline void mdm_readline(char *buf, int bufsiz)
  339. {
  340. char c;
  341. char *p;
  342. int n;
  343. n = 0;
  344. p = buf;
  345. for(;;) {
  346. c = serial_getc();
  347. /* dbg("(%c)", c); */
  348. switch(c) {
  349. case '\r':
  350. break;
  351. case '\n':
  352. *p = '\0';
  353. return;
  354. default:
  355. if(n++ > bufsiz) {
  356. *p = '\0';
  357. return; /* sanity check */
  358. }
  359. *p = c;
  360. p++;
  361. break;
  362. }
  363. }
  364. }
  365. #endif /* CONFIG_MODEM_SUPPORT */