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