drv_eth.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024/04/25 flyingcys first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #define DBG_TAG "drv.eth"
  15. #define DBG_LEVEL DBG_INFO
  16. #include <rtdbg.h>
  17. #include <lwip/sys.h>
  18. #include <netif/ethernetif.h>
  19. #include "drv_eth.h"
  20. // #define ETH_TX_DUMP
  21. // #define ETH_RX_DUMP
  22. #define MAX_ADDR_LEN 6
  23. struct _dw_eth
  24. {
  25. rt_uint32_t *base;
  26. rt_uint32_t irq;
  27. struct eth_device parent; /* inherit from ethernet device */
  28. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* interface address info, hw address */
  29. struct rt_semaphore rx_sem;
  30. };
  31. static struct _dw_eth dw_eth_device = {0};
  32. #define GMAC_BUF_LEN (1500 + 20)
  33. static uint8_t g_mac_addr[6] = {0xf2, 0x42, 0x9f, 0xa5, 0x0a, 0x72};
  34. static uint8_t g_mac_phy_init_finish = 0;
  35. static eth_mac_handle_t g_mac_handle;
  36. static eth_phy_handle_t g_phy_handle;
  37. static uint8_t SendDataBuf[GMAC_BUF_LEN];
  38. static uint8_t RecvDataBuf[GMAC_BUF_LEN];
  39. static void cvi_ephy_id_init(void)
  40. {
  41. // set rg_ephy_apb_rw_sel 0x0804@[0]=1/APB by using APB interface
  42. mmio_write_32(0x03009804, 0x0001);
  43. // Release 0x0800[0]=0/shutdown
  44. mmio_write_32(0x03009800, 0x0900);
  45. // Release 0x0800[2]=1/dig_rst_n, Let mii_reg can be accessabile
  46. mmio_write_32(0x03009800, 0x0904);
  47. // PHY_ID
  48. mmio_write_32(0x03009008, 0x0043);
  49. mmio_write_32(0x0300900c, 0x5649);
  50. // switch to MDIO control by ETH_MAC
  51. mmio_write_32(0x03009804, 0x0000);
  52. }
  53. static int cvi_eth_mac_phy_enable(uint32_t enable)
  54. {
  55. eth_mac_addr_t addr;
  56. int32_t ret;
  57. if ((g_mac_phy_init_finish == 0) && enable)
  58. {
  59. /* startup mac */
  60. ret = cvi_eth_mac_control(g_mac_handle, CSI_ETH_MAC_CONFIGURE, 1);
  61. if (ret != 0)
  62. {
  63. LOG_E("Failed to control mac");
  64. return -1;
  65. }
  66. /* Start up the PHY */
  67. ret = cvi_eth_phy_power_control(g_phy_handle, CSI_ETH_POWER_FULL);
  68. if (ret != 0)
  69. {
  70. LOG_E("Failed to control phy, ret:0x%d", ret);
  71. return -1;
  72. }
  73. }
  74. /* enable mac TX/RX */
  75. ret = cvi_eth_mac_control(g_mac_handle, CSI_ETH_MAC_CONTROL_TX, enable ? 1 : 0);
  76. if (ret != 0)
  77. {
  78. LOG_E("Failed to enable mac TX");
  79. return ret;
  80. }
  81. ret = cvi_eth_mac_control(g_mac_handle, CSI_ETH_MAC_CONTROL_RX, enable ? 1 : 0);
  82. if (ret != 0)
  83. {
  84. LOG_E("Failed to enable mac RX");
  85. return ret;
  86. }
  87. /* set mac address */
  88. memcpy(addr.b, g_mac_addr, sizeof(g_mac_addr));
  89. ret = cvi_eth_mac_set_macaddr(g_mac_handle, &addr);
  90. if (ret != 0)
  91. {
  92. LOG_E("Failed to set mac address");
  93. return -1;
  94. }
  95. /* adjust mac link parameter */
  96. ret = cvi_eth_mac_control(g_mac_handle, DRV_ETH_MAC_ADJUST_LINK, 1);
  97. if (ret != 0)
  98. {
  99. LOG_E("Failed to adjust link");
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static int32_t fn_phy_read(uint8_t phy_addr, uint8_t reg_addr, uint16_t *data)
  105. {
  106. return dw_eth_mac_phy_read(g_mac_handle, phy_addr, reg_addr, data);
  107. }
  108. static int32_t fn_phy_write(uint8_t phy_addr, uint8_t reg_addr, uint16_t data)
  109. {
  110. return dw_eth_mac_phy_write(g_mac_handle, phy_addr, reg_addr, data);
  111. }
  112. static void dw_gmac_handler_irq(int vector, void *param)
  113. {
  114. gmac_dev_t *mac_dev = (gmac_dev_t *)param;
  115. struct dw_gmac_dma_regs *dma_reg = mac_dev->priv->dma_regs_p;
  116. uint32_t dma_status;
  117. uint32_t event = 0;
  118. /* no ephy or ephy link down */
  119. if (!mac_dev->phy_dev || !mac_dev->phy_dev->link_state)
  120. return;
  121. /* read and clear dma interrupt */
  122. dma_status = dma_reg->status;
  123. /* Clear the interrupt by writing a logic 1 to the CSR5[15-0] */
  124. dma_reg->status = dma_status & 0x1ffff;
  125. if (dma_status & CVI_DMA_STATUS_RI)
  126. {
  127. LOG_D("CVI_DMA_STATUS_RI");
  128. /* a frame has been received */
  129. eth_device_ready(&(dw_eth_device.parent));
  130. }
  131. if (dma_status & CVI_DMA_STATUS_TI)
  132. {
  133. LOG_D("CVI_DMA_STATUS_TI");
  134. }
  135. if (dma_status & CVI_DMA_STATUS_ERI)
  136. {
  137. LOG_D("CVI_DMA_STATUS_TI");
  138. }
  139. }
  140. static rt_err_t rt_dw_eth_init(rt_device_t dev)
  141. {
  142. struct _dw_eth *dw_eth;
  143. struct eth_device *eth_dev;
  144. RT_ASSERT(dev != RT_NULL);
  145. eth_dev = rt_container_of(dev, struct eth_device, parent);
  146. if (eth_dev == RT_NULL)
  147. return -RT_ERROR;
  148. dw_eth = rt_container_of(eth_dev, struct _dw_eth, parent);
  149. if (dw_eth == RT_NULL)
  150. return -RT_ERROR;
  151. /* init phy id */
  152. cvi_ephy_id_init();
  153. /* initialize MAC & PHY */
  154. g_mac_handle = cvi_eth_mac_init(dw_eth->base);
  155. if (g_mac_handle == NULL)
  156. return -RT_ERROR;
  157. g_phy_handle = cvi_eth_phy_init(fn_phy_read, fn_phy_write);
  158. dw_eth_mac_connect_phy(g_mac_handle, g_phy_handle);
  159. if (cvi_eth_mac_phy_enable(1))
  160. {
  161. LOG_E("PHY MAC init fail");
  162. return -RT_ERROR;
  163. }
  164. rt_hw_interrupt_install(dw_eth->irq, dw_gmac_handler_irq, g_mac_handle, "e0");
  165. rt_hw_interrupt_umask(dw_eth->irq);
  166. /* change device link status */
  167. eth_device_linkchange(&(dw_eth_device.parent), RT_TRUE);
  168. return RT_EOK;
  169. }
  170. static rt_err_t rt_dw_eth_control(rt_device_t dev, int cmd, void *args)
  171. {
  172. switch (cmd)
  173. {
  174. case NIOCTL_GADDR:
  175. if (args)
  176. rt_memcpy(args, g_mac_addr, MAX_ADDR_LEN);
  177. break;
  178. default:
  179. break;
  180. }
  181. return RT_EOK;
  182. }
  183. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  184. static void packet_dump(const char *msg, const struct pbuf *p)
  185. {
  186. const struct pbuf *q;
  187. rt_uint32_t i, j;
  188. rt_uint8_t *ptr;
  189. rt_kprintf("%s %d byte\n", msg, p->tot_len);
  190. i = 0;
  191. for (q = p; q != RT_NULL; q = q->next)
  192. {
  193. ptr = q->payload;
  194. for (j = 0; j < q->len; j++)
  195. {
  196. if ((i % 8) == 0)
  197. {
  198. rt_kprintf(" ");
  199. }
  200. if ((i % 16) == 0)
  201. {
  202. rt_kprintf("\r\n");
  203. }
  204. rt_kprintf("%02x ", *ptr);
  205. i ++;
  206. ptr ++;
  207. }
  208. }
  209. rt_kprintf("\n\n");
  210. }
  211. #endif
  212. struct pbuf* rt_dw_eth_rx(rt_device_t dev)
  213. {
  214. struct pbuf *p = NULL;
  215. struct pbuf *q = NULL;
  216. uint32_t i = 0;
  217. int32_t len = cvi_eth_mac_read_frame(g_mac_handle, RecvDataBuf, GMAC_BUF_LEN);
  218. if((len <= 0) || (len > GMAC_BUF_LEN))
  219. {
  220. return NULL;
  221. }
  222. #if RT_LWIP_ETH_PAD_SIZE
  223. len += RT_LWIP_ETH_PAD_SIZE; /* allow room for Ethernet padding */
  224. #endif
  225. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  226. if (p == NULL)
  227. {
  228. LOG_E("eth_rx: pbuf_alloc failed");
  229. len = 0;
  230. return NULL;
  231. }
  232. #if RT_LWIP_ETH_PAD_SIZE
  233. pbuf_header(p, -RT_LWIP_ETH_PAD_SIZE); /* drop the padding word */
  234. #endif
  235. /* We iterate over the pbuf chain until we have read the entire
  236. * packet into the pbuf. */
  237. for (q = p; q != NULL; q = q->next)
  238. {
  239. /* Read enough bytes to fill this pbuf in the chain. The
  240. * available data in the pbuf is given by the q->len
  241. * variable.
  242. * This does not necessarily have to be a memcpy, you can also preallocate
  243. * pbufs for a DMA-enabled MAC and after receiving truncate it to the
  244. * actually received size. In this case, ensure the tot_len member of the
  245. * pbuf is the sum of the chained pbuf len members.
  246. */
  247. memcpy((u8_t*)q->payload, (u8_t*)&RecvDataBuf[i], q->len);
  248. i = i + q->len;
  249. }
  250. if((i != p->tot_len) || (i > len))
  251. {
  252. return NULL;
  253. }
  254. #ifdef ETH_RX_DUMP
  255. packet_dump("RX dump", p);
  256. #endif /* ETH_RX_DUMP */
  257. return p;
  258. }
  259. rt_err_t rt_dw_eth_tx(rt_device_t dev, struct pbuf* p)
  260. {
  261. rt_err_t ret = RT_EOK;
  262. #ifdef ETH_TX_DUMP
  263. packet_dump("send", p);
  264. #endif
  265. struct pbuf *q;
  266. uint32_t len = 0;
  267. #if RT_LWIP_ETH_PAD_SIZE
  268. pbuf_header(p, -RT_LWIP_ETH_PAD_SIZE); /* drop the padding word */
  269. #endif
  270. for (q = p; q != NULL; q = q->next)
  271. {
  272. /* Send the data from the pbuf to the interface, one pbuf at a
  273. time. The size of the data in each pbuf is kept in the ->len
  274. variable. */
  275. MEMCPY((uint8_t *)&SendDataBuf[len], (uint8_t *)q->payload, q->len);
  276. len = len + q->len;
  277. if((len > GMAC_BUF_LEN) || (len > p->tot_len))
  278. {
  279. LOG_E("rt_dw_eth_tx: error, len=%d, tot_len=%d", len, p->tot_len);
  280. return -RT_ERROR;
  281. }
  282. }
  283. if(len == p->tot_len)
  284. {
  285. if (cvi_eth_mac_send_frame(g_mac_handle, SendDataBuf, len) < 0)
  286. ret = -RT_ERROR;
  287. }
  288. else
  289. ret = -RT_ERROR;
  290. #if RT_LWIP_ETH_PAD_SIZE
  291. pbuf_header(p, RT_LWIP_ETH_PAD_SIZE); /* reclaim the padding word */
  292. #endif
  293. return ret;
  294. }
  295. const static struct rt_device_ops dw_eth_ops =
  296. {
  297. rt_dw_eth_init,
  298. RT_NULL,
  299. RT_NULL,
  300. RT_NULL,
  301. RT_NULL,
  302. rt_dw_eth_control
  303. };
  304. static int rthw_eth_init(void)
  305. {
  306. rt_err_t ret = RT_EOK;
  307. dw_eth_device.base = (rt_uint32_t *)DW_MAC_BASE;
  308. dw_eth_device.irq = DW_MAC_IRQ;
  309. dw_eth_device.parent.parent.ops = &dw_eth_ops;
  310. dw_eth_device.parent.eth_rx = rt_dw_eth_rx;
  311. dw_eth_device.parent.eth_tx = rt_dw_eth_tx;
  312. ret = rt_sem_init(&dw_eth_device.rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  313. if (ret != RT_EOK)
  314. {
  315. LOG_E("rt_sem_init failed: %d", ret);
  316. return ret;
  317. }
  318. ret = eth_device_init(&dw_eth_device.parent, "e0");
  319. if (ret != RT_EOK)
  320. {
  321. LOG_E("eth_device_init failed: %d", ret);
  322. return ret;
  323. }
  324. return RT_EOK;
  325. }
  326. INIT_DEVICE_EXPORT(rthw_eth_init);