|
| 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 | +/******************************************************************************* |
| 34 | + * Variables |
| 35 | + ******************************************************************************/ |
| 36 | + |
| 37 | +/*! @brief Define Phy API structure for MAC application*/ |
| 38 | +const enet_phy_api_t g_enetPhyApi = |
| 39 | +{ |
| 40 | + phy_auto_discover, |
| 41 | + phy_init, |
| 42 | + phy_get_link_speed, |
| 43 | + phy_get_link_status, |
| 44 | + phy_get_link_duplex, |
| 45 | +}; |
| 46 | +/******************************************************************************* |
| 47 | + * Code |
| 48 | + ******************************************************************************/ |
| 49 | +/*FUNCTION**************************************************************** |
| 50 | + * |
| 51 | + * Function Name: phy_init |
| 52 | + * Return Value: The execution status. |
| 53 | + * Description: Initialize Phy. |
| 54 | + * This interface provides initialize functions for Phy, This is called by enet |
| 55 | + * initialize function. Phy is usually deault auto-negotiation. so there is no |
| 56 | + * need to do the intialize about this. we just need to check the loop mode. |
| 57 | + *END*********************************************************************/ |
| 58 | +uint32_t phy_init(enet_dev_if_t * enetIfPtr) |
| 59 | +{ |
| 60 | + uint32_t data; |
| 61 | + uint32_t counter; |
| 62 | + uint32_t result; |
| 63 | + |
| 64 | + /* Check input parameters*/ |
| 65 | + if (!enetIfPtr) |
| 66 | + { |
| 67 | + return kStatus_PHY_InvaildInput; |
| 68 | + } |
| 69 | + |
| 70 | + /* Reset Phy*/ |
| 71 | + if ((result = (enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 72 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhySR,&data))) == kStatus_PHY_Success) |
| 73 | + { |
| 74 | + if ((data & kEnetPhyAutoNegAble) != 0) |
| 75 | + { |
| 76 | + /* Set Autonegotiation*/ |
| 77 | + enetIfPtr->macApiPtr->enet_mii_write(enetIfPtr->deviceNumber, |
| 78 | + enetIfPtr->phyCfgPtr->phyAddr, kEnetPhyCR, kEnetPhyAutoNeg); |
| 79 | + for (counter = 0; counter < kPhyTimeout; counter++) |
| 80 | + { |
| 81 | + if (enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 82 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhySR,&data)== kStatus_PHY_Success) |
| 83 | + { |
| 84 | + if ((data & kEnetPhyAutoNegComplete) != 0) |
| 85 | + { |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (counter == kPhyTimeout) |
| 92 | + { |
| 93 | + return kStatus_PHY_TimeOut; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (enetIfPtr->phyCfgPtr->isLoopEnabled) |
| 99 | + { |
| 100 | + /* First read the current status in control register*/ |
| 101 | + if (enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 102 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCR,&data)) |
| 103 | + { |
| 104 | + result = enetIfPtr->macApiPtr->enet_mii_write(enetIfPtr->deviceNumber, |
| 105 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCR,(data|kEnetPhyLoop)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +/*FUNCTION**************************************************************** |
| 113 | + * |
| 114 | + * Function Name: phy_auto_discover |
| 115 | + * Return Value: The execution status. |
| 116 | + * Description: Phy address auto discover. |
| 117 | + * This function provides a interface to get phy address using phy address auto |
| 118 | + * discovering, this interface is used when the phy address is unknown. |
| 119 | + *END*********************************************************************/ |
| 120 | +uint32_t phy_auto_discover(enet_dev_if_t * enetIfPtr) |
| 121 | +{ |
| 122 | + uint32_t addrIdx,data; |
| 123 | + uint32_t result = kStatus_PHY_Fail; |
| 124 | + |
| 125 | + /* Check input parameters*/ |
| 126 | + if (!enetIfPtr) |
| 127 | + { |
| 128 | + return kStatus_PHY_InvaildInput; |
| 129 | + } |
| 130 | + |
| 131 | + for (addrIdx = 0; addrIdx < 32; addrIdx++) |
| 132 | + { |
| 133 | + enetIfPtr->phyCfgPtr->phyAddr = addrIdx; |
| 134 | + result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 135 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyId1,&data); |
| 136 | + if ((result == kStatus_PHY_Success) && (data != 0) && (data != 0xffff) ) |
| 137 | + { |
| 138 | + return kStatus_PHY_Success; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return result; |
| 143 | +} |
| 144 | + |
| 145 | +/*FUNCTION**************************************************************** |
| 146 | + * |
| 147 | + * Function Name: phy_get_link_speed |
| 148 | + * Return Value: The execution status. |
| 149 | + * Description: Get phy link speed. |
| 150 | + * This function provides a interface to get link speed. |
| 151 | + *END*********************************************************************/ |
| 152 | +uint32_t phy_get_link_speed(enet_dev_if_t * enetIfPtr, enet_phy_speed_t *status) |
| 153 | +{ |
| 154 | + uint32_t result = kStatus_PHY_Success; |
| 155 | + uint32_t data; |
| 156 | + |
| 157 | + /* Check input parameters*/ |
| 158 | + if ((!enetIfPtr) || (!status)) |
| 159 | + { |
| 160 | + return kStatus_PHY_InvaildInput; |
| 161 | + } |
| 162 | + |
| 163 | + result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 164 | + enetIfPtr->phyCfgPtr->phyAddr, kEnetPhyCt2,&data); |
| 165 | + if (result == kStatus_PHY_Success) |
| 166 | + { |
| 167 | + data &= kEnetPhySpeedDulpexMask; |
| 168 | + if ((kEnetPhy100HalfDuplex == data) || (kEnetPhy100FullDuplex == data)) |
| 169 | + { |
| 170 | + *status = kEnetSpeed100M; |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + *status = kEnetSpeed10M; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return result; |
| 179 | +} |
| 180 | + |
| 181 | +/*FUNCTION**************************************************************** |
| 182 | + * |
| 183 | + * Function Name: phy_get_link_status |
| 184 | + * Return Value: The execution status. |
| 185 | + * Description: Get phy link status. |
| 186 | + * This function provides a interface to get link status to see if the link |
| 187 | + * status is on or off. |
| 188 | + *END*********************************************************************/ |
| 189 | + uint32_t phy_get_link_status(enet_dev_if_t * enetIfPtr, bool *status) |
| 190 | +{ |
| 191 | + uint32_t result = kStatus_PHY_Success; |
| 192 | + uint32_t data; |
| 193 | + |
| 194 | + /* Check input parameters*/ |
| 195 | + if ((!enetIfPtr) || (!status)) |
| 196 | + { |
| 197 | + return kStatus_PHY_InvaildInput; |
| 198 | + } |
| 199 | + |
| 200 | + result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 201 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCR,&data); |
| 202 | + if ((result == kStatus_PHY_Success) && (!(data & kEnetPhyReset))) |
| 203 | + { |
| 204 | + data = 0; |
| 205 | + result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 206 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhySR, &data); |
| 207 | + if (result == kStatus_PHY_Success) |
| 208 | + { |
| 209 | + if (!(kEnetPhyLinkStatus & data)) |
| 210 | + { |
| 211 | + *status = false; |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + *status = true; |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return result; |
| 221 | +} |
| 222 | + |
| 223 | +/*FUNCTION**************************************************************** |
| 224 | + * |
| 225 | + * Function Name: phy_get_link_duplex |
| 226 | + * Return Value: The execution status. |
| 227 | + * Description: Get phy link duplex. |
| 228 | + * This function provides a interface to get link duplex to see if the link |
| 229 | + * duplex is full or half. |
| 230 | + *END*********************************************************************/ |
| 231 | +uint32_t phy_get_link_duplex(enet_dev_if_t * enetIfPtr, enet_phy_duplex_t *status) |
| 232 | +{ |
| 233 | + uint32_t result = kStatus_PHY_Success; |
| 234 | + uint32_t data; |
| 235 | + |
| 236 | + /* Check input parameters*/ |
| 237 | + if ((!enetIfPtr) || (!status)) |
| 238 | + { |
| 239 | + return kStatus_PHY_InvaildInput; |
| 240 | + } |
| 241 | + |
| 242 | + result = enetIfPtr->macApiPtr->enet_mii_read(enetIfPtr->deviceNumber, |
| 243 | + enetIfPtr->phyCfgPtr->phyAddr,kEnetPhyCt2,&data); |
| 244 | + if (result == kStatus_PHY_Success) |
| 245 | + { |
| 246 | + data &= kEnetPhySpeedDulpexMask; |
| 247 | + if ((kEnetPhy10FullDuplex == data) || (kEnetPhy100FullDuplex == data)) |
| 248 | + { |
| 249 | + *status = kEnetFullDuplex; |
| 250 | + } |
| 251 | + else |
| 252 | + { |
| 253 | + *status = kEnetHalfDuplex; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + return result; |
| 258 | +} |
| 259 | + |
| 260 | + |
| 261 | +/******************************************************************************* |
| 262 | + * EOF |
| 263 | + ******************************************************************************/ |
| 264 | + |
0 commit comments