drv_eth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. * Copyright (c) 2022, Xiaohua Semiconductor Co., Ltd.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2022-04-28 CDT first version
  10. */
  11. /*******************************************************************************
  12. * Include files
  13. ******************************************************************************/
  14. #include "drv_eth.h"
  15. #if defined(BSP_USING_ETH)
  16. #include <netif/ethernetif.h>
  17. #include <lwipopts.h>
  18. #include "drv_irq.h"
  19. #include "board_config.h"
  20. /*******************************************************************************
  21. * Local pre-processor symbols/macros ('#define')
  22. ******************************************************************************/
  23. //#define DRV_DEBUG
  24. #define LOG_TAG "drv.eth"
  25. #include <drv_log.h>
  26. #define MAX_ADDR_LEN 6
  27. /*******************************************************************************
  28. * Local type definitions ('typedef')
  29. ******************************************************************************/
  30. struct hc32_eth
  31. {
  32. /* inherit from ethernet device */
  33. struct eth_device parent;
  34. #if !(defined(PHY_USING_INTERRUPT_MODE) && defined(ETH_USING_INTERFACE_RMII))
  35. rt_timer_t poll_link_timer;
  36. #endif
  37. /* interface address info, hw address */
  38. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  39. /* ETH_Speed */
  40. rt_uint32_t eth_speed;
  41. /* ETH_Duplex_Mode */
  42. rt_uint32_t eth_mode;
  43. /* eth irq */
  44. struct hc32_irq_config irq_config;
  45. func_ptr_t irq_callback;
  46. };
  47. /* eth phy status */
  48. enum
  49. {
  50. ETH_PHY_LINK = 0x01U,
  51. ETH_PHY_100M = 0x02U,
  52. ETH_PHY_FULL_DUPLEX = 0x04U,
  53. };
  54. /*******************************************************************************
  55. * Global variable definitions (declared in header file with 'extern')
  56. ******************************************************************************/
  57. extern rt_err_t rt_hw_eth_board_init(CM_ETH_TypeDef *CM_ETHx);
  58. extern rt_err_t rt_hw_eth_phy_reset(CM_ETH_TypeDef *CM_ETHx);
  59. /*******************************************************************************
  60. * Local function prototypes ('static')
  61. ******************************************************************************/
  62. static void eth_global_irq_handle(void);
  63. /*******************************************************************************
  64. * Local variable definitions ('static')
  65. ******************************************************************************/
  66. static stc_eth_handle_t EthHandle;
  67. /* Ethernet Tx,Rx DMA Descriptor */
  68. static stc_eth_dma_desc_t *EthDmaTxDscrTab, *EthDmaRxDscrTab;
  69. /* Ethernet Transmit,Receive Buffer */
  70. static rt_uint8_t *EthTxBuff, *EthRxBuff;
  71. static struct hc32_eth hc32_eth_device =
  72. {
  73. .irq_config = ETH_IRQ_CONFIG,
  74. .irq_callback = eth_global_irq_handle,
  75. };
  76. /*******************************************************************************
  77. * Function implementation - global ('extern') and local ('static')
  78. ******************************************************************************/
  79. static rt_err_t rt_hc32_eth_init(rt_device_t dev)
  80. {
  81. stc_eth_init_t stcEthInit;
  82. uint16_t u16RegVal;
  83. /* Enable ETH clock */
  84. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_ETHMAC, ENABLE);
  85. /* Init Ethernet GPIO */
  86. rt_hw_eth_phy_reset(CM_ETH);
  87. rt_hw_eth_board_init(CM_ETH);
  88. /* Reset ETHERNET */
  89. (void)ETH_DeInit();
  90. /* Configure structure initialization */
  91. (void)ETH_CommStructInit(&EthHandle.stcCommInit);
  92. (void)ETH_StructInit(&stcEthInit);
  93. EthHandle.stcCommInit.u16AutoNego = ETH_AUTO_NEGO_DISABLE;
  94. EthHandle.stcCommInit.au8MacAddr[0] = hc32_eth_device.dev_addr[0];
  95. EthHandle.stcCommInit.au8MacAddr[1] = hc32_eth_device.dev_addr[1];
  96. EthHandle.stcCommInit.au8MacAddr[2] = hc32_eth_device.dev_addr[2];
  97. EthHandle.stcCommInit.au8MacAddr[3] = hc32_eth_device.dev_addr[3];
  98. EthHandle.stcCommInit.au8MacAddr[4] = hc32_eth_device.dev_addr[4];
  99. EthHandle.stcCommInit.au8MacAddr[5] = hc32_eth_device.dev_addr[5];
  100. #ifdef ETH_USING_INTERFACE_RMII
  101. EthHandle.stcCommInit.u32Interface = ETH_MAC_IF_RMII;
  102. #else
  103. EthHandle.stcCommInit.u32Interface = ETH_MAC_IF_MII;
  104. #endif
  105. EthHandle.stcCommInit.u32ReceiveMode = ETH_RX_MD_INT;
  106. #ifdef RT_LWIP_USING_HW_CHECKSUM
  107. EthHandle.stcCommInit.u32ChecksumMode = ETH_MAC_CHECKSUM_MD_HW;
  108. #else
  109. EthHandle.stcCommInit.u32ChecksumMode = ETH_MAC_CHECKSUM_MD_SW;
  110. #endif
  111. /* Configure ethernet peripheral */
  112. if (LL_OK != ETH_Init(&EthHandle, &stcEthInit))
  113. {
  114. LOG_E("eth hardware init failed");
  115. }
  116. else
  117. {
  118. LOG_D("eth hardware init success");
  119. }
  120. /* Initialize Tx Descriptors list: Chain Mode */
  121. (void)ETH_DMA_TxDescListInit(&EthHandle, EthDmaTxDscrTab, EthTxBuff, ETH_TX_BUF_NUM);
  122. /* Initialize Rx Descriptors list: Chain Mode */
  123. (void)ETH_DMA_RxDescListInit(&EthHandle, EthDmaRxDscrTab, EthRxBuff, ETH_RX_BUF_NUM);
  124. /* Enable ETH interrupt */
  125. NVIC_EnableIRQ(hc32_eth_device.irq_config.irq_num);
  126. /* Enable MAC and DMA transmission and reception */
  127. if (LL_OK == ETH_Start())
  128. {
  129. LOG_D("eth hardware start");
  130. }
  131. else
  132. {
  133. LOG_E("eth hardware start faild");
  134. return -RT_ERROR;
  135. }
  136. /* Configure PHY LED mode */
  137. u16RegVal = PHY_PAGE_ADDR_7;
  138. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  139. (void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
  140. MODIFY_REG16(u16RegVal, PHY_LED_SELECT, PHY_LED_SELECT_10);
  141. (void)ETH_PHY_WriteReg(&EthHandle, PHY_P7_IWLFR, u16RegVal);
  142. u16RegVal = PHY_PAGE_ADDR_0;
  143. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  144. #ifdef ETH_USING_INTERFACE_RMII
  145. /* Disable Power Saving Mode */
  146. (void)ETH_PHY_ReadReg(&EthHandle, PHY_PSMR, &u16RegVal);
  147. CLR_REG16_BIT(u16RegVal, PHY_EN_PWR_SAVE);
  148. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSMR, u16RegVal);
  149. #endif
  150. return RT_EOK;
  151. }
  152. static rt_err_t rt_hc32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  153. {
  154. LOG_D("eth open");
  155. return RT_EOK;
  156. }
  157. static rt_err_t rt_hc32_eth_close(rt_device_t dev)
  158. {
  159. LOG_D("eth close");
  160. return RT_EOK;
  161. }
  162. static rt_ssize_t rt_hc32_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  163. {
  164. LOG_D("eth read");
  165. rt_set_errno(-RT_ENOSYS);
  166. return 0;
  167. }
  168. static rt_ssize_t rt_hc32_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  169. {
  170. LOG_D("eth write");
  171. rt_set_errno(-RT_ENOSYS);
  172. return 0;
  173. }
  174. static rt_err_t rt_hc32_eth_control(rt_device_t dev, int cmd, void *args)
  175. {
  176. switch (cmd)
  177. {
  178. case NIOCTL_GADDR:
  179. /* get mac address */
  180. if (args)
  181. {
  182. SMEMCPY(args, hc32_eth_device.dev_addr, 6);
  183. }
  184. else
  185. {
  186. return -RT_ERROR;
  187. }
  188. break;
  189. default :
  190. break;
  191. }
  192. return RT_EOK;
  193. }
  194. /* ethernet device interface */
  195. /* transmit data*/
  196. rt_err_t rt_hc32_eth_tx(rt_device_t dev, struct pbuf *p)
  197. {
  198. rt_err_t errval = -RT_ERROR;
  199. struct pbuf *q;
  200. uint8_t *txBuffer;
  201. __IO stc_eth_dma_desc_t *DmaTxDesc;
  202. uint32_t byteCnt;
  203. uint32_t frameLength = 0UL;
  204. uint32_t bufferOffset;
  205. uint32_t payloadOffset;
  206. DmaTxDesc = EthHandle.stcTxDesc;
  207. txBuffer = (uint8_t *)((EthHandle.stcTxDesc)->u32Buf1Addr);
  208. bufferOffset = 0UL;
  209. /* Copy frame from pbufs to driver buffers */
  210. for (q = p; q != NULL; q = q->next)
  211. {
  212. /* If this buffer isn't available, goto error */
  213. if (0UL != (DmaTxDesc->u32ControlStatus & ETH_DMA_TXDESC_OWN))
  214. {
  215. LOG_D("buffer not valid");
  216. errval = (err_t)ERR_USE;
  217. goto error;
  218. }
  219. /* Get bytes in current lwIP buffer */
  220. byteCnt = q->len;
  221. payloadOffset = 0UL;
  222. /* Check if the length of data to copy is bigger than Tx buffer size */
  223. while ((byteCnt + bufferOffset) > ETH_TX_BUF_SIZE)
  224. {
  225. /* Copy data to Tx buffer*/
  226. SMEMCPY((uint8_t *) & (txBuffer[bufferOffset]), (uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), (ETH_TX_BUF_SIZE - bufferOffset));
  227. /* Point to next descriptor */
  228. DmaTxDesc = (stc_eth_dma_desc_t *)(DmaTxDesc->u32Buf2NextDescAddr);
  229. /* Check if the buffer is available */
  230. if (0UL != (DmaTxDesc->u32ControlStatus & ETH_DMA_TXDESC_OWN))
  231. {
  232. errval = (err_t)ERR_USE;
  233. goto error;
  234. }
  235. txBuffer = (uint8_t *)(DmaTxDesc->u32Buf1Addr);
  236. byteCnt = byteCnt - (ETH_TX_BUF_SIZE - bufferOffset);
  237. payloadOffset = payloadOffset + (ETH_TX_BUF_SIZE - bufferOffset);
  238. frameLength = frameLength + (ETH_TX_BUF_SIZE - bufferOffset);
  239. bufferOffset = 0UL;
  240. }
  241. /* Copy the remaining bytes */
  242. SMEMCPY((uint8_t *) & (txBuffer[bufferOffset]), (uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), byteCnt);
  243. bufferOffset = bufferOffset + byteCnt;
  244. frameLength = frameLength + byteCnt;
  245. }
  246. LOG_D("transmit frame length :%d", framelength);
  247. /* Prepare transmit descriptors to give to DMA */
  248. (void)ETH_DMA_SetTransFrame(&EthHandle, frameLength);
  249. errval = (err_t)ERR_OK;
  250. error:
  251. /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  252. if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_UNS))
  253. {
  254. /* Clear DMA UNS flag */
  255. ETH_DMA_ClearStatus(ETH_DMA_FLAG_UNS);
  256. /* Resume DMA transmission */
  257. WRITE_REG32(CM_ETH->DMA_TXPOLLR, 0UL);
  258. }
  259. return errval;
  260. }
  261. /* receive data*/
  262. struct pbuf *rt_hc32_eth_rx(rt_device_t dev)
  263. {
  264. struct pbuf *p = NULL;
  265. struct pbuf *q;
  266. uint32_t len;
  267. uint8_t *rxBuffer;
  268. __IO stc_eth_dma_desc_t *DmaRxDesc;
  269. uint32_t byteCnt;
  270. uint32_t bufferOffset;
  271. uint32_t payloadOffset;
  272. uint32_t i;
  273. /* Get received frame */
  274. if (LL_OK != ETH_DMA_GetReceiveFrame_Int(&EthHandle))
  275. {
  276. LOG_D("receive frame faild");
  277. return NULL;
  278. }
  279. /* Obtain the size of the packet */
  280. len = (EthHandle.stcRxFrame).u32Len;
  281. rxBuffer = (uint8_t *)(EthHandle.stcRxFrame).u32Buf;
  282. LOG_D("receive frame len : %d", len);
  283. if (len > 0UL)
  284. {
  285. /* Allocate a pbuf chain of pbufs from the Lwip buffer pool */
  286. p = pbuf_alloc(PBUF_RAW, (uint16_t)len, PBUF_POOL);
  287. }
  288. if (p != NULL)
  289. {
  290. DmaRxDesc = (EthHandle.stcRxFrame).pstcFSDesc;
  291. bufferOffset = 0UL;
  292. for (q = p; q != NULL; q = q->next)
  293. {
  294. byteCnt = q->len;
  295. payloadOffset = 0UL;
  296. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size */
  297. while ((byteCnt + bufferOffset) > ETH_RX_BUF_SIZE)
  298. {
  299. /* Copy data to pbuf */
  300. SMEMCPY((uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), (uint8_t *) & (rxBuffer[bufferOffset]), (ETH_RX_BUF_SIZE - bufferOffset));
  301. /* Point to next descriptor */
  302. DmaRxDesc = (stc_eth_dma_desc_t *)(DmaRxDesc->u32Buf2NextDescAddr);
  303. rxBuffer = (uint8_t *)(DmaRxDesc->u32Buf1Addr);
  304. byteCnt = byteCnt - (ETH_RX_BUF_SIZE - bufferOffset);
  305. payloadOffset = payloadOffset + (ETH_RX_BUF_SIZE - bufferOffset);
  306. bufferOffset = 0UL;
  307. }
  308. /* Copy remaining data in pbuf */
  309. SMEMCPY((uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), (uint8_t *) & (rxBuffer[bufferOffset]), byteCnt);
  310. bufferOffset = bufferOffset + byteCnt;
  311. }
  312. }
  313. /* Release descriptors to DMA */
  314. DmaRxDesc = (EthHandle.stcRxFrame).pstcFSDesc;
  315. for (i = 0UL; i < (EthHandle.stcRxFrame).u32SegCount; i++)
  316. {
  317. DmaRxDesc->u32ControlStatus |= ETH_DMA_RXDESC_OWN;
  318. DmaRxDesc = (stc_eth_dma_desc_t *)(DmaRxDesc->u32Buf2NextDescAddr);
  319. }
  320. /* Clear Segment_Count */
  321. (EthHandle.stcRxFrame).u32SegCount = 0UL;
  322. /* When Rx Buffer unavailable flag is set, clear it and resume reception */
  323. if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_RUS))
  324. {
  325. /* Clear DMA RUS flag */
  326. ETH_DMA_ClearStatus(ETH_DMA_FLAG_RUS);
  327. /* Resume DMA reception */
  328. WRITE_REG32(CM_ETH->DMA_RXPOLLR, 0UL);
  329. }
  330. return p;
  331. }
  332. static void hc32_eth_irq_handle(stc_eth_handle_t *eth_handle)
  333. {
  334. rt_err_t result;
  335. (void)eth_handle;
  336. /* Frame received */
  337. if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_RIS))
  338. {
  339. result = eth_device_ready(&(hc32_eth_device.parent));
  340. if (result != RT_EOK)
  341. {
  342. LOG_I("eth rx complete callback err = %d", result);
  343. }
  344. /* Clear the Eth DMA Rx IT pending bits */
  345. ETH_DMA_ClearStatus(ETH_DMA_FLAG_RIS | ETH_DMA_FLAG_NIS);
  346. }
  347. }
  348. /* interrupt service routine */
  349. static void eth_global_irq_handle(void)
  350. {
  351. /* enter interrupt */
  352. rt_interrupt_enter();
  353. hc32_eth_irq_handle(&EthHandle);
  354. /* leave interrupt */
  355. rt_interrupt_leave();
  356. }
  357. static void hc32_phy_link_change(void)
  358. {
  359. static rt_uint8_t phy_status = 0;
  360. rt_uint8_t phy_status_new = 0;
  361. uint16_t u16RegVal = 0U;
  362. uint16_t u16Page = 0U;
  363. /* Switch page */
  364. (void)ETH_PHY_ReadReg(&EthHandle, PHY_PSR, &u16Page);
  365. if (u16Page != PHY_PAGE_ADDR_0)
  366. {
  367. u16RegVal = PHY_PAGE_ADDR_0;
  368. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  369. }
  370. /* Read PHY_BSR */
  371. (void)ETH_PHY_ReadReg(&EthHandle, PHY_BASIC_STATUS_REG, &u16RegVal);
  372. LOG_D("phy basic status reg is 0x%X", u16RegVal);
  373. if ((0x0000U != u16RegVal) && (0xFFFFU != u16RegVal))
  374. {
  375. if (u16RegVal & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  376. {
  377. phy_status_new |= ETH_PHY_LINK;
  378. if (0U != (u16RegVal & (PHY_100BASE_TX_FD | PHY_10BASE_T_FD)))
  379. {
  380. phy_status_new |= ETH_PHY_FULL_DUPLEX;
  381. }
  382. if (0U != (u16RegVal & (PHY_100BASE_TX_FD | PHY_100BASE_TX_HD)))
  383. {
  384. phy_status_new |= ETH_PHY_100M;
  385. }
  386. }
  387. }
  388. /* Restore page */
  389. if (u16Page != PHY_PAGE_ADDR_0)
  390. {
  391. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16Page);
  392. }
  393. if (phy_status != phy_status_new)
  394. {
  395. phy_status = phy_status_new;
  396. if (phy_status & ETH_PHY_LINK)
  397. {
  398. if (phy_status & ETH_PHY_FULL_DUPLEX)
  399. {
  400. hc32_eth_device.eth_mode = ETH_MAC_DUPLEX_MD_FULL;
  401. }
  402. else
  403. {
  404. hc32_eth_device.eth_mode = ETH_MAC_DUPLEX_MD_HALF;
  405. }
  406. if (phy_status & ETH_PHY_100M)
  407. {
  408. hc32_eth_device.eth_speed = ETH_MAC_SPEED_100M;
  409. }
  410. else
  411. {
  412. hc32_eth_device.eth_speed = ETH_MAC_SPEED_10M;
  413. }
  414. LOG_D("link up");
  415. eth_device_linkchange(&hc32_eth_device.parent, RT_TRUE);
  416. }
  417. else
  418. {
  419. LOG_I("link down");
  420. eth_device_linkchange(&hc32_eth_device.parent, RT_FALSE);
  421. }
  422. }
  423. }
  424. #if defined(PHY_USING_INTERRUPT_MODE) && defined(ETH_USING_INTERFACE_RMII)
  425. static void eth_phy_irq_handler(void *args)
  426. {
  427. rt_uint16_t status = 0;
  428. ETH_PHY_ReadReg(&EthHandle, PHY_IISDR, &status);
  429. LOG_D("phy interrupt status reg is 0x%X", status);
  430. hc32_phy_link_change();
  431. }
  432. #endif
  433. static void hc32_phy_monitor_thread(void *parameter)
  434. {
  435. uint8_t phy_addr = 0xFF;
  436. uint8_t detected_count = 0;
  437. /* phy search */
  438. while (phy_addr == 0xFF)
  439. {
  440. rt_uint16_t i, temp;
  441. for (i = 0; i <= 0x1F; i++)
  442. {
  443. EthHandle.stcCommInit.u16PhyAddr = i;
  444. ETH_PHY_ReadReg(&EthHandle, PHY_ID1_REG, &temp);
  445. if (temp != 0xFFFF && temp != 0x00)
  446. {
  447. phy_addr = i;
  448. break;
  449. }
  450. }
  451. detected_count++;
  452. rt_thread_mdelay(1000);
  453. if (detected_count > 10)
  454. {
  455. LOG_E("No PHY device was detected!");
  456. }
  457. }
  458. LOG_D("Found a phy, address:0x%02X", phy_addr);
  459. /* Reset PHY */
  460. ETH_PHY_WriteReg(&EthHandle, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK);
  461. rt_thread_mdelay(2000);
  462. ETH_PHY_WriteReg(&EthHandle, PHY_BASIC_CONTROL_REG, PHY_AUTO_NEGOTIATION_MASK);
  463. hc32_phy_link_change();
  464. #if defined(PHY_USING_INTERRUPT_MODE) && defined(ETH_USING_INTERFACE_RMII)
  465. /* configuration intterrupt pin */
  466. rt_pin_mode(ETH_PHY_INT_PIN, PIN_MODE_INPUT_PULLUP);
  467. rt_pin_attach_irq(ETH_PHY_INT_PIN, PIN_IRQ_MODE_FALLING, eth_phy_irq_handler, (void *)"callbackargs");
  468. rt_pin_irq_enable(ETH_PHY_INT_PIN, PIN_IRQ_ENABLE);
  469. uint16_t u16RegVal;
  470. /* Configure PHY to generate an interrupt when Eth Link state changes */
  471. u16RegVal = PHY_PAGE_ADDR_7;
  472. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  473. /* Enable Interrupt on change of link status */
  474. (void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
  475. SET_REG16_BIT(u16RegVal, PHY_INT_LINK_CHANGE);
  476. (void)ETH_PHY_WriteReg(&EthHandle, PHY_P7_IWLFR, u16RegVal);
  477. u16RegVal = PHY_PAGE_ADDR_0;
  478. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  479. #else
  480. hc32_eth_device.poll_link_timer = rt_timer_create("eth_phy_link", (void (*)(void *))hc32_phy_link_change,
  481. NULL, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
  482. if (!hc32_eth_device.poll_link_timer || rt_timer_start(hc32_eth_device.poll_link_timer) != RT_EOK)
  483. {
  484. LOG_E("Start eth phy link change detection timer failed");
  485. }
  486. #endif
  487. }
  488. /* Register the eth device */
  489. static int rt_hw_hc32_eth_init(void)
  490. {
  491. rt_err_t state = RT_EOK;
  492. /* register eth handler */
  493. hc32_install_irq_handler(&hc32_eth_device.irq_config, hc32_eth_device.irq_callback, RT_FALSE);
  494. /* Prepare receive and send buffers */
  495. EthRxBuff = (rt_uint8_t *)rt_calloc(ETH_RX_BUF_NUM, ETH_MAX_PACKET_SIZE);
  496. if (EthRxBuff == RT_NULL)
  497. {
  498. LOG_E("No memory");
  499. state = -RT_ENOMEM;
  500. goto __exit;
  501. }
  502. EthTxBuff = (rt_uint8_t *)rt_calloc(ETH_TX_BUF_NUM, ETH_MAX_PACKET_SIZE);
  503. if (EthTxBuff == RT_NULL)
  504. {
  505. LOG_E("No memory");
  506. state = -RT_ENOMEM;
  507. goto __exit;
  508. }
  509. EthDmaRxDscrTab = (stc_eth_dma_desc_t *)rt_calloc(ETH_RX_BUF_NUM, sizeof(stc_eth_dma_desc_t));
  510. if (EthDmaRxDscrTab == RT_NULL)
  511. {
  512. LOG_E("No memory");
  513. state = -RT_ENOMEM;
  514. goto __exit;
  515. }
  516. EthDmaTxDscrTab = (stc_eth_dma_desc_t *)rt_calloc(ETH_TX_BUF_NUM, sizeof(stc_eth_dma_desc_t));
  517. if (EthDmaTxDscrTab == RT_NULL)
  518. {
  519. LOG_E("No memory");
  520. state = -RT_ENOMEM;
  521. goto __exit;
  522. }
  523. hc32_eth_device.eth_speed = ETH_MAC_SPEED_100M;
  524. hc32_eth_device.eth_mode = ETH_MAC_DUPLEX_MD_FULL;
  525. /* 00-80 uid */
  526. hc32_eth_device.dev_addr[0] = 0x02;
  527. hc32_eth_device.dev_addr[1] = 0x80;
  528. hc32_eth_device.dev_addr[2] = 0x00;
  529. /* generate MAC addr from unique ID */
  530. hc32_eth_device.dev_addr[3] = (rt_uint8_t)READ_REG32(CM_EFM->UQID0);
  531. hc32_eth_device.dev_addr[4] = (rt_uint8_t)READ_REG32(CM_EFM->UQID1);
  532. hc32_eth_device.dev_addr[5] = (rt_uint8_t)READ_REG32(CM_EFM->UQID2);
  533. hc32_eth_device.parent.parent.init = rt_hc32_eth_init;
  534. hc32_eth_device.parent.parent.open = rt_hc32_eth_open;
  535. hc32_eth_device.parent.parent.close = rt_hc32_eth_close;
  536. hc32_eth_device.parent.parent.read = rt_hc32_eth_read;
  537. hc32_eth_device.parent.parent.write = rt_hc32_eth_write;
  538. hc32_eth_device.parent.parent.control = rt_hc32_eth_control;
  539. hc32_eth_device.parent.parent.user_data = RT_NULL;
  540. hc32_eth_device.parent.eth_rx = rt_hc32_eth_rx;
  541. hc32_eth_device.parent.eth_tx = rt_hc32_eth_tx;
  542. /* register eth device */
  543. state = eth_device_init(&(hc32_eth_device.parent), "e0");
  544. if (RT_EOK == state)
  545. {
  546. LOG_D("eth device init success");
  547. }
  548. else
  549. {
  550. LOG_E("eth device init faild: %d", state);
  551. state = -RT_ERROR;
  552. goto __exit;
  553. }
  554. /* start phy monitor */
  555. rt_thread_t tid;
  556. tid = rt_thread_create("phy_monitor", hc32_phy_monitor_thread, RT_NULL, 1024, 12, 5);
  557. if (tid != RT_NULL)
  558. {
  559. rt_thread_startup(tid);
  560. }
  561. else
  562. {
  563. state = -RT_ERROR;
  564. }
  565. __exit:
  566. if (state != RT_EOK)
  567. {
  568. if (EthRxBuff)
  569. {
  570. rt_free(EthRxBuff);
  571. }
  572. if (EthTxBuff)
  573. {
  574. rt_free(EthTxBuff);
  575. }
  576. if (EthDmaRxDscrTab)
  577. {
  578. rt_free(EthDmaRxDscrTab);
  579. }
  580. if (EthDmaTxDscrTab)
  581. {
  582. rt_free(EthDmaTxDscrTab);
  583. }
  584. }
  585. return state;
  586. }
  587. INIT_DEVICE_EXPORT(rt_hw_hc32_eth_init);
  588. #endif