1 /* $NetBSD: if_ei.c,v 1.5 2002/05/22 22:43:14 bjh21 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.5 2002/05/22 22:43:14 bjh21 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 struct cfattach ei_ca = { 89 sizeof(struct ei_softc), ei_match, ei_attach 90 }; 91 92 static inline void 93 ei_setpage(struct ei_softc *sc, int page) 94 { 95 96 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_PAGE, page); 97 } 98 99 static inline void 100 ei_cli(struct ei_softc *sc) 101 { 102 103 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_CLI); 104 } 105 106 static int 107 ei_match(struct device *parent, struct cfdata *cf, void *aux) 108 { 109 struct podulebus_attach_args *pa = aux; 110 111 return pa->pa_product == PODULE_ETHER1; 112 } 113 114 static void 115 ei_attach(struct device *parent, struct device *self, void *aux) 116 { 117 struct podulebus_attach_args *pa = aux; 118 struct ei_softc *sc = (struct ei_softc *)self; 119 int i; 120 char descr[16]; 121 122 /* Set up bus spaces */ 123 sc->sc_ctl_t = sc->sc_mem_t = pa->pa_fast_t; 124 bus_space_map(pa->pa_fast_t, pa->pa_fast_base, EI_MEMOFF, 0, 125 &sc->sc_ctl_h); 126 bus_space_map(pa->pa_fast_t, pa->pa_fast_base + EI_MEMOFF, 127 EI_PAGESIZE * 2, 0, &sc->sc_mem_h); 128 sc->sc_rom_t = pa->pa_sync_t; 129 bus_space_map(pa->pa_sync_t, pa->pa_sync_base, EI_ROMSIZE, 0, 130 &sc->sc_rom_h); 131 132 /* Set up callbacks */ 133 sc->sc_ie.hwinit = NULL; 134 sc->sc_ie.intrhook = ei_intrhook; 135 sc->sc_ie.hwreset = ei_hwreset; 136 sc->sc_ie.chan_attn = ei_attn; 137 138 sc->sc_ie.memcopyin = ei_copyin; 139 sc->sc_ie.memcopyout = ei_copyout; 140 141 sc->sc_ie.ie_bus_barrier = NULL; 142 sc->sc_ie.ie_bus_read16 = ei_read16; 143 sc->sc_ie.ie_bus_write16 = ei_write16; 144 sc->sc_ie.ie_bus_write24 = ei_write24; 145 146 sc->sc_ie.do_xmitnopchain = 0; /* Does it listen? */ 147 148 sc->sc_ie.sc_mediachange = NULL; 149 sc->sc_ie.sc_mediastatus = NULL; 150 151 sc->sc_ie.bt = sc->sc_mem_t; /* XXX hope it doesn't use them */ 152 sc->sc_ie.bh = sc->sc_mem_h; 153 154 printf(":"); 155 156 /* All Ether1s are 64k (I believe) */ 157 sc->sc_ie.sc_msize = EI_MEMSIZE; 158 159 /* set up pointers to important on-card control structures */ 160 sc->sc_ie.iscp = 0; 161 sc->sc_ie.scb = IE_ISCP_SZ; 162 sc->sc_ie.scp = sc->sc_ie.sc_msize + IE_SCP_ADDR - (1 << 24); 163 164 sc->sc_ie.buf_area = sc->sc_ie.scb + IE_SCB_SZ; 165 sc->sc_ie.buf_area_sz = 166 sc->sc_ie.sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ; 167 168 /* Wipe the whole of the card's memory */ 169 for (i = 0; i < EI_NPAGES; i++) { 170 ei_setpage(sc, i); 171 bus_space_set_region_2(sc->sc_mem_t, sc->sc_mem_h, 172 0, 0,EI_PAGESIZE / 2); 173 } 174 175 /* set up pointers to key structures */ 176 ei_write24(&sc->sc_ie, IE_SCP_ISCP((u_long)sc->sc_ie.scp), 177 (u_long) sc->sc_ie.iscp); 178 ei_write16(&sc->sc_ie, IE_ISCP_SCB((u_long)sc->sc_ie.iscp), 179 (u_long) sc->sc_ie.scb); 180 ei_write24(&sc->sc_ie, IE_ISCP_BASE((u_long)sc->sc_ie.iscp), 181 (u_long) sc->sc_ie.iscp); 182 183 /* check if chip answers */ 184 if (i82586_proberam(&sc->sc_ie) == 0) { 185 printf(" memory probe failed\n"); 186 return; 187 } 188 189 /* Read ROM */ 190 bus_space_read_region_1(sc->sc_rom_t, sc->sc_rom_h, 0, 191 sc->sc_idrom, EI_ROMSIZE); 192 193 snprintf(descr, 15, "AKA25 iss. %d", sc->sc_idrom[EI_ROM_HWREV]); 194 i82586_attach(&sc->sc_ie, descr, sc->sc_idrom + EI_ROM_EADDR, 195 NULL, 0, 0); 196 197 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 198 self->dv_xname, "intr"); 199 sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_NET, i82586_intr, 200 self, &sc->sc_intrcnt); 201 ei_cli(sc); 202 } 203 204 static void 205 ei_hwreset(struct ie_softc *sc_ie, int why) 206 { 207 struct ei_softc *sc = (struct ei_softc *)sc_ie; 208 209 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_RST); 210 DELAY(1000); 211 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, 0); 212 DELAY(1000); 213 ei_cli(sc); 214 } 215 216 static int 217 ei_intrhook(struct ie_softc *sc_ie, int why) 218 { 219 struct ei_softc *sc = (struct ei_softc *)sc_ie; 220 221 switch (why) { 222 case INTR_EXIT: 223 case INTR_ACK: 224 ei_cli(sc); 225 break; 226 } 227 return 0; 228 } 229 230 static void 231 ei_attn(struct ie_softc *sc_ie, int why) 232 { 233 struct ei_softc *sc = (void *)sc_ie; 234 235 bus_space_write_1(sc->sc_ctl_t, sc->sc_ctl_h, EI_CONTROL, EI_CTL_CA); 236 } 237 238 /* 239 * Various access functions to allow the MI 82586 driver to get at the 240 * board's memory. All memory accesses must go through these, as 241 * nothing else knows how to manipulate the page register. Note that 242 * all addresses and lengths passed in are in bytes of actual data. 243 * The bus_space functions deal in words, though, so we have to 244 * convert on the way through. 245 */ 246 247 static void 248 ei_copyin(struct ie_softc *sc_ie, void *dest, int src, size_t size) 249 { 250 struct ei_softc *sc = (struct ei_softc *)sc_ie; 251 int cnt, s, extra_byte; 252 u_int16_t *wptr; 253 254 #ifdef DIAGNOSTIC 255 if (src % 2 != 0 || !ALIGNED_POINTER(dest, u_int16_t)) 256 panic("%s: unaligned copyin", sc_ie->sc_dev.dv_xname); 257 #endif 258 wptr = dest; 259 extra_byte = size % 2; 260 size -= extra_byte; 261 while (size > 0) { 262 cnt = EI_PAGESIZE - src % EI_PAGESIZE; 263 if (cnt > size) 264 cnt = size; 265 s = splnet(); 266 ei_setpage(sc, ei_atop(src)); 267 /* bus ops are in words */ 268 bus_space_read_region_2(sc->sc_mem_t, sc->sc_mem_h, 269 ei_atopo(src) / 2, wptr, cnt / 2); 270 splx(s); 271 src += cnt; 272 wptr += cnt / 2; 273 size -= cnt; 274 } 275 if (extra_byte) { 276 /* Do we need to be this careful? */ 277 s = splnet(); 278 ei_setpage(sc, ei_atop(src)); 279 *(u_int8_t *)wptr = 280 bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h, 281 ei_atopo(src) / 2) & 0xff; 282 splx(s); 283 } 284 } 285 286 static void 287 ei_copyout(struct ie_softc *sc_ie, const void *src, int dest, size_t size) 288 { 289 struct ei_softc *sc = (struct ei_softc *)sc_ie; 290 int cnt, s; 291 const u_int16_t *wptr; 292 u_int16_t *bounce = NULL; 293 294 #ifdef DIAGNOSTIC 295 if (dest % 2 != 0) 296 panic("%s: unaligned copyout", sc_ie->sc_dev.dv_xname); 297 #endif 298 if (!ALIGNED_POINTER(src, u_int16_t)) { 299 MALLOC(bounce, u_int16_t *, size, M_DEVBUF, M_NOWAIT); 300 if (bounce == NULL) 301 panic("%s: no memory to align copyout", 302 sc_ie->sc_dev.dv_xname); 303 memcpy(bounce, src, size); 304 src = bounce; 305 } 306 wptr = src; 307 if (size % 2 != 0) 308 size++; /* This is safe, since the buffer is 16bit aligned */ 309 while (size > 0) { 310 cnt = EI_PAGESIZE - dest % EI_PAGESIZE; 311 if (cnt > size) 312 cnt = size; 313 s = splnet(); 314 ei_setpage(sc, ei_atop(dest)); 315 /* bus ops are in words */ 316 bus_space_write_region_2(sc->sc_mem_t, sc->sc_mem_h, 317 ei_atopo(dest) / 2, wptr, cnt / 2); 318 splx(s); 319 wptr += cnt / 2; 320 dest += cnt; 321 size -= cnt; 322 } 323 if (bounce != NULL) 324 FREE(bounce, M_DEVBUF); 325 } 326 327 static u_int16_t 328 ei_read16(struct ie_softc *sc_ie, int addr) 329 { 330 struct ei_softc *sc = (struct ei_softc *)sc_ie; 331 int result, s; 332 333 #ifdef DIAGNOSTIC 334 if (addr % 2 != 0) 335 panic("%s: unaligned read16", sc_ie->sc_dev.dv_xname); 336 #endif 337 s = splnet(); 338 ei_setpage(sc, ei_atop(addr)); 339 result = bus_space_read_2(sc->sc_mem_t, sc->sc_mem_h, 340 ei_atopo(addr) / 2); 341 splx(s); 342 return result; 343 } 344 345 static void 346 ei_write16(struct ie_softc *sc_ie, int addr, u_int16_t value) 347 { 348 struct ei_softc *sc = (struct ei_softc *)sc_ie; 349 int s; 350 351 #ifdef DIAGNOSTIC 352 if (addr % 2 != 0) 353 panic("%s: unaligned write16", sc_ie->sc_dev.dv_xname); 354 #endif 355 s = splnet(); 356 ei_setpage(sc, ei_atop(addr)); 357 bus_space_write_2(sc->sc_mem_t, sc->sc_mem_h, ei_atopo(addr) / 2, 358 value); 359 splx(s); 360 } 361 362 static void 363 ei_write24(struct ie_softc *sc_ie, int addr, int value) 364 { 365 int s; 366 367 #ifdef DIAGNOSTIC 368 if (addr % 2 != 0) 369 panic("%s: unaligned write24", sc_ie->sc_dev.dv_xname); 370 #endif 371 s = splnet(); 372 ei_write16(sc_ie, addr, value & 0xffff); 373 ei_write16(sc_ie, addr + 2, (value >> 16) & 0xff); 374 splx(s); 375 } 376 377