1 /* $NetBSD: if_ei.c,v 1.8 2002/10/02 16:52:24 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2000, 2001 Ben Harris 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 /* 30 * if_ei.c -- driver for Acorn AKA25 Ether1 card 31 */ 32 /* 33 * This is not directly based on the arm32 ie driver, as that doesn't 34 * use the MI 82586 driver, and generally looks a mess (not to mention 35 * having dodgy copyright status). Let's try again. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: if_ei.c,v 1.8 2002/10/02 16:52:24 thorpej Exp $"); 40 41 #include <sys/param.h> 42 43 #include <sys/device.h> 44 #include <sys/malloc.h> 45 #include <sys/reboot.h> /* For bootverbose */ 46 #include <sys/socket.h> 47 #include <sys/syslog.h> 48 #include <sys/systm.h> 49 #include <net/if.h> 50 #include <net/if_ether.h> 51 #include <net/if_media.h> 52 #include <machine/bus.h> 53 #include <dev/ic/i82586reg.h> 54 #include <dev/ic/i82586var.h> 55 #include <dev/podulebus/podulebus.h> 56 #include <dev/podulebus/podules.h> 57 #include <dev/podulebus/if_eireg.h> 58 59 /* Callbacks from the MI 82586 driver */ 60 static void ei_hwreset(struct ie_softc *, int); 61 /* static void ei_hwinit(struct ie_softc *); */ 62 static void ei_attn(struct ie_softc *, int); 63 static int ei_intrhook(struct ie_softc *, int); 64 65 static void ei_copyin(struct ie_softc *, void *, int, size_t); 66 static void ei_copyout(struct ie_softc *, const void *, int, size_t); 67 static u_int16_t ei_read16(struct ie_softc *, int); 68 static void ei_write16(struct ie_softc *, int, u_int16_t); 69 static void ei_write24(struct ie_softc *, int, int); 70 71 /* autoconfiguration glue */ 72 static int ei_match(struct device *, struct cfdata *, void *); 73 static void ei_attach(struct device *, struct device *, void *); 74 75 struct ei_softc { 76 struct ie_softc sc_ie; 77 bus_space_tag_t sc_ctl_t; 78 bus_space_handle_t sc_ctl_h; 79 bus_space_tag_t sc_mem_t; 80 bus_space_handle_t sc_mem_h; 81 bus_space_tag_t sc_rom_t; 82 bus_space_handle_t sc_rom_h; 83 u_int8_t sc_idrom[EI_ROMSIZE]; 84 void *sc_ih; 85 struct evcnt sc_intrcnt; 86 }; 87 88 CFATTACH_DECL(ei, sizeof(struct ei_softc), 89 ei_match, ei_attach, NULL, NULL); 90 91 static inline void 92 ei_setpage(struct ei_softc *sc, int page) 93 { 94 95 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_PAGE, page); 96 } 97 98 static inline void 99 ei_cli(struct ei_softc *sc) 100 { 101 102 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_CLI); 103 } 104 105 static int 106 ei_match(struct device *parent, struct cfdata *cf, void *aux) 107 { 108 struct podulebus_attach_args *pa = aux; 109 110 return pa->pa_product == PODULE_ETHER1; 111 } 112 113 static void 114 ei_attach(struct device *parent, struct device *self, void *aux) 115 { 116 struct podulebus_attach_args *pa = aux; 117 struct ei_softc *sc = (struct ei_softc *)self; 118 int i; 119 char descr[16]; 120 121 /* Set up bus spaces */ 122 sc->sc_ctl_t = sc->sc_mem_t = pa->pa_fast_t; 123 bus_space_map(pa->pa_fast_t, pa->pa_fast_base, EI_MEMOFF, 0, 124 &sc->sc_ctl_h); 125 bus_space_map(pa->pa_fast_t, pa->pa_fast_base + EI_MEMOFF, 126 EI_PAGESIZE * 2, 0, &sc->sc_mem_h); 127 sc->sc_rom_t = pa->pa_sync_t; 128 bus_space_map(pa->pa_sync_t, pa->pa_sync_base, EI_ROMSIZE, 0, 129 &sc->sc_rom_h); 130 131 /* Set up callbacks */ 132 sc->sc_ie.hwinit = NULL; 133 sc->sc_ie.intrhook = ei_intrhook; 134 sc->sc_ie.hwreset = ei_hwreset; 135 sc->sc_ie.chan_attn = ei_attn; 136 137 sc->sc_ie.memcopyin = ei_copyin; 138 sc->sc_ie.memcopyout = ei_copyout; 139 140 sc->sc_ie.ie_bus_barrier = NULL; 141 sc->sc_ie.ie_bus_read16 = ei_read16; 142 sc->sc_ie.ie_bus_write16 = ei_write16; 143 sc->sc_ie.ie_bus_write24 = ei_write24; 144 145 sc->sc_ie.do_xmitnopchain = 0; /* Does it listen? */ 146 147 sc->sc_ie.sc_mediachange = NULL; 148 sc->sc_ie.sc_mediastatus = NULL; 149 150 sc->sc_ie.bt = sc->sc_mem_t; /* XXX hope it doesn't use them */ 151 sc->sc_ie.bh = sc->sc_mem_h; 152 153 printf(":"); 154 155 /* All Ether1s are 64k (I believe) */ 156 sc->sc_ie.sc_msize = EI_MEMSIZE; 157 158 /* set up pointers to important on-card control structures */ 159 sc->sc_ie.iscp = 0; 160 sc->sc_ie.scb = IE_ISCP_SZ; 161 sc->sc_ie.scp = sc->sc_ie.sc_msize + IE_SCP_ADDR - (1 << 24); 162 163 sc->sc_ie.buf_area = sc->sc_ie.scb + IE_SCB_SZ; 164 sc->sc_ie.buf_area_sz = 165 sc->sc_ie.sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ; 166 167 /* Wipe the whole of the card's memory */ 168 for (i = 0; i < EI_NPAGES; i++) { 169 ei_setpage(sc, i); 170 bus_space_set_region_2(sc->sc_mem_t, sc->sc_mem_h, 171 0, 0,EI_PAGESIZE / 2); 172 } 173 174 /* set up pointers to key structures */ 175 ei_write24(&sc->sc_ie, IE_SCP_ISCP((u_long)sc->sc_ie.scp), 176 (u_long) sc->sc_ie.iscp); 177 ei_write16(&sc->sc_ie, IE_ISCP_SCB((u_long)sc->sc_ie.iscp), 178 (u_long) sc->sc_ie.scb); 179 ei_write24(&sc->sc_ie, IE_ISCP_BASE((u_long)sc->sc_ie.iscp), 180 (u_long) sc->sc_ie.iscp); 181 182 /* check if chip answers */ 183 if (i82586_proberam(&sc->sc_ie) == 0) { 184 printf(" memory probe failed\n"); 185 return; 186 } 187 188 /* Read ROM */ 189 bus_space_read_region_1(sc->sc_rom_t, sc->sc_rom_h, 0, 190 sc->sc_idrom, EI_ROMSIZE); 191 192 snprintf(descr, 15, "AKA25 iss. %d", sc->sc_idrom[EI_ROM_HWREV]); 193 i82586_attach(&sc->sc_ie, descr, sc->sc_idrom + EI_ROM_EADDR, 194 NULL, 0, 0); 195 196 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 197 self->dv_xname, "intr"); 198 sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_NET, i82586_intr, 199 self, &sc->sc_intrcnt); 200 ei_cli(sc); 201 } 202 203 static void 204 ei_hwreset(struct ie_softc *sc_ie, int why) 205 { 206 struct ei_softc *sc = (struct ei_softc *)sc_ie; 207 208 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_RST); 209 DELAY(1000); 210 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, 0); 211 DELAY(1000); 212 ei_cli(sc); 213 } 214 215 static int 216 ei_intrhook(struct ie_softc *sc_ie, int why) 217 { 218 struct ei_softc *sc = (struct ei_softc *)sc_ie; 219 220 switch (why) { 221 case INTR_EXIT: 222 case INTR_ACK: 223 ei_cli(sc); 224 break; 225 } 226 return 0; 227 } 228 229 static void 230 ei_attn(struct ie_softc *sc_ie, int why) 231 { 232 struct ei_softc *sc = (void *)sc_ie; 233 234 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_CA); 235 } 236 237 /* 238 * Various access functions to allow the MI 82586 driver to get at the 239 * board's memory. All memory accesses must go through these, as 240 * nothing else knows how to manipulate the page register. Note that 241 * all addresses and lengths passed in are in bytes of actual data. 242 * The bus_space functions deal in words, though, so we have to 243 * convert on the way through. 244 */ 245 246 static void 247 ei_copyin(struct ie_softc *sc_ie, void *dest, int src, size_t size) 248 { 249 struct ei_softc *sc = (struct ei_softc *)sc_ie; 250 int cnt, s, extra_byte; 251 u_int16_t *wptr; 252 253 #ifdef DIAGNOSTIC 254 if (src % 2 != 0 || !ALIGNED_POINTER(dest, u_int16_t)) 255 panic("%s: unaligned copyin", sc_ie->sc_dev.dv_xname); 256 #endif 257 wptr = dest; 258 extra_byte = size % 2; 259 size -= extra_byte; 260 while (size > 0) { 261 cnt = EI_PAGESIZE - src % EI_PAGESIZE; 262 if (cnt > size) 263 cnt = size; 264 s = splnet(); 265 ei_setpage(sc, ei_atop(src)); 266 /* bus ops are in words */ 267 bus_space_read_region_2(sc->sc_mem_t, sc->sc_mem_h, 268 ei_atopo(src) / 2, wptr, cnt / 2); 269 splx(s); 270 src += cnt; 271 wptr += cnt / 2; 272 size -= cnt; 273 } 274 if (extra_byte) { 275 /* Do we need to be this careful? */ 276 s = splnet(); 277 ei_setpage(sc, ei_atop(src)); 278 *(u_int8_t *)wptr = 279 bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h, 280 ei_atopo(src) / 2) & 0xff; 281 splx(s); 282 } 283 } 284 285 static void 286 ei_copyout(struct ie_softc *sc_ie, const void *src, int dest, size_t size) 287 { 288 struct ei_softc *sc = (struct ei_softc *)sc_ie; 289 int cnt, s; 290 const u_int16_t *wptr; 291 u_int16_t *bounce = NULL; 292 293 #ifdef DIAGNOSTIC 294 if (dest % 2 != 0) 295 panic("%s: unaligned copyout", sc_ie->sc_dev.dv_xname); 296 #endif 297 if (!ALIGNED_POINTER(src, u_int16_t)) { 298 MALLOC(bounce, u_int16_t *, size, M_DEVBUF, M_NOWAIT); 299 if (bounce == NULL) 300 panic("%s: no memory to align copyout", 301 sc_ie->sc_dev.dv_xname); 302 memcpy(bounce, src, size); 303 src = bounce; 304 } 305 wptr = src; 306 if (size % 2 != 0) 307 size++; /* This is safe, since the buffer is 16bit aligned */ 308 while (size > 0) { 309 cnt = EI_PAGESIZE - dest % EI_PAGESIZE; 310 if (cnt > size) 311 cnt = size; 312 s = splnet(); 313 ei_setpage(sc, ei_atop(dest)); 314 /* bus ops are in words */ 315 bus_space_write_region_2(sc->sc_mem_t, sc->sc_mem_h, 316 ei_atopo(dest) / 2, wptr, cnt / 2); 317 splx(s); 318 wptr += cnt / 2; 319 dest += cnt; 320 size -= cnt; 321 } 322 if (bounce != NULL) 323 FREE(bounce, M_DEVBUF); 324 } 325 326 static u_int16_t 327 ei_read16(struct ie_softc *sc_ie, int addr) 328 { 329 struct ei_softc *sc = (struct ei_softc *)sc_ie; 330 int result, s; 331 332 #ifdef DIAGNOSTIC 333 if (addr % 2 != 0) 334 panic("%s: unaligned read16", sc_ie->sc_dev.dv_xname); 335 #endif 336 s = splnet(); 337 ei_setpage(sc, ei_atop(addr)); 338 result = bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h, 339 ei_atopo(addr) / 2); 340 splx(s); 341 return result; 342 } 343 344 static void 345 ei_write16(struct ie_softc *sc_ie, int addr, u_int16_t value) 346 { 347 struct ei_softc *sc = (struct ei_softc *)sc_ie; 348 int s; 349 350 #ifdef DIAGNOSTIC 351 if (addr % 2 != 0) 352 panic("%s: unaligned write16", sc_ie->sc_dev.dv_xname); 353 #endif 354 s = splnet(); 355 ei_setpage(sc, ei_atop(addr)); 356 bus_space_write_2(sc->sc_mem_t, sc->sc_mem_h, ei_atopo(addr) / 2, 357 value); 358 splx(s); 359 } 360 361 static void 362 ei_write24(struct ie_softc *sc_ie, int addr, int value) 363 { 364 int s; 365 366 #ifdef DIAGNOSTIC 367 if (addr % 2 != 0) 368 panic("%s: unaligned write24", sc_ie->sc_dev.dv_xname); 369 #endif 370 s = splnet(); 371 ei_write16(sc_ie, addr, value & 0xffff); 372 ei_write16(sc_ie, addr + 2, (value >> 16) & 0xff); 373 splx(s); 374 } 375 376