drv_enet_phy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) 2023-2025 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-12-20 Jiading Optimization for all-in-one version
  9. * 2024-04-17 Jiading Support multiple PHYs
  10. */
  11. #include "rtthread.h"
  12. #if !defined(RT_USING_PHY)
  13. #error Please enable phy abstraction layer or remove the current file from your project!
  14. #endif
  15. #ifdef RT_USING_PHY
  16. #include <rtdevice.h>
  17. #include <rtdbg.h>
  18. #include "hpm_enet_drv.h"
  19. #include "drv_enet_phy.h"
  20. #include "hpm_enet_phy.h"
  21. #include "hpm_soc.h"
  22. #include "netif/ethernetif.h"
  23. #include "board.h"
  24. typedef struct
  25. {
  26. char *mdio_name;
  27. ENET_Type *instance;
  28. struct eth_device *eth_dev;
  29. phy_device_t *phy_dev;
  30. struct rt_mdio_bus *mdio_bus;
  31. } eth_phy_handle_t;
  32. typedef struct
  33. {
  34. uint8_t phy_handle_cnt;
  35. eth_phy_handle_t **phy_handle;
  36. } eth_phy_monitor_handle_t;
  37. #ifdef BSP_USING_ETH0
  38. extern struct eth_device eth0_dev;
  39. static struct rt_mdio_bus_ops mdio0_bus_ops;
  40. static struct rt_mdio_bus mdio0_bus = {.ops = &mdio0_bus_ops};
  41. static phy_device_t phy0_dev;
  42. static eth_phy_handle_t eth0_phy_handle =
  43. {
  44. .instance = HPM_ENET0,
  45. .eth_dev = &eth0_dev,
  46. .phy_dev = &phy0_dev,
  47. .mdio_name = "MDIO0",
  48. .mdio_bus = &mdio0_bus,
  49. };
  50. #endif
  51. #ifdef BSP_USING_ETH1
  52. extern struct eth_device eth1_dev;
  53. static struct rt_mdio_bus_ops mdio1_bus_ops;
  54. static struct rt_mdio_bus mdio1_bus = {.ops = &mdio1_bus_ops};
  55. static phy_device_t phy1_dev;
  56. static eth_phy_handle_t eth1_phy_handle =
  57. {
  58. .instance = HPM_ENET1,
  59. .eth_dev = &eth1_dev,
  60. .phy_dev = &phy1_dev,
  61. .mdio_name = "MDIO1",
  62. .mdio_bus = &mdio1_bus,
  63. };
  64. #endif
  65. static eth_phy_handle_t *s_gphys[] =
  66. {
  67. #ifdef BSP_USING_ETH0
  68. &eth0_phy_handle,
  69. #endif
  70. #ifdef BSP_USING_ETH1
  71. &eth1_phy_handle
  72. #endif
  73. };
  74. eth_phy_monitor_handle_t phy_monitor_handle =
  75. {
  76. .phy_handle_cnt = ARRAY_SIZE(s_gphys),
  77. .phy_handle = s_gphys
  78. };
  79. static struct rt_phy_ops phy_ops;
  80. static rt_phy_status phy_init(void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz)
  81. {
  82. #if defined(BSP_USING_ETH0) && defined(BSP_USING_ENET_PHY_DP83867)
  83. if ((ENET_Type *)object == HPM_ENET0)
  84. {
  85. dp83867_config_t phy_config;
  86. dp83867_reset((ENET_Type *)object);
  87. #if defined(__DISABLE_AUTO_NEGO) && __DISABLE_AUTO_NEGO
  88. dp83867_set_mdi_crossover_mode((ENET_Type *)object, enet_phy_mdi_crossover_manual_mdix);
  89. #endif
  90. dp83867_basic_mode_default_config((ENET_Type *)object, &phy_config);
  91. if (dp83867_basic_mode_init((ENET_Type *)object, &phy_config) == true) {
  92. return PHY_STATUS_OK;
  93. } else {
  94. return PHY_STATUS_FAIL;
  95. }
  96. }
  97. #endif
  98. #if defined(BSP_USING_ETH0) && defined(BSP_USING_ENET_PHY_RTL8211)
  99. if ((ENET_Type *)object == HPM_ENET0)
  100. {
  101. rtl8211_config_t phy_config;
  102. rtl8211_reset((ENET_Type *)object);
  103. rtl8211_basic_mode_default_config((ENET_Type *)object, &phy_config);
  104. if (rtl8211_basic_mode_init((ENET_Type *)object, &phy_config) == true) {
  105. return PHY_STATUS_OK;
  106. } else {
  107. return PHY_STATUS_FAIL;
  108. }
  109. }
  110. #endif
  111. #if defined(BSP_USING_ETH0) && defined(BSP_USING_ENET_PHY_RTL8201) && !defined(BSP_USING_ETH1)
  112. if ((ENET_Type *)object == HPM_ENET0)
  113. {
  114. rtl8201_config_t phy_config;
  115. rtl8201_reset((ENET_Type *)object);
  116. rtl8201_basic_mode_default_config((ENET_Type *)object, &phy_config);
  117. if (rtl8201_basic_mode_init((ENET_Type *)object, &phy_config) == true) {
  118. return PHY_STATUS_OK;
  119. } else {
  120. return PHY_STATUS_FAIL;
  121. }
  122. }
  123. #endif
  124. #if defined(BSP_USING_ETH1) && defined(BSP_USING_ENET_PHY_DP83848)
  125. if ((ENET_Type *)object == HPM_ENET1)
  126. {
  127. dp83848_config_t phy_config;
  128. dp83848_reset((ENET_Type *)object);
  129. dp83848_basic_mode_default_config((ENET_Type *)object, &phy_config);
  130. if (dp83848_basic_mode_init((ENET_Type *)object, &phy_config) == true) {
  131. return PHY_STATUS_OK;
  132. } else {
  133. return PHY_STATUS_FAIL;
  134. }
  135. }
  136. #endif
  137. #if defined(BSP_USING_ETH1) && defined(BSP_USING_ENET_PHY_RTL8201)
  138. if ((ENET_Type *)object == HPM_ENET1)
  139. {
  140. rtl8201_config_t phy_config;
  141. rtl8201_reset((ENET_Type *)object);
  142. rtl8201_basic_mode_default_config((ENET_Type *)object, &phy_config);
  143. if (rtl8201_basic_mode_init((ENET_Type *)object, &phy_config) == true) {
  144. return PHY_STATUS_OK;
  145. } else {
  146. return PHY_STATUS_FAIL;
  147. }
  148. }
  149. #endif
  150. #if defined(BSP_USING_ETH1) && defined(BSP_USING_ENET_PHY_LAN8720)
  151. if ((ENET_Type *)object == HPM_ENET1)
  152. {
  153. lan8720_config_t phy_config;
  154. lan8720_reset((ENET_Type *)object);
  155. lan8720_basic_mode_default_config((ENET_Type *)object, &phy_config);
  156. if (lan8720_basic_mode_init((ENET_Type *)object, &phy_config) == true) {
  157. return PHY_STATUS_OK;
  158. } else {
  159. return PHY_STATUS_FAIL;
  160. }
  161. }
  162. #endif
  163. }
  164. static rt_size_t phy_read(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size)
  165. {
  166. *(uint16_t *)data = enet_read_phy(((struct rt_mdio_bus *)bus)->hw_obj, addr, reg);
  167. return size;
  168. }
  169. static rt_size_t phy_write(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size)
  170. {
  171. enet_write_phy(((struct rt_mdio_bus *)bus)->hw_obj, addr, reg, *(uint16_t *)data);
  172. return size;
  173. }
  174. static rt_phy_status phy_get_link_status(rt_phy_t *phy, rt_bool_t *status)
  175. {
  176. enet_phy_status_t phy_status = {0};
  177. if (phy->bus->hw_obj == HPM_ENET0)
  178. {
  179. #if defined(__USE_DP83867) && __USE_DP83867
  180. dp83867_get_phy_status(phy->bus->hw_obj, &phy_status);
  181. #endif
  182. #if defined(__USE_RTL8211) && __USE_RTL8211
  183. rtl8211_get_phy_status(phy->bus->hw_obj, &phy_status);
  184. #endif
  185. #if defined(__USE_RTL8201) && __USE_RTL8201 && !defined(BSP_USING_ETH1)
  186. rtl8201_get_phy_status(phy->bus->hw_obj, &phy_status);
  187. #endif
  188. }
  189. #if defined(HPM_ENET1_BASE)
  190. if (phy->bus->hw_obj == HPM_ENET1)
  191. {
  192. #if defined(__USE_DP83848) && __USE_DP83848
  193. dp83848_get_phy_status(phy->bus->hw_obj, &phy_status);
  194. #endif
  195. #if defined(__USE_RTL8201) && __USE_RTL8201
  196. rtl8201_get_phy_status(phy->bus->hw_obj, &phy_status);
  197. #endif
  198. #if defined(__USE_LAN8720) && __USE_LAN8720
  199. lan8720_get_phy_status(phy->bus->hw_obj, &phy_status);
  200. #endif
  201. }
  202. #endif
  203. *status = phy_status.enet_phy_link;
  204. return PHY_STATUS_OK;
  205. }
  206. static rt_phy_status phy_get_link_speed_duplex(rt_phy_t *phy, rt_uint32_t *speed, rt_uint32_t *duplex)
  207. {
  208. enet_phy_status_t phy_status;
  209. if (phy->bus->hw_obj == HPM_ENET0)
  210. {
  211. #if defined(__USE_DP83867) && __USE_DP83867
  212. dp83867_get_phy_status(phy->bus->hw_obj, &phy_status);
  213. #endif
  214. #if defined(__USE_RTL8211) && __USE_RTL8211
  215. rtl8211_get_phy_status(phy->bus->hw_obj, &phy_status);
  216. #endif
  217. #if defined(__USE_RTL8201) && __USE_RTL8201 && !defined(BSP_USING_ETH1)
  218. rtl8201_get_phy_status(phy->bus->hw_obj, &phy_status);
  219. #endif
  220. }
  221. #if defined(HPM_ENET1_BASE)
  222. if (phy->bus->hw_obj == HPM_ENET1)
  223. {
  224. #if defined(__USE_DP83848) && __USE_DP83848
  225. dp83848_get_phy_status(phy->bus->hw_obj, &phy_status);
  226. #endif
  227. #if defined(__USE_RTL8201) && __USE_RTL8201
  228. rtl8201_get_phy_status(phy->bus->hw_obj, &phy_status);
  229. #endif
  230. #if defined(__USE_LAN8720) && __USE_LAN8720
  231. lan8720_get_phy_status(phy->bus->hw_obj, &phy_status);
  232. #endif
  233. }
  234. #endif
  235. *speed = phy_status.enet_phy_speed;
  236. *duplex = phy_status.enet_phy_duplex;
  237. return PHY_STATUS_OK;
  238. }
  239. static void phy_poll_status(void *parameter)
  240. {
  241. int ret;
  242. phy_info_t phy_info;
  243. rt_bool_t status;
  244. rt_device_t dev;
  245. rt_phy_msg_t msg;
  246. rt_uint32_t speed, duplex;
  247. phy_device_t *phy_dev;
  248. struct eth_device* eth_dev;
  249. char const *ps[] = {"10Mbps", "100Mbps", "1000Mbps"};
  250. enet_line_speed_t line_speed[] = {enet_line_speed_10mbps, enet_line_speed_100mbps, enet_line_speed_1000mbps};
  251. eth_phy_monitor_handle_t *phy_monitor_handle = (eth_phy_monitor_handle_t *)parameter;
  252. for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++)
  253. {
  254. eth_dev = phy_monitor_handle->phy_handle[i]->eth_dev;
  255. phy_dev = phy_monitor_handle->phy_handle[i]->phy_dev;
  256. phy_dev->phy.ops->get_link_status(&phy_dev->phy, &status);
  257. if (status)
  258. {
  259. phy_dev->phy.ops->get_link_speed_duplex(&phy_dev->phy, &phy_info.phy_speed, &phy_info.phy_duplex);
  260. ret = memcmp(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
  261. if (ret != 0)
  262. {
  263. memcpy(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
  264. }
  265. }
  266. if (phy_dev->phy_link != status)
  267. {
  268. phy_dev->phy_link = status ? PHY_LINK_UP : PHY_LINK_DOWN;
  269. eth_device_linkchange(eth_dev, status);
  270. LOG_I("%s", phy_dev->phy.bus->hw_obj == HPM_ENET0 ? "ENET0" : "ENET1");
  271. LOG_I("PHY Status: %s", status ? "Link up" : "Link down\n");
  272. if (status == PHY_LINK_UP)
  273. {
  274. LOG_I("PHY Speed: %s", ps[phy_dev->phy_info.phy_speed]);
  275. LOG_I("PHY Duplex: %s\n", phy_dev->phy_info.phy_duplex & PHY_FULL_DUPLEX ? "full duplex" : "half duplex");
  276. enet_set_line_speed(phy_monitor_handle->phy_handle[i]->instance, line_speed[phy_dev->phy_info.phy_speed]);
  277. enet_set_duplex_mode(phy_monitor_handle->phy_handle[i]->instance, phy_dev->phy_info.phy_duplex);
  278. }
  279. }
  280. }
  281. }
  282. static void phy_detection(void *parameter)
  283. {
  284. phy_device_t *phy_dev = (phy_device_t *)parameter;
  285. if (phy_dev->phy.ops->init(phy_dev->phy.bus->hw_obj, 0, PHY_MDIO_CSR_CLK_FREQ) != PHY_STATUS_OK)
  286. {
  287. LOG_E("No any PHY device is detected! Please check your hardware!\n");
  288. }
  289. return;
  290. }
  291. static void phy_monitor_thread_entry(void *args)
  292. {
  293. rt_timer_t phy_status_timer;
  294. eth_phy_monitor_handle_t *phy_monitor_handle = (eth_phy_monitor_handle_t *)args;
  295. for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++)
  296. {
  297. LOG_D("Detect a PHY%d\n", i);
  298. phy_detection(phy_monitor_handle->phy_handle[i]->phy_dev);
  299. }
  300. phy_status_timer = rt_timer_create("PHY_Monitor", phy_poll_status, phy_monitor_handle, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  301. if (!phy_status_timer || rt_timer_start(phy_status_timer) != RT_EOK)
  302. {
  303. LOG_E("Failed to start link change detection timer\n");
  304. }
  305. }
  306. int phy_device_register(void)
  307. {
  308. rt_err_t err = -RT_ERROR;
  309. rt_thread_t thread_phy_monitor;
  310. /* Set ops for PHY */
  311. phy_ops.init = phy_init;
  312. phy_ops.get_link_status = phy_get_link_status;
  313. phy_ops.get_link_speed_duplex = phy_get_link_speed_duplex;
  314. for (uint32_t i = 0; i < ARRAY_SIZE(s_gphys); i++)
  315. {
  316. /* Set PHY address */
  317. s_gphys[i]->phy_dev->phy.addr = 0xffff;
  318. /* Set MIDO bus */
  319. s_gphys[i]->mdio_bus->hw_obj = s_gphys[i]->instance;
  320. s_gphys[i]->mdio_bus->name = s_gphys[i]->mdio_name;
  321. s_gphys[i]->mdio_bus->ops->read = phy_read;
  322. s_gphys[i]->mdio_bus->ops->write = phy_write;
  323. s_gphys[i]->phy_dev->phy.bus = s_gphys[i]->mdio_bus;
  324. s_gphys[i]->phy_dev->phy.ops = &phy_ops;
  325. rt_hw_phy_register(&s_gphys[i]->phy_dev->phy, NULL);
  326. }
  327. /* Start PHY monitor */
  328. thread_phy_monitor = rt_thread_create("PHY Monitor", phy_monitor_thread_entry, &phy_monitor_handle, 1024, RT_THREAD_PRIORITY_MAX - 2, 2);
  329. if (thread_phy_monitor != RT_NULL)
  330. {
  331. rt_thread_startup(thread_phy_monitor);
  332. }
  333. else
  334. {
  335. err = -RT_ERROR;
  336. }
  337. return err;
  338. }
  339. INIT_PREV_EXPORT(phy_device_register);
  340. #endif /* RT_USING_PHY */