1 /* $NetBSD: icsphy.c,v 1.41 2006/11/16 21:24:07 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 55 */ 56 57 #include <sys/cdefs.h> 58 __FBSDID("$FreeBSD$"); 59 60 /* 61 * driver for Integrated Circuit Systems' ICS1889-1893 ethernet 10/100 PHY 62 * datasheet from www.icst.com 63 */ 64 65 #include <sys/param.h> 66 #include <sys/systm.h> 67 #include <sys/kernel.h> 68 #include <sys/module.h> 69 #include <sys/socket.h> 70 #include <sys/bus.h> 71 72 #include <net/if.h> 73 #include <net/if_media.h> 74 75 #include <dev/mii/mii.h> 76 #include <dev/mii/miivar.h> 77 #include "miidevs.h" 78 79 #include <dev/mii/icsphyreg.h> 80 81 #include "miibus_if.h" 82 83 static int icsphy_probe(device_t dev); 84 static int icsphy_attach(device_t dev); 85 86 struct icsphy_softc { 87 struct mii_softc mii_sc; 88 int mii_model; 89 }; 90 91 static device_method_t icsphy_methods[] = { 92 /* device interface */ 93 DEVMETHOD(device_probe, icsphy_probe), 94 DEVMETHOD(device_attach, icsphy_attach), 95 DEVMETHOD(device_detach, mii_phy_detach), 96 DEVMETHOD(device_shutdown, bus_generic_shutdown), 97 { 0, 0 } 98 }; 99 100 static devclass_t icsphy_devclass; 101 102 static driver_t icsphy_driver = { 103 "icsphy", 104 icsphy_methods, 105 sizeof(struct icsphy_softc) 106 }; 107 108 DRIVER_MODULE(icsphy, miibus, icsphy_driver, icsphy_devclass, 0, 0); 109 110 static int icsphy_service(struct mii_softc *, struct mii_data *, int); 111 static void icsphy_status(struct mii_softc *); 112 static void icsphy_reset(struct mii_softc *); 113 114 static const struct mii_phydesc icsphys[] = { 115 MII_PHY_DESC(xxICS, 1889), 116 MII_PHY_DESC(xxICS, 1890), 117 MII_PHY_DESC(xxICS, 1892), 118 MII_PHY_DESC(xxICS, 1893), 119 MII_PHY_END 120 }; 121 122 static int 123 icsphy_probe(device_t dev) 124 { 125 126 return (mii_phy_dev_probe(dev, icsphys, BUS_PROBE_DEFAULT)); 127 } 128 129 static int 130 icsphy_attach(device_t dev) 131 { 132 struct icsphy_softc *isc; 133 struct mii_softc *sc; 134 struct mii_attach_args *ma; 135 struct mii_data *mii; 136 137 isc = device_get_softc(dev); 138 sc = &isc->mii_sc; 139 ma = device_get_ivars(dev); 140 sc->mii_dev = device_get_parent(dev); 141 mii = ma->mii_data; 142 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 143 144 sc->mii_flags = miibus_get_flags(dev); 145 sc->mii_inst = mii->mii_instance++; 146 sc->mii_phy = ma->mii_phyno; 147 sc->mii_service = icsphy_service; 148 sc->mii_pdata = mii; 149 150 sc->mii_flags |= MIIF_NOISOLATE; 151 152 ifmedia_add(&mii->mii_media, 153 IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst), 154 MII_MEDIA_100_TX, NULL); 155 156 isc->mii_model = MII_MODEL(ma->mii_id2); 157 icsphy_reset(sc); 158 159 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 160 device_printf(dev, " "); 161 mii_phy_add_media(sc); 162 printf("\n"); 163 164 MIIBUS_MEDIAINIT(sc->mii_dev); 165 166 return (0); 167 } 168 169 static int 170 icsphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 171 { 172 173 switch (cmd) { 174 case MII_POLLSTAT: 175 break; 176 177 case MII_MEDIACHG: 178 /* 179 * If the interface is not up, don't do anything. 180 */ 181 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 182 break; 183 184 mii_phy_setmedia(sc); 185 break; 186 187 case MII_TICK: 188 if (mii_phy_tick(sc) == EJUSTRETURN) 189 return (0); 190 break; 191 } 192 193 /* Update the media status. */ 194 icsphy_status(sc); 195 196 /* Callback if something changed. */ 197 mii_phy_update(sc, cmd); 198 return (0); 199 } 200 201 static void 202 icsphy_status(struct mii_softc *sc) 203 { 204 struct mii_data *mii = sc->mii_pdata; 205 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 206 int bmcr, qpr; 207 208 mii->mii_media_status = IFM_AVALID; 209 mii->mii_media_active = IFM_ETHER; 210 211 /* 212 * Don't get link from the BMSR. It's available in the QPR, 213 * and we have to read it twice to unlatch it anyhow. This 214 * gives us fewer register reads. 215 */ 216 qpr = PHY_READ(sc, MII_ICSPHY_QPR); /* unlatch */ 217 qpr = PHY_READ(sc, MII_ICSPHY_QPR); /* real value */ 218 219 if (qpr & QPR_LINK) 220 mii->mii_media_status |= IFM_ACTIVE; 221 222 bmcr = PHY_READ(sc, MII_BMCR); 223 if (bmcr & BMCR_ISO) { 224 mii->mii_media_active |= IFM_NONE; 225 mii->mii_media_status = 0; 226 return; 227 } 228 229 if (bmcr & BMCR_LOOP) 230 mii->mii_media_active |= IFM_LOOP; 231 232 if (bmcr & BMCR_AUTOEN) { 233 if ((qpr & QPR_ACOMP) == 0) { 234 /* Erg, still trying, I guess... */ 235 mii->mii_media_active |= IFM_NONE; 236 return; 237 } 238 if (qpr & QPR_SPEED) 239 mii->mii_media_active |= IFM_100_TX; 240 else 241 mii->mii_media_active |= IFM_10_T; 242 if (qpr & QPR_FDX) 243 mii->mii_media_active |= IFM_FDX; 244 else 245 mii->mii_media_active |= IFM_HDX; 246 } else 247 mii->mii_media_active = ife->ifm_media; 248 } 249 250 static void 251 icsphy_reset(struct mii_softc *sc) 252 { 253 struct icsphy_softc *isc = (struct icsphy_softc *)sc; 254 255 mii_phy_reset(sc); 256 /* set powerdown feature */ 257 switch (isc->mii_model) { 258 case MII_MODEL_xxICS_1890: 259 case MII_MODEL_xxICS_1893: 260 PHY_WRITE(sc, MII_ICSPHY_ECR2, ECR2_100AUTOPWRDN); 261 break; 262 case MII_MODEL_xxICS_1892: 263 PHY_WRITE(sc, MII_ICSPHY_ECR2, 264 ECR2_10AUTOPWRDN|ECR2_100AUTOPWRDN); 265 break; 266 default: 267 /* 1889 have no ECR2 */ 268 break; 269 } 270 /* 271 * There is no description that the reset do auto-negotiation in the 272 * data sheet. 273 */ 274 PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_STARTNEG|BMCR_FDX); 275 } 276