blob: caf7291ffaf83d544e1c44858f282ce6b4728789 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0+
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011 * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
12 *
13 */
14
Olivier Deprez157378f2022-04-04 15:47:50 +020015#include <linux/clk.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#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
Olivier Deprez157378f2022-04-04 15:47:50 +020025/* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036struct smsc_hw_stat {
37 const char *string;
38 u8 reg;
39 u8 bits;
40};
41
42static struct smsc_hw_stat smsc_hw_stats[] = {
43 { "phy_symbol_errors", 26, 16},
44};
45
46struct smsc_phy_priv {
47 bool energy_enable;
Olivier Deprez157378f2022-04-04 15:47:50 +020048 struct clk *refclk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049};
50
51static int smsc_phy_config_intr(struct phy_device *phydev)
52{
Olivier Deprez157378f2022-04-04 15:47:50 +020053 struct smsc_phy_priv *priv = phydev->priv;
54 u16 intmask = 0;
55 int rc;
56
57 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
58 intmask = MII_LAN83C185_ISF_INT4 | MII_LAN83C185_ISF_INT6;
59 if (priv->energy_enable)
60 intmask |= MII_LAN83C185_ISF_INT7;
61 }
62
63 rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064
65 return rc < 0 ? rc : 0;
66}
67
68static int smsc_phy_ack_interrupt(struct phy_device *phydev)
69{
70 int rc = phy_read (phydev, MII_LAN83C185_ISF);
71
72 return rc < 0 ? rc : 0;
73}
74
75static int smsc_phy_config_init(struct phy_device *phydev)
76{
77 struct smsc_phy_priv *priv = phydev->priv;
Olivier Deprez157378f2022-04-04 15:47:50 +020078 int rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079
Olivier Deprez157378f2022-04-04 15:47:50 +020080 if (!priv->energy_enable)
81 return 0;
82
83 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084
85 if (rc < 0)
86 return rc;
87
Olivier Deprez157378f2022-04-04 15:47:50 +020088 /* Enable energy detect mode for this SMSC Transceivers */
89 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
90 rc | MII_LAN83C185_EDPWRDOWN);
91 if (rc < 0)
92 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
94 return smsc_phy_ack_interrupt(phydev);
95}
96
97static int smsc_phy_reset(struct phy_device *phydev)
98{
99 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
100 if (rc < 0)
101 return rc;
102
103 /* If the SMSC PHY is in power down mode, then set it
104 * in all capable mode before using it.
105 */
106 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
107 /* set "all capable" mode */
108 rc |= MII_LAN83C185_MODE_ALL;
109 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
110 }
111
112 /* reset the phy */
113 return genphy_soft_reset(phydev);
114}
115
116static int lan911x_config_init(struct phy_device *phydev)
117{
118 return smsc_phy_ack_interrupt(phydev);
119}
120
Olivier Deprez157378f2022-04-04 15:47:50 +0200121static int lan87xx_config_aneg(struct phy_device *phydev)
122{
123 int rc;
124 int val;
125
126 switch (phydev->mdix_ctrl) {
127 case ETH_TP_MDI:
128 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
129 break;
130 case ETH_TP_MDI_X:
131 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
132 SPECIAL_CTRL_STS_AMDIX_STATE_;
133 break;
134 case ETH_TP_MDI_AUTO:
135 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
136 break;
137 default:
138 return genphy_config_aneg(phydev);
139 }
140
141 rc = phy_read(phydev, SPECIAL_CTRL_STS);
142 if (rc < 0)
143 return rc;
144
145 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
146 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
147 SPECIAL_CTRL_STS_AMDIX_STATE_);
148 rc |= val;
149 phy_write(phydev, SPECIAL_CTRL_STS, rc);
150
151 phydev->mdix = phydev->mdix_ctrl;
152 return genphy_config_aneg(phydev);
153}
154
155static int lan95xx_config_aneg_ext(struct phy_device *phydev)
156{
157 int rc;
158
159 if (phydev->phy_id != 0x0007c0f0) /* not (LAN9500A or LAN9505A) */
160 return lan87xx_config_aneg(phydev);
161
162 /* Extend Manual AutoMDIX timer */
163 rc = phy_read(phydev, PHY_EDPD_CONFIG);
164 if (rc < 0)
165 return rc;
166
167 rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
168 phy_write(phydev, PHY_EDPD_CONFIG, rc);
169 return lan87xx_config_aneg(phydev);
170}
171
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172/*
173 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
174 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
175 * unstable detection of plugging in Ethernet cable.
176 * This workaround disables Energy Detect Power-Down mode and waiting for
177 * response on link pulses to detect presence of plugged Ethernet cable.
178 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
179 * save approximately 220 mW of power if cable is unplugged.
180 */
181static int lan87xx_read_status(struct phy_device *phydev)
182{
183 struct smsc_phy_priv *priv = phydev->priv;
184
185 int err = genphy_read_status(phydev);
186
187 if (!phydev->link && priv->energy_enable) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000188 /* Disable EDPD to wake up PHY */
189 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
190 if (rc < 0)
191 return rc;
192
193 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
194 rc & ~MII_LAN83C185_EDPWRDOWN);
195 if (rc < 0)
196 return rc;
197
Olivier Deprez157378f2022-04-04 15:47:50 +0200198 /* Wait max 640 ms to detect energy and the timeout is not
199 * an actual error.
200 */
201 read_poll_timeout(phy_read, rc,
202 rc & MII_LAN83C185_ENERGYON || rc < 0,
203 10000, 640000, true, phydev,
204 MII_LAN83C185_CTRL_STATUS);
205 if (rc < 0)
206 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207
208 /* Re-enable EDPD */
209 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
210 if (rc < 0)
211 return rc;
212
213 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
214 rc | MII_LAN83C185_EDPWRDOWN);
215 if (rc < 0)
216 return rc;
217 }
218
219 return err;
220}
221
222static int smsc_get_sset_count(struct phy_device *phydev)
223{
224 return ARRAY_SIZE(smsc_hw_stats);
225}
226
227static void smsc_get_strings(struct phy_device *phydev, u8 *data)
228{
229 int i;
230
231 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
232 strncpy(data + i * ETH_GSTRING_LEN,
233 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
234 }
235}
236
237static u64 smsc_get_stat(struct phy_device *phydev, int i)
238{
239 struct smsc_hw_stat stat = smsc_hw_stats[i];
240 int val;
241 u64 ret;
242
243 val = phy_read(phydev, stat.reg);
244 if (val < 0)
245 ret = U64_MAX;
246 else
247 ret = val;
248
249 return ret;
250}
251
252static void smsc_get_stats(struct phy_device *phydev,
253 struct ethtool_stats *stats, u64 *data)
254{
255 int i;
256
257 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
258 data[i] = smsc_get_stat(phydev, i);
259}
260
Olivier Deprez157378f2022-04-04 15:47:50 +0200261static void smsc_phy_remove(struct phy_device *phydev)
262{
263 struct smsc_phy_priv *priv = phydev->priv;
264
265 clk_disable_unprepare(priv->refclk);
266 clk_put(priv->refclk);
267}
268
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000269static int smsc_phy_probe(struct phy_device *phydev)
270{
271 struct device *dev = &phydev->mdio.dev;
272 struct device_node *of_node = dev->of_node;
273 struct smsc_phy_priv *priv;
Olivier Deprez157378f2022-04-04 15:47:50 +0200274 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275
276 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
277 if (!priv)
278 return -ENOMEM;
279
280 priv->energy_enable = true;
281
282 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
283 priv->energy_enable = false;
284
285 phydev->priv = priv;
286
Olivier Deprez157378f2022-04-04 15:47:50 +0200287 /* Make clk optional to keep DTB backward compatibility. */
288 priv->refclk = clk_get_optional(dev, NULL);
289 if (IS_ERR(priv->refclk))
290 return dev_err_probe(dev, PTR_ERR(priv->refclk),
291 "Failed to request clock\n");
292
293 ret = clk_prepare_enable(priv->refclk);
294 if (ret)
295 return ret;
296
297 ret = clk_set_rate(priv->refclk, 50 * 1000 * 1000);
298 if (ret) {
299 clk_disable_unprepare(priv->refclk);
300 return ret;
301 }
302
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303 return 0;
304}
305
306static struct phy_driver smsc_phy_driver[] = {
307{
308 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
309 .phy_id_mask = 0xfffffff0,
310 .name = "SMSC LAN83C185",
311
David Brazdil0f672f62019-12-10 10:32:29 +0000312 /* PHY_BASIC_FEATURES */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000313
314 .probe = smsc_phy_probe,
315
316 /* basic functions */
317 .config_init = smsc_phy_config_init,
318 .soft_reset = smsc_phy_reset,
319
320 /* IRQ related */
321 .ack_interrupt = smsc_phy_ack_interrupt,
322 .config_intr = smsc_phy_config_intr,
323
324 .suspend = genphy_suspend,
325 .resume = genphy_resume,
326}, {
327 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
328 .phy_id_mask = 0xfffffff0,
329 .name = "SMSC LAN8187",
330
David Brazdil0f672f62019-12-10 10:32:29 +0000331 /* PHY_BASIC_FEATURES */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332
333 .probe = smsc_phy_probe,
334
335 /* basic functions */
336 .config_init = smsc_phy_config_init,
337 .soft_reset = smsc_phy_reset,
338
339 /* IRQ related */
340 .ack_interrupt = smsc_phy_ack_interrupt,
341 .config_intr = smsc_phy_config_intr,
342
343 /* Statistics */
344 .get_sset_count = smsc_get_sset_count,
345 .get_strings = smsc_get_strings,
346 .get_stats = smsc_get_stats,
347
348 .suspend = genphy_suspend,
349 .resume = genphy_resume,
350}, {
Olivier Deprez157378f2022-04-04 15:47:50 +0200351 /* This covers internal PHY (phy_id: 0x0007C0C3) for
352 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
353 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000354 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
355 .phy_id_mask = 0xfffffff0,
356 .name = "SMSC LAN8700",
357
David Brazdil0f672f62019-12-10 10:32:29 +0000358 /* PHY_BASIC_FEATURES */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359
360 .probe = smsc_phy_probe,
361
362 /* basic functions */
363 .read_status = lan87xx_read_status,
364 .config_init = smsc_phy_config_init,
365 .soft_reset = smsc_phy_reset,
Olivier Deprez157378f2022-04-04 15:47:50 +0200366 .config_aneg = lan87xx_config_aneg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367
368 /* IRQ related */
369 .ack_interrupt = smsc_phy_ack_interrupt,
370 .config_intr = smsc_phy_config_intr,
371
372 /* Statistics */
373 .get_sset_count = smsc_get_sset_count,
374 .get_strings = smsc_get_strings,
375 .get_stats = smsc_get_stats,
376
377 .suspend = genphy_suspend,
378 .resume = genphy_resume,
379}, {
380 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
381 .phy_id_mask = 0xfffffff0,
382 .name = "SMSC LAN911x Internal PHY",
383
David Brazdil0f672f62019-12-10 10:32:29 +0000384 /* PHY_BASIC_FEATURES */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000385
386 .probe = smsc_phy_probe,
387
388 /* basic functions */
389 .config_init = lan911x_config_init,
390
391 /* IRQ related */
392 .ack_interrupt = smsc_phy_ack_interrupt,
393 .config_intr = smsc_phy_config_intr,
394
395 .suspend = genphy_suspend,
396 .resume = genphy_resume,
397}, {
Olivier Deprez157378f2022-04-04 15:47:50 +0200398 /* This covers internal PHY (phy_id: 0x0007C0F0) for
399 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
400 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
402 .phy_id_mask = 0xfffffff0,
403 .name = "SMSC LAN8710/LAN8720",
404
David Brazdil0f672f62019-12-10 10:32:29 +0000405 /* PHY_BASIC_FEATURES */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406
407 .probe = smsc_phy_probe,
Olivier Deprez157378f2022-04-04 15:47:50 +0200408 .remove = smsc_phy_remove,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409
410 /* basic functions */
411 .read_status = lan87xx_read_status,
412 .config_init = smsc_phy_config_init,
413 .soft_reset = smsc_phy_reset,
Olivier Deprez157378f2022-04-04 15:47:50 +0200414 .config_aneg = lan95xx_config_aneg_ext,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415
416 /* IRQ related */
417 .ack_interrupt = smsc_phy_ack_interrupt,
418 .config_intr = smsc_phy_config_intr,
419
420 /* Statistics */
421 .get_sset_count = smsc_get_sset_count,
422 .get_strings = smsc_get_strings,
423 .get_stats = smsc_get_stats,
424
425 .suspend = genphy_suspend,
426 .resume = genphy_resume,
427}, {
428 .phy_id = 0x0007c110,
429 .phy_id_mask = 0xfffffff0,
430 .name = "SMSC LAN8740",
431
David Brazdil0f672f62019-12-10 10:32:29 +0000432 /* PHY_BASIC_FEATURES */
433 .flags = PHY_RST_AFTER_CLK_EN,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434
435 .probe = smsc_phy_probe,
436
437 /* basic functions */
438 .read_status = lan87xx_read_status,
439 .config_init = smsc_phy_config_init,
440 .soft_reset = smsc_phy_reset,
441
442 /* IRQ related */
443 .ack_interrupt = smsc_phy_ack_interrupt,
444 .config_intr = smsc_phy_config_intr,
445
446 /* Statistics */
447 .get_sset_count = smsc_get_sset_count,
448 .get_strings = smsc_get_strings,
449 .get_stats = smsc_get_stats,
450
451 .suspend = genphy_suspend,
452 .resume = genphy_resume,
453} };
454
455module_phy_driver(smsc_phy_driver);
456
457MODULE_DESCRIPTION("SMSC PHY driver");
458MODULE_AUTHOR("Herbert Valerio Riedel");
459MODULE_LICENSE("GPL");
460
461static struct mdio_device_id __maybe_unused smsc_tbl[] = {
462 { 0x0007c0a0, 0xfffffff0 },
463 { 0x0007c0b0, 0xfffffff0 },
464 { 0x0007c0c0, 0xfffffff0 },
465 { 0x0007c0d0, 0xfffffff0 },
466 { 0x0007c0f0, 0xfffffff0 },
467 { 0x0007c110, 0xfffffff0 },
468 { }
469};
470
471MODULE_DEVICE_TABLE(mdio, smsc_tbl);