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