drv_emac.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-08-08 Yang the first version
  9. */
  10. #include <rtthread.h>
  11. #include "lwipopts.h"
  12. #include <netif/ethernetif.h>
  13. #include <board.h>
  14. #include "drv_emac.h"
  15. #include "fsl_iocon.h"
  16. #include "fsl_sctimer.h"
  17. #include "fsl_phy.h"
  18. #define DEBUG
  19. //#define ETH_RX_DUMP
  20. //#define ETH_TX_DUMP
  21. #define ETH_STATISTICS
  22. #ifdef DEBUG
  23. #define ETH_PRINTF rt_kprintf
  24. #else
  25. #define ETH_PRINTF(...)
  26. #endif
  27. #define IOCON_PIO_DIGITAL_EN 0x0100u /*!< Enables digital function */
  28. #define IOCON_PIO_FUNC0 0x00u /*!< Selects pin function 0 */
  29. #define IOCON_PIO_FUNC1 0x01u /*!< Selects pin function 1 */
  30. #define IOCON_PIO_FUNC7 0x07u /*!< Selects pin function 7 */
  31. #define IOCON_PIO_INPFILT_OFF 0x0200u /*!< Input filter disabled */
  32. #define IOCON_PIO_INV_DI 0x00u /*!< Input function is not inverted */
  33. #define IOCON_PIO_MODE_INACT 0x00u /*!< No addition pin function */
  34. #define IOCON_PIO_MODE_PULLUP 0x20u /*!< Selects pull-up function */
  35. #define IOCON_PIO_OPENDRAIN_DI 0x00u /*!< Open drain is disabled */
  36. #define IOCON_PIO_SLEW_FAST 0x0400u /*!< Fast mode, slew rate control is disabled */
  37. #define IOCON_PIO_SLEW_STANDARD 0x00u /*!< Standard mode, output slew rate control is enabled */
  38. #define PIN8_IDX 8u /*!< Pin number for pin 8 in a port 4 */
  39. #define PIN10_IDX 10u /*!< Pin number for pin 10 in a port 4 */
  40. #define PIN11_IDX 11u /*!< Pin number for pin 11 in a port 4 */
  41. #define PIN12_IDX 12u /*!< Pin number for pin 12 in a port 4 */
  42. #define PIN13_IDX 13u /*!< Pin number for pin 13 in a port 4 */
  43. #define PIN14_IDX 14u /*!< Pin number for pin 14 in a port 4 */
  44. #define PIN15_IDX 15u /*!< Pin number for pin 15 in a port 4 */
  45. #define PIN16_IDX 16u /*!< Pin number for pin 16 in a port 4 */
  46. #define PIN17_IDX 17u /*!< Pin number for pin 17 in a port 0 */
  47. #define PIN26_IDX 26u /*!< Pin number for pin 26 in a port 2 */
  48. #define PIN29_IDX 29u /*!< Pin number for pin 29 in a port 0 */
  49. #define PIN30_IDX 30u /*!< Pin number for pin 30 in a port 0 */
  50. #define PORT0_IDX 0u /*!< Port index */
  51. #define PORT2_IDX 2u /*!< Port index */
  52. #define PORT4_IDX 4u /*!< Port index */
  53. #define MAX_ADDR_LEN 6u
  54. #define ENET_RXBD_NUM 4u
  55. #define ENET_TXBD_NUM 4u
  56. #define ENET_ALIGN(x) \
  57. ((unsigned int)((x) + ((ENET_BUFF_ALIGNMENT)-1)) & (unsigned int)(~(unsigned int)((ENET_BUFF_ALIGNMENT)-1)))
  58. #define ENET_RXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  59. #define ENET_TXBUFF_SIZE (ENET_FRAME_MAX_FRAMELEN)
  60. struct lpc_emac
  61. {
  62. /* inherit from ethernet device */
  63. struct eth_device parent;
  64. struct rt_semaphore tx_wait;
  65. ENET_Type *base;
  66. enet_handle_t handle;
  67. /* interface address info. */
  68. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  69. uint32_t phyAddr;
  70. uint8_t RxBuffDescrip[ENET_RXBD_NUM * sizeof(enet_rx_bd_struct_t) + ENET_BUFF_ALIGNMENT];
  71. uint8_t TxBuffDescrip[ENET_TXBD_NUM * sizeof(enet_tx_bd_struct_t) + ENET_BUFF_ALIGNMENT];
  72. uint8_t RxDataBuff[ENET_RXBD_NUM * ENET_ALIGN(ENET_RXBUFF_SIZE) + ENET_BUFF_ALIGNMENT];
  73. uint8_t TxDataBuff[ENET_TXBD_NUM * ENET_ALIGN(ENET_TXBUFF_SIZE) + ENET_BUFF_ALIGNMENT];
  74. uint8_t txIdx;
  75. };
  76. static struct lpc_emac lpc_emac_device;
  77. #ifdef ETH_STATISTICS
  78. static uint32_t isr_rx_counter = 0;
  79. static uint32_t isr_tx_counter = 0;
  80. #endif
  81. static inline enet_rx_bd_struct_t *get_rx_desc(uint32_t index)
  82. {
  83. return (enet_rx_bd_struct_t *)ENET_ALIGN(&lpc_emac_device.RxBuffDescrip[index * sizeof(enet_rx_bd_struct_t)]);
  84. }
  85. static inline enet_tx_bd_struct_t *get_tx_desc(uint32_t index)
  86. {
  87. return (enet_tx_bd_struct_t *)ENET_ALIGN(&lpc_emac_device.TxBuffDescrip[index * sizeof(enet_tx_bd_struct_t)]);
  88. }
  89. #if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
  90. static void packet_dump(const char * msg, const struct pbuf* p)
  91. {
  92. const struct pbuf* q;
  93. rt_uint32_t i,j;
  94. rt_uint8_t *ptr;
  95. rt_kprintf("%s %d byte\n", msg, p->tot_len);
  96. i=0;
  97. for(q=p; q != RT_NULL; q= q->next)
  98. {
  99. ptr = q->payload;
  100. for(j=0; j<q->len; j++)
  101. {
  102. if( (i%8) == 0 )
  103. {
  104. rt_kprintf(" ");
  105. }
  106. if( (i%16) == 0 )
  107. {
  108. rt_kprintf("\r\n");
  109. }
  110. rt_kprintf("%02x ",*ptr);
  111. i++;
  112. ptr++;
  113. }
  114. }
  115. rt_kprintf("\n\n");
  116. }
  117. #else
  118. #define packet_dump(...)
  119. #endif /* dump */
  120. static void ethernet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, uint8_t channel, void *param)
  121. {
  122. switch (event)
  123. {
  124. case kENET_RxIntEvent:
  125. #ifdef ETH_STATISTICS
  126. isr_rx_counter++;
  127. #endif
  128. /* a frame has been received */
  129. eth_device_ready(&(lpc_emac_device.parent));
  130. break;
  131. case kENET_TxIntEvent:
  132. #ifdef ETH_STATISTICS
  133. isr_tx_counter++;
  134. #endif
  135. /* set event */
  136. rt_sem_release(&lpc_emac_device.tx_wait);
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. static void lcp_emac_io_init(void)
  143. {
  144. const uint32_t port0_pin17_config = (
  145. IOCON_PIO_FUNC7 | /* Pin is configured as ENET_TXD1 */
  146. IOCON_PIO_MODE_INACT | /* No addition pin function */
  147. IOCON_PIO_INV_DI | /* Input function is not inverted */
  148. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  149. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  150. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  151. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  152. );
  153. IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN17_IDX, port0_pin17_config); /* PORT0 PIN17 (coords: E14) is configured as ENET_TXD1 */
  154. const uint32_t port2_pin26_config = (
  155. IOCON_PIO_FUNC0 | /* Pin is configured as PIO2_26 */
  156. IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */
  157. IOCON_PIO_INV_DI | /* Input function is not inverted */
  158. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  159. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  160. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  161. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  162. );
  163. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN26_IDX, port2_pin26_config); /* PORT2 PIN26 (coords: H11) is configured as PIO2_26 */
  164. const uint32_t port4_pin10_config = (
  165. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_RX_DV */
  166. IOCON_PIO_MODE_INACT | /* No addition pin function */
  167. IOCON_PIO_INV_DI | /* Input function is not inverted */
  168. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  169. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  170. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  171. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  172. );
  173. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN10_IDX, port4_pin10_config); /* PORT4 PIN10 (coords: B9) is configured as ENET_RX_DV */
  174. const uint32_t port4_pin11_config = (
  175. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_RXD0 */
  176. IOCON_PIO_MODE_INACT | /* No addition pin function */
  177. IOCON_PIO_INV_DI | /* Input function is not inverted */
  178. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  179. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  180. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  181. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  182. );
  183. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN11_IDX, port4_pin11_config); /* PORT4 PIN11 (coords: A9) is configured as ENET_RXD0 */
  184. const uint32_t port4_pin12_config = (
  185. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_RXD1 */
  186. IOCON_PIO_MODE_INACT | /* No addition pin function */
  187. IOCON_PIO_INV_DI | /* Input function is not inverted */
  188. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  189. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  190. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  191. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  192. );
  193. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN12_IDX, port4_pin12_config); /* PORT4 PIN12 (coords: A6) is configured as ENET_RXD1 */
  194. const uint32_t port4_pin13_config = (
  195. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_TX_EN */
  196. IOCON_PIO_MODE_INACT | /* No addition pin function */
  197. IOCON_PIO_INV_DI | /* Input function is not inverted */
  198. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  199. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  200. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  201. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  202. );
  203. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN13_IDX, port4_pin13_config); /* PORT4 PIN13 (coords: B6) is configured as ENET_TX_EN */
  204. const uint32_t port4_pin14_config = (
  205. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_RX_CLK */
  206. IOCON_PIO_MODE_INACT | /* No addition pin function */
  207. IOCON_PIO_INV_DI | /* Input function is not inverted */
  208. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  209. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  210. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  211. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  212. );
  213. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN14_IDX, port4_pin14_config); /* PORT4 PIN14 (coords: B5) is configured as ENET_RX_CLK */
  214. const uint32_t port4_pin15_config = (
  215. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_MDC */
  216. IOCON_PIO_MODE_INACT | /* No addition pin function */
  217. IOCON_PIO_INV_DI | /* Input function is not inverted */
  218. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  219. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  220. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  221. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  222. );
  223. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN15_IDX, port4_pin15_config); /* PORT4 PIN15 (coords: A4) is configured as ENET_MDC */
  224. const uint32_t port4_pin16_config = (
  225. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_MDIO */
  226. IOCON_PIO_MODE_INACT | /* No addition pin function */
  227. IOCON_PIO_INV_DI | /* Input function is not inverted */
  228. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  229. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  230. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  231. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  232. );
  233. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN16_IDX, port4_pin16_config); /* PORT4 PIN16 (coords: C4) is configured as ENET_MDIO */
  234. const uint32_t port4_pin8_config = (
  235. IOCON_PIO_FUNC1 | /* Pin is configured as ENET_TXD0 */
  236. IOCON_PIO_MODE_INACT | /* No addition pin function */
  237. IOCON_PIO_INV_DI | /* Input function is not inverted */
  238. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  239. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  240. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  241. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  242. );
  243. IOCON_PinMuxSet(IOCON, PORT4_IDX, PIN8_IDX, port4_pin8_config); /* PORT4 PIN8 (coords: B14) is configured as ENET_TXD0 */
  244. }
  245. static rt_err_t lpc_emac_phy_init(phy_speed_t * speed, phy_duplex_t * duplex)
  246. {
  247. bool link = false;
  248. int32_t status;
  249. RT_ASSERT(speed != NULL);
  250. RT_ASSERT(duplex != NULL);
  251. status = PHY_Init(lpc_emac_device.base, lpc_emac_device.phyAddr, 0);
  252. if (status != kStatus_Success)
  253. {
  254. /* Half duplex. */
  255. *duplex = kPHY_HalfDuplex;
  256. /* 10M speed. */
  257. *speed = kPHY_Speed10M;
  258. eth_device_linkchange(&lpc_emac_device.parent, RT_FALSE);
  259. ETH_PRINTF("PHY_Init failed!\n");
  260. return -RT_ERROR;
  261. }
  262. /* Wait for link up and get the actual PHY link speed. */
  263. PHY_GetLinkStatus(lpc_emac_device.base, lpc_emac_device.phyAddr, &link);
  264. while (!link)
  265. {
  266. uint32_t timedelay;
  267. ETH_PRINTF("PHY Wait for link up!\n");
  268. for (timedelay = 0; timedelay < 0xFFFFFU; timedelay++)
  269. {
  270. __ASM("nop");
  271. }
  272. PHY_GetLinkStatus(lpc_emac_device.base, lpc_emac_device.phyAddr, &link);
  273. }
  274. PHY_GetLinkSpeedDuplex(lpc_emac_device.base, lpc_emac_device.phyAddr, speed, duplex);
  275. eth_device_linkchange(&lpc_emac_device.parent, RT_TRUE);
  276. return RT_EOK;
  277. }
  278. static rt_err_t lpc_emac_init(rt_device_t dev)
  279. {
  280. int i;
  281. phy_speed_t speed;
  282. phy_duplex_t duplex;
  283. enet_config_t config;
  284. enet_buffer_config_t buffCfg;
  285. uint32_t rxBufferStartAddr[ENET_RXBD_NUM];
  286. lcp_emac_io_init();
  287. lpc_emac_phy_init(&speed, &duplex);
  288. /* calculate start addresses of all rx buffers */
  289. for (i = 0; i < ENET_RXBD_NUM; i++)
  290. {
  291. rxBufferStartAddr[i] = ENET_ALIGN(&lpc_emac_device.RxDataBuff[i * ENET_ALIGN(ENET_RXBUFF_SIZE)]);
  292. }
  293. buffCfg.rxRingLen = ENET_RXBD_NUM;
  294. buffCfg.txRingLen = ENET_TXBD_NUM;
  295. buffCfg.txDescStartAddrAlign = get_tx_desc(0U);
  296. buffCfg.txDescTailAddrAlign = get_tx_desc(0U);
  297. buffCfg.rxDescStartAddrAlign = get_rx_desc(0U);
  298. buffCfg.rxDescTailAddrAlign = get_rx_desc(ENET_RXBD_NUM);
  299. buffCfg.rxBufferStartAddr = rxBufferStartAddr;
  300. buffCfg.rxBuffSizeAlign = ENET_ALIGN(ENET_RXBUFF_SIZE);
  301. /* Get default configuration 100M RMII. */
  302. ENET_GetDefaultConfig(&config);
  303. /* Use the actual speed and duplex when phy success to finish the autonegotiation. */
  304. config.miiSpeed = (enet_mii_speed_t)speed;
  305. config.miiDuplex = (enet_mii_duplex_t)duplex;
  306. ETH_PRINTF("Auto negotiation, Speed: ");
  307. if (config.miiSpeed == kENET_MiiSpeed100M)
  308. ETH_PRINTF("100M");
  309. else
  310. ETH_PRINTF("10M");
  311. ETH_PRINTF(", Duplex: ");
  312. if (config.miiSpeed == kENET_MiiSpeed100M)
  313. ETH_PRINTF("Full\n");
  314. else
  315. ETH_PRINTF("Half\n");
  316. /* Initialize lpc_emac_device.base. */
  317. ENET_Init(lpc_emac_device.base, &config, &lpc_emac_device.dev_addr[0], CLOCK_GetFreq(kCLOCK_CoreSysClk));
  318. /* Enable the tx/rx interrupt. */
  319. ENET_EnableInterrupts(lpc_emac_device.base, (kENET_DmaTx | kENET_DmaRx));
  320. ENET_CreateHandler(lpc_emac_device.base, &lpc_emac_device.handle, &config, &buffCfg, ethernet_callback, NULL);
  321. /* Initialize Descriptor. */
  322. ENET_DescriptorInit(lpc_emac_device.base, &config, &buffCfg);
  323. /* Active TX/RX. */
  324. ENET_StartRxTx(lpc_emac_device.base, 1, 1);
  325. return RT_EOK;
  326. }
  327. static rt_err_t lpc_emac_open(rt_device_t dev, rt_uint16_t oflag)
  328. {
  329. return RT_EOK;
  330. }
  331. static rt_err_t lpc_emac_close(rt_device_t dev)
  332. {
  333. return RT_EOK;
  334. }
  335. static rt_ssize_t lpc_emac_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  336. {
  337. rt_set_errno(-RT_ENOSYS);
  338. return 0;
  339. }
  340. static rt_ssize_t lpc_emac_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  341. {
  342. rt_set_errno(-RT_ENOSYS);
  343. return 0;
  344. }
  345. static rt_err_t lpc_emac_control(rt_device_t dev, int cmd, void *args)
  346. {
  347. switch (cmd)
  348. {
  349. case NIOCTL_GADDR:
  350. /* get mac address */
  351. if (args) rt_memcpy(args, lpc_emac_device.dev_addr, 6);
  352. else return -RT_ERROR;
  353. break;
  354. default :
  355. break;
  356. }
  357. return RT_EOK;
  358. }
  359. /* EtherNet Device Interface */
  360. /* transmit packet. */
  361. rt_err_t lpc_emac_tx(rt_device_t dev, struct pbuf *p)
  362. {
  363. rt_err_t result = RT_EOK;
  364. enet_handle_t * enet_handle = &lpc_emac_device.handle;
  365. ENET_Type *enet_base = lpc_emac_device.base;
  366. uint8_t * data;
  367. uint16_t len;
  368. RT_ASSERT(p != NULL);
  369. RT_ASSERT(enet_handle != RT_NULL);
  370. if (p->tot_len > ENET_TXBUFF_SIZE)
  371. {
  372. return -RT_ERROR;
  373. }
  374. packet_dump("TX dump", p);
  375. /* get free tx buffer */
  376. {
  377. rt_err_t result;
  378. result = rt_sem_take(&lpc_emac_device.tx_wait, RT_TICK_PER_SECOND/10);
  379. if (result != RT_EOK)
  380. {
  381. return -RT_ERROR;
  382. }
  383. }
  384. // fix RxDataBuff -> TxDataBuff, ENET_RXBUFF_SIZE -> ENET_TXBUFF_SIZE
  385. data = (uint8_t *)ENET_ALIGN(&lpc_emac_device.TxDataBuff[lpc_emac_device.txIdx * ENET_ALIGN(ENET_TXBUFF_SIZE)]);
  386. len = pbuf_copy_partial(p, data, p->tot_len, 0);
  387. lpc_emac_device.txIdx = (lpc_emac_device.txIdx + 1) / ENET_TXBD_NUM;
  388. // fix 'p->len' to 'len', avoid send wrong partial packet.
  389. result = ENET_SendFrame(enet_base, enet_handle, data, len);
  390. if ((result == kStatus_ENET_TxFrameFail) || (result == kStatus_ENET_TxFrameOverLen) || (result == kStatus_ENET_TxFrameBusy))
  391. {
  392. return -RT_ERROR;
  393. }
  394. return RT_EOK;
  395. }
  396. /* reception packet. */
  397. struct pbuf *lpc_emac_rx(rt_device_t dev)
  398. {
  399. uint32_t length = 0;
  400. status_t status;
  401. struct pbuf* p = RT_NULL;
  402. enet_handle_t * enet_handle = &lpc_emac_device.handle;
  403. ENET_Type *enet_base = lpc_emac_device.base;
  404. /* Get the Frame size */
  405. status = ENET_GetRxFrameSize(enet_base, enet_handle, &length, 0);
  406. /* Call ENET_ReadFrame when there is a received frame. */
  407. if (length != 0)
  408. {
  409. /* Received valid frame. Deliver the rx buffer with the size equal to length. */
  410. p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
  411. if (p != NULL)
  412. {
  413. status = ENET_ReadFrame(enet_base, enet_handle, p->payload, length, 0);
  414. if (status == kStatus_Success)
  415. {
  416. packet_dump("RX dump", p);
  417. return p;
  418. }
  419. else
  420. {
  421. ETH_PRINTF(" A frame read failed\n");
  422. pbuf_free(p);
  423. }
  424. }
  425. else
  426. {
  427. ETH_PRINTF(" pbuf_alloc faild\n");
  428. }
  429. }
  430. else if (status == kStatus_ENET_RxFrameError)
  431. {
  432. ETH_PRINTF("ENET_GetRxFrameSize: kStatus_ENET_RxFrameError\n");
  433. ENET_ReadFrame(enet_base, enet_handle, NULL, 0, 0);
  434. }
  435. return NULL;
  436. }
  437. int lpc_emac_hw_init(void)
  438. {
  439. /* init tx semaphore */
  440. rt_sem_init(&lpc_emac_device.tx_wait, "tx_wait", ENET_TXBD_NUM, RT_IPC_FLAG_FIFO);
  441. lpc_emac_device.phyAddr = 0;
  442. lpc_emac_device.txIdx = 0;
  443. lpc_emac_device.base = ENET;
  444. // OUI 00-60-37 NXP Semiconductors
  445. lpc_emac_device.dev_addr[0] = 0x00;
  446. lpc_emac_device.dev_addr[1] = 0x60;
  447. lpc_emac_device.dev_addr[2] = 0x37;
  448. /* set mac address: (only for test) */
  449. lpc_emac_device.dev_addr[3] = 0x12;
  450. lpc_emac_device.dev_addr[4] = 0x34;
  451. lpc_emac_device.dev_addr[5] = 0x56;
  452. lpc_emac_device.parent.parent.init = lpc_emac_init;
  453. lpc_emac_device.parent.parent.open = lpc_emac_open;
  454. lpc_emac_device.parent.parent.close = lpc_emac_close;
  455. lpc_emac_device.parent.parent.read = lpc_emac_read;
  456. lpc_emac_device.parent.parent.write = lpc_emac_write;
  457. lpc_emac_device.parent.parent.control = lpc_emac_control;
  458. lpc_emac_device.parent.parent.user_data = RT_NULL;
  459. lpc_emac_device.parent.eth_rx = lpc_emac_rx;
  460. lpc_emac_device.parent.eth_tx = lpc_emac_tx;
  461. eth_device_init(&(lpc_emac_device.parent), "e0");
  462. return 0;
  463. }
  464. INIT_DEVICE_EXPORT(lpc_emac_hw_init);
  465. #ifdef ETH_STATISTICS
  466. int emac_stat(void)
  467. {
  468. rt_kprintf("enter rx isr coutner : %d\n", isr_rx_counter);
  469. rt_kprintf("enter tx isr coutner : %d\n", isr_tx_counter);
  470. return 0;
  471. }
  472. #endif
  473. void phy_dump(void)
  474. {
  475. status_t PHY_Read(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr);
  476. int i;
  477. for (i = 0; i < 31; i++)
  478. {
  479. status_t result = kStatus_Success;
  480. uint32_t reg;
  481. result = PHY_Read(lpc_emac_device.base, lpc_emac_device.phyAddr, i, &reg);
  482. if (result == kStatus_Success)
  483. {
  484. rt_kprintf("%02d: %08d\n", i, reg);
  485. }
  486. else
  487. {
  488. rt_kprintf("read register %d faild\n", i);
  489. }
  490. }
  491. }
  492. void emac_dump(void)
  493. {
  494. #define DUMP_REG(__NAME) \
  495. rt_kprintf("%-40s, %08x: %08x\n", #__NAME, (uint32_t)&(lpc_emac_device.base->__NAME), lpc_emac_device.base->__NAME)
  496. DUMP_REG(MAC_CONFIG);
  497. DUMP_REG(MAC_EXT_CONFIG);
  498. DUMP_REG(MAC_FRAME_FILTER);
  499. DUMP_REG(MAC_WD_TIMEROUT);
  500. DUMP_REG(MAC_VLAN_TAG);
  501. DUMP_REG(MAC_TX_FLOW_CTRL_Q[0]);
  502. DUMP_REG(MAC_TX_FLOW_CTRL_Q[1]);
  503. DUMP_REG(MAC_RX_FLOW_CTRL);
  504. DUMP_REG(MAC_TXQ_PRIO_MAP);
  505. DUMP_REG(MAC_RXQ_CTRL[0]);
  506. DUMP_REG(MAC_RXQ_CTRL[1]);
  507. DUMP_REG(MAC_RXQ_CTRL[2]);
  508. DUMP_REG(MAC_INTR_STAT);
  509. DUMP_REG(MAC_INTR_EN);
  510. DUMP_REG(MAC_RXTX_STAT);
  511. DUMP_REG(MAC_PMT_CRTL_STAT);
  512. DUMP_REG(MAC_RWAKE_FRFLT);
  513. DUMP_REG(MAC_LPI_CTRL_STAT);
  514. DUMP_REG(MAC_LPI_TIMER_CTRL);
  515. DUMP_REG(MAC_LPI_ENTR_TIMR);
  516. DUMP_REG(MAC_1US_TIC_COUNTR);
  517. DUMP_REG(MAC_VERSION);
  518. DUMP_REG(MAC_DBG);
  519. DUMP_REG(MAC_HW_FEAT[0]);
  520. DUMP_REG(MAC_HW_FEAT[1]);
  521. DUMP_REG(MAC_HW_FEAT[2]);
  522. DUMP_REG(MAC_MDIO_ADDR);
  523. DUMP_REG(MAC_MDIO_DATA);
  524. DUMP_REG(MAC_ADDR_HIGH);
  525. DUMP_REG(MAC_ADDR_LOW);
  526. DUMP_REG(MAC_TIMESTAMP_CTRL);
  527. DUMP_REG(MAC_SUB_SCND_INCR);
  528. DUMP_REG(MAC_SYS_TIME_SCND);
  529. DUMP_REG(MAC_SYS_TIME_NSCND);
  530. DUMP_REG(MAC_SYS_TIME_SCND_UPD);
  531. DUMP_REG(MAC_SYS_TIME_NSCND_UPD);
  532. DUMP_REG(MAC_SYS_TIMESTMP_ADDEND);
  533. DUMP_REG(MAC_SYS_TIME_HWORD_SCND);
  534. DUMP_REG(MAC_SYS_TIMESTMP_STAT);
  535. DUMP_REG(MAC_TX_TIMESTAMP_STATUS_NANOSECONDS);
  536. DUMP_REG(MAC_TX_TIMESTAMP_STATUS_SECONDS);
  537. DUMP_REG(MAC_TIMESTAMP_INGRESS_CORR_NANOSECOND);
  538. DUMP_REG(MAC_TIMESTAMP_EGRESS_CORR_NANOSECOND);
  539. DUMP_REG(MTL_OP_MODE);
  540. DUMP_REG(MTL_INTR_STAT);
  541. DUMP_REG(MTL_RXQ_DMA_MAP);
  542. DUMP_REG(DMA_MODE);
  543. DUMP_REG(DMA_SYSBUS_MODE);
  544. DUMP_REG(DMA_INTR_STAT);
  545. DUMP_REG(DMA_DBG_STAT);
  546. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_OP_MODE);
  547. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_UNDRFLW);
  548. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_DBG);
  549. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_ETS_CTRL);
  550. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_ETS_STAT);
  551. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_QNTM_WGHT);
  552. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_SNDSLP_CRDT);
  553. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_HI_CRDT);
  554. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_LO_CRDT);
  555. DUMP_REG(MTL_QUEUE[0].MTL_TXQX_INTCTRL_STAT);
  556. DUMP_REG(MTL_QUEUE[0].MTL_RXQX_OP_MODE);
  557. DUMP_REG(MTL_QUEUE[0].MTL_RXQX_MISSPKT_OVRFLW_CNT);
  558. DUMP_REG(MTL_QUEUE[0].MTL_RXQX_DBG);
  559. DUMP_REG(MTL_QUEUE[0].MTL_RXQX_CTRL);
  560. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_OP_MODE);
  561. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_UNDRFLW);
  562. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_DBG);
  563. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_ETS_CTRL);
  564. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_ETS_STAT);
  565. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_QNTM_WGHT);
  566. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_SNDSLP_CRDT);
  567. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_HI_CRDT);
  568. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_LO_CRDT);
  569. DUMP_REG(MTL_QUEUE[1].MTL_TXQX_INTCTRL_STAT);
  570. DUMP_REG(MTL_QUEUE[1].MTL_RXQX_OP_MODE);
  571. DUMP_REG(MTL_QUEUE[1].MTL_RXQX_MISSPKT_OVRFLW_CNT);
  572. DUMP_REG(MTL_QUEUE[1].MTL_RXQX_DBG);
  573. DUMP_REG(MTL_QUEUE[1].MTL_RXQX_CTRL);
  574. DUMP_REG(DMA_CH[0].DMA_CHX_CTRL);
  575. DUMP_REG(DMA_CH[0].DMA_CHX_TX_CTRL);
  576. DUMP_REG(DMA_CH[0].DMA_CHX_RX_CTRL);
  577. DUMP_REG(DMA_CH[0].DMA_CHX_TXDESC_LIST_ADDR);
  578. DUMP_REG(DMA_CH[0].DMA_CHX_RXDESC_LIST_ADDR);
  579. DUMP_REG(DMA_CH[0].DMA_CHX_TXDESC_TAIL_PTR);
  580. DUMP_REG(DMA_CH[0].DMA_CHX_RXDESC_TAIL_PTR);
  581. DUMP_REG(DMA_CH[0].DMA_CHX_TXDESC_RING_LENGTH);
  582. DUMP_REG(DMA_CH[0].DMA_CHX_RXDESC_RING_LENGTH);
  583. DUMP_REG(DMA_CH[0].DMA_CHX_INT_EN);
  584. DUMP_REG(DMA_CH[0].DMA_CHX_RX_INT_WDTIMER);
  585. DUMP_REG(DMA_CH[0].DMA_CHX_SLOT_FUNC_CTRL_STAT);
  586. DUMP_REG(DMA_CH[0].DMA_CHX_CUR_HST_TXDESC);
  587. DUMP_REG(DMA_CH[0].DMA_CHX_CUR_HST_RXDESC);
  588. DUMP_REG(DMA_CH[0].DMA_CHX_CUR_HST_TXBUF);
  589. DUMP_REG(DMA_CH[0].DMA_CHX_CUR_HST_RXBUF);
  590. DUMP_REG(DMA_CH[0].DMA_CHX_STAT);
  591. DUMP_REG(DMA_CH[1].DMA_CHX_CTRL);
  592. DUMP_REG(DMA_CH[1].DMA_CHX_TX_CTRL);
  593. DUMP_REG(DMA_CH[1].DMA_CHX_RX_CTRL);
  594. DUMP_REG(DMA_CH[1].DMA_CHX_TXDESC_LIST_ADDR);
  595. DUMP_REG(DMA_CH[1].DMA_CHX_RXDESC_LIST_ADDR);
  596. DUMP_REG(DMA_CH[1].DMA_CHX_TXDESC_TAIL_PTR);
  597. DUMP_REG(DMA_CH[1].DMA_CHX_RXDESC_TAIL_PTR);
  598. DUMP_REG(DMA_CH[1].DMA_CHX_TXDESC_RING_LENGTH);
  599. DUMP_REG(DMA_CH[1].DMA_CHX_RXDESC_RING_LENGTH);
  600. DUMP_REG(DMA_CH[1].DMA_CHX_INT_EN);
  601. DUMP_REG(DMA_CH[1].DMA_CHX_RX_INT_WDTIMER);
  602. DUMP_REG(DMA_CH[1].DMA_CHX_SLOT_FUNC_CTRL_STAT);
  603. DUMP_REG(DMA_CH[1].DMA_CHX_CUR_HST_TXDESC);
  604. DUMP_REG(DMA_CH[1].DMA_CHX_CUR_HST_RXDESC);
  605. DUMP_REG(DMA_CH[1].DMA_CHX_CUR_HST_TXBUF);
  606. DUMP_REG(DMA_CH[1].DMA_CHX_CUR_HST_RXBUF);
  607. DUMP_REG(DMA_CH[1].DMA_CHX_STAT);
  608. }
  609. void emac_bd_dump(void)
  610. {
  611. int i;
  612. rt_kprintf("rx bd dump: \n");
  613. for (i = 0; i < ENET_RXBD_NUM; i++)
  614. {
  615. enet_rx_bd_struct_t * rx_bd = get_rx_desc(i);
  616. rt_kprintf("buf1: %p, buf2: %p, ctrl: %08x\n",
  617. rx_bd->buff1Addr,
  618. rx_bd->buff2Addr,
  619. rx_bd->control);
  620. }
  621. rt_kprintf("tx bd dump: \n");
  622. for (i = 0; i < ENET_TXBD_NUM; i++)
  623. {
  624. enet_tx_bd_struct_t * tx_bd = get_tx_desc(i);
  625. rt_kprintf("buf1: %p, buf2: %p, len: %08x, ctrl: %08x\n",
  626. tx_bd->buff1Addr,
  627. tx_bd->buff2Addr,
  628. tx_bd->buffLen,
  629. tx_bd->controlStat);
  630. }
  631. }
  632. #ifdef RT_USING_FINSH
  633. #include <finsh.h>
  634. FINSH_FUNCTION_EXPORT(emac_stat, dump emac stat data);
  635. FINSH_FUNCTION_EXPORT(phy_dump, dump phy registers);
  636. FINSH_FUNCTION_EXPORT(emac_dump, dump emac registers);
  637. FINSH_FUNCTION_EXPORT(emac_bd_dump, dump emac tx and rx descriptor);
  638. #endif