fsl_phy.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  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 the copyright holder 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. #ifndef _FSL_PHY_H_
  31. #define _FSL_PHY_H_
  32. #include "fsl_enet.h"
  33. /*!
  34. * @addtogroup phy_driver
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @brief PHY driver version */
  41. #define FSL_PHY_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0. */
  42. /*! @brief Defines the PHY registers. */
  43. #define PHY_BASICCONTROL_REG 0x00U /*!< The PHY basic control register. */
  44. #define PHY_BASICSTATUS_REG 0x01U /*!< The PHY basic status register. */
  45. #define PHY_ID1_REG 0x02U /*!< The PHY ID one register. */
  46. #define PHY_ID2_REG 0x03U /*!< The PHY ID two register. */
  47. #define PHY_AUTONEG_ADVERTISE_REG 0x04U /*!< The PHY auto-negotiate advertise register. */
  48. #define PHY_CONTROL1_REG 0x1EU /*!< The PHY control one register. */
  49. #define PHY_CONTROL2_REG 0x1FU /*!< The PHY control two register. */
  50. #define PHY_CONTROL_ID1 0x22U /*!< The PHY ID1*/
  51. /*! @brief Defines the mask flag in basic control register. */
  52. #define PHY_BCTL_DUPLEX_MASK 0x0100U /*!< The PHY duplex bit mask. */
  53. #define PHY_BCTL_RESTART_AUTONEG_MASK 0x0200U /*!< The PHY restart auto negotiation mask. */
  54. #define PHY_BCTL_AUTONEG_MASK 0x1000U /*!< The PHY auto negotiation bit mask. */
  55. #define PHY_BCTL_SPEED_MASK 0x2000U /*!< The PHY speed bit mask. */
  56. #define PHY_BCTL_LOOP_MASK 0x4000U /*!< The PHY loop bit mask. */
  57. #define PHY_BCTL_RESET_MASK 0x8000U /*!< The PHY reset bit mask. */
  58. /*!@brief Defines the mask flag of operation mode in control two register*/
  59. #define PHY_CTL2_REMOTELOOP_MASK 0x0004U /*!< The PHY remote loopback mask. */
  60. #define PHY_CTL1_10HALFDUPLEX_MASK 0x0001U /*!< The PHY 10M half duplex mask. */
  61. #define PHY_CTL1_100HALFDUPLEX_MASK 0x0002U /*!< The PHY 100M half duplex mask. */
  62. #define PHY_CTL1_10FULLDUPLEX_MASK 0x0005U /*!< The PHY 10M full duplex mask. */
  63. #define PHY_CTL1_100FULLDUPLEX_MASK 0x0006U /*!< The PHY 100M full duplex mask. */
  64. #define PHY_CTL1_SPEEDUPLX_MASK 0x0007U /*!< The PHY speed and duplex mask. */
  65. /*! @brief Defines the mask flag in basic status register. */
  66. #define PHY_BSTATUS_LINKSTATUS_MASK 0x0004U /*!< The PHY link status mask. */
  67. #define PHY_BSTATUS_AUTONEGABLE_MASK 0x0008U /*!< The PHY auto-negotiation ability mask. */
  68. #define PHY_BSTATUS_AUTONEGCOMP_MASK 0x0020U /*!< The PHY auto-negotiation complete mask. */
  69. /*! @brief Defines the mask flag in PHY auto-negotiation advertise register. */
  70. #define PHY_100BaseT4_ABILITY_MASK 0x200U /*!< The PHY have the T4 ability. */
  71. #define PHY_100BASETX_FULLDUPLEX_MASK 0x100U /*!< The PHY has the 100M full duplex ability.*/
  72. #define PHY_100BASETX_HALFDUPLEX_MASK 0x080U /*!< The PHY has the 100M full duplex ability.*/
  73. #define PHY_10BASETX_FULLDUPLEX_MASK 0x040U /*!< The PHY has the 10M full duplex ability.*/
  74. #define PHY_10BASETX_HALFDUPLEX_MASK 0x020U /*!< The PHY has the 10M full duplex ability.*/
  75. /*! @brief Defines the PHY status. */
  76. enum _phy_status
  77. {
  78. kStatus_PHY_SMIVisitTimeout = MAKE_STATUS(kStatusGroup_PHY, 1), /*!< ENET PHY SMI visit timeout. */
  79. kStatus_PHY_AutoNegotiateFail = MAKE_STATUS(kStatusGroup_PHY, 2) /*!< ENET PHY AutoNegotiate Fail. */
  80. };
  81. /*! @brief Defines the PHY link speed. This is align with the speed for ENET MAC. */
  82. typedef enum _phy_speed
  83. {
  84. kPHY_Speed10M = 0U, /*!< ENET PHY 10M speed. */
  85. kPHY_Speed100M /*!< ENET PHY 100M speed. */
  86. } phy_speed_t;
  87. /*! @brief Defines the PHY link duplex. */
  88. typedef enum _phy_duplex
  89. {
  90. kPHY_HalfDuplex = 0U, /*!< ENET PHY half duplex. */
  91. kPHY_FullDuplex /*!< ENET PHY full duplex. */
  92. } phy_duplex_t;
  93. /*! @brief Defines the PHY loopback mode. */
  94. typedef enum _phy_loop
  95. {
  96. kPHY_LocalLoop = 0U, /*!< ENET PHY local loopback. */
  97. kPHY_RemoteLoop /*!< ENET PHY remote loopback. */
  98. } phy_loop_t;
  99. /*******************************************************************************
  100. * API
  101. ******************************************************************************/
  102. #if defined(__cplusplus)
  103. extern "C" {
  104. #endif
  105. /*!
  106. * @name PHY Driver
  107. * @{
  108. */
  109. /*!
  110. * @brief Initializes PHY.
  111. *
  112. * This function initialize the SMI interface and initialize PHY.
  113. * The SMI is the MII management interface between PHY and MAC, which should be
  114. * firstly initialized before any other operation for PHY.
  115. *
  116. * @param base ENET peripheral base address.
  117. * @param phyAddr The PHY address.
  118. * @param srcClock_Hz The module clock frequency - system clock for MII management interface - SMI.
  119. * @retval kStatus_Success PHY initialize success
  120. * @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
  121. * @retval kStatus_PHY_AutoNegotiateFail PHY auto negotiate fail
  122. */
  123. status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz);
  124. /*!
  125. * @brief PHY Write function. This function write data over the SMI to
  126. * the specified PHY register. This function is called by all PHY interfaces.
  127. *
  128. * @param base ENET peripheral base address.
  129. * @param phyAddr The PHY address.
  130. * @param phyReg The PHY register.
  131. * @param data The data written to the PHY register.
  132. * @retval kStatus_Success PHY write success
  133. * @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
  134. */
  135. status_t PHY_Write(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data);
  136. /*!
  137. * @brief PHY Read function. This interface read data over the SMI from the
  138. * specified PHY register. This function is called by all PHY interfaces.
  139. *
  140. * @param base ENET peripheral base address.
  141. * @param phyAddr The PHY address.
  142. * @param phyReg The PHY register.
  143. * @param dataPtr The address to store the data read from the PHY register.
  144. * @retval kStatus_Success PHY read success
  145. * @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
  146. */
  147. status_t PHY_Read(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr);
  148. /*!
  149. * @brief Enables/disables PHY loopback.
  150. *
  151. * @param base ENET peripheral base address.
  152. * @param phyAddr The PHY address.
  153. * @param mode The loopback mode to be enabled, please see "phy_loop_t".
  154. * the two loopback mode should not be both set. when one loopback mode is set
  155. * the other one should be disabled.
  156. * @param enable True to enable, false to disable.
  157. * @retval kStatus_Success PHY loopback success
  158. * @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
  159. */
  160. status_t PHY_EnableLoopback(ENET_Type *base, uint32_t phyAddr, phy_loop_t mode, bool enable);
  161. /*!
  162. * @brief Gets the PHY link status.
  163. *
  164. * @param base ENET peripheral base address.
  165. * @param phyAddr The PHY address.
  166. * @param status The link up or down status of the PHY.
  167. * - true the link is up.
  168. * - false the link is down.
  169. * @retval kStatus_Success PHY get link status success
  170. * @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
  171. */
  172. status_t PHY_GetLinkStatus(ENET_Type *base, uint32_t phyAddr, bool *status);
  173. /*!
  174. * @brief Gets the PHY link speed and duplex.
  175. *
  176. * @param base ENET peripheral base address.
  177. * @param phyAddr The PHY address.
  178. * @param speed The address of PHY link speed.
  179. * @param duplex The link duplex of PHY.
  180. * @retval kStatus_Success PHY get link speed and duplex success
  181. * @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
  182. */
  183. status_t PHY_GetLinkSpeedDuplex(ENET_Type *base, uint32_t phyAddr, phy_speed_t *speed, phy_duplex_t *duplex);
  184. /* @} */
  185. #if defined(__cplusplus)
  186. }
  187. #endif
  188. /*! @}*/
  189. #endif /* _FSL_PHY_H_ */