214026ece46138e18865faab89538cf64acabdf9
[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.14 2004/05/30 17:57:40 phk 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 "mii.h"
46 #include "miivar.h"
47 #include "miidevs.h"
48
49 #include "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_service(struct mii_softc *, struct mii_data *, int);
56 static void     inphy_status(struct mii_softc *);
57
58 static device_method_t inphy_methods[] = {
59         /* device interface */
60         DEVMETHOD(device_probe,         inphy_probe),
61         DEVMETHOD(device_attach,        inphy_attach),
62         DEVMETHOD(device_detach,        ukphy_detach),
63         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
64         { 0, 0 }
65 };
66
67 static const struct mii_phydesc inphys[] = {
68         MII_PHYDESC(xxINTEL,    I82553AB),      /* Intel 82553 A/B steppings */
69         MII_PHYDESC(INTEL,      I82555),
70         MII_PHYDESC(INTEL,      I82553C),
71         MII_PHYDESC(INTEL,      I82562EM),
72         MII_PHYDESC(INTEL,      I82562ET),
73         MII_PHYDESC_NULL
74 };
75
76 static devclass_t inphy_devclass;
77
78 static driver_t inphy_driver = {
79         "inphy",
80         inphy_methods,
81         sizeof(struct mii_softc)
82 };
83
84 DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, NULL, NULL);
85
86 static int
87 inphy_probe(device_t dev)
88 {
89         struct mii_attach_args *ma = device_get_ivars(dev);
90         const struct mii_phydesc *mpd;
91
92         mpd = mii_phy_match(ma, inphys);
93         if (mpd != NULL) {
94                 device_set_desc(dev, mpd->mpd_name);
95                 return (0);
96         }
97         return (ENXIO);
98 }
99
100 static int
101 inphy_attach(device_t dev)
102 {
103         struct mii_softc *sc;
104         struct mii_attach_args *ma;
105         struct mii_data *mii;
106
107         sc = device_get_softc(dev);
108         ma = device_get_ivars(dev);
109         mii_softc_init(sc, ma);
110         sc->mii_dev = device_get_parent(dev);
111         mii = device_get_softc(sc->mii_dev);
112         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
113
114         sc->mii_inst = mii->mii_instance;
115         sc->mii_service = inphy_service;
116         sc->mii_pdata = mii;
117         mii->mii_instance++;
118
119 #if 0
120         sc->mii_flags |= MIIF_NOISOLATE;
121 #endif
122
123         ifmedia_add(&mii->mii_media,
124             IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
125             MII_MEDIA_100_TX, NULL);
126
127         mii_phy_reset(sc);
128
129         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
130
131         device_printf(dev, " ");
132         if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
133                 kprintf("no media present");
134         else
135                 mii_phy_add_media(sc);
136         kprintf("\n");
137
138         MIIBUS_MEDIAINIT(sc->mii_dev);
139         return (0);
140 }
141
142 static int
143 inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
144 {
145         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
146         int reg;
147
148         switch (cmd) {
149         case MII_POLLSTAT:
150                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
151                         return (0);
152                 break;
153
154         case MII_MEDIACHG:
155                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
156                         reg = PHY_READ(sc, MII_BMCR);
157                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
158                         return (0);
159                 }
160
161                 /*
162                  * If the interface is not up, don't do anything.
163                  */
164                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
165                         break;
166
167                 mii_phy_set_media(sc);
168                 break;
169
170         case MII_TICK:
171                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
172                         return (0);
173
174                 if (mii_phy_tick(sc) == EJUSTRETURN)
175                         return (0);
176                 break;
177         }
178
179         /* Update the media status. */
180         inphy_status(sc);
181
182         /* Callback if something changed. */
183         mii_phy_update(sc, cmd);
184         return (0);
185 }
186
187 static void
188 inphy_status(struct mii_softc *sc)
189 {
190         struct mii_data *mii = sc->mii_pdata;
191         int bmsr, bmcr, scr;
192
193         mii->mii_media_status = IFM_AVALID;
194         mii->mii_media_active = IFM_ETHER;
195
196         bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
197         if (bmsr & BMSR_LINK)
198                 mii->mii_media_status |= IFM_ACTIVE;
199
200         bmcr = PHY_READ(sc, MII_BMCR);
201         if (bmcr & BMCR_ISO) {
202                 mii->mii_media_active |= IFM_NONE;
203                 mii->mii_media_status = 0;
204                 return;
205         }
206
207         if (bmcr & BMCR_LOOP)
208                 mii->mii_media_active |= IFM_LOOP;
209
210         if (bmcr & BMCR_AUTOEN) {
211                 if ((bmsr & BMSR_ACOMP) == 0) {
212                         mii->mii_media_active |= IFM_NONE;
213                         return;
214                 }
215
216                 scr = PHY_READ(sc, MII_INPHY_SCR);
217                 if ((bmsr & BMSR_100T4) && (scr & SCR_T4))
218                         mii->mii_media_active |= IFM_100_T4;
219                 else if (scr & SCR_S100)
220                         mii->mii_media_active |= IFM_100_TX;
221                 else
222                         mii->mii_media_active |= IFM_10_T;
223                 if (scr & SCR_FDX)
224                         mii->mii_media_active |= IFM_FDX;
225         } else {
226                 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
227         }
228 }