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