summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/arch/mips/board/bcm63xx_ram/src/bcm63xx_devs.c
blob: ac750837bf488ba2d56e65247d4fe0727ac728df (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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *
    *  Board device initialization      File: bcm94710_devs.c
    *
    *  This is the "C" part of the board support package.  The
    *  routines to create and initialize the console, wire up
    *  device drivers, and do other customization live here.
    *
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *
    *********************************************************************
    *
    *  XX Copyright 2000,2001
    *  Broadcom Corporation. All rights reserved.
    *
    *  BROADCOM PROPRIETARY AND CONFIDENTIAL
    *
    *  This software is furnished under license and may be used and
    *  copied only in accordance with the license.
    ********************************************************************* */

#include "lib_types.h"
#include "lib_printf.h"

/* Foxconn add start by Cliff Wang, 03/23/2010 */
#include "lib_string.h"
/* Foxconn add end by Cliff Wang, 03/23/2010 */

#include "cfe_timer.h"
#include "cfe.h"
#include "bcm_map.h"
#include "bcm_hwdefs.h"
#include "bcmTag.h"
#include "dev_bcm63xx_flash.h"
#include "bcm63xx_util.h"
#include "flash_api.h"

/* Foxconn add start by Cliff Wang, 03/23/2010 */
#include "tftpd.h"
/* Foxconn add end by Cliff Wang, 03/23/2010 */

static int checkForResetToDefaultHold( unsigned short rstToDfltIrq );

/* Foxconn added start , 1/29/2010 */
#include "gpio_drv.h"
#include "foxconnCfg.h"

typedef enum
{
    kGpioInactive,
    kGpioActive
} GPIO_STATE_t;
static int test_led_control(int on_off);
int power_led_toggle(int state);
int nmrp_led_toggle(void);
int verify_board_id(char *buf);
int verify_checksum(char *buf, unsigned long buf_len, unsigned long chksum);
#define NR_LED 24
/* Foxconn added end , 1/29/2010 */

/*  *********************************************************************
    *  Devices we're importing
    ********************************************************************* */

extern cfe_driver_t bcm63xx_uart;
extern cfe_driver_t bcm63xx_enet;

/*  *********************************************************************
    *  board_console_init()
    *
    *  Add the console device and set it to be the primary
    *  console.
    *
    *  Input parameters:
    *      nothing
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void board_console_init(void)
{
    /* Add the serial port driver. */
    cfe_add_device(&bcm63xx_uart,0,0,0);

    cfe_set_console( "uart0" );
}


/*  *********************************************************************
    *  board_device_init()
    *
    *  Initialize and add other devices.  Add everything you need
    *  for bootstrap here, like disk drives, flash memory, UARTs,
    *  network controllers, etc.
    *
    *  Input parameters:
    *      nothing
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void board_device_init(void)
{
    unsigned short GPIOOverlays;

    kerSysFlashInit();

#if defined (_BCM96328_)
    if( BpGetGPIOverlays(&GPIOOverlays) == BP_SUCCESS ) {
        /* Start with all HW LEDs disabled */
        LED->ledHWDis |= 0xFFFFFF;
        if (GPIOOverlays & BP_OVERLAY_SERIAL_LEDS) {
            GPIO->PinMuxSel |= PINMUX_SERIAL_LED_DATA;
            GPIO->PinMuxSel |= PINMUX_SERIAL_LED_CLK;
            LED->ledInit |= LED_SERIAL_LED_EN;
        }
        /* Enable LED controller to drive GPIO */
        /* foxconn modified start to let hardware control only act LEDs, Bob, 08/04/2010 */
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_0) {
            GPIO->PinMuxSel |= PINMUX_EPHY0_ACT_LED;
            //GPIO->GPIOMode |= (1 << EPHY0_SPD_LED);
            //LED->ledHWDis &= ~(1 << EPHY0_SPD_LED);
        }
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_1) {
            GPIO->PinMuxSel |= PINMUX_EPHY1_ACT_LED;
            //GPIO->GPIOMode |= (1 << EPHY1_SPD_LED);
            //LED->ledHWDis &= ~(1 << EPHY1_SPD_LED);
        }
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_2) {
            GPIO->PinMuxSel |= PINMUX_EPHY2_ACT_LED;
            //GPIO->GPIOMode |= (1 << EPHY2_SPD_LED);
            //LED->ledHWDis &= ~(1 << EPHY2_SPD_LED);
        }
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_3) {
            GPIO->PinMuxSel |= PINMUX_EPHY3_ACT_LED;
            //GPIO->GPIOMode |= (1 << EPHY3_SPD_LED);
            //LED->ledHWDis &= ~(1 << EPHY3_SPD_LED);
        }
        /* foxconn modified end, Bob, 08/04/2010 */
    }
#endif

#if defined (_BCM96362_)
    if( BpGetGPIOverlays(&GPIOOverlays) == BP_SUCCESS ) {
        /* Start with all HW LEDs disabled */
        LED->ledHWDis |= 0xFFFFFF;
        if (GPIOOverlays & BP_OVERLAY_SERIAL_LEDS) {
            GPIO->GPIOMode |= (GPIO_MODE_SERIAL_LED_CLK | GPIO_MODE_SERIAL_LED_DATA);
            LED->ledInit |= LED_SERIAL_LED_EN;
        }
        /* Map HW LEDs to LED controller inputs and enable LED controller to drive GPIO */
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_0) {
            LED->ledLinkActSelHigh |= ((1 << (LED_ENET0 - 4)) << LED_4_LINK_SHIFT);
            GPIO->LEDCtrl |= (1 << LED_ENET0);
            LED->ledHWDis &= ~(1 << LED_ENET0);
        }
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_1) {
            LED->ledLinkActSelHigh |= ((1 << (LED_ENET1 - 4)) << LED_5_LINK_SHIFT);
            GPIO->LEDCtrl |= (1 << LED_ENET1);
            LED->ledHWDis &= ~(1 << LED_ENET1);
        }
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_2) {
            LED->ledLinkActSelHigh |= ((1 << (LED_ENET2 - 4)) << LED_6_LINK_SHIFT);
            GPIO->LEDCtrl |= (1 << LED_ENET2);
            LED->ledHWDis &= ~(1 << LED_ENET2);
        }
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_3) {
            LED->ledLinkActSelHigh |= ((1 << (LED_ENET3 - 4)) << LED_7_LINK_SHIFT);
            GPIO->LEDCtrl |= (1 << LED_ENET3);
            LED->ledHWDis &= ~(1 << LED_ENET3);
        }
    }
#endif

#if defined (_BCM96368_)
    TIMER->WDResetCount = 50000;    // Assert reset for 1ms only. If reset asserted for too
                                    // long chip reboots twice
    if( BpGetGPIOverlays(&GPIOOverlays) == BP_SUCCESS ) {
        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_0) {
            GPIO->GPIOMode |= GPIO_MODE_EPHY0_LED;
            GPIO->GPIODir |= GPIO_MODE_EPHY0_LED;
        }

        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_1) {
            GPIO->GPIOMode |= GPIO_MODE_EPHY1_LED;
            GPIO->GPIODir |= GPIO_MODE_EPHY1_LED;
        }

        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_2) {
            GPIO->GPIOMode |= GPIO_MODE_EPHY2_LED;
            GPIO->GPIODir |= GPIO_MODE_EPHY2_LED;
        }

        if (GPIOOverlays & BP_OVERLAY_EPHY_LED_3) {
            GPIO->GPIOMode |= GPIO_MODE_EPHY3_LED;
            GPIO->GPIODir |= GPIO_MODE_EPHY3_LED;
        }

        if (GPIOOverlays & BP_OVERLAY_SERIAL_LEDS) {
            GPIO->GPIOMode |= (GPIO_MODE_SERIAL_LED_CLK | GPIO_MODE_SERIAL_LED_DATA);
            GPIO->GPIODir |= (GPIO_MODE_SERIAL_LED_CLK | GPIO_MODE_SERIAL_LED_DATA);
            GPIO->SerialLed = 0xffffffff;
        }
    }

    /* These blocks will be enabled by the appropriate driver if they are
     * compiled into a router image.
     */
    PERF->blkEnables &= ~(USBH_CLK_EN | PCM_CLK_EN | SAR_CLK_EN | USBD_CLK_EN);
#endif

#if defined (_BCM96816_)
    TIMER->WDResetCount = 50000;    // Assert reset for 1ms only. If reset asserted for too
                                    // long chip reboots twice

    if( BpGetGPIOverlays(&GPIOOverlays) == BP_SUCCESS ) {
        if (GPIOOverlays & BP_OVERLAY_GPHY_LED_0) {
            GPIO->GPIOMode |= GPIO_MODE_GPHY0_LED;
            GPIO->GPIODir |= GPIO_MODE_GPHY0_LED;
        }

        if (GPIOOverlays & BP_OVERLAY_GPHY_LED_1) {
            GPIO->GPIOMode |= GPIO_MODE_GPHY1_LED;
            GPIO->GPIODir |= GPIO_MODE_GPHY1_LED;
        }

        if (GPIOOverlays & BP_OVERLAY_SERIAL_LEDS) {
            GPIO->GPIOMode |= (GPIO_MODE_SERIAL_LED_CLK | GPIO_MODE_SERIAL_LED_DATA);
            GPIO->GPIODir |= (GPIO_MODE_SERIAL_LED_CLK | GPIO_MODE_SERIAL_LED_DATA);
            GPIO->SerialLed = 0xffffffff;
        }

        if (GPIOOverlays & BP_OVERLAY_MOCA_LED) {
            GPIO->GPIOMode |= GPIO_MODE_MOCA_LED;
            GPIO->GPIODir |= GPIO_MODE_MOCA_LED;
        }
    }

    MISC->miscMoCARst &= ~(MISC_MOCA_RST_REF_DIV2RST | MISC_MOCA_RST_REF_FBDIVRST);
    MISC->miscMoCARst &= ~MISC_MOCA_RST_REF_VCRST;
    MISC->miscMoCARst &= ~(MISC_MOCA_RST_REF_OUTDIV_RESET_M_MASK | MISC_MOCA_RST_REF_MDIV2RST);
    MISC->miscMoCACtl |= (7 << MISC_MOCA_CTL_REF_QP_ICTRL_SHIFT);
    MISC->miscMoCARst &= ~MISC_MOCA_RST_REF_LD_RESET_STRT;
    PERF->softResetB &= ~(SOFT_RST_MOCA_CPU | SOFT_RST_MOCA_SYS | SOFT_RST_MOCA);
#endif

#if defined (_BCM96328_) || defined (_BCM96362_)
    LED->ledInit &= ~LED_FAST_INTV_MASK;
    LED->ledInit |= (LED_INTERVAL_20MS * 4) << LED_FAST_INTV_SHIFT;
#else
    /* Set blink rate for hardware LEDs. */
    GPIO->LEDCtrl &= ~LED_INTERVAL_SET_MASK;
    GPIO->LEDCtrl |= LED_INTERVAL_SET_80MS;
#endif

    /* Add the ethernet driver. */
    cfe_add_device( &bcm63xx_enet, 0, 0, 0);
}


void setGpio (unsigned short led_gpio, unsigned short led_state)
{
#if 0	
    unsigned short gpio_state;

    if (((led_gpio & BP_ACTIVE_LOW) && (led_state == LED_ON)) || 
        (!(led_gpio & BP_ACTIVE_LOW) && (led_state == LED_OFF)))
        gpio_state = 0;
    else
        gpio_state = 1;
#endif

    GPIO->GPIODir |= GPIO_NUM_TO_MASK(led_gpio);
    if( led_state )
        GPIO->GPIOio |= GPIO_NUM_TO_MASK(led_gpio);
    else
        GPIO->GPIOio &= ~GPIO_NUM_TO_MASK(led_gpio);
}


/*  *********************************************************************
    *  board_final_init()
    *
    *  Do any final initialization, such as adding commands to the
    *  user interface.
    *
    *  If you don't want a user interface, put the startup code here.
    *  This routine is called just before CFE starts its user interface.
    *
    *  Input parameters:
    *      nothing
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void board_final_init(void)
{
    unsigned short rstToDfltIrq;
    int breakIntoCfe = 0;

    /* Foxconn add start by Cliff Wang, 03/23/2010 */    
    int goAuto;
    /* Foxconn add end by Cliff Wang, 03/23/2010 */

    setAllLedsOff();
    setPowerOnLedOn();
    setGpio(GPIO_POWER_RED_LED, GPIO_POWER_RED_LED_ON);
#if (INC_SPI_PROG_NAND==1)
    rstToDfltIrq = 1 ;
    breakIntoCfe = rstToDfltIrq;
#else
    if( (bootInfo.runFrom == 'f') && getBootImageTag() == NULL ) {
        setBreakIntoCfeLed();
        printf("** Flash image not found. **\n\n");
        kerSysErasePsi();
        breakIntoCfe = 1;
    }

    if( BpGetResetToDefaultExtIntr( &rstToDfltIrq ) == BP_SUCCESS ) {
        if (checkForResetToDefaultHold( rstToDfltIrq )) {
            kerSysErasePsi();
            /* Reset the default bootline if the board IP address has changed. */
            if (strcmp(bootInfo.boardIp, DEFAULT_BOARD_IP) != 0) {
                setDefaultBootline();
            }
            /* Foxconn modified start pling 09/09/2008 */
            /* just load default, don't break into console */
            //breakIntoCfe = 1;
            printf("\n** Load default! **\n\n");
            /* Foxconn modified end pling 09/09/2008 */
        }
    }
#endif
    /* Foxconn add start by Cliff Wang, 03/23/2010 */
    goAuto = bcm63xx_run(breakIntoCfe);
    setBreakIntoCfeLed();
    if(goAuto)
    {
        ui_docommand("tftpd");
    }
    /* Foxconn add end by Cliff Wang, 03/23/2010 */
}

/*  *********************************************************************
    * Miscellaneous Board Functions
    ********************************************************************* */

/*  *********************************************************************
    *  checkForResetToDefaultHold()
    *
    *  Determines if the user is holding the reset to default button.
    *
    *  Input parameters:
    *      Reset to default irq#
    *
    *  Return value:
    *      1 - break into the CFE, 0 - continue boot sequence
    ********************************************************************* */
#if (INC_SPI_PROG_NAND==0)
static int checkForResetToDefaultHold( unsigned short rstToDfltIrq )
{
    const int nBreakIntoCfeDelay = 5;
    int ret = 0;
    int i;
    uint32 irqBit;
    volatile uint32 *extIrqReg;

    extIrqReg = &PERF->ExtIrqCfg;
    irqBit = 1 << (rstToDfltIrq + EI_STATUS_SHFT);

#if defined(_BCM96368_)
    if (rstToDfltIrq >= BP_EXT_INTR_4 && rstToDfltIrq <= BP_EXT_INTR_5) {
        extIrqReg = &PERF->ExtIrqCfg1;
        irqBit = 1 << (rstToDfltIrq - BP_EXT_INTR_4 + EI_STATUS_SHFT);
    }
#endif

    /* Loop while the reset to default button is depressed. */
    for(i = 0; !(*extIrqReg & irqBit); i++) {
        if (i == nBreakIntoCfeDelay) {
            setBreakIntoCfeLed();
            printf("\n*** Break into CFE console ***\n\n");
            ret = 1;
            break;
        }
        cfe_sleep(CFE_HZ);
    }

    return( ret );
}
#endif
/*  *********************************************************************
    *  setLed(led_gpio, led_state)
    *
    *  Turns on an LED.
    *
    *  Input parameters:
    *      LED purpose
    *      LED State
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void setLed (unsigned short led_gpio, unsigned short led_state)
{
    unsigned short gpio_state;

    if (((led_gpio & BP_ACTIVE_LOW) && (led_state == LED_ON)) || 
        (!(led_gpio & BP_ACTIVE_LOW) && (led_state == LED_OFF)))
        gpio_state = 0;
    else
        gpio_state = 1;

#if defined(_BCM96328_)
    /* Enable LED controller to drive this GPIO */
    if (!(led_gpio & BP_GPIO_SERIAL))
        GPIO->GPIOMode |= GPIO_NUM_TO_MASK(led_gpio);
#endif

#if defined(_BCM96362_)
    /* Enable LED controller to drive this GPIO */
    if (!(led_gpio & BP_GPIO_SERIAL))
        GPIO->LEDCtrl |= GPIO_NUM_TO_MASK(led_gpio);
#endif

#if defined(_BCM96328_) || defined(_BCM96362_)
    LED->ledMode &= ~(LED_MODE_MASK << GPIO_NUM_TO_LED_MODE_SHIFT(led_gpio));
    if( gpio_state )
        LED->ledMode |= (LED_MODE_OFF << GPIO_NUM_TO_LED_MODE_SHIFT(led_gpio));
    else
        LED->ledMode |= (LED_MODE_ON << GPIO_NUM_TO_LED_MODE_SHIFT(led_gpio));

#else
    if (led_gpio & BP_GPIO_SERIAL) {
        while (GPIO->SerialLedCtrl & SER_LED_BUSY);
        if( gpio_state )
            GPIO->SerialLed |= GPIO_NUM_TO_MASK(led_gpio);
        else
            GPIO->SerialLed &= ~GPIO_NUM_TO_MASK(led_gpio);
    }
    else {
        GPIO->GPIODir |= GPIO_NUM_TO_MASK(led_gpio);
        if( gpio_state )
            GPIO->GPIOio |= GPIO_NUM_TO_MASK(led_gpio);
        else
            GPIO->GPIOio &= ~GPIO_NUM_TO_MASK(led_gpio);
    }
#endif
}

/*  *********************************************************************
    *  setAllLedsOff()
    *
    *  Turns off all board LEDs on init
    *
    *  Input parameters:
    *      LED purpose
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void setAllLedsOff(void)
{
    unsigned short gpio;

    if( BpGetAdslLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetAdslFailLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetSecAdslLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetSecAdslFailLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetWirelessSesLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetHpnaLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetWanDataLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetWanErrorLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetBootloaderPowerOnLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetBootloaderStopLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetVoipLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetVoip1LedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetVoip2LedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetPotsLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetGponLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetMoCALedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetGponFailLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetMoCAFailLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
}

/*  *********************************************************************
    *  setPowerOnLedOn()
    *
    *  Turns on the Power LED.
    *
    *  Input parameters:
    *      LED purpose
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void setPowerOnLedOn(void)
{
    unsigned short gpio;
    if( BpGetBootloaderStopLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_OFF );
    if( BpGetBootloaderPowerOnLedGpio( &gpio ) == BP_SUCCESS )
        setLed( gpio, LED_ON );
}


/*  *********************************************************************
    *  setBreakIntoCfeLed()
    *
    *  Turns on the alarm LED.
    *
    *  Input parameters:
    *      LED purpose
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void setBreakIntoCfeLed(void)
{
    unsigned short gpio;
    if( BpGetBootloaderStopLedGpio( &gpio ) == BP_SUCCESS ) {
        setLed( gpio, LED_ON );
        if( BpGetBootloaderPowerOnLedGpio( &gpio ) == BP_SUCCESS )
            setLed( gpio, LED_OFF );
    }
}

/*  *********************************************************************
    *  softReset()
    *
    *  Resets the board.
    *
    *  Input parameters:
    *      nothing
    *
    *  Return value:
    *      nothing
    ********************************************************************* */

void softReset(void)
{
    printf( "\nResetting board...\n" );
#if defined (_BCM96816_)
    /* Work around reset issues */
    HVG->reg_hvg_cha_misc |= HVG_SOFT_INIT_0;
    HVG->reg_hvg_chb_misc |= HVG_SOFT_INIT_0;
#endif

#if defined(_BCM96328_)
    TIMER->SoftRst |= SOFT_RESET;
#else
    PERF->pll_control |= SOFT_RESET;    // soft reset mips
#if defined(_BCM96368_) || defined(_BCM96816_)
    PERF->pll_control = 0;
#endif
#endif
    while (1);
}

/* Foxconn added start , 1/29/2010 */
extern int nmrp_server_detected;

int test_led_control(int on_off)
{
    // unsigned short gpio;
    int tftpd_state = get_tftpd_state();

    if (tftpd_state == TFTPD_STATE_OFF)
    {
    	setGpio(GPIO_POWER_RED_LED, GPIO_POWER_RED_LED_ON);	//turn red power led
    }
    else if ( (tftpd_state == TFTPD_STATE_WAIT_IMAGE) || (tftpd_state == TFTPD_STATE_WRITE_IMAGE) )
    {
        if (on_off==1)
        {            
            setGpio(GPIO_POWER_RED_LED, GPIO_POWER_RED_LED_ON);
        }
        else
        {
            setGpio(GPIO_POWER_RED_LED, GPIO_POWER_RED_LED_OFF);
        }
    }

    return 0;
}

int power_led_toggle(int state)
{
    test_led_control(state);
    return 0;
}

int nmrp_led_toggle(void)
{
    static int led_state = 0;

    /* Foxconn added end for U12H154 */

    if (led_state == 0)
    {
        led_state = 1;
        setGpio(18, 1);     
    }
    else
    {
        led_state = 0;
        setGpio(18, 0);
    }

    return 0;
}


static unsigned long calculate_checksum (int action, char *s, int size)
{
    static unsigned long c0, c1;
    unsigned long checksum, b;
    int i;
    switch (action)
    {
        case 0:
            c0 = c1 = 0;
            break;

        case 1:
            for (i = 0; i < size; i++)
            {
                c0 += s[i] & 0xff;
                c1 += c0;
                /* check the lan/wan ports status, Foxconn added by EricHuang, 10/24/2006 */
                if ((i % 90000) == 0)
                {
                    printf(".");
//                    bcm_robo_check_link_status();
                }
            }
            break;

        case 2:
            b = (c0 & 65535) + ((c0 >> 16) & 65535);
            c0 = ((b >> 16) + b) & 65535;

            b = (c1 & 65535) + ((c1 >> 16) & 65535);
            c1 = ((b >> 16) + b) & 65535;

            checksum = ((c1 << 16) | c0);

            return checksum;
    }
    return 0;
}

int verify_checksum(char *buf, unsigned long buf_len, unsigned long chksum)
{
    unsigned long cal_chksum = 0;

    xprintf("Loading .");
    calculate_checksum (0, NULL, 0);
    calculate_checksum (1, buf, buf_len);
    cal_chksum = calculate_checksum (2, NULL, 0);
    printf("\n");


    if (cal_chksum != chksum) {
        xprintf("Image chksum: 0x%08X\n", chksum);
        xprintf("Calc  chksum: 0x%08X\n", cal_chksum);
        return -1;
    }

#ifdef _DEBUG
    //printf("verify_checksum calculate_checksum    = 0x%08X\n", cal_chksum);
    //printf("verify_checksum image_chksum          = 0x%08X\n", chksum);
#endif

    return 0;
}

int verify_board_id(char *buf)
{
    /* char *board_id = ( char *)BOARD_DATA_ADDR; */
    char board_id[PROJECT_ID_LEN];

    kerSysReadFromFlash(board_id, BOARD_DATA_ADDR, PROJECT_ID_LEN);

    if(nmrp_server_detected==1) // in NMRP mode
    {
        if (strncmp(buf, board_id, PROJECT_ID_LEN) != 0)
        {
            printf("verify_board_id failed: (%s)\n", buf);
            return -1;
        }
    }
    else
    {
        if (strcmp(buf, board_id) != 0)
        {
            /* Foxconn removed start pling 12/10/2008 */
            /* Don't check board ID in 'tftpd' mode */
            printf("verify_board_id failed: (%s)\n", buf);
            //return -1;
            /* Foxconn removed end pling 12/10/2008 */
         }
    }
    return 0;
}

/* Foxconn added end , 1/29/2010 */