drv_eth.c 21 KB

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