Skip to content

Commit

Permalink
Merge branch 'net-phy-move-phy-package-code-to-its-own-source-file'
Browse files Browse the repository at this point in the history
Heiner Kallweit says:

====================
net: phy: move PHY package code to its own source file

This series contributes to cleaning up phylib by moving PHY package
related code to its own source file.
====================

Link: https://patch.msgid.link/5c5e60b3-0378-4960-8cf0-07ce0e219c68@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Jakub Kicinski committed Mar 6, 2025
2 parents 89b12ca + a400284 commit e0c8368
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 384 deletions.
3 changes: 2 additions & 1 deletion drivers/net/phy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Makefile for Linux PHY drivers

libphy-y := phy.o phy-c45.o phy-core.o phy_device.o \
linkmode.o phy_link_topology.o
linkmode.o phy_link_topology.o \
phy_package.o
mdio-bus-y += mdio_bus.o mdio_device.o

ifdef CONFIG_MDIO_DEVICE
Expand Down
1 change: 1 addition & 0 deletions drivers/net/phy/bcm54140.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <linux/module.h>
#include <linux/phy.h>

#include "phylib.h"
#include "bcm-phy-lib.h"

/* RDB per-port registers
Expand Down
7 changes: 4 additions & 3 deletions drivers/net/phy/mediatek/mtk-ge-soc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <linux/phy.h>
#include <linux/regmap.h>

#include "../phylib.h"
#include "mtk.h"

#define MTK_GPHY_ID_MT7981 0x03a29461
Expand Down Expand Up @@ -1278,7 +1279,7 @@ static int mt798x_phy_led_hw_control_set(struct phy_device *phydev, u8 index,

static bool mt7988_phy_led_get_polarity(struct phy_device *phydev, int led_num)
{
struct mtk_socphy_shared *priv = phydev->shared->priv;
struct mtk_socphy_shared *priv = phy_package_get_priv(phydev);
u32 polarities;

if (led_num == 0)
Expand Down Expand Up @@ -1317,7 +1318,7 @@ static int mt7988_phy_fix_leds_polarities(struct phy_device *phydev)
static int mt7988_phy_probe_shared(struct phy_device *phydev)
{
struct device_node *np = dev_of_node(&phydev->mdio.bus->dev);
struct mtk_socphy_shared *shared = phydev->shared->priv;
struct mtk_socphy_shared *shared = phy_package_get_priv(phydev);
struct regmap *regmap;
u32 reg;
int ret;
Expand Down Expand Up @@ -1368,7 +1369,7 @@ static int mt7988_phy_probe(struct phy_device *phydev)
return err;
}

shared = phydev->shared->priv;
shared = phy_package_get_priv(phydev);
priv = &shared->priv[phydev->mdio.addr];

phydev->priv = priv;
Expand Down
9 changes: 5 additions & 4 deletions drivers/net/phy/micrel.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <linux/net_tstamp.h>
#include <linux/gpio/consumer.h>

#include "phylib.h"

/* Operation Mode Strap Override */
#define MII_KSZPHY_OMSO 0x16
#define KSZPHY_OMSO_FACTORY_TEST BIT(15)
Expand Down Expand Up @@ -2631,8 +2633,7 @@ static void lan8814_ptp_tx_ts_get(struct phy_device *phydev,
static int lan8814_ts_info(struct mii_timestamper *mii_ts, struct kernel_ethtool_ts_info *info)
{
struct kszphy_ptp_priv *ptp_priv = container_of(mii_ts, struct kszphy_ptp_priv, mii_ts);
struct phy_device *phydev = ptp_priv->phydev;
struct lan8814_shared_priv *shared = phydev->shared->priv;
struct lan8814_shared_priv *shared = phy_package_get_priv(ptp_priv->phydev);

info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
SOF_TIMESTAMPING_RX_HARDWARE |
Expand Down Expand Up @@ -3653,7 +3654,7 @@ static int lan8814_gpio_process_cap(struct lan8814_shared_priv *shared)

static int lan8814_handle_gpio_interrupt(struct phy_device *phydev, u16 status)
{
struct lan8814_shared_priv *shared = phydev->shared->priv;
struct lan8814_shared_priv *shared = phy_package_get_priv(phydev);
int ret;

mutex_lock(&shared->shared_lock);
Expand Down Expand Up @@ -3864,7 +3865,7 @@ static void lan8814_ptp_init(struct phy_device *phydev)

static int lan8814_ptp_probe_once(struct phy_device *phydev)
{
struct lan8814_shared_priv *shared = phydev->shared->priv;
struct lan8814_shared_priv *shared = phy_package_get_priv(phydev);

/* Initialise shared lock for clock*/
mutex_init(&shared->shared_lock);
Expand Down
2 changes: 2 additions & 0 deletions drivers/net/phy/mscc/mscc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <linux/of.h>
#include <linux/netdevice.h>
#include <dt-bindings/net/mscc-phy-vsc8531.h>

#include "../phylib.h"
#include "mscc_serdes.h"
#include "mscc.h"

Expand Down
14 changes: 8 additions & 6 deletions drivers/net/phy/mscc/mscc_ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <linux/udp.h>
#include <linux/unaligned.h>

#include "../phylib.h"
#include "mscc.h"
#include "mscc_ptp.h"

Expand Down Expand Up @@ -645,11 +646,12 @@ static int __vsc85xx_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
{
struct vsc85xx_ptp *ptp = container_of(info, struct vsc85xx_ptp, caps);
struct phy_device *phydev = ptp->phydev;
struct vsc85xx_shared_private *shared =
(struct vsc85xx_shared_private *)phydev->shared->priv;
struct vsc8531_private *priv = phydev->priv;
struct vsc85xx_shared_private *shared;
u32 val;

shared = phy_package_get_priv(phydev);

val = vsc85xx_ts_read_csr(phydev, PROCESSOR, MSCC_PHY_PTP_LTC_CTRL);
val |= PTP_LTC_CTRL_SAVE_ENA;
vsc85xx_ts_write_csr(phydev, PROCESSOR, MSCC_PHY_PTP_LTC_CTRL, val);
Expand Down Expand Up @@ -696,11 +698,12 @@ static int __vsc85xx_settime(struct ptp_clock_info *info,
{
struct vsc85xx_ptp *ptp = container_of(info, struct vsc85xx_ptp, caps);
struct phy_device *phydev = ptp->phydev;
struct vsc85xx_shared_private *shared =
(struct vsc85xx_shared_private *)phydev->shared->priv;
struct vsc8531_private *priv = phydev->priv;
struct vsc85xx_shared_private *shared;
u32 val;

shared = phy_package_get_priv(phydev);

vsc85xx_ts_write_csr(phydev, PROCESSOR, MSCC_PHY_PTP_LTC_LOAD_SEC_MSB,
PTP_LTC_LOAD_SEC_MSB(ts->tv_sec));
vsc85xx_ts_write_csr(phydev, PROCESSOR, MSCC_PHY_PTP_LTC_LOAD_SEC_LSB,
Expand Down Expand Up @@ -1580,8 +1583,7 @@ int vsc8584_ptp_probe(struct phy_device *phydev)

int vsc8584_ptp_probe_once(struct phy_device *phydev)
{
struct vsc85xx_shared_private *shared =
(struct vsc85xx_shared_private *)phydev->shared->priv;
struct vsc85xx_shared_private *shared = phy_package_get_priv(phydev);

/* Initialize shared GPIO lock */
mutex_init(&shared->gpio_lock);
Expand Down
1 change: 1 addition & 0 deletions drivers/net/phy/phy-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <linux/phy.h>
#include <linux/of.h>

#include "phylib.h"
#include "phylib-internal.h"

/**
Expand Down
237 changes: 0 additions & 237 deletions drivers/net/phy/phy_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1686,243 +1686,6 @@ bool phy_driver_is_genphy_10g(struct phy_device *phydev)
}
EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);

/**
* phy_package_join - join a common PHY group
* @phydev: target phy_device struct
* @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 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 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
* allocated. If priv_size is non-zero, the given amount of bytes are
* allocated for the priv member.
*
* 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 base_addr, size_t priv_size)
{
struct mii_bus *bus = phydev->mdio.bus;
struct phy_package_shared *shared;
int ret;

if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
return -EINVAL;

mutex_lock(&bus->shared_lock);
shared = bus->shared[base_addr];
if (!shared) {
ret = -ENOMEM;
shared = kzalloc(sizeof(*shared), GFP_KERNEL);
if (!shared)
goto err_unlock;
if (priv_size) {
shared->priv = kzalloc(priv_size, GFP_KERNEL);
if (!shared->priv)
goto err_free;
shared->priv_size = priv_size;
}
shared->base_addr = base_addr;
shared->np = NULL;
refcount_set(&shared->refcnt, 1);
bus->shared[base_addr] = shared;
} else {
ret = -EINVAL;
if (priv_size && priv_size != shared->priv_size)
goto err_unlock;
refcount_inc(&shared->refcnt);
}
mutex_unlock(&bus->shared_lock);

phydev->shared = shared;

return 0;

err_free:
kfree(shared);
err_unlock:
mutex_unlock(&bus->shared_lock);
return ret;
}
EXPORT_SYMBOL_GPL(phy_package_join);

/**
* of_phy_package_join - join a common PHY group in PHY package
* @phydev: target phy_device struct
* @priv_size: if non-zero allocate this amount of bytes for private data
*
* This is a variant of phy_package_join for PHY package defined in DT.
*
* The parent node of the @phydev is checked as a valid PHY package node
* structure (by matching the node name "ethernet-phy-package") and the
* base_addr for the PHY package is passed to phy_package_join.
*
* With this configuration the shared struct will also have the np value
* filled to use additional DT defined properties in PHY specific
* probe_once and config_init_once PHY package OPs.
*
* Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
* with the same cookie but a different priv_size is an error. Or a parent
* node is not detected or is not valid or doesn't match the expected node
* name for PHY package.
*/
int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
{
struct device_node *node = phydev->mdio.dev.of_node;
struct device_node *package_node;
u32 base_addr;
int ret;

if (!node)
return -EINVAL;

package_node = of_get_parent(node);
if (!package_node)
return -EINVAL;

if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
ret = -EINVAL;
goto exit;
}

if (of_property_read_u32(package_node, "reg", &base_addr)) {
ret = -EINVAL;
goto exit;
}

ret = phy_package_join(phydev, base_addr, priv_size);
if (ret)
goto exit;

phydev->shared->np = package_node;

return 0;
exit:
of_node_put(package_node);
return ret;
}
EXPORT_SYMBOL_GPL(of_phy_package_join);

/**
* phy_package_leave - leave a common PHY group
* @phydev: target phy_device struct
*
* This leaves a PHY group created by phy_package_join(). If this phydev
* was the last user of the shared data between the group, this data is
* freed. Resets the phydev->shared pointer to NULL.
*/
void phy_package_leave(struct phy_device *phydev)
{
struct phy_package_shared *shared = phydev->shared;
struct mii_bus *bus = phydev->mdio.bus;

if (!shared)
return;

/* Decrease the node refcount on leave if present */
if (shared->np)
of_node_put(shared->np);

if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
bus->shared[shared->base_addr] = NULL;
mutex_unlock(&bus->shared_lock);
kfree(shared->priv);
kfree(shared);
}

phydev->shared = NULL;
}
EXPORT_SYMBOL_GPL(phy_package_leave);

static void devm_phy_package_leave(struct device *dev, void *res)
{
phy_package_leave(*(struct phy_device **)res);
}

/**
* devm_phy_package_join - resource managed phy_package_join()
* @dev: device that is registering this PHY package
* @phydev: target phy_device struct
* @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,
* phy_package_leave() is automatically called on driver detach. See
* phy_package_join() for more information.
*/
int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
int base_addr, size_t priv_size)
{
struct phy_device **ptr;
int ret;

ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
GFP_KERNEL);
if (!ptr)
return -ENOMEM;

ret = phy_package_join(phydev, base_addr, priv_size);

if (!ret) {
*ptr = phydev;
devres_add(dev, ptr);
} else {
devres_free(ptr);
}

return ret;
}
EXPORT_SYMBOL_GPL(devm_phy_package_join);

/**
* devm_of_phy_package_join - resource managed of_phy_package_join()
* @dev: device that is registering this PHY package
* @phydev: target phy_device struct
* @priv_size: if non-zero allocate this amount of bytes for private data
*
* Managed of_phy_package_join(). Shared storage fetched by this function,
* phy_package_leave() is automatically called on driver detach. See
* of_phy_package_join() for more information.
*/
int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
size_t priv_size)
{
struct phy_device **ptr;
int ret;

ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
GFP_KERNEL);
if (!ptr)
return -ENOMEM;

ret = of_phy_package_join(phydev, priv_size);

if (!ret) {
*ptr = phydev;
devres_add(dev, ptr);
} else {
devres_free(ptr);
}

return ret;
}
EXPORT_SYMBOL_GPL(devm_of_phy_package_join);

/**
* phy_detach - detach a PHY device from its network device
* @phydev: target phy_device struct
Expand Down
Loading

0 comments on commit e0c8368

Please sign in to comment.