GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / net / phy / smsc.c
1 /*
2  * drivers/net/phy/smsc.c
3  *
4  * Driver for SMSC PHYs
5  *
6  * Author: Herbert Valerio Riedel
7  *
8  * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org>
9  *
10  * This program is free software; you can redistribute  it and/or modify it
11  * under  the terms of  the GNU General  Public License as published by the
12  * Free Software Foundation;  either version 2 of the  License, or (at your
13  * option) any later version.
14  *
15  * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mii.h>
22 #include <linux/ethtool.h>
23 #include <linux/of.h>
24 #include <linux/phy.h>
25 #include <linux/netdevice.h>
26 #include <linux/smscphy.h>
27
28 struct smsc_hw_stat {
29         const char *string;
30         u8 reg;
31         u8 bits;
32 };
33
34 static struct smsc_hw_stat smsc_hw_stats[] = {
35         { "phy_symbol_errors", 26, 16},
36 };
37
38 struct smsc_phy_priv {
39         bool energy_enable;
40 };
41
42 static int smsc_phy_config_intr(struct phy_device *phydev)
43 {
44         int rc = phy_write (phydev, MII_LAN83C185_IM,
45                         ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
46                         ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
47                         : 0));
48
49         return rc < 0 ? rc : 0;
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_init(struct phy_device *phydev)
60 {
61         struct smsc_phy_priv *priv = phydev->priv;
62
63         int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
64
65         if (rc < 0)
66                 return rc;
67
68         if (priv->energy_enable) {
69                 /* Enable energy detect mode for this SMSC Transceivers */
70                 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
71                                rc | MII_LAN83C185_EDPWRDOWN);
72                 if (rc < 0)
73                         return rc;
74         }
75
76         return smsc_phy_ack_interrupt(phydev);
77 }
78
79 static int smsc_phy_reset(struct phy_device *phydev)
80 {
81         int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
82         if (rc < 0)
83                 return rc;
84
85         /* If the SMSC PHY is in power down mode, then set it
86          * in all capable mode before using it.
87          */
88         if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
89                 /* set "all capable" mode */
90                 rc |= MII_LAN83C185_MODE_ALL;
91                 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
92         }
93
94         /* reset the phy */
95         return genphy_soft_reset(phydev);
96 }
97
98 static int lan911x_config_init(struct phy_device *phydev)
99 {
100         return smsc_phy_ack_interrupt(phydev);
101 }
102
103 /*
104  * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
105  * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
106  * unstable detection of plugging in Ethernet cable.
107  * This workaround disables Energy Detect Power-Down mode and waiting for
108  * response on link pulses to detect presence of plugged Ethernet cable.
109  * The Energy Detect Power-Down mode is enabled again in the end of procedure to
110  * save approximately 220 mW of power if cable is unplugged.
111  */
112 static int lan87xx_read_status(struct phy_device *phydev)
113 {
114         struct smsc_phy_priv *priv = phydev->priv;
115         int err;
116
117         err = genphy_read_status(phydev);
118         if (err)
119                 return err;
120
121         if (!phydev->link && priv->energy_enable) {
122                 int i;
123
124                 /* Disable EDPD to wake up PHY */
125                 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
126                 if (rc < 0)
127                         return rc;
128
129                 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
130                                rc & ~MII_LAN83C185_EDPWRDOWN);
131                 if (rc < 0)
132                         return rc;
133
134                 /* Wait max 640 ms to detect energy */
135                 for (i = 0; i < 64; i++) {
136                         /* Sleep to allow link test pulses to be sent */
137                         msleep(10);
138                         rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
139                         if (rc < 0)
140                                 return rc;
141                         if (rc & MII_LAN83C185_ENERGYON)
142                                 break;
143                 }
144
145                 /* Re-enable EDPD */
146                 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
147                 if (rc < 0)
148                         return rc;
149
150                 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
151                                rc | MII_LAN83C185_EDPWRDOWN);
152                 if (rc < 0)
153                         return rc;
154         }
155
156         return err;
157 }
158
159 static int smsc_get_sset_count(struct phy_device *phydev)
160 {
161         return ARRAY_SIZE(smsc_hw_stats);
162 }
163
164 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
165 {
166         int i;
167
168         for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
169                 strncpy(data + i * ETH_GSTRING_LEN,
170                        smsc_hw_stats[i].string, ETH_GSTRING_LEN);
171         }
172 }
173
174 #ifndef UINT64_MAX
175 #define UINT64_MAX              (u64)(~((u64)0))
176 #endif
177 static u64 smsc_get_stat(struct phy_device *phydev, int i)
178 {
179         struct smsc_hw_stat stat = smsc_hw_stats[i];
180         int val;
181         u64 ret;
182
183         val = phy_read(phydev, stat.reg);
184         if (val < 0)
185                 ret = UINT64_MAX;
186         else
187                 ret = val;
188
189         return ret;
190 }
191
192 static void smsc_get_stats(struct phy_device *phydev,
193                            struct ethtool_stats *stats, u64 *data)
194 {
195         int i;
196
197         for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
198                 data[i] = smsc_get_stat(phydev, i);
199 }
200
201 static int smsc_phy_probe(struct phy_device *phydev)
202 {
203         struct device *dev = &phydev->mdio.dev;
204         struct device_node *of_node = dev->of_node;
205         struct smsc_phy_priv *priv;
206
207         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
208         if (!priv)
209                 return -ENOMEM;
210
211         priv->energy_enable = true;
212
213         if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
214                 priv->energy_enable = false;
215
216         phydev->priv = priv;
217
218         return 0;
219 }
220
221 static struct phy_driver smsc_phy_driver[] = {
222 {
223         .phy_id         = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
224         .phy_id_mask    = 0xfffffff0,
225         .name           = "SMSC LAN83C185",
226
227         .features       = PHY_BASIC_FEATURES,
228         .flags          = PHY_HAS_INTERRUPT,
229
230         .probe          = smsc_phy_probe,
231
232         /* basic functions */
233         .config_aneg    = genphy_config_aneg,
234         .read_status    = genphy_read_status,
235         .config_init    = smsc_phy_config_init,
236         .soft_reset     = smsc_phy_reset,
237
238         /* IRQ related */
239         .ack_interrupt  = smsc_phy_ack_interrupt,
240         .config_intr    = smsc_phy_config_intr,
241
242         .suspend        = genphy_suspend,
243         .resume         = genphy_resume,
244 }, {
245         .phy_id         = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
246         .phy_id_mask    = 0xfffffff0,
247         .name           = "SMSC LAN8187",
248
249         .features       = PHY_BASIC_FEATURES,
250         .flags          = PHY_HAS_INTERRUPT,
251
252         .probe          = smsc_phy_probe,
253
254         /* basic functions */
255         .config_aneg    = genphy_config_aneg,
256         .read_status    = genphy_read_status,
257         .config_init    = smsc_phy_config_init,
258         .soft_reset     = smsc_phy_reset,
259
260         /* IRQ related */
261         .ack_interrupt  = smsc_phy_ack_interrupt,
262         .config_intr    = smsc_phy_config_intr,
263
264         /* Statistics */
265         .get_sset_count = smsc_get_sset_count,
266         .get_strings    = smsc_get_strings,
267         .get_stats      = smsc_get_stats,
268
269         .suspend        = genphy_suspend,
270         .resume         = genphy_resume,
271 }, {
272         .phy_id         = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
273         .phy_id_mask    = 0xfffffff0,
274         .name           = "SMSC LAN8700",
275
276         .features       = PHY_BASIC_FEATURES,
277         .flags          = PHY_HAS_INTERRUPT,
278
279         .probe          = smsc_phy_probe,
280
281         /* basic functions */
282         .config_aneg    = genphy_config_aneg,
283         .read_status    = lan87xx_read_status,
284         .config_init    = smsc_phy_config_init,
285         .soft_reset     = smsc_phy_reset,
286
287         /* IRQ related */
288         .ack_interrupt  = smsc_phy_ack_interrupt,
289         .config_intr    = smsc_phy_config_intr,
290
291         /* Statistics */
292         .get_sset_count = smsc_get_sset_count,
293         .get_strings    = smsc_get_strings,
294         .get_stats      = smsc_get_stats,
295
296         .suspend        = genphy_suspend,
297         .resume         = genphy_resume,
298 }, {
299         .phy_id         = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
300         .phy_id_mask    = 0xfffffff0,
301         .name           = "SMSC LAN911x Internal PHY",
302
303         .features       = PHY_BASIC_FEATURES,
304         .flags          = PHY_HAS_INTERRUPT,
305
306         .probe          = smsc_phy_probe,
307
308         /* basic functions */
309         .config_aneg    = genphy_config_aneg,
310         .read_status    = genphy_read_status,
311         .config_init    = lan911x_config_init,
312
313         /* IRQ related */
314         .ack_interrupt  = smsc_phy_ack_interrupt,
315         .config_intr    = smsc_phy_config_intr,
316
317         .suspend        = genphy_suspend,
318         .resume         = genphy_resume,
319 }, {
320         .phy_id         = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
321         .phy_id_mask    = 0xfffffff0,
322         .name           = "SMSC LAN8710/LAN8720",
323
324         .features       = PHY_BASIC_FEATURES,
325         .flags          = PHY_HAS_INTERRUPT,
326
327         .probe          = smsc_phy_probe,
328
329         /* basic functions */
330         .config_aneg    = genphy_config_aneg,
331         .read_status    = lan87xx_read_status,
332         .config_init    = smsc_phy_config_init,
333         .soft_reset     = smsc_phy_reset,
334
335         /* IRQ related */
336         .ack_interrupt  = smsc_phy_ack_interrupt,
337         .config_intr    = smsc_phy_config_intr,
338
339         /* Statistics */
340         .get_sset_count = smsc_get_sset_count,
341         .get_strings    = smsc_get_strings,
342         .get_stats      = smsc_get_stats,
343
344         .suspend        = genphy_suspend,
345         .resume         = genphy_resume,
346 }, {
347         .phy_id         = 0x0007c110,
348         .phy_id_mask    = 0xfffffff0,
349         .name           = "SMSC LAN8740",
350
351         .features       = PHY_BASIC_FEATURES,
352         .flags          = PHY_HAS_INTERRUPT,
353
354         .probe          = smsc_phy_probe,
355
356         /* basic functions */
357         .config_aneg    = genphy_config_aneg,
358         .read_status    = lan87xx_read_status,
359         .config_init    = smsc_phy_config_init,
360         .soft_reset     = smsc_phy_reset,
361
362         /* IRQ related */
363         .ack_interrupt  = smsc_phy_ack_interrupt,
364         .config_intr    = smsc_phy_config_intr,
365
366         /* Statistics */
367         .get_sset_count = smsc_get_sset_count,
368         .get_strings    = smsc_get_strings,
369         .get_stats      = smsc_get_stats,
370
371         .suspend        = genphy_suspend,
372         .resume         = genphy_resume,
373 } };
374
375 module_phy_driver(smsc_phy_driver);
376
377 MODULE_DESCRIPTION("SMSC PHY driver");
378 MODULE_AUTHOR("Herbert Valerio Riedel");
379 MODULE_LICENSE("GPL");
380
381 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
382         { 0x0007c0a0, 0xfffffff0 },
383         { 0x0007c0b0, 0xfffffff0 },
384         { 0x0007c0c0, 0xfffffff0 },
385         { 0x0007c0d0, 0xfffffff0 },
386         { 0x0007c0f0, 0xfffffff0 },
387         { 0x0007c110, 0xfffffff0 },
388         { }
389 };
390
391 MODULE_DEVICE_TABLE(mdio, smsc_tbl);