Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 |
| 4 | * Alex Marginean, NXP |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 11 | #include <miiphy.h> |
| 12 | #include <dm/device-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Bin Meng | e0518f0 | 2021-03-14 20:14:47 +0800 | [diff] [blame] | 14 | #include <dm/of_extra.h> |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 15 | #include <dm/uclass-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <linux/compat.h> |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 17 | |
| 18 | void dm_mdio_probe_devices(void) |
| 19 | { |
| 20 | struct udevice *it; |
| 21 | struct uclass *uc; |
| 22 | |
| 23 | uclass_get(UCLASS_MDIO, &uc); |
| 24 | uclass_foreach_dev(it, uc) { |
| 25 | device_probe(it); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | static int dm_mdio_post_bind(struct udevice *dev) |
| 30 | { |
Alex Marginean | 2e859ad | 2019-07-25 12:33:17 +0300 | [diff] [blame] | 31 | const char *dt_name; |
| 32 | |
| 33 | /* set a custom name for the MDIO device, if present in DT */ |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 34 | if (dev_has_ofnode(dev)) { |
| 35 | dt_name = dev_read_string(dev, "device-name"); |
Alex Marginean | 2e859ad | 2019-07-25 12:33:17 +0300 | [diff] [blame] | 36 | if (dt_name) { |
| 37 | debug("renaming dev %s to %s\n", dev->name, dt_name); |
| 38 | device_set_name(dev, dt_name); |
| 39 | } |
| 40 | } |
| 41 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 42 | /* |
| 43 | * MDIO command doesn't like spaces in names, don't allow them to keep |
| 44 | * it happy |
| 45 | */ |
| 46 | if (strchr(dev->name, ' ')) { |
| 47 | debug("\nError: MDIO device name \"%s\" has a space!\n", |
| 48 | dev->name); |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Marek Behún | 0e0651d | 2022-04-07 00:32:58 +0200 | [diff] [blame] | 55 | int dm_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg) |
| 56 | { |
| 57 | struct mdio_ops *ops = mdio_get_ops(mdio_dev); |
| 58 | |
| 59 | if (!ops->read) |
| 60 | return -ENOSYS; |
| 61 | |
| 62 | return ops->read(mdio_dev, addr, devad, reg); |
| 63 | } |
| 64 | |
| 65 | int dm_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg, |
| 66 | u16 val) |
| 67 | { |
| 68 | struct mdio_ops *ops = mdio_get_ops(mdio_dev); |
| 69 | |
| 70 | if (!ops->write) |
| 71 | return -ENOSYS; |
| 72 | |
| 73 | return ops->write(mdio_dev, addr, devad, reg, val); |
| 74 | } |
| 75 | |
| 76 | int dm_mdio_reset(struct udevice *mdio_dev) |
| 77 | { |
| 78 | struct mdio_ops *ops = mdio_get_ops(mdio_dev); |
| 79 | |
| 80 | if (!ops->reset) |
| 81 | return 0; |
| 82 | |
| 83 | return ops->reset(mdio_dev); |
| 84 | } |
| 85 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 86 | /* |
| 87 | * Following read/write/reset functions are registered with legacy MII code. |
| 88 | * These are called for PHY operations by upper layers and we further call the |
| 89 | * DM MDIO driver functions. |
| 90 | */ |
| 91 | static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg) |
| 92 | { |
Marek Behún | e4dedf2 | 2022-04-07 00:32:59 +0200 | [diff] [blame] | 93 | return dm_mdio_read(mii_bus->priv, addr, devad, reg); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg, |
| 97 | u16 val) |
| 98 | { |
Marek Behún | e4dedf2 | 2022-04-07 00:32:59 +0200 | [diff] [blame] | 99 | return dm_mdio_write(mii_bus->priv, addr, devad, reg, val); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int mdio_reset(struct mii_dev *mii_bus) |
| 103 | { |
Marek Behún | e4dedf2 | 2022-04-07 00:32:59 +0200 | [diff] [blame] | 104 | return dm_mdio_reset(mii_bus->priv); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int dm_mdio_post_probe(struct udevice *dev) |
| 108 | { |
| 109 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
| 110 | |
| 111 | pdata->mii_bus = mdio_alloc(); |
| 112 | pdata->mii_bus->read = mdio_read; |
| 113 | pdata->mii_bus->write = mdio_write; |
| 114 | pdata->mii_bus->reset = mdio_reset; |
| 115 | pdata->mii_bus->priv = dev; |
Vladimir Oltean | 5404544 | 2021-09-27 14:22:00 +0300 | [diff] [blame] | 116 | strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 117 | |
| 118 | return mdio_register(pdata->mii_bus); |
| 119 | } |
| 120 | |
| 121 | static int dm_mdio_pre_remove(struct udevice *dev) |
| 122 | { |
| 123 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 124 | |
Marek Behún | e4dedf2 | 2022-04-07 00:32:59 +0200 | [diff] [blame] | 125 | dm_mdio_reset(dev); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 126 | mdio_unregister(pdata->mii_bus); |
| 127 | mdio_free(pdata->mii_bus); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Marek Behún | 1c1c37f | 2022-04-27 12:41:49 +0200 | [diff] [blame^] | 132 | struct phy_device *dm_phy_find_by_ofnode(ofnode phynode) |
| 133 | { |
| 134 | struct mdio_perdev_priv *pdata; |
| 135 | struct udevice *mdiodev; |
| 136 | u32 phy_addr; |
| 137 | |
| 138 | if (ofnode_read_u32(phynode, "reg", &phy_addr)) |
| 139 | return NULL; |
| 140 | |
| 141 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
| 142 | ofnode_get_parent(phynode), |
| 143 | &mdiodev)) |
| 144 | return NULL; |
| 145 | |
| 146 | if (device_probe(mdiodev)) |
| 147 | return NULL; |
| 148 | |
| 149 | pdata = dev_get_uclass_priv(mdiodev); |
| 150 | |
| 151 | return phy_find_by_mask(pdata->mii_bus, BIT(phy_addr)); |
| 152 | } |
| 153 | |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 154 | struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 155 | struct udevice *ethdev, |
| 156 | phy_interface_t interface) |
| 157 | { |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 158 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 159 | |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 160 | if (device_probe(mdiodev)) |
| 161 | return NULL; |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 162 | |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 163 | return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 164 | } |
| 165 | |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 166 | static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev, |
| 167 | phy_interface_t interface) |
| 168 | { |
| 169 | u32 phy_addr; |
| 170 | struct udevice *mdiodev; |
| 171 | struct phy_device *phy; |
Vladimir Oltean | 6ca194a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 172 | ofnode phynode; |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 173 | |
Rasmus Villemoes | e505164 | 2020-10-05 15:15:16 +0200 | [diff] [blame] | 174 | if (CONFIG_IS_ENABLED(PHY_FIXED) && |
Vladimir Oltean | 6ca194a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 175 | ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) { |
| 176 | phy = phy_connect(NULL, 0, ethdev, interface); |
Rasmus Villemoes | e505164 | 2020-10-05 15:15:16 +0200 | [diff] [blame] | 177 | goto out; |
| 178 | } |
| 179 | |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 180 | phynode = dev_get_phy_node(ethdev); |
| 181 | if (!ofnode_valid(phynode)) { |
Sean Anderson | 836cb5e | 2020-09-15 10:44:53 -0400 | [diff] [blame] | 182 | dev_dbg(ethdev, "can't find PHY node\n"); |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * reading 'reg' directly should be fine. This is a PHY node, the |
| 188 | * address is always size 1 and requires no translation |
| 189 | */ |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 190 | if (ofnode_read_u32(phynode, "reg", &phy_addr)) { |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 191 | dev_dbg(ethdev, "missing reg property in phy node\n"); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 196 | ofnode_get_parent(phynode), |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 197 | &mdiodev)) { |
Sean Anderson | 836cb5e | 2020-09-15 10:44:53 -0400 | [diff] [blame] | 198 | dev_dbg(ethdev, "can't find MDIO bus for node %s\n", |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 199 | ofnode_get_name(ofnode_get_parent(phynode))); |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface); |
| 204 | |
Rasmus Villemoes | e505164 | 2020-10-05 15:15:16 +0200 | [diff] [blame] | 205 | out: |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 206 | if (phy) |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 207 | phy->node = phynode; |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 208 | |
| 209 | return phy; |
| 210 | } |
| 211 | |
| 212 | /* Connect to a PHY linked in eth DT node */ |
| 213 | struct phy_device *dm_eth_phy_connect(struct udevice *ethdev) |
| 214 | { |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 215 | phy_interface_t interface; |
| 216 | struct phy_device *phy; |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 217 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 218 | if (!dev_has_ofnode(ethdev)) { |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 219 | debug("%s: supplied eth dev has no DT node!\n", ethdev->name); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 223 | interface = dev_read_phy_mode(ethdev); |
Marek Behún | 48631e4 | 2022-04-07 00:33:03 +0200 | [diff] [blame] | 224 | if (interface == PHY_INTERFACE_MODE_NA) |
| 225 | dev_dbg(ethdev, "can't find interface mode, default to NA\n"); |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 226 | |
| 227 | phy = dm_eth_connect_phy_handle(ethdev, interface); |
| 228 | |
| 229 | if (!phy) |
| 230 | return NULL; |
| 231 | |
| 232 | phy->interface = interface; |
| 233 | |
| 234 | return phy; |
| 235 | } |
| 236 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 237 | UCLASS_DRIVER(mdio) = { |
| 238 | .id = UCLASS_MDIO, |
| 239 | .name = "mdio", |
| 240 | .post_bind = dm_mdio_post_bind, |
| 241 | .post_probe = dm_mdio_post_probe, |
| 242 | .pre_remove = dm_mdio_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 243 | .per_device_auto = sizeof(struct mdio_perdev_priv), |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 244 | }; |