]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/tdkphy.c
Merge ACPICA 20150619.
[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 <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59 #include "miidevs.h"
60
61 #include <dev/mii/tdkphyreg.h>
62
63 #include "miibus_if.h"
64
65 static int tdkphy_probe(device_t);
66 static int tdkphy_attach(device_t);
67
68 static device_method_t tdkphy_methods[] = {
69         /* device interface */
70         DEVMETHOD(device_probe,         tdkphy_probe),
71         DEVMETHOD(device_attach,        tdkphy_attach),
72         DEVMETHOD(device_detach,        mii_phy_detach),
73         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
74         DEVMETHOD_END
75 };
76
77 static devclass_t tdkphy_devclass;
78
79 static driver_t tdkphy_driver = {
80         "tdkphy",
81         tdkphy_methods,
82         sizeof(struct mii_softc)
83 };
84
85 DRIVER_MODULE(tdkphy, miibus, tdkphy_driver, tdkphy_devclass, 0, 0);
86
87 static int tdkphy_service(struct mii_softc *, struct mii_data *, int);
88 static void tdkphy_status(struct mii_softc *);
89
90 static const struct mii_phydesc tdkphys[] = {
91         MII_PHY_DESC(xxTSC, 78Q2120),
92         MII_PHY_END
93 };
94
95 static const struct mii_phy_funcs tdkphy_funcs = {
96         tdkphy_service,
97         tdkphy_status,
98         mii_phy_reset
99 };
100
101 static int
102 tdkphy_probe(device_t dev)
103 {
104
105         return (mii_phy_dev_probe(dev, tdkphys, BUS_PROBE_DEFAULT));
106 }
107
108 static int
109 tdkphy_attach(device_t dev)
110 {
111
112         mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &tdkphy_funcs, 1);
113         return (0);
114 }
115
116 static int
117 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
118 {
119
120         switch (cmd) {
121         case MII_POLLSTAT:
122                 break;
123
124         case MII_MEDIACHG:
125                 mii_phy_setmedia(sc);
126                 break;
127
128         case MII_TICK:
129                 if (mii_phy_tick(sc) == EJUSTRETURN)
130                         return (0);
131                 break;
132         }
133
134         /* Update the media status. */
135         PHY_STATUS(sc);
136         if (sc->mii_pdata->mii_media_active & IFM_FDX)
137                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
138         else
139                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
140
141         /* Callback if something changed. */
142         mii_phy_update(sc, cmd);
143         return (0);
144 }
145
146 static void
147 tdkphy_status(struct mii_softc *phy)
148 {
149         struct mii_data *mii = phy->mii_pdata;
150         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
151         int bmsr, bmcr, anlpar, diag;
152
153         mii->mii_media_status = IFM_AVALID;
154         mii->mii_media_active = IFM_ETHER;
155
156         bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
157         if (bmsr & BMSR_LINK)
158                 mii->mii_media_status |= IFM_ACTIVE;
159
160         bmcr = PHY_READ(phy, MII_BMCR);
161         if (bmcr & BMCR_ISO) {
162                 mii->mii_media_active |= IFM_NONE;
163                 mii->mii_media_status = 0;
164                 return;
165         }
166
167         if (bmcr & BMCR_LOOP)
168                 mii->mii_media_active |= IFM_LOOP;
169
170         if (bmcr & BMCR_AUTOEN) {
171                 /*
172                  * NWay autonegotiation takes the highest-order common
173                  * bit of the ANAR and ANLPAR (i.e. best media advertised
174                  * both by us and our link partner).
175                  */
176                 if ((bmsr & BMSR_ACOMP) == 0) {
177                         /* Erg, still trying, I guess... */
178                         mii->mii_media_active |= IFM_NONE;
179                         return;
180                 }
181
182                 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
183                 /*
184                  * ANLPAR doesn't get set on my card, but we check it anyway,
185                  * since it is mentioned in the 78Q2120 specs.
186                  */
187                 if (anlpar & ANLPAR_TX_FD)
188                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
189                 else if (anlpar & ANLPAR_T4)
190                         mii->mii_media_active |= IFM_100_T4|IFM_HDX;
191                 else if (anlpar & ANLPAR_TX)
192                         mii->mii_media_active |= IFM_100_TX|IFM_HDX;
193                 else if (anlpar & ANLPAR_10_FD)
194                         mii->mii_media_active |= IFM_10_T|IFM_FDX;
195                 else if (anlpar & ANLPAR_10)
196                         mii->mii_media_active |= IFM_10_T|IFM_HDX;
197                 else {
198                         /*
199                          * ANLPAR isn't set, which leaves two possibilities:
200                          * 1) Auto negotiation failed
201                          * 2) Auto negotiation completed, but the card forgot
202                          *    to set ANLPAR.
203                          * So we check the MII_DIAG(18) register...
204                          */
205                         diag = PHY_READ(phy, MII_DIAG);
206                         if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
207                                 mii->mii_media_active |= IFM_10_T|IFM_HDX;
208                         else {
209                                 if (diag & DIAG_DUPLEX)
210                                         mii->mii_media_active |= IFM_FDX;
211                                 else
212                                         mii->mii_media_active |= IFM_HDX;
213                                 if (diag & DIAG_RATE_100)
214                                         mii->mii_media_active |= IFM_100_TX;
215                                 else
216                                         mii->mii_media_active |= IFM_10_T;
217                         }
218                 }
219                 if ((mii->mii_media_active & IFM_FDX) != 0)
220                         mii->mii_media_active |= mii_phy_flowstatus(phy);
221         } else
222                 mii->mii_media_active = ife->ifm_media;
223 }