brgphy.c revision 1.23 1 /* $NetBSD: brgphy.c,v 1.23 2005/02/27 00:27:31 perry 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.23 2005/02/27 00:27:31 perry 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_BCM5705,
163 MII_STR_BROADCOM_BCM5705 },
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_BCM5750:
248 sc->mii_funcs = &brgphy_5750_funcs;
249 break;
250
251 default:
252 sc->mii_funcs = &brgphy_funcs;
253 break;
254 }
255
256 PHY_RESET(sc);
257
258 sc->mii_capabilities =
259 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
260 if (sc->mii_capabilities & BMSR_EXTSTAT)
261 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
262
263 aprint_normal("%s: ", sc->mii_dev.dv_xname);
264 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
265 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
266 aprint_error("no media present");
267 else
268 mii_phy_add_media(sc);
269 aprint_normal("\n");
270 }
271
272 static int
273 brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
274 {
275 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
276 int reg;
277
278 switch (cmd) {
279 case MII_POLLSTAT:
280 /*
281 * If we're not polling our PHY instance, just return.
282 */
283 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
284 return (0);
285 break;
286
287 case MII_MEDIACHG:
288 /*
289 * If the media indicates a different PHY instance,
290 * isolate ourselves.
291 */
292 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
293 reg = PHY_READ(sc, MII_BMCR);
294 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
295 return (0);
296 }
297
298 /*
299 * If the interface is not up, don't do anything.
300 */
301 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
302 break;
303
304 if (sc->mii_funcs != &brgphy_5705_funcs)
305 mii_phy_reset(sc); /* XXX hardware bug work-around */
306 mii_phy_setmedia(sc);
307 break;
308
309 case MII_TICK:
310 /*
311 * If we're not currently selected, just return.
312 */
313 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
314 return (0);
315
316 if (mii_phy_tick(sc) == EJUSTRETURN)
317 return (0);
318 break;
319
320 case MII_DOWN:
321 mii_phy_down(sc);
322 return (0);
323 }
324
325 /* Update the media status. */
326 mii_phy_status(sc);
327
328 /*
329 * Callback if something changed. Note that we need to poke
330 * the DSP on the Broadcom PHYs if the media changes.
331 */
332 if (sc->mii_media_active != mii->mii_media_active ||
333 sc->mii_media_status != mii->mii_media_status ||
334 cmd == MII_MEDIACHG) {
335 mii_phy_update(sc, cmd);
336 if (sc->mii_funcs == &brgphy_5401_funcs)
337 bcm5401_load_dspcode(sc);
338 else if (sc->mii_funcs == &brgphy_5411_funcs)
339 bcm5411_load_dspcode(sc);
340 }
341 return (0);
342 }
343
344 static void
345 brgphy_status(struct mii_softc *sc)
346 {
347 struct mii_data *mii = sc->mii_pdata;
348 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
349 int bmcr, auxsts, gtsr;
350
351 mii->mii_media_status = IFM_AVALID;
352 mii->mii_media_active = IFM_ETHER;
353
354 auxsts = PHY_READ(sc, BRGPHY_MII_AUXSTS);
355
356 if (auxsts & BRGPHY_AUXSTS_LINK)
357 mii->mii_media_status |= IFM_ACTIVE;
358
359 bmcr = PHY_READ(sc, MII_BMCR);
360 if (bmcr & BMCR_ISO) {
361 mii->mii_media_active |= IFM_NONE;
362 mii->mii_media_status = 0;
363 return;
364 }
365
366 if (bmcr & BMCR_LOOP)
367 mii->mii_media_active |= IFM_LOOP;
368
369 if (bmcr & BMCR_AUTOEN) {
370 /*
371 * The media status bits are only valid of autonegotiation
372 * has completed (or it's disabled).
373 */
374 if ((auxsts & BRGPHY_AUXSTS_ACOMP) == 0) {
375 /* Erg, still trying, I guess... */
376 mii->mii_media_active |= IFM_NONE;
377 return;
378 }
379
380 switch (auxsts & BRGPHY_AUXSTS_AN_RES) {
381 case BRGPHY_RES_1000FD:
382 mii->mii_media_active |= IFM_1000_T|IFM_FDX;
383 gtsr = PHY_READ(sc, MII_100T2SR);
384 if (gtsr & GTSR_MS_RES)
385 mii->mii_media_active |= IFM_ETH_MASTER;
386 break;
387
388 case BRGPHY_RES_1000HD:
389 mii->mii_media_active |= IFM_1000_T;
390 gtsr = PHY_READ(sc, MII_100T2SR);
391 if (gtsr & GTSR_MS_RES)
392 mii->mii_media_active |= IFM_ETH_MASTER;
393 break;
394
395 case BRGPHY_RES_100FD:
396 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
397 break;
398
399 case BRGPHY_RES_100T4:
400 mii->mii_media_active |= IFM_100_T4;
401 break;
402
403 case BRGPHY_RES_100HD:
404 mii->mii_media_active |= IFM_100_TX;
405 break;
406
407 case BRGPHY_RES_10FD:
408 mii->mii_media_active |= IFM_10_T|IFM_FDX;
409 break;
410
411 case BRGPHY_RES_10HD:
412 mii->mii_media_active |= IFM_10_T;
413 break;
414
415 default:
416 mii->mii_media_active |= IFM_NONE;
417 mii->mii_media_status = 0;
418 }
419 if (mii->mii_media_active & IFM_FDX)
420 mii->mii_media_active |= mii_phy_flowstatus(sc);
421 } else
422 mii->mii_media_active = ife->ifm_media;
423 }
424
425 static void
426 brgphy_5401_reset(struct mii_softc *sc)
427 {
428
429 mii_phy_reset(sc);
430 bcm5401_load_dspcode(sc);
431 }
432
433 static void
434 brgphy_5411_reset(struct mii_softc *sc)
435 {
436
437 mii_phy_reset(sc);
438 bcm5411_load_dspcode(sc);
439 }
440
441
442 static void
443 brgphy_5703_reset(struct mii_softc *sc)
444 {
445
446 mii_phy_reset(sc);
447 bcm5703_load_dspcode(sc);
448 }
449
450 static void
451 brgphy_5704_reset(struct mii_softc *sc)
452 {
453
454 mii_phy_reset(sc);
455 bcm5704_load_dspcode(sc);
456 }
457
458 /*
459 * Hardware bug workaround. Do nothing since after
460 * reset the 5705 PHY would get stuck in 10/100 MII mode.
461 */
462
463 static void
464 brgphy_5705_reset(struct mii_softc *sc)
465 {
466 }
467
468 static void
469 brgphy_5750_reset(struct mii_softc *sc)
470 {
471 mii_phy_reset(sc);
472 bcm5750_load_dspcode(sc);
473 }
474
475 /* Turn off tap power management on 5401. */
476 static void
477 bcm5401_load_dspcode(struct mii_softc *sc)
478 {
479 static const struct {
480 int reg;
481 uint16_t val;
482 } dspcode[] = {
483 { BRGPHY_MII_AUXCTL, 0x0c20 },
484 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 },
485 { BRGPHY_MII_DSP_RW_PORT, 0x1804 },
486 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 },
487 { BRGPHY_MII_DSP_RW_PORT, 0x1204 },
488 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
489 { BRGPHY_MII_DSP_RW_PORT, 0x0132 },
490 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
491 { BRGPHY_MII_DSP_RW_PORT, 0x0232 },
492 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
493 { BRGPHY_MII_DSP_RW_PORT, 0x0a20 },
494 { 0, 0 },
495 };
496 int i;
497
498 for (i = 0; dspcode[i].reg != 0; i++)
499 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
500 delay(40);
501 }
502
503 static void
504 bcm5411_load_dspcode(struct mii_softc *sc)
505 {
506 static const struct {
507 int reg;
508 uint16_t val;
509 } dspcode[] = {
510 { 0x1c, 0x8c23 },
511 { 0x1c, 0x8ca3 },
512 { 0x1c, 0x8c23 },
513 { 0, 0 },
514 };
515 int i;
516
517 for (i = 0; dspcode[i].reg != 0; i++)
518 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
519 }
520
521 static void
522 bcm5703_load_dspcode(struct mii_softc *sc)
523 {
524 static const struct {
525 int reg;
526 uint16_t val;
527 } dspcode[] = {
528 { BRGPHY_MII_AUXCTL, 0x0c00 },
529 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
530 { BRGPHY_MII_DSP_RW_PORT, 0x2aaa },
531 { 0, 0 },
532 };
533 int i;
534
535 for (i = 0; dspcode[i].reg != 0; i++)
536 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
537 }
538
539 static void
540 bcm5704_load_dspcode(struct mii_softc *sc)
541 {
542 static const struct {
543 int reg;
544 uint16_t val;
545 } dspcode[] = {
546 { 0x1c, 0x8d68 },
547 { 0x1c, 0x8d68 },
548 { 0, 0 },
549 };
550 int i;
551
552 for (i = 0; dspcode[i].reg != 0; i++)
553 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
554 }
555
556 static void
557 bcm5750_load_dspcode(struct mii_softc *sc)
558 {
559 static const struct {
560 int reg;
561 uint16_t val;
562 } dspcode[] = {
563 { BRGPHY_MII_AUXCTL, 0x0c00 },
564 { BRGPHY_MII_DSP_ADDR_REG, 0x000a },
565 { BRGPHY_MII_DSP_RW_PORT, 0x310b },
566 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
567 { BRGPHY_MII_DSP_RW_PORT, 0x9506 },
568 { BRGPHY_MII_DSP_ADDR_REG, 0x401f },
569 { BRGPHY_MII_DSP_RW_PORT, 0x14e2 },
570 { BRGPHY_MII_AUXCTL, 0x0400 },
571 { 0, 0 },
572 };
573 int i;
574
575 for (i = 0; dspcode[i].reg != 0; i++)
576 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
577 }
578