Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-block.git] / drivers / net / phy / smsc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * drivers/net/phy/smsc.c
4  *
5  * Driver for SMSC PHYs
6  *
7  * Author: Herbert Valerio Riedel
8  *
9  * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org>
10  *
11  * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
12  *
13  */
14
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mii.h>
19 #include <linux/ethtool.h>
20 #include <linux/of.h>
21 #include <linux/phy.h>
22 #include <linux/netdevice.h>
23 #include <linux/smscphy.h>
24
25 /* Vendor-specific PHY Definitions */
26 /* EDPD NLP / crossover time configuration */
27 #define PHY_EDPD_CONFIG                 16
28 #define PHY_EDPD_CONFIG_EXT_CROSSOVER_  0x0001
29
30 /* Control/Status Indication Register */
31 #define SPECIAL_CTRL_STS                27
32 #define SPECIAL_CTRL_STS_OVRRD_AMDIX_   0x8000
33 #define SPECIAL_CTRL_STS_AMDIX_ENABLE_  0x4000
34 #define SPECIAL_CTRL_STS_AMDIX_STATE_   0x2000
35
36 struct smsc_hw_stat {
37         const char *string;
38         u8 reg;
39         u8 bits;
40 };
41
42 static struct smsc_hw_stat smsc_hw_stats[] = {
43         { "phy_symbol_errors", 26, 16},
44 };
45
46 struct smsc_phy_priv {
47         bool energy_enable;
48 };
49
50 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
51 {
52         int rc = phy_read(phydev, MII_LAN83C185_ISF);
53
54         return rc < 0 ? rc : 0;
55 }
56
57 static int smsc_phy_config_intr(struct phy_device *phydev)
58 {
59         int rc;
60
61         if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
62                 rc = smsc_phy_ack_interrupt(phydev);
63                 if (rc)
64                         return rc;
65
66                 rc = phy_write(phydev, MII_LAN83C185_IM,
67                                MII_LAN83C185_ISF_INT_PHYLIB_EVENTS);
68         } else {
69                 rc = phy_write(phydev, MII_LAN83C185_IM, 0);
70                 if (rc)
71                         return rc;
72
73                 rc = smsc_phy_ack_interrupt(phydev);
74         }
75
76         return rc < 0 ? rc : 0;
77 }
78
79 static irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
80 {
81         int irq_status;
82
83         irq_status = phy_read(phydev, MII_LAN83C185_ISF);
84         if (irq_status < 0) {
85                 if (irq_status != -ENODEV)
86                         phy_error(phydev);
87
88                 return IRQ_NONE;
89         }
90
91         if (!(irq_status & MII_LAN83C185_ISF_INT_PHYLIB_EVENTS))
92                 return IRQ_NONE;
93
94         phy_trigger_machine(phydev);
95
96         return IRQ_HANDLED;
97 }
98
99 static int smsc_phy_config_init(struct phy_device *phydev)
100 {
101         struct smsc_phy_priv *priv = phydev->priv;
102         int rc;
103
104         if (!priv->energy_enable || phydev->irq != PHY_POLL)
105                 return 0;
106
107         rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
108
109         if (rc < 0)
110                 return rc;
111
112         /* Enable energy detect mode for this SMSC Transceivers */
113         rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
114                        rc | MII_LAN83C185_EDPWRDOWN);
115         return rc;
116 }
117
118 static int smsc_phy_reset(struct phy_device *phydev)
119 {
120         int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
121         if (rc < 0)
122                 return rc;
123
124         /* If the SMSC PHY is in power down mode, then set it
125          * in all capable mode before using it.
126          */
127         if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
128                 /* set "all capable" mode */
129                 rc |= MII_LAN83C185_MODE_ALL;
130                 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
131         }
132
133         /* reset the phy */
134         return genphy_soft_reset(phydev);
135 }
136
137 static int lan87xx_config_aneg(struct phy_device *phydev)
138 {
139         int rc;
140         int val;
141
142         switch (phydev->mdix_ctrl) {
143         case ETH_TP_MDI:
144                 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
145                 break;
146         case ETH_TP_MDI_X:
147                 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
148                         SPECIAL_CTRL_STS_AMDIX_STATE_;
149                 break;
150         case ETH_TP_MDI_AUTO:
151                 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
152                 break;
153         default:
154                 return genphy_config_aneg(phydev);
155         }
156
157         rc = phy_read(phydev, SPECIAL_CTRL_STS);
158         if (rc < 0)
159                 return rc;
160
161         rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
162                 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
163                 SPECIAL_CTRL_STS_AMDIX_STATE_);
164         rc |= val;
165         phy_write(phydev, SPECIAL_CTRL_STS, rc);
166
167         phydev->mdix = phydev->mdix_ctrl;
168         return genphy_config_aneg(phydev);
169 }
170
171 static int lan95xx_config_aneg_ext(struct phy_device *phydev)
172 {
173         if (phydev->phy_id == 0x0007c0f0) { /* LAN9500A or LAN9505A */
174                 /* Extend Manual AutoMDIX timer */
175                 int rc = phy_set_bits(phydev, PHY_EDPD_CONFIG,
176                                       PHY_EDPD_CONFIG_EXT_CROSSOVER_);
177
178                 if (rc < 0)
179                         return rc;
180         }
181
182         return lan87xx_config_aneg(phydev);
183 }
184
185 /*
186  * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
187  * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
188  * unstable detection of plugging in Ethernet cable.
189  * This workaround disables Energy Detect Power-Down mode and waiting for
190  * response on link pulses to detect presence of plugged Ethernet cable.
191  * The Energy Detect Power-Down mode is enabled again in the end of procedure to
192  * save approximately 220 mW of power if cable is unplugged.
193  * The workaround is only applicable to poll mode. Energy Detect Power-Down may
194  * not be used in interrupt mode lest link change detection becomes unreliable.
195  */
196 static int lan87xx_read_status(struct phy_device *phydev)
197 {
198         struct smsc_phy_priv *priv = phydev->priv;
199
200         int err = genphy_read_status(phydev);
201
202         if (!phydev->link && priv->energy_enable && phydev->irq == PHY_POLL) {
203                 /* Disable EDPD to wake up PHY */
204                 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
205                 if (rc < 0)
206                         return rc;
207
208                 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
209                                rc & ~MII_LAN83C185_EDPWRDOWN);
210                 if (rc < 0)
211                         return rc;
212
213                 /* Wait max 640 ms to detect energy and the timeout is not
214                  * an actual error.
215                  */
216                 read_poll_timeout(phy_read, rc,
217                                   rc & MII_LAN83C185_ENERGYON || rc < 0,
218                                   10000, 640000, true, phydev,
219                                   MII_LAN83C185_CTRL_STATUS);
220                 if (rc < 0)
221                         return rc;
222
223                 /* Re-enable EDPD */
224                 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
225                 if (rc < 0)
226                         return rc;
227
228                 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
229                                rc | MII_LAN83C185_EDPWRDOWN);
230                 if (rc < 0)
231                         return rc;
232         }
233
234         return err;
235 }
236
237 static int smsc_get_sset_count(struct phy_device *phydev)
238 {
239         return ARRAY_SIZE(smsc_hw_stats);
240 }
241
242 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
243 {
244         int i;
245
246         for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
247                 strncpy(data + i * ETH_GSTRING_LEN,
248                        smsc_hw_stats[i].string, ETH_GSTRING_LEN);
249         }
250 }
251
252 static u64 smsc_get_stat(struct phy_device *phydev, int i)
253 {
254         struct smsc_hw_stat stat = smsc_hw_stats[i];
255         int val;
256         u64 ret;
257
258         val = phy_read(phydev, stat.reg);
259         if (val < 0)
260                 ret = U64_MAX;
261         else
262                 ret = val;
263
264         return ret;
265 }
266
267 static void smsc_get_stats(struct phy_device *phydev,
268                            struct ethtool_stats *stats, u64 *data)
269 {
270         int i;
271
272         for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
273                 data[i] = smsc_get_stat(phydev, i);
274 }
275
276 static int smsc_phy_probe(struct phy_device *phydev)
277 {
278         struct device *dev = &phydev->mdio.dev;
279         struct device_node *of_node = dev->of_node;
280         struct smsc_phy_priv *priv;
281         struct clk *refclk;
282
283         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
284         if (!priv)
285                 return -ENOMEM;
286
287         priv->energy_enable = true;
288
289         if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
290                 priv->energy_enable = false;
291
292         phydev->priv = priv;
293
294         /* Make clk optional to keep DTB backward compatibility. */
295         refclk = devm_clk_get_optional_enabled(dev, NULL);
296         if (IS_ERR(refclk))
297                 return dev_err_probe(dev, PTR_ERR(refclk),
298                                      "Failed to request clock\n");
299
300         return clk_set_rate(refclk, 50 * 1000 * 1000);
301 }
302
303 static struct phy_driver smsc_phy_driver[] = {
304 {
305         .phy_id         = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
306         .phy_id_mask    = 0xfffffff0,
307         .name           = "SMSC LAN83C185",
308
309         /* PHY_BASIC_FEATURES */
310
311         .probe          = smsc_phy_probe,
312
313         /* basic functions */
314         .config_init    = smsc_phy_config_init,
315         .soft_reset     = smsc_phy_reset,
316
317         /* IRQ related */
318         .config_intr    = smsc_phy_config_intr,
319         .handle_interrupt = smsc_phy_handle_interrupt,
320
321         .suspend        = genphy_suspend,
322         .resume         = genphy_resume,
323 }, {
324         .phy_id         = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
325         .phy_id_mask    = 0xfffffff0,
326         .name           = "SMSC LAN8187",
327
328         /* PHY_BASIC_FEATURES */
329
330         .probe          = smsc_phy_probe,
331
332         /* basic functions */
333         .config_init    = smsc_phy_config_init,
334         .soft_reset     = smsc_phy_reset,
335
336         /* IRQ related */
337         .config_intr    = smsc_phy_config_intr,
338         .handle_interrupt = smsc_phy_handle_interrupt,
339
340         /* Statistics */
341         .get_sset_count = smsc_get_sset_count,
342         .get_strings    = smsc_get_strings,
343         .get_stats      = smsc_get_stats,
344
345         .suspend        = genphy_suspend,
346         .resume         = genphy_resume,
347 }, {
348         /* This covers internal PHY (phy_id: 0x0007C0C3) for
349          * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
350          */
351         .phy_id         = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
352         .phy_id_mask    = 0xfffffff0,
353         .name           = "SMSC LAN8700",
354
355         /* PHY_BASIC_FEATURES */
356
357         .probe          = smsc_phy_probe,
358
359         /* basic functions */
360         .read_status    = lan87xx_read_status,
361         .config_init    = smsc_phy_config_init,
362         .soft_reset     = smsc_phy_reset,
363         .config_aneg    = lan87xx_config_aneg,
364
365         /* IRQ related */
366         .config_intr    = smsc_phy_config_intr,
367         .handle_interrupt = smsc_phy_handle_interrupt,
368
369         /* Statistics */
370         .get_sset_count = smsc_get_sset_count,
371         .get_strings    = smsc_get_strings,
372         .get_stats      = smsc_get_stats,
373
374         .suspend        = genphy_suspend,
375         .resume         = genphy_resume,
376 }, {
377         .phy_id         = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
378         .phy_id_mask    = 0xfffffff0,
379         .name           = "SMSC LAN911x Internal PHY",
380
381         /* PHY_BASIC_FEATURES */
382
383         .probe          = smsc_phy_probe,
384
385         /* IRQ related */
386         .config_intr    = smsc_phy_config_intr,
387         .handle_interrupt = smsc_phy_handle_interrupt,
388
389         .suspend        = genphy_suspend,
390         .resume         = genphy_resume,
391 }, {
392         /* This covers internal PHY (phy_id: 0x0007C0F0) for
393          * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
394          */
395         .phy_id         = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
396         .phy_id_mask    = 0xfffffff0,
397         .name           = "SMSC LAN8710/LAN8720",
398
399         /* PHY_BASIC_FEATURES */
400
401         .probe          = smsc_phy_probe,
402
403         /* basic functions */
404         .read_status    = lan87xx_read_status,
405         .config_init    = smsc_phy_config_init,
406         .soft_reset     = smsc_phy_reset,
407         .config_aneg    = lan95xx_config_aneg_ext,
408
409         /* IRQ related */
410         .config_intr    = smsc_phy_config_intr,
411         .handle_interrupt = smsc_phy_handle_interrupt,
412
413         /* Statistics */
414         .get_sset_count = smsc_get_sset_count,
415         .get_strings    = smsc_get_strings,
416         .get_stats      = smsc_get_stats,
417
418         .suspend        = genphy_suspend,
419         .resume         = genphy_resume,
420 }, {
421         .phy_id         = 0x0007c110,
422         .phy_id_mask    = 0xfffffff0,
423         .name           = "SMSC LAN8740",
424
425         /* PHY_BASIC_FEATURES */
426         .flags          = PHY_RST_AFTER_CLK_EN,
427
428         .probe          = smsc_phy_probe,
429
430         /* basic functions */
431         .read_status    = lan87xx_read_status,
432         .config_init    = smsc_phy_config_init,
433         .soft_reset     = smsc_phy_reset,
434
435         /* IRQ related */
436         .config_intr    = smsc_phy_config_intr,
437         .handle_interrupt = smsc_phy_handle_interrupt,
438
439         /* Statistics */
440         .get_sset_count = smsc_get_sset_count,
441         .get_strings    = smsc_get_strings,
442         .get_stats      = smsc_get_stats,
443
444         .suspend        = genphy_suspend,
445         .resume         = genphy_resume,
446 }, {
447         .phy_id         = 0x0007c130,   /* 0x0007c130 and 0x0007c131 */
448         /* This mask (0xfffffff2) is to differentiate from
449          * LAN88xx (phy_id 0x0007c132)
450          * and allows future phy_id revisions.
451          */
452         .phy_id_mask    = 0xfffffff2,
453         .name           = "Microchip LAN8742",
454
455         /* PHY_BASIC_FEATURES */
456         .flags          = PHY_RST_AFTER_CLK_EN,
457
458         .probe          = smsc_phy_probe,
459
460         /* basic functions */
461         .read_status    = lan87xx_read_status,
462         .config_init    = smsc_phy_config_init,
463         .soft_reset     = smsc_phy_reset,
464
465         /* IRQ related */
466         .config_intr    = smsc_phy_config_intr,
467         .handle_interrupt = smsc_phy_handle_interrupt,
468
469         /* Statistics */
470         .get_sset_count = smsc_get_sset_count,
471         .get_strings    = smsc_get_strings,
472         .get_stats      = smsc_get_stats,
473
474         .suspend        = genphy_suspend,
475         .resume         = genphy_resume,
476 } };
477
478 module_phy_driver(smsc_phy_driver);
479
480 MODULE_DESCRIPTION("SMSC PHY driver");
481 MODULE_AUTHOR("Herbert Valerio Riedel");
482 MODULE_LICENSE("GPL");
483
484 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
485         { 0x0007c0a0, 0xfffffff0 },
486         { 0x0007c0b0, 0xfffffff0 },
487         { 0x0007c0c0, 0xfffffff0 },
488         { 0x0007c0d0, 0xfffffff0 },
489         { 0x0007c0f0, 0xfffffff0 },
490         { 0x0007c110, 0xfffffff0 },
491         { 0x0007c130, 0xfffffff2 },
492         { }
493 };
494
495 MODULE_DEVICE_TABLE(mdio, smsc_tbl);