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> |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 22 | #include <linux/idr.h> |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
Heiner Kallweit | 0f3b1cf | 2019-02-24 18:01:18 +0100 | [diff] [blame] | 24 | #include <linux/linkmode.h> |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 25 | |
Russell King | 5ae68b0 | 2016-06-23 14:50:05 +0100 | [diff] [blame] | 26 | #include "swphy.h" |
| 27 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 28 | struct fixed_mdio_bus { |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 29 | struct mii_bus *mii_bus; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 30 | struct list_head phys; |
| 31 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 32 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 33 | struct fixed_phy { |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 34 | int addr; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 35 | struct phy_device *phydev; |
| 36 | struct fixed_phy_status status; |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 37 | bool no_carrier; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 38 | int (*link_update)(struct net_device *, struct fixed_phy_status *); |
| 39 | struct list_head node; |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 40 | struct gpio_desc *link_gpiod; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static struct platform_device *pdev; |
| 44 | static struct fixed_mdio_bus platform_fmb = { |
| 45 | .phys = LIST_HEAD_INIT(platform_fmb.phys), |
| 46 | }; |
| 47 | |
Joakim Tjernlund | b3e5464 | 2018-12-14 15:17:05 +0100 | [diff] [blame] | 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 | |
Russell King | 37688e3 | 2016-06-23 14:50:20 +0100 | [diff] [blame] | 67 | static void fixed_phy_update(struct fixed_phy *fp) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 68 | { |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 69 | if (!fp->no_carrier && fp->link_gpiod) |
| 70 | fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 73 | 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] | 74 | { |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 75 | struct fixed_mdio_bus *fmb = bus->priv; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 76 | struct fixed_phy *fp; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 77 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 78 | list_for_each_entry(fp, &fmb->phys, node) { |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 79 | if (fp->addr == phy_addr) { |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 80 | struct fixed_phy_status state; |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 81 | |
Ahmed S. Darwish | 79cbb6b | 2020-06-03 16:49:45 +0200 | [diff] [blame] | 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; |
Russell King | bf7afb2 | 2016-06-23 14:50:25 +0100 | [diff] [blame] | 92 | |
| 93 | return swphy_read_reg(reg_num, &state); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 94 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 97 | return 0xFFFF; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 100 | 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] | 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 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 118 | if (!phydev || !phydev->mdio.bus) |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 119 | return -EINVAL; |
| 120 | |
| 121 | list_for_each_entry(fp, &fmb->phys, node) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 122 | if (fp->addr == phydev->mdio.addr) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 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 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 133 | static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr, |
| 134 | struct fixed_phy_status *status, |
| 135 | struct gpio_desc *gpiod) |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 136 | { |
| 137 | int ret; |
| 138 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 139 | struct fixed_phy *fp; |
| 140 | |
Russell King | 68888ce | 2016-06-23 14:50:15 +0100 | [diff] [blame] | 141 | ret = swphy_validate_state(status); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 145 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); |
| 146 | if (!fp) |
| 147 | return -ENOMEM; |
| 148 | |
Rabin Vincent | 185be5a | 2016-05-18 12:47:31 +0200 | [diff] [blame] | 149 | if (irq != PHY_POLL) |
| 150 | fmb->mii_bus->irq[phy_addr] = irq; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 151 | |
Thomas Petazzoni | 9b74494 | 2014-05-16 16:14:03 +0200 | [diff] [blame] | 152 | fp->addr = phy_addr; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 153 | fp->status = *status; |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 154 | fp->link_gpiod = gpiod; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 155 | |
Russell King | 37688e3 | 2016-06-23 14:50:20 +0100 | [diff] [blame] | 156 | fixed_phy_update(fp); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 157 | |
| 158 | list_add_tail(&fp->node, &fmb->phys); |
| 159 | |
| 160 | return 0; |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 161 | } |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 162 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 163 | int fixed_phy_add(unsigned int irq, int phy_addr, |
| 164 | struct fixed_phy_status *status) { |
| 165 | |
| 166 | return fixed_phy_add_gpiod(irq, phy_addr, status, NULL); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 167 | } |
| 168 | EXPORT_SYMBOL_GPL(fixed_phy_add); |
| 169 | |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 170 | static DEFINE_IDA(phy_fixed_ida); |
| 171 | |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 172 | static void fixed_phy_del(int phy_addr) |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 173 | { |
| 174 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 175 | struct fixed_phy *fp, *tmp; |
| 176 | |
| 177 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
| 178 | if (fp->addr == phy_addr) { |
| 179 | list_del(&fp->node); |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 180 | if (fp->link_gpiod) |
| 181 | gpiod_put(fp->link_gpiod); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 182 | kfree(fp); |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 183 | ida_simple_remove(&phy_fixed_ida, phy_addr); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | } |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 188 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 189 | #ifdef CONFIG_OF_GPIO |
| 190 | static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np) |
| 191 | { |
| 192 | struct device_node *fixed_link_node; |
| 193 | struct gpio_desc *gpiod; |
| 194 | |
| 195 | if (!np) |
| 196 | return NULL; |
| 197 | |
| 198 | fixed_link_node = of_get_child_by_name(np, "fixed-link"); |
| 199 | if (!fixed_link_node) |
| 200 | return NULL; |
| 201 | |
| 202 | /* |
| 203 | * As the fixed link is just a device tree node without any |
| 204 | * Linux device associated with it, we simply have obtain |
| 205 | * the GPIO descriptor from the device tree like this. |
| 206 | */ |
Dmitry Torokhov | 5ffcc85 | 2020-01-02 17:03:20 -0800 | [diff] [blame] | 207 | gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node), |
| 208 | "link", 0, GPIOD_IN, "mdio"); |
Dmitry Torokhov | d266f19 | 2020-01-02 17:03:19 -0800 | [diff] [blame] | 209 | if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) { |
Hubert Feurstein | ab98c00 | 2019-07-30 11:46:23 +0200 | [diff] [blame] | 210 | if (PTR_ERR(gpiod) != -ENOENT) |
| 211 | pr_err("error getting GPIO for fixed link %pOF, proceed without\n", |
| 212 | fixed_link_node); |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 213 | gpiod = NULL; |
| 214 | } |
Dmitry Torokhov | d266f19 | 2020-01-02 17:03:19 -0800 | [diff] [blame] | 215 | of_node_put(fixed_link_node); |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 216 | |
| 217 | return gpiod; |
| 218 | } |
| 219 | #else |
| 220 | static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np) |
| 221 | { |
| 222 | return NULL; |
| 223 | } |
| 224 | #endif |
| 225 | |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 226 | static struct phy_device *__fixed_phy_register(unsigned int irq, |
| 227 | struct fixed_phy_status *status, |
| 228 | struct device_node *np, |
| 229 | struct gpio_desc *gpiod) |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 230 | { |
| 231 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 232 | struct phy_device *phy; |
| 233 | int phy_addr; |
| 234 | int ret; |
| 235 | |
Rabin Vincent | 185be5a | 2016-05-18 12:47:31 +0200 | [diff] [blame] | 236 | if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) |
| 237 | return ERR_PTR(-EPROBE_DEFER); |
| 238 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 239 | /* Check if we have a GPIO associated with this fixed phy */ |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 240 | if (!gpiod) { |
| 241 | gpiod = fixed_phy_get_gpiod(np); |
| 242 | if (IS_ERR(gpiod)) |
| 243 | return ERR_CAST(gpiod); |
| 244 | } |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 245 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 246 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 247 | phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL); |
| 248 | if (phy_addr < 0) |
| 249 | return ERR_PTR(phy_addr); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 250 | |
Linus Walleij | 5468e82 | 2019-02-04 11:26:18 +0100 | [diff] [blame] | 251 | ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod); |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 252 | if (ret < 0) { |
| 253 | ida_simple_remove(&phy_fixed_ida, phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 254 | return ERR_PTR(ret); |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 255 | } |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 256 | |
| 257 | phy = get_phy_device(fmb->mii_bus, phy_addr, false); |
Sergei Shtylyov | 4914a58 | 2016-04-24 20:29:23 +0300 | [diff] [blame] | 258 | if (IS_ERR(phy)) { |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 259 | fixed_phy_del(phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 260 | return ERR_PTR(-EINVAL); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Madalin Bucur | 4b19536 | 2015-08-26 17:58:47 +0300 | [diff] [blame] | 263 | /* propagate the fixed link values to struct phy_device */ |
| 264 | phy->link = status->link; |
| 265 | if (status->link) { |
| 266 | phy->speed = status->speed; |
| 267 | phy->duplex = status->duplex; |
| 268 | phy->pause = status->pause; |
| 269 | phy->asym_pause = status->asym_pause; |
| 270 | } |
| 271 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 272 | of_node_get(np); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 273 | phy->mdio.dev.of_node = np; |
Florian Fainelli | 5a11dd7 | 2015-08-31 15:56:46 +0200 | [diff] [blame] | 274 | phy->is_pseudo_fixed_link = true; |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 275 | |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 276 | switch (status->speed) { |
| 277 | case SPEED_1000: |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 278 | linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 279 | phy->supported); |
| 280 | linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 281 | phy->supported); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 282 | fallthrough; |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 283 | case SPEED_100: |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 284 | linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 285 | phy->supported); |
| 286 | linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 287 | phy->supported); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 288 | fallthrough; |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 289 | case SPEED_10: |
| 290 | default: |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 291 | linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 292 | phy->supported); |
| 293 | linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 294 | phy->supported); |
Andrew Lunn | 34b31da4 | 2015-08-31 15:56:48 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Heiner Kallweit | 22c0ef6 | 2019-05-01 21:34:43 +0200 | [diff] [blame] | 297 | phy_advertise_supported(phy); |
Heiner Kallweit | 0f3b1cf | 2019-02-24 18:01:18 +0100 | [diff] [blame] | 298 | |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 299 | ret = phy_device_register(phy); |
| 300 | if (ret) { |
| 301 | phy_device_free(phy); |
| 302 | of_node_put(np); |
| 303 | fixed_phy_del(phy_addr); |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 304 | return ERR_PTR(ret); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Petri Gynther | fd2ef0b | 2014-10-06 11:38:30 -0700 | [diff] [blame] | 307 | return phy; |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 308 | } |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 309 | |
| 310 | struct phy_device *fixed_phy_register(unsigned int irq, |
| 311 | struct fixed_phy_status *status, |
| 312 | struct device_node *np) |
| 313 | { |
| 314 | return __fixed_phy_register(irq, status, np, NULL); |
| 315 | } |
Mark Salter | 37e9a69 | 2014-12-11 23:03:26 -0500 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
Thomas Petazzoni | a759512 | 2014-05-16 16:14:04 +0200 | [diff] [blame] | 317 | |
Moritz Fischer | 71bd106 | 2019-02-07 09:52:10 -0800 | [diff] [blame] | 318 | struct phy_device * |
| 319 | fixed_phy_register_with_gpiod(unsigned int irq, |
| 320 | struct fixed_phy_status *status, |
| 321 | struct gpio_desc *gpiod) |
| 322 | { |
| 323 | return __fixed_phy_register(irq, status, NULL, gpiod); |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod); |
| 326 | |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 327 | void fixed_phy_unregister(struct phy_device *phy) |
| 328 | { |
| 329 | phy_device_remove(phy); |
Johan Hovold | 13c9d93 | 2016-11-16 15:20:38 +0100 | [diff] [blame] | 330 | of_node_put(phy->mdio.dev.of_node); |
Andrew Lunn | 5bcbe0f | 2016-03-12 00:01:40 +0100 | [diff] [blame] | 331 | fixed_phy_del(phy->mdio.addr); |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(fixed_phy_unregister); |
| 334 | |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 335 | static int __init fixed_mdio_bus_init(void) |
| 336 | { |
| 337 | struct fixed_mdio_bus *fmb = &platform_fmb; |
| 338 | int ret; |
| 339 | |
| 340 | pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0); |
Fabio Estevam | b0c1638 | 2018-06-23 21:28:22 -0300 | [diff] [blame] | 341 | if (IS_ERR(pdev)) |
| 342 | return PTR_ERR(pdev); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 343 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 344 | fmb->mii_bus = mdiobus_alloc(); |
| 345 | if (fmb->mii_bus == NULL) { |
| 346 | ret = -ENOMEM; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 347 | goto err_mdiobus_reg; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Florian Fainelli | 9e6c643 | 2012-01-09 23:59:25 +0000 | [diff] [blame] | 350 | snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0"); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 351 | fmb->mii_bus->name = "Fixed MDIO Bus"; |
Lennert Buytenhek | ec2a565 | 2008-10-09 09:45:04 -0700 | [diff] [blame] | 352 | fmb->mii_bus->priv = fmb; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 353 | fmb->mii_bus->parent = &pdev->dev; |
| 354 | fmb->mii_bus->read = &fixed_mdio_read; |
| 355 | fmb->mii_bus->write = &fixed_mdio_write; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 356 | |
| 357 | ret = mdiobus_register(fmb->mii_bus); |
| 358 | if (ret) |
| 359 | goto err_mdiobus_alloc; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 363 | err_mdiobus_alloc: |
| 364 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 365 | err_mdiobus_reg: |
| 366 | platform_device_unregister(pdev); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 367 | return ret; |
| 368 | } |
| 369 | module_init(fixed_mdio_bus_init); |
| 370 | |
| 371 | static void __exit fixed_mdio_bus_exit(void) |
| 372 | { |
| 373 | struct fixed_mdio_bus *fmb = &platform_fmb; |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 374 | struct fixed_phy *fp, *tmp; |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 375 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 376 | mdiobus_unregister(fmb->mii_bus); |
| 377 | mdiobus_free(fmb->mii_bus); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 378 | platform_device_unregister(pdev); |
| 379 | |
Adrian Bunk | 651be3a | 2008-02-02 23:15:02 +0200 | [diff] [blame] | 380 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 381 | list_del(&fp->node); |
| 382 | kfree(fp); |
| 383 | } |
Florian Fainelli | 69fc58a | 2016-06-24 16:25:24 -0700 | [diff] [blame] | 384 | ida_destroy(&phy_fixed_ida); |
Vitaly Bordug | a79d8e9 | 2007-12-07 01:51:22 +0300 | [diff] [blame] | 385 | } |
| 386 | module_exit(fixed_mdio_bus_exit); |
| 387 | |
| 388 | MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)"); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 389 | MODULE_AUTHOR("Vitaly Bordug"); |
| 390 | MODULE_LICENSE("GPL"); |