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