drv_ksz8081.c 12 KB

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