1 /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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
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.
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.
39 * $FreeBSD: src/sys/dev/mii/mii.c,v 1.6.2.2 2002/08/19 16:56:33 ambrisko Exp $
40 * $DragonFly: src/sys/dev/netif/mii_layer/mii.c,v 1.4 2003/08/27 09:38:31 rob Exp $
44 * MII bus layer, glues MII-capable network interface drivers to sharable
45 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
46 * plus some NetBSD extensions.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
57 #include <net/if_media.h>
62 #include "miibus_if.h"
64 static int miibus_readreg (device_t, int, int);
65 static int miibus_writereg (device_t, int, int, int);
66 static void miibus_statchg (device_t);
67 static void miibus_mediainit (device_t);
69 static device_method_t miibus_methods[] = {
70 /* device interface */
71 DEVMETHOD(device_probe, miibus_probe),
72 DEVMETHOD(device_attach, miibus_attach),
73 DEVMETHOD(device_detach, miibus_detach),
74 DEVMETHOD(device_shutdown, bus_generic_shutdown),
77 DEVMETHOD(bus_print_child, bus_generic_print_child),
78 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
81 DEVMETHOD(miibus_readreg, miibus_readreg),
82 DEVMETHOD(miibus_writereg, miibus_writereg),
83 DEVMETHOD(miibus_statchg, miibus_statchg),
84 DEVMETHOD(miibus_mediainit, miibus_mediainit),
89 devclass_t miibus_devclass;
91 driver_t miibus_driver = {
94 sizeof(struct mii_data)
98 * Helper function used by network interface drivers, attaches PHYs
99 * to the network interface driver parent.
102 int miibus_probe(dev)
105 struct mii_attach_args ma, *args;
106 struct mii_data *mii;
107 device_t child = NULL, parent;
108 int bmsr, capmask = 0xFFFFFFFF;
110 mii = device_get_softc(dev);
111 parent = device_get_parent(dev);
112 LIST_INIT(&mii->mii_phys);
114 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
116 * Check to see if there is a PHY at this address. Note,
117 * many braindead PHYs report 0/0 in their ID registers,
118 * so we test for media in the BMSR.
120 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
121 if (bmsr == 0 || bmsr == 0xffff ||
122 (bmsr & BMSR_MEDIAMASK) == 0) {
123 /* Assume no PHY at this address. */
128 * Extract the IDs. Braindead PHYs will be handled by
129 * the `ukphy' driver, as we have no ID information to
132 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
134 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
138 ma.mii_capmask = capmask;
140 args = malloc(sizeof(struct mii_attach_args),
142 bcopy((char *)&ma, (char *)args, sizeof(ma));
143 child = device_add_child(dev, NULL, -1);
144 device_set_ivars(child, args);
150 device_set_desc(dev, "MII bus");
155 int miibus_attach(dev)
159 ifm_change_cb_t ifmedia_upd;
160 ifm_stat_cb_t ifmedia_sts;
161 struct mii_data *mii;
163 mii = device_get_softc(dev);
164 mii->mii_ifp = device_get_softc(device_get_parent(dev));
165 v = device_get_ivars(dev);
168 ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
169 bus_generic_attach(dev);
174 int miibus_detach(dev)
177 struct mii_data *mii;
179 bus_generic_detach(dev);
180 mii = device_get_softc(dev);
181 ifmedia_removeall(&mii->mii_media);
187 static int miibus_readreg(dev, phy, reg)
193 parent = device_get_parent(dev);
194 return(MIIBUS_READREG(parent, phy, reg));
197 static int miibus_writereg(dev, phy, reg, data)
203 parent = device_get_parent(dev);
204 return(MIIBUS_WRITEREG(parent, phy, reg, data));
207 static void miibus_statchg(dev)
212 parent = device_get_parent(dev);
213 MIIBUS_STATCHG(parent);
217 static void miibus_mediainit(dev)
220 struct mii_data *mii;
221 struct ifmedia_entry *m;
224 /* Poke the parent in case it has any media of its own to add. */
225 MIIBUS_MEDIAINIT(device_get_parent(dev));
227 mii = device_get_softc(dev);
228 for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL;
229 m = LIST_NEXT(m, ifm_list)) {
230 media = m->ifm_media;
231 if (media == (IFM_ETHER|IFM_AUTO))
235 ifmedia_set(&mii->mii_media, media);
240 int mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts)
243 ifm_change_cb_t ifmedia_upd;
244 ifm_stat_cb_t ifmedia_sts;
249 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT);
252 *child = device_add_child(dev, "miibus", -1);
253 device_set_ivars(*child, v);
255 for (i = 0; i < MII_NPHY; i++) {
256 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
257 if (bmsr == 0 || bmsr == 0xffff ||
258 (bmsr & BMSR_MEDIAMASK) == 0) {
259 /* Assume no PHY at this address. */
266 device_delete_child(dev, *child);
271 bus_generic_attach(dev);
277 * Media changed; notify all PHYs.
281 struct mii_data *mii;
283 struct mii_softc *child;
286 mii->mii_media_status = 0;
287 mii->mii_media_active = IFM_NONE;
289 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
290 child = LIST_NEXT(child, mii_list)) {
291 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
299 * Call the PHY tick routines, used during autonegotiation.
303 struct mii_data *mii;
305 struct mii_softc *child;
307 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
308 child = LIST_NEXT(child, mii_list))
309 (void) (*child->mii_service)(child, mii, MII_TICK);
313 * Get media status from PHYs.
317 struct mii_data *mii;
319 struct mii_softc *child;
321 mii->mii_media_status = 0;
322 mii->mii_media_active = IFM_NONE;
324 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
325 child = LIST_NEXT(child, mii_list))
326 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);