drv_eth.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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.u16AutoNego = ETH_AUTO_NEGO_DISABLE;
  92. EthHandle.stcCommInit.au8MacAddr[0] = hc32_eth_device.dev_addr[0];
  93. EthHandle.stcCommInit.au8MacAddr[1] = hc32_eth_device.dev_addr[1];
  94. EthHandle.stcCommInit.au8MacAddr[2] = hc32_eth_device.dev_addr[2];
  95. EthHandle.stcCommInit.au8MacAddr[3] = hc32_eth_device.dev_addr[3];
  96. EthHandle.stcCommInit.au8MacAddr[4] = hc32_eth_device.dev_addr[4];
  97. EthHandle.stcCommInit.au8MacAddr[5] = hc32_eth_device.dev_addr[5];
  98. EthHandle.stcCommInit.u32ReceiveMode = ETH_RX_MD_INT;
  99. #if defined(ETH_INTERFACE_USING_RMII)
  100. EthHandle.stcCommInit.u32Interface = ETH_MAC_IF_RMII;
  101. #else
  102. EthHandle.stcCommInit.u32Interface = ETH_MAC_IF_MII;
  103. #endif
  104. #if defined(RT_LWIP_USING_HW_CHECKSUM)
  105. EthHandle.stcCommInit.u32ChecksumMode = ETH_MAC_CHECKSUM_MD_HW;
  106. #else
  107. EthHandle.stcCommInit.u32ChecksumMode = ETH_MAC_CHECKSUM_MD_SW;
  108. #endif
  109. /* Configure ethernet peripheral */
  110. if (LL_OK != ETH_Init(&EthHandle, &stcEthInit))
  111. {
  112. LOG_E("eth hardware init failed");
  113. }
  114. else
  115. {
  116. LOG_D("eth hardware init success");
  117. }
  118. /* Initialize Tx Descriptors list: Chain Mode */
  119. (void)ETH_DMA_TxDescListInit(&EthHandle, EthDmaTxDscrTab, EthTxBuff, ETH_TX_BUF_NUM);
  120. /* Initialize Rx Descriptors list: Chain Mode */
  121. (void)ETH_DMA_RxDescListInit(&EthHandle, EthDmaRxDscrTab, EthRxBuff, ETH_RX_BUF_NUM);
  122. /* Enable ETH interrupt */
  123. NVIC_EnableIRQ(hc32_eth_device.irq_config.irq_num);
  124. /* Enable MAC and DMA transmission and reception */
  125. if (LL_OK == ETH_Start())
  126. {
  127. LOG_D("eth hardware start");
  128. }
  129. else
  130. {
  131. LOG_E("eth hardware start faild");
  132. return -RT_ERROR;
  133. }
  134. return RT_EOK;
  135. }
  136. static rt_err_t rt_hc32_eth_open(rt_device_t dev, rt_uint16_t oflag)
  137. {
  138. LOG_D("eth open");
  139. return RT_EOK;
  140. }
  141. static rt_err_t rt_hc32_eth_close(rt_device_t dev)
  142. {
  143. LOG_D("eth close");
  144. return RT_EOK;
  145. }
  146. static rt_ssize_t rt_hc32_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  147. {
  148. LOG_D("eth read");
  149. rt_set_errno(-RT_ENOSYS);
  150. return 0;
  151. }
  152. static rt_ssize_t rt_hc32_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  153. {
  154. LOG_D("eth write");
  155. rt_set_errno(-RT_ENOSYS);
  156. return 0;
  157. }
  158. static rt_err_t rt_hc32_eth_control(rt_device_t dev, int cmd, void *args)
  159. {
  160. switch (cmd)
  161. {
  162. case NIOCTL_GADDR:
  163. /* get mac address */
  164. if (args)
  165. {
  166. SMEMCPY(args, hc32_eth_device.dev_addr, 6);
  167. }
  168. else
  169. {
  170. return -RT_ERROR;
  171. }
  172. break;
  173. default :
  174. break;
  175. }
  176. return RT_EOK;
  177. }
  178. /* ethernet device interface */
  179. /* transmit data*/
  180. rt_err_t rt_hc32_eth_tx(rt_device_t dev, struct pbuf *p)
  181. {
  182. rt_err_t errval = -RT_ERROR;
  183. struct pbuf *q;
  184. uint8_t *txBuffer;
  185. __IO stc_eth_dma_desc_t *DmaTxDesc;
  186. uint32_t byteCnt;
  187. uint32_t frameLength = 0UL;
  188. uint32_t bufferOffset;
  189. uint32_t payloadOffset;
  190. DmaTxDesc = EthHandle.stcTxDesc;
  191. txBuffer = (uint8_t *)((EthHandle.stcTxDesc)->u32Buf1Addr);
  192. bufferOffset = 0UL;
  193. /* Copy frame from pbufs to driver buffers */
  194. for (q = p; q != NULL; q = q->next)
  195. {
  196. /* If this buffer isn't available, goto error */
  197. if (0UL != (DmaTxDesc->u32ControlStatus & ETH_DMA_TXDESC_OWN))
  198. {
  199. LOG_D("buffer not valid");
  200. errval = (err_t)ERR_USE;
  201. goto error;
  202. }
  203. /* Get bytes in current lwIP buffer */
  204. byteCnt = q->len;
  205. payloadOffset = 0UL;
  206. /* Check if the length of data to copy is bigger than Tx buffer size */
  207. while ((byteCnt + bufferOffset) > ETH_TX_BUF_SIZE)
  208. {
  209. /* Copy data to Tx buffer*/
  210. SMEMCPY((uint8_t *) & (txBuffer[bufferOffset]), (uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), (ETH_TX_BUF_SIZE - bufferOffset));
  211. /* Point to next descriptor */
  212. DmaTxDesc = (stc_eth_dma_desc_t *)(DmaTxDesc->u32Buf2NextDescAddr);
  213. /* Check if the buffer is available */
  214. if (0UL != (DmaTxDesc->u32ControlStatus & ETH_DMA_TXDESC_OWN))
  215. {
  216. errval = (err_t)ERR_USE;
  217. goto error;
  218. }
  219. txBuffer = (uint8_t *)(DmaTxDesc->u32Buf1Addr);
  220. byteCnt = byteCnt - (ETH_TX_BUF_SIZE - bufferOffset);
  221. payloadOffset = payloadOffset + (ETH_TX_BUF_SIZE - bufferOffset);
  222. frameLength = frameLength + (ETH_TX_BUF_SIZE - bufferOffset);
  223. bufferOffset = 0UL;
  224. }
  225. /* Copy the remaining bytes */
  226. SMEMCPY((uint8_t *) & (txBuffer[bufferOffset]), (uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), byteCnt);
  227. bufferOffset = bufferOffset + byteCnt;
  228. frameLength = frameLength + byteCnt;
  229. }
  230. LOG_D("transmit frame length :%d", frameLength);
  231. /* Prepare transmit descriptors to give to DMA */
  232. (void)ETH_DMA_SetTransFrame(&EthHandle, frameLength);
  233. errval = (err_t)ERR_OK;
  234. error:
  235. /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
  236. if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_UNS))
  237. {
  238. /* Clear DMA UNS flag */
  239. ETH_DMA_ClearStatus(ETH_DMA_FLAG_UNS);
  240. /* Resume DMA transmission */
  241. WRITE_REG32(CM_ETH->DMA_TXPOLLR, 0UL);
  242. }
  243. return errval;
  244. }
  245. /* receive data*/
  246. struct pbuf *rt_hc32_eth_rx(rt_device_t dev)
  247. {
  248. struct pbuf *p = NULL;
  249. struct pbuf *q;
  250. uint32_t len;
  251. uint8_t *rxBuffer;
  252. __IO stc_eth_dma_desc_t *DmaRxDesc;
  253. uint32_t byteCnt;
  254. uint32_t bufferOffset;
  255. uint32_t payloadOffset;
  256. uint32_t i;
  257. /* Get received frame */
  258. if (LL_OK != ETH_DMA_GetReceiveFrame_Int(&EthHandle))
  259. {
  260. LOG_D("receive frame faild");
  261. return NULL;
  262. }
  263. /* Obtain the size of the packet */
  264. len = (EthHandle.stcRxFrame).u32Len;
  265. rxBuffer = (uint8_t *)(EthHandle.stcRxFrame).u32Buf;
  266. LOG_D("receive frame len : %d", len);
  267. if (len > 0UL)
  268. {
  269. /* Allocate a pbuf chain of pbufs from the Lwip buffer pool */
  270. p = pbuf_alloc(PBUF_RAW, (uint16_t)len, PBUF_POOL);
  271. }
  272. if (p != NULL)
  273. {
  274. DmaRxDesc = (EthHandle.stcRxFrame).pstcFSDesc;
  275. bufferOffset = 0UL;
  276. for (q = p; q != NULL; q = q->next)
  277. {
  278. byteCnt = q->len;
  279. payloadOffset = 0UL;
  280. /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size */
  281. while ((byteCnt + bufferOffset) > ETH_RX_BUF_SIZE)
  282. {
  283. /* Copy data to pbuf */
  284. SMEMCPY((uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), (uint8_t *) & (rxBuffer[bufferOffset]), (ETH_RX_BUF_SIZE - bufferOffset));
  285. /* Point to next descriptor */
  286. DmaRxDesc = (stc_eth_dma_desc_t *)(DmaRxDesc->u32Buf2NextDescAddr);
  287. rxBuffer = (uint8_t *)(DmaRxDesc->u32Buf1Addr);
  288. byteCnt = byteCnt - (ETH_RX_BUF_SIZE - bufferOffset);
  289. payloadOffset = payloadOffset + (ETH_RX_BUF_SIZE - bufferOffset);
  290. bufferOffset = 0UL;
  291. }
  292. /* Copy remaining data in pbuf */
  293. SMEMCPY((uint8_t *) & (((uint8_t *)q->payload)[payloadOffset]), (uint8_t *) & (rxBuffer[bufferOffset]), byteCnt);
  294. bufferOffset = bufferOffset + byteCnt;
  295. }
  296. }
  297. /* Release descriptors to DMA */
  298. DmaRxDesc = (EthHandle.stcRxFrame).pstcFSDesc;
  299. for (i = 0UL; i < (EthHandle.stcRxFrame).u32SegCount; i++)
  300. {
  301. DmaRxDesc->u32ControlStatus |= ETH_DMA_RXDESC_OWN;
  302. DmaRxDesc = (stc_eth_dma_desc_t *)(DmaRxDesc->u32Buf2NextDescAddr);
  303. }
  304. /* Clear Segment_Count */
  305. (EthHandle.stcRxFrame).u32SegCount = 0UL;
  306. /* When Rx Buffer unavailable flag is set, clear it and resume reception */
  307. if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_RUS))
  308. {
  309. /* Clear DMA RUS flag */
  310. ETH_DMA_ClearStatus(ETH_DMA_FLAG_RUS);
  311. /* Resume DMA reception */
  312. WRITE_REG32(CM_ETH->DMA_RXPOLLR, 0UL);
  313. }
  314. return p;
  315. }
  316. static void hc32_eth_irq_handle(stc_eth_handle_t *eth_handle)
  317. {
  318. rt_err_t result;
  319. (void)eth_handle;
  320. /* Frame received */
  321. if (RESET != ETH_DMA_GetStatus(ETH_DMA_FLAG_RIS))
  322. {
  323. result = eth_device_ready(&(hc32_eth_device.parent));
  324. if (result != RT_EOK)
  325. {
  326. LOG_I("eth rx complete callback err = %d", result);
  327. }
  328. /* Clear the Eth DMA Rx IT pending bits */
  329. ETH_DMA_ClearStatus(ETH_DMA_FLAG_RIS | ETH_DMA_FLAG_NIS);
  330. }
  331. }
  332. /* interrupt service routine */
  333. static void eth_global_irq_handle(void)
  334. {
  335. /* enter interrupt */
  336. rt_interrupt_enter();
  337. hc32_eth_irq_handle(&EthHandle);
  338. /* leave interrupt */
  339. rt_interrupt_leave();
  340. }
  341. static void hc32_phy_link_change(void)
  342. {
  343. static rt_uint8_t phy_status = 0;
  344. rt_uint8_t phy_status_new = 0;
  345. #if defined (ETH_PHY_USING_RTL8201F)
  346. uint16_t u16RegVal = 0U;
  347. uint16_t u16Page = 0U;
  348. #endif
  349. #if defined (ETH_PHY_USING_RTL8201F)
  350. /* Switch page */
  351. (void)ETH_PHY_ReadReg(&EthHandle, PHY_PSR, &u16Page);
  352. if (u16Page != PHY_PAGE_ADDR_0)
  353. {
  354. u16RegVal = PHY_PAGE_ADDR_0;
  355. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  356. }
  357. /* Read PHY_BSR */
  358. (void)ETH_PHY_ReadReg(&EthHandle, PHY_BASIC_STATUS_REG, &u16RegVal);
  359. LOG_D("phy basic status reg is 0x%X", u16RegVal);
  360. if ((0x0000U != u16RegVal) && (0xFFFFU != u16RegVal))
  361. {
  362. if (u16RegVal & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
  363. {
  364. phy_status_new |= ETH_PHY_LINK;
  365. /* Read the result of the auto-negotiation */
  366. ETH_PHY_ReadReg(&EthHandle, PHY_SR, &u16RegVal);
  367. /* Configure ETH duplex mode according to the result of automatic negotiation */
  368. if (0U != (u16RegVal & PHY_DUPLEX_STATUS))
  369. {
  370. phy_status_new |= ETH_PHY_FULL_DUPLEX;
  371. }
  372. /* Configure ETH speed according to the result of automatic negotiation */
  373. if (0U != (u16RegVal & PHY_SPEED_STATUS))
  374. {
  375. phy_status_new |= ETH_PHY_100M;
  376. }
  377. }
  378. }
  379. /* Restore page */
  380. if (u16Page != PHY_PAGE_ADDR_0)
  381. {
  382. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16Page);
  383. }
  384. #endif
  385. if (phy_status != phy_status_new)
  386. {
  387. phy_status = phy_status_new;
  388. if (phy_status & ETH_PHY_LINK)
  389. {
  390. if (phy_status & ETH_PHY_FULL_DUPLEX)
  391. {
  392. hc32_eth_device.eth_mode = ETH_MAC_DUPLEX_MD_FULL;
  393. }
  394. else
  395. {
  396. hc32_eth_device.eth_mode = ETH_MAC_DUPLEX_MD_HALF;
  397. }
  398. if (phy_status & ETH_PHY_100M)
  399. {
  400. hc32_eth_device.eth_speed = ETH_MAC_SPEED_100M;
  401. }
  402. else
  403. {
  404. hc32_eth_device.eth_speed = ETH_MAC_SPEED_10M;
  405. }
  406. ETH_MAC_SetDuplexSpeed(hc32_eth_device.eth_mode, hc32_eth_device.eth_speed);
  407. ETH_Start();
  408. LOG_D("link up");
  409. eth_device_linkchange(&hc32_eth_device.parent, RT_TRUE);
  410. }
  411. else
  412. {
  413. LOG_I("link down");
  414. eth_device_linkchange(&hc32_eth_device.parent, RT_FALSE);
  415. ETH_Stop();
  416. (void)ETH_DMA_TxDescListInit(&EthHandle, EthDmaTxDscrTab, EthTxBuff, ETH_TX_BUF_NUM);
  417. (void)ETH_DMA_RxDescListInit(&EthHandle, EthDmaRxDscrTab, EthRxBuff, ETH_RX_BUF_NUM);
  418. }
  419. }
  420. }
  421. #if defined(ETH_PHY_USING_INTERRUPT_MODE)
  422. static void eth_phy_irq_handler(void *args)
  423. {
  424. #if defined (ETH_PHY_USING_RTL8201F)
  425. rt_uint16_t status = 0;
  426. ETH_PHY_ReadReg(&EthHandle, PHY_IISDR, &status);
  427. LOG_D("phy interrupt status reg is 0x%X", status);
  428. #endif
  429. hc32_phy_link_change();
  430. }
  431. #endif
  432. static void hc32_phy_monitor_thread(void *parameter)
  433. {
  434. uint8_t phy_addr = 0xFF;
  435. uint8_t detected_count = 0;
  436. uint16_t u16RegVal;
  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 (ETH_PHY_USING_RTL8201F)
  465. /* Configure PHY LED mode */
  466. u16RegVal = PHY_PAGE_ADDR_7;
  467. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  468. (void)ETH_PHY_ReadReg(&EthHandle, PHY_PSR, &u16RegVal);
  469. (void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
  470. MODIFY_REG16(u16RegVal, PHY_LED_SELECT, PHY_LED_SELECT_10);
  471. (void)ETH_PHY_WriteReg(&EthHandle, PHY_P7_IWLFR, u16RegVal);
  472. (void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
  473. u16RegVal = PHY_PAGE_ADDR_0;
  474. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  475. (void)ETH_PHY_ReadReg(&EthHandle, PHY_PSR, &u16RegVal);
  476. #if defined(ETH_PHY_USING_RTL8201F) && defined(ETH_INTERFACE_USING_RMII)
  477. /* Disable Power Saving Mode */
  478. (void)ETH_PHY_ReadReg(&EthHandle, PHY_PSMR, &u16RegVal);
  479. CLR_REG16_BIT(u16RegVal, PHY_EN_PWR_SAVE);
  480. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSMR, u16RegVal);
  481. #endif
  482. #endif
  483. #if defined(ETH_PHY_USING_INTERRUPT_MODE)
  484. /* configuration intterrupt pin */
  485. rt_pin_mode(ETH_PHY_INTERRUPT_PIN, PIN_MODE_INPUT_PULLUP);
  486. rt_pin_attach_irq(ETH_PHY_INTERRUPT_PIN, PIN_IRQ_MODE_FALLING, eth_phy_irq_handler, (void *)"callbackargs");
  487. rt_pin_irq_enable(ETH_PHY_INTERRUPT_PIN, PIN_IRQ_ENABLE);
  488. #if defined (ETH_PHY_USING_RTL8201F)
  489. /* Configure PHY to generate an interrupt when Eth Link state changes */
  490. u16RegVal = PHY_PAGE_ADDR_7;
  491. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  492. /* Enable Interrupt on change of link status */
  493. (void)ETH_PHY_ReadReg(&EthHandle, PHY_P7_IWLFR, &u16RegVal);
  494. SET_REG16_BIT(u16RegVal, PHY_INT_LINK_CHANGE);
  495. (void)ETH_PHY_WriteReg(&EthHandle, PHY_P7_IWLFR, u16RegVal);
  496. u16RegVal = PHY_PAGE_ADDR_0;
  497. (void)ETH_PHY_WriteReg(&EthHandle, PHY_PSR, u16RegVal);
  498. #endif
  499. #else
  500. hc32_eth_device.poll_link_timer = rt_timer_create("eth_phy_link", (void (*)(void *))hc32_phy_link_change,
  501. NULL, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
  502. if (!hc32_eth_device.poll_link_timer || rt_timer_start(hc32_eth_device.poll_link_timer) != RT_EOK)
  503. {
  504. LOG_E("Start eth phy link change detection timer failed");
  505. }
  506. #endif
  507. }
  508. /* Register the eth device */
  509. static int rt_hw_hc32_eth_init(void)
  510. {
  511. rt_err_t state = RT_EOK;
  512. /* register eth handler */
  513. hc32_install_irq_handler(&hc32_eth_device.irq_config, hc32_eth_device.irq_callback, RT_FALSE);
  514. /* Prepare receive and send buffers */
  515. EthRxBuff = (rt_uint8_t *)rt_calloc(ETH_RX_BUF_NUM, ETH_MAX_PACKET_SIZE);
  516. if (EthRxBuff == RT_NULL)
  517. {
  518. LOG_E("No memory");
  519. state = -RT_ENOMEM;
  520. goto __exit;
  521. }
  522. EthTxBuff = (rt_uint8_t *)rt_calloc(ETH_TX_BUF_NUM, ETH_MAX_PACKET_SIZE);
  523. if (EthTxBuff == RT_NULL)
  524. {
  525. LOG_E("No memory");
  526. state = -RT_ENOMEM;
  527. goto __exit;
  528. }
  529. EthDmaRxDscrTab = (stc_eth_dma_desc_t *)rt_calloc(ETH_RX_BUF_NUM, sizeof(stc_eth_dma_desc_t));
  530. if (EthDmaRxDscrTab == RT_NULL)
  531. {
  532. LOG_E("No memory");
  533. state = -RT_ENOMEM;
  534. goto __exit;
  535. }
  536. EthDmaTxDscrTab = (stc_eth_dma_desc_t *)rt_calloc(ETH_TX_BUF_NUM, sizeof(stc_eth_dma_desc_t));
  537. if (EthDmaTxDscrTab == RT_NULL)
  538. {
  539. LOG_E("No memory");
  540. state = -RT_ENOMEM;
  541. goto __exit;
  542. }
  543. hc32_eth_device.eth_speed = ETH_MAC_SPEED_100M;
  544. hc32_eth_device.eth_mode = ETH_MAC_DUPLEX_MD_FULL;
  545. /* 00-80 uid */
  546. hc32_eth_device.dev_addr[0] = 0x02;
  547. hc32_eth_device.dev_addr[1] = 0x80;
  548. /* generate MAC addr from unique ID */
  549. hc32_eth_device.dev_addr[2] = (rt_uint8_t)(READ_REG32(CM_EFM->UQID1) >> 8U);
  550. hc32_eth_device.dev_addr[3] = (rt_uint8_t)(READ_REG32(CM_EFM->UQID1) >> 16U);
  551. hc32_eth_device.dev_addr[4] = (rt_uint8_t)READ_REG32(CM_EFM->UQID2);
  552. hc32_eth_device.dev_addr[5] = (rt_uint8_t)(READ_REG32(CM_EFM->UQID2) >> 8U);
  553. hc32_eth_device.parent.parent.init = rt_hc32_eth_init;
  554. hc32_eth_device.parent.parent.open = rt_hc32_eth_open;
  555. hc32_eth_device.parent.parent.close = rt_hc32_eth_close;
  556. hc32_eth_device.parent.parent.read = rt_hc32_eth_read;
  557. hc32_eth_device.parent.parent.write = rt_hc32_eth_write;
  558. hc32_eth_device.parent.parent.control = rt_hc32_eth_control;
  559. hc32_eth_device.parent.parent.user_data = RT_NULL;
  560. hc32_eth_device.parent.eth_rx = rt_hc32_eth_rx;
  561. hc32_eth_device.parent.eth_tx = rt_hc32_eth_tx;
  562. /* register eth device */
  563. state = eth_device_init(&(hc32_eth_device.parent), "e0");
  564. if (RT_EOK == state)
  565. {
  566. LOG_D("eth device init success");
  567. }
  568. else
  569. {
  570. LOG_E("eth device init faild: %d", state);
  571. state = -RT_ERROR;
  572. goto __exit;
  573. }
  574. /* start phy monitor */
  575. rt_thread_t tid;
  576. tid = rt_thread_create("phy_monitor", hc32_phy_monitor_thread, RT_NULL, 1024, 12, 5);
  577. if (tid != RT_NULL)
  578. {
  579. rt_thread_startup(tid);
  580. }
  581. else
  582. {
  583. state = -RT_ERROR;
  584. }
  585. __exit:
  586. if (state != RT_EOK)
  587. {
  588. if (EthRxBuff)
  589. {
  590. rt_free(EthRxBuff);
  591. }
  592. if (EthTxBuff)
  593. {
  594. rt_free(EthTxBuff);
  595. }
  596. if (EthDmaRxDscrTab)
  597. {
  598. rt_free(EthDmaRxDscrTab);
  599. }
  600. if (EthDmaTxDscrTab)
  601. {
  602. rt_free(EthDmaTxDscrTab);
  603. }
  604. }
  605. return state;
  606. }
  607. INIT_DEVICE_EXPORT(rt_hw_hc32_eth_init);
  608. #endif