Merge from vendor branch GDB:
[dragonfly.git] / sys / dev / netif / mii_layer / rgephy.c
1 /*-
2  * Copyright (c) 2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
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
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/mii/rgephy.c,v 1.7 2005/09/30 19:39:27 imp Exp $
33  * $DragonFly: src/sys/dev/netif/mii_layer/rgephy.c,v 1.1 2005/12/26 13:36:18 sephe Exp $
34  */
35
36 /*
37  * Driver for the RealTek 8169S/8110S internal 10/100/1000 PHY.
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/bus.h>
46
47 #include <machine/bus.h>
48 #include <machine/clock.h>
49
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_media.h>
53
54 #include <dev/netif/mii_layer/mii.h>
55 #include <dev/netif/mii_layer/miivar.h>
56 #include <dev/netif/mii_layer/miidevs.h>
57
58 #include <dev/netif/re/if_rereg.h>
59 #include <dev/netif/mii_layer/rgephyreg.h>
60
61 #include "miibus_if.h"
62
63 #include <machine/bus.h>
64
65 static int rgephy_probe(device_t);
66 static int rgephy_attach(device_t);
67
68 static device_method_t rgephy_methods[] = {
69         /* device interface */
70         DEVMETHOD(device_probe,         rgephy_probe),
71         DEVMETHOD(device_attach,        rgephy_attach),
72         DEVMETHOD(device_detach,        ukphy_detach),
73         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
74         { 0, 0 }
75 };
76
77 static devclass_t rgephy_devclass;
78
79 static driver_t rgephy_driver = {
80         "rgephy",
81         rgephy_methods,
82         sizeof(struct mii_softc)
83 };
84
85 DRIVER_MODULE(rgephy, miibus, rgephy_driver, rgephy_devclass, 0, 0);
86
87 static int      rgephy_service(struct mii_softc *, struct mii_data *, int);
88 static void     rgephy_status(struct mii_softc *);
89 static int      rgephy_mii_phy_auto(struct mii_softc *);
90 static void     rgephy_reset(struct mii_softc *);
91 static void     rgephy_loop(struct mii_softc *);
92 static void     rgephy_load_dspcode(struct mii_softc *);
93 static int      rgephy_mii_model;
94
95 static int
96 rgephy_probe(device_t dev)
97 {
98         struct mii_attach_args *ma;
99
100         ma = device_get_ivars(dev);
101
102         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxREALTEK &&
103             MII_MODEL(ma->mii_id2) == MII_MODEL_xxREALTEK_RTL8169S) {
104                 device_set_desc(dev, MII_STR_xxREALTEK_RTL8169S);
105                 return(0);
106         }
107
108         return(ENXIO);
109 }
110
111 static int
112 rgephy_attach(device_t dev)
113 {
114         struct mii_softc *sc;
115         struct mii_attach_args *ma;
116         struct mii_data *mii;
117         const char *sep = "";
118
119         sc = device_get_softc(dev);
120         ma = device_get_ivars(dev);
121         mii_softc_init(sc, ma);
122         sc->mii_dev = device_get_parent(dev);
123
124         mii = device_get_softc(sc->mii_dev);
125         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
126
127         sc->mii_inst = mii->mii_instance;
128         sc->mii_service = rgephy_service;
129         sc->mii_pdata = mii;
130
131         sc->mii_flags |= MIIF_NOISOLATE;
132         mii->mii_instance++;
133
134 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
135 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
136
137         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
138             BMCR_ISO);
139 #if 0
140         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
141             BMCR_LOOP|BMCR_S100);
142 #endif
143
144         rgephy_mii_model = MII_MODEL(ma->mii_id2);
145         rgephy_reset(sc);
146
147         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
148         sc->mii_capabilities &= ~BMSR_ANEG;
149
150         device_printf(dev, " ");
151         mii_add_media(sc, sc->mii_capabilities);
152         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
153             RGEPHY_BMCR_FDX);
154         PRINT(", 1000baseTX");
155         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst), 0);
156         PRINT("1000baseTX-FDX");
157         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
158         PRINT("auto");
159
160         printf("\n");
161 #undef ADD
162 #undef PRINT
163
164         /* Make mii_bmsr_media_to_anar() work correctly */
165         sc->mii_flags |= MIIF_IS_1000X;
166
167         MIIBUS_MEDIAINIT(sc->mii_dev);
168         return(0);
169 }
170
171 static int
172 rgephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
173 {
174         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
175         int reg, speed, gig;
176
177         switch (cmd) {
178         case MII_POLLSTAT:
179                 /*
180                  * If we're not polling our PHY instance, just return.
181                  */
182                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
183                         return (0);
184                 break;
185
186         case MII_MEDIACHG:
187                 /*
188                  * If the media indicates a different PHY instance,
189                  * isolate ourselves.
190                  */
191                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
192                         reg = PHY_READ(sc, MII_BMCR);
193                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
194                         return (0);
195                 }
196
197                 /*
198                  * If the interface is not up, don't do anything.
199                  */
200                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
201                         break;
202
203                 rgephy_reset(sc);       /* XXX hardware bug work-around */
204
205                 switch (IFM_SUBTYPE(ife->ifm_media)) {
206                 case IFM_AUTO:
207 #ifdef foo
208                         /*
209                          * If we're already in auto mode, just return.
210                          */
211                         if (PHY_READ(sc, RGEPHY_MII_BMCR) & RGEPHY_BMCR_AUTOEN)
212                                 return (0);
213 #endif
214                         rgephy_mii_phy_auto(sc);
215                         break;
216                 case IFM_1000_T:
217                         speed = RGEPHY_S1000;
218                         goto setit;
219                 case IFM_100_TX:
220                         speed = RGEPHY_S100;
221                         goto setit;
222                 case IFM_10_T:
223                         speed = RGEPHY_S10;
224 setit:
225                         rgephy_loop(sc);
226                         if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
227                                 speed |= RGEPHY_BMCR_FDX;
228                                 gig = RGEPHY_1000CTL_AFD;
229                         } else {
230                                 gig = RGEPHY_1000CTL_AHD;
231                         }
232
233                         PHY_WRITE(sc, RGEPHY_MII_1000CTL, 0);
234                         PHY_WRITE(sc, RGEPHY_MII_BMCR, speed);
235                         PHY_WRITE(sc, RGEPHY_MII_ANAR, RGEPHY_SEL_TYPE);
236
237                         if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T) 
238                                 break;
239
240                         PHY_WRITE(sc, RGEPHY_MII_1000CTL, gig);
241                         PHY_WRITE(sc, RGEPHY_MII_BMCR,
242                             speed|RGEPHY_BMCR_AUTOEN|RGEPHY_BMCR_STARTNEG);
243
244                         /*
245                          * When settning the link manually, one side must
246                          * be the master and the other the slave. However
247                          * ifmedia doesn't give us a good way to specify
248                          * this, so we fake it by using one of the LINK
249                          * flags. If LINK0 is set, we program the PHY to
250                          * be a master, otherwise it's a slave.
251                          */
252                         if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
253                                 PHY_WRITE(sc, RGEPHY_MII_1000CTL,
254                                     gig|RGEPHY_1000CTL_MSE|RGEPHY_1000CTL_MSC);
255                         } else {
256                                 PHY_WRITE(sc, RGEPHY_MII_1000CTL,
257                                     gig|RGEPHY_1000CTL_MSE);
258                         }
259                         break;
260 #ifdef foo
261                 case IFM_NONE:
262                         PHY_WRITE(sc, MII_BMCR, BMCR_ISO|BMCR_PDOWN);
263                         break;
264 #endif
265                 case IFM_100_T4:
266                 default:
267                         return (EINVAL);
268                 }
269                 break;
270
271         case MII_TICK:
272                 /*
273                  * If we're not currently selected, just return.
274                  */
275                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
276                         return (0);
277
278                 /*
279                  * Is the interface even up?
280                  */
281                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
282                         return (0);
283
284                 /*
285                  * Only used for autonegotiation.
286                  */
287                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
288                         return (0);
289
290                 /*
291                  * Check to see if we have link.  If we do, we don't
292                  * need to restart the autonegotiation process.
293                  *
294                  * XXX Read the BMSR twice in case it's latched?
295                  */
296                 reg = PHY_READ(sc, RE_GMEDIASTAT);
297                 if (reg & RE_GMEDIASTAT_LINK)
298                         break;
299
300                 /*
301                  * Only retry autonegotiation every 5 seconds.
302                  */
303                 if (++sc->mii_ticks <= 5/*10*/)
304                         break;
305                 
306                 sc->mii_ticks = 0;
307                 rgephy_mii_phy_auto(sc);
308                 return (0);
309         }
310
311         /* Update the media status. */
312         rgephy_status(sc);
313
314         /*
315          * Callback if something changed. Note that we need to poke
316          * the DSP on the RealTek PHYs if the media changes.
317          */
318         if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
319                 rgephy_load_dspcode(sc);
320                 MIIBUS_STATCHG(sc->mii_dev);
321                 sc->mii_active = mii->mii_media_active;
322         }
323         return (0);
324 }
325
326 static void
327 rgephy_status(struct mii_softc *sc)
328 {
329         struct mii_data *mii = sc->mii_pdata;
330         int bmsr, bmcr;
331
332         mii->mii_media_status = IFM_AVALID;
333         mii->mii_media_active = IFM_ETHER;
334
335         bmsr = PHY_READ(sc, RE_GMEDIASTAT);
336
337         if (bmsr & RE_GMEDIASTAT_LINK)
338                 mii->mii_media_status |= IFM_ACTIVE;
339         bmsr = PHY_READ(sc, RGEPHY_MII_BMSR);
340
341         bmcr = PHY_READ(sc, RGEPHY_MII_BMCR);
342
343         if (bmcr & RGEPHY_BMCR_LOOP)
344                 mii->mii_media_active |= IFM_LOOP;
345
346         if (bmcr & RGEPHY_BMCR_AUTOEN) {
347                 if ((bmsr & RGEPHY_BMSR_ACOMP) == 0) {
348                         /* Erg, still trying, I guess... */
349                         mii->mii_media_active |= IFM_NONE;
350                         return;
351                 }
352         }
353
354         bmsr = PHY_READ(sc, RE_GMEDIASTAT);
355
356         if (bmsr & RE_GMEDIASTAT_1000MBPS) {
357                 mii->mii_media_active |= IFM_1000_T;
358         } else if (bmsr & RE_GMEDIASTAT_100MBPS) {
359                 mii->mii_media_active |= IFM_100_TX;
360         } else if (bmsr & RE_GMEDIASTAT_10MBPS) {
361                 mii->mii_media_active |= IFM_10_T;
362         } else {
363                 mii->mii_media_active |= IFM_NONE;
364                 return;
365         }
366
367         if (bmsr & RE_GMEDIASTAT_FDX)
368                 mii->mii_media_active |= IFM_FDX;
369 }
370
371 static int
372 rgephy_mii_phy_auto(struct mii_softc *mii)
373 {
374         rgephy_loop(mii);
375         rgephy_reset(mii);
376
377         PHY_WRITE(mii, RGEPHY_MII_ANAR, mii_bmsr_media_to_anar(mii));
378         DELAY(1000);
379         PHY_WRITE(mii, RGEPHY_MII_1000CTL, RGEPHY_1000CTL_AFD);
380         DELAY(1000);
381         PHY_WRITE(mii, RGEPHY_MII_BMCR,
382             RGEPHY_BMCR_AUTOEN | RGEPHY_BMCR_STARTNEG);
383         DELAY(100);
384
385         return (EJUSTRETURN);
386 }
387
388 static void
389 rgephy_loop(struct mii_softc *sc)
390 {
391         u_int32_t bmsr;
392         int i;
393
394         PHY_WRITE(sc, RGEPHY_MII_BMCR, RGEPHY_BMCR_PDOWN);
395         DELAY(1000);
396
397         for (i = 0; i < 15000; i++) {
398                 bmsr = PHY_READ(sc, RGEPHY_MII_BMSR);
399                 if (!(bmsr & RGEPHY_BMSR_LINK)) {
400 #if 0
401                         device_printf(sc->mii_dev, "looped %d\n", i);
402 #endif
403                         break;
404                 }
405                 DELAY(10);
406         }
407 }
408
409 #define PHY_SETBIT(x, y, z) \
410         PHY_WRITE(x, y, (PHY_READ(x, y) | (z)))
411 #define PHY_CLRBIT(x, y, z) \
412         PHY_WRITE(x, y, (PHY_READ(x, y) & ~(z)))
413
414 /*
415  * Initialize RealTek PHY per the datasheet. The DSP in the PHYs of
416  * existing revisions of the 8169S/8110S chips need to be tuned in
417  * order to reliably negotiate a 1000Mbps link. Later revs of the
418  * chips may not require this software tuning.
419  */
420 static void
421 rgephy_load_dspcode(struct mii_softc *sc)
422 {
423         int val;
424
425         PHY_WRITE(sc, 31, 0x0001);
426         PHY_WRITE(sc, 21, 0x1000);
427         PHY_WRITE(sc, 24, 0x65C7);
428         PHY_CLRBIT(sc, 4, 0x0800);
429         val = PHY_READ(sc, 4) & 0xFFF;
430         PHY_WRITE(sc, 4, val);
431         PHY_WRITE(sc, 3, 0x00A1);
432         PHY_WRITE(sc, 2, 0x0008);
433         PHY_WRITE(sc, 1, 0x1020);
434         PHY_WRITE(sc, 0, 0x1000);
435         PHY_SETBIT(sc, 4, 0x0800);
436         PHY_CLRBIT(sc, 4, 0x0800);
437         val = (PHY_READ(sc, 4) & 0xFFF) | 0x7000;
438         PHY_WRITE(sc, 4, val);
439         PHY_WRITE(sc, 3, 0xFF41);
440         PHY_WRITE(sc, 2, 0xDE60);
441         PHY_WRITE(sc, 1, 0x0140);
442         PHY_WRITE(sc, 0, 0x0077);
443         val = (PHY_READ(sc, 4) & 0xFFF) | 0xA000;
444         PHY_WRITE(sc, 4, val);
445         PHY_WRITE(sc, 3, 0xDF01);
446         PHY_WRITE(sc, 2, 0xDF20);
447         PHY_WRITE(sc, 1, 0xFF95);
448         PHY_WRITE(sc, 0, 0xFA00);
449         val = (PHY_READ(sc, 4) & 0xFFF) | 0xB000;
450         PHY_WRITE(sc, 4, val);
451         PHY_WRITE(sc, 3, 0xFF41);
452         PHY_WRITE(sc, 2, 0xDE20);
453         PHY_WRITE(sc, 1, 0x0140);
454         PHY_WRITE(sc, 0, 0x00BB);
455         val = (PHY_READ(sc, 4) & 0xFFF) | 0xF000;
456         PHY_WRITE(sc, 4, val);
457         PHY_WRITE(sc, 3, 0xDF01);
458         PHY_WRITE(sc, 2, 0xDF20);
459         PHY_WRITE(sc, 1, 0xFF95);
460         PHY_WRITE(sc, 0, 0xBF00);
461         PHY_SETBIT(sc, 4, 0x0800);
462         PHY_CLRBIT(sc, 4, 0x0800);
463         PHY_WRITE(sc, 31, 0x0000);
464         
465         DELAY(40);
466 }
467
468 static void
469 rgephy_reset(struct mii_softc *sc)
470 {
471         mii_phy_reset(sc);
472         DELAY(1000);
473         PHY_WRITE(sc, RGEPHY_MII_BMCR, RGEPHY_BMCR_AUTOEN);
474         DELAY(1000);
475         rgephy_load_dspcode(sc);
476 }