brgphy.c revision 1.14 1 /* $NetBSD: brgphy.c,v 1.14 2002/12/27 03:15:52 matt 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.14 2002/12/27 03:15:52 matt 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 int brgphymatch(struct device *, struct cfdata *, void *);
96 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 int brgphy_service(struct mii_softc *, struct mii_data *, int);
102 void brgphy_status(struct mii_softc *);
103
104 void brgphy_5401_reset(struct mii_softc *);
105 void brgphy_5411_reset(struct mii_softc *);
106
107 const struct mii_phy_funcs brgphy_funcs = {
108 brgphy_service, brgphy_status, mii_phy_reset,
109 };
110
111 const struct mii_phy_funcs brgphy_5401_funcs = {
112 brgphy_service, brgphy_status, brgphy_5401_reset,
113 };
114
115 const struct mii_phy_funcs brgphy_5411_funcs = {
116 brgphy_service, brgphy_status, brgphy_5411_reset,
117 };
118
119 const struct mii_phydesc brgphys[] = {
120 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5400,
121 MII_STR_BROADCOM_BCM5400 },
122
123 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5401,
124 MII_STR_BROADCOM_BCM5401 },
125
126 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5411,
127 MII_STR_BROADCOM_BCM5411 },
128
129 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5421,
130 MII_STR_BROADCOM_BCM5421 },
131
132 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5701,
133 MII_STR_BROADCOM_BCM5701 },
134
135 { MII_OUI_BROADCOM, MII_MODEL_BROADCOM_BCM5703,
136 MII_STR_BROADCOM_BCM5703 },
137
138 { 0, 0,
139 NULL },
140 };
141
142 static void bcm5401_load_dspcode(struct mii_softc *);
143 static void bcm5411_load_dspcode(struct mii_softc *);
144
145 int
146 brgphymatch(struct device *parent, struct cfdata *match, void *aux)
147 {
148 struct mii_attach_args *ma = aux;
149
150 if (mii_phy_match(ma, brgphys) != NULL)
151 return (10);
152
153 return (0);
154 }
155
156 void
157 brgphyattach(struct device *parent, struct device *self, void *aux)
158 {
159 struct mii_softc *sc = (struct mii_softc *)self;
160 struct mii_attach_args *ma = aux;
161 struct mii_data *mii = ma->mii_data;
162 const struct mii_phydesc *mpd;
163
164 mpd = mii_phy_match(ma, brgphys);
165 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
166
167 sc->mii_inst = mii->mii_instance;
168 sc->mii_phy = ma->mii_phyno;
169 sc->mii_pdata = mii;
170 sc->mii_flags = ma->mii_flags;
171 sc->mii_anegticks = 5;
172
173 switch (MII_MODEL(ma->mii_id2)) {
174 case MII_MODEL_BROADCOM_BCM5400:
175 sc->mii_funcs = &brgphy_5401_funcs;
176 printf("%s: using BCM5401 DSP patch\n", sc->mii_dev.dv_xname);
177 break;
178
179 case MII_MODEL_BROADCOM_BCM5401:
180 if (MII_REV(ma->mii_id2) == 1 || MII_REV(ma->mii_id2) == 3) {
181 sc->mii_funcs = &brgphy_5401_funcs;
182 printf("%s: using BCM5401 DSP patch\n",
183 sc->mii_dev.dv_xname);
184 } else
185 sc->mii_funcs = &brgphy_funcs;
186 break;
187
188 case MII_MODEL_BROADCOM_BCM5411:
189 sc->mii_funcs = &brgphy_5411_funcs;
190 printf("%s: using BCM5411 DSP patch\n", sc->mii_dev.dv_xname);
191 break;
192
193 default:
194 sc->mii_funcs = &brgphy_funcs;
195 break;
196 }
197
198 PHY_RESET(sc);
199
200 sc->mii_capabilities =
201 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
202 if (sc->mii_capabilities & BMSR_EXTSTAT)
203 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
204
205 printf("%s: ", sc->mii_dev.dv_xname);
206 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
207 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
208 printf("no media present");
209 else
210 mii_phy_add_media(sc);
211 printf("\n");
212 }
213
214 int
215 brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
216 {
217 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
218 int reg;
219
220 switch (cmd) {
221 case MII_POLLSTAT:
222 /*
223 * If we're not polling our PHY instance, just return.
224 */
225 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
226 return (0);
227 break;
228
229 case MII_MEDIACHG:
230 /*
231 * If the media indicates a different PHY instance,
232 * isolate ourselves.
233 */
234 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
235 reg = PHY_READ(sc, MII_BMCR);
236 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
237 return (0);
238 }
239
240 /*
241 * If the interface is not up, don't do anything.
242 */
243 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
244 break;
245
246 mii_phy_reset(sc); /* XXX hardware bug work-around */
247 mii_phy_setmedia(sc);
248 break;
249
250 case MII_TICK:
251 /*
252 * If we're not currently selected, just return.
253 */
254 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
255 return (0);
256
257 if (mii_phy_tick(sc) == EJUSTRETURN)
258 return (0);
259 break;
260
261 case MII_DOWN:
262 mii_phy_down(sc);
263 return (0);
264 }
265
266 /* Update the media status. */
267 mii_phy_status(sc);
268
269 /*
270 * Callback if something changed. Note that we need to poke
271 * the DSP on the Broadcom PHYs if the media changes.
272 */
273 if (sc->mii_media_active != mii->mii_media_active ||
274 sc->mii_media_status != mii->mii_media_status ||
275 cmd == MII_MEDIACHG) {
276 mii_phy_update(sc, cmd);
277 if (sc->mii_funcs == &brgphy_5401_funcs)
278 bcm5401_load_dspcode(sc);
279 else if (sc->mii_funcs == &brgphy_5411_funcs)
280 bcm5411_load_dspcode(sc);
281 }
282 return (0);
283 }
284
285 void
286 brgphy_status(struct mii_softc *sc)
287 {
288 struct mii_data *mii = sc->mii_pdata;
289 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
290 int bmcr, auxsts, gtsr;
291
292 mii->mii_media_status = IFM_AVALID;
293 mii->mii_media_active = IFM_ETHER;
294
295 auxsts = PHY_READ(sc, BRGPHY_MII_AUXSTS);
296
297 if (auxsts & BRGPHY_AUXSTS_LINK)
298 mii->mii_media_status |= IFM_ACTIVE;
299
300 bmcr = PHY_READ(sc, MII_BMCR);
301 if (bmcr & BMCR_ISO) {
302 mii->mii_media_active |= IFM_NONE;
303 mii->mii_media_status = 0;
304 return;
305 }
306
307 if (bmcr & BMCR_LOOP)
308 mii->mii_media_active |= IFM_LOOP;
309
310 if (bmcr & BMCR_AUTOEN) {
311 /*
312 * The media status bits are only valid of autonegotiation
313 * has completed (or it's disabled).
314 */
315 if ((auxsts & BRGPHY_AUXSTS_ACOMP) == 0) {
316 /* Erg, still trying, I guess... */
317 mii->mii_media_active |= IFM_NONE;
318 return;
319 }
320
321 switch (auxsts & BRGPHY_AUXSTS_AN_RES) {
322 case BRGPHY_RES_1000FD:
323 mii->mii_media_active |= IFM_1000_T|IFM_FDX;
324 gtsr = PHY_READ(sc, MII_100T2SR);
325 if (gtsr & GTSR_MS_RES)
326 mii->mii_media_active |= IFM_ETH_MASTER;
327 break;
328
329 case BRGPHY_RES_1000HD:
330 mii->mii_media_active |= IFM_1000_T;
331 gtsr = PHY_READ(sc, MII_100T2SR);
332 if (gtsr & GTSR_MS_RES)
333 mii->mii_media_active |= IFM_ETH_MASTER;
334 break;
335
336 case BRGPHY_RES_100FD:
337 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
338 break;
339
340 case BRGPHY_RES_100T4:
341 mii->mii_media_active |= IFM_100_T4;
342 break;
343
344 case BRGPHY_RES_100HD:
345 mii->mii_media_active |= IFM_100_TX;
346 break;
347
348 case BRGPHY_RES_10FD:
349 mii->mii_media_active |= IFM_10_T|IFM_FDX;
350 break;
351
352 case BRGPHY_RES_10HD:
353 mii->mii_media_active |= IFM_10_T;
354 break;
355
356 default:
357 mii->mii_media_active |= IFM_NONE;
358 mii->mii_media_status = 0;
359 }
360 } else
361 mii->mii_media_active = ife->ifm_media;
362 }
363
364 void
365 brgphy_5401_reset(struct mii_softc *sc)
366 {
367
368 mii_phy_reset(sc);
369 bcm5401_load_dspcode(sc);
370 }
371
372 void
373 brgphy_5411_reset(struct mii_softc *sc)
374 {
375
376 mii_phy_reset(sc);
377 bcm5411_load_dspcode(sc);
378 }
379
380 static void
381 bcm5401_load_dspcode(struct mii_softc *sc)
382 {
383 static const struct {
384 int reg;
385 uint16_t val;
386 } dspcode[] = {
387 { BRGPHY_MII_AUXCTL, 0x4c20 },
388 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 },
389 { BRGPHY_MII_DSP_RW_PORT, 0x1804 },
390 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 },
391 { BRGPHY_MII_DSP_RW_PORT, 0x1204 },
392 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
393 { BRGPHY_MII_DSP_RW_PORT, 0x0132 },
394 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
395 { BRGPHY_MII_DSP_RW_PORT, 0x0232 },
396 { BRGPHY_MII_DSP_ADDR_REG, 0x201f },
397 { BRGPHY_MII_DSP_RW_PORT, 0x0a20 },
398 { 0, 0 },
399 };
400 int i;
401
402 for (i = 0; dspcode[i].reg != 0; i++)
403 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
404 }
405
406 static void
407 bcm5411_load_dspcode(struct mii_softc *sc)
408 {
409 static const struct {
410 int reg;
411 uint16_t val;
412 } dspcode[] = {
413 { 0x1c, 0x8c23 },
414 { 0x1c, 0x8ca3 },
415 { 0x1c, 0x8c23 },
416 { 0, 0 },
417 };
418 int i;
419
420 for (i = 0; dspcode[i].reg != 0; i++)
421 PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
422 }
423