fc88e56ca7953ffa983ee7233a43eaf2c1b6e331
[keystone-rtos/mcsdk-tools.git] / post / src / evmc665x_sgmii.c
1 /******************************************************************************
2  * Copyright (c) 2011-2012 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 /******************************************************************************
35  *
36  * File Name:   evm665x_sgmii.c
37  *
38  * Description: This file contains the SGMII initialization API
39  *
40  ******************************************************************************/
42 #ifdef _EVMC6657L_
43 #include "platform.h"
44 #include <csl_sgmii.h>
45 /************************
46  * Defines and Macros
47  ************************/
49 #ifndef CSL_EMAC_0_REGS
50 #define CSL_EMAC_0_REGS                 (CSL_EMAC_SS_CFG_REGS)
51 #endif
53 #ifndef CSL_MDIO_0_REGS
54 #define CSL_MDIO_0_REGS                 (CSL_EMAC_SS_CFG_REGS + 0x0800)
55 #endif
57 #ifndef CSL_SGMII_0_REGS
58 #define CSL_SGMII_0_REGS                (CSL_EMAC_SS_CFG_REGS + 0x0900)
59 #endif
61 #define EMAC_RAM_BASE                   (CSL_EMAC_SS_CFG_REGS + 0x2000)
62 #define EMAC_RAM_LEN                    (0x00002000u)
64 /* MTU Size */
65 #define MAX_MTU_SIZE    1536
67 /* TX & RX Descriptor setup */
68 #define DESC_SIZE               sizeof(EMAC_Desc)
69 #define TX_DESC_START   EMAC_RAM_BASE
70 #define TX_DESC_COUNT   1
71 #define TX_DESC_ADD(x)  (TX_DESC_START + (DESC_SIZE * x))
72 #define TX_DESC_END             (TX_DESC_START + (DESC_SIZE * TX_DESC_COUNT))
73 #define RX_DESC_START   TX_DESC_END
74 #define RX_DESC_COUNT   1
75 #define RX_DESC_ADD(x)  (RX_DESC_START + (DESC_SIZE * x))
76 #define RX_DESC_END             (RX_DESC_START + (DESC_SIZE * RX_DESC_COUNT))
77 #define RX_BUFF_SIZE    MAX_MTU_SIZE
78 #define RX_BUFF_START   RX_DESC_END
79 #define RX_BUFF_ADD(x)  (RX_BUFF_START + (RX_BUFF_SIZE * x))
80 #define RX_BUFF_END             (RX_BUFF_START + (RX_BUFF_SIZE * RX_DESC_COUNT))
81 #define TX_BUFF_ADD             RX_BUFF_END
84 #define SGMII_SERDES_STS                        (*(unsigned int*)(CSL_BOOT_CFG_REGS + 0x158))
87 static SGMII_delay(int delay)
88 {
89         while(--delay);
90 }
92 Uint32 SGMII_reset ()
93 {
94         int timeout = 10000;
95         CSL_FINS(SGMII_REGS->SOFT_RESET, SGMII_SOFT_RESET_SOFT_RESET, 1);
97         while(--timeout && CSL_FEXT(SGMII_REGS->SOFT_RESET, SGMII_SOFT_RESET_SOFT_RESET));
99         return 0;
102 Uint32 SGMII_config (SGMII_Config *config)
104         int timeout;
106         if(!config)
107                 return SGMII_ERROR_INVALID;
109         timeout = 10000;
110         while(--timeout && !(SGMII_REGS->STATUS & SGMII_STATUS_LOCK))
111                 SGMII_delay(1000);
113         SGMII_REGS->CONTROL = 0;
114         if(config->loopbackEn) {
115                 CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MR_AN_ENABLE, 0);
116                 CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MASTER, 1);
117                 CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_LOOPBACK, config->loopbackEn);
118         } else {
119                 CSL_FINS(SGMII_REGS->SOFT_RESET, SGMII_SOFT_RESET_RT_SOFT_RESET, 1);
120                 if((config->masterEn) && (config->modeOfOperation == SGMII_MODE_OF_OPERATION_WITH_AN)) {
121                         CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MASTER, 1);
122                         CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MR_AN_ENABLE, 1);
123                         SGMII_REGS->MR_ADV_ABILITY = 0x01;
124                 } else if((config->masterEn) && (config->modeOfOperation == SGMII_MODE_OF_OPERATION_WITHOUT_AN)) {
125                         CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MASTER, 1);
126                         CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MR_AN_ENABLE, 0);
127                         SGMII_REGS->MR_ADV_ABILITY = 0x9801;
128                 } else {
129                         CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MASTER, 0);
130                         CSL_FINS(SGMII_REGS->CONTROL, SGMII_CONTROL_MR_AN_ENABLE, 1);
131                         SGMII_REGS->MR_ADV_ABILITY = 0x1;
132                 }
133                 CSL_FINS(SGMII_REGS->SOFT_RESET, SGMII_SOFT_RESET_RT_SOFT_RESET, 0);
134         }
136         SGMII_REGS->TX_CFG = config->txConfig;
137         SGMII_REGS->RX_CFG = config->rxConfig;
138         SGMII_REGS->AUX_CFG = config->auxConfig;
140         return 0;
143 Uint32 SGMII_getStatus (SGMII_Status *pStatus)
145         if(!pStatus)
146                 return SGMII_ERROR_INVALID;
148         pStatus->txCfgStatus = SGMII_REGS->TX_CFG;
149         pStatus->rxCfgStatus = SGMII_REGS->RX_CFG;
150         pStatus->auxCfgStatus = SGMII_REGS->AUX_CFG;
152         return 0;
155 Uint32 SGMII_getLinkStatus ()
157         int timeout = 1000;
158         while(--timeout && !(SGMII_REGS->STATUS & SGMII_STATUS_LOCK))
159                 SGMII_delay(1000);
160         if(!timeout)
161                 return SGMII_ERROR_DEVICE;
163         timeout = 1000;
164         while(--timeout && !(timeout & SGMII_STATUS_LINK)) {
165                 SGMII_delay(1000);
166         }
168         if(SGMII_REGS->STATUS & SGMII_STATUS_LINK)
169                 return 1;
170         else
171                 return 0;
174 Uint32 SGMII_getLinkPartnerStatus ()
176         int timeout = 1000;
177         while(--timeout && !(SGMII_REGS->STATUS & SGMII_STATUS_LOCK))
178                 SGMII_delay(1000);
179         if(!timeout)
180                 return SGMII_ERROR_DEVICE;
182         if(SGMII_REGS->STATUS & SGMII_STATUS_MR_AN_COMPLTE)
183                 return 1;
184         else
185                 return 0;
188 Uint32 SGMII_getAnErrorStatus ()
190         int timeout = 1000;
191         while(--timeout && !(SGMII_REGS->STATUS & SGMII_STATUS_LOCK))
192                 SGMII_delay(1000);
193         if(!timeout)
194                 return SGMII_ERROR_DEVICE;
196         if(SGMII_REGS->STATUS & SGMII_STATUS_AN_ERROR)
197                 return 1;
198         else
199                 return 0;
202 Uint32 SGMII_getStatusReg ()
204         return SGMII_REGS->STATUS;
207 Uint32 sgmii_init()
209         SGMII_Config sgmii_config;
211         sgmii_config.loopbackEn = 0;
212         sgmii_config.masterEn = 0;
213         sgmii_config.modeOfOperation = SGMII_MODE_OF_OPERATION_WITH_AN;
215         sgmii_config.txConfig = 0x108A1;
216         sgmii_config.rxConfig = 0x00700611;
217         sgmii_config.auxConfig = 0x51;
219         SGMII_reset();
221         SGMII_REGS->CONTROL = 0;
223         SGMII_config(&sgmii_config);
225         /* link status */
226         return !SGMII_getLinkStatus();
228 #endif