1 /* $OpenBSD: if_le_ledma.c,v 1.14 2008/06/26 05:42:18 ray Exp $ */ 2 /* $NetBSD: if_le_ledma.c,v 1.14 2001/05/30 11:46:35 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace 10 * Simulation Facility, NASA Ames Research Center; Paul Kranenburg. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "bpfilter.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/mbuf.h> 39 #include <sys/syslog.h> 40 #include <sys/socket.h> 41 #include <sys/device.h> 42 #include <sys/malloc.h> 43 44 #include <net/if.h> 45 #include <net/if_media.h> 46 47 #ifdef INET 48 #include <netinet/in.h> 49 #include <netinet/if_ether.h> 50 #endif 51 52 #include <machine/bus.h> 53 #include <machine/intr.h> 54 #include <machine/autoconf.h> 55 56 #include <dev/sbus/sbusvar.h> 57 58 #include <dev/ic/lsi64854reg.h> 59 #include <dev/ic/lsi64854var.h> 60 61 #include <dev/ic/am7990reg.h> 62 #include <dev/ic/am7990var.h> 63 64 /* 65 * LANCE registers. 66 */ 67 #define LEREG1_RDP 0 /* Register Data port */ 68 #define LEREG1_RAP 2 /* Register Address port */ 69 70 struct le_softc { 71 struct am7990_softc sc_am7990; /* glue to MI code */ 72 bus_space_tag_t sc_bustag; 73 bus_dmamap_t sc_dmamap; 74 bus_space_handle_t sc_reg; /* LANCE registers */ 75 struct lsi64854_softc *sc_dma; /* pointer to my dma */ 76 u_int sc_laddr; /* LANCE DMA address */ 77 }; 78 79 #define MEMSIZE (16*1024) /* LANCE memory size */ 80 #define LEDMA_BOUNDARY (16*1024*1024) /* must not cross 16MB boundary */ 81 82 int lematch_ledma(struct device *, void *, void *); 83 void leattach_ledma(struct device *, struct device *, void *); 84 85 /* 86 * Media types supported by the Sun4m. 87 */ 88 89 void le_ledma_setutp(struct am7990_softc *); 90 void le_ledma_setaui(struct am7990_softc *); 91 92 int lemediachange(struct ifnet *); 93 void lemediastatus(struct ifnet *, struct ifmediareq *); 94 95 struct cfattach le_ledma_ca = { 96 sizeof(struct le_softc), lematch_ledma, leattach_ledma 97 }; 98 99 void le_ledma_wrcsr(struct am7990_softc *, u_int16_t, u_int16_t); 100 u_int16_t le_ledma_rdcsr(struct am7990_softc *, u_int16_t); 101 void le_ledma_hwreset(struct am7990_softc *); 102 void le_ledma_hwinit(struct am7990_softc *); 103 void le_ledma_nocarrier(struct am7990_softc *); 104 105 void 106 le_ledma_wrcsr(struct am7990_softc *sc, u_int16_t port, u_int16_t val) 107 { 108 struct le_softc *lesc = (struct le_softc *)sc; 109 110 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 111 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 112 BUS_SPACE_BARRIER_WRITE); 113 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val); 114 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2, 115 BUS_SPACE_BARRIER_WRITE); 116 117 #if defined(SUN4M) 118 /* 119 * We need to flush the SBus->MBus write buffers. This can most 120 * easily be accomplished by reading back the register that we 121 * just wrote (thanks to Chris Torek for this solution). 122 */ 123 if (CPU_ISSUN4M) { 124 volatile u_int16_t discard; 125 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, 126 LEREG1_RDP); 127 } 128 #endif 129 } 130 131 u_int16_t 132 le_ledma_rdcsr(struct am7990_softc *sc, u_int16_t port) 133 { 134 struct le_softc *lesc = (struct le_softc *)sc; 135 136 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 137 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 138 BUS_SPACE_BARRIER_WRITE); 139 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP)); 140 } 141 142 void 143 le_ledma_setutp(struct am7990_softc *sc) 144 { 145 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma; 146 u_int32_t csr; 147 148 csr = L64854_GCSR(dma); 149 csr |= E_TP_AUI; 150 L64854_SCSR(dma, csr); 151 delay(20000); /* must not touch le for 20ms */ 152 } 153 154 void 155 le_ledma_setaui(struct am7990_softc *sc) 156 { 157 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma; 158 u_int32_t csr; 159 160 csr = L64854_GCSR(dma); 161 csr &= ~E_TP_AUI; 162 L64854_SCSR(dma, csr); 163 delay(20000); /* must not touch le for 20ms */ 164 } 165 166 int 167 lemediachange(struct ifnet *ifp) 168 { 169 struct am7990_softc *sc = ifp->if_softc; 170 struct ifmedia *ifm = &sc->sc_ifmedia; 171 172 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 173 return (EINVAL); 174 175 /* 176 * Switch to the selected media. If autoselect is 177 * set, we don't really have to do anything. We'll 178 * switch to the other media when we detect loss of 179 * carrier. 180 */ 181 switch (IFM_SUBTYPE(ifm->ifm_media)) { 182 case IFM_10_T: 183 le_ledma_setutp(sc); 184 break; 185 186 case IFM_10_5: 187 le_ledma_setaui(sc); 188 break; 189 190 case IFM_AUTO: 191 break; 192 193 default: 194 return (EINVAL); 195 } 196 197 return (0); 198 } 199 200 void 201 lemediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 202 { 203 struct am7990_softc *sc = ifp->if_softc; 204 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma; 205 206 /* 207 * Notify the world which media we're currently using. 208 */ 209 if (L64854_GCSR(dma) & E_TP_AUI) 210 ifmr->ifm_active = IFM_ETHER|IFM_10_T; 211 else 212 ifmr->ifm_active = IFM_ETHER|IFM_10_5; 213 } 214 215 void 216 le_ledma_hwreset(struct am7990_softc *sc) 217 { 218 struct le_softc *lesc = (struct le_softc *)sc; 219 struct lsi64854_softc *dma = lesc->sc_dma; 220 u_int32_t csr; 221 u_int aui_bit; 222 223 /* 224 * Reset DMA channel. 225 */ 226 csr = L64854_GCSR(dma); 227 aui_bit = csr & E_TP_AUI; 228 DMA_RESET(dma); 229 230 /* Write bits 24-31 of Lance address */ 231 bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR, 232 lesc->sc_laddr & 0xff000000); 233 234 DMA_ENINTR(dma); 235 236 /* 237 * Disable E-cache invalidates on chip writes. 238 * Retain previous cable selection bit. 239 */ 240 csr = L64854_GCSR(dma); 241 csr |= (E_DSBL_WR_INVAL | aui_bit); 242 L64854_SCSR(dma, csr); 243 delay(20000); /* must not touch le for 20ms */ 244 } 245 246 void 247 le_ledma_hwinit(struct am7990_softc *sc) 248 { 249 250 /* 251 * Make sure we're using the currently-enabled media type. 252 * XXX Actually, this is probably unnecessary, now. 253 */ 254 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_cur->ifm_media)) { 255 case IFM_10_T: 256 le_ledma_setutp(sc); 257 break; 258 259 case IFM_10_5: 260 le_ledma_setaui(sc); 261 break; 262 } 263 } 264 265 void 266 le_ledma_nocarrier(struct am7990_softc *sc) 267 { 268 struct le_softc *lesc = (struct le_softc *)sc; 269 270 /* 271 * Check if the user has requested a certain cable type, and 272 * if so, honor that request. 273 */ 274 275 if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) { 276 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) { 277 case IFM_10_5: 278 case IFM_AUTO: 279 printf("%s: lost carrier on UTP port" 280 ", switching to AUI port\n", sc->sc_dev.dv_xname); 281 le_ledma_setaui(sc); 282 } 283 } else { 284 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) { 285 case IFM_10_T: 286 case IFM_AUTO: 287 printf("%s: lost carrier on AUI port" 288 ", switching to UTP port\n", sc->sc_dev.dv_xname); 289 le_ledma_setutp(sc); 290 } 291 } 292 } 293 294 int 295 lematch_ledma(struct device *parent, void *vcf, void *aux) 296 { 297 struct cfdata *cf = vcf; 298 struct sbus_attach_args *sa = aux; 299 300 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0); 301 } 302 303 304 void 305 leattach_ledma(struct device *parent, struct device *self, void *aux) 306 { 307 struct sbus_attach_args *sa = aux; 308 struct le_softc *lesc = (struct le_softc *)self; 309 struct lsi64854_softc *lsi = (struct lsi64854_softc *)parent; 310 struct am7990_softc *sc = &lesc->sc_am7990; 311 bus_dma_tag_t dmatag = sa->sa_dmatag; 312 bus_dma_segment_t seg; 313 int rseg, error; 314 /* XXX the following declarations should be elsewhere */ 315 extern void myetheraddr(u_char *); 316 317 lesc->sc_bustag = sa->sa_bustag; 318 319 /* Establish link to `ledma' device */ 320 lesc->sc_dma = lsi; 321 lesc->sc_dma->sc_client = lesc; 322 323 /* Map device registers */ 324 if (sbus_bus_map(sa->sa_bustag, 325 sa->sa_slot, 326 sa->sa_offset, 327 sa->sa_size, 328 BUS_SPACE_MAP_LINEAR, 329 0, &lesc->sc_reg) != 0) { 330 printf("%s @ ledma: cannot map registers\n", self->dv_xname); 331 return; 332 } 333 334 /* Allocate buffer memory */ 335 sc->sc_memsize = MEMSIZE; 336 337 /* Get a DMA handle */ 338 if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 339 LEDMA_BOUNDARY, BUS_DMA_NOWAIT, 340 &lesc->sc_dmamap)) != 0) { 341 printf("%s: DMA map create error %d\n", self->dv_xname, error); 342 return; 343 } 344 345 /* Allocate DMA buffer */ 346 if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, LEDMA_BOUNDARY, 347 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 348 printf("%s @ ledma: DMA buffer alloc error %d\n", 349 self->dv_xname, error); 350 return; 351 } 352 353 /* Map DMA buffer into kernel space */ 354 if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE, 355 (caddr_t *)&sc->sc_mem, 356 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 357 printf("%s @ ledma: DMA buffer map error %d\n", 358 self->dv_xname, error); 359 bus_dmamem_free(dmatag, &seg, rseg); 360 return; 361 } 362 363 /* Load DMA buffer */ 364 if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem, 365 MEMSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 366 printf("%s: DMA buffer map load error %d\n", 367 self->dv_xname, error); 368 bus_dmamem_free(dmatag, &seg, rseg); 369 bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE); 370 return; 371 } 372 373 lesc->sc_laddr = lesc->sc_dmamap->dm_segs[0].ds_addr; 374 sc->sc_addr = lesc->sc_laddr & 0xffffff; 375 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON; 376 377 ifmedia_init(&sc->sc_ifmedia, 0, lemediachange, lemediastatus); 378 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T, 0, NULL); 379 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_5, 0, NULL); 380 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL); 381 ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO); 382 sc->sc_hasifmedia = 1; 383 384 myetheraddr(sc->sc_arpcom.ac_enaddr); 385 386 sc->sc_copytodesc = am7990_copytobuf_contig; 387 sc->sc_copyfromdesc = am7990_copyfrombuf_contig; 388 sc->sc_copytobuf = am7990_copytobuf_contig; 389 sc->sc_copyfrombuf = am7990_copyfrombuf_contig; 390 sc->sc_zerobuf = am7990_zerobuf_contig; 391 392 sc->sc_rdcsr = le_ledma_rdcsr; 393 sc->sc_wrcsr = le_ledma_wrcsr; 394 sc->sc_hwinit = le_ledma_hwinit; 395 sc->sc_nocarrier = le_ledma_nocarrier; 396 sc->sc_hwreset = le_ledma_hwreset; 397 398 /* Establish interrupt handler */ 399 if (sa->sa_nintr != 0) 400 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0, 401 am7990_intr, sc, self->dv_xname); 402 403 am7990_config(&lesc->sc_am7990); 404 405 /* now initialize DMA */ 406 le_ledma_hwreset(sc); 407 } 408