c66x POST: added makefile, disabled C6678 emac test until CSL is synced
[keystone-rtos/mcsdk-tools.git] / post / src / post.c
1 /******************************************************************************
2  * Copyright (c) 2011-12 Texas Instruments Incorporated - http://www.ti.com
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    distribution.
15  *
16  *    Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *****************************************************************************/
34 #include <string.h>
35 #include "platform.h"
36 #include "types.h"
37 #include "post.h"
38 #include "target.h"
39 #include "pscapi.h"
41 #if !(defined(_EVMC6657L_))
42   #include "net.h"
43   #include "cpmacdrv.h"
44   #include "qm_api.h"
45   #include "cpdma_api.h"
46 #else
47   #include "evmc665x_phy.h"
48   #include "evmc665x_emac.h"
49 #endif
51 /* CSL EMAC include */
52 #if !(defined(_EVMC6657L_))
53   #include <ti/csl/csl_cpsw.h>
54   #include <ti/csl/csl_cpsgmii.h>
55   #include <ti/csl/csl_cpsgmiiAux.h>
56   #include <ti/csl/cslr_cpsgmii.h>
57   #include <ti/csl/csl_mdioAux.h>
58 #else
59   #include <ti/csl/csl_cpsgmii.h>
60   #include <ti/csl/cslr_cpsgmii.h>
61 #endif
63 #include <ti/csl/csl_mdio.h>
65 /* BootCfg module include */
66 #include <ti/csl/csl_bootcfg.h>
67 #include <ti/csl/csl_bootcfgAux.h>
69 /* The version string */
70 #pragma DATA_SECTION(post_version, ".version")
71 #pragma DATA_ALIGN(post_version, 16)
72 char post_version[] = POST_VERSION;
74 /* OSAL functions for Platform Library */
75 uint8_t *Osal_platformMalloc (uint32_t num_bytes, uint32_t alignment)
76 {
77         return malloc(num_bytes);
78 }
80 void Osal_platformFree (uint8_t *dataPtr, uint32_t num_bytes)
81 {
82     /* Free up the memory */
83     if (dataPtr)
84     {
85         free(dataPtr);
86     }
87 }
89 /******************************************************************************
90  * Function:    post_display_led_error
91  ******************************************************************************/
92 void
93 post_display_led_error
94 (
95     POST_TEST_ID     test_id
96 )
97 {
98     uint8_t     led_status[POST_MAX_NUM_LED];
99     uint32_t    i;
101     memset(led_status, POST_LED_ON, POST_MAX_NUM_LED);
102     while (TRUE)
103     {
104         for (i = 0; i < POST_MAX_NUM_LED; i++)
105         {
106             if (post_led_status[test_id][i] == POST_LED_BLINK)
107             {
108                 if (led_status[i] == POST_LED_ON)
109                 {
110                     led_status[i] = POST_LED_OFF;
111                 }
112                 else
113                 {
114                     led_status[i] = POST_LED_ON;
115                 }
116                 platform_led(i, (PLATFORM_LED_OP)led_status[i], PLATFORM_USER_LED_CLASS);
117             }
118             else
119             {
120                 platform_led(i, (PLATFORM_LED_OP)post_led_status[test_id][i], PLATFORM_USER_LED_CLASS);
121             }
122         }
123         platform_delay(POST_LED_BLINK_DELAY);
124         /* POST in the while(1) loop to display the LED error status */
125     }
128 /******************************************************************************
129  * Function:    post_write_uart
130  ******************************************************************************/
131 Bool
132 post_write_uart
134     char*      msg
137     uint32_t i;
138     uint32_t msg_len = strlen(msg);
140     /* Write the message to the UART */
141     for (i = 0; i < msg_len; i++)
142     {
143         if (platform_uart_write(msg[i]) != Platform_EOK)
144         {
145             return FALSE;
146         }
147     }
149     return TRUE;
152 /******************************************************************************
153  * Function:    post_display_status
154  ******************************************************************************/
155 void
156 post_display_status
158     POST_TEST_ID        test_id,
159     POST_TEST_RESULT    test_result
162     uint32_t    i;
163     char        *msg;
164     char        msg1[40] = "\r\n\rPOST ";
165     char        msg2[] = " test passed!";
166     char        msg3[] = " test failed!";
167     char        msg4[] = " test started!";
169     msg = strcat(msg1, post_status[test_id]);
170     switch (test_id)
171     {
172     case POST_TEST_IN_PROGRESS:
173     case POST_TEST_COMPLETE:
174         /* Form the POST status message to write to the UART */
175         if (post_write_uart(msg) != TRUE)
176         {
177             post_display_led_error(POST_TEST_UART);   /* Never return from this function */
178         }
180         for (i = 0; i < POST_MAX_NUM_LED; i++)
181         {
182             if (platform_led(i, (PLATFORM_LED_OP)post_led_status[test_id][i], PLATFORM_USER_LED_CLASS) != Platform_EOK)
183             {
184                 post_write_uart("POST LED test failed \r\n");
185             }
186         }
187         break;
189     default:
190         /* Form the POST status message to write to the UART */
191         if (test_result == POST_TEST_RESULT_PASSED)
192         {
193             msg = strcat(msg, msg2);
194             if (post_write_uart(msg) != TRUE)
195             {
196                 post_display_led_error(POST_TEST_UART);   /* Never return from this function */
197             }
198         }
199         else if (test_result == POST_TEST_RESULT_FAILED)
200         {
201             msg = strcat(msg, msg3);
202             if (post_write_uart(msg) != TRUE)
203             {
204                 post_display_led_error(POST_TEST_UART);   /* Never return from this function */
205             }
206             post_display_led_error(test_id);  /* Never return from this function */
207         }
208         else
209         {
210             msg = strcat(msg, msg4);
211             if (post_write_uart(msg) != TRUE)
212             {
213                 post_display_led_error(POST_TEST_UART);   /* Never return from this function */
214             }
215         }
216         break;
217     }
220 /******************************************************************************
221  * Function:    post_test_external_memory
222  ******************************************************************************/
223 POST_TEST_RESULT
224 post_test_external_memory
226     void
229     POST_TEST_RESULT    test_result = POST_TEST_RESULT_PASSED;
231     if(platform_external_memory_test(0, 0) != Platform_EOK)
232     {
233         test_result = POST_TEST_RESULT_FAILED;
234     }
236     return test_result;
239 /******************************************************************************
240  * Function:    post_test_eeprom
241  ******************************************************************************/
242 POST_TEST_RESULT
243 post_test_eeprom
245     void
248     uint8_t                 test_buf[POST_EEPROM_TEST_READ_LENGTH];
249     POST_TEST_RESULT        test_result = POST_TEST_RESULT_PASSED;
250     PLATFORM_DEVICE_info    *p_device;
252     p_device = platform_device_open(POST_EEPROM_TEST_DEVICE_ID, 0);
253     if (p_device == NULL)
254     {
255         return POST_TEST_RESULT_FAILED;
256     }
258     if(platform_device_read(p_device->handle,
259                             POST_EEPROM_TEST_READ_ADDRESS,
260                             test_buf,
261                             POST_EEPROM_TEST_READ_LENGTH) != Platform_EOK)
262     {
263         test_result = POST_TEST_RESULT_FAILED;
264     }
266     platform_device_close(p_device->handle);
267     return test_result;
270 /******************************************************************************
271  * Function:    post_test_nand
272  ******************************************************************************/
273 POST_TEST_RESULT
274 post_test_nand
276     void
279     uint8_t                 test_buf[POST_NAND_TEST_READ_LENGTH];
280     POST_TEST_RESULT        test_result = POST_TEST_RESULT_PASSED;
281     uint32_t                addr;
282     PLATFORM_DEVICE_info    *p_device;
284     p_device = platform_device_open(POST_NAND_TEST_DEVICE_ID, 0);
285     if (p_device == NULL)
286     {
287         return POST_TEST_RESULT_FAILED;
288     }
290     addr = (POST_NAND_TEST_READ_BLOCK_NUM * p_device->page_count + POST_NAND_TEST_READ_PAGE_NUM) * p_device->page_size;
291     if(platform_device_read(p_device->handle,
292                             addr,
293                             test_buf,
294                             POST_NAND_TEST_READ_LENGTH) != Platform_EOK)
295     {
296         test_result = POST_TEST_RESULT_FAILED;
297     }
299     platform_device_close(p_device->handle);
300     return test_result;
303 /******************************************************************************
304  * Function:    post_test_nor
305  ******************************************************************************/
306 POST_TEST_RESULT
307 post_test_nor
309     void
312     uint8_t                 test_buf[POST_NOR_TEST_READ_LENGTH];
313     POST_TEST_RESULT        test_result = POST_TEST_RESULT_PASSED;
314     PLATFORM_DEVICE_info    *p_device;
316     p_device = platform_device_open(POST_NOR_TEST_DEVICE_ID, 0);
317     if (p_device == NULL)
318     {
319         return POST_TEST_RESULT_FAILED;
320     }
322     if(platform_device_read(p_device->handle,
323                             POST_NOR_TEST_READ_ADDR,
324                             test_buf,
325                             POST_NOR_TEST_READ_LENGTH) != Platform_EOK)
326     {
327         test_result = POST_TEST_RESULT_FAILED;
328     }
330     platform_device_close(p_device->handle);
331     return test_result;
333 #if !(defined(_EVMC6657L_))
334   /** Number of ports in the ethernet subsystem */
335   #define         NUM_PORTS                   3u
337   /** Number of MAC/GMII ports in the ethernet switch */
338   #define         NUM_MAC_PORTS               2u
341 #else
342   /** Number of ports in the ethernet subsystem */
343   #define         NUM_PORTS                   1u
345   /** Number of MAC/GMII ports in the ethernet switch */
346   #define         NUM_MAC_PORTS               1u
348 #endif
350 /*Define LoopBack Mode for C6657*/
351 #define MAC_LOOPBACK                    (1 << 0)
352 #define EXT_COPPER_LOOPBACK             (1 << 3)
353 #define EXT_FIBER_LOOPBACK              (1 << 4)
355 /* Define LoopBack modes for C6678 & C6670*/
356 #define CPSW_LOOPBACK_NONE           0
357 #define CPSW_LOOPBACK_INTERNAL       1
358 #define CPSW_LOOPBACK_EXTERNAL       2
360 #ifdef  SIMULATOR_SUPPORT
361 int32_t cpswSimTest = 1;
362 int32_t cpswLpbkMode = CPSW_LOOPBACK_EXTERNAL;
363 #else
364 int32_t LpbkMode = EXT_COPPER_LOOPBACK;
365 int32_t cpswSimTest = 0;
366 int32_t cpswLpbkMode = CPSW_LOOPBACK_INTERNAL;
367 #endif
368 int32_t cpswEvm6678 = 0;
370 #if !(defined(_EVMC6657L_)) && !(defined(_EVMC6678L_))
371 /** ============================================================================
372  *   @n@b Init_SGMII
373  *
374  *   @b Description
375  *   @n SGMII peripheral initialization code.
376  *
377  *   @param[in]
378  *   @n macPortNum      MAC port number for which the SGMII port setup must
379  *                      be performed.
380  *
381  *   @return
382  *   @n None
383  * =============================================================================
384  */
385 int32_t Init_sgmii (uint32_t macPortNum)
387     CSL_SGMII_ADVABILITY    sgmiiCfg;
388         CSL_SGMII_STATUS        sgmiiStatus;
390     if ((macPortNum == 0) && (cpswEvm6678))
391     {
392         /* EVM6678 back end: MAC-to-MAC force link */
394         /* Reset the port before configuring it */
395         CSL_SGMII_doSoftReset (macPortNum);
396         while (CSL_SGMII_getSoftResetStatus (macPortNum) != 0);
398         /* Hold the port in soft reset and set up
399         * the SGMII control register:
400         *      (1) Enable Master Mode (default)
401         */
402         CSL_SGMII_startRxTxSoftReset (macPortNum);
403         CSL_SGMII_enableMasterMode (macPortNum);
404         if (cpswLpbkMode != CPSW_LOOPBACK_NONE)
405         {
406                 CSL_SGMII_enableLoopback (macPortNum);
407         }
409             /* Setup the Advertised Ability register for this port:
410         *      (1) Enable Full duplex mode
411         *      (2) Speed = 1000M
412         *      (3) Force the Link
413         */
414         sgmiiCfg.bLinkUp        =   1;
415         sgmiiCfg.linkSpeed      =   CSL_SGMII_1000_MBPS;
416         sgmiiCfg.duplexMode     =   CSL_SGMII_FULL_DUPLEX;
417         CSL_SGMII_setAdvAbility (macPortNum, &sgmiiCfg);
419         CSL_SGMII_endRxTxSoftReset (macPortNum);
421             /* Wait for SGMII Link */
422             do
423             {
424                 CSL_SGMII_getStatus(macPortNum, &sgmiiStatus);
425             } while (sgmiiStatus.bIsLinkUp != 1);
426     }
427     else
428     {
429         /* Reset the port before configuring it */
430         CSL_SGMII_doSoftReset (macPortNum);
431         while (CSL_SGMII_getSoftResetStatus (macPortNum) != 0);
433         /* Hold the port in soft reset and set up
434         * the SGMII control register:
435         *      (1) Enable Master Mode (default)
436         *      (2) Enable Auto-negotiation
437         */
438         CSL_SGMII_startRxTxSoftReset (macPortNum);
439         if (cpswLpbkMode == CPSW_LOOPBACK_NONE)
440         {
441             CSL_SGMII_disableMasterMode (macPortNum);
442         }
443         else
444         {
445             CSL_SGMII_enableMasterMode (macPortNum);
447             if (cpswLpbkMode == CPSW_LOOPBACK_INTERNAL)
448             {
449                     CSL_SGMII_enableLoopback (macPortNum);
450             }
451         }
453             /* Setup the Advertised Ability register for this port:
454         *      (1) Enable Full duplex mode
455         *      (2) Enable Auto Negotiation
456         */
457         sgmiiCfg.linkSpeed      =   CSL_SGMII_1000_MBPS;
458         sgmiiCfg.duplexMode     =   CSL_SGMII_FULL_DUPLEX;
459         CSL_SGMII_setAdvAbility (macPortNum, &sgmiiCfg);
461         CSL_SGMII_enableAutoNegotiation (macPortNum);
462         CSL_SGMII_endRxTxSoftReset (macPortNum);
464             /* Wait for SGMII Link */
465         if (!cpswSimTest)
466         {
467                 do
468                 {
469                     CSL_SGMII_getStatus(macPortNum, &sgmiiStatus);
470                 } while (sgmiiStatus.bIsLinkUp != 1);
472                 /* Wait for SGMII Autonegotiation to complete without error */
473                 do
474                 {
475                     CSL_SGMII_getStatus(macPortNum, &sgmiiStatus);
476                     if (sgmiiStatus.bIsAutoNegError != 0)
477                         return -1;
478                 } while (sgmiiStatus.bIsAutoNegComplete != 1);
479         }
480     }
482     /* All done with configuration. Return Now. */
483     return 0;
486 int Init_MAC (uint32_t macPortNum, uint8_t macAddress[6], uint32_t mtu)
488     /* Reset MAC Sliver 0 */
489     CSL_CPGMAC_SL_resetMac (macPortNum);
490     while (CSL_CPGMAC_SL_isMACResetDone (macPortNum) != TRUE);
492     /* Setup the MAC Control Register for this port:
493      *      (1) Enable Full duplex
494      *      (2) Enable GMII
495      *      (3) Enable Gigabit
496      *      (4) Enable External Configuration. This enables
497      *          the "Full duplex" and "Gigabit" settings to be
498      *          controlled externally from SGMII
499      *      (5) Don't enable any control/error/short frames
500      */
501     CSL_CPGMAC_SL_enableFullDuplex (macPortNum);
502     CSL_CPGMAC_SL_enableGMII (macPortNum);
503     CSL_CPGMAC_SL_enableGigabit (macPortNum);
504     CSL_CPGMAC_SL_enableExtControl (macPortNum);
506     /* Configure the MAC address for this port */
507     CSL_CPSW_3GF_setPortMACAddress (macPortNum, macAddress);
509     /* Configure VLAN ID/CFI/Priority.
510      *
511      * For now, we are not using VLANs so just configure them
512      * to all zeros.
513      */
514     CSL_CPSW_3GF_setPortVlanReg (macPortNum, 0, 0, 0);
516     /* Configure the Receive Maximum length on this port,
517      * i.e., the maximum size the port can receive without
518      * any errors.
519      *
520      * Set the Rx Max length to the MTU configured for the
521      * interface.
522      */
523     CSL_CPGMAC_SL_setRxMaxLen (macPortNum, mtu);
525     /* Done setting up the MAC port */
526     return 0;
529 void Init_Switch (uint32_t mtu)
531     CSL_CPSW_3GF_PORTSTAT               portStatCfg;
533     /* Enable the CPPI port, i.e., port 0 that does all
534      * the data streaming in/out of EMAC.
535      */
536     CSL_CPSW_3GF_enablePort0 ();
537     CSL_CPSW_3GF_disableVlanAware ();
538     CSL_CPSW_3GF_setPort0VlanReg (0, 0, 0);
539     CSL_CPSW_3GF_setPort0RxMaxLen (mtu);
541     /* Enable statistics on both the port groups:
542      *
543      * MAC Sliver ports -   Port 1, Port 2
544      * CPPI Port        -   Port 0
545      */
546     portStatCfg.p0AStatEnable   =   1;
547     portStatCfg.p0BStatEnable   =   1;
548     portStatCfg.p1StatEnable    =   1;
549     portStatCfg.p2StatEnable    =   1;
550     CSL_CPSW_3GF_setPortStatsEnableReg (&portStatCfg);
552     /* Setup the Address Lookup Engine (ALE) Configuration:
553      *      (1) Enable ALE.
554      *      (2) Clear stale ALE entries.
555      *      (3) Disable VLAN Aware lookups in ALE since
556      *          we are not using VLANs by default.
557      *      (4) No Flow control
558      *      (5) Configure the Unknown VLAN processing
559      *          properties for the switch, i.e., which
560      *          ports to send the packets to.
561      */
562     CSL_CPSW_3GF_enableAle ();
563     CSL_CPSW_3GF_clearAleTable ();
565     CSL_CPSW_3GF_disableAleVlanAware ();
566     CSL_CPSW_3GF_disableAleTxRateLimit ();
567     CSL_CPSW_3GF_setAlePrescaleReg (125000000u/1000u);
568     CSL_CPSW_3GF_setAleUnkownVlanReg (7, 3, 3, 7);
570     if(cpswLpbkMode != CPSW_LOOPBACK_NONE)
571         CSL_CPSW_3GF_enableAleBypass();
573     /* Done with switch configuration */
574     return;
577 int Switch_update_addr (uint32_t portNum, uint8_t macAddress[6], Uint16 add)
579     uint32_t                              i;
580     CSL_CPSW_3GF_ALE_PORTCONTROL        alePortControlCfg;
581     CSL_CPSW_3GF_ALE_UNICASTADDR_ENTRY  ucastAddrCfg;
584     /* Configure the address in "Learning"/"Forward" state */
585     alePortControlCfg.portState             =   ALE_PORTSTATE_FORWARD;
586     alePortControlCfg.dropUntaggedEnable    =   0;
587     alePortControlCfg.vidIngressCheckEnable =   0;
588     alePortControlCfg.noLearnModeEnable     =   (cpswLpbkMode != CPSW_LOOPBACK_NONE)?1:0;
589     alePortControlCfg.mcastLimit            =   0;
590     alePortControlCfg.bcastLimit            =   0;
592     CSL_CPSW_3GF_setAlePortControlReg (portNum, &alePortControlCfg);
594     if (cpswLpbkMode != CPSW_LOOPBACK_NONE)
595     {
596         /* Program the ALE with the MAC address.
597         *
598         * The ALE entries determine the switch port to which any
599         * matching received packet must be forwarded to.
600         */
601         /* Get the next free ALE entry to program */
602         for (i = 0; i < CSL_CPSW_3GF_NUMALE_ENTRIES; i++)
603         {
604             if (CSL_CPSW_3GF_getALEEntryType (i) == ALE_ENTRYTYPE_FREE)
605             {
606                 /* Found a free entry */
607                 break;
608             }
609         }
610         if (i == CSL_CPSW_3GF_NUMALE_ENTRIES)
611         {
612             /* No free ALE entry found. return error. */
613             return -1;
614         }
615         else
616         {
617             /* Found a free ALE entry to program our MAC address */
618             memcpy (ucastAddrCfg.macAddress, macAddress, 6);    // Set the MAC address
619             ucastAddrCfg.ucastType      =      ALE_UCASTTYPE_UCAST_NOAGE;   // Add a permanent unicast address entryALE_UCASTTYPE_UCAST_NOAGE.
620             ucastAddrCfg.secureEnable   =      FALSE;
621             ucastAddrCfg.blockEnable    =      FALSE;
622             ucastAddrCfg.portNumber     =      portNum;   // Add the ALE entry for this port
624             /* Setup the ALE entry for this port's MAC address */
625             CSL_CPSW_3GF_setAleUnicastAddrEntry (i, &ucastAddrCfg);
626         }
627     }
629     /* Done with upading address */
630     return 0;
633 int32_t Init_SGMII_SERDES(void)
635     if (cpswSimTest)
636     {
638             /* Unlock the chip configuration registers to allow SGMII SERDES registers to
639             * be written */
640             CSL_BootCfgUnlockKicker();
642         CSL_BootCfgSetSGMIIConfigPLL (0x00000041);
643         CSL_BootCfgSetSGMIIRxConfig (0, 0x00700621);
644         CSL_BootCfgSetSGMIITxConfig (0, 0x000108A1);
645         CSL_BootCfgSetSGMIIRxConfig (1, 0x00700621);
646         CSL_BootCfgSetSGMIITxConfig (1, 0x000108A1);
648             /* Re-lock the chip configuration registers to prevent unintentional writes */
649             CSL_BootCfgLockKicker();
651     }
653         /* SGMII SERDES Configuration complete. Return. */
654         return 0;
657 int32_t Init_Cpsw (void)
659     uint32_t       macPortNum, mtu = 1518;
660     uint8_t        mac_address[6];
661     uint8_t        sw_port0_mac_addr[6] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
663         /* Initialize the SERDES modules */
664         Init_SGMII_SERDES();
666     platform_get_macaddr(PLATFORM_MAC_TYPE_EFUSE, mac_address);
668     /* Initialize the SGMII/Sliver submodules for the
669      * two corresponding MAC ports.
670      */
671     for (macPortNum = 0; macPortNum < NUM_MAC_PORTS; macPortNum++)
672     {
673         if (Init_sgmii (macPortNum))
674                   return -1;
675         mac_address[5] += macPortNum;
676         Init_MAC (macPortNum, mac_address, mtu);
677         mac_address[5] -= macPortNum;
678     }
680     /* Setup the Ethernet switch finally. */
681     Init_Switch (mtu);
683     Switch_update_addr(0, sw_port0_mac_addr, 0);
684     Switch_update_addr(1, mac_address, 0);
685     mac_address[5] += 1;
686     Switch_update_addr(2, mac_address, 0);
688     /* CPSW subsystem setup done. Return success */
689     return 0;
692 #endif
694 #if !(defined(_EVMC6678L_))
695 /******************************************************************************
696  * Function:    post_test_emac_loopback
697  ******************************************************************************/
698 POST_TEST_RESULT
699 post_test_emac_loopback
701     void
704     uint8_t   test_buf[POST_EMAC_TEST_PKT_LENGTH+14];
705 #if (defined(_EVMC6657L_))
706     SGMII_Config config;
707     uint8_t mac_address[6];
708 #endif
710 #if !(defined(_EVMC6657L_))
711     uint8_t   sw_port0_mac_addr[6] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
712     uint8_t   mac_address[6];
713     uint8_t             ret;
714     NET_DRV_DEVICE nDevice;
715 #endif
718 #if !(defined(_EVMC6657L_))
719     int modNum = 8;
720     if (modNum == TARGET_PWR_ETH(x))  {
721         ret = (int32_t)pscEnableModule (TARGET_PWR_PA);
722         if (ret != 0)
723             return (POST_TEST_RESULT_FAILED);
724     }
725 #endif
728 #if !(defined(_EVMC6657L_))
729     Init_Cpsw();
731     /* Queue manager configuration */
732     hwQmSetup ((qmConfig_t *)(targetGetQmConfig()));
733     targetInitQs ();
736     /* Cpdma configuration. */
737     hwCpdmaRxConfig ((cpdmaRxCfg_t *)targetGetCpdmaRxConfig());
738     hwCpdmaTxConfig ((cpdmaTxCfg_t *)targetGetCpdmaTxConfig());
741     /* Packet accelerator configuration. If PA is not present this statement is defined
742      * to void in target.h */
743     targetPaConfig(sw_port0_mac_addr);
745     /* Streaming switch configuration. If not present this statement is defined to void
746      * in target.h.  If present this is usually defined to a series of register writes */
747     hwConfigStreamingSwitch();
749     /* Initialize the network device driver */
750     memset (&nDevice, 0, sizeof(nDevice));
752     /* Start the networking device */
753     if (cpmac_drv_start(&nDevice) < 0)
754     {
755         return POST_TEST_RESULT_FAILED;
756     }
758     /* Get the MAC address from efuse */
759     platform_get_macaddr(PLATFORM_MAC_TYPE_EFUSE, mac_address);
761     /* Set the dest MAC address to be broadcast, so that PA firmware will not filter out */
762     memset(test_buf, 0xff, 6);
763     memcpy(&test_buf[6], sw_port0_mac_addr, 6);
765     /* set the payload length to 256 bytes */
766     test_buf[12] = 0x01;
767     test_buf[13] = 0x00;
769     /* Send the Ethernet packet */
770     if (cpmac_drv_send (&nDevice, test_buf, POST_EMAC_TEST_PKT_LENGTH+14) < 0)
771     {
772         return POST_TEST_RESULT_FAILED;
773     }
774     
775     platform_delay(100);
776     
777     /* Receive the loopback packet */
778     if (ret = cpmac_drv_receive (&nDevice, test_buf) <= 0)
779     {
780         return POST_TEST_RESULT_FAILED;
781     }
783 #else
784     sgmii_init();
785     EMAC_init();
786     config.loopbackEn = 1;
787     SGMII_config(&config);
789     platform_get_macaddr(PLATFORM_MAC_TYPE_EFUSE, mac_address);
791     /* Set the dest MAC address to be broadcast, so that PA firmware will not filter out */
792     memset(test_buf, 0xff, 6);
793     memcpy(&test_buf[6], mac_address, 6);
795     /* set the payload length to 256 bytes */
796     test_buf[12] = 0x01;
797     test_buf[13] = 0x00;
799     if(EMAC_Send(test_buf, POST_EMAC_TEST_PKT_LENGTH+14) < 0)
800     {
801         return POST_TEST_RESULT_FAILED;
802     }
804     platform_delay(1000);
806     if(EMAC_Recv(test_buf) < 0)
807     {
808         return POST_TEST_RESULT_FAILED;
809     }
811 #endif
812     return POST_TEST_RESULT_PASSED;
814 #endif
816 void
817 post_hex_to_string
819     uint32_t    hex,
820     uint32_t    nibbles,
821     char        *msg
824     int32_t     i;
825     uint8_t     nibble;
827     for (i = (nibbles-1); i >= 0; i--)
828     {
829         nibble = hex & 0xf;
830         if (nibble <= 0x9)
831         {
832             nibble += '0';
833         }
834         else
835         {
836             nibble += ('A' - 0xa);
837         }
839         msg[i] = nibble;
840         hex = hex >> 4;
841     }
842     msg[nibbles] = 0;
845 Bool
846 post_serial_num_isvalid
848     char    c
851     if (
852         ((c >= '0') && (c <= '9'))    ||
853         ((c >= 'a') && (c <= 'z'))    ||
854         ((c >= 'A') && (c <= 'Z'))
855        )
856     {
857         return TRUE;
858     }
859     else
860     {
861         return FALSE;
862     }
865 /******************************************************************************
866  * Function:    post_write_serial_no
867  ******************************************************************************/
868 void
869 post_write_serial_no
871     void
874     uint32_t                i, j;
875     uint8_t                 msg[20], msg2[2];
876     PLATFORM_DEVICE_info    *p_device;
878     /* Check if user key in the passcode "ti" to enter board serial number */
879     if (platform_uart_read(msg, POST_UART_READ_TIMEOUT) != Platform_EOK)
880     {
881         return;
882     }
883     if (msg[0] != 't')
884     {
885         return;
886     }
888     if (platform_uart_read(msg, POST_UART_READ_TIMEOUT) != Platform_EOK)
889     {
890         return;
891     }
892     if (msg[0] != 'i')
893     {
894         return;
895     }
897     /* Passcode verified, prompt the user to enter serial number */
898     p_device = platform_device_open(PLATFORM_DEVID_EEPROM50, 0);
899     if (p_device == NULL)
900     {
901         return;
902     }
904     post_write_uart("\r\n\r\nPlease enter the 10 digit serial number for this board, and then press ENTER key:\r\n\r\n");
906     i = 0;
907     msg2[1] = 0;
908     while (TRUE)
909     {
910         if (platform_uart_read(&msg[i], POST_UART_READ_TIMEOUT) != Platform_EOK)
911         {
912             platform_device_close(p_device->handle);
913             post_write_uart("\r\n\r\nSerial number input time out!");
914             return;
915         }
917         if (msg[i] == '\r')
918         {
919             break;
920         }
921         if ((i < POST_MAX_SN_SIZE) &&  post_serial_num_isvalid(msg[i]))
922         {
923             msg2[0] = msg[i];
924             post_write_uart((char *)msg2);
925             i++;
926         }
927     }
929     if (i < POST_MAX_SN_SIZE)
930     {
931         for (j = i; j < POST_MAX_SN_SIZE; j++)
932         {
933             msg[j] = 0xff;
934         }
935     }
937     if(platform_device_write(p_device->handle, POST_SERIAL_NUM_ADDR, msg, 16) == Platform_EOK)
938     {
939         post_write_uart("\r\n\r\nSerial number programmed to EEPROM successfully! ");
940     }
941     else
942     {
943         post_write_uart("\r\n\r\nFailed to program the serial number to EEPROM!");
944     }
945     platform_device_close(p_device->handle);
949 /******************************************************************************
950  * Function:    post_dump_register_val
951  ******************************************************************************/
952 void
953 post_dump_register_val
955     uint32_t            reg_addr,
956     char*               desc_string
959    char                    msg[10];
960    uint32_t                reg_val;
962    reg_val = *(volatile uint32_t *)reg_addr;
963    post_write_uart(desc_string);
964    post_hex_to_string(reg_val, 8, msg);
965    msg[8] = ' ';
966    msg[9] = 0;
967    post_write_uart(msg);   
970 /******************************************************************************
971  * Function:    main function for POST
972  ******************************************************************************/
973 void
974 main
976     void
978  {
979     platform_init_flags     init_flags;
980     platform_init_config    init_config;
981     POST_TEST_ID            test_id = POST_TEST_IN_PROGRESS;
982     POST_TEST_RESULT        test_result;
983     uint32_t                reset_type;
984     int32_t                 i;
985     char                    msg[9];
986     uint8_t                 mac_addr[6];
987     platform_info           info;
988 #if !(defined(_EVMC6657L_)||defined(_EVMC6655L_))
989     uint32_t                sa_enable;
990 #endif
991     uint32_t                acc_fail;
992     extern uint32_t platform_init_return_code;
994     /* Turn on all the platform initialize flags */
995     memset(&init_config, 0, sizeof(platform_init_config));
996     memset(&init_flags, 0x01, sizeof(platform_init_flags));
998     init_flags.phy = 1;
999     acc_fail = 0;
1001     /* Initialize the platform */
1002     if (platform_init(&init_flags, &init_config) != Platform_EOK)
1003     {
1004         switch (platform_errno)
1005         {
1006         case PLATFORM_ERRNO_PLL_SETUP:
1007             test_id = POST_TEST_PLL_INIT;
1008             break;
1009         case PLATFORM_ERRNO_NAND:
1010             test_id = POST_TEST_NAND_INIT;
1011             break;
1012         case PLATFORM_ERRNO_NOR:
1013             test_id = POST_TEST_NOR_INIT;
1014             break;
1015         default:
1016             test_id = POST_TEST_GENERAL;
1017             break;
1018         }
1019         test_result = POST_TEST_RESULT_FAILED;
1020     }
1021     if (test_result == POST_TEST_RESULT_FAILED)
1022     {
1023         acc_fail++;
1024     }
1025     platform_uart_init();
1026     platform_uart_set_baudrate(POST_UART_BAUDRATE);
1027     if (test_id != POST_TEST_PLL_INIT)
1028     {
1029         if (post_write_uart("\r\n\r\n") != TRUE)
1030         {
1031             post_display_led_error(POST_TEST_UART);   /* Never return from this function */
1032         }
1034         platform_get_info(&info);
1036         /* Display the board name */
1037         post_write_uart(info.board_name);
1039         /* Display the POST version */
1040         post_write_uart(POST_EVM_VERSION_MSG);
1041         post_write_uart(post_version);
1043         post_write_uart("\r\n\r------------------------------------------");
1044         post_write_uart("\r\n\rSOC Information");
1045         post_hex_to_string(info.board_rev, 4, msg);
1046         post_write_uart("\r\n\r\nFPGA Version: ");
1047         post_write_uart(msg);
1049         if (info.serial_nbr[0] != 0)
1050         {
1051             post_write_uart("\r\n\rBoard Serial Number: ");
1052             post_write_uart(info.serial_nbr);
1053         }
1055         /* Display the EFUSE MAC address */
1056         platform_get_macaddr(PLATFORM_MAC_TYPE_EFUSE, mac_addr);
1057         post_write_uart("\r\n\rEFUSE MAC ID is: ");
1058         for (i = 0; i < 6; i++)
1059         {
1060             post_hex_to_string(info.emac.efuse_mac_address[i], 2, msg);
1061             msg[2] = ' ';
1062             msg[3] = 0;
1063             post_write_uart(msg);
1064         }
1065 #if !(defined(_EVMC6657L_)||defined(_EVMC6655L_))
1066         sa_enable = *(volatile uint32_t *)0x20c0004;
1067         sa_enable &= 0x1;
1069         if (sa_enable)
1070         {
1071             post_write_uart("\r\n\rSA is enabled on this board.");
1072         }
1073         else
1074         {
1075             post_write_uart("\r\n\rSA is disabled on this board.");
1076         }
1077 #endif
1078         /* Read the PLL Reset Type Status register and display on UART */
1079         reset_type = PLL_CTRL_REG_RSTYPE;
1080         post_hex_to_string(reset_type, 8, msg);
1081         post_write_uart("\r\n\rPLL Reset Type Status Register: 0x");
1082         post_write_uart(msg);
1084         /* Dump Additional Information */
1085          post_dump_register_val ((uint32_t)&platform_init_return_code, "\r\n\rPlatform init return code: 0x");
1086          post_write_uart("\r\n\rAdditional Information: ");
1087          post_dump_register_val (0x02350014, "\r\n\r   (0x02350014) :");
1088          post_dump_register_val (0x02350624, "\r\n\r   (0x02350624) :");
1089          post_dump_register_val (0x02350678, "\r\n\r   (0x02350678) :");
1090          post_dump_register_val (0x0235063C, "\r\n\r   (0x0235063C) :");
1091          post_dump_register_val (0x02350640, "\r\n\r   (0x02350640) :");
1092          post_dump_register_val (0x02350644, "\r\n\r   (0x02350644) :");
1093          post_dump_register_val (0x02350648, "\r\n\r   (0x02350648) :");
1094          post_dump_register_val (0x0235064C, "\r\n\r   (0x0235064C) :");
1095          post_dump_register_val (0x02350650, "\r\n\r   (0x02350650) :");
1096          post_dump_register_val (0x02350654, "\r\n\r   (0x02350654) :");
1097          post_dump_register_val (0x02350658, "\r\n\r   (0x02350658) :");
1098          post_dump_register_val (0x0235065C, "\r\n\r   (0x0235065C) :");
1099          post_dump_register_val (0x02350660, "\r\n\r   (0x02350660) :");
1100          post_dump_register_val (0x02350668, "\r\n\r   (0x02350668) :");
1101          post_dump_register_val (0x02350670, "\r\n\r   (0x02350670) :");
1103          post_dump_register_val (0x02620008, "\r\n\r   (0x02620008) :");
1104          post_dump_register_val (0x0262000c, "\r\n\r   (0x0262000c) :");
1105          post_dump_register_val (0x02620010, "\r\n\r   (0x02620010) :");
1106          post_dump_register_val (0x02620014, "\r\n\r   (0x02620014) :");
1107          post_dump_register_val (0x02620018, "\r\n\r   (0x02620018) :");
1108          post_dump_register_val (0x02620180, "\r\n\r   (0x02620180) :");
1110          post_write_uart("\r\n\r------------------------------------------");
1111     }
1113     post_write_uart("\r\n\r\nPower On Self Test\n");
1115     /* Display test in progress UART/LED status or init error */
1116     post_display_status(test_id, POST_TEST_RESULT_STARTED);
1118     post_display_status(POST_TEST_EEPROM, POST_TEST_RESULT_STARTED);
1119     test_result = post_test_eeprom();
1120     if (test_result == POST_TEST_RESULT_FAILED)
1121     {
1122         acc_fail++;
1123     }
1124     post_display_status(POST_TEST_EEPROM, test_result);
1126     post_display_status(POST_TEST_NOR, POST_TEST_RESULT_STARTED);
1127     test_result = post_test_nor();
1128     if (test_result == POST_TEST_RESULT_FAILED)
1129     {
1130         acc_fail++;
1131     }
1132     post_display_status(POST_TEST_NOR, test_result);
1134     post_display_status(POST_TEST_NAND, POST_TEST_RESULT_STARTED);
1135     test_result = post_test_nand();
1136     if (test_result == POST_TEST_RESULT_FAILED)
1137     {
1138         acc_fail++;
1139     }
1140     post_display_status(POST_TEST_NAND, test_result);
1142 #if !(defined(_EVMC6678L_))
1143     post_display_status(POST_TEST_EMAC_LOOPBACK, POST_TEST_RESULT_STARTED);
1144     test_result = post_test_emac_loopback();
1145     if (test_result == POST_TEST_RESULT_FAILED)
1146     {
1147         acc_fail++;
1148     }
1149     post_display_status(POST_TEST_EMAC_LOOPBACK, test_result);
1150 #endif
1152     post_display_status(POST_TEST_DDR, POST_TEST_RESULT_STARTED);
1153     test_result = post_test_external_memory();
1154     if (test_result == POST_TEST_RESULT_FAILED)
1155     {
1156         acc_fail++;
1157     }
1158     post_display_status(POST_TEST_DDR, test_result);
1160     post_display_status(POST_TEST_COMPLETE, POST_TEST_RESULT_PASSED);
1162     if (acc_fail == 0)
1163     {
1164         post_write_uart("\r\n\r\nPOST result: PASS");
1165     }
1166     else
1167     {
1168         post_write_uart("\r\n\r\nPOST result: FAIL");
1169     }
1171     post_write_serial_no();