summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/main/cfe_main.c
blob: 1a451a67bd457a99404108bc13fb07185cedfbb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Main Module				File: cfe_main.c       
    *  
    *  This module contains the main "C" routine for CFE and 
    *  the main processing loop.  There should not be any board-specific
    *  stuff in here.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_string.h"
#include "lib_malloc.h"
#include "lib_printf.h"

#include "cfe_iocb.h"
#include "cfe_device.h"
#include "cfe_console.h"
#include "cfe_timer.h"

#include "env_subr.h"
#include "ui_command.h"
#include "cfe_mem.h"
#include "cfe.h"

#include "exception.h"

#include "bsp_config.h"

#include "segtable.h"

#include "initdata.h"

#if CFG_PCI
#include "pcivar.h"
#endif

/*  *********************************************************************
    *  Constants
    ********************************************************************* */

#ifndef CFG_STACK_SIZE
#define STACK_SIZE	8192
#else
#define STACK_SIZE	((CFG_STACK_SIZE+1023) & ~1023)
#endif

/*  *********************************************************************
    *  Externs
    ********************************************************************* */

void cfe_main(int,int);
extern void cfe_device_poll(void *x);

extern int ui_init_envcmds(void);
extern int ui_init_devcmds(void);
extern int ui_init_netcmds(void);
extern int ui_init_memcmds(void);
extern int ui_init_loadcmds(void);
extern int ui_init_pcicmds(void);
extern int ui_init_examcmds(void);
extern int ui_init_flashcmds(void);
extern int ui_init_misccmds(void);
#if CFG_VAPI
extern int ui_init_vapicmds(void);
#endif

#if CFG_VENDOR_EXTENSIONS
extern int ui_init_vendorcmds(void);
#endif

void cfe_command_restart(uint64_t status);

extern segtable_t *_getsegtbl(void);

extern const char *builddate;
extern const char *builduser;

#if CFG_MULTI_CPUS
extern int altcpu_cmd_start(uint64_t,uint64_t *);
extern int altcpu_cmd_stop(uint64_t);
#endif




/*  *********************************************************************
    *  Globals
    ********************************************************************* */

const char *cfe_boardname = CFG_BOARDNAME;
unsigned int cfe_startflags =
#if CFG_PCI
    CFE_INIT_PCI |
#endif
    0;

/*  *********************************************************************
    *  cfe_setup_default_env()
    *  
    *  Initialize the default environment for CFE.  These are all
    *  the temporary variables that do not get stored in the NVRAM
    *  but are available to other programs and command-line macros.
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void cfe_setup_default_env(void)
{
    char buffer[80];

    xsprintf(buffer,"%d.%d.%d",CFE_VER_MAJOR,CFE_VER_MINOR,CFE_VER_BUILD);
    env_setenv("CFE_VERSION",buffer,ENV_FLG_BUILTIN | ENV_FLG_READONLY);

    if (cfe_boardname) {
	env_setenv("CFE_BOARDNAME",(char *) cfe_boardname,
		   ENV_FLG_BUILTIN | ENV_FLG_READONLY);
	}

    xsprintf(buffer,"%d",mem_totalsize);
    env_setenv("CFE_MEMORYSIZE",buffer,ENV_FLG_BUILTIN | ENV_FLG_READONLY);

}


/*  *********************************************************************
    *  cfe_init_ui()
    *  
    *  Call all the other UI initialization modules.  Each of these
    *  modules usually calls back to the UI dispatcher to add command
    *  tables.
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

#if CFG_MINIMAL_SIZE
#define OPTIONAL(x)
#else 
#define OPTIONAL(x) x
#endif

static void cfe_init_ui(void)
{
    ui_init_cmddisp();

    ui_init_envcmds();
    OPTIONAL(ui_init_devcmds());
#if CFG_NETWORK
    ui_init_netcmds();
#endif
    ui_init_loadcmds();
    OPTIONAL(ui_init_memcmds());

#if CFG_PCI
    ui_init_pcicmds();
#endif
    OPTIONAL(ui_init_examcmds());
    ui_init_flashcmds();
#if CFG_VAPI
    ui_init_vapicmds();
#endif

#if CFG_VENDOR_EXTENSIONS
    ui_init_vendorcmds();
#endif

    OPTIONAL(ui_init_misccmds());

}


/*  *********************************************************************
    *  cfe_ledstr(leds)
    *  
    *  Display a string on the board's LED display, if it has one.
    *  This routine depends on the board-support package to
    *  include a "driver" to write to the actual LED, if the board
    *  does not have one this routine will do nothing.
    *  
    *  The LEDs are written at various places in the initialization
    *  sequence, to debug board problems.
    *  
    *  Input parameters: 
    *  	   leds - pointer to four-character ASCII string
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void cfe_ledstr(const char *leds)
{
    unsigned int val;

    val = ((((unsigned int) leds[0]) << 24) |
	   (((unsigned int) leds[1]) << 16) |
	   (((unsigned int) leds[2]) << 8) |
	   ((unsigned int) leds[3]));

    cfe_leds(val);
}


/*  *********************************************************************
    *  cfe_say_hello()
    *  
    *  Print out the CFE startup message and copyright notice
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */


static void cfe_say_hello(void)
{
    xprintf("\n\n");
    xprintf("CFE version 2.0.2"
#ifdef CFE_VER_RELEASE
	    ".%d"
#endif
	    " for DGN2200v2 (%s)\n",
	    //CFE_VER_MAJOR,CFE_VER_MINOR,CFE_VER_BUILD,
#ifdef CFE_VER_RELEASE
	    CFE_VER_RELEASE,
#endif
	    //cfe_boardname,
#ifdef __long64
	    "64bit,"
#else
	    "32bit,"
#endif	
#if CFG_MULTI_CPUS
	    "MP,"
#else
	    "SP,"
#endif
#ifdef __MIPSEL
	    "LE"
#endif
#ifdef __MIPSEB
	    "BE"
#endif
#if CFG_VAPI
	    ",VAPI"
#endif
	);

    xprintf("Build Date: %s (%s)\n",builddate,builduser);
    xprintf("Copyright (C) 2000,2001,2002,2003 Broadcom Corporation.\n");
    xprintf("\n");
}


/*  *********************************************************************
    *  cfe_restart()
    *  
    *  Restart CFE from scratch, jumping back to the boot vector.
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   does not return
    ********************************************************************* */

void cfe_restart(void)
{
    _exc_restart();
}


/*  *********************************************************************
    *  cfe_start(ept)
    *  
    *  Start a user program
    *  
    *  Input parameters: 
    *  	   ept - entry point
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */
void cfe_start(unsigned long ept)
{
    SETLEDS("RUN!");
    cfe_launch(ept);
}


/*  *********************************************************************
    *  cfe_startup_info()
    *  
    *  Display startup memory configuration messages
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void cfe_startup_info(void)
{
    segtable_t *segtbl;

    segtbl = _getsegtbl();
    xprintf("CPU type 0x%X: ",(uint32_t)cpu_prid);
    if (cfe_cpu_speed < 1000000) xprintf("%dKHz\n",cfe_cpu_speed/1000);
    else xprintf("%dMHz\n",cfe_cpu_speed/1000000);
    xprintf("Total memory: 0x%llX bytes (%dMB)\n",
	    (((uint64_t)mem_totalsize) << 20),(uint32_t)mem_totalsize);

    xprintf("\n");
    xprintf("Total memory used by CFE:  0x%08X - 0x%08X (%d)\n",
	    (uint32_t) mem_bottomofmem,
	    (uint32_t) mem_topofmem,
	    (uint32_t) mem_topofmem-mem_bottomofmem);
    xprintf("Initialized Data:          0x%08X - 0x%08X (%d)\n",
	    (uint32_t) (segtbl[R_SEG_FDATA] + mem_datareloc),
	    (uint32_t) (segtbl[R_SEG_EDATA] + mem_datareloc),
	    (uint32_t) (segtbl[R_SEG_EDATA] - segtbl[R_SEG_FDATA]));
    xprintf("BSS Area:                  0x%08X - 0x%08X (%d)\n",
	    (uint32_t) (segtbl[R_SEG_FBSS] + mem_datareloc),
	    (uint32_t) (segtbl[R_SEG_END] + mem_datareloc),
	    (uint32_t) (segtbl[R_SEG_END] - segtbl[R_SEG_FBSS]));
    xprintf("Local Heap:                0x%08X - 0x%08X (%d)\n",
	    (uint32_t)(mem_heapstart),
	    (uint32_t)(mem_heapstart + (CFG_HEAP_SIZE*1024)),
	    (CFG_HEAP_SIZE*1024));
    xprintf("Stack Area:                0x%08X - 0x%08X (%d)\n",
	    (uint32_t)(mem_heapstart + (CFG_HEAP_SIZE*1024)),
	    (uint32_t)(mem_heapstart + (CFG_HEAP_SIZE*1024) + STACK_SIZE),
	    STACK_SIZE);
    xprintf("Text (code) segment:       0x%08X - 0x%08X (%d)\n",
	    (uint32_t)mem_textbase,
	    (uint32_t)(mem_textbase+mem_textsize),
	    (uint32_t) mem_textsize);
    xprintf("Boot area (physical):      0x%08X - 0x%08X\n",
	    mem_bootarea_start,mem_bootarea_start+mem_bootarea_size);
    xprintf("Relocation Factor:         I:%08X - D:%08X\n",
	    (uint32_t) mem_textreloc,(uint32_t) mem_datareloc);

}


/*  *********************************************************************
    *  cfe_autostart()
    *  
    *  Process automatic commands at startup
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

static void cfe_autostart(void)
{
    char *env;
    int noauto = 0;
    char ch;

    env = env_getenv("STARTUP");
    if (!env) return;

    while (console_status()) {
	console_read(&ch,1);
	if (ch == 3) noauto = TRUE;	/* Ctrl-C means no auto */
	}

    if (noauto) {
	xprintf("Automatic startup canceled via Ctrl-C\n");
	return;
	}

    ui_docommands(env);
}

/*  *********************************************************************
    *  cfe_main(a,b)
    *  
    *  It's gotta start somewhere.
    *  
    *  Input parameters: 
    *  	   a,b - not used
    *  	   
    *  Return value:
    *  	   does not return
    ********************************************************************* */

void cfe_main(int a,int b)
{

    /*
     * By the time this routine is called, the following things have
     * already been done:
     *
     * 1. The processor(s) is(are) initialized.
     * 2. The caches are initialized.
     * 3. The memory controller is initialized.
     * 4. BSS has been zeroed.
     * 5. The data has been moved to R/W space.
     * 6. The "C" Stack has been initialized.
     */

    cfe_bg_init();		       		/* init background processing */
    cfe_attach_init();
    cfe_timer_init();				/* Timer process */
    cfe_bg_add(cfe_device_poll,NULL);

    /*
     * Initialize the memory allocator
     */

    SETLEDS("KMEM");
    KMEMINIT((unsigned char *) (uintptr_t) mem_heapstart,
	     ((CFG_HEAP_SIZE)*1024));

    /*
     * Initialize the console.  It is done before the other devices
     * get turned on.  The console init also sets the variable that
     * contains the CPU speed.
     */

    SETLEDS("CONS");
    board_console_init();

    /*
     * Set up the exception vectors
     */

    cfe_setup_exceptions();

    /* 
     * Say hello.
     */

    SETLEDS("CIOK");
    cfe_say_hello();

    /*
     * Initialize the other devices.
     */

    SETLEDS("AREN");
    xprintf("Initializing Arena.\n");
    cfe_arena_init();

#if CFG_PCI
    if (cfe_startflags & CFE_INIT_PCI) {
	pci_flags_t flags = PCI_FLG_NORMAL | PCI_FLG_LDT_PREFETCH;
	char *str;
	extern cons_t pci_optnames[];

	flags = PCI_FLG_NORMAL | PCI_FLG_LDT_PREFETCH;
#if CFG_LDT_REV_017
	flags |= PCI_FLG_LDT_REV_017;
#endif
	str = env_getenv("PCI_OPTIONS");
	setoptions(pci_optnames,str,&flags);
	
	xprintf("Initializing PCI. [%s]\n",str ? str : "normal");
	pci_configure(flags);
	}
#endif

    SETLEDS("DEVI");
    xprintf("Initializing Devices.\n");
    board_device_init();

    cfe_startup_info();
    SETLEDS("ENVI");
    cfe_setup_default_env();

    xprintf("\n");

    cfe_init_ui();

    board_final_init();

    cfe_autostart();

    cfe_command_loop();
}

/*  *********************************************************************
    *  cfe_command_restart()
    *  
    *  This routine is called when an application wants to restart
    *  the firmware's command processor.  Reopen the console and
    *  jump back into the command loop.
    *  
    *  Input parameters: 
    *  	   status - A0 value of program returning to firmware
    *  	   
    *  Return value:
    *  	   does not return
    ********************************************************************* */

void cfe_command_restart(uint64_t status)
{
    /*
     * Stop alternate CPU(s).  If they're already stopped, this
     * command will make sure they're stopped.
     */

#if CFG_MULTI_CPUS
    altcpu_cmd_stop(1);			/* stop CPU 1 (XXX more CPUs?) */
#endif

    /*
     * Call board reset functions
     */
    board_device_reset();

    /*
     * Reset devices
     */
    cfe_device_reset();

    /*
     * Reset timers
     */
    cfe_timer_init();

    /*
     * Reopen console 
     */
    console_open(console_name);

    /*
     * Display program return status
     */

    xprintf("*** program exit status = %d\n", (int)status);

    /*
     * Back to processing user commands
     */
    cfe_command_loop();
}



/*  *********************************************************************
    *  cfe_command_loop()
    *  
    *  This routine reads and processes user commands
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   does not return
    ********************************************************************* */

void cfe_command_loop()
{
    char buffer[300];
    int status;
    char *prompt;

    SETLEDS("CFE ");

    for (;;) {
	prompt = env_getenv("PROMPT");
#if CFG_RAMAPP
	SETLEDS("CFE*");
	if (!prompt) prompt = "CFE_RAM> ";
#else
	if (!prompt) prompt = "CFE> ";
#endif
	console_readline(prompt,buffer,sizeof(buffer));

	status = ui_docommands(buffer);

	if (status != CMD_ERR_BLANK) {
	    xprintf("*** command status = %d\n", status);
	    }
	}
}