1 /* $NetBSD: le_elb.c,v 1.8 2008/04/28 20:23:17 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Juergen Hannken-Illjes. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: le_elb.c,v 1.8 2008/04/28 20:23:17 martin Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/conf.h> 37 #include <sys/device.h> 38 #include <sys/systm.h> 39 40 #include <uvm/uvm_extern.h> 41 42 #include <machine/bus.h> 43 44 #include <net/if.h> 45 #include <net/if_ether.h> 46 #include <net/if_media.h> 47 48 #include <dev/ic/lancereg.h> 49 #include <dev/ic/lancevar.h> 50 #include <dev/ic/am79900reg.h> 51 #include <dev/ic/am79900var.h> 52 53 #include <evbppc/explora/dev/elbvar.h> 54 55 #define LE_MEMSIZE 16384 56 #define LE_RDP 0x10 /* Indirect data register. */ 57 #define LE_RAP 0x14 /* Indirect address register. */ 58 #define LE_NPORTS 32 59 60 struct le_elb_softc { 61 struct am79900_softc sc_am79900; 62 bus_dma_tag_t sc_dmat; 63 bus_dmamap_t sc_dmam; 64 bus_space_tag_t sc_iot; 65 bus_space_handle_t sc_ioh; 66 void *sc_ih; 67 }; 68 69 static int le_elb_probe(device_t, cfdata_t, void *); 70 static void le_elb_attach(device_t, device_t, void *); 71 static uint16_t le_rdcsr(struct lance_softc *, uint16_t); 72 static void le_wrcsr(struct lance_softc *, uint16_t, uint16_t); 73 static void le_copytodesc(struct lance_softc *, void *, int, int); 74 static void le_copyfromdesc(struct lance_softc *, void *, int, int); 75 static void le_copytobuf(struct lance_softc *, void *, int, int); 76 static void le_copyfrombuf(struct lance_softc *, void *, int, int); 77 static void le_zerobuf(struct lance_softc *, int, int); 78 79 CFATTACH_DECL_NEW(le_elb, sizeof(struct le_elb_softc), 80 le_elb_probe, le_elb_attach, NULL, NULL); 81 82 int 83 le_elb_probe(device_t parent, cfdata_t cf, void *aux) 84 { 85 struct elb_attach_args *oaa = aux; 86 87 if (strcmp(oaa->elb_name, cf->cf_name) != 0) 88 return 0; 89 90 return (1); 91 } 92 93 void 94 le_elb_attach(device_t parent, device_t self, void *aux) 95 { 96 struct le_elb_softc *msc = device_private(self); 97 struct lance_softc *sc = &msc->sc_am79900.lsc; 98 struct elb_attach_args *eaa = aux; 99 bus_dma_segment_t seg; 100 int i, rseg; 101 102 sc->sc_dev = self; 103 aprint_normal("\n"); 104 105 if (booted_device == NULL) /*XXX*/ 106 booted_device = self; 107 108 msc->sc_iot = eaa->elb_bt; 109 msc->sc_dmat = eaa->elb_dmat; 110 111 bus_space_map(msc->sc_iot, eaa->elb_base, LE_NPORTS, 0, &msc->sc_ioh); 112 113 /* 114 * Allocate a DMA area for the card. 115 */ 116 if (bus_dmamem_alloc(msc->sc_dmat, LE_MEMSIZE, PAGE_SIZE, 0, 117 &seg, 1, &rseg, BUS_DMA_NOWAIT)) { 118 aprint_error_dev(self, "couldn't allocate memory for card\n"); 119 return; 120 } 121 if (bus_dmamem_map(msc->sc_dmat, &seg, rseg, LE_MEMSIZE, 122 (void **)&sc->sc_mem, 123 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) { 124 aprint_error_dev(self, "couldn't map memory for card\n"); 125 return; 126 } 127 128 /* 129 * Create and load the DMA map for the DMA area. 130 */ 131 if (bus_dmamap_create(msc->sc_dmat, LE_MEMSIZE, 1, 132 LE_MEMSIZE, 0, BUS_DMA_NOWAIT, &msc->sc_dmam)) { 133 aprint_error_dev(self, "couldn't create DMA map\n"); 134 bus_dmamem_free(msc->sc_dmat, &seg, rseg); 135 return; 136 } 137 if (bus_dmamap_load(msc->sc_dmat, msc->sc_dmam, 138 sc->sc_mem, LE_MEMSIZE, NULL, BUS_DMA_NOWAIT)) { 139 aprint_error_dev(self, "coundn't load DMA map\n"); 140 bus_dmamem_free(msc->sc_dmat, &seg, rseg); 141 return; 142 } 143 144 /* 145 * This is magic -- DMA doesn't work without address 146 * bit 30 set to one. 147 */ 148 sc->sc_addr = 0x40000000 | msc->sc_dmam->dm_segs[0].ds_addr; 149 sc->sc_memsize = LE_MEMSIZE; 150 151 sc->sc_copytodesc = le_copytodesc; 152 sc->sc_copyfromdesc = le_copyfromdesc; 153 sc->sc_copytobuf = le_copytobuf; 154 sc->sc_copyfrombuf = le_copyfrombuf; 155 sc->sc_zerobuf = le_zerobuf; 156 157 sc->sc_rdcsr = le_rdcsr; 158 sc->sc_wrcsr = le_wrcsr; 159 160 aprint_normal("%s", device_xname(self)); 161 162 /* Save the MAC address. */ 163 for (i = 0; i < 3; i++) { 164 sc->sc_enaddr[i * 2] = le_rdcsr(sc, 12 + i); 165 sc->sc_enaddr[i * 2 + 1] = le_rdcsr(sc, 12 + i) >> 8; 166 } 167 168 am79900_config(&msc->sc_am79900); 169 170 /* Chip is stopped. Set "software style" to 32-bit. */ 171 le_wrcsr(sc, LE_CSR58, 2); 172 173 intr_establish(eaa->elb_irq, IST_LEVEL, IPL_NET, am79900_intr, sc); 174 } 175 176 /* 177 * Read from an indirect CSR. 178 */ 179 static uint16_t 180 le_rdcsr(struct lance_softc *sc, uint16_t reg) 181 { 182 struct le_elb_softc *lesc = (struct le_elb_softc *)sc; 183 bus_space_tag_t iot = lesc->sc_iot; 184 bus_space_handle_t ioh = lesc->sc_ioh; 185 uint16_t val; 186 187 bus_space_write_4(iot, ioh, LE_RAP, reg); 188 val = bus_space_read_4(iot, ioh, LE_RDP); 189 190 return val; 191 } 192 193 /* 194 * Write to an indirect CSR. 195 */ 196 static void 197 le_wrcsr(struct lance_softc *sc, uint16_t reg, uint16_t val) 198 { 199 struct le_elb_softc *lesc = (struct le_elb_softc *)sc; 200 bus_space_tag_t iot = lesc->sc_iot; 201 bus_space_handle_t ioh = lesc->sc_ioh; 202 203 bus_space_write_4(iot, ioh, LE_RAP, reg); 204 bus_space_write_4(iot, ioh, LE_RDP, val); 205 } 206 207 /* 208 * Copy data to memory and swap bytes. 209 */ 210 static void 211 le_copytodesc(struct lance_softc *sc, void *from, int boff, int len) 212 { 213 struct le_elb_softc *msc = (struct le_elb_softc *)sc; 214 volatile uint32_t *src = from; 215 volatile uint32_t *dst = (uint32_t *)((uint8_t *)sc->sc_mem + boff); 216 int todo = len; 217 218 /* XXX lance_setladrf should be modified to use u_int32_t instead. 219 * The init block contains u_int16_t values that require 220 * special swapping. 221 */ 222 if (boff == LE_INITADDR(sc) && len == sizeof(struct leinit)) { 223 src[3] = (src[3] >> 16) | (src[3] << 16); 224 src[4] = (src[4] >> 16) | (src[4] << 16); 225 } 226 227 todo /= sizeof(uint32_t); 228 while (todo-- > 0) 229 *dst++ = bswap32(*src++); 230 231 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len, 232 BUS_DMASYNC_PREWRITE); 233 } 234 235 /* 236 * Copy data from memory and swap bytes. 237 */ 238 static void 239 le_copyfromdesc(struct lance_softc *sc, void *to, int boff, int len) 240 { 241 struct le_elb_softc *msc = (struct le_elb_softc *)sc; 242 volatile uint32_t *src = (uint32_t *)((uint8_t *)sc->sc_mem + boff); 243 volatile uint32_t *dst = to; 244 245 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len, 246 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 247 248 len /= sizeof(uint32_t); 249 while (len-- > 0) 250 *dst++ = bswap32(*src++); 251 } 252 253 /* 254 * Copy data to memory. 255 */ 256 static void 257 le_copytobuf(struct lance_softc *sc, void *from, int boff, int len) 258 { 259 struct le_elb_softc *msc = (struct le_elb_softc *)sc; 260 volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff); 261 262 memcpy(__UNVOLATILE(buf), from, len); 263 264 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len, 265 BUS_DMASYNC_PREWRITE); 266 } 267 268 /* 269 * Copy data from memory. 270 */ 271 static void 272 le_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len) 273 { 274 struct le_elb_softc *msc = (struct le_elb_softc *)sc; 275 volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff); 276 277 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len, 278 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 279 280 memcpy(to, __UNVOLATILE(buf), len); 281 } 282 283 /* 284 * Zero memory. 285 */ 286 static void 287 le_zerobuf(struct lance_softc *sc, int boff, int len) 288 { 289 struct le_elb_softc *msc = (struct le_elb_softc *)sc; 290 volatile void *buf = (void *)((uint8_t *)sc->sc_mem + boff); 291 292 memset(__UNVOLATILE(buf), 0, len); 293 294 bus_dmamap_sync(msc->sc_dmat, msc->sc_dmam, boff, len, 295 BUS_DMASYNC_PREWRITE); 296 } 297