This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2b7e4217747346485072066f36207e6de8a55ecf Author: Jari Nippula <[email protected]> AuthorDate: Thu Oct 29 19:20:00 2026 +0200 arch/arm/src/imxrt: NETC driver NETC ethernet controller driver for IMXRT118x Assisted-by: ChatGPT:GPT-6.1 Signed-off-by: Jari Nippula <[email protected]> --- Documentation/platforms/arm/imxrt/index.rst | 152 +++ arch/arm/include/imxrt/chip.h | 5 + arch/arm/src/imxrt/CMakeLists.txt | 4 + arch/arm/src/imxrt/Kconfig | 139 ++ arch/arm/src/imxrt/Make.defs | 4 + arch/arm/src/imxrt/hardware/imxrt_netc.h | 245 ++++ arch/arm/src/imxrt/hardware/imxrt_netc_switch.h | 96 ++ .../imxrt/hardware/rt118x/imxrt118x_memorymap.h | 29 +- arch/arm/src/imxrt/imxrt_netc.c | 1419 ++++++++++++++++++++ arch/arm/src/imxrt/imxrt_netc.h | 146 ++ arch/arm/src/imxrt/imxrt_netc_port.c | 535 ++++++++ arch/arm/src/imxrt/imxrt_netc_priv.h | 146 ++ arch/arm/src/imxrt/imxrt_netc_switch.c | 528 ++++++++ 13 files changed, 3436 insertions(+), 12 deletions(-) diff --git a/Documentation/platforms/arm/imxrt/index.rst b/Documentation/platforms/arm/imxrt/index.rst index c430b981f49..759b421515b 100644 --- a/Documentation/platforms/arm/imxrt/index.rst +++ b/Documentation/platforms/arm/imxrt/index.rst @@ -52,6 +52,158 @@ ELE firmware, and the TRDC driver configures access to RT118x memory and peripheral resources. The Cortex-M7 release helper is used by Cortex-M33 firmware to set the Cortex-M7 boot vector and start the M7 core. +NETC architecture driver +------------------------ + +``CONFIG_IMXRT_NETC`` provides a NETDEV lower-half Ethernet driver for +RT118x. ENETC0 (PF3, SI0) connects directly to a PHY on physical link 4; +ENETC1 (PF4, SI0) connects through switch CPU port 4 to selected external +ports 0--3. These are independent interfaces and may operate separately +or together; ENETC0 is not bridged to the switch. + +Required configuration +~~~~~~~~~~~~~~~~~~~~~~ + +On an RT118x target, enable ``CONFIG_NET``, ``CONFIG_NET_ETHERNET``, +``CONFIG_NETDEVICES`` and ``CONFIG_MM_IOB``. In the **NETC Ethernet ports** +menu, select at least one of the following: + +* ``CONFIG_IMXRT_NETC_ENETC0`` enables the independent direct-PHY interface + on physical link 4. +* ``CONFIG_IMXRT_NETC_ENETC1_PORT0``, ``CONFIG_IMXRT_NETC_ENETC1_PORT1``, + ``CONFIG_IMXRT_NETC_ENETC1_PORT2`` and + ``CONFIG_IMXRT_NETC_ENETC1_PORT3`` select external switch ports 0--3. + Select each required port; selected ports share one ENETC1 interface. + +These selections enable the hidden ``CONFIG_IMXRT_NETC`` symbol, which +automatically selects ``CONFIG_NETDEV_LATEINIT`` and ``CONFIG_SCHED_LPWORK``. +Do not try to enable ``CONFIG_IMXRT_NETC`` directly. + +Also ensure that the PHY drivers used by the board are enabled through +``CONFIG_ETH0_PHY_*`` and/or ``CONFIG_ETH1_PHY_*`` under +**Network Device/PHY Support**. These selections enable compilation of +the corresponding PHY drivers. A board may instead select the driver +symbols directly in its Kconfig, as the i.MXRT1180-EVK does for its enabled +NETC ports. The per-port PHY descriptors described below are required in +addition to enabling the PHY drivers. + +The driver requires ``CONFIG_IOB_BUFSIZE >= 128``, +``CONFIG_IOB_NBUFFERS >= 96`` and ``CONFIG_IOB_THROTTLE <= 16``. +The TX descriptor choices require at least 96, 160, 320 or 640 IOBs for +8, 16, 32 or 64 descriptors respectively; the default is 16 descriptors. +Also keep ``CONFIG_NET_ETH_PKTSIZE <= 1518`` and +``CONFIG_NET_LL_GUARDSIZE <= 32``, and disable ``CONFIG_NET_PROMISCUOUS``. + +For example, for a board using an RTL8201F PHY, the following configuration +fragment selects its PHY driver and switch port 0 with 16 TX and RX +descriptors:: + + CONFIG_NET=y + CONFIG_NET_ETHERNET=y + CONFIG_NETDEVICES=y + CONFIG_MM_IOB=y + CONFIG_IMXRT_NETC_ENETC1_PORT0=y + CONFIG_ETH0_PHY_RTL8201F=y + CONFIG_IMXRT_NETC_TX16=y + CONFIG_IMXRT_NETC_RX16=y + CONFIG_IOB_BUFSIZE=128 + CONFIG_IOB_NBUFFERS=160 + CONFIG_IOB_THROTTLE=0 + # CONFIG_NET_PROMISCUOUS is not set + +For the direct interface alone, replace +``CONFIG_IMXRT_NETC_ENETC1_PORT0=y`` with ``CONFIG_IMXRT_NETC_ENETC0=y``. +Enable both to use both interfaces. This fragment covers driver selection +and buffer sizing, not a complete board or network configuration. Kconfig +selection alone does not initialize NETC or configure its PHYs. Replace +the example PHY selection with the model selections needed by the board. + +Board integration +~~~~~~~~~~~~~~~~~ + +Call ``imxrt_netc_initialize(&config)`` from board late initialization with +a ``static const`` configuration using the public types in +``imxrt_netc.h``. The driver copies the configuration. Supply all interfaces +together; shared IERB setup runs once. + +* Match the board configuration to the Kconfig selections. For + ``CONFIG_IMXRT_NETC_ENETC0``, set both ``enetc[0].enabled`` and + ``enetc[0].ports[0].enabled`` to ``true``; its other port slots must be + disabled. For each selected ``CONFIG_IMXRT_NETC_ENETC1_PORTn``, set + ``enetc[1].enabled`` and ``enetc[1].ports[n].enabled`` to ``true``. + These slots describe external switch ports, not the CPU port. + Each enabled controller needs at least one enabled port. Disabled entries + are ignored; Kconfig does not populate these board configuration fields. +* Give each controller a distinct valid unicast MAC and an exclusive + ``msgintr`` instance (1--6), including its NVIC interrupt and all three + message registers. MAC changes take effect at the next interface up. + +PHY configuration +""""""""""""""""" + +Each enabled port must supply its strapped ``phyaddr`` (0--31) and +``interface`` in ``imxrt_netc_port_s``. NETC selects PHYLIB; board Kconfig +must enable the corresponding PHY drivers. PHYLIB matches drivers by the +hardware PHY ID and owns model-specific initialization and link decoding. +MDIO endpoints are hardware-selected, not board-selectable. + +Set ``interface`` to ``PHY_INTERFACE_MII``, ``PHY_INTERFACE_RMII``, +``PHY_INTERFACE_RMII_PHY_CLK_OUT``, or an appropriate +``PHY_INTERFACE_RGMII`` variant. For RMII, use ``PHY_INTERFACE_RMII`` when +NETC supplies the 50 MHz reference clock, or +``PHY_INTERFACE_RMII_PHY_CLK_OUT`` when the PHY supplies it. NETC and the +PHY driver derive clock direction from this single interface setting. + +Enable ``CONFIG_IMXRT_NETC_PHYINIT`` only if the board needs the optional +``board_netc_phyinit(unsigned int if_idx, +const struct imxrt_netc_port_s *config)`` hook, and provide that function +in board code. It runs for each enabled port during port preparation, +after interface mode and clock-direction setup but before MDIO probing. +``if_idx`` identifies physical link 0--4. Use this hook for board-specific +GPIO/reset setup and complete any required reset delays before returning. + +The separate optional ``phy.ops->phyinit`` callback runs after PHY +identification and before advertisement. It receives a PHY descriptor +with the resolved address, MDIO operations and the hardware-selected +MDIO endpoint. Use these supplied parameters for PHY-specific register +setup, and restore the default PHY page before returning. + +The board owns pinmux, clock direction, PHY reset/timing, NETCMIX reset +release and TRDC permissions. Stabilize the NETC root clock before +initialization and keep it unchanged; its frequency must be supported and +in whole MHz. RMII MAC clock output requires SION on the reference-clock +pad. Apply RGMII delays at only one end and advertise only board/PHY-supported +modes. + +DMA uses aligned heap allocations with cache maintenance and address +translation through ``imxrt_dma_address()``. Retain the supported TCM +partition (256 KB M7 DTCM or 128 KB M33 system TCM) and ensure system-bus +accessibility, TRDC permissions and external-memory setup. Size the common +IOB pool for aggregate traffic when both interfaces operate. + +Operation and validation +~~~~~~~~~~~~~~~~~~~~~~~~ + +Interface names depend on successful registrations and other network +devices; identify interfaces by MAC rather than hardware index. +Initialization stops at the first error, leaving earlier registrations +active. Do not blindly retry after partial success. + +ENETC0 enables its MAC after full-duplex negotiation and removes carrier +on link loss or PHY errors. ENETC1 carrier requires an operational CPU path +and at least one selected external link. Interface down and TX-stall +recovery reset only the affected ENETC function; external switching and +link monitoring continue for ENETC1. Failed recovery leaves the interface +failed, and DMA references are retained if quiesce/reset fails. + +PHY ioctls use Clause 22 registers 0--31. ENETC0 uses the resolved PHY +address directly; ENETC1 encodes the switch port in ``phy_id[9:8]`` and +address in ``phy_id[4:0]``. ``SIOCGMIIPHY`` preserves the requested port. +Register access returns ``ENETDOWN`` after interface down disables PF +memory access; address queries do not access MDIO. MDIO helpers require +task context and an initialized endpoint; runtime multi-register PHY +paging requires the network lock. + Data and Instruction Cache ========================== diff --git a/arch/arm/include/imxrt/chip.h b/arch/arm/include/imxrt/chip.h index 82e2218bbe0..918f600663f 100644 --- a/arch/arm/include/imxrt/chip.h +++ b/arch/arm/include/imxrt/chip.h @@ -93,6 +93,11 @@ # error "Unknown i.MX RT chip type" #endif +#if defined(CONFIG_ARCH_FAMILY_IMXRT118x) +# define IMXRT_NETC_NENETC 2 /* Two eth controllers in NETC */ +# define IMXRT_NETC_SW0_NPORTS 4 /* Four ports in NETC switch 0 */ +#endif + /* NVIC priority levels *****************************************************/ /* Each priority field holds an 8-bit priority value, 0-15. The lower the diff --git a/arch/arm/src/imxrt/CMakeLists.txt b/arch/arm/src/imxrt/CMakeLists.txt index c3eee67567a..0fa80c4402f 100644 --- a/arch/arm/src/imxrt/CMakeLists.txt +++ b/arch/arm/src/imxrt/CMakeLists.txt @@ -141,6 +141,10 @@ if(CONFIG_IMXRT_ENET) list(APPEND SRCS imxrt_enet.c) endif() +if(CONFIG_IMXRT_NETC) + list(APPEND SRCS imxrt_netc.c imxrt_netc_port.c imxrt_netc_switch.c) +endif() + if(CONFIG_IMXRT_LPI2C) list(APPEND SRCS imxrt_lpi2c.c) endif() diff --git a/arch/arm/src/imxrt/Kconfig b/arch/arm/src/imxrt/Kconfig index 17013e26c15..c5634ce844d 100644 --- a/arch/arm/src/imxrt/Kconfig +++ b/arch/arm/src/imxrt/Kconfig @@ -257,6 +257,7 @@ config ARCH_FAMILY_IMXRT118x select IMXRT_HAVE_LPUART10 select IMXRT_HAVE_LPUART11 select IMXRT_HAVE_LPUART12 + select IMXRT_HAVE_NETC select IMXRT_CLOCKCONFIG_VER3 select IMXRT_ADC_VER3 select IMXRT_IOMUX_VER3 @@ -587,9 +588,147 @@ config IMXRT_USBDEV config IMXRT_ENET bool "Ethernet" default n + depends on !ARCH_FAMILY_IMXRT118x select ARCH_HAVE_PHY select ARCH_HAVE_NETDEV_STATISTICS +config IMXRT_HAVE_NETC + bool + +menu "NETC Ethernet ports" + depends on IMXRT_HAVE_NETC && NET && NET_ETHERNET + # Include the remaining existing NETC prerequisites here. + +config IMXRT_NETC_ENETC0 + bool "NETC Eth0 port" + default n + select IMXRT_NETC + ---help--- + Enable the independent ENETC0 direct-PHY interface on + physical link 4. + +config IMXRT_NETC_ENETC1_PORT0 + bool "NETC Eth1 switch port0" + default n + select IMXRT_NETC + +config IMXRT_NETC_ENETC1_PORT1 + bool "NETC Eth1 switch port1" + default n + select IMXRT_NETC + +config IMXRT_NETC_ENETC1_PORT2 + bool "NETC Eth1 switch port2" + default n + select IMXRT_NETC + +config IMXRT_NETC_ENETC1_PORT3 + bool "NETC Eth1 switch port3" + default n + select IMXRT_NETC + +endmenu + +if IMXRT_NETC_ENETC0 || IMXRT_NETC_ENETC1_PORT0 || \ + IMXRT_NETC_ENETC1_PORT1 || IMXRT_NETC_ENETC1_PORT2 || \ + IMXRT_NETC_ENETC1_PORT3 + +config IMXRT_NETC + bool + default y + select ARCH_HAVE_PHY + select ARCH_HAVE_NETDEV_STATISTICS + select MDIO_BUS + select NET_PHYLIB + select NETDEV_LATEINIT + ---help--- + NETDEV lower-half Ethernet interfaces for direct-PHY ENETC0 and + switch-attached ENETC1. Selected switch ports share one ENETC1 + interface. + +menu "NETC settings" + depends on IMXRT_NETC + +choice + prompt "NETC transmit descriptors" + default IMXRT_NETC_TX16 + ---help--- + Two 16-byte hardware descriptors form one cache-isolated packet + slot. One slot remains unused to distinguish full from empty. + Pool constraints reserve room for chained 1518-byte packets, + stack work and an IOB throttle reserve of up to 16 buffers. + +config IMXRT_NETC_TX8 + bool "8 descriptors (3 packets)" + depends on IOB_NBUFFERS >= 96 + +config IMXRT_NETC_TX16 + bool "16 descriptors (7 packets)" + depends on IOB_NBUFFERS >= 160 + +config IMXRT_NETC_TX32 + bool "32 descriptors (15 packets)" + depends on IOB_NBUFFERS >= 320 + +config IMXRT_NETC_TX64 + bool "64 descriptors (31 packets)" + depends on IOB_NBUFFERS >= 640 + +endchoice + +config IMXRT_NETC_NTXDESC + int + default 8 if IMXRT_NETC_TX8 + default 16 if IMXRT_NETC_TX16 + default 32 if IMXRT_NETC_TX32 + default 64 if IMXRT_NETC_TX64 + +choice + prompt "NETC receive descriptors" + default IMXRT_NETC_RX16 + +config IMXRT_NETC_RX8 + bool "8 descriptors" + +config IMXRT_NETC_RX16 + bool "16 descriptors" + +config IMXRT_NETC_RX32 + bool "32 descriptors" + +config IMXRT_NETC_RX64 + bool "64 descriptors" + +endchoice + +config IMXRT_NETC_NRXDESC + int + default 8 if IMXRT_NETC_RX8 + default 16 if IMXRT_NETC_RX16 + default 32 if IMXRT_NETC_RX32 + default 64 if IMXRT_NETC_RX64 + +config IMXRT_NETC_LINK_POLL_MS + int "NETC MAC update and watchdog interval (milliseconds)" + default 1000 + range 100 1000 + ---help--- + Interval for updating MAC modes and carrier from PHYLIB link + state and checking for stalled TX. TX recovery resets the + affected CPU function. + +config IMXRT_NETC_PHYINIT + bool "Board-specific PHY Initialization" + default n + ---help--- + Provide board_netc_phyinit() for per-port setup after interface + mode and clock-direction setup, before MDIO probing. It receives + the physical link index (0..4) and port configuration. Complete + any reset delays before returning. + +endmenu # "NETC settings" +endif + config IMXRT_LCD bool "LCD controller" default n diff --git a/arch/arm/src/imxrt/Make.defs b/arch/arm/src/imxrt/Make.defs index e05641963ed..66494632315 100644 --- a/arch/arm/src/imxrt/Make.defs +++ b/arch/arm/src/imxrt/Make.defs @@ -146,6 +146,10 @@ ifeq ($(CONFIG_IMXRT_RNG),y) CHIP_CSRCS += imxrt_rng.c endif +ifeq ($(CONFIG_IMXRT_NETC),y) +CHIP_CSRCS += imxrt_netc.c imxrt_netc_port.c imxrt_netc_switch.c +endif + ifeq ($(CONFIG_IMXRT_LPI2C),y) CHIP_CSRCS += imxrt_lpi2c.c endif diff --git a/arch/arm/src/imxrt/hardware/imxrt_netc.h b/arch/arm/src/imxrt/hardware/imxrt_netc.h new file mode 100644 index 00000000000..1f23609f551 --- /dev/null +++ b/arch/arm/src/imxrt/hardware/imxrt_netc.h @@ -0,0 +1,245 @@ +/**************************************************************************** + * arch/arm/src/imxrt/hardware/imxrt_netc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_NETC_H +#define __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_NETC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdint.h> +#include "hardware/imxrt_memorymap.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Base address helpers. + * (IMXRT1180RM Rev. 10, chapters 3, 53 and 54.) + */ + +#define IMXRT_NETC_PF(n) (IMXRT_NETC_ECAM_BASE + (n) * 0x1000) +#define IMXRT_NETC_EMDIO_REGS (IMXRT_EMDIO_BASE + 0x1c00) +#define IMXRT_NETC_ENETC0_MDIO_REGS (IMXRT_ENETC0_ETH_MAC_BASE + 0xc00) +#define IMXRT_NETC_PORT_MDIO(n) (IMXRT_SW0_BASE + 0x5c00 + (n) * 0x4000) +#define IMXRT_NETC_SW_PORT(n) (IMXRT_SW0_BASE + ((n) + 1) * 0x4000) +#define IMXRT_NETC_SW_MAC(n) (IMXRT_NETC_SW_PORT(n) + 0x1000) + +/* BLK_CTRL_WAKEUP (IMXRT1180RM Rev. 10, section 16.6.1.) + * These protocol encodings differ from NETC PMIFR. + */ + +#define IMXRT_NETC_PORT_MISC_CFG (IMXRT_BLK_CTRL_WAKEUPMIX_BASE + 0x24) +#define NETC_PORT_MISC_RMII_CLK_OUT(n) (1u << (n)) +#define IMXRT_NETC_LINK_CFG(n) (IMXRT_BLK_CTRL_WAKEUPMIX_BASE + \ + 0x100 + (n) * 4) +#define NETC_LINK_CFG_MII_PROT_MASK 0x0fu +#define NETC_LINK_CFG_MII 0u +#define NETC_LINK_CFG_RMII 1u +#define NETC_LINK_CFG_RGMII 2u +#define NETC_LINK_CFG_REVMII (1u << 31) + +#define NETC_PRB_NETCRR 0x0100 +#define NETC_PRB_NETCSR 0x0104 +#define NETC_PRB_SR (1 << 0) +#define NETC_PRB_LOCK (1 << 1) +#define NETC_PRB_ERROR (1 << 0) +#define NETC_PRB_STATE (1 << 1) +#define NETC_IERB_CLKFR 0x0178 +#define NETC_IERB_CLKCR 0x017c +#define NETC_IERB_CLOCK(mhz, ns) ((mhz) | ((ns) << 16)) + +/* Nonzero whole-MHz FREQ encoding limits, not device operating limits. */ + +#define NETC_IERB_CLKCR_MIN_HZ 1000000u +#define NETC_IERB_CLKCR_MAX_HZ 2047000000u + +#define NETC_IERB_LINKBCR(n) (0x1010 + (n) * 0x40) +#define NETC_IERB_PHYADDR_MASK (31 << 8) +#define NETC_IERB_PHYADDR(n) ((uint32_t)(n) << 8) + +#define NETC_MSGINTR_MSIIR 0x00 +#define NETC_MSGINTR_MSIR 0x04 +#define NETC_MSIX_ADDRESS 0x00 +#define NETC_MSIX_ADDRESS_HI 0x04 +#define NETC_MSIX_DATA 0x08 +#define NETC_MSIX_CONTROL 0x0c +#define NETC_MSIX_VECTOR_MASK 1 + +/* ECAM configuration offsets and masks. + * (IMXRT1180RM Rev. 10, sections 53.4.2.2 and 53.4.6.) + */ + +#define NETC_PCI_CMD 0x0004 +#define NETC_PCI_CMD_MEM_ACCESS (1 << 1) +#define NETC_PCI_CMD_ENABLE 0x0006 +#define NETC_PCI_DEVCTL 0x0048 +#define NETC_PCI_FLR (1 << 15) +#define NETC_PCI_MSIXCTL 0x0082 +#define NETC_PCI_MSIX_ENABLE (1 << 15) +#define NETC_PCI_MSIX_MASK (1 << 14) +#define NETC_PCI_SRIOVCTL 0x0158 +#define NETC_PCI_VF_ENABLE (1 << 0) + +/* Switch and ENETC common registers (IMXRT1180RM sec. 53.4.6.7). */ + +#define NETC_COMMON_OSR 0x1010 +#define NETC_COMMON_STATE (1 << 0) +#define NETC_COMMON_ITM_STATE (1 << 1) + +#define NETC_SI_SIMR 0x0000 +#define NETC_SI_EN (1u << 31) +#define NETC_SI_SIRBGCR 0x0038 +#define NETC_SI_SIBCAR 0x0040 +#define NETC_DMA_NONCOHERENT 0x02020202 +#define NETC_SI_SIIPVBDRMR0 0x0150 +#define NETC_SI_SIMSITRVR(n) (0x0b00 + (n) * 4) +#define NETC_SI_SIMSIRRVR(n) (0x0b80 + (n) * 4) + +#define NETC_TX_BASE(n) (0x8000 + (n) * 0x200) +#define NETC_RX_BASE(n) (0x8100 + (n) * 0x200) +#define NETC_BDR_MR 0x00 +#define NETC_BDR_EN (1u << 31) +#define NETC_RX_EXTENDED (1 << 2) +#define NETC_BDR_SR 0x04 +#define NETC_BDR_SBE (1 << 16) +#define NETC_RX_BSR 0x08 +#define NETC_RX_CIR 0x0c +#define NETC_BDR_BAR0 0x10 +#define NETC_BDR_BAR1 0x14 +#define NETC_BDR_PIR 0x18 +#define NETC_TX_CIR 0x1c +#define NETC_BDR_LENR 0x20 +#define NETC_BDR_IER 0xa0 +#define NETC_BDR_IDR 0xa4 +#define NETC_TX_FRAME_IE (1 << 1) +#define NETC_RX_THRESHOLD_IE (1 << 0) +#define NETC_BDR_ICR0 0xa8 +#define NETC_RX_COALESCE (1u << 31) +#define NETC_RX_THRESHOLD 1 +#define NETC_BDR_ICR1 0xac +#define NETC_BDR_INDEX_MASK 0xffff + +#define NETC_ENETC_PMR 0x0010 +#define NETC_ENETC_SI0EN (1 << 16) +#define NETC_ENETC_PARCSCR 0x009c +#define NETC_ENETC_PSIPMMR 0x0200 +#define NETC_ENETC_MCPROMISC (1 << 16) +#define NETC_ENETC_PSIPVMR 0x0204 +#define NETC_ENETC_VLANPROMISC (1 << 0) +#define NETC_ENETC_VLANUNTAGGED (1 << 16) +#define NETC_ENETC_PRIO2TCMR0 0x0380 +#define NETC_ENETC_SMHRBDRMR(n) (0x0880 + ((n) - 1) * 4) +#define NETC_ENETC_PSICFGR0 0x2010 +#define NETC_ENETC_PSICFGR2 0x2018 +#define NETC_ENETC_RINGS(tx, rx) ((uint32_t)(tx) | ((uint32_t)(rx) << 16)) + +#define NETC_PORT_PCR 0x0010 +#define NETC_PORT_PSPEED_MASK (0x3fffu << 16) +#define NETC_PORT_PSPEED(n) ((uint32_t)(n) << 16) +#define NETC_PORT_PMAR0 0x0020 +#define NETC_PORT_PMAR1 0x0024 +#define NETC_PORT_POR 0x0100 +#define NETC_PORT_DISABLE 3 +#define NETC_PORT_PSR 0x0104 +#define NETC_PORT_BUSY 3 +#define NETC_PORT_RXBUSY (1 << 1) +#define NETC_PORT_PRXSDUOR 0x0108 +#define NETC_PORT_PTXSDUOR 0x010c +#define NETC_PORT_PTCTMSDUR(n) (0x0208 + (n) * 0x20) +#define NETC_PORT_MAXSDU_DISABLE (1 << 24) +#define NETC_PSEUDO_PPMCR 0x0010 +#define NETC_PSEUDO_TXPAD (1 << 16) + +#define NETC_MAC_CMD 0x0008 +#define NETC_MAC_TX (1 << 0) +#define NETC_MAC_RX (1 << 1) +#define NETC_MAC_TXPAD (1 << 15) +#define NETC_MAC_MAXFRM 0x0014 +#define NETC_MAC_MINFRM 0x0018 +#define NETC_MAC_IEVENT 0x0040 +#define NETC_MAC_TXEMPTY (1 << 5) +#define NETC_MAC_RXEMPTY (1 << 6) +#define NETC_MAC_IFMODE 0x0300 +#define NETC_MAC_IF_MASK (7 | (1 << 4) | (1 << 6) | (3 << 13)) +#define NETC_MAC_IF_MII 1 +#define NETC_MAC_IF_RMII 3 +#define NETC_MAC_IF_RGMII 4 +#define NETC_MAC_IF_10M (1 << 4) +#define NETC_MAC_IF_HALF (1 << 6) +#define NETC_MAC_IF_SSP(n) ((uint32_t)(n) << 13) + +#define NETC_MDIO_CFG 0x00 +#define NETC_MDIO_BUSY (1u << 31) +#define NETC_MDIO_READ_ERROR (1 << 1) +#define NETC_MDIO_ADDR_ERROR (1 << 28) +#define NETC_MDIO_COMPLETE (1 << 30) +#define NETC_MDIO_C45 (1 << 6) +#define NETC_MDIO_NEG (1 << 23) +#define NETC_MDIO_DIV(n) ((uint32_t)(n) << 7) +#define NETC_MDIO_HOLD(n) ((uint32_t)(n) << 2) +#define NETC_MDIO_CTL 0x04 +#define NETC_MDIO_DATA 0x08 +#define NETC_MDIO_READ (1 << 15) +#define NETC_MDIO_PHY(n) ((uint32_t)(n) << 5) + +#define NETC_BD_FINAL (1u << 31) +#define NETC_TX_BD_EXTENDED (1 << 30) +#define NETC_TX_BD_INTERRUPT (1 << 29) +#define NETC_TX_BD_WRITTEN (1 << 26) +#define NETC_TX_BD_ERROR_MASK (0x1ff << 16) +#define NETC_RX_BD_ERROR_MASK (0xff << 16) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* A TX packet uses two hardware BDs; the extension has no offloads enabled. + * RX uses the hardware's extended 32-byte format. Each independently owned + * entry occupies a complete Cortex-M7/M33 cache line. + */ + +struct imxrt_netc_txdesc_s +{ + uint32_t address; + uint32_t address_hi; + uint16_t length; + uint16_t frame_length; + uint32_t flags; + uint32_t timestamp; + uint32_t vlan; + uint32_t reserved; + uint32_t extension; +}; + +struct imxrt_netc_rxdesc_s +{ + uint32_t address; + uint32_t address_hi; + uint32_t length; + uint32_t flags; + uint32_t timestamp; + uint32_t reserved[3]; +}; + +#endif /* __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_NETC_H */ diff --git a/arch/arm/src/imxrt/hardware/imxrt_netc_switch.h b/arch/arm/src/imxrt/hardware/imxrt_netc_switch.h new file mode 100644 index 00000000000..7fc09319835 --- /dev/null +++ b/arch/arm/src/imxrt/hardware/imxrt_netc_switch.h @@ -0,0 +1,96 @@ +/**************************************************************************** + * arch/arm/src/imxrt/hardware/imxrt_netc_switch.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_NETC_SWITCH_H +#define __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_NETC_SWITCH_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* IMXRT1180RM Rev. 10, sections 53.4.2.2.2, 53.4.2.4.5 and 53.4.6.5. */ + +#define NETC_SW_CCAR 0x0080 +#define NETC_SW_CBDRMR 0x0800 +#define NETC_SW_CBDRBAR0 0x0810 +#define NETC_SW_CBDRBAR1 0x0814 +#define NETC_SW_CBDRPIR 0x0818 +#define NETC_SW_CBDRCIR 0x081c +#define NETC_SW_CBDRLENR 0x0820 +#define NETC_SW_VFHTDECR0 0x2010 +#define NETC_SW_CPU (1 << 4) +#define NETC_SW_VFHTDECR1 0x2014 +#define NETC_SW_SHARED_FID ((0xffffu << 16) | (1 << 12)) +#define NETC_SW_VFHTDECR2 0x2018 +#define NETC_SW_LEARN_FLOOD ((2 << 24) | (2 << 27)) +#define NETC_SW_FDBHTMCR 0x2024 + +#define NETC_SW_PTAR 0x0050 +#define NETC_SW_STANDARD_TPIDS 0x33 +#define NETC_SW_PQOSMR 0x0054 +#define NETC_SW_PIPFCR 0x0084 +#define NETC_SW_PDGSR 0x0138 +#define NETC_SW_PCTFCR 0x0450 +#define NETC_SW_STORE_FORWARD 3 +#define NETC_SW_PISIDCR 0x0460 +#define NETC_SW_NULL_STREAM (0xffffu << 16) +#define NETC_SW_PIPV2QMR0 0x0470 +#define NETC_SW_BPCR 0x0500 +#define NETC_SW_BPDVR 0x0510 +#define NETC_SW_DEFAULT_VLAN (1 | (1 << 24) | (15 << 20)) +#define NETC_SW_BPSTGSR 0x0520 +#define NETC_SW_STG_FORWARD 2 + +#define NETC_NTMP_FDB 15 +#define NETC_NTMP_DELETE 1 +#define NETC_NTMP_ADDQUERYUPDATE 14 +#define NETC_NTMP_EXACT (1 << 12) +#define NETC_NTMP_VERSION2 (2 << 24) +#define NETC_NTMP_NPF (1 << 15) +#define NETC_NTMP_READY (1u << 31) +#define NETC_NTMP_ERROR_MASK (0xfff << 16) +#define NETC_NTMP_FDB_CFGEU (1 << 0) +#define NETC_NTMP_FDB_REQUEST 48 +#define NETC_NTMP_FDB_DELETE 36 +#define NETC_NTMP_FDB_RESPONSE 36 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct imxrt_netc_cmd_s +{ + uint32_t address; + uint32_t address_hi; + uint32_t lengths; + uint32_t command; + uint32_t reserved[3]; + uint32_t format; +}; + +#endif /* __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_NETC_SWITCH_H */ diff --git a/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h b/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h index 25f8b5dc660..ef91f5b33d1 100644 --- a/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h +++ b/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h @@ -272,20 +272,25 @@ /* NETCMIX Domain (0x6000_0000 - 0x60FF_FFFF) *******************************/ +#define IMXRT_NETC_ECAM_BASE (0x60000000ul) /* Enhanced Configuration Access Mechanism */ +#define IMXRT_NETC_IERB_BASE (0x60800000ul) /* Integrated Endpoint Register Block */ +#define IMXRT_NETC_PRB_BASE (0x60900000ul) /* Privileged Register Block */ +#define IMXRT_SW0_BASE (0x60a00000ul) /* Switch base registers */ +#define IMXRT_SW0_GLOBAL_BASE (0x60a80000ul) /* Switch Global registers */ #define IMXRT_ENETC0_SI0_BASE (0x60b00000ul) /* ENETC0 station interface 0 */ -#define IMXRT_ENETC0_BASE (0x60b10000ul) /* ENETC0 common regs */ -#define IMXRT_ENETC0_PORT_BASE (0x60b14000ul) /* ENETC0 port regs */ -#define IMXRT_ENETC0_ETH_MAC_BASE (0x60b15000ul) /* ENETC0 MAC regs */ -#define IMXRT_ENETC0_GLOBAL_BASE (0x60b20000ul) -#define IMXRT_ENETC1_SI0_BASE (0x60b40000ul) -#define IMXRT_ENETC1_BASE (0x60b50000ul) -#define IMXRT_ENETC1_PORT_BASE (0x60b54000ul) -#define IMXRT_ENETC1_PSEUDO_MAC_BASE (0x60b55000ul) -#define IMXRT_ENETC1_GLOBAL_BASE (0x60b60000ul) -#define IMXRT_ENETC1_SI1_BASE (0x60c10000ul) -#define IMXRT_EMDIO_BASE (0x60ba0000ul) /* External MDIO */ +#define IMXRT_ENETC0_BASE (0x60b10000ul) /* ENETC0 base registers */ +#define IMXRT_ENETC0_PORT_BASE (0x60b14000ul) /* ENETC0 port registers */ +#define IMXRT_ENETC0_ETH_MAC_BASE (0x60b15000ul) /* ENETC0 MAC registers */ +#define IMXRT_ENETC0_GLOBAL_BASE (0x60b20000ul) /* ENETC0 Global registers */ +#define IMXRT_ENETC1_SI0_BASE (0x60b40000ul) /* ENETC1 station interface 0 */ +#define IMXRT_ENETC1_BASE (0x60b50000ul) /* ENETC1 base registers */ +#define IMXRT_ENETC1_PORT_BASE (0x60b54000ul) /* ENETC1 port registers */ +#define IMXRT_ENETC1_ETH_MAC_BASE (0x60b55000ul) /* ENETC1 MAC registers */ +#define IMXRT_ENETC1_GLOBAL_BASE (0x60b60000ul) /* ENETC1 Global registers */ #define IMXRT_TMR0_BASE (0x60b80000ul) /* NETC 1588 Timer */ -#define IMXRT_SW0_GLOBAL_BASE (0x60a80000ul) /* Switch 0 global */ +#define IMXRT_EMDIO_BASE (0x60ba0000ul) /* External MDIO */ +#define IMXRT_NETC_ENETC0_MSIX_BASE (0x60bf0000ul) +#define IMXRT_NETC_ENETC1_MSIX_BASE (0x60c00000ul) /* CM7 Private Peripheral Bus (0xE000_0000 - 0xE00F_FFFF) *******************/ diff --git a/arch/arm/src/imxrt/imxrt_netc.c b/arch/arm/src/imxrt/imxrt_netc.c new file mode 100644 index 00000000000..d1071c80540 --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_netc.c @@ -0,0 +1,1419 @@ +/**************************************************************************** + * arch/arm/src/imxrt/imxrt_netc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <stddef.h> +#include <string.h> +#include <sys/param.h> +#include <debug.h> +#include <endian.h> +#include <nuttx/clock.h> +#include <nuttx/irq.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> +#include <nuttx/wqueue.h> +#include <nuttx/net/ethernet.h> +#include <nuttx/net/mii.h> +#include <nuttx/net/netdev_lowerhalf.h> + +#include "imxrt_netc_priv.h" +#include "imxrt_clockconfig.h" +#include "imxrt_dtcm.h" +#include "hardware/imxrt_netc.h" +#include "hardware/imxrt_src.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#if !defined(CONFIG_SCHED_WORKQUEUE) +# error NETC requires work queue support +#else + + /* Select work queue. Always use the LP work queue if available. If not, + * then LPWORK will re-direct to the HP work queue. + * + * NOTE: However, the network should NEVER run on the high priority work + * queue! That queue is intended only to service short back end interrupt + * processing that never suspends. Suspending the high priority work queue + * may bring the system to its knees! + */ + +# define ETHWORK LPWORK +#endif + +#ifdef CONFIG_NET_PROMISCUOUS +# error "NETC does not support promiscuous mode; disable CONFIG_NET_PROMISCUOUS" +#endif +#if CONFIG_NET_ETH_PKTSIZE > 1518 +# error "NETC requires CONFIG_NET_ETH_PKTSIZE <= 1518" +#endif +#if CONFIG_NET_LL_GUARDSIZE > 32 +# error "NETC requires CONFIG_NET_LL_GUARDSIZE <= 32" +#endif +#if CONFIG_IOB_BUFSIZE < 128 +# error "NETC requires CONFIG_IOB_BUFSIZE >= 128" +#endif +#if CONFIG_IOB_NBUFFERS < 96 +# error "NETC requires CONFIG_IOB_NBUFFERS >= 96" +#endif +#if CONFIG_IOB_THROTTLE > 16 +# error "NETC requires CONFIG_IOB_THROTTLE <= 16" +#endif + +#define NETC_LINK_INTERVAL MSEC2TICK(CONFIG_IMXRT_NETC_LINK_POLL_MS) +#define NETC_TX_TIMEOUT SEC2TICK(5) +#define NETC_TX_RING 1 +#define TXREG(p, r) ((p)->hw->si + NETC_TX_BASE(NETC_TX_RING) + (r)) +#define RXREG(p, r) ((p)->hw->si + NETC_RX_BASE(0) + (r)) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +enum imxrt_netc_state_e +{ + NETC_UNINITIALIZED = 0, + NETC_DOWN, + NETC_UP, + NETC_RECOVERING, + NETC_FAILED +}; + +struct imxrt_netc_dma_s +{ + struct imxrt_netc_txdesc_s tx[NETC_TX_SLOTS]; + struct imxrt_netc_rxdesc_s rx[NETC_RX_SLOTS] + aligned_data(NETC_DMA_ALIGN); + uint8_t txbuffer[NETC_TX_SLOTS][NETC_BUFFER_SIZE] + aligned_data(NETC_DMA_ALIGN); + uint8_t rxbuffer[NETC_RX_SLOTS][NETC_BUFFER_SIZE] + aligned_data(NETC_DMA_ALIGN); +}; + +struct imxrt_netc_s +{ + struct netdev_lowerhalf_s lower; + const struct imxrt_netc_hw_s *hw; + struct imxrt_netc_enetc_s config; + struct imxrt_netc_link_s link; + struct imxrt_netc_dma_s *dma_cpu; + uint32_t dma_bus; + enum imxrt_netc_state_e state; + struct work_s linkwork; + netpkt_t *txpacket[NETC_TX_SLOTS]; + clock_t txtime[NETC_TX_SLOTS]; + uintptr_t msgintr; + int irq; + uint16_t txprod; + uint16_t txclean; + uint16_t rxcons; + bool rxdiscard; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int imxrt_netc_ifup(struct netdev_lowerhalf_s *lower); +static int imxrt_netc_ifdown(struct netdev_lowerhalf_s *lower); +static int imxrt_netc_transmit(struct netdev_lowerhalf_s *lower, + netpkt_t *packet); +static netpkt_t *imxrt_netc_receive(struct netdev_lowerhalf_s *lower); +static void imxrt_netc_reclaim(struct netdev_lowerhalf_s *lower); +#ifdef CONFIG_NET_MCASTGROUP +static int imxrt_netc_multicast(struct netdev_lowerhalf_s *lower, + const uint8_t *mac); +#endif +#ifdef CONFIG_NETDEV_IOCTL +static int imxrt_netc_ioctl(struct netdev_lowerhalf_s *lower, int cmd, + unsigned long arg); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct netdev_ops_s g_netc_ops = +{ + .ifup = imxrt_netc_ifup, + .ifdown = imxrt_netc_ifdown, + .transmit = imxrt_netc_transmit, + .receive = imxrt_netc_receive, + .reclaim = imxrt_netc_reclaim, +#ifdef CONFIG_NET_MCASTGROUP + .addmac = imxrt_netc_multicast, + .rmmac = imxrt_netc_multicast, +#endif +#ifdef CONFIG_NETDEV_IOCTL + .ioctl = imxrt_netc_ioctl, +#endif +}; + +static const struct imxrt_netc_hw_s g_netc_hw[IMXRT_NETC_NENETC] = +{ + { + .si = IMXRT_ENETC0_SI0_BASE, + .enetc = IMXRT_ENETC0_BASE, + .port = IMXRT_ENETC0_PORT_BASE, + .mac = IMXRT_ENETC0_ETH_MAC_BASE, + .function = IMXRT_NETC_ENETC0_PF, + .msix = IMXRT_NETC_ENETC0_MSIX_BASE, + .attachment = IMXRT_NETC_DIRECT, + .portcount = 1, + .ports = + { + { + .reg_idx = 4, + .mdio = IMXRT_NETC_ENETC0_MDIO, + }, + } + }, + { + .si = IMXRT_ENETC1_SI0_BASE, + .enetc = IMXRT_ENETC1_BASE, + .port = IMXRT_ENETC1_PORT_BASE, + .mac = IMXRT_ENETC1_ETH_MAC_BASE, + .function = IMXRT_NETC_ENETC1_PF, + .msix = IMXRT_NETC_ENETC1_MSIX_BASE, + .attachment = IMXRT_NETC_SWITCH, + .portcount = IMXRT_NETC_SW0_NPORTS, + .sw_cpu_port_reg_idx = 4, + .ports = + { + { + .reg_idx = 0, + .mdio = IMXRT_NETC_PORT0_MDIO, + }, + { + .reg_idx = 1, + .mdio = IMXRT_NETC_PORT1_MDIO, + }, + { + .reg_idx = 2, + .mdio = IMXRT_NETC_PORT2_MDIO, + }, + { + .reg_idx = 3, + .mdio = IMXRT_NETC_PORT3_MDIO, + }, + }, + } +}; + +static const uintptr_t g_msgintr_base[6] = +{ + IMXRT_MSGINTR1_BASE, IMXRT_MSGINTR2_BASE, IMXRT_MSGINTR3_BASE, + IMXRT_MSGINTR4_BASE, IMXRT_MSGINTR5_BASE, IMXRT_MSGINTR6_BASE +}; + +static const int g_msgintr_irq[6] = +{ + IMXRT_IRQ_MSGINTR1, IMXRT_IRQ_MSGINTR2, IMXRT_IRQ_MSGINTR3, + IMXRT_IRQ_MSGINTR4, IMXRT_IRQ_MSGINTR5, IMXRT_IRQ_MSGINTR6 +}; + +static struct imxrt_netc_s g_netc[IMXRT_NETC_NENETC]; +static mutex_t g_initlock = NXMUTEX_INITIALIZER; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool imxrt_netc_macvalid(const uint8_t *mac) +{ + static const uint8_t zero[6]; + + return (mac[0] & 1) == 0 && memcmp(mac, zero, sizeof(zero)) != 0; +} + +static int +imxrt_netc_shared_initialize(const struct imxrt_netc_config_s *config, + uint32_t clock_hz) +{ + uintptr_t prb = IMXRT_NETC_PRB_BASE; + uintptr_t ierb = IMXRT_NETC_IERB_BASE; + uint32_t mhz = clock_hz / 1000000; + uint32_t period; + uint32_t fraction; + uint32_t status; + uint8_t phyaddr[IMXRT_NETC_PHY_PORT_COUNT]; + const struct imxrt_netc_enetc_s *enetc; + const struct imxrt_netc_hw_s *hw; + const struct imxrt_netc_port_s *port; + enum imxrt_netc_mdio_e mdio; + unsigned int link; + size_t index; + size_t portindex; + unsigned int function; + bool locked; + int ret; + + memset(phyaddr, UINT8_MAX, sizeof(phyaddr)); + for (index = 0; index < nitems(config->enetc); index++) + { + enetc = &config->enetc[index]; + if (!enetc->enabled) + { + continue; + } + + hw = &g_netc_hw[index]; + for (portindex = 0; portindex < hw->portcount; portindex++) + { + port = &enetc->ports[portindex]; + mdio = hw->ports[portindex].mdio; + + if (!port->enabled || mdio == IMXRT_NETC_EMDIO) + { + continue; + } + + link = hw->ports[portindex].reg_idx; + DEBUGASSERT(link < nitems(phyaddr)); + DEBUGASSERT(phyaddr[link] == UINT8_MAX || + phyaddr[link] == port->phyaddr); + + phyaddr[link] = port->phyaddr; + } + } + + ret = imxrt_netc_wait(prb + NETC_PRB_NETCRR, NETC_PRB_SR, 0); + if (ret < 0) + { + return ret; + } + + locked = (getreg32(prb + NETC_PRB_NETCRR) & NETC_PRB_LOCK) != 0; + if (!locked) + { + for (function = 0; function < 5; function++) + { + if ((getreg16(IMXRT_NETC_PF(function) + NETC_PCI_CMD) & + NETC_PCI_CMD_ENABLE) != 0) + { + return -EBUSY; + } + } + + period = 1000000000 / clock_hz; + fraction = ((uint64_t)(1000000000 % clock_hz) << 32) / clock_hz; + putreg32(fraction, ierb + NETC_IERB_CLKFR); + putreg32(NETC_IERB_CLOCK(mhz, period), ierb + NETC_IERB_CLKCR); + for (link = 0; link < nitems(phyaddr); link++) + { + if (phyaddr[link] != UINT8_MAX) + { + modifyreg32(ierb + NETC_IERB_LINKBCR(link), + NETC_IERB_PHYADDR_MASK, + NETC_IERB_PHYADDR(phyaddr[link])); + } + } + + modifyreg32(prb + NETC_PRB_NETCRR, 0, NETC_PRB_LOCK); + } + else if ((getreg32(ierb + NETC_IERB_CLKCR) & 0x7ff) != mhz) + { + /* Unlocking would reset every function, including another owner. */ + + return -EBUSY; + } + + for (link = 0; locked && link < nitems(phyaddr); link++) + { + if (phyaddr[link] != UINT8_MAX && + (getreg32(ierb + NETC_IERB_LINKBCR(link)) & + NETC_IERB_PHYADDR_MASK) != NETC_IERB_PHYADDR(phyaddr[link])) + { + return -EBUSY; + } + } + + ret = imxrt_netc_wait(prb + NETC_PRB_NETCSR, NETC_PRB_STATE, 0); + if (ret == OK) + { + status = getreg32(prb + NETC_PRB_NETCSR); + if ((status & NETC_PRB_ERROR) != 0) + { + nerr("NETC invalid IERB configuration: NETCSR=%08lx\n", + (unsigned long)status); + ret = -EIO; + } + } + + return ret; +} + +static void imxrt_netc_fault(struct imxrt_netc_s *priv) +{ + priv->state = NETC_RECOVERING; + up_disable_irq(priv->irq); + netdev_lower_carrier_off(&priv->lower); +} + +static void imxrt_netc_release_tx(struct imxrt_netc_s *priv) +{ + unsigned int slot; + + for (slot = 0; slot < NETC_TX_SLOTS; slot++) + { + if (priv->txpacket[slot] != NULL) + { + NETDEV_TXERRORS(&priv->lower.netdev); + netpkt_free(&priv->lower, priv->txpacket[slot], NETPKT_TX); + priv->txpacket[slot] = NULL; + } + } + + priv->txprod = 0; + priv->txclean = 0; +} + +static int imxrt_netc_stop(struct imxrt_netc_s *priv) +{ + int ret; + + up_disable_irq(priv->irq); + netdev_lower_carrier_off(&priv->lower); + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + ret = imxrt_netc_switch_cpu(false, NULL); + } + else + { + /* A failed start may not have enabled register access yet. */ + + putreg16(NETC_PCI_CMD_ENABLE, priv->hw->function + NETC_PCI_CMD); + if (priv->link.phydev != NULL) + { + phy_stop(priv->link.phydev); + } + + ret = imxrt_netc_wait(priv->hw->enetc + NETC_COMMON_OSR, + NETC_COMMON_STATE, 0); + if (ret == OK) + { + putreg32(0, priv->hw->si + NETC_SI_SIMR); + + /* ERR051936: leave half duplex before quiescing and PF reset. */ + + modifyreg32(priv->hw->mac + NETC_MAC_IFMODE, + NETC_MAC_IF_HALF, 0); + ret = imxrt_netc_port_mode(priv->hw->port, priv->hw->mac, + priv->config.ports[0].interface, 0); + if (ret == OK) + { + ret = imxrt_netc_wait(priv->hw->port + NETC_PORT_PSR, + NETC_PORT_BUSY, 0); + } + } + + priv->link.speed = 0; + } + + if (ret < 0) + { + nerr("NETC port stop failed: %d\n", ret); + priv->state = NETC_FAILED; + return ret; + } + + /* Only this PF is reset; no VFs are enabled. Pending DMA storage is + * retained unless reset and readiness checks have completed. + */ + + ret = imxrt_netc_reset(priv->hw->function, priv->hw->enetc); + if (ret < 0) + { + nerr("NETC function reset failed: %d\n", ret); + priv->state = NETC_FAILED; + return ret; + } + + putreg16(0, priv->hw->function + NETC_PCI_CMD); + imxrt_netc_release_tx(priv); + priv->state = NETC_DOWN; + return OK; +} + +static void imxrt_netc_rxarm(struct imxrt_netc_s *priv, unsigned int slot) +{ + struct imxrt_netc_rxdesc_s *desc = &priv->dma_cpu->rx[slot]; + + /* No dirty payload lines may survive the transfer back to DMA. */ + + up_invalidate_dcache((uintptr_t)priv->dma_cpu->rxbuffer[slot], + (uintptr_t)priv->dma_cpu->rxbuffer[slot] + + NETC_BUFFER_SIZE); + memset(desc, 0, sizeof(*desc)); + desc->address = htole32(priv->dma_bus + + offsetof(struct imxrt_netc_dma_s, rxbuffer) + + slot * NETC_BUFFER_SIZE); + up_clean_dcache((uintptr_t)desc, (uintptr_t)desc + sizeof(*desc)); +} + +static int imxrt_netc_start(struct imxrt_netc_s *priv) +{ + const uint8_t *mac = priv->lower.netdev.d_mac.ether.ether_addr_octet; + unsigned int slot; + unsigned int reason; + uint32_t address; + uint32_t status; + int ret; + + if (!imxrt_netc_macvalid(mac)) + { + return -EINVAL; + } + + putreg16(NETC_PCI_CMD_ENABLE, priv->hw->function + NETC_PCI_CMD); + ret = imxrt_netc_wait(priv->hw->enetc + NETC_COMMON_OSR, + NETC_COMMON_STATE, 0); + if (ret < 0) + { + return ret; + } + + status = getreg32(priv->hw->enetc + NETC_COMMON_OSR); + if ((status & NETC_COMMON_ITM_STATE) != 0) + { + nerr("NETC ENETC at %08lx index table memory overprovisioned: " + "OSR=%08lx\n", + (unsigned long)priv->hw->enetc, (unsigned long)status); + return -EIO; + } + + putreg32(NETC_PORT_DISABLE, priv->hw->port + NETC_PORT_POR); + putreg32(0, priv->hw->si + NETC_SI_SIMR); + + /* TX0 is reserved for direct switch enqueue; normal traffic uses TX1. */ + + putreg32(NETC_ENETC_RINGS(NETC_TX_RING + 1, 1), + priv->hw->enetc + NETC_ENETC_PSICFGR0); + putreg32(0, priv->hw->enetc + NETC_ENETC_PSICFGR2); + putreg32(NETC_ENETC_SI0EN, priv->hw->enetc + NETC_ENETC_PMR); + + address = mac[0] | ((uint32_t)mac[1] << 8) | + ((uint32_t)mac[2] << 16) | ((uint32_t)mac[3] << 24); + putreg32(address, priv->hw->port + NETC_PORT_PMAR0); + putreg32(mac[4] | ((uint32_t)mac[5] << 8), + priv->hw->port + NETC_PORT_PMAR1); + putreg32(NETC_ENETC_MCPROMISC, priv->hw->enetc + NETC_ENETC_PSIPMMR); + putreg32(NETC_ENETC_VLANPROMISC | NETC_ENETC_VLANUNTAGGED, + priv->hw->enetc + NETC_ENETC_PSIPVMR); + putreg32(0, priv->hw->enetc + NETC_ENETC_PARCSCR); + putreg32(0, priv->hw->enetc + NETC_ENETC_PRIO2TCMR0); + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + for (reason = 1; reason < 16; reason++) + { + putreg32(0, priv->hw->enetc + NETC_ENETC_SMHRBDRMR(reason)); + } + + imxrt_netc_pseudo(priv->hw->port); + putreg32(NETC_PSEUDO_TXPAD, priv->hw->mac + NETC_PSEUDO_PPMCR); + } + else + { + /* Negotiated speed must be reapplied after every PF reset. Keep the + * physical MAC and port gated until direct-link monitoring does so. + */ + + priv->link.speed = 0; + modifyreg32(priv->hw->mac + NETC_MAC_CMD, + NETC_MAC_TX | NETC_MAC_RX, NETC_MAC_TXPAD); + putreg32(NETC_PACKET_SIZE + 4, priv->hw->mac + NETC_MAC_MAXFRM); + putreg32(64, priv->hw->mac + NETC_MAC_MINFRM); + imxrt_netc_port_sdu(priv->hw->port); + } + + putreg32(NETC_DMA_NONCOHERENT, priv->hw->si + NETC_SI_SIBCAR); + putreg32(0, priv->hw->si + NETC_SI_SIRBGCR); + putreg32(0, priv->hw->si + NETC_SI_SIIPVBDRMR0); + priv->txprod = 0; + priv->txclean = 0; + priv->rxcons = 0; + priv->rxdiscard = false; + memset(priv->dma_cpu, 0, sizeof(*priv->dma_cpu)); + up_clean_dcache((uintptr_t)priv->dma_cpu, + (uintptr_t)priv->dma_cpu + sizeof(*priv->dma_cpu)); + + for (slot = 0; slot < NETC_RX_SLOTS; slot++) + { + imxrt_netc_rxarm(priv, slot); + } + + putreg32(priv->dma_bus + offsetof(struct imxrt_netc_dma_s, tx), + TXREG(priv, NETC_BDR_BAR0)); + putreg32(0, TXREG(priv, NETC_BDR_BAR1)); + putreg32(0, TXREG(priv, NETC_BDR_PIR)); + putreg32(0, TXREG(priv, NETC_TX_CIR)); + putreg32(CONFIG_IMXRT_NETC_NTXDESC, TXREG(priv, NETC_BDR_LENR)); + putreg32(0, TXREG(priv, NETC_BDR_ICR0)); + putreg32(0, TXREG(priv, NETC_BDR_ICR1)); + putreg32(priv->dma_bus + offsetof(struct imxrt_netc_dma_s, rx), + RXREG(priv, NETC_BDR_BAR0)); + putreg32(0, RXREG(priv, NETC_BDR_BAR1)); + putreg32(0, RXREG(priv, NETC_BDR_PIR)); + putreg32(0, RXREG(priv, NETC_RX_CIR)); + putreg32(NETC_RX_SLOTS, RXREG(priv, NETC_BDR_LENR)); + putreg32(NETC_BUFFER_SIZE, RXREG(priv, NETC_RX_BSR)); + putreg32(0, RXREG(priv, NETC_BDR_ICR1)); + putreg32(NETC_RX_COALESCE | NETC_RX_THRESHOLD, + RXREG(priv, NETC_BDR_ICR0)); + + /* ERR051622: use the fixed EA MSI-X resource, not the reported BIR. */ + + putreg32(NETC_MSIX_VECTOR_MASK, priv->hw->msix + NETC_MSIX_CONTROL); + putreg32(priv->msgintr + NETC_MSGINTR_MSIIR, + priv->hw->msix + NETC_MSIX_ADDRESS); + putreg32(0, priv->hw->msix + NETC_MSIX_ADDRESS_HI); + putreg32(0, priv->hw->msix + NETC_MSIX_DATA); + putreg32(0, priv->hw->si + NETC_SI_SIMSITRVR(NETC_TX_RING)); + putreg32(0, priv->hw->si + NETC_SI_SIMSIRRVR(0)); + getreg32(priv->msgintr + NETC_MSGINTR_MSIR); + getreg32(TXREG(priv, NETC_BDR_IDR)); + getreg32(RXREG(priv, NETC_BDR_IDR)); + putreg32(NETC_TX_FRAME_IE, TXREG(priv, NETC_BDR_IER)); + putreg32(NETC_RX_THRESHOLD_IE, RXREG(priv, NETC_BDR_IER)); + putreg32(0, priv->hw->msix + NETC_MSIX_CONTROL); + modifyreg16(priv->hw->function + NETC_PCI_MSIXCTL, + NETC_PCI_MSIX_MASK, NETC_PCI_MSIX_ENABLE); + UP_DSB(); + putreg32(NETC_BDR_EN | NETC_RX_EXTENDED, RXREG(priv, NETC_BDR_MR)); + putreg32(NETC_BDR_EN, TXREG(priv, NETC_BDR_MR)); + putreg32(NETC_SI_EN, priv->hw->si + NETC_SI_SIMR); + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + putreg32(0, priv->hw->port + NETC_PORT_POR); + ret = imxrt_netc_switch_cpu(true, mac); + if (ret < 0) + { + return ret; + } + } + else + { + ret = phy_start(priv->link.phydev); + if (ret < 0) + { + nerr("NETC direct PHY start failed: %d\n", ret); + return ret; + } + } + + priv->state = NETC_UP; + up_enable_irq(priv->irq); + return OK; +} + +static int imxrt_netc_interrupt(int irq, void *context, void *arg) +{ + struct imxrt_netc_s *priv = arg; + + getreg32(priv->msgintr + NETC_MSGINTR_MSIR); + + /* Reading IDR acknowledges events and rearms coalescing. Hardware + * generates a new interrupt on a simultaneous event/ack collision. + * Reclamation and protocol dispatch remain with the upper-half worker. + */ + + getreg32(TXREG(priv, NETC_BDR_IDR)); + getreg32(RXREG(priv, NETC_BDR_IDR)); + netdev_lower_rxready(&priv->lower); + return OK; +} + +static void imxrt_netc_carrier(struct imxrt_netc_s *priv, uint8_t linkmask) +{ + if (priv->state == NETC_UP && linkmask != 0) + { + netdev_lower_carrier_on(&priv->lower); + } + else + { + netdev_lower_carrier_off(&priv->lower); + } +} + +static uint8_t imxrt_netc_direct_link(struct imxrt_netc_s *priv) +{ + uint16_t speed; + int ret; + + if (priv->state != NETC_UP) + { + return 0; + } + + ret = imxrt_netc_port_link(&priv->link, &speed); + if (ret < 0) + { + nerr("NETC direct PHY link failed: %d\n", ret); + speed = 0; + } + + if (speed != priv->link.speed) + { + netdev_lower_carrier_off(&priv->lower); + ret = imxrt_netc_port_mode(priv->hw->port, priv->hw->mac, + priv->config.ports[0].interface, speed); + if (ret < 0) + { + nerr("NETC direct MAC mode failed: %d\n", ret); + priv->link.speed = 0; + imxrt_netc_fault(priv); + return 0; + } + + priv->link.speed = speed; + ninfo("NETC direct link: %u Mbps\n", speed); + } + + return speed != 0; +} + +static void imxrt_netc_linkwork(void *arg) +{ + struct imxrt_netc_s *priv = arg; + uint8_t linkmask = 0; + int ret; + + net_lock(); + if (priv->state == NETC_UNINITIALIZED) + { + net_unlock(); + return; + } + + /* Serialize ring access and recovery with upper-half packet callbacks. */ + + netdev_lock(&priv->lower.netdev); + + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + imxrt_netc_switch_link(&linkmask); + } + + if (priv->state == NETC_UP) + { + imxrt_netc_reclaim(&priv->lower); + if (priv->state == NETC_UP && priv->txclean != priv->txprod && + clock_systime_ticks() - priv->txtime[priv->txclean] > + NETC_TX_TIMEOUT) + { + nerr("NETC transmit timeout\n"); + NETDEV_TXTIMEOUTS(&priv->lower.netdev); + imxrt_netc_fault(priv); + } + } + + if (priv->state == NETC_RECOVERING) + { + ret = imxrt_netc_stop(priv); + if (ret == OK) + { + ret = imxrt_netc_start(priv); + } + + if (ret < 0) + { + priv->state = NETC_FAILED; + nerr("NETC recovery failed: %d\n", ret); + } + else + { + netdev_lower_rxready(&priv->lower); + } + } + + if (priv->hw->attachment == IMXRT_NETC_DIRECT) + { + linkmask = imxrt_netc_direct_link(priv); + } + + imxrt_netc_carrier(priv, linkmask); + ret = work_queue(ETHWORK, &priv->linkwork, imxrt_netc_linkwork, priv, + NETC_LINK_INTERVAL); + if (ret < 0) + { + nerr("NETC link monitoring stopped: %d\n", ret); + imxrt_netc_stop(priv); + priv->state = NETC_FAILED; + } + + netdev_unlock(&priv->lower.netdev); + net_unlock(); +} + +static int imxrt_netc_ifup(struct netdev_lowerhalf_s *lower) +{ + struct imxrt_netc_s *priv = (struct imxrt_netc_s *)lower; + uint8_t linkmask = 0; + int ret; + + if (priv->state == NETC_UP) + { + return OK; + } + + if (priv->state != NETC_DOWN) + { + return -EIO; + } + + ret = imxrt_netc_start(priv); + if (ret < 0) + { + nerr("NETC interface start failed: %d\n", ret); + imxrt_netc_stop(priv); + return ret; + } + + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + imxrt_netc_switch_link(&linkmask); + } + else + { + linkmask = imxrt_netc_direct_link(priv); + } + + imxrt_netc_carrier(priv, linkmask); + return OK; +} + +static int imxrt_netc_ifdown(struct netdev_lowerhalf_s *lower) +{ + struct imxrt_netc_s *priv = (struct imxrt_netc_s *)lower; + bool failed = priv->state == NETC_FAILED; + int ret; + + if (priv->state == NETC_DOWN) + { + return OK; + } + + ret = imxrt_netc_stop(priv); + if (failed) + { + priv->state = NETC_FAILED; + } + + if (ret < 0) + { + nerr("NETC interface stop failed: %d\n", ret); + } + + return ret; +} + +static void imxrt_netc_reclaim(struct netdev_lowerhalf_s *lower) +{ + struct imxrt_netc_s *priv = (struct imxrt_netc_s *)lower; + struct imxrt_netc_txdesc_s *desc; + unsigned int consumer; + uint32_t flags; + + if (priv->state != NETC_UP) + { + return; + } + + if ((getreg32(TXREG(priv, NETC_BDR_SR)) & NETC_BDR_SBE) != 0) + { + nerr("NETC transmit DMA bus error\n"); + imxrt_netc_fault(priv); + return; + } + + consumer = getreg32(TXREG(priv, NETC_TX_CIR)) & NETC_BDR_INDEX_MASK; + if (consumer >= CONFIG_IMXRT_NETC_NTXDESC) + { + nerr("NETC invalid transmit consumer: %u\n", consumer); + imxrt_netc_fault(priv); + return; + } + + /* The extension is the final BD. Never recycle half a descriptor pair. */ + + consumer /= 2; + if ((consumer + NETC_TX_SLOTS - priv->txclean) % NETC_TX_SLOTS > + (priv->txprod + NETC_TX_SLOTS - priv->txclean) % NETC_TX_SLOTS) + { + nerr("NETC transmit consumer exceeds outstanding packets\n"); + imxrt_netc_fault(priv); + return; + } + + UP_DMB(); + while (priv->txclean != consumer) + { + desc = &priv->dma_cpu->tx[priv->txclean]; + up_invalidate_dcache((uintptr_t)desc, (uintptr_t)desc + sizeof(*desc)); + flags = le32toh(desc->flags); + if ((flags & NETC_TX_BD_WRITTEN) != 0 && + (flags & NETC_TX_BD_ERROR_MASK) != 0) + { + nerr("NETC transmit descriptor error: %08lx\n", + (unsigned long)flags); + NETDEV_TXERRORS(&lower->netdev); + } + + DEBUGASSERT(priv->txpacket[priv->txclean] != NULL); + netpkt_free(lower, priv->txpacket[priv->txclean], NETPKT_TX); + priv->txpacket[priv->txclean] = NULL; + priv->txclean = (priv->txclean + 1) % NETC_TX_SLOTS; + netdev_lower_txdone(lower); + } +} + +static int imxrt_netc_transmit(struct netdev_lowerhalf_s *lower, + netpkt_t *packet) +{ + struct imxrt_netc_s *priv = (struct imxrt_netc_s *)lower; + struct imxrt_netc_txdesc_s *desc; + unsigned int length = netpkt_getdatalen(lower, packet); + unsigned int slot = priv->txprod; + unsigned int next = (slot + 1) % NETC_TX_SLOTS; + int ret; + + if (priv->state != NETC_UP) + { + return -ENETDOWN; + } + + if (length < ETH_HDRLEN || length > NETC_PACKET_SIZE) + { + return -EMSGSIZE; + } + + imxrt_netc_reclaim(lower); + if (priv->state != NETC_UP) + { + return -EIO; + } + + if (next == priv->txclean) + { + nerr("NETC TX quota exceeded\n"); + return -EBUSY; + } + + ret = netpkt_copyout(lower, priv->dma_cpu->txbuffer[slot], packet, + length, 0); + if (ret != length) + { + nerr("NETC TX packet copy failed: %d\n", ret); + return ret < 0 ? ret : -EIO; + } + + up_clean_dcache((uintptr_t)priv->dma_cpu->txbuffer[slot], + (uintptr_t)priv->dma_cpu->txbuffer[slot] + + NETC_BUFFER_SIZE); + desc = &priv->dma_cpu->tx[slot]; + memset(desc, 0, sizeof(*desc)); + desc->address = htole32(priv->dma_bus + + offsetof(struct imxrt_netc_dma_s, txbuffer) + + slot * NETC_BUFFER_SIZE); + desc->length = htole16(length); + desc->frame_length = htole16(length); + desc->flags = htole32(NETC_TX_BD_EXTENDED | NETC_TX_BD_INTERRUPT); + desc->extension = htole32(NETC_BD_FINAL); + up_clean_dcache((uintptr_t)desc, (uintptr_t)desc + sizeof(*desc)); + priv->txpacket[slot] = packet; + priv->txtime[slot] = clock_systime_ticks(); + priv->txprod = next; + UP_DSB(); + putreg32(next * 2, TXREG(priv, NETC_BDR_PIR)); + return OK; +} + +static netpkt_t *imxrt_netc_receive(struct netdev_lowerhalf_s *lower) +{ + struct imxrt_netc_s *priv = (struct imxrt_netc_s *)lower; + struct imxrt_netc_rxdesc_s *desc; + netpkt_t *packet; + unsigned int budget = NETC_RX_SLOTS; + unsigned int producer; + unsigned int slot; + unsigned int length; + uint32_t flags; + bool discard; + int ret; + + while (priv->state == NETC_UP && budget-- != 0) + { + producer = getreg32(RXREG(priv, NETC_BDR_PIR)) & NETC_BDR_INDEX_MASK; + if (producer >= NETC_RX_SLOTS || + (getreg32(RXREG(priv, NETC_BDR_SR)) & NETC_BDR_SBE) != 0) + { + nerr("NETC receive DMA error, producer %u\n", producer); + NETDEV_RXERRORS(&lower->netdev); + imxrt_netc_fault(priv); + return NULL; + } + + slot = priv->rxcons; + if (producer == slot) + { + return NULL; + } + + desc = &priv->dma_cpu->rx[slot]; + up_invalidate_dcache((uintptr_t)desc, (uintptr_t)desc + sizeof(*desc)); + UP_DMB(); + flags = le32toh(desc->flags); + length = le32toh(desc->length) & UINT16_MAX; + discard = priv->rxdiscard || (flags & NETC_BD_FINAL) == 0; + priv->rxdiscard = (flags & NETC_BD_FINAL) == 0; + packet = NULL; + if (discard || (flags & NETC_RX_BD_ERROR_MASK) != 0 || + length < ETH_HDRLEN || length > NETC_PACKET_SIZE) + { + nerr("NETC RX descriptor error: flags=%08lx length=%u " + "discard=%u\n", (unsigned long)flags, length, discard); + NETDEV_RXERRORS(&lower->netdev); + } + else + { + up_invalidate_dcache((uintptr_t)priv->dma_cpu->rxbuffer[slot], + (uintptr_t)priv->dma_cpu->rxbuffer[slot] + + NETC_BUFFER_SIZE); + packet = netpkt_alloc(lower, NETPKT_RX); + if (packet != NULL) + { + ret = netpkt_copyin(lower, packet, + priv->dma_cpu->rxbuffer[slot], length, 0); + if (ret != length) + { + nerr("NETC RX packet copy failed: %d\n", ret); + netpkt_free(lower, packet, NETPKT_RX); + packet = NULL; + } + } + } + + imxrt_netc_rxarm(priv, slot); + priv->rxcons = (slot + 1) % NETC_RX_SLOTS; + UP_DSB(); + putreg32(priv->rxcons, RXREG(priv, NETC_RX_CIR)); + if (packet != NULL) + { + return packet; + } + + NETDEV_RXDROPPED(&lower->netdev); + } + + return NULL; +} + +#ifdef CONFIG_NET_MCASTGROUP +static int imxrt_netc_multicast(struct netdev_lowerhalf_s *lower, + const uint8_t *mac) +{ + /* The bridge floods multicast to members and SI0 accepts all multicast, + * including mandatory IPv6 discovery without optional group callbacks. + */ + + return (mac[0] & 1) != 0 ? OK : -EINVAL; +} +#endif + +#ifdef CONFIG_NETDEV_IOCTL +static int imxrt_netc_ioctl(struct netdev_lowerhalf_s *lower, int cmd, + unsigned long arg) +{ + struct imxrt_netc_s *priv = (struct imxrt_netc_s *)lower; + struct mii_ioctl_data_s *req = (struct mii_ioctl_data_s *)arg; + + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + return imxrt_netc_switch_ioctl(cmd, arg); + } + + if (cmd != SIOCETHTOOL && cmd != SIOCGMIIPHY && + cmd != SIOCGMIIREG && cmd != SIOCSMIIREG) + { + return -ENOTTY; + } + + if (req == NULL) + { + return -EINVAL; + } + + if (priv->state == NETC_UNINITIALIZED || priv->link.phydev == NULL) + { + return -ENODEV; + } + + if (priv->state == NETC_FAILED) + { + return -EIO; + } + + if (priv->state == NETC_RECOVERING) + { + return -EBUSY; + } + + if (cmd == SIOCGMIIPHY) + { + req->phy_id = priv->link.phydev->mdio_addr; + return OK; + } + + if (cmd != SIOCETHTOOL && + (req->phy_id != priv->link.phydev->mdio_addr || req->reg_num > 31)) + { + return -EINVAL; + } + + /* netdev ioctl holds the network lock; the MDIO bus serializes access. + * Never access the physical MDIO window after stop disabled this PF. + */ + + if ((getreg16(priv->hw->function + NETC_PCI_CMD) & + NETC_PCI_CMD_MEM_ACCESS) == 0) + { + return -ENETDOWN; + } + + return phy_ioctl(priv->link.phydev, cmd, arg); +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int imxrt_netc_wait(uintptr_t reg, uint32_t mask, uint32_t value) +{ + unsigned int count; + + for (count = 0; count < 1000; count++) + { + if ((getreg32(reg) & mask) == value) + { + return OK; + } + + up_udelay(10); + } + + return -ETIMEDOUT; +} + +int imxrt_netc_reset(uintptr_t function, uintptr_t common) +{ + unsigned int count; + uint32_t status; + int ret; + + modifyreg16(function + NETC_PCI_DEVCTL, 0, NETC_PCI_FLR); + for (count = 0; count < 10000; count++) + { + if ((getreg16(function + NETC_PCI_DEVCTL) & NETC_PCI_FLR) == 0) + { + putreg16(NETC_PCI_CMD_ENABLE, function + NETC_PCI_CMD); + ret = imxrt_netc_wait(common + NETC_COMMON_OSR, + NETC_COMMON_STATE, 0); + if (ret == OK) + { + status = getreg32(common + NETC_COMMON_OSR); + if ((status & NETC_COMMON_ITM_STATE) != 0) + { + nerr("NETC reset at %08lx index table memory " + "overprovisioned: OSR=%08lx\n", + (unsigned long)common, (unsigned long)status); + ret = -EIO; + } + } + + return ret; + } + + up_udelay(10); + } + + nerr("NETC function reset timed out at %08lx\n", (unsigned long)function); + return -ETIMEDOUT; +} + +void *imxrt_netc_alloc(size_t size, uint32_t *dma_bus) +{ + void *memory; + + memory = kmm_memalign(NETC_DMA_ALIGN, size); + if (memory == NULL) + { + return NULL; + } + + /* Convert CPU pointer to DMA address space. + * Keep the CPU pointer for memory access and cache maintenance. + */ + + if (!imxrt_dma_address(memory, size, dma_bus)) + { + nerr("NETC allocation cannot be mapped for DMA: %p\n", memory); + kmm_free(memory); + return NULL; + } + + memset(memory, 0, size); + return memory; +} + +static int imxrt_netc_register(size_t index, + const struct imxrt_netc_enetc_s *config, + uint32_t clock_hz) +{ + struct imxrt_netc_s *priv; + uint32_t status; + uint16_t command; + int cleanup; + int ret; + + priv = &g_netc[index]; + priv->hw = &g_netc_hw[index]; + priv->config = *config; + priv->link.speed = 0; + priv->msgintr = g_msgintr_base[config->msgintr - 1]; + priv->irq = g_msgintr_irq[config->msgintr - 1]; + priv->lower.ops = &g_netc_ops; + priv->lower.priority = ETHWORK; + priv->lower.quota[NETPKT_TX] = NETC_TX_QUOTA; + priv->lower.quota[NETPKT_RX] = 1; + memcpy(priv->lower.netdev.d_mac.ether.ether_addr_octet, config->mac, 6); + priv->dma_cpu = imxrt_netc_alloc(sizeof(*priv->dma_cpu), &priv->dma_bus); + if (priv->dma_cpu == NULL) + { + ret = -ENOMEM; + goto failed; + } + + command = getreg16(priv->hw->function + NETC_PCI_CMD); + if ((command & NETC_PCI_CMD_ENABLE) != 0 || + (getreg16(priv->hw->function + NETC_PCI_SRIOVCTL) & + NETC_PCI_VF_ENABLE) != 0) + { + ret = -EBUSY; + goto free_dma; + } + + ret = irq_attach(priv->irq, imxrt_netc_interrupt, priv); + if (ret < 0) + { + goto free_dma; + } + + /* No switch or packet callbacks can overlap initialization. */ + + net_lock(); + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + ret = imxrt_netc_switch_initialize(priv->hw, &priv->config, clock_hz); + if (ret < 0) + { + goto release_hw; + } + } + else + { + putreg16(command | NETC_PCI_CMD_ENABLE, + priv->hw->function + NETC_PCI_CMD); + ret = imxrt_netc_wait(priv->hw->enetc + NETC_COMMON_OSR, + NETC_COMMON_STATE, 0); + if (ret < 0) + { + goto release_hw; + } + + status = getreg32(priv->hw->enetc + NETC_COMMON_OSR); + if ((status & NETC_COMMON_ITM_STATE) != 0) + { + nerr("NETC ENETC%u index table memory overprovisioned: " + "OSR=%08lx\n", + (unsigned int)index, (unsigned long)status); + ret = -EIO; + goto release_hw; + } + + putreg32(0, priv->hw->si + NETC_SI_SIMR); + putreg32(NETC_PORT_DISABLE, priv->hw->port + NETC_PORT_POR); + modifyreg32(priv->hw->mac + NETC_MAC_CMD, + NETC_MAC_RX | NETC_MAC_TX, 0); + + ret = imxrt_netc_port_prepare(&priv->hw->ports[0], + &priv->config.ports[0]); + if (ret < 0) + { + goto release_hw; + } + + ret = imxrt_netc_port_initialize(&priv->hw->ports[0], + &priv->config.ports[0], + &priv->link, clock_hz); + if (ret < 0) + { + goto release_hw; + } + } + + priv->state = NETC_DOWN; + ret = work_queue(ETHWORK, &priv->linkwork, imxrt_netc_linkwork, priv, + NETC_LINK_INTERVAL); + if (ret < 0) + { + goto release_hw; + } + + ret = netdev_lower_register(&priv->lower, NET_LL_ETHERNET); + if (ret < 0) + { + priv->state = NETC_UNINITIALIZED; + net_unlock(); + work_cancel_sync(ETHWORK, &priv->linkwork); + net_lock(); + goto release_hw; + } + + net_unlock(); + return OK; + +release_hw: + priv->state = NETC_UNINITIALIZED; + if (priv->hw->attachment == IMXRT_NETC_SWITCH) + { + cleanup = imxrt_netc_switch_uninitialize(); + if (cleanup < 0) + { + nerr("NETC switch cleanup failed: %d\n", cleanup); + priv->state = NETC_FAILED; + } + } + else + { + imxrt_netc_port_uninitialize(&priv->hw->ports[0], &priv->link); + putreg16(command, priv->hw->function + NETC_PCI_CMD); + if (getreg16(priv->hw->function + NETC_PCI_CMD) != command) + { + nerr("NETC ENETC%u function disable failed\n", + (unsigned int)index); + priv->state = NETC_FAILED; + } + } + + net_unlock(); + irq_detach(priv->irq); +free_dma: + /* No ENETC ring addresses are published before ifup. Retain the allocation + * if hardware cleanup failed, so the failed instance cannot be reused. + */ + + if (priv->state != NETC_FAILED) + { + kmm_free(priv->dma_cpu); + priv->dma_cpu = NULL; + } + +failed: + nerr("NETC ENETC%u initialization failed: %d\n", (unsigned int)index, ret); + return ret; +} + +int imxrt_netc_initialize(const struct imxrt_netc_config_s *config) +{ + uint32_t clock_hz = 0; + uint32_t status; + size_t index; + int ret; + + ret = nxmutex_lock(&g_initlock); + if (ret < 0) + { + return ret; + } + + status = getreg32(IMXRT_SRC_SLICE_FUNC_STAT(IMXRT_NETC_MIX_SLICE_BASE)); + if ((status & (SRC_SLICE_FUNC_STAT_PSW_STAT | + SRC_SLICE_FUNC_STAT_RST_STAT | + SRC_SLICE_FUNC_STAT_ISO_STAT)) != + SRC_SLICE_FUNC_STAT_RST_STAT) + { + nerr("NETCMIX must be powered, released and deisolated: %#lx\n", + (unsigned long)status); + ret = -EHOSTDOWN; + goto out; + } + + ret = imxrt_get_rootclock(CCM_CR_NETC, &clock_hz); + if (ret < 0) + { + nerr("NETC root clock query failed: %d\n", ret); + goto out; + } + + if (clock_hz < NETC_IERB_CLKCR_MIN_HZ || + clock_hz > NETC_IERB_CLKCR_MAX_HZ || + clock_hz % 1000000 != 0) + { + nerr("NETC unsupported root clock frequency: %lu Hz\n", + (unsigned long)clock_hz); + ret = -EINVAL; + goto out; + } + + ret = imxrt_netc_shared_initialize(config, clock_hz); + if (ret < 0) + { + nerr("NETC shared infrastructure initialization failed: %d\n", ret); + goto out; + } + + for (index = 0; index < nitems(config->enetc); index++) + { + if (!config->enetc[index].enabled) + { + continue; + } + + ret = imxrt_netc_register(index, &config->enetc[index], clock_hz); + if (ret < 0) + { + goto out; + } + } + +out: + nxmutex_unlock(&g_initlock); + return ret; +} diff --git a/arch/arm/src/imxrt/imxrt_netc.h b/arch/arm/src/imxrt/imxrt_netc.h new file mode 100644 index 00000000000..7e82f8e27a6 --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_netc.h @@ -0,0 +1,146 @@ +/**************************************************************************** + * arch/arm/src/imxrt/imxrt_netc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_IMXRT_IMXRT_NETC_H +#define __ARCH_ARM_SRC_IMXRT_IMXRT_NETC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/net/phy.h> +#include <stdbool.h> +#include <stdint.h> +#include <arch/imxrt/chip.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Total amount of external ports / PHY devices supported + * by NETC. This is also the port register index limit. + */ + +#define IMXRT_NETC_PHY_PORT_COUNT 5 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* MDIO access endpoints. + * PORT0..PORT3 access the PHYs on switch physical links 0..3. + * ENETC0 accesses the PHY on physical link 4. + * Per-port endpoints require a fixed PHY address matching the + * corresponding IERB LINKn_BCR PHY address field. If IERB is already + * locked, that field must already match the configured address. + * EMDIO accesses the external MDIO bus. + */ + +enum imxrt_netc_mdio_e +{ + IMXRT_NETC_EMDIO = 0, + IMXRT_NETC_PORT0_MDIO, + IMXRT_NETC_PORT1_MDIO, + IMXRT_NETC_PORT2_MDIO, + IMXRT_NETC_PORT3_MDIO, + IMXRT_NETC_ENETC0_MDIO, + IMXRT_NETC_MDIO_COUNT +}; + +struct imxrt_netc_port_s +{ + bool enabled; + uint8_t phyaddr; + enum phy_interface_e interface; +}; + +struct imxrt_netc_enetc_s +{ + bool enabled; + uint8_t msgintr; /* Exclusively reserved MSGINTR channel 1..6 */ + uint8_t mac[6]; + struct imxrt_netc_port_s ports[IMXRT_NETC_SW0_NPORTS]; +}; + +struct imxrt_netc_config_s +{ + struct imxrt_netc_enetc_s enetc[IMXRT_NETC_NENETC]; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Function: board_netc_phyinit + * + * Description: + * Perform board-specific PHY initialization, such as configuring GPIOs + * and resetting the PHY. When CONFIG_IMXRT_NETC_PHYINIT is enabled, + * board logic must provide this function. The NETC driver calls it + * during port preparation, after interface mode and clock-direction + * setup and before MDIO probing. + * + * Input Parameters: + * if_idx - Physical NETC link index (0..4), identifying the PHY to + * initialize. + * config - Configuration for the port, including its PHY settings. + * + * Returned Value: + * OK on success; negated errno on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_IMXRT_NETC_PHYINIT +int board_netc_phyinit(unsigned int if_idx, + const struct imxrt_netc_port_s *config); +#endif + +/**************************************************************************** + * Function: imxrt_netc_initialize + * + * Description: + * Initialize shared NETC resources and register the enabled ENETC + * network interfaces. + * + * Input Parameters: + * config - Board-specific configuration selecting enabled ENETC + * instances and ports, MAC addresses, interrupt channels, + * and per-port PHY settings. + * + * Returned Value: + * OK on success; negated errno on failure. + * + ****************************************************************************/ + +int imxrt_netc_initialize(const struct imxrt_netc_config_s *config); + +#ifdef __cplusplus +} +#endif +#endif /* __ARCH_ARM_SRC_IMXRT_IMXRT_NETC_H */ diff --git a/arch/arm/src/imxrt/imxrt_netc_port.c b/arch/arm/src/imxrt/imxrt_netc_port.c new file mode 100644 index 00000000000..6855d10fc16 --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_netc_port.c @@ -0,0 +1,535 @@ +/**************************************************************************** + * arch/arm/src/imxrt/imxrt_netc_port.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <debug.h> +#include <sys/param.h> +#include <nuttx/mutex.h> +#include <nuttx/nuttx.h> +#include <nuttx/net/gmii.h> +#include <nuttx/net/mdio.h> +#include <nuttx/ethtool.h> + +#include "imxrt_netc_priv.h" +#include "hardware/imxrt_netc.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct imxrt_netc_mdio_s +{ + struct mdio_lowerhalf_s lower; + struct mdio_bus_s *bus; + enum imxrt_netc_mdio_e id; + uint32_t config; + unsigned int users; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int imxrt_netc_mdio_read(struct mdio_lowerhalf_s *lower, + uint8_t phyaddr, uint8_t reg, + uint16_t *value); +static int imxrt_netc_mdio_write(struct mdio_lowerhalf_s *lower, + uint8_t phyaddr, uint8_t reg, + uint16_t value); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Lock order: network/device lock, PHY lock, MDIO lock, hardware lock. + * Endpoint registration and reference counts use the network lock. + * PHYLIB serializes paged accesses with the MDIO bus lock. + */ + +static mutex_t g_mdiolock = NXMUTEX_INITIALIZER; +static struct imxrt_netc_mdio_s g_mdio[IMXRT_NETC_MDIO_COUNT]; + +static const struct mdio_ops_s g_mdio_ops = +{ + .read = imxrt_netc_mdio_read, + .write = imxrt_netc_mdio_write, + .reset = NULL +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static uintptr_t imxrt_netc_mdiobase(enum imxrt_netc_mdio_e bus) +{ + switch (bus) + { + case IMXRT_NETC_EMDIO: + return IMXRT_NETC_EMDIO_REGS; + + case IMXRT_NETC_PORT0_MDIO: + return IMXRT_NETC_PORT_MDIO(0); + + case IMXRT_NETC_PORT1_MDIO: + return IMXRT_NETC_PORT_MDIO(1); + + case IMXRT_NETC_PORT2_MDIO: + return IMXRT_NETC_PORT_MDIO(2); + + case IMXRT_NETC_PORT3_MDIO: + return IMXRT_NETC_PORT_MDIO(3); + + case IMXRT_NETC_ENETC0_MDIO: + return IMXRT_NETC_ENETC0_MDIO_REGS; + + default: + return 0; + } +} + +static int imxrt_netc_mdio(struct imxrt_netc_mdio_s *priv, + uint8_t phyaddr, uint8_t reg, uint16_t *value, + bool read) +{ + uintptr_t base = imxrt_netc_mdiobase(priv->id); + uint32_t status; + int ret; + + DEBUGASSERT(phyaddr <= 31 && reg <= 31 && value != NULL); + + ret = nxmutex_lock(&g_mdiolock); + if (ret < 0) + { + return ret; + } + + ret = imxrt_netc_wait(base + NETC_MDIO_CFG, NETC_MDIO_BUSY, 0); + if (ret == OK) + { + /* Restore timing on every transaction, including after a PF reset. */ + + putreg32(priv->config | NETC_MDIO_ADDR_ERROR | NETC_MDIO_COMPLETE, + base + NETC_MDIO_CFG); + putreg32(NETC_MDIO_PHY(phyaddr) | reg | + (read ? NETC_MDIO_READ : 0), base + NETC_MDIO_CTL); + if (!read) + { + putreg32(*value, base + NETC_MDIO_DATA); + } + + ret = imxrt_netc_wait(base + NETC_MDIO_CFG, NETC_MDIO_BUSY, 0); + if (ret == OK) + { + status = getreg32(base + NETC_MDIO_CFG); + if ((status & NETC_MDIO_ADDR_ERROR) != 0) + { + ret = -EACCES; + } + else if (read && (status & NETC_MDIO_READ_ERROR) != 0) + { + ret = -ENODEV; + } + else if (read) + { + *value = getreg32(base + NETC_MDIO_DATA); + } + } + } + + nxmutex_unlock(&g_mdiolock); + if (ret < 0 && ret != -ENODEV) + { + nerr("MDIO bus %u PHY %u register %u: %d\n", + priv->id, phyaddr, reg, ret); + } + + return ret; +} + +static int imxrt_netc_mdio_read(struct mdio_lowerhalf_s *lower, + uint8_t phyaddr, uint8_t reg, + uint16_t *value) +{ + struct imxrt_netc_mdio_s *priv = + container_of(lower, struct imxrt_netc_mdio_s, lower); + + return imxrt_netc_mdio(priv, phyaddr, reg, value, true); +} + +static int imxrt_netc_mdio_write(struct mdio_lowerhalf_s *lower, + uint8_t phyaddr, uint8_t reg, + uint16_t value) +{ + struct imxrt_netc_mdio_s *priv = + container_of(lower, struct imxrt_netc_mdio_s, lower); + + return imxrt_netc_mdio(priv, phyaddr, reg, &value, false); +} + +static int imxrt_netc_mdio_initialize(enum imxrt_netc_mdio_e id, + uint32_t clock_hz, + struct mdio_bus_s **bus) +{ + struct imxrt_netc_mdio_s *priv; + uint32_t divisor; + + DEBUGASSERT((unsigned int)id < nitems(g_mdio)); + priv = &g_mdio[id]; + if (priv->users == 0) + { + /* MDC <= 2.5 MHz: NETC clock / (2 * divisor + 1). */ + + divisor = (clock_hz + 4999999) / 5000000; + if (divisor < 5) + { + divisor = 5; + } + + DEBUGASSERT(divisor <= 511); + priv->lower.ops = &g_mdio_ops; + priv->id = id; + priv->config = NETC_MDIO_NEG | NETC_MDIO_DIV(divisor) | + NETC_MDIO_HOLD(2); + priv->bus = mdio_register(&priv->lower); + if (priv->bus == NULL) + { + return -ENOMEM; + } + } + + priv->users++; + *bus = priv->bus; + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void imxrt_netc_port_uninitialize(const struct imxrt_netc_ports_hw_s *hw, + struct imxrt_netc_link_s *link) +{ + struct imxrt_netc_mdio_s *priv; + + if (link->bus == NULL) + { + return; + } + + priv = &g_mdio[hw->mdio]; + DEBUGASSERT(priv->users > 0 && priv->bus == link->bus); + + if (link->phydev != NULL) + { + phy_disconnect(link->phydev); + link->phydev = NULL; + } + + link->bus = NULL; + link->speed = 0; + if (--priv->users == 0) + { + mdio_unregister(priv->bus); + priv->bus = NULL; + } +} + +int imxrt_netc_port_prepare(const struct imxrt_netc_ports_hw_s *hw, + const struct imxrt_netc_port_s *config) +{ + uint32_t mask = NETC_LINK_CFG_MII_PROT_MASK; + uint32_t mode; + uint32_t clock = NETC_PORT_MISC_RMII_CLK_OUT(hw->reg_idx); + + switch (config->interface) + { + case PHY_INTERFACE_MII: + mode = NETC_LINK_CFG_MII; + break; + + case PHY_INTERFACE_RMII: + case PHY_INTERFACE_RMII_PHY_CLK_OUT: + mode = NETC_LINK_CFG_RMII; + break; + + case PHY_INTERFACE_RGMII: + case PHY_INTERFACE_RGMII_ID: + case PHY_INTERFACE_RGMII_TXID: + case PHY_INTERFACE_RGMII_RXID: + mode = NETC_LINK_CFG_RGMII; + break; + + default: + nerr("NETC port %u unsupported interface mode: %u\n", + hw->reg_idx, config->interface); + return -EOPNOTSUPP; + } + + /* REVMII is reserved on ports 0 and 1. */ + + if (hw->reg_idx >= 2) + { + mask |= NETC_LINK_CFG_REVMII; + } + + modifyreg32(IMXRT_NETC_PORT_MISC_CFG, clock, + config->interface == PHY_INTERFACE_RMII_PHY_CLK_OUT ? + 0 : clock); + modifyreg32(IMXRT_NETC_LINK_CFG(hw->reg_idx), mask, mode); + +#ifdef CONFIG_IMXRT_NETC_PHYINIT + return board_netc_phyinit(hw->reg_idx, config); +#else + return OK; +#endif +} + +int imxrt_netc_port_initialize(const struct imxrt_netc_ports_hw_s *hw, + const struct imxrt_netc_port_s *config, + struct imxrt_netc_link_s *link, + uint32_t clock_hz) +{ + struct phy_device_s *phydev; + uint32_t supported; + int ret; + + if (config->phyaddr > 31) + { + nerr("NETC port %u invalid PHY address: %u\n", + hw->reg_idx, config->phyaddr); + return -EINVAL; + } + + DEBUGASSERT(link->bus == NULL); + + ret = imxrt_netc_mdio_initialize(hw->mdio, clock_hz, &link->bus); + if (ret < 0) + { + return ret; + } + + phydev = phy_device_create(link->bus, config->phyaddr); + if (phydev == NULL) + { + nerr("NETC port %u PHY creation failed on bus %u address %u\n", + hw->reg_idx, hw->mdio, config->phyaddr); + ret = -ENODEV; + goto failed; + } + + phydev->interface = config->interface; + ret = phy_connect(phydev, NULL); + if (ret < 0) + { + phy_device_free(phydev); + goto failed; + } + + link->phydev = phydev; + + /* NETC supports full duplex only. Limit both capabilities and advertising + * so ethtool cannot request a mode that the MAC cannot implement. + */ + + supported = SUPPORTED_10BASET_FULL | SUPPORTED_100BASET_FULL | + SUPPORTED_AUTONEG | SUPPORTED_TP | SUPPORTED_MII; + if (config->interface == PHY_INTERFACE_RGMII || + config->interface == PHY_INTERFACE_RGMII_ID || + config->interface == PHY_INTERFACE_RGMII_TXID || + config->interface == PHY_INTERFACE_RGMII_RXID) + { + supported |= SUPPORTED_1000BASET_FULL; + } + + phydev->supported &= supported; + phydev->advertising = phydev->supported; + link->speed = 0; + + if (hw->reg_idx < IMXRT_NETC_SW0_NPORTS) + { + ret = phy_start(phydev); + if (ret < 0) + { + goto failed; + } + } + + return OK; + +failed: + nerr("NETC port %u PHY initialization failed: %d\n", hw->reg_idx, ret); + imxrt_netc_port_uninitialize(hw, link); + return ret; +} + +int imxrt_netc_port_link(struct imxrt_netc_link_s *link, uint16_t *speed) +{ + struct phy_device_s *phydev = link->phydev; + int ret; + + *speed = 0; + if (phydev == NULL) + { + return -ENODEV; + } + + ret = nxmutex_lock(&phydev->lock); + if (ret < 0) + { + return ret; + } + + if (phydev->state == PHY_ERROR) + { + ret = -EIO; + } + else if (phydev->state == PHY_RUNNING && phydev->link) + { + if (phydev->duplex != DUPLEX_FULL || + (phydev->speed != 10 && phydev->speed != 100 && + phydev->speed != 1000)) + { + ret = -EPROTO; + } + else + { + *speed = phydev->speed; + } + } + + nxmutex_unlock(&phydev->lock); + + if (ret == -EIO) + { + /* PHYLIB halts polling after an MDIO error. Retry on the next NETC + * update rather than leaving a transient bus failure unrecoverable. + */ + + nerr("NETC PHY %u restarting after an MDIO error\n", + phydev->mdio_addr); + phy_stop(phydev); + ret = phy_start(phydev); + return ret < 0 ? ret : -EIO; + } + + return ret; +} + +int imxrt_netc_port_mode(uintptr_t port, uintptr_t mac, + enum phy_interface_e mode, uint16_t speed) +{ + uint32_t interface; + int ret; + + switch (mode) + { + case PHY_INTERFACE_MII: + interface = NETC_MAC_IF_MII; + break; + case PHY_INTERFACE_RMII: + case PHY_INTERFACE_RMII_PHY_CLK_OUT: + interface = NETC_MAC_IF_RMII; + break; + case PHY_INTERFACE_RGMII: + case PHY_INTERFACE_RGMII_ID: + case PHY_INTERFACE_RGMII_TXID: + case PHY_INTERFACE_RGMII_RXID: + interface = NETC_MAC_IF_RGMII; + break; + default: + return -EINVAL; + } + + modifyreg32(mac + NETC_MAC_CMD, NETC_MAC_RX, 0); + ret = imxrt_netc_wait(mac + NETC_MAC_IEVENT, NETC_MAC_RXEMPTY, + NETC_MAC_RXEMPTY); + if (ret == OK) + { + ret = imxrt_netc_wait(port + NETC_PORT_PSR, NETC_PORT_RXBUSY, 0); + } + + if (ret == OK) + { + modifyreg32(port + NETC_PORT_POR, 0, NETC_PORT_DISABLE); + ret = imxrt_netc_wait(mac + NETC_MAC_IEVENT, NETC_MAC_TXEMPTY, + NETC_MAC_TXEMPTY); + } + + if (ret < 0) + { + modifyreg32(port + NETC_PORT_POR, 0, NETC_PORT_DISABLE); + return ret; + } + + /* Allow the final minimum frame to leave the MAC before disabling TX. */ + + up_udelay(52); + modifyreg32(mac + NETC_MAC_CMD, NETC_MAC_TX, 0); + if (speed == 0) + { + return OK; + } + + if (speed == 10) + { + interface |= NETC_MAC_IF_10M | NETC_MAC_IF_SSP(1); + } + else if (speed == 1000) + { + interface |= NETC_MAC_IF_SSP(2); + } + + modifyreg32(mac + NETC_MAC_IFMODE, NETC_MAC_IF_MASK, interface); + modifyreg32(port + NETC_PORT_PCR, NETC_PORT_PSPEED_MASK, + NETC_PORT_PSPEED(speed - 1)); + putreg32(NETC_PACKET_SIZE + 4, mac + NETC_MAC_MAXFRM); + putreg32(64, mac + NETC_MAC_MINFRM); + modifyreg32(mac + NETC_MAC_CMD, 0, + NETC_MAC_TXPAD | NETC_MAC_TX | NETC_MAC_RX); + modifyreg32(port + NETC_PORT_POR, NETC_PORT_DISABLE, 0); + return OK; +} + +void imxrt_netc_port_sdu(uintptr_t port) +{ + unsigned int tc; + + putreg32(20, port + NETC_PORT_PRXSDUOR); + putreg32(20, port + NETC_PORT_PTXSDUOR); + for (tc = 0; tc < 8; tc++) + { + putreg32(NETC_PORT_MAXSDU_DISABLE, port + NETC_PORT_PTCTMSDUR(tc)); + } +} + +void imxrt_netc_pseudo(uintptr_t port) +{ + modifyreg32(port + NETC_PORT_PCR, NETC_PORT_PSPEED_MASK, + NETC_PORT_PSPEED(999)); + imxrt_netc_port_sdu(port); +} diff --git a/arch/arm/src/imxrt/imxrt_netc_priv.h b/arch/arm/src/imxrt/imxrt_netc_priv.h new file mode 100644 index 00000000000..7b95ee45290 --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_netc_priv.h @@ -0,0 +1,146 @@ +/**************************************************************************** + * arch/arm/src/imxrt/imxrt_netc_priv.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_IMXRT_IMXRT_NETC_PRIV_H +#define __ARCH_ARM_SRC_IMXRT_IMXRT_NETC_PRIV_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/cache.h> +#include <nuttx/compiler.h> +#include <nuttx/net/mdio.h> +#include <assert.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#include <arch/barriers.h> + +#include "arm_internal.h" +#include "chip.h" +#include "imxrt_netc.h" +#include "hardware/imxrt_netc.h" +#include "hardware/imxrt_netc_switch.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NETC_DMA_ALIGN 128 +#define NETC_PACKET_SIZE CONFIG_NET_ETH_PKTSIZE +#define NETC_BUFFER_SIZE ((NETC_PACKET_SIZE + NETC_DMA_ALIGN - 1) & \ + ~(NETC_DMA_ALIGN - 1)) +#define NETC_TX_SLOTS (CONFIG_IMXRT_NETC_NTXDESC / 2) +#define NETC_RX_SLOTS CONFIG_IMXRT_NETC_NRXDESC +#define NETC_TX_QUOTA (NETC_TX_SLOTS - 1) + +#ifdef CONFIG_ARCH_CORTEXM33 +# define NETC_DCACHE_LINESIZE ARMV8M_DCACHE_LINESIZE +#else +# define NETC_DCACHE_LINESIZE ARMV7M_DCACHE_LINESIZE +#endif + +#define IMXRT_NETC_EMDIO_PF IMXRT_NETC_PF(1) +#define IMXRT_NETC_SW0_PF IMXRT_NETC_PF(2) +#define IMXRT_NETC_ENETC0_PF IMXRT_NETC_PF(3) +#define IMXRT_NETC_ENETC1_PF IMXRT_NETC_PF(4) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +enum imxrt_netc_attachment_e +{ + IMXRT_NETC_DIRECT = 0, + IMXRT_NETC_SWITCH +}; + +struct imxrt_netc_ports_hw_s +{ + uint8_t reg_idx; + enum imxrt_netc_mdio_e mdio; +}; + +struct imxrt_netc_hw_s +{ + uintptr_t si; + uintptr_t enetc; + uintptr_t port; + uintptr_t mac; + uintptr_t function; + uintptr_t msix; + enum imxrt_netc_attachment_e attachment; + uint8_t portcount; + uint8_t sw_cpu_port_reg_idx; + struct imxrt_netc_ports_hw_s ports[IMXRT_NETC_SW0_NPORTS]; +}; + +struct imxrt_netc_link_s +{ + struct mdio_bus_s *bus; /* Registered endpoint, shared by PHYs */ + struct phy_device_s *phydev; + uint16_t speed; /* Zero means link down */ +}; + +static_assert( + sizeof(struct imxrt_netc_txdesc_s) % NETC_DCACHE_LINESIZE == 0 && + sizeof(struct imxrt_netc_rxdesc_s) % NETC_DCACHE_LINESIZE == 0 && + sizeof(struct imxrt_netc_cmd_s) % NETC_DCACHE_LINESIZE == 0 && + NETC_DMA_ALIGN % NETC_DCACHE_LINESIZE == 0, + "NETC DMA entries must occupy whole cache lines"); + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int imxrt_netc_wait(uintptr_t reg, uint32_t mask, uint32_t value); +int imxrt_netc_reset(uintptr_t function, uintptr_t common); +void *imxrt_netc_alloc(size_t size, uint32_t *dma_bus); + +int imxrt_netc_port_prepare(const struct imxrt_netc_ports_hw_s *hw, + const struct imxrt_netc_port_s *config); +int imxrt_netc_port_initialize(const struct imxrt_netc_ports_hw_s *hw, + const struct imxrt_netc_port_s *config, + struct imxrt_netc_link_s *link, + uint32_t clock_hz); +void imxrt_netc_port_uninitialize(const struct imxrt_netc_ports_hw_s *hw, + struct imxrt_netc_link_s *link); +int imxrt_netc_port_link(struct imxrt_netc_link_s *link, uint16_t *speed); +int imxrt_netc_port_mode(uintptr_t port, uintptr_t mac, + enum phy_interface_e mode, uint16_t speed); +void imxrt_netc_port_sdu(uintptr_t port); +void imxrt_netc_pseudo(uintptr_t port); + +int imxrt_netc_switch_initialize(const struct imxrt_netc_hw_s *hw, + const struct imxrt_netc_enetc_s *config, + uint32_t clock_hz); +int imxrt_netc_switch_uninitialize(void); +int imxrt_netc_switch_cpu(bool enable, const uint8_t *mac); +int imxrt_netc_switch_link(uint8_t *linkmask); + +#ifdef CONFIG_NETDEV_IOCTL +int imxrt_netc_switch_ioctl(int cmd, unsigned long arg); +#endif + +#endif /* __ARCH_ARM_SRC_IMXRT_IMXRT_NETC_PRIV_H */ diff --git a/arch/arm/src/imxrt/imxrt_netc_switch.c b/arch/arm/src/imxrt/imxrt_netc_switch.c new file mode 100644 index 00000000000..aa4249a530e --- /dev/null +++ b/arch/arm/src/imxrt/imxrt_netc_switch.c @@ -0,0 +1,528 @@ +/**************************************************************************** + * arch/arm/src/imxrt/imxrt_netc_switch.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <string.h> +#include <debug.h> +#include <endian.h> +#include <nuttx/clock.h> +#include <nuttx/kmalloc.h> +#include <nuttx/net/gmii.h> +#include <nuttx/net/ioctl.h> +#include <nuttx/net/netdev.h> + +#include "imxrt_netc_priv.h" +#include "hardware/imxrt_netc.h" +#include "hardware/imxrt_netc_switch.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define SWREG(r) (IMXRT_SW0_BASE + (r)) +#define PORTREG(p, r) (IMXRT_NETC_SW_PORT(p) + (r)) +#define SWITCH_NCMD 8 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +enum imxrt_netc_switch_state_e +{ + SWITCH_UNINITIALIZED = 0, + SWITCH_READY, + SWITCH_FAILED +}; + +struct imxrt_netc_switch_dma_s +{ + struct imxrt_netc_cmd_s command[SWITCH_NCMD]; + uint32_t data[NETC_DMA_ALIGN / sizeof(uint32_t)] + aligned_data(NETC_DMA_ALIGN); +}; + +struct imxrt_netc_switch_s +{ + const struct imxrt_netc_hw_s *hw; + const struct imxrt_netc_enetc_s *config; + struct imxrt_netc_switch_dma_s *dma_cpu; + uint32_t dma_bus; + struct imxrt_netc_link_s link[4]; + enum imxrt_netc_switch_state_e state; + unsigned int producer; + uint8_t fdbmac[6]; /* Installed address may differ from config */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Switch operations and initialization use the network lock. PHYLIB polls + * independently and serializes PHY register access with the MDIO bus lock. + */ + +static struct imxrt_netc_switch_s g_switch; + +static_assert(sizeof(struct imxrt_netc_cmd_s) == 32, + "NETC NTMP descriptor size"); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int imxrt_netc_fdb(const uint8_t *mac, bool add) +{ + struct imxrt_netc_cmd_s *cmd; + uint32_t *data = g_switch.dma_cpu->data; + unsigned int next = (g_switch.producer + 1) % SWITCH_NCMD; + uint32_t status; + int ret; + + memset(data, 0, NETC_DMA_ALIGN); + data[0] = htole32(add ? NETC_NTMP_FDB_CFGEU : 0); + memcpy(&data[1], mac, 6); + data[9] = htole32(UINT32_C(1) << g_switch.hw->sw_cpu_port_reg_idx); + data[11] = htole32(UINT32_MAX); + up_clean_dcache((uintptr_t)data, (uintptr_t)data + NETC_DMA_ALIGN); + + cmd = &g_switch.dma_cpu->command[g_switch.producer]; + memset(cmd, 0, sizeof(*cmd)); + cmd->address = htole32(g_switch.dma_bus + + offsetof(struct imxrt_netc_switch_dma_s, data)); + cmd->lengths = htole32(((add ? NETC_NTMP_FDB_REQUEST : + NETC_NTMP_FDB_DELETE) << 20) | + NETC_NTMP_FDB_RESPONSE); + cmd->command = htole32(NETC_NTMP_VERSION2 | (NETC_NTMP_FDB << 16) | + NETC_NTMP_EXACT | (add ? + NETC_NTMP_ADDQUERYUPDATE : NETC_NTMP_DELETE)); + cmd->format = htole32(NETC_NTMP_NPF); + up_clean_dcache((uintptr_t)cmd, (uintptr_t)cmd + sizeof(*cmd)); + UP_DSB(); + putreg32(next, SWREG(NETC_SW_CBDRPIR)); + + ret = imxrt_netc_wait(SWREG(NETC_SW_CBDRCIR), NETC_BDR_INDEX_MASK, next); + if (ret < 0) + { + /* The command and response storage remain owned by DMA until FLR. */ + + g_switch.state = SWITCH_FAILED; + nerr("NETC switch command timed out\n"); + return ret; + } + + UP_DMB(); + up_invalidate_dcache((uintptr_t)cmd, (uintptr_t)cmd + sizeof(*cmd)); + up_invalidate_dcache((uintptr_t)data, (uintptr_t)data + NETC_DMA_ALIGN); + g_switch.producer = next; + status = le32toh(cmd->command); + if ((status & NETC_NTMP_READY) == 0 || + (status & NETC_NTMP_ERROR_MASK) != 0) + { + nerr("NETC FDB command response: %08lx\n", (unsigned long)status); + return -EIO; + } + + /* Add/query/update reports 0 for a new entry and 1 for + * an existing entry. + */ + + if (add && (status & UINT16_MAX) > 1) + { + nerr("NETC FDB update returned invalid match count %lu\n", + (unsigned long)(status & UINT16_MAX)); + return -EIO; + } + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int imxrt_netc_switch_uninitialize(void) +{ + unsigned int port; + int ret; + + if (g_switch.state == SWITCH_UNINITIALIZED) + { + return OK; + } + + for (port = 0; port < g_switch.hw->portcount; port++) + { + imxrt_netc_port_uninitialize(&g_switch.hw->ports[port], + &g_switch.link[port]); + modifyreg32(PORTREG(g_switch.hw->ports[port].reg_idx, NETC_PORT_POR), + 0, NETC_PORT_DISABLE); + } + + modifyreg32(PORTREG(g_switch.hw->sw_cpu_port_reg_idx, NETC_PORT_POR), + 0, NETC_PORT_DISABLE); + + ret = imxrt_netc_reset(IMXRT_NETC_SW0_PF, IMXRT_SW0_BASE); + if (ret < 0) + { + g_switch.state = SWITCH_FAILED; + return ret; + } + + putreg16(0, IMXRT_NETC_SW0_PF + NETC_PCI_CMD); + kmm_free(g_switch.dma_cpu); + memset(&g_switch, 0, sizeof(g_switch)); + return OK; +} + +int imxrt_netc_switch_initialize(const struct imxrt_netc_hw_s *hw, + const struct imxrt_netc_enetc_s *config, + uint32_t clock_hz) +{ + unsigned int port; + uint32_t portmask = 0; + uint32_t status; + unsigned int tc; + int ret; + + if (g_switch.state != SWITCH_UNINITIALIZED || + (getreg16(IMXRT_NETC_SW0_PF + NETC_PCI_CMD) & + NETC_PCI_CMD_ENABLE) != 0) + { + return -EBUSY; + } + + for (port = 0; port < hw->portcount; port++) + { + if (!config->ports[port].enabled) + { + continue; + } + + ret = imxrt_netc_port_prepare(&hw->ports[port], &config->ports[port]); + if (ret < 0) + { + nerr("NETC switch port %u preparation failed: %d\n", port, ret); + return ret; + } + } + + g_switch.dma_cpu = imxrt_netc_alloc(sizeof(*g_switch.dma_cpu), + &g_switch.dma_bus); + if (g_switch.dma_cpu == NULL) + { + return -ENOMEM; + } + + g_switch.hw = hw; + g_switch.config = config; + for (port = 0; port < hw->portcount; port++) + { + if (config->ports[port].enabled) + { + portmask |= UINT32_C(1) << hw->ports[port].reg_idx; + } + } + + g_switch.state = SWITCH_FAILED; + putreg16(NETC_PCI_CMD_ENABLE, IMXRT_NETC_SW0_PF + NETC_PCI_CMD); + ret = imxrt_netc_wait(SWREG(NETC_COMMON_OSR), + NETC_COMMON_STATE, 0); + if (ret < 0) + { + return ret; + } + + status = getreg32(SWREG(NETC_COMMON_OSR)); + if ((status & NETC_COMMON_ITM_STATE) != 0) + { + nerr("NETC switch index table memory overprovisioned: OSR=%08lx\n", + (unsigned long)status); + return -EIO; + } + + for (port = 0; port < hw->portcount; port++) + { + modifyreg32(PORTREG(hw->ports[port].reg_idx, NETC_PORT_POR), + 0, NETC_PORT_DISABLE); + } + + modifyreg32(PORTREG(hw->sw_cpu_port_reg_idx, NETC_PORT_POR), + 0, NETC_PORT_DISABLE); + + putreg32(portmask, SWREG(NETC_SW_VFHTDECR0)); + putreg32(NETC_SW_SHARED_FID, SWREG(NETC_SW_VFHTDECR1)); + putreg32(NETC_SW_LEARN_FLOOD, SWREG(NETC_SW_VFHTDECR2)); + putreg32(UINT16_MAX, SWREG(NETC_SW_FDBHTMCR)); + + int reg_idx = 0; + + for (port = 0; port < hw->portcount + 1; port++) + { + if (port < hw->portcount) + { + reg_idx = hw->ports[port].reg_idx; + + if (!config->ports[port].enabled) + { + putreg32(0, PORTREG(reg_idx, NETC_SW_BPSTGSR)); + continue; + } + } + else + { + reg_idx = hw->sw_cpu_port_reg_idx; + } + + putreg32(NETC_SW_STG_FORWARD, PORTREG(reg_idx, NETC_SW_BPSTGSR)); + putreg32(NETC_SW_DEFAULT_VLAN, PORTREG(reg_idx, NETC_SW_BPDVR)); + putreg32(UINT16_MAX, PORTREG(reg_idx, NETC_SW_BPCR)); + putreg32(NETC_SW_STANDARD_TPIDS, PORTREG(reg_idx, NETC_SW_PTAR)); + putreg32(NETC_SW_NULL_STREAM, PORTREG(reg_idx, NETC_SW_PISIDCR)); + putreg32(0, PORTREG(reg_idx, NETC_SW_PIPFCR)); + + /* ERR051649: only VLAN-to-QoS profile 0 is supported. Keep + * IPV2ICMPMR0 at reset (single ICM priority), per ERR051444. + */ + + putreg32(0, PORTREG(reg_idx, NETC_SW_PQOSMR)); + putreg32(0, PORTREG(reg_idx, NETC_SW_PIPV2QMR0)); + putreg32(0xff, PORTREG(reg_idx, NETC_SW_PDGSR)); + for (tc = 0; tc < 8; tc++) + { + putreg32(NETC_PORT_MAXSDU_DISABLE, + PORTREG(reg_idx, NETC_PORT_PTCTMSDUR(tc))); + } + + if (port < hw->portcount) + { + putreg32(NETC_SW_STORE_FORWARD, PORTREG(reg_idx, NETC_SW_PCTFCR)); + ret = imxrt_netc_port_initialize(&hw->ports[port], + &config->ports[port], + &g_switch.link[port], + clock_hz); + if (ret < 0) + { + nerr("NETC switch port %u initialization: %d\n", port, ret); + return ret; + } + } + else + { + imxrt_netc_pseudo(IMXRT_NETC_SW_PORT(reg_idx)); + } + } + + putreg32(NETC_DMA_NONCOHERENT, SWREG(NETC_SW_CCAR)); + up_clean_dcache((uintptr_t)g_switch.dma_cpu, + (uintptr_t)g_switch.dma_cpu + sizeof(*g_switch.dma_cpu)); + putreg32(g_switch.dma_bus + + offsetof(struct imxrt_netc_switch_dma_s, command), + SWREG(NETC_SW_CBDRBAR0)); + putreg32(0, SWREG(NETC_SW_CBDRBAR1)); + putreg32(0, SWREG(NETC_SW_CBDRPIR)); + putreg32(0, SWREG(NETC_SW_CBDRCIR)); + putreg32(SWITCH_NCMD, SWREG(NETC_SW_CBDRLENR)); + UP_DSB(); + putreg32(NETC_BDR_EN, SWREG(NETC_SW_CBDRMR)); + ret = imxrt_netc_fdb(config->mac, true); + if (ret == OK) + { + memcpy(g_switch.fdbmac, config->mac, sizeof(g_switch.fdbmac)); + g_switch.state = SWITCH_READY; + } + + return ret; +} + +int imxrt_netc_switch_cpu(bool enable, const uint8_t *mac) +{ + int ret; + + if (g_switch.state == SWITCH_UNINITIALIZED) + { + return -ENODEV; + } + + /* Gate both static-FDB delivery and flooding before changing the CPU + * function. External membership and learned entries remain untouched. + */ + + modifyreg32(PORTREG(g_switch.hw->sw_cpu_port_reg_idx, NETC_PORT_POR), + 0, NETC_PORT_DISABLE); + modifyreg32(SWREG(NETC_SW_VFHTDECR0), + UINT32_C(1) << g_switch.hw->sw_cpu_port_reg_idx, 0); + UP_DSB(); + ret = imxrt_netc_wait(PORTREG(g_switch.hw->sw_cpu_port_reg_idx, + NETC_PORT_PSR), NETC_PORT_BUSY, 0); + if (ret < 0 || !enable) + { + return ret; + } + + if (g_switch.state != SWITCH_READY) + { + return -EIO; + } + + if (memcmp(g_switch.fdbmac, mac, sizeof(g_switch.fdbmac)) != 0) + { + ret = imxrt_netc_fdb(mac, true); + if (ret == OK) + { + ret = imxrt_netc_fdb(g_switch.fdbmac, false); + } + + if (ret < 0) + { + return ret; + } + + memcpy(g_switch.fdbmac, mac, sizeof(g_switch.fdbmac)); + } + + modifyreg32(SWREG(NETC_SW_VFHTDECR0), 0, + UINT32_C(1) << g_switch.hw->sw_cpu_port_reg_idx); + UP_DSB(); + modifyreg32(PORTREG(g_switch.hw->sw_cpu_port_reg_idx, + NETC_PORT_POR), NETC_PORT_DISABLE, 0); + return OK; +} + +int imxrt_netc_switch_link(uint8_t *linkmask) +{ + unsigned int port; + uint16_t speed; + int result = OK; + int ret; + + *linkmask = 0; + if (g_switch.state == SWITCH_UNINITIALIZED) + { + return -ENODEV; + } + + for (port = 0; port < g_switch.hw->portcount; port++) + { + if (!g_switch.config->ports[port].enabled) + { + continue; + } + + ret = imxrt_netc_port_link(&g_switch.link[port], &speed); + if (ret < 0) + { + nerr("NETC switch port %u link: %d\n", port, ret); + result = ret; + speed = 0; + } + + if (speed != g_switch.link[port].speed) + { + ret = imxrt_netc_port_mode( + IMXRT_NETC_SW_PORT(g_switch.hw->ports[port].reg_idx), + IMXRT_NETC_SW_MAC(g_switch.hw->ports[port].reg_idx), + g_switch.config->ports[port].interface, + speed); + if (ret < 0) + { + nerr("NETC switch port %u MAC: %d\n", port, ret); + result = ret; + speed = 0; + } + + g_switch.link[port].speed = speed; + ninfo("NETC port %u: %u Mbps\n", port, speed); + } + + if (speed != 0) + { + *linkmask |= 1 << port; + } + } + + return result; +} + +#ifdef CONFIG_NETDEV_IOCTL +int imxrt_netc_switch_ioctl(int cmd, unsigned long arg) +{ + struct mii_ioctl_data_s *req = (struct mii_ioctl_data_s *)arg; + struct mii_ioctl_data_s request; + unsigned int port; + int ret; + + if (cmd != SIOCGMIIPHY && cmd != SIOCGMIIREG && cmd != SIOCSMIIREG) + { + return -ENOTTY; + } + + if (req == NULL) + { + return -EINVAL; + } + + /* phy_id[9:8] selects an external switch port, [4:0] is its MDIO + * address. SIOCGMIIPHY preserves the requested port and resolves address. + */ + + port = req->phy_id >> 8; + if (g_switch.state == SWITCH_UNINITIALIZED) + { + return -ENODEV; + } + + if (port >= g_switch.hw->portcount || (req->phy_id & 0xe0) != 0 || + !g_switch.config->ports[port].enabled) + { + return -EINVAL; + } + + if (cmd == SIOCGMIIPHY) + { + req->phy_id = (port << 8) | g_switch.link[port].phydev->mdio_addr; + return OK; + } + + if ((req->phy_id & 31) != g_switch.link[port].phydev->mdio_addr || + req->reg_num > 31) + { + return -EINVAL; + } + + request = *req; + request.phy_id &= 31; + ret = phy_ioctl(g_switch.link[port].phydev, cmd, + (unsigned long)&request); + if (ret == OK) + { + req->val_out = request.val_out; + } + + return ret; +} +#endif
