1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs) 4 * 5 * Author: Vitaly Bordug <vbordug@ru.mvista.com> 6 * Anton Vorontsov <avorontsov@ru.mvista.com> 7 * 8 * Copyright (c) 2006-2007 MontaVista Software, Inc. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/device/faux.h> 14 #include <linux/list.h> 15 #include <linux/mii.h> 16 #include <linux/phy.h> 17 #include <linux/phy_fixed.h> 18 #include <linux/err.h> 19 #include <linux/slab.h> 20 #include <linux/of.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/idr.h> 23 #include <linux/netdevice.h> 24 #include <linux/linkmode.h> 25 26 #include "swphy.h" 27 28 struct fixed_mdio_bus { 29 struct mii_bus *mii_bus; 30 struct list_head phys; 31 }; 32 33 struct fixed_phy { 34 int addr; 35 struct phy_device *phydev; 36 struct fixed_phy_status status; 37 bool no_carrier; 38 int (*link_update)(struct net_device *, struct fixed_phy_status *); 39 struct list_head node; 40 struct gpio_desc *link_gpiod; 41 }; 42 43 static struct faux_device *fdev; 44 static struct fixed_mdio_bus platform_fmb = { 45 .phys = LIST_HEAD_INIT(platform_fmb.phys), 46 }; 47 48 int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier) 49 { 50 struct fixed_mdio_bus *fmb = &platform_fmb; 51 struct phy_device *phydev = dev->phydev; 52 struct fixed_phy *fp; 53 54 if (!phydev || !phydev->mdio.bus) 55 return -EINVAL; 56 57 list_for_each_entry(fp, &fmb->phys, node) { 58 if (fp->addr == phydev->mdio.addr) { 59 fp->no_carrier = !new_carrier; 60 return 0; 61 } 62 } 63 return -EINVAL; 64 } 65 EXPORT_SYMBOL_GPL(fixed_phy_change_carrier); 66 67 static void fixed_phy_update(struct fixed_phy *fp) 68 { 69 if (!fp->no_carrier && fp->link_gpiod) 70 fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod); 71 } 72 73 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) 74 { 75 struct fixed_mdio_bus *fmb = bus->priv; 76 struct fixed_phy *fp; 77 78 list_for_each_entry(fp, &fmb->phys, node) { 79 if (fp->addr == phy_addr) { 80 struct fixed_phy_status state; 81 82 fp->status.link = !fp->no_carrier; 83 84 /* Issue callback if user registered it. */ 85 if (fp->link_update) 86 fp->link_update(fp->phydev->attached_dev, 87 &fp->status); 88 89 /* Check the GPIO for change in status */ 90 fixed_phy_update(fp); 91 state = fp->status; 92 93 return swphy_read_reg(reg_num, &state); 94 } 95 } 96 97 return 0xFFFF; 98 } 99 100 static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num, 101 u16 val) 102 { 103 return 0; 104 } 105 106 /* 107 * If something weird is required to be done with link/speed, 108 * network driver is able to assign a function to implement this. 109 * May be useful for PHY's that need to be software-driven. 110 */ 111 int fixed_phy_set_link_update(struct phy_device *phydev, 112 int (*link_update)(struct net_device *, 113 struct fixed_phy_status *)) 114 { 115 struct fixed_mdio_bus *fmb = &platform_fmb; 116 struct fixed_phy *fp; 117 118 if (!phydev || !phydev->mdio.bus) 119 return -EINVAL; 120 121 list_for_each_entry(fp, &fmb->phys, node) { 122 if (fp->addr == phydev->mdio.addr) { 123 fp->link_update = link_update; 124 fp->phydev = phydev; 125 return 0; 126 } 127 } 128 129 return -ENOENT; 130 } 131 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); 132 133 static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr, 134 const struct fixed_phy_status *status, 135 struct gpio_desc *gpiod) 136 { 137 int ret; 138 struct fixed_mdio_bus *fmb = &platform_fmb; 139 struct fixed_phy *fp; 140 141 ret = swphy_validate_state(status); 142 if (ret < 0) 143 return ret; 144 145 fp = kzalloc(sizeof(*fp), GFP_KERNEL); 146 if (!fp) 147 return -ENOMEM; 148 149 if (irq != PHY_POLL) 150 fmb->mii_bus->irq[phy_addr] = irq; 151 152 fp->addr = phy_addr; 153 fp->status = *status; 154 fp->link_gpiod = gpiod; 155 156 fixed_phy_update(fp); 157 158 list_add_tail(&fp->node, &fmb->phys); 159 160 return 0; 161 } 162 163 int fixed_phy_add(int phy_addr, const struct fixed_phy_status *status) 164 { 165 return fixed_phy_add_gpiod(PHY_POLL, phy_addr, status, NULL); 166 } 167 EXPORT_SYMBOL_GPL(fixed_phy_add); 168 169 static DEFINE_IDA(phy_fixed_ida); 170 171 static void fixed_phy_del(int phy_addr) 172 { 173 struct fixed_mdio_bus *fmb = &platform_fmb; 174 struct fixed_phy *fp, *tmp; 175 176 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { 177 if (fp->addr == phy_addr) { 178 list_del(&fp->node); 179 if (fp->link_gpiod) 180 gpiod_put(fp->link_gpiod); 181 kfree(fp); 182 ida_free(&phy_fixed_ida, phy_addr); 183 return; 184 } 185 } 186 } 187 188 #ifdef CONFIG_OF_GPIO 189 static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np) 190 { 191 struct device_node *fixed_link_node; 192 struct gpio_desc *gpiod; 193 194 if (!np) 195 return NULL; 196 197 fixed_link_node = of_get_child_by_name(np, "fixed-link"); 198 if (!fixed_link_node) 199 return NULL; 200 201 /* 202 * As the fixed link is just a device tree node without any 203 * Linux device associated with it, we simply have obtain 204 * the GPIO descriptor from the device tree like this. 205 */ 206 gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node), 207 "link", 0, GPIOD_IN, "mdio"); 208 if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) { 209 if (PTR_ERR(gpiod) != -ENOENT) 210 pr_err("error getting GPIO for fixed link %pOF, proceed without\n", 211 fixed_link_node); 212 gpiod = NULL; 213 } 214 of_node_put(fixed_link_node); 215 216 return gpiod; 217 } 218 #else 219 static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np) 220 { 221 return NULL; 222 } 223 #endif 224 225 struct phy_device *fixed_phy_register(const struct fixed_phy_status *status, 226 struct device_node *np) 227 { 228 struct fixed_mdio_bus *fmb = &platform_fmb; 229 struct gpio_desc *gpiod; 230 struct phy_device *phy; 231 int phy_addr; 232 int ret; 233 234 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) 235 return ERR_PTR(-EPROBE_DEFER); 236 237 /* Check if we have a GPIO associated with this fixed phy */ 238 gpiod = fixed_phy_get_gpiod(np); 239 if (IS_ERR(gpiod)) 240 return ERR_CAST(gpiod); 241 242 /* Get the next available PHY address, up to PHY_MAX_ADDR */ 243 phy_addr = ida_alloc_max(&phy_fixed_ida, PHY_MAX_ADDR - 1, GFP_KERNEL); 244 if (phy_addr < 0) 245 return ERR_PTR(phy_addr); 246 247 ret = fixed_phy_add_gpiod(PHY_POLL, phy_addr, status, gpiod); 248 if (ret < 0) { 249 ida_free(&phy_fixed_ida, phy_addr); 250 return ERR_PTR(ret); 251 } 252 253 phy = get_phy_device(fmb->mii_bus, phy_addr, false); 254 if (IS_ERR(phy)) { 255 fixed_phy_del(phy_addr); 256 return ERR_PTR(-EINVAL); 257 } 258 259 /* propagate the fixed link values to struct phy_device */ 260 phy->link = status->link; 261 if (status->link) { 262 phy->speed = status->speed; 263 phy->duplex = status->duplex; 264 phy->pause = status->pause; 265 phy->asym_pause = status->asym_pause; 266 } 267 268 of_node_get(np); 269 phy->mdio.dev.of_node = np; 270 phy->is_pseudo_fixed_link = true; 271 272 switch (status->speed) { 273 case SPEED_1000: 274 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 275 phy->supported); 276 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 277 phy->supported); 278 fallthrough; 279 case SPEED_100: 280 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 281 phy->supported); 282 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 283 phy->supported); 284 fallthrough; 285 case SPEED_10: 286 default: 287 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, 288 phy->supported); 289 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 290 phy->supported); 291 } 292 293 phy_advertise_supported(phy); 294 295 ret = phy_device_register(phy); 296 if (ret) { 297 phy_device_free(phy); 298 of_node_put(np); 299 fixed_phy_del(phy_addr); 300 return ERR_PTR(ret); 301 } 302 303 return phy; 304 } 305 EXPORT_SYMBOL_GPL(fixed_phy_register); 306 307 void fixed_phy_unregister(struct phy_device *phy) 308 { 309 phy_device_remove(phy); 310 of_node_put(phy->mdio.dev.of_node); 311 fixed_phy_del(phy->mdio.addr); 312 } 313 EXPORT_SYMBOL_GPL(fixed_phy_unregister); 314 315 static int __init fixed_mdio_bus_init(void) 316 { 317 struct fixed_mdio_bus *fmb = &platform_fmb; 318 int ret; 319 320 fdev = faux_device_create("Fixed MDIO bus", NULL, NULL); 321 if (!fdev) 322 return -ENODEV; 323 324 fmb->mii_bus = mdiobus_alloc(); 325 if (fmb->mii_bus == NULL) { 326 ret = -ENOMEM; 327 goto err_mdiobus_reg; 328 } 329 330 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); 331 fmb->mii_bus->name = "Fixed MDIO Bus"; 332 fmb->mii_bus->priv = fmb; 333 fmb->mii_bus->parent = &fdev->dev; 334 fmb->mii_bus->read = &fixed_mdio_read; 335 fmb->mii_bus->write = &fixed_mdio_write; 336 fmb->mii_bus->phy_mask = ~0; 337 338 ret = mdiobus_register(fmb->mii_bus); 339 if (ret) 340 goto err_mdiobus_alloc; 341 342 return 0; 343 344 err_mdiobus_alloc: 345 mdiobus_free(fmb->mii_bus); 346 err_mdiobus_reg: 347 faux_device_destroy(fdev); 348 return ret; 349 } 350 module_init(fixed_mdio_bus_init); 351 352 static void __exit fixed_mdio_bus_exit(void) 353 { 354 struct fixed_mdio_bus *fmb = &platform_fmb; 355 struct fixed_phy *fp, *tmp; 356 357 mdiobus_unregister(fmb->mii_bus); 358 mdiobus_free(fmb->mii_bus); 359 faux_device_destroy(fdev); 360 361 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { 362 list_del(&fp->node); 363 kfree(fp); 364 } 365 ida_destroy(&phy_fixed_ida); 366 } 367 module_exit(fixed_mdio_bus_exit); 368 369 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); 370 MODULE_AUTHOR("Vitaly Bordug"); 371 MODULE_LICENSE("GPL"); 372