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 194570bd35f83e20a1fff86539eea4c8534d15ea
Author: zhangkai25 <[email protected]>
AuthorDate: Thu Jul 23 16:14:09 2026 +0800

    net/phy: 100BASE-T1 modes and paged register access
    
    Extend the PHYLIB framework with capabilities needed by paged, T1-class
    PHYs (e.g. Realtek RTL9000B) while staying backward compatible:
    
    - PHY_SUPPORTED_100BASET1_FULL / _1000BASET1_FULL link-mode bits for
      automotive single-pair Ethernet PHYs.
    
    - Paged register access: phy_read_paged / phy_write_paged /
      phy_modify_paged, built on phy_select_page / phy_restore_page, holding
      the MDIO bus lock across "select page -> access -> restore page" so it
      cannot interleave with MII ioctl.
    
    - MDIO bus primitives backing the above: mdio_lock / mdio_unlock and the
      lockless mdio_read_nolock / mdio_write_nolock.
    
    All additions are opt-in and compile out with CONFIG_NET_PHYLIB=n.
    
    Signed-off-by: zhangkai25 <[email protected]>
---
 drivers/net/mdio.c           | 104 ++++++++++++++++
 drivers/net/phy/phy.h        |  45 +++++++
 drivers/net/phy/phy_access.c | 289 +++++++++++++++++++++++++++++++++++++++++++
 include/nuttx/net/mdio.h     |  48 +++++++
 4 files changed, 486 insertions(+)

diff --git a/drivers/net/mdio.c b/drivers/net/mdio.c
index b7f1c507e7f..8af09249f87 100644
--- a/drivers/net/mdio.c
+++ b/drivers/net/mdio.c
@@ -122,6 +122,110 @@ int mdio_unregister(FAR struct mdio_bus_s *dev)
   return 0;
 }
 
+/****************************************************************************
+ * Name: mdio_lock
+ *
+ * Description:
+ *   Acquire the MDIO bus mutex explicitly.  Used by callers that need to
+ *   perform a multi-transaction atomic sequence on the bus (e.g. paged
+ *   register access: select page -> read/write -> restore page) that must
+ *   not be interleaved with other bus users such as MII ioctl.
+ *
+ *   Between mdio_lock() and mdio_unlock() the caller must use the lockless
+ *   primitives mdio_read_nolock() / mdio_write_nolock(); calling the locking
+ *   mdio_read()/mdio_write() while holding the lock would deadlock.
+ *
+ * Input Parameters:
+ *   dev - The MDIO bus handle.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int mdio_lock(FAR struct mdio_bus_s *dev)
+{
+  DEBUGASSERT(dev != NULL);
+  return nxmutex_lock(&dev->lock);
+}
+
+/****************************************************************************
+ * Name: mdio_unlock
+ *
+ * Description:
+ *   Release the MDIO bus mutex previously acquired with mdio_lock().  Must
+ *   balance a successful mdio_lock().
+ *
+ * Input Parameters:
+ *   dev - The MDIO bus handle.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+void mdio_unlock(FAR struct mdio_bus_s *dev)
+{
+  DEBUGASSERT(dev != NULL);
+  nxmutex_unlock(&dev->lock);
+}
+
+/****************************************************************************
+ * Name: mdio_read_nolock
+ *
+ * Description:
+ *   Clause 22 read WITHOUT taking the bus mutex.  The caller must already
+ *   hold the bus lock via mdio_lock().  Intended for composing atomic
+ *   multi-transaction sequences (see mdio_lock()).
+ *
+ * Input Parameters:
+ *   dev     - The MDIO bus handle.
+ *   phyaddr - The PHY address (0-31).
+ *   regaddr - The PHY register address (0-31).
+ *   value   - Pointer to store the value read.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int mdio_read_nolock(FAR struct mdio_bus_s *dev, uint8_t phyaddr,
+                     uint8_t regaddr, FAR uint16_t *value)
+{
+  DEBUGASSERT(dev != NULL && dev->lower != NULL);
+  DEBUGASSERT(dev->lower->ops->read != NULL);
+
+  return MDIO_READ(dev, phyaddr, regaddr, value);
+}
+
+/****************************************************************************
+ * Name: mdio_write_nolock
+ *
+ * Description:
+ *   Clause 22 write WITHOUT taking the bus mutex.  The caller must already
+ *   hold the bus lock via mdio_lock().  Intended for composing atomic
+ *   multi-transaction sequences (see mdio_lock()).
+ *
+ * Input Parameters:
+ *   dev     - The MDIO bus handle.
+ *   phyaddr - The PHY address (0-31).
+ *   regaddr - The PHY register address (0-31).
+ *   value   - The value to write.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int mdio_write_nolock(FAR struct mdio_bus_s *dev, uint8_t phyaddr,
+                      uint8_t regaddr, uint16_t value)
+{
+  DEBUGASSERT(dev != NULL && dev->lower != NULL);
+  DEBUGASSERT(dev->lower->ops->write != NULL);
+
+  return MDIO_WRITE(dev, phyaddr, regaddr, value);
+}
+
 /****************************************************************************
  * Name: mdio_read
  *
diff --git a/drivers/net/phy/phy.h b/drivers/net/phy/phy.h
index a7cfa242cd5..4675f1e46bf 100644
--- a/drivers/net/phy/phy.h
+++ b/drivers/net/phy/phy.h
@@ -97,6 +97,29 @@ struct phy_driver_s
 
   CODE int (*config_intr)(FAR struct phy_device_s *phydev, bool enable);
   CODE int (*handle_interrupt)(FAR struct phy_device_s *phydev);
+
+  /* Paged register access (optional).
+   *
+   * Some PHYs place most registers behind a vendor-specific page-select
+   * register, and the register number / switching mechanism differs per
+   * vendor (e.g. Realtek reg 0x1f, Marvell reg 22, Realtek extended pages
+   * needing a two-step 0x1e/0x1f write).  These callbacks let the phylib
+   * core switch pages without hardcoding any vendor detail:
+   *
+   * read_page  - Return the currently selected page (>= 0), or a negated
+   *              errno on failure.
+   * write_page - Select 'page'.  Return OK, or a negated errno on failure.
+   *
+   * Both are invoked by the phylib core with the MDIO bus lock already held
+   * (see phy_select_page()), so the driver MUST use the lockless bus
+   * primitives (mdio_read_nolock() / mdio_write_nolock()) inside them.
+   *
+   * Both NULL => the PHY has no pages; the phy_*_paged() helpers return
+   * -ENOTSUP.
+   */
+
+  CODE int (*read_page)(FAR struct phy_device_s *phydev);
+  CODE int (*write_page)(FAR struct phy_device_s *phydev, int page);
 };
 
 /****************************************************************************
@@ -150,6 +173,28 @@ int phy_modify_mmd(FAR struct phy_device_s *phydev,
                    uint16_t mask, uint16_t set);
 #endif
 
+/* Paged register access (phy_core.c).
+ *
+ * Some PHYs place most registers behind a page-select register.  Accessing
+ * them requires "select page -> read/write target -> restore page", which
+ * must be atomic against other bus users (MII ioctl).  These helpers hold
+ * the MDIO bus lock across the whole sequence.
+ *
+ * The page-select register and switching mechanism are vendor specific and
+ * are never hardcoded in the core: page switching is delegated to the PHY
+ * driver's read_page/write_page callbacks (see struct phy_driver_s).  A PHY
+ * driver that leaves both callbacks NULL has no pages, and these helpers
+ * return -ENOTSUP.
+ */
+
+int phy_read_paged(FAR struct phy_device_s *phydev,
+                   int page, uint8_t regaddr, FAR uint16_t *value);
+int phy_write_paged(FAR struct phy_device_s *phydev,
+                    int page, uint8_t regaddr, uint16_t value);
+int phy_modify_paged(FAR struct phy_device_s *phydev,
+                     int page, uint8_t regaddr,
+                     uint16_t mask, uint16_t set);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/drivers/net/phy/phy_access.c b/drivers/net/phy/phy_access.c
index 72d5806d86a..d78d129337f 100644
--- a/drivers/net/phy/phy_access.c
+++ b/drivers/net/phy/phy_access.c
@@ -34,6 +34,179 @@
 
 #include "phy.h"
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: phy_read_page_nolock
+ *
+ * Description:
+ *   Read the currently selected page via the vendor driver's read_page
+ *   callback.  Lockless: the caller must already hold the MDIO bus lock.
+ *
+ * Input Parameters:
+ *   phydev - Pointer to the PHY device structure
+ *
+ * Returned Value:
+ *   The current page (>= 0) on success; -ENOTSUP if the driver has no
+ *   read_page callback; a negated errno on failure.
+ *
+ ****************************************************************************/
+
+static inline int phy_read_page_nolock(FAR struct phy_device_s *phydev)
+{
+  if (phydev->drv->read_page == NULL)
+    {
+      return -ENOTSUP;
+    }
+
+  return phydev->drv->read_page(phydev);
+}
+
+/****************************************************************************
+ * Name: phy_write_page_nolock
+ *
+ * Description:
+ *   Select 'page' via the vendor driver's write_page callback.  Lockless:
+ *   the caller must already hold the MDIO bus lock.
+ *
+ * Input Parameters:
+ *   phydev - Pointer to the PHY device structure
+ *   page   - Page number to select
+ *
+ * Returned Value:
+ *   OK on success; -ENOTSUP if the driver has no write_page callback; a
+ *   negated errno on failure.
+ *
+ ****************************************************************************/
+
+static inline int phy_write_page_nolock(FAR struct phy_device_s *phydev,
+                                        int page)
+{
+  if (phydev->drv->write_page == NULL)
+    {
+      return -ENOTSUP;
+    }
+
+  return phydev->drv->write_page(phydev, page);
+}
+
+/****************************************************************************
+ * Name: phy_select_page
+ *
+ * Description:
+ *   Take the MDIO bus lock, save the current page, and select the requested
+ *   page.  On success the bus lock is held and the previous page (>= 0) is
+ *   returned; the caller must release it via phy_restore_page().
+ *
+ *   On failure a negated errno is returned and the bus lock is NOT held:
+ *   if acquiring the lock fails there is nothing to release, and if reading
+ *   or switching the page fails this function releases the lock itself.
+ *   The caller must therefore only call phy_restore_page() when
+ *   phy_select_page() succeeds (returns >= 0).
+ *
+ *   Between phy_select_page() and phy_restore_page() the caller uses the
+ *   lockless mdio_*_nolock() primitives to access registers on the selected
+ *   page.
+ *
+ *   The page-select register and switching mechanism are vendor specific,
+ *   so the actual page read/write is delegated to the driver's
+ *   read_page/write_page callbacks; the core never hardcodes a page
+ *   register.
+ *
+ * Input Parameters:
+ *   phydev - Pointer to the PHY device structure
+ *   page   - Page number to select
+ *
+ * Returned Value:
+ *   The previous page number (>= 0, lock held) on success, or a negated
+ *   errno (lock released) on failure.
+ *
+ ****************************************************************************/
+
+static int phy_select_page(FAR struct phy_device_s *phydev, int page)
+{
+  int oldpage;
+  int ret;
+
+  ret = mdio_lock(phydev->bus);
+  if (ret < 0)
+    {
+      /* Lock not acquired: nothing to release, and the caller must not
+       * call phy_restore_page().
+       */
+
+      return ret;
+    }
+
+  oldpage = phy_read_page_nolock(phydev);
+  if (oldpage < 0)
+    {
+      ret = oldpage;
+      goto err_unlock;
+    }
+
+  if (oldpage != page)
+    {
+      ret = phy_write_page_nolock(phydev, page);
+      if (ret < 0)
+        {
+          goto err_unlock;
+        }
+    }
+
+  return oldpage;
+
+err_unlock:
+
+  /* The page could not be read or switched: drop the lock we took so the
+   * caller can simply return the error without calling phy_restore_page().
+   */
+
+  mdio_unlock(phydev->bus);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: phy_restore_page
+ *
+ * Description:
+ *   Restore the page saved by phy_select_page() and release the MDIO bus
+ *   lock.  Must only be called after a successful phy_select_page() (i.e.
+ *   with the bus lock held); the page-selection failure paths release the
+ *   lock themselves and must not reach here.
+ *
+ * Input Parameters:
+ *   phydev  - Pointer to the PHY device structure
+ *   oldpage - Page returned by a successful phy_select_page() (>= 0)
+ *   ret     - Result of the operation performed on the selected page
+ *
+ * Returned Value:
+ *   ret if it was negative; otherwise the page-restore error if that
+ *   failed; otherwise ret.
+ *
+ ****************************************************************************/
+
+static int phy_restore_page(FAR struct phy_device_s *phydev,
+                            int oldpage, int ret)
+{
+  int ret2;
+
+  /* Restore the saved page.  Propagate the restore error only if the
+   * operation itself succeeded.
+   */
+
+  ret2 = phy_write_page_nolock(phydev, oldpage);
+  if (ret >= 0 && ret2 < 0)
+    {
+      ret = ret2;
+    }
+
+  mdio_unlock(phydev->bus);
+  return ret;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -267,3 +440,119 @@ int phy_modify_mmd(FAR struct phy_device_s *phydev,
 }
 
 #endif /* CONFIG_MDIO_CLAUSE_45 */
+
+/****************************************************************************
+ * Name: phy_read_paged
+ *
+ * Description:
+ *   Read a register on a non-default PHY page.  The select-page ->
+ *   read -> restore-page sequence runs atomically under the MDIO bus lock,
+ *   so it cannot interleave with other bus users (e.g. MII ioctl).
+ *
+ * Input Parameters:
+ *   phydev  - Pointer to the PHY device structure
+ *   page    - Page number to select (via the driver's write_page callback)
+ *   regaddr - MII register address to read within that page
+ *   value   - Location to store the 16-bit value read
+ *
+ * Returned Value:
+ *   OK on success; negated errno on failure.
+ *
+ ****************************************************************************/
+
+int phy_read_paged(FAR struct phy_device_s *phydev,
+                   int page, uint8_t regaddr, FAR uint16_t *value)
+{
+  int oldpage;
+  int ret;
+
+  oldpage = phy_select_page(phydev, page);
+  if (oldpage < 0)
+    {
+      return oldpage;
+    }
+
+  ret = mdio_read_nolock(phydev->bus, phydev->mdio_addr, regaddr, value);
+
+  return phy_restore_page(phydev, oldpage, ret);
+}
+
+/****************************************************************************
+ * Name: phy_write_paged
+ *
+ * Description:
+ *   Write a register on a non-default PHY page.  Atomic against other bus
+ *   users (see phy_read_paged).
+ *
+ * Input Parameters:
+ *   phydev  - Pointer to the PHY device structure
+ *   page    - Page number to select (via the driver's write_page callback)
+ *   regaddr - MII register address to write within that page
+ *   value   - 16-bit value to write
+ *
+ * Returned Value:
+ *   OK on success; negated errno on failure.
+ *
+ ****************************************************************************/
+
+int phy_write_paged(FAR struct phy_device_s *phydev,
+                    int page, uint8_t regaddr, uint16_t value)
+{
+  int oldpage;
+  int ret;
+
+  oldpage = phy_select_page(phydev, page);
+  if (oldpage < 0)
+    {
+      return oldpage;
+    }
+
+  ret = mdio_write_nolock(phydev->bus, phydev->mdio_addr, regaddr, value);
+
+  return phy_restore_page(phydev, oldpage, ret);
+}
+
+/****************************************************************************
+ * Name: phy_modify_paged
+ *
+ * Description:
+ *   Read-modify-write a register on a non-default PHY page, atomically
+ *   under the MDIO bus lock.
+ *
+ * Input Parameters:
+ *   phydev  - Pointer to the PHY device structure
+ *   page    - Page number to select (via the driver's write_page callback)
+ *   regaddr - MII register address to modify within that page
+ *   mask    - Bitmask of bits to clear
+ *   set     - Bitmask of bits to set (applied after mask)
+ *
+ * Returned Value:
+ *   OK on success; negated errno on failure.
+ *
+ ****************************************************************************/
+
+int phy_modify_paged(FAR struct phy_device_s *phydev,
+                     int page, uint8_t regaddr,
+                     uint16_t mask, uint16_t set)
+{
+  uint16_t value;
+  int oldpage;
+  int ret;
+
+  oldpage = phy_select_page(phydev, page);
+  if (oldpage < 0)
+    {
+      return oldpage;
+    }
+
+  ret = mdio_read_nolock(phydev->bus, phydev->mdio_addr, regaddr, &value);
+  if (ret >= 0)
+    {
+      value = (value & ~mask) | (set & mask);
+      ret = mdio_write_nolock(phydev->bus, phydev->mdio_addr, regaddr,
+                              value);
+    }
+
+  return phy_restore_page(phydev, oldpage, ret);
+}
+
diff --git a/include/nuttx/net/mdio.h b/include/nuttx/net/mdio.h
index 71c20dc58ba..6d862cff754 100644
--- a/include/nuttx/net/mdio.h
+++ b/include/nuttx/net/mdio.h
@@ -189,6 +189,54 @@ int mdio_read(FAR struct mdio_bus_s *dev, uint8_t phyaddr, 
uint8_t regaddr,
 int mdio_write(FAR struct mdio_bus_s *dev, uint8_t phyaddr, uint8_t regaddr,
                uint16_t value);
 
+/****************************************************************************
+ * Name: mdio_lock
+ *
+ * Description:
+ *   Acquire the MDIO bus mutex explicitly, so a caller can perform an atomic
+ *   multi-transaction sequence on the bus (e.g. paged register access) using
+ *   the lockless primitives below.  Must be balanced with mdio_unlock().
+ *
+ ****************************************************************************/
+
+int mdio_lock(FAR struct mdio_bus_s *dev);
+
+/****************************************************************************
+ * Name: mdio_unlock
+ *
+ * Description:
+ *   Release the MDIO bus mutex previously acquired with mdio_lock().
+ *
+ ****************************************************************************/
+
+void mdio_unlock(FAR struct mdio_bus_s *dev);
+
+/****************************************************************************
+ * Name: mdio_read_nolock
+ *
+ * Description:
+ *   Clause 22 read WITHOUT taking the bus mutex.  The caller must already
+ *   hold the bus lock via mdio_lock().  Returns zero (OK) on success; a
+ *   negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int mdio_read_nolock(FAR struct mdio_bus_s *dev, uint8_t phyaddr,
+                     uint8_t regaddr, FAR uint16_t *value);
+
+/****************************************************************************
+ * Name: mdio_write_nolock
+ *
+ * Description:
+ *   Clause 22 write WITHOUT taking the bus mutex.  The caller must already
+ *   hold the bus lock via mdio_lock().  Returns zero (OK) on success; a
+ *   negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int mdio_write_nolock(FAR struct mdio_bus_s *dev, uint8_t phyaddr,
+                      uint8_t regaddr, uint16_t value);
+
 /****************************************************************************
  * Name: mdio_c45_read / mdio_c45_write
  *

Reply via email to