kernel: Use DEVMETHOD_END in the drivers.
[dragonfly.git] / sys / dev / netif / mii_layer / mlphy.c
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  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/mlphy.c,v 1.2.2.3 2001/02/09 09:50:15 asmodai Exp $
33  */
34
35 /*
36  * driver for Micro Linear 6692 PHYs
37  *
38  * The Micro Linear 6692 is a strange beast, and dealing with it using
39  * this code framework is tricky. The 6692 is actually a 100Mbps-only
40  * device, which means that a second PHY is required to support 10Mbps
41  * modes. However, even though the 6692 does not support 10Mbps modes,
42  * it can still advertise them when performing autonegotiation. If a
43  * 10Mbps mode is negotiated, we must program the registers of the
44  * companion PHY accordingly in addition to programming the registers
45  * of the 6692.
46  *
47  * This device also does not have vendor/device ID registers.
48  */
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/module.h>
56 #include <sys/bus.h>
57
58 #include <machine/clock.h>
59
60 #include <net/if.h>
61 #include <net/if_media.h>
62
63 #include "mii.h"
64 #include "miivar.h"
65
66 #include "miibus_if.h"
67
68 #define ML_STATE_AUTO_SELF      1
69 #define ML_STATE_AUTO_OTHER     2
70
71 struct mlphy_softc      {
72         struct mii_softc        ml_mii;
73         int                     ml_state;
74         int                     ml_linked;
75 };
76
77 static int mlphy_probe          (device_t);
78 static int mlphy_attach         (device_t);
79 static int mlphy_detach         (device_t);
80
81 static device_method_t mlphy_methods[] = {
82         /* device interface */
83         DEVMETHOD(device_probe,         mlphy_probe),
84         DEVMETHOD(device_attach,        mlphy_attach),
85         DEVMETHOD(device_detach,        mlphy_detach),
86         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
87         DEVMETHOD_END
88 };
89
90 static devclass_t mlphy_devclass;
91
92 static driver_t mlphy_driver = {
93         "mlphy",
94         mlphy_methods,
95         sizeof(struct mlphy_softc)
96 };
97
98 DRIVER_MODULE(mlphy, miibus, mlphy_driver, mlphy_devclass, NULL, NULL);
99
100 static int      mlphy_service (struct mii_softc *, struct mii_data *, int);
101 static void     mlphy_reset (struct mii_softc *);
102 static void     mlphy_status (struct mii_softc *);
103
104 static int
105 mlphy_probe(device_t dev)
106 {
107         struct mii_attach_args  *ma;
108         device_t                parent;
109
110         ma = device_get_ivars(dev);
111         parent = device_get_parent(device_get_parent(dev));
112
113         /*
114          * Micro Linear PHY reports oui == 0 model == 0
115          */
116         if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
117             MII_MODEL(ma->mii_id2) != 0)
118                 return (ENXIO);
119
120         /*
121          * Make sure the parent is a `tl'. So far, I have only
122          * encountered the 6692 on an Olicom card with a ThunderLAN
123          * controller chip.
124          */
125         if (strcmp(device_get_name(parent), "tl") != 0)
126                 return (ENXIO);
127
128         device_set_desc(dev, "Micro Linear 6692 media interface");
129
130         return (0);
131 }
132
133 static int
134 mlphy_attach(device_t dev)
135 {
136         struct mlphy_softc *msc;
137         struct mii_softc *sc;
138         struct mii_attach_args *ma;
139         struct mii_data *mii;
140
141         msc = device_get_softc(dev);
142         sc = &msc->ml_mii;
143         ma = device_get_ivars(dev);
144         mii_softc_init(sc, ma);
145         sc->mii_dev = device_get_parent(dev);
146         mii = device_get_softc(sc->mii_dev);
147         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
148
149         sc->mii_inst = mii->mii_instance;
150         sc->mii_service = mlphy_service;
151         sc->mii_reset = mlphy_reset;
152         sc->mii_pdata = mii;
153
154         mii->mii_instance++;
155
156 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
157
158 #if 0 /* See above. */
159         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
160             MII_MEDIA_NONE);
161 #endif
162         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
163             MII_MEDIA_100_TX);
164
165         sc->mii_flags &= ~MIIF_NOISOLATE;
166         mii_phy_reset(sc);
167         sc->mii_flags |= MIIF_NOISOLATE;
168
169         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
170
171         device_printf(dev, " ");
172         if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
173                 kprintf("no media present");
174         else
175                 mii_phy_add_media(sc);
176         kprintf("\n");
177 #undef ADD
178         MIIBUS_MEDIAINIT(sc->mii_dev);
179         return(0);
180 }
181
182 static int
183 mlphy_detach(device_t dev)
184 {
185         struct mlphy_softc *sc;
186
187         sc = device_get_softc(dev);
188         sc->ml_mii.mii_dev = NULL;
189         LIST_REMOVE(&sc->ml_mii, mii_list);
190
191         return(0);
192 }
193
194 static int
195 mlphy_service(struct mii_softc *xsc, struct mii_data *mii, int cmd)
196 {
197         struct ifmedia_entry    *ife = mii->mii_media.ifm_cur;
198         int                     reg;
199         struct mii_softc        *other = NULL;
200         struct mlphy_softc      *msc = (struct mlphy_softc *)xsc;
201         struct mii_softc        *sc = &msc->ml_mii;
202         device_t                *devlist;
203         int                     devs, i;
204         const struct mii_media  *mm;
205
206         /*
207          * See if there's another PHY on this bus with us.
208          * If so, we may need it for 10Mbps modes.
209          */
210         device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
211         for (i = 0; i < devs; i++) {
212                 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
213                         other = device_get_softc(devlist[i]);
214                         break;
215                 }
216         }
217         kfree(devlist, M_TEMP);
218
219         KKASSERT(ife->ifm_data >= 0 && ife->ifm_data < MII_NMEDIA);
220         mm = &mii_media_table[ife->ifm_data];
221
222         switch (cmd) {
223         case MII_POLLSTAT:
224                 /*
225                  * If we're not polling our PHY instance, just return.
226                  */
227                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
228                         return (0);
229                 break;
230
231         case MII_MEDIACHG:
232                 /*
233                  * If the media indicates a different PHY instance,
234                  * isolate ourselves.
235                  */
236                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
237                         reg = PHY_READ(sc, MII_BMCR);
238                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
239                         return (0);
240                 }
241
242                 /*
243                  * If the interface is not up, don't do anything.
244                  */
245                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
246                         break;
247
248                 switch (IFM_SUBTYPE(ife->ifm_media)) {
249                 case IFM_AUTO:
250                         /*
251                          * For autonegotiation, reset and isolate the
252                          * companion PHY (if any) and then do NWAY
253                          * autonegotiation ourselves.
254                          */
255                         msc->ml_state = ML_STATE_AUTO_SELF;
256                         if (other != NULL) {
257                                 mii_phy_reset(other);
258                                 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
259                         }
260                         mii_phy_auto(sc, 1);
261                         msc->ml_linked = 0;
262                         return(0);
263
264                 case IFM_10_T:
265                         /*
266                          * For 10baseT modes, reset and program the
267                          * companion PHY (of any), then program ourselves
268                          * to match. This will put us in pass-through
269                          * mode and let the companion PHY do all the
270                          * work.
271                          */
272                         if (other != NULL) {
273                                 mii_phy_reset(other);
274                                 PHY_WRITE(other, MII_BMCR, mm->mm_bmcr);
275                         }
276                         PHY_WRITE(sc, MII_ANAR, mm->mm_anar);
277                         PHY_WRITE(sc, MII_BMCR, mm->mm_bmcr);
278                         msc->ml_state = 0;
279                         break;
280
281                 case IFM_100_TX:
282                         /*
283                          * For 100baseTX modes, reset and isolate the
284                          * companion PHY (if any), then program ourselves
285                          * accordingly.
286                          */
287                         if (other != NULL) {
288                                 mii_phy_reset(other);
289                                 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
290                         }
291                         PHY_WRITE(sc, MII_ANAR, mm->mm_anar);
292                         PHY_WRITE(sc, MII_BMCR, mm->mm_bmcr);
293                         msc->ml_state = 0;
294                         break;
295
296                 case IFM_100_T4:
297                         /*
298                          * XXX Not supported as a manual setting right now.
299                          */
300                         return (EINVAL);
301                 default:
302                         break;
303                 }
304                 break;
305
306         case MII_TICK:
307                 /*
308                  * If we're not currently selected, just return.
309                  */
310                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
311                         return (0);
312
313                 /*
314                  * Is the interface even up?
315                  */
316                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
317                         return (0);
318
319                 /*
320                  * Only used for autonegotiation.
321                  */
322                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
323                         break;
324
325                 /*
326                  * Check to see if we have link.  If we do, we don't
327                  * need to restart the autonegotiation process.  Read
328                  * the BMSR twice in case it's latched.
329                  * If we're in a 10Mbps mode, check the link of the
330                  * 10Mbps PHY. Sometimes the Micro Linear PHY's
331                  * linkstat bit will clear while the linkstat bit of
332                  * the companion PHY will remain set.
333                  */
334                 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
335                         reg = PHY_READ(other, MII_BMSR) |
336                             PHY_READ(other, MII_BMSR);
337                 } else {
338                         reg = PHY_READ(sc, MII_BMSR) |
339                             PHY_READ(sc, MII_BMSR);
340                 }
341                 if (reg & BMSR_LINK) {
342                         if (!msc->ml_linked) {
343                                 msc->ml_linked = 1;
344                                 mlphy_status(sc);
345                         }
346                         sc->mii_ticks = 0;
347                         break;
348                 }
349
350                 /*
351                  * Only retry autonegotiation every 5 seconds.
352                  */
353                 if (++sc->mii_ticks <= sc->mii_anegticks)
354                         return (0);
355                 
356                 sc->mii_ticks = 0;
357                 msc->ml_linked = 0;
358                 mii->mii_media_active = IFM_NONE;
359                 mii_phy_reset(sc);
360                 msc->ml_state = ML_STATE_AUTO_SELF;
361                 if (other != NULL) {
362                         mii_phy_reset(other);
363                         PHY_WRITE(other, MII_BMCR, BMCR_ISO);
364                 }
365                 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
366                         return(0);
367                 break;
368         }
369
370         /* Update the media status. */
371         if (msc->ml_state == ML_STATE_AUTO_OTHER) {
372                 int other_inst;
373
374                 other_inst = other->mii_inst;
375                 other->mii_inst = sc->mii_inst;
376                 other->mii_service(other, mii, MII_POLLSTAT);
377                 other->mii_inst = other_inst;
378                 sc->mii_media_active = other->mii_media_active;
379                 sc->mii_media_status = other->mii_media_status;
380         } else {
381                 ukphy_status(sc);
382         }
383
384         /* Callback if something changed. */
385         mii_phy_update(sc, cmd);
386         return (0);
387 }
388
389 /*
390  * The Micro Linear PHY comes out of reset with the 'autoneg
391  * enable' bit set, which we don't want.
392  */
393 static void
394 mlphy_reset(struct mii_softc *sc)
395 {
396         int                     reg;
397
398         mii_phy_reset(sc);
399         reg = PHY_READ(sc, MII_BMCR);
400         reg &= ~BMCR_AUTOEN;
401         PHY_WRITE(sc, MII_BMCR, reg);
402 }
403
404 /*
405  * If we negotiate a 10Mbps mode, we need to check for an alternate
406  * PHY and make sure it's enabled and set correctly.
407  */
408 static void
409 mlphy_status(struct mii_softc *sc)
410 {
411         struct mlphy_softc      *msc = (struct mlphy_softc *)sc;
412         struct mii_data         *mii = msc->ml_mii.mii_pdata;
413         struct mii_softc        *other = NULL;
414         device_t                *devlist;
415         int                     devs, i;
416
417         /* See if there's another PHY on the bus with us. */
418         device_get_children(msc->ml_mii.mii_dev, &devlist, &devs);
419         for (i = 0; i < devs; i++) {
420                 if (strcmp(device_get_name(devlist[i]), "mlphy")) {
421                         other = device_get_softc(devlist[i]);
422                         break;
423                 }
424         }
425         kfree(devlist, M_TEMP);
426
427         if (other == NULL)
428                 return;
429
430         ukphy_status(sc);
431
432         if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
433                 msc->ml_state = ML_STATE_AUTO_SELF;
434                 mii_phy_reset(other);
435                 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
436         }
437
438         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
439                 msc->ml_state = ML_STATE_AUTO_OTHER;
440                 mlphy_reset(&msc->ml_mii);
441                 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
442                 mii_phy_reset(other);
443                 mii_phy_auto(other, 1);
444         }
445 }