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