drv_eth.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-10-10 Tanek the first version
  9. * 2019-5-10 misonyo add DMA TX and RX function
  10. * 2020-10-14 wangqiang use phy device in phy monitor thread
  11. */
  12. #include <rtthread.h>
  13. #include "board.h"
  14. #include <rtdevice.h>
  15. #ifdef RT_USING_FINSH
  16. #include <finsh.h>
  17. #endif
  18. #include "fsl_enet.h"
  19. #include "fsl_gpio.h"
  20. #include "fsl_cache.h"
  21. #include "fsl_iomuxc.h"
  22. #include "fsl_common.h"
  23. #ifdef RT_USING_LWIP
  24. #include <netif/ethernetif.h>
  25. #include "lwipopts.h"
  26. #define ENET_RXBD_NUM (4)
  27. #define ENET_TXBD_NUM (4)
  28. #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  29. #define ENET_TXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  30. /* debug option */
  31. #define ETH_RX_DUMP
  32. #undef ETH_RX_DUMP
  33. #define ETH_TX_DUMP
  34. #undef ETH_TX_DUMP
  35. #define DBG_ENABLE
  36. #define DBG_SECTION_NAME "[ETH]"
  37. #define DBG_COLOR
  38. #define DBG_LEVEL DBG_INFO
  39. #include <rtdbg.h>
  40. #define MAX_ADDR_LEN 6
  41. #define ENET_RING_NUM 1U
  42. #define RING_ID 0
  43. typedef uint8_t rx_buffer_t[RT_ALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  44. typedef uint8_t tx_buffer_t[RT_ALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  45. #ifndef ENET_RXBUFF_NUM
  46. #define ENET_RXBUFF_NUM (ENET_RXBD_NUM * 2)
  47. #endif
  48. typedef void (*pbuf_free_custom_fn)(struct pbuf *p);
  49. /** A custom pbuf: like a pbuf, but following a function pointer to free it. */
  50. struct pbuf_custom
  51. {
  52. /** The actual pbuf */
  53. struct pbuf pbuf;
  54. /** This function is called when pbuf_free deallocates this pbuf(_custom) */
  55. pbuf_free_custom_fn custom_free_function;
  56. };
  57. typedef struct rx_pbuf_wrapper
  58. {
  59. struct pbuf_custom p; /*!< Pbuf wrapper. Has to be first. */
  60. void *buffer; /*!< Original buffer wrapped by p. */
  61. volatile bool buffer_used; /*!< Wrapped buffer is used by ENET */
  62. } rx_pbuf_wrapper_t;
  63. struct rt_imxrt_eth
  64. {
  65. /* inherit from ethernet device */
  66. struct eth_device parent;
  67. enet_handle_t enet_handle;
  68. ENET_Type *enet_base;
  69. enet_data_error_stats_t error_statistic;
  70. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  71. rt_bool_t tx_is_waiting;
  72. struct rt_semaphore tx_wait;
  73. struct rt_semaphore buff_wait;
  74. enet_mii_speed_t speed;
  75. enet_mii_duplex_t duplex;
  76. enet_rx_bd_struct_t *RxBuffDescrip;
  77. enet_tx_bd_struct_t *TxBuffDescrip;
  78. rx_buffer_t *RxDataBuff;
  79. tx_buffer_t *TxDataBuff;
  80. rx_pbuf_wrapper_t RxPbufs[ENET_RXBUFF_NUM];
  81. };
  82. AT_NONCACHEABLE_SECTION_ALIGN(static enet_tx_bd_struct_t g_txBuffDescrip[ENET_TXBD_NUM], ENET_BUFF_ALIGNMENT);
  83. ALIGN(ENET_BUFF_ALIGNMENT)
  84. rt_uint8_t g_txDataBuff[ENET_TXBD_NUM][RT_ALIGN(ENET_TXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  85. AT_NONCACHEABLE_SECTION_ALIGN(static enet_rx_bd_struct_t g_rxBuffDescrip[ENET_RXBD_NUM], ENET_BUFF_ALIGNMENT);
  86. ALIGN(ENET_BUFF_ALIGNMENT)
  87. rt_uint8_t g_rxDataBuff[ENET_RXBD_NUM][RT_ALIGN(ENET_RXBUFF_SIZE, ENET_BUFF_ALIGNMENT)];
  88. static struct rt_imxrt_eth imxrt_eth_device;
  89. void _enet_rx_callback(struct rt_imxrt_eth *eth)
  90. {
  91. rt_err_t result;
  92. ENET_DisableInterrupts(eth->enet_base, kENET_RxFrameInterrupt);
  93. result = eth_device_ready(&(eth->parent));
  94. if (result != RT_EOK)
  95. rt_kprintf("RX err =%d\n", result);
  96. }
  97. void _enet_tx_callback(struct rt_imxrt_eth *eth)
  98. {
  99. if (eth->tx_is_waiting == RT_TRUE)
  100. {
  101. eth->tx_is_waiting = RT_FALSE;
  102. rt_sem_release(&eth->tx_wait);
  103. }
  104. }
  105. static void _enet_callback(ENET_Type *base,
  106. enet_handle_t *handle,
  107. #if FSL_FEATURE_ENET_QUEUE > 1
  108. uint32_t ringId,
  109. #endif /* FSL_FEATURE_ENET_QUEUE */
  110. enet_event_t event,
  111. enet_frame_info_t *frameInfo,
  112. void *userData)
  113. {
  114. switch (event)
  115. {
  116. case kENET_RxEvent:
  117. _enet_rx_callback((struct rt_imxrt_eth *)userData);
  118. break;
  119. case kENET_TxEvent:
  120. _enet_tx_callback((struct rt_imxrt_eth *)userData);
  121. break;
  122. case kENET_ErrEvent:
  123. dbg_log(DBG_LOG, "kENET_ErrEvent\n");
  124. break;
  125. case kENET_WakeUpEvent:
  126. dbg_log(DBG_LOG, "kENET_WakeUpEvent\n");
  127. break;
  128. case kENET_TimeStampEvent:
  129. dbg_log(DBG_LOG, "kENET_TimeStampEvent\n");
  130. break;
  131. case kENET_TimeStampAvailEvent:
  132. dbg_log(DBG_LOG, "kENET_TimeStampAvailEvent \n");
  133. break;
  134. default:
  135. dbg_log(DBG_LOG, "unknow error\n");
  136. break;
  137. }
  138. }
  139. static void _enet_clk_init(void)
  140. {
  141. #ifdef SOC_IMXRT1170_SERIES
  142. #ifdef PHY_USING_RTL8211F
  143. const clock_sys_pll1_config_t sysPll1Config = {
  144. .pllDiv2En = true,
  145. };
  146. CLOCK_InitSysPll1(&sysPll1Config);
  147. clock_root_config_t rootCfg = {.mux = 4, .div = 4}; /* Generate 125M root clock. */
  148. CLOCK_SetRootClock(kCLOCK_Root_Enet2, &rootCfg);
  149. IOMUXC_GPR->GPR5 |= IOMUXC_GPR_GPR5_ENET1G_RGMII_EN_MASK; /* bit1:iomuxc_gpr_enet_clk_dir
  150. bit0:GPR_ENET_TX_CLK_SEL(internal or OSC) */
  151. #else
  152. const clock_sys_pll1_config_t sysPll1Config = {
  153. .pllDiv2En = true,
  154. };
  155. CLOCK_InitSysPll1(&sysPll1Config);
  156. clock_root_config_t rootCfg = {.mux = 4, .div = 10}; /* Generate 50M root clock. */
  157. CLOCK_SetRootClock(kCLOCK_Root_Enet1, &rootCfg);
  158. /* Select syspll2pfd3, 528*18/24 = 396M */
  159. CLOCK_InitPfd(kCLOCK_PllSys2, kCLOCK_Pfd3, 24);
  160. rootCfg.mux = 7;
  161. rootCfg.div = 2;
  162. CLOCK_SetRootClock(kCLOCK_Root_Bus, &rootCfg); /* Generate 198M bus clock. */
  163. IOMUXC_GPR->GPR4 |= 0x3;
  164. #endif
  165. #else
  166. const clock_enet_pll_config_t config = {.enableClkOutput = true, .enableClkOutput25M = false, .loopDivider = 1};
  167. CLOCK_InitEnetPll(&config);
  168. IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
  169. IOMUXC_GPR->GPR1 |= 1 << 23;
  170. #endif
  171. }
  172. static void *_enet_rx_alloc(ENET_Type *base, void *userData, uint8_t ringId)
  173. {
  174. void *buffer = NULL;
  175. int i;
  176. // dbg_log(DBG_LOG, "get buff_wait sem in %d\r\n", __LINE__);
  177. rt_sem_take(&imxrt_eth_device.buff_wait, RT_WAITING_FOREVER);
  178. for (i = 0; i < ENET_RXBUFF_NUM; i++)
  179. {
  180. if (!imxrt_eth_device.RxPbufs[i].buffer_used)
  181. {
  182. imxrt_eth_device.RxPbufs[i].buffer_used = true;
  183. buffer = &imxrt_eth_device.RxDataBuff[i];
  184. break;
  185. }
  186. }
  187. rt_sem_release(&imxrt_eth_device.buff_wait);
  188. // dbg_log(DBG_LOG, "release buff_wait sem in %d\r\n", __LINE__);
  189. return buffer;
  190. }
  191. static void _enet_rx_free(ENET_Type *base, void *buffer, void *userData, uint8_t ringId)
  192. {
  193. int idx = ((rx_buffer_t *)buffer) - imxrt_eth_device.RxDataBuff;
  194. if (!((idx >= 0) && (idx < ENET_RXBUFF_NUM)))
  195. {
  196. LOG_E("Freed buffer out of range\r\n");
  197. }
  198. // dbg_log(DBG_LOG, "get buff_wait sem in %d\r\n", __LINE__);
  199. rt_sem_take(&imxrt_eth_device.buff_wait, RT_WAITING_FOREVER);
  200. if (!(imxrt_eth_device.RxPbufs[idx].buffer_used))
  201. {
  202. LOG_E("_enet_rx_free: freeing unallocated buffer\r\n");
  203. }
  204. imxrt_eth_device.RxPbufs[idx].buffer_used = false;
  205. rt_sem_release(&imxrt_eth_device.buff_wait);
  206. // dbg_log(DBG_LOG, "release buff_wait sem in %d\r\n", __LINE__);
  207. }
  208. /**
  209. * Reclaims RX buffer held by the p after p is no longer used
  210. * by the application / lwIP.
  211. */
  212. static void _enet_rx_release(struct pbuf *p)
  213. {
  214. rx_pbuf_wrapper_t *wrapper = (rx_pbuf_wrapper_t *)p;
  215. _enet_rx_free(imxrt_eth_device.enet_base, wrapper->buffer, &imxrt_eth_device, 0);
  216. }
  217. static void _enet_config(void)
  218. {
  219. enet_config_t config;
  220. uint32_t sysClock;
  221. enet_buffer_config_t buffConfig[ENET_RING_NUM];
  222. int i;
  223. #ifdef PHY_USING_RTL8211F
  224. EnableIRQ(ENET_1G_MAC0_Tx_Rx_1_IRQn);
  225. EnableIRQ(ENET_1G_MAC0_Tx_Rx_2_IRQn);
  226. #endif
  227. imxrt_eth_device.RxBuffDescrip = &g_rxBuffDescrip[0];
  228. imxrt_eth_device.TxBuffDescrip = &g_txBuffDescrip[0];
  229. imxrt_eth_device.RxDataBuff = &g_rxDataBuff[0];
  230. imxrt_eth_device.TxDataBuff = &g_txDataBuff[0];
  231. /* prepare the buffer configuration. */
  232. // enet_buffer_config_t buffConfig[RING_NUM] =
  233. // {
  234. // ENET_RXBD_NUM,
  235. // ENET_TXBD_NUM,
  236. // sizeof(rx_buffer_t),
  237. // sizeof(tx_buffer_t),
  238. // &imxrt_eth_device.RxBuffDescrip[0],
  239. // &imxrt_eth_device.TxBuffDescrip[0],
  240. // NULL,
  241. // &imxrt_eth_device.TxDataBuff[0][0],
  242. // true,
  243. // true,
  244. // NULL,
  245. // };
  246. buffConfig[0].rxBdNumber = ENET_RXBD_NUM; /* Receive buffer descriptor number. */
  247. buffConfig[0].txBdNumber = ENET_TXBD_NUM; /* Transmit buffer descriptor number. */
  248. buffConfig[0].rxBuffSizeAlign = sizeof(rx_buffer_t); /* Aligned receive data buffer size. */
  249. buffConfig[0].txBuffSizeAlign = sizeof(tx_buffer_t); /* Aligned transmit data buffer size. */
  250. buffConfig[0].rxBdStartAddrAlign =
  251. &(imxrt_eth_device.RxBuffDescrip[0]); /* Aligned receive buffer descriptor start address. */
  252. buffConfig[0].txBdStartAddrAlign =
  253. &(imxrt_eth_device.TxBuffDescrip[0]); /* Aligned transmit buffer descriptor start address. */
  254. buffConfig[0].rxBufferAlign =
  255. NULL; /* Receive data buffer start address. NULL when buffers are allocated by callback for RX zero-copy. */
  256. buffConfig[0].txBufferAlign = &(imxrt_eth_device.TxDataBuff[0][0]); /* Transmit data buffer start address. */
  257. buffConfig[0].txFrameInfo = NULL; /* Transmit frame information start address. Set only if using zero-copy transmit. */
  258. buffConfig[0].rxMaintainEnable = true; /* Receive buffer cache maintain. */
  259. buffConfig[0].txMaintainEnable = true; /* Transmit buffer cache maintain. */
  260. /* Get default configuration. */
  261. /*
  262. * config.miiMode = kENET_RmiiMode;
  263. * config.miiSpeed = kENET_MiiSpeed100M;
  264. * config.miiDuplex = kENET_MiiFullDuplex;
  265. * config.rxMaxFrameLen = ENET_FRAME_MAX_FRAMELEN;
  266. */
  267. ENET_GetDefaultConfig(&config);
  268. config.ringNum = ENET_RING_NUM;
  269. config.miiSpeed = imxrt_eth_device.speed;
  270. config.miiDuplex = imxrt_eth_device.duplex;
  271. #ifdef PHY_USING_RTL8211F
  272. config.miiMode = kENET_RgmiiMode;
  273. #else
  274. config.miiMode = kENET_RmiiMode;
  275. #endif
  276. config.rxBuffAlloc = _enet_rx_alloc;
  277. config.rxBuffFree = _enet_rx_free;
  278. config.userData = &imxrt_eth_device;
  279. /* Set SMI to get PHY link status. */
  280. #ifdef SOC_IMXRT1170_SERIES
  281. sysClock = CLOCK_GetRootClockFreq(kCLOCK_Root_Bus);
  282. #else
  283. sysClock = CLOCK_GetFreq(kCLOCK_AhbClk);
  284. #endif
  285. // dbg_log(DBG_LOG, "deinit\n");
  286. // ENET_Deinit(imxrt_eth_device.enet_base);
  287. config.interrupt |= kENET_TxFrameInterrupt | kENET_RxFrameInterrupt | kENET_TxBufferInterrupt | kENET_LateCollisionInterrupt;
  288. config.callback = _enet_callback;
  289. for (i = 0; i < ENET_RXBUFF_NUM; i++)
  290. {
  291. imxrt_eth_device.RxPbufs[i].p.custom_free_function = _enet_rx_release;
  292. imxrt_eth_device.RxPbufs[i].buffer = &(imxrt_eth_device.RxDataBuff[i][0]);
  293. imxrt_eth_device.RxPbufs[i].buffer_used = false;
  294. }
  295. // dbg_log(DBG_LOG, "deinit\n");
  296. // ENET_Deinit(imxrt_eth_device.enet_base);
  297. dbg_log(DBG_LOG, "init\n");
  298. ENET_Init(imxrt_eth_device.enet_base, &imxrt_eth_device.enet_handle, &config, &buffConfig[0], &imxrt_eth_device.dev_addr[0], sysClock);
  299. // dbg_log(DBG_LOG, "set call back\n");
  300. // ENET_SetCallback(&imxrt_eth_device.enet_handle, _enet_callback, &imxrt_eth_device);
  301. dbg_log(DBG_LOG, "active read\n");
  302. ENET_ActiveRead(imxrt_eth_device.enet_base);
  303. }
  304. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  305. static void packet_dump(const char *msg, const struct pbuf *p)
  306. {
  307. const struct pbuf *q;
  308. rt_uint32_t i, j;
  309. rt_uint8_t *ptr;
  310. rt_kprintf("%s %d byte\n", msg, p->tot_len);
  311. i = 0;
  312. for (q = p; q != RT_NULL; q = q->next)
  313. {
  314. ptr = q->payload;
  315. for (j = 0; j < q->len; j++)
  316. {
  317. if ((i % 8) == 0)
  318. {
  319. rt_kprintf(" ");
  320. }
  321. if ((i % 16) == 0)
  322. {
  323. rt_kprintf("\r\n");
  324. }
  325. rt_kprintf("%02x ", *ptr);
  326. i++;
  327. ptr++;
  328. }
  329. }
  330. rt_kprintf("\n\n");
  331. }
  332. #else
  333. #define packet_dump(...)
  334. #endif /* dump */
  335. /* initialize the interface */
  336. static rt_err_t rt_imxrt_eth_init(rt_device_t dev)
  337. {
  338. dbg_log(DBG_LOG, "rt_imxrt_eth_init...\n");
  339. _enet_config();
  340. return RT_EOK;
  341. }
  342. static rt_err_t rt_imxrt_eth_open(rt_device_t dev, rt_uint16_t oflag)
  343. {
  344. dbg_log(DBG_LOG, "rt_imxrt_eth_open...\n");
  345. return RT_EOK;
  346. }
  347. static rt_err_t rt_imxrt_eth_close(rt_device_t dev)
  348. {
  349. dbg_log(DBG_LOG, "rt_imxrt_eth_close...\n");
  350. return RT_EOK;
  351. }
  352. static rt_size_t rt_imxrt_eth_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  353. {
  354. dbg_log(DBG_LOG, "rt_imxrt_eth_read...\n");
  355. rt_set_errno(-RT_ENOSYS);
  356. return 0;
  357. }
  358. static rt_size_t rt_imxrt_eth_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  359. {
  360. dbg_log(DBG_LOG, "rt_imxrt_eth_write...\n");
  361. rt_set_errno(-RT_ENOSYS);
  362. return 0;
  363. }
  364. static rt_err_t rt_imxrt_eth_control(rt_device_t dev, int cmd, void *args)
  365. {
  366. dbg_log(DBG_LOG, "rt_imxrt_eth_control...\n");
  367. switch (cmd)
  368. {
  369. case NIOCTL_GADDR:
  370. /* get mac address */
  371. if (args)
  372. rt_memcpy(args, imxrt_eth_device.dev_addr, 6);
  373. else
  374. return -RT_ERROR;
  375. break;
  376. default:
  377. break;
  378. }
  379. return RT_EOK;
  380. }
  381. /* ethernet device interface */
  382. /* transmit packet. */
  383. rt_err_t rt_imxrt_eth_tx(rt_device_t dev, struct pbuf *p)
  384. {
  385. rt_err_t result = RT_EOK;
  386. enet_handle_t *enet_handle = &imxrt_eth_device.enet_handle;
  387. RT_ASSERT(p != NULL);
  388. RT_ASSERT(enet_handle != RT_NULL);
  389. dbg_log(DBG_LOG, "rt_imxrt_eth_tx: %d\n", p->len);
  390. #ifdef ETH_TX_DUMP
  391. packet_dump("send", p);
  392. #endif
  393. do
  394. {
  395. result = ENET_SendFrame(imxrt_eth_device.enet_base, enet_handle, (const uint8_t *)p, p->tot_len, RING_ID, false, NULL);
  396. if (result == kStatus_ENET_TxFrameBusy)
  397. {
  398. imxrt_eth_device.tx_is_waiting = RT_TRUE;
  399. rt_sem_take(&imxrt_eth_device.tx_wait, RT_WAITING_FOREVER);
  400. }
  401. } while (result == kStatus_ENET_TxFrameBusy);
  402. return RT_EOK;
  403. }
  404. /* reception packet. */
  405. struct pbuf *rt_imxrt_eth_rx(rt_device_t dev)
  406. {
  407. uint32_t length = 0;
  408. status_t status;
  409. struct pbuf *p = RT_NULL;
  410. enet_handle_t *enet_handle = &imxrt_eth_device.enet_handle;
  411. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  412. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  413. /* Get the Frame size */
  414. status = ENET_GetRxFrameSize(enet_handle, &length, RING_ID);
  415. /* Call ENET_ReadFrame when there is a received frame. */
  416. if (length != 0)
  417. {
  418. /* Received valid frame. Deliver the rx buffer with the size equal to length. */
  419. p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
  420. if (p != NULL)
  421. {
  422. status = ENET_ReadFrame(enet_base, enet_handle, p->payload, length, RING_ID, NULL);
  423. if (status == kStatus_Success)
  424. {
  425. #ifdef ETH_RX_DUMP
  426. packet_dump("recv", p);
  427. #endif
  428. return p;
  429. }
  430. else
  431. {
  432. dbg_log(DBG_LOG, " A frame read failed\n");
  433. pbuf_free(p);
  434. }
  435. }
  436. else
  437. {
  438. dbg_log(DBG_LOG, " pbuf_alloc faild\n");
  439. }
  440. }
  441. else if (status == kStatus_ENET_RxFrameError)
  442. {
  443. dbg_log(DBG_WARNING, "ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
  444. /* Update the received buffer when error happened. */
  445. /* Get the error information of the received g_frame. */
  446. ENET_GetRxErrBeforeReadFrame(enet_handle, error_statistic, RING_ID);
  447. /* update the receive buffer. */
  448. ENET_ReadFrame(enet_base, enet_handle, NULL, 0, RING_ID, NULL);
  449. }
  450. ENET_EnableInterrupts(enet_base, kENET_RxFrameInterrupt);
  451. return NULL;
  452. }
  453. #ifdef BSP_USING_PHY
  454. static struct rt_phy_device *phy_dev = RT_NULL;
  455. static void phy_monitor_thread_entry(void *parameter)
  456. {
  457. rt_uint32_t speed;
  458. rt_uint32_t duplex;
  459. rt_bool_t link = RT_FALSE;
  460. #ifdef SOC_IMXRT1170_SERIES
  461. #ifdef PHY_USING_RTL8211F
  462. phy_dev = (struct rt_phy_device *)rt_device_find("rtl8211f");
  463. if ((RT_NULL == phy_dev) || (RT_NULL == phy_dev->ops))
  464. {
  465. // TODO print warning information
  466. LOG_E("Can not find phy device called \"rtl8211f\"");
  467. return;
  468. }
  469. #else
  470. phy_dev = (struct rt_phy_device *)rt_device_find("ksz8081");
  471. if ((RT_NULL == phy_dev) || (RT_NULL == phy_dev->ops))
  472. {
  473. // TODO print warning information
  474. LOG_E("Can not find phy device called \"ksz8081\"");
  475. return;
  476. }
  477. #endif
  478. #else
  479. phy_dev = (struct rt_phy_device *)rt_device_find("rtt-phy");
  480. if ((RT_NULL == phy_dev) || (RT_NULL == phy_dev->ops))
  481. {
  482. // TODO print warning information
  483. LOG_E("Can not find phy device called \"rtt-phy\"");
  484. return;
  485. }
  486. #endif
  487. if (RT_NULL == phy_dev->ops->init)
  488. {
  489. LOG_E("phy driver error!");
  490. return;
  491. }
  492. #ifdef SOC_IMXRT1170_SERIES
  493. #ifdef PHY_USING_RTL8211F
  494. rt_phy_status status = phy_dev->ops->init(imxrt_eth_device.enet_base, PHY_RTL8211F_ADDRESS, CLOCK_GetRootClockFreq(kCLOCK_Root_Bus));
  495. #else
  496. rt_phy_status status = phy_dev->ops->init(imxrt_eth_device.enet_base, PHY_KSZ8081_ADDRESS, CLOCK_GetRootClockFreq(kCLOCK_Root_Bus));
  497. #endif
  498. #else
  499. rt_phy_status status = phy_dev->ops->init(imxrt_eth_device.enet_base, PHY_DEVICE_ADDRESS, CLOCK_GetFreq(kCLOCK_AhbClk));
  500. #endif
  501. if (PHY_STATUS_OK != status)
  502. {
  503. LOG_E("Phy device initialize unsuccessful!\n");
  504. return;
  505. }
  506. LOG_I("Phy device initialize successfully!\n");
  507. while (1)
  508. {
  509. rt_bool_t new_link = RT_FALSE;
  510. rt_phy_status status = phy_dev->ops->get_link_status(&new_link);
  511. if ((PHY_STATUS_OK == status) && (link != new_link))
  512. {
  513. link = new_link;
  514. if (link) // link up
  515. {
  516. phy_dev->ops->get_link_speed_duplex(&speed, &duplex);
  517. if (PHY_SPEED_10M == speed)
  518. {
  519. dbg_log(DBG_LOG, "10M\n");
  520. }
  521. else if (PHY_SPEED_100M == speed)
  522. {
  523. dbg_log(DBG_LOG, "100M\n");
  524. }
  525. else
  526. {
  527. dbg_log(DBG_LOG, "1000M\n");
  528. }
  529. if (PHY_HALF_DUPLEX == duplex)
  530. {
  531. dbg_log(DBG_LOG, "half dumplex\n");
  532. }
  533. else
  534. {
  535. dbg_log(DBG_LOG, "full dumplex\n");
  536. }
  537. if ((imxrt_eth_device.speed != (enet_mii_speed_t)speed) || (imxrt_eth_device.duplex != (enet_mii_duplex_t)duplex))
  538. {
  539. imxrt_eth_device.speed = (enet_mii_speed_t)speed;
  540. imxrt_eth_device.duplex = (enet_mii_duplex_t)duplex;
  541. dbg_log(DBG_LOG, "link up, and update eth mode.\n");
  542. rt_imxrt_eth_init((rt_device_t)&imxrt_eth_device);
  543. }
  544. else
  545. {
  546. dbg_log(DBG_LOG, "link up, eth not need re-config.\n");
  547. }
  548. dbg_log(DBG_LOG, "link up.\n");
  549. eth_device_linkchange(&imxrt_eth_device.parent, RT_TRUE);
  550. }
  551. else
  552. {
  553. dbg_log(DBG_LOG, "link down.\n");
  554. eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
  555. }
  556. }
  557. rt_thread_delay(RT_TICK_PER_SECOND * 2);
  558. }
  559. }
  560. #endif
  561. static int rt_hw_imxrt_eth_init(void)
  562. {
  563. rt_err_t state;
  564. _enet_clk_init();
  565. #ifdef PHY_USING_RTL8211F
  566. /* NXP (Freescale) MAC OUI */
  567. imxrt_eth_device.dev_addr[0] = 0x54;
  568. imxrt_eth_device.dev_addr[1] = 0x27;
  569. imxrt_eth_device.dev_addr[2] = 0x8d;
  570. /* generate MAC addr from 96bit unique ID (only for test). */
  571. imxrt_eth_device.dev_addr[3] = 0x11;
  572. imxrt_eth_device.dev_addr[4] = 0x22;
  573. imxrt_eth_device.dev_addr[5] = 0x33;
  574. imxrt_eth_device.speed = kENET_MiiSpeed1000M;
  575. imxrt_eth_device.duplex = kENET_MiiFullDuplex;
  576. imxrt_eth_device.enet_base = ENET_1G;
  577. #else
  578. /* NXP (Freescale) MAC OUI */
  579. imxrt_eth_device.dev_addr[0] = 0x00;
  580. imxrt_eth_device.dev_addr[1] = 0x04;
  581. imxrt_eth_device.dev_addr[2] = 0x9F;
  582. /* generate MAC addr from 96bit unique ID (only for test). */
  583. imxrt_eth_device.dev_addr[3] = 0x05;
  584. imxrt_eth_device.dev_addr[4] = 0x44;
  585. imxrt_eth_device.dev_addr[5] = 0xE5;
  586. imxrt_eth_device.speed = kENET_MiiSpeed100M;
  587. imxrt_eth_device.duplex = kENET_MiiFullDuplex;
  588. imxrt_eth_device.enet_base = ENET;
  589. #endif
  590. imxrt_eth_device.parent.parent.init = rt_imxrt_eth_init;
  591. imxrt_eth_device.parent.parent.open = rt_imxrt_eth_open;
  592. imxrt_eth_device.parent.parent.close = rt_imxrt_eth_close;
  593. imxrt_eth_device.parent.parent.read = rt_imxrt_eth_read;
  594. imxrt_eth_device.parent.parent.write = rt_imxrt_eth_write;
  595. imxrt_eth_device.parent.parent.control = rt_imxrt_eth_control;
  596. imxrt_eth_device.parent.parent.user_data = RT_NULL;
  597. imxrt_eth_device.parent.eth_rx = rt_imxrt_eth_rx;
  598. imxrt_eth_device.parent.eth_tx = rt_imxrt_eth_tx;
  599. dbg_log(DBG_LOG, "sem init: tx_wait\r\n");
  600. /* init tx semaphore */
  601. rt_sem_init(&imxrt_eth_device.tx_wait, "tx_wait", 0, RT_IPC_FLAG_FIFO);
  602. dbg_log(DBG_LOG, "sem init: buff_wait\r\n");
  603. /* init tx semaphore */
  604. rt_sem_init(&imxrt_eth_device.buff_wait, "buff_wait", 1, RT_IPC_FLAG_FIFO);
  605. /* register eth device */
  606. dbg_log(DBG_LOG, "eth_device_init start\r\n");
  607. state = eth_device_init(&(imxrt_eth_device.parent), "e0");
  608. if (RT_EOK == state)
  609. {
  610. dbg_log(DBG_LOG, "eth_device_init success\r\n");
  611. }
  612. else
  613. {
  614. dbg_log(DBG_LOG, "eth_device_init faild: %d\r\n", state);
  615. }
  616. eth_device_linkchange(&imxrt_eth_device.parent, RT_FALSE);
  617. /* start phy monitor */
  618. {
  619. #ifdef BSP_USING_PHY
  620. rt_thread_t tid;
  621. tid = rt_thread_create("phy",
  622. phy_monitor_thread_entry,
  623. RT_NULL,
  624. 1024,
  625. RT_THREAD_PRIORITY_MAX - 2,
  626. 2);
  627. if (tid != RT_NULL)
  628. rt_thread_startup(tid);
  629. #endif
  630. }
  631. return state;
  632. }
  633. INIT_DEVICE_EXPORT(rt_hw_imxrt_eth_init);
  634. #endif
  635. #if defined(RT_USING_FINSH) && defined(RT_USING_PHY)
  636. #include <finsh.h>
  637. void phy_read(rt_uint32_t phy_reg)
  638. {
  639. rt_uint32_t data;
  640. rt_phy_status status = phy_dev->ops->read(phy_reg, &data);
  641. if (PHY_STATUS_OK == status)
  642. {
  643. rt_kprintf("PHY_Read: %02X --> %08X", phy_reg, data);
  644. }
  645. else
  646. {
  647. rt_kprintf("PHY_Read: %02X --> faild", phy_reg);
  648. }
  649. }
  650. void phy_write(rt_uint32_t phy_reg, rt_uint32_t data)
  651. {
  652. rt_phy_status status = phy_dev->ops->write(phy_reg, data);
  653. if (PHY_STATUS_OK == status)
  654. {
  655. rt_kprintf("PHY_Write: %02X --> %08X\n", phy_reg, data);
  656. }
  657. else
  658. {
  659. rt_kprintf("PHY_Write: %02X --> faild\n", phy_reg);
  660. }
  661. }
  662. void phy_dump(void)
  663. {
  664. rt_uint32_t data;
  665. rt_phy_status status;
  666. int i;
  667. for (i = 0; i < 32; i++)
  668. {
  669. status = phy_dev->ops->read(i, &data);
  670. if (PHY_STATUS_OK != status)
  671. {
  672. rt_kprintf("phy_dump: %02X --> faild", i);
  673. break;
  674. }
  675. if (i % 8 == 7)
  676. {
  677. rt_kprintf("%02X --> %08X ", i, data);
  678. }
  679. else
  680. {
  681. rt_kprintf("%02X --> %08X\n", i, data);
  682. }
  683. }
  684. }
  685. #endif
  686. #if defined(RT_USING_FINSH) && defined(RT_USING_LWIP)
  687. void enet_reg_dump(void)
  688. {
  689. ENET_Type *enet_base = imxrt_eth_device.enet_base;
  690. #define DUMP_REG(__REG) \
  691. rt_kprintf("%s(%08X): %08X\n", #__REG, (uint32_t)&enet_base->__REG, enet_base->__REG)
  692. DUMP_REG(EIR);
  693. DUMP_REG(EIMR);
  694. DUMP_REG(RDAR);
  695. DUMP_REG(TDAR);
  696. DUMP_REG(ECR);
  697. DUMP_REG(MMFR);
  698. DUMP_REG(MSCR);
  699. DUMP_REG(MIBC);
  700. DUMP_REG(RCR);
  701. DUMP_REG(TCR);
  702. DUMP_REG(PALR);
  703. DUMP_REG(PAUR);
  704. DUMP_REG(OPD);
  705. DUMP_REG(TXIC);
  706. DUMP_REG(RXIC);
  707. DUMP_REG(IAUR);
  708. DUMP_REG(IALR);
  709. DUMP_REG(GAUR);
  710. DUMP_REG(GALR);
  711. DUMP_REG(TFWR);
  712. DUMP_REG(RDSR);
  713. DUMP_REG(TDSR);
  714. DUMP_REG(MRBR);
  715. DUMP_REG(RSFL);
  716. DUMP_REG(RSEM);
  717. DUMP_REG(RAEM);
  718. DUMP_REG(RAFL);
  719. DUMP_REG(TSEM);
  720. DUMP_REG(TAEM);
  721. DUMP_REG(TAFL);
  722. DUMP_REG(TIPG);
  723. DUMP_REG(FTRL);
  724. DUMP_REG(TACC);
  725. DUMP_REG(RACC);
  726. // DUMP_REG(RMON_T_DROP);
  727. DUMP_REG(RMON_T_PACKETS);
  728. DUMP_REG(RMON_T_BC_PKT);
  729. DUMP_REG(RMON_T_MC_PKT);
  730. DUMP_REG(RMON_T_CRC_ALIGN);
  731. DUMP_REG(RMON_T_UNDERSIZE);
  732. DUMP_REG(RMON_T_OVERSIZE);
  733. DUMP_REG(RMON_T_FRAG);
  734. DUMP_REG(RMON_T_JAB);
  735. DUMP_REG(RMON_T_COL);
  736. DUMP_REG(RMON_T_P64);
  737. DUMP_REG(RMON_T_P65TO127);
  738. DUMP_REG(RMON_T_P128TO255);
  739. DUMP_REG(RMON_T_P256TO511);
  740. DUMP_REG(RMON_T_P512TO1023);
  741. DUMP_REG(RMON_T_P1024TO2047);
  742. DUMP_REG(RMON_T_P_GTE2048);
  743. DUMP_REG(RMON_T_OCTETS);
  744. DUMP_REG(IEEE_T_DROP);
  745. DUMP_REG(IEEE_T_FRAME_OK);
  746. DUMP_REG(IEEE_T_1COL);
  747. DUMP_REG(IEEE_T_MCOL);
  748. DUMP_REG(IEEE_T_DEF);
  749. DUMP_REG(IEEE_T_LCOL);
  750. DUMP_REG(IEEE_T_EXCOL);
  751. DUMP_REG(IEEE_T_MACERR);
  752. DUMP_REG(IEEE_T_CSERR);
  753. DUMP_REG(IEEE_T_SQE);
  754. DUMP_REG(IEEE_T_FDXFC);
  755. DUMP_REG(IEEE_T_OCTETS_OK);
  756. DUMP_REG(RMON_R_PACKETS);
  757. DUMP_REG(RMON_R_BC_PKT);
  758. DUMP_REG(RMON_R_MC_PKT);
  759. DUMP_REG(RMON_R_CRC_ALIGN);
  760. DUMP_REG(RMON_R_UNDERSIZE);
  761. DUMP_REG(RMON_R_OVERSIZE);
  762. DUMP_REG(RMON_R_FRAG);
  763. DUMP_REG(RMON_R_JAB);
  764. // DUMP_REG(RMON_R_RESVD_0);
  765. DUMP_REG(RMON_R_P64);
  766. DUMP_REG(RMON_R_P65TO127);
  767. DUMP_REG(RMON_R_P128TO255);
  768. DUMP_REG(RMON_R_P256TO511);
  769. DUMP_REG(RMON_R_P512TO1023);
  770. DUMP_REG(RMON_R_P1024TO2047);
  771. DUMP_REG(RMON_R_P_GTE2048);
  772. DUMP_REG(RMON_R_OCTETS);
  773. DUMP_REG(IEEE_R_DROP);
  774. DUMP_REG(IEEE_R_FRAME_OK);
  775. DUMP_REG(IEEE_R_CRC);
  776. DUMP_REG(IEEE_R_ALIGN);
  777. DUMP_REG(IEEE_R_MACERR);
  778. DUMP_REG(IEEE_R_FDXFC);
  779. DUMP_REG(IEEE_R_OCTETS_OK);
  780. DUMP_REG(ATCR);
  781. DUMP_REG(ATVR);
  782. DUMP_REG(ATOFF);
  783. DUMP_REG(ATPER);
  784. DUMP_REG(ATCOR);
  785. DUMP_REG(ATINC);
  786. DUMP_REG(ATSTMP);
  787. DUMP_REG(TGSR);
  788. }
  789. void enet_nvic_tog(void)
  790. {
  791. NVIC_SetPendingIRQ(ENET_IRQn);
  792. }
  793. void enet_rx_stat(void)
  794. {
  795. enet_data_error_stats_t *error_statistic = &imxrt_eth_device.error_statistic;
  796. #define DUMP_STAT(__VAR) \
  797. rt_kprintf("%-25s: %08X\n", #__VAR, error_statistic->__VAR);
  798. DUMP_STAT(statsRxLenGreaterErr);
  799. DUMP_STAT(statsRxAlignErr);
  800. DUMP_STAT(statsRxFcsErr);
  801. DUMP_STAT(statsRxOverRunErr);
  802. DUMP_STAT(statsRxTruncateErr);
  803. #ifdef ENET_ENHANCEDBUFFERDESCRIPTOR_MODE
  804. DUMP_STAT(statsRxProtocolChecksumErr);
  805. DUMP_STAT(statsRxIpHeadChecksumErr);
  806. DUMP_STAT(statsRxMacErr);
  807. DUMP_STAT(statsRxPhyErr);
  808. DUMP_STAT(statsRxCollisionErr);
  809. DUMP_STAT(statsTxErr);
  810. DUMP_STAT(statsTxFrameErr);
  811. DUMP_STAT(statsTxOverFlowErr);
  812. DUMP_STAT(statsTxLateCollisionErr);
  813. DUMP_STAT(statsTxExcessCollisionErr);
  814. DUMP_STAT(statsTxUnderFlowErr);
  815. DUMP_STAT(statsTxTsErr);
  816. #endif
  817. }
  818. void enet_buf_info(void)
  819. {
  820. int i = 0;
  821. for (i = 0; i < ENET_RXBD_NUM; i++)
  822. {
  823. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  824. i,
  825. g_rxBuffDescrip[i].length,
  826. g_rxBuffDescrip[i].control,
  827. g_rxBuffDescrip[i].buffer);
  828. }
  829. for (i = 0; i < ENET_TXBD_NUM; i++)
  830. {
  831. rt_kprintf("%d: length: %-8d, control: %04X, buffer:%p\n",
  832. i,
  833. g_txBuffDescrip[i].length,
  834. g_txBuffDescrip[i].control,
  835. g_txBuffDescrip[i].buffer);
  836. }
  837. }
  838. FINSH_FUNCTION_EXPORT(phy_read, read phy register);
  839. FINSH_FUNCTION_EXPORT(phy_write, write phy register);
  840. FINSH_FUNCTION_EXPORT(phy_dump, dump phy registers);
  841. FINSH_FUNCTION_EXPORT(enet_reg_dump, dump enet registers);
  842. FINSH_FUNCTION_EXPORT(enet_nvic_tog, toggle enet nvic pendding bit);
  843. FINSH_FUNCTION_EXPORT(enet_rx_stat, dump enet rx statistic);
  844. FINSH_FUNCTION_EXPORT(enet_buf_info, dump enet tx and tx buffer descripter);
  845. #endif