1 /* $OpenBSD: if_le.c,v 1.14 2008/06/26 05:42:18 ray Exp $ */ 2 /* $NetBSD: if_le.c,v 1.17 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 #include <dev/sbus/lebuffervar.h> /*XXX*/ 58 59 #include <dev/ic/am7990reg.h> 60 #include <dev/ic/am7990var.h> 61 62 /* 63 * LANCE registers. 64 */ 65 #define LEREG1_RDP 0 /* Register Data port */ 66 #define LEREG1_RAP 2 /* Register Address port */ 67 68 struct le_softc { 69 struct am7990_softc sc_am7990; /* glue to MI code */ 70 bus_space_tag_t sc_bustag; 71 bus_dma_tag_t sc_dmatag; 72 bus_dmamap_t sc_dmamap; 73 bus_space_handle_t sc_reg; 74 }; 75 76 #define MEMSIZE 0x4000 /* LANCE memory size */ 77 78 int lematch_sbus(struct device *, void *, void *); 79 void leattach_sbus(struct device *, struct device *, void *); 80 81 /* 82 * Media types supported. 83 */ 84 struct cfattach le_sbus_ca = { 85 sizeof(struct le_softc), lematch_sbus, leattach_sbus 86 }; 87 88 void le_sbus_wrcsr(struct am7990_softc *, u_int16_t, u_int16_t); 89 u_int16_t le_sbus_rdcsr(struct am7990_softc *, u_int16_t); 90 91 void 92 le_sbus_wrcsr(struct am7990_softc *sc, u_int16_t port, u_int16_t val) 93 { 94 struct le_softc *lesc = (struct le_softc *)sc; 95 96 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 97 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 98 BUS_SPACE_BARRIER_WRITE); 99 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, val); 100 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP, 2, 101 BUS_SPACE_BARRIER_WRITE); 102 103 #if defined(SUN4M) 104 /* 105 * We need to flush the SBus->MBus write buffers. This can most 106 * easily be accomplished by reading back the register that we 107 * just wrote (thanks to Chris Torek for this solution). 108 */ 109 if (CPU_ISSUN4M) { 110 volatile u_int16_t discard; 111 discard = bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, 112 LEREG1_RDP); 113 } 114 #endif 115 } 116 117 u_int16_t 118 le_sbus_rdcsr(struct am7990_softc *sc, u_int16_t port) 119 { 120 struct le_softc *lesc = (struct le_softc *)sc; 121 122 bus_space_write_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, port); 123 bus_space_barrier(lesc->sc_bustag, lesc->sc_reg, LEREG1_RAP, 2, 124 BUS_SPACE_BARRIER_WRITE); 125 return (bus_space_read_2(lesc->sc_bustag, lesc->sc_reg, LEREG1_RDP)); 126 } 127 128 129 int 130 lematch_sbus(struct device *parent, void *vcf, void *aux) 131 { 132 struct cfdata *cf = vcf; 133 struct sbus_attach_args *sa = aux; 134 135 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0); 136 } 137 138 void 139 leattach_sbus(struct device *parent, struct device *self, void *aux) 140 { 141 struct sbus_attach_args *sa = aux; 142 struct le_softc *lesc = (struct le_softc *)self; 143 struct am7990_softc *sc = &lesc->sc_am7990; 144 bus_dma_tag_t dmatag; 145 /* XXX the following declarations should be elsewhere */ 146 extern void myetheraddr(u_char *); 147 extern struct cfdriver lebuffer_cd; 148 149 lesc->sc_bustag = sa->sa_bustag; 150 lesc->sc_dmatag = dmatag = sa->sa_dmatag; 151 152 if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot, 153 sa->sa_reg[0].sbr_offset, sa->sa_reg[0].sbr_size, 154 BUS_SPACE_MAP_LINEAR, 0, &lesc->sc_reg) != 0) { 155 printf(": cannot map registers\n"); 156 return; 157 } 158 159 /* 160 * Look for an "unallocated" lebuffer and pair it with 161 * this `le' device on the assumption that we're on 162 * a pre-historic ROM that doesn't establish le<=>lebuffer 163 * parent-child relationships. 164 */ 165 if (lebuffer_cd.cd_ndevs != 0) { 166 struct lebuf_softc *lebuf; 167 int i; 168 169 for (i = 0; i < lebuffer_cd.cd_ndevs; i++) { 170 lebuf = (struct lebuf_softc *)lebuffer_cd.cd_devs[i]; 171 if (lebuf == NULL || lebuf->attached != 0) 172 continue; 173 174 sc->sc_mem = lebuf->sc_buffer; 175 sc->sc_memsize = lebuf->sc_bufsiz; 176 /* Lance view is offset by buffer location */ 177 sc->sc_addr = 0; 178 lebuf->attached = 1; 179 180 /* That old black magic... */ 181 sc->sc_conf3 = getpropint(sa->sa_node, 182 "busmaster-regval", 183 LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON); 184 break; 185 } 186 } 187 188 if (sc->sc_mem == 0) { 189 bus_dma_segment_t seg; 190 int rseg, error; 191 192 /* Get a DMA handle */ 193 if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0, 194 BUS_DMA_NOWAIT|BUS_DMA_24BIT, &lesc->sc_dmamap)) != 0) { 195 printf(": DMA map create error %d\n", error); 196 return; 197 } 198 199 /* Allocate DMA buffer */ 200 if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, 0, 0, 201 &seg, 1, &rseg, BUS_DMA_NOWAIT|BUS_DMA_24BIT)) != 0){ 202 printf(": DMA buffer allocation error %d\n", error); 203 return; 204 } 205 206 /* Map DMA buffer into kernel space */ 207 if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE, 208 (caddr_t *)&sc->sc_mem, 209 BUS_DMA_NOWAIT|BUS_DMA_COHERENT|BUS_DMA_24BIT)) != 0) { 210 printf(": DMA buffer map error %d\n", error); 211 bus_dmamem_free(lesc->sc_dmatag, &seg, rseg); 212 return; 213 } 214 215 /* Load DMA buffer */ 216 if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap, sc->sc_mem, 217 MEMSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT|BUS_DMA_24BIT)) != 0) { 218 printf(": DMA buffer map load error %d\n", error); 219 bus_dmamem_free(dmatag, &seg, rseg); 220 bus_dmamem_unmap(dmatag, sc->sc_mem, MEMSIZE); 221 return; 222 } 223 224 sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff; 225 sc->sc_memsize = MEMSIZE; 226 sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON; 227 } 228 229 myetheraddr(sc->sc_arpcom.ac_enaddr); 230 231 sc->sc_copytodesc = am7990_copytobuf_contig; 232 sc->sc_copyfromdesc = am7990_copyfrombuf_contig; 233 sc->sc_copytobuf = am7990_copytobuf_contig; 234 sc->sc_copyfrombuf = am7990_copyfrombuf_contig; 235 sc->sc_zerobuf = am7990_zerobuf_contig; 236 237 sc->sc_rdcsr = le_sbus_rdcsr; 238 sc->sc_wrcsr = le_sbus_wrcsr; 239 240 am7990_config(&lesc->sc_am7990); 241 242 /* Establish interrupt handler */ 243 if (sa->sa_nintr != 0) 244 (void)bus_intr_establish(lesc->sc_bustag, sa->sa_pri, 245 IPL_NET, 0, am7990_intr, sc, self->dv_xname); 246 } 247