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 struct phy_device *fixed_phy_register(unsigned int irq, 233 struct fixed_phy_status *status, 234 struct device_node *np) 235 { 236 struct fixed_mdio_bus *fmb = &platform_fmb; 237 struct gpio_desc *gpiod = NULL; 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 gpiod = fixed_phy_get_gpiod(np); 247 if (IS_ERR(gpiod)) 248 return ERR_CAST(gpiod); 249 250 /* Get the next available PHY address, up to PHY_MAX_ADDR */ 251 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL); 252 if (phy_addr < 0) 253 return ERR_PTR(phy_addr); 254 255 ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod); 256 if (ret < 0) { 257 ida_simple_remove(&phy_fixed_ida, phy_addr); 258 return ERR_PTR(ret); 259 } 260 261 phy = get_phy_device(fmb->mii_bus, phy_addr, false); 262 if (IS_ERR(phy)) { 263 fixed_phy_del(phy_addr); 264 return ERR_PTR(-EINVAL); 265 } 266 267 /* propagate the fixed link values to struct phy_device */ 268 phy->link = status->link; 269 if (status->link) { 270 phy->speed = status->speed; 271 phy->duplex = status->duplex; 272 phy->pause = status->pause; 273 phy->asym_pause = status->asym_pause; 274 } 275 276 of_node_get(np); 277 phy->mdio.dev.of_node = np; 278 phy->is_pseudo_fixed_link = true; 279 280 switch (status->speed) { 281 case SPEED_1000: 282 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 283 phy->supported); 284 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 285 phy->supported); 286 /* fall through */ 287 case SPEED_100: 288 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 289 phy->supported); 290 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 291 phy->supported); 292 /* fall through */ 293 case SPEED_10: 294 default: 295 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, 296 phy->supported); 297 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, 298 phy->supported); 299 } 300 301 ret = phy_device_register(phy); 302 if (ret) { 303 phy_device_free(phy); 304 of_node_put(np); 305 fixed_phy_del(phy_addr); 306 return ERR_PTR(ret); 307 } 308 309 return phy; 310 } 311 EXPORT_SYMBOL_GPL(fixed_phy_register); 312 313 void fixed_phy_unregister(struct phy_device *phy) 314 { 315 phy_device_remove(phy); 316 of_node_put(phy->mdio.dev.of_node); 317 fixed_phy_del(phy->mdio.addr); 318 } 319 EXPORT_SYMBOL_GPL(fixed_phy_unregister); 320 321 static int __init fixed_mdio_bus_init(void) 322 { 323 struct fixed_mdio_bus *fmb = &platform_fmb; 324 int ret; 325 326 pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); 327 if (IS_ERR(pdev)) 328 return PTR_ERR(pdev); 329 330 fmb->mii_bus = mdiobus_alloc(); 331 if (fmb->mii_bus == NULL) { 332 ret = -ENOMEM; 333 goto err_mdiobus_reg; 334 } 335 336 snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); 337 fmb->mii_bus->name = "Fixed MDIO Bus"; 338 fmb->mii_bus->priv = fmb; 339 fmb->mii_bus->parent = &pdev->dev; 340 fmb->mii_bus->read = &fixed_mdio_read; 341 fmb->mii_bus->write = &fixed_mdio_write; 342 343 ret = mdiobus_register(fmb->mii_bus); 344 if (ret) 345 goto err_mdiobus_alloc; 346 347 return 0; 348 349 err_mdiobus_alloc: 350 mdiobus_free(fmb->mii_bus); 351 err_mdiobus_reg: 352 platform_device_unregister(pdev); 353 return ret; 354 } 355 module_init(fixed_mdio_bus_init); 356 357 static void __exit fixed_mdio_bus_exit(void) 358 { 359 struct fixed_mdio_bus *fmb = &platform_fmb; 360 struct fixed_phy *fp, *tmp; 361 362 mdiobus_unregister(fmb->mii_bus); 363 mdiobus_free(fmb->mii_bus); 364 platform_device_unregister(pdev); 365 366 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { 367 list_del(&fp->node); 368 kfree(fp); 369 } 370 ida_destroy(&phy_fixed_ida); 371 } 372 module_exit(fixed_mdio_bus_exit); 373 374 MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); 375 MODULE_AUTHOR("Vitaly Bordug"); 376 MODULE_LICENSE("GPL"); 377