1 /*- 2 * Copyright (c) 2009, M. Warner Losh 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * driver for internal phy in the AX88x9x chips. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/socket.h> 39 #include <sys/bus.h> 40 41 #include <net/if.h> 42 #include <net/if_media.h> 43 44 #include <dev/mii/mii.h> 45 #include <dev/mii/miivar.h> 46 #include "miidevs.h" 47 48 #include <dev/mii/axphyreg.h> 49 50 #include "miibus_if.h" 51 52 static int axphy_probe(device_t dev); 53 static int axphy_attach(device_t dev); 54 55 static device_method_t axphy_methods[] = { 56 /* device interface */ 57 DEVMETHOD(device_probe, axphy_probe), 58 DEVMETHOD(device_attach, axphy_attach), 59 DEVMETHOD(device_detach, mii_phy_detach), 60 DEVMETHOD(device_shutdown, bus_generic_shutdown), 61 { 0, 0 } 62 }; 63 64 static devclass_t axphy_devclass; 65 66 static driver_t axphy_driver = { 67 "axphy", 68 axphy_methods, 69 sizeof(struct mii_softc) 70 }; 71 72 DRIVER_MODULE(axphy, miibus, axphy_driver, axphy_devclass, 0, 0); 73 74 static int axphy_service(struct mii_softc *, struct mii_data *, int); 75 static void axphy_status(struct mii_softc *); 76 77 static const struct mii_phydesc axphys[] = { 78 MII_PHY_DESC(ASIX, AX88X9X), 79 MII_PHY_END 80 }; 81 82 static int 83 axphy_probe(device_t dev) 84 { 85 86 return (mii_phy_dev_probe(dev, axphys, BUS_PROBE_DEFAULT)); 87 } 88 89 static int 90 axphy_attach(device_t dev) 91 { 92 struct mii_softc *sc; 93 struct mii_attach_args *ma; 94 struct mii_data *mii; 95 96 sc = device_get_softc(dev); 97 ma = device_get_ivars(dev); 98 sc->mii_dev = device_get_parent(dev); 99 mii = device_get_softc(sc->mii_dev); 100 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 101 102 sc->mii_inst = mii->mii_instance; 103 sc->mii_phy = ma->mii_phyno; 104 sc->mii_service = axphy_service; 105 sc->mii_pdata = mii; 106 sc->mii_flags |= MIIF_NOISOLATE; 107 mii->mii_instance++; 108 109 mii_phy_reset(sc); 110 111 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 112 device_printf(dev, " "); 113 mii_phy_add_media(sc); 114 printf("\n"); 115 116 MIIBUS_MEDIAINIT(sc->mii_dev); 117 mii_phy_setmedia(sc); 118 119 return (0); 120 } 121 122 static int 123 axphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 124 { 125 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 126 int reg; 127 128 switch (cmd) { 129 case MII_POLLSTAT: 130 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 131 return (0); 132 break; 133 134 case MII_MEDIACHG: 135 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 136 reg = PHY_READ(sc, MII_BMCR); 137 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 138 return (0); 139 } 140 141 /* 142 * If the interface is not up, don't do anything. 143 */ 144 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 145 break; 146 147 mii_phy_setmedia(sc); 148 break; 149 150 case MII_TICK: 151 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 152 return (0); 153 if (mii_phy_tick(sc) == EJUSTRETURN) 154 return (0); 155 break; 156 } 157 158 /* Update the media status. */ 159 axphy_status(sc); 160 161 /* Callback if something changed. */ 162 mii_phy_update(sc, cmd); 163 return (0); 164 } 165 166 static void 167 axphy_status(struct mii_softc *sc) 168 { 169 struct mii_data *mii = sc->mii_pdata; 170 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 171 int bmsr, bmcr; 172 173 mii->mii_media_status = IFM_AVALID; 174 mii->mii_media_active = IFM_ETHER; 175 176 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 177 if (bmsr & BMSR_LINK) 178 mii->mii_media_status |= IFM_ACTIVE; 179 180 bmcr = PHY_READ(sc, MII_BMCR); 181 if (bmcr & BMCR_ISO) { 182 mii->mii_media_active |= IFM_NONE; 183 mii->mii_media_status = 0; 184 return; 185 } 186 187 if (bmcr & BMCR_LOOP) 188 mii->mii_media_active |= IFM_LOOP; 189 190 if (bmcr & BMCR_AUTOEN) { 191 if ((bmsr & BMSR_ACOMP) == 0) { 192 mii->mii_media_active |= IFM_NONE; 193 return; 194 } 195 196 #if 0 197 scr = PHY_READ(sc, MII_AXPHY_SCR); 198 if (scr & SCR_S100) 199 mii->mii_media_active |= IFM_100_TX; 200 else 201 mii->mii_media_active |= IFM_10_T; 202 if (scr & SCR_FDX) 203 mii->mii_media_active |= IFM_FDX; 204 #endif 205 } else 206 mii->mii_media_active = ife->ifm_media; 207 } 208