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 57aae57532705bc34892ef67865bcd9250013998 Author: lijiale11 <[email protected]> AuthorDate: Mon Jul 27 19:33:59 2026 +0800 net/phy: add RTL9000B 100BASE-T1 PHY driver Add Realtek RTL9000B 100BASE-T1 automotive PHY driver to the phylib framework. Includes convergence parameter loading, DSP configuration, DVDDIO selection, soft reset, operation mode control, and interrupt handling for UV/OT/link-change events. Uses independent registration model consistent with RTL8221B. Signed-off-by: lijiale11 <[email protected]> --- drivers/net/CMakeLists.txt | 4 + drivers/net/Kconfig | 7 + drivers/net/Make.defs | 4 + drivers/net/phy/realtek/rtl9000b.c | 1055 ++++++++++++++++++++ drivers/net/phy/realtek/rtl9000b.h | 164 +++ .../net/phy/realtek/rtl9000b_fw.c | 59 +- .../net/phy/realtek/rtl9000b_fw.h | 36 +- include/nuttx/net/realtek_phy.h | 4 + 8 files changed, 1280 insertions(+), 53 deletions(-) diff --git a/drivers/net/CMakeLists.txt b/drivers/net/CMakeLists.txt index 8074a1f480b..987aa730235 100644 --- a/drivers/net/CMakeLists.txt +++ b/drivers/net/CMakeLists.txt @@ -136,6 +136,10 @@ if(CONFIG_NET) if(CONFIG_NET_PHY_REALTEK_RTL8221B) list(APPEND SRCS phy/realtek/rtl8221b.c phy/realtek/rtl8221b_fw.c) endif() + + if(CONFIG_NET_PHY_REALTEK_RTL9000B) + list(APPEND SRCS phy/realtek/rtl9000b.c phy/realtek/rtl9000b_fw.c) + endif() endif() target_sources(drivers PRIVATE ${SRCS}) diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index 9a5766d9a0d..f6a95c7b265 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -885,6 +885,13 @@ config NET_PHY_REALTEK_RTL8221B (ramcode) blob, which consumes a few KB of flash, so leaving this disabled keeps flash/RAM usage minimal on embedded targets. +config NET_PHY_REALTEK_RTL9000B + bool "Realtek RTL9000B 100BASE-T1 PHY" + default n + ---help--- + Support for the Realtek RTL9000B 100BASE-T1 automotive PHY. + Enable only on boards that use an RTL9000B. + endif # NET_PHYLIB endif # NETDEVICES diff --git a/drivers/net/Make.defs b/drivers/net/Make.defs index 2c7dcf53734..354dc836336 100644 --- a/drivers/net/Make.defs +++ b/drivers/net/Make.defs @@ -143,6 +143,10 @@ ifeq ($(CONFIG_NET_PHY_REALTEK_RTL8221B),y) CSRCS += rtl8221b.c rtl8221b_fw.c endif +ifeq ($(CONFIG_NET_PHY_REALTEK_RTL9000B),y) + CSRCS += rtl9000b.c rtl9000b_fw.c +endif + DEPPATH += --dep-path net/phy/realtek VPATH += :net/phy/realtek endif diff --git a/drivers/net/phy/realtek/rtl9000b.c b/drivers/net/phy/realtek/rtl9000b.c new file mode 100644 index 00000000000..f26e14d6565 --- /dev/null +++ b/drivers/net/phy/realtek/rtl9000b.c @@ -0,0 +1,1055 @@ +/**************************************************************************** + * drivers/net/phy/realtek/rtl9000b.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 <inttypes.h> +#include <nuttx/arch.h> + +#include <nuttx/net/phy.h> +#include <nuttx/net/mii.h> +#include <nuttx/net/realtek_phy.h> +#include <nuttx/ethtool.h> + +#include "phy.h" +#include "rtl9000b.h" +#include "rtl9000b_fw.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Indirect window address iterations for uCode ready polling loops */ + +#define RTL_UCODE_ITERS 2000 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rtl9000b_config_init(FAR struct phy_device_s *phydev); +static int rtl9000b_config_aneg(FAR struct phy_device_s *phydev); +static int rtl9000b_read_status(FAR struct phy_device_s *phydev); +static int rtl9000b_config_power(FAR struct phy_device_s *phydev, + bool enable); +static int rtl9000b_config_intr(FAR struct phy_device_s *phydev, + bool enable); +static int rtl9000b_handle_interrupt(FAR struct phy_device_s *phydev); +static int rtl9000b_read_page(FAR struct phy_device_s *phydev); +static int rtl9000b_write_page(FAR struct phy_device_s *phydev, int page); + +static int rtl9000b_win_read(FAR struct phy_device_s *phydev, + uint16_t addr, FAR uint16_t *value); +static int rtl9000b_win_write(FAR struct phy_device_s *phydev, + uint16_t addr, uint16_t value); +static int rtl9000b_win_modify(FAR struct phy_device_s *phydev, + uint16_t addr, uint16_t mask, + uint16_t set); +static int rtl9000b_poll_ucode(FAR struct phy_device_s *phydev, + uint16_t expected, int timeout); +static int rtl9000b_load_dsp(FAR struct phy_device_s *phydev, + FAR const uint16_t *params, int count); +static int rtl9000b_wait_ready(FAR struct phy_device_s *phydev); +static int rtl9000b_write_config(FAR struct phy_device_s *phydev); +static int rtl9000b_check_config(FAR struct phy_device_s *phydev); +static int rtl9000b_set_dvddio(FAR struct phy_device_s *phydev, + rtl9000b_dvddio_e sel); +static int rtl9000b_soft_reset(FAR struct phy_device_s *phydev); +static int rtl9000b_set_op_mode(FAR struct phy_device_s *phydev, + uint8_t cmd); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct phy_driver_s g_rtl9000b_driver = +{ + .name = "Realtek RTL9000B", + .phy_id = RTL9000B_PHY_ID, + .phy_id_mask = RTL9000B_PHY_ID_MASK, + .config_init = rtl9000b_config_init, + .config_aneg = rtl9000b_config_aneg, + .read_status = rtl9000b_read_status, + .config_power = rtl9000b_config_power, + .config_intr = rtl9000b_config_intr, + .handle_interrupt = rtl9000b_handle_interrupt, + .read_page = rtl9000b_read_page, + .write_page = rtl9000b_write_page, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl9000b_win_read + * + * Description: + * Read a register through the indirect window (reg 0x1b/0x1c = reg 27/28). + * Holds the MDIO bus lock across the entire write-addr+read-data sequence. + * + ****************************************************************************/ + +static int rtl9000b_win_read(FAR struct phy_device_s *phydev, + uint16_t addr, FAR uint16_t *value) +{ + FAR struct mdio_bus_s *bus = phydev->bus; + uint8_t pa = phydev->mdio_addr; + int ret; + + mdio_lock(bus); + ret = mdio_write_nolock(bus, pa, RTL_WIN_ADDR, addr); + if (ret >= 0) + { + ret = mdio_read_nolock(bus, pa, RTL_WIN_DATA, value); + } + + mdio_unlock(bus); + return ret; +} + +/**************************************************************************** + * Name: rtl9000b_win_write + * + * Description: + * Write a register through the indirect window. Holds the MDIO bus + * lock across the entire write-addr+write-data sequence. + * + ****************************************************************************/ + +static int rtl9000b_win_write(FAR struct phy_device_s *phydev, + uint16_t addr, uint16_t value) +{ + FAR struct mdio_bus_s *bus = phydev->bus; + uint8_t pa = phydev->mdio_addr; + int ret; + + mdio_lock(bus); + ret = mdio_write_nolock(bus, pa, RTL_WIN_ADDR, addr); + if (ret >= 0) + { + ret = mdio_write_nolock(bus, pa, RTL_WIN_DATA, value); + } + + mdio_unlock(bus); + return ret; +} + +/**************************************************************************** + * Name: rtl9000b_win_modify + * + * Description: + * Read-modify-write a register through the indirect window. + * Clears bits in 'mask' then sets bits in 'set'. + * + ****************************************************************************/ + +static int rtl9000b_win_modify(FAR struct phy_device_s *phydev, + uint16_t addr, uint16_t mask, + uint16_t set) +{ + uint16_t val; + int ret; + + ret = rtl9000b_win_read(phydev, addr, &val); + if (ret < 0) + { + return ret; + } + + val &= ~mask; + val |= set; + + return rtl9000b_win_write(phydev, addr, val); +} + +/**************************************************************************** + * Name: rtl9000b_poll_ucode + * + * Description: + * Poll the uCode ready bit (bit6 of indirect window 0xb800) until it + * matches 'expected'. Holds the bus lock for the entire poll loop, + * matching the original driver's single write27 + repeated read28 pattern. + * + ****************************************************************************/ + +static int rtl9000b_poll_ucode(FAR struct phy_device_s *phydev, + uint16_t expected, int timeout) +{ + FAR struct mdio_bus_s *bus = phydev->bus; + uint8_t pa = phydev->mdio_addr; + uint16_t val; + int ret; + + mdio_lock(bus); + ret = mdio_write_nolock(bus, pa, RTL_WIN_ADDR, 0xb800); + if (ret < 0) + { + goto out; + } + + do + { + ret = mdio_read_nolock(bus, pa, RTL_WIN_DATA, &val); + if (ret < 0) + { + goto out; + } + + if ((val & RTL_UCODE_RDY) == expected) + { + break; + } + } + while (--timeout > 0); + + ret = timeout > 0 ? OK : -ETIMEDOUT; + +out: + mdio_unlock(bus); + return ret; +} + +/**************************************************************************** + * Name: rtl9000b_load_dsp + * + * Description: + * Write DSP parameter words sequentially through indirect window 0xa014. + * Holds the bus lock for the entire burst, matching the original driver's + * single write27 + repeated write28 pattern. + * + ****************************************************************************/ + +static int rtl9000b_load_dsp(FAR struct phy_device_s *phydev, + FAR const uint16_t *params, int count) +{ + FAR struct mdio_bus_s *bus = phydev->bus; + uint8_t pa = phydev->mdio_addr; + int ret; + int i; + + mdio_lock(bus); + ret = mdio_write_nolock(bus, pa, RTL_WIN_ADDR, 0xa014); + if (ret < 0) + { + goto out; + } + + for (i = 0; i < count; i++) + { + ret = mdio_write_nolock(bus, pa, RTL_WIN_DATA, params[i]); + if (ret < 0) + { + goto out; + } + } + +out: + mdio_unlock(bus); + return ret; +} + +/**************************************************************************** + * Name: rtl9000b_wait_ready + * + * Description: + * Wait for the PHY to complete its internal power-on sequence. + * Polls page 0xa42 register 0x10 until bit [2:0] == 0x3. + * + ****************************************************************************/ + +static int rtl9000b_wait_ready(FAR struct phy_device_s *phydev) +{ + int timeout = RTL_READY_TO; + uint16_t val; + int ret; + + do + { + ret = phy_read_paged(phydev, RTL_PAGE_DEFAULT, 0x10, &val); + if (ret < 0) + { + return ret; + } + + if ((val & RTL_READY_MASK) == RTL_READY_VAL) + { + return OK; + } + } + while (--timeout > 0); + + return -ETIMEDOUT; +} + +/**************************************************************************** + * Name: rtl9000b_write_config + * + * Description: + * Write the vendor-provided initialization register sequence. + * Exact transliteration of RTL9000Bx_Initial_Configuration(). + * + ****************************************************************************/ + +static int rtl9000b_write_config(FAR struct phy_device_s *phydev) +{ + int ret; + + /* Phase 1: indirect window writes + page-banked writes */ + + rtl9000b_win_write(phydev, 0x8017, 0xfb03); + rtl9000b_win_write(phydev, 0x8092, 0x6000); + rtl9000b_win_write(phydev, 0x80af, 0x6000); + rtl9000b_win_write(phydev, 0x807d, 0x4443); + rtl9000b_win_write(phydev, 0x809a, 0x4443); + rtl9000b_win_write(phydev, 0x81a3, 0x0f00); + phy_write_paged(phydev, 0x0a81, 18, 0x0004); + rtl9000b_win_write(phydev, 0x83c8, 0x0005); + phy_write_paged(phydev, 0x0a5c, 18, 0x0003); + + /* Phase 2: start uCode loading */ + + rtl9000b_win_write(phydev, 0xb820, 0x0010); + rtl9000b_win_write(phydev, 0xb830, 0x8000); + + ret = rtl9000b_poll_ucode(phydev, RTL_UCODE_RDY, RTL_UCODE_TO); + if (ret < 0) + { + return ret; + } + + /* Phase 3: DSP parameter loading */ + + rtl9000b_win_write(phydev, 0x8020, 0x3300); + rtl9000b_win_write(phydev, 0xb82e, 0x0001); + rtl9000b_win_write(phydev, 0xb820, 0x0290); + rtl9000b_win_write(phydev, 0xa012, 0x0000); + ret = rtl9000b_load_dsp(phydev, g_rtl9000b_param_check2, 5); + if (ret < 0) + { + return ret; + } + + rtl9000b_win_write(phydev, 0xa01a, 0x0000); + rtl9000b_win_write(phydev, 0xa000, 0x11ef); + + /* Phase 4: wait for uCode loading to complete */ + + rtl9000b_win_write(phydev, 0xb820, 0x0210); + rtl9000b_win_write(phydev, 0xb82e, 0x0000); + rtl9000b_win_write(phydev, 0x8020, 0x0000); + rtl9000b_win_write(phydev, 0xb820, 0x0000); + + return rtl9000b_poll_ucode(phydev, 0, RTL_UCODE_TO); +} + +/**************************************************************************** + * Name: rtl9000b_check_config + * + * Description: + * Read back and verify all configured registers. + * Exact transliteration of RTL9000Bx_Initial_Configuration_Check(). + * + ****************************************************************************/ + +static int rtl9000b_check_config(FAR struct phy_device_s *phydev) +{ + uint16_t data; + uint16_t data_temp; + uint16_t expected; + uint16_t page; + uint16_t reg; + uint16_t i; + uint16_t param_addr; + int ret; + + /* Pass 1: verify param_check[] 9 triplets */ + + for (i = 0; i < 27; i += 3) + { + page = g_rtl9000b_param_check[i]; + reg = g_rtl9000b_param_check[i + 1]; + expected = g_rtl9000b_param_check[i + 2]; + + if (page == 0) + { + ret = rtl9000b_win_read(phydev, reg, &data); + } + else + { + ret = phy_read_paged(phydev, page, reg, &data); + } + + if (ret < 0) + { + return ret; + } + + if (expected != data) + { + nerr("ERROR: RTL9000B param[%d] mismatch: " + "page=0x%04x reg=0x%04x got=0x%04x exp=0x%04x\n", + i / 3, page, reg, data, expected); + return -EIO; + } + } + + /* Pass 2: re-start uCode */ + + rtl9000b_win_write(phydev, 0xb820, 0x0010); + rtl9000b_win_write(phydev, 0xb830, 0x8000); + + ret = rtl9000b_poll_ucode(phydev, RTL_UCODE_RDY, RTL_UCODE_ITERS); + if (ret < 0) + { + return ret; + } + + /* Pass 3: verify DSP parameters via page 0xa01 */ + + rtl9000b_win_write(phydev, 0x8020, 0x3300); + rtl9000b_win_write(phydev, 0xb82e, 0x0001); + + param_addr = 0; + for (i = 0; i < 5; i++) + { + ret = phy_read_paged(phydev, 0x0a01, 17, &data_temp); + if (ret < 0) + { + return ret; + } + + data_temp &= ~0x01ff; + param_addr &= 0x01ff; + ret = phy_write_paged(phydev, 0x0a01, 17, + (data_temp | param_addr)); + if (ret < 0) + { + return ret; + } + + ret = phy_read_paged(phydev, 0x0a01, 18, &data); + if (ret < 0) + { + return ret; + } + + expected = g_rtl9000b_param_check2[i]; + if (expected != data) + { + nerr("ERROR: RTL9000B dsp param[%d] mismatch: " + "got=0x%04x exp=0x%04x\n", + i, data, expected); + return -EIO; + } + + param_addr++; + } + + /* Pass 4: verify remaining parameter */ + + for (i = 0; i < 3; i += 3) + { + page = g_rtl9000b_param_check3[i]; + reg = g_rtl9000b_param_check3[i + 1]; + expected = g_rtl9000b_param_check3[i + 2]; + + if (page == 0) + { + ret = rtl9000b_win_read(phydev, reg, &data); + } + else + { + ret = phy_read_paged(phydev, page, reg, &data); + } + + if (ret < 0) + { + return ret; + } + + if (expected != data) + { + nerr("ERROR: RTL9000B check3 mismatch: " + "page=0x%04x reg=0x%04x got=0x%04x exp=0x%04x\n", + page, reg, data, expected); + return -EIO; + } + } + + /* Cleanup: release uCode */ + + rtl9000b_win_write(phydev, 0xb82e, 0x0000); + rtl9000b_win_write(phydev, 0x8020, 0x0000); + rtl9000b_win_write(phydev, 0xb820, 0x0000); + + return rtl9000b_poll_ucode(phydev, 0, RTL_UCODE_ITERS); +} + +/**************************************************************************** + * Name: rtl9000b_set_dvddio + * + * Description: + * Select the DVDDIO supply source (page 0xa4c reg 18 bits [12:10]). + * Transliteration of RTL9000Bx_ISR_SEL(). + * + ****************************************************************************/ + +static int rtl9000b_set_dvddio(FAR struct phy_device_s *phydev, + rtl9000b_dvddio_e sel) +{ + uint16_t set_val; + + switch (sel) + { + case DVDDIO_LDO_1V8: + set_val = 0x0800; + break; + case DVDDIO_LDO_2V5: + set_val = 0x1000; + break; + case DVDDIO_EXTPWR_1V8: + set_val = 0x2000; + break; + case DVDDIO_EXTPWR_2V5: + set_val = 0x2800; + break; + case DVDDIO_EXTPWR_3V3: + set_val = 0x3000; + break; + default: + return -EINVAL; + } + + return phy_modify_paged(phydev, RTL_PAGE_DVDDIO, 18, 0x3800, set_val); +} + +/**************************************************************************** + * Name: rtl9000b_soft_reset + * + * Description: + * Soft-reset the PHY via MCR bit 15. Polls until the reset completes + * (MCR reads back as 0x2100). + * + ****************************************************************************/ + +static int rtl9000b_soft_reset(FAR struct phy_device_s *phydev) +{ + int timeout = RTL_RST_TO; + int ret; + + ret = phy_write(phydev, MII_MCR, RTL_MCR_RST); + if (ret < 0) + { + return ret; + } + + do + { + ret = phy_read(phydev, MII_MCR); + if (ret < 0) + { + return ret; + } + + if (ret == RTL_RST_DONE) + { + return OK; + } + } + while (--timeout > 0); + + return -ETIMEDOUT; +} + +/**************************************************************************** + * Name: rtl9000b_set_op_mode + * + * Description: + * Set the PHY operational mode (Normal / Sleep / Standby) via the OPCR + * registers (OPCR2 at indirect 0xdd00, OPCR4 at indirect 0xdd20). + * Transliteration of RTL9000Bx_Set_OP_state(). + * + ****************************************************************************/ + +static int rtl9000b_set_op_mode(FAR struct phy_device_s *phydev, uint8_t cmd) +{ + uint16_t mode; + int ret; + + switch (cmd) + { + case OP_CMD_NORMAL: + mode = RTL_OPCR_NORMAL; + break; + case OP_CMD_SLEEP: + mode = RTL_OPCR_SLEEP; + break; + case OP_CMD_STANDBY: + mode = RTL_OPCR_STANDBY; + break; + default: + return -EINVAL; + } + + ret = rtl9000b_win_modify(phydev, RTL_OPCR2, RTL_OPCR_MODE_MSK, mode); + if (ret < 0) + { + return ret; + } + + /* Normal mode also requires OPCR4 configuration */ + + if (cmd == OP_CMD_NORMAL) + { + ret = rtl9000b_win_modify(phydev, RTL_OPCR4, RTL_OPCR4_MSK, + RTL_OPCR4_NORMAL); + } + + return ret; +} + +/**************************************************************************** + * Name: rtl9000b_config_init + * + * Description: + * Initialize RTL9000B PHY: wait for the PHY to become ready, write the + * vendor initialization sequence (convergence parameters, uCode, DSP), + * verify all configured registers, set DVDDIO to external 3.3V, soft- + * reset and set Normal operation mode. 100BASE-T1 has no Clause 28 AN, + * so autoneg is disabled and DSP training completes automatically. + * + * Input Parameters: + * phydev - PHY device + * + * Returned Value: + * OK on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int rtl9000b_config_init(FAR struct phy_device_s *phydev) +{ + int ret; + + /* 6-step vendor initialization sequence */ + + ret = rtl9000b_wait_ready(phydev); + if (ret < 0) + { + nerr("ERROR: RTL9000B not ready: %d\n", ret); + return ret; + } + + ret = rtl9000b_write_config(phydev); + if (ret < 0) + { + nerr("ERROR: RTL9000B config write failed: %d\n", ret); + return ret; + } + + ret = rtl9000b_check_config(phydev); + if (ret < 0) + { + nerr("ERROR: RTL9000B config check failed: %d\n", ret); + return ret; + } + + ret = rtl9000b_set_dvddio(phydev, DVDDIO_EXTPWR_3V3); + if (ret < 0) + { + nerr("ERROR: RTL9000B DVDDIO set failed: %d\n", ret); + return ret; + } + + ret = rtl9000b_soft_reset(phydev); + if (ret < 0) + { + nerr("ERROR: RTL9000B soft reset failed: %d\n", ret); + return ret; + } + + ret = rtl9000b_set_op_mode(phydev, OP_CMD_NORMAL); + if (ret < 0) + { + nerr("ERROR: RTL9000B set OP mode failed: %d\n", ret); + return ret; + } + + /* Advertise 100BASE-T1 full-duplex capabilities */ + + phydev->supported = SUPPORTED_100BASET1_FULL | + SUPPORTED_TP; + phydev->advertising = phydev->supported; + phydev->autoneg = false; + + ninfo("RTL9000B config_init done: supported=0x%08" PRIx32 "\n", + phydev->supported); + return OK; +} + +/**************************************************************************** + * Name: rtl9000b_config_aneg + * + * Description: + * Configure auto-negotiation. 100BASE-T1 has no Clause 28 AN; DSP + * training completes automatically in Normal mode. When recovering from + * HALTED or ERROR state the full config_init sequence is re-run because + * the convergence parameters must be rewritten. + * + * Input Parameters: + * phydev - PHY device + * + * Returned Value: + * OK on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int rtl9000b_config_aneg(FAR struct phy_device_s *phydev) +{ + /* 100BASE-T1 has no Clause 28 auto-negotiation. + * "Re-trigger negotiation" = re-run the full vendor init sequence: + * convergence parameters, DSP loading, DVDDIO, soft reset, Normal mode. + * + * Use phydev->state (not hardware OPCR2) to decide whether to + * re-init: PHY_READY means config_init was just called by + * phy_connect(), so skip. PHY_HALTED/PHY_ERROR mean recovery + * from stop or error, so full re-init is needed. + */ + + if (phydev->state == PHY_HALTED || phydev->state == PHY_ERROR) + { + return rtl9000b_config_init(phydev); + } + + return OK; +} + +/**************************************************************************** + * Name: rtl9000b_read_status + * + * Description: + * Poll the PHY for MDIO liveness and link status. Uses PHYSR2 (reg 0x1a) + * bit2 for real-time link detection per vendor recommendation. Returns + * -EIO when the PHY is unreachable (Sleep/Power-Off); otherwise updates + * phydev->link/speed/duplex and returns OK. + * + * Input Parameters: + * phydev - PHY device + * + * Returned Value: + * OK on success (link up or down); -EIO if the PHY is unreachable. + * + ****************************************************************************/ + +static int rtl9000b_read_status(FAR struct phy_device_s *phydev) +{ + int val; + + /* PHYSR2 (reg 0x1a) bit2: real-time link status. + * Vendor note: this bit also implies local receiver OK (locok) + * and descrambler OK, so separate locok/remok/PCS checks are + * unnecessary. + */ + + val = phy_read(phydev, RTL_PHYSR2); + if (val < 0 || val == 0xffff) + { + return -EIO; + } + + val = phy_read(phydev, RTL_PHYSR2); + if (val < 0 || val == 0xffff) + { + return -EIO; + } + + phydev->link = (val & RTL_PHYSR2_LINK) != 0; + + /* Publish the result. + * The caller (phy_check_link) holds phydev->lock, so no additional + * locking is needed here. link == false is a normal outcome + * (cable unplugged) -- return OK so the framework continues polling. + */ + + if (phydev->link) + { + phydev->speed = 100; + phydev->duplex = DUPLEX_FULL; + } + else + { + phydev->link = false; + phydev->speed = 0; + phydev->duplex = DUPLEX_UNKNOWN; + } + + return OK; +} + +/**************************************************************************** + * Name: rtl9000b_config_power + * + * Description: + * Power the PHY up or down. Power down enters Sleep mode; power up + * soft-resets, sets DVDDIO to external 3.3V, and returns to Normal + * operation mode. + * + * Input Parameters: + * phydev - PHY device + * enable - true to power up, false to power down + * + * Returned Value: + * OK on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int rtl9000b_config_power(FAR struct phy_device_s *phydev, + bool enable) +{ + if (enable) + { + int ret; + + ret = rtl9000b_soft_reset(phydev); + if (ret < 0) + { + return ret; + } + + ret = rtl9000b_set_dvddio(phydev, DVDDIO_EXTPWR_3V3); + if (ret < 0) + { + return ret; + } + + return rtl9000b_set_op_mode(phydev, OP_CMD_NORMAL); + } + + return rtl9000b_set_op_mode(phydev, OP_CMD_SLEEP); +} + +/**************************************************************************** + * Name: rtl9000b_config_intr + * + * Description: + * Enable or disable PHY interrupt output (INTB). + * Enable: clear pending, program GINER/GINMR and OPINER/OPINMR. + * Disable: mask all, clear pending. + * + ****************************************************************************/ + +static int rtl9000b_config_intr(FAR struct phy_device_s *phydev, + bool enable) +{ + uint16_t dummy; + + if (enable) + { + /* Clear all pending interrupts first (read-clear) */ + + phy_read(phydev, RTL_GINSR); + rtl9000b_win_read(phydev, RTL_OPINSR1, &dummy); + rtl9000b_win_read(phydev, RTL_OPINSR2, &dummy); + rtl9000b_win_read(phydev, RTL_OPINSR3, &dummy); + + /* Enable General interrupts: Fatal + Link Change + PHY Status */ + + phy_write_paged(phydev, RTL_PAGE_DEFAULT, + RTL_GINER, RTL_GINTR_ENABLE); + phy_write_paged(phydev, RTL_PAGE_DEFAULT, + RTL_GINMR, ~RTL_GINTR_ENABLE); + + /* Enable only known OP interrupt sources (leave reserved bits + * alone to avoid spurious interrupts from undefined bits). + */ + + rtl9000b_win_write(phydev, RTL_OPINER1, RTL_UV_MASK); + rtl9000b_win_write(phydev, RTL_OPINER2, + RTL_OT_FLAG | RTL_REC_MASK); + rtl9000b_win_write(phydev, RTL_OPINER3, 0x0000); + + /* Unmask corresponding bits (write 0 = unmask) */ + + rtl9000b_win_write(phydev, RTL_OPINMR1, + ~RTL_UV_MASK); + rtl9000b_win_write(phydev, RTL_OPINMR2, + ~(RTL_OT_FLAG | RTL_REC_MASK)); + rtl9000b_win_write(phydev, RTL_OPINMR3, 0xffff); + } + else + { + /* Mask all interrupts */ + + phy_write_paged(phydev, RTL_PAGE_DEFAULT, RTL_GINMR, 0xffff); + rtl9000b_win_write(phydev, RTL_OPINMR1, 0xffff); + rtl9000b_win_write(phydev, RTL_OPINMR2, 0xffff); + rtl9000b_win_write(phydev, RTL_OPINMR3, 0xffff); + + /* Clear any pending status */ + + phy_read(phydev, RTL_GINSR); + } + + return OK; +} + +/**************************************************************************** + * Name: rtl9000b_handle_interrupt + * + * Description: + * Handle a pending PHY interrupt. Reads GINSR (read-clear, releases + * INTB) and OPINSR1/2/3 for detailed source decode. + * + ****************************************************************************/ + +static int rtl9000b_handle_interrupt(FAR struct phy_device_s *phydev) +{ + int ginsr; + uint16_t op1; + uint16_t op2; + uint16_t op3; + bool link_event = false; + + ginsr = phy_read(phydev, RTL_GINSR); + if (ginsr < 0) + { + return ginsr; + } + + if (ginsr & RTL_GINTR_FATAL) + { + nerr("RTL9000B: PHY FATAL ERROR\n"); + return -EIO; + } + + if (ginsr & RTL_GINTR_LINK) + { + link_event = true; + } + + if (ginsr & RTL_GINTR_PHY) + { + /* Read OP interrupt status for detailed source decode */ + + rtl9000b_win_read(phydev, RTL_OPINSR1, &op1); + rtl9000b_win_read(phydev, RTL_OPINSR2, &op2); + rtl9000b_win_read(phydev, RTL_OPINSR3, &op3); + + if (op1 & RTL_UV_MASK) + { + nwarn("WARNING: RTL9000B UV SLEEP OPINSR1=0x%04x\n", op1); + link_event = true; + } + + if (op2 & RTL_OT_FLAG) + { + nwarn("WARNING: RTL9000B OVER TEMP\n"); + } + + if (op2 & RTL_REC_MASK) + { + ninfo("RTL9000B UV recovered, restoring Normal mode\n"); + + /* PHY auto-transitions Sleep → Standby on UV recovery. + * Must explicitly switch back to Normal mode for link training. + */ + + rtl9000b_set_op_mode(phydev, OP_CMD_NORMAL); + link_event = true; + } + + if (op3) + { + link_event = true; + } + } + + return link_event; +} + +/**************************************************************************** + * Name: rtl9000b_read_page + * + * Description: + * Return the currently selected page. RTL9000B uses register 31 (0x1f) + * as the page-select register. The MDIO bus lock is already held by the + * phylib core (phy_select_page), so use the lockless primitive. + * + ****************************************************************************/ + +static int rtl9000b_read_page(FAR struct phy_device_s *phydev) +{ + uint16_t page; + int ret; + + ret = mdio_read_nolock(phydev->bus, phydev->mdio_addr, 31, &page); + if (ret < 0) + { + return ret; + } + + return page; +} + +/**************************************************************************** + * Name: rtl9000b_write_page + * + * Description: + * Select 'page' by writing it to register 31 (0x1f). The MDIO bus lock + * is already held by the phylib core (phy_select_page), so use the + * lockless primitive. + * + ****************************************************************************/ + +static int rtl9000b_write_page(FAR struct phy_device_s *phydev, int page) +{ + return mdio_write_nolock(phydev->bus, phydev->mdio_addr, 31, page); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl9000b_register + * + * Description: + * Register the RTL9000B PHY driver with the phylib framework. + * + * Input Parameters: + * None + * + * Returned Value: + * OK on success; a negated errno value on failure. + * + ****************************************************************************/ + +int rtl9000b_register(void) +{ + return phy_driver_register(&g_rtl9000b_driver); +} diff --git a/drivers/net/phy/realtek/rtl9000b.h b/drivers/net/phy/realtek/rtl9000b.h new file mode 100644 index 00000000000..b57de2d7e0a --- /dev/null +++ b/drivers/net/phy/realtek/rtl9000b.h @@ -0,0 +1,164 @@ +/**************************************************************************** + * drivers/net/phy/realtek/rtl9000b.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 __DRIVERS_NET_PHY_REALTEK_RTL9000B_H +#define __DRIVERS_NET_PHY_REALTEK_RTL9000B_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/net/phy.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* PHY ID: IEEE silicon-fixed (Realtek OUI 0x001cc), unaffected by Ax-pin + * strapping. Mask 0xfffff000 matches all RTL9000B variants. + */ + +#define RTL9000B_PHY_ID 0x001ccb00 +#define RTL9000B_PHY_ID_MASK 0xffffff00 + +/* OP mode commands */ + +#define OP_CMD_STANDBY 1 +#define OP_CMD_NORMAL 2 +#define OP_CMD_SLEEP 3 + +/* Page addresses (reg31 page select) */ + +#define RTL_PAGE_DEFAULT 0x0a42 +#define RTL_PAGE_LINK 0x0a60 +#define RTL_PAGE_DVDDIO 0x0a4c +#define RTL_PAGE_PARAM 0x0a01 + +/* PHY ready polling (page 0xa42 reg 0x10) */ + +#define RTL_READY_TO 4000 +#define RTL_READY_MASK 0x0007 +#define RTL_READY_VAL 0x0003 + +/* Soft reset (standard MCR) */ + +#define RTL_RST_TO 2000 +#define RTL_MCR_RST 0x8000 +#define RTL_RST_DONE 0x2100 + +/* uCode loading poll (indirect 0xb800 bit6) */ + +#define RTL_UCODE_TO 2000 +#define RTL_UCODE_RDY 0x0040 + +/* Indirect window registers (= reg 27 / reg 28) */ + +#define RTL_WIN_ADDR 0x1b +#define RTL_WIN_DATA 0x1c + +/* OPCR registers (via indirect window) */ + +#define RTL_OPCR2 0xdd00 +#define RTL_OPCR4 0xdd20 +#define RTL_OPCR_MODE_MSK 0x001f +#define RTL_OPCR4_MSK 0x000f +#define RTL_OPCR_NORMAL 0x0013 +#define RTL_OPCR_SLEEP 0x0014 +#define RTL_OPCR_STANDBY 0x0012 +#define RTL_OPCR4_NORMAL 0x000b + +/* General interrupt registers (page 0xa42) */ + +#define RTL_GINER 0x12 +#define RTL_GINMR 0x14 +#define RTL_GINSR 0x1d /* C22 direct, read-clear */ + +#define RTL_GINTR_FATAL (1 << 11) +#define RTL_GINTR_LINK (1 << 4) +#define RTL_GINTR_PHY (1 << 0) + +#define RTL_GINTR_ENABLE \ + (RTL_GINTR_FATAL | RTL_GINTR_LINK | RTL_GINTR_PHY) + +/* OP interrupt registers (via indirect window) */ + +#define RTL_OPINSR1 0xdd08 +#define RTL_OPINER1 0xdd0c +#define RTL_OPINMR1 0xdd0e +#define RTL_OPINSR2 0xdd10 +#define RTL_OPINER2 0xdd14 +#define RTL_OPINMR2 0xdd16 +#define RTL_OPINSR3 0xdd18 +#define RTL_OPINER3 0xdd1c +#define RTL_OPINMR3 0xdd1e + +/* OPINSR1 bit definitions (UV sources) */ + +#define RTL_UV_AVDD33 (1 << 5) +#define RTL_UV_DVDD (1 << 4) +#define RTL_UV_AVDD09 (1 << 3) +#define RTL_UV_DVDD09 (1 << 2) +#define RTL_UV_MII (1 << 1) +#define RTL_UV_MASK 0x00be + +/* OPINSR2 bit definitions */ + +#define RTL_OT_FLAG (1 << 11) +#define RTL_REC_MASK 0x001f + +/* PHYSR2 (Realtek PHY Specific Status Register 2, C22 reg 0x1a). + * bit2: Real-time link status (1 = link up). Updates faster than the + * MSR latch, recommended by the vendor for link detection. + */ + +#define RTL_PHYSR2 0x1a +#define RTL_PHYSR2_LINK (1 << 2) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* DVDDIO supply selection */ + +typedef enum +{ + DVDDIO_LDO_1V8 = 1, + DVDDIO_LDO_2V5 = 2, + DVDDIO_EXTPWR_1V8 = 4, + DVDDIO_EXTPWR_2V5 = 5, + DVDDIO_EXTPWR_3V3 = 6, +} rtl9000b_dvddio_e; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __DRIVERS_NET_PHY_REALTEK_RTL9000B_H */ diff --git a/include/nuttx/net/realtek_phy.h b/drivers/net/phy/realtek/rtl9000b_fw.c similarity index 64% copy from include/nuttx/net/realtek_phy.h copy to drivers/net/phy/realtek/rtl9000b_fw.c index a4c49c81a9b..5d3ec74a348 100644 --- a/include/nuttx/net/realtek_phy.h +++ b/drivers/net/phy/realtek/rtl9000b_fw.c @@ -1,5 +1,5 @@ /**************************************************************************** - * include/nuttx/net/realtek_phy.h + * drivers/net/phy/realtek/rtl9000b_fw.c * * SPDX-License-Identifier: Apache-2.0 * @@ -20,45 +20,44 @@ * ****************************************************************************/ -#ifndef __INCLUDE_NUTTX_NET_REALTEK_PHY_H -#define __INCLUDE_NUTTX_NET_REALTEK_PHY_H - /**************************************************************************** * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "rtl9000b_fw.h" /**************************************************************************** - * Public Function Prototypes + * Public Data ****************************************************************************/ -#ifdef __cplusplus -extern "C" +/* Parameter verification tables: exact copies from vendor reference + * (RTL9000Bx_Initial_Configuration_Check). + * Each triplet is {page, register, expected_value}. + */ + +const uint16_t g_rtl9000b_param_check[27] = +{ + 0, 0x8017, 0xfb03, 0, 0x8092, 0x6000, + 0, 0x80af, 0x6000, 0, 0x807d, 0x4443, + 0, 0x809a, 0x4443, 0, 0x81a3, 0x0f00, + 0x0a81, 0x12, 0x0004, 0, 0x83c8, 0x0005, + 0x0a5c, 0x12, 0x0003 +}; + +const uint16_t g_rtl9000b_param_check2[5] = +{ + 0x401c, 0xa610, 0x8520, 0xa510, 0x21f4 +}; + +const uint16_t g_rtl9000b_param_check3[3] = { -#endif + 0, 0xa000, 0x11ef +}; /**************************************************************************** - * Name: rtl8221b_register - * - * Description: - * Register the RTL8221B PHY driver with the PHYLIB framework. Must be - * called once from board/MAC initialization, before connecting an - * RTL8221B PHY. Registration is not idempotent: each call consumes one - * entry of the fixed-size driver table (CONFIG_NET_PHYLIB_MAX_DRIVERS), - * so it must not be called repeatedly. - * - * Returned Value: - * OK on success; negated errno on failure. - * + * Public Functions ****************************************************************************/ -#ifdef CONFIG_NET_PHY_REALTEK_RTL8221B -int rtl8221b_register(void); -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* __INCLUDE_NUTTX_NET_REALTEK_PHY_H */ +/* This file only provides the RTL9000B config verification parameter + * tables; there are no functions defined here. + */ diff --git a/include/nuttx/net/realtek_phy.h b/drivers/net/phy/realtek/rtl9000b_fw.h similarity index 60% copy from include/nuttx/net/realtek_phy.h copy to drivers/net/phy/realtek/rtl9000b_fw.h index a4c49c81a9b..864987d3f07 100644 --- a/include/nuttx/net/realtek_phy.h +++ b/drivers/net/phy/realtek/rtl9000b_fw.h @@ -1,5 +1,5 @@ /**************************************************************************** - * include/nuttx/net/realtek_phy.h + * drivers/net/phy/realtek/rtl9000b_fw.h * * SPDX-License-Identifier: Apache-2.0 * @@ -20,17 +20,18 @@ * ****************************************************************************/ -#ifndef __INCLUDE_NUTTX_NET_REALTEK_PHY_H -#define __INCLUDE_NUTTX_NET_REALTEK_PHY_H +#ifndef __DRIVERS_NET_PHY_REALTEK_RTL9000B_FW_H +#define __DRIVERS_NET_PHY_REALTEK_RTL9000B_FW_H /**************************************************************************** * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include <stdint.h> +#include <stddef.h> /**************************************************************************** - * Public Function Prototypes + * Public Data ****************************************************************************/ #ifdef __cplusplus @@ -38,27 +39,16 @@ extern "C" { #endif -/**************************************************************************** - * Name: rtl8221b_register - * - * Description: - * Register the RTL8221B PHY driver with the PHYLIB framework. Must be - * called once from board/MAC initialization, before connecting an - * RTL8221B PHY. Registration is not idempotent: each call consumes one - * entry of the fixed-size driver table (CONFIG_NET_PHYLIB_MAX_DRIVERS), - * so it must not be called repeatedly. - * - * Returned Value: - * OK on success; negated errno on failure. - * - ****************************************************************************/ +/* DSP / config verification parameter tables, copied verbatim from the + * vendor reference (RTL9000Bx_Initial_Configuration_Check). + */ -#ifdef CONFIG_NET_PHY_REALTEK_RTL8221B -int rtl8221b_register(void); -#endif +extern const uint16_t g_rtl9000b_param_check[27]; +extern const uint16_t g_rtl9000b_param_check2[5]; +extern const uint16_t g_rtl9000b_param_check3[3]; #ifdef __cplusplus } #endif -#endif /* __INCLUDE_NUTTX_NET_REALTEK_PHY_H */ +#endif /* __DRIVERS_NET_PHY_REALTEK_RTL9000B_FW_H */ diff --git a/include/nuttx/net/realtek_phy.h b/include/nuttx/net/realtek_phy.h index a4c49c81a9b..f36a4c6412d 100644 --- a/include/nuttx/net/realtek_phy.h +++ b/include/nuttx/net/realtek_phy.h @@ -57,6 +57,10 @@ extern "C" int rtl8221b_register(void); #endif +#ifdef CONFIG_NET_PHY_REALTEK_RTL9000B +int rtl9000b_register(void); +#endif + #ifdef __cplusplus } #endif
