1 /* $NetBSD: if_le_isapnp.c,v 1.34 2008/04/28 20:23:53 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org> and 35 * Matthias Drochner. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by Jonathan Stone. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: if_le_isapnp.c,v 1.34 2008/04/28 20:23:53 martin Exp $"); 65 66 #include <sys/param.h> 67 #include <sys/systm.h> 68 #include <sys/mbuf.h> 69 #include <sys/socket.h> 70 #include <sys/ioctl.h> 71 #include <sys/errno.h> 72 #include <sys/syslog.h> 73 #include <sys/select.h> 74 #include <sys/device.h> 75 76 #include <uvm/uvm_extern.h> 77 78 #include <net/if.h> 79 #include <net/if_dl.h> 80 #include <net/if_ether.h> 81 #include <net/if_media.h> 82 83 #include <sys/cpu.h> 84 #include <sys/bus.h> 85 #include <sys/intr.h> 86 87 #include <dev/isa/isavar.h> 88 #include <dev/isa/isadmavar.h> 89 90 #include <dev/isapnp/isapnpreg.h> 91 #include <dev/isapnp/isapnpvar.h> 92 #include <dev/isapnp/isapnpdevs.h> 93 94 #include <dev/ic/lancereg.h> 95 #include <dev/ic/lancevar.h> 96 #include <dev/ic/am7990reg.h> 97 #include <dev/ic/am7990var.h> 98 99 #define LE_ISAPNP_MEMSIZE 16384 100 101 #define PCNET_SAPROM 0x00 102 #define PCNET_RDP 0x10 103 #define PCNET_RAP 0x12 104 105 /* 106 * Ethernet software status per interface. 107 * 108 * Each interface is referenced by a network interface structure, 109 * ethercom.ec_if, which the routing code uses to locate the interface. 110 * This structure contains the output queue for the interface, its address, ... 111 */ 112 struct le_isapnp_softc { 113 struct am7990_softc sc_am7990; /* glue to MI code */ 114 115 void *sc_ih; 116 bus_space_tag_t sc_iot; /* space cookie */ 117 bus_space_handle_t sc_ioh; /* bus space handle */ 118 bus_dma_tag_t sc_dmat; /* bus dma tag */ 119 bus_dmamap_t sc_dmam; /* bus dma map */ 120 int sc_rap, sc_rdp; /* offsets to LANCE registers */ 121 }; 122 123 int le_isapnp_match(device_t, cfdata_t, void *); 124 void le_isapnp_attach(device_t, device_t, void *); 125 126 CFATTACH_DECL_NEW(le_isapnp, sizeof(struct le_isapnp_softc), 127 le_isapnp_match, le_isapnp_attach, NULL, NULL); 128 129 int le_isapnp_intredge(void *); 130 static void le_isapnp_wrcsr(struct lance_softc *, uint16_t, uint16_t); 131 static uint16_t le_isapnp_rdcsr(struct lance_softc *, uint16_t); 132 133 static void 134 le_isapnp_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 135 { 136 struct le_isapnp_softc *lesc = (struct le_isapnp_softc *)sc; 137 bus_space_tag_t iot = lesc->sc_iot; 138 bus_space_handle_t ioh = lesc->sc_ioh; 139 140 bus_space_write_2(iot, ioh, lesc->sc_rap, port); 141 bus_space_write_2(iot, ioh, lesc->sc_rdp, val); 142 } 143 144 static uint16_t 145 le_isapnp_rdcsr(struct lance_softc *sc, uint16_t port) 146 { 147 struct le_isapnp_softc *lesc = (struct le_isapnp_softc *)sc; 148 bus_space_tag_t iot = lesc->sc_iot; 149 bus_space_handle_t ioh = lesc->sc_ioh; 150 uint16_t val; 151 152 bus_space_write_2(iot, ioh, lesc->sc_rap, port); 153 val = bus_space_read_2(iot, ioh, lesc->sc_rdp); 154 return (val); 155 } 156 157 int 158 le_isapnp_match(device_t parent, cfdata_t cf, void *aux) 159 { 160 int pri, variant; 161 162 pri = isapnp_devmatch(aux, &isapnp_le_devinfo, &variant); 163 if (pri && variant > 0) 164 pri = 0; 165 return (pri); 166 } 167 168 void 169 le_isapnp_attach(device_t parent, device_t self, void *aux) 170 { 171 struct le_isapnp_softc *lesc = device_private(self); 172 struct lance_softc *sc = &lesc->sc_am7990.lsc; 173 struct isapnp_attach_args *ipa = aux; 174 bus_space_tag_t iot; 175 bus_space_handle_t ioh; 176 bus_dma_tag_t dmat; 177 bus_dma_segment_t seg; 178 int i, rseg, error; 179 180 sc->sc_dev = self; 181 182 if (isapnp_config(ipa->ipa_iot, ipa->ipa_memt, ipa)) { 183 aprint_error(": error in region allocation\n"); 184 return; 185 } 186 187 lesc->sc_iot = iot = ipa->ipa_iot; 188 lesc->sc_ioh = ioh = ipa->ipa_io[0].h; 189 lesc->sc_dmat = dmat = ipa->ipa_dmat; 190 191 lesc->sc_rap = PCNET_RAP; 192 lesc->sc_rdp = PCNET_RDP; 193 194 /* 195 * Extract the physical MAC address from the ROM. 196 */ 197 for (i = 0; i < sizeof(sc->sc_enaddr); i++) 198 sc->sc_enaddr[i] = bus_space_read_1(iot, ioh, PCNET_SAPROM + i); 199 200 /* 201 * Allocate a DMA area for the card. 202 */ 203 if (bus_dmamem_alloc(dmat, LE_ISAPNP_MEMSIZE, PAGE_SIZE, 0, &seg, 1, 204 &rseg, BUS_DMA_NOWAIT)) { 205 aprint_error(": couldn't allocate memory for card\n"); 206 return; 207 } 208 if (bus_dmamem_map(dmat, &seg, rseg, LE_ISAPNP_MEMSIZE, 209 (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) { 210 aprint_error(": couldn't map memory for card\n"); 211 return; 212 } 213 214 /* 215 * Create and load the DMA map for the DMA area. 216 */ 217 if (bus_dmamap_create(dmat, LE_ISAPNP_MEMSIZE, 1, 218 LE_ISAPNP_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) { 219 aprint_error(": couldn't create DMA map\n"); 220 bus_dmamem_free(dmat, &seg, rseg); 221 return; 222 } 223 if (bus_dmamap_load(dmat, lesc->sc_dmam, 224 sc->sc_mem, LE_ISAPNP_MEMSIZE, NULL, BUS_DMA_NOWAIT)) { 225 aprint_error(": coundn't load DMA map\n"); 226 bus_dmamem_free(dmat, &seg, rseg); 227 return; 228 } 229 230 sc->sc_conf3 = 0; 231 sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr; 232 sc->sc_memsize = LE_ISAPNP_MEMSIZE; 233 234 sc->sc_copytodesc = lance_copytobuf_contig; 235 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 236 sc->sc_copytobuf = lance_copytobuf_contig; 237 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 238 sc->sc_zerobuf = lance_zerobuf_contig; 239 240 sc->sc_rdcsr = le_isapnp_rdcsr; 241 sc->sc_wrcsr = le_isapnp_wrcsr; 242 sc->sc_hwinit = NULL; 243 244 if (ipa->ipa_ndrq > 0) { 245 if ((error = isa_dmacascade(ipa->ipa_ic, 246 ipa->ipa_drq[0].num)) != 0) { 247 aprint_error(": unable to cascade DRQ, error = %d\n", 248 error); 249 return; 250 } 251 } 252 253 lesc->sc_ih = isa_intr_establish(ipa->ipa_ic, ipa->ipa_irq[0].num, 254 ipa->ipa_irq[0].type, IPL_NET, le_isapnp_intredge, sc); 255 256 aprint_normal(": %s %s\n", ipa->ipa_devident, ipa->ipa_devclass); 257 258 aprint_normal("%s", device_xname(self)); 259 am7990_config(&lesc->sc_am7990); 260 } 261 262 263 /* 264 * Controller interrupt. 265 */ 266 int 267 le_isapnp_intredge(void *arg) 268 { 269 270 if (am7990_intr(arg) == 0) 271 return (0); 272 for (;;) 273 if (am7990_intr(arg) == 0) 274 return (1); 275 } 276