Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | // SPDX-License-Identifier: GPL-2.0+ /* * (C) Copyright 2019 * Alex Marginean, NXP */ #include <dm.h> #include <dm/lists.h> #include <eth_phy.h> #include <log.h> #include <malloc.h> #include <miiphy.h> #include <dm/device-internal.h> #include <dm/device_compat.h> #include <dm/of_extra.h> #include <dm/uclass-internal.h> #include <linux/compat.h> #include <linux/delay.h> #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */ 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) { const char *dt_name; /* set a custom name for the MDIO device, if present in DT */ if (dev_has_ofnode(dev)) { dt_name = dev_read_string(dev, "device-name"); if (dt_name) { debug("renaming dev %s to %s\n", dev->name, dt_name); device_set_name(dev, dt_name); } } /* * 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; } #if CONFIG_IS_ENABLED(OF_REAL) return dm_scan_fdt_dev(dev); #else return 0; #endif } 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); struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdio_dev); struct mii_dev *mii_bus = pdata->mii_bus; if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&mii_bus->reset_gpiod)) { dm_gpio_set_value(&mii_bus->reset_gpiod, 1); udelay(mii_bus->reset_delay_us); dm_gpio_set_value(&mii_bus->reset_gpiod, 0); if (mii_bus->reset_post_delay_us > 0) udelay(mii_bus->reset_post_delay_us); } if (!ops->reset) return 0; return ops->reset(mdio_dev); } /* * 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) { return dm_mdio_read(mii_bus->priv, addr, devad, reg); } static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg, u16 val) { return dm_mdio_write(mii_bus->priv, addr, devad, reg, val); } static int mdio_reset(struct mii_dev *mii_bus) { return dm_mdio_reset(mii_bus->priv); } static int mdio_bind_phy_nodes(struct udevice *mdio_dev) { ofnode mdio_node, phy_node; struct udevice *phy_dev; const char *node_name; int ret; mdio_node = dev_ofnode(mdio_dev); if (!ofnode_valid(mdio_node)) { dev_dbg(mdio_dev, "invalid ofnode for mdio_dev\n"); return -ENXIO; } ofnode_for_each_subnode(phy_node, mdio_node) { node_name = ofnode_get_name(phy_node); dev_dbg(mdio_dev, "* Found child node: '%s'\n", node_name); ret = device_bind_driver_to_node(mdio_dev, "eth_phy_generic_drv", node_name, phy_node, &phy_dev); if (ret) { dev_dbg(mdio_dev, " - Eth phy binding error: %d\n", ret); continue; } dev_dbg(mdio_dev, " - bound phy device: '%s'\n", node_name); ret = device_probe(phy_dev); if (ret) { dev_dbg(mdio_dev, "Device '%s' probe failed\n", phy_dev->name); device_unbind(phy_dev); continue; } } return 0; } static int dm_mdio_post_probe(struct udevice *dev) { struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); struct mii_dev *mii_bus; int ret; mii_bus = mdio_alloc(); if (!mii_bus) { dev_err(dev, "couldn't allocate mii_bus\n"); return -ENOMEM; } pdata->mii_bus = mii_bus; pdata->mii_bus->read = mdio_read; pdata->mii_bus->write = mdio_write; pdata->mii_bus->reset = mdio_reset; pdata->mii_bus->priv = dev; strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN); if (IS_ENABLED(CONFIG_DM_GPIO)) { /* Get bus level PHY reset GPIO details */ mii_bus->reset_delay_us = dev_read_u32_default(dev, "reset-delay-us", DEFAULT_GPIO_RESET_DELAY); mii_bus->reset_post_delay_us = dev_read_u32_default(dev, "reset-post-delay-us", 0); ret = gpio_request_by_name(dev, "reset-gpios", 0, &mii_bus->reset_gpiod, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); if (ret && ret != -ENOENT) { dev_err(dev, "couldn't get reset-gpios: %d\n", ret); return ret; } } if (CONFIG_IS_ENABLED(DM_ETH_PHY)) mdio_bind_phy_nodes(dev); 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); dm_mdio_reset(dev); mdio_unregister(pdata->mii_bus); mdio_free(pdata->mii_bus); return 0; } 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)); } struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, struct udevice *ethdev, phy_interface_t interface) { struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev); if (device_probe(mdiodev)) return NULL; return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); } struct phy_device *dm_eth_phy_connect_interface(struct udevice *ethdev, phy_interface_t interface) { u32 phy_addr; struct udevice *mdiodev; struct phy_device *phy; ofnode phynode; if (IS_ENABLED(CONFIG_PHY_FIXED) && ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) { phy = phy_connect(NULL, 0, ethdev, interface); goto out; } phynode = dev_get_phy_node(ethdev); if (!ofnode_valid(phynode)) { dev_dbg(ethdev, "can't find PHY node\n"); return NULL; } /* * reading 'reg' directly should be fine. This is a PHY node, the * address is always size 1 and requires no translation */ if (ofnode_read_u32(phynode, "reg", &phy_addr)) { dev_dbg(ethdev, "missing reg property in phy node\n"); return NULL; } if (uclass_get_device_by_ofnode(UCLASS_MDIO, ofnode_get_parent(phynode), &mdiodev)) { dev_dbg(ethdev, "can't find MDIO bus for node %s\n", ofnode_get_name(ofnode_get_parent(phynode))); return NULL; } phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface); out: if (phy) { phy->node = phynode; phy->interface = interface; } 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; if (!dev_has_ofnode(ethdev)) { debug("%s: supplied eth dev has no DT node!\n", ethdev->name); return NULL; } interface = dev_read_phy_mode(ethdev); if (interface == PHY_INTERFACE_MODE_NA) dev_dbg(ethdev, "can't find interface mode, default to NA\n"); return dm_eth_phy_connect_interface(ethdev, interface); } 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, .per_device_auto = sizeof(struct mdio_perdev_priv), }; |