1 /* $OpenBSD: if_le_ledma.c,v 1.17 2013/09/24 20:11:04 miod 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/lancereg.h> 62 #include <dev/ic/lancevar.h> 63 #include <dev/ic/am7990reg.h> 64 #include <dev/ic/am7990var.h> 65 66 /* 67 * LANCE registers. 68 */ 69 #define LEREG1_RDP 0 /* Register Data port */ 70 #define LEREG1_RAP 2 /* Register Address port */ 71 72 struct le_softc { 73 struct am7990_softc sc_am7990; /* glue to MI code */ 74 bus_space_tag_t sc_bustag; 75 bus_dmamap_t sc_dmamap; 76 bus_space_handle_t sc_reg; /* LANCE registers */ 77 struct lsi64854_softc *sc_dma; /* pointer to my dma */ 78 u_int sc_laddr; /* LANCE DMA address */ 79 }; 80 81 #define MEMSIZE (16*1024) /* LANCE memory size */ 82 #define LEDMA_BOUNDARY (16*1024*1024) /* must not cross 16MB boundary */ 83 84 int lematch_ledma(struct device *, void *, void *); 85 void leattach_ledma(struct device *, struct device *, void *); 86 87 /* 88 * Media types supported by the Sun4m. 89 */ 90 static int lemedia[] = { 91 IFM_ETHER | IFM_10_T, 92 IFM_ETHER | IFM_10_5, 93 IFM_ETHER | IFM_AUTO 94 }; 95 96 void le_ledma_setutp(struct lance_softc *); 97 void le_ledma_setaui(struct lance_softc *); 98 99 int lemediachange(struct lance_softc *); 100 void lemediastatus(struct lance_softc *, struct ifmediareq *); 101 102 struct cfattach le_ledma_ca = { 103 sizeof(struct le_softc), lematch_ledma, leattach_ledma 104 }; 105 106 void le_ledma_wrcsr(struct lance_softc *, uint16_t, uint16_t); 107 uint16_t le_ledma_rdcsr(struct lance_softc *, uint16_t); 108 void le_ledma_hwreset(struct lance_softc *); 109 void le_ledma_hwinit(struct lance_softc *); 110 void le_ledma_nocarrier(struct lance_softc *); 111 112 void 113 le_ledma_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 114 { 115 struct le_softc *lesc = (struct le_softc *)sc; 116 117 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 118 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 119 BUS_SPACE_BARRIER_WRITE); 120 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val); 121 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2, 122 BUS_SPACE_BARRIER_WRITE); 123 124 #if defined(SUN4M) 125 /* 126 * We need to flush the SBus->MBus write buffers. This can most 127 * easily be accomplished by reading back the register that we 128 * just wrote (thanks to Chris Torek for this solution). 129 */ 130 if (CPU_ISSUN4M) { 131 volatile uint16_t discard; 132 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, 133 LEREG1_RDP); 134 } 135 #endif 136 } 137 138 uint16_t 139 le_ledma_rdcsr(struct lance_softc *sc, uint16_t port) 140 { 141 struct le_softc *lesc = (struct le_softc *)sc; 142 143 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 144 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 145 BUS_SPACE_BARRIER_WRITE); 146 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP)); 147 } 148 149 void 150 le_ledma_setutp(struct lance_softc *sc) 151 { 152 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma; 153 u_int32_t csr; 154 155 csr = L64854_GCSR(dma); 156 csr |= E_TP_AUI; 157 L64854_SCSR(dma, csr); 158 delay(20000); /* must not touch le for 20ms */ 159 } 160 161 void 162 le_ledma_setaui(struct lance_softc *sc) 163 { 164 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma; 165 u_int32_t csr; 166 167 csr = L64854_GCSR(dma); 168 csr &= ~E_TP_AUI; 169 L64854_SCSR(dma, csr); 170 delay(20000); /* must not touch le for 20ms */ 171 } 172 173 int 174 lemediachange(struct lance_softc *sc) 175 { 176 struct ifmedia *ifm = &sc->sc_ifmedia; 177 178 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 179 return (EINVAL); 180 181 /* 182 * Switch to the selected media. If autoselect is 183 * set, we don't really have to do anything. We'll 184 * switch to the other media when we detect loss of 185 * carrier. 186 */ 187 switch (IFM_SUBTYPE(ifm->ifm_media)) { 188 case IFM_10_T: 189 le_ledma_setutp(sc); 190 break; 191 192 case IFM_10_5: 193 le_ledma_setaui(sc); 194 break; 195 196 case IFM_AUTO: 197 break; 198 199 default: 200 return (EINVAL); 201 } 202 203 return (0); 204 } 205 206 void 207 lemediastatus(struct lance_softc *sc, struct ifmediareq *ifmr) 208 { 209 struct lsi64854_softc *dma = ((struct le_softc *)sc)->sc_dma; 210 211 /* 212 * Notify the world which media we're currently using. 213 */ 214 if (L64854_GCSR(dma) & E_TP_AUI) 215 ifmr->ifm_active = IFM_ETHER|IFM_10_T; 216 else 217 ifmr->ifm_active = IFM_ETHER|IFM_10_5; 218 } 219 220 void 221 le_ledma_hwreset(struct lance_softc *sc) 222 { 223 struct le_softc *lesc = (struct le_softc *)sc; 224 struct lsi64854_softc *dma = lesc->sc_dma; 225 u_int32_t csr; 226 u_int aui_bit; 227 228 /* 229 * Reset DMA channel. 230 */ 231 csr = L64854_GCSR(dma); 232 aui_bit = csr & E_TP_AUI; 233 DMA_RESET(dma); 234 235 /* Write bits 24-31 of Lance address */ 236 bus_space_write_4(dma->sc_bustag, dma->sc_regs, L64854_REG_ENBAR, 237 lesc->sc_laddr & 0xff000000); 238 239 DMA_ENINTR(dma); 240 241 /* 242 * Disable E-cache invalidates on chip writes. 243 * Retain previous cable selection bit. 244 */ 245 csr = L64854_GCSR(dma); 246 csr |= (E_DSBL_WR_INVAL | aui_bit); 247 L64854_SCSR(dma, csr); 248 delay(20000); /* must not touch le for 20ms */ 249 } 250 251 void 252 le_ledma_hwinit(struct lance_softc *sc) 253 { 254 255 /* 256 * Make sure we're using the currently-enabled media type. 257 * XXX Actually, this is probably unnecessary, now. 258 */ 259 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_cur->ifm_media)) { 260 case IFM_10_T: 261 le_ledma_setutp(sc); 262 break; 263 264 case IFM_10_5: 265 le_ledma_setaui(sc); 266 break; 267 } 268 } 269 270 void 271 le_ledma_nocarrier(struct lance_softc *sc) 272 { 273 struct le_softc *lesc = (struct le_softc *)sc; 274 275 /* 276 * Check if the user has requested a certain cable type, and 277 * if so, honor that request. 278 */ 279 280 if (L64854_GCSR(lesc->sc_dma) & E_TP_AUI) { 281 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) { 282 case IFM_10_5: 283 case IFM_AUTO: 284 printf("%s: lost carrier on UTP port" 285 ", switching to AUI port\n", sc->sc_dev.dv_xname); 286 le_ledma_setaui(sc); 287 } 288 } else { 289 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) { 290 case IFM_10_T: 291 case IFM_AUTO: 292 printf("%s: lost carrier on AUI port" 293 ", switching to UTP port\n", sc->sc_dev.dv_xname); 294 le_ledma_setutp(sc); 295 } 296 } 297 } 298 299 int 300 lematch_ledma(struct device *parent, void *vcf, void *aux) 301 { 302 struct cfdata *cf = vcf; 303 struct sbus_attach_args *sa = aux; 304 305 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0); 306 } 307 308 309 void 310 leattach_ledma(struct device *parent, struct device *self, void *aux) 311 { 312 struct sbus_attach_args *sa = aux; 313 struct le_softc *lesc = (struct le_softc *)self; 314 struct lsi64854_softc *lsi = (struct lsi64854_softc *)parent; 315 struct lance_softc *sc = &lesc->sc_am7990.lsc; 316 bus_dma_tag_t dmatag = sa->sa_dmatag; 317 bus_dma_segment_t seg; 318 int rseg, error; 319 /* XXX the following declarations should be elsewhere */ 320 extern void myetheraddr(u_char *); 321 322 lesc->sc_bustag = sa->sa_bustag; 323 324 /* Establish link to `ledma' device */ 325 lesc->sc_dma = lsi; 326 lesc->sc_dma->sc_client = lesc; 327 328 /* Map device registers */ 329 if (sbus_bus_map(sa->sa_bustag, 330 sa->sa_slot, 331 sa->sa_offset, 332 sa->sa_size, 333 BUS_SPACE_MAP_LINEAR, 334 0, &lesc->sc_reg) != 0) { 335 printf("%s @ ledma: cannot map registers\n", self->dv_xname); 336 return; 337 } 338 339 /* Allocate buffer memory */ 340 sc->sc_memsize = MEMSIZE; 341 342 /* Get a DMA handle */ 343 if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 344 LEDMA_BOUNDARY, BUS_DMA_NOWAIT, 345 &lesc->sc_dmamap)) != 0) { 346 printf("%s: DMA map create error %d\n", self->dv_xname, error); 347 return; 348 } 349 350 /* Allocate DMA buffer */ 351 if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, LEDMA_BOUNDARY, 352 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) { 353 printf("%s @ ledma: DMA buffer alloc error %d\n", 354 self->dv_xname, error); 355 return; 356 } 357 358 /* Map DMA buffer into kernel space */ 359 if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE, 360 (caddr_t *)&sc->sc_mem, 361 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 362 printf("%s @ ledma: DMA buffer map error %d\n", 363 self->dv_xname, error); 364 bus_dmamem_free(dmatag, &seg, rseg); 365 return; 366 } 367 368 /* Load DMA buffer */ 369 if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem, 370 MEMSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) { 371 printf("%s: DMA buffer map load error %d\n", 372 self->dv_xname, error); 373 bus_dmamem_free(dmatag, &seg, rseg); 374 bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE); 375 return; 376 } 377 378 lesc->sc_laddr = lesc->sc_dmamap->dm_segs[0].ds_addr; 379 sc->sc_addr = lesc->sc_laddr & 0xffffff; 380 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON; 381 382 myetheraddr(sc->sc_arpcom.ac_enaddr); 383 384 sc->sc_mediachange = lemediachange; 385 sc->sc_mediastatus = lemediastatus; 386 sc->sc_supmedia = lemedia; 387 sc->sc_nsupmedia = nitems(lemedia); 388 sc->sc_defaultmedia = sc->sc_supmedia[sc->sc_nsupmedia - 1]; 389 390 sc->sc_copytodesc = lance_copytobuf_contig; 391 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 392 sc->sc_copytobuf = lance_copytobuf_contig; 393 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 394 sc->sc_zerobuf = lance_zerobuf_contig; 395 396 sc->sc_rdcsr = le_ledma_rdcsr; 397 sc->sc_wrcsr = le_ledma_wrcsr; 398 sc->sc_hwinit = le_ledma_hwinit; 399 sc->sc_nocarrier = le_ledma_nocarrier; 400 sc->sc_hwreset = le_ledma_hwreset; 401 402 /* Establish interrupt handler */ 403 if (sa->sa_nintr != 0) 404 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET, 0, 405 am7990_intr, sc, self->dv_xname); 406 407 am7990_config(&lesc->sc_am7990); 408 409 /* now initialize DMA */ 410 le_ledma_hwreset(sc); 411 } 412