]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/common/phyksz8081/fsl_phy_driver.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / common / phyksz8081 / fsl_phy_driver.c
1 /*
2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 *   of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 *   list of conditions and the following disclaimer in the documentation and/or
13 *   other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 *   contributors may be used to endorse or promote products derived from this
17 *   software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "fsl_phy_driver.h"
32
33 #ifndef MBED_NO_ENET
34
35 /*******************************************************************************
36  * Variables
37  ******************************************************************************/
38
39 /*! @brief Define Phy API structure for MAC application*/
40 const enet_phy_api_t g_enetPhyApi = 
41 {
42     phy_auto_discover,
43     phy_init,
44     phy_get_link_speed,
45     phy_get_link_status,
46     phy_get_link_duplex,
47 };
48 /*******************************************************************************
49  * Code
50  ******************************************************************************/
51 /*FUNCTION****************************************************************
52  *
53  * Function Name: phy_init
54  * Return Value: The execution status.
55  * Description: Initialize Phy.
56  * This interface provides initialize functions for Phy, This is called by enet  
57  * initialize function. Phy is usually deault auto-negotiation. so there is no 
58  * need to do the intialize about this. we just need to check the loop mode.
59  *END*********************************************************************/
60 uint32_t phy_init(enet_dev_if_t * enetIfPtr)
61 {
62     uint32_t data;
63     uint32_t counter;
64     uint32_t result;
65    
66     /* Check input parameters*/
67     if (!enetIfPtr)
68     {
69         return kStatus_PHY_InvaildInput;
70     }
71
72     /* Reset Phy*/
73     if ((result = (enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
74             enetIfPtr->phyCfgPtr->phyAddr,kEnetPhySR,&data))) == kStatus_PHY_Success)
75     {
76         if ((data & kEnetPhyAutoNegAble) != 0)
77         {
78             /* Set Autonegotiation*/
79             enetIfPtr->macApiPtr->enet_mii_write(enetIfPtr->deviceNumber, 
80                 enetIfPtr->phyCfgPtr->phyAddr, kEnetPhyCR, kEnetPhyAutoNeg);
81             for (counter = 0; counter < kPhyTimeout; counter++)
82             {
83                 if (enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
84                        enetIfPtr->phyCfgPtr->phyAddr,kEnetPhySR,&data)== kStatus_PHY_Success)
85                 {
86                     if ((data & kEnetPhyAutoNegComplete) != 0)
87                     {
88                         break;
89                     }
90                 }                                   
91             }
92
93             if (counter == kPhyTimeout)
94             {
95                 return kStatus_PHY_TimeOut;
96             }
97         }
98     }
99
100     if (enetIfPtr->phyCfgPtr->isLoopEnabled)
101     {
102         /* First read the current status in control register*/ 
103         if (enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
104             enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCR,&data))
105         {
106             result = enetIfPtr->macApiPtr->enet_mii_write(enetIfPtr->deviceNumber, 
107                 enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCR,(data|kEnetPhyLoop));
108         }               
109     }
110
111     return result;
112 }
113
114 /*FUNCTION****************************************************************
115  *
116  * Function Name: phy_auto_discover
117  * Return Value: The execution status.
118  * Description: Phy address auto discover.
119  * This function provides a interface to get phy address using phy address auto 
120  * discovering, this interface is used when the phy address is unknown.
121  *END*********************************************************************/
122 uint32_t phy_auto_discover(enet_dev_if_t * enetIfPtr)
123 {
124     uint32_t addrIdx,data;
125     uint32_t result = kStatus_PHY_Fail;
126         
127     /* Check input parameters*/
128     if (!enetIfPtr)
129     {
130         return kStatus_PHY_InvaildInput;
131     }
132
133     for (addrIdx = 0; addrIdx < 32; addrIdx++)
134     {
135         enetIfPtr->phyCfgPtr->phyAddr = addrIdx;
136         result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber,
137             enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyId1,&data);
138         if ((result == kStatus_PHY_Success) && (data != 0) && (data != 0xffff) )
139         {
140             return kStatus_PHY_Success;
141         }
142     }
143
144     return result;
145 }
146
147 /*FUNCTION****************************************************************
148  *
149  * Function Name: phy_get_link_speed
150  * Return Value: The execution status.
151  * Description: Get phy link speed.
152  * This function provides a interface to get link speed.
153  *END*********************************************************************/
154 uint32_t phy_get_link_speed(enet_dev_if_t * enetIfPtr, enet_phy_speed_t *status)
155 {
156     uint32_t result = kStatus_PHY_Success;
157     uint32_t data;
158         
159     /* Check input parameters*/
160     if ((!enetIfPtr) || (!status))
161     {
162         return kStatus_PHY_InvaildInput;
163     }
164
165     result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
166         enetIfPtr->phyCfgPtr->phyAddr, kEnetPhyCt2,&data);
167     if (result == kStatus_PHY_Success)
168     {
169         data &= kEnetPhySpeedDulpexMask; 
170         if ((kEnetPhy100HalfDuplex == data) || (kEnetPhy100FullDuplex == data))
171         {
172             *status = kEnetSpeed100M;
173         }
174         else
175         {
176             *status = kEnetSpeed10M;
177         }
178     }
179
180     return result;
181 }
182
183 /*FUNCTION****************************************************************
184  *
185  * Function Name: phy_get_link_status
186  * Return Value: The execution status.
187  * Description: Get phy link status.
188  * This function provides a interface to get link status to see if the link 
189  * status is on or off.
190  *END*********************************************************************/
191  uint32_t phy_get_link_status(enet_dev_if_t * enetIfPtr, bool *status)
192 {
193     uint32_t result = kStatus_PHY_Success;
194     uint32_t data;
195         
196     /* Check input parameters*/
197     if ((!enetIfPtr) || (!status))
198     {
199         return kStatus_PHY_InvaildInput;
200     }
201
202     result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
203         enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCR,&data);
204     if ((result == kStatus_PHY_Success) && (!(data & kEnetPhyReset)))
205     {
206         data = 0;
207         result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
208             enetIfPtr->phyCfgPtr->phyAddr,kEnetPhySR, &data);
209         if (result == kStatus_PHY_Success)
210         {
211             if (!(kEnetPhyLinkStatus & data))
212             {
213                 *status = false;
214             }
215             else
216             {
217                 *status = true;
218             }
219         }
220     }
221
222     return result;     
223 }
224
225 /*FUNCTION****************************************************************
226  *
227  * Function Name: phy_get_link_duplex
228  * Return Value: The execution status.
229  * Description: Get phy link duplex.
230  * This function provides a interface to get link duplex to see if the link 
231  * duplex is full or half.
232  *END*********************************************************************/
233 uint32_t phy_get_link_duplex(enet_dev_if_t * enetIfPtr, enet_phy_duplex_t *status)
234 {
235     uint32_t result = kStatus_PHY_Success;
236     uint32_t data;
237         
238     /* Check input parameters*/
239     if ((!enetIfPtr) || (!status))
240     {
241         return kStatus_PHY_InvaildInput;
242     }
243
244     result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, 
245         enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCt2,&data);
246     if (result == kStatus_PHY_Success)
247     {
248         data &= kEnetPhySpeedDulpexMask; 
249         if ((kEnetPhy10FullDuplex == data) || (kEnetPhy100FullDuplex == data))
250         {
251             *status = kEnetFullDuplex;
252         }
253         else
254         {
255             *status = kEnetHalfDuplex;
256         }
257     }
258
259     return result;
260 }
261
262 #endif
263
264 /*******************************************************************************
265  * EOF
266  ******************************************************************************/
267