makphy.c revision 1.53 1 /* $NetBSD: makphy.c,v 1.53 2019/02/08 09:17:12 msaitoh 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.53 2019/02/08 09:17:12 msaitoh 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 uint16_t reg;
190
191 mpd = mii_phy_match(ma, makphys);
192 aprint_naive(": Media interface\n");
193 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
194
195 sc->mii_dev = self;
196 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
197 sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
198 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
199 sc->mii_inst = mii->mii_instance;
200 sc->mii_phy = ma->mii_phyno;
201 sc->mii_funcs = &makphy_funcs;
202 sc->mii_pdata = mii;
203 sc->mii_flags = ma->mii_flags;
204 sc->mii_anegticks = MII_ANEGTICKS;
205
206 /* Make sure page 0 is selected. */
207 PHY_WRITE(sc, MAKPHY_EADR, 0);
208
209 switch (sc->mii_mpd_model) {
210 case MII_MODEL_xxMARVELL_E1011:
211 case MII_MODEL_xxMARVELL_E1112:
212 PHY_READ(sc, MAKPHY_ESSR, ®);
213 if (reg & ESSR_FIBER_LINK)
214 sc->mii_flags |= MIIF_HAVEFIBER;
215 break;
216 default:
217 break;
218 }
219
220 PHY_RESET(sc);
221
222 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
223 sc->mii_capabilities &= ma->mii_capmask;
224 if (sc->mii_capabilities & BMSR_EXTSTAT)
225 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
226
227 aprint_normal_dev(self, "");
228 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
229 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
230 aprint_error("no media present");
231 else
232 mii_phy_add_media(sc);
233 aprint_normal("\n");
234 }
235
236 static void
237 makphy_reset(struct mii_softc *sc)
238 {
239 uint16_t reg;
240
241 mii_phy_reset(sc);
242
243 /*
244 * Initialize PHY Specific Control Register.
245 */
246 PHY_READ(sc, MAKPHY_PSCR, ®);
247
248 /* Assert CRS on transmit. */
249 switch (sc->mii_mpd_model) {
250 case MII_MODEL_MARVELL_E1000_0:
251 case MII_MODEL_MARVELL_E1000_3:
252 case MII_MODEL_MARVELL_E1000_5:
253 case MII_MODEL_MARVELL_E1000_6:
254 case MII_MODEL_xxMARVELL_E1000S:
255 case MII_MODEL_xxMARVELL_E1011:
256 case MII_MODEL_xxMARVELL_E1111:
257 reg |= PSCR_CRS_ON_TX;
258 break;
259 default: /* No PSCR_CRS_ON_TX bit */
260 break;
261 }
262
263 /* Enable scrambler if necessary. */
264 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
265 reg &= ~E3016_PSCR_SCRAMBLE_DIS;
266
267 /*
268 * Store next page in the Link Partner Next Page register for
269 * compatibility with 802.3ab.
270 */
271 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016)
272 reg |= E3016_PSCR_REG8NXTPG;
273
274 PHY_WRITE(sc, MAKPHY_PSCR, reg);
275
276 /* Configure LEDs if they were left unconfigured. */
277 if (sc->mii_mpd_model == MII_MODEL_xxMARVELL_E3016) {
278 PHY_READ(sc, 0x16, ®);
279 if (reg == 0) {
280 reg = (0x0b << 8) | (0x05 << 4) | 0x04; /* XXX */
281 PHY_WRITE(sc, 0x16, reg);
282 }
283 }
284
285 mii_phy_reset(sc);
286 }
287
288 static void
289 makphy_pdown(struct mii_softc *sc, bool pdown)
290 {
291 uint16_t bmcr, new;
292 bool need_reset = false;
293
294 /*
295 * XXX
296 * PSCR (register 16) should be modified on some chips.
297 */
298
299 PHY_READ(sc, MII_BMCR, &bmcr);
300 if (pdown)
301 new = bmcr | BMCR_PDOWN;
302 else
303 new = bmcr & ~BMCR_PDOWN;
304 if (bmcr != new)
305 need_reset = true;
306
307 if (need_reset)
308 new |= BMCR_RESET;
309 PHY_WRITE(sc, MII_BMCR, new);
310 }
311
312 static int
313 makphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
314 {
315 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
316 uint16_t bmcr;
317
318 if (!device_is_active(sc->mii_dev))
319 return (ENXIO);
320
321 switch (cmd) {
322 case MII_POLLSTAT:
323 /*
324 * If we're not polling our PHY instance, just return.
325 */
326 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
327 return (0);
328 break;
329
330 case MII_MEDIACHG:
331 /*
332 * If the media indicates a different PHY instance,
333 * isolate ourselves.
334 */
335 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
336 PHY_READ(sc, MII_BMCR, &bmcr);
337 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
338 return (0);
339 }
340
341 /*
342 * If the interface is not up, don't do anything.
343 */
344 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
345 break;
346
347 /* Try to power up the PHY in case it's down */
348 if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
349 makphy_pdown(sc, MAKARG_PUP);
350
351 mii_phy_setmedia(sc);
352
353 /*
354 * If autonegitation is not enabled, we need a
355 * software reset for the settings to take effect.
356 */
357 if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
358 makphy_pdown(sc, MAKARG_PDOWN);
359 else if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
360 PHY_READ(sc, MII_BMCR, &bmcr);
361 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET);
362 }
363 break;
364
365 case MII_TICK:
366 /*
367 * If we're not currently selected, just return.
368 */
369 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
370 return (0);
371
372 if (mii_phy_tick(sc) == EJUSTRETURN)
373 return (0);
374 break;
375
376 case MII_DOWN:
377 mii_phy_down(sc);
378 return (0);
379 }
380
381 /* Update the media status. */
382 mii_phy_status(sc);
383
384 /* Callback if something changed. */
385 mii_phy_update(sc, cmd);
386 return (0);
387 }
388
389 static void
390 makphy_status(struct mii_softc *sc)
391 {
392 struct mii_data *mii = sc->mii_pdata;
393 uint16_t bmcr, gsr, pssr;
394
395 mii->mii_media_status = IFM_AVALID;
396 mii->mii_media_active = IFM_ETHER;
397
398 PHY_READ(sc, MII_BMCR, &bmcr);
399 /* XXX FIXME: Use different page for Fiber on newer chips */
400 PHY_READ(sc, MAKPHY_PSSR, &pssr);
401
402 if (pssr & PSSR_LINK)
403 mii->mii_media_status |= IFM_ACTIVE;
404
405 if (bmcr & BMCR_LOOP)
406 mii->mii_media_active |= IFM_LOOP;
407
408 if (bmcr & BMCR_ISO) {
409 mii->mii_media_active |= IFM_NONE;
410 mii->mii_media_status = 0;
411 return;
412 }
413
414 if ((bmcr & BMCR_AUTOEN) != 0) {
415 /*
416 * Check Speed and Duplex Resolved bit.
417 * Note that this bit is always 1 when autonego is not enabled.
418 */
419 if (!(pssr & PSSR_RESOLVED)) {
420 /* Erg, still trying, I guess... */
421 mii->mii_media_active |= IFM_NONE;
422 return;
423 }
424 } else {
425 if ((pssr & PSSR_LINK) == 0) {
426 mii->mii_media_active |= IFM_NONE;
427 return;
428 }
429 }
430
431 /* XXX FIXME: Use different page for Fiber on newer chips */
432 if (sc->mii_flags & MIIF_IS_1000X) {
433 mii->mii_media_active |= IFM_1000_SX;
434 } else {
435 switch (PSSR_SPEED_get(pssr)) {
436 case SPEED_1000:
437 mii->mii_media_active |= IFM_1000_T;
438 break;
439 case SPEED_100:
440 mii->mii_media_active |= IFM_100_TX;
441 break;
442 case SPEED_10:
443 mii->mii_media_active |= IFM_10_T;
444 break;
445 default: /* Undefined (reserved) value */
446 mii->mii_media_active |= IFM_NONE;
447 mii->mii_media_status = 0;
448 return;
449 }
450 }
451
452 if (pssr & PSSR_DUPLEX)
453 mii->mii_media_active |= mii_phy_flowstatus(sc) | IFM_FDX;
454 else
455 mii->mii_media_active |= IFM_HDX;
456
457 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
458 PHY_READ(sc, MII_100T2SR, &gsr);
459 if (gsr & GTSR_MS_RES)
460 mii->mii_media_active |= IFM_ETH_MASTER;
461 }
462 }
463