summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/arch/mips/board/bcm63xx_ram/src/bcm63xx_main.c
blob: 2cf2546436a2c7e538412f33517541e3508b4408 (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
/*  *********************************************************************
    *  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 "cfe_mem.h"
#include "cfe.h"

#include "exception.h"

#include "bsp_config.h"
#include "bcm_hwdefs.h"
#include "boardparms.h"
#include "bcm_map.h"
#include "bcm_cpu.h"
#include "bcm63xx_util.h"

#include "segtable.h"
#include "initdata.h"

/*  *********************************************************************
    *  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);
void cfe_command_restart(uint64_t status);

extern void cfe_device_poll(void *x);

extern int cfe_web_check(void);
extern void cfe_web_fg_process(void);
extern void cfe_web_poll(void *x);

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

/* Foxconn add start by Cliff Wang, 03/23/2010 */
extern int ui_init_netcmds(void);
/* Foxconn add end by Cliff Wang, 03/23/2010 */

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

const char *cfe_boardname = CFG_BOARDNAME;
unsigned int cfe_startflags = 0;
#if defined (_BCM96368_)
static int cfe_bus_speed = 0;
static int cfe_ref_speed = 0;
#endif
#if defined (_BCM96328_) || defined (_BCM96362_) || defined (_BCM96816_)
static int cfe_bus_speed = 0;
static int cfe_ddr_speed = 0;
#endif
unsigned long cfe_sdramsize = 8 * 1024 * 1024;

static void calculateCpuSpeed(void);

#if defined (_BCM96328_)
const uint32 cpu_speed_table[0x20] = {
    320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320, 320,
    0, 320, 160, 200, 160, 200, 400, 320, 320, 160, 384, 320, 192, 320, 320, 320
};
const uint32 ddr_speed_table[0x20] = {
    160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
    0, 80, 100, 100, 160, 200, 400, 400, 80, 160, 160, 160, 192, 192, 240, 320
};
const uint32 bus_speed_table[0x20] = {
    160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
    0, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160
};
#endif

#if defined (_BCM96362_)
const uint32 cpu_speed_table[0x20] = {
    320, 320, 320, 240, 160, 400, 440, 384, 320, 320, 320, 240, 160, 320, 400, 320,
    320, 320, 320, 240, 160, 200, 400, 384, 320, 320, 320, 240, 160, 200, 400, 400
};
const uint32 ddr_speed_table[0x20] = {
    320, 400, 192, 320, 200, 400, 367, 320, 320, 400, 192, 320, 200, 400, 400, 320,
    320, 400, 192, 320, 200, 267, 400, 320, 320, 320, 192, 320, 200, 200, 400, 333
};
const uint32 bus_speed_table[0x20] = {
    160, 200, 96, 160, 100, 160, 183, 160, 160, 200, 96, 160, 100, 160, 200, 160,
    160, 200, 96, 160, 100, 100, 200, 160, 160, 160, 96, 160, 100, 100, 200, 166
};
#endif

#if defined (_BCM96816_)
const uint32 cpu_speed_table[0x20] = {
    200, 400, 400, 320, 200, 400, 333, 333, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 400, 400, 200, 360, 400, 400, 300, 300, 320, 320, 400, 400
};
const uint32 ddr_speed_table[0x20] = {
    200, 333, 200, 200, 200, 333, 333, 333, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 400, 200, 200, 300, 300, 300, 300, 300, 400, 400, 400, 400
};
const uint32 bus_speed_table[0x20] = {
    200, 200, 200, 160, 100, 167, 200, 167, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 200, 200, 160, 180, 171, 200, 171, 200, 160, 200, 160, 200
};
#endif

static unsigned long cfe_get_sdram_size(void);

/*  *********************************************************************
    *  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);
    }
}


/*  *********************************************************************
    *  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)
{
#if 0
    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);
#endif
}


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

void cfe_command_restart(uint64_t status)
{
}

static void cfe_say_hello(void)
{
    xprintf("\n\n");
    xprintf("CFE version 2.0.3"
#ifdef CFE_VER_RELEASE
        ".%d"
#endif
        " for DGN2200v2 (%s)\n",
        //CFE_VER_MAJOR,CFE_VER_MINOR,CFE_VER_BUILD, BCM63XX_MAJOR, BCM63XX_MINOR,
#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-2009 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)
{
    cfe_launch(ept);
}

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

static void cfe_startup_info(void)
{
    xprintf("Chip ID: BCM%X%X, ", (PERF->RevID & 0xFFFF0000) >> 16, PERF->RevID & 0xFF);
    xprintf("MIPS: %dMHz",cfe_cpu_speed/1000000);
#if defined (_BCM96328_) || defined (_BCM96362_) || defined (_BCM96816_)
    xprintf(", DDR: %dMHz, Bus: %dMHz\n", cfe_ddr_speed/1000000, cfe_bus_speed/1000000);
#else
    xprintf("\n");
#endif
    {
    unsigned long tp;
    __asm __volatile(
    "mfc0 $9, $22, 3;"
    "move %0, $9"
    :"=r" (tp));
    tp = ((tp & CP0_CMT_TPID) == CP0_CMT_TPID) ? 1 : 0;
    xprintf("Main Thread: TP%d\n", tp);
    }
#if defined (_BCM96328_) || defined (_BCM96362_) || defined (_BCM96816_)
    if (DDR->TEST_CFG1 & 0x2) {   /* Memory Test is finished */
        xprintf("Memory Test ");
        if (DDR->TEST_CFG1 & 0x4) {
            xprintf("Failed\n");
        }
        else {
            xprintf("Passed\n");
        }
    }
#endif
    cfe_sdramsize = cfe_get_sdram_size();
    xprintf("Total Memory: %lu bytes (%luMB)\n", cfe_sdramsize, cfe_sdramsize >> 20);
    xprintf("Boot Address: 0x%x\n\n", FLASH_BASE);
}

#if defined (_BCM96816_)
/*  *********************************************************************
    *  apm_hvg_init()
    *  
    *  Work around 6816-specific reset logic
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   does not return
    ********************************************************************* */

static void apm_hvg_init(void)
{
    HVG->reg_hvg_cha_misc ^= HVG_SOFT_INIT_0;
    HVG->reg_hvg_chb_misc ^= HVG_SOFT_INIT_0;
}
#endif
      
/*  *********************************************************************
    *  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();
    calculateCpuSpeed();
    cfe_timer_init();                /* Timer process */
    cfe_bg_add(cfe_device_poll,NULL);

    /*
     * Initialize the memory allocator
     */
    KMEMINIT((unsigned char *) (uintptr_t) mem_heapstart,
         ((CFG_HEAP_SIZE)*1024));

    /*
     * Initialize the console.  It is done before the other devices
     * get turned on. 
     */

    board_console_init();
    cfe_setup_exceptions();
    cfe_say_hello();

#if defined(CONFIG_BRCM_IKOS)
    {
    /*0x694b6f32 (iKo2) is replaced with actual addr during the build process*/
    static unsigned long linuxStartAddr=0x694b6f32;
    printf("IKOS Build: Jump to Linux start address 0x%8.8lx.\n\n",
        linuxStartAddr);
    cfe_launch(linuxStartAddr);
    }
#endif

    cfe_arena_init();
    board_device_init();
    cfe_startup_info();
    cfe_setup_default_env();
    ui_init_cmddisp();
    getBootLine();
    getBoardParam();
#if defined (_BCM96816_)
    apm_hvg_init();
#endif

    /* Foxconn add start by Cliff Wang, 03/23/2010 */
    ui_init_netcmds();
    /* Foxconn add end by Cliff Wang, 03/23/2010 */

    board_final_init();
    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;

    /* Start Web interface. */
    cfe_bg_add(cfe_web_poll,NULL);

    for (;;) {
        prompt = env_getenv("PROMPT");
        if (!prompt) prompt = "CFE> ";
        console_readline(prompt,buffer,sizeof(buffer));

        if (cfe_web_check())
            cfe_web_fg_process();
        else {
            status = ui_docommands(buffer);
            if (status != CMD_ERR_BLANK) {
                xprintf("*** command status = %d\n", status);
            }
        }
    }
}

/*  *********************************************************************
    *  cfe_errortext(err)
    *  
    *  Returns an error message with a number in it.  The number can be
    *  looked up in cfe_error.h. 
    *  
    *  Input parameters: 
    *         err - error code
    *         
    *  Return value:
    *         string description of error
    ********************************************************************* */

const char *cfe_errortext(int err)
{
    static char err_buf[20];

    sprintf(err_buf, "CFE error %d", err);
    return (const char *) err_buf;
}

#if defined (_BCM96368_)
/*  *********************************************************************
    *  calculateCpuSpeed()
    *      Calculate BCM6368 CPU speed by reading the PLL Config register
    *      and applying the following formula:
    *      Fref_clk = (64 * (P2/P1) * (MIPSDDR_NDIV / REF_MDIV)
    *      Fbus_clk = (64 * (P2/P1) * (MIPSDDR_NDIV / DDR_MDIV)
    *      Fcpu_clk = (64 * (P2/P1) * (MIPSDDR_NDIV / CPU_MDIV)
    *  Input parameters:
    *      none
    *  Return value:
    *      none
    ********************************************************************* */
void static calculateCpuSpeed(void)
{
    UINT32 numerator;
    UINT32 pllConfig = DDR->MIPSDDRPLLConfig;
    UINT32 pllMDiv = DDR->MIPSDDRPLLMDiv;

    numerator = 64000000 / ((pllConfig & MIPSDDR_P1_MASK) >> MIPSDDR_P1_SHFT) * 
        ((pllConfig & MIPSDDR_P2_MASK) >> MIPSDDR_P2_SHFT) *
        ((pllConfig & MIPSDDR_NDIV_MASK) >> MIPSDDR_NDIV_SHFT);

    cfe_cpu_speed = numerator / ((pllMDiv & MIPS_MDIV_MASK) >> MIPS_MDIV_SHFT);
    cfe_bus_speed = numerator / ((pllMDiv & DDR_MDIV_MASK) >> DDR_MDIV_SHFT);
    cfe_ref_speed = numerator / ((pllConfig & REF_MDIV_MASK) >> REF_MDIV_SHFT);
}
#endif

#if defined (_BCM96328_) || defined (_BCM96362_) || (_BCM96816_)
/*  *********************************************************************
    *  calculateCpuSpeed()
    *      Calculate CPU speed by reading strap register
    *  Input parameters:
    *      none
    *  Return value:
    *      none
    ********************************************************************* */
void static calculateCpuSpeed(void)
{
    uint32 mips_pll_fvco;

    mips_pll_fvco = MISC->miscStrapBus & MISC_STRAP_BUS_MIPS_PLL_FVCO_MASK;
    mips_pll_fvco >>= MISC_STRAP_BUS_MIPS_PLL_FVCO_SHIFT;
    cfe_cpu_speed = cpu_speed_table[mips_pll_fvco] * 1000000;
    cfe_ddr_speed = ddr_speed_table[mips_pll_fvco] * 1000000;
    cfe_bus_speed = bus_speed_table[mips_pll_fvco] * 1000000;
}
#endif

/*  *********************************************************************
    *  cfe_get_sdram_size(void)
    *  
    *  Return amount of SDRAM on the board.
    *  
    *  Input parameters: 
    *         None.
    *         
    *  Return value:
    *         Amount of SDRAM on the board.
    ********************************************************************* */
static unsigned long cfe_get_sdram_size(void)
{
#if defined (_BCM96368_)
    uint32 size;
    uint32 memCfg;

    size = 1;
    memCfg = MEMC->Config;
    /* Number of column bits */
    size <<= (((memCfg & MEMC_COL_MASK) >> MEMC_COL_SHFT) + 8);
    /* Plus number of row bits */
    size <<= (((memCfg & MEMC_ROW_MASK) >> MEMC_ROW_SHFT) + 11);
    /* Plus bus width */
    if (((memCfg & MEMC_WIDTH_MASK) >> MEMC_WIDTH_SHFT) == MEMC_32BIT_BUS)
        size <<= 2;
    else
        size <<= 1;

    /* Plus number of banks */
    size <<= 2;

    return( size );
#else
    return (DDR->CSEND << 24);
#endif
}