fsl_phy.h 11 KB

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