diff --git a/arch/arm/src/imxrt/Kconfig b/arch/arm/src/imxrt/Kconfig index 61a4ea3d20..bb270190aa 100644 --- a/arch/arm/src/imxrt/Kconfig +++ b/arch/arm/src/imxrt/Kconfig @@ -69,6 +69,12 @@ config IMXRT_EDMA default n select ARCH_DMA +config IMXRT_ENET + bool "Ethernet" + default n + select ARCH_HAVE_PHY + select ARCH_HAVE_NETDEV_STATISTICS + menu "FlexIO Peripherals" endmenu # FlexIO Peripherals @@ -179,6 +185,38 @@ config IMXRT_GPIO5_16_31_IRQ endif # IMXRT_GPIO_IRQ +menu "Ethernet Configuration" + depends on IMXRT_ENET + +config MXRT_ENET_NRXBUFFERS + int "Number Rx buffers" + default 6 + +config IMXRT_ENET_NTXBUFFERS + int "Number Tx buffers" + default 2 + +config IMXRT_ENET_ENHANCEDBD + bool # not optional + default n + +config IMXRT_ENET_NETHIFS + int # Not optional + default 1 + +config IMXRT_ENET_PHYINIT + bool "Board-specific PHY Initialization" + default n + ---help--- + Some boards require specialized initialization of the PHY before it + can be used. This may include such things as configuring GPIOs, + resetting the PHY, etc. If CONFIG_IMXRT_ENET_PHYINIT is defined in + the configuration then the board specific logic must provide + imxrt_phy_boardinitialize(); The i.MXRT ENET driver will call this + function one time before it first uses the PHY. + +endmenu # IMXRT_ENET + menu "Memory Configuration" config IMXRT_DTCM diff --git a/arch/arm/src/imxrt/Make.defs b/arch/arm/src/imxrt/Make.defs index 2eea410a04..be72815971 100644 --- a/arch/arm/src/imxrt/Make.defs +++ b/arch/arm/src/imxrt/Make.defs @@ -123,3 +123,7 @@ endif ifeq ($(CONFIG_IMXRT_EDMA),y) CHIP_CSRCS += imxrt_edma.c endif + +ifeq ($(CONFIG_IMXRT_ENET),y) +CHIP_CSRCS += imxrt_enet.c +endif diff --git a/arch/arm/src/imxrt/chip/imxrt105x_pinmux.h b/arch/arm/src/imxrt/chip/imxrt105x_pinmux.h index 405ad79c63..8fba0521b3 100644 --- a/arch/arm/src/imxrt/chip/imxrt105x_pinmux.h +++ b/arch/arm/src/imxrt/chip/imxrt105x_pinmux.h @@ -41,6 +41,7 @@ *****************************************************************************************************/ #include +#include "chip/imxrt_iomuxc.h" /***************************************************************************************************** * Pre-processor Definitions @@ -187,7 +188,8 @@ #define GPIO_ENET_RX_ER_2 (GPIO_PERIPH | GPIO_ALT3 | GPIO_PADMUX(IMXRT_PADMUX_GPIO_EMC_26_INDEX)) #define GPIO_ENET_TDATA00 (GPIO_PERIPH | GPIO_ALT3 | GPIO_PADMUX(IMXRT_PADMUX_GPIO_EMC_22_INDEX)) #define GPIO_ENET_TDATA01 (GPIO_PERIPH | GPIO_ALT3 | GPIO_PADMUX(IMXRT_PADMUX_GPIO_EMC_21_INDEX)) -#define GPIO_ENET_TX_CLK_1 (GPIO_PERIPH | GPIO_ALT6 | GPIO_PADMUX(IMXRT_PADMUX_GPIO_B1_10_INDEX) | \ +#define GPIO_ENET_TX_CLK_1 (GPIO_PERIPH | GPIO_ALT6 | GPIO_SION_ENABLE | \ + GPIO_PADMUX(IMXRT_PADMUX_GPIO_B1_10_INDEX) | \ IOMUX_SLEW_FAST | IOMUX_DRIVE_40OHM | IOMUX_SPEED_LOW | \ IOMUX_PULL_DOWN_100K | IOMUX_PULL_KEEP) #define GPIO_ENET_TX_CLK_2 (GPIO_PERIPH | GPIO_ALT3 | GPIO_PADMUX(IMXRT_PADMUX_GPIO_EMC_25_INDEX)) diff --git a/arch/arm/src/imxrt/chip/imxrt_enet.h b/arch/arm/src/imxrt/chip/imxrt_enet.h new file mode 100644 index 0000000000..1e2ae7db66 --- /dev/null +++ b/arch/arm/src/imxrt/chip/imxrt_enet.h @@ -0,0 +1,669 @@ +/******************************************************************************************** + * arch/arm/src/imxrt/chip/imxrt_enet.h + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ********************************************************************************************/ + +#ifndef __ARCH_ARM_SRC_IMXRT_CHIP_IMXRT_ENET_H +#define __ARCH_ARM_SRC_IMXRT_CHIP_IMXRT_ENET_H + +/******************************************************************************************** + * Included Files + ********************************************************************************************/ + +#include + +#include "chip.h" + +/******************************************************************************************** + * Pre-processor Definitions + ********************************************************************************************/ + +/* Register Offsets *************************************************************************/ + +#define IMXRT_ENET_EIR_OFFSET 0x0004 /* Interrupt Event Register */ +#define IMXRT_ENET_EIMR_OFFSET 0x0008 /* Interrupt Mask Register */ +#define IMXRT_ENET_RDAR_OFFSET 0x0010 /* Receive Descriptor Active Register */ +#define IMXRT_ENET_TDAR_OFFSET 0x0014 /* Transmit Descriptor Active Register */ +#define IMXRT_ENET_ECR_OFFSET 0x0024 /* Ethernet Control Register */ +#define IMXRT_ENET_MMFR_OFFSET 0x0040 /* MII Management Frame Register */ +#define IMXRT_ENET_MSCR_OFFSET 0x0044 /* MII Speed Control Register */ +#define IMXRT_ENET_MIBC_OFFSET 0x0064 /* MIB Control Register */ +#define IMXRT_ENET_RCR_OFFSET 0x0084 /* Receive Control Register */ +#define IMXRT_ENET_TCR_OFFSET 0x00c4 /* Transmit Control Register */ +#define IMXRT_ENET_PALR_OFFSET 0x00e4 /* Physical Address Lower Register */ +#define IMXRT_ENET_PAUR_OFFSET 0x00e8 /* Physical Address Upper Register */ +#define IMXRT_ENET_OPD_OFFSET 0x00ec /* Opcode/Pause Duration Register */ +#define IMXRT_ENET_TXIC_OFFSET 0x00F0 /* Transmit Interrupt Coalescing Register */ +#define IMXRT_ENET_RXIC_OFFSET 0x0100 /* Receive Interrupt Coalescing Register */ +#define IMXRT_ENET_IAUR_OFFSET 0x0118 /* Descriptor Individual Upper Address Register */ +#define IMXRT_ENET_IALR_OFFSET 0x011c /* Descriptor Individual Lower Address Register */ +#define IMXRT_ENET_GAUR_OFFSET 0x0120 /* Descriptor Group Upper Address Register */ +#define IMXRT_ENET_GALR_OFFSET 0x0124 /* Descriptor Group Lower Address Register */ +#define IMXRT_ENET_TFWR_OFFSET 0x0144 /* Transmit FIFO Watermark Register */ +#define IMXRT_ENET_RDSR_OFFSET 0x0180 /* Receive Descriptor Ring Start Register */ +#define IMXRT_ENET_TDSR_OFFSET 0x0184 /* Transmit Buffer Descriptor Ring Start Register */ +#define IMXRT_ENET_MRBR_OFFSET 0x0188 /* Maximum Receive Buffer Size Register */ +#define IMXRT_ENET_RSFL_OFFSET 0x0190 /* Receive FIFO Section Full Threshold */ +#define IMXRT_ENET_RSEM_OFFSET 0x0194 /* Receive FIFO Section Empty Threshold */ +#define IMXRT_ENET_RAEM_OFFSET 0x0198 /* Receive FIFO Almost Empty Threshold */ +#define IMXRT_ENET_RAFL_OFFSET 0x019c /* Receive FIFO Almost Full Threshold */ +#define IMXRT_ENET_TSEM_OFFSET 0x01a0 /* Transmit FIFO Section Empty Threshold */ +#define IMXRT_ENET_TAEM_OFFSET 0x01a4 /* Transmit FIFO Almost Empty Threshold */ +#define IMXRT_ENET_TAFL_OFFSET 0x01a8 /* Transmit FIFO Almost Full Threshold */ +#define IMXRT_ENET_TIPG_OFFSET 0x01ac /* Transmit Inter-Packet Gap */ +#define IMXRT_ENET_FTRL_OFFSET 0x01b0 /* Frame Truncation Length */ +#define IMXRT_ENET_TACC_OFFSET 0x01c0 /* Transmit Accelerator Function Configuration */ +#define IMXRT_ENET_RACC_OFFSET 0x01c4 /* Receive Accelerator Function Configuration */ + +#define IMXRT_ENET_ATCR_OFFSET 0x0400 /* Timer Control Register */ +#define IMXRT_ENET_ATVR_OFFSET 0x0404 /* Timer Value Register */ +#define IMXRT_ENET_ATOFF_OFFSET 0x0408 /* Timer Offset Register */ +#define IMXRT_ENET_ATPER_OFFSET 0x040c /* Timer Period Register */ +#define IMXRT_ENET_ATCOR_OFFSET 0x0410 /* Timer Correction Register */ +#define IMXRT_ENET_ATINC_OFFSET 0x0414 /* Time-Stamping Clock Period Register */ +#define IMXRT_ENET_ATSTMP_OFFSET 0x0418 /* Timestamp of Last Transmitted Frame */ + +#define IMXRT_ENET_TGSR_OFFSET 0x0604 /* Timer Global Status Register */ +#define IMXRT_ENET_TCSR0_OFFSET 0x0608 /* Timer Control Status Register */ +#define IMXRT_ENET_TCCR0_OFFSET 0x060c /* Timer Compare Capture Register */ +#define IMXRT_ENET_TCSR1_OFFSET 0x0610 /* Timer Control Status Register */ +#define IMXRT_ENET_TCCR1_OFFSET 0x0614 /* Timer Compare Capture Register */ +#define IMXRT_ENET_TCSR2_OFFSET 0x0618 /* Timer Control Status Register */ +#define IMXRT_ENET_TCCR2_OFFSET 0x061c /* Timer Compare Capture Register */ +#define IMXRT_ENET_TCSR3_OFFSET 0x0620 /* Timer Control Status Register */ +#define IMXRT_ENET_TCCR3_OFFSET 0x0624 /* Timer Compare Capture Register */ + +/* Register Addresses ***********************************************************************/ + +#define IMXRT_ENET_EIR (IMXRT_ENET_BASE+IMXRT_ENET_EIR_OFFSET) +#define IMXRT_ENET_EIMR (IMXRT_ENET_BASE+IMXRT_ENET_EIMR_OFFSET) +#define IMXRT_ENET_RDAR (IMXRT_ENET_BASE+IMXRT_ENET_RDAR_OFFSET) +#define IMXRT_ENET_TDAR (IMXRT_ENET_BASE+IMXRT_ENET_TDAR_OFFSET) +#define IMXRT_ENET_ECR (IMXRT_ENET_BASE+IMXRT_ENET_ECR_OFFSET) +#define IMXRT_ENET_MMFR (IMXRT_ENET_BASE+IMXRT_ENET_MMFR_OFFSET) +#define IMXRT_ENET_MSCR (IMXRT_ENET_BASE+IMXRT_ENET_MSCR_OFFSET) +#define IMXRT_ENET_MIBC (IMXRT_ENET_BASE+IMXRT_ENET_MIBC_OFFSET) +#define IMXRT_ENET_RCR (IMXRT_ENET_BASE+IMXRT_ENET_RCR_OFFSET) +#define IMXRT_ENET_TCR (IMXRT_ENET_BASE+IMXRT_ENET_TCR_OFFSET) +#define IMXRT_ENET_PALR (IMXRT_ENET_BASE+IMXRT_ENET_PALR_OFFSET) +#define IMXRT_ENET_PAUR (IMXRT_ENET_BASE+IMXRT_ENET_PAUR_OFFSET) +#define IMXRT_ENET_OPD (IMXRT_ENET_BASE+IMXRT_ENET_OPD_OFFSET) +#define IMXRT_ENET_IAUR (IMXRT_ENET_BASE+IMXRT_ENET_IAUR_OFFSET) +#define IMXRT_ENET_IALR (IMXRT_ENET_BASE+IMXRT_ENET_IALR_OFFSET) +#define IMXRT_ENET_GAUR (IMXRT_ENET_BASE+IMXRT_ENET_GAUR_OFFSET) +#define IMXRT_ENET_GALR (IMXRT_ENET_BASE+IMXRT_ENET_GALR_OFFSET) +#define IMXRT_ENET_TFWR (IMXRT_ENET_BASE+IMXRT_ENET_TFWR_OFFSET) +#define IMXRT_ENET_RDSR (IMXRT_ENET_BASE+IMXRT_ENET_RDSR_OFFSET) +#define IMXRT_ENET_TDSR (IMXRT_ENET_BASE+IMXRT_ENET_TDSR_OFFSET) +#define IMXRT_ENET_MRBR (IMXRT_ENET_BASE+IMXRT_ENET_MRBR_OFFSET) +#define IMXRT_ENET_RSFL (IMXRT_ENET_BASE+IMXRT_ENET_RSFL_OFFSET) +#define IMXRT_ENET_RSEM (IMXRT_ENET_BASE+IMXRT_ENET_RSEM_OFFSET) +#define IMXRT_ENET_RAEM (IMXRT_ENET_BASE+IMXRT_ENET_RAEM_OFFSET) +#define IMXRT_ENET_RAFL (IMXRT_ENET_BASE+IMXRT_ENET_RAFL_OFFSET) +#define IMXRT_ENET_TSEM (IMXRT_ENET_BASE+IMXRT_ENET_TSEM_OFFSET) +#define IMXRT_ENET_TAEM (IMXRT_ENET_BASE+IMXRT_ENET_TAEM_OFFSET) +#define IMXRT_ENET_TAFL (IMXRT_ENET_BASE+IMXRT_ENET_TAFL_OFFSET) +#define IMXRT_ENET_TIPG (IMXRT_ENET_BASE+IMXRT_ENET_TIPG_OFFSET) +#define IMXRT_ENET_FTRL (IMXRT_ENET_BASE+IMXRT_ENET_FTRL_OFFSET) +#define IMXRT_ENET_TACC (IMXRT_ENET_BASE+IMXRT_ENET_TACC_OFFSET) +#define IMXRT_ENET_RACC (IMXRT_ENET_BASE+IMXRT_ENET_RACC_OFFSET) + +#define IMXRT_ENET_ATCR (IMXRT_ENET_BASE+IMXRT_ENET_ATCR_OFFSET) +#define IMXRT_ENET_ATVR (IMXRT_ENET_BASE+IMXRT_ENET_ATVR_OFFSET) +#define IMXRT_ENET_ATOFF (IMXRT_ENET_BASE+IMXRT_ENET_ATOFF_OFFSET) +#define IMXRT_ENET_ATPER (IMXRT_ENET_BASE+IMXRT_ENET_ATPER_OFFSET) +#define IMXRT_ENET_ATCOR (IMXRT_ENET_BASE+IMXRT_ENET_ATCOR_OFFSET) +#define IMXRT_ENET_ATINC (IMXRT_ENET_BASE+IMXRT_ENET_ATINC_OFFSET) +#define IMXRT_ENET_ATSTMP (IMXRT_ENET_BASE+IMXRT_ENET_ATSTMP_OFFSET) + +#define IMXRT_ENET_TGSR (IMXRT_ENET_BASE+IMXRT_ENET_TGSR_OFFSET) +#define IMXRT_ENET_TCSR0 (IMXRT_ENET_BASE+IMXRT_ENET_TCSR0_OFFSET) +#define IMXRT_ENET_TCCR0 (IMXRT_ENET_BASE+IMXRT_ENET_TCCR0_OFFSET) +#define IMXRT_ENET_TCSR1 (IMXRT_ENET_BASE+IMXRT_ENET_TCSR1_OFFSET) +#define IMXRT_ENET_TCCR1 (IMXRT_ENET_BASE+IMXRT_ENET_TCCR1_OFFSET) +#define IMXRT_ENET_TCSR2 (IMXRT_ENET_BASE+IMXRT_ENET_TCSR2_OFFSET) +#define IMXRT_ENET_TCCR2 (IMXRT_ENET_BASE+IMXRT_ENET_TCCR2_OFFSET) +#define IMXRT_ENET_TCSR3 (IMXRT_ENET_BASE+IMXRT_ENET_TCSR3_OFFSET) +#define IMXRT_ENET_TCCR3 (IMXRT_ENET_BASE+IMXRT_ENET_TCCR3_OFFSET) + +/* Register Bit Definitions *****************************************************************/ + +/* Interrupt Event Register, Interrupt Mask Register */ + /* Bits 0-14: Reserved */ +#define ENET_INT_TS_TIMER (1 << 15) /* Bit 15: Timestamp timer */ +#define ENET_INT_TS_AVAIL (1 << 16) /* Bit 16: Transmit timestamp available */ +#define ENET_INT_WAKEUP (1 << 17) /* Bit 17: Node wake-up request indication */ +#define ENET_INT_PLR (1 << 18) /* Bit 18: Payload receive error */ +#define ENET_INT_UN (1 << 19) /* Bit 19: Transmit FIFO underrun */ +#define ENET_INT_RL (1 << 20) /* Bit 20: Collision Retry Limit */ +#define ENET_INT_LC (1 << 21) /* Bit 21: Late Collision */ +#define ENET_INT_EBERR (1 << 22) /* Bit 22: Ethernet Bus Error */ +#define ENET_INT_MII (1 << 23) /* Bit 23: MII Interrupt */ +#define ENET_INT_RXB (1 << 24) /* Bit 24: Receive Buffer Interrupt */ +#define ENET_INT_RXF (1 << 25) /* Bit 25: Receive Frame Interrupt */ +#define ENET_INT_TXB (1 << 26) /* Bit 26: Transmit Buffer Interrupt */ +#define ENET_INT_TXF (1 << 27) /* Bit 27: Transmit Frame Interrupt */ +#define ENET_INT_GRA (1 << 28) /* Bit 28: Graceful Stop Complete */ +#define ENET_INT_BABT (1 << 29) /* Bit 29: Babbling Transmit Error */ +#define ENET_INT_BABR (1 << 30) /* Bit 30: Babbling Receive Error */ + /* Bit 31: Reserved */ +/* Receive Descriptor Active Register */ + /* Bits 0-23: Reserved */ +#define ENET_RDAR (1 << 24) /* Bit 24: Receive descriptor active */ + /* Bits 25-31: Reserved */ +/* Transmit Descriptor Active Register */ + /* Bits 0-23: Reserved */ +#define ENET_TDAR (1 << 24) /* Bit 24: Transmit descriptor active */ + /* Bits 25-31: Reserved */ +/* Ethernet Control Register */ + +#define ENET_ECR_RESET (1 << 0) /* Bit 0: Ethernet MAC reset */ +#define ENET_ECR_ETHEREN (1 << 1) /* Bit 1: Ethernet enable */ +#define ENET_ECR_MAGICEN (1 << 2) /* Bit 2: Magic packet detection enable */ +#define ENET_ECR_SLEEP (1 << 3) /* Bit 3: Sleep mode enable */ +#define ENET_ECR_EN1588 (1 << 4) /* Bit 4: EN1588 enable */ + /* Bit 5: Reserved */ +#define ENET_ECR_DBGEN (1 << 6) /* Bit 6: Debug enable */ +#define ENET_ECR_STOPEN (1 << 7) /* Bit 7: STOPEN Signal Control */ +#ifdef IMXRT_ENET_HAS_DBSWAP +#define ENET_ECR_DBSWP (1 << 8) /* Bit 8: Swap bytes */ +#endif + /* Bits 9-31: Reserved */ +/* MII Management Frame Register */ + +#define ENET_MMFR_DATA_SHIFT (0) /* Bits 0-15: Management frame data */ +#define ENET_MMFR_DATA_MASK (0xffff << ENET_MMFR_DATA_SHIFT) +#define ENET_MMFR_TA_SHIFT (16) /* Bits 16-17: Turn around */ +#define ENET_MMFR_TA_MASK (3 << ENET_MMFR_TA_SHIFT) +#define ENET_MMFR_RA_SHIFT (18) /* Bits 18-22: Register address */ +#define ENET_MMFR_RA_MASK (31 << ENET_MMFR_RA_SHIFT) +#define ENET_MMFR_PA_SHIFT (23) /* Bits 23-27: PHY address */ +#define ENET_MMFR_PA_MASK (31 << ENET_MMFR_PA_SHIFT) +#define ENET_MMFR_OP_SHIFT (28) /* Bits 28-29: Operation code */ +#define ENET_MMFR_OP_MASK (3 << ENET_MMFR_OP_SHIFT) +# define ENET_MMFR_OP_WRNOTMII (0 << ENET_MMFR_OP_SHIFT) /* Write frame, not MII compliant */ +# define ENET_MMFR_OP_WRMII (1 << ENET_MMFR_OP_SHIFT) /* Write frame, MII management frame */ +# define ENET_MMFR_OP_RDMII (2 << ENET_MMFR_OP_SHIFT) /* Read frame, MII management frame */ +# define ENET_MMFR_OP_RdNOTMII (3 << ENET_MMFR_OP_SHIFT) /* Read frame, not MII compliant */ +#define ENET_MMFR_ST_SHIFT (30) /* Bits 30-31: Start of frame delimiter */ +#define ENET_MMFR_ST_MASK (3 << ENET_MMFR_ST_SHIFT) + +/* MII Speed Control Register */ + /* Bit 0: Reserved */ +#define ENET_MSCR_MII_SPEED_SHIFT (1) /* Bits 1-6: MII speed */ +#define ENET_MSCR_MII_SPEED_MASK (63 << ENET_MSCR_MII_SPEED_SHIFT) +#define ENET_MSCR_DIS_PRE (1 << 7) /* Bit 7: Disable preamble */ +#define ENET_MSCR_HOLDTIME_SHIFT (8) /* Bits 8-10: Holdtime on MDIO output */ +#define ENET_MSCR_HOLDTIME_MASK (7 << ENET_MSCR_HOLDTIME_SHIFT) +# define ENET_MSCR_HOLDTIME_1CYCLE (0 << ENET_MSCR_HOLDTIME_SHIFT) /* 1 internal module clock cycle */ +# define ENET_MSCR_HOLDTIME_2CYCLES (1 << ENET_MSCR_HOLDTIME_SHIFT) /* 2 internal module clock cycles */ +# define ENET_MSCR_HOLDTIME_3CYCLES (2 << ENET_MSCR_HOLDTIME_SHIFT) /* 3 internal module clock cycles */ +# define ENET_MSCR_HOLDTIME_8CYCLES (7 << ENET_MSCR_HOLDTIME_SHIFT) /* 8 internal module clock cycles */ + /* Bits 11-31: Reserved */ +/* MIB Control Register */ + /* Bits 0-28: Reserved */ +#define ENET_MIBC_MIB_CLEAR (1 << 29) /* Bit 29: MIB clear */ +#define ENET_MIBC_MIB_IDLE (1 << 30) /* Bit 30: MIB idle */ +#define ENET_MIBC_MIB_DIS (1 << 31) /* Bit 31: Disable MIB logic */ + +/* Receive Control Register */ + +#define ENET_RCR_LOOP (1 << 0) /* Bit 0: Internal loopback */ +#define ENET_RCR_DRT (1 << 1) /* Bit 1: Disable receive on transmit */ +#define ENET_RCR_MII_MODE (1 << 2) /* Bit 2: Media independent interface mode */ +#define ENET_RCR_PROM (1 << 3) /* Bit 3: Promiscuous mode */ +#define ENET_RCR_BC_REJ (1 << 4) /* Bit 4: Broadcast frame reject */ +#define ENET_RCR_FCE (1 << 5) /* Bit 5: Flow control enable */ + /* Bits 6-7: Reserved */ +#define ENET_RCR_RMII_MODE (1 << 8) /* Bit 8: RMII mode enable */ +#define ENET_RCR_RMII_10T (1 << 9) /* Bit 9: Enables 10-Mbps mode of the RMII */ + /* Bits 10-11: Reserved */ +#define ENET_RCR_PADEN (1 << 12) /* Bit 12: Enable frame padding remove on receive */ +#define ENET_RCR_PAUFWD (1 << 13) /* Bit 13: Terminate/forward pause frames */ +#define ENET_RCR_CRCFWD (1 << 14) /* Bit 14: Terminate/forward received CRC */ +#define ENET_RCR_CFEN (1 << 15) /* Bit 15: MAC control frame enable */ +#define ENET_RCR_MAX_FL_SHIFT (16) /* Bits 16-29: Maximum frame length */ +#define ENET_RCR_MAX_FL_MASK (0x3fff << ENET_RCR_MAX_FL_SHIFT) +#define ENET_RCR_NLC (1 << 30) /* Bit 30: Payload length check disable */ +#define ENET_RCR_GRS (1 << 31) /* Bit 31: Graceful receive stopped */ + +/* Transmit Control Register */ + +#define ENET_TCR_GTS (1 << 0) /* Bit 0: Graceful transmit stop */ + /* Bit 1: Reserved */ +#define ENET_TCR_ADDINS (1 << 8) /* Bit 8: Set MAC address on transmit */ +#define ENET_TCR_FDEN (1 << 2) /* Bit 2: Full duplex enable */ +#define ENET_TCR_TFC_PAUSE (1 << 3) /* Bit 3: Transmit frame control pause */ +#define ENET_TCR_RFC_PAUSE (1 << 4) /* Bit 4: Receive frame control pause */ +#define ENET_TCR_ADDSEL_SHIFT (5) /* Bits 5-7: Source MAC address select on transmit */ +#define ENET_TCR_ADDSEL_MASK (7 << ENET_TCR_ADDSEL_SHIFT) +# define ENET_TCR_ADDSEL_PADDR12 (0 << ENET_TCR_ADDSEL_SHIFT) /* Node MAC address programmed on PADDR1/2 registers */ +#define ENET_TCR_CRCFWD (1 << 9) /* Bit 9: Forward frame from application with CRC */ + /* Bits 10-31: Reserved */ +/* Physical Address Lower/Upper Register (32-bits of 48-address) */ +/* Physical Address Upper Register */ + +#define ENET_PAUR_TYPE_SHIFT (0) /* Bits 0-15: Type field in PAUSE frame */ +#define ENET_PAUR_TYPE_MASK (0xffff << ENET_PAUR_TYPE_MASK) +#define ENET_PAUR_PADDR2_SHIFT (16) /* Bits 16-31: Bytes 4 and 5 of the 6-byte address */ +#define ENET_PAUR_PADDR2_MASK (0xffff << ENET_PAUR_PADDR2_SHIFT) + +/* Opcode/Pause Duration Register */ + +#define ENET_OPD_PAUSE_DUR_SHIFT (0) /* Bits 0-15: Pause duration */ +#define ENET_OPD_PAUSE_DUR_MASK (0xffff << ENET_OPD_PAUSE_DUR_SHIFT) +#define ENET_OPD_OPCODE_SHIFT (16) /* Bits 16-31: Opcode field in PAUSE frames */ +#define ENET_OPD_OPCODE_MASK (0xffff << ENET_OPD_OPCODE_SHIFT) + +/* Descriptor Individual Uupper/Lower Address Register (64-bit address in two 32-bit registers) */ +/* Descriptor Group Upper/Lower Address Register (64-bit address in two 32-bit registers) */ + +/* Transmit Interrupt Coalescing Register */ +#define ENET_TXIC_ICTT_SHIFT (0) /* Bits 0-15: Interrupt coalescing timer threshold */ +#define ENET_TXIC_ICTT_SHIFT_MASK (0xffff << ENET_TXIC_ICTT_SHIFT) + /* Bits 16-19: Reserved */ +#define ENET_TXIC_ICFT_SHIFT (20) /* Bits 0-15: Interrupt coalescing timer threshold */ +#define ENET_TXIC_ICFT_SHIFT_MASK (0xff << ENET_TXIC_ICFT_SHIFT) +#define ENET_TXIC_ICTT_ICCS (1 << 30) /* Bit 30: Interrupt Coalescing Timer Clock Source Select */ +#define ENET_TXIC_ICTT_ICEN (1 << 31) /* Bit 31: Eable/disabel Interrupt Coalescing */ + +/* Receive Interrupt Coalescing Register */ +#define ENET_RXIC_ICTT_SHIFT (0) /* Bits 0-15: Interrupt coalescing timer threshold */ +#define ENET_RXIC_ICTT_SHIFT_MASK (0xffff << ENET_TXIC_ICTT_SHIFT) + /* Bits 16-19: Reserved */ +#define ENET_RXIC_ICFT_SHIFT (20) /* Bits 0-15: Interrupt coalescing timer threshold */ +#define ENET_RXIC_ICFT_SHIFT_MASK (0xff << ENET_TXIC_ICFT_SHIFT) +#define ENET_RXIC_ICTT_ICCS (1 << 30) /* Bit 30: Interrupt Coalescing Timer Clock Source Select */ +#define ENET_RXIC_ICTT_ICEN (1 << 31) /* Bit 31: Eable/disabel Interrupt Coalescing */ + +/* Transmit FIFO Watermark Register */ + +#define ENET_TFWR_TFWR_SHIFT (0) /* Bits 0-5: Transmit FIFO write */ + /* Bits 6-7: Reserved */ +#define ENET_TFWR_TFWR_MASK (63 << ENET_TFWR_TFWR_SHIFT) +#define ENET_TFWR_STRFWD (1 << 8) /* Bit 8: Store and forward enable */ + /* Bits 9-31: Reserved */ +/* Receive Descriptor Ring Start Register */ + /* Bits 0-2: Reserved */ +#define ENET_RDSR_SHIFT (3) /* Bits 3-31: Start of the receive buffer descriptor queue */ +#define ENET_RDSR_MASK (0xfffffff8) + +/* Transmit Buffer Descriptor Ring Start Register */ + /* Bits 0-2: Reserved */ +#define ENET_TDSR_SHIFT (3) /* Bits 3-31: Start of the transmit buffer descriptor queue */ +#define ENET_TDSR_MASK (0xfffffff8) + +/* Maximum Receive Buffer Size Register */ + /* Bits 14-31: Reserved */ +#define ENET_MRBR_SHIFT (4) /* Bits 4-13: Receive buffer size in bytes */ +#define ENET_MRBR_MASK (0x3ff << ENET_MRBR_SHIFT) + /* Bits 0-3: Reserved */ +/* Receive FIFO Section Full Threshold */ + /* Bits 8-31: Reserved */ +#define ENET_RSFL_SHIFT (0) /* Bits 0-7: Value of receive FIFO section full threshold */ +#define ENET_RSFL_MASK (0xff << ENET_RSFL_SHIFT) + +/* Receive FIFO Section Empty Threshold */ + +#define ENET_RSEM_SHIFT (0) /* Bits 0-7: Value of the receive FIFO section empty threshold */ +#define ENET_RSEM_MASK (0xff << ENET_RSEM_SHIFT) + /* Bits 8-31: Reserved */ +/* Receive FIFO Almost Empty Threshold */ + +#define ENET_RAEM_SHIFT (0) /* Bits 0-7: Value of the receive FIFO almost empty threshold */ +#define ENET_RAEM_MASK (0xff << ENET_RAEM_SHIFT) + /* Bits 8-31: Reserved */ +/* Receive FIFO Almost Full Threshold */ + +#define ENET_RAFL_SHIFT (0) /* Bits 0-7: Value of the receive FIFO almost full threshold */ +#define ENET_RAFL_MASK (0xff << ENET_RAFL_SHIFT) + /* Bits 8-31: Reserved */ +/* Transmit FIFO Section Empty Threshold */ + +#define ENET_TSEM_SHIFT (0) /* Bits 0-7: Value of the transmit FIFO section empty threshold */ +#define ENET_TSEM_MASK (0xff << ENET_TSEM_SHIFT) + /* Bits 8-31: Reserved */ +/* Transmit FIFO Almost Empty Threshold */ + +#define ENET_TAEM_SHIFT (0) /* Bits 0-7: Value of the transmit FIFO section empty threshold */ +#define ENET_TAEM_MASK (0xff << ENET_TAEM_SHIFT) + /* Bits 8-31: Reserved */ +/* Transmit FIFO Almost Full Threshold */ + +#define ENET_TAFL_SHIFT (0) /* Bits 0-7: Value of the transmit FIFO section empty threshold */ +#define ENET_TAFL_MASK (0xff << ENET_TAFL_SHIFT) + /* Bits 8-31: Reserved */ +/* Transmit Inter-Packet Gap */ + +#define ENET_TIPG_SHIFT (0) /* Bits 0-4: Value of the transmit FIFO section empty threshold */ +#define ENET_TIPG_MASK (31 << ENET_TIPG_SHIFT) + /* Bits 5-31: Reserved */ +/* Frame Truncation Length */ + +#define ENET_FTRL_SHIFT (0) /* Bits 0-13: Value of the transmit FIFO section empty threshold */ +#define ENET_FTRL_MASK (0x3fff << ENET_FTRL_SHIFT) + /* Bits 14-31: Reserved */ +/* Transmit Accelerator Function Configuration */ + +#define ENET_TACC_SHIFT16 (1 << 0) /* Bit 0: TX FIFO shift-16 */ + /* Bits 1-2: Reserved */ +#define ENET_TACC_IPCHK (1 << 3) /* Bit 3: Enables insertion of IP header checksum */ +#define ENET_TACC_PROCHK (1 << 4) /* Bit 4: Enables insertion of protocol checksum */ + /* Bits 5-31: Reserved */ +/* Receive Accelerator Function Configuration */ + +#define ENET_RACC_PADREM (1 << 0) /* Bit 0: Enable padding removal for short IP frames */ +#define ENET_RACC_IPDIS (1 << 1) /* Bit 1: Enable discard of frames with wrong IPv4 header checksum */ +#define ENET_RACC_PRODIS (1 << 2) /* Bit 2: Enable discard of frames with wrong protocol checksum */ + /* Bits 3-5: Reserved */ +#define ENET_RACC_LINEDIS (1 << 6) /* Bit 6: Enable discard of frames with MAC layer errors */ +#define ENET_RACC_SHIFT16 (1 << 7) /* Bit 7: RX FIFO shift-16 */ + /* Bits 8-31: Reserved */ +/* Timer Control Register */ + +#define ENET_ATCR_EN (1 << 0) /* Bit 0: Enable timer */ + /* Bit 1: Reserved */ +#define ENET_ATCR_OFFEN (1 << 2) /* Bit 2: Enable one-shot offset event */ +#define ENET_ATCR_OFFRST (1 << 3) /* Bit 3: Reset timer on offset event */ +#define ENET_ATCR_PEREN (1 << 4) /* Bit 4: Enable periodical event */ + /* Bits 5-6: Reserved */ +#define ENET_ATCR_PINPER (1 << 7) /* Bit 7: Enables event signal output assertion on period event */ + /* Bit 8: Reserved */ +#define ENET_ATCR_RESTART (1 << 9) /* Bit 9: Reset timer */ + /* Bit 10: Reserved */ +#define ENET_ATCR_CAPTURE (1 << 11) /* Bit 11: Capture timer value */ + /* Bit 12: Reserved */ +#define ENET_ATCR_SLAVE (1 << 13) /* Bit 13: Enable timer slave mode */ + /* Bits 14-31: Reserved */ +/* Timer Value Register (32-bit timer value) */ +/* Timer Offset Register (32-bit offset value) */ +/* Timer Period Register (32-bit timer period) */ + +/* Timer Correction Register */ + +#define ENET_ATCOR_MASK (0x7fffffff) /* Bits 0-3: Correction counter wrap-around value */ + /* Bit 31: Reserved */ +/* Time-Stamping Clock Period Register */ + +#define ENET_ATINC_INC_SHIFT (0) /* Bits 0-6: Clock period of the timestamping clock (ts_clk) in nanoseconds */ +#define ENET_ATINC_INC_MASK (0x7f << ENET_ATINC_INC_SHIFT) + /* Bit 7: Reserved */ +#define ENET_ATINC_INC_CORR_SHIFT (8) /* Bits 8-14: Correction increment value */ +#define ENET_ATINC_INC_CORR_MASK (0x7f << ENET_ATINC_INC_CORR_SHIFT) + /* Bits 15-31: Reserved */ +/* Timestamp of Last Transmitted Frame (32-bit timestamp) */ + +/* Timer Global Status Register */ + +#define ENET_TGSR_TF0 (1 << 0) /* Bit 0: Copy of Timer Flag for channel 0 */ +#define ENET_TGSR_TF1 (1 << 1) /* Bit 1: Copy of Timer Flag for channel 1 */ +#define ENET_TGSR_TF2 (1 << 2) /* Bit 2: Copy of Timer Flag for channel 2 */ +#define ENET_TGSR_TF3 (1 << 3) /* Bit 3: Copy of Timer Flag for channel 3 */ + /* Bits 14-31: Reserved */ +/* Timer Control Status Register n */ + +#define ENET_TCSR_TDRE (1 << 0) /* Bit 0: Timer DMA Request Enable */ + /* Bit 1: Reserved */ +#define ENET_TCSR_TMODE_SHIFT (2) /* Bits 2-5: Timer Mode */ +#define ENET_TCSR_TMODE_MASK (15 << ENET_TCSR_TMODE_SHIFT) +# define ENET_TCSR_TMODE_DISABLED (0 << ENET_TCSR_TMODE_SHIFT) /* Disabled */ +# define ENET_TCSR_TMODE_ICRISING (1 << ENET_TCSR_TMODE_SHIFT) /* Input Capture on rising edge */ +# define ENET_TCSR_TMODE_ICFALLLING (2 << ENET_TCSR_TMODE_SHIFT) /* Input Capture on falling edge */ +# define ENET_TCSR_TMODE_ICBOTH (3 << ENET_TCSR_TMODE_SHIFT) /* Input Capture on both edges */ +# define ENET_TCSR_TMODE_OCSW (4 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, S/W only */ +# define ENET_TCSR_TMODE_OCTOGGLE (5 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, toggle on compare */ +# define ENET_TCSR_TMODE_OCCLR (6 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, clear on compare */ +# define ENET_TCSR_TMODE_OCSET (7 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, set on compare */ +# define ENET_TCSR_TMODE_OCSETCLR (9 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, set on compare, clear on overflow */ +# define ENET_TCSR_TMODE_OCCLRSET (10 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, clear on compare, set on overflow */ +# define ENET_TCSR_TMODE_PCPULSEL (14 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, pulse low on compare */ +# define ENET_TCSR_TMODE_PCPULSEH (15 << ENET_TCSR_TMODE_SHIFT) /* Output Compare, pulse high on compare */ +#define ENET_TCSR_TIE (1 << 6) /* Bit 6: Timer interrupt enable */ +#define ENET_TCSR_TF (1 << 7) /* Bit 7: Timer Flag */ + /* Bits 8-31: Reserved */ +/* Timer Compare Capture Register (32-bit compare value) */ + +/* Buffer Descriptors ***********************************************************************/ +/* Endian-independent descriptor offsets */ + +#define DESC_STATUS1_OFFSET (0) +#define DESC_LENGTH_OFFSET (2) +#define DESC_DATAPTR_OFFSET (4) +#define DESC_LEGACY_LEN (8) + +#define DESC_STATUS2_OFFSET (8) +#define DESC_LENPROTO_OFFSET (12) +#define DESC_CHECKSUM_OFFSET (14) +#define DESC_BDU_OFFSET (16) +#define DESC_TIMESTAMP_OFFSET (20) +#define DESC_ENHANCED_LEN (32) + +/* Legacy/Common TX Buffer Descriptor Bit Definitions. + * + * The descriptors are represented by structures Unfortunately, when the + * structures are overlayed on the data, the bytes are reversed because + * the underlying hardware writes the data in big-endian byte order. + */ + +#ifdef IMXRT_ENET_HAS_DBSWAP +# ifndef CONFIG_ENDIAN_BIG +# define IMXRT_USE_DBSWAP +# endif +#else +# ifndef CONFIG_ENDIAN_BIG +# define IMXRT_BUFFERS_SWAP +# endif +#endif + +#ifndef IMXRT_BUFFERS_SWAP +# define TXDESC_ABC (1 << 9) /* Legacy */ +# define TXDESC_TC (1 << 10) /* Common */ +# define TXDESC_L (1 << 11) /* Common */ +# define TXDESC_TO2 (1 << 12) /* Common */ +# define TXDESC_W (1 << 13) /* Common */ +# define TXDESC_TO1 (1 << 14) /* Common */ +# define TXDESC_R (1 << 15) /* Common */ +#else +# define TXDESC_ABC (1 << 1) /* Legacy */ +# define TXDESC_TC (1 << 2) /* Common */ +# define TXDESC_L (1 << 3) /* Common */ +# define TXDESC_TO2 (1 << 4) /* Common */ +# define TXDESC_W (1 << 5) /* Common */ +# define TXDESC_TO1 (1 << 6) /* Common */ +# define TXDESC_R (1 << 7) /* Common */ +#endif + +/* Enhanced (only) TX Buffer Descriptor Bit Definitions */ + +#ifndef IMXRT_BUFFERS_SWAP +# define TXDESC_TSE (1 << 8) +# define TXDESC_OE (1 << 9) +# define TXDESC_LCE (1 << 10) +# define TXDESC_FE (1 << 11) +# define TXDESC_EE (1 << 12) +# define TXDESC_UE (1 << 13) +# define TXDESC_TXE (1 << 15) + +# define TXDESC_IINS (1 << 27) +# define TXDESC_PINS (1 << 28) +# define TXDESC_TS (1 << 29) +# define TXDESC_INT (1 << 30) + +# define TXDESC_BDU (1 << 31) +#else +# define TXDESC_IINS (1 << 3) +# define TXDESC_PINS (1 << 4) +# define TXDESC_TS (1 << 5) +# define TXDESC_INT (1 << 6) + +# define TXDESC_TSE (1 << 16) +# define TXDESC_OE (1 << 17) +# define TXDESC_LCE (1 << 18) +# define TXDESC_FE (1 << 19) +# define TXDESC_EE (1 << 20) +# define TXDESC_UE (1 << 21) +# define TXDESC_TXE (1 << 23) + +# define TXDESC_BDU (1 << 7) +#endif + +/* Legacy (and Common) RX Buffer Descriptor Bit Definitions */ + +#ifndef IMXRT_BUFFERS_SWAP +# define RXDESC_TR (1 << 0) +# define RXDESC_OV (1 << 1) +# define RXDESC_CR (1 << 2) +# define RXDESC_NO (1 << 4) +# define RXDESC_LG (1 << 5) +# define RXDESC_MC (1 << 6) +# define RXDESC_BC (1 << 7) +# define RXDESC_M (1 << 8) +# define RXDESC_L (1 << 11) +# define RXDESC_R02 (1 << 12) +# define RXDESC_W (1 << 13) +# define RXDESC_R01 (1 << 14) +# define RXDESC_E (1 << 15) +#else +# define RXDESC_M (1 << 0) +# define RXDESC_L (1 << 3) +# define RXDESC_R02 (1 << 4) +# define RXDESC_W (1 << 5) +# define RXDESC_R01 (1 << 6) +# define RXDESC_E (1 << 7) +# define RXDESC_TR (1 << 8) +# define RXDESC_OV (1 << 9) +# define RXDESC_CR (1 << 10) +# define RXDESC_NO (1 << 12) +# define RXDESC_LG (1 << 13) +# define RXDESC_MC (1 << 14) +# define RXDESC_BC (1 << 15) +#endif + +/* Enhanced (only) TX Buffer Descriptor Bit Definitions */ + +#ifndef IMXRT_BUFFERS_SWAP +# define RXDESC_FRAG (1 << 0) +# define RXDESC_IPV6 (1 << 1) +# define RXDESC_VLAN (1 << 2) +# define RXDESC_PCR (1 << 4) +# define RXDESC_ICE (1 << 5) +# define RXDESC_INT (1 << 23) +# define RXDESC_UC (1 << 24) +# define RXDESC_CE (1 << 25) +# define RXDESC_PE (1 << 26) +# define RXDESC_ME (1 << 31) + +# define RXDESC_BDU (1 << 31) +#else +# define RXDESC_UC (1 << 0) +# define RXDESC_CE (1 << 1) +# define RXDESC_PE (1 << 2) +# define RXDESC_ME (1 << 7) +# define RXDESC_INT (1 << 15) +# define RXDESC_FRAG (1 << 24) +# define RXDESC_IPV6 (1 << 25) +# define RXDESC_VLAN (1 << 26) +# define RXDESC_PCR (1 << 28) +# define RXDESC_ICE (1 << 29) + +# define RXDESC_BDU (1 << 7) +#endif + +/******************************************************************************************** + * Public Types + ********************************************************************************************/ +/* Buffer Descriptors ***********************************************************************/ +/* Legacy Buffer Descriptor */ + +#ifdef CONFIG_ENET_ENHANCEDBD +#ifdef IMXRT_USE_DBSWAP +/* When DBSWP is used to swap the bytes in hardware, it is done 32-bits + * at a time. Therefore, all 16 bit elements need to be swapped to + * compensate. + */ + +struct enet_desc_s +{ + uint16_t length; /* Data length */ + uint16_t status1; /* Control and status */ + uint8_t *data; /* Buffer address */ + uint32_t status2; /* Extended status */ + uint16_t checksum; /* Payload checksum */ + uint16_t lenproto; /* Header length + Protocol type */ + uint32_t bdu; /* BDU */ + uint32_t timestamp; /* Time stamp */ + uint32_t reserved1; /* unused */ + uint32_t reserved2; /* unused */ +}; +#else +struct enet_desc_s +{ + uint16_t status1; /* Control and status */ + uint16_t length; /* Data length */ + uint8_t *data; /* Buffer address */ + uint32_t status2; /* Extended status */ + uint16_t lenproto; /* Header length + Protocol type */ + uint16_t checksum; /* Payload checksum */ + uint32_t bdu; /* BDU */ + uint32_t timestamp; /* Time stamp */ + uint32_t reserved1; /* unused */ + uint32_t reserved2; /* unused */ +}; +#endif /* IMXRT_USE_DBSWAP */ +#else /* CONFIG_ENET_ENHANCEDBD */ +#ifdef IMXRT_USE_DBSWAP +struct enet_desc_s +{ + uint16_t length; /* Data length */ + uint16_t status1; /* Control and status */ + uint8_t *data; /* Buffer address */ +}; +#else +struct enet_desc_s +{ + uint16_t status1; /* Control and status */ + uint16_t length; /* Data length */ + uint8_t *data; /* Buffer address */ +}; +#endif /* IMXRT_USE_DBSWAP */ +#endif /* CONFIG_ENET_ENHANCEDBD */ + +/******************************************************************************************** + * Public Data + ********************************************************************************************/ + +/******************************************************************************************** + * Public Functions + ********************************************************************************************/ + +#endif /* __ARCH_ARM_SRC_IMXRT_CHIP_IMXRT_ENET_H */ diff --git a/arch/arm/src/imxrt/chip/imxrt_iomuxc.h b/arch/arm/src/imxrt/chip/imxrt_iomuxc.h index 82ab34bb80..6f8c7d92fc 100644 --- a/arch/arm/src/imxrt/chip/imxrt_iomuxc.h +++ b/arch/arm/src/imxrt/chip/imxrt_iomuxc.h @@ -1359,7 +1359,8 @@ # define PADMUX_MUXMODE_ALT4 (4 << PADMUX_MUXMODE_SHIFT) # define PADMUX_MUXMODE_ALT5 (5 << PADMUX_MUXMODE_SHIFT) # define PADMUX_MUXMODE_ALT7 (7 << PADMUX_MUXMODE_SHIFT) -#define PADMUX_SION (1 << 4) /* Bit 4: Software Input On Field */ +#define PADMUX_SION_SHIFT (4) /* Bit 4: Software Input On Field */ +# define PADMUX_SION (1 << PADMUX_SION_SHIFT) /* Pad Control Registers */ diff --git a/arch/arm/src/imxrt/imxrt_config.h b/arch/arm/src/imxrt/imxrt_config.h index 57e20cab11..129fcc25b6 100644 --- a/arch/arm/src/imxrt/imxrt_config.h +++ b/arch/arm/src/imxrt/imxrt_config.h @@ -199,6 +199,28 @@ # undef CONFIG_LPUART7_FLOWCONTROL # undef CONFIG_LPUART8_FLOWCONTROL +/* Ethernet controller configuration */ + +#ifndef CONFIG_IMXRT_ENET_NRXBUFFERS +# define CONFIG_IMXRT_ENET_NRXBUFFERS 6 +#endif + +#ifndef CONFIG_IMXRT_ENET_NTXBUFFERS +# define CONFIG_IMXRT_ENET_NTXBUFFERS 2 +#endif + +#ifndef CONFIG_IMXRT_ENET_NETHIFS +# define CONFIG_IMXRT_ENET_NETHIFS 1 +#endif + +#define IMXRT_ENET_HAS_DBSWAP 1 + +/* EMAC Default Interrupt Priorities */ + +#ifndef CONFIG_IMXRT_ENET_PRIO +# define CONFIG_IMXRT_ENET_PRIO NVIC_SYSH_PRIORITY_DEFAULT +#endif + /************************************************************************************ * Public Functions ************************************************************************************/ diff --git a/arch/arm/src/imxrt/imxrt_enet.c b/arch/arm/src/imxrt/imxrt_enet.c new file mode 100644 index 0000000000..3ade9f7b51 --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_enet.c @@ -0,0 +1,2287 @@ +/**************************************************************************** + * arch/arm/src/imxrt/imxrt_enet.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_NET_PKT +# include +#endif + +#include "up_arch.h" +#include "cache.h" +#include "chip.h" +#include "imxrt_config.h" +#include "chip/imxrt_enet.h" +#include "chip/imxrt_ccm.h" +#include "chip/imxrt_pinmux.h" +#include "imxrt_periphclks.h" +#include "imxrt_gpio.h" +#include "imxrt_enet.h" + +#ifdef CONFIG_IMXRT_ENET + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* If processing is not done at the interrupt level, then work queue support + * is required. + */ + +#if !defined(CONFIG_SCHED_WORKQUEUE) +# error Work queue support is required +#else + + /* Select work queue. Always use the LP work queue if available. If not, + * then LPWORK will re-direct to the HP work queue. + */ + +# define ETHWORK LPWORK +#endif + +/* CONFIG_IMXRT_ENET_NETHIFS determines the number of physical interfaces + * that will be supported. + */ + +#if CONFIG_IMXRT_ENET_NETHIFS != 1 +# error "CONFIG_IMXRT_ENET_NETHIFS must be one for now" +#endif + +#if CONFIG_IMXRT_ENET_NTXBUFFERS < 1 +# error "Need at least one TX buffer" +#endif + +#if CONFIG_IMXRT_ENET_NRXBUFFERS < 1 +# error "Need at least one RX buffer" +#endif + +#define NENET_NBUFFERS \ + (CONFIG_IMXRT_ENET_NTXBUFFERS+CONFIG_IMXRT_ENET_NRXBUFFERS) + +/* Normally you would clean the cache after writing new values to the DMA + * memory so assure that the dirty cache lines are flushed to memory + * before the DMA occurs. And you would invalid the cache after a data is + * received via DMA so that you fetch the actual content of the data from + * the cache. + * + * These conditions are not fully supported here. If the write-throuch + * D-Cache is enabled, however, then many of these issues go away: The + * cache clean operation does nothing (because there are not dirty cache + * lines) and the cache invalid operation is innocuous (because there are + * never dirty cache lines to be lost; valid data will always be reloaded). + * + * At present, we simply insist that write through cache be enabled. + */ + +#if defined(CONFIG_ARMV7M_DCACHE) && !defined(CONFIG_ARMV7M_DCACHE_WRITETHROUGH) +# error Write back D-Cache not yet supported +#endif + +/* TX poll delay = 1 seconds. CLK_TCK is the number of clock ticks per + * second. + */ + +#define IMXRT_WDDELAY (1*CLK_TCK) + +/* Align assuming that the D-Cache is enabled (probably 32-bytes). + * + * REVISIT: The size of descriptors and buffers must also be in even units + * of the cache line size That is because the operations to clean and + * invalidate the cache will operate on a full 32-byte cache line. If + * CONFIG_ENET_ENHANCEDBD is selected, then the size of the descriptor is + * 32-bytes (and probably already the correct size for the cache line); + * otherwise, the size of the descriptors much smaller, only 8 bytes. + */ + +#define ENET_ALIGN ARMV7M_DCACHE_LINESIZE +#define ENET_ALIGN_MASK (ENET_ALIGN - 1) +#define ENET_ALIGN_UP(n) (((n) + ENET_ALIGN_MASK) & ~ENET_ALIGN_MASK) + +/* TX timeout = 1 minute */ + +#define IMXRT_TXTIMEOUT (60*CLK_TCK) +#define MII_MAXPOLLS (0x1ffff) +#define LINK_WAITUS (500*1000) +#define LINK_NLOOPS (10) + +/* PHY definitions. + * + * The selected PHY must be selected from the drivers/net/Kconfig PHY menu. + * A description of the PHY must be provided here. That description must + * include: + * + * 1. BOARD_PHY_NAME: A PHY name string (for debug output), + * 2. BOARD_PHYID1 and BOARD_PHYID2: The PHYID1 and PHYID2 values (from + * include/nuttx/net/mii.h) + * 3. BOARD_PHY_STATUS: The address of the status register to use when + * querying link status (from include/nuttx/net/mii.h) + * 4. BOARD_PHY_ISDUPLEX: A macro that can convert the status register + * value into a boolean: true=duplex mode, false=half-duplex mode + * 5. BOARD_PHY_10BASET: A macro that can convert the status register + * value into a boolean: true=10Base-T, false=Not 10Base-T + * 6. BOARD_PHY_100BASET: A macro that can convert the status register + * value into a boolean: true=100Base-T, false=Not 100Base-T + * + * The imxrt1050-evk board uses a KSZ8081 PHY + */ + +#if defined(CONFIG_ETH0_PHY_KSZ8081) +# define BOARD_PHY_NAME "KSZ8081" +# define BOARD_PHYID1 MII_PHYID1_KSZ8081 +# define BOARD_PHYID2 MII_PHYID2_KSZ8081 +# define BOARD_PHY_STATUS MII_KSZ8081_PHYCTRL1 +# define BOARD_PHY_10BASET(s) (((s) & MII_PHYCTRL1_MODE_10HDX) != 0) +# define BOARD_PHY_100BASET(s) (((s) & MII_PHYCTRL1_MODE_100HDX) != 0) +# define BOARD_PHY_ISDUPLEX(s) (((s) & MII_PHYCTRL1_MODE_DUPLEX) != 0) +#else +# error "Unrecognized or missing PHY selection" +#endif + +/* Estimate the MII_SPEED in order to get an MDC close to 2.5MHz, + based on the internal module (ENET) clock: + * + * MII_SPEED = ENET_FREQ/5000000 -1 + * + * For example, if ENET_FREQ_MHZ=120 (MHz): + * + * MII_SPEED = 120000000/5000000 -1 + * = 23 + */ + +#define IMXRT_MII_SPEED 0x38 /* 100Mbs. Revisit and remove hardcoded value */ +#if IMXRT_MII_SPEED > 63 +# error "IMXRT_MII_SPEED is out-of-range" +#endif + +/* Interrupt groups */ + +#define RX_INTERRUPTS (ENET_INT_RXF | ENET_INT_RXB) +#define TX_INTERRUPTS ENET_INT_TXF +#define ERROR_INTERRUPTS (ENET_INT_UN | ENET_INT_RL | ENET_INT_LC | \ + ENET_INT_EBERR | ENET_INT_BABT | ENET_INT_BABR) + +/* This is a helper pointer for accessing the contents of the Ethernet header */ + +#define BUF ((struct eth_hdr_s *)priv->dev.d_buf) + +#define IMXRT_BUF_SIZE ENET_ALIGN_UP(CONFIG_NET_ETH_MTU) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The imxrt_driver_s encapsulates all state information for a single hardware + * interface + */ + +struct imxrt_driver_s +{ + bool bifup; /* true:ifup false:ifdown */ + uint8_t txtail; /* The oldest busy TX descriptor */ + uint8_t txhead; /* The next TX descriptor to use */ + uint8_t rxtail; /* The next RX descriptor to use */ + uint8_t phyaddr; /* Selected PHY address */ + WDOG_ID txpoll; /* TX poll timer */ + WDOG_ID txtimeout; /* TX timeout timer */ + struct work_s irqwork; /* For deferring interrupt work to the work queue */ + struct work_s pollwork; /* For deferring poll work to the work queue */ + struct enet_desc_s *txdesc; /* A pointer to the list of TX descriptor */ + struct enet_desc_s *rxdesc; /* A pointer to the list of RX descriptors */ + + /* This holds the information visible to the NuttX network */ + + struct net_driver_s dev; /* Interface understood by the network */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct imxrt_driver_s g_enet[CONFIG_IMXRT_ENET_NETHIFS]; + + +/* The DMA descriptors. A unaligned uint8_t is used to allocate the + * memory; 16 is added to assure that we can meet the descriptor alignment + * requirements. + */ + +static uint8_t g_desc_pool[NENET_NBUFFERS * sizeof(struct enet_desc_s)] + __attribute__((aligned(ENET_ALIGN))); + +/* The DMA buffers. Again, A unaligned uint8_t is used to allocate the + * memory; 16 is added to assure that we can meet the descriptor alignment + * requirements. + */ + +static uint8_t g_buffer_pool[NENET_NBUFFERS * IMXRT_BUF_SIZE] + __attribute__((aligned(ENET_ALIGN))); + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* Utility functions */ + +#ifndef IMXRT_BUFFERS_SWAP +# define imxrt_swap32(value) (value) +# define imxrt_swap16(value) (value) +#else +#if 0 /* Use builtins if the compiler supports them */ +static inline uint32_t imxrt_swap32(uint32_t value); +static inline uint16_t imxrt_swap16(uint16_t value); +#else +# define imxrt_swap32 __builtin_bswap32 +# define imxrt_swap16 __builtin_bswap16 +#endif +#endif + +/* Common TX logic */ + +static bool imxrt_txringfull(FAR struct imxrt_driver_s *priv); +static int imxrt_transmit(FAR struct imxrt_driver_s *priv); +static int imxrt_txpoll(struct net_driver_s *dev); + +/* Interrupt handling */ + +static void imxrt_dispatch(FAR struct imxrt_driver_s *priv); +static void imxrt_receive(FAR struct imxrt_driver_s *priv); +static void imxrt_txdone(FAR struct imxrt_driver_s *priv); + +static void imxrt_enet_interrupt_work(FAR void *arg); +static int imxrt_enet_interrupt(int irq, FAR void *context, FAR void *arg); + +/* Watchdog timer expirations */ + +static void imxrt_txtimeout_work(FAR void *arg); +static void imxrt_txtimeout_expiry(int argc, uint32_t arg, ...); + +static void imxrt_poll_work(FAR void *arg); +static void imxrt_polltimer_expiry(int argc, uint32_t arg, ...); + +/* NuttX callback functions */ + +static int imxrt_ifup(struct net_driver_s *dev); +static int imxrt_ifdown(struct net_driver_s *dev); + +static void imxrt_txavail_work(FAR void *arg); +static int imxrt_txavail(struct net_driver_s *dev); + +#ifdef CONFIG_NET_IGMP +static int imxrt_addmac(struct net_driver_s *dev, + FAR const uint8_t *mac); +static int imxrt_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac); +#endif + +#ifdef CONFIG_NETDEV_IOCTL +static int imxrt_ioctl(struct net_driver_s *dev, int cmd, + unsigned long arg); +#endif + +/* PHY/MII support */ + +static inline void imxrt_initmii(struct imxrt_driver_s *priv); +static int imxrt_writemii(struct imxrt_driver_s *priv, uint8_t phyaddr, + uint8_t regaddr, uint16_t data); +static int imxrt_readmii(struct imxrt_driver_s *priv, uint8_t phyaddr, + uint8_t regaddr, uint16_t *data); +static inline int imxrt_initphy(struct imxrt_driver_s *priv); + +/* Initialization */ + +static void imxrt_initbuffers(struct imxrt_driver_s *priv); +static void imxrt_reset(struct imxrt_driver_s *priv); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: imxrt_swap16/32 + * + * Description: + * The descriptors are represented by structures Unfortunately, when the + * structures are overlayed on the data, the bytes are reversed because + * the underlying hardware writes the data in big-endian byte order. + * + * Input Parameters: + * value - The value to be byte swapped + * + * Returned Value: + * The byte swapped value + * + ****************************************************************************/ + +#if 0 /* Use builtins if the compiler supports them */ +#ifndef CONFIG_ENDIAN_BIG +static inline uint32_t imxrt_swap32(uint32_t value) +{ + uint32_t result = 0; + + __asm__ __volatile__ + ( + "rev %0, %1" + :"=r" (result) + : "r"(value) + ); + return result; +} + +static inline uint16_t imxrt_swap16(uint16_t value) +{ + uint16_t result = 0; + + __asm__ __volatile__ + ( + "revsh %0, %1" + :"=r" (result) + : "r"(value) + ); + return result; +} +#endif +#endif + +/**************************************************************************** + * Function: imxrt_txringfull + * + * Description: + * Check if all of the TX descriptors are in use. + * + * Input Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * true is the TX ring is full; false if there are free slots at the + * head index. + * + ****************************************************************************/ + +static bool imxrt_txringfull(FAR struct imxrt_driver_s *priv) +{ + uint8_t txnext; + + /* Check if there is room in the hardware to hold another outgoing + * packet. The ring is full if incrementing the head pointer would + * collide with the tail pointer. + */ + + txnext = priv->txhead + 1; + if (txnext >= CONFIG_IMXRT_ENET_NTXBUFFERS) + { + txnext = 0; + } + + return priv->txtail == txnext; +} + +/**************************************************************************** + * Function: imxrt_transmit + * + * Description: + * Start hardware transmission. Called either from the txdone interrupt + * handling or from watchdog based polling. + * + * Input Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * OK on success; a negated errno on failure + * + * Assumptions: + * May or may not be called from an interrupt handler. In either case, + * global interrupts are disabled, either explicitly or indirectly through + * interrupt handling logic. + * + ****************************************************************************/ + +static int imxrt_transmit(FAR struct imxrt_driver_s *priv) +{ + struct enet_desc_s *txdesc; + uint32_t regval; + uint8_t *buf; + + /* Since this can be called from imxrt_receive, it is possible that + * the transmit queue is full, so check for that now. If this is the + * case, the outgoing packet will be dropped (e.g. an ARP reply) + */ + + if (imxrt_txringfull(priv)) + { + return -EBUSY; + } + + /* When we get here the TX descriptor should show that the previous + * transfer has completed. If we get here, then we are committed to + * sending a packet; Higher level logic must have assured that there is + * no transmission in progress. + */ + + txdesc = &priv->txdesc[priv->txhead]; + priv->txhead++; + if (priv->txhead >= CONFIG_IMXRT_ENET_NTXBUFFERS) + { + priv->txhead = 0; + } + +#ifdef CONFIG_DEBUG_ASSERTIONS + arch_invalidate_dcache((uintptr_t)txdesc, + (uintptr_t)txdesc + sizeof(struct enet_desc_s)); + + DEBUGASSERT(priv->txtail != priv->txhead && + (txdesc->status1 & TXDESC_R) == 0); +#endif + + /* Increment statistics */ + + NETDEV_TXPACKETS(&priv->dev); + + /* Setup the buffer descriptor for transmission: address=priv->dev.d_buf, + * length=priv->dev.d_len + */ + + txdesc->length = imxrt_swap16(priv->dev.d_len); +#ifdef CONFIG_IMXRT_ENETENHANCEDBD + txdesc->bdu = 0x00000000; + txdesc->status2 = TXDESC_INT | TXDESC_TS; /* | TXDESC_IINS | TXDESC_PINS; */ +#endif + txdesc->status1 |= (TXDESC_R | TXDESC_L | TXDESC_TC); + + buf = (uint8_t*)imxrt_swap32((uint32_t)priv->dev.d_buf); + if (priv->rxdesc[priv->rxtail].data == buf) + { + struct enet_desc_s *rxdesc = &priv->rxdesc[priv->rxtail]; + + /* Data was written into the RX buffer, so swap the TX and RX buffers */ + + DEBUGASSERT((rxdesc->status1 & RXDESC_E) == 0); + rxdesc->data = txdesc->data; + txdesc->data = buf; + } + else + { + ASSERT(txdesc->data == buf); + } + + /* Start the TX transfer (if it was not already waiting for buffers) */ + + putreg32(ENET_TDAR, IMXRT_ENET_TDAR); + + /* Enable TX interrupts */ + + regval = getreg32(IMXRT_ENET_EIMR); + regval |= TX_INTERRUPTS; + putreg32(regval, IMXRT_ENET_EIMR); + + /* Setup the TX timeout watchdog (perhaps restarting the timer) */ + + (void)wd_start(priv->txtimeout, IMXRT_TXTIMEOUT, imxrt_txtimeout_expiry, 1, + (wdparm_t)priv); + return OK; +} + +/**************************************************************************** + * Function: imxrt_txpoll + * + * Description: + * The transmitter is available, check if the network has any outgoing packets ready + * to send. This is a callback from devif_poll(). devif_poll() may be called: + * + * 1. When the preceding TX packet send is complete, + * 2. When the preceding TX packet send timesout and the interface is reset + * 3. During normal TX polling + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * OK on success; a negated errno on failure + * + * Assumptions: + * May or may not be called from an interrupt handler. In either case, + * global interrupts are disabled, either explicitly or indirectly through + * interrupt handling logic. + * + ****************************************************************************/ + +static int imxrt_txpoll(struct net_driver_s *dev) +{ + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; + + /* If the polling resulted in data that should be sent out on the network, + * the field d_len is set to a value > 0. + */ + + if (priv->dev.d_len > 0) + { + /* Look up the destination MAC address and add it to the Ethernet + * header. + */ + +#ifdef CONFIG_NET_IPv4 +#ifdef CONFIG_NET_IPv6 + if (IFF_IS_IPv4(priv->dev.d_flags)) +#endif + { + arp_out(&priv->dev); + } +#endif /* CONFIG_NET_IPv4 */ + +#ifdef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 + else +#endif + { + neighbor_out(&priv->dev); + } +#endif /* CONFIG_NET_IPv6 */ + + /* Send the packet */ + + imxrt_transmit(priv); + priv->dev.d_buf = + (uint8_t*)imxrt_swap32((uint32_t)priv->txdesc[priv->txhead].data); + + /* Check if there is room in the device to hold another packet. If not, + * return a non-zero value to terminate the poll. + */ + + if (imxrt_txringfull(priv)) + { + return -EBUSY; + } + } + + /* If zero is returned, the polling will continue until all connections have + * been examined. + */ + + return 0; +} + +/**************************************************************************** + * Function: imxrt_dispatch + * + * Description: + * A new Rx packet was received; dispatch that packet to the network layer + * as necessary. + * + * Input Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by interrupt handling logic. + * + ****************************************************************************/ + +static inline void imxrt_dispatch(FAR struct imxrt_driver_s *priv) +{ + /* Update statistics */ + + NETDEV_RXPACKETS(&priv->dev); + +#ifdef CONFIG_NET_PKT + /* When packet sockets are enabled, feed the frame into the packet tap */ + + pkt_input(&priv->dev); +#endif + + /* We only accept IP packets of the configured type and ARP packets */ + +#ifdef CONFIG_NET_IPv4 + if (BUF->type == HTONS(ETHTYPE_IP)) + { + ninfo("IPv4 frame\n"); + NETDEV_RXIPV4(&priv->dev); + + /* Handle ARP on input then give the IPv4 packet to the network + * layer + */ + + arp_ipin(&priv->dev); + ipv4_input(&priv->dev); + + /* If the above function invocation resulted in data that should be + * sent out on the network, the field d_len will set to a value > 0. + */ + + if (priv->dev.d_len > 0) + { + /* Update the Ethernet header with the correct MAC address */ + +#ifdef CONFIG_NET_IPv6 + if (IFF_IS_IPv4(priv->dev.d_flags)) +#endif + { + arp_out(&priv->dev); + } +#ifdef CONFIG_NET_IPv6 + else + { + neighbor_out(&priv->dev); + } +#endif + + /* And send the packet */ + + imxrt_transmit(priv); + } + } + else +#endif +#ifdef CONFIG_NET_IPv6 + if (BUF->type == HTONS(ETHTYPE_IP6)) + { + ninfo("Iv6 frame\n"); + NETDEV_RXIPV6(&priv->dev); + + /* Give the IPv6 packet to the network layer */ + + ipv6_input(&priv->dev); + + /* If the above function invocation resulted in data that should be + * sent out on the network, the field d_len will set to a value > 0. + */ + + if (priv->dev.d_len > 0) + { + /* Update the Ethernet header with the correct MAC address */ + +#ifdef CONFIG_NET_IPv4 + if (IFF_IS_IPv4(priv->dev.d_flags)) + { + arp_out(&priv->dev); + } + else +#endif +#ifdef CONFIG_NET_IPv6 + { + neighbor_out(&priv->dev); + } +#endif + + /* And send the packet */ + + imxrt_transmit(priv); + } + } + else +#endif +#ifdef CONFIG_NET_ARP + if (BUF->type == htons(ETHTYPE_ARP)) + { + NETDEV_RXARP(&priv->dev); + arp_arpin(&priv->dev); + + /* If the above function invocation resulted in data that should + * be sent out on the network, the field d_len will set to a + * value > 0. + */ + + if (priv->dev.d_len > 0) + { + imxrt_transmit(priv); + } + } +#endif + else + { + NETDEV_RXDROPPED(&priv->dev); + } +} + +/**************************************************************************** + * Function: imxrt_receive + * + * Description: + * An interrupt was received indicating the availability of a new RX packet + * + * Input Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by interrupt handling logic. + * + ****************************************************************************/ + +static void imxrt_receive(FAR struct imxrt_driver_s *priv) +{ + struct enet_desc_s *rxdesc; + bool received; + + /* Loop while there are received packets to be processed */ + + do + { + /* Invalidate the Rx descriptor. Since it has been modified via DMA, + * we must assure that we must invalid any cached values and re-read + * the descriptor from the memory. + */ + + rxdesc = &priv->rxdesc[priv->rxtail]; + arch_invalidate_dcache((uintptr_t)rxdesc, + (uintptr_t)rxdesc + sizeof(struct enet_desc_s)); + + /* Check if the data buffer associated with the descriptor has + * been filled with valid data. + */ + + received = ((rxdesc->status1 & RXDESC_E) == 0); + if (received) + { + /* Copy the buffer pointer to priv->dev.d_buf. Set amount of data + * in priv->dev.d_len + */ + + priv->dev.d_len = imxrt_swap16(rxdesc->length); + priv->dev.d_buf = (uint8_t *)imxrt_swap32((uint32_t)rxdesc->data); + + /* Invalidate the buffer so that the correct packet will be re-read + * from memory when the packet content is accessed. + */ + + arch_invalidate_dcache((uintptr_t)priv->dev.d_buf, + (uintptr_t)priv->dev.d_buf + priv->dev.d_len); + + /* Dispatch (or drop) the newly received packet */ + + imxrt_dispatch(priv); + + /* Point the packet buffer back to the next Tx buffer that will be + * used during the next write. If the write queue is full, then + * this will point at an active buffer, which must not be written + * to. This is OK because devif_poll won't be called unless the + * queue is not full. + */ + + priv->dev.d_buf = + (uint8_t*)imxrt_swap32((uint32_t)priv->txdesc[priv->txhead].data); + rxdesc->status1 |= RXDESC_E; + + /* Update the index to the next descriptor */ + + priv->rxtail++; + if (priv->rxtail >= CONFIG_IMXRT_ENET_NRXBUFFERS) + { + priv->rxtail = 0; + } + + /* Indicate that there have been empty receive buffers produced */ + + putreg32(ENET_RDAR, IMXRT_ENET_RDAR); + } + } + while (received); +} + +/**************************************************************************** + * Function: imxrt_txdone + * + * Description: + * An interrupt was received indicating that the last TX packet(s) is done + * + * Input Parameters: + * priv - Reference to the driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by the watchdog logic. + * + ****************************************************************************/ + +static void imxrt_txdone(FAR struct imxrt_driver_s *priv) +{ + struct enet_desc_s *txdesc; + uint32_t regval; + bool txdone; + + /* Verify that the oldest descriptor descriptor completed */ + + do + { + /* Invalidate the Tx descriptor. Since status information has been + * modified via DMA, we must assure that we must invalid any cached + * values and re-read the descriptor from the memory. + */ + + txdesc = &priv->txdesc[priv->txtail]; + arch_invalidate_dcache((uintptr_t)txdesc, + (uintptr_t)txdesc + sizeof(struct enet_desc_s)); + + txdone = false; + if ((txdesc->status1 & TXDESC_R) == 0 && priv->txtail != priv->txhead) + { + /* Yes.. bump up the tail pointer, making space for a new TX + * descriptor. + */ + + priv->txtail++; + if (priv->txtail >= CONFIG_IMXRT_ENET_NTXBUFFERS) + { + priv->txtail = 0; + } + + /* Update statistics */ + + NETDEV_TXDONE(&priv->dev); + txdone = true; + } + } + while (txdone); + + /* Are there other transmissions queued? */ + + if (priv->txtail == priv->txhead) + { + /* No.. Cancel the TX timeout and disable further Tx interrupts. */ + + wd_cancel(priv->txtimeout); + + regval = getreg32(IMXRT_ENET_EIMR); + regval &= ~TX_INTERRUPTS; + putreg32(regval, IMXRT_ENET_EIMR); + } + + /* There should be space for a new TX in any event. Poll the network for + * new XMIT data + */ + + (void)devif_poll(&priv->dev, imxrt_txpoll); +} + +/**************************************************************************** + * Function: imxrt_enet_interrupt_work + * + * Description: + * Perform interrupt related work from the worker thread + * + * Input Parameters: + * arg - The argument passed when work_queue() was called. + * + * Returned Value: + * OK on success + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +static void imxrt_enet_interrupt_work(FAR void *arg) +{ + FAR struct imxrt_driver_s *priv = (FAR struct imxrt_driver_s *)arg; + uint32_t pending; + + /* Process pending Ethernet interrupts */ + + net_lock(); + + /* Get the set of unmasked, pending interrupt. */ + + pending = getreg32(IMXRT_ENET_EIR) & getreg32(IMXRT_ENET_EIMR); + + /* Clear the pending interrupts */ + + putreg32(pending, IMXRT_ENET_EIR); + + /* Check for the receipt of a packet */ + + if ((pending & ENET_INT_RXF) != 0) + { + /* A packet has been received, call imxrt_receive() to handle the + * packet. + */ + + imxrt_receive(priv); + } + + /* Check if a packet transmission has completed */ + + if ((pending & ENET_INT_TXF) != 0) + { + /* Call imxrt_txdone to handle the end of transfer even. NOTE that + * this may disable further Tx interrupts if there are no pending + * transmissions. + */ + + imxrt_txdone(priv); + } + + /* Check for errors */ + + if (pending & ERROR_INTERRUPTS) + { + /* An error has occurred, update statistics */ + + NETDEV_ERRORS(&priv->dev); + + /* Reinitialize all buffers. */ + + imxrt_initbuffers(priv); + + /* Indicate that there have been empty receive buffers produced */ + + putreg32(ENET_RDAR, IMXRT_ENET_RDAR); + } + + net_unlock(); + + /* Re-enable Ethernet interrupts */ + +#if 0 + up_enable_irq(IMXRT_IRQ_EMACTMR); +#endif + up_enable_irq(IMXRT_IRQ_ENET); +} + +/**************************************************************************** + * Function: imxrt_enet_interrupt + * + * Description: + * Three interrupt sources will vector this this function: + * 1. Ethernet MAC transmit interrupt handler + * 2. Ethernet MAC receive interrupt handler + * 3. + * + * Input Parameters: + * irq - Number of the IRQ that generated the interrupt + * context - Interrupt register state save info (architecture-specific) + * + * Returned Value: + * OK on success + * + * Assumptions: + * + ****************************************************************************/ + +static int imxrt_enet_interrupt(int irq, FAR void *context, FAR void *arg) +{ + register FAR struct imxrt_driver_s *priv = &g_enet[0]; + + /* Disable further Ethernet interrupts. Because Ethernet interrupts are + * also disabled if the TX timeout event occurs, there can be no race + * condition here. + */ + + up_disable_irq(IMXRT_IRQ_ENET); + + /* TODO: Determine if a TX transfer just completed */ + + { + /* If a TX transfer just completed, then cancel the TX timeout so + * there will be do race condition between any subsequent timeout + * expiration and the deferred interrupt processing. + */ + + wd_cancel(priv->txtimeout); + } + + /* Schedule to perform the interrupt processing on the worker thread. */ + + work_queue(ETHWORK, &priv->irqwork, imxrt_enet_interrupt_work, priv, 0); + return OK; +} + +/**************************************************************************** + * Function: imxrt_txtimeout_work + * + * Description: + * Perform TX timeout related work from the worker thread + * + * Input Parameters: + * arg - The argument passed when work_queue() as called. + * + * Returned Value: + * OK on success + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +static void imxrt_txtimeout_work(FAR void *arg) +{ + FAR struct imxrt_driver_s *priv = (FAR struct imxrt_driver_s *)arg; + + /* Increment statistics and dump debug info */ + + net_lock(); + NETDEV_TXTIMEOUTS(&priv->dev); + + /* Take the interface down and bring it back up. That is the most + * aggressive hardware reset. + */ + + (void)imxrt_ifdown(&priv->dev); + (void)imxrt_ifup(&priv->dev); + + /* Then poll the network for new XMIT data */ + + (void)devif_poll(&priv->dev, imxrt_txpoll); + net_unlock(); +} + +/**************************************************************************** + * Function: imxrt_txtimeout_expiry + * + * Description: + * Our TX watchdog timed out. Called from the timer interrupt handler. + * The last TX never completed. Reset the hardware and start again. + * + * Input Parameters: + * argc - The number of available arguments + * arg - The first argument + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by the watchdog logic. + * + ****************************************************************************/ + +static void imxrt_txtimeout_expiry(int argc, uint32_t arg, ...) +{ + FAR struct imxrt_driver_s *priv = (FAR struct imxrt_driver_s *)arg; + + /* Disable further Ethernet interrupts. This will prevent some race + * conditions with interrupt work. There is still a potential race + * condition with interrupt work that is already queued and in progress. + */ + + up_disable_irq(IMXRT_IRQ_ENET); + + /* Schedule to perform the TX timeout processing on the worker thread, + * canceling any pending interrupt work. + */ + + work_queue(ETHWORK, &priv->irqwork, imxrt_txtimeout_work, priv, 0); +} + +/**************************************************************************** + * Function: imxrt_poll_work + * + * Description: + * Perform periodic polling from the worker thread + * + * Input Parameters: + * arg - The argument passed when work_queue() as called. + * + * Returned Value: + * OK on success + * + * Assumptions: + * The network is locked. + * + ****************************************************************************/ + +static void imxrt_poll_work(FAR void *arg) +{ + FAR struct imxrt_driver_s *priv = (FAR struct imxrt_driver_s *)arg; + + /* Check if there is there is a transmission in progress. We cannot perform + * the TX poll if he are unable to accept another packet for transmission. + */ + + net_lock(); + if (!imxrt_txringfull(priv)) + { + /* If so, update TCP timing states and poll the network for new XMIT + * data. Hmmm.. might be bug here. Does this mean if there is a + * transmit in progress, we will missing TCP time state updates? + */ + + (void)devif_timer(&priv->dev, imxrt_txpoll); + } + + /* Setup the watchdog poll timer again in any case */ + + (void)wd_start(priv->txpoll, IMXRT_WDDELAY, imxrt_polltimer_expiry, + 1, (wdparm_t)priv); + net_unlock(); +} + +/**************************************************************************** + * Function: imxrt_polltimer_expiry + * + * Description: + * Periodic timer handler. Called from the timer interrupt handler. + * + * Input Parameters: + * argc - The number of available arguments + * arg - The first argument + * + * Returned Value: + * None + * + * Assumptions: + * Global interrupts are disabled by the watchdog logic. + * + ****************************************************************************/ + +static void imxrt_polltimer_expiry(int argc, uint32_t arg, ...) +{ + FAR struct imxrt_driver_s *priv = (FAR struct imxrt_driver_s *)arg; + + /* Schedule to perform the poll processing on the worker thread. */ + + work_queue(ETHWORK, &priv->pollwork, imxrt_poll_work, priv, 0); +} + +/**************************************************************************** + * Function: imxrt_ifup + * + * Description: + * NuttX Callback: Bring up the Ethernet interface when an IP address is + * provided + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static int imxrt_ifup(struct net_driver_s *dev) +{ + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; + uint8_t *mac = dev->d_mac.ether.ether_addr_octet; + uint32_t regval; + int ret; + + ninfo("Bringing up: %d.%d.%d.%d\n", + dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff, + (dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24); + + /* Initialize ENET buffers */ + + imxrt_initbuffers(priv); + + /* Reset and disable the interface */ + + imxrt_reset(priv); + + /* Configure the MII interface */ + + imxrt_initmii(priv); + + /* Set the MAC address */ + + putreg32((mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3], + IMXRT_ENET_PALR); + putreg32((mac[4] << 24) | (mac[5] << 16), IMXRT_ENET_PAUR); + + /* Clear the Individual and Group Address Hash registers */ + + putreg32(0, IMXRT_ENET_IALR); + putreg32(0, IMXRT_ENET_IAUR); + putreg32(0, IMXRT_ENET_GALR); + putreg32(0, IMXRT_ENET_GAUR); + + /* Configure the PHY */ + + ret = imxrt_initphy(priv); + if (ret < 0) + { + nerr("ERROR: Failed to configure the PHY: %d\n", ret); + return ret; + } + + /* Handle promiscuous mode */ + +#ifdef CONFIG_NET_PROMISCUOUS + regval = getreg32(IMXRT_ENET_RCR); + regval |= ENET_RCR_PROM; + putreg32(regval, IMXRT_ENET_RCR); +#endif + + /* Select legacy of enhanced buffer descriptor format */ + +#ifdef CONFIG_IMXRT_ENETENHANCEDBD + putreg32(ENET_ECR_EN1588, IMXRT_ENET_ECR); +#else + putreg32(0, IMXRT_ENET_ECR); +#endif + + /* Set the RX buffer size */ + + putreg32(IMXRT_BUF_SIZE, IMXRT_ENET_MRBR); + + /* Point to the start of the circular RX buffer descriptor queue */ + + putreg32((uint32_t)priv->rxdesc, IMXRT_ENET_RDSR); + + /* Point to the start of the circular TX buffer descriptor queue */ + + putreg32((uint32_t)priv->txdesc, IMXRT_ENET_TDSR); + + /* And enable the MAC itself */ + + regval = getreg32(IMXRT_ENET_ECR); + regval |= ENET_ECR_ETHEREN +#ifdef IMXRT_USE_DBSWAP + | ENET_ECR_DBSWP +#endif + ; + putreg32(regval, IMXRT_ENET_ECR); + + /* Indicate that there have been empty receive buffers produced */ + + putreg32(ENET_RDAR, IMXRT_ENET_RDAR); + + /* Set and activate a timer process */ + + (void)wd_start(priv->txpoll, IMXRT_WDDELAY, imxrt_polltimer_expiry, 1, + (wdparm_t)priv); + + /* Clear all pending ENET interrupt */ + + putreg32(0xffffffff, IMXRT_ENET_EIR); + + /* Enable RX and error interrupts at the controller (TX interrupts are + * still disabled). + */ + + putreg32(RX_INTERRUPTS | ERROR_INTERRUPTS, + IMXRT_ENET_EIMR); + + /* Mark the interrupt "up" and enable interrupts at the NVIC */ + + priv->bifup = true; + +#if 0 + up_enable_irq(IMXRT_IRQ_EMACTMR); +#endif + up_enable_irq(IMXRT_IRQ_ENET); + + return OK; +} + +/**************************************************************************** + * Function: imxrt_ifdown + * + * Description: + * NuttX Callback: Stop the interface. + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static int imxrt_ifdown(struct net_driver_s *dev) +{ + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; + irqstate_t flags; + + /* Disable the Ethernet interrupts at the NVIC */ + + flags = enter_critical_section(); + + up_disable_irq(IMXRT_IRQ_ENET); + putreg32(0, IMXRT_ENET_EIMR); + + /* Cancel the TX poll timer and TX timeout timers */ + + wd_cancel(priv->txpoll); + wd_cancel(priv->txtimeout); + + /* Put the EMAC in its reset, non-operational state. This should be + * a known configuration that will guarantee the imxrt_ifup() always + * successfully brings the interface back up. + */ + + imxrt_reset(priv); + + /* Mark the device "down" */ + + priv->bifup = false; + leave_critical_section(flags); + return OK; +} + +/**************************************************************************** + * Function: imxrt_txavail_work + * + * Description: + * Perform an out-of-cycle poll on the worker thread. + * + * Input Parameters: + * arg - Reference to the NuttX driver state structure (cast to void*) + * + * Returned Value: + * None + * + * Assumptions: + * Called on the higher priority worker thread. + * + ****************************************************************************/ + +static void imxrt_txavail_work(FAR void *arg) +{ + FAR struct imxrt_driver_s *priv = (FAR struct imxrt_driver_s *)arg; + + /* Ignore the notification if the interface is not yet up */ + + net_lock(); + if (priv->bifup) + { + /* Check if there is room in the hardware to hold another outgoing + * packet. + */ + + if (!imxrt_txringfull(priv)) + { + /* No, there is space for another transfer. Poll the network for new + * XMIT data. + */ + + (void)devif_poll(&priv->dev, imxrt_txpoll); + } + } + + net_unlock(); +} + +/**************************************************************************** + * Function: imxrt_txavail + * + * Description: + * Driver callback invoked when new TX data is available. This is a + * stimulus perform an out-of-cycle poll and, thereby, reduce the TX + * latency. + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * Called in normal user mode + * + ****************************************************************************/ + +static int imxrt_txavail(struct net_driver_s *dev) +{ + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; + + /* Is our single work structure available? It may not be if there are + * pending interrupt actions and we will have to ignore the Tx + * availability action. + */ + + if (work_available(&priv->pollwork)) + { + /* Schedule to serialize the poll on the worker thread. */ + + work_queue(ETHWORK, &priv->pollwork, imxrt_txavail_work, priv, 0); + } + + return OK; +} + +/**************************************************************************** + * Function: imxrt_addmac + * + * Description: + * NuttX Callback: Add the specified MAC address to the hardware multicast + * address filtering + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * mac - The MAC address to be added + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IGMP +static int imxrt_addmac(struct net_driver_s *dev, FAR const uint8_t *mac) +{ + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; + + /* Add the MAC address to the hardware multicast routing table */ + + return OK; +} +#endif + +/**************************************************************************** + * Function: imxrt_rmmac + * + * Description: + * NuttX Callback: Remove the specified MAC address from the hardware multicast + * address filtering + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * mac - The MAC address to be removed + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#ifdef CONFIG_NET_IGMP +static int imxrt_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac) +{ + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; + + /* Add the MAC address to the hardware multicast routing table */ + + UNUSED(priv); + return OK; +} +#endif + +/**************************************************************************** + * Function: imxrt_ioctl + * + * Description: + * PHY ioctl command handler + * + * Input Parameters: + * dev - Reference to the NuttX driver state structure + * cmd - ioctl command + * arg - Argument accompanying the command + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + * Assumptions: + * + ****************************************************************************/ + +#ifdef CONFIG_NETDEV_IOCTL +static int imxrt_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg) +{ +#ifdef CONFIG_NETDEV_PHY_IOCTL + FAR struct imxrt_driver_s *priv = + (FAR struct imxrt_driver_s *)dev->d_private; +#endif + int ret; + + switch (cmd) + { +#ifdef CONFIG_NETDEV_PHY_IOCTL + case SIOCGMIIPHY: /* Get MII PHY address */ + { + struct mii_ioctl_data_s *req = + (struct mii_ioctl_data_s *)((uintptr_t)arg); + req->phy_id = priv->phyaddr; + ret = OK; + } + break; + + case SIOCGMIIREG: /* Get register from MII PHY */ + { + struct mii_ioctl_data_s *req = + (struct mii_ioctl_data_s *)((uintptr_t)arg); + ret = imxrt_readmii(priv, req->phy_id, req->reg_num, &req->val_out); + } + break; + + case SIOCSMIIREG: /* Set register in MII PHY */ + { + struct mii_ioctl_data_s *req = + (struct mii_ioctl_data_s *)((uintptr_t)arg); + ret = imxrt_writemii(priv, req->phy_id, req->reg_num, req->val_in); + } + break; +#endif /* ifdef CONFIG_NETDEV_PHY_IOCTL */ + + default: + ret = -ENOTTY; + break; + } + + return ret; +} +#endif /* CONFIG_NETDEV_IOCTL */ + +/**************************************************************************** + * Function: imxrt_initmii + * + * Description: + * Configure the MII interface + * + * Input Parameters: + * priv - Reference to the private ENET driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static void imxrt_initmii(struct imxrt_driver_s *priv) +{ + /* Speed is based on the peripheral (bus) clock; hold time is 2 module + * clock. This hold time value may need to be increased on some platforms + */ + + putreg32(ENET_MSCR_HOLDTIME_2CYCLES | + IMXRT_MII_SPEED << ENET_MSCR_MII_SPEED_SHIFT, + IMXRT_ENET_MSCR); +} + +/**************************************************************************** + * Function: imxrt_writemii + * + * Description: + * Write a 16-bit value to a PHY register. + * + * Input Parameters: + * priv - Reference to the private ENET driver state structure + * phyaddr - The PHY address + * regaddr - The PHY register address + * data - The data to write to the PHY register + * + * Returned Value: + * Zero on success, a negated errno value on failure. + * + ****************************************************************************/ + +static int imxrt_writemii(struct imxrt_driver_s *priv, uint8_t phyaddr, + uint8_t regaddr, uint16_t data) +{ + int timeout; + + /* Clear the MII interrupt bit */ + + putreg32(ENET_INT_MII, IMXRT_ENET_EIR); + + /* Initiate the MII Management write */ + + putreg32(data | + 2 << ENET_MMFR_TA_SHIFT | + (uint32_t)regaddr << ENET_MMFR_RA_SHIFT | + (uint32_t)phyaddr << ENET_MMFR_PA_SHIFT | + ENET_MMFR_OP_WRMII | + 1 << ENET_MMFR_ST_SHIFT, + IMXRT_ENET_MMFR); + + /* Wait for the transfer to complete */ + + for (timeout = 0; timeout < MII_MAXPOLLS; timeout++) + { + if ((getreg32(IMXRT_ENET_EIR) & ENET_INT_MII) != 0) + { + break; + } + } + + /* Check for a timeout */ + + if (timeout == MII_MAXPOLLS) + { + return -ETIMEDOUT; + } + + /* Clear the MII interrupt bit */ + + putreg32(ENET_INT_MII, IMXRT_ENET_EIR); + return OK; +} + +/**************************************************************************** + * Function: imxrt_reademii + * + * Description: + * Read a 16-bit value from a PHY register. + * + * Input Parameters: + * priv - Reference to the private ENET driver state structure + * phyaddr - The PHY address + * regaddr - The PHY register address + * data - A pointer to the location to return the data + * + * Returned Value: + * Zero on success, a negated errno value on failure. + * + ****************************************************************************/ + +static int imxrt_readmii(struct imxrt_driver_s *priv, uint8_t phyaddr, + uint8_t regaddr, uint16_t *data) +{ + int timeout; + + /* Clear the MII interrupt bit */ + + putreg32(ENET_INT_MII, IMXRT_ENET_EIR); + + /* Initiate the MII Management read */ + + putreg32(2 << ENET_MMFR_TA_SHIFT | + (uint32_t)regaddr << ENET_MMFR_RA_SHIFT | + (uint32_t)phyaddr << ENET_MMFR_PA_SHIFT | + ENET_MMFR_OP_RDMII | + 1 << ENET_MMFR_ST_SHIFT, + IMXRT_ENET_MMFR); + + /* Wait for the transfer to complete */ + + for (timeout = 0; timeout < MII_MAXPOLLS; timeout++) + { + if ((getreg32(IMXRT_ENET_EIR) & ENET_INT_MII) != 0) + { + break; + } + } + + /* Check for a timeout */ + + if (timeout >= MII_MAXPOLLS) + { + nerr("ERROR: Timed out waiting for transfer to complete\n"); + return -ETIMEDOUT; + } + + /* Clear the MII interrupt bit */ + + putreg32(ENET_INT_MII, IMXRT_ENET_EIR); + + /* And return the MII data */ + + *data = (uint16_t)(getreg32(IMXRT_ENET_MMFR) & ENET_MMFR_DATA_MASK); + return OK; +} + +/**************************************************************************** + * Function: imxrt_initphy + * + * Description: + * Configure the PHY + * + * Input Parameters: + * priv - Reference to the private ENET driver state structure + * + * Returned Value: + * Zero (OK) returned on success; a negated errno value is returned on any + * failure; + * + * Assumptions: + * + ****************************************************************************/ + +static inline int imxrt_initphy(struct imxrt_driver_s *priv) +{ + uint32_t rcr; + uint32_t tcr; + uint16_t phydata; + uint8_t phyaddr; + int retries; + int ret; + + /* Loop (potentially infinitely?) until we successfully communicate with + * the PHY. + */ + + for (phyaddr = 0; phyaddr < 32; phyaddr++) + { + ninfo("%s: Try phyaddr: %u\n", BOARD_PHY_NAME, phyaddr); + + /* Try to read PHYID1 few times using this address */ + + retries = 0; + do + { + nxsig_usleep(LINK_WAITUS); + ninfo("%s: Read PHYID1, retries=%d\n", + BOARD_PHY_NAME, retries + 1); + phydata = 0xffff; + ret = imxrt_readmii(priv, phyaddr, MII_PHYID1, &phydata); + } + while ((ret < 0 || phydata == 0xffff) && ++retries < 3); + + /* If we successfully read anything then break out, using this PHY address */ + + if (retries < 3) + { + break; + } + } + + if (phyaddr >= 32) + { + nerr("ERROR: Failed to read %s PHYID1 at any address\n"); + return -ENOENT; + } + + ninfo("%s: Using PHY address %u\n", BOARD_PHY_NAME, phyaddr); + priv->phyaddr = phyaddr; + + /* Verify PHYID1. Compare OUI bits 3-18 */ + + ninfo("%s: PHYID1: %04x\n", BOARD_PHY_NAME, phydata); + if (phydata != BOARD_PHYID1) + { + nerr("ERROR: PHYID1=%04x incorrect for %s. Expected %04x\n", + phydata, BOARD_PHY_NAME, BOARD_PHYID1); + return -ENXIO; + } + + /* Read PHYID2 */ + + ret = imxrt_readmii(priv, phyaddr, MII_PHYID2, &phydata); + if (ret < 0) + { + nerr("ERROR: Failed to read %s PHYID2: %d\n", BOARD_PHY_NAME, ret); + return ret; + } + + ninfo("%s: PHYID2: %04x\n", BOARD_PHY_NAME, phydata); + + /* Verify PHYID2: Compare OUI bits 19-24 and the 6-bit model number + * (ignoring the 4-bit revision number). + */ + + if ((phydata & 0xfff0) != (BOARD_PHYID2 & 0xfff0)) + { + nerr("ERROR: PHYID2=%04x incorrect for %s. Expected %04x\n", + (phydata & 0xfff0), BOARD_PHY_NAME, (BOARD_PHYID2 & 0xfff0)); + return -ENXIO; + } + + /* Reset PHY */ + + imxrt_writemii(priv, phyaddr, MII_MCR, MII_MCR_RESET); + + /* Set RMII mode */ + + ret = imxrt_readmii(priv, phyaddr, MII_KSZ8081_PHYCTRL2, &phydata); + if (ret < 0) + { + nerr("ERROR: Failed to read MII_KSZ8081_PHYCTRL2\n"); + return ret; + } + + imxrt_writemii(priv, phyaddr, MII_KSZ8081_PHYCTRL2, + (phydata | (1 << 7))); + + /* Start auto negotiation */ + + ninfo("%s: Start Autonegotiation...\n", BOARD_PHY_NAME); + imxrt_writemii(priv, phyaddr, MII_MCR, + (MII_MCR_ANRESTART | MII_MCR_ANENABLE)); + + /* Wait for auto negotiation to complete */ + + for (retries = 0; retries < LINK_NLOOPS; retries++) + { + ret = imxrt_readmii(priv, phyaddr, MII_MSR, &phydata); + if (ret < 0) + { + nerr("ERROR: Failed to read %s MII_MSR: %d\n", + BOARD_PHY_NAME, ret); + return ret; + } + + if (phydata & MII_MSR_ANEGCOMPLETE) + { + break; + } + + nxsig_usleep(LINK_WAITUS); + } + + if (phydata & MII_MSR_ANEGCOMPLETE) + { + ninfo("%s: Autonegotiation complete\n", BOARD_PHY_NAME); + ninfo("%s: MII_MSR: %04x\n", BOARD_PHY_NAME, phydata); + } + else + { + /* TODO: Autonegotitation has right now failed. Maybe the Eth cable is + * not connected. PHY chip have mechanisms to configure link OK. We + * should leave autconf on, and find a way to re-configure MCU + * whenever the link is ready. + */ + + ninfo("%s: Autonegotiation failed [%d] (is cable plugged-in ?), " + "default to 10Mbs mode\n", \ + BOARD_PHY_NAME, retries); + + /* Stop auto negotiation */ + + imxrt_writemii(priv, phyaddr, MII_MCR, 0); + } + + /* set ethernet led to green = activity and yellow = link and */ + + ret = imxrt_readmii(priv, phyaddr, MII_KSZ8081_PHYCTRL2, &phydata); + if (ret < 0) + { + nerr("ERROR: Failed to read MII_KSZ8081_PHYCTRL2\n"); + return ret; + } + + imxrt_writemii(priv, phyaddr, MII_KSZ8081_PHYCTRL2, + (phydata | (1 << 4))); + + /* When we get here we have a (negotiated) speed and duplex. */ + + phydata = 0; + ret = imxrt_readmii(priv, phyaddr, BOARD_PHY_STATUS, &phydata); + if (ret < 0) + { + nerr("ERROR: Failed to read %s BOARD_PHY_STATUS[%02x]: %d\n", + BOARD_PHY_NAME, BOARD_PHY_STATUS, ret); + return ret; + } + + ninfo("%s: BOARD_PHY_STATUS: %04x\n", BOARD_PHY_NAME, phydata); + + /* Set up the transmit and receive control registers based on the + * configuration and the auto negotiation results. + */ + +#ifdef CONFIG_IMXRT_ENETUSEMII + rcr = ENET_RCR_CRCFWD | + CONFIG_NET_ETH_MTU << ENET_RCR_MAX_FL_SHIFT | + ENET_RCR_MII_MODE; +#else + rcr = ENET_RCR_RMII_MODE | ENET_RCR_CRCFWD | + CONFIG_NET_ETH_MTU << ENET_RCR_MAX_FL_SHIFT | + ENET_RCR_MII_MODE; +#endif + tcr = 0; + + putreg32(rcr, IMXRT_ENET_RCR); + putreg32(tcr, IMXRT_ENET_TCR); + + /* Setup half or full duplex */ + + if (BOARD_PHY_ISDUPLEX(phydata)) + { + /* Full duplex */ + + ninfo("%s: Full duplex\n", BOARD_PHY_NAME); + tcr |= ENET_TCR_FDEN; + } + else + { + /* Half duplex */ + + ninfo("%s: Half duplex\n", BOARD_PHY_NAME); + rcr |= ENET_RCR_DRT; + } + + if (BOARD_PHY_10BASET(phydata)) + { + /* 10 Mbps */ + + ninfo("%s: 10 Base-T\n", BOARD_PHY_NAME); + rcr |= ENET_RCR_RMII_10T; + } + else if (BOARD_PHY_100BASET(phydata)) + { + /* 100 Mbps */ + + ninfo("%s: 100 Base-T\n", BOARD_PHY_NAME); + } + else + { + /* This might happen if Autonegotiation did not complete(?) */ + + nerr("ERROR: Neither 10- nor 100-BaseT reported: PHY STATUS=%04x\n", + phydata); + return -EIO; + } + + putreg32(rcr, IMXRT_ENET_RCR); + putreg32(tcr, IMXRT_ENET_TCR); + return OK; +} + +/**************************************************************************** + * Function: imxrt_initbuffers + * + * Description: + * Initialize ENET buffers and descriptors + * + * Input Parameters: + * priv - Reference to the private ENET driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static void imxrt_initbuffers(struct imxrt_driver_s *priv) +{ + uintptr_t addr; + int i; + + /* Get an aligned TX descriptor (array) address */ + + addr = (uintptr_t)g_desc_pool; + priv->txdesc = (struct enet_desc_s *)addr; + + /* Get an aligned RX descriptor (array) address */ + + addr += CONFIG_IMXRT_ENET_NTXBUFFERS * sizeof(struct enet_desc_s); + priv->rxdesc = (struct enet_desc_s *)addr; + + /* Get the beginning of the first aligned buffer */ + + addr = (uintptr_t)g_buffer_pool; + + /* Then fill in the TX descriptors */ + + for (i = 0; i < CONFIG_IMXRT_ENET_NTXBUFFERS; i++) + { + priv->txdesc[i].status1 = 0; + priv->txdesc[i].length = 0; + priv->txdesc[i].data = (uint8_t *)imxrt_swap32((uint32_t)addr); +#ifdef CONFIG_IMXRT_ENETENHANCEDBD + priv->txdesc[i].status2 = TXDESC_IINS | TXDESC_PINS; +#endif + addr += IMXRT_BUF_SIZE; + } + + /* Then fill in the RX descriptors */ + + for (i = 0; i < CONFIG_IMXRT_ENET_NRXBUFFERS; i++) + { + priv->rxdesc[i].status1 = RXDESC_E; + priv->rxdesc[i].length = 0; + priv->rxdesc[i].data = (uint8_t *)imxrt_swap32((uint32_t)addr); +#ifdef CONFIG_IMXRT_ENETENHANCEDBD + priv->rxdesc[i].bdu = 0; + priv->rxdesc[i].status2 = RXDESC_INT; +#endif + addr += IMXRT_BUF_SIZE; + } + + /* Set the wrap bit in the last descriptors to form a ring */ + + priv->txdesc[CONFIG_IMXRT_ENET_NTXBUFFERS-1].status1 |= TXDESC_W; + priv->rxdesc[CONFIG_IMXRT_ENET_NRXBUFFERS-1].status1 |= RXDESC_W; + + /* We start with RX descriptor 0 and with no TX descriptors in use */ + + priv->txtail = 0; + priv->txhead = 0; + priv->rxtail = 0; + + /* Initialize the packet buffer, which is used when sending */ + + priv->dev.d_buf = + (uint8_t*)imxrt_swap32((uint32_t)priv->txdesc[priv->txhead].data); +} + +/**************************************************************************** + * Function: imxrt_reset + * + * Description: + * Put the EMAC in the non-operational, reset state + * + * Input Parameters: + * priv - Reference to the private ENET driver state structure + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +static void imxrt_reset(struct imxrt_driver_s *priv) +{ + unsigned int i; + + /* Set the reset bit and clear the enable bit */ + + putreg32(ENET_ECR_RESET, IMXRT_ENET_ECR); + + /* Wait at least 8 clock cycles */ + + for (i = 0; i < 10; i++) + { + asm volatile ("nop"); + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: imxrt_netinitialize + * + * Description: + * Initialize the Ethernet controller and driver + * + * Input Parameters: + * intf - In the case where there are multiple EMACs, this value + * identifies which EMAC is to be initialized. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + * Assumptions: + * + ****************************************************************************/ + +int imxrt_netinitialize(int intf) +{ + struct imxrt_driver_s *priv; +#ifdef CONFIG_NET_ETHERNET + uint32_t uidl; + uint32_t uidml; + uint8_t *mac; +#endif + uint32_t regval; + int ret; + + /* Get the interface structure associated with this interface number. */ + + DEBUGASSERT(intf < CONFIG_IMXRT_ENET_NETHIFS); + priv = &g_enet[intf]; + + /* Init ENET PLL6 */ + + regval = CCM_ANALOG_PLL_ENET_ENET0_DIV_SELECT_50MHZ | + CCM_ANALOG_PLL_ENET_ENET1_DIV_SELECT_50MHZ | + CCM_ANALOG_PLL_ENET_BYPASS_CLK_SRC_REF_24M | + CCM_ANALOG_PLL_ENET_ENET1_125M_EN; + putreg32(regval, IMXRT_CCM_ANALOG_PLL_ENET); + + while ((getreg32(IMXRT_CCM_ANALOG_PLL_ENET) & CCM_ANALOG_PLL_ENET_LOCK) == 0) + { + } + + /* Enable ENET1_TX_CLK_DIR */ + + regval = getreg32(IMXRT_IOMUXC_GPR_GPR1); + regval |= GPR_GPR1_ENET1_TX_CLK_OUT_EN; + putreg32(regval, IMXRT_IOMUXC_GPR_GPR1); + + /* Enable the ENET clock */ + /* Clock is on during all modes, except STOP mode.*/ + + imxrt_clockall_enet(); + + /* Configure all ENET/MII pins */ + + imxrt_config_gpio(GPIO_ENET_MDIO_3); + imxrt_config_gpio(GPIO_ENET_MDC_3); + imxrt_config_gpio(GPIO_ENET_RX_EN_1); + imxrt_config_gpio(GPIO_ENET_RX_ER_1); + imxrt_config_gpio(GPIO_ENET_RX_DATA00); + imxrt_config_gpio(GPIO_ENET_RX_DATA01); + imxrt_config_gpio(GPIO_ENET_TX_DATA00); + imxrt_config_gpio(GPIO_ENET_TX_DATA01); + imxrt_config_gpio(GPIO_ENET_TX_CLK_1); + imxrt_config_gpio(GPIO_ENET_TX_EN_1); + + /* Configure daisy chain pins */ + + putreg32(1, IMXRT_IOMUXC_BASE+IMXRT_INPUT_ENET_IPG_CLK_RMII_OFFSET); + putreg32(1, IMXRT_IOMUXC_BASE+IMXRT_INPUT_ENET_MDIO_OFFSET); + putreg32(1, IMXRT_IOMUXC_BASE+IMXRT_INPUT_ENET0_RXDATA_OFFSET); + putreg32(1, IMXRT_IOMUXC_BASE+IMXRT_INPUT_ENET1_RXDATA_OFFSET); + putreg32(1, IMXRT_IOMUXC_BASE+IMXRT_INPUT_ENET_RXEN_OFFSET); + putreg32(1, IMXRT_IOMUXC_BASE+IMXRT_INPUT_ENET_RXERR_OFFSET); + +#ifdef CONFIG_ARCH_IRQPRIO + /* Set interrupt priority levels */ + + up_prioritize_irq(IMXRT_IRQ_ENET, CONFIG_IMXRT_ENET_PRIO); +#endif + + /* Attach the Ethernet MAC IEEE 1588 timer interrupt handler */ + +#if 0 + if (irq_attach(IMXRT_IRQ_EMACTMR, imxrt_tmrinterrupt, NULL)) + { + /* We could not attach the ISR to the interrupt */ + + nerr("ERROR: Failed to attach EMACTMR IRQ\n"); + return -EAGAIN; + } +#endif + + /* Attach the Ethernet interrupt handler */ + + if (irq_attach(IMXRT_IRQ_ENET, imxrt_enet_interrupt, NULL)) + { + /* We could not attach the ISR to the interrupt */ + + nerr("ERROR: Failed to attach EMACTX IRQ\n"); + return -EAGAIN; + } + + /* Initialize the driver structure */ + + memset(priv, 0, sizeof(struct imxrt_driver_s)); + priv->dev.d_ifup = imxrt_ifup; /* I/F up (new IP address) callback */ + priv->dev.d_ifdown = imxrt_ifdown; /* I/F down callback */ + priv->dev.d_txavail = imxrt_txavail; /* New TX data callback */ +#ifdef CONFIG_NET_IGMP + priv->dev.d_addmac = imxrt_addmac; /* Add multicast MAC address */ + priv->dev.d_rmmac = imxrt_rmmac; /* Remove multicast MAC address */ +#endif +#ifdef CONFIG_NETDEV_IOCTL + priv->dev.d_ioctl = imxrt_ioctl; /* Support PHY ioctl() calls */ +#endif + priv->dev.d_private = (void *)g_enet; /* Used to recover private state from dev */ + + /* Create a watchdog for timing polling for and timing of transmissions */ + + priv->txpoll = wd_create(); /* Create periodic poll timer */ + priv->txtimeout = wd_create(); /* Create TX timeout timer */ + +#ifdef CONFIG_NET_ETHERNET + /* Determine a semi-unique MAC address from MCU UID + * We use UID Low and Mid Low registers to get 64 bits, from which we keep + * 48 bits. We then force unicast and locally administered bits (b0 and b1, + * 1st octet) + */ + + /* REVISIT: temporary hardcode mac. Use chip's uniqe ID later' */ + + uidl = 0x11223344; + uidml = 0x55667788; + mac = priv->dev.d_mac.ether.ether_addr_octet; + + uidml |= 0x00000200; + uidml &= 0x0000FEFF; + + mac[0] = (uidml & 0x0000ff00) >> 8; + mac[1] = (uidml & 0x000000ff); + mac[2] = (uidl & 0xff000000) >> 24; + mac[3] = (uidl & 0x00ff0000) >> 16; + mac[4] = (uidl & 0x0000ff00) >> 8; + mac[5] = (uidl & 0x000000ff); + +#endif + +#ifdef CONFIG_IMXRT_ENET_PHYINIT + /* Perform any necessary, one-time, board-specific PHY initialization */ + + ret = imxrt_phy_boardinitialize(0); + if (ret < 0) + { + nerr("ERROR: Failed to initialize the PHY: %d\n", ret); + return ret; + } +#endif + + /* Put the interface in the down state. This usually amounts to resetting + * the device and/or calling imxrt_ifdown(). + */ + + (void)imxrt_ifdown(&priv->dev); + + /* Register the device with the OS so that socket IOCTLs can be performed */ + + (void)netdev_register(&priv->dev, NET_LL_ETHERNET); + + UNUSED(ret); + return OK; +} + +/**************************************************************************** + * Name: up_netinitialize + * + * Description: + * Initialize the first network interface. If there are more than one + * interface in the chip, then board-specific logic will have to provide + * this function to determine which, if any, Ethernet controllers should + * be initialized. + * + ****************************************************************************/ + +#if CONFIG_IMXRT_ENET_NETHIFS == 1 +void up_netinitialize(void) +{ + (void)imxrt_netinitialize(0); +} +#endif + +#endif /* CONFIG_IMXRT_ENET */ diff --git a/arch/arm/src/imxrt/imxrt_enet.h b/arch/arm/src/imxrt/imxrt_enet.h new file mode 100644 index 0000000000..311fda56ad --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_enet.h @@ -0,0 +1,124 @@ +/************************************************************************************ + * arch/arm/src/imxrt/imxrt_enet.h + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************************************/ + +#ifndef __ARCH_ARM_SRC_IMXRT_IMXRT_ENET_H +#define __ARCH_ARM_SRC_IMXRT_IMXRT_ENET_H + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include + +#include "chip/imxrt_enet.h" + +#ifdef CONFIG_IMXRT_ENET + +/************************************************************************************ + * Pre-processor Definitions + ************************************************************************************/ + +/* Definitions for use with imxrt_phy_boardinitialize */ + +#define EMAC_INTF 0 + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/************************************************************************************ + * Function: up_netinitialize + * + * Description: + * Initialize the first network interface. If there are more than one + * interface in the chip, then board-specific logic will have to provide + * this function to determine which, if any, Ethernet controllers should + * be initialized. Also prototyped in up_internal.h. + * + * Input Parameters: + * None + * + * Returned Value: + * OK on success; Negated errno on failure. + * + * Assumptions: + * Called very early in the initialization sequence. + * + ************************************************************************************/ + +void up_netinitialize(void); + +/************************************************************************************ + * Function: imxrt_phy_boardinitialize + * + * Description: + * Some boards require specialized initialization of the PHY before it can be + * used. This may include such things as configuring GPIOs, resetting the PHY, + * etc. If CONFIG_IMXRT_ENET_PHYINIT is defined in the configuration then the + * board specific logic must provide imxrt_phyinitialize(); The i.MX RT Ethernet + * driver will call this function one time before it first uses the PHY. + * + * Input Parameters: + * intf - Always zero for now. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ************************************************************************************/ + +#ifdef CONFIG_IMXRT_ENET_PHYINIT +int imxrt_phy_boardinitialize(int intf); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* CONFIG_IMXRT_ENET */ +#endif /* __ARCH_ARM_SRC_IMXRT_IMXRT_ENET_H */ + diff --git a/arch/arm/src/imxrt/imxrt_gpio.c b/arch/arm/src/imxrt/imxrt_gpio.c index 684e650a00..e88dcf302b 100644 --- a/arch/arm/src/imxrt/imxrt_gpio.c +++ b/arch/arm/src/imxrt/imxrt_gpio.c @@ -463,11 +463,16 @@ static inline int imxrt_gpio_configperiph(gpio_pinset_t pinset) /* Configure pin as a peripheral */ - index = ((pinset & GPIO_PADMUX_MASK) >> GPIO_PADMUX_SHIFT); + index = ((pinset & GPIO_PADMUX_MASK) >> GPIO_PADMUX_SHIFT); regaddr = imxrt_padmux_address(index); - value = ((pinset & GPIO_ALT_MASK) >> GPIO_ALT_SHIFT); - regval = (value << PADMUX_MUXMODE_SHIFT); + value = ((pinset & GPIO_ALT_MASK) >> GPIO_ALT_SHIFT); +#if GPIO_SION_SHIFT >= PADMUX_SION_SHIFT + value |= ((pinset & GPIO_SION_MASK) >> (GPIO_SION_SHIFT - PADMUX_SION_SHIFT)); +#else + value |= ((pinset & GPIO_SION_MASK) << (PADMUX_SION_SHIFT - GPIO_SION_SHIFT)); +#endif + regval = (value << PADMUX_MUXMODE_SHIFT); putreg32(regval, regaddr); diff --git a/arch/arm/src/imxrt/imxrt_gpio.h b/arch/arm/src/imxrt/imxrt_gpio.h index 67ed5bc7ae..647b125b70 100644 --- a/arch/arm/src/imxrt/imxrt_gpio.h +++ b/arch/arm/src/imxrt/imxrt_gpio.h @@ -55,8 +55,8 @@ * * ENCODING IIXX XXXX XXXX XXXX MMMM MMMM MMMM MMMM * GPIO INPUT 00.. ..EE .GGP PPPP MMMM MMMM MMMM MMMM - * GPIO OUTPUT 01V. .... .GGP PPPP MMMM MMMM MMMM MMMM - * PERIPHERAL 10AA A... IIII IIII MMMM MMMM MMMM MMMM + * GPIO OUTPUT 01V. .S.. .GGP PPPP MMMM MMMM MMMM MMMM + * PERIPHERAL 10AA AS.. IIII IIII MMMM MMMM MMMM MMMM */ /* Input/Output Selection: @@ -136,7 +136,7 @@ /* Peripheral Alternate Function: * - * PERIPHERAL 10AA A... .... .... MMMM MMMM MMMM MMMM + * PERIPHERAL ..AA A... .... .... .... .... .... .... */ #define GPIO_ALT_SHIFT (27) /* Bits 27-29: Peripheral alternate function */ @@ -150,6 +150,15 @@ # define GPIO_ALT6 (6 << GPIO_ALT_SHIFT) /* Alternate function 6 */ # define GPIO_ALT7 (7 << GPIO_ALT_SHIFT) /* Alternate function 7 */ +/* Peripheral Software Input On Field: + * + * PERIPHERAL .... .S.. .... .... .... .... .... .... + */ + +#define GPIO_SION_SHIFT (26) /* Bits 26: Peripheral SION function */ +#define GPIO_SION_MASK (1 << GPIO_SION_SHIFT) +# define GPIO_SION_ENABLE (1 << GPIO_SION_SHIFT) /* enable SION */ + /* Interrupt edge/level configuration * * GPIO INPUT ... ..EE .... .... .... .... .... .... diff --git a/configs/imxrt1050-evk/README.txt b/configs/imxrt1050-evk/README.txt index ee76e631e0..5ea13cefb5 100644 --- a/configs/imxrt1050-evk/README.txt +++ b/configs/imxrt1050-evk/README.txt @@ -166,6 +166,16 @@ Configurations Configuration sub-directories ----------------------------- + netnsh: + + This configuration is similar to the nsh configuration except that is + has networking enabled, both IPv4 and IPv6. This NSH configuration is + focused on network-related testing. + + NOTES: + 1. LED support is disabled because there is a conflict between the LED + GPIO and PHY pin usage. + nsh: Configures the NuttShell (nsh) located at examples/nsh. This NSH diff --git a/configs/imxrt1050-evk/netnsh/defconfig b/configs/imxrt1050-evk/netnsh/defconfig new file mode 100644 index 0000000000..2089eb2603 --- /dev/null +++ b/configs/imxrt1050-evk/netnsh/defconfig @@ -0,0 +1,60 @@ +# CONFIG_ARCH_LEDS is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="imxrt1050-evk" +CONFIG_ARCH_BOARD_IMXRT1050_EVK=y +CONFIG_ARCH_CHIP_IMXRT=y +CONFIG_ARCH_CHIP_MIMXRT1052DVL6A=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARMV7M_DCACHE=y +CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y +CONFIG_ARMV7M_ICACHE=y +CONFIG_ARMV7M_USEBASEPRI=y +CONFIG_BOARD_LOOPSPERMSEC=104926 +CONFIG_BUILTIN=y +CONFIG_ETH0_PHY_KSZ8081=y +CONFIG_EXAMPLES_NSH=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_IMXRT_ENET=y +CONFIG_IMXRT_LPUART1=y +CONFIG_INTELHEX_BINARY=y +CONFIG_LPUART1_SERIAL_CONSOLE=y +CONFIG_MAX_TASKS=16 +CONFIG_MAX_WDOGPARMS=2 +CONFIG_NET=y +CONFIG_NETDEV_STATISTICS=y +CONFIG_NETUTILS_TELNETD=y +CONFIG_NET_ARP_SEND=y +CONFIG_NET_BROADCAST=y +CONFIG_NET_HOSTNAME="i.MXRT1050 EVK" +CONFIG_NET_ICMP=y +CONFIG_NET_ICMP_SOCKET=y +CONFIG_NET_ICMPv6=y +CONFIG_NET_ICMPv6_NEIGHBOR=y +CONFIG_NET_ICMPv6_SOCKET=y +CONFIG_NET_IPv6=y +CONFIG_NET_SOCKOPTS=y +CONFIG_NET_STATISTICS=y +CONFIG_NET_TCP=y +CONFIG_NET_TCPBACKLOG=y +CONFIG_NET_TCP_WRITE_BUFFERS=y +CONFIG_NET_UDP=y +CONFIG_NET_UDP_WRITE_BUFFERS=y +CONFIG_NFILE_DESCRIPTORS=8 +CONFIG_NFILE_STREAMS=8 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_NOMAC=y +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_MQ_MSGS=4 +CONFIG_PREALLOC_WDOGS=16 +CONFIG_RAM_SIZE=524288 +CONFIG_RAM_START=0x20200000 +CONFIG_SCHED_LPWORK=y +CONFIG_START_DAY=14 +CONFIG_START_MONTH=3 +CONFIG_SYSTEM_PING6=y +CONFIG_SYSTEM_PING=y +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/configs/imxrt1050-evk/src/Makefile b/configs/imxrt1050-evk/src/Makefile index 9028c2d927..bccd353bc4 100644 --- a/configs/imxrt1050-evk/src/Makefile +++ b/configs/imxrt1050-evk/src/Makefile @@ -58,4 +58,8 @@ ifeq ($(CONFIG_ARCH_BUTTONS),y) CSRCS += imxrt_buttons.c endif +ifeq ($(CONFIG_IMXRT_ENET),y) +CSRCS += imxrt_ethernet.c +endif + include $(TOPDIR)/configs/Board.mk diff --git a/configs/imxrt1050-evk/src/imxrt1050-evk.h b/configs/imxrt1050-evk/src/imxrt1050-evk.h index a629f2ef8f..cfbd1721c6 100644 --- a/configs/imxrt1050-evk/src/imxrt1050-evk.h +++ b/configs/imxrt1050-evk/src/imxrt1050-evk.h @@ -49,6 +49,7 @@ #include #include "imxrt_gpio.h" +#include "imxrt_iomuxc.h" /**************************************************************************** * Pre-processor Definitions @@ -76,10 +77,11 @@ * must be configured as ALT5, GPIO1_IO09 */ -#define IOMUX_LED (IOMUX_PULL_NONE | IOMUX_CMOS_OUTPUT | IOMUX_DRIVE_40OHM | \ - IOMUX_SPEED_MEDIUM | IOMUX_SLEW_SLOW) -#define GPIO_LED (GPIO_OUTPUT | GPIO_OUTPUT_ZERO | GPIO_PORT1 | GPIO_PIN9 | \ - IOMUX_LED) +#define IOMUX_LED (IOMUX_PULL_NONE | IOMUX_CMOS_OUTPUT | \ + IOMUX_DRIVE_40OHM | IOMUX_SPEED_MEDIUM | \ + IOMUX_SLEW_SLOW) +#define GPIO_LED (GPIO_OUTPUT | GPIO_OUTPUT_ZERO | GPIO_PORT1 | \ + GPIO_PIN9 | IOMUX_LED) /* Buttons * @@ -88,9 +90,28 @@ * 1. SW8 (IRQ88) GPIO5-00 * */ +#define IOMUX_SW8 (IOMUX_SLEW_FAST | IOMUX_DRIVE_50OHM | \ + IOMUX_SPEED_MEDIUM | IOMUX_PULL_UP_100K | \ + _IOMUX_PULL_ENABLE) +#define GPIO_SW8 (GPIO_INTERRUPT | GPIO_INT_FALLINGEDGE | \ + GPIO_PORT5 | GPIO_PIN0 | IOMUX_SW8) -#define GPIO_SW8 (GPIO_INTERRUPT | GPIO_INT_FALLINGEDGE | \ - GPIO_PORT5 | GPIO_PIN0) +/* GPIOAD_B0_10 */ + +#define IOMUX_ENET_INT (IOMUX_SLEW_FAST | IOMUX_DRIVE_50OHM | \ + IOMUX_SPEED_MEDIUM | IOMUX_PULL_UP_100K | \ + _IOMUX_PULL_ENABLE) +#define GPIO_ENET_INT (GPIO_OUTPUT | GPIO_OUTPUT_ONE | \ + GPIO_PORT1 | GPIO_PIN10 | IOMUX_ENET_INT) +#define GPIO_ENET_IRQ IMXRT_IRQ_GPIO1_10 + +/* GPIOAD_B0_09 */ + +#define IOMUX_ENET_RST (IOMUX_SLEW_FAST | IOMUX_DRIVE_50OHM | \ + IOMUX_SPEED_MEDIUM | IOMUX_PULL_UP_100K | \ + _IOMUX_PULL_ENABLE) +#define GPIO_ENET_RST (GPIO_OUTPUT | GPIO_OUTPUT_ZERO | \ + GPIO_PORT1 | GPIO_PIN9 | IOMUX_ENET_RST) /**************************************************************************** * Public Types diff --git a/configs/imxrt1050-evk/src/imxrt_ethernet.c b/configs/imxrt1050-evk/src/imxrt_ethernet.c new file mode 100644 index 0000000000..69a55a9898 --- /dev/null +++ b/configs/imxrt1050-evk/src/imxrt_ethernet.c @@ -0,0 +1,280 @@ +/**************************************************************************** + * configs/imxrt1050-evk/src/imxrt_ethernet.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/* Force verbose debug on in this file only to support unit-level testing. */ + +#ifdef CONFIG_NETDEV_PHY_DEBUG +# undef CONFIG_DEBUG_INFO +# define CONFIG_DEBUG_INFO 1 +# undef CONFIG_DEBUG_NET +# define CONFIG_DEBUG_NET 1 +#endif + +#include +#include +#include + +#include +#include + +#include "imxrt_gpio.h" +#include "imxrt_enet.h" + +#include "imxrt1050-evk.h" + +#ifdef CONFIG_IMXRT_ENET + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define IMXRT_ENET_DEVNAME "eth0" + +/* Debug ********************************************************************/ +/* Extra, in-depth debug output that is only available if + * CONFIG_NETDEV_PHY_DEBUG us defined. + */ + +#ifdef CONFIG_NETDEV_PHY_DEBUG +# define phyerr _err +# define phywarn _warn +# define phyinfo _info +#else +# define phyerr(x...) +# define phywarn(x...) +# define phyinfo(x...) +#endif + +/************************************************************************************ + * Private Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: imxrt_enet_phy_enable + ************************************************************************************/ + +#ifdef CONFIG_IMXRT_GPIO1_0_15_IRQ +static void imxrt_enet_phy_enable(bool enable) +{ + phyinfo("IRQ%d: enable=%d\n", GPIO_ENET_INT, enable); + + if (enable) + { + up_enable_irq(GPIO_ENET_IRQ); + } + else + { + up_disable_irq(GPIO_ENET_IRQ); + } +} +#endif + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************************ + * Function: imxrt_phy_boardinitialize + * + * Description: + * Some boards require specialized initialization of the PHY before it can be + * used. This may include such things as configuring GPIOs, resetting the PHY, + * etc. If CONFIG_IMXRT_ENET_PHYINIT is defined in the configuration then the + * board specific logic must provide imxrt_phyinitialize(); The i.MX RT Ethernet + * driver will call this function one time before it first uses the PHY. + * + * Input Parameters: + * intf - Always zero for now. + * + * Returned Value: + * OK on success; Negated errno on failure. + * + ************************************************************************************/ + +void weak_function imxrt_phy_boardinitialize(void) +{ +#ifdef CONFIG_IMXRT_GPIO1_0_15_IRQ + /* Configure the PHY interrupt pin */ + + phyinfo("Configuring interrupt: %08x\n", GPIO_ENET_INT); + imxrt_config_gpio(GPIO_ENET_INT); +#endif + + /* Configure the PHY reset pin. This will also take the PHY out of reset. */ + + phyinfo("Configuring reset: %08x\n", GPIO_ENET_RST); + imxrt_config_gpio(GPIO_ENET_RST); +} + +/**************************************************************************** + * Name: arch_phy_irq + * + * Description: + * This function may be called to register an interrupt handler that will + * be called when a PHY interrupt occurs. This function both attaches + * the interrupt handler and enables the interrupt if 'handler' is non- + * NULL. If handler is NULL, then the interrupt is detached and disabled + * instead. + * + * The PHY interrupt is always disabled upon return. The caller must + * call back through the enable function point to control the state of + * the interrupt. + * + * This interrupt may or may not be available on a given platform depending + * on how the network hardware architecture is implemented. In a typical + * case, the PHY interrupt is provided to board-level logic as a GPIO + * interrupt (in which case this is a board-specific interface and really + * should be called board_phy_irq()); In other cases, the PHY interrupt + * may be cause by the chip's MAC logic (in which case arch_phy_irq()) is + * an appropriate name. Other other boards, there may be no PHY interrupts + * available at all. If client attachable PHY interrupts are available + * from the board or from the chip, then CONFIG_ARCH_PHY_INTERRUPT should + * be defined to indicate that fact. + * + * Typical usage: + * a. OS service logic (not application logic*) attaches to the PHY + * PHY interrupt and enables the PHY interrupt. + * b. When the PHY interrupt occurs: (1) the interrupt should be + * disabled and () work should be scheduled on the worker thread (or + * perhaps a dedicated application thread). + * c. That worker thread should use the SIOCGMIIPHY, SIOCGMIIREG, + * and SIOCSMIIREG ioctl calls** to communicate with the PHY, + * determine what network event took place (Link Up/Down?), and + * take the appropriate actions. + * d. It should then interact the PHY to clear any pending + * interrupts, then re-enable the PHY interrupt. + * + * * This is an OS internal interface and should not be used from + * application space. Rather applications should use the SIOCMIISIG + * ioctl to receive a signal when a PHY event occurs. + * ** This interrupt is really of no use if the Ethernet MAC driver + * does not support these ioctl calls. + * + * Input Parameters: + * intf - Identifies the network interface. For example "eth0". Only + * useful on platforms that support multiple Ethernet interfaces + * and, hence, multiple PHYs and PHY interrupts. + * handler - The client interrupt handler to be invoked when the PHY + * asserts an interrupt. Must reside in OS space, but can + * signal tasks in user space. A value of NULL can be passed + * in order to detach and disable the PHY interrupt. + * arg - The argument that will accompany the interrupt + * enable - A function pointer that be unsed to enable or disable the + * PHY interrupt. + * + * Returned Value: + * Zero (OK) returned on success; a negated errno value is returned on + * failure. + * + ****************************************************************************/ + +#ifdef CONFIG_IMXRT_GPIO1_0_15_IRQ +int arch_phy_irq(FAR const char *intf, xcpt_t handler, void *arg, + phy_enable_t *enable) +{ + irqstate_t flags; + gpio_pinset_t pinset; + phy_enable_t enabler; + int irq; + + DEBUGASSERT(intf); + + ninfo("%s: handler=%p\n", intf, handler); + phyinfo("EMAC: devname=%s\n", IMXRT_ENET_DEVNAME); + + if (strcmp(intf, IMXRT_ENET_DEVNAME) == 0) + { + phyinfo("Select EMAC\n"); + pinset = GPIO_ENET_INT; + irq = GPIO_ENET_IRQ; + enabler = imxrt_enet_phy_enable; + } + else + { + nerr("ERROR: Unsupported interface: %s\n", intf); + return NULL; + } + + /* Disable interrupts until we are done. This guarantees that the + * following operations are atomic. + */ + + flags = spin_lock_irqsave(); + + /* Configure the interrupt */ + + if (handler) + { + /* The interrupt pin has already been configured as an interrupting + * input (by imxrt_phy_boardinitialize() above). + * + * Attach the new button handler. + */ + + phyinfo("Attach IRQ%d\n", irq); + (void)irq_attach(irq, handler, arg); + } + else + { + phyinfo("Detach IRQ%d\n", irq); + (void)irq_detach(irq); + enabler = NULL; + } + + /* Return with the interrupt disabled in either case */ + + up_disable_irq(GPIO_ENET_IRQ); + + /* Return the enabling function pointer */ + + if (enable) + { + *enable = enabler; + } + + /* Return the old handler (so that it can be restored) */ + + spin_unlock_irqrestore(flags); + return OK; +} +#endif /* CONFIG_IMXRT_GPIO1_0_15_IRQ */ + +#endif /* CONFIG_IMXRT_ENET */