xref: /dragonfly/sys/dev/netif/mii_layer/acphy.c (revision 9dbf638f)
1 /*-
2  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7  * NASA Ames Research Center.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the NetBSD
20  *	Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $FreeBSD: src/sys/dev/mii/acphy.c,v 1.2.2.2 2002/10/21 21:20:19 semenu Exp $
38  * $DragonFly: src/sys/dev/netif/mii_layer/acphy.c,v 1.4 2003/08/27 09:38:31 rob Exp $
39  */
40 
41 /*
42  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by Manuel Bouyer.
55  * 4. The name of the author may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  */
69 
70 /*
71  * Driver for Altima AC101 10/100 PHY
72  */
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/socket.h>
78 #include <sys/errno.h>
79 #include <sys/module.h>
80 #include <sys/bus.h>
81 
82 #include <net/if.h>
83 #include <net/if_media.h>
84 
85 #include "mii.h"
86 #include "miivar.h"
87 #include "miidevs.h"
88 
89 #include "acphyreg.h"
90 
91 #include "miibus_if.h"
92 
93 static int acphy_probe		(device_t);
94 static int acphy_attach		(device_t);
95 static int acphy_detach		(device_t);
96 
97 static device_method_t acphy_methods[] = {
98 	/* device interface */
99 	DEVMETHOD(device_probe,		acphy_probe),
100 	DEVMETHOD(device_attach,	acphy_attach),
101 	DEVMETHOD(device_detach,	acphy_detach),
102 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
103 	{ 0, 0 }
104 };
105 
106 static devclass_t acphy_devclass;
107 
108 static driver_t acphy_driver = {
109 	"acphy",
110 	acphy_methods,
111 	sizeof(struct mii_softc)
112 };
113 
114 DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
115 
116 int	acphy_service (struct mii_softc *, struct mii_data *, int);
117 void	acphy_reset (struct mii_softc *);
118 void	acphy_status (struct mii_softc *);
119 
120 static int acphy_probe(dev)
121 	device_t		dev;
122 {
123 	struct mii_attach_args *ma;
124 
125 	ma = device_get_ivars(dev);
126 
127 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
128 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101) {
129 		device_set_desc(dev, MII_STR_xxALTIMA_AC101);
130 	} else
131 		return (ENXIO);
132 
133 	return (0);
134 }
135 
136 static int acphy_attach(dev)
137 	device_t		dev;
138 {
139 	struct mii_softc *sc;
140 	struct mii_attach_args *ma;
141 	struct mii_data *mii;
142 
143 	sc = device_get_softc(dev);
144 	ma = device_get_ivars(dev);
145 	sc->mii_dev = device_get_parent(dev);
146 	mii = device_get_softc(sc->mii_dev);
147 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
148 
149 	sc->mii_inst = mii->mii_instance;
150 	sc->mii_phy = ma->mii_phyno;
151 	sc->mii_service = acphy_service;
152 	sc->mii_pdata = mii;
153 	sc->mii_flags |= MIIF_NOISOLATE;
154 
155 	acphy_reset(sc);
156 
157 	mii->mii_instance++;
158 
159 	sc->mii_capabilities =
160 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
161 	device_printf(dev, " ");
162 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
163 		mii_add_media(mii, sc->mii_capabilities,
164 		    sc->mii_inst);
165 	printf("\n");
166 
167 	MIIBUS_MEDIAINIT(sc->mii_dev);
168 	return (0);
169 }
170 
171 static int acphy_detach(dev)
172 	device_t		dev;
173 {
174 	struct mii_softc *sc;
175 	struct mii_data *mii;
176 
177 	sc = device_get_softc(dev);
178 	mii = device_get_softc(device_get_parent(dev));
179 	sc->mii_dev = NULL;
180 	LIST_REMOVE(sc, mii_list);
181 
182 	return(0);
183 }
184 
185 int
186 acphy_service(sc, mii, cmd)
187 	struct mii_softc *sc;
188 	struct mii_data *mii;
189 	int cmd;
190 {
191 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
192 	int reg;
193 
194 	/*
195 	 * If we're not selected, then do nothing, just isolate and power
196 	 * down, if changing media.
197 	 */
198 	if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
199 		if (cmd == MII_MEDIACHG) {
200 			reg = PHY_READ(sc, MII_BMCR);
201 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN);
202 		}
203 
204 		return (0);
205 	}
206 
207 	switch (cmd) {
208 	case MII_POLLSTAT:
209 		break;
210 
211 	case MII_MEDIACHG:
212 		/*
213 		 * If the interface is not up, don't do anything.
214 		 */
215 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
216 			break;
217 
218 		/* Wake & deisolate up if necessary */
219 		reg = PHY_READ(sc, MII_BMCR);
220 		if (reg & (BMCR_ISO | BMCR_PDOWN))
221 			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
222 
223 		switch (IFM_SUBTYPE(ife->ifm_media)) {
224 		case IFM_AUTO:
225 			/*
226 			 * If we're already in auto mode, just return.
227 			 */
228 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
229 				return (0);
230 
231 			(void) mii_phy_auto(sc, 1);
232 			break;
233 
234 		default:
235 			/*
236 			 * BMCR data is stored in the ifmedia entry.
237 			 */
238 			PHY_WRITE(sc, MII_ANAR,
239 			    mii_anar(ife->ifm_media));
240 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
241 		}
242 		break;
243 
244 	case MII_TICK:
245 		/*
246 		 * Is the interface even up?
247 		 */
248 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
249 			return (0);
250 
251 		/*
252 		 * Only used for autonegotiation.
253 		 */
254 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
255 			break;
256 
257 		/*
258 		 * This PHY's autonegotiation doesn't need to be kicked.
259 		 */
260 		break;
261 	}
262 
263 	/* Update the media status. */
264 	acphy_status(sc);
265 
266 	/* Callback if something changed. */
267 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
268 		MIIBUS_STATCHG(sc->mii_dev);
269 		sc->mii_active = mii->mii_media_active;
270 	}
271 	return (0);
272 }
273 
274 void
275 acphy_status(sc)
276 	struct mii_softc *sc;
277 {
278 	struct mii_data *mii = sc->mii_pdata;
279 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
280 	int bmsr, bmcr, diag;
281 
282 	mii->mii_media_status = IFM_AVALID;
283 	mii->mii_media_active = IFM_ETHER;
284 
285 	bmsr = PHY_READ(sc, MII_BMSR) |
286 	    PHY_READ(sc, MII_BMSR);
287 	if (bmsr & BMSR_LINK)
288 		mii->mii_media_status |= IFM_ACTIVE;
289 
290 	bmcr = PHY_READ(sc, MII_BMCR);
291 	if (bmcr & BMCR_ISO) {
292 		mii->mii_media_active |= IFM_NONE;
293 		mii->mii_media_status = 0;
294 		return;
295 	}
296 
297 	if (bmcr & BMCR_LOOP)
298 		mii->mii_media_active |= IFM_LOOP;
299 
300 	if (bmcr & BMCR_AUTOEN) {
301 		if ((bmsr & BMSR_ACOMP) == 0) {
302 			/* Erg, still trying, I guess... */
303 			mii->mii_media_active |= IFM_NONE;
304 			return;
305 		}
306 		diag = PHY_READ(sc, MII_ACPHY_DIAG);
307 		if (diag & AC_DIAG_SPEED)
308 			mii->mii_media_active |= IFM_100_TX;
309 		else
310 			mii->mii_media_active |= IFM_10_T;
311 
312 		if (diag & AC_DIAG_DUPLEX)
313 			mii->mii_media_active |= IFM_FDX;
314 	} else
315 		mii->mii_media_active = ife->ifm_media;
316 }
317 
318 void
319 acphy_reset(sc)
320 	struct mii_softc *sc;
321 {
322 
323 	mii_phy_reset(sc);
324 	PHY_WRITE(sc, MII_ACPHY_INT, 0);
325 }
326