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