Skip to content

Commit

Permalink
net: introduce helpers to get PHY interface mode from a device/ofnode
Browse files Browse the repository at this point in the history
Add helpers ofnode_read_phy_mode() and dev_read_phy_mode() to parse the
"phy-mode" / "phy-connection-type" property. Add corresponding UT test.

Use them treewide.

This allows us to inline the phy_get_interface_by_name() into
ofnode_read_phy_mode(), since the former is not used anymore.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Ramon Fried <rfried.dev@gmail.com>
Tested-by: Patrice Chotard <patrice.chotard@foss.st.com>
  • Loading branch information
elkablo authored and rfried-nrl committed Apr 10, 2022
1 parent 9c06b48 commit 123ca11
Show file tree
Hide file tree
Showing 41 changed files with 166 additions and 372 deletions.
1 change: 1 addition & 0 deletions arch/sandbox/dts/test.dts
Expand Up @@ -535,6 +535,7 @@
reg = <0x10007000 0x1000>;
fake-host-hwaddr = [00 00 66 44 22 77];
phy-handle = <&ethphy1>;
phy-mode = "2500base-x";
};

dsa_eth0: dsa-test-eth {
Expand Down
13 changes: 5 additions & 8 deletions board/st/stm32f746-disco/stm32f746-disco.c
Expand Up @@ -117,24 +117,21 @@ int board_late_init(void)
int board_init(void)
{
#ifdef CONFIG_ETH_DESIGNWARE
const char *phy_mode;
int node;
ofnode node;

node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "st,stm32-dwmac");
if (node < 0)
node = ofnode_by_compatible(ofnode_null(), "st,stm32-dwmac");
if (!ofnode_valid(node))
return -1;

phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);

switch (phy_get_interface_by_name(phy_mode)) {
switch (ofnode_read_phy_mode(node)) {
case PHY_INTERFACE_MODE_RMII:
STM32_SYSCFG->pmc |= SYSCFG_PMC_MII_RMII_SEL;
break;
case PHY_INTERFACE_MODE_MII:
STM32_SYSCFG->pmc &= ~SYSCFG_PMC_MII_RMII_SEL;
break;
default:
printf("PHY interface %s not supported !\n", phy_mode);
printf("Unsupported PHY interface!\n");
}
#endif

Expand Down
23 changes: 23 additions & 0 deletions drivers/core/ofnode.c
Expand Up @@ -1219,3 +1219,26 @@ ofnode ofnode_get_phy_node(ofnode node)

return args.node;
}

phy_interface_t ofnode_read_phy_mode(ofnode node)
{
const char *mode;
int i;

assert(ofnode_valid(node));

mode = ofnode_read_string(node, "phy-mode");
if (!mode)
mode = ofnode_read_string(node, "phy-connection-type");

if (!mode)
return PHY_INTERFACE_MODE_NONE;

for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++)
if (!strcmp(mode, phy_interface_strings[i]))
return i;

debug("%s: Invalid PHY interface '%s'\n", __func__, mode);

return PHY_INTERFACE_MODE_NONE;
}
5 changes: 5 additions & 0 deletions drivers/core/read.c
Expand Up @@ -403,3 +403,8 @@ ofnode dev_get_phy_node(const struct udevice *dev)
{
return ofnode_get_phy_node(dev_ofnode(dev));
}

phy_interface_t dev_read_phy_mode(const struct udevice *dev)
{
return ofnode_read_phy_mode(dev_ofnode(dev));
}
9 changes: 2 additions & 7 deletions drivers/net/ag7xxx.c
Expand Up @@ -1254,7 +1254,6 @@ static const struct eth_ops ag7xxx_eth_ops = {
static int ag7xxx_eth_of_to_plat(struct udevice *dev)
{
struct eth_pdata *pdata = dev_get_plat(dev);
const char *phy_mode;
int ret;

pdata->iobase = dev_read_addr(dev);
Expand All @@ -1265,13 +1264,9 @@ static int ag7xxx_eth_of_to_plat(struct udevice *dev)
if (ret <= 0)
return ret;

phy_mode = fdt_getprop(gd->fdt_blob, ret, "phy-mode", NULL);
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
if (pdata->phy_interface == -1) {
debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -EINVAL;
}

return 0;
}
Expand Down
13 changes: 3 additions & 10 deletions drivers/net/altera_tse.c
Expand Up @@ -676,17 +676,10 @@ static int altera_tse_probe(struct udevice *dev)
static int altera_tse_of_to_plat(struct udevice *dev)
{
struct eth_pdata *pdata = dev_get_plat(dev);
const char *phy_mode;

pdata->phy_interface = -1;
phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
NULL);
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
if (pdata->phy_interface == -1) {
debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);

pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -EINVAL;
}

return 0;
}
Expand Down
6 changes: 1 addition & 5 deletions drivers/net/bcm6348-eth.c
Expand Up @@ -415,7 +415,6 @@ static int bcm6348_eth_probe(struct udevice *dev)
struct eth_pdata *pdata = dev_get_plat(dev);
struct bcm6348_eth_priv *priv = dev_get_priv(dev);
struct ofnode_phandle_args phy;
const char *phy_mode;
int ret, i;

/* get base address */
Expand All @@ -425,10 +424,7 @@ static int bcm6348_eth_probe(struct udevice *dev)
pdata->iobase = (phys_addr_t) priv->base;

/* get phy mode */
pdata->phy_interface = PHY_INTERFACE_MODE_NONE;
phy_mode = dev_read_string(dev, "phy-mode");
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -ENODEV;

Expand Down
10 changes: 2 additions & 8 deletions drivers/net/bcmgenet.c
Expand Up @@ -690,20 +690,14 @@ static int bcmgenet_eth_of_to_plat(struct udevice *dev)
struct eth_pdata *pdata = dev_get_plat(dev);
struct bcmgenet_eth_priv *priv = dev_get_priv(dev);
struct ofnode_phandle_args phy_node;
const char *phy_mode;
int ret;

pdata->iobase = dev_read_addr(dev);

/* Get phy mode from DT */
pdata->phy_interface = -1;
phy_mode = dev_read_string(dev, "phy-mode");
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
if (pdata->phy_interface == -1) {
debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -EINVAL;
}

ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
&phy_node);
Expand Down
10 changes: 2 additions & 8 deletions drivers/net/designware.c
Expand Up @@ -914,21 +914,15 @@ int designware_eth_of_to_plat(struct udevice *dev)
struct dw_eth_dev *priv = dev_get_priv(dev);
#endif
struct eth_pdata *pdata = &dw_pdata->eth_pdata;
const char *phy_mode;
#if CONFIG_IS_ENABLED(DM_GPIO)
int reset_flags = GPIOD_IS_OUT;
#endif
int ret = 0;

pdata->iobase = dev_read_addr(dev);
pdata->phy_interface = -1;
phy_mode = dev_read_string(dev, "phy-mode");
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
if (pdata->phy_interface == -1) {
debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -EINVAL;
}

pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);

Expand Down
36 changes: 4 additions & 32 deletions drivers/net/dwc_eth_qos.c
Expand Up @@ -270,7 +270,7 @@ struct eqos_config {
int config_mac;
int config_mac_mdio;
unsigned int axi_bus_width;
phy_interface_t (*interface)(struct udevice *dev);
phy_interface_t (*interface)(const struct udevice *dev);
struct eqos_ops *ops;
};

Expand Down Expand Up @@ -1729,21 +1729,7 @@ static int eqos_probe_resources_stm32(struct udevice *dev)
return ret;
}

static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
{
const char *phy_mode;
phy_interface_t interface = PHY_INTERFACE_MODE_NONE;

debug("%s(dev=%p):\n", __func__, dev);

phy_mode = dev_read_prop(dev, "phy-mode", NULL);
if (phy_mode)
interface = phy_get_interface_by_name(phy_mode);

return interface;
}

static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
static phy_interface_t eqos_get_interface_tegra186(const struct udevice *dev)
{
return PHY_INTERFACE_MODE_MII;
}
Expand All @@ -1766,20 +1752,6 @@ static int eqos_probe_resources_imx(struct udevice *dev)
return 0;
}

static phy_interface_t eqos_get_interface_imx(struct udevice *dev)
{
const char *phy_mode;
phy_interface_t interface = PHY_INTERFACE_MODE_NONE;

debug("%s(dev=%p):\n", __func__, dev);

phy_mode = dev_read_prop(dev, "phy-mode", NULL);
if (phy_mode)
interface = phy_get_interface_by_name(phy_mode);

return interface;
}

static int eqos_remove_resources_tegra186(struct udevice *dev)
{
struct eqos_priv *eqos = dev_get_priv(dev);
Expand Down Expand Up @@ -1985,7 +1957,7 @@ static const struct eqos_config __maybe_unused eqos_stm32_config = {
.config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
.config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
.axi_bus_width = EQOS_AXI_WIDTH_64,
.interface = eqos_get_interface_stm32,
.interface = dev_read_phy_mode,
.ops = &eqos_stm32_ops
};

Expand Down Expand Up @@ -2013,7 +1985,7 @@ struct eqos_config __maybe_unused eqos_imx_config = {
.config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
.config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
.axi_bus_width = EQOS_AXI_WIDTH_64,
.interface = eqos_get_interface_imx,
.interface = dev_read_phy_mode,
.ops = &eqos_imx_ops
};

Expand Down
11 changes: 2 additions & 9 deletions drivers/net/fec_mxc.c
Expand Up @@ -1310,20 +1310,13 @@ static int fecmxc_of_to_plat(struct udevice *dev)
int ret = 0;
struct eth_pdata *pdata = dev_get_plat(dev);
struct fec_priv *priv = dev_get_priv(dev);
const char *phy_mode;

pdata->iobase = dev_read_addr(dev);
priv->eth = (struct ethernet_regs *)pdata->iobase;

pdata->phy_interface = -1;
phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
NULL);
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
if (pdata->phy_interface == -1) {
debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -EINVAL;
}

#ifdef CONFIG_DM_REGULATOR
device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
Expand Down
13 changes: 1 addition & 12 deletions drivers/net/fm/eth.c
Expand Up @@ -954,17 +954,6 @@ int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
return 0;
}
#else /* CONFIG_DM_ETH */
#ifdef CONFIG_PHYLIB
phy_interface_t fman_read_sys_if(struct udevice *dev)
{
const char *if_str;

if_str = ofnode_read_string(dev_ofnode(dev), "phy-connection-type");
debug("MAC system interface mode %s\n", if_str);

return phy_get_interface_by_name(if_str);
}
#endif

static int fm_eth_bind(struct udevice *dev)
{
Expand Down Expand Up @@ -1038,7 +1027,7 @@ static int fm_eth_probe(struct udevice *dev)
reg = (void *)(uintptr_t)dev_read_addr(dev);
fm_eth->mac_type = dev_get_driver_data(dev);
#ifdef CONFIG_PHYLIB
fm_eth->enet_if = fman_read_sys_if(dev);
fm_eth->enet_if = dev_read_phy_mode(dev);
#else
fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII;
printf("%s: warning - unable to determine interface type\n", __func__);
Expand Down
13 changes: 4 additions & 9 deletions drivers/net/fsl_enetc.c
Expand Up @@ -260,9 +260,6 @@ static int enetc_init_sxgmii(struct udevice *dev)
static void enetc_start_pcs(struct udevice *dev)
{
struct enetc_priv *priv = dev_get_priv(dev);
const char *if_str;

priv->if_type = PHY_INTERFACE_MODE_NONE;

/* register internal MDIO for debug purposes */
if (enetc_read_port(priv, ENETC_PCAPR0) & ENETC_PCAPRO_MDIO) {
Expand All @@ -279,14 +276,12 @@ static void enetc_start_pcs(struct udevice *dev)
return;
}

if_str = ofnode_read_string(dev_ofnode(dev), "phy-mode");
if (if_str)
priv->if_type = phy_get_interface_by_name(if_str);
else
priv->if_type = dev_read_phy_mode(dev);
if (priv->if_type == PHY_INTERFACE_MODE_NONE) {
enetc_dbg(dev,
"phy-mode property not found, defaulting to SGMII\n");
if (priv->if_type < 0)
priv->if_type = PHY_INTERFACE_MODE_NONE;
priv->if_type = PHY_INTERFACE_MODE_SGMII;
}

switch (priv->if_type) {
case PHY_INTERFACE_MODE_SGMII:
Expand Down
11 changes: 3 additions & 8 deletions drivers/net/ftgmac100.c
Expand Up @@ -549,17 +549,12 @@ static int ftgmac100_of_to_plat(struct udevice *dev)
{
struct eth_pdata *pdata = dev_get_plat(dev);
struct ftgmac100_data *priv = dev_get_priv(dev);
const char *phy_mode;

pdata->iobase = dev_read_addr(dev);
pdata->phy_interface = -1;
phy_mode = dev_read_string(dev, "phy-mode");
if (phy_mode)
pdata->phy_interface = phy_get_interface_by_name(phy_mode);
if (pdata->phy_interface == -1) {
dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);

pdata->phy_interface = dev_read_phy_mode(dev);
if (pdata->phy_interface == PHY_INTERFACE_MODE_NONE)
return -EINVAL;
}

pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);

Expand Down
9 changes: 2 additions & 7 deletions drivers/net/higmacv300.c
Expand Up @@ -561,19 +561,14 @@ static int higmac_remove(struct udevice *dev)
static int higmac_of_to_plat(struct udevice *dev)
{
struct higmac_priv *priv = dev_get_priv(dev);
int phyintf = PHY_INTERFACE_MODE_NONE;
const char *phy_mode;
ofnode phy_node;

priv->base = dev_remap_addr_index(dev, 0);
priv->macif_ctrl = dev_remap_addr_index(dev, 1);

phy_mode = dev_read_string(dev, "phy-mode");
if (phy_mode)
phyintf = phy_get_interface_by_name(phy_mode);
if (phyintf == PHY_INTERFACE_MODE_NONE)
priv->phyintf = dev_read_phy_mode(dev);
if (priv->phyintf == PHY_INTERFACE_MODE_NONE)
return -ENODEV;
priv->phyintf = phyintf;

phy_node = dev_read_subnode(dev, "phy");
if (!ofnode_valid(phy_node)) {
Expand Down

0 comments on commit 123ca11

Please sign in to comment.