Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / dev / netif / mii_layer / nsgphy.c
1 /*
2  * Copyright (c) 2001 Wind River Systems
3  * Copyright (c) 2001
4  *      Bill Paul <wpaul@bsdi.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/mii/nsgphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $
34  * $DragonFly: src/sys/dev/netif/mii_layer/nsgphy.c,v 1.14 2006/12/22 23:26:20 swildner Exp $
35  */
36
37 /*
38  * Driver for the National Semiconductor DP83891 and DP83861
39  * 10/100/1000 PHYs.
40  * Datasheet available at: http://www.national.com/ds/DP/DP83861.pdf
41  *
42  * The DP83891 is the older NatSemi gigE PHY which isn't being sold
43  * anymore. The DP83861 is its replacement, which is an 'enhanced'
44  * firmware driven component. The major difference between the
45  * two is that the 83891 can't generate interrupts, while the
46  * 83861 can. (I think it wasn't originally designed to do this, but
47  * it can now thanks to firmware updates.) The 83861 also allows
48  * access to its internal RAM via indirect register access.
49  */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/bus.h>
56
57 #include <machine/clock.h>
58
59 #include <net/if.h>
60 #include <net/if_media.h>
61
62 #include "mii.h"
63 #include "miivar.h"
64 #include "miidevs.h"
65
66 #include "nsgphyreg.h"
67
68 #include "miibus_if.h"
69
70 static int nsgphy_probe         (device_t);
71 static int nsgphy_attach        (device_t);
72
73 static device_method_t nsgphy_methods[] = {
74         /* device interface */
75         DEVMETHOD(device_probe,         nsgphy_probe),
76         DEVMETHOD(device_attach,        nsgphy_attach),
77         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
78         { 0, 0 }
79 };
80
81 static const struct mii_phydesc nsgphys[] = {
82         MII_PHYDESC(NATSEMI,    DP83891),
83         MII_PHYDESC(NATSEMI,    DP83861),
84         MII_PHYDESC_NULL
85 };
86
87 static devclass_t nsgphy_devclass;
88
89 static driver_t nsgphy_driver = {
90         "nsgphy",
91         nsgphy_methods,
92         sizeof(struct mii_softc)
93 };
94
95 DRIVER_MODULE(nsgphy, miibus, nsgphy_driver, nsgphy_devclass, 0, 0);
96
97 static int      nsgphy_service(struct mii_softc *, struct mii_data *, int);
98 static void     nsgphy_status(struct mii_softc *);
99
100 static int
101 nsgphy_probe(device_t dev)
102 {
103         struct mii_attach_args *ma = device_get_ivars(dev);
104         const struct mii_phydesc *mpd;
105
106         mpd = mii_phy_match(ma, nsgphys);
107         if (mpd != NULL) {
108                 device_set_desc(dev, mpd->mpd_name);
109                 return (0);
110         }
111         return(ENXIO);
112 }
113
114 static int
115 nsgphy_attach(device_t dev)
116 {
117         struct mii_softc *sc;
118         struct mii_attach_args *ma;
119         struct mii_data *mii;
120
121         sc = device_get_softc(dev);
122         ma = device_get_ivars(dev);
123         mii_softc_init(sc, ma);
124         sc->mii_dev = device_get_parent(dev);
125         mii = device_get_softc(sc->mii_dev);
126         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
127
128         sc->mii_inst = mii->mii_instance;
129         sc->mii_service = nsgphy_service;
130         /*
131          * Only retry autonegotiation every 17 seconds.
132          * Actually, for gigE PHYs, we should wait longer, since
133          * 5 seconds is the mimimum time the documentation
134          * says to wait for a 1000mbps link to be established.
135          */
136         sc->mii_anegticks = 17;
137         sc->mii_pdata = mii;
138
139         sc->mii_flags |= MIIF_NOISOLATE;
140         mii->mii_instance++;
141
142 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
143
144         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
145             MII_MEDIA_NONE);
146 #if 0
147         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
148             MII_MEDIA_100_TX);
149 #endif
150
151         mii_phy_reset(sc);
152
153         sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
154         if (sc->mii_capabilities & BMSR_EXTSTAT)
155                 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
156
157         device_printf(dev, " ");
158         if ((sc->mii_capabilities & BMSR_MEDIAMASK) ||
159             (sc->mii_extcapabilities & EXTSR_MEDIAMASK))
160                 mii_phy_add_media(sc);
161         else
162                 kprintf("no media present");
163
164         kprintf("\n");
165 #undef ADD
166
167         MIIBUS_MEDIAINIT(sc->mii_dev);
168         return(0);
169 }
170
171 int
172 nsgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
173 {
174         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
175         int reg;
176
177         switch (cmd) {
178         case MII_POLLSTAT:
179                 /*
180                  * If we're not polling our PHY instance, just return.
181                  */
182                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
183                         return (0);
184                 break;
185
186         case MII_MEDIACHG:
187                 /*
188                  * If the media indicates a different PHY instance,
189                  * isolate ourselves.
190                  */
191                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
192                         reg = PHY_READ(sc, MII_BMCR);
193                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
194                         return (0);
195                 }
196
197                 /*
198                  * If the interface is not up, don't do anything.
199                  */
200                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
201                         break;
202
203
204                 mii_phy_set_media(sc);
205                 break;
206
207         case MII_TICK:
208                 /*
209                  * If we're not currently selected, just return.
210                  */
211                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
212                         return (0);
213
214                 if (mii_phy_tick(sc) == EJUSTRETURN)
215                         return (0);
216                 break;
217         }
218
219         /* Update the media status. */
220         nsgphy_status(sc);
221
222         /* Callback if something changed. */
223         mii_phy_update(sc, cmd);
224         return (0);
225 }
226
227 static void
228 nsgphy_status(struct mii_softc *sc)
229 {
230         struct mii_data *mii = sc->mii_pdata;
231         int bmsr, bmcr, physup, anlpar, gstat;
232
233         mii->mii_media_status = IFM_AVALID;
234         mii->mii_media_active = IFM_ETHER;
235
236         bmsr = PHY_READ(sc, NSGPHY_MII_BMSR);
237         physup = PHY_READ(sc, NSGPHY_MII_PHYSUP);
238         if (physup & NSGPHY_PHYSUP_LNKSTS)
239                 mii->mii_media_status |= IFM_ACTIVE;
240
241         bmcr = PHY_READ(sc, NSGPHY_MII_BMCR);
242
243         if (bmcr & NSGPHY_BMCR_LOOP)
244                 mii->mii_media_active |= IFM_LOOP;
245
246         if (bmcr & NSGPHY_BMCR_AUTOEN) {
247                 if ((bmsr & NSGPHY_BMSR_ACOMP) == 0) {
248                         /* Erg, still trying, I guess... */
249                         mii->mii_media_active |= IFM_NONE;
250                         return;
251                 }
252                 anlpar = PHY_READ(sc, NSGPHY_MII_ANLPAR);
253                 gstat = PHY_READ(sc, NSGPHY_MII_1000STS);
254                 if (gstat & NSGPHY_1000STS_LPFD)
255                         mii->mii_media_active |= IFM_1000_T | IFM_FDX;
256                 else if (gstat & NSGPHY_1000STS_LPHD)
257                         mii->mii_media_active |= IFM_1000_T | IFM_HDX;
258                 else if (anlpar & NSGPHY_ANLPAR_100T4)
259                         mii->mii_media_active |= IFM_100_T4;
260                 else if (anlpar & NSGPHY_ANLPAR_100FDX)
261                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
262                 else if (anlpar & NSGPHY_ANLPAR_100HDX)
263                         mii->mii_media_active |= IFM_100_TX;
264                 else if (anlpar & NSGPHY_ANLPAR_10FDX)
265                         mii->mii_media_active |= IFM_10_T|IFM_FDX;
266                 else if (anlpar & NSGPHY_ANLPAR_10HDX)
267                         mii->mii_media_active |= IFM_10_T|IFM_HDX;
268                 else
269                         mii->mii_media_active |= IFM_NONE;
270                 return;
271         }
272
273         switch(bmcr & (NSGPHY_BMCR_SPD1|NSGPHY_BMCR_SPD0)) {
274         case NSGPHY_S1000:
275                 mii->mii_media_active |= IFM_1000_T;
276                 break;
277         case NSGPHY_S100:
278                 mii->mii_media_active |= IFM_100_TX;
279                 break;
280         case NSGPHY_S10:
281                 mii->mii_media_active |= IFM_10_T;
282                 break;
283         default:
284                 break;
285         }
286
287         if (bmcr & NSGPHY_BMCR_FDX)
288                 mii->mii_media_active |= IFM_FDX;
289         else
290                 mii->mii_media_active |= IFM_HDX;
291 }