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