allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / cfe / cfe / main / cfe_main.c
blobc5fdfe1fd392fd021865391c3b82c21ad60599d2
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * Main Module File: cfe_main.c
5 *
6 * This module contains the main "C" routine for CFE and
7 * the main processing loop. There should not be any board-specific
8 * stuff in here.
9 *
10 * Author: Mitch Lichtenberg (mpl@broadcom.com)
12 *********************************************************************
14 * Copyright 2000,2001,2002,2003
15 * Broadcom Corporation. All rights reserved.
17 * This software is furnished under license and may be used and
18 * copied only in accordance with the following terms and
19 * conditions. Subject to these conditions, you may download,
20 * copy, install, use, modify and distribute modified or unmodified
21 * copies of this software in source and/or binary form. No title
22 * or ownership is transferred hereby.
24 * 1) Any source code used, modified or distributed must reproduce
25 * and retain this copyright notice and list of conditions
26 * as they appear in the source file.
28 * 2) No right is granted to use any trade name, trademark, or
29 * logo of Broadcom Corporation. The "Broadcom Corporation"
30 * name may not be used to endorse or promote products derived
31 * from this software without the prior written permission of
32 * Broadcom Corporation.
34 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
35 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
36 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
38 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
39 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
42 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
44 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
45 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
46 * THE POSSIBILITY OF SUCH DAMAGE.
47 ********************************************************************* */
50 #include "lib_types.h"
51 #include "lib_string.h"
52 #include "lib_malloc.h"
53 #include "lib_printf.h"
55 #include "cfe_iocb.h"
56 #include "cfe_device.h"
57 #include "cfe_console.h"
58 #include "cfe_timer.h"
60 #include "env_subr.h"
61 #include "ui_command.h"
62 #include "cfe_mem.h"
63 #include "cfe.h"
65 #include "exception.h"
67 #include "bsp_config.h"
69 #include "segtable.h"
71 #include "initdata.h"
73 #if CFG_PCI
74 #include "pcivar.h"
75 #endif
77 #include "epivers.h"
79 /* *********************************************************************
80 * Constants
81 ********************************************************************* */
83 #ifndef CFG_STACK_SIZE
84 #define STACK_SIZE 8192
85 #else
86 #define STACK_SIZE ((CFG_STACK_SIZE+1023) & ~1023)
87 #endif
89 /* *********************************************************************
90 * Externs
91 ********************************************************************* */
93 void cfe_main(int,int);
94 extern void cfe_device_poll(void *x);
96 extern int ui_init_envcmds(void);
97 extern int ui_init_devcmds(void);
98 extern int ui_init_netcmds(void);
99 extern int ui_init_memcmds(void);
100 extern int ui_init_loadcmds(void);
101 extern int ui_init_pcicmds(void);
102 extern int ui_init_examcmds(void);
103 extern int ui_init_flashcmds(void);
104 extern int ui_init_misccmds(void);
105 #if CFG_VAPI
106 extern int ui_init_vapicmds(void);
107 #endif
109 #if CFG_VENDOR_EXTENSIONS
110 extern int ui_init_vendorcmds(void);
111 #endif
113 void cfe_command_restart(uint64_t status);
115 extern segtable_t *_getsegtbl(void);
117 extern const char *builddate;
118 extern const char *builduser;
120 #if CFG_MULTI_CPUS
121 extern int altcpu_cmd_start(uint64_t,uint64_t *);
122 extern int altcpu_cmd_stop(uint64_t);
123 #endif
126 /* *********************************************************************
127 * Globals
128 ********************************************************************* */
130 const char *cfe_boardname = CFG_BOARDNAME;
131 unsigned int cfe_startflags =
132 #if CFG_PCI
133 CFE_INIT_PCI |
134 #endif
137 /* *********************************************************************
138 * cfe_setup_default_env()
140 * Initialize the default environment for CFE. These are all
141 * the temporary variables that do not get stored in the NVRAM
142 * but are available to other programs and command-line macros.
144 * Input parameters:
145 * nothing
147 * Return value:
148 * nothing
149 ********************************************************************* */
151 static void cfe_setup_default_env(void)
153 char buffer[80];
155 xsprintf(buffer,"%d.%d.%d",CFE_VER_MAJOR,CFE_VER_MINOR,CFE_VER_BUILD);
156 env_setenv("CFE_VERSION",buffer,ENV_FLG_BUILTIN | ENV_FLG_READONLY);
158 if (cfe_boardname) {
159 env_setenv("CFE_BOARDNAME",(char *) cfe_boardname,
160 ENV_FLG_BUILTIN | ENV_FLG_READONLY);
163 xsprintf(buffer,"%d",mem_totalsize);
164 env_setenv("CFE_MEMORYSIZE",buffer,ENV_FLG_BUILTIN | ENV_FLG_READONLY);
169 /* *********************************************************************
170 * cfe_init_ui()
172 * Call all the other UI initialization modules. Each of these
173 * modules usually calls back to the UI dispatcher to add command
174 * tables.
176 * Input parameters:
177 * nothing
179 * Return value:
180 * nothing
181 ********************************************************************* */
183 #if CFG_MINIMAL_SIZE
184 #define OPTIONAL(x)
185 #else
186 #define OPTIONAL(x) x
187 #endif
189 static void cfe_init_ui(void)
191 ui_init_cmddisp();
193 OPTIONAL(ui_init_envcmds());
194 OPTIONAL(ui_init_devcmds());
195 #if CFG_NETWORK
196 ui_init_netcmds();
197 #endif
198 ui_init_loadcmds();
199 OPTIONAL(ui_init_memcmds());
201 #if CFG_PCI
202 ui_init_pcicmds();
203 #endif
204 OPTIONAL(ui_init_examcmds());
205 ui_init_flashcmds();
206 #if CFG_VAPI
207 ui_init_vapicmds();
208 #endif
210 #if CFG_VENDOR_EXTENSIONS
211 ui_init_vendorcmds();
212 #endif
214 OPTIONAL(ui_init_misccmds());
219 /* *********************************************************************
220 * cfe_ledstr(leds)
222 * Display a string on the board's LED display, if it has one.
223 * This routine depends on the board-support package to
224 * include a "driver" to write to the actual LED, if the board
225 * does not have one this routine will do nothing.
227 * The LEDs are written at various places in the initialization
228 * sequence, to debug board problems.
230 * Input parameters:
231 * leds - pointer to four-character ASCII string
233 * Return value:
234 * nothing
235 ********************************************************************* */
237 void cfe_ledstr(const char *leds)
239 unsigned int val;
241 val = ((((unsigned int) leds[0]) << 24) |
242 (((unsigned int) leds[1]) << 16) |
243 (((unsigned int) leds[2]) << 8) |
244 ((unsigned int) leds[3]));
246 cfe_leds(val);
250 /* *********************************************************************
251 * cfe_say_hello()
253 * Print out the CFE startup message and copyright notice
255 * Input parameters:
256 * nothing
258 * Return value:
259 * nothing
260 ********************************************************************* */
263 static void cfe_say_hello(void)
265 xprintf("\n\n");
266 xprintf("CFE version %s based on BBP %d.%d.%d"
267 #ifdef CFE_VER_RELEASE
268 ".%d"
269 #endif
270 " for %s (%s)\n", EPI_VERSION_STR,
271 CFE_VER_MAJOR,CFE_VER_MINOR,CFE_VER_BUILD,
272 #ifdef CFE_VER_RELEASE
273 CFE_VER_RELEASE,
274 #endif
275 cfe_boardname,
276 #ifdef __long64
277 "64bit,"
278 #else
279 "32bit,"
280 #endif
281 #if CFG_MULTI_CPUS
282 "MP,"
283 #else
284 "SP,"
285 #endif
286 #ifdef __MIPSEL
287 "LE"
288 #endif
289 #ifdef __MIPSEB
290 "BE"
291 #endif
292 #if CFG_VAPI
293 ",VAPI"
294 #endif
297 xprintf("Build Date: %s (%s)\n",builddate,builduser);
298 xprintf("Copyright (C) 2000-2008 Broadcom Corporation.\n");
299 xprintf("\n");
303 /* *********************************************************************
304 * cfe_restart()
306 * Restart CFE from scratch, jumping back to the boot vector.
308 * Input parameters:
309 * nothing
311 * Return value:
312 * does not return
313 ********************************************************************* */
315 void cfe_restart(void)
317 _exc_restart();
321 /* *********************************************************************
322 * cfe_start(ept)
324 * Start a user program
326 * Input parameters:
327 * ept - entry point
329 * Return value:
330 * nothing
331 ********************************************************************* */
332 void cfe_start(unsigned long ept)
334 SETLEDS("RUN!");
335 cfe_launch(ept);
339 /* *********************************************************************
340 * cfe_startup_info()
342 * Display startup memory configuration messages
344 * Input parameters:
345 * nothing
347 * Return value:
348 * nothing
349 ********************************************************************* */
351 static void cfe_startup_info(void)
353 segtable_t *segtbl;
355 segtbl = _getsegtbl();
356 xprintf("CPU type 0x%X: ",(uint32_t)cpu_prid);
357 if (cfe_cpu_speed < 1000000) xprintf("%dKHz\n",cfe_cpu_speed/1000);
358 else xprintf("%dMHz\n",cfe_cpu_speed/1000000);
359 xprintf("Tot mem: %u KBytes\n", mem_totalsize);
361 xprintf("\n");
362 xprintf("CFE mem: 0x%08X - 0x%08X (%d)\n",
363 (uint32_t) mem_bottomofmem,
364 (uint32_t) mem_topofmem,
365 (uint32_t) mem_topofmem-mem_bottomofmem);
366 xprintf("Data: 0x%08X - 0x%08X (%d)\n",
367 (uint32_t) (segtbl[R_SEG_FDATA] + mem_datareloc),
368 (uint32_t) (segtbl[R_SEG_EDATA] + mem_datareloc),
369 (uint32_t) (segtbl[R_SEG_EDATA] - segtbl[R_SEG_FDATA]));
370 xprintf("BSS: 0x%08X - 0x%08X (%d)\n",
371 (uint32_t) (segtbl[R_SEG_FBSS] + mem_datareloc),
372 (uint32_t) (segtbl[R_SEG_END] + mem_datareloc),
373 (uint32_t) (segtbl[R_SEG_END] - segtbl[R_SEG_FBSS]));
374 xprintf("Heap: 0x%08X - 0x%08X (%d)\n",
375 (uint32_t)(mem_heapstart),
376 (uint32_t)(mem_heapstart + (CFG_HEAP_SIZE*1024)),
377 (CFG_HEAP_SIZE*1024));
378 xprintf("Stack: 0x%08X - 0x%08X (%d)\n",
379 (uint32_t)(mem_heapstart + (CFG_HEAP_SIZE*1024)),
380 (uint32_t)(mem_heapstart + (CFG_HEAP_SIZE*1024) + STACK_SIZE),
381 STACK_SIZE);
382 xprintf("Text: 0x%08X - 0x%08X (%d)\n",
383 (uint32_t)mem_textbase,
384 (uint32_t)(mem_textbase+mem_textsize),
385 (uint32_t) mem_textsize);
386 #if !CFG_MINIMAL_SIZE
387 xprintf("Boot: 0x%08X - 0x%08X\n",
388 mem_bootarea_start,mem_bootarea_start+mem_bootarea_size);
389 xprintf("Reloc: I:%08X - D:%08X\n",
390 (uint32_t) mem_textreloc,(uint32_t) mem_datareloc);
391 #endif
395 /* *********************************************************************
396 * cfe_autostart()
398 * Process automatic commands at startup
400 * Input parameters:
401 * nothing
403 * Return value:
404 * nothing
405 ********************************************************************* */
407 static void cfe_autostart(void)
409 char *env;
410 int noauto = 0;
411 char ch = 0;
413 env = env_getenv("STARTUP");
414 if (!env) return;
416 while (console_status()) {
417 console_read(&ch,1);
418 if (ch == 3) noauto = TRUE; /* Ctrl-C means no auto */
421 if (noauto) {
422 xprintf("Startup canceled\n");
423 return;
426 ui_docommands(env);
429 /* *********************************************************************
430 * cfe_main(a,b)
432 * It's gotta start somewhere.
434 * Input parameters:
435 * a,b - not used
437 * Return value:
438 * does not return
439 ********************************************************************* */
441 void cfe_main(int a,int b)
445 * By the time this routine is called, the following things have
446 * already been done:
448 * 1. The processor(s) is(are) initialized.
449 * 2. The caches are initialized.
450 * 3. The memory controller is initialized.
451 * 4. BSS has been zeroed.
452 * 5. The data has been moved to R/W space.
453 * 6. The "C" Stack has been initialized.
456 cfe_bg_init(); /* init background processing */
457 cfe_attach_init();
458 cfe_timer_init(); /* Timer process */
459 cfe_bg_add(cfe_device_poll,NULL);
462 * Initialize the memory allocator
465 SETLEDS("KMEM");
466 KMEMINIT((unsigned char *) (uintptr_t) mem_heapstart,
467 ((CFG_HEAP_SIZE)*1024));
470 * Initialize the console. It is done before the other devices
471 * get turned on. The console init also sets the variable that
472 * contains the CPU speed.
475 SETLEDS("CONS");
476 board_console_init();
479 * Set up the exception vectors
482 cfe_setup_exceptions();
485 * Say hello.
488 SETLEDS("CIOK");
489 cfe_say_hello();
492 * Initialize the other devices.
495 SETLEDS("AREN");
496 #if CFG_XIP
497 xprintf("CFE XIP\n");
498 #endif
499 xprintf("Init Arena\n");
500 cfe_arena_init();
502 #if CFG_PCI
503 if (cfe_startflags & CFE_INIT_PCI) {
504 pci_flags_t flags = PCI_FLG_NORMAL | PCI_FLG_LDT_PREFETCH;
505 #ifndef EB332
506 char *str;
507 extern cons_t pci_optnames[];
509 flags = PCI_FLG_NORMAL | PCI_FLG_LDT_PREFETCH;
510 #if CFG_LDT_REV_017
511 flags |= PCI_FLG_LDT_REV_017;
512 #endif
513 str = env_getenv("PCI_OPTIONS");
514 setoptions(pci_optnames,str,&flags);
516 xprintf("Initializing PCI. [%s]\n",str ? str : "normal");
517 #endif
518 pci_configure(flags);
520 #endif
522 SETLEDS("DEVI");
523 xprintf("Init Devs.\n");
524 board_device_init();
526 cfe_startup_info();
527 SETLEDS("ENVI");
528 cfe_setup_default_env();
530 xprintf("\n");
532 cfe_init_ui();
534 board_final_init();
536 cfe_autostart();
538 cfe_command_loop();
541 /* *********************************************************************
542 * cfe_command_restart()
544 * This routine is called when an application wants to restart
545 * the firmware's command processor. Reopen the console and
546 * jump back into the command loop.
548 * Input parameters:
549 * status - A0 value of program returning to firmware
551 * Return value:
552 * does not return
553 ********************************************************************* */
555 void cfe_command_restart(uint64_t status)
558 * Stop alternate CPU(s). If they're already stopped, this
559 * command will make sure they're stopped.
562 #if CFG_MULTI_CPUS
563 altcpu_cmd_stop(1);
564 #endif
567 * Call board reset functions
569 board_device_reset();
572 * Reset devices
574 cfe_device_reset();
577 * Reset timers
579 cfe_timer_init();
582 * Reopen console
584 console_open(console_name);
587 * Display program return status
590 xprintf("*exit status = %d\n", (int)status);
593 * Back to processing user commands
595 cfe_command_loop();
600 /* *********************************************************************
601 * cfe_command_loop()
603 * This routine reads and processes user commands
605 * Input parameters:
606 * nothing
608 * Return value:
609 * does not return
610 ********************************************************************* */
612 void cfe_command_loop()
614 char buffer[300];
615 int status;
616 char *prompt;
618 SETLEDS("CFE ");
620 for (;;) {
621 prompt = env_getenv("PROMPT");
622 #if CFG_RAMAPP
623 SETLEDS("CFE*");
624 if (!prompt) prompt = "CFE_RAM> ";
625 #else
626 if (!prompt) prompt = "CFE> ";
627 #endif
628 console_readline(prompt,buffer,sizeof(buffer));
630 status = ui_docommands(buffer);
632 if (status != CMD_ERR_BLANK) {
633 xprintf("*** command status = %d\n", status);