makphy.c revision 1.63.2.1 1 /* $NetBSD: makphy.c,v 1.63.2.1 2020/02/29 20:19:09 ad 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.63.2.1 2020/02/29 20:19:09 ad 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 switch (model) {
185 case MII_MODEL_xxMARVELL_E1000:
186 if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
187 goto page0;
188 /* FALLTHROUGH */
189 case MII_MODEL_xxMARVELL_E1000_3:
190 case MII_MODEL_xxMARVELL_E1000S:
191 case MII_MODEL_xxMARVELL_E1000_5:
192 /* 88E1000 series has no EADR */
193 break;
194 default:
195 page0:
196 /* Make sure page 0 is selected. */
197 if (PHY_WRITE(sc, MAKPHY_EADR, 0) != 0)
198 aprint_verbose_dev(self,
199 "Failed to access EADR. Are you an emulator?\n");
200 break;
201 }
202
203 PHY_RESET(sc);
204
205 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
206 sc->mii_capabilities &= ma->mii_capmask;
207 if (sc->mii_capabilities & BMSR_EXTSTAT)
208 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
209
210 if (((sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
211 != 0)
212 && ((sc->mii_extcapabilities & (EXTSR_1000XFDX | EXTSR_1000XHDX))
213 != 0)) {
214 bool fiberonly = false, copperonly = false;
215
216 /* Both copper and fiber are set. check MODE[] */
217 switch (sc->mii_mpd_model) {
218 case MII_MODEL_xxMARVELL_E1011:
219 case MII_MODEL_xxMARVELL_E1111:
220 /* These devices have ESSR register */
221 PHY_READ(sc, MAKPHY_ESSR, ®);
222 if ((reg & ESSR_AUTOSEL_DISABLE) != 0) {
223 switch (reg & ESSR_HWCFG_MODE) {
224 case ESSR_RTBI_FIBER:
225 case ESSR_RGMII_FIBER:
226 case ESSR_RGMII_SGMII: /* right? */
227 case ESSR_TBI_FIBER:
228 case ESSR_GMII_FIBER:
229 fiberonly = true;
230 break;
231 case ESSR_SGMII_WC_COPPER:
232 case ESSR_SGMII_WOC_COPPER:
233 case ESSR_RTBI_COPPER:
234 case ESSR_RGMII_COPPER:
235 case ESSR_GMII_COPPER:
236 copperonly = true;
237 default:
238 break;
239 }
240 }
241 break;
242 default:
243 break;
244 }
245 if (fiberonly || copperonly)
246 aprint_debug_dev(self, "both copper and fiber are set "
247 "but MODE[] is %s only.\n",
248 fiberonly ? "fiber" : "copper");
249 if (fiberonly)
250 sc->mii_extcapabilities
251 &= ~(EXTSR_1000TFDX | EXTSR_1000THDX);
252 else if (copperonly) {
253 sc->mii_extcapabilities
254 &= ~(EXTSR_1000XFDX | EXTSR_1000XHDX);
255 sc->mii_flags &= ~MIIF_IS_1000X;
256 }
257 }
258 mii_phy_add_media(sc);
259 }
260
261 static void
262 makphy_reset(struct mii_softc *sc)
263 {
264 struct makphy_softc *maksc = (struct makphy_softc *)sc;
265 uint16_t reg;
266
267 mii_phy_reset(sc);
268
269 /* Initialize PHY Specific Control Register. */
270 PHY_READ(sc, MAKPHY_PSCR, ®);
271
272 /* Assert CRS on transmit. */
273 switch (sc->mii_mpd_model) {
274 case MII_MODEL_MARVELL_E1000_0:
275 if ((maksc->sc_flags & MAKPHY_F_I210) != 0)
276 break;
277 /* FALLTHROUGH */
278 case MII_MODEL_MARVELL_E1000_3:
279 case MII_MODEL_MARVELL_E1000_5:
280 case MII_MODEL_MARVELL_E1000_6:
281 case MII_MODEL_xxMARVELL_E1000S:
282 case MII_MODEL_xxMARVELL_E1011:
283 case MII_MODEL_xxMARVELL_E1111:
284 reg |= PSCR_CRS_ON_TX;
285 break;
286 default: /* No PSCR_CRS_ON_TX bit */
287 break;
288 }
289
290 /* Enable scrambler if necessary. */
291 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
292 reg &= ~E3016_PSCR_SCRAMBLE_DIS;
293
294 /*
295 * Store next page in the Link Partner Next Page register for
296 * compatibility with 802.3ab.
297 */
298 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
299 reg |= E3016_PSCR_REG8NXTPG;
300
301 PHY_WRITE(sc, MAKPHY_PSCR, reg);
302
303 /* Configure LEDs if they were left unconfigured. */
304 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016) {
305 PHY_READ(sc, 0x16, ®);
306 if (reg == 0) {
307 reg = (0x0b << 8) | (0x05 << 4) | 0x04; /* XXX */
308 PHY_WRITE(sc, 0x16, reg);
309 }
310 }
311
312 mii_phy_reset(sc);
313 }
314
315 static void
316 makphy_pdown(struct mii_softc *sc, bool pdown)
317 {
318 uint16_t bmcr, new;
319 bool need_reset = false;
320
321 /*
322 * XXX
323 * PSCR (register 16) should be modified on some chips.
324 */
325
326 PHY_READ(sc, MII_BMCR, &bmcr);
327 if (pdown)
328 new = bmcr | BMCR_PDOWN;
329 else
330 new = bmcr & ~BMCR_PDOWN;
331 if (bmcr != new)
332 need_reset = true;
333
334 if (need_reset)
335 new |= BMCR_RESET;
336 PHY_WRITE(sc, MII_BMCR, new);
337 }
338
339 static int
340 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
341 {
342 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
343 uint16_t bmcr;
344
345 if (!device_is_active(sc->mii_dev))
346 return ENXIO;
347
348 switch (cmd) {
349 case MII_POLLSTAT:
350 /* If we're not polling our PHY instance, just return. */
351 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
352 return 0;
353 break;
354
355 case MII_MEDIACHG:
356 /*
357 * If the media indicates a different PHY instance,
358 * isolate ourselves.
359 */
360 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
361 PHY_READ(sc, MII_BMCR, &bmcr);
362 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
363 return 0;
364 }
365
366 /* If the interface is not up, don't do anything. */
367 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
368 break;
369
370 /* Try to power up the PHY in case it's down */
371 if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
372 makphy_pdown(sc, MAKARG_PUP);
373
374 mii_phy_setmedia(sc);
375
376 /*
377 * If autonegitation is not enabled, we need a
378 * software reset for the settings to take effect.
379 */
380 if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
381 makphy_pdown(sc, MAKARG_PDOWN);
382 else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
383 PHY_READ(sc, MII_BMCR, &bmcr);
384 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
385 }
386 break;
387
388 case MII_TICK:
389 /* If we're not currently selected, just return. */
390 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
391 return 0;
392
393 if (mii_phy_tick(sc) == EJUSTRETURN)
394 return 0;
395 break;
396
397 case MII_DOWN:
398 mii_phy_down(sc);
399 return 0;
400 }
401
402 /* Update the media status. */
403 mii_phy_status(sc);
404
405 /* Callback if something changed. */
406 mii_phy_update(sc, cmd);
407 return 0;
408 }
409
410 static void
411 makphy_status(struct mii_softc *sc)
412 {
413 struct mii_data *mii = sc->mii_pdata;
414 uint16_t bmcr, gsr, pssr, essr;
415
416 mii->mii_media_status = IFM_AVALID;
417 mii->mii_media_active = IFM_ETHER;
418
419 PHY_READ(sc, MII_BMCR, &bmcr);
420 /* XXX FIXME: Use different page for Fiber on newer chips */
421 PHY_READ(sc, MAKPHY_PSSR, &pssr);
422
423 if (pssr & PSSR_LINK)
424 mii->mii_media_status |= IFM_ACTIVE;
425
426 if (bmcr & BMCR_LOOP)
427 mii->mii_media_active |= IFM_LOOP;
428
429 if (bmcr & BMCR_ISO) {
430 mii->mii_media_active |= IFM_NONE;
431 mii->mii_media_status = 0;
432 return;
433 }
434
435 if ((bmcr & BMCR_AUTOEN) != 0) {
436 /*
437 * Check Speed and Duplex Resolved bit.
438 * Note that this bit is always 1 when autonego is not enabled.
439 */
440 if (!(pssr & PSSR_RESOLVED)) {
441 /* Erg, still trying, I guess... */
442 mii->mii_media_active |= IFM_NONE;
443 return;
444 }
445 } else {
446 if ((pssr & PSSR_LINK) == 0) {
447 mii->mii_media_active |= IFM_NONE;
448 return;
449 }
450 }
451
452 /*
453 * XXX The following code support Fiber/Copper auto select mode
454 * only for 88E1011, 88E1111 and 88E1112. For other chips, the document
455 * is required.
456 */
457 if (sc->mii_flags & MIIF_IS_1000X) {
458 /* Not in Fiber/Copper auto select mode */
459 mii->mii_media_active |= IFM_1000_SX;
460 } else if ((sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1011) ||
461 (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1111)) {
462 /* Fiber/Copper auto select mode */
463
464 PHY_READ(sc, MAKPHY_ESSR, &essr);
465 if ((essr & ESSR_FIBER_LINK) == 0)
466 goto copper;
467
468 /* XXX Assume 1000BASE-SX only */
469 mii->mii_media_active |= IFM_1000_SX;
470 } else if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E1112) {
471 /* Fiber/Copper auto select mode */
472
473 PHY_READ(sc, MAKPHY_PSSR, &pssr);
474 if ((pssr & PSSR_RESOLUTION_FIBER) == 0)
475 goto copper;
476
477 switch (PSSR_SPEED_get(pssr)) {
478 case SPEED_1000:
479 mii->mii_media_active |= IFM_1000_SX;
480 break;
481 case SPEED_100:
482 mii->mii_media_active |= IFM_100_FX;
483 break;
484 default: /* Undefined (reserved) value */
485 mii->mii_media_active |= IFM_NONE;
486 mii->mii_media_status = 0;
487 return;
488 }
489 } else {
490 copper:
491 switch (PSSR_SPEED_get(pssr)) {
492 case SPEED_1000:
493 mii->mii_media_active |= IFM_1000_T;
494 break;
495 case SPEED_100:
496 mii->mii_media_active |= IFM_100_TX;
497 break;
498 case SPEED_10:
499 mii->mii_media_active |= IFM_10_T;
500 break;
501 default: /* Undefined (reserved) value */
502 mii->mii_media_active |= IFM_NONE;
503 mii->mii_media_status = 0;
504 return;
505 }
506 }
507
508 if (pssr & PSSR_DUPLEX)
509 mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
510 else
511 mii->mii_media_active |= IFM_HDX;
512
513 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
514 PHY_READ(sc, MII_100T2SR, &gsr);
515 if (gsr & GTSR_MS_RES)
516 mii->mii_media_active |= IFM_ETH_MASTER;
517 }
518 }
519