2019-06-03 16:10:30 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* (C) Copyright 2019
|
|
|
|
* Alex Marginean, NXP
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2019-06-03 16:10:30 +00:00
|
|
|
#include <miiphy.h>
|
|
|
|
#include <dm/device-internal.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2021-03-14 12:14:47 +00:00
|
|
|
#include <dm/of_extra.h>
|
2019-06-03 16:10:30 +00:00
|
|
|
#include <dm/uclass-internal.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <linux/compat.h>
|
2019-06-03 16:10:30 +00:00
|
|
|
|
|
|
|
void dm_mdio_probe_devices(void)
|
|
|
|
{
|
|
|
|
struct udevice *it;
|
|
|
|
struct uclass *uc;
|
|
|
|
|
|
|
|
uclass_get(UCLASS_MDIO, &uc);
|
|
|
|
uclass_foreach_dev(it, uc) {
|
|
|
|
device_probe(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dm_mdio_post_bind(struct udevice *dev)
|
|
|
|
{
|
2019-07-25 09:33:17 +00:00
|
|
|
const char *dt_name;
|
|
|
|
|
|
|
|
/* set a custom name for the MDIO device, if present in DT */
|
2020-12-19 17:40:14 +00:00
|
|
|
if (dev_has_ofnode(dev)) {
|
|
|
|
dt_name = dev_read_string(dev, "device-name");
|
2019-07-25 09:33:17 +00:00
|
|
|
if (dt_name) {
|
|
|
|
debug("renaming dev %s to %s\n", dev->name, dt_name);
|
|
|
|
device_set_name(dev, dt_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 16:10:30 +00:00
|
|
|
/*
|
|
|
|
* MDIO command doesn't like spaces in names, don't allow them to keep
|
|
|
|
* it happy
|
|
|
|
*/
|
|
|
|
if (strchr(dev->name, ' ')) {
|
|
|
|
debug("\nError: MDIO device name \"%s\" has a space!\n",
|
|
|
|
dev->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:42:44 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
|
|
|
return dm_scan_fdt_dev(dev);
|
|
|
|
#else
|
2019-06-03 16:10:30 +00:00
|
|
|
return 0;
|
2022-11-30 17:42:44 +00:00
|
|
|
#endif
|
2019-06-03 16:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 22:32:58 +00:00
|
|
|
int dm_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
|
|
|
|
{
|
|
|
|
struct mdio_ops *ops = mdio_get_ops(mdio_dev);
|
|
|
|
|
|
|
|
if (!ops->read)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return ops->read(mdio_dev, addr, devad, reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg,
|
|
|
|
u16 val)
|
|
|
|
{
|
|
|
|
struct mdio_ops *ops = mdio_get_ops(mdio_dev);
|
|
|
|
|
|
|
|
if (!ops->write)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return ops->write(mdio_dev, addr, devad, reg, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_mdio_reset(struct udevice *mdio_dev)
|
|
|
|
{
|
|
|
|
struct mdio_ops *ops = mdio_get_ops(mdio_dev);
|
|
|
|
|
|
|
|
if (!ops->reset)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ops->reset(mdio_dev);
|
|
|
|
}
|
|
|
|
|
2019-06-03 16:10:30 +00:00
|
|
|
/*
|
|
|
|
* Following read/write/reset functions are registered with legacy MII code.
|
|
|
|
* These are called for PHY operations by upper layers and we further call the
|
|
|
|
* DM MDIO driver functions.
|
|
|
|
*/
|
|
|
|
static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
|
|
|
|
{
|
2022-04-06 22:32:59 +00:00
|
|
|
return dm_mdio_read(mii_bus->priv, addr, devad, reg);
|
2019-06-03 16:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
|
|
|
|
u16 val)
|
|
|
|
{
|
2022-04-06 22:32:59 +00:00
|
|
|
return dm_mdio_write(mii_bus->priv, addr, devad, reg, val);
|
2019-06-03 16:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mdio_reset(struct mii_dev *mii_bus)
|
|
|
|
{
|
2022-04-06 22:32:59 +00:00
|
|
|
return dm_mdio_reset(mii_bus->priv);
|
2019-06-03 16:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dm_mdio_post_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
pdata->mii_bus = mdio_alloc();
|
|
|
|
pdata->mii_bus->read = mdio_read;
|
|
|
|
pdata->mii_bus->write = mdio_write;
|
|
|
|
pdata->mii_bus->reset = mdio_reset;
|
|
|
|
pdata->mii_bus->priv = dev;
|
2021-09-27 11:22:00 +00:00
|
|
|
strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
|
2019-06-03 16:10:30 +00:00
|
|
|
|
|
|
|
return mdio_register(pdata->mii_bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dm_mdio_pre_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
|
|
|
|
|
2022-04-06 22:32:59 +00:00
|
|
|
dm_mdio_reset(dev);
|
2019-06-03 16:10:30 +00:00
|
|
|
mdio_unregister(pdata->mii_bus);
|
|
|
|
mdio_free(pdata->mii_bus);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:41:49 +00:00
|
|
|
struct phy_device *dm_phy_find_by_ofnode(ofnode phynode)
|
|
|
|
{
|
|
|
|
struct mdio_perdev_priv *pdata;
|
|
|
|
struct udevice *mdiodev;
|
|
|
|
u32 phy_addr;
|
|
|
|
|
|
|
|
if (ofnode_read_u32(phynode, "reg", &phy_addr))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (uclass_get_device_by_ofnode(UCLASS_MDIO,
|
|
|
|
ofnode_get_parent(phynode),
|
|
|
|
&mdiodev))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (device_probe(mdiodev))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pdata = dev_get_uclass_priv(mdiodev);
|
|
|
|
|
|
|
|
return phy_find_by_mask(pdata->mii_bus, BIT(phy_addr));
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:15:11 +00:00
|
|
|
struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
|
2019-06-03 16:10:30 +00:00
|
|
|
struct udevice *ethdev,
|
|
|
|
phy_interface_t interface)
|
|
|
|
{
|
2019-11-25 15:15:11 +00:00
|
|
|
struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
|
2019-06-03 16:10:30 +00:00
|
|
|
|
2019-11-25 15:15:11 +00:00
|
|
|
if (device_probe(mdiodev))
|
|
|
|
return NULL;
|
2019-06-03 16:10:30 +00:00
|
|
|
|
2019-11-25 15:15:11 +00:00
|
|
|
return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
|
2019-06-03 16:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 15:15:12 +00:00
|
|
|
static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
|
|
|
|
phy_interface_t interface)
|
|
|
|
{
|
|
|
|
u32 phy_addr;
|
|
|
|
struct udevice *mdiodev;
|
|
|
|
struct phy_device *phy;
|
net: phy: fixed: Be compatible with live OF tree
On systems that use CONFIG_OF_LIVE, the "ofnode" type is defined
as const struct device_node *np, while on the flat DT systems it
is defined as a long of_offset into gd->fdt_blob.
It is desirable that the fixed PHY driver uses the higher-level
ofnode abstraction instead of parsing gd->fdt_blob directly,
because that enables it to work on live OF systems.
The fixed PHY driver has used a nasty hack since its introduction in
commit db40c1aa1c10 ("drivers/net/phy: add fixed-phy /
fixed-link support"),
which is to pass the long gd->fdt_blob offset inside int phydev->addr
(a value that normally holds the MDIO bus address at which the PHY
responds). Even ignoring the fact that the types were already
mismatched leading to a potential truncation (flat OF offset was
supposed to be a long and not an int), we really cannot extend this
hack any longer, because there's no way an int will hold the other
representation of ofnode, the struct device_node *np.
So we unfortunately need to do the right thing, which is to use the
framework introduced by Grygorii Strashko in
commit eef0b8a930d1 ("net: phy: add ofnode node to struct phy_device").
This will populate phydev->node for the fixed PHY.
Note that phydev->node will not be valid in the probe function, since
that is called synchronously from phy_device_create and we really have
no way of passing the ofnode directly through the phy_device_create API.
So we do what other drivers do too: we move the OF parsing logic from
the .probe to the .config method of the PHY driver. The new function
will be called at phy_config() time.
I do believe I've converted all the possible call paths for creating
a PHY with PHY_FIXED_ID, so there is really no reason to maintain
compatibility with the old logic of retrieving a flat OF tree offset
from phydev->addr. We just pass 0 to phydev->addr now.
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Message-Id: <20210216224804.3355044-2-olteanv@gmail.com>
[bmeng: keep fixedphy_probe(); update mdio-uclass.c to handle fixed phy]
Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Priyanka Jain <priyanka.jain@nxp.com>
2021-03-14 12:14:48 +00:00
|
|
|
ofnode phynode;
|
2019-11-25 15:15:12 +00:00
|
|
|
|
2023-02-22 16:33:51 +00:00
|
|
|
if (IS_ENABLED(CONFIG_PHY_FIXED) &&
|
net: phy: fixed: Be compatible with live OF tree
On systems that use CONFIG_OF_LIVE, the "ofnode" type is defined
as const struct device_node *np, while on the flat DT systems it
is defined as a long of_offset into gd->fdt_blob.
It is desirable that the fixed PHY driver uses the higher-level
ofnode abstraction instead of parsing gd->fdt_blob directly,
because that enables it to work on live OF systems.
The fixed PHY driver has used a nasty hack since its introduction in
commit db40c1aa1c10 ("drivers/net/phy: add fixed-phy /
fixed-link support"),
which is to pass the long gd->fdt_blob offset inside int phydev->addr
(a value that normally holds the MDIO bus address at which the PHY
responds). Even ignoring the fact that the types were already
mismatched leading to a potential truncation (flat OF offset was
supposed to be a long and not an int), we really cannot extend this
hack any longer, because there's no way an int will hold the other
representation of ofnode, the struct device_node *np.
So we unfortunately need to do the right thing, which is to use the
framework introduced by Grygorii Strashko in
commit eef0b8a930d1 ("net: phy: add ofnode node to struct phy_device").
This will populate phydev->node for the fixed PHY.
Note that phydev->node will not be valid in the probe function, since
that is called synchronously from phy_device_create and we really have
no way of passing the ofnode directly through the phy_device_create API.
So we do what other drivers do too: we move the OF parsing logic from
the .probe to the .config method of the PHY driver. The new function
will be called at phy_config() time.
I do believe I've converted all the possible call paths for creating
a PHY with PHY_FIXED_ID, so there is really no reason to maintain
compatibility with the old logic of retrieving a flat OF tree offset
from phydev->addr. We just pass 0 to phydev->addr now.
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Message-Id: <20210216224804.3355044-2-olteanv@gmail.com>
[bmeng: keep fixedphy_probe(); update mdio-uclass.c to handle fixed phy]
Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Priyanka Jain <priyanka.jain@nxp.com>
2021-03-14 12:14:48 +00:00
|
|
|
ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
|
|
|
|
phy = phy_connect(NULL, 0, ethdev, interface);
|
mdio-uclass.c: support fixed-link subnodes
When trying to port our mpc8309-based board to DM_ETH, on top of
Heiko's patches, I found that nothing in mdio-uclass.c seems to
support the use of a fixed-link subnode of the ethernet DT node. That
is, the ethernet node looks like
enet0: ethernet@2000 {
device_type = "network";
compatible = "ucc_geth";
...
fixed-link {
reg = <0xffffffff>;
speed = <100>;
full-duplex;
};
but the current code expects there to be phy-handle property. Adding
that, i.e.
phy-handle = <&enet0phy>;
enet0phy: fixed-link {
just makes the code break a few lines later since a fixed-link node
doesn't have a reg property. Ignoring the dtc complaint and adding a
dummy reg property, we of course hit "can't find MDIO bus for node
ethernet@2000" since indeed, the parent node of the phy node does not
represent an MDIO bus. So that's obviously the wrong path.
Now, in linux, it seems that the fixed link case is treated specially;
in the of_phy_get_and_connect() which roughly corresponds to
dm_eth_connect_phy_handle() we have
if (of_phy_is_fixed_link(np)) {
ret = of_phy_register_fixed_link(np);
...
} else {
phy_np = of_parse_phandle(np, "phy-handle", 0);
...
}
phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
And U-Boot's phy_connect() does have support for fixed-link
subnodes. Calling phy_connect() directly with NULL bus and a dummy
address does seem to make the ethernet work.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Heiko Schocher <hs@denx.de>
2020-10-05 13:15:16 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-04-06 22:32:57 +00:00
|
|
|
phynode = dev_get_phy_node(ethdev);
|
|
|
|
if (!ofnode_valid(phynode)) {
|
2020-09-15 14:44:53 +00:00
|
|
|
dev_dbg(ethdev, "can't find PHY node\n");
|
2019-11-25 15:15:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reading 'reg' directly should be fine. This is a PHY node, the
|
|
|
|
* address is always size 1 and requires no translation
|
|
|
|
*/
|
2022-04-06 22:32:57 +00:00
|
|
|
if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
|
2019-11-25 15:15:12 +00:00
|
|
|
dev_dbg(ethdev, "missing reg property in phy node\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uclass_get_device_by_ofnode(UCLASS_MDIO,
|
2022-04-06 22:32:57 +00:00
|
|
|
ofnode_get_parent(phynode),
|
2019-11-25 15:15:12 +00:00
|
|
|
&mdiodev)) {
|
2020-09-15 14:44:53 +00:00
|
|
|
dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
|
2022-04-06 22:32:57 +00:00
|
|
|
ofnode_get_name(ofnode_get_parent(phynode)));
|
2019-11-25 15:15:12 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
|
|
|
|
|
mdio-uclass.c: support fixed-link subnodes
When trying to port our mpc8309-based board to DM_ETH, on top of
Heiko's patches, I found that nothing in mdio-uclass.c seems to
support the use of a fixed-link subnode of the ethernet DT node. That
is, the ethernet node looks like
enet0: ethernet@2000 {
device_type = "network";
compatible = "ucc_geth";
...
fixed-link {
reg = <0xffffffff>;
speed = <100>;
full-duplex;
};
but the current code expects there to be phy-handle property. Adding
that, i.e.
phy-handle = <&enet0phy>;
enet0phy: fixed-link {
just makes the code break a few lines later since a fixed-link node
doesn't have a reg property. Ignoring the dtc complaint and adding a
dummy reg property, we of course hit "can't find MDIO bus for node
ethernet@2000" since indeed, the parent node of the phy node does not
represent an MDIO bus. So that's obviously the wrong path.
Now, in linux, it seems that the fixed link case is treated specially;
in the of_phy_get_and_connect() which roughly corresponds to
dm_eth_connect_phy_handle() we have
if (of_phy_is_fixed_link(np)) {
ret = of_phy_register_fixed_link(np);
...
} else {
phy_np = of_parse_phandle(np, "phy-handle", 0);
...
}
phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
And U-Boot's phy_connect() does have support for fixed-link
subnodes. Calling phy_connect() directly with NULL bus and a dummy
address does seem to make the ethernet work.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Heiko Schocher <hs@denx.de>
2020-10-05 13:15:16 +00:00
|
|
|
out:
|
2019-11-25 15:15:12 +00:00
|
|
|
if (phy)
|
2022-04-06 22:32:57 +00:00
|
|
|
phy->node = phynode;
|
2019-11-25 15:15:12 +00:00
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to a PHY linked in eth DT node */
|
|
|
|
struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
|
|
|
|
{
|
|
|
|
phy_interface_t interface;
|
|
|
|
struct phy_device *phy;
|
|
|
|
|
2020-12-19 17:40:14 +00:00
|
|
|
if (!dev_has_ofnode(ethdev)) {
|
2019-11-25 15:15:12 +00:00
|
|
|
debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-06 22:33:01 +00:00
|
|
|
interface = dev_read_phy_mode(ethdev);
|
2022-04-06 22:33:03 +00:00
|
|
|
if (interface == PHY_INTERFACE_MODE_NA)
|
|
|
|
dev_dbg(ethdev, "can't find interface mode, default to NA\n");
|
2019-11-25 15:15:12 +00:00
|
|
|
|
|
|
|
phy = dm_eth_connect_phy_handle(ethdev, interface);
|
|
|
|
|
|
|
|
if (!phy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
phy->interface = interface;
|
|
|
|
|
|
|
|
return phy;
|
|
|
|
}
|
|
|
|
|
2019-06-03 16:10:30 +00:00
|
|
|
UCLASS_DRIVER(mdio) = {
|
|
|
|
.id = UCLASS_MDIO,
|
|
|
|
.name = "mdio",
|
|
|
|
.post_bind = dm_mdio_post_bind,
|
|
|
|
.post_probe = dm_mdio_post_probe,
|
|
|
|
.pre_remove = dm_mdio_pre_remove,
|
2020-12-03 23:55:17 +00:00
|
|
|
.per_device_auto = sizeof(struct mdio_perdev_priv),
|
2019-06-03 16:10:30 +00:00
|
|
|
};
|