xref: /freebsd/sys/dev/mii/mii.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*	$NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
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.
10  *
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.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * MII bus layer, glues MII-capable network interface drivers to sharable
42  * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
43  * plus some NetBSD extensions.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 
53 #include <net/if.h>
54 #include <net/if_media.h>
55 
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58 
59 MODULE_VERSION(miibus, 1);
60 
61 #include "miibus_if.h"
62 
63 #if !defined(lint)
64 static const char rcsid[] =
65   "$FreeBSD$";
66 #endif
67 
68 static int miibus_readreg	__P((device_t, int, int));
69 static int miibus_writereg	__P((device_t, int, int, int));
70 static void miibus_statchg	__P((device_t));
71 static void miibus_mediainit	__P((device_t));
72 
73 static device_method_t miibus_methods[] = {
74 	/* device interface */
75 	DEVMETHOD(device_probe,		miibus_probe),
76 	DEVMETHOD(device_attach,	miibus_attach),
77 	DEVMETHOD(device_detach,	miibus_detach),
78 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
79 
80 	/* bus interface */
81 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
82 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
83 
84 	/* MII interface */
85 	DEVMETHOD(miibus_readreg,	miibus_readreg),
86 	DEVMETHOD(miibus_writereg,	miibus_writereg),
87 	DEVMETHOD(miibus_statchg,	miibus_statchg),
88 	DEVMETHOD(miibus_mediainit,	miibus_mediainit),
89 
90 	{ 0, 0 }
91 };
92 
93 devclass_t miibus_devclass;
94 
95 driver_t miibus_driver = {
96 	"miibus",
97 	miibus_methods,
98 	sizeof(struct mii_data)
99 };
100 
101 /*
102  * Helper function used by network interface drivers, attaches PHYs
103  * to the network interface driver parent.
104  */
105 
106 int miibus_probe(dev)
107 	device_t		dev;
108 {
109 	struct mii_attach_args	ma, *args;
110 	struct mii_data		*mii;
111 	device_t		child = NULL, parent;
112 	int			bmsr, capmask = 0xFFFFFFFF;
113 
114 	mii = device_get_softc(dev);
115 	parent = device_get_parent(dev);
116 	LIST_INIT(&mii->mii_phys);
117 
118 	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
119 		/*
120 		 * Check to see if there is a PHY at this address.  Note,
121 		 * many braindead PHYs report 0/0 in their ID registers,
122 		 * so we test for media in the BMSR.
123 	 	 */
124 		bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
125 		if (bmsr == 0 || bmsr == 0xffff ||
126 		    (bmsr & BMSR_MEDIAMASK) == 0) {
127 			/* Assume no PHY at this address. */
128 			continue;
129 		}
130 
131 		/*
132 		 * Extract the IDs. Braindead PHYs will be handled by
133 		 * the `ukphy' driver, as we have no ID information to
134 		 * match on.
135 	 	 */
136 		ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
137 		    MII_PHYIDR1);
138 		ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
139 		    MII_PHYIDR2);
140 
141 		ma.mii_data = mii;
142 		ma.mii_capmask = capmask;
143 
144 		args = malloc(sizeof(struct mii_attach_args),
145 		    M_DEVBUF, M_NOWAIT);
146 		bcopy((char *)&ma, (char *)args, sizeof(ma));
147 		child = device_add_child(dev, NULL, -1);
148 		device_set_ivars(child, args);
149 	}
150 
151 	if (child == NULL)
152 		return(ENXIO);
153 
154 	device_set_desc(dev, "MII bus");
155 
156 	return(0);
157 }
158 
159 int miibus_attach(dev)
160 	device_t		dev;
161 {
162 	void			**v;
163 	ifm_change_cb_t		ifmedia_upd;
164 	ifm_stat_cb_t		ifmedia_sts;
165 	struct mii_data		*mii;
166 
167 	mii = device_get_softc(dev);
168 	mii->mii_ifp = device_get_softc(device_get_parent(dev));
169 	v = device_get_ivars(dev);
170 	ifmedia_upd = v[0];
171 	ifmedia_sts = v[1];
172 	ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
173 	bus_generic_attach(dev);
174 
175 	return(0);
176 }
177 
178 int miibus_detach(dev)
179 	device_t		dev;
180 {
181 	struct mii_data		*mii;
182 
183 	bus_generic_detach(dev);
184 	mii = device_get_softc(dev);
185 	ifmedia_removeall(&mii->mii_media);
186 	mii->mii_ifp = NULL;
187 
188 	return(0);
189 }
190 
191 static int miibus_readreg(dev, phy, reg)
192 	device_t		dev;
193 	int			phy, reg;
194 {
195 	device_t		parent;
196 
197 	parent = device_get_parent(dev);
198 	return(MIIBUS_READREG(parent, phy, reg));
199 }
200 
201 static int miibus_writereg(dev, phy, reg, data)
202 	device_t		dev;
203 	int			phy, reg, data;
204 {
205 	device_t		parent;
206 
207 	parent = device_get_parent(dev);
208 	return(MIIBUS_WRITEREG(parent, phy, reg, data));
209 }
210 
211 static void miibus_statchg(dev)
212 	device_t		dev;
213 {
214 	device_t		parent;
215 
216 	parent = device_get_parent(dev);
217 	MIIBUS_STATCHG(parent);
218 	return;
219 }
220 
221 static void miibus_mediainit(dev)
222 	device_t		dev;
223 {
224 	struct mii_data		*mii;
225 	struct ifmedia_entry	*m;
226 	int			media = 0;
227 
228 	/* Poke the parent in case it has any media of its own to add. */
229 	MIIBUS_MEDIAINIT(device_get_parent(dev));
230 
231 	mii = device_get_softc(dev);
232 	for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL;
233 	     m = LIST_NEXT(m, ifm_list)) {
234 		media = m->ifm_media;
235 		if (media == (IFM_ETHER|IFM_AUTO))
236 			break;
237 	}
238 
239 	ifmedia_set(&mii->mii_media, media);
240 
241 	return;
242 }
243 
244 int mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts)
245 	device_t		dev;
246 	device_t		*child;
247 	ifm_change_cb_t		ifmedia_upd;
248 	ifm_stat_cb_t		ifmedia_sts;
249 {
250 	void			**v;
251 	int			bmsr, i;
252 
253 	v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT);
254 	v[0] = ifmedia_upd;
255 	v[1] = ifmedia_sts;
256 	*child = device_add_child(dev, "miibus", -1);
257 	device_set_ivars(*child, v);
258 
259 	for (i = 0; i < MII_NPHY; i++) {
260 		bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
261                 if (bmsr == 0 || bmsr == 0xffff ||
262                     (bmsr & BMSR_MEDIAMASK) == 0) {
263                         /* Assume no PHY at this address. */
264                         continue;
265                 } else
266 			break;
267 	}
268 
269 	if (i == MII_NPHY) {
270 		device_delete_child(dev, *child);
271 		*child = NULL;
272 		return(ENXIO);
273 	}
274 
275 	bus_generic_attach(dev);
276 
277 	return(0);
278 }
279 
280 /*
281  * Media changed; notify all PHYs.
282  */
283 int
284 mii_mediachg(mii)
285 	struct mii_data *mii;
286 {
287 	struct mii_softc *child;
288 	int rv;
289 
290 	mii->mii_media_status = 0;
291 	mii->mii_media_active = IFM_NONE;
292 
293 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
294 	     child = LIST_NEXT(child, mii_list)) {
295 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
296 		if (rv)
297 			return (rv);
298 	}
299 	return (0);
300 }
301 
302 /*
303  * Call the PHY tick routines, used during autonegotiation.
304  */
305 void
306 mii_tick(mii)
307 	struct mii_data *mii;
308 {
309 	struct mii_softc *child;
310 
311 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
312 	     child = LIST_NEXT(child, mii_list))
313 		(void) (*child->mii_service)(child, mii, MII_TICK);
314 }
315 
316 /*
317  * Get media status from PHYs.
318  */
319 void
320 mii_pollstat(mii)
321 	struct mii_data *mii;
322 {
323 	struct mii_softc *child;
324 
325 	mii->mii_media_status = 0;
326 	mii->mii_media_active = IFM_NONE;
327 
328 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
329 	     child = LIST_NEXT(child, mii_list))
330 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
331 }
332