hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/drivers/net/phy/fixed_phy.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0+
12 /*
23 * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
34 *
....@@ -5,11 +6,6 @@
56 * Anton Vorontsov <avorontsov@ru.mvista.com>
67 *
78 * 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.
139 */
1410
1511 #include <linux/kernel.h>
....@@ -22,9 +18,10 @@
2218 #include <linux/err.h>
2319 #include <linux/slab.h>
2420 #include <linux/of.h>
25
-#include <linux/gpio.h>
26
-#include <linux/seqlock.h>
21
+#include <linux/gpio/consumer.h>
2722 #include <linux/idr.h>
23
+#include <linux/netdevice.h>
24
+#include <linux/linkmode.h>
2825
2926 #include "swphy.h"
3027
....@@ -36,11 +33,11 @@
3633 struct fixed_phy {
3734 int addr;
3835 struct phy_device *phydev;
39
- seqcount_t seqcount;
4036 struct fixed_phy_status status;
37
+ bool no_carrier;
4138 int (*link_update)(struct net_device *, struct fixed_phy_status *);
4239 struct list_head node;
43
- int link_gpio;
40
+ struct gpio_desc *link_gpiod;
4441 };
4542
4643 static struct platform_device *pdev;
....@@ -48,10 +45,29 @@
4845 .phys = LIST_HEAD_INIT(platform_fmb.phys),
4946 };
5047
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
+
5167 static void fixed_phy_update(struct fixed_phy *fp)
5268 {
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);
5571 }
5672
5773 static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
....@@ -62,18 +78,17 @@
6278 list_for_each_entry(fp, &fmb->phys, node) {
6379 if (fp->addr == phy_addr) {
6480 struct fixed_phy_status state;
65
- int s;
6681
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;
7792
7893 return swphy_read_reg(reg_num, &state);
7994 }
....@@ -115,9 +130,9 @@
115130 }
116131 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
117132
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)
121136 {
122137 int ret;
123138 struct fixed_mdio_bus *fmb = &platform_fmb;
....@@ -131,31 +146,24 @@
131146 if (!fp)
132147 return -ENOMEM;
133148
134
- seqcount_init(&fp->seqcount);
135
-
136149 if (irq != PHY_POLL)
137150 fmb->mii_bus->irq[phy_addr] = irq;
138151
139152 fp->addr = phy_addr;
140153 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;
149155
150156 fixed_phy_update(fp);
151157
152158 list_add_tail(&fp->node, &fmb->phys);
153159
154160 return 0;
161
+}
155162
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);
159167 }
160168 EXPORT_SYMBOL_GPL(fixed_phy_add);
161169
....@@ -169,8 +177,8 @@
169177 list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
170178 if (fp->addr == phy_addr) {
171179 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);
174182 kfree(fp);
175183 ida_simple_remove(&phy_fixed_ida, phy_addr);
176184 return;
....@@ -178,10 +186,47 @@
178186 }
179187 }
180188
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)
185230 {
186231 struct fixed_mdio_bus *fmb = &platform_fmb;
187232 struct phy_device *phy;
....@@ -191,12 +236,19 @@
191236 if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
192237 return ERR_PTR(-EPROBE_DEFER);
193238
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
+
194246 /* Get the next available PHY address, up to PHY_MAX_ADDR */
195247 phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
196248 if (phy_addr < 0)
197249 return ERR_PTR(phy_addr);
198250
199
- ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
251
+ ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
200252 if (ret < 0) {
201253 ida_simple_remove(&phy_fixed_ida, phy_addr);
202254 return ERR_PTR(ret);
....@@ -223,15 +275,26 @@
223275
224276 switch (status->speed) {
225277 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;
228283 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;
231289 case SPEED_10:
232290 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);
234295 }
296
+
297
+ phy_advertise_supported(phy);
235298
236299 ret = phy_device_register(phy);
237300 if (ret) {
....@@ -243,8 +306,24 @@
243306
244307 return phy;
245308 }
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
+}
246316 EXPORT_SYMBOL_GPL(fixed_phy_register);
247317
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
+
248327 void fixed_phy_unregister(struct phy_device *phy)
249328 {
250329 phy_device_remove(phy);