phyksz8081.c 12 KB

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