Andrew Lunn | a2443fd | 2019-01-21 19:05:50 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 2 | /* |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 3 | * Fixed MDIO bus (MDIO bus emulation with fixed PHYs) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 4 | * |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 5 | * Author: Vitaly Bordug <vbordug@ru.mvista.com> |
| 6 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 7 | * |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 8 | * Copyright (c) 2006-2007 MontaVista Software, Inc. |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 9 | */ |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 10 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/list.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 15 | #include <linux/mii.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 16 | #include <linux/phy.h> |
Vitaly Bordug | 7c32f47 | 2007-08-10 14:05:16 -0700 | [diff] [blame] | 17 | #include <linux/phy_fixed.h> |
Dan Carpenter | 57401d5 | 2009-04-11 01:52:29 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 20 | #include <linux/of.h> |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 21 | #include <linux/gpio/consumer.h> |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 22 | #include <linux/seqlock.h> |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 23 | #include <linux/idr.h> |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 24 | #include <linux/netdevice.h> |
Heiner Kallweit | 0f3b1cf | 2019-02-24 18:01:18 +0100 | [diff] [blame^] | 25 | #include <linux/linkmode.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 26 | |
Russell King | 5ae68b0 | 2016-06-23 14:50:05 +0100 | [diff] [blame] | 27 | #include "swphy.h" |
| 28 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 29 | struct fixed_mdio_bus { |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 30 | struct mii_bus *mii_bus; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 31 | struct list_head phys; |
| 32 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 33 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 34 | struct fixed_phy { |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 35 | int addr; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 36 | struct phy_device *phydev; |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 37 | seqcount_t seqcount; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 38 | struct fixed_phy_status status; |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 39 | bool no_carrier; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 40 | int (*link_update)(struct net_device *, struct fixed_phy_status *); |
| 41 | struct list_head node; |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 42 | struct gpio_desc *link_gpiod; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static struct platform_device *pdev; |
| 46 | static struct fixed_mdio_bus platform_fmb = { |
| 47 | .phys = LIST_HEAD_INIT(platform_fmb.phys), |
| 48 | }; |
| 49 | |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 50 | int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier) |
| 51 | { |
| 52 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 53 | struct phy_device *phydev = dev->phydev; |
| 54 | struct fixed_phy *fp; |
| 55 | |
| 56 | if (!phydev || !phydev->mdio.bus) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | list_for_each_entry(fp, &fmb->phys, node) { |
| 60 | if (fp->addr == phydev->mdio.addr) { |
| 61 | fp->no_carrier = !new_carrier; |
| 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(fixed_phy_change_carrier); |
| 68 | |
Russell King | 37688e3 | 2016-06-23 14:50:20 +0100 | [diff] [blame] | 69 | static void fixed_phy_update(struct fixed_phy *fp) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 70 | { |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 71 | if (!fp->no_carrier && fp->link_gpiod) |
| 72 | fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 75 | static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 76 | { |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 77 | struct fixed_mdio_bus *fmb = bus->priv; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 78 | struct fixed_phy *fp; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 79 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 80 | list_for_each_entry(fp, &fmb->phys, node) { |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 81 | if (fp->addr == phy_addr) { |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 82 | struct fixed_phy_status state; |
| 83 | int s; |
| 84 | |
| 85 | do { |
| 86 | s = read_seqcount_begin(&fp->seqcount); |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 87 | fp->status.link = !fp->no_carrier; |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 88 | /* Issue callback if user registered it. */ |
Moritz Fischer | 8f28980 | 2019-02-06 21:45:29 -0800 | [diff] [blame] | 89 | if (fp->link_update) |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 90 | fp->link_update(fp->phydev->attached_dev, |
| 91 | &fp->status); |
Moritz Fischer | 8f28980 | 2019-02-06 21:45:29 -0800 | [diff] [blame] | 92 | /* Check the GPIO for change in status */ |
| 93 | fixed_phy_update(fp); |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 94 | state = fp->status; |
| 95 | } while (read_seqcount_retry(&fp->seqcount, s)); |
| 96 | |
| 97 | return swphy_read_reg(reg_num, &state); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 98 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 101 | return 0xFFFF; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 104 | static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num, |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 105 | u16 val) |
| 106 | { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * If something weird is required to be done with link/speed, |
| 112 | * network driver is able to assign a function to implement this. |
| 113 | * May be useful for PHY's that need to be software-driven. |
| 114 | */ |
| 115 | int fixed_phy_set_link_update(struct phy_device *phydev, |
| 116 | int (*link_update)(struct net_device *, |
| 117 | struct fixed_phy_status *)) |
| 118 | { |
| 119 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 120 | struct fixed_phy *fp; |
| 121 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 122 | if (!phydev || !phydev->mdio.bus) |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | |
| 125 | list_for_each_entry(fp, &fmb->phys, node) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 126 | if (fp->addr == phydev->mdio.addr) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 127 | fp->link_update = link_update; |
| 128 | fp->phydev = phydev; |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return -ENOENT; |
| 134 | } |
| 135 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
| 136 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 137 | static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr, |
| 138 | struct fixed_phy_status *status, |
| 139 | struct gpio_desc *gpiod) |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 140 | { |
| 141 | int ret; |
| 142 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 143 | struct fixed_phy *fp; |
| 144 | |
Russell King | 68888ce | 2016-06-23 14:50:15 +0100 | [diff] [blame] | 145 | ret = swphy_validate_state(status); |
| 146 | if (ret < 0) |
| 147 | return ret; |
| 148 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 149 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
| 150 | if (!fp) |
| 151 | return -ENOMEM; |
| 152 | |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 153 | seqcount_init(&fp->seqcount); |
| 154 | |
Rabin Vincent | 185be5a | 2016-05-18 12:47:31 +0200 | [diff] [blame] | 155 | if (irq != PHY_POLL) |
| 156 | fmb->mii_bus->irq[phy_addr] = irq; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 157 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 158 | fp->addr = phy_addr; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 159 | fp->status = *status; |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 160 | fp->link_gpiod = gpiod; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 161 | |
Russell King | 37688e3 | 2016-06-23 14:50:20 +0100 | [diff] [blame] | 162 | fixed_phy_update(fp); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 163 | |
| 164 | list_add_tail(&fp->node, &fmb->phys); |
| 165 | |
| 166 | return 0; |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 167 | } |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 168 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 169 | int fixed_phy_add(unsigned int irq, int phy_addr, |
| 170 | struct fixed_phy_status *status) { |
| 171 | |
| 172 | return fixed_phy_add_gpiod(irq, phy_addr, status, NULL); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 173 | } |
| 174 | EXPORT_SYMBOL_GPL(fixed_phy_add); |
| 175 | |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 176 | static DEFINE_IDA(phy_fixed_ida); |
| 177 | |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 178 | static void fixed_phy_del(int phy_addr) |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 179 | { |
| 180 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 181 | struct fixed_phy *fp, *tmp; |
| 182 | |
| 183 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
| 184 | if (fp->addr == phy_addr) { |
| 185 | list_del(&fp->node); |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 186 | if (fp->link_gpiod) |
| 187 | gpiod_put(fp->link_gpiod); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 188 | kfree(fp); |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 189 | ida_simple_remove(&phy_fixed_ida, phy_addr); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 190 | return; |
| 191 | } |
| 192 | } |
| 193 | } |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 194 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 195 | #ifdef CONFIG_OF_GPIO |
| 196 | static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np) |
| 197 | { |
| 198 | struct device_node *fixed_link_node; |
| 199 | struct gpio_desc *gpiod; |
| 200 | |
| 201 | if (!np) |
| 202 | return NULL; |
| 203 | |
| 204 | fixed_link_node = of_get_child_by_name(np, "fixed-link"); |
| 205 | if (!fixed_link_node) |
| 206 | return NULL; |
| 207 | |
| 208 | /* |
| 209 | * As the fixed link is just a device tree node without any |
| 210 | * Linux device associated with it, we simply have obtain |
| 211 | * the GPIO descriptor from the device tree like this. |
| 212 | */ |
| 213 | gpiod = gpiod_get_from_of_node(fixed_link_node, "link-gpios", 0, |
| 214 | GPIOD_IN, "mdio"); |
| 215 | of_node_put(fixed_link_node); |
| 216 | if (IS_ERR(gpiod)) { |
| 217 | if (PTR_ERR(gpiod) == -EPROBE_DEFER) |
| 218 | return gpiod; |
| 219 | pr_err("error getting GPIO for fixed link %pOF, proceed without\n", |
| 220 | fixed_link_node); |
| 221 | gpiod = NULL; |
| 222 | } |
| 223 | |
| 224 | return gpiod; |
| 225 | } |
| 226 | #else |
| 227 | static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np) |
| 228 | { |
| 229 | return NULL; |
| 230 | } |
| 231 | #endif |
| 232 | |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 233 | static struct phy_device *__fixed_phy_register(unsigned int irq, |
| 234 | struct fixed_phy_status *status, |
| 235 | struct device_node *np, |
| 236 | struct gpio_desc *gpiod) |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 237 | { |
| 238 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 239 | struct phy_device *phy; |
| 240 | int phy_addr; |
| 241 | int ret; |
| 242 | |
Rabin Vincent | 185be5a | 2016-05-18 12:47:31 +0200 | [diff] [blame] | 243 | if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) |
| 244 | return ERR_PTR(-EPROBE_DEFER); |
| 245 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 246 | /* Check if we have a GPIO associated with this fixed phy */ |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 247 | if (!gpiod) { |
| 248 | gpiod = fixed_phy_get_gpiod(np); |
| 249 | if (IS_ERR(gpiod)) |
| 250 | return ERR_CAST(gpiod); |
| 251 | } |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 252 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 253 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 254 | phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL); |
| 255 | if (phy_addr < 0) |
| 256 | return ERR_PTR(phy_addr); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 257 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 258 | ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod); |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 259 | if (ret < 0) { |
| 260 | ida_simple_remove(&phy_fixed_ida, phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 261 | return ERR_PTR(ret); |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 262 | } |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 263 | |
| 264 | phy = get_phy_device(fmb->mii_bus, phy_addr, false); |
Sergei Shtylyov | 4914a58 | 2016-04-24 20:29:23 +0300 | [diff] [blame] | 265 | if (IS_ERR(phy)) { |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 266 | fixed_phy_del(phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 267 | return ERR_PTR(-EINVAL); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Madalin Bucur | 4b19536 | 2015-08-26 17:58:47 +0300 | [diff] [blame] | 270 | /* propagate the fixed link values to struct phy_device */ |
| 271 | phy->link = status->link; |
| 272 | if (status->link) { |
| 273 | phy->speed = status->speed; |
| 274 | phy->duplex = status->duplex; |
| 275 | phy->pause = status->pause; |
| 276 | phy->asym_pause = status->asym_pause; |
| 277 | } |
| 278 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 279 | of_node_get(np); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 280 | phy->mdio.dev.of_node = np; |
Florian Fainelli | 5a11dd7 | 2015-08-31 15:56:46 +0200 | [diff] [blame] | 281 | phy->is_pseudo_fixed_link = true; |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 282 | |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 283 | switch (status->speed) { |
| 284 | case SPEED_1000: |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 285 | linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 286 | phy->supported); |
| 287 | linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 288 | phy->supported); |
| 289 | /* fall through */ |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 290 | case SPEED_100: |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 291 | linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 292 | phy->supported); |
| 293 | linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 294 | phy->supported); |
| 295 | /* fall through */ |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 296 | case SPEED_10: |
| 297 | default: |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 298 | linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 299 | phy->supported); |
| 300 | linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 301 | phy->supported); |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Heiner Kallweit | 0f3b1cf | 2019-02-24 18:01:18 +0100 | [diff] [blame^] | 304 | linkmode_copy(phy->advertising, phy->supported); |
| 305 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 306 | ret = phy_device_register(phy); |
| 307 | if (ret) { |
| 308 | phy_device_free(phy); |
| 309 | of_node_put(np); |
| 310 | fixed_phy_del(phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 311 | return ERR_PTR(ret); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 314 | return phy; |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 315 | } |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 316 | |
| 317 | struct phy_device *fixed_phy_register(unsigned int irq, |
| 318 | struct fixed_phy_status *status, |
| 319 | struct device_node *np) |
| 320 | { |
| 321 | return __fixed_phy_register(irq, status, np, NULL); |
| 322 | } |
Mark Salter | 37e9a69 | 2014-12-11 23:03:26 -0500 | [diff] [blame] | 323 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 324 | |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 325 | struct phy_device * |
| 326 | fixed_phy_register_with_gpiod(unsigned int irq, |
| 327 | struct fixed_phy_status *status, |
| 328 | struct gpio_desc *gpiod) |
| 329 | { |
| 330 | return __fixed_phy_register(irq, status, NULL, gpiod); |
| 331 | } |
| 332 | EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod); |
| 333 | |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 334 | void fixed_phy_unregister(struct phy_device *phy) |
| 335 | { |
| 336 | phy_device_remove(phy); |
Johan Hovold | 13c9d93 | 2016-11-16 15:20:38 +0100 | [diff] [blame] | 337 | of_node_put(phy->mdio.dev.of_node); |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 338 | fixed_phy_del(phy->mdio.addr); |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(fixed_phy_unregister); |
| 341 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 342 | static int __init fixed_mdio_bus_init(void) |
| 343 | { |
| 344 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 345 | int ret; |
| 346 | |
| 347 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
Fabio Estevam | b0c1638 | 2018-06-23 21:28:22 -0300 | [diff] [blame] | 348 | if (IS_ERR(pdev)) |
| 349 | return PTR_ERR(pdev); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 350 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 351 | fmb->mii_bus = mdiobus_alloc(); |
| 352 | if (fmb->mii_bus == NULL) { |
| 353 | ret = -ENOMEM; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 354 | goto err_mdiobus_reg; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Florian Fainelli | 9e6c643 | 2012-01-09 23:59:25 +0000 | [diff] [blame] | 357 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 358 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 359 | fmb->mii_bus->priv = fmb; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 360 | fmb->mii_bus->parent = &pdev->dev; |
| 361 | fmb->mii_bus->read = &fixed_mdio_read; |
| 362 | fmb->mii_bus->write = &fixed_mdio_write; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 363 | |
| 364 | ret = mdiobus_register(fmb->mii_bus); |
| 365 | if (ret) |
| 366 | goto err_mdiobus_alloc; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 367 | |
| 368 | return 0; |
| 369 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 370 | err_mdiobus_alloc: |
| 371 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 372 | err_mdiobus_reg: |
| 373 | platform_device_unregister(pdev); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 374 | return ret; |
| 375 | } |
| 376 | module_init(fixed_mdio_bus_init); |
| 377 | |
| 378 | static void __exit fixed_mdio_bus_exit(void) |
| 379 | { |
| 380 | struct fixed_mdio_bus *fmb = &platform_fmb; |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 381 | struct fixed_phy *fp, *tmp; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 382 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 383 | mdiobus_unregister(fmb->mii_bus); |
| 384 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 385 | platform_device_unregister(pdev); |
| 386 | |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 387 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 388 | list_del(&fp->node); |
| 389 | kfree(fp); |
| 390 | } |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 391 | ida_destroy(&phy_fixed_ida); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 392 | } |
| 393 | module_exit(fixed_mdio_bus_exit); |
| 394 | |
| 395 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 396 | MODULE_AUTHOR("Vitaly Bordug"); |
| 397 | MODULE_LICENSE("GPL"); |