xref: /freebsd/sys/dev/mii/acphy.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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/cdefs.h>
75 __FBSDID("$FreeBSD$");
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/socket.h>
81 #include <sys/errno.h>
82 #include <sys/module.h>
83 #include <sys/bus.h>
84 
85 #include <net/if.h>
86 #include <net/if_media.h>
87 
88 #include <dev/mii/mii.h>
89 #include <dev/mii/miivar.h>
90 #include "miidevs.h"
91 
92 #include <dev/mii/acphyreg.h>
93 
94 #include "miibus_if.h"
95 
96 static int acphy_probe(device_t);
97 static int acphy_attach(device_t);
98 
99 static device_method_t acphy_methods[] = {
100 	/* device interface */
101 	DEVMETHOD(device_probe,		acphy_probe),
102 	DEVMETHOD(device_attach,	acphy_attach),
103 	DEVMETHOD(device_detach,	mii_phy_detach),
104 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
105 	{ 0, 0 }
106 };
107 
108 static devclass_t acphy_devclass;
109 
110 static driver_t acphy_driver = {
111 	"acphy",
112 	acphy_methods,
113 	sizeof(struct mii_softc)
114 };
115 
116 DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
117 
118 static int	acphy_service(struct mii_softc *, struct mii_data *, int);
119 static void	acphy_reset(struct mii_softc *);
120 static void	acphy_status(struct mii_softc *);
121 
122 static int
123 acphy_probe(dev)
124 	device_t		dev;
125 {
126 	struct mii_attach_args *ma;
127 
128 	ma = device_get_ivars(dev);
129 
130 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
131 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101) {
132 		device_set_desc(dev, MII_STR_xxALTIMA_AC101);
133 	} else if(MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
134 	          MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101L) {
135 		device_set_desc(dev, MII_STR_xxALTIMA_AC101L);
136 	} else
137 		return (ENXIO);
138 
139 	return (0);
140 }
141 
142 static int
143 acphy_attach(dev)
144 	device_t		dev;
145 {
146 	struct mii_softc *sc;
147 	struct mii_attach_args *ma;
148 	struct mii_data *mii;
149 
150 	sc = device_get_softc(dev);
151 	ma = device_get_ivars(dev);
152 	sc->mii_dev = device_get_parent(dev);
153 	mii = device_get_softc(sc->mii_dev);
154 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
155 
156 	sc->mii_inst = mii->mii_instance;
157 	sc->mii_phy = ma->mii_phyno;
158 	sc->mii_service = acphy_service;
159 	sc->mii_pdata = mii;
160 	sc->mii_flags |= MIIF_NOISOLATE;
161 
162 	acphy_reset(sc);
163 
164 	mii->mii_instance++;
165 
166 	sc->mii_capabilities =
167 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
168 	device_printf(dev, " ");
169 	mii_add_media(sc);
170 	printf("\n");
171 
172 	MIIBUS_MEDIAINIT(sc->mii_dev);
173 	return (0);
174 }
175 
176 static int
177 acphy_service(sc, mii, cmd)
178 	struct mii_softc *sc;
179 	struct mii_data *mii;
180 	int cmd;
181 {
182 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
183 	int reg;
184 
185 	/*
186 	 * If we're not selected, then do nothing, just isolate and power
187 	 * down, if changing media.
188 	 */
189 	if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
190 		if (cmd == MII_MEDIACHG) {
191 			reg = PHY_READ(sc, MII_BMCR);
192 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN);
193 		}
194 
195 		return (0);
196 	}
197 
198 	switch (cmd) {
199 	case MII_POLLSTAT:
200 		break;
201 
202 	case MII_MEDIACHG:
203 		/*
204 		 * If the interface is not up, don't do anything.
205 		 */
206 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
207 			break;
208 
209 		/* Wake & deisolate up if necessary */
210 		reg = PHY_READ(sc, MII_BMCR);
211 		if (reg & (BMCR_ISO | BMCR_PDOWN))
212 			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
213 
214 		switch (IFM_SUBTYPE(ife->ifm_media)) {
215 		case IFM_AUTO:
216 			/*
217 			 * If we're already in auto mode, just return.
218 			 */
219 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
220 				return (0);
221 
222 			(void) mii_phy_auto(sc);
223 			break;
224 
225 		default:
226 			/*
227 			 * BMCR data is stored in the ifmedia entry.
228 			 */
229 			PHY_WRITE(sc, MII_ANAR,
230 			    mii_anar(ife->ifm_media));
231 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
232 		}
233 		break;
234 
235 	case MII_TICK:
236 		/*
237 		 * Is the interface even up?
238 		 */
239 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
240 			return (0);
241 
242 		/*
243 		 * Only used for autonegotiation.
244 		 */
245 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
246 			break;
247 
248 		/*
249 		 * This PHY's autonegotiation doesn't need to be kicked.
250 		 */
251 		break;
252 	}
253 
254 	/* Update the media status. */
255 	acphy_status(sc);
256 
257 	/* Callback if something changed. */
258 	mii_phy_update(sc, cmd);
259 	return (0);
260 }
261 
262 static void
263 acphy_status(sc)
264 	struct mii_softc *sc;
265 {
266 	struct mii_data *mii = sc->mii_pdata;
267 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
268 	int bmsr, bmcr, diag;
269 
270 	mii->mii_media_status = IFM_AVALID;
271 	mii->mii_media_active = IFM_ETHER;
272 
273 	bmsr = PHY_READ(sc, MII_BMSR) |
274 	    PHY_READ(sc, MII_BMSR);
275 	if (bmsr & BMSR_LINK)
276 		mii->mii_media_status |= IFM_ACTIVE;
277 
278 	bmcr = PHY_READ(sc, MII_BMCR);
279 	if (bmcr & BMCR_ISO) {
280 		mii->mii_media_active |= IFM_NONE;
281 		mii->mii_media_status = 0;
282 		return;
283 	}
284 
285 	if (bmcr & BMCR_LOOP)
286 		mii->mii_media_active |= IFM_LOOP;
287 
288 	if (bmcr & BMCR_AUTOEN) {
289 		if ((bmsr & BMSR_ACOMP) == 0) {
290 			/* Erg, still trying, I guess... */
291 			mii->mii_media_active |= IFM_NONE;
292 			return;
293 		}
294 		diag = PHY_READ(sc, MII_ACPHY_DIAG);
295 		if (diag & AC_DIAG_SPEED)
296 			mii->mii_media_active |= IFM_100_TX;
297 		else
298 			mii->mii_media_active |= IFM_10_T;
299 
300 		if (diag & AC_DIAG_DUPLEX)
301 			mii->mii_media_active |= IFM_FDX;
302 	} else
303 		mii->mii_media_active = ife->ifm_media;
304 }
305 
306 static void
307 acphy_reset(sc)
308 	struct mii_softc *sc;
309 {
310 
311 	mii_phy_reset(sc);
312 	PHY_WRITE(sc, MII_ACPHY_INT, 0);
313 }
314