fsl_phy_fire.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * File : fsl_phy_fire.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-21 zylx first version
  23. */
  24. #include "fsl_phy_fire.h"
  25. #include <rtthread.h>
  26. #define DBG_ENABLE
  27. #define DBG_SECTION_NAME "[PHY]"
  28. #define DBG_COLOR
  29. #define DBG_LEVEL DBG_LOG
  30. #include <rtdbg.h>
  31. #define PHY_TIMEOUT_COUNT 0x3FFFFFFU
  32. extern uint32_t ENET_GetInstance(ENET_Type *base);
  33. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  34. /*! @brief Pointers to enet clocks for each instance. */
  35. extern clock_ip_name_t s_enetClock[FSL_FEATURE_SOC_ENET_COUNT];
  36. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  37. /*******************************************************************************
  38. * Code
  39. ******************************************************************************/
  40. status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
  41. {
  42. uint32_t bssReg;
  43. uint32_t i;
  44. uint32_t counter = PHY_TIMEOUT_COUNT;
  45. uint32_t idReg = 0;
  46. status_t result = kStatus_Success;
  47. uint32_t instance = ENET_GetInstance(base);
  48. uint32_t timeDelay;
  49. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  50. /* Set SMI first. */
  51. CLOCK_EnableClock(s_enetClock[instance]);
  52. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  53. ENET_SetSMI(base, srcClock_Hz, false);
  54. /* Initialization after PHY stars to work. */
  55. while ((idReg != PHY_CONTROL_ID1) && (counter != 0))
  56. {
  57. PHY_Read(base, phyAddr, PHY_ID1_REG, &idReg);
  58. counter --;
  59. }
  60. if (!counter)
  61. {
  62. return kStatus_Fail;
  63. }
  64. /* Reset PHY. */
  65. counter = PHY_TIMEOUT_COUNT;
  66. result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
  67. if (result == kStatus_Success)
  68. {
  69. for (i = 0x10000; i > 0; i--)
  70. {
  71. result = PHY_Read(base, phyAddr, PHY_BASICCONTROL_REG, &bssReg);
  72. if (!(bssReg & PHY_BCTL_POWER_DOWN_MASK))
  73. {
  74. break;
  75. }
  76. }
  77. if (i != 0)
  78. {
  79. /* Set the negotiation. */
  80. result = PHY_Write(base, phyAddr, PHY_AUTONEG_ADVERTISE_REG,
  81. (PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
  82. PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
  83. if (result == kStatus_Success)
  84. {
  85. result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG,
  86. (PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
  87. if (result == kStatus_Success)
  88. {
  89. /* Check auto negotiation complete. */
  90. while (counter --)
  91. {
  92. result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &bssReg);
  93. if (result == kStatus_Success)
  94. {
  95. if (((bssReg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0))
  96. {
  97. rt_thread_delay(1);
  98. }
  99. else
  100. {
  101. dbg_log(DBG_LOG, "auto negotiation complete success\n");
  102. break;
  103. }
  104. }
  105. }
  106. if (!counter)
  107. {
  108. dbg_log(DBG_LOG, "auto negotiation complete falied\n");
  109. return kStatus_PHY_AutoNegotiateFail;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. return result;
  116. }
  117. status_t PHY_Write(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data)
  118. {
  119. uint32_t counter;
  120. /* Clear the SMI interrupt event. */
  121. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  122. /* Starts a SMI write command. */
  123. ENET_StartSMIWrite(base, phyAddr, phyReg, kENET_MiiWriteValidFrame, data);
  124. /* Wait for SMI complete. */
  125. for (counter = PHY_TIMEOUT_COUNT; counter > 0; counter--)
  126. {
  127. if (ENET_GetInterruptStatus(base) & ENET_EIR_MII_MASK)
  128. {
  129. break;
  130. }
  131. }
  132. /* Check for timeout. */
  133. if (!counter)
  134. {
  135. return kStatus_PHY_SMIVisitTimeout;
  136. }
  137. /* Clear MII interrupt event. */
  138. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  139. return kStatus_Success;
  140. }
  141. status_t PHY_Read(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr)
  142. {
  143. assert(dataPtr);
  144. uint32_t counter;
  145. /* Clear the MII interrupt event. */
  146. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  147. /* Starts a SMI read command operation. */
  148. ENET_StartSMIRead(base, phyAddr, phyReg, kENET_MiiReadValidFrame);
  149. /* Wait for MII complete. */
  150. for (counter = PHY_TIMEOUT_COUNT; counter > 0; counter--)
  151. {
  152. if (ENET_GetInterruptStatus(base) & ENET_EIR_MII_MASK)
  153. {
  154. break;
  155. }
  156. }
  157. /* Check for timeout. */
  158. if (!counter)
  159. {
  160. return kStatus_PHY_SMIVisitTimeout;
  161. }
  162. /* Get data from MII register. */
  163. *dataPtr = ENET_ReadSMIData(base);
  164. /* Clear MII interrupt event. */
  165. ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK);
  166. return kStatus_Success;
  167. }
  168. status_t PHY_EnableLoopback(ENET_Type *base, uint32_t phyAddr, phy_loop_t mode, phy_speed_t speed, bool enable)
  169. {
  170. status_t result;
  171. uint32_t data = 0;
  172. /* Set the loop mode. */
  173. if (enable)
  174. {
  175. if (mode == kPHY_LocalLoop)
  176. {
  177. if (speed == kPHY_Speed100M)
  178. {
  179. data = PHY_BCTL_SPEED_100M_MASK | PHY_BCTL_DUPLEX_MASK | PHY_BCTL_LOOP_MASK;
  180. }
  181. else
  182. {
  183. data = PHY_BCTL_DUPLEX_MASK | PHY_BCTL_LOOP_MASK;
  184. }
  185. return PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, data);
  186. }
  187. else
  188. {
  189. /* First read the current status in control register. */
  190. result = PHY_Read(base, phyAddr, PHY_CONTROL2_REG, &data);
  191. if (result == kStatus_Success)
  192. {
  193. return PHY_Write(base, phyAddr, PHY_CONTROL2_REG, (data | PHY_CTL2_REMOTELOOP_MASK));
  194. }
  195. }
  196. }
  197. else
  198. {
  199. /* Disable the loop mode. */
  200. if (mode == kPHY_LocalLoop)
  201. {
  202. /* First read the current status in control register. */
  203. result = PHY_Read(base, phyAddr, PHY_BASICCONTROL_REG, &data);
  204. if (result == kStatus_Success)
  205. {
  206. data &= ~PHY_BCTL_LOOP_MASK;
  207. return PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, (data | PHY_BCTL_RESTART_AUTONEG_MASK));
  208. }
  209. }
  210. else
  211. {
  212. /* First read the current status in control one register. */
  213. result = PHY_Read(base, phyAddr, PHY_CONTROL2_REG, &data);
  214. if (result == kStatus_Success)
  215. {
  216. return PHY_Write(base, phyAddr, PHY_CONTROL2_REG, (data & ~PHY_CTL2_REMOTELOOP_MASK));
  217. }
  218. }
  219. }
  220. return result;
  221. }
  222. status_t PHY_GetLinkStatus(ENET_Type *base, uint32_t phyAddr, bool *status)
  223. {
  224. assert(status);
  225. status_t result = kStatus_Success;
  226. uint32_t data;
  227. /* Read the basic status register. */
  228. result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &data);
  229. if (result == kStatus_Success)
  230. {
  231. if (!(PHY_BSTATUS_LINKSTATUS_MASK & data))
  232. {
  233. /* link down. */
  234. *status = false;
  235. }
  236. else
  237. {
  238. /* link up. */
  239. *status = true;
  240. }
  241. }
  242. return result;
  243. }
  244. status_t PHY_GetLinkSpeedDuplex(ENET_Type *base, uint32_t phyAddr, phy_speed_t *speed, phy_duplex_t *duplex)
  245. {
  246. assert(duplex);
  247. status_t result = kStatus_Success;
  248. uint32_t data, ctlReg;
  249. /* Read the control two register. */
  250. result = PHY_Read(base, phyAddr, PHY_CONTROL2_REG, &ctlReg);
  251. if (result == kStatus_Success)
  252. {
  253. data = ctlReg & PHY_CTL1_SPEEDUPLX_MASK;
  254. if ((PHY_CTL1_10FULLDUPLEX_MASK == data) || (PHY_CTL1_100FULLDUPLEX_MASK == data))
  255. {
  256. /* Full duplex. */
  257. *duplex = kPHY_FullDuplex;
  258. }
  259. else
  260. {
  261. /* Half duplex. */
  262. *duplex = kPHY_HalfDuplex;
  263. }
  264. data = ctlReg & PHY_CTL1_SPEEDUPLX_MASK;
  265. if ((PHY_CTL1_100HALFDUPLEX_MASK == data) || (PHY_CTL1_100FULLDUPLEX_MASK == data))
  266. {
  267. /* 100M speed. */
  268. *speed = kPHY_Speed100M;
  269. }
  270. else
  271. {
  272. /* 10M speed. */
  273. *speed = kPHY_Speed10M;
  274. }
  275. }
  276. return result;
  277. }