Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / mii_layer / inphy.c
1 /*-
2  * Copyright (c) 2001 Jonathan Lemon
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  * 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. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      $FreeBSD: src/sys/dev/mii/inphy.c,v 1.4.2.2 2002/11/08 21:53:49 semenu Exp $
30  */
31
32 /*
33  * driver for Intel 82553 and 82555 PHYs
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/bus.h>
41
42 #include <net/if.h>
43 #include <net/if_media.h>
44
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47 #include <dev/mii/miidevs.h>
48
49 #include <dev/mii/inphyreg.h>
50
51 #include "miibus_if.h"
52
53 static int      inphy_probe(device_t dev);
54 static int      inphy_attach(device_t dev);
55 static int      inphy_detach(device_t dev);
56
57 static device_method_t inphy_methods[] = {
58         /* device interface */
59         DEVMETHOD(device_probe,         inphy_probe),
60         DEVMETHOD(device_attach,        inphy_attach),
61         DEVMETHOD(device_detach,        inphy_detach),
62         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
63         { 0, 0 }
64 };
65
66 static devclass_t inphy_devclass;
67
68 static driver_t inphy_driver = {
69         "inphy",
70         inphy_methods,
71         sizeof(struct mii_softc)
72 };
73
74 DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, 0, 0);
75
76 int     inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd);
77 void    inphy_status(struct mii_softc *sc);
78
79
80 static int
81 inphy_probe(device_t dev)
82 {
83         struct mii_attach_args *ma;
84         device_t parent;
85
86         ma = device_get_ivars(dev);
87         parent = device_get_parent(device_get_parent(dev));
88
89         /* Intel 82553 A/B steppings */
90         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxINTEL &&
91             MII_MODEL(ma->mii_id2) == MII_MODEL_xxINTEL_I82553AB) {
92                 device_set_desc(dev, MII_STR_xxINTEL_I82553AB);
93                 return (0);
94         }
95
96         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_INTEL) {
97                 switch (MII_MODEL(ma->mii_id2)) {
98                 case MII_MODEL_INTEL_I82555:
99                         device_set_desc(dev, MII_STR_INTEL_I82555);
100                         return (0);
101                 case MII_MODEL_INTEL_I82553C:
102                         device_set_desc(dev, MII_STR_INTEL_I82553C);
103                         return (0);
104                 case MII_MODEL_INTEL_I82562EM:
105                         device_set_desc(dev, MII_STR_INTEL_I82562EM);
106                         return (0);
107                 case MII_MODEL_INTEL_I82562ET:
108                         device_set_desc(dev, MII_STR_INTEL_I82562ET);
109                         return (0);
110                 }
111         }
112
113         return (ENXIO);
114 }
115
116 static int
117 inphy_attach(device_t dev)
118 {
119         struct mii_softc *sc;
120         struct mii_attach_args *ma;
121         struct mii_data *mii;
122
123         sc = device_get_softc(dev);
124         ma = device_get_ivars(dev);
125         sc->mii_dev = device_get_parent(dev);
126         mii = device_get_softc(sc->mii_dev);
127         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
128
129         sc->mii_inst = mii->mii_instance;
130         sc->mii_phy = ma->mii_phyno;
131         sc->mii_service = inphy_service;
132         sc->mii_pdata = mii;
133         mii->mii_instance++;
134
135 #if 0
136         sc->mii_flags |= MIIF_NOISOLATE;
137 #endif
138
139         ifmedia_add(&mii->mii_media,
140             IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
141             BMCR_LOOP|BMCR_S100, NULL);
142
143         mii_phy_reset(sc);
144
145         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
146         device_printf(dev, " ");
147         if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
148                 printf("no media present");
149         else
150                 mii_add_media(mii, sc->mii_capabilities, sc->mii_inst);
151         printf("\n");
152
153         MIIBUS_MEDIAINIT(sc->mii_dev);
154
155         return (0);
156 }
157
158 static int
159 inphy_detach(device_t dev)
160 {
161         struct mii_softc *sc;
162         struct mii_data *mii;
163
164         sc = device_get_softc(dev);
165         mii = device_get_softc(device_get_softc(dev));
166         mii_phy_auto_stop(sc);
167         sc->mii_dev = NULL;
168         LIST_REMOVE(sc, mii_list);
169
170         return (0);
171 }
172
173 int
174 inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
175 {
176         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
177         int reg;
178
179         switch (cmd) {
180         case MII_POLLSTAT:
181                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
182                         return (0);
183                 break;
184
185         case MII_MEDIACHG:
186                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
187                         reg = PHY_READ(sc, MII_BMCR);
188                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
189                         return (0);
190                 }
191
192                 /*
193                  * If the interface is not up, don't do anything.
194                  */
195                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
196                         break;
197
198                 switch (IFM_SUBTYPE(ife->ifm_media)) {
199                 case IFM_AUTO:
200                         /*
201                          * If we're already in auto mode, just return.
202                          */
203                         if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
204                                 return (0);
205                         (void) mii_phy_auto(sc, 0);
206                         break;
207                 case IFM_100_T4:
208                         /*
209                          * XXX Not supported as a manual setting right now.
210                          */
211                         return (EINVAL);
212                 default:
213                         /*
214                          * BMCR data is stored in the ifmedia entry.
215                          */
216                         PHY_WRITE(sc, MII_ANAR, mii_anar(ife->ifm_media));
217                         PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
218                 }
219                 break;
220
221         case MII_TICK:
222                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
223                         return (0);
224
225                 /*
226                  * Is the interface even up?
227                  */
228                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
229                         return (0);
230
231                 /*
232                  * Only used for autonegotiation.
233                  */
234                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
235                         return (0);
236
237                 /*
238                  * check for link.
239                  * Read the status register twice; BMSR_LINK is latch-low.
240                  */
241                 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
242                 if (reg & BMSR_LINK)
243                         return (0);
244
245                 /*
246                  * Only retry autonegotiation every 5 seconds.
247                  */
248                 if (++sc->mii_ticks != 5)
249                         return (0);
250
251                 sc->mii_ticks = 0;
252                 mii_phy_reset(sc);
253                 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
254                         return (0);
255                 break;
256         }
257
258         /* Update the media status. */
259         inphy_status(sc);
260
261         /* Callback if something changed. */
262         if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
263                 MIIBUS_STATCHG(sc->mii_dev);
264                 sc->mii_active = mii->mii_media_active;
265         }
266         return (0);
267 }
268
269 void
270 inphy_status(struct mii_softc *sc)
271 {
272         struct mii_data *mii = sc->mii_pdata;
273         int bmsr, bmcr, scr;
274
275         mii->mii_media_status = IFM_AVALID;
276         mii->mii_media_active = IFM_ETHER;
277
278         bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
279         if (bmsr & BMSR_LINK)
280                 mii->mii_media_status |= IFM_ACTIVE;
281
282         bmcr = PHY_READ(sc, MII_BMCR);
283         if (bmcr & BMCR_ISO) {
284                 mii->mii_media_active |= IFM_NONE;
285                 mii->mii_media_status = 0;
286                 return;
287         }
288
289         if (bmcr & BMCR_LOOP)
290                 mii->mii_media_active |= IFM_LOOP;
291
292         if (bmcr & BMCR_AUTOEN) {
293                 if ((bmsr & BMSR_ACOMP) == 0) {
294                         mii->mii_media_active |= IFM_NONE;
295                         return;
296                 }
297
298                 scr = PHY_READ(sc, MII_INPHY_SCR);
299                 if (scr & SCR_S100)
300                         mii->mii_media_active |= IFM_100_TX;
301                 else
302                         mii->mii_media_active |= IFM_10_T;
303                 if (scr & SCR_FDX)
304                         mii->mii_media_active |= IFM_FDX;
305         } else
306                 mii->mii_media_active |= mii_media_from_bmcr(bmcr);
307 }