eth_phy_port.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2021 - 2022 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_uint32_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. eth_phy_monitor_handle_t *phy_monitor_handle = (eth_phy_monitor_handle_t *)parameter;
  131. for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++)
  132. {
  133. eth_dev = phy_monitor_handle->phy_handle[i]->eth_dev;
  134. phy_dev = phy_monitor_handle->phy_handle[i]->phy_dev;
  135. phy_dev->phy.ops->get_link_status(&phy_dev->phy, &status);
  136. if (status)
  137. {
  138. phy_dev->phy.ops->get_link_speed_duplex(&phy_dev->phy, &phy_info.phy_speed, &phy_info.phy_duplex);
  139. ret = memcmp(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
  140. if (ret != 0)
  141. {
  142. memcpy(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
  143. }
  144. }
  145. if (phy_dev->phy_link != status)
  146. {
  147. phy_dev->phy_link = status ? PHY_LINK_UP : PHY_LINK_DOWN;
  148. eth_device_linkchange(eth_dev, status);
  149. LOG_I("PHY Status: %s", status ? "Link up" : "Link down\n");
  150. if (status == PHY_LINK_UP)
  151. {
  152. LOG_I("PHY Speed: %s", ps[phy_dev->phy_info.phy_speed]);
  153. LOG_I("PHY Duplex: %s\n", phy_dev->phy_info.phy_duplex & PHY_FULL_DUPLEX ? "full duplex" : "half duplex");
  154. }
  155. }
  156. }
  157. }
  158. static void phy_detection(void *parameter)
  159. {
  160. uint8_t detected_count = 0;
  161. struct rt_phy_msg msg = {0, 0};
  162. phy_device_t *phy_dev = (phy_device_t *)parameter;
  163. rt_uint32_t i;
  164. msg.reg = phy_dev->phy.reg_list[PHY_ID1_REG_IDX];
  165. phy_dev->phy.ops->init(phy_dev->phy.bus->hw_obj, phy_dev->phy.addr, PHY_MDIO_CSR_CLK_FREQ);
  166. while(phy_dev->phy.addr == 0xffff)
  167. {
  168. /* Search a PHY */
  169. for (i = 0; i <= 0x1f; i++)
  170. {
  171. ((rt_phy_t *)(phy_dev->phy.parent.user_data))->addr = i;
  172. phy_dev->phy.parent.read(&(phy_dev->phy.parent), 0, &msg, 1);
  173. if (msg.value == PHY_ID1)
  174. {
  175. phy_dev->phy.addr = i;
  176. LOG_D("Found a PHY device[address:0x%02x].\n", phy_dev->phy.addr);
  177. return;
  178. }
  179. }
  180. phy_dev->phy.addr = 0xffff;
  181. detected_count++;
  182. rt_thread_mdelay(1000);
  183. if (detected_count > 3)
  184. {
  185. LOG_E("No any PHY device is detected! Please check your hardware!\n");
  186. return;
  187. }
  188. }
  189. }
  190. static void phy_monitor_thread_entry(void *args)
  191. {
  192. rt_timer_t phy_status_timer;
  193. eth_phy_monitor_handle_t *phy_monitor_handle = (eth_phy_monitor_handle_t *)args;
  194. for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++)
  195. {
  196. LOG_D("Detect a PHY%d\n", i);
  197. phy_detection(phy_monitor_handle->phy_handle[i]->phy_dev);
  198. }
  199. 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);
  200. if (!phy_status_timer || rt_timer_start(phy_status_timer) != RT_EOK)
  201. {
  202. LOG_E("Failed to start link change detection timer\n");
  203. }
  204. }
  205. int phy_device_register(void)
  206. {
  207. rt_err_t err = RT_ERROR;
  208. rt_thread_t thread_phy_monitor;
  209. /* Set ops for PHY */
  210. phy_ops.init = phy_init;
  211. phy_ops.get_link_status = phy_get_link_status;
  212. phy_ops.get_link_speed_duplex = phy_get_link_speed_duplex;
  213. for (uint32_t i = 0; i < ARRAY_SIZE(s_gphys); i++)
  214. {
  215. /* Set PHY address */
  216. s_gphys[i]->phy_dev->phy.addr = 0xffff;
  217. /* Set MIDO bus */
  218. s_gphys[i]->mdio_bus->hw_obj = s_gphys[i]->instance;
  219. s_gphys[i]->mdio_bus->name = s_gphys[i]->mdio_name;
  220. s_gphys[i]->mdio_bus->ops->read = phy_read;
  221. s_gphys[i]->mdio_bus->ops->write = phy_write;
  222. s_gphys[i]->phy_dev->phy.bus = s_gphys[i]->mdio_bus;
  223. s_gphys[i]->phy_dev->phy.ops = &phy_ops;
  224. /* Set PHY register list */
  225. s_gphys[i]->phy_dev->phy.reg_list = s_gphy_reg_list[i];
  226. rt_hw_phy_register(&s_gphys[i]->phy_dev->phy, PHY_NAME);
  227. }
  228. /* Start PHY monitor */
  229. thread_phy_monitor = rt_thread_create("PHY Monitor", phy_monitor_thread_entry, &phy_monitor_handle, 1024, RT_THREAD_PRIORITY_MAX - 2, 2);
  230. if (thread_phy_monitor != RT_NULL)
  231. {
  232. rt_thread_startup(thread_phy_monitor);
  233. }
  234. else
  235. {
  236. err = RT_ERROR;
  237. }
  238. return err;
  239. }
  240. INIT_PREV_EXPORT(phy_device_register);
  241. #endif /* RT_USING_PHY */