fsl_phy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include "fsl_phy.h"
  31. #include "fsl_gpio.h"
  32. #include "ioremap.h"
  33. #include <rtthread.h>
  34. /*******************************************************************************
  35. * Definitions
  36. ******************************************************************************/
  37. /*! @brief Defines the timeout macro. */
  38. #define PHY_TIMEOUT_COUNT 0x4FFFFFFU
  39. #define PHY_NEGOTIATION_DELAY 100
  40. #define PHY_ID 0X7
  41. /*******************************************************************************
  42. * Prototypes
  43. ******************************************************************************/
  44. /*!
  45. * @brief Get the ENET instance from peripheral base address.
  46. *
  47. * @param base ENET peripheral base address.
  48. * @return ENET instance.
  49. */
  50. extern uint32_t ENET_GetInstance(ENET_Type *base);
  51. /*******************************************************************************
  52. * Variables
  53. ******************************************************************************/
  54. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  55. /*! @brief Pointers to enet clocks for each instance. */
  56. extern clock_ip_name_t s_enetClock[FSL_FEATURE_SOC_ENET_COUNT];
  57. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  58. /*******************************************************************************
  59. * Code
  60. ******************************************************************************/
  61. status_t phy_reset(GPIO_Type *base,uint32_t pin)
  62. {
  63. GPIO_Type *gpio_base = NULL;
  64. gpio_pin_config_t sw_config =
  65. {
  66. kGPIO_DigitalOutput,
  67. 0,
  68. kGPIO_NoIntmode,
  69. };
  70. gpio_base = (GPIO_Type *)rt_ioremap((void *)base,0x1000);
  71. GPIO_PinInit(gpio_base, pin, &sw_config);
  72. GPIO_WritePinOutput(gpio_base,pin,0);
  73. rt_thread_delay(50);
  74. GPIO_WritePinOutput(gpio_base,pin,1);
  75. return kStatus_Success;
  76. }
  77. status_t PHY_StartNegotiation(ENET_Type *base, uint32_t phyAddr)
  78. {
  79. uint32_t counter = PHY_TIMEOUT_COUNT;
  80. status_t result = kStatus_Success;
  81. uint32_t bssReg;
  82. uint32_t timeDelay;
  83. result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
  84. if (result == kStatus_Success)
  85. {
  86. #if defined(FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE)
  87. uint32_t data = 0;
  88. result = PHY_Read(base, phyAddr, PHY_CONTROL2_REG, &data);
  89. if ( result != kStatus_Success)
  90. {
  91. return result;
  92. }
  93. result = PHY_Write(base, phyAddr, PHY_CONTROL2_REG, (data | PHY_CTL2_REFCLK_SELECT_MASK));
  94. if (result != kStatus_Success)
  95. {
  96. return result;
  97. }
  98. #endif /* FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE */
  99. /* Set the negotiation. */
  100. result = PHY_Write(base, phyAddr, PHY_AUTONEG_ADVERTISE_REG,
  101. (PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
  102. PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
  103. if (result == kStatus_Success)
  104. {
  105. result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG,
  106. (PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
  107. if (result == kStatus_Success)
  108. {
  109. /* Check auto negotiation complete. */
  110. while (counter --)
  111. {
  112. result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &bssReg);
  113. if ( result == kStatus_Success)
  114. {
  115. if ((bssReg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0)
  116. {
  117. /* Wait a moment for Phy status stable. */
  118. for (timeDelay = 0; timeDelay < PHY_TIMEOUT_COUNT; timeDelay ++)
  119. {
  120. __ASM("nop");
  121. }
  122. break;
  123. }
  124. }
  125. rt_thread_delay(PHY_NEGOTIATION_DELAY);
  126. if (!counter)
  127. {
  128. return kStatus_PHY_AutoNegotiateFail;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return kStatus_Success;
  135. }
  136. status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz,uint32_t phy_id)
  137. {
  138. uint32_t counter = PHY_TIMEOUT_COUNT;
  139. uint32_t idReg = 0;
  140. status_t result = kStatus_Success;
  141. ENET_SetSMI(base, srcClock_Hz, false);
  142. PHY_Read(base, phyAddr, PHY_ID1_REG, &idReg);
  143. while ((idReg != phy_id) && (counter != 0))
  144. {
  145. PHY_Read(base, phyAddr, PHY_ID1_REG, &idReg);
  146. counter --;
  147. }
  148. if (!counter)
  149. {
  150. return kStatus_Fail;
  151. }
  152. /* Reset PHY. */
  153. counter = PHY_TIMEOUT_COUNT;
  154. return result;
  155. }
  156. status_t PHY_Write(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data)
  157. {
  158. uint32_t counter;
  159. /* Clear the SMI interrupt event. */
  160. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  161. /* Starts a SMI write command. */
  162. ENET_StartSMIWrite(base, phyAddr, phyReg, kENET_MiiWriteValidFrame, data);
  163. /* Wait for SMI complete. */
  164. for (counter = PHY_TIMEOUT_COUNT; counter > 0; counter--)
  165. {
  166. if (ENET_GetInterruptStatus(base) & ENET_EIR_MII_MASK)
  167. {
  168. break;
  169. }
  170. }
  171. /* Check for timeout. */
  172. if (!counter)
  173. {
  174. return kStatus_PHY_SMIVisitTimeout;
  175. }
  176. /* Clear MII interrupt event. */
  177. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  178. return kStatus_Success;
  179. }
  180. status_t PHY_Read(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr)
  181. {
  182. RT_ASSERT(dataPtr);
  183. uint32_t counter;
  184. /* Clear the MII interrupt event. */
  185. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  186. /* Starts a SMI read command operation. */
  187. ENET_StartSMIRead(base, phyAddr, phyReg, kENET_MiiReadValidFrame);
  188. /* Wait for MII complete. */
  189. for (counter = PHY_TIMEOUT_COUNT; counter > 0; counter--)
  190. {
  191. if (ENET_GetInterruptStatus(base) & ENET_EIR_MII_MASK)
  192. {
  193. break;
  194. }
  195. }
  196. /* Check for timeout. */
  197. if (!counter)
  198. {
  199. return kStatus_PHY_SMIVisitTimeout;
  200. }
  201. /* Get data from MII register. */
  202. *dataPtr = ENET_ReadSMIData(base);
  203. /* Clear MII interrupt event. */
  204. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  205. return kStatus_Success;
  206. }
  207. status_t PHY_EnableLoopback(ENET_Type *base, uint32_t phyAddr, phy_loop_t mode, bool enable)
  208. {
  209. status_t result;
  210. uint32_t data = 0;
  211. /* Set the loop mode. */
  212. if (enable)
  213. {
  214. if (mode == kPHY_LocalLoop)
  215. {
  216. /* First read the current status in control register. */
  217. result = PHY_Read(base, phyAddr, PHY_BASICCONTROL_REG, &data);
  218. if (result == kStatus_Success)
  219. {
  220. return PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, (data | PHY_BCTL_LOOP_MASK));
  221. }
  222. }
  223. else
  224. {
  225. /* First read the current status in control register. */
  226. result = PHY_Read(base, phyAddr, PHY_CONTROL2_REG, &data);
  227. if (result == kStatus_Success)
  228. {
  229. return PHY_Write(base, phyAddr, PHY_CONTROL2_REG, (data | PHY_CTL2_REMOTELOOP_MASK));
  230. }
  231. }
  232. }
  233. else
  234. {
  235. /* Disable the loop mode. */
  236. if (mode == kPHY_LocalLoop)
  237. {
  238. /* First read the current status in the basic control register. */
  239. result = PHY_Read(base, phyAddr, PHY_BASICCONTROL_REG, &data);
  240. if (result == kStatus_Success)
  241. {
  242. return PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, (data & ~PHY_BCTL_LOOP_MASK));
  243. }
  244. }
  245. else
  246. {
  247. /* First read the current status in control one register. */
  248. result = PHY_Read(base, phyAddr, PHY_CONTROL2_REG, &data);
  249. if (result == kStatus_Success)
  250. {
  251. return PHY_Write(base, phyAddr, PHY_CONTROL2_REG, (data & ~PHY_CTL2_REMOTELOOP_MASK));
  252. }
  253. }
  254. }
  255. return result;
  256. }
  257. status_t PHY_GetLinkStatus(ENET_Type *base, uint32_t phyAddr, bool *status)
  258. {
  259. RT_ASSERT(status);
  260. status_t result = kStatus_Success;
  261. uint32_t data;
  262. /* Read the basic status register. */
  263. result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &data);
  264. if (result == kStatus_Success)
  265. {
  266. if (!(PHY_BSTATUS_LINKSTATUS_MASK & data))
  267. {
  268. /* link down. */
  269. *status = false;
  270. }
  271. else
  272. {
  273. /* link up. */
  274. *status = true;
  275. }
  276. }
  277. return result;
  278. }
  279. status_t PHY_GetLinkSpeedDuplex(ENET_Type *base, uint32_t phyAddr, phy_speed_t *speed, phy_duplex_t *duplex)
  280. {
  281. RT_ASSERT(duplex);
  282. status_t result = kStatus_Success;
  283. uint32_t data, ctlReg;
  284. /* Read the control two register. */
  285. result = PHY_Read(base, phyAddr, 31, &ctlReg);
  286. data = ((ctlReg>>2) & 0x7);
  287. switch (data)
  288. {
  289. case 1:
  290. *speed = kPHY_Speed10M;
  291. *duplex = kPHY_HalfDuplex;
  292. break;
  293. case 5:
  294. *speed = kPHY_Speed10M;
  295. *duplex = kPHY_FullDuplex;
  296. break;
  297. case 2:
  298. *speed = kPHY_Speed100M;
  299. *duplex = kPHY_HalfDuplex;
  300. break;
  301. case 6:
  302. *speed = kPHY_Speed100M;
  303. *duplex = kPHY_FullDuplex;
  304. break;
  305. default:
  306. *speed = kPHY_Speed100M;
  307. *duplex = kPHY_FullDuplex;
  308. }
  309. return result;
  310. }