.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
---|
1 | 2 | /* |
---|
2 | 3 | * drivers/net/phy/smsc.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
7 | 8 | * |
---|
8 | 9 | * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org> |
---|
9 | 10 | * |
---|
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 | 11 | * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net |
---|
16 | 12 | * |
---|
17 | 13 | */ |
---|
18 | 14 | |
---|
| 15 | +#include <linux/clk.h> |
---|
19 | 16 | #include <linux/kernel.h> |
---|
20 | 17 | #include <linux/module.h> |
---|
21 | 18 | #include <linux/mii.h> |
---|
.. | .. |
---|
24 | 21 | #include <linux/phy.h> |
---|
25 | 22 | #include <linux/netdevice.h> |
---|
26 | 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 |
---|
27 | 35 | |
---|
28 | 36 | struct smsc_hw_stat { |
---|
29 | 37 | const char *string; |
---|
.. | .. |
---|
37 | 45 | |
---|
38 | 46 | struct smsc_phy_priv { |
---|
39 | 47 | bool energy_enable; |
---|
| 48 | + struct clk *refclk; |
---|
40 | 49 | }; |
---|
41 | 50 | |
---|
42 | 51 | static int smsc_phy_config_intr(struct phy_device *phydev) |
---|
43 | 52 | { |
---|
44 | | - int rc = phy_write (phydev, MII_LAN83C185_IM, |
---|
45 | | - ((PHY_INTERRUPT_ENABLED == phydev->interrupts) |
---|
46 | | - ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS |
---|
47 | | - : 0)); |
---|
| 53 | + 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); |
---|
48 | 64 | |
---|
49 | 65 | return rc < 0 ? rc : 0; |
---|
50 | 66 | } |
---|
.. | .. |
---|
59 | 75 | static int smsc_phy_config_init(struct phy_device *phydev) |
---|
60 | 76 | { |
---|
61 | 77 | struct smsc_phy_priv *priv = phydev->priv; |
---|
| 78 | + int rc; |
---|
62 | 79 | |
---|
63 | | - int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); |
---|
| 80 | + if (!priv->energy_enable) |
---|
| 81 | + return 0; |
---|
| 82 | + |
---|
| 83 | + rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); |
---|
64 | 84 | |
---|
65 | 85 | if (rc < 0) |
---|
66 | 86 | return rc; |
---|
67 | 87 | |
---|
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 | | - } |
---|
| 88 | + /* 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; |
---|
75 | 93 | |
---|
76 | 94 | return smsc_phy_ack_interrupt(phydev); |
---|
77 | 95 | } |
---|
.. | .. |
---|
100 | 118 | return smsc_phy_ack_interrupt(phydev); |
---|
101 | 119 | } |
---|
102 | 120 | |
---|
| 121 | +static 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 | + |
---|
| 155 | +static 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 | + |
---|
103 | 172 | /* |
---|
104 | 173 | * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable |
---|
105 | 174 | * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to |
---|
.. | .. |
---|
116 | 185 | int err = genphy_read_status(phydev); |
---|
117 | 186 | |
---|
118 | 187 | if (!phydev->link && priv->energy_enable) { |
---|
119 | | - int i; |
---|
120 | | - |
---|
121 | 188 | /* Disable EDPD to wake up PHY */ |
---|
122 | 189 | int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); |
---|
123 | 190 | if (rc < 0) |
---|
.. | .. |
---|
128 | 195 | if (rc < 0) |
---|
129 | 196 | return rc; |
---|
130 | 197 | |
---|
131 | | - /* Wait max 640 ms to detect energy */ |
---|
132 | | - for (i = 0; i < 64; i++) { |
---|
133 | | - /* Sleep to allow link test pulses to be sent */ |
---|
134 | | - msleep(10); |
---|
135 | | - rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); |
---|
136 | | - if (rc < 0) |
---|
137 | | - return rc; |
---|
138 | | - if (rc & MII_LAN83C185_ENERGYON) |
---|
139 | | - break; |
---|
140 | | - } |
---|
| 198 | + /* 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; |
---|
141 | 207 | |
---|
142 | 208 | /* Re-enable EDPD */ |
---|
143 | 209 | rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); |
---|
.. | .. |
---|
192 | 258 | data[i] = smsc_get_stat(phydev, i); |
---|
193 | 259 | } |
---|
194 | 260 | |
---|
| 261 | +static 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 | + |
---|
195 | 269 | static int smsc_phy_probe(struct phy_device *phydev) |
---|
196 | 270 | { |
---|
197 | 271 | struct device *dev = &phydev->mdio.dev; |
---|
198 | 272 | struct device_node *of_node = dev->of_node; |
---|
199 | 273 | struct smsc_phy_priv *priv; |
---|
| 274 | + int ret; |
---|
200 | 275 | |
---|
201 | 276 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
---|
202 | 277 | if (!priv) |
---|
.. | .. |
---|
209 | 284 | |
---|
210 | 285 | phydev->priv = priv; |
---|
211 | 286 | |
---|
| 287 | + /* 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 | + |
---|
212 | 303 | return 0; |
---|
213 | 304 | } |
---|
214 | 305 | |
---|
.. | .. |
---|
218 | 309 | .phy_id_mask = 0xfffffff0, |
---|
219 | 310 | .name = "SMSC LAN83C185", |
---|
220 | 311 | |
---|
221 | | - .features = PHY_BASIC_FEATURES, |
---|
222 | | - .flags = PHY_HAS_INTERRUPT, |
---|
| 312 | + /* PHY_BASIC_FEATURES */ |
---|
223 | 313 | |
---|
224 | 314 | .probe = smsc_phy_probe, |
---|
225 | 315 | |
---|
.. | .. |
---|
238 | 328 | .phy_id_mask = 0xfffffff0, |
---|
239 | 329 | .name = "SMSC LAN8187", |
---|
240 | 330 | |
---|
241 | | - .features = PHY_BASIC_FEATURES, |
---|
242 | | - .flags = PHY_HAS_INTERRUPT, |
---|
| 331 | + /* PHY_BASIC_FEATURES */ |
---|
243 | 332 | |
---|
244 | 333 | .probe = smsc_phy_probe, |
---|
245 | 334 | |
---|
.. | .. |
---|
259 | 348 | .suspend = genphy_suspend, |
---|
260 | 349 | .resume = genphy_resume, |
---|
261 | 350 | }, { |
---|
| 351 | + /* This covers internal PHY (phy_id: 0x0007C0C3) for |
---|
| 352 | + * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505) |
---|
| 353 | + */ |
---|
262 | 354 | .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */ |
---|
263 | 355 | .phy_id_mask = 0xfffffff0, |
---|
264 | 356 | .name = "SMSC LAN8700", |
---|
265 | 357 | |
---|
266 | | - .features = PHY_BASIC_FEATURES, |
---|
267 | | - .flags = PHY_HAS_INTERRUPT, |
---|
| 358 | + /* PHY_BASIC_FEATURES */ |
---|
268 | 359 | |
---|
269 | 360 | .probe = smsc_phy_probe, |
---|
270 | 361 | |
---|
.. | .. |
---|
272 | 363 | .read_status = lan87xx_read_status, |
---|
273 | 364 | .config_init = smsc_phy_config_init, |
---|
274 | 365 | .soft_reset = smsc_phy_reset, |
---|
| 366 | + .config_aneg = lan87xx_config_aneg, |
---|
275 | 367 | |
---|
276 | 368 | /* IRQ related */ |
---|
277 | 369 | .ack_interrupt = smsc_phy_ack_interrupt, |
---|
.. | .. |
---|
289 | 381 | .phy_id_mask = 0xfffffff0, |
---|
290 | 382 | .name = "SMSC LAN911x Internal PHY", |
---|
291 | 383 | |
---|
292 | | - .features = PHY_BASIC_FEATURES, |
---|
293 | | - .flags = PHY_HAS_INTERRUPT, |
---|
| 384 | + /* PHY_BASIC_FEATURES */ |
---|
294 | 385 | |
---|
295 | 386 | .probe = smsc_phy_probe, |
---|
296 | 387 | |
---|
.. | .. |
---|
304 | 395 | .suspend = genphy_suspend, |
---|
305 | 396 | .resume = genphy_resume, |
---|
306 | 397 | }, { |
---|
| 398 | + /* This covers internal PHY (phy_id: 0x0007C0F0) for |
---|
| 399 | + * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01) |
---|
| 400 | + */ |
---|
307 | 401 | .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */ |
---|
308 | 402 | .phy_id_mask = 0xfffffff0, |
---|
309 | 403 | .name = "SMSC LAN8710/LAN8720", |
---|
310 | 404 | |
---|
311 | | - .features = PHY_BASIC_FEATURES, |
---|
312 | | - .flags = PHY_HAS_INTERRUPT | PHY_RST_AFTER_CLK_EN, |
---|
| 405 | + /* PHY_BASIC_FEATURES */ |
---|
313 | 406 | |
---|
314 | 407 | .probe = smsc_phy_probe, |
---|
| 408 | + .remove = smsc_phy_remove, |
---|
315 | 409 | |
---|
316 | 410 | /* basic functions */ |
---|
317 | 411 | .read_status = lan87xx_read_status, |
---|
318 | 412 | .config_init = smsc_phy_config_init, |
---|
319 | 413 | .soft_reset = smsc_phy_reset, |
---|
| 414 | + .config_aneg = lan95xx_config_aneg_ext, |
---|
320 | 415 | |
---|
321 | 416 | /* IRQ related */ |
---|
322 | 417 | .ack_interrupt = smsc_phy_ack_interrupt, |
---|
.. | .. |
---|
334 | 429 | .phy_id_mask = 0xfffffff0, |
---|
335 | 430 | .name = "SMSC LAN8740", |
---|
336 | 431 | |
---|
337 | | - .features = PHY_BASIC_FEATURES, |
---|
338 | | - .flags = PHY_HAS_INTERRUPT, |
---|
| 432 | + /* PHY_BASIC_FEATURES */ |
---|
| 433 | + .flags = PHY_RST_AFTER_CLK_EN, |
---|
339 | 434 | |
---|
340 | 435 | .probe = smsc_phy_probe, |
---|
341 | 436 | |
---|