.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
---|
1 | 2 | /* |
---|
2 | 3 | * Fixed MDIO bus (MDIO bus emulation with fixed PHYs) |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
---|
6 | 7 | * |
---|
7 | 8 | * Copyright (c) 2006-2007 MontaVista Software, Inc. |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify it |
---|
10 | | - * under the terms of the GNU General Public License as published by the |
---|
11 | | - * Free Software Foundation; either version 2 of the License, or (at your |
---|
12 | | - * option) any later version. |
---|
13 | 9 | */ |
---|
14 | 10 | |
---|
15 | 11 | #include <linux/kernel.h> |
---|
.. | .. |
---|
22 | 18 | #include <linux/err.h> |
---|
23 | 19 | #include <linux/slab.h> |
---|
24 | 20 | #include <linux/of.h> |
---|
25 | | -#include <linux/gpio.h> |
---|
26 | | -#include <linux/seqlock.h> |
---|
| 21 | +#include <linux/gpio/consumer.h> |
---|
27 | 22 | #include <linux/idr.h> |
---|
| 23 | +#include <linux/netdevice.h> |
---|
| 24 | +#include <linux/linkmode.h> |
---|
28 | 25 | |
---|
29 | 26 | #include "swphy.h" |
---|
30 | 27 | |
---|
.. | .. |
---|
36 | 33 | struct fixed_phy { |
---|
37 | 34 | int addr; |
---|
38 | 35 | struct phy_device *phydev; |
---|
39 | | - seqcount_t seqcount; |
---|
40 | 36 | struct fixed_phy_status status; |
---|
| 37 | + bool no_carrier; |
---|
41 | 38 | int (*link_update)(struct net_device *, struct fixed_phy_status *); |
---|
42 | 39 | struct list_head node; |
---|
43 | | - int link_gpio; |
---|
| 40 | + struct gpio_desc *link_gpiod; |
---|
44 | 41 | }; |
---|
45 | 42 | |
---|
46 | 43 | static struct platform_device *pdev; |
---|
.. | .. |
---|
48 | 45 | .phys = LIST_HEAD_INIT(platform_fmb.phys), |
---|
49 | 46 | }; |
---|
50 | 47 | |
---|
| 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 | + |
---|
51 | 67 | static void fixed_phy_update(struct fixed_phy *fp) |
---|
52 | 68 | { |
---|
53 | | - if (gpio_is_valid(fp->link_gpio)) |
---|
54 | | - fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio); |
---|
| 69 | + if (!fp->no_carrier && fp->link_gpiod) |
---|
| 70 | + fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod); |
---|
55 | 71 | } |
---|
56 | 72 | |
---|
57 | 73 | static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num) |
---|
.. | .. |
---|
62 | 78 | list_for_each_entry(fp, &fmb->phys, node) { |
---|
63 | 79 | if (fp->addr == phy_addr) { |
---|
64 | 80 | struct fixed_phy_status state; |
---|
65 | | - int s; |
---|
66 | 81 | |
---|
67 | | - do { |
---|
68 | | - s = read_seqcount_begin(&fp->seqcount); |
---|
69 | | - /* Issue callback if user registered it. */ |
---|
70 | | - if (fp->link_update) |
---|
71 | | - fp->link_update(fp->phydev->attached_dev, |
---|
72 | | - &fp->status); |
---|
73 | | - /* Check the GPIO for change in status */ |
---|
74 | | - fixed_phy_update(fp); |
---|
75 | | - state = fp->status; |
---|
76 | | - } while (read_seqcount_retry(&fp->seqcount, s)); |
---|
| 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; |
---|
77 | 92 | |
---|
78 | 93 | return swphy_read_reg(reg_num, &state); |
---|
79 | 94 | } |
---|
.. | .. |
---|
115 | 130 | } |
---|
116 | 131 | EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); |
---|
117 | 132 | |
---|
118 | | -int fixed_phy_add(unsigned int irq, int phy_addr, |
---|
119 | | - struct fixed_phy_status *status, |
---|
120 | | - int link_gpio) |
---|
| 133 | +static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr, |
---|
| 134 | + struct fixed_phy_status *status, |
---|
| 135 | + struct gpio_desc *gpiod) |
---|
121 | 136 | { |
---|
122 | 137 | int ret; |
---|
123 | 138 | struct fixed_mdio_bus *fmb = &platform_fmb; |
---|
.. | .. |
---|
131 | 146 | if (!fp) |
---|
132 | 147 | return -ENOMEM; |
---|
133 | 148 | |
---|
134 | | - seqcount_init(&fp->seqcount); |
---|
135 | | - |
---|
136 | 149 | if (irq != PHY_POLL) |
---|
137 | 150 | fmb->mii_bus->irq[phy_addr] = irq; |
---|
138 | 151 | |
---|
139 | 152 | fp->addr = phy_addr; |
---|
140 | 153 | fp->status = *status; |
---|
141 | | - fp->link_gpio = link_gpio; |
---|
142 | | - |
---|
143 | | - if (gpio_is_valid(fp->link_gpio)) { |
---|
144 | | - ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN, |
---|
145 | | - "fixed-link-gpio-link"); |
---|
146 | | - if (ret) |
---|
147 | | - goto err_regs; |
---|
148 | | - } |
---|
| 154 | + fp->link_gpiod = gpiod; |
---|
149 | 155 | |
---|
150 | 156 | fixed_phy_update(fp); |
---|
151 | 157 | |
---|
152 | 158 | list_add_tail(&fp->node, &fmb->phys); |
---|
153 | 159 | |
---|
154 | 160 | return 0; |
---|
| 161 | +} |
---|
155 | 162 | |
---|
156 | | -err_regs: |
---|
157 | | - kfree(fp); |
---|
158 | | - return ret; |
---|
| 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); |
---|
159 | 167 | } |
---|
160 | 168 | EXPORT_SYMBOL_GPL(fixed_phy_add); |
---|
161 | 169 | |
---|
.. | .. |
---|
169 | 177 | list_for_each_entry_safe(fp, tmp, &fmb->phys, node) { |
---|
170 | 178 | if (fp->addr == phy_addr) { |
---|
171 | 179 | list_del(&fp->node); |
---|
172 | | - if (gpio_is_valid(fp->link_gpio)) |
---|
173 | | - gpio_free(fp->link_gpio); |
---|
| 180 | + if (fp->link_gpiod) |
---|
| 181 | + gpiod_put(fp->link_gpiod); |
---|
174 | 182 | kfree(fp); |
---|
175 | 183 | ida_simple_remove(&phy_fixed_ida, phy_addr); |
---|
176 | 184 | return; |
---|
.. | .. |
---|
178 | 186 | } |
---|
179 | 187 | } |
---|
180 | 188 | |
---|
181 | | -struct phy_device *fixed_phy_register(unsigned int irq, |
---|
182 | | - struct fixed_phy_status *status, |
---|
183 | | - int link_gpio, |
---|
184 | | - struct device_node *np) |
---|
| 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 | + */ |
---|
| 207 | + gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node), |
---|
| 208 | + "link", 0, GPIOD_IN, "mdio"); |
---|
| 209 | + if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) { |
---|
| 210 | + if (PTR_ERR(gpiod) != -ENOENT) |
---|
| 211 | + pr_err("error getting GPIO for fixed link %pOF, proceed without\n", |
---|
| 212 | + fixed_link_node); |
---|
| 213 | + gpiod = NULL; |
---|
| 214 | + } |
---|
| 215 | + of_node_put(fixed_link_node); |
---|
| 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 | + |
---|
| 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) |
---|
185 | 230 | { |
---|
186 | 231 | struct fixed_mdio_bus *fmb = &platform_fmb; |
---|
187 | 232 | struct phy_device *phy; |
---|
.. | .. |
---|
191 | 236 | if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED) |
---|
192 | 237 | return ERR_PTR(-EPROBE_DEFER); |
---|
193 | 238 | |
---|
| 239 | + /* Check if we have a GPIO associated with this fixed phy */ |
---|
| 240 | + if (!gpiod) { |
---|
| 241 | + gpiod = fixed_phy_get_gpiod(np); |
---|
| 242 | + if (IS_ERR(gpiod)) |
---|
| 243 | + return ERR_CAST(gpiod); |
---|
| 244 | + } |
---|
| 245 | + |
---|
194 | 246 | /* Get the next available PHY address, up to PHY_MAX_ADDR */ |
---|
195 | 247 | phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL); |
---|
196 | 248 | if (phy_addr < 0) |
---|
197 | 249 | return ERR_PTR(phy_addr); |
---|
198 | 250 | |
---|
199 | | - ret = fixed_phy_add(irq, phy_addr, status, link_gpio); |
---|
| 251 | + ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod); |
---|
200 | 252 | if (ret < 0) { |
---|
201 | 253 | ida_simple_remove(&phy_fixed_ida, phy_addr); |
---|
202 | 254 | return ERR_PTR(ret); |
---|
.. | .. |
---|
223 | 275 | |
---|
224 | 276 | switch (status->speed) { |
---|
225 | 277 | case SPEED_1000: |
---|
226 | | - phy->supported = PHY_1000BT_FEATURES; |
---|
227 | | - break; |
---|
| 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); |
---|
| 282 | + fallthrough; |
---|
228 | 283 | case SPEED_100: |
---|
229 | | - phy->supported = PHY_100BT_FEATURES; |
---|
230 | | - break; |
---|
| 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); |
---|
| 288 | + fallthrough; |
---|
231 | 289 | case SPEED_10: |
---|
232 | 290 | default: |
---|
233 | | - phy->supported = PHY_10BT_FEATURES; |
---|
| 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); |
---|
234 | 295 | } |
---|
| 296 | + |
---|
| 297 | + phy_advertise_supported(phy); |
---|
235 | 298 | |
---|
236 | 299 | ret = phy_device_register(phy); |
---|
237 | 300 | if (ret) { |
---|
.. | .. |
---|
243 | 306 | |
---|
244 | 307 | return phy; |
---|
245 | 308 | } |
---|
| 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 | +} |
---|
246 | 316 | EXPORT_SYMBOL_GPL(fixed_phy_register); |
---|
247 | 317 | |
---|
| 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 | + |
---|
248 | 327 | void fixed_phy_unregister(struct phy_device *phy) |
---|
249 | 328 | { |
---|
250 | 329 | phy_device_remove(phy); |
---|