]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/tdkphy.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / sys / dev / mii / tdkphy.c
1 /*-
2  * Copyright (c) 2000,2001 Jonathan Chen.
3  * 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  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*
33  * Driver for the TDK 78Q2120 MII
34  *
35  * References:
36  *   Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf
37  *   Most of this code stolen from ukphy.c
38  */
39
40 /*
41  * The TDK 78Q2120 is found on some Xircom X3201 based cardbus cards.  It's just
42  * like any other normal phy, except it does auto negotiation in a different
43  * way.
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/module.h>
52 #include <sys/bus.h>
53
54 #include <net/if.h>
55 #include <net/if_media.h>
56
57 #include <machine/clock.h>
58
59 #include <dev/mii/mii.h>
60 #include <dev/mii/miivar.h>
61 #include "miidevs.h"
62
63 #include <dev/mii/tdkphyreg.h>
64
65 #include "miibus_if.h"
66
67 static int tdkphy_probe(device_t);
68 static int tdkphy_attach(device_t);
69
70 static device_method_t tdkphy_methods[] = {
71         /* device interface */
72         DEVMETHOD(device_probe,         tdkphy_probe),
73         DEVMETHOD(device_attach,        tdkphy_attach),
74         DEVMETHOD(device_detach,        mii_phy_detach),
75         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
76         { 0, 0 }
77 };
78
79 static devclass_t tdkphy_devclass;
80
81 static driver_t tdkphy_driver = {
82         "tdkphy",
83         tdkphy_methods,
84         sizeof(struct mii_softc)
85 };
86
87 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
88
89 static int tdkphy_service(struct mii_softc *, struct mii_data *, int);
90 static void tdkphy_status(struct mii_softc *);
91
92 static const struct mii_phydesc tdkphys[] = {
93         MII_PHY_DESC(TDK, 78Q2120),
94         MII_PHY_END
95 };
96
97 static int
98 tdkphy_probe(device_t dev)
99 {
100
101         return (mii_phy_dev_probe(dev, tdkphys, BUS_PROBE_DEFAULT));
102 }
103
104 static int
105 tdkphy_attach(device_t dev)
106 {
107         struct mii_softc *sc;
108         struct mii_attach_args *ma;
109         struct mii_data *mii;
110         sc = device_get_softc(dev);
111         ma = device_get_ivars(dev);
112         sc->mii_dev = device_get_parent(dev);
113         mii = device_get_softc(sc->mii_dev);
114         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
115
116         if (bootverbose)
117                 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
118                     MII_OUI(ma->mii_id1, ma->mii_id2),
119                     MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
120
121         sc->mii_inst = mii->mii_instance;
122         sc->mii_phy = ma->mii_phyno;
123         sc->mii_service = tdkphy_service;
124         sc->mii_pdata = mii;
125
126         mii->mii_instance++;
127
128         /*
129          * Apparently, we can't do loopback on this PHY.
130          */
131         sc->mii_flags |= MIIF_NOLOOP;
132
133         mii_phy_reset(sc);
134
135         sc->mii_capabilities =
136             PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
137         device_printf(dev, " ");
138         mii_phy_add_media(sc);
139         printf("\n");
140
141         MIIBUS_MEDIAINIT(sc->mii_dev);
142         return (0);
143 }
144
145 static int
146 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
147 {
148         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
149         int reg;
150
151         switch (cmd) {
152         case MII_POLLSTAT:
153                 /*
154                  * If we're not polling our PHY instance, just return.
155                  */
156                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
157                         return (0);
158                 break;
159
160         case MII_MEDIACHG:
161                 /*
162                  * If the media indicates a different PHY instance,
163                  * isolate ourselves.
164                  */
165                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
166                         reg = PHY_READ(sc, MII_BMCR);
167                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
168                         return (0);
169                 }
170
171                 /*
172                  * If the interface is not up, don't do anything.
173                  */
174                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
175                         break;
176
177                 mii_phy_setmedia(sc);
178                 break;
179
180         case MII_TICK:
181                 /*
182                  * If we're not currently selected, just return.
183                  */
184                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
185                         return (0);
186                 if (mii_phy_tick(sc) == EJUSTRETURN)
187                         return (0);
188                 break;
189         }
190
191         /* Update the media status. */
192         tdkphy_status(sc);
193         if (sc->mii_pdata->mii_media_active & IFM_FDX)
194                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
195         else
196                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
197
198         /* Callback if something changed. */
199         mii_phy_update(sc, cmd);
200         return (0);
201 }
202
203 static void
204 tdkphy_status(struct mii_softc *phy)
205 {
206         struct mii_data *mii = phy->mii_pdata;
207         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
208         int bmsr, bmcr, anlpar, diag;
209
210         mii->mii_media_status = IFM_AVALID;
211         mii->mii_media_active = IFM_ETHER;
212
213         bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
214         if (bmsr & BMSR_LINK)
215                 mii->mii_media_status |= IFM_ACTIVE;
216
217         bmcr = PHY_READ(phy, MII_BMCR);
218         if (bmcr & BMCR_ISO) {
219                 mii->mii_media_active |= IFM_NONE;
220                 mii->mii_media_status = 0;
221                 return;
222         }
223
224         if (bmcr & BMCR_LOOP)
225                 mii->mii_media_active |= IFM_LOOP;
226
227         if (bmcr & BMCR_AUTOEN) {
228                 /*
229                  * NWay autonegotiation takes the highest-order common
230                  * bit of the ANAR and ANLPAR (i.e. best media advertised
231                  * both by us and our link partner).
232                  */
233                 if ((bmsr & BMSR_ACOMP) == 0) {
234                         /* Erg, still trying, I guess... */
235                         mii->mii_media_active |= IFM_NONE;
236                         return;
237                 }
238
239                 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
240                 /*
241                  * ANLPAR doesn't get set on my card, but we check it anyway,
242                  * since it is mentioned in the 78Q2120 specs.
243                  */
244                 if (anlpar & ANLPAR_T4)
245                         mii->mii_media_active |= IFM_100_T4;
246                 else if (anlpar & ANLPAR_TX_FD)
247                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
248                 else if (anlpar & ANLPAR_TX)
249                         mii->mii_media_active |= IFM_100_TX;
250                 else if (anlpar & ANLPAR_10_FD)
251                         mii->mii_media_active |= IFM_10_T|IFM_FDX;
252                 else if (anlpar & ANLPAR_10)
253                         mii->mii_media_active |= IFM_10_T;
254                 else {
255                         /*
256                          * ANLPAR isn't set, which leaves two possibilities:
257                          * 1) Auto negotiation failed
258                          * 2) Auto negotiation completed, but the card forgot
259                          *    to set ANLPAR.
260                          * So we check the MII_DIAG(18) register...
261                          */
262                         diag = PHY_READ(phy, MII_DIAG);
263                         if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
264                                 mii->mii_media_active |= IFM_10_T;
265                         else {
266                                 if (diag & DIAG_DUPLEX)
267                                         mii->mii_media_active |= IFM_FDX;
268                                 if (diag & DIAG_RATE_100)
269                                         mii->mii_media_active |= IFM_100_TX;
270                                 else
271                                         mii->mii_media_active |= IFM_10_T;
272                         }
273                 }
274         } else
275                 mii->mii_media_active = ife->ifm_media;
276 }