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