eth_phy_port.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2021-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-11 hpmicro First version
  9. */
  10. #include "rtthread.h"
  11. #ifdef RT_USING_PHY
  12. #include <rtdevice.h>
  13. #include <rtdbg.h>
  14. #include "hpm_enet_drv.h"
  15. #include "eth_phy_port.h"
  16. #include "hpm_soc.h"
  17. #include "netif/ethernetif.h"
  18. #include "board.h"
  19. typedef struct
  20. {
  21. char *mdio_name;
  22. ENET_Type *instance;
  23. struct eth_device *eth_dev;
  24. phy_device_t *phy_dev;
  25. struct rt_mdio_bus *mdio_bus;
  26. } eth_phy_handle_t;
  27. typedef struct
  28. {
  29. uint8_t phy_handle_cnt;
  30. eth_phy_handle_t **phy_handle;
  31. } eth_phy_monitor_handle_t;
  32. #ifdef BSP_USING_ETH0
  33. extern struct eth_device eth0_dev;
  34. static struct rt_mdio_bus mdio0_bus;
  35. static phy_device_t phy0_dev;
  36. static uint8_t phy0_reg_list[]= {PHY0_REG_LIST};
  37. static eth_phy_handle_t eth0_phy_handle =
  38. {
  39. .instance = HPM_ENET0,
  40. .eth_dev = &eth0_dev,
  41. .phy_dev = &phy0_dev,
  42. .mdio_name = "MDIO0",
  43. .mdio_bus = &mdio0_bus,
  44. };
  45. #endif
  46. #ifdef BSP_USING_ETH1
  47. extern struct eth_device eth1_dev;
  48. static struct rt_mdio_bus mdio1_bus;
  49. static phy_device_t phy1_dev;
  50. static uint8_t phy1_reg_list[]= {PHY1_REG_LIST};
  51. static eth_phy_handle_t eth1_phy_handle =
  52. {
  53. .instance = HPM_ENET1,
  54. .eth_dev = &eth1_dev,
  55. .phy_dev = &phy1_dev,
  56. .mdio_name = "MDIO1",
  57. .mdio_bus = &mdio1_bus,
  58. };
  59. #endif
  60. static eth_phy_handle_t *s_gphys[] =
  61. {
  62. #ifdef BSP_USING_ETH0
  63. &eth0_phy_handle,
  64. #endif
  65. #ifdef BSP_USING_ETH1
  66. &eth1_phy_handle
  67. #endif
  68. };
  69. static uint8_t *s_gphy_reg_list[] =
  70. {
  71. #ifdef BSP_USING_ETH0
  72. phy0_reg_list,
  73. #endif
  74. #ifdef BSP_USING_ETH1
  75. phy1_reg_list,
  76. #endif
  77. };
  78. eth_phy_monitor_handle_t phy_monitor_handle =
  79. {
  80. .phy_handle_cnt = ARRAY_SIZE(s_gphys),
  81. .phy_handle = s_gphys
  82. };
  83. static struct rt_phy_ops phy_ops;
  84. static rt_phy_status phy_init(void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz)
  85. {
  86. return PHY_STATUS_OK;
  87. }
  88. static rt_size_t phy_read(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size)
  89. {
  90. *(uint16_t *)data = enet_read_phy(((struct rt_mdio_bus *)bus)->hw_obj, addr, reg);
  91. return size;
  92. }
  93. static rt_size_t phy_write(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size)
  94. {
  95. enet_write_phy(((struct rt_mdio_bus *)bus)->hw_obj, addr, reg, *(uint16_t *)data);
  96. return size;
  97. }
  98. static rt_phy_status phy_get_link_status(rt_phy_t *phy, rt_bool_t *status)
  99. {
  100. uint16_t reg_status;
  101. reg_status = enet_read_phy(phy->bus->hw_obj, phy->addr, phy->reg_list[PHY_BASIC_STATUS_REG_IDX]);
  102. #if PHY_AUTO_NEGO
  103. reg_status &= PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK;
  104. *status = reg_status ? RT_TRUE : RT_FALSE;
  105. #else
  106. reg_status &= PHY_LINKED_STATUS_MASK;
  107. *status = reg_status ? RT_TRUE : RT_FALSE;
  108. #endif
  109. return PHY_STATUS_OK;
  110. }
  111. static rt_phy_status phy_get_link_speed_duplex(rt_phy_t *phy, rt_uint32_t *speed, rt_uint32_t *duplex)
  112. {
  113. uint16_t reg_status;
  114. reg_status = enet_read_phy(phy->bus->hw_obj, phy->addr, phy->reg_list[PHY_STATUS_REG_IDX]);
  115. *speed = PHY_STATUS_SPEED_100M(reg_status) ? PHY_SPEED_100M : PHY_SPEED_10M;
  116. *duplex = PHY_STATUS_FULL_DUPLEX(reg_status) ? PHY_FULL_DUPLEX: PHY_HALF_DUPLEX;
  117. return PHY_STATUS_OK;
  118. }
  119. static void phy_poll_status(void *parameter)
  120. {
  121. int ret;
  122. phy_info_t phy_info;
  123. rt_bool_t status;
  124. rt_device_t dev;
  125. rt_phy_msg_t msg;
  126. rt_uint32_t speed, duplex;
  127. phy_device_t *phy_dev;
  128. struct eth_device* eth_dev;
  129. char const *ps[] = {"10Mbps", "100Mbps", "1000Mbps"};
  130. enet_line_speed_t line_speed[] = {enet_line_speed_10mbps, enet_line_speed_100mbps, enet_line_speed_1000mbps};
  131. eth_phy_monitor_handle_t *phy_monitor_handle = (eth_phy_monitor_handle_t *)parameter;
  132. for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++)
  133. {
  134. eth_dev = phy_monitor_handle->phy_handle[i]->eth_dev;
  135. phy_dev = phy_monitor_handle->phy_handle[i]->phy_dev;
  136. phy_dev->phy.ops->get_link_status(&phy_dev->phy, &status);
  137. if (status)
  138. {
  139. phy_dev->phy.ops->get_link_speed_duplex(&phy_dev->phy, &phy_info.phy_speed, &phy_info.phy_duplex);
  140. ret = memcmp(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
  141. if (ret != 0)
  142. {
  143. memcpy(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
  144. }
  145. }
  146. if (phy_dev->phy_link != status)
  147. {
  148. phy_dev->phy_link = status ? PHY_LINK_UP : PHY_LINK_DOWN;
  149. eth_device_linkchange(eth_dev, status);
  150. LOG_I("PHY Status: %s", status ? "Link up" : "Link down\n");
  151. if (status == PHY_LINK_UP)
  152. {
  153. LOG_I("PHY Speed: %s", ps[phy_dev->phy_info.phy_speed]);
  154. LOG_I("PHY Duplex: %s\n", phy_dev->phy_info.phy_duplex & PHY_FULL_DUPLEX ? "full duplex" : "half duplex");
  155. enet_set_line_speed(phy_monitor_handle->phy_handle[i]->instance, line_speed[phy_dev->phy_info.phy_speed]);
  156. enet_set_duplex_mode(phy_monitor_handle->phy_handle[i]->instance, phy_dev->phy_info.phy_duplex);
  157. }
  158. }
  159. }
  160. }
  161. static void phy_detection(void *parameter)
  162. {
  163. uint8_t detected_count = 0;
  164. struct rt_phy_msg msg = {0, 0};
  165. phy_device_t *phy_dev = (phy_device_t *)parameter;
  166. rt_uint32_t i;
  167. msg.reg = phy_dev->phy.reg_list[PHY_ID1_REG_IDX];
  168. phy_dev->phy.ops->init(phy_dev->phy.bus->hw_obj, phy_dev->phy.addr, PHY_MDIO_CSR_CLK_FREQ);
  169. while(phy_dev->phy.addr == 0xffff)
  170. {
  171. /* Search a PHY */
  172. for (i = 0; i <= 0x1f; i++)
  173. {
  174. ((rt_phy_t *)(phy_dev->phy.parent.user_data))->addr = i;
  175. phy_dev->phy.parent.read(&(phy_dev->phy.parent), 0, &msg, 1);
  176. if (msg.value == PHY_ID1)
  177. {
  178. phy_dev->phy.addr = i;
  179. LOG_D("Found a PHY device[address:0x%02x].\n", phy_dev->phy.addr);
  180. return;
  181. }
  182. }
  183. phy_dev->phy.addr = 0xffff;
  184. detected_count++;
  185. rt_thread_mdelay(1000);
  186. if (detected_count > 3)
  187. {
  188. LOG_E("No any PHY device is detected! Please check your hardware!\n");
  189. return;
  190. }
  191. }
  192. }
  193. static void phy_monitor_thread_entry(void *args)
  194. {
  195. rt_timer_t phy_status_timer;
  196. eth_phy_monitor_handle_t *phy_monitor_handle = (eth_phy_monitor_handle_t *)args;
  197. for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++)
  198. {
  199. LOG_D("Detect a PHY%d\n", i);
  200. phy_detection(phy_monitor_handle->phy_handle[i]->phy_dev);
  201. }
  202. 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);
  203. if (!phy_status_timer || rt_timer_start(phy_status_timer) != RT_EOK)
  204. {
  205. LOG_E("Failed to start link change detection timer\n");
  206. }
  207. }
  208. int phy_device_register(void)
  209. {
  210. rt_err_t err = -RT_ERROR;
  211. rt_thread_t thread_phy_monitor;
  212. /* Set ops for PHY */
  213. phy_ops.init = phy_init;
  214. phy_ops.get_link_status = phy_get_link_status;
  215. phy_ops.get_link_speed_duplex = phy_get_link_speed_duplex;
  216. for (uint32_t i = 0; i < ARRAY_SIZE(s_gphys); i++)
  217. {
  218. /* Set PHY address */
  219. s_gphys[i]->phy_dev->phy.addr = 0xffff;
  220. /* Set MIDO bus */
  221. s_gphys[i]->mdio_bus->hw_obj = s_gphys[i]->instance;
  222. s_gphys[i]->mdio_bus->name = s_gphys[i]->mdio_name;
  223. s_gphys[i]->mdio_bus->ops->read = phy_read;
  224. s_gphys[i]->mdio_bus->ops->write = phy_write;
  225. s_gphys[i]->phy_dev->phy.bus = s_gphys[i]->mdio_bus;
  226. s_gphys[i]->phy_dev->phy.ops = &phy_ops;
  227. /* Set PHY register list */
  228. s_gphys[i]->phy_dev->phy.reg_list = s_gphy_reg_list[i];
  229. rt_hw_phy_register(&s_gphys[i]->phy_dev->phy, PHY_NAME);
  230. }
  231. /* Start PHY monitor */
  232. thread_phy_monitor = rt_thread_create("PHY Monitor", phy_monitor_thread_entry, &phy_monitor_handle, 1024, RT_THREAD_PRIORITY_MAX - 2, 2);
  233. if (thread_phy_monitor != RT_NULL)
  234. {
  235. rt_thread_startup(thread_phy_monitor);
  236. }
  237. else
  238. {
  239. err = -RT_ERROR;
  240. }
  241. return err;
  242. }
  243. INIT_PREV_EXPORT(phy_device_register);
  244. #endif /* RT_USING_PHY */