drv_ksz8081.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. */
  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. #define RESET_PIN GET_PIN(PHY_RESET_KSZ8081_PORT, PHY_RESET_KSZ8081_PIN)
  61. /*******************************************************************************
  62. * Prototypes
  63. ******************************************************************************/
  64. /*******************************************************************************
  65. * Variables
  66. ******************************************************************************/
  67. static struct rt_phy_device phy_ksz8081;
  68. /*******************************************************************************
  69. * Code
  70. ******************************************************************************/
  71. static inline rt_bool_t read_reg(rt_mdio_t *bus, rt_uint32_t addr, rt_uint32_t reg_id, rt_uint32_t *value)
  72. {
  73. if (4 != bus->ops->read(bus, addr, reg_id, value, 4))
  74. {
  75. return RT_FALSE;
  76. }
  77. return RT_TRUE;
  78. }
  79. static inline rt_bool_t write_reg(rt_mdio_t *bus, rt_uint32_t addr, rt_uint32_t reg_id, rt_uint32_t value)
  80. {
  81. if (4 != bus->ops->write(bus, addr, reg_id, &value, 4))
  82. {
  83. return RT_FALSE;
  84. }
  85. return RT_TRUE;
  86. }
  87. static rt_phy_status rt_phy_init(void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz)
  88. {
  89. rt_bool_t ret;
  90. rt_phy_status result;
  91. rt_uint32_t counter = PHY_TIMEOUT_COUNT;
  92. rt_uint32_t id_reg = 0;
  93. rt_uint32_t time_delay;
  94. rt_uint32_t bss_reg;
  95. rt_uint32_t ctl_reg = 0;
  96. // reset phy device by gpio
  97. rt_pin_mode(RESET_PIN, PIN_MODE_OUTPUT);
  98. rt_pin_write(RESET_PIN, PIN_LOW);
  99. rt_thread_mdelay(100);
  100. rt_pin_write(RESET_PIN, PIN_HIGH);
  101. rt_mdio_t *mdio_bus = rt_hw_mdio_register(object, "phy_mdio");
  102. if (RT_NULL == mdio_bus)
  103. {
  104. return PHY_STATUS_FAIL;
  105. }
  106. phy_ksz8081.bus = mdio_bus;
  107. phy_ksz8081.addr = phy_addr;
  108. ret = mdio_bus->ops->init(mdio_bus, src_clock_hz);
  109. if ( !ret )
  110. {
  111. return PHY_STATUS_FAIL;
  112. }
  113. /* Initialization after PHY stars to work. */
  114. while ((id_reg != PHY_CONTROL_ID1) && (counter != 0))
  115. {
  116. phy_ksz8081.ops->read(PHY_ID1_REG, &id_reg);
  117. counter--;
  118. }
  119. if (!counter)
  120. {
  121. return PHY_STATUS_FAIL;
  122. }
  123. /* Reset PHY. */
  124. counter = PHY_TIMEOUT_COUNT;
  125. result = phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
  126. if (PHY_STATUS_OK == result)
  127. {
  128. #if defined(FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE)
  129. rt_uint32_t data = 0;
  130. result = phy_ksz8081.ops->read(PHY_CONTROL2_REG, &data);
  131. if (PHY_STATUS_FAIL == result)
  132. {
  133. return PHY_STATUS_FAIL;
  134. }
  135. result = phy_ksz8081.ops->write(PHY_CONTROL2_REG, (data | PHY_CTL2_REFCLK_SELECT_MASK));
  136. if (PHY_STATUS_FAIL == result)
  137. {
  138. return PHY_STATUS_FAIL;
  139. }
  140. #endif /* FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE */
  141. /* Set the negotiation. */
  142. result = phy_ksz8081.ops->write(PHY_AUTONEG_ADVERTISE_REG,
  143. (PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
  144. PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
  145. if (PHY_STATUS_OK == result)
  146. {
  147. result = phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, (PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
  148. if (PHY_STATUS_OK == result)
  149. {
  150. /* Check auto negotiation complete. */
  151. while (counter--)
  152. {
  153. result = phy_ksz8081.ops->read(PHY_BASICSTATUS_REG, &bss_reg);
  154. if (PHY_STATUS_OK == result)
  155. {
  156. phy_ksz8081.ops->read(PHY_CONTROL1_REG, &ctl_reg);
  157. if (((bss_reg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0) && (ctl_reg & PHY_LINK_READY_MASK))
  158. {
  159. /* Wait a moment for Phy status stable. */
  160. for (time_delay = 0; time_delay < PHY_TIMEOUT_COUNT; time_delay++)
  161. {
  162. __ASM("nop");
  163. }
  164. break;
  165. }
  166. }
  167. if (!counter)
  168. {
  169. return PHY_STATUS_FAIL;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. return PHY_STATUS_OK;
  176. }
  177. static rt_phy_status rt_phy_read(rt_uint32_t reg, rt_uint32_t *data)
  178. {
  179. rt_mdio_t *mdio_bus = phy_ksz8081.bus;
  180. rt_uint32_t device_id = phy_ksz8081.addr;
  181. if (read_reg(mdio_bus, device_id, reg, data))
  182. {
  183. return PHY_STATUS_OK;
  184. }
  185. return PHY_STATUS_FAIL;
  186. }
  187. static rt_phy_status rt_phy_write(rt_uint32_t reg, rt_uint32_t data)
  188. {
  189. rt_mdio_t *mdio_bus = phy_ksz8081.bus;
  190. rt_uint32_t device_id = phy_ksz8081.addr;
  191. if (write_reg(mdio_bus, device_id, reg, data))
  192. {
  193. return PHY_STATUS_OK;
  194. }
  195. return PHY_STATUS_FAIL;
  196. }
  197. static rt_phy_status rt_phy_loopback(rt_uint32_t mode, rt_uint32_t speed, rt_bool_t enable)
  198. {
  199. rt_uint32_t data = 0;
  200. rt_phy_status result;
  201. /* Set the loop mode. */
  202. if (enable)
  203. {
  204. if (PHY_LOCAL_LOOP == mode)
  205. {
  206. if (PHY_SPEED_100M == speed)
  207. {
  208. data = PHY_BCTL_SPEED_100M_MASK | PHY_BCTL_DUPLEX_MASK | PHY_BCTL_LOOP_MASK;
  209. }
  210. else
  211. {
  212. data = PHY_BCTL_DUPLEX_MASK | PHY_BCTL_LOOP_MASK;
  213. }
  214. return phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, data);
  215. }
  216. else
  217. {
  218. /* First read the current status in control register. */
  219. result = phy_ksz8081.ops->read(PHY_CONTROL2_REG, &data);
  220. if (PHY_STATUS_OK == result)
  221. {
  222. return phy_ksz8081.ops->write(PHY_CONTROL2_REG, (data | PHY_CTL2_REMOTELOOP_MASK));
  223. }
  224. }
  225. }
  226. else
  227. {
  228. /* Disable the loop mode. */
  229. if (PHY_LOCAL_LOOP == mode)
  230. {
  231. /* First read the current status in control register. */
  232. result = phy_ksz8081.ops->read(PHY_BASICCONTROL_REG, &data);
  233. if (PHY_STATUS_OK == result)
  234. {
  235. data &= ~PHY_BCTL_LOOP_MASK;
  236. return phy_ksz8081.ops->write(PHY_BASICCONTROL_REG, (data | PHY_BCTL_RESTART_AUTONEG_MASK));
  237. }
  238. }
  239. else
  240. {
  241. /* First read the current status in control one register. */
  242. result = phy_ksz8081.ops->read(PHY_CONTROL2_REG, &data);
  243. if (PHY_STATUS_OK == result)
  244. {
  245. return phy_ksz8081.ops->write(PHY_CONTROL2_REG, (data & ~PHY_CTL2_REMOTELOOP_MASK));
  246. }
  247. }
  248. }
  249. return result;
  250. }
  251. static rt_phy_status get_link_status(rt_bool_t *status)
  252. {
  253. rt_phy_status result;
  254. rt_uint32_t data;
  255. /* Read the basic status register. */
  256. result = phy_ksz8081.ops->read(PHY_BASICSTATUS_REG, &data);
  257. if (PHY_STATUS_OK == result)
  258. {
  259. if (!(PHY_BSTATUS_LINKSTATUS_MASK & data))
  260. {
  261. /* link down. */
  262. *status = RT_FALSE;
  263. }
  264. else
  265. {
  266. /* link up. */
  267. *status = RT_TRUE;
  268. }
  269. }
  270. return result;
  271. }
  272. static rt_phy_status get_link_speed_duplex(rt_uint32_t *speed, rt_uint32_t *duplex)
  273. {
  274. rt_phy_status result = PHY_STATUS_OK;
  275. rt_uint32_t data, ctl_reg;
  276. /* Read the control two register. */
  277. result = phy_ksz8081.ops->read(PHY_CONTROL1_REG, &ctl_reg);
  278. if (PHY_STATUS_OK == result)
  279. {
  280. data = ctl_reg & PHY_CTL1_SPEEDUPLX_MASK;
  281. if ((PHY_CTL1_10FULLDUPLEX_MASK == data) || (PHY_CTL1_100FULLDUPLEX_MASK == data))
  282. {
  283. /* Full duplex. */
  284. *duplex = PHY_FULL_DUPLEX;
  285. }
  286. else
  287. {
  288. /* Half duplex. */
  289. *duplex = PHY_HALF_DUPLEX;
  290. }
  291. data = ctl_reg & PHY_CTL1_SPEEDUPLX_MASK;
  292. if ((PHY_CTL1_100HALFDUPLEX_MASK == data) || (PHY_CTL1_100FULLDUPLEX_MASK == data))
  293. {
  294. /* 100M speed. */
  295. *speed = PHY_SPEED_100M;
  296. }
  297. else
  298. { /* 10M speed. */
  299. *speed = PHY_SPEED_10M;
  300. }
  301. }
  302. return result;
  303. }
  304. static struct rt_phy_ops phy_ops =
  305. {
  306. .init = rt_phy_init,
  307. .read = rt_phy_read,
  308. .write = rt_phy_write,
  309. .loopback = rt_phy_loopback,
  310. .get_link_status = get_link_status,
  311. .get_link_speed_duplex = get_link_speed_duplex,
  312. };
  313. static int rt_phy_ksz8081_register( void )
  314. {
  315. phy_ksz8081.ops = &phy_ops;
  316. rt_hw_phy_register(&phy_ksz8081, "ksz8081");
  317. return 1;
  318. }
  319. INIT_DEVICE_EXPORT(rt_phy_ksz8081_register);
  320. #endif /* PHY_USING_KSZ8081 */