]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/mii/mlphy.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / dev / mii / 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  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * driver for Micro Linear 6692 PHYs
39  *
40  * The Micro Linear 6692 is a strange beast, and dealing with it using
41  * this code framework is tricky. The 6692 is actually a 100Mbps-only
42  * device, which means that a second PHY is required to support 10Mbps
43  * modes. However, even though the 6692 does not support 10Mbps modes,
44  * it can still advertise them when performing autonegotiation. If a
45  * 10Mbps mode is negotiated, we must program the registers of the
46  * companion PHY accordingly in addition to programming the registers
47  * of the 6692.
48  *
49  * This device also does not have vendor/device ID registers.
50  */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <sys/malloc.h>
59
60 #include <net/if.h>
61 #include <net/if_media.h>
62
63 #include <dev/mii/mii.h>
64 #include <dev/mii/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         device_t                ml_dev;
74         int                     ml_state;
75         int                     ml_linked;
76 };
77
78 static int mlphy_probe(device_t);
79 static int mlphy_attach(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,        mii_phy_detach),
86         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
87         { 0, 0 }
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, 0, 0);
99
100 static struct mii_softc *mlphy_find_other(struct mlphy_softc *);
101 static int      mlphy_service(struct mii_softc *, struct mii_data *, int);
102 static void     mlphy_reset(struct mii_softc *);
103 static void     mlphy_status(struct mii_softc *);
104
105 static int
106 mlphy_probe(dev)
107         device_t                dev;
108 {
109         struct mii_attach_args  *ma;
110
111         ma = device_get_ivars(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(device_get_parent(device_get_parent(dev))),
126             "tl") != 0)
127                 return (ENXIO);
128
129         device_set_desc(dev, "Micro Linear 6692 media interface");
130
131         return (BUS_PROBE_DEFAULT);
132 }
133
134 static int
135 mlphy_attach(dev)
136         device_t                dev;
137 {
138         struct mlphy_softc *msc;
139         struct mii_softc *sc;
140         struct mii_attach_args *ma;
141         struct mii_data *mii;
142
143         msc = device_get_softc(dev);
144         sc = &msc->ml_mii;
145         msc->ml_dev = dev;
146         ma = device_get_ivars(dev);
147         sc->mii_dev = device_get_parent(dev);
148         mii = ma->mii_data;
149         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
150
151         sc->mii_flags = miibus_get_flags(dev);
152         sc->mii_inst = mii->mii_instance++;
153         sc->mii_phy = ma->mii_phyno;
154         sc->mii_service = mlphy_service;
155         sc->mii_pdata = mii;
156
157 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
158
159         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
160             MII_MEDIA_100_TX);
161
162         mii_phy_reset(sc);
163
164         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
165         /* Let the companion PHY (if any) only handle the media we don't. */
166         ma->mii_capmask = ~sc->mii_capabilities;
167         device_printf(dev, " ");
168         mii_phy_add_media(sc);
169         printf("\n");
170 #undef ADD
171         MIIBUS_MEDIAINIT(sc->mii_dev);
172         return (0);
173 }
174
175 static struct mii_softc *
176 mlphy_find_other(struct mlphy_softc *msc)
177 {
178         device_t                *devlist;
179         struct mii_softc *retval;
180         int i, devs;
181
182         retval = NULL;
183         if (device_get_children(msc->ml_mii.mii_dev, &devlist, &devs) != 0)
184                 return (NULL);
185         for (i = 0; i < devs; i++) {
186                 if (devlist[i] != msc->ml_dev) {
187                         retval = device_get_softc(devlist[i]);
188                         break;
189                 }
190         }
191         free(devlist, M_TEMP);
192         return (retval);
193 }
194
195 static int
196 mlphy_service(xsc, mii, cmd)
197         struct mii_softc *xsc;
198         struct mii_data *mii;
199         int cmd;
200 {
201         struct ifmedia_entry    *ife = mii->mii_media.ifm_cur;
202         struct mii_softc        *other = NULL;
203         struct mlphy_softc      *msc = (struct mlphy_softc *)xsc;
204         struct mii_softc        *sc = (struct mii_softc *)&msc->ml_mii;
205         int                     other_inst, reg;
206
207         /*
208          * See if there's another PHY on this bus with us.
209          * If so, we may need it for 10Mbps modes.
210          */
211         other = mlphy_find_other(msc);
212
213         switch (cmd) {
214         case MII_POLLSTAT:
215                 break;
216
217         case MII_MEDIACHG:
218                 /*
219                  * If the interface is not up, don't do anything.
220                  */
221                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
222                         break;
223
224                 switch (IFM_SUBTYPE(ife->ifm_media)) {
225                 case IFM_AUTO:
226                         /*
227                          * For autonegotiation, reset and isolate the
228                          * companion PHY (if any) and then do NWAY
229                          * autonegotiation ourselves.
230                          */
231                         msc->ml_state = ML_STATE_AUTO_SELF;
232                         if (other != NULL) {
233                                 mii_phy_reset(other);
234                                 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
235                         }
236                         (void)mii_phy_auto(sc);
237                         msc->ml_linked = 0;
238                         return (0);
239                 case IFM_10_T:
240                         /*
241                          * For 10baseT modes, reset and program the
242                          * companion PHY (of any), then program ourselves
243                          * to match. This will put us in pass-through
244                          * mode and let the companion PHY do all the
245                          * work.
246                          *
247                          * BMCR data is stored in the ifmedia entry.
248                          */
249                         if (other != NULL) {
250                                 mii_phy_reset(other);
251                                 PHY_WRITE(other, MII_BMCR, ife->ifm_data);
252                         }
253                         mii_phy_setmedia(sc);
254                         msc->ml_state = 0;
255                         break;
256                 case IFM_100_TX:
257                         /*
258                          * For 100baseTX modes, reset and isolate the
259                          * companion PHY (if any), then program ourselves
260                          * accordingly.
261                          *
262                          * BMCR data is stored in the ifmedia entry.
263                          */
264                         if (other != NULL) {
265                                 mii_phy_reset(other);
266                                 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
267                         }
268                         mii_phy_setmedia(sc);
269                         msc->ml_state = 0;
270                         break;
271                 default:
272                         return (EINVAL);
273
274                 }
275                 break;
276
277         case MII_TICK:
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                         break;
289
290                 /*
291                  * Check to see if we have link.  If we do, we don't
292                  * need to restart the autonegotiation process.  Read
293                  * the BMSR twice in case it's latched.
294                  * If we're in a 10Mbps mode, check the link of the
295                  * 10Mbps PHY. Sometimes the Micro Linear PHY's
296                  * linkstat bit will clear while the linkstat bit of
297                  * the companion PHY will remain set.
298                  */
299                 if (msc->ml_state == ML_STATE_AUTO_OTHER) {
300                         reg = PHY_READ(other, MII_BMSR) |
301                             PHY_READ(other, MII_BMSR);
302                 } else {
303                         reg = PHY_READ(sc, MII_BMSR) |
304                             PHY_READ(sc, MII_BMSR);
305                 }
306
307                 if (reg & BMSR_LINK) {
308                         if (!msc->ml_linked) {
309                                 msc->ml_linked = 1;
310                                 mlphy_status(sc);
311                         }
312                         break;
313                 }
314
315                 /*
316                  * Only retry autonegotiation every 5 seconds.
317                  */
318                 if (++sc->mii_ticks <= MII_ANEGTICKS)
319                         break;
320
321                 sc->mii_ticks = 0;
322                 msc->ml_linked = 0;
323                 mii->mii_media_active = IFM_NONE;
324                 mii_phy_reset(sc);
325                 msc->ml_state = ML_STATE_AUTO_SELF;
326                 if (other != NULL) {
327                         mii_phy_reset(other);
328                         PHY_WRITE(other, MII_BMCR, BMCR_ISO);
329                 }
330                 mii_phy_auto(sc);
331                 return (0);
332         }
333
334         /* Update the media status. */
335
336         if (msc->ml_state == ML_STATE_AUTO_OTHER) {
337                 other_inst = other->mii_inst;
338                 other->mii_inst = sc->mii_inst;
339                 if (IFM_INST(ife->ifm_media) == other->mii_inst)
340                         (void)(*other->mii_service)(other, mii, MII_POLLSTAT);
341                 other->mii_inst = other_inst;
342                 sc->mii_media_active = other->mii_media_active;
343                 sc->mii_media_status = other->mii_media_status;
344         } else
345                 ukphy_status(sc);
346
347         /* Callback if something changed. */
348         mii_phy_update(sc, cmd);
349         return (0);
350 }
351
352 /*
353  * The Micro Linear PHY comes out of reset with the 'autoneg
354  * enable' bit set, which we don't want.
355  */
356 static void
357 mlphy_reset(sc)
358         struct mii_softc        *sc;
359 {
360         int                     reg;
361
362         mii_phy_reset(sc);
363         reg = PHY_READ(sc, MII_BMCR);
364         reg &= ~BMCR_AUTOEN;
365         PHY_WRITE(sc, MII_BMCR, reg);
366 }
367
368 /*
369  * If we negotiate a 10Mbps mode, we need to check for an alternate
370  * PHY and make sure it's enabled and set correctly.
371  */
372 static void
373 mlphy_status(sc)
374         struct mii_softc        *sc;
375 {
376         struct mlphy_softc      *msc = (struct mlphy_softc *)sc;
377         struct mii_data         *mii = msc->ml_mii.mii_pdata;
378         struct mii_softc        *other = NULL;
379
380         /* See if there's another PHY on the bus with us. */
381         other = mlphy_find_other(msc);
382         if (other == NULL)
383                 return;
384
385         ukphy_status(sc);
386
387         if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
388                 msc->ml_state = ML_STATE_AUTO_SELF;
389                 mii_phy_reset(other);
390                 PHY_WRITE(other, MII_BMCR, BMCR_ISO);
391         }
392
393         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
394                 msc->ml_state = ML_STATE_AUTO_OTHER;
395                 mlphy_reset(&msc->ml_mii);
396                 PHY_WRITE(&msc->ml_mii, MII_BMCR, BMCR_ISO);
397                 mii_phy_reset(other);
398                 mii_phy_auto(other);
399         }
400 }