makphy.c revision 1.42.8.2 1 /* $NetBSD: makphy.c,v 1.42.8.2 2019/03/07 17:19:38 martin 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.42.8.2 2019/03/07 17:19:38 martin 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
80 static int makphymatch(device_t, cfdata_t, void *);
81 static void makphyattach(device_t, device_t, void *);
82
83 CFATTACH_DECL_NEW(makphy, sizeof(struct mii_softc),
84 makphymatch, makphyattach, mii_phy_detach, mii_phy_activate);
85
86 static int makphy_service(struct mii_softc *, struct mii_data *, int);
87 static void makphy_status(struct mii_softc *);
88 static void makphy_reset(struct mii_softc *);
89
90 static const struct mii_phy_funcs makphy_funcs = {
91 makphy_service, makphy_status, makphy_reset,
92 };
93
94 static const struct mii_phydesc makphys[] = {
95 { MII_OUI_MARVELL, MII_MODEL_MARVELL_E1000_0,
96 MII_STR_MARVELL_E1000_0 },
97
98 { MII_OUI_MARVELL, MII_MODEL_MARVELL_E1000_3,
99 MII_STR_MARVELL_E1000_3 },
100
101 { MII_OUI_MARVELL, MII_MODEL_MARVELL_E1000_5,
102 MII_STR_MARVELL_E1000_5 },
103
104 { MII_OUI_MARVELL, MII_MODEL_MARVELL_E1000_6,
105 MII_STR_MARVELL_E1000_6 },
106
107 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1000_3,
108 MII_STR_xxMARVELL_E1000_3 },
109
110 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1000_5,
111 MII_STR_xxMARVELL_E1000_5 },
112
113 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1000S,
114 MII_STR_xxMARVELL_E1000S },
115
116 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1011,
117 MII_STR_xxMARVELL_E1011 },
118
119 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1111,
120 MII_STR_xxMARVELL_E1111 },
121
122 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1112,
123 MII_STR_xxMARVELL_E1112 },
124
125 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1116,
126 MII_STR_xxMARVELL_E1116 },
127
128 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1116R,
129 MII_STR_xxMARVELL_E1116R },
130
131 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1318S,
132 MII_STR_xxMARVELL_E1318S },
133
134 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1118,
135 MII_STR_xxMARVELL_E1118 },
136
137 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1145,
138 MII_STR_xxMARVELL_E1145 },
139
140 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1149,
141 MII_STR_xxMARVELL_E1149 },
142
143 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1149R,
144 MII_STR_xxMARVELL_E1149R },
145
146 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1240,
147 MII_STR_xxMARVELL_E1240 },
148
149 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1512,
150 MII_STR_xxMARVELL_E1512 },
151
152 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E1543,
153 MII_STR_xxMARVELL_E1543 },
154
155 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E3016,
156 MII_STR_xxMARVELL_E3016 },
157
158 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E3082,
159 MII_STR_xxMARVELL_E3082 },
160
161 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_PHYG65G,
162 MII_STR_xxMARVELL_PHYG65G },
163
164 { 0, 0,
165 NULL },
166 };
167
168 #define MAKARG_PDOWN true /* Power DOWN */
169 #define MAKARG_PUP false /* Power UP */
170
171 static int
172 makphymatch(device_t parent, cfdata_t match, void *aux)
173 {
174 struct mii_attach_args *ma = aux;
175
176 if (mii_phy_match(ma, makphys) != NULL)
177 return (10);
178
179 return (0);
180 }
181
182 static void
183 makphyattach(device_t parent, device_t self, void *aux)
184 {
185 struct mii_softc *sc = device_private(self);
186 struct mii_attach_args *ma = aux;
187 struct mii_data *mii = ma->mii_data;
188 const struct mii_phydesc *mpd;
189
190 mpd = mii_phy_match(ma, makphys);
191 aprint_naive(": Media interface\n");
192 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
193
194 sc->mii_dev = self;
195 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
196 sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
197 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
198 sc->mii_inst = mii->mii_instance;
199 sc->mii_phy = ma->mii_phyno;
200 sc->mii_funcs = &makphy_funcs;
201 sc->mii_pdata = mii;
202 sc->mii_flags = ma->mii_flags;
203 sc->mii_anegticks = MII_ANEGTICKS;
204
205 /* Make sure page 0 is selected. */
206 PHY_WRITE(sc, MAKPHY_EADR, 0);
207
208 switch (sc->mii_mpd_model) {
209 case MII_MODEL_xxMARVELL_E1011:
210 case MII_MODEL_xxMARVELL_E1112:
211 if (PHY_READ(sc, MAKPHY_ESSR) & ESSR_FIBER_LINK)
212 sc->mii_flags |= MIIF_HAVEFIBER;
213 break;
214 default:
215 break;
216 }
217
218 PHY_RESET(sc);
219
220 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
221 if (sc->mii_capabilities & BMSR_EXTSTAT)
222 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
223
224 aprint_normal_dev(self, "");
225 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
226 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
227 aprint_error("no media present");
228 else
229 mii_phy_add_media(sc);
230 aprint_normal("\n");
231 }
232
233 static void
234 makphy_reset(struct mii_softc *sc)
235 {
236 uint16_t reg;
237
238 mii_phy_reset(sc);
239
240 /*
241 * Initialize PHY Specific Control Register.
242 */
243 reg = PHY_READ(sc, MAKPHY_PSCR);
244
245 /* Assert CRS on transmit. */
246 switch (sc->mii_mpd_model) {
247 case MII_MODEL_MARVELL_E1000_0:
248 case MII_MODEL_MARVELL_E1000_3:
249 case MII_MODEL_MARVELL_E1000_5:
250 case MII_MODEL_MARVELL_E1000_6:
251 case MII_MODEL_xxMARVELL_E1000S:
252 case MII_MODEL_xxMARVELL_E1011:
253 case MII_MODEL_xxMARVELL_E1111:
254 reg |= PSCR_CRS_ON_TX;
255 break;
256 default: /* No PSCR_CRS_ON_TX bit */
257 break;
258 }
259
260 /* Enable scrambler if necessary. */
261 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
262 reg &= ~E3016_PSCR_SCRAMBLE_DIS;
263
264 /*
265 * Store next page in the Link Partner Next Page register for
266 * compatibility with 802.3ab.
267 */
268 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
269 reg |= E3016_PSCR_REG8NXTPG;
270
271 PHY_WRITE(sc, MAKPHY_PSCR, reg);
272
273 /* Configure LEDs if they were left unconfigured. */
274 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016 &&
275 PHY_READ(sc, 0x16) == 0) {
276 reg = (0x0b << 8) | (0x05 << 4) | 0x04; /* XXX */
277 PHY_WRITE(sc, 0x16, reg);
278 }
279
280 mii_phy_reset(sc);
281 }
282
283 static void
284 makphy_pdown(struct mii_softc *sc, bool pdown)
285 {
286 int bmcr, new;
287 bool need_reset = false;
288
289 /*
290 * XXX
291 * PSCR (register 16) should be modified on some chips.
292 */
293
294 bmcr = PHY_READ(sc, MII_BMCR);
295 if (pdown)
296 new = bmcr | BMCR_PDOWN;
297 else
298 new = bmcr & ~BMCR_PDOWN;
299 if (bmcr != new)
300 need_reset = true;
301
302 if (need_reset)
303 new |= BMCR_RESET;
304 PHY_WRITE(sc, MII_BMCR, new);
305 }
306
307 static int
308 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
309 {
310 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
311 int bmcr;
312
313 if (!device_is_active(sc->mii_dev))
314 return (ENXIO);
315
316 switch (cmd) {
317 case MII_POLLSTAT:
318 /*
319 * If we're not polling our PHY instance, just return.
320 */
321 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
322 return (0);
323 break;
324
325 case MII_MEDIACHG:
326 /*
327 * If the media indicates a different PHY instance,
328 * isolate ourselves.
329 */
330 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
331 bmcr = PHY_READ(sc, MII_BMCR);
332 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
333 return (0);
334 }
335
336 /*
337 * If the interface is not up, don't do anything.
338 */
339 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
340 break;
341
342 /* Try to power up the PHY in case it's down */
343 if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
344 makphy_pdown(sc, MAKARG_PUP);
345
346 mii_phy_setmedia(sc);
347
348 /*
349 * If autonegitation is not enabled, we need a
350 * software reset for the settings to take effect.
351 */
352 if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
353 makphy_pdown(sc, MAKARG_PDOWN);
354 else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
355 bmcr = PHY_READ(sc, MII_BMCR);
356 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
357 }
358 break;
359
360 case MII_TICK:
361 /*
362 * If we're not currently selected, just return.
363 */
364 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
365 return (0);
366
367 if (mii_phy_tick(sc) == EJUSTRETURN)
368 return (0);
369 break;
370
371 case MII_DOWN:
372 mii_phy_down(sc);
373 return (0);
374 }
375
376 /* Update the media status. */
377 mii_phy_status(sc);
378
379 /* Callback if something changed. */
380 mii_phy_update(sc, cmd);
381 return (0);
382 }
383
384 static void
385 makphy_status(struct mii_softc *sc)
386 {
387 struct mii_data *mii = sc->mii_pdata;
388 int bmcr, gsr, pssr;
389
390 mii->mii_media_status = IFM_AVALID;
391 mii->mii_media_active = IFM_ETHER;
392
393 bmcr = PHY_READ(sc, MII_BMCR);
394 /* XXX FIXME: Use different page for Fiber on newer chips */
395 pssr = PHY_READ(sc, MAKPHY_PSSR);
396
397 if (pssr & PSSR_LINK)
398 mii->mii_media_status |= IFM_ACTIVE;
399
400 if (bmcr & BMCR_LOOP)
401 mii->mii_media_active |= IFM_LOOP;
402
403 if (bmcr & BMCR_ISO) {
404 mii->mii_media_active |= IFM_NONE;
405 mii->mii_media_status = 0;
406 return;
407 }
408
409 if ((bmcr & BMCR_AUTOEN) != 0) {
410 /*
411 * Check Speed and Duplex Resolved bit.
412 * Note that this bit is always 1 when autonego is not enabled.
413 */
414 if (!(pssr & PSSR_RESOLVED)) {
415 /* Erg, still trying, I guess... */
416 mii->mii_media_active |= IFM_NONE;
417 return;
418 }
419 } else {
420 if ((pssr & PSSR_LINK) == 0) {
421 mii->mii_media_active |= IFM_NONE;
422 return;
423 }
424 }
425
426 /* XXX FIXME: Use different page for Fiber on newer chips */
427 if (sc->mii_flags & MIIF_IS_1000X) {
428 mii->mii_media_active |= IFM_1000_SX;
429 } else {
430 switch (PSSR_SPEED_get(pssr)) {
431 case SPEED_1000:
432 mii->mii_media_active |= IFM_1000_T;
433 break;
434 case SPEED_100:
435 mii->mii_media_active |= IFM_100_TX;
436 break;
437 case SPEED_10:
438 mii->mii_media_active |= IFM_10_T;
439 break;
440 default: /* Undefined (reserved) value */
441 mii->mii_media_active |= IFM_NONE;
442 mii->mii_media_status = 0;
443 return;
444 }
445 }
446
447 if (pssr & PSSR_DUPLEX)
448 mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
449 else
450 mii->mii_media_active |= IFM_HDX;
451
452 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
453 gsr = PHY_READ(sc, MII_100T2SR);
454 if (gsr & GTSR_MS_RES)
455 mii->mii_media_active |= IFM_ETH_MASTER;
456 }
457 }
458