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 #include "miibus_if.h" 60 61 #if !defined(lint) 62 static const char rcsid[] = 63 "$FreeBSD$"; 64 #endif 65 66 static int miibus_readreg __P((device_t, int, int)); 67 static int miibus_writereg __P((device_t, int, int, int)); 68 static void miibus_statchg __P((device_t)); 69 static void miibus_mediainit __P((device_t)); 70 71 static device_method_t miibus_methods[] = { 72 /* device interface */ 73 DEVMETHOD(device_probe, miibus_probe), 74 DEVMETHOD(device_attach, miibus_attach), 75 DEVMETHOD(device_detach, miibus_detach), 76 DEVMETHOD(device_shutdown, bus_generic_shutdown), 77 78 /* bus interface */ 79 DEVMETHOD(bus_print_child, bus_generic_print_child), 80 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 81 82 /* MII interface */ 83 DEVMETHOD(miibus_readreg, miibus_readreg), 84 DEVMETHOD(miibus_writereg, miibus_writereg), 85 DEVMETHOD(miibus_statchg, miibus_statchg), 86 DEVMETHOD(miibus_mediainit, miibus_mediainit), 87 88 { 0, 0 } 89 }; 90 91 devclass_t miibus_devclass; 92 93 driver_t miibus_driver = { 94 "miibus", 95 miibus_methods, 96 sizeof(struct mii_data) 97 }; 98 99 /* 100 * Helper function used by network interface drivers, attaches PHYs 101 * to the network interface driver parent. 102 */ 103 104 int miibus_probe(dev) 105 device_t dev; 106 { 107 struct mii_attach_args ma, *args; 108 struct mii_data *mii; 109 device_t child = NULL, parent; 110 int bmsr, capmask = 0xFFFFFFFF; 111 112 mii = device_get_softc(dev); 113 parent = device_get_parent(dev); 114 LIST_INIT(&mii->mii_phys); 115 116 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) { 117 /* 118 * Check to see if there is a PHY at this address. Note, 119 * many braindead PHYs report 0/0 in their ID registers, 120 * so we test for media in the BMSR. 121 */ 122 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR); 123 if (bmsr == 0 || bmsr == 0xffff || 124 (bmsr & BMSR_MEDIAMASK) == 0) { 125 /* Assume no PHY at this address. */ 126 continue; 127 } 128 129 /* 130 * Extract the IDs. Braindead PHYs will be handled by 131 * the `ukphy' driver, as we have no ID information to 132 * match on. 133 */ 134 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno, 135 MII_PHYIDR1); 136 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno, 137 MII_PHYIDR2); 138 139 ma.mii_data = mii; 140 ma.mii_capmask = capmask; 141 142 args = malloc(sizeof(struct mii_attach_args), 143 M_DEVBUF, M_NOWAIT); 144 bcopy((char *)&ma, (char *)args, sizeof(ma)); 145 child = device_add_child(dev, NULL, -1); 146 device_set_ivars(child, args); 147 } 148 149 if (child == NULL) 150 return(ENXIO); 151 152 device_set_desc(dev, "MII bus"); 153 154 return(0); 155 } 156 157 int miibus_attach(dev) 158 device_t dev; 159 { 160 void **v; 161 ifm_change_cb_t ifmedia_upd; 162 ifm_stat_cb_t ifmedia_sts; 163 struct mii_data *mii; 164 165 mii = device_get_softc(dev); 166 mii->mii_ifp = device_get_softc(device_get_parent(dev)); 167 v = device_get_ivars(dev); 168 ifmedia_upd = v[0]; 169 ifmedia_sts = v[1]; 170 ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts); 171 bus_generic_attach(dev); 172 173 return(0); 174 } 175 176 int miibus_detach(dev) 177 device_t dev; 178 { 179 struct mii_data *mii; 180 181 bus_generic_detach(dev); 182 mii = device_get_softc(dev); 183 ifmedia_removeall(&mii->mii_media); 184 mii->mii_ifp = NULL; 185 186 return(0); 187 } 188 189 static int miibus_readreg(dev, phy, reg) 190 device_t dev; 191 int phy, reg; 192 { 193 device_t parent; 194 195 parent = device_get_parent(dev); 196 return(MIIBUS_READREG(parent, phy, reg)); 197 } 198 199 static int miibus_writereg(dev, phy, reg, data) 200 device_t dev; 201 int phy, reg, data; 202 { 203 device_t parent; 204 205 parent = device_get_parent(dev); 206 return(MIIBUS_WRITEREG(parent, phy, reg, data)); 207 } 208 209 static void miibus_statchg(dev) 210 device_t dev; 211 { 212 device_t parent; 213 214 parent = device_get_parent(dev); 215 MIIBUS_STATCHG(parent); 216 return; 217 } 218 219 static void miibus_mediainit(dev) 220 device_t dev; 221 { 222 struct mii_data *mii; 223 struct ifmedia_entry *m; 224 int media = 0; 225 226 /* Poke the parent in case it has any media of its own to add. */ 227 MIIBUS_MEDIAINIT(device_get_parent(dev)); 228 229 mii = device_get_softc(dev); 230 for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL; 231 m = LIST_NEXT(m, ifm_list)) { 232 media = m->ifm_media; 233 if (media == (IFM_ETHER|IFM_AUTO)) 234 break; 235 } 236 237 ifmedia_set(&mii->mii_media, media); 238 239 return; 240 } 241 242 int mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts) 243 device_t dev; 244 device_t *child; 245 ifm_change_cb_t ifmedia_upd; 246 ifm_stat_cb_t ifmedia_sts; 247 { 248 void **v; 249 int bmsr, i; 250 251 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT); 252 v[0] = ifmedia_upd; 253 v[1] = ifmedia_sts; 254 *child = device_add_child(dev, "miibus", -1); 255 device_set_ivars(*child, v); 256 257 for (i = 0; i < MII_NPHY; i++) { 258 bmsr = MIIBUS_READREG(dev, i, MII_BMSR); 259 if (bmsr == 0 || bmsr == 0xffff || 260 (bmsr & BMSR_MEDIAMASK) == 0) { 261 /* Assume no PHY at this address. */ 262 continue; 263 } else 264 break; 265 } 266 267 if (i == MII_NPHY) { 268 device_delete_child(dev, *child); 269 *child = NULL; 270 return(ENXIO); 271 } 272 273 bus_generic_attach(dev); 274 275 return(0); 276 } 277 278 /* 279 * Media changed; notify all PHYs. 280 */ 281 int 282 mii_mediachg(mii) 283 struct mii_data *mii; 284 { 285 struct mii_softc *child; 286 int rv; 287 288 mii->mii_media_status = 0; 289 mii->mii_media_active = IFM_NONE; 290 291 for (child = LIST_FIRST(&mii->mii_phys); child != NULL; 292 child = LIST_NEXT(child, mii_list)) { 293 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 294 if (rv) 295 return (rv); 296 } 297 return (0); 298 } 299 300 /* 301 * Call the PHY tick routines, used during autonegotiation. 302 */ 303 void 304 mii_tick(mii) 305 struct mii_data *mii; 306 { 307 struct mii_softc *child; 308 309 for (child = LIST_FIRST(&mii->mii_phys); child != NULL; 310 child = LIST_NEXT(child, mii_list)) 311 (void) (*child->mii_service)(child, mii, MII_TICK); 312 } 313 314 /* 315 * Get media status from PHYs. 316 */ 317 void 318 mii_pollstat(mii) 319 struct mii_data *mii; 320 { 321 struct mii_softc *child; 322 323 mii->mii_media_status = 0; 324 mii->mii_media_active = IFM_NONE; 325 326 for (child = LIST_FIRST(&mii->mii_phys); child != NULL; 327 child = LIST_NEXT(child, mii_list)) 328 (void) (*child->mii_service)(child, mii, MII_POLLSTAT); 329 } 330