makphy.c revision 1.65 1 /* $NetBSD: makphy.c,v 1.65 2020/03/15 23:04:50 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 /*
58 * Driver for the Marvell 88E1000 ``Alaska'' 10/100/1000 PHY.
59 */
60
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: makphy.c,v 1.65 2020/03/15 23:04:50 thorpej Exp $");
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/device.h>
68 #include <sys/socket.h>
69 #include <sys/errno.h>
70
71 #include <net/if.h>
72 #include <net/if_media.h>
73
74 #include <dev/mii/mii.h>
75 #include <dev/mii/miivar.h>
76 #include <dev/mii/miidevs.h>
77
78 #include <dev/mii/makphyreg.h>
79 #include <dev/mii/makphyvar.h>
80
81 static int makphymatch(device_t, cfdata_t, void *);
82 static void makphyattach(device_t, device_t, void *);
83
84 CFATTACH_DECL_NEW(makphy, sizeof(struct makphy_softc),
85 makphymatch, makphyattach, mii_phy_detach, mii_phy_activate);
86
87 static int makphy_service(struct mii_softc *, struct mii_data *, int);
88 static void makphy_status(struct mii_softc *);
89 static void makphy_reset(struct mii_softc *);
90
91 static const struct mii_phy_funcs makphy_funcs = {
92 makphy_service, makphy_status, makphy_reset,
93 };
94
95 static const struct mii_phydesc makphys[] = {
96 MII_PHY_DESC(MARVELL, E1000_0),
97 MII_PHY_DESC(MARVELL, E1000_3),
98 MII_PHY_DESC(MARVELL, E1000_5),
99 MII_PHY_DESC(MARVELL, E1000_6),
100 MII_PHY_DESC(xxMARVELL, E1000_3),
101 MII_PHY_DESC(xxMARVELL, E1000_5),
102 MII_PHY_DESC(xxMARVELL, E1000S),
103 MII_PHY_DESC(xxMARVELL, E1011),
104 MII_PHY_DESC(xxMARVELL, E1101),
105 MII_PHY_DESC(xxMARVELL, E1111),
106 MII_PHY_DESC(xxMARVELL, E1112),
107 MII_PHY_DESC(xxMARVELL, E1116),
108 MII_PHY_DESC(xxMARVELL, E1116R),
109 MII_PHY_DESC(xxMARVELL, E1118),
110 MII_PHY_DESC(xxMARVELL, E1145),
111 MII_PHY_DESC(xxMARVELL, E1149),
112 MII_PHY_DESC(xxMARVELL, E1149R),
113 MII_PHY_DESC(xxMARVELL, E1240),
114 MII_PHY_DESC(xxMARVELL, E1318S),
115 MII_PHY_DESC(xxMARVELL, E1512),
116 MII_PHY_DESC(xxMARVELL, E1543),
117 MII_PHY_DESC(xxMARVELL, E3016),
118 MII_PHY_DESC(xxMARVELL, E3082),
119 MII_PHY_DESC(xxMARVELL, PHYG65G),
120 MII_PHY_END,
121 };
122
123 #define MAKARG_PDOWN true /* Power DOWN */
124 #define MAKARG_PUP false /* Power UP */
125
126 static bool
127 makphy_isi210(device_t parent, struct mii_attach_args *ma)
128 {
129
130 /* I21[01]'s model number is 0 */
131 if ((MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxMARVELL)
132 && (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_I210)
133 && (device_is_a(parent, "wm")))
134 return true;
135 return false;
136 }
137
138 static int
139 makphymatch(device_t parent, cfdata_t match, void *aux)
140 {
141 struct mii_attach_args *ma = aux;
142
143 if (mii_phy_match(ma, makphys) != NULL)
144 return 10;
145
146 if (makphy_isi210(parent, ma))
147 return 10;
148
149 return 0;
150 }
151
152 static void
153 makphyattach(device_t parent, device_t self, void *aux)
154 {
155 struct mii_softc *sc = device_private(self);
156 struct mii_attach_args *ma = aux;
157 struct mii_data *mii = ma->mii_data;
158 const struct mii_phydesc *mpd;
159 struct makphy_softc *maksc = (struct makphy_softc *)sc;
160 const char *name;
161 uint16_t reg, model;
162
163 mpd = mii_phy_match(ma, makphys);
164 aprint_naive(": Media interface\n");
165 if (mpd)
166 name = mpd->mpd_name;
167 else if (makphy_isi210(parent, ma)) {
168 name = MII_STR_xxMARVELL_I210;
169 maksc->sc_flags |= MAKPHY_F_I210;
170 } else
171 panic("Unknown PHY");
172 aprint_normal(": %s, rev. %d\n", name, MII_REV(ma->mii_id2));
173
174 sc->mii_dev = self;
175 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
176 sc->mii_mpd_model = model = MII_MODEL(ma->mii_id2);
177 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
178 sc->mii_inst = mii->mii_instance;
179 sc->mii_phy = ma->mii_phyno;
180 sc->mii_funcs = &makphy_funcs;
181 sc->mii_pdata = mii;
182 sc->mii_flags = ma->mii_flags;
183
184 mii_lock(mii);
185
186 switch (model) {
187 case MII_MODEL_xxMARVELL_E1000:
188 if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
189 goto page0;
190 /* FALLTHROUGH */
191 case MII_MODEL_xxMARVELL_E1000_3:
192 case MII_MODEL_xxMARVELL_E1000S:
193 case MII_MODEL_xxMARVELL_E1000_5:
194 /* 88E1000 series has no EADR */
195 break;
196 default:
197 page0:
198 /* Make sure page 0 is selected. */
199 if (PHY_WRITE(sc, MAKPHY_EADR, 0) != 0)
200 aprint_verbose_dev(self,
201 "Failed to access EADR. Are you an emulator?\n");
202 break;
203 }
204
205 PHY_RESET(sc);
206
207 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
208 sc->mii_capabilities &= ma->mii_capmask;
209 if (sc->mii_capabilities & BMSR_EXTSTAT)
210 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
211
212 if (((sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
213 != 0)
214 && ((sc->mii_extcapabilities & (EXTSR_1000XFDX | EXTSR_1000XHDX))
215 != 0)) {
216 bool fiberonly = false, copperonly = false;
217
218 /* Both copper and fiber are set. check MODE[] */
219 switch (sc->mii_mpd_model) {
220 case MII_MODEL_xxMARVELL_E1011:
221 case MII_MODEL_xxMARVELL_E1111:
222 /* These devices have ESSR register */
223 PHY_READ(sc, MAKPHY_ESSR, ®);
224 if ((reg & ESSR_AUTOSEL_DISABLE) != 0) {
225 switch (reg & ESSR_HWCFG_MODE) {
226 case ESSR_RTBI_FIBER:
227 case ESSR_RGMII_FIBER:
228 case ESSR_RGMII_SGMII: /* right? */
229 case ESSR_TBI_FIBER:
230 case ESSR_GMII_FIBER:
231 fiberonly = true;
232 break;
233 case ESSR_SGMII_WC_COPPER:
234 case ESSR_SGMII_WOC_COPPER:
235 case ESSR_RTBI_COPPER:
236 case ESSR_RGMII_COPPER:
237 case ESSR_GMII_COPPER:
238 copperonly = true;
239 default:
240 break;
241 }
242 }
243 break;
244 default:
245 break;
246 }
247 if (fiberonly || copperonly)
248 aprint_debug_dev(self, "both copper and fiber are set "
249 "but MODE[] is %s only.\n",
250 fiberonly ? "fiber" : "copper");
251 if (fiberonly)
252 sc->mii_extcapabilities
253 &= ~(EXTSR_1000TFDX | EXTSR_1000THDX);
254 else if (copperonly) {
255 sc->mii_extcapabilities
256 &= ~(EXTSR_1000XFDX | EXTSR_1000XHDX);
257 sc->mii_flags &= ~MIIF_IS_1000X;
258 }
259 }
260 mii_unlock(mii);
261 mii_phy_add_media(sc);
262 }
263
264 static void
265 makphy_reset(struct mii_softc *sc)
266 {
267 struct makphy_softc *maksc = (struct makphy_softc *)sc;
268 uint16_t reg;
269
270 KASSERT(mii_locked(sc->mii_pdata));
271
272 mii_phy_reset(sc);
273
274 /* Initialize PHY Specific Control Register. */
275 PHY_READ(sc, MAKPHY_PSCR, ®);
276
277 /* Assert CRS on transmit. */
278 switch (sc->mii_mpd_model) {
279 case MII_MODEL_MARVELL_E1000_0:
280 if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
281 break;
282 /* FALLTHROUGH */
283 case MII_MODEL_MARVELL_E1000_3:
284 case MII_MODEL_MARVELL_E1000_5:
285 case MII_MODEL_MARVELL_E1000_6:
286 case MII_MODEL_xxMARVELL_E1000S:
287 case MII_MODEL_xxMARVELL_E1011:
288 case MII_MODEL_xxMARVELL_E1111:
289 reg |= PSCR_CRS_ON_TX;
290 break;
291 default: /* No PSCR_CRS_ON_TX bit */
292 break;
293 }
294
295 /* Enable scrambler if necessary. */
296 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
297 reg &= ~E3016_PSCR_SCRAMBLE_DIS;
298
299 /*
300 * Store next page in the Link Partner Next Page register for
301 * compatibility with 802.3ab.
302 */
303 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
304 reg |= E3016_PSCR_REG8NXTPG;
305
306 PHY_WRITE(sc, MAKPHY_PSCR, reg);
307
308 /* Configure LEDs if they were left unconfigured. */
309 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016) {
310 PHY_READ(sc, 0x16, ®);
311 if (reg == 0) {
312 reg = (0x0b << 8) | (0x05 << 4) | 0x04; /* XXX */
313 PHY_WRITE(sc, 0x16, reg);
314 }
315 }
316
317 mii_phy_reset(sc);
318 }
319
320 static void
321 makphy_pdown(struct mii_softc *sc, bool pdown)
322 {
323 uint16_t bmcr, new;
324 bool need_reset = false;
325
326 /*
327 * XXX
328 * PSCR (register 16) should be modified on some chips.
329 */
330
331 PHY_READ(sc, MII_BMCR, &bmcr);
332 if (pdown)
333 new = bmcr | BMCR_PDOWN;
334 else
335 new = bmcr & ~BMCR_PDOWN;
336 if (bmcr != new)
337 need_reset = true;
338
339 if (need_reset)
340 new |= BMCR_RESET;
341 PHY_WRITE(sc, MII_BMCR, new);
342 }
343
344 static int
345 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
346 {
347 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
348 uint16_t bmcr;
349
350 if (!device_is_active(sc->mii_dev))
351 return ENXIO;
352
353 KASSERT(mii_locked(mii));
354
355 switch (cmd) {
356 case MII_POLLSTAT:
357 /* If we're not polling our PHY instance, just return. */
358 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
359 return 0;
360 break;
361
362 case MII_MEDIACHG:
363 /*
364 * If the media indicates a different PHY instance,
365 * isolate ourselves.
366 */
367 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
368 PHY_READ(sc, MII_BMCR, &bmcr);
369 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
370 return 0;
371 }
372
373 /* If the interface is not up, don't do anything. */
374 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
375 break;
376
377 /* Try to power up the PHY in case it's down */
378 if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
379 makphy_pdown(sc, MAKARG_PUP);
380
381 mii_phy_setmedia(sc);
382
383 /*
384 * If autonegitation is not enabled, we need a
385 * software reset for the settings to take effect.
386 */
387 if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
388 makphy_pdown(sc, MAKARG_PDOWN);
389 else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
390 PHY_READ(sc, MII_BMCR, &bmcr);
391 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
392 }
393 break;
394
395 case MII_TICK:
396 /* If we're not currently selected, just return. */
397 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
398 return 0;
399
400 if (mii_phy_tick(sc) == EJUSTRETURN)
401 return 0;
402 break;
403
404 case MII_DOWN:
405 mii_phy_down(sc);
406 return 0;
407 }
408
409 /* Update the media status. */
410 mii_phy_status(sc);
411
412 /* Callback if something changed. */
413 mii_phy_update(sc, cmd);
414 return 0;
415 }
416
417 static void
418 makphy_status(struct mii_softc *sc)
419 {
420 struct mii_data *mii = sc->mii_pdata;
421 uint16_t bmcr, gsr, pssr, essr;
422
423 KASSERT(mii_locked(mii));
424
425 mii->mii_media_status = IFM_AVALID;
426 mii->mii_media_active = IFM_ETHER;
427
428 PHY_READ(sc, MII_BMCR, &bmcr);
429 /* XXX FIXME: Use different page for Fiber on newer chips */
430 PHY_READ(sc, MAKPHY_PSSR, &pssr);
431
432 if (pssr & PSSR_LINK)
433 mii->mii_media_status |= IFM_ACTIVE;
434
435 if (bmcr & BMCR_LOOP)
436 mii->mii_media_active |= IFM_LOOP;
437
438 if (bmcr & BMCR_ISO) {
439 mii->mii_media_active |= IFM_NONE;
440 mii->mii_media_status = 0;
441 return;
442 }
443
444 if ((bmcr & BMCR_AUTOEN) != 0) {
445 /*
446 * Check Speed and Duplex Resolved bit.
447 * Note that this bit is always 1 when autonego is not enabled.
448 */
449 if (!(pssr & PSSR_RESOLVED)) {
450 /* Erg, still trying, I guess... */
451 mii->mii_media_active |= IFM_NONE;
452 return;
453 }
454 } else {
455 if ((pssr & PSSR_LINK) == 0) {
456 mii->mii_media_active |= IFM_NONE;
457 return;
458 }
459 }
460
461 /*
462 * XXX The following code support Fiber/Copper auto select mode
463 * only for 88E1011, 88E1111 and 88E1112. For other chips, the document
464 * is required.
465 */
466 if (sc->mii_flags & MIIF_IS_1000X) {
467 /* Not in Fiber/Copper auto select mode */
468 mii->mii_media_active |= IFM_1000_SX;
469 } else if ((sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1011) ||
470 (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1111)) {
471 /* Fiber/Copper auto select mode */
472
473 PHY_READ(sc, MAKPHY_ESSR, &essr);
474 if ((essr & ESSR_FIBER_LINK) == 0)
475 goto copper;
476
477 /* XXX Assume 1000BASE-SX only */
478 mii->mii_media_active |= IFM_1000_SX;
479 } else if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1112) {
480 /* Fiber/Copper auto select mode */
481
482 PHY_READ(sc, MAKPHY_PSSR, &pssr);
483 if ((pssr & PSSR_RESOLUTION_FIBER) == 0)
484 goto copper;
485
486 switch (PSSR_SPEED_get(pssr)) {
487 case SPEED_1000:
488 mii->mii_media_active |= IFM_1000_SX;
489 break;
490 case SPEED_100:
491 mii->mii_media_active |= IFM_100_FX;
492 break;
493 default: /* Undefined (reserved) value */
494 mii->mii_media_active |= IFM_NONE;
495 mii->mii_media_status = 0;
496 return;
497 }
498 } else {
499 copper:
500 switch (PSSR_SPEED_get(pssr)) {
501 case SPEED_1000:
502 mii->mii_media_active |= IFM_1000_T;
503 break;
504 case SPEED_100:
505 mii->mii_media_active |= IFM_100_TX;
506 break;
507 case SPEED_10:
508 mii->mii_media_active |= IFM_10_T;
509 break;
510 default: /* Undefined (reserved) value */
511 mii->mii_media_active |= IFM_NONE;
512 mii->mii_media_status = 0;
513 return;
514 }
515 }
516
517 if (pssr & PSSR_DUPLEX)
518 mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
519 else
520 mii->mii_media_active |= IFM_HDX;
521
522 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
523 PHY_READ(sc, MII_100T2SR, &gsr);
524 if (gsr & GTSR_MS_RES)
525 mii->mii_media_active |= IFM_ETH_MASTER;
526 }
527 }
528