| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: rgephy.c,v 1.55 2019/06/05 17:50:06 triaxx Exp $ */ | | 1 | /* $NetBSD: rgephy.c,v 1.56 2019/10/11 03:40:01 msaitoh Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2003 | | 4 | * Copyright (c) 2003 |
5 | * Bill Paul <wpaul@windriver.com>. All rights reserved. | | 5 | * Bill Paul <wpaul@windriver.com>. All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -23,27 +23,27 @@ | | | @@ -23,27 +23,27 @@ |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD | | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD |
26 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 26 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
32 | * THE POSSIBILITY OF SUCH DAMAGE. | | 32 | * THE POSSIBILITY OF SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #include <sys/cdefs.h> | | 35 | #include <sys/cdefs.h> |
36 | __KERNEL_RCSID(0, "$NetBSD: rgephy.c,v 1.55 2019/06/05 17:50:06 triaxx Exp $"); | | 36 | __KERNEL_RCSID(0, "$NetBSD: rgephy.c,v 1.56 2019/10/11 03:40:01 msaitoh Exp $"); |
37 | | | 37 | |
38 | | | 38 | |
39 | /* | | 39 | /* |
40 | * Driver for the RealTek 8169S/8110S internal 10/100/1000 PHY. | | 40 | * Driver for the RealTek 8169S/8110S internal 10/100/1000 PHY. |
41 | */ | | 41 | */ |
42 | | | 42 | |
43 | #include <sys/param.h> | | 43 | #include <sys/param.h> |
44 | #include <sys/systm.h> | | 44 | #include <sys/systm.h> |
45 | #include <sys/kernel.h> | | 45 | #include <sys/kernel.h> |
46 | #include <sys/device.h> | | 46 | #include <sys/device.h> |
47 | #include <sys/socket.h> | | 47 | #include <sys/socket.h> |
48 | | | 48 | |
49 | | | 49 | |
| @@ -65,26 +65,27 @@ static void rgephy_attach(device_t, devi | | | @@ -65,26 +65,27 @@ static void rgephy_attach(device_t, devi |
65 | struct rgephy_softc { | | 65 | struct rgephy_softc { |
66 | struct mii_softc mii_sc; | | 66 | struct mii_softc mii_sc; |
67 | bool mii_no_rx_delay; | | 67 | bool mii_no_rx_delay; |
68 | }; | | 68 | }; |
69 | | | 69 | |
70 | CFATTACH_DECL_NEW(rgephy, sizeof(struct rgephy_softc), | | 70 | CFATTACH_DECL_NEW(rgephy, sizeof(struct rgephy_softc), |
71 | rgephy_match, rgephy_attach, mii_phy_detach, mii_phy_activate); | | 71 | rgephy_match, rgephy_attach, mii_phy_detach, mii_phy_activate); |
72 | | | 72 | |
73 | | | 73 | |
74 | static int rgephy_service(struct mii_softc *, struct mii_data *, int); | | 74 | static int rgephy_service(struct mii_softc *, struct mii_data *, int); |
75 | static void rgephy_status(struct mii_softc *); | | 75 | static void rgephy_status(struct mii_softc *); |
76 | static int rgephy_mii_phy_auto(struct mii_softc *); | | 76 | static int rgephy_mii_phy_auto(struct mii_softc *); |
77 | static void rgephy_reset(struct mii_softc *); | | 77 | static void rgephy_reset(struct mii_softc *); |
| | | 78 | static bool rgephy_linkup(struct mii_softc *); |
78 | static void rgephy_loop(struct mii_softc *); | | 79 | static void rgephy_loop(struct mii_softc *); |
79 | static void rgephy_load_dspcode(struct mii_softc *); | | 80 | static void rgephy_load_dspcode(struct mii_softc *); |
80 | | | 81 | |
81 | static const struct mii_phy_funcs rgephy_funcs = { | | 82 | static const struct mii_phy_funcs rgephy_funcs = { |
82 | rgephy_service, rgephy_status, rgephy_reset, | | 83 | rgephy_service, rgephy_status, rgephy_reset, |
83 | }; | | 84 | }; |
84 | | | 85 | |
85 | static const struct mii_phydesc rgephys[] = { | | 86 | static const struct mii_phydesc rgephys[] = { |
86 | MII_PHY_DESC(xxREALTEK, RTL8169S), | | 87 | MII_PHY_DESC(xxREALTEK, RTL8169S), |
87 | MII_PHY_DESC(REALTEK, RTL8169S), | | 88 | MII_PHY_DESC(REALTEK, RTL8169S), |
88 | MII_PHY_DESC(REALTEK, RTL8251), | | 89 | MII_PHY_DESC(REALTEK, RTL8251), |
89 | MII_PHY_END, | | 90 | MII_PHY_END, |
90 | }; | | 91 | }; |
| @@ -285,46 +286,29 @@ rgephy_service(struct mii_softc *sc, str | | | @@ -285,46 +286,29 @@ rgephy_service(struct mii_softc *sc, str |
285 | /* | | 286 | /* |
286 | * Reset autonegotiation timer to 0 to make sure | | 287 | * Reset autonegotiation timer to 0 to make sure |
287 | * the future autonegotiation start with 0. | | 288 | * the future autonegotiation start with 0. |
288 | */ | | 289 | */ |
289 | sc->mii_ticks = 0; | | 290 | sc->mii_ticks = 0; |
290 | break; | | 291 | break; |
291 | } | | 292 | } |
292 | | | 293 | |
293 | /* | | 294 | /* |
294 | * Check to see if we have link. If we do, we don't | | 295 | * Check to see if we have link. If we do, we don't |
295 | * need to restart the autonegotiation process. Read | | 296 | * need to restart the autonegotiation process. Read |
296 | * the BMSR twice in case it's latched. | | 297 | * the BMSR twice in case it's latched. |
297 | */ | | 298 | */ |
298 | if (sc->mii_mpd_rev >= RGEPHY_8211F) { | | 299 | if (rgephy_linkup(sc)) { |
299 | /* RTL8211F */ | | 300 | sc->mii_ticks = 0; |
300 | PHY_READ(sc, RGEPHY_MII_PHYSR, ®); | | 301 | break; |
301 | if (reg & RGEPHY_PHYSR_LINK) { | | | |
302 | sc->mii_ticks = 0; | | | |
303 | break; | | | |
304 | } | | | |
305 | } else if (sc->mii_mpd_rev >= RGEPHY_8211B) { | | | |
306 | /* RTL8211B(L) */ | | | |
307 | PHY_READ(sc, RGEPHY_MII_SSR, ®); | | | |
308 | if (reg & RGEPHY_SSR_LINK) { | | | |
309 | sc->mii_ticks = 0; | | | |
310 | break; | | | |
311 | } | | | |
312 | } else { | | | |
313 | PHY_READ(sc, RTK_GMEDIASTAT, ®); | | | |
314 | if ((reg & RTK_GMEDIASTAT_LINK) != 0) { | | | |
315 | sc->mii_ticks = 0; | | | |
316 | break; | | | |
317 | } | | | |
318 | } | | 302 | } |
319 | | | 303 | |
320 | /* Announce link loss right after it happens. */ | | 304 | /* Announce link loss right after it happens. */ |
321 | if (sc->mii_ticks++ == 0) | | 305 | if (sc->mii_ticks++ == 0) |
322 | break; | | 306 | break; |
323 | | | 307 | |
324 | /* Only retry autonegotiation every mii_anegticks seconds. */ | | 308 | /* Only retry autonegotiation every mii_anegticks seconds. */ |
325 | if (sc->mii_ticks <= sc->mii_anegticks) | | 309 | if (sc->mii_ticks <= sc->mii_anegticks) |
326 | return 0; | | 310 | return 0; |
327 | | | 311 | |
328 | rgephy_mii_phy_auto(sc); | | 312 | rgephy_mii_phy_auto(sc); |
329 | break; | | 313 | break; |
330 | } | | 314 | } |
| @@ -335,48 +319,60 @@ rgephy_service(struct mii_softc *sc, str | | | @@ -335,48 +319,60 @@ rgephy_service(struct mii_softc *sc, str |
335 | /* | | 319 | /* |
336 | * Callback if something changed. Note that we need to poke | | 320 | * Callback if something changed. Note that we need to poke |
337 | * the DSP on the RealTek PHYs if the media changes. | | 321 | * the DSP on the RealTek PHYs if the media changes. |
338 | */ | | 322 | */ |
339 | if (sc->mii_media_active != mii->mii_media_active || | | 323 | if (sc->mii_media_active != mii->mii_media_active || |
340 | sc->mii_media_status != mii->mii_media_status || | | 324 | sc->mii_media_status != mii->mii_media_status || |
341 | cmd == MII_MEDIACHG) { | | 325 | cmd == MII_MEDIACHG) { |
342 | rgephy_load_dspcode(sc); | | 326 | rgephy_load_dspcode(sc); |
343 | } | | 327 | } |
344 | mii_phy_update(sc, cmd); | | 328 | mii_phy_update(sc, cmd); |
345 | return 0; | | 329 | return 0; |
346 | } | | 330 | } |
347 | | | 331 | |
| | | 332 | static bool |
| | | 333 | rgephy_linkup(struct mii_softc *sc) |
| | | 334 | { |
| | | 335 | bool linkup = false; |
| | | 336 | uint16_t reg; |
| | | 337 | |
| | | 338 | if (sc->mii_mpd_rev >= RGEPHY_8211F) { |
| | | 339 | PHY_READ(sc, RGEPHY_MII_PHYSR, ®); |
| | | 340 | if (reg & RGEPHY_PHYSR_LINK) |
| | | 341 | linkup = true; |
| | | 342 | } else if (sc->mii_mpd_rev >= RGEPHY_8211B) { |
| | | 343 | PHY_READ(sc, RGEPHY_MII_SSR, ®); |
| | | 344 | if (reg & RGEPHY_SSR_LINK) |
| | | 345 | linkup = true; |
| | | 346 | } else { |
| | | 347 | PHY_READ(sc, RTK_GMEDIASTAT, ®); |
| | | 348 | if ((reg & RTK_GMEDIASTAT_LINK) != 0) |
| | | 349 | linkup = true; |
| | | 350 | } |
| | | 351 | |
| | | 352 | return linkup; |
| | | 353 | } |
| | | 354 | |
348 | static void | | 355 | static void |
349 | rgephy_status(struct mii_softc *sc) | | 356 | rgephy_status(struct mii_softc *sc) |
350 | { | | 357 | { |
351 | struct mii_data *mii = sc->mii_pdata; | | 358 | struct mii_data *mii = sc->mii_pdata; |
352 | uint16_t gstat, bmsr, bmcr, physr, ssr; | | 359 | uint16_t gstat, bmsr, bmcr, physr, ssr; |
353 | | | 360 | |
354 | mii->mii_media_status = IFM_AVALID; | | 361 | mii->mii_media_status = IFM_AVALID; |
355 | mii->mii_media_active = IFM_ETHER; | | 362 | mii->mii_media_active = IFM_ETHER; |
356 | | | 363 | |
357 | if (sc->mii_mpd_rev >= RGEPHY_8211F) { | | 364 | if (rgephy_linkup(sc)) |
358 | PHY_READ(sc, RGEPHY_MII_PHYSR, &physr); | | 365 | mii->mii_media_status |= IFM_ACTIVE; |
359 | if (physr & RGEPHY_PHYSR_LINK) | | | |
360 | mii->mii_media_status |= IFM_ACTIVE; | | | |
361 | } else if (sc->mii_mpd_rev >= RGEPHY_8211B) { | | | |
362 | PHY_READ(sc, RGEPHY_MII_SSR, &ssr); | | | |
363 | if (ssr & RGEPHY_SSR_LINK) | | | |
364 | mii->mii_media_status |= IFM_ACTIVE; | | | |
365 | } else { | | | |
366 | PHY_READ(sc, RTK_GMEDIASTAT, &gstat); | | | |
367 | if ((gstat & RTK_GMEDIASTAT_LINK) != 0) | | | |
368 | mii->mii_media_status |= IFM_ACTIVE; | | | |
369 | } | | | |
370 | | | 366 | |
371 | PHY_READ(sc, MII_BMSR, &bmsr); | | 367 | PHY_READ(sc, MII_BMSR, &bmsr); |
372 | PHY_READ(sc, MII_BMCR, &bmcr); | | 368 | PHY_READ(sc, MII_BMCR, &bmcr); |
373 | | | 369 | |
374 | if ((bmcr & BMCR_ISO) != 0) { | | 370 | if ((bmcr & BMCR_ISO) != 0) { |
375 | mii->mii_media_active |= IFM_NONE; | | 371 | mii->mii_media_active |= IFM_NONE; |
376 | mii->mii_media_status = 0; | | 372 | mii->mii_media_status = 0; |
377 | return; | | 373 | return; |
378 | } | | 374 | } |
379 | | | 375 | |
380 | if ((bmcr & BMCR_LOOP) != 0) | | 376 | if ((bmcr & BMCR_LOOP) != 0) |
381 | mii->mii_media_active |= IFM_LOOP; | | 377 | mii->mii_media_active |= IFM_LOOP; |
382 | | | 378 | |