1 /* $NetBSD: dl10019.c,v 1.6 2001/11/13 13:14:36 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: dl10019.c,v 1.6 2001/11/13 13:14:36 lukem Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mbuf.h> 45 #include <sys/syslog.h> 46 #include <sys/socket.h> 47 #include <sys/device.h> 48 49 #include <net/if.h> 50 #include <net/if_ether.h> 51 #include <net/if_media.h> 52 53 #include <machine/bus.h> 54 #include <machine/intr.h> 55 56 #include <dev/mii/miivar.h> 57 #include <dev/mii/mii.h> 58 #include <dev/mii/mii_bitbang.h> 59 60 #include <dev/ic/dp8390reg.h> 61 #include <dev/ic/dp8390var.h> 62 63 #include <dev/ic/ne2000reg.h> 64 #include <dev/ic/ne2000var.h> 65 66 #include <dev/ic/dl10019reg.h> 67 #include <dev/ic/dl10019var.h> 68 69 int dl10019_mii_readreg(struct device *, int, int); 70 void dl10019_mii_writereg(struct device *, int, int, int); 71 void dl10019_mii_statchg(struct device *); 72 73 /* 74 * MII bit-bang glue. 75 */ 76 u_int32_t dl10019_mii_bitbang_read(struct device *); 77 void dl10019_mii_bitbang_write(struct device *, u_int32_t); 78 79 const struct mii_bitbang_ops dl10019_mii_bitbang_ops = { 80 dl10019_mii_bitbang_read, 81 dl10019_mii_bitbang_write, 82 { 83 DL0_GPIO_MII_DATAOUT, /* MII_BIT_MDO */ 84 DL0_GPIO_MII_DATAIN, /* MII_BIT_MDI */ 85 DL0_GPIO_MII_CLK, /* MII_BIT_MDC */ 86 DL0_19_GPIO_MII_DIROUT, /* MII_BIT_DIR_HOST_PHY */ 87 0, /* MII_BIT_DIR_PHY_HOST */ 88 } 89 }; 90 91 const struct mii_bitbang_ops dl10022_mii_bitbang_ops = { 92 dl10019_mii_bitbang_read, 93 dl10019_mii_bitbang_write, 94 { 95 DL0_GPIO_MII_DATAOUT, /* MII_BIT_MDO */ 96 DL0_GPIO_MII_DATAIN, /* MII_BIT_MDI */ 97 DL0_GPIO_MII_CLK, /* MII_BIT_MDC */ 98 DL0_22_GPIO_MII_DIROUT, /* MII_BIT_DIR_HOST_PHY */ 99 0, /* MII_BIT_DIR_PHY_HOST */ 100 } 101 }; 102 103 static void 104 dl10019_mii_reset(struct dp8390_softc *sc) 105 { 106 struct ne2000_softc *nsc = (void *) sc; 107 int i; 108 109 if (nsc->sc_type != NE2000_TYPE_DL10022) 110 return; 111 112 for (i = 0; i < 2; i++) { 113 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 114 0x08); 115 delay(1); 116 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 117 0x0c); 118 delay(1); 119 } 120 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 0x00); 121 } 122 123 void 124 dl10019_media_init(struct dp8390_softc *sc) 125 { 126 struct ifnet *ifp = &sc->sc_ec.ec_if; 127 128 sc->sc_mii.mii_ifp = ifp; 129 sc->sc_mii.mii_readreg = dl10019_mii_readreg; 130 sc->sc_mii.mii_writereg = dl10019_mii_writereg; 131 sc->sc_mii.mii_statchg = dl10019_mii_statchg; 132 ifmedia_init(&sc->sc_mii.mii_media, 0, dp8390_mediachange, 133 dp8390_mediastatus); 134 135 dl10019_mii_reset(sc); 136 137 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 138 MII_OFFSET_ANY, 0); 139 140 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 141 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 142 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 143 } else 144 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 145 } 146 147 void 148 dl10019_media_fini(struct dp8390_softc *sc) 149 { 150 151 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 152 } 153 154 int 155 dl10019_mediachange(struct dp8390_softc *sc) 156 { 157 158 mii_mediachg(&sc->sc_mii); 159 return (0); 160 } 161 162 void 163 dl10019_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr) 164 { 165 166 mii_pollstat(&sc->sc_mii); 167 ifmr->ifm_status = sc->sc_mii.mii_media_status; 168 ifmr->ifm_active = sc->sc_mii.mii_media_active; 169 } 170 171 void 172 dl10019_init_card(struct dp8390_softc *sc) 173 { 174 175 dl10019_mii_reset(sc); 176 mii_mediachg(&sc->sc_mii); 177 } 178 179 void 180 dl10019_stop_card(struct dp8390_softc *sc) 181 { 182 183 mii_down(&sc->sc_mii); 184 } 185 186 u_int32_t 187 dl10019_mii_bitbang_read(struct device *self) 188 { 189 struct dp8390_softc *sc = (void *) self; 190 191 /* We're already in Page 0. */ 192 return (bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO) & 193 ~DL0_GPIO_PRESERVE); 194 } 195 196 void 197 dl10019_mii_bitbang_write(struct device *self, u_int32_t val) 198 { 199 struct dp8390_softc *sc = (void *) self; 200 u_int8_t gpio; 201 202 /* We're already in Page 0. */ 203 gpio = bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO); 204 bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 205 (val & ~DL0_GPIO_PRESERVE) | (gpio & DL0_GPIO_PRESERVE)); 206 } 207 208 int 209 dl10019_mii_readreg(struct device *self, int phy, int reg) 210 { 211 struct ne2000_softc *nsc = (void *) self; 212 const struct mii_bitbang_ops *ops; 213 int val; 214 215 ops = (nsc->sc_type == NE2000_TYPE_DL10022) ? 216 &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops; 217 218 val = mii_bitbang_readreg(self, ops, phy, reg); 219 220 return (val); 221 } 222 223 void 224 dl10019_mii_writereg(struct device *self, int phy, int reg, int val) 225 { 226 struct ne2000_softc *nsc = (void *) self; 227 const struct mii_bitbang_ops *ops; 228 229 ops = (nsc->sc_type == NE2000_TYPE_DL10022) ? 230 &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops; 231 232 mii_bitbang_writereg(self, ops, phy, reg, val); 233 } 234 235 void 236 dl10019_mii_statchg(struct device *self) 237 { 238 struct dp8390_softc *sc = (void *) self; 239 struct ne2000_softc *nsc = (void *) self; 240 241 /* 242 * Disable collision detection on the DL10022 if 243 * we are on a full-duplex link. 244 */ 245 if (nsc->sc_type == NE2000_TYPE_DL10022) { 246 u_int8_t diag; 247 248 if (sc->sc_mii.mii_media_active & IFM_FDX) 249 diag = DL0_DIAG_NOCOLLDETECT; 250 else 251 diag = 0; 252 bus_space_write_1(sc->sc_regt, sc->sc_regh, 253 NEDL_DL0_DIAG, diag); 254 } 255 } 256