drv_ksz8081.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-10-14 wangqiang the first version
  9. * 2022-08-29 xjy198903 add rt1170 support
  10. */
  11. #include <rtthread.h>
  12. #ifdef PHY_USING_KSZ8081
  13. #include <rtdevice.h>
  14. #include "drv_gpio.h"
  15. #include "drv_mdio.h"
  16. /*******************************************************************************
  17. * Definitions
  18. ******************************************************************************/
  19. /*! @brief Defines the PHY registers. */
  20. #define PHY_BASICCONTROL_REG 0x00U /*!< The PHY basic control register. */
  21. #define PHY_BASICSTATUS_REG 0x01U /*!< The PHY basic status register. */
  22. #define PHY_ID1_REG 0x02U /*!< The PHY ID one register. */
  23. #define PHY_ID2_REG 0x03U /*!< The PHY ID two register. */
  24. #define PHY_AUTONEG_ADVERTISE_REG 0x04U /*!< The PHY auto-negotiate advertise register. */
  25. #define PHY_CONTROL1_REG 0x1EU /*!< The PHY control one register. */
  26. #define PHY_CONTROL2_REG 0x1FU /*!< The PHY control two register. */
  27. #define PHY_CONTROL_ID1 0x22U /*!< The PHY ID1*/
  28. /*! @brief Defines the mask flag in basic control register. */
  29. #define PHY_BCTL_DUPLEX_MASK 0x0100U /*!< The PHY duplex bit mask. */
  30. #define PHY_BCTL_RESTART_AUTONEG_MASK 0x0200U /*!< The PHY restart auto negotiation mask. */
  31. #define PHY_BCTL_AUTONEG_MASK 0x1000U /*!< The PHY auto negotiation bit mask. */
  32. #define PHY_BCTL_SPEED_MASK 0x2000U /*!< The PHY speed bit mask. */
  33. #define PHY_BCTL_LOOP_MASK 0x4000U /*!< The PHY loop bit mask. */
  34. #define PHY_BCTL_RESET_MASK 0x8000U /*!< The PHY reset bit mask. */
  35. #define PHY_BCTL_SPEED_100M_MASK 0x2000U /*!< The PHY 100M speed mask. */
  36. /*!@brief Defines the mask flag of operation mode in control two register*/
  37. #define PHY_CTL2_REMOTELOOP_MASK 0x0004U /*!< The PHY remote loopback mask. */
  38. #define PHY_CTL2_REFCLK_SELECT_MASK 0x0080U /*!< The PHY RMII reference clock select. */
  39. #define PHY_CTL1_10HALFDUPLEX_MASK 0x0001U /*!< The PHY 10M half duplex mask. */
  40. #define PHY_CTL1_100HALFDUPLEX_MASK 0x0002U /*!< The PHY 100M half duplex mask. */
  41. #define PHY_CTL1_10FULLDUPLEX_MASK 0x0005U /*!< The PHY 10M full duplex mask. */
  42. #define PHY_CTL1_100FULLDUPLEX_MASK 0x0006U /*!< The PHY 100M full duplex mask. */
  43. #define PHY_CTL1_SPEEDUPLX_MASK 0x0007U /*!< The PHY speed and duplex mask. */
  44. #define PHY_CTL1_ENERGYDETECT_MASK 0x10U /*!< The PHY signal present on rx differential pair. */
  45. #define PHY_CTL1_LINKUP_MASK 0x100U /*!< The PHY link up. */
  46. #define PHY_LINK_READY_MASK (PHY_CTL1_ENERGYDETECT_MASK | PHY_CTL1_LINKUP_MASK)
  47. /*! @brief Defines the mask flag in basic status register. */
  48. #define PHY_BSTATUS_LINKSTATUS_MASK 0x0004U /*!< The PHY link status mask. */
  49. #define PHY_BSTATUS_AUTONEGABLE_MASK 0x0008U /*!< The PHY auto-negotiation ability mask. */
  50. #define PHY_BSTATUS_AUTONEGCOMP_MASK 0x0020U /*!< The PHY auto-negotiation complete mask. */
  51. /*! @brief Defines the mask flag in PHY auto-negotiation advertise register. */
  52. #define PHY_100BaseT4_ABILITY_MASK 0x200U /*!< The PHY have the T4 ability. */
  53. #define PHY_100BASETX_FULLDUPLEX_MASK 0x100U /*!< The PHY has the 100M full duplex ability.*/
  54. #define PHY_100BASETX_HALFDUPLEX_MASK 0x080U /*!< The PHY has the 100M full duplex ability.*/
  55. #define PHY_10BASETX_FULLDUPLEX_MASK 0x040U /*!< The PHY has the 10M full duplex ability.*/
  56. #define PHY_10BASETX_HALFDUPLEX_MASK 0x020U /*!< The PHY has the 10M full duplex ability.*/
  57. /*! @brief Defines the timeout macro. */
  58. #define PHY_TIMEOUT_COUNT 0x3FFFFFFU
  59. /* defined the Reset pin, PORT and PIN config by menuconfig */
  60. #ifdef SOC_IMXRT1170_SERIES
  61. #define RESET_PIN GET_PIN(PHY_RESET_KSZ8081_PORT, PHY_RESET_KSZ8081_PIN)
  62. #else
  63. #define RESET_PIN GET_PIN(PHY_RESET_PORT, PHY_RESET_PIN)
  64. #endif
  65. /*******************************************************************************
  66. * Prototypes
  67. ******************************************************************************/
  68. /*******************************************************************************
  69. * Variables
  70. ******************************************************************************/
  71. static struct rt_phy_device phy_ksz8081;
  72. /*******************************************************************************
  73. * Code
  74. ******************************************************************************/
  75. static inline rt_bool_t read_reg(rt_mdio_t *bus, rt_uint32_t addr, rt_uint32_t reg_id, rt_uint32_t *value)
  76. {
  77. if (4 != bus->ops->read(bus, addr, reg_id, value, 4))
  78. {
  79. return RT_FALSE;
  80. }
  81. return RT_TRUE;
  82. }
  83. static inline rt_bool_t write_reg(rt_mdio_t *bus, rt_uint32_t addr, rt_uint32_t reg_id, rt_uint32_t value)
  84. {
  85. if (4 != bus->ops->write(bus, addr, reg_id, &value, 4))
  86. {
  87. return RT_FALSE;
  88. }
  89. return RT_TRUE;
  90. }
  91. static rt_phy_status rt_phy_init(void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz)
  92. {
  93. rt_bool_t ret;
  94. rt_phy_status result;
  95. rt_uint32_t counter = PHY_TIMEOUT_COUNT;
  96. rt_uint32_t id_reg = 0;
  97. rt_uint32_t time_delay;
  98. rt_uint32_t bss_reg;
  99. rt_uint32_t ctl_reg = 0;
  100. // reset phy device by gpio
  101. rt_pin_mode(RESET_PIN, PIN_MODE_OUTPUT);
  102. rt_pin_write(RESET_PIN, PIN_LOW);
  103. rt_thread_mdelay(100);
  104. rt_pin_write(RESET_PIN, PIN_HIGH);
  105. rt_mdio_t *mdio_bus = rt_hw_mdio_register(object, "phy_mdio");
  106. if (RT_NULL == mdio_bus)
  107. {
  108. return PHY_STATUS_FAIL;
  109. }
  110. phy_ksz8081.bus = mdio_bus;
  111. phy_ksz8081.addr = phy_addr;
  112. ret = mdio_bus->ops->init(mdio_bus, src_clock_hz);
  113. if ( !ret )
  114. {
  115. return PHY_STATUS_FAIL;
  116. }
  117. /* Initialization after PHY stars to work. */
  118. while ((id_reg != PHY_CONTROL_ID1) && (counter != 0))
  119. {
  120. phy_ksz8081.ops->read(PHY_ID1_REG, &id_reg);
  121. counter--;
  122. }
  123. if (!counter)
  124. {
  125. return PHY_STATUS_FAIL;
  126. }
  127. /* Reset PHY. */
  128. counter = PHY_TIMEOUT_COUNT;
  129. result = phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
  130. if (PHY_STATUS_OK == result)
  131. {
  132. #if defined(FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE)
  133. rt_uint32_t data = 0;
  134. result = phy_ksz8081.ops->read(PHY_CONTROL2_REG, &data);
  135. if (PHY_STATUS_FAIL == result)
  136. {
  137. return PHY_STATUS_FAIL;
  138. }
  139. result = phy_ksz8081.ops->write(PHY_CONTROL2_REG, (data | PHY_CTL2_REFCLK_SELECT_MASK));
  140. if (PHY_STATUS_FAIL == result)
  141. {
  142. return PHY_STATUS_FAIL;
  143. }
  144. #endif /* FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE */
  145. /* Set the negotiation. */
  146. result = phy_ksz8081.ops->write(PHY_AUTONEG_ADVERTISE_REG,
  147. (PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
  148. PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
  149. if (PHY_STATUS_OK == result)
  150. {
  151. result = phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, (PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
  152. if (PHY_STATUS_OK == result)
  153. {
  154. /* Check auto negotiation complete. */
  155. while (counter--)
  156. {
  157. result = phy_ksz8081.ops->read(PHY_BASICSTATUS_REG, &bss_reg);
  158. if (PHY_STATUS_OK == result)
  159. {
  160. phy_ksz8081.ops->read(PHY_CONTROL1_REG, &ctl_reg);
  161. if (((bss_reg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0) && (ctl_reg & PHY_LINK_READY_MASK))
  162. {
  163. /* Wait a moment for Phy status stable. */
  164. for (time_delay = 0; time_delay < PHY_TIMEOUT_COUNT; time_delay++)
  165. {
  166. __ASM("nop");
  167. }
  168. break;
  169. }
  170. }
  171. if (!counter)
  172. {
  173. return PHY_STATUS_FAIL;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. return PHY_STATUS_OK;
  180. }
  181. static rt_phy_status rt_phy_read(rt_uint32_t reg, rt_uint32_t *data)
  182. {
  183. rt_mdio_t *mdio_bus = phy_ksz8081.bus;
  184. rt_uint32_t device_id = phy_ksz8081.addr;
  185. if (read_reg(mdio_bus, device_id, reg, data))
  186. {
  187. return PHY_STATUS_OK;
  188. }
  189. return PHY_STATUS_FAIL;
  190. }
  191. static rt_phy_status rt_phy_write(rt_uint32_t reg, rt_uint32_t data)
  192. {
  193. rt_mdio_t *mdio_bus = phy_ksz8081.bus;
  194. rt_uint32_t device_id = phy_ksz8081.addr;
  195. if (write_reg(mdio_bus, device_id, reg, data))
  196. {
  197. return PHY_STATUS_OK;
  198. }
  199. return PHY_STATUS_FAIL;
  200. }
  201. static rt_phy_status rt_phy_loopback(rt_uint32_t mode, rt_uint32_t speed, rt_bool_t enable)
  202. {
  203. rt_uint32_t data = 0;
  204. rt_phy_status result;
  205. /* Set the loop mode. */
  206. if (enable)
  207. {
  208. if (PHY_LOCAL_LOOP == mode)
  209. {
  210. if (PHY_SPEED_100M == speed)
  211. {
  212. data = PHY_BCTL_SPEED_100M_MASK | PHY_BCTL_DUPLEX_MASK | PHY_BCTL_LOOP_MASK;
  213. }
  214. else
  215. {
  216. data = PHY_BCTL_DUPLEX_MASK | PHY_BCTL_LOOP_MASK;
  217. }
  218. return phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, data);
  219. }
  220. else
  221. {
  222. /* First read the current status in control register. */
  223. result = phy_ksz8081.ops->read(PHY_CONTROL2_REG, &data);
  224. if (PHY_STATUS_OK == result)
  225. {
  226. return phy_ksz8081.ops->write(PHY_CONTROL2_REG, (data | PHY_CTL2_REMOTELOOP_MASK));
  227. }
  228. }
  229. }
  230. else
  231. {
  232. /* Disable the loop mode. */
  233. if (PHY_LOCAL_LOOP == mode)
  234. {
  235. /* First read the current status in control register. */
  236. result = phy_ksz8081.ops->read(PHY_BASICCONTROL_REG, &data);
  237. if (PHY_STATUS_OK == result)
  238. {
  239. data &= ~PHY_BCTL_LOOP_MASK;
  240. return phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, (data | PHY_BCTL_RESTART_AUTONEG_MASK));
  241. }
  242. }
  243. else
  244. {
  245. /* First read the current status in control one register. */
  246. result = phy_ksz8081.ops->read(PHY_CONTROL2_REG, &data);
  247. if (PHY_STATUS_OK == result)
  248. {
  249. return phy_ksz8081.ops->write(PHY_CONTROL2_REG, (data & ~PHY_CTL2_REMOTELOOP_MASK));
  250. }
  251. }
  252. }
  253. return result;
  254. }
  255. static rt_phy_status get_link_status(rt_bool_t *status)
  256. {
  257. rt_phy_status result;
  258. rt_uint32_t data;
  259. /* Read the basic status register. */
  260. result = phy_ksz8081.ops->read(PHY_BASICSTATUS_REG, &data);
  261. if (PHY_STATUS_OK == result)
  262. {
  263. if (!(PHY_BSTATUS_LINKSTATUS_MASK & data))
  264. {
  265. /* link down. */
  266. *status = RT_FALSE;
  267. }
  268. else
  269. {
  270. /* link up. */
  271. *status = RT_TRUE;
  272. }
  273. }
  274. return result;
  275. }
  276. static rt_phy_status get_link_speed_duplex(rt_uint32_t *speed, rt_uint32_t *duplex)
  277. {
  278. rt_phy_status result = PHY_STATUS_OK;
  279. rt_uint32_t data, ctl_reg;
  280. /* Read the control two register. */
  281. result = phy_ksz8081.ops->read(PHY_CONTROL1_REG, &ctl_reg);
  282. if (PHY_STATUS_OK == result)
  283. {
  284. data = ctl_reg & PHY_CTL1_SPEEDUPLX_MASK;
  285. if ((PHY_CTL1_10FULLDUPLEX_MASK == data) || (PHY_CTL1_100FULLDUPLEX_MASK == data))
  286. {
  287. /* Full duplex. */
  288. *duplex = PHY_FULL_DUPLEX;
  289. }
  290. else
  291. {
  292. /* Half duplex. */
  293. *duplex = PHY_HALF_DUPLEX;
  294. }
  295. data = ctl_reg & PHY_CTL1_SPEEDUPLX_MASK;
  296. if ((PHY_CTL1_100HALFDUPLEX_MASK == data) || (PHY_CTL1_100FULLDUPLEX_MASK == data))
  297. {
  298. /* 100M speed. */
  299. *speed = PHY_SPEED_100M;
  300. }
  301. else
  302. { /* 10M speed. */
  303. *speed = PHY_SPEED_10M;
  304. }
  305. }
  306. return result;
  307. }
  308. static struct rt_phy_ops phy_ops =
  309. {
  310. .init = rt_phy_init,
  311. .read = rt_phy_read,
  312. .write = rt_phy_write,
  313. .loopback = rt_phy_loopback,
  314. .get_link_status = get_link_status,
  315. .get_link_speed_duplex = get_link_speed_duplex,
  316. };
  317. static int rt_phy_ksz8081_register( void )
  318. {
  319. phy_ksz8081.ops = &phy_ops;
  320. rt_hw_phy_register(&phy_ksz8081, "ksz8081");
  321. return 1;
  322. }
  323. INIT_DEVICE_EXPORT(rt_phy_ksz8081_register);
  324. #endif /* PHY_USING_KSZ8081 */