]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/tdkphy.c
Catch up to the nvenetlib 1.0-0310 import.
[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,
42  * also spotted on some 3C575 cards.  It's just like any other normal
43  * phy, except it does auto negotiation in a different 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 #if 0
68 #if !defined(lint)
69 static const char rcsid[] =
70   "$Id: tdkphy.c,v 1.3 2000/10/14 06:20:56 jon Exp $";
71 #endif
72 #endif
73
74 static int tdkphy_probe(device_t);
75 static int tdkphy_attach(device_t);
76
77 static device_method_t tdkphy_methods[] = {
78         /* device interface */
79         DEVMETHOD(device_probe,         tdkphy_probe),
80         DEVMETHOD(device_attach,        tdkphy_attach),
81         DEVMETHOD(device_detach,        mii_phy_detach),
82         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
83         { 0, 0 }
84 };
85
86 static devclass_t tdkphy_devclass;
87
88 static driver_t tdkphy_driver = {
89         "tdkphy",
90         tdkphy_methods,
91         sizeof(struct mii_softc)
92 };
93
94 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
95
96 static int tdkphy_service(struct mii_softc *, struct mii_data *, int);
97 static void tdkphy_status(struct mii_softc *);
98
99 static int
100 tdkphy_probe(device_t dev)
101 {
102         struct mii_attach_args *ma;
103         ma = device_get_ivars(dev);
104         if ((MII_OUI(ma->mii_id1, ma->mii_id2) != MII_OUI_TDK ||
105              MII_MODEL(ma->mii_id2) != MII_MODEL_TDK_78Q2120))
106                 return (ENXIO);
107
108         device_set_desc(dev, MII_STR_TDK_78Q2120);
109         return (0);
110 }
111
112 static int
113 tdkphy_attach(device_t dev)
114 {
115         struct mii_softc *sc;
116         struct mii_attach_args *ma;
117         struct mii_data *mii;
118         sc = device_get_softc(dev);
119         ma = device_get_ivars(dev);
120         sc->mii_dev = device_get_parent(dev);
121         mii = device_get_softc(sc->mii_dev);
122         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
123
124         if (bootverbose)
125                 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
126                     MII_OUI(ma->mii_id1, ma->mii_id2),
127                     MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
128
129         sc->mii_inst = mii->mii_instance;
130         sc->mii_phy = ma->mii_phyno;
131         sc->mii_service = tdkphy_service;
132         sc->mii_pdata = mii;
133
134         mii->mii_instance++;
135
136         sc->mii_flags |= MIIF_NOISOLATE;
137
138 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
139
140         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
141             BMCR_ISO);
142 #if 0
143         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
144             BMCR_LOOP|BMCR_S100);
145 #endif
146
147         mii_phy_reset(sc);
148
149         sc->mii_capabilities =
150             PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
151         device_printf(dev, " ");
152         mii_add_media(sc);
153         printf("\n");
154 #undef ADD
155
156         MIIBUS_MEDIAINIT(sc->mii_dev);
157
158         return(0);
159 }
160
161 static int
162 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
163 {
164         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
165         int reg;
166
167         switch (cmd) {
168         case MII_POLLSTAT:
169                 /*
170                  * If we're not polling our PHY instance, just return.
171                  */
172                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
173                         return (0);
174                 break;
175
176         case MII_MEDIACHG:
177                 /*
178                  * If the media indicates a different PHY instance,
179                  * isolate ourselves.
180                  */
181                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
182                         reg = PHY_READ(sc, MII_BMCR);
183                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
184                         return (0);
185                 }
186
187                 /*
188                  * If the interface is not up, don't do anything.
189                  */
190                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
191                         break;
192
193                 switch (IFM_SUBTYPE(ife->ifm_media)) {
194                 case IFM_AUTO:
195                         /*
196                          * If we're already in auto mode, just return.
197                          */
198                         if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
199                                 return (0);
200                         (void) mii_phy_auto(sc);
201                         break;
202                 case IFM_100_T4:
203                         /*
204                          * Not supported on MII
205                          */
206                         return (EINVAL);
207                 default:
208                         /*
209                          * BMCR data is stored in the ifmedia entry.
210                          */
211                         PHY_WRITE(sc, MII_ANAR,
212                             mii_anar(ife->ifm_media));
213                         PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
214                 }
215                 break;
216
217         case MII_TICK:
218                 /*
219                  * If we're not currently selected, just return.
220                  */
221                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
222                         return (0);
223                 if (mii_phy_tick(sc) == EJUSTRETURN)
224                         return (0);
225                 break;
226         }
227
228         /* Update the media status. */
229         tdkphy_status(sc);
230         if (sc->mii_pdata->mii_media_active & IFM_FDX)
231                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
232         else
233                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
234
235         /* Callback if something changed. */
236         mii_phy_update(sc, cmd);
237         return (0);
238 }
239
240 static void
241 tdkphy_status(struct mii_softc *phy)
242 {
243         struct mii_data *mii = phy->mii_pdata;
244         int bmsr, bmcr, anlpar, diag;
245
246         mii->mii_media_status = IFM_AVALID;
247         mii->mii_media_active = IFM_ETHER;
248
249         bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
250         if (bmsr & BMSR_LINK)
251                 mii->mii_media_status |= IFM_ACTIVE;
252
253         bmcr = PHY_READ(phy, MII_BMCR);
254         if (bmcr & BMCR_ISO) {
255                 mii->mii_media_active |= IFM_NONE;
256                 mii->mii_media_status = 0;
257                 return;
258         }
259
260         if (bmcr & BMCR_LOOP)
261                 mii->mii_media_active |= IFM_LOOP;
262
263         if (bmcr & BMCR_AUTOEN) {
264                 /*
265                  * NWay autonegotiation takes the highest-order common
266                  * bit of the ANAR and ANLPAR (i.e. best media advertised
267                  * both by us and our link partner).
268                  */
269                 if ((bmsr & BMSR_ACOMP) == 0) {
270                         /* Erg, still trying, I guess... */
271                         mii->mii_media_active |= IFM_NONE;
272                         return;
273                 }
274
275                 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
276                 /*
277                  * ANLPAR doesn't get set on my card, but we check it anyway,
278                  * since it is mentioned in the 78Q2120 specs.
279                  */
280                 if (anlpar & ANLPAR_T4)
281                         mii->mii_media_active |= IFM_100_T4;
282                 else if (anlpar & ANLPAR_TX_FD)
283                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
284                 else if (anlpar & ANLPAR_TX)
285                         mii->mii_media_active |= IFM_100_TX;
286                 else if (anlpar & ANLPAR_10_FD)
287                         mii->mii_media_active |= IFM_10_T|IFM_FDX;
288                 else if (anlpar & ANLPAR_10)
289                         mii->mii_media_active |= IFM_10_T;
290                 else {
291                         /*
292                          * ANLPAR isn't set, which leaves two possibilities:
293                          * 1) Auto negotiation failed
294                          * 2) Auto negotiation completed, but the card forgot
295                          *    to set ANLPAR.
296                          * So we check the MII_DIAG(18) register...
297                          */
298                         diag = PHY_READ(phy, MII_DIAG);
299                         if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
300                                 mii->mii_media_active |= IFM_10_T;
301                         else {
302                                 if (diag & DIAG_DUPLEX)
303                                         mii->mii_media_active |= IFM_FDX;
304                                 if (diag & DIAG_RATE_100)
305                                         mii->mii_media_active |= IFM_100_TX;
306                                 else
307                                         mii->mii_media_active |= IFM_10_T;
308                         }
309                 }
310         } else {
311                 mii->mii_media_active = mii_media_from_bmcr(bmcr);
312         }
313 }