brgphy.c revision 1.24 1 /* $NetBSD: brgphy.c,v 1.24 2005/12/08 03:16:43 jonathan 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Manuel Bouyer.
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * driver for the Broadcom BCM5400 Gig-E PHY.
71 *
72 * Programming information for this PHY was gleaned from FreeBSD
73 * (they were apparently able to get a datasheet from Broadcom).
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: brgphy.c,v 1.24 2005/12/08 03:16:43 jonathan Exp $");
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/device.h>
83 #include <sys/socket.h>
84 #include <sys/errno.h>
85
86 #include <net/if.h>
87 #include <net/if_media.h>
88
89 #include <dev/mii/mii.h>
90 #include <dev/mii/miivar.h>
91 #include <dev/mii/miidevs.h>
92
93 #include <dev/mii/brgphyreg.h>
94
95 static int brgphymatch(struct device *, struct cfdata *, void *);
96 static void brgphyattach(struct device *, struct device *, void *);
97
98 CFATTACH_DECL(brgphy, sizeof(struct mii_softc),
99 brgphymatch, brgphyattach, mii_phy_detach, mii_phy_activate);
100
101 static int brgphy_service(struct mii_softc *, struct mii_data *, int);
102 static void brgphy_status(struct mii_softc *);
103
104 static void brgphy_5401_reset(struct mii_softc *);
105 static void brgphy_5411_reset(struct mii_softc *);
106 static void brgphy_5703_reset(struct mii_softc *);
107 static void brgphy_5704_reset(struct mii_softc *);
108 static void brgphy_5705_reset(struct mii_softc *);
109 static void brgphy_5750_reset(struct mii_softc *);
110
111 static const struct mii_phy_funcs brgphy_funcs = {
112 brgphy_service, brgphy_status, mii_phy_reset,
113 };
114
115 static const struct mii_phy_funcs brgphy_5401_funcs = {
116 brgphy_service, brgphy_status, brgphy_5401_reset,
117 };
118
119 static const struct mii_phy_funcs brgphy_5411_funcs = {
120 brgphy_service, brgphy_status, brgphy_5411_reset,
121 };
122
123 static const struct mii_phy_funcs brgphy_5703_funcs = {
124 brgphy_service, brgphy_status, brgphy_5703_reset,
125 };
126
127 static const struct mii_phy_funcs brgphy_5704_funcs = {
128 brgphy_service, brgphy_status, brgphy_5704_reset,
129 };
130
131 static const struct mii_phy_funcs brgphy_5705_funcs = {
132 brgphy_service, brgphy_status, brgphy_5705_reset,
133 };
134
135 const struct mii_phy_funcs brgphy_5750_funcs = {
136 brgphy_service, brgphy_status, brgphy_5750_reset,
137 };
138
139
140 static const struct mii_phydesc brgphys[] = {
141 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5400,
142 MII_STR_BROADCOM_BCM5400 },
143
144 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5401,
145 MII_STR_BROADCOM_BCM5401 },
146
147 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5411,
148 MII_STR_BROADCOM_BCM5411 },
149
150 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5421,
151 MII_STR_BROADCOM_BCM5421 },
152
153 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5701,
154 MII_STR_BROADCOM_BCM5701 },
155
156 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5703,
157 MII_STR_BROADCOM_BCM5703 },
158
159 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5704,
160 MII_STR_BROADCOM_BCM5704 },
161
162 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5714,
163 MII_STR_BROADCOM_BCM5714 },
164
165 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5750,
166 MII_STR_BROADCOM_BCM5750 },
167
168 { 0, 0,
169 NULL },
170 };
171
172 static void bcm5401_load_dspcode(struct mii_softc *);
173 static void bcm5411_load_dspcode(struct mii_softc *);
174 static void bcm5703_load_dspcode(struct mii_softc *);
175 static void bcm5704_load_dspcode(struct mii_softc *);
176 static void bcm5750_load_dspcode(struct mii_softc *);
177
178 static int
179 brgphymatch(struct device *parent, struct cfdata *match, void *aux)
180 {
181 struct mii_attach_args *ma = aux;
182
183 if (mii_phy_match(ma, brgphys) != NULL)
184 return (10);
185
186 return (0);
187 }
188
189 static void
190 brgphyattach(struct device *parent, struct device *self, void *aux)
191 {
192 struct mii_softc *sc = (struct mii_softc *)self;
193 struct mii_attach_args *ma = aux;
194 struct mii_data *mii = ma->mii_data;
195 const struct mii_phydesc *mpd;
196
197 mpd = mii_phy_match(ma, brgphys);
198 aprint_naive(": Media interface\n");
199 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
200
201 sc->mii_inst = mii->mii_instance;
202 sc->mii_phy = ma->mii_phyno;
203 sc->mii_pdata = mii;
204 sc->mii_flags = ma->mii_flags;
205 sc->mii_anegticks = 5;
206
207 switch (MII_MODEL(ma->mii_id2)) {
208 case MII_MODEL_BROADCOM_BCM5400:
209 sc->mii_funcs = &brgphy_5401_funcs;
210 aprint_normal("%s: using BCM5401 DSP patch\n",
211 sc->mii_dev.dv_xname);
212 break;
213
214 case MII_MODEL_BROADCOM_BCM5401:
215 if (MII_REV(ma->mii_id2) == 1 || MII_REV(ma->mii_id2) == 3) {
216 sc->mii_funcs = &brgphy_5401_funcs;
217 aprint_normal("%s: using BCM5401 DSP patch\n",
218 sc->mii_dev.dv_xname);
219 } else
220 sc->mii_funcs = &brgphy_funcs;
221 break;
222
223 case MII_MODEL_BROADCOM_BCM5411:
224 sc->mii_funcs = &brgphy_5411_funcs;
225 aprint_normal("%s: using BCM5411 DSP patch\n",
226 sc->mii_dev.dv_xname);
227 break;
228
229 #ifdef notyet /* unverified, untested */
230 case MII_MODEL_BROADCOM_BCM5703:
231 sc->mii_funcs = &brgphy_5703_funcs;
232 aprint_normal("%s: using BCM5703 DSP patch\n",
233 sc->mii_dev.dv_xname);
234 break;
235 #endif
236
237 case MII_MODEL_BROADCOM_BCM5704:
238 sc->mii_funcs = &brgphy_5704_funcs;
239 aprint_normal("%s: using BCM5704 DSP patch\n",
240 sc->mii_dev.dv_xname);
241 break;
242
243 case MII_MODEL_BROADCOM_BCM5705:
244 sc->mii_funcs = &brgphy_5705_funcs;
245 break;
246
247 case MII_MODEL_BROADCOM_BCM5714:
248 case MII_MODEL_BROADCOM_BCM5750:
249 sc->mii_funcs = &brgphy_5750_funcs;
250 break;
251
252 default:
253 sc->mii_funcs = &brgphy_funcs;
254 break;
255 }
256
257 PHY_RESET(sc);
258
259 sc->mii_capabilities =
260 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
261 if (sc->mii_capabilities & BMSR_EXTSTAT)
262 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
263
264 aprint_normal("%s: ", sc->mii_dev.dv_xname);
265 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
266 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
267 aprint_error("no media present");
268 else
269 mii_phy_add_media(sc);
270 aprint_normal("\n");
271 }
272
273 static int
274 brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
275 {
276 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
277 int reg;
278
279 switch (cmd) {
280 case MII_POLLSTAT:
281 /*
282 * If we're not polling our PHY instance, just return.
283 */
284 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
285 return (0);
286 break;
287
288 case MII_MEDIACHG:
289 /*
290 * If the media indicates a different PHY instance,
291 * isolate ourselves.
292 */
293 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
294 reg = PHY_READ(sc, MII_BMCR);
295 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
296 return (0);
297 }
298
299 /*
300 * If the interface is not up, don't do anything.
301 */
302 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
303 break;
304
305 if (sc->mii_funcs != &brgphy_5705_funcs)
306 mii_phy_reset(sc); /* XXX hardware bug work-around */
307 mii_phy_setmedia(sc);
308 break;
309
310 case MII_TICK:
311 /*
312 * If we're not currently selected, just return.
313 */
314 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
315 return (0);
316
317 if (mii_phy_tick(sc) == EJUSTRETURN)
318 return (0);
319 break;
320
321 case MII_DOWN:
322 mii_phy_down(sc);
323 return (0);
324 }
325
326 /* Update the media status. */
327 mii_phy_status(sc);
328
329 /*
330 * Callback if something changed. Note that we need to poke
331 * the DSP on the Broadcom PHYs if the media changes.
332 */
333 if (sc->mii_media_active != mii->mii_media_active ||
334 sc->mii_media_status != mii->mii_media_status ||
335 cmd == MII_MEDIACHG) {
336 mii_phy_update(sc, cmd);
337 if (sc->mii_funcs == &brgphy_5401_funcs)
338 bcm5401_load_dspcode(sc);
339 else if (sc->mii_funcs == &brgphy_5411_funcs)
340 bcm5411_load_dspcode(sc);
341 }
342 return (0);
343 }
344
345 static void
346 brgphy_status(struct mii_softc *sc)
347 {
348 struct mii_data *mii = sc->mii_pdata;
349 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
350 int bmcr, auxsts, gtsr;
351
352 mii->mii_media_status = IFM_AVALID;
353 mii->mii_media_active = IFM_ETHER;
354
355 auxsts = PHY_READ(sc, BRGPHY_MII_AUXSTS);
356
357 if (auxsts & BRGPHY_AUXSTS_LINK)
358 mii->mii_media_status |= IFM_ACTIVE;
359
360 bmcr = PHY_READ(sc, MII_BMCR);
361 if (bmcr & BMCR_ISO) {
362 mii->mii_media_active |= IFM_NONE;
363 mii->mii_media_status = 0;
364 return;
365 }
366
367 if (bmcr & BMCR_LOOP)
368 mii->mii_media_active |= IFM_LOOP;
369
370 if (bmcr & BMCR_AUTOEN) {
371 /*
372 * The media status bits are only valid of autonegotiation
373 * has completed (or it's disabled).
374 */
375 if ((auxsts & BRGPHY_AUXSTS_ACOMP) == 0) {
376 /* Erg, still trying, I guess... */
377 mii->mii_media_active |= IFM_NONE;
378 return;
379 }
380
381 switch (auxsts & BRGPHY_AUXSTS_AN_RES) {
382 case BRGPHY_RES_1000FD:
383 mii->mii_media_active |= IFM_1000_T|IFM_FDX;
384 gtsr = PHY_READ(sc, MII_100T2SR);
385 if (gtsr & GTSR_MS_RES)
386 mii->mii_media_active |= IFM_ETH_MASTER;
387 break;
388
389 case BRGPHY_RES_1000HD:
390 mii->mii_media_active |= IFM_1000_T;
391 gtsr = PHY_READ(sc, MII_100T2SR);
392 if (gtsr & GTSR_MS_RES)
393 mii->mii_media_active |= IFM_ETH_MASTER;
394 break;
395
396 case BRGPHY_RES_100FD:
397 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
398 break;
399
400 case BRGPHY_RES_100T4:
401 mii->mii_media_active |= IFM_100_T4;
402 break;
403
404 case BRGPHY_RES_100HD:
405 mii->mii_media_active |= IFM_100_TX;
406 break;
407
408 case BRGPHY_RES_10FD:
409 mii->mii_media_active |= IFM_10_T|IFM_FDX;
410 break;
411
412 case BRGPHY_RES_10HD:
413 mii->mii_media_active |= IFM_10_T;
414 break;
415
416 default:
417 mii->mii_media_active |= IFM_NONE;
418 mii->mii_media_status = 0;
419 }
420 if (mii->mii_media_active & IFM_FDX)
421 mii->mii_media_active |= mii_phy_flowstatus(sc);
422 } else
423 mii->mii_media_active = ife->ifm_media;
424 }
425
426 static void
427 brgphy_5401_reset(struct mii_softc *sc)
428 {
429
430 mii_phy_reset(sc);
431 bcm5401_load_dspcode(sc);
432 }
433
434 static void
435 brgphy_5411_reset(struct mii_softc *sc)
436 {
437
438 mii_phy_reset(sc);
439 bcm5411_load_dspcode(sc);
440 }
441
442
443 static void
444 brgphy_5703_reset(struct mii_softc *sc)
445 {
446
447 mii_phy_reset(sc);
448 bcm5703_load_dspcode(sc);
449 }
450
451 static void
452 brgphy_5704_reset(struct mii_softc *sc)
453 {
454
455 mii_phy_reset(sc);
456 bcm5704_load_dspcode(sc);
457 }
458
459 /*
460 * Hardware bug workaround. Do nothing since after
461 * reset the 5705 PHY would get stuck in 10/100 MII mode.
462 */
463
464 static void
465 brgphy_5705_reset(struct mii_softc *sc)
466 {
467 }
468
469 static void
470 brgphy_5750_reset(struct mii_softc *sc)
471 {
472 mii_phy_reset(sc);
473 bcm5750_load_dspcode(sc);
474 }
475
476 /* Turn off tap power management on 5401. */
477 static void
478 bcm5401_load_dspcode(struct mii_softc *sc)
479 {
480 static const struct {
481 int reg;
482 uint16_t val;
483 } dspcode[] = {
484 { BRGPHY_MII_AUXCTL, 0x0c20 },
485 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 },
486 { BRGPHY_MII_DSP_RW_PORT, 0x1804 },
487 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 },
488 { BRGPHY_MII_DSP_RW_PORT, 0x1204 },
489 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
490 { BRGPHY_MII_DSP_RW_PORT, 0x0132 },
491 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
492 { BRGPHY_MII_DSP_RW_PORT, 0x0232 },
493 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
494 { BRGPHY_MII_DSP_RW_PORT, 0x0a20 },
495 { 0, 0 },
496 };
497 int i;
498
499 for (i = 0; dspcode[i].reg != 0; i++)
500 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
501 delay(40);
502 }
503
504 static void
505 bcm5411_load_dspcode(struct mii_softc *sc)
506 {
507 static const struct {
508 int reg;
509 uint16_t val;
510 } dspcode[] = {
511 { 0x1c, 0x8c23 },
512 { 0x1c, 0x8ca3 },
513 { 0x1c, 0x8c23 },
514 { 0, 0 },
515 };
516 int i;
517
518 for (i = 0; dspcode[i].reg != 0; i++)
519 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
520 }
521
522 static void
523 bcm5703_load_dspcode(struct mii_softc *sc)
524 {
525 static const struct {
526 int reg;
527 uint16_t val;
528 } dspcode[] = {
529 { BRGPHY_MII_AUXCTL, 0x0c00 },
530 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
531 { BRGPHY_MII_DSP_RW_PORT, 0x2aaa },
532 { 0, 0 },
533 };
534 int i;
535
536 for (i = 0; dspcode[i].reg != 0; i++)
537 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
538 }
539
540 static void
541 bcm5704_load_dspcode(struct mii_softc *sc)
542 {
543 static const struct {
544 int reg;
545 uint16_t val;
546 } dspcode[] = {
547 { 0x1c, 0x8d68 },
548 { 0x1c, 0x8d68 },
549 { 0, 0 },
550 };
551 int i;
552
553 for (i = 0; dspcode[i].reg != 0; i++)
554 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
555 }
556
557 static void
558 bcm5750_load_dspcode(struct mii_softc *sc)
559 {
560 static const struct {
561 int reg;
562 uint16_t val;
563 } dspcode[] = {
564 { BRGPHY_MII_AUXCTL, 0x0c00 },
565 { BRGPHY_MII_DSP_ADDR_REG, 0x000a },
566 { BRGPHY_MII_DSP_RW_PORT, 0x310b },
567 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
568 { BRGPHY_MII_DSP_RW_PORT, 0x9506 },
569 { BRGPHY_MII_DSP_ADDR_REG, 0x401f },
570 { BRGPHY_MII_DSP_RW_PORT, 0x14e2 },
571 { BRGPHY_MII_AUXCTL, 0x0400 },
572 { 0, 0 },
573 };
574 int i;
575
576 for (i = 0; dspcode[i].reg != 0; i++)
577 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
578 }
579