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