]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mii/tdkphy.c
More cleanup in response queue and reset code.
[FreeBSD/FreeBSD.git] / sys / dev / mii / tdkphy.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000,2001 Jonathan Chen.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35  * Driver for the TDK 78Q2120 MII
36  *
37  * References:
38  *   Datasheet for the 78Q2120 - http://www.tsc.tdk.com/lan/78q2120.pdf
39  *   Most of this code stolen from ukphy.c
40  */
41
42 /*
43  * The TDK 78Q2120 is found on some Xircom X3201 based CardBus cards,
44  * also spotted on some 3C575 cards.  It's just like any other normal
45  * phy, except it does auto negotiation in a different way.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
52 #include <sys/errno.h>
53 #include <sys/module.h>
54 #include <sys/bus.h>
55
56 #include <net/if.h>
57 #include <net/if_media.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         DEVMETHOD_END
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(xxTSC, 78Q2120),
94         MII_PHY_END
95 };
96
97 static const struct mii_phy_funcs tdkphy_funcs = {
98         tdkphy_service,
99         tdkphy_status,
100         mii_phy_reset
101 };
102
103 static int
104 tdkphy_probe(device_t dev)
105 {
106
107         return (mii_phy_dev_probe(dev, tdkphys, BUS_PROBE_DEFAULT));
108 }
109
110 static int
111 tdkphy_attach(device_t dev)
112 {
113
114         mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &tdkphy_funcs, 1);
115         return (0);
116 }
117
118 static int
119 tdkphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
120 {
121
122         switch (cmd) {
123         case MII_POLLSTAT:
124                 break;
125
126         case MII_MEDIACHG:
127                 mii_phy_setmedia(sc);
128                 break;
129
130         case MII_TICK:
131                 if (mii_phy_tick(sc) == EJUSTRETURN)
132                         return (0);
133                 break;
134         }
135
136         /* Update the media status. */
137         PHY_STATUS(sc);
138         if (sc->mii_pdata->mii_media_active & IFM_FDX)
139                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) | BMCR_FDX);
140         else
141                 PHY_WRITE(sc, MII_BMCR, PHY_READ(sc, MII_BMCR) & ~BMCR_FDX);
142
143         /* Callback if something changed. */
144         mii_phy_update(sc, cmd);
145         return (0);
146 }
147
148 static void
149 tdkphy_status(struct mii_softc *phy)
150 {
151         struct mii_data *mii = phy->mii_pdata;
152         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
153         int bmsr, bmcr, anlpar, diag;
154
155         mii->mii_media_status = IFM_AVALID;
156         mii->mii_media_active = IFM_ETHER;
157
158         bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
159         if (bmsr & BMSR_LINK)
160                 mii->mii_media_status |= IFM_ACTIVE;
161
162         bmcr = PHY_READ(phy, MII_BMCR);
163         if (bmcr & BMCR_ISO) {
164                 mii->mii_media_active |= IFM_NONE;
165                 mii->mii_media_status = 0;
166                 return;
167         }
168
169         if (bmcr & BMCR_LOOP)
170                 mii->mii_media_active |= IFM_LOOP;
171
172         if (bmcr & BMCR_AUTOEN) {
173                 /*
174                  * NWay autonegotiation takes the highest-order common
175                  * bit of the ANAR and ANLPAR (i.e. best media advertised
176                  * both by us and our link partner).
177                  */
178                 if ((bmsr & BMSR_ACOMP) == 0) {
179                         /* Erg, still trying, I guess... */
180                         mii->mii_media_active |= IFM_NONE;
181                         return;
182                 }
183
184                 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
185                 /*
186                  * ANLPAR doesn't get set on my card, but we check it anyway,
187                  * since it is mentioned in the 78Q2120 specs.
188                  */
189                 if (anlpar & ANLPAR_TX_FD)
190                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
191                 else if (anlpar & ANLPAR_T4)
192                         mii->mii_media_active |= IFM_100_T4|IFM_HDX;
193                 else if (anlpar & ANLPAR_TX)
194                         mii->mii_media_active |= IFM_100_TX|IFM_HDX;
195                 else if (anlpar & ANLPAR_10_FD)
196                         mii->mii_media_active |= IFM_10_T|IFM_FDX;
197                 else if (anlpar & ANLPAR_10)
198                         mii->mii_media_active |= IFM_10_T|IFM_HDX;
199                 else {
200                         /*
201                          * ANLPAR isn't set, which leaves two possibilities:
202                          * 1) Auto negotiation failed
203                          * 2) Auto negotiation completed, but the card forgot
204                          *    to set ANLPAR.
205                          * So we check the MII_DIAG(18) register...
206                          */
207                         diag = PHY_READ(phy, MII_DIAG);
208                         if (diag & DIAG_NEGFAIL) /* assume 10baseT if no neg */
209                                 mii->mii_media_active |= IFM_10_T|IFM_HDX;
210                         else {
211                                 if (diag & DIAG_DUPLEX)
212                                         mii->mii_media_active |= IFM_FDX;
213                                 else
214                                         mii->mii_media_active |= IFM_HDX;
215                                 if (diag & DIAG_RATE_100)
216                                         mii->mii_media_active |= IFM_100_TX;
217                                 else
218                                         mii->mii_media_active |= IFM_10_T;
219                         }
220                 }
221                 if ((mii->mii_media_active & IFM_FDX) != 0)
222                         mii->mii_media_active |= mii_phy_flowstatus(phy);
223         } else
224                 mii->mii_media_active = ife->ifm_media;
225 }