From patchwork Sun Nov 26 23:51:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13468980 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GHNRa5P+" Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29E97BF; Sun, 26 Nov 2023 15:51:56 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-40b479ec4a3so787375e9.2; Sun, 26 Nov 2023 15:51:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701042714; x=1701647514; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=aqVEiehGB93HQ19d7TI9TDkVLe/UWdQF1ND0VBnLrHk=; b=GHNRa5P+3/OqwKMgXNB8PTtuZUk1bpiWWk1ZYcXtSgcVbsUWZbrMOTRd1ruW0HchCi b7Q+FzWkPYCa+HyE3xIvluDe6HuStT1HyuoICcacjUTR8ka9GhYFaEwsXIID8+1Vyji1 bGbRN5yDtoWhjMgTkbVkDC4XV0HiBYKGkMIPLSRFe5huvp2mR7RlWlcmX2kpWMpHeX0X 7YogQVQ+k79DjN5S/VBdqPMrRAYzaw+32EBYAzmB/07fSzCSOGZZ3veuITGV3GEAZJth oFprUbreg6gCKR3buIE7MD/UGQJy1/tZFGOJh+1QswoU0oq4Zl6F4cKuTSqIBxjwE3ey wDLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701042714; x=1701647514; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aqVEiehGB93HQ19d7TI9TDkVLe/UWdQF1ND0VBnLrHk=; b=FK3Wpqy/b8RTrQ0BpdUsxuT9mmF31zyFISCsHLFQgefIfEF6GDkoOMEKBzW0d+jMiX LChgUSSn+vB6u9ioge2+DhBHEAOTAITr693hVyQmISO3MLen9gnllsjdf0xJkCoVyNts jFQ02ygH8tPm76kZbtpyzBqPJNjnxLWbINB1ONaSx5UqKL5z/18mMvvVzDstMLEozFEX 5g37bZLObBzPtyMndn966GSLSLszJX81BfJXMEq7xRtYT6K+VHcWyqEcuGTrS6HuyLAj MdoKzmA45dPcMnAdvh+48fz6/r4mCeRdqL/kTO4CKP93jEvgJS+AN/myJt56Ma3QeJD9 H3cA== X-Gm-Message-State: AOJu0YyQkUUqPJUHCAN8miti3UPIDmYQ2XKdvlPu9nqWBthCWZS4rnRT 4N8zwfKa5X7zzWC5olcTeko= X-Google-Smtp-Source: AGHT+IEuERgJ3Qp9kYfoyEeW0rG5YgMuOv2R5JnryymgDer25tFHq/UBBkArqAt8O2QqeI3J8oEnnw== X-Received: by 2002:a05:600c:3110:b0:3f5:fff8:d4f3 with SMTP id g16-20020a05600c311000b003f5fff8d4f3mr7381079wmo.7.1701042714126; Sun, 26 Nov 2023 15:51:54 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id m4-20020a05600c4f4400b0040b3dbb5c93sm1202151wmq.1.2023.11.26.15.51.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 15:51:53 -0800 (PST) From: Christian Marangi To: Florian Fainelli , Broadcom internal kernel review list , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Vladimir Oltean , David Epping , "Russell King (Oracle)" , Harini Katakam , Christian Marangi , Simon Horman , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [net-next PATCH v2 1/4] net: phy: extend PHY package API to support multiple global address Date: Mon, 27 Nov 2023 00:51:38 +0100 Message-Id: <20231126235141.17996-1-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Current API for PHY package are limited to single address to configure global settings for the PHY package. It was found that some PHY package (for example the qca807x, a PHY package that is shipped with a bundle of 5 PHY) requires multiple PHY address to configure global settings. An example scenario is a PHY that have a dedicated PHY for PSGMII/serdes calibrarion and have a specific PHY in the package where the global PHY mode is set and affects every other PHY in the package. Change the API in the following way: - Change phy_package_join() to take the base addr of the PHY package instead of the global PHY addr. - Make __/phy_package_write/read() require an additional arg that select what global PHY address to use by passing the offset from the base addr passed on phy_package_join(). Each user of this API is updated to follow this new implementation following a pattern where an enum is defined to declare the offset of the addr. We also drop the check if shared is defined as any user of the phy_package_read/write is expected to use phy_package_join first. Misuse of this will correctly trigger a kernel panic for NULL pointer exception. Signed-off-by: Christian Marangi --- Changes v2: - Make kernel panic if shared is not init (bugged scenario) - Fix some confusing comments drivers/net/phy/bcm54140.c | 16 +++++++++---- drivers/net/phy/mscc/mscc.h | 5 ++++ drivers/net/phy/mscc/mscc_main.c | 4 ++-- drivers/net/phy/phy_device.c | 35 ++++++++++++++------------- include/linux/phy.h | 41 +++++++++++++++++++------------- 5 files changed, 63 insertions(+), 38 deletions(-) diff --git a/drivers/net/phy/bcm54140.c b/drivers/net/phy/bcm54140.c index d43076592f81..2eea3d09b1e6 100644 --- a/drivers/net/phy/bcm54140.c +++ b/drivers/net/phy/bcm54140.c @@ -128,6 +128,10 @@ #define BCM54140_DEFAULT_DOWNSHIFT 5 #define BCM54140_MAX_DOWNSHIFT 9 +enum bcm54140_global_phy { + BCM54140_BASE_ADDR = 0, +}; + struct bcm54140_priv { int port; int base_addr; @@ -429,11 +433,13 @@ static int bcm54140_base_read_rdb(struct phy_device *phydev, u16 rdb) int ret; phy_lock_mdio_bus(phydev); - ret = __phy_package_write(phydev, MII_BCM54XX_RDB_ADDR, rdb); + ret = __phy_package_write(phydev, BCM54140_BASE_ADDR, + MII_BCM54XX_RDB_ADDR, rdb); if (ret < 0) goto out; - ret = __phy_package_read(phydev, MII_BCM54XX_RDB_DATA); + ret = __phy_package_read(phydev, BCM54140_BASE_ADDR, + MII_BCM54XX_RDB_DATA); out: phy_unlock_mdio_bus(phydev); @@ -446,11 +452,13 @@ static int bcm54140_base_write_rdb(struct phy_device *phydev, int ret; phy_lock_mdio_bus(phydev); - ret = __phy_package_write(phydev, MII_BCM54XX_RDB_ADDR, rdb); + ret = __phy_package_write(phydev, BCM54140_BASE_ADDR, + MII_BCM54XX_RDB_ADDR, rdb); if (ret < 0) goto out; - ret = __phy_package_write(phydev, MII_BCM54XX_RDB_DATA, val); + ret = __phy_package_write(phydev, BCM54140_BASE_ADDR, + MII_BCM54XX_RDB_DATA, val); out: phy_unlock_mdio_bus(phydev); diff --git a/drivers/net/phy/mscc/mscc.h b/drivers/net/phy/mscc/mscc.h index 7a962050a4d4..6a3d8a754eb8 100644 --- a/drivers/net/phy/mscc/mscc.h +++ b/drivers/net/phy/mscc/mscc.h @@ -416,6 +416,11 @@ struct vsc8531_private { * gpio_lock: used for PHC operations. Common for all PHYs as the load/save GPIO * is shared. */ + +enum vsc85xx_global_phy { + VSC88XX_BASE_ADDR = 0, +}; + struct vsc85xx_shared_private { struct mutex gpio_lock; }; diff --git a/drivers/net/phy/mscc/mscc_main.c b/drivers/net/phy/mscc/mscc_main.c index 4171f01d34e5..6f74ce0ab1aa 100644 --- a/drivers/net/phy/mscc/mscc_main.c +++ b/drivers/net/phy/mscc/mscc_main.c @@ -711,7 +711,7 @@ int phy_base_write(struct phy_device *phydev, u32 regnum, u16 val) dump_stack(); } - return __phy_package_write(phydev, regnum, val); + return __phy_package_write(phydev, VSC88XX_BASE_ADDR, regnum, val); } /* phydev->bus->mdio_lock should be locked when using this function */ @@ -722,7 +722,7 @@ int phy_base_read(struct phy_device *phydev, u32 regnum) dump_stack(); } - return __phy_package_read(phydev, regnum); + return __phy_package_read(phydev, VSC88XX_BASE_ADDR, regnum); } u32 vsc85xx_csr_read(struct phy_device *phydev, diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 478126f6b5bc..424cbb13de13 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -1648,20 +1648,22 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); /** * phy_package_join - join a common PHY group * @phydev: target phy_device struct - * @addr: cookie and PHY address for global register access + * @base_addr: cookie and base PHY address of PHY package for offset + * calculation of global register access * @priv_size: if non-zero allocate this amount of bytes for private data * * This joins a PHY group and provides a shared storage for all phydevs in * this group. This is intended to be used for packages which contain * more than one PHY, for example a quad PHY transceiver. * - * The addr parameter serves as a cookie which has to have the same value - * for all members of one group and as a PHY address to access generic - * registers of a PHY package. Usually, one of the PHY addresses of the - * different PHYs in the package provides access to these global registers. + * The base_addr parameter serves as cookie which has to have the same values + * for all members of one group and as the base PHY address of the PHY package + * for offset calculation to access generic registers of a PHY package. + * Usually, one of the PHY addresses of the different PHYs in the package + * provides access to these global registers. * The address which is given here, will be used in the phy_package_read() - * and phy_package_write() convenience functions. If your PHY doesn't have - * global registers you can just pick any of the PHY addresses. + * and phy_package_write() convenience functions as base and added to the + * passed offset in those functions. * * This will set the shared pointer of the phydev to the shared storage. * If this is the first call for a this cookie the shared storage will be @@ -1671,17 +1673,17 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); * Returns < 1 on error, 0 on success. Esp. calling phy_package_join() * with the same cookie but a different priv_size is an error. */ -int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size) +int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size) { struct mii_bus *bus = phydev->mdio.bus; struct phy_package_shared *shared; int ret; - if (addr < 0 || addr >= PHY_MAX_ADDR) + if (base_addr < 0 || base_addr >= PHY_MAX_ADDR) return -EINVAL; mutex_lock(&bus->shared_lock); - shared = bus->shared[addr]; + shared = bus->shared[base_addr]; if (!shared) { ret = -ENOMEM; shared = kzalloc(sizeof(*shared), GFP_KERNEL); @@ -1693,9 +1695,9 @@ int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size) goto err_free; shared->priv_size = priv_size; } - shared->addr = addr; + shared->base_addr = base_addr; refcount_set(&shared->refcnt, 1); - bus->shared[addr] = shared; + bus->shared[base_addr] = shared; } else { ret = -EINVAL; if (priv_size && priv_size != shared->priv_size) @@ -1733,7 +1735,7 @@ void phy_package_leave(struct phy_device *phydev) return; if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) { - bus->shared[shared->addr] = NULL; + bus->shared[shared->base_addr] = NULL; mutex_unlock(&bus->shared_lock); kfree(shared->priv); kfree(shared); @@ -1752,7 +1754,8 @@ static void devm_phy_package_leave(struct device *dev, void *res) * devm_phy_package_join - resource managed phy_package_join() * @dev: device that is registering this PHY package * @phydev: target phy_device struct - * @addr: cookie and PHY address for global register access + * @base_addr: cookie and base PHY address of PHY package for offset + * calculation of global register access * @priv_size: if non-zero allocate this amount of bytes for private data * * Managed phy_package_join(). Shared storage fetched by this function, @@ -1760,7 +1763,7 @@ static void devm_phy_package_leave(struct device *dev, void *res) * phy_package_join() for more information. */ int devm_phy_package_join(struct device *dev, struct phy_device *phydev, - int addr, size_t priv_size) + int base_addr, size_t priv_size) { struct phy_device **ptr; int ret; @@ -1770,7 +1773,7 @@ int devm_phy_package_join(struct device *dev, struct phy_device *phydev, if (!ptr) return -ENOMEM; - ret = phy_package_join(phydev, addr, priv_size); + ret = phy_package_join(phydev, base_addr, priv_size); if (!ret) { *ptr = phydev; diff --git a/include/linux/phy.h b/include/linux/phy.h index e5f1f41e399c..51702e349d83 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -327,7 +327,8 @@ struct mdio_bus_stats { /** * struct phy_package_shared - Shared information in PHY packages - * @addr: Common PHY address used to combine PHYs in one package + * @base_addr: Base PHY address of PHY package used to combine PHYs + * in one package and for offset calculation of phy_package_read/write * @refcnt: Number of PHYs connected to this shared data * @flags: Initialization of PHY package * @priv_size: Size of the shared private data @priv @@ -338,7 +339,7 @@ struct mdio_bus_stats { * phy_package_leave(). */ struct phy_package_shared { - int addr; + int base_addr; refcount_t refcnt; unsigned long flags; size_t priv_size; @@ -1972,10 +1973,10 @@ int phy_ethtool_get_link_ksettings(struct net_device *ndev, int phy_ethtool_set_link_ksettings(struct net_device *ndev, const struct ethtool_link_ksettings *cmd); int phy_ethtool_nway_reset(struct net_device *ndev); -int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size); +int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size); void phy_package_leave(struct phy_device *phydev); int devm_phy_package_join(struct device *dev, struct phy_device *phydev, - int addr, size_t priv_size); + int base_addr, size_t priv_size); int __init mdio_bus_init(void); void mdio_bus_exit(void); @@ -1998,46 +1999,54 @@ int __phy_hwtstamp_set(struct phy_device *phydev, struct kernel_hwtstamp_config *config, struct netlink_ext_ack *extack); -static inline int phy_package_read(struct phy_device *phydev, u32 regnum) +static inline int phy_package_read(struct phy_device *phydev, + unsigned int addr_offset, u32 regnum) { struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; - if (!shared) + if (addr >= PHY_MAX_ADDR) return -EIO; - return mdiobus_read(phydev->mdio.bus, shared->addr, regnum); + return mdiobus_read(phydev->mdio.bus, addr, regnum); } -static inline int __phy_package_read(struct phy_device *phydev, u32 regnum) +static inline int __phy_package_read(struct phy_device *phydev, + unsigned int addr_offset, u32 regnum) { struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; - if (!shared) + if (addr >= PHY_MAX_ADDR) return -EIO; - return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum); + return __mdiobus_read(phydev->mdio.bus, addr, regnum); } static inline int phy_package_write(struct phy_device *phydev, - u32 regnum, u16 val) + unsigned int addr_offset, u32 regnum, + u16 val) { struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; - if (!shared) + if (addr >= PHY_MAX_ADDR) return -EIO; - return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val); + return mdiobus_write(phydev->mdio.bus, addr, regnum, val); } static inline int __phy_package_write(struct phy_device *phydev, - u32 regnum, u16 val) + unsigned int addr_offset, u32 regnum, + u16 val) { struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; - if (!shared) + if (addr >= PHY_MAX_ADDR) return -EIO; - return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val); + return __mdiobus_write(phydev->mdio.bus, addr, regnum, val); } static inline bool __phy_package_set_once(struct phy_device *phydev, From patchwork Sun Nov 26 23:51:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13468979 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Uz5+1sSV" Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31D28ED; Sun, 26 Nov 2023 15:51:57 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-4079ed65582so24907595e9.1; Sun, 26 Nov 2023 15:51:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701042715; x=1701647515; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Afqq+Hb2PFb58HdScLJ0dW+5Ky8jYx/Z5oemC8zCj40=; b=Uz5+1sSVEUO/fu6npO79SuXh5Bzgh0DGCBfRWxpiRfXuZLnyoKcaXYre0W1ebYS9KM DqgvvQccfNrbByL9OXZssuP28SAaTnODP4kg2AOPdydjB0lv82W4TkoZtkWWH28KL352 +GeI8YqDrex/+mblw84NvFqSxG+gRmfi0Bs20du2whO6x3CzEeplF8gojCCGzvOFoppn Z52//5dmqzl7VRpTf9FvctcoW9J+4RYVyw2yHQnAvUjRUxkzRJLy0f9JApRpbZuhQb0O 4J3HSLtrDdEkNciYw7Vlk7TAnWoc+UBv0XK+CWCBQx2icDbLxQTfHcHPX9scSkih/+xZ fepw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701042715; x=1701647515; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Afqq+Hb2PFb58HdScLJ0dW+5Ky8jYx/Z5oemC8zCj40=; b=Pdmp18heg4LV6nfSK6wfwfBv3taTGZ42YhyHADdwlfWRkEmNlvbrDYfmuKsJGB22h3 Yz1wURt3ZcSQoRZ/HrkKX96grv/C6LVC/Q6nmRj1x8amtmD1ordQvPUq4KUpJzrvuTP9 GWFL4dVGJVxMwcOLb+KuOzRU46NWnpCEt045sOKM0pzK9blpP7efshOInApsGUyvQnwE +s2sHW5zON3Fl9KmYzTMT67+90tyaOZxVdAisUclTzSSW29MI9sIu2E3sgpvxDos7L2D +TWduZLB1Yw2ywPU8sPPObcCp74VqWCNgRXeDvjeeIQfHdsY7yGEUH2fN+WolMJWr5zM 9u4w== X-Gm-Message-State: AOJu0Yz7kweiFtbQ+Jkfnb7n1Xv9zV2wGg/G/3Cn1jiCmYQulwuJg7lb arF93pzYojvdSTvEKK5VLBA= X-Google-Smtp-Source: AGHT+IEwRV0W3djiHwQK9Trs5S8HNyruJEnsR+oP2hEE1bFWHYJf4qPcQulSGlaFXrKgeIkLHSrtbg== X-Received: by 2002:a05:600c:78f:b0:40b:3725:4228 with SMTP id z15-20020a05600c078f00b0040b37254228mr7265523wmo.18.1701042715461; Sun, 26 Nov 2023 15:51:55 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id m4-20020a05600c4f4400b0040b3dbb5c93sm1202151wmq.1.2023.11.26.15.51.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 15:51:54 -0800 (PST) From: Christian Marangi To: Florian Fainelli , Broadcom internal kernel review list , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Vladimir Oltean , David Epping , "Russell King (Oracle)" , Harini Katakam , Christian Marangi , Simon Horman , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [net-next PATCH v2 2/4] net: phy: move mmd_phy_indirect to generic header Date: Mon, 27 Nov 2023 00:51:39 +0100 Message-Id: <20231126235141.17996-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231126235141.17996-1-ansuelsmth@gmail.com> References: <20231126235141.17996-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Move mmd_phy_indirect function from phy-core to generic phy.h to permit future usage for PHY package read/write_mmd. Signed-off-by: Christian Marangi --- drivers/net/phy/phy-core.c | 14 -------------- include/linux/phy.h | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c index 966c93cbe616..b4f80847eefd 100644 --- a/drivers/net/phy/phy-core.c +++ b/drivers/net/phy/phy-core.c @@ -526,20 +526,6 @@ int phy_speed_down_core(struct phy_device *phydev) return 0; } -static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad, - u16 regnum) -{ - /* Write the desired MMD Devad */ - __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad); - - /* Write the desired MMD register address */ - __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum); - - /* Select the Function : DATA with no post increment */ - __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, - devad | MII_MMD_CTRL_NOINCR); -} - /** * __phy_read_mmd - Convenience function for reading a register * from an MMD on a given PHY. diff --git a/include/linux/phy.h b/include/linux/phy.h index 51702e349d83..a63d45880b1a 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -1316,6 +1316,20 @@ static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum, regnum, mask, set); } +static inline void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad, + u16 regnum) +{ + /* Write the desired MMD Devad */ + __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad); + + /* Write the desired MMD register address */ + __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum); + + /* Select the Function : DATA with no post increment */ + __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, + devad | MII_MMD_CTRL_NOINCR); +} + /* * phy_read_mmd - Convenience function for reading a register * from an MMD on a given PHY. From patchwork Sun Nov 26 23:51:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13468981 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I1bWzWnr" Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5909A10F; Sun, 26 Nov 2023 15:51:58 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-40b402c36c4so8850715e9.1; Sun, 26 Nov 2023 15:51:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701042717; x=1701647517; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=zF+z7Qj3nnOkw7ulJR8RUudV7hbfK2K0WF/z5k7nkeM=; b=I1bWzWnr7/jocbOWR2V+1WftfEUK+KNTP1JoJ9b69np04b2vKxqyEuLx9RPAqmrF63 edeLeh/pV/R6xjQ3b/Mcq6NkhwkAcNu6YyZEbiiRfEAHm92RD9ul8JLy6AWEuAxoW8tM GmdQS5riR/uuhGaD7LmmN7OhEGIbzwvj5puENC7/W1RhTAMzYv87XeEPDsjanu6lmFBG x9s5T2Knb1ma1sgyyGzNyN/V9LqmS3BFlNFxVs61nP74KZh7oIMdC26crf6Gfiz4STyM BDW69dYPAMu1Mcha+CihGGd28g2fJDbDCgYles1EnBddrwnYfJItLeIzj7ip9WB5ZPVf HYRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701042717; x=1701647517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zF+z7Qj3nnOkw7ulJR8RUudV7hbfK2K0WF/z5k7nkeM=; b=SBpZ/6kOXotZDFujmuoDZi+imfOzDfZe51GJYOiyA8Q9zfBEO/s5o9UazpIma9sOiL kG66HTV7vmK5MPpj0iYyF4mfM0dA47P4E0GYPiH1/2HgrZc4dw6uXJJlmPBPwhInLtMr eIeyYSpFosDwqkoty+5DRt8duy4e8YHkCagdCj+Ne6uuwTxTeZsSXVaN5M5jaE+uMw8p zDiZScDXCnr+3OvcJ+aTrfED4C/Ps4NM7XIUsSSurYIGS9hW1daORi4DJSSyS7Zazod4 nolugpjw07sfCZqD0k/f0iWEbUnwrAUsbPKR8Lns63lla6kAjJyAVvklWr1tk2qiQGDA 7D/Q== X-Gm-Message-State: AOJu0YyL1+o0WU/0Dl0zl5Aa+I8n/RjY/RcpZW/ILotnRD1HIxJHIh5a etoJPf/PMd+ATVWZcnvrmJw= X-Google-Smtp-Source: AGHT+IFlAVY/HPlFFMbksINqw2y3ZoFPo4noy5Cxqd3oQu9Q7cwBHUniISeyQFfBXbHhnUaOsxyB9g== X-Received: by 2002:a05:600c:3b16:b0:40b:4007:11f1 with SMTP id m22-20020a05600c3b1600b0040b400711f1mr3232906wms.11.1701042716716; Sun, 26 Nov 2023 15:51:56 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id m4-20020a05600c4f4400b0040b3dbb5c93sm1202151wmq.1.2023.11.26.15.51.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 15:51:56 -0800 (PST) From: Christian Marangi To: Florian Fainelli , Broadcom internal kernel review list , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Vladimir Oltean , David Epping , "Russell King (Oracle)" , Harini Katakam , Christian Marangi , Simon Horman , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [net-next PATCH v2 3/4] net: phy: restructure __phy_write/read_mmd to helper and phydev user Date: Mon, 27 Nov 2023 00:51:40 +0100 Message-Id: <20231126235141.17996-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231126235141.17996-1-ansuelsmth@gmail.com> References: <20231126235141.17996-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Restructure phy_write_mmd and phy_read_mmd to implement generic helper for direct mdiobus access for mmd and use these helper for phydev user. This is needed in preparation of PHY package API that requires generic access to the mdiobus and are deatched from phydev struct but instead access them based on PHY package base_addr and offsets. Signed-off-by: Christian Marangi --- Changes v2: - Introduce this patch drivers/net/phy/phy-core.c | 42 ++++++++------------------------------ include/linux/phy.h | 22 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c index b4f80847eefd..65d312889f3b 100644 --- a/drivers/net/phy/phy-core.c +++ b/drivers/net/phy/phy-core.c @@ -537,26 +537,14 @@ int phy_speed_down_core(struct phy_device *phydev) */ int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) { - int val; - if (regnum > (u16)~0 || devad > 32) return -EINVAL; - if (phydev->drv && phydev->drv->read_mmd) { - val = phydev->drv->read_mmd(phydev, devad, regnum); - } else if (phydev->is_c45) { - val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr, - devad, regnum); - } else { - struct mii_bus *bus = phydev->mdio.bus; - int phy_addr = phydev->mdio.addr; - - mmd_phy_indirect(bus, phy_addr, devad, regnum); + if (phydev->drv && phydev->drv->read_mmd) + return phydev->drv->read_mmd(phydev, devad, regnum); - /* Read the content of the MMD's selected register */ - val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA); - } - return val; + return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr, + phydev->is_c45, devad, regnum); } EXPORT_SYMBOL(__phy_read_mmd); @@ -593,28 +581,14 @@ EXPORT_SYMBOL(phy_read_mmd); */ int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) { - int ret; - if (regnum > (u16)~0 || devad > 32) return -EINVAL; - if (phydev->drv && phydev->drv->write_mmd) { - ret = phydev->drv->write_mmd(phydev, devad, regnum, val); - } else if (phydev->is_c45) { - ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr, - devad, regnum, val); - } else { - struct mii_bus *bus = phydev->mdio.bus; - int phy_addr = phydev->mdio.addr; - - mmd_phy_indirect(bus, phy_addr, devad, regnum); - - /* Write the data into MMD's selected register */ - __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val); + if (phydev->drv && phydev->drv->write_mmd) + return phydev->drv->write_mmd(phydev, devad, regnum, val); - ret = 0; - } - return ret; + return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr, + phydev->is_c45, devad, regnum, val); } EXPORT_SYMBOL(__phy_write_mmd); diff --git a/include/linux/phy.h b/include/linux/phy.h index a63d45880b1a..96f6f34be051 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -1330,6 +1330,28 @@ static inline void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad devad | MII_MMD_CTRL_NOINCR); } +static inline int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45, + int devad, u32 regnum) +{ + if (is_c45) + return __mdiobus_c45_read(bus, phy_addr, devad, regnum); + + mmd_phy_indirect(bus, phy_addr, devad, regnum); + /* Read the content of the MMD's selected register */ + return __mdiobus_read(bus, phy_addr, MII_MMD_DATA); +} + +static inline int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45, + int devad, u32 regnum, u16 val) +{ + if (is_c45) + return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val); + + mmd_phy_indirect(bus, phy_addr, devad, regnum); + /* Write the data into MMD's selected register */ + return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val); +} + /* * phy_read_mmd - Convenience function for reading a register * from an MMD on a given PHY. From patchwork Sun Nov 26 23:51:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13468982 X-Patchwork-Delegate: kuba@kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aqNDAG4d" Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EFB3113; Sun, 26 Nov 2023 15:51:59 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-40b34563987so20146135e9.1; Sun, 26 Nov 2023 15:51:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701042718; x=1701647518; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=igZnYyFKrMpXiYvtb+6rWbbtYfx7FdvfY2mFz0zvQMQ=; b=aqNDAG4dR+GOEi6nxXyLmhVH+JQKjdL31f+4gCCI/I8HAE5FfJhcTljaqWBuaEts9e lCntGbQXiibkWsx/dISe8kjUsfU89QEw42HuS+2mCJdE4o+CME1xsSPoSINn3uUScnFP fmKYzB2giv3lsHUp81zlrbvpqPLfBQ013c08k0JtTj3l3Yh7Ad8gUn9oBPDW111JLaxD oUKK0xQIRFbUxKRlJND6ieVMnbCbISHd7VY6cVb4nZQXoXYWjZcrbX5BMVyYAdvUwjKc QreSiotRrpiOZUTCNIbIfzu1w7YgTafZ42aq5QD/5eBMMK37PbSXcpD8qGNUF36JWFRb 8V5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701042718; x=1701647518; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=igZnYyFKrMpXiYvtb+6rWbbtYfx7FdvfY2mFz0zvQMQ=; b=Jd2TTs10QBI/kjzUJkWieKEW/lznS4ytJ/5FlIo2TrKp4yxdtlBL5+kb81i/bUcCvU xv22rRwQYT9yj0IZdPc/tiNy8+xbRZ6X0ymWfwBNmJwibVG+VusBsf+v/ORAvo+MKFBD N8+1GtWggmadYdlwWb1gcYL7ucnOVrZVKotjJBb5CN08X9P7+pQ997dedZ+YdxQL5p2V zHKHFusqZy3IKCWaiNfYGplY4D+2IOVrovbNot1zZIPcOYLYdt9TsNS79Q8YQbrcA2/h Qq/jhSe8Y7yjFmBWOa9lyp/lcGotquXAPMlgfi5iaqfYg5eIFfurHaXwnkF5gJ7fS+p7 NacA== X-Gm-Message-State: AOJu0Yyr5emJO39qenHBwDAJaCAy0UhMaMfqcK0wVv1noDmnhUlPWsdR OS3c7ni8a3U4oxiv5FD1mEM= X-Google-Smtp-Source: AGHT+IGD6L6HK5CRxztosRedXkEPAJPvYZxo5tQWQ68zrG+TnQAE5/fkLrmeChD84JeEw7FrVx4/dg== X-Received: by 2002:a05:600c:4746:b0:405:784a:d53e with SMTP id w6-20020a05600c474600b00405784ad53emr6744788wmo.20.1701042717734; Sun, 26 Nov 2023 15:51:57 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id m4-20020a05600c4f4400b0040b3dbb5c93sm1202151wmq.1.2023.11.26.15.51.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 15:51:57 -0800 (PST) From: Christian Marangi To: Florian Fainelli , Broadcom internal kernel review list , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Vladimir Oltean , David Epping , "Russell King (Oracle)" , Harini Katakam , Christian Marangi , Simon Horman , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [net-next PATCH v2 4/4] net: phy: add support for PHY package MMD read/write Date: Mon, 27 Nov 2023 00:51:41 +0100 Message-Id: <20231126235141.17996-4-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231126235141.17996-1-ansuelsmth@gmail.com> References: <20231126235141.17996-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package. Add support for these additional required function in both lock and no lock variant. It's possible to set is_c45 bool for phy_package_read/write to true to access mmd regs for accessing C45 PHY in PHY package for global configuration. Signed-off-by: Christian Marangi --- Changes v2: - Rework to use newly introduced helper - Add common check for regnum and devad include/linux/phy.h | 78 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/include/linux/phy.h b/include/linux/phy.h index 96f6f34be051..3e507bd2c3b4 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -2085,6 +2085,84 @@ static inline int __phy_package_write(struct phy_device *phydev, return __mdiobus_write(phydev->mdio.bus, addr, regnum, val); } +static inline int phy_package_read_mmd(struct phy_device *phydev, + unsigned int addr_offset, bool is_c45, + int devad, u32 regnum) +{ + struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; + int val; + + if (addr >= PHY_MAX_ADDR) + return -EIO; + + if (regnum > (u16)~0 || devad > 32) + return -EINVAL; + + phy_lock_mdio_bus(phydev); + val = mmd_phy_read(phydev->mdio.bus, addr, is_c45, devad, + regnum); + phy_unlock_mdio_bus(phydev); + + return val; +} + +static inline int __phy_package_read_mmd(struct phy_device *phydev, + unsigned int addr_offset, bool is_c45, + int devad, u32 regnum) +{ + struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; + + if (addr >= PHY_MAX_ADDR) + return -EIO; + + if (regnum > (u16)~0 || devad > 32) + return -EINVAL; + + return mmd_phy_read(phydev->mdio.bus, addr, is_c45, devad, + regnum); +} + +static inline int phy_package_write_mmd(struct phy_device *phydev, + unsigned int addr_offset, bool is_c45, + int devad, u32 regnum, u16 val) +{ + struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; + int ret; + + if (addr >= PHY_MAX_ADDR) + return -EIO; + + if (regnum > (u16)~0 || devad > 32) + return -EINVAL; + + phy_lock_mdio_bus(phydev); + ret = mmd_phy_write(phydev->mdio.bus, addr, is_c45, devad, + regnum, val); + phy_unlock_mdio_bus(phydev); + + return ret; +} + +static inline int __phy_package_write_mmd(struct phy_device *phydev, + unsigned int addr_offset, bool is_c45, + int devad, u32 regnum, u16 val) +{ + struct phy_package_shared *shared = phydev->shared; + int addr = shared->base_addr + addr_offset; + + if (addr >= PHY_MAX_ADDR) + return -EIO; + + if (regnum > (u16)~0 || devad > 32) + return -EINVAL; + + return mmd_phy_write(phydev->mdio.bus, addr, is_c45, devad, + regnum, val); +} + static inline bool __phy_package_set_once(struct phy_device *phydev, unsigned int b) {