Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / mii / gphyter.c
blobf84604e26295751c436ab055c1a2a97eee8a9b15
1 /* $NetBSD: gphyter.c,v 1.27 2009/10/19 18:41:13 bouyer Exp $ */
3 /*-
4 * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * driver for National Semiconductor's DP83891, DP83861 and DP83865
59 * `Gig PHYTER' ethernet 10/100/1000 PHYs. The DP83891 is an older,
60 * non-firmware-driven version of the DP83861. The DP83865 is a low
61 * power version of the DP83861.
63 * Data Sheets available from www.national.com:
64 * http://www.national.com/ds/DP/DP83861.pdf
65 * http://www.national.com/ds/DP/DP83865.pdf
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: gphyter.c,v 1.27 2009/10/19 18:41:13 bouyer Exp $");
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/device.h>
75 #include <sys/socket.h>
76 #include <sys/errno.h>
78 #include <net/if.h>
79 #include <net/if_media.h>
81 #include <dev/mii/mii.h>
82 #include <dev/mii/miivar.h>
83 #include <dev/mii/miidevs.h>
85 #include <dev/mii/gphyterreg.h>
87 static int gphytermatch(device_t, cfdata_t, void *);
88 static void gphyterattach(device_t, device_t, void *);
90 CFATTACH_DECL_NEW(gphyter, sizeof(struct mii_softc),
91 gphytermatch, gphyterattach, mii_phy_detach, mii_phy_activate);
93 static int gphyter_service(struct mii_softc *, struct mii_data *, int);
94 static void gphyter_status(struct mii_softc *);
95 static void gphyter_reset(struct mii_softc *);
97 static const struct mii_phy_funcs gphyter_funcs = {
98 gphyter_service, gphyter_status, gphyter_reset,
101 static const struct mii_phydesc gphyters[] = {
102 { MII_OUI_xxNATSEMI, MII_MODEL_xxNATSEMI_DP83861,
103 MII_STR_xxNATSEMI_DP83861 },
105 { MII_OUI_xxNATSEMI, MII_MODEL_xxNATSEMI_DP83865,
106 MII_STR_xxNATSEMI_DP83865 },
108 { MII_OUI_xxNATSEMI, MII_MODEL_xxNATSEMI_DP83891,
109 MII_STR_xxNATSEMI_DP83891 },
111 { 0, 0,
112 NULL },
115 static int
116 gphytermatch(device_t parent, cfdata_t match, void *aux)
118 struct mii_attach_args *ma = aux;
120 if (mii_phy_match(ma, gphyters) != NULL)
121 return (10);
123 return (0);
126 static void
127 gphyterattach(device_t parent, device_t self, void *aux)
129 struct mii_softc *sc = device_private(self);
130 struct mii_attach_args *ma = aux;
131 struct mii_data *mii = ma->mii_data;
132 const struct mii_phydesc *mpd;
133 int anar, strap;
135 mpd = mii_phy_match(ma, gphyters);
136 aprint_naive(": Media interface\n");
137 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
139 sc->mii_dev = self;
140 sc->mii_inst = mii->mii_instance;
141 sc->mii_phy = ma->mii_phyno;
142 sc->mii_funcs = &gphyter_funcs;
143 sc->mii_pdata = mii;
144 sc->mii_flags = ma->mii_flags;
145 sc->mii_anegticks = MII_ANEGTICKS;
147 PHY_RESET(sc);
149 sc->mii_capabilities =
150 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
151 if (sc->mii_capabilities & BMSR_EXTSTAT)
152 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
155 * The Gig PHYTER seems to have the 10baseT BMSR bits
156 * hard-wired to 0, even though the device supports
157 * 10baseT. What we do instead is read the post-reset
158 * ANAR, who's 10baseT-related bits are set by strapping
159 * pin 180, and fake the BMSR bits.
161 anar = PHY_READ(sc, MII_ANAR);
162 if (anar & ANAR_10)
163 sc->mii_capabilities |= (BMSR_10THDX & ma->mii_capmask);
164 if (anar & ANAR_10_FD)
165 sc->mii_capabilities |= (BMSR_10TFDX & ma->mii_capmask);
167 aprint_normal_dev(self, "");
168 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
169 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
170 aprint_error("no media present");
171 else
172 mii_phy_add_media(sc);
173 aprint_normal("\n");
175 strap = PHY_READ(sc, MII_GPHYTER_STRAP);
176 aprint_normal_dev(self, "strapped to %s mode",
177 (strap & STRAP_MS_VAL) ? "master" : "slave");
178 if (strap & STRAP_NC_MODE)
179 aprint_normal(", pre-C5 BCM5400 compat enabled");
180 aprint_normal("\n");
183 static int
184 gphyter_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
186 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
187 int reg;
189 switch (cmd) {
190 case MII_POLLSTAT:
192 * If we're not polling our PHY instance, just return.
194 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
195 return (0);
196 break;
198 case MII_MEDIACHG:
200 * If the media indicates a different PHY instance,
201 * isolate ourselves.
203 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
204 reg = PHY_READ(sc, MII_BMCR);
205 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
206 return (0);
210 * If the interface is not up, don't do anything.
212 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
213 break;
215 mii_phy_setmedia(sc);
216 break;
218 case MII_TICK:
220 * If we're not currently selected, just return.
222 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
223 return (0);
225 if (mii_phy_tick(sc) == EJUSTRETURN)
226 return (0);
227 break;
229 case MII_DOWN:
230 mii_phy_down(sc);
231 return (0);
234 /* Update the media status. */
235 mii_phy_status(sc);
237 /* Callback if something changed. */
238 mii_phy_update(sc, cmd);
239 return (0);
242 static void
243 gphyter_status(struct mii_softc *sc)
245 struct mii_data *mii = sc->mii_pdata;
246 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
247 int bmsr, bmcr, physup, gtsr;
249 mii->mii_media_status = IFM_AVALID;
250 mii->mii_media_active = IFM_ETHER;
252 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
254 physup = PHY_READ(sc, MII_GPHYTER_PHY_SUP);
256 if (physup & PHY_SUP_LINK)
257 mii->mii_media_status |= IFM_ACTIVE;
259 bmcr = PHY_READ(sc, MII_BMCR);
260 if (bmcr & BMCR_ISO) {
261 mii->mii_media_active |= IFM_NONE;
262 mii->mii_media_status = 0;
263 return;
266 if (bmcr & BMCR_LOOP)
267 mii->mii_media_active |= IFM_LOOP;
269 if (bmcr & BMCR_AUTOEN) {
271 * The media status bits are only valid of autonegotiation
272 * has completed (or it's disabled).
274 if ((bmsr & BMSR_ACOMP) == 0) {
275 /* Erg, still trying, I guess... */
276 mii->mii_media_active |= IFM_NONE;
277 return;
280 switch (physup & (PHY_SUP_SPEED1|PHY_SUP_SPEED0)) {
281 case PHY_SUP_SPEED1:
282 mii->mii_media_active |= IFM_1000_T;
283 gtsr = PHY_READ(sc, MII_100T2SR);
284 if (gtsr & GTSR_MS_RES)
285 mii->mii_media_active |= IFM_ETH_MASTER;
286 break;
288 case PHY_SUP_SPEED0:
289 mii->mii_media_active |= IFM_100_TX;
290 break;
292 case 0:
293 mii->mii_media_active |= IFM_10_T;
294 break;
296 default:
297 mii->mii_media_active |= IFM_NONE;
298 mii->mii_media_status = 0;
300 if (physup & PHY_SUP_DUPLEX)
301 mii->mii_media_active |=
302 IFM_FDX | mii_phy_flowstatus(sc);
303 } else
304 mii->mii_media_active = ife->ifm_media;
307 void
308 gphyter_reset(struct mii_softc *sc)
310 int reg, i;
312 if (sc->mii_flags & MIIF_NOISOLATE)
313 reg = BMCR_RESET;
314 else
315 reg = BMCR_RESET | BMCR_ISO;
316 PHY_WRITE(sc, MII_BMCR, reg);
319 * It is best to allow a little time for the reset to settle
320 * in before we start polling the BMCR again. Notably, the
321 * DP83840A manual states that there should be a 500us delay
322 * between asserting software reset and attempting MII serial
323 * operations. Also, a DP83815 can get into a bad state on
324 * cable removal and reinsertion if we do not delay here.
326 delay(500);
328 /* Wait another 100ms for it to complete. */
329 for (i = 0; i < 100; i++) {
330 reg = PHY_READ(sc, MII_BMCR);
331 if ((reg & BMCR_RESET) == 0)
332 break;
333 delay(1000);
336 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
337 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);