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