diff --git a/src/port/nxp_mcxe_enet/README.md b/src/port/nxp_mcxe_enet/README.md index dd493451..58818452 100644 --- a/src/port/nxp_mcxe_enet/README.md +++ b/src/port/nxp_mcxe_enet/README.md @@ -1,13 +1,14 @@ # wolfIP NXP MCX E247 port -Ethernet driver for the NXP MCX E247, built on the MCUXpresso SDK `fsl_enet` driver and `phylan8741` PHY component. It is polled, uses no interrupts, and drives one MAC. +Ethernet driver for the NXP MCX E247: it programs the ENET MAC, the LAN8741 PHY (over MDIO) and, optionally, an eDMA channel. It can be polled or interrupt driven. It is consumed by the wolfBoot test-app, which links wolfIP core, this driver and the TFTP client. See the NXP MCXE247 section of the wolfBoot `docs/Targets.md` for the build. ## Files -- `nxp_mcxe_enet.h` - public API and board parameters. -- `nxp_mcxe_enet.c` - pin setup, PHY reset and link wait, MAC start, wolfIP poll/send callbacks. +- `nxp_mcxe_enet.h` - public API, board parameters and build options. +- `nxp_mcxe_enet.c` - pin setup, PHY reset and link wait, MAC start, descriptor rings, wolfIP poll/send callbacks, interrupt handler. +- `nxp_mcxe_enet_regs.h` - the ENET, eDMA, clock-gate, pin, SCG, NVIC and DWT registers the driver uses (from the MCXE24x Reference Manual). - `config.h` - wolfIP compile-time configuration for this port. ## Board @@ -18,7 +19,32 @@ Defaults target the FRDM-MCXE247: RMII to a LAN8741A-EN PHY at MDIO address 0, 5 |---|---|---| | `NXP_MCXE_ENET_PHY_ADDR` | `0` | MDIO address of the PHY | | `NXP_MCXE_ENET_MAC` | `{ 0x02, 0x00, 0xE2, 0x47, 0x00, 0x01 }` | used when `nxp_mcxe_enet_init()` is passed `mac=NULL` | +| `NXP_MCXE_ENET_SOSC_HZ` | `8000000` | system oscillator, used to work out the core clock when it runs from SOSC or the SPLL | + +## Build options + +| Define | Default | Notes | +|---|---|---| +| `NXP_MCXE_ENET_IRQ` | `0` | `1`: the receive interrupt marks frames waiting, so `poll()` returns at once when there are none. Route ENET IRQs 74 (receive) and 75 (error) to `nxp_mcxe_enet_irq_handler()`; the transmit interrupt (73) is not enabled. | +| `NXP_MCXE_ENET_IRQ_PRIO` | `0x80` | NVIC priority of the ENET interrupts | +| `NXP_MCXE_ENET_EDMA` | `1` | copy frames between the descriptor rings and the wolfIP buffers with an eDMA channel instead of `memcpy`. The CPU waits for each copy. | +| `NXP_MCXE_ENET_EDMA_CH` | `0` | eDMA channel, reserved for the driver | +| `NXP_MCXE_ENET_EDMA_MIN` | `128` | shorter frames are copied with `memcpy` | +| `NXP_MCXE_ENET_STATS` | `0` | `1`: count frames and the core cycles (DWT) spent receiving, sending, polling an empty ring, copying and waiting for a free transmit descriptor; read with `nxp_mcxe_enet_stats_get()`. The wolfBoot test-app throughput test uses it for its CPU breakdown. | + +## Data path + +wolfIP hands the driver its own buffers: `poll()` must fill the buffer it is given, and `send()` must be done with the buffer when it returns. + +- Receive: the MAC DMA writes each frame into one of 8 ring buffers (1536 bytes, one frame each). `poll()` copies it into the wolfIP buffer and returns the descriptor to the MAC. +- Transmit: `send()` copies each frame into one of 4 ring buffers, pads it to 60 bytes when short, and returns while the MAC sends it in the background. When all 4 are in use, `send()` polls until the oldest has gone out, for at most 5 ms. ## Memory -Buffer descriptors and frame buffers must be in SRAM_U, which is outside the code cache. The system MPU is disabled at init so that the ENET bus master can reach them. +`config.h` defaults `RXBUF_SIZE` and `TXBUF_SIZE` to one `LINK_MTU`, which allows one TCP segment in flight. Each socket carries one buffer of each, so with the 5 sockets of this configuration every extra `LINK_MTU` on both costs 15 KB. The wolfBoot test-app sets both to 9216 (6 segments) and places the wolfIP state, 124.5 KB, in SRAM_L. + +The receive buffers must be 32-byte aligned, although the RM asks for 16: with a buffer at 16 mod 32, a UDP flood at line rate stops the receive DMA until the MAC is reset. The driver aligns the frame buffers to 64 bytes, like most other nxp enet drivers use. + +Descriptors and ring buffers, and every wolfIP buffer the MAC or the eDMA reads or writes, must be outside the code cache, e.g. in SRAM_U. The system MPU is disabled at init so that the ENET and eDMA bus masters can reach them. + +An application that uses the ENET interrupts must place its vector table at a 1 KB aligned address. diff --git a/src/port/nxp_mcxe_enet/config.h b/src/port/nxp_mcxe_enet/config.h index 58ee0c74..21a3f269 100644 --- a/src/port/nxp_mcxe_enet/config.h +++ b/src/port/nxp_mcxe_enet/config.h @@ -30,12 +30,18 @@ #define ETHERNET #define LINK_MTU 1536 -/* Sized for the 124 KB of SRAM_U shared with the TFTP receive buffer */ #define MAX_TCPSOCKETS 2 #define MAX_UDPSOCKETS 2 #define MAX_ICMPSOCKETS 1 +/* Every socket has one receive and one transmit buffer, so each extra + * LINK_MTU on both costs 5 * 2 * LINK_MTU (15 KB). One MTU each keeps TCP to + * one segment in flight; the application may raise them when it has RAM. */ +#ifndef RXBUF_SIZE #define RXBUF_SIZE LINK_MTU +#endif +#ifndef TXBUF_SIZE #define TXBUF_SIZE LINK_MTU +#endif #define MAX_NEIGHBORS 4 #define WOLFIP_ARP_PENDING_MAX 2 diff --git a/src/port/nxp_mcxe_enet/nxp_mcxe_enet.c b/src/port/nxp_mcxe_enet/nxp_mcxe_enet.c index 30c1d604..f0f79511 100644 --- a/src/port/nxp_mcxe_enet/nxp_mcxe_enet.c +++ b/src/port/nxp_mcxe_enet/nxp_mcxe_enet.c @@ -1,8 +1,17 @@ /* nxp_mcxe_enet.c * - * NXP MCX E247 ethernet driver for wolfIP, built on the MCUXpresso SDK - * fsl_enet driver and the LAN8741 PHY component. The driver is polled and - * uses no interrupts. + * NXP MCX E247 ethernet driver for wolfIP. Standalone: it programs the ENET + * MAC, the LAN8741 PHY and the eDMA directly and needs no vendor SDK. + * + * Build options (see nxp_mcxe_enet.h): + * NXP_MCXE_ENET_IRQ 0: polled. 1: receive and transmit complete + * interrupts. The application routes ENET IRQs + * 73-75 to nxp_mcxe_enet_irq_handler(). + * NXP_MCXE_ENET_EDMA copy frames between the descriptor rings and + * wolfIP buffers with an eDMA channel instead + * of memcpy. + * NXP_MCXE_ENET_STATS count frames and core cycles spent in the + * driver, read with nxp_mcxe_enet_stats_get(). * * Copyright (C) 2026 wolfSSL Inc. * @@ -27,202 +36,708 @@ #include "config.h" #include "wolfip.h" -#include "fsl_enet.h" -#include "fsl_phylan8741.h" #include "nxp_mcxe_enet.h" +#include "nxp_mcxe_enet_regs.h" + +#define ENET_RX_BD_NUM 8U +#define ENET_TX_BD_NUM 4U +/* Receive buffer size: a multiple of 16 that holds a maximum frame, so + * every frame fits in one descriptor */ +#define ENET_BUF_SIZE 1536U +#define ENET_MAX_FRAME_LEN 1518U +#define ENET_MIN_FRAME_LEN 60U +#define ENET_MDC_HZ 2500000U + +#define PHY_RESET_PIN 3U /* PTC3 */ +#define PHY_RESET_US 25000U +#define PHY_LINK_POLL_US 10000U +#define PHY_LINK_POLLS 500U /* 5 seconds */ +#define PHY_ID_POLLS 100U +#define PHY_RESET_POLLS 500U + +/* IEEE 802.3 clause 22 and LAN8741 registers */ +#define PHY_BMCR 0x00U +#define PHY_BSR 0x01U +#define PHY_ID1 0x02U +#define PHY_ID2 0x03U +#define PHY_ANAR 0x04U +#define PHY_SCSR 0x1FU /* special control/status */ +#define PHY_BMCR_RESET 0x8000U +#define PHY_BMCR_ANEN 0x1000U +#define PHY_BMCR_ANRESTART 0x0200U +#define PHY_BSR_LINK 0x0004U +#define PHY_ANAR_ALL 0x01E1U /* 10/100 half/full, 802.3 */ +#define PHY_SCSR_ANDONE 0x1000U +#define PHY_SCSR_FULL 0x0010U +#define PHY_SCSR_100M 0x0008U +#define PHY_LAN8741_ID 0x0007C120UL +#define PHY_ID_REV_MASK 0xFFFFFFF0UL + +#define MDIO_TIMEOUT_US 1000U +#define ENET_RESET_TIMEOUT_US 1000U +#define EDMA_TIMEOUT_US 1000U +#define TX_TIMEOUT_US 5000U /* a maximum frame at 10 Mbit/s */ -#define ENET_RX_BD_NUM 8U -#define ENET_TX_BD_NUM 4U -#define ENET_BUF_SIZE SDK_SIZEALIGN(ENET_FRAME_MAX_FRAMELEN, \ - ENET_BUFF_ALIGNMENT) -#define ENET_MIN_FRAME_LEN 60U - -#define PHY_RESET_PIN 3U /* PTC3 */ -#define PHY_RESET_US 25000U -#define PHY_LINK_POLL_US 10000U -#define PHY_LINK_POLLS 500U /* 5 seconds */ - -/* The linker must place these in SRAM_U, which is outside the code cache. */ -SDK_ALIGN(static enet_rx_bd_struct_t g_rx_bd[ENET_RX_BD_NUM], - ENET_BUFF_ALIGNMENT); -SDK_ALIGN(static enet_tx_bd_struct_t g_tx_bd[ENET_TX_BD_NUM], - ENET_BUFF_ALIGNMENT); -SDK_ALIGN(static uint8_t g_rx_buf[ENET_RX_BD_NUM][ENET_BUF_SIZE], - ENET_BUFF_ALIGNMENT); -SDK_ALIGN(static uint8_t g_tx_buf[ENET_TX_BD_NUM][ENET_BUF_SIZE], - ENET_BUFF_ALIGNMENT); - -static enet_handle_t g_handle; -static phy_handle_t g_phy; -static phy_lan8741_resource_t g_phy_resource; +#define ENET_ALIGN16 __attribute__((aligned(16))) +/* Frame buffers. The RM asks for 16-byte alignment, but the enet dma gets + * stuck with receive FIFO overflow (UDP flood at line rate). + * 32 seems to work, but using 64 like other nxp enets */ +#define ENET_BUF_ALIGN __attribute__((aligned(64))) +/* Keeps every buffer of the arrays below aligned */ +typedef char enet_buf_size_check[((ENET_BUF_SIZE % 64U) == 0U) ? 1 : -1]; + +/* Legacy buffer descriptor, little-endian with ECR[DBSWP] set */ +struct enet_bd { + volatile uint16_t length; + volatile uint16_t control; + volatile uint32_t buffer; +}; + +/* The ENET DMA and the eDMA read and write these. They must be in memory + * outside the code cache (SRAM_U). */ +static struct enet_bd g_rx_bd[ENET_RX_BD_NUM] ENET_ALIGN16; +static struct enet_bd g_tx_bd[ENET_TX_BD_NUM] ENET_ALIGN16; +static uint8_t g_rx_buf[ENET_RX_BD_NUM][ENET_BUF_SIZE] ENET_BUF_ALIGN; +static uint8_t g_tx_buf[ENET_TX_BD_NUM][ENET_BUF_SIZE] ENET_BUF_ALIGN; + +static uint32_t g_rx_idx; +static uint32_t g_tx_idx; +static uint32_t g_core_hz; static int g_running; static int g_phy_addr = -1; -static status_t mdio_write(uint8_t phy, uint8_t reg, uint16_t data) +#if NXP_MCXE_ENET_IRQ +static volatile uint32_t g_rx_pending; +#endif +static volatile uint32_t g_bus_errors; +#if NXP_MCXE_ENET_EDMA +static int g_edma_dead; /* a timed-out transfer could not be stopped */ +#endif +#if NXP_MCXE_ENET_STATS +static struct nxp_mcxe_enet_stats g_stats; +#endif + +static uint32_t core_clock_hz(void) +{ + uint32_t csr = SCG_CSR; + uint32_t cfg; + uint32_t src; + + switch (SCG_CSR_SCS(csr)) { + case SCG_SCS_SOSC: + src = NXP_MCXE_ENET_SOSC_HZ; + break; + case SCG_SCS_SIRC: + src = 8000000U; + break; + case SCG_SCS_SPLL: + cfg = SCG_SPLLCFG; + src = (NXP_MCXE_ENET_SOSC_HZ / (SCG_SPLLCFG_PREDIV(cfg) + 1U)) * + (SCG_SPLLCFG_MULT(cfg) + 16U) / 2U; + break; + case SCG_SCS_FIRC: + default: + src = 48000000U; + break; + } + return src / (SCG_CSR_DIVCORE(csr) + 1U); +} + +/* Core cycles in us microseconds. */ +static uint32_t us_to_cycles(uint32_t us) { - return ENET_MDIOWrite(ENET, phy, reg, data); + return us * (g_core_hz / 1000000U); } -static status_t mdio_read(uint8_t phy, uint8_t reg, uint16_t *data) +static void delay_us(uint32_t us) { - return ENET_MDIORead(ENET, phy, reg, data); + uint32_t start = DWT_CYCCNT; + uint32_t ticks = us_to_cycles(us); + + while ((DWT_CYCCNT - start) < ticks) { + } } -static void enet_pins_init(uint32_t sysclk) +static int mdio_wait(void) { - CLOCK_EnableClock(kCLOCK_PortB); - CLOCK_EnableClock(kCLOCK_PortC); - CLOCK_EnableClock(kCLOCK_PortD); - CLOCK_EnableClock(kCLOCK_PortE); + uint32_t start = DWT_CYCCNT; + uint32_t ticks = us_to_cycles(MDIO_TIMEOUT_US); + + while ((ENET_EIR & ENET_EIR_MII) == 0U) { + if ((DWT_CYCCNT - start) >= ticks) + return -1; + } + ENET_EIR = ENET_EIR_MII; + return 0; +} + +static int mdio_read(uint8_t phy, uint8_t reg, uint16_t *value) +{ + int ret; + + ENET_EIR = ENET_EIR_MII; + ENET_MMFR = ENET_MMFR_ST | ENET_MMFR_OP_READ | ENET_MMFR_PA(phy) | + ENET_MMFR_RA(reg) | ENET_MMFR_TA; + ret = mdio_wait(); + if (ret == 0) + *value = (uint16_t)(ENET_MMFR & ENET_MMFR_DATA_MASK); + return ret; +} + +static int mdio_write(uint8_t phy, uint8_t reg, uint16_t value) +{ + ENET_EIR = ENET_EIR_MII; + ENET_MMFR = ENET_MMFR_ST | ENET_MMFR_OP_WRITE | ENET_MMFR_PA(phy) | + ENET_MMFR_RA(reg) | ENET_MMFR_TA | value; + return mdio_wait(); +} + +#if NXP_MCXE_ENET_EDMA +static uint16_t edma_size(uintptr_t addr, uint32_t *bytes) +{ + if ((addr & 3U) == 0U) { + *bytes = 4U; + return DMA_SIZE_32BIT; + } + if ((addr & 1U) == 0U) { + *bytes = 2U; + return DMA_SIZE_16BIT; + } + *bytes = 1U; + return DMA_SIZE_8BIT; +} + +/* Stop the channel after a timeout, before its buffers change hands. A + * channel that has not started yet is kept from starting by clearing START. + * A running one is cancelled with CR.CX, which retires it after its current + * read and write; only one channel runs at a time, so with ACTIVE set that is + * this one. Returns 0 once the channel is idle, or -1 if it is still busy + * after EDMA_TIMEOUT_US. */ +static int edma_stop(uint32_t ch) +{ + uint32_t start = DWT_CYCCNT; + uint32_t ticks = us_to_cycles(EDMA_TIMEOUT_US); + uint16_t csr; + int ret = -1; + + while ((DWT_CYCCNT - start) < ticks) { + csr = DMA_TCD_CSR(ch); + if ((csr & (DMA_CSR_START | DMA_CSR_ACTIVE)) == 0U) { + ret = 0; + break; + } + if ((csr & DMA_CSR_ACTIVE) != 0U) { + if ((DMA_CR & DMA_CR_CX) == 0U) + DMA_CR |= DMA_CR_CX; + } + else { + DMA_TCD_CSR(ch) = 0U; + } + } + return ret; +} + +/* Copy with the eDMA channel and wait for it. Each side uses the widest + * access its alignment allows. Copies the bytes past the last whole word with + * the CPU. Returns 0, or -1 on a transfer error or timeout (nothing is then + * known about the destination). On a timeout the channel is stopped first; + * if it cannot be, the eDMA is not used again. */ +static int edma_copy(uint8_t *dst, const uint8_t *src, uint32_t len) +{ + const uint32_t ch = NXP_MCXE_ENET_EDMA_CH; + uint32_t sbytes; + uint32_t dbytes; + uint32_t body = len & ~3U; + uint32_t start; + uint32_t ticks; + uint16_t ssize = edma_size((uintptr_t)src, &sbytes); + uint16_t dsize = edma_size((uintptr_t)dst, &dbytes); + int ret = 0; + + if (body > 0U) { + DMA_CDNE = (uint8_t)ch; + DMA_TCD_SADDR(ch) = (uint32_t)(uintptr_t)src; + DMA_TCD_SOFF(ch) = (uint16_t)sbytes; + DMA_TCD_ATTR(ch) = (uint16_t)(DMA_ATTR_SSIZE(ssize) | + DMA_ATTR_DSIZE(dsize)); + DMA_TCD_NBYTES(ch) = body; + DMA_TCD_SLAST(ch) = 0U; + DMA_TCD_DADDR(ch) = (uint32_t)(uintptr_t)dst; + DMA_TCD_DOFF(ch) = (uint16_t)dbytes; + DMA_TCD_CITER(ch) = 1U; + DMA_TCD_DLASTSGA(ch) = 0U; + DMA_TCD_BITER(ch) = 1U; + __asm__ volatile ("dsb" ::: "memory"); + DMA_TCD_CSR(ch) = DMA_CSR_START; + + start = DWT_CYCCNT; + ticks = us_to_cycles(EDMA_TIMEOUT_US); + while ((DMA_TCD_CSR(ch) & DMA_CSR_DONE) == 0U) { + if ((DMA_ERR & (1UL << ch)) != 0U || + (DWT_CYCCNT - start) >= ticks) { + ret = -1; + break; + } + } + if (ret != 0 && edma_stop(ch) != 0) + g_edma_dead = 1; + if ((DMA_ERR & (1UL << ch)) != 0U) { + DMA_CERR = (uint8_t)ch; + ret = -1; + } + DMA_CDNE = (uint8_t)ch; + __asm__ volatile ("dmb" ::: "memory"); + } + if (ret == 0 && body < len) + memcpy(dst + body, src + body, len - body); + return ret; +} +#endif + +static void frame_copy(uint8_t *dst, const uint8_t *src, uint32_t len) +{ + int done = 0; +#if NXP_MCXE_ENET_STATS + uint32_t start = DWT_CYCCNT; +#endif + +#if NXP_MCXE_ENET_EDMA + if (len >= NXP_MCXE_ENET_EDMA_MIN && !g_edma_dead && + edma_copy(dst, src, len) == 0) + done = 1; +#endif + if (!done) + memcpy(dst, src, len); +#if NXP_MCXE_ENET_STATS + g_stats.copy_cycles += DWT_CYCCNT - start; +#endif +} + +static void enet_pins_init(void) +{ + /* Enable clocks */ + PCC_PORTB |= PCC_CGC; + PCC_PORTC |= PCC_CGC; + PCC_PORTD |= PCC_CGC; + PCC_PORTE |= PCC_CGC; /* MDIO needs its GPIO direction set as an output before use (RM 51.1) */ - PORTB->PCR[4] = PORT_PCR_MUX(5); /* MDIO */ - GPIOB->PDDR |= (1UL << 4); - PORTC->PCR[0] = PORT_PCR_MUX(4); /* RXD1 */ - PORTC->PCR[1] = PORT_PCR_MUX(5); /* RXD0 */ - PORTC->PCR[17] = PORT_PCR_MUX(5); /* RX_DV */ - PORTD->PCR[11] = PORT_PCR_MUX(5); /* TX_CLK (50 MHz reference) */ + PORT_PCR(PORTB_BASE, 4) = PORT_PCR_MUX(5); /* MDIO */ + GPIO_PDDR(GPIOB_BASE) |= (1UL << 4); + PORT_PCR(PORTC_BASE, 0) = PORT_PCR_MUX(4); /* RXD1 */ + PORT_PCR(PORTC_BASE, 1) = PORT_PCR_MUX(5); /* RXD0 */ + PORT_PCR(PORTC_BASE, 17) = PORT_PCR_MUX(5); /* RX_DV */ + PORT_PCR(PORTD_BASE, 11) = PORT_PCR_MUX(5); /* TX_CLK (50 MHz ref) */ /* Transmit pads need high drive strength (RM 51.1) */ - PORTC->PCR[2] = PORT_PCR_MUX(5) | PORT_PCR_DSE_MASK; /* TXD0 */ - PORTD->PCR[7] = PORT_PCR_MUX(5) | PORT_PCR_DSE_MASK; /* TXD1 */ - PORTD->PCR[12] = PORT_PCR_MUX(5) | PORT_PCR_DSE_MASK; /* TX_EN */ - PORTE->PCR[8] = PORT_PCR_MUX(5) | PORT_PCR_DSE_MASK; /* MDC */ + PORT_PCR(PORTC_BASE, 2) = PORT_PCR_MUX(5) | PORT_PCR_DSE; /* TXD0 */ + PORT_PCR(PORTD_BASE, 7) = PORT_PCR_MUX(5) | PORT_PCR_DSE; /* TXD1 */ + PORT_PCR(PORTD_BASE, 12) = PORT_PCR_MUX(5) | PORT_PCR_DSE; /* TX_EN */ + PORT_PCR(PORTE_BASE, 8) = PORT_PCR_MUX(5) | PORT_PCR_DSE; /* MDC */ /* Pulse the PHY reset */ - PORTC->PCR[PHY_RESET_PIN] = PORT_PCR_MUX(1); - GPIOC->PCOR = (1UL << PHY_RESET_PIN); - GPIOC->PDDR |= (1UL << PHY_RESET_PIN); - SDK_DelayAtLeastUs(PHY_RESET_US, sysclk); - GPIOC->PSOR = (1UL << PHY_RESET_PIN); - SDK_DelayAtLeastUs(PHY_RESET_US, sysclk); + PORT_PCR(PORTC_BASE, PHY_RESET_PIN) = PORT_PCR_MUX(1); + GPIO_PCOR(GPIOC_BASE) = (1UL << PHY_RESET_PIN); + GPIO_PDDR(GPIOC_BASE) |= (1UL << PHY_RESET_PIN); + delay_us(PHY_RESET_US); + GPIO_PSOR(GPIOC_BASE) = (1UL << PHY_RESET_PIN); + delay_us(PHY_RESET_US); +} + +static void enet_irq_disable(void) +{ + ENET_EIMR = 0U; + NVIC_ICER(ENET_TX_IRQN / 32U) = (1UL << (ENET_TX_IRQN % 32U)); + NVIC_ICER(ENET_RX_IRQN / 32U) = (1UL << (ENET_RX_IRQN % 32U)); + NVIC_ICER(ENET_ERR_IRQN / 32U) = (1UL << (ENET_ERR_IRQN % 32U)); +} + +#if NXP_MCXE_ENET_IRQ +static void enet_irq_enable(void) +{ + const uint32_t irqs[2] = { ENET_RX_IRQN, ENET_ERR_IRQN }; + uint32_t i; + + g_rx_pending = 1U; /* look at the ring once, frames may be waiting */ + ENET_EIR = ENET_EIR_ALL; + ENET_EIMR = ENET_EIR_RXF | ENET_EIR_EBERR; + for (i = 0; i < sizeof(irqs) / sizeof(irqs[0]); i++) { + NVIC_IPR(irqs[i]) = (uint8_t)NXP_MCXE_ENET_IRQ_PRIO; + NVIC_ICPR(irqs[i] / 32U) = (1UL << (irqs[i] % 32U)); + NVIC_ISER(irqs[i] / 32U) = (1UL << (irqs[i] % 32U)); + } +} + +void nxp_mcxe_enet_irq_handler(void) +{ + uint32_t eir = ENET_EIR & ENET_EIMR; + + ENET_EIR = eir; + if ((eir & ENET_EIR_RXF) != 0U) + g_rx_pending = 1U; + if ((eir & ENET_EIR_EBERR) != 0U) + g_bus_errors++; + __asm__ volatile ("dsb" ::: "memory"); +} +#endif + +static int phy_init(uint8_t phy) +{ + uint16_t id1 = 0; + uint16_t id2 = 0; + uint16_t bmcr = PHY_BMCR_RESET; + uint32_t i; + int ret = -1; + + for (i = 0; i < PHY_ID_POLLS && ret != 0; i++) { + if (mdio_read(phy, PHY_ID1, &id1) == 0 && + mdio_read(phy, PHY_ID2, &id2) == 0 && + ((((uint32_t)id1 << 16) | id2) & PHY_ID_REV_MASK) == + PHY_LAN8741_ID) { + ret = 0; + } + else { + delay_us(1000U); + } + } + if (ret == 0) + ret = mdio_write(phy, PHY_BMCR, PHY_BMCR_RESET); + for (i = 0; ret == 0 && (bmcr & PHY_BMCR_RESET) != 0U; i++) { + if (i >= PHY_RESET_POLLS) + ret = -1; + else { + delay_us(1000U); + ret = mdio_read(phy, PHY_BMCR, &bmcr); + } + } + if (ret == 0) + ret = mdio_write(phy, PHY_ANAR, PHY_ANAR_ALL); + if (ret == 0) + ret = mdio_write(phy, PHY_BMCR, PHY_BMCR_ANEN | PHY_BMCR_ANRESTART); + return ret; +} + +/* Wait for link and auto-negotiation. Returns 0 with the special + * control/status register in scsr, or -1 without link. */ +static int phy_wait_link(uint8_t phy, uint16_t *scsr) +{ + uint16_t bsr = 0; + uint32_t polls; + + for (polls = 0; polls < PHY_LINK_POLLS; polls++) { + if (mdio_read(phy, PHY_BSR, &bsr) == 0 && + (bsr & PHY_BSR_LINK) != 0U && + mdio_read(phy, PHY_SCSR, scsr) == 0 && + (*scsr & PHY_SCSR_ANDONE) != 0U) { + return 0; + } + delay_us(PHY_LINK_POLL_US); + } + return -1; +} + +static void enet_rings_init(void) +{ + uint32_t i; + + for (i = 0; i < ENET_RX_BD_NUM; i++) { + g_rx_bd[i].buffer = (uint32_t)(uintptr_t)g_rx_buf[i]; + g_rx_bd[i].length = 0U; + g_rx_bd[i].control = (uint16_t)(ENET_RXBD_EMPTY | + ((i == ENET_RX_BD_NUM - 1U) ? ENET_RXBD_WRAP : 0U)); + } + for (i = 0; i < ENET_TX_BD_NUM; i++) { + g_tx_bd[i].buffer = (uint32_t)(uintptr_t)g_tx_buf[i]; + g_tx_bd[i].length = 0U; + g_tx_bd[i].control = (uint16_t) + ((i == ENET_TX_BD_NUM - 1U) ? ENET_TXBD_WRAP : 0U); + } + g_rx_idx = 0U; + g_tx_idx = 0U; + __asm__ volatile ("dsb" ::: "memory"); +} + +static void enet_mac_start(const uint8_t *mac, uint16_t scsr) +{ + uint32_t rcr = ENET_RCR_MAX_FL(ENET_MAX_FRAME_LEN) | ENET_RCR_CRCFWD | + ENET_RCR_MII_MODE | ENET_RCR_RMII_MODE; + int full = ((scsr & PHY_SCSR_FULL) != 0U); + + if ((scsr & PHY_SCSR_100M) == 0U) + rcr |= ENET_RCR_RMII_10T; + if (!full) + rcr |= ENET_RCR_DRT; + ENET_RCR = rcr; + ENET_TCR = full ? ENET_TCR_FDEN : 0U; + ENET_TACC = 0U; + ENET_RACC = 0U; + ENET_TFWR = ENET_TFWR_STRFWD; + ENET_RSFL = 0U; + + ENET_PALR = ((uint32_t)mac[0] << 24) | ((uint32_t)mac[1] << 16) | + ((uint32_t)mac[2] << 8) | (uint32_t)mac[3]; + ENET_PAUR = (((uint32_t)mac[4] << 8) | (uint32_t)mac[5]) << 16; + ENET_IAUR = 0U; + ENET_IALR = 0U; + ENET_GAUR = 0U; + ENET_GALR = 0U; + + enet_rings_init(); + ENET_TDSR = (uint32_t)(uintptr_t)g_tx_bd; + ENET_RDSR = (uint32_t)(uintptr_t)g_rx_bd; + ENET_MRBR = ENET_BUF_SIZE; + + ENET_EIR = ENET_EIR_ALL; +#if NXP_MCXE_ENET_IRQ + enet_irq_enable(); +#endif + ENET_ECR = ENET_ECR_ETHEREN | ENET_ECR_DBSWP; + ENET_RDAR = ENET_RDAR_RDAR; +} + +/* Give the current receive descriptor back to the MAC. */ +static void rx_release(uint16_t control) +{ + struct enet_bd *bd = &g_rx_bd[g_rx_idx]; + + __asm__ volatile ("dmb" ::: "memory"); + bd->control = (uint16_t)((control & ENET_RXBD_WRAP) | ENET_RXBD_EMPTY); + __asm__ volatile ("dsb" ::: "memory"); + g_rx_idx = (g_rx_idx + 1U) % ENET_RX_BD_NUM; + ENET_RDAR = ENET_RDAR_RDAR; } static int eth_poll(struct wolfIP_ll_dev *dev, void *frame, uint32_t len) { - uint32_t flen = 0; - status_t status; + struct enet_bd *bd; + uint16_t control; + uint32_t flen; + uint32_t n; + int ret = 0; (void)dev; - status = ENET_GetRxFrameSize(&g_handle, &flen, 0); - if (status == kStatus_ENET_RxFrameEmpty) - return 0; - if (status != kStatus_Success || flen > len) { - (void)ENET_ReadFrame(ENET, &g_handle, NULL, 0, 0, NULL); +#if NXP_MCXE_ENET_IRQ + if (g_rx_pending == 0U) return 0; +#else + if ((ENET_EIR & ENET_EIR_EBERR) != 0U) { + ENET_EIR = ENET_EIR_EBERR; + g_bus_errors++; } - if (ENET_ReadFrame(ENET, &g_handle, frame, flen, 0, NULL) != kStatus_Success) - return 0; - return (int)flen; +#endif + /* Skip errored frames (FIFO overruns, CRC errors) and look at the next + * descriptor. wolfIP takes 0 as "ring empty", so returning 0 for a bad + * frame would leave the rest of a full ring for the next poll; under a + * flood the MAC then fills each freed descriptor with another overrun + * fragment and the ring never drains. */ + for (n = 0; n < ENET_RX_BD_NUM && ret == 0; n++) { + bd = &g_rx_bd[g_rx_idx]; + control = bd->control; +#if NXP_MCXE_ENET_IRQ + if ((control & ENET_RXBD_EMPTY) != 0U) { + /* Ring drained: wait for the next receive interrupt. Look + * again after clearing, in case a frame landed in between. */ + g_rx_pending = 0U; + __asm__ volatile ("dmb" ::: "memory"); + control = bd->control; + if ((control & ENET_RXBD_EMPTY) == 0U) + g_rx_pending = 1U; + } +#endif + if ((control & ENET_RXBD_EMPTY) != 0U) + break; + + __asm__ volatile ("dmb" ::: "memory"); + flen = bd->length; + if ((control & ENET_RXBD_LAST) != 0U && + (control & ENET_RXBD_ERRORS) == 0U && + flen > 0U && flen <= len && frame != NULL) { + frame_copy((uint8_t *)frame, + (const uint8_t *)(uintptr_t)bd->buffer, flen); + ret = (int)flen; + } + rx_release(control); + } + return ret; +} + +/* Wait until the MAC has sent the frame in descriptor bd. This polls rather + * than sleeping in wfi: the core cycle counter stops while the core sleeps, + * so a transmitter that never raises its interrupt would never time out. */ +static int tx_wait(const struct enet_bd *bd) +{ + uint32_t start = DWT_CYCCNT; + uint32_t ticks = us_to_cycles(TX_TIMEOUT_US); + + while ((bd->control & ENET_TXBD_READY) != 0U) { + if ((DWT_CYCCNT - start) >= ticks) + return -1; + } + return 0; } static int eth_send(struct wolfIP_ll_dev *dev, void *frame, uint32_t len) { - uint8_t padded[ENET_MIN_FRAME_LEN]; + uint32_t idx = g_tx_idx; + struct enet_bd *bd = &g_tx_bd[idx]; const uint8_t *data = (const uint8_t *)frame; uint32_t tx_len = len; - status_t status; (void)dev; - if (len == 0 || len > ENET_FRAME_MAX_FRAMELEN) + if (frame == NULL || len == 0U || len > ENET_MAX_FRAME_LEN) return -1; + /* Ring full: wait for the oldest frame rather than fail, since wolfIP + * does not retry a dropped ACK */ + if ((bd->control & ENET_TXBD_READY) != 0U) { +#if NXP_MCXE_ENET_STATS + uint32_t start = DWT_CYCCNT; + int rc = tx_wait(bd); + + g_stats.tx_wait_cycles += DWT_CYCCNT - start; + g_stats.tx_ring_full++; + if (rc != 0) + return -WOLFIP_EAGAIN; +#else + if (tx_wait(bd) != 0) + return -WOLFIP_EAGAIN; +#endif + } + + /* wolfIP reuses its buffer when send() returns, so the frame is copied + * into the ring and sent in the background */ + frame_copy(g_tx_buf[idx], data, len); if (len < ENET_MIN_FRAME_LEN) { - memcpy(padded, frame, len); - memset(padded + len, 0, ENET_MIN_FRAME_LEN - len); - data = padded; + memset(g_tx_buf[idx] + len, 0, ENET_MIN_FRAME_LEN - len); tx_len = ENET_MIN_FRAME_LEN; } - status = ENET_SendFrame(ENET, &g_handle, data, tx_len, 0, false, NULL); - if (status == kStatus_ENET_TxFrameBusy) - return -2; - return (status == kStatus_Success) ? (int)len : -1; + bd->buffer = (uint32_t)(uintptr_t)g_tx_buf[idx]; + bd->length = (uint16_t)tx_len; + + __asm__ volatile ("dmb" ::: "memory"); + bd->control = (uint16_t)(ENET_TXBD_READY | ENET_TXBD_LAST | ENET_TXBD_TC | + ((idx == ENET_TX_BD_NUM - 1U) ? ENET_TXBD_WRAP : 0U)); + __asm__ volatile ("dsb" ::: "memory"); + ENET_TDAR = ENET_TDAR_TDAR; + g_tx_idx = (idx + 1U) % ENET_TX_BD_NUM; + return (int)len; +} + +#if NXP_MCXE_ENET_STATS +static int eth_poll_stats(struct wolfIP_ll_dev *dev, void *frame, + uint32_t len) +{ + uint32_t start = DWT_CYCCNT; + int ret = eth_poll(dev, frame, len); + uint32_t cycles = DWT_CYCCNT - start; + + if (ret > 0) { + g_stats.rx_cycles += cycles; + g_stats.rx_bytes += (uint32_t)ret; + g_stats.rx_frames++; + } + else { + g_stats.idle_cycles += cycles; + } + return ret; +} + +static int eth_send_stats(struct wolfIP_ll_dev *dev, void *frame, + uint32_t len) +{ + uint32_t start = DWT_CYCCNT; + int ret = eth_send(dev, frame, len); + + g_stats.tx_cycles += DWT_CYCCNT - start; + if (ret > 0) { + g_stats.tx_bytes += (uint32_t)ret; + g_stats.tx_frames++; + } + return ret; +} + +void nxp_mcxe_enet_stats_get(struct nxp_mcxe_enet_stats *st) +{ + if (st != NULL) { + memcpy(st, &g_stats, sizeof(*st)); + st->core_hz = g_core_hz; + } +} + +void nxp_mcxe_enet_stats_reset(void) +{ + memset(&g_stats, 0, sizeof(g_stats)); } +#endif int nxp_mcxe_enet_init(struct wolfIP_ll_dev *ll, const uint8_t *mac) { static const uint8_t default_mac[6] = NXP_MCXE_ENET_MAC; - enet_config_t config; - enet_buffer_config_t buffers; - phy_config_t phy_config; - phy_speed_t speed = kPHY_Speed100M; - phy_duplex_t duplex = kPHY_FullDuplex; - bool link = false; - bool autoneg = false; - uint32_t sysclk; - uint32_t polls; + const uint8_t phy = (uint8_t)NXP_MCXE_ENET_PHY_ADDR; + uint16_t scsr = 0; + uint32_t start; + uint32_t mii_speed; + uint32_t hold; if (ll == NULL) return -1; if (mac == NULL) mac = default_mac; + g_core_hz = core_clock_hz(); + DEMCR |= DEMCR_TRCENA; + DWT_CTRL |= DWT_CTRL_CYCCNTENA; + if (g_running) { - ENET_Deinit(ENET); + enet_irq_disable(); + ENET_ECR = 0U; g_running = 0; } g_phy_addr = -1; - /* The system MPU blocks the ENET bus master by default */ - SYSMPU->CESR &= ~SYSMPU_CESR_VLD_MASK; + /* The system MPU blocks the ENET and eDMA bus masters by default */ + SYSMPU_CESR &= ~SYSMPU_CESR_VLD; +#if NXP_MCXE_ENET_EDMA + SIM_PLATCGC |= SIM_PLATCGC_CGCDMA; +#endif - sysclk = CLOCK_GetCoreSysClkFreq(); - enet_pins_init(sysclk); + enet_pins_init(); + PCC_ENET |= PCC_CGC; - CLOCK_EnableClock(kCLOCK_Enet); - ENET_SetSMI(ENET, sysclk, false); + /* Reset the MAC; it clears itself in a few clocks */ + ENET_ECR = ENET_ECR_RESET; + start = DWT_CYCCNT; + while ((ENET_ECR & ENET_ECR_RESET) != 0U) { + if ((DWT_CYCCNT - start) >= us_to_cycles(ENET_RESET_TIMEOUT_US)) + return -4; + } + enet_irq_disable(); - g_phy_resource.read = mdio_read; - g_phy_resource.write = mdio_write; - memset(&phy_config, 0, sizeof(phy_config)); - phy_config.phyAddr = NXP_MCXE_ENET_PHY_ADDR; - phy_config.ops = &phylan8741_ops; - phy_config.resource = &g_phy_resource; - phy_config.autoNeg = true; - if (PHY_Init(&g_phy, &phy_config) != kStatus_Success) - return -2; - g_phy_addr = (int)NXP_MCXE_ENET_PHY_ADDR; + /* MDC = core clock / (2 * (MII_SPEED + 1)) <= 2.5 MHz, MDIO hold + * time >= 10 ns */ + mii_speed = (g_core_hz + 2U * ENET_MDC_HZ - 1U) / (2U * ENET_MDC_HZ) - 1U; + hold = (10U + 1000000000U / g_core_hz - 1U) / (1000000000U / g_core_hz); + ENET_MSCR = ENET_MSCR_MII_SPEED(mii_speed) | + ENET_MSCR_HOLDTIME(hold > 0U ? hold - 1U : 0U); - for (polls = 0; polls < PHY_LINK_POLLS; polls++) { - (void)PHY_GetLinkStatus(&g_phy, &link); - if (link) { - (void)PHY_GetAutoNegotiationStatus(&g_phy, &autoneg); - if (autoneg) - break; - } - SDK_DelayAtLeastUs(PHY_LINK_POLL_US, sysclk); - } - if (!link) + if (phy_init(phy) != 0) + return -2; + g_phy_addr = (int)phy; + if (phy_wait_link(phy, &scsr) != 0) return -3; - else if (autoneg) - (void)PHY_GetLinkSpeedDuplex(&g_phy, &speed, &duplex); - - ENET_GetDefaultConfig(&config); - config.miiMode = kENET_RmiiMode; - config.miiSpeed = (enet_mii_speed_t)speed; - config.miiDuplex = (enet_mii_duplex_t)duplex; - - memset(&buffers, 0, sizeof(buffers)); - buffers.rxBdNumber = ENET_RX_BD_NUM; - buffers.txBdNumber = ENET_TX_BD_NUM; - buffers.rxBuffSizeAlign = ENET_BUF_SIZE; - buffers.txBuffSizeAlign = ENET_BUF_SIZE; - buffers.rxBdStartAddrAlign = g_rx_bd; - buffers.txBdStartAddrAlign = g_tx_bd; - buffers.rxBufferAlign = &g_rx_buf[0][0]; - buffers.txBufferAlign = &g_tx_buf[0][0]; - - if (ENET_Init(ENET, &g_handle, &config, &buffers, (uint8_t *)mac, - sysclk) != kStatus_Success) { - return -4; - } + + enet_mac_start(mac, scsr); g_running = 1; - ENET_ActiveRead(ENET); memcpy(ll->mac, mac, 6); memset(ll->ifname, 0, sizeof(ll->ifname)); memcpy(ll->ifname, "eth0", 4); ll->mtu = LINK_MTU; +#if NXP_MCXE_ENET_STATS + ll->poll = eth_poll_stats; + ll->send = eth_send_stats; +#else ll->poll = eth_poll; ll->send = eth_send; +#endif return 0; } @@ -235,19 +750,26 @@ uint16_t nxp_mcxe_enet_phy_read(uint8_t reg) { uint16_t value = 0xFFFFU; - if (g_phy_addr < 0 || - ENET_MDIORead(ENET, (uint8_t)g_phy_addr, reg, &value) != kStatus_Success) { + if (g_phy_addr < 0 || mdio_read((uint8_t)g_phy_addr, reg, &value) != 0) return 0xFFFFU; - } return value; } uint32_t nxp_mcxe_enet_link_up(void) { - bool link = false; + uint16_t bsr = 0; if (g_phy_addr < 0) return 0; - (void)PHY_GetLinkStatus(&g_phy, &link); - return link ? 1U : 0U; + /* The link bit latches low: the first read reports a past drop */ + if (mdio_read((uint8_t)g_phy_addr, PHY_BSR, &bsr) != 0 || + mdio_read((uint8_t)g_phy_addr, PHY_BSR, &bsr) != 0) { + return 0; + } + return ((bsr & PHY_BSR_LINK) != 0U) ? 1U : 0U; +} + +uint32_t nxp_mcxe_enet_bus_errors(void) +{ + return g_bus_errors; } diff --git a/src/port/nxp_mcxe_enet/nxp_mcxe_enet.h b/src/port/nxp_mcxe_enet/nxp_mcxe_enet.h index f5168491..169b1730 100644 --- a/src/port/nxp_mcxe_enet/nxp_mcxe_enet.h +++ b/src/port/nxp_mcxe_enet/nxp_mcxe_enet.h @@ -1,8 +1,7 @@ /* nxp_mcxe_enet.h * - * NXP MCX E247 ethernet driver for wolfIP, built on the MCUXpresso SDK - * fsl_enet driver and the LAN8741 PHY component. Defaults target the - * FRDM-MCXE247 board (RMII, PHY at MDIO address 0, reset on PTC3). + * NXP MCX E247 ethernet driver for wolfIP on the ENET MAC and the LAN8741 PHY + * Defaults target the FRDM-MCXE247 board (RMII, PHY at MDIO 0, reset on PTC3). * * Copyright (C) 2026 wolfSSL Inc. * @@ -39,13 +38,58 @@ #define NXP_MCXE_ENET_MAC { 0x02, 0x00, 0xE2, 0x47, 0x00, 0x01 } #endif -/* Reset the PHY, wait for link, start the MAC, and populate ll with the MAC - * address, interface name and poll/send callbacks. May be called again to - * restart the interface. Returns 0 when the MAC is running, even without - * link, or a negative value when the PHY or MAC cannot be initialized. */ +/* 1: interrupt driven. The receive interrupt marks frames waiting, so + * poll() returns at once when there are none, and send() sleeps in wfi + * until the transmit interrupt when the transmit ring is full. The application must route ENET + * interrupts 73 (transmit), 74 (receive) and 75 (error) to + * nxp_mcxe_enet_irq_handler(). 0: polled, no interrupts. */ +#ifndef NXP_MCXE_ENET_IRQ +#define NXP_MCXE_ENET_IRQ 0 +#endif + +/* NVIC priority of the ENET interrupts (upper 4 bits are used). */ +#ifndef NXP_MCXE_ENET_IRQ_PRIO +#define NXP_MCXE_ENET_IRQ_PRIO 0x80U +#endif + +/* 1: copy frames between the descriptor rings and the wolfIP buffers with + * an eDMA channel instead of memcpy. The CPU waits for each copy, because + * wolfIP needs the data when poll() or send() returns. */ +#ifndef NXP_MCXE_ENET_EDMA +#define NXP_MCXE_ENET_EDMA 1 +#endif + +/* eDMA channel used for the copies; reserved for this driver. */ +#ifndef NXP_MCXE_ENET_EDMA_CH +#define NXP_MCXE_ENET_EDMA_CH 0U +#endif + +/* Shorter frames are copied with memcpy, which is faster than setting up + * an eDMA transfer for a few bytes. */ +#ifndef NXP_MCXE_ENET_EDMA_MIN +#define NXP_MCXE_ENET_EDMA_MIN 128U +#endif + +/* 1: count frames and the core cycles (DWT) spent in the driver, for + * throughput profiling. Read with nxp_mcxe_enet_stats_get(). */ +#ifndef NXP_MCXE_ENET_STATS +#define NXP_MCXE_ENET_STATS 0 +#endif + +/* System oscillator frequency, used to work out the core clock when it + * runs from SOSC or the SPLL. */ +#ifndef NXP_MCXE_ENET_SOSC_HZ +#define NXP_MCXE_ENET_SOSC_HZ 8000000U +#endif + +/* Reset the PHY, wait up to 5 s for link, start the MAC, and populate ll + * with the MAC address, interface name and poll/send callbacks. May be + * called again to restart the interface. Returns 0 when the MAC is running, + * -2 when the PHY does not respond, -3 without link and -4 when the MAC + * does not come out of reset. */ int nxp_mcxe_enet_init(struct wolfIP_ll_dev *ll, const uint8_t *mac); -/* MDIO address of the PHY, or -1 before a successful init. */ +/* MDIO address of the PHY, or -1 before the PHY was found. */ int nxp_mcxe_enet_phy_addr(void); /* MDIO clause-22 read of the PHY (0xFFFF on error). */ @@ -54,4 +98,34 @@ uint16_t nxp_mcxe_enet_phy_read(uint8_t reg); /* Non-zero when the PHY reports link up. */ uint32_t nxp_mcxe_enet_link_up(void); +/* ENET DMA bus errors seen since start. */ +uint32_t nxp_mcxe_enet_bus_errors(void); + +#if NXP_MCXE_ENET_STATS +/* Driver counters since the last nxp_mcxe_enet_stats_reset(). Cycle counts + * are core clock cycles; copy_cycles is included in rx_cycles and + * tx_cycles, tx_wait_cycles in tx_cycles. */ +struct nxp_mcxe_enet_stats { + uint64_t rx_cycles; /* poll() calls that returned a frame */ + uint64_t idle_cycles; /* poll() calls that found no frame */ + uint64_t tx_cycles; /* send() calls */ + uint64_t tx_wait_cycles; /* send() waiting for a free descriptor */ + uint64_t copy_cycles; /* frame copies, both directions */ + uint64_t rx_bytes; + uint64_t tx_bytes; + uint32_t rx_frames; + uint32_t tx_frames; + uint32_t tx_ring_full; /* send() calls that found the ring full */ + uint32_t core_hz; +}; + +void nxp_mcxe_enet_stats_get(struct nxp_mcxe_enet_stats *st); +void nxp_mcxe_enet_stats_reset(void); +#endif + +#if NXP_MCXE_ENET_IRQ +/* Interrupt handler for ENET IRQs 73, 74 and 75. */ +void nxp_mcxe_enet_irq_handler(void); +#endif + #endif /* WOLFIP_NXP_MCXE_ENET_H */ diff --git a/src/port/nxp_mcxe_enet/nxp_mcxe_enet_regs.h b/src/port/nxp_mcxe_enet/nxp_mcxe_enet_regs.h new file mode 100644 index 00000000..8918d058 --- /dev/null +++ b/src/port/nxp_mcxe_enet/nxp_mcxe_enet_regs.h @@ -0,0 +1,200 @@ +/* nxp_mcxe_enet_regs.h + * + * Register definitions used by the NXP MCX E247 ethernet driver: ENET MAC, + * eDMA, clock gating, pin muxing, SCG and the Cortex-M4 NVIC and DWT. + * Offsets and bits are from the MCXE24x Reference Manual (MCXE24XRM). + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfIP TCP/IP stack. + * + * wolfIP is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfIP is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ +#ifndef WOLFIP_NXP_MCXE_ENET_REGS_H +#define WOLFIP_NXP_MCXE_ENET_REGS_H + +#include + +#define MCXE_REG32(addr) (*(volatile uint32_t *)(addr)) +#define MCXE_REG16(addr) (*(volatile uint16_t *)(addr)) +#define MCXE_REG8(addr) (*(volatile uint8_t *)(addr)) + +/* ENET MAC */ +#define ENET_BASE 0x40079000UL +#define ENET_EIR MCXE_REG32(ENET_BASE + 0x004UL) +#define ENET_EIMR MCXE_REG32(ENET_BASE + 0x008UL) +#define ENET_RDAR MCXE_REG32(ENET_BASE + 0x010UL) +#define ENET_TDAR MCXE_REG32(ENET_BASE + 0x014UL) +#define ENET_ECR MCXE_REG32(ENET_BASE + 0x024UL) +#define ENET_MMFR MCXE_REG32(ENET_BASE + 0x040UL) +#define ENET_MSCR MCXE_REG32(ENET_BASE + 0x044UL) +#define ENET_RCR MCXE_REG32(ENET_BASE + 0x084UL) +#define ENET_TCR MCXE_REG32(ENET_BASE + 0x0C4UL) +#define ENET_PALR MCXE_REG32(ENET_BASE + 0x0E4UL) +#define ENET_PAUR MCXE_REG32(ENET_BASE + 0x0E8UL) +#define ENET_IAUR MCXE_REG32(ENET_BASE + 0x118UL) +#define ENET_IALR MCXE_REG32(ENET_BASE + 0x11CUL) +#define ENET_GAUR MCXE_REG32(ENET_BASE + 0x120UL) +#define ENET_GALR MCXE_REG32(ENET_BASE + 0x124UL) +#define ENET_TFWR MCXE_REG32(ENET_BASE + 0x144UL) +#define ENET_RDSR MCXE_REG32(ENET_BASE + 0x180UL) +#define ENET_TDSR MCXE_REG32(ENET_BASE + 0x184UL) +#define ENET_MRBR MCXE_REG32(ENET_BASE + 0x188UL) +#define ENET_RSFL MCXE_REG32(ENET_BASE + 0x190UL) +#define ENET_TACC MCXE_REG32(ENET_BASE + 0x1C0UL) +#define ENET_RACC MCXE_REG32(ENET_BASE + 0x1C4UL) + +#define ENET_EIR_EBERR (1UL << 22) +#define ENET_EIR_MII (1UL << 23) +#define ENET_EIR_RXB (1UL << 24) +#define ENET_EIR_RXF (1UL << 25) +#define ENET_EIR_TXB (1UL << 26) +#define ENET_EIR_TXF (1UL << 27) +#define ENET_EIR_ALL 0x7FFFFFFFUL + +#define ENET_RDAR_RDAR (1UL << 24) +#define ENET_TDAR_TDAR (1UL << 24) + +#define ENET_ECR_RESET (1UL << 0) +#define ENET_ECR_ETHEREN (1UL << 1) +#define ENET_ECR_DBSWP (1UL << 8) /* little-endian descriptors */ + +#define ENET_MMFR_ST (1UL << 30) +#define ENET_MMFR_OP_WRITE (1UL << 28) +#define ENET_MMFR_OP_READ (2UL << 28) +#define ENET_MMFR_PA(x) (((uint32_t)(x) & 0x1FUL) << 23) +#define ENET_MMFR_RA(x) (((uint32_t)(x) & 0x1FUL) << 18) +#define ENET_MMFR_TA (2UL << 16) +#define ENET_MMFR_DATA_MASK 0xFFFFUL + +#define ENET_MSCR_MII_SPEED(x) (((uint32_t)(x) & 0x3FUL) << 1) +#define ENET_MSCR_HOLDTIME(x) (((uint32_t)(x) & 0x7UL) << 8) + +#define ENET_RCR_DRT (1UL << 1) +#define ENET_RCR_MII_MODE (1UL << 2) +#define ENET_RCR_RMII_MODE (1UL << 8) +#define ENET_RCR_RMII_10T (1UL << 9) +#define ENET_RCR_CRCFWD (1UL << 14) /* strip the received CRC */ +#define ENET_RCR_MAX_FL(x) (((uint32_t)(x) & 0x3FFFUL) << 16) + +#define ENET_TCR_FDEN (1UL << 2) +#define ENET_TFWR_STRFWD (1UL << 8) + +/* Legacy buffer descriptor control bits (16-bit field) */ +#define ENET_RXBD_EMPTY 0x8000U +#define ENET_RXBD_WRAP 0x2000U +#define ENET_RXBD_LAST 0x0800U +#define ENET_RXBD_LG 0x0020U +#define ENET_RXBD_NO 0x0010U +#define ENET_RXBD_CR 0x0004U +#define ENET_RXBD_OV 0x0002U +#define ENET_RXBD_TR 0x0001U +#define ENET_RXBD_ERRORS (ENET_RXBD_LG | ENET_RXBD_NO | ENET_RXBD_CR | \ + ENET_RXBD_OV | ENET_RXBD_TR) + +#define ENET_TXBD_READY 0x8000U +#define ENET_TXBD_WRAP 0x2000U +#define ENET_TXBD_LAST 0x0800U +#define ENET_TXBD_TC 0x0400U /* append the CRC */ + +/* Interrupt numbers */ +#define ENET_TX_IRQN 73U +#define ENET_RX_IRQN 74U +#define ENET_ERR_IRQN 75U + +/* SIM, PCC: clock gating */ +#define SIM_PLATCGC MCXE_REG32(0x40048040UL) +#define SIM_PLATCGC_CGCDMA (1UL << 2) + +#define PCC_BASE 0x40065000UL +#define PCC_PORTB MCXE_REG32(PCC_BASE + 0x128UL) +#define PCC_PORTC MCXE_REG32(PCC_BASE + 0x12CUL) +#define PCC_PORTD MCXE_REG32(PCC_BASE + 0x130UL) +#define PCC_PORTE MCXE_REG32(PCC_BASE + 0x134UL) +#define PCC_ENET MCXE_REG32(PCC_BASE + 0x1E4UL) +#define PCC_CGC (1UL << 30) + +/* System MPU */ +#define SYSMPU_CESR MCXE_REG32(0x4000D000UL) +#define SYSMPU_CESR_VLD (1UL << 0) + +/* PORT pin control and GPIO */ +#define PORTB_BASE 0x4004A000UL +#define PORTC_BASE 0x4004B000UL +#define PORTD_BASE 0x4004C000UL +#define PORTE_BASE 0x4004D000UL +#define PORT_PCR(base, pin) MCXE_REG32((base) + 4UL * (uint32_t)(pin)) +#define PORT_PCR_MUX(x) (((uint32_t)(x) & 0x7UL) << 8) +#define PORT_PCR_DSE (1UL << 6) + +#define GPIOB_BASE 0x400FF040UL +#define GPIOC_BASE 0x400FF080UL +#define GPIO_PSOR(base) MCXE_REG32((base) + 0x04UL) +#define GPIO_PCOR(base) MCXE_REG32((base) + 0x08UL) +#define GPIO_PDDR(base) MCXE_REG32((base) + 0x14UL) + +/* SCG: core clock source and dividers */ +#define SCG_CSR MCXE_REG32(0x40064010UL) +#define SCG_SPLLCFG MCXE_REG32(0x40064608UL) +#define SCG_CSR_SCS(v) (((v) >> 24) & 0xFUL) +#define SCG_CSR_DIVCORE(v) (((v) >> 16) & 0xFUL) +#define SCG_SCS_SOSC 1UL +#define SCG_SCS_SIRC 2UL +#define SCG_SCS_FIRC 3UL +#define SCG_SCS_SPLL 6UL +#define SCG_SPLLCFG_PREDIV(v) (((v) >> 8) & 0x7UL) +#define SCG_SPLLCFG_MULT(v) (((v) >> 16) & 0x1FUL) + +/* eDMA */ +#define DMA_BASE 0x40008000UL +#define DMA_CR MCXE_REG32(DMA_BASE + 0x000UL) +#define DMA_CR_CX (1UL << 17) /* cancel the running channel */ +#define DMA_ES MCXE_REG32(DMA_BASE + 0x004UL) +#define DMA_CDNE MCXE_REG8(DMA_BASE + 0x01CUL) +#define DMA_CERR MCXE_REG8(DMA_BASE + 0x01EUL) +#define DMA_ERR MCXE_REG32(DMA_BASE + 0x02CUL) +#define DMA_TCD(ch) (DMA_BASE + 0x1000UL + 0x20UL * (uint32_t)(ch)) +#define DMA_TCD_SADDR(ch) MCXE_REG32(DMA_TCD(ch) + 0x00UL) +#define DMA_TCD_SOFF(ch) MCXE_REG16(DMA_TCD(ch) + 0x04UL) +#define DMA_TCD_ATTR(ch) MCXE_REG16(DMA_TCD(ch) + 0x06UL) +#define DMA_TCD_NBYTES(ch) MCXE_REG32(DMA_TCD(ch) + 0x08UL) +#define DMA_TCD_SLAST(ch) MCXE_REG32(DMA_TCD(ch) + 0x0CUL) +#define DMA_TCD_DADDR(ch) MCXE_REG32(DMA_TCD(ch) + 0x10UL) +#define DMA_TCD_DOFF(ch) MCXE_REG16(DMA_TCD(ch) + 0x14UL) +#define DMA_TCD_CITER(ch) MCXE_REG16(DMA_TCD(ch) + 0x16UL) +#define DMA_TCD_DLASTSGA(ch) MCXE_REG32(DMA_TCD(ch) + 0x18UL) +#define DMA_TCD_CSR(ch) MCXE_REG16(DMA_TCD(ch) + 0x1CUL) +#define DMA_TCD_BITER(ch) MCXE_REG16(DMA_TCD(ch) + 0x1EUL) +#define DMA_ATTR_SSIZE(x) ((uint16_t)(((x) & 0x7U) << 8)) +#define DMA_ATTR_DSIZE(x) ((uint16_t)((x) & 0x7U)) +#define DMA_SIZE_8BIT 0U +#define DMA_SIZE_16BIT 1U +#define DMA_SIZE_32BIT 2U +#define DMA_CSR_START 0x0001U +#define DMA_CSR_ACTIVE 0x0040U +#define DMA_CSR_DONE 0x0080U + +/* Cortex-M4 NVIC and DWT */ +#define NVIC_ISER(n) MCXE_REG32(0xE000E100UL + 4UL * (uint32_t)(n)) +#define NVIC_ICER(n) MCXE_REG32(0xE000E180UL + 4UL * (uint32_t)(n)) +#define NVIC_ICPR(n) MCXE_REG32(0xE000E280UL + 4UL * (uint32_t)(n)) +#define NVIC_IPR(irq) MCXE_REG8(0xE000E400UL + (uint32_t)(irq)) +#define DEMCR MCXE_REG32(0xE000EDFCUL) +#define DEMCR_TRCENA (1UL << 24) +#define DWT_CTRL MCXE_REG32(0xE0001000UL) +#define DWT_CTRL_CYCCNTENA (1UL << 0) +#define DWT_CYCCNT MCXE_REG32(0xE0001004UL) + +#endif /* WOLFIP_NXP_MCXE_ENET_REGS_H */ diff --git a/src/wolfip.c b/src/wolfip.c index 411a5519..aa9bb429 100644 --- a/src/wolfip.c +++ b/src/wolfip.c @@ -246,7 +246,9 @@ struct wolfIP_icmp_packet; #define TCP_PREACCEPT_TIMEOUT_MS 5000U #endif /* Arbitrary upper limit to avoid monopolizing the CPU during poll loops. */ +#ifndef WOLFIP_POLL_BUDGET #define WOLFIP_POLL_BUDGET 128 +#endif /* Macros */ #define IS_IP_BCAST(ip) ((ip) == 0xFFFFFFFFU) @@ -2288,22 +2290,63 @@ static uint32_t get_be32(const uint8_t *p) return ee32(be); } +/* Add len bytes at p to a running one's complement sum (RFC 1071). The data + * is summed as 32-bit words in host byte order: the folded sum is the same + * as one of 16-bit network order words, byte swapped (RFC 1071 2(B)), and + * csum_fold() swaps it back. p need not be aligned. Only the last block + * added to a sum may have an odd length; its last byte is padded with zero. */ +static uint64_t csum_add(uint64_t sum, const uint8_t *p, uint32_t len) +{ + uint32_t w0, w1, w2, w3; + uint16_t h; + uint8_t tail[2]; + + while (len >= 16U) { + memcpy(&w0, p, sizeof(w0)); + memcpy(&w1, p + 4, sizeof(w1)); + memcpy(&w2, p + 8, sizeof(w2)); + memcpy(&w3, p + 12, sizeof(w3)); + sum += (uint64_t)w0 + w1 + w2 + w3; + p += 16; + len -= 16U; + } + while (len >= 4U) { + memcpy(&w0, p, sizeof(w0)); + sum += w0; + p += 4; + len -= 4U; + } + if (len >= 2U) { + memcpy(&h, p, sizeof(h)); + sum += h; + p += 2; + len -= 2U; + } + if (len != 0U) { + tail[0] = p[0]; + tail[1] = 0U; + memcpy(&h, tail, sizeof(h)); + sum += h; + } + return sum; +} + +/* Fold a csum_add() sum to 16 bits and return its one's complement in host + * byte order, as the checksum field takes it through ee16(). */ +static uint16_t csum_fold(uint64_t sum) +{ + sum = (sum & 0xffffffffULL) + (sum >> 32); + sum = (sum & 0xffffffffULL) + (sum >> 32); + sum = (sum & 0xffffU) + (sum >> 16); + sum = (sum & 0xffffU) + (sum >> 16); + sum = (sum & 0xffffU) + (sum >> 16); + return ee16((uint16_t)~sum); +} + #ifdef IP_MULTICAST static uint16_t ip_checksum_buf(const void *buf, uint16_t len) { - const uint8_t *p = (const uint8_t *)buf; - uint32_t sum = 0; - - while (len > 1) { - sum += ((uint16_t)p[0] << 8) | p[1]; - p += 2; - len -= 2; - } - if (len) - sum += (uint16_t)p[0] << 8; - while (sum >> 16) - sum = (sum & 0xffffU) + (sum >> 16); - return (uint16_t)~sum; + return csum_fold(csum_add(0, (const uint8_t *)buf, len)); } static void put_be16(uint8_t *p, uint16_t v) @@ -5671,29 +5714,11 @@ static void tcp_recv_len(struct tsocket *t, struct wolfIP_tcp_seg *seg, static uint16_t transport_checksum(union transport_pseudo_header *ph, void *_data) { - uint32_t sum = 0; - uint32_t i = 0; - const uint8_t *ptr = (const uint8_t *)ph->buf; - const uint8_t *data = (const uint8_t *)_data; - uint16_t len = ee16(ph->ph.len); - uint16_t word; - for (i = 0; i < 12; i += 2) { - memcpy(&word, ptr + i, sizeof(word)); - sum += ee16(word); - } - for (i = 0; i < (len & ~1u); i += 2) { - memcpy(&word, data + i, sizeof(word)); - sum += ee16(word); - } - if (len & 0x01) { - uint16_t spare = 0; - spare |= (uint16_t)((uint16_t)data[len - 1] << 8); - sum += spare; - } - while (sum >> 16) { - sum = (sum & 0xffff) + (sum >> 16); - } - return (uint16_t)~sum; + uint64_t sum; + + sum = csum_add(0, (const uint8_t *)ph->buf, sizeof(ph->buf)); + sum = csum_add(sum, (const uint8_t *)_data, ee16(ph->ph.len)); + return csum_fold(sum); } static int transport_verify_checksum(union transport_pseudo_header *ph, void *data) @@ -5703,29 +5728,11 @@ static int transport_verify_checksum(union transport_pseudo_header *ph, void *da static uint16_t icmp_checksum(struct wolfIP_icmp_packet *icmp, uint16_t len) { - uint32_t sum = 0; - uint32_t i = 0; - const uint8_t *ptr = (const uint8_t *)(&icmp->type); - uint16_t word; - for (i = 0; i < (len & ~1u); i += 2) { - memcpy(&word, ptr + i, sizeof(word)); - sum += ee16(word); - } - if (len & 0x01) { - uint16_t spare = 0; - spare |= (uint16_t)((uint16_t)ptr[len - 1] << 8); - sum += spare; - } - while (sum >> 16) { - sum = (sum & 0xffff) + (sum >> 16); - } - return (uint16_t)(~sum & 0xFFFF); + return csum_fold(csum_add(0, (const uint8_t *)(&icmp->type), len)); } static void iphdr_set_checksum(struct wolfIP_ip_packet *ip) { - uint32_t sum = 0; - uint32_t i = 0; uint32_t ip_hlen = (uint32_t)(ip->ver_ihl & 0x0fU) << 2; const uint8_t *ptr = (const uint8_t *)(&ip->ver_ihl); @@ -5737,21 +5744,11 @@ static void iphdr_set_checksum(struct wolfIP_ip_packet *ip) * makes the setter idempotent (set/verify always holds) and removes the * implicit "caller must clear csum first" precondition. */ ip->csum = 0; - for (i = 0; i < ip_hlen; i += 2) { - uint16_t word; - memcpy(&word, ptr + i, sizeof(word)); - sum += ee16(word); - } - while (sum >> 16) { - sum = (sum & 0xffff) + (sum >> 16); - } - ip->csum = ee16((uint16_t)~sum); + ip->csum = ee16(csum_fold(csum_add(0, ptr, ip_hlen))); } static int iphdr_verify_checksum(struct wolfIP_ip_packet *ip) { - uint32_t sum = 0; - uint32_t i; uint32_t ip_hlen; const uint8_t *ptr = (const uint8_t *)(&ip->ver_ihl); @@ -5761,14 +5758,7 @@ static int iphdr_verify_checksum(struct wolfIP_ip_packet *ip) if (ip_hlen < IP_HEADER_LEN) return -1; - for (i = 0; i < ip_hlen; i += 2) { - uint16_t word; - memcpy(&word, ptr + i, sizeof(word)); - sum += ee16(word); - } - while (sum >> 16) - sum = (sum & 0xffff) + (sum >> 16); - return ((uint16_t)sum == 0xffffU) ? 0 : -1; + return (csum_fold(csum_add(0, ptr, ip_hlen)) == 0U) ? 0 : -1; } #ifdef ETHERNET