drv_enet_phy.c 12 KB

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