kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / netif / mii_layer / e1000phy.c
1 /* $FreeBSD: src/sys/dev/mii/e1000phy.c,v 1.1.2.2 2002/11/08 21:53:49 semenu Exp $ */
2 /* $DragonFly: src/sys/dev/netif/mii_layer/e1000phy.c,v 1.3 2003/08/07 21:17:03 dillon Exp $ */
3 /*
4  * Principal Author: Parag Patel
5  * Copyright (c) 2001
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * Additonal Copyright (c) 2001 by Traakan Software under same licence.
31  * Secondary Author: Matthew Jacob
32  */
33
34 /*
35  * driver for the Marvell 88E1000 series external 1000/100/10-BT PHY.
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/bus.h>
43
44 #include <machine/clock.h>
45
46 #include <net/if.h>
47 #include <net/if_media.h>
48
49 #include "mii.h"
50 #include "miivar.h"
51 #include "miidevs.h"
52
53 #include "e1000phyreg.h"
54
55 #include "miibus_if.h"
56
57 static int e1000phy_probe(device_t);
58 static int e1000phy_attach(device_t);
59 static int e1000phy_detach(device_t);
60
61 static device_method_t e1000phy_methods[] = {
62         /* device interface */
63         DEVMETHOD(device_probe,         e1000phy_probe),
64         DEVMETHOD(device_attach,        e1000phy_attach),
65         DEVMETHOD(device_detach,        e1000phy_detach),
66         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
67         { 0, 0 }
68 };
69
70 static devclass_t e1000phy_devclass;
71 static driver_t e1000phy_driver = {
72         "e1000phy", e1000phy_methods, sizeof (struct mii_softc)
73 };
74 DRIVER_MODULE(e1000phy, miibus, e1000phy_driver, e1000phy_devclass, 0, 0);
75
76 int     e1000phy_service(struct mii_softc *, struct mii_data *, int);
77 void    e1000phy_status(struct mii_softc *);
78
79 static int      e1000phy_mii_phy_auto(struct mii_softc *, int);
80 extern void     mii_phy_auto_timeout(void *);
81 static void e1000phy_reset(struct mii_softc *);
82
83 static int e1000phy_debug = 0;
84
85 static int
86 e1000phy_probe(device_t dev)
87 {
88         struct mii_attach_args *ma;
89         u_int32_t id;
90
91         ma = device_get_ivars(dev);
92         id = ((ma->mii_id1 << 16) | ma->mii_id2) & E1000_ID_MASK;
93
94         if (id != E1000_ID_88E1000 && id != E1000_ID_88E1000S) {
95                 return ENXIO;
96         }
97
98         device_set_desc(dev, MII_STR_MARVELL_E1000);
99         return 0;
100 }
101
102 static int
103 e1000phy_attach(device_t dev)
104 {
105         struct mii_softc *sc;
106         struct mii_attach_args *ma;
107         struct mii_data *mii;
108         const char *sep = "";
109
110         getenv_int("e1000phy_debug", &e1000phy_debug);
111
112         sc = device_get_softc(dev);
113         ma = device_get_ivars(dev);
114         sc->mii_dev = device_get_parent(dev);
115         mii = device_get_softc(sc->mii_dev);
116         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
117
118         sc->mii_inst = mii->mii_instance;
119         sc->mii_phy = ma->mii_phyno;
120         sc->mii_service = e1000phy_service;
121         sc->mii_pdata = mii;
122
123         sc->mii_flags |= MIIF_NOISOLATE;
124         mii->mii_instance++;
125         e1000phy_reset(sc);
126
127 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
128 #define PRINT(s)        printf("%s%s", sep, s); sep = ", "
129
130 #if     0
131         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
132             E1000_CR_ISOLATE);
133 #endif
134
135         device_printf(dev, " ");
136         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX, sc->mii_inst),
137                         E1000_CR_SPEED_1000 | E1000_CR_FULL_DUPLEX);
138         PRINT("1000baseTX-FDX");
139         /*
140         TODO - apparently 1000BT-simplex not supported?
141         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0, sc->mii_inst),
142                         E1000_CR_SPEED_1000);
143         PRINT("1000baseTX");
144         */
145         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
146                         E1000_CR_SPEED_100 | E1000_CR_FULL_DUPLEX);
147         PRINT("100baseTX-FDX");
148         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
149                         E1000_CR_SPEED_100);
150         PRINT("100baseTX");
151         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
152                         E1000_CR_SPEED_10 | E1000_CR_FULL_DUPLEX);
153         PRINT("10baseTX-FDX");
154         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
155                         E1000_CR_SPEED_10);
156         PRINT("10baseTX");
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         MIIBUS_MEDIAINIT(sc->mii_dev);
165         return(0);
166 }
167
168 static int
169 e1000phy_detach(device_t dev)
170 {
171         struct mii_softc *sc;
172         struct mii_data *mii;
173
174         sc = device_get_softc(dev);
175         mii = device_get_softc(device_get_parent(dev));
176
177         if (sc->mii_flags & MIIF_DOINGAUTO)
178                 untimeout(mii_phy_auto_timeout, sc, sc->mii_auto_ch);
179
180         sc->mii_dev = NULL;
181         LIST_REMOVE(sc, mii_list);
182
183         return 0;
184 }
185
186 static void
187 e1000phy_reset(struct mii_softc *sc)
188 {
189         u_int32_t reg;
190         int i;
191
192         /* initialize custom E1000 registers to magic values */
193         reg = PHY_READ(sc, E1000_SCR);
194         reg &= ~E1000_SCR_AUTO_X_MODE;
195         PHY_WRITE(sc, E1000_SCR, reg);
196
197         /* normal PHY reset */
198         /*mii_phy_reset(sc);*/
199         reg = PHY_READ(sc, E1000_CR);
200         reg |= E1000_CR_RESET;
201         PHY_WRITE(sc, E1000_CR, reg);
202
203         for (i = 0; i < 500; i++) {
204                 DELAY(1);
205                 reg = PHY_READ(sc, E1000_CR);
206                 if (!(reg & E1000_CR_RESET))
207                         break;
208         }
209
210         /* set more custom E1000 registers to magic values */
211         reg = PHY_READ(sc, E1000_SCR);
212         reg |= E1000_SCR_ASSERT_CRS_ON_TX;
213         PHY_WRITE(sc, E1000_SCR, reg);
214
215         reg = PHY_READ(sc, E1000_ESCR);
216         reg |= E1000_ESCR_TX_CLK_25;
217         PHY_WRITE(sc, E1000_ESCR, reg);
218
219         /* even more magic to reset DSP? */
220         PHY_WRITE(sc, 29, 0x1d);
221         PHY_WRITE(sc, 30, 0xc1);
222         PHY_WRITE(sc, 30, 0x00);
223 }
224
225 int
226 e1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
227 {
228         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
229         int reg;
230
231         switch (cmd) {
232         case MII_POLLSTAT:
233                 /*
234                  * If we're not polling our PHY instance, just return.
235                  */
236                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
237                         return (0);
238                 break;
239
240         case MII_MEDIACHG:
241                 /*
242                  * If the media indicates a different PHY instance,
243                  * isolate ourselves.
244                  */
245                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
246                         reg = PHY_READ(sc, E1000_CR);
247                         PHY_WRITE(sc, E1000_CR, reg | E1000_CR_ISOLATE);
248                         return (0);
249                 }
250
251                 /*
252                  * If the interface is not up, don't do anything.
253                  */
254                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
255                         break;
256                 }
257
258                 switch (IFM_SUBTYPE(ife->ifm_media)) {
259                 case IFM_AUTO:
260                         /*
261                          * If we're already in auto mode, just return.
262                          */
263                         if (sc->mii_flags & MIIF_DOINGAUTO) {
264                                 return (0);
265                         }
266                         e1000phy_reset(sc);
267                         (void)e1000phy_mii_phy_auto(sc, 1);
268                         break;
269
270                 case IFM_1000_TX:
271                         if (sc->mii_flags & MIIF_DOINGAUTO)
272                                 return (0);
273
274                         e1000phy_reset(sc);
275
276                         /* TODO - any other way to force 1000BT? */
277                         (void)e1000phy_mii_phy_auto(sc, 1);
278                         break;
279
280                 case IFM_100_TX:
281                         e1000phy_reset(sc);
282
283                         if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
284                                 PHY_WRITE(sc, E1000_CR,
285                                     E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_100);
286                                 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX_FD);
287                         } else {
288                                 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_100);
289                                 PHY_WRITE(sc, E1000_AR, E1000_AR_100TX);
290                         }
291                         break;
292
293                 case IFM_10_T:
294                         e1000phy_reset(sc);
295
296                         if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
297                                 PHY_WRITE(sc, E1000_CR,
298                                     E1000_CR_FULL_DUPLEX | E1000_CR_SPEED_10);
299                                 PHY_WRITE(sc, E1000_AR, E1000_AR_10T_FD);
300                         } else {
301                                 PHY_WRITE(sc, E1000_CR, E1000_CR_SPEED_10);
302                                 PHY_WRITE(sc, E1000_AR, E1000_AR_10T);
303                         }
304
305                         break;
306
307                 default:
308                         return (EINVAL);
309                 }
310
311                 break;
312
313         case MII_TICK:
314                 /*
315                  * If we're not currently selected, just return.
316                  */
317                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
318                         return (0);
319                 }
320
321                 /*
322                  * Only used for autonegotiation.
323                  */
324                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
325                         return (0);
326                 }
327
328                 /*
329                  * Is the interface even up?
330                  */
331                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
332                         return (0);
333                 }
334
335                 /*
336                  * Only retry autonegotiation every 5 seconds.
337                  */
338                 if (++(sc->mii_ticks) != 5) {
339                         return (0);
340                 }
341                 sc->mii_ticks = 0;
342
343                 /*
344                  * Check to see if we have link.  If we do, we don't
345                  * need to restart the autonegotiation process.  Read
346                  * the BMSR twice in case it's latched.
347                  */
348                 reg = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
349
350                 if (reg & E1000_SR_LINK_STATUS)
351                         break;
352
353                 e1000phy_reset(sc);
354
355                 if (e1000phy_mii_phy_auto(sc, 0) == EJUSTRETURN) {
356                         return(0);
357                 }
358
359                 break;
360         }
361
362         /* Update the media status. */
363         e1000phy_status(sc);
364
365         /* Callback if something changed. */
366         if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
367                 MIIBUS_STATCHG(sc->mii_dev);
368                 sc->mii_active = mii->mii_media_active;
369         }
370
371         return (0);
372 }
373
374 void
375 e1000phy_status(struct mii_softc *sc)
376 {
377         struct mii_data *mii = sc->mii_pdata;
378         int bmsr, bmcr, esr, ssr, isr, ar, lpar;
379
380         mii->mii_media_status = IFM_AVALID;
381         mii->mii_media_active = IFM_ETHER;
382
383         bmsr = PHY_READ(sc, E1000_SR) | PHY_READ(sc, E1000_SR);
384         esr = PHY_READ(sc, E1000_ESR);
385         bmcr = PHY_READ(sc, E1000_CR);
386         ssr = PHY_READ(sc, E1000_SSR);
387         isr = PHY_READ(sc, E1000_ISR);
388         ar = PHY_READ(sc, E1000_AR);
389         lpar = PHY_READ(sc, E1000_LPAR);
390
391         if (bmsr & E1000_SR_LINK_STATUS)
392                 mii->mii_media_status |= IFM_ACTIVE;
393
394         if (bmcr & E1000_CR_LOOPBACK)
395                 mii->mii_media_active |= IFM_LOOP;
396
397         if ((sc->mii_flags & MIIF_DOINGAUTO) &&
398             (!(bmsr & E1000_SR_AUTO_NEG_COMPLETE) || !(ssr & E1000_SSR_LINK) ||
399             !(ssr & E1000_SSR_SPD_DPLX_RESOLVED))) {
400                 /* Erg, still trying, I guess... */
401                 mii->mii_media_active |= IFM_NONE;
402                 return;
403         }
404
405         if (ssr & E1000_SSR_1000MBS)
406                 mii->mii_media_active |= IFM_1000_TX;
407         else if (ssr & E1000_SSR_100MBS)
408                 mii->mii_media_active |= IFM_100_TX;
409         else
410                 mii->mii_media_active |= IFM_10_T;
411
412         if (ssr & E1000_SSR_DUPLEX)
413                 mii->mii_media_active |= IFM_FDX;
414         else
415                 mii->mii_media_active |= IFM_HDX;
416
417         /* FLAG0==rx-flow-control FLAG1==tx-flow-control */
418         if ((ar & E1000_AR_PAUSE) && (lpar & E1000_LPAR_PAUSE)) {
419                 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
420         } else if (!(ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
421             (lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
422                 mii->mii_media_active |= IFM_FLAG1;
423         } else if ((ar & E1000_AR_PAUSE) && (ar & E1000_AR_ASM_DIR) &&
424             !(lpar & E1000_LPAR_PAUSE) && (lpar & E1000_LPAR_ASM_DIR)) {
425                 mii->mii_media_active |= IFM_FLAG0;
426         }
427 }
428
429 static int
430 e1000phy_mii_phy_auto(struct mii_softc *mii, int waitfor)
431 {
432         int bmsr, i;
433
434         if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
435                 PHY_WRITE(mii, E1000_AR, E1000_AR_10T | E1000_AR_10T_FD |
436                     E1000_AR_100TX | E1000_AR_100TX_FD | 
437                     E1000_AR_PAUSE | E1000_AR_ASM_DIR);
438                 PHY_WRITE(mii, E1000_1GCR, E1000_1GCR_1000T_FD);
439                 PHY_WRITE(mii, E1000_CR,
440                     E1000_CR_AUTO_NEG_ENABLE | E1000_CR_RESTART_AUTO_NEG);
441         }
442
443         if (waitfor) {
444                 /* Wait 500ms for it to complete. */
445                 for (i = 0; i < 500; i++) {
446                         bmsr =
447                             PHY_READ(mii, E1000_SR) | PHY_READ(mii, E1000_SR);
448
449                         if (bmsr & E1000_SR_AUTO_NEG_COMPLETE) {
450                                 return (0);
451                         }
452                         DELAY(1000);
453                 }
454
455                 /*
456                  * Don't need to worry about clearing MIIF_DOINGAUTO.
457                  * If that's set, a timeout is pending, and it will
458                  * clear the flag. [do it anyway]
459                  */
460         }
461
462         /*
463          * Just let it finish asynchronously.  This is for the benefit of
464          * the tick handler driving autonegotiation.  Don't want 500ms
465          * delays all the time while the system is running!
466          */
467         if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
468                 mii->mii_flags |= MIIF_DOINGAUTO;
469                 mii->mii_ticks = 0;
470                 mii->mii_auto_ch = timeout(mii_phy_auto_timeout, mii, hz >> 1);
471         }
472         return (EJUSTRETURN);
473 }