1 /* $NetBSD: if_ai.c,v 1.11 2001/07/18 20:39:53 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Rafal K. Boni. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/mbuf.h> 42 #include <sys/errno.h> 43 #include <sys/device.h> 44 #include <sys/protosw.h> 45 #include <sys/socket.h> 46 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_types.h> 50 #include <net/if_media.h> 51 #include <net/if_ether.h> 52 53 #include <machine/cpu.h> 54 #include <machine/bus.h> 55 #include <machine/intr.h> 56 57 #include <dev/isa/isareg.h> 58 #include <dev/isa/isavar.h> 59 60 #include <dev/ic/i82586reg.h> 61 #include <dev/ic/i82586var.h> 62 #include <dev/isa/if_aireg.h> 63 64 #ifdef AI_DEBUG 65 #define DPRINTF(x) printf x 66 #else 67 #define DPRINTF(x) 68 #endif 69 70 struct ai_softc { 71 struct ie_softc sc_ie; 72 73 bus_space_tag_t sc_regt; /* space tag for registers */ 74 bus_space_handle_t sc_regh; /* space handle for registers */ 75 76 u_int8_t card_rev; 77 u_int8_t card_type; 78 79 void *sc_ih; /* interrupt handle */ 80 }; 81 82 const char *ai_names[] = { 83 "StarLAN 10", 84 "EN100", 85 "StarLAN Fiber", 86 }; 87 88 /* Functions required by the i82586 MI driver */ 89 static void ai_reset __P((struct ie_softc *, int)); 90 static void ai_atten __P((struct ie_softc *, int)); 91 92 static void ai_copyin __P((struct ie_softc *, void *, int, size_t)); 93 static void ai_copyout __P((struct ie_softc *, const void *, int, size_t)); 94 95 static u_int16_t ai_read_16 __P((struct ie_softc *, int)); 96 static void ai_write_16 __P((struct ie_softc *, int, u_int16_t)); 97 static void ai_write_24 __P((struct ie_softc *, int, int)); 98 99 /* Local support functions */ 100 static int check_ie_present __P((struct ie_softc*, bus_space_tag_t, 101 bus_space_handle_t, bus_size_t)); 102 static int ai_find_mem_size __P((struct ai_softc*, bus_space_tag_t, 103 bus_size_t)); 104 105 int ai_match __P((struct device *, struct cfdata *, void *)); 106 void ai_attach __P((struct device *, struct device *, void *)); 107 108 /* 109 * AT&T StarLan support routines 110 */ 111 static void 112 ai_reset(sc, why) 113 struct ie_softc *sc; 114 int why; 115 { 116 struct ai_softc* asc = (struct ai_softc *) sc; 117 118 switch (why) { 119 case CHIP_PROBE: 120 /* reset to chip to see if it responds */ 121 bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_RESET, 0); 122 DELAY(100); 123 break; 124 125 case CARD_RESET: 126 /* 127 * this takes around 10sec, and we can get 128 * by quite well w/out it... 129 */ 130 break; 131 } 132 } 133 134 static void 135 ai_atten(sc, why) 136 struct ie_softc *sc; 137 int why; 138 { 139 struct ai_softc* asc = (struct ai_softc *) sc; 140 bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_ATTN, 0); 141 } 142 143 static void 144 ai_copyin (sc, dst, offset, size) 145 struct ie_softc *sc; 146 void *dst; 147 int offset; 148 size_t size; 149 { 150 int dribble; 151 u_int8_t* bptr = dst; 152 153 bus_space_barrier(sc->bt, sc->bh, offset, size, 154 BUS_SPACE_BARRIER_READ); 155 156 if (offset % 2) { 157 *bptr = bus_space_read_1(sc->bt, sc->bh, offset); 158 offset++; bptr++; size--; 159 } 160 161 dribble = size % 2; 162 bus_space_read_region_2(sc->bt, sc->bh, offset, (u_int16_t *) bptr, 163 size >> 1); 164 165 if (dribble) { 166 bptr += size - 1; 167 offset += size - 1; 168 *bptr = bus_space_read_1(sc->bt, sc->bh, offset); 169 } 170 } 171 172 static void 173 ai_copyout (sc, src, offset, size) 174 struct ie_softc *sc; 175 const void *src; 176 int offset; 177 size_t size; 178 { 179 int dribble; 180 int osize = size; 181 int ooffset = offset; 182 const u_int8_t* bptr = src; 183 184 if (offset % 2) { 185 bus_space_write_1(sc->bt, sc->bh, offset, *bptr); 186 offset++; bptr++; size--; 187 } 188 189 dribble = size % 2; 190 bus_space_write_region_2(sc->bt, sc->bh, offset, (u_int16_t *)bptr, 191 size >> 1); 192 if (dribble) { 193 bptr += size - 1; 194 offset += size - 1; 195 bus_space_write_1(sc->bt, sc->bh, offset, *bptr); 196 } 197 198 bus_space_barrier(sc->bt, sc->bh, ooffset, osize, 199 BUS_SPACE_BARRIER_WRITE); 200 } 201 202 static u_int16_t 203 ai_read_16 (sc, offset) 204 struct ie_softc *sc; 205 int offset; 206 { 207 bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_READ); 208 return bus_space_read_2(sc->bt, sc->bh, offset); 209 } 210 211 static void 212 ai_write_16 (sc, offset, value) 213 struct ie_softc *sc; 214 int offset; 215 u_int16_t value; 216 { 217 bus_space_write_2(sc->bt, sc->bh, offset, value); 218 bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_WRITE); 219 } 220 221 static void 222 ai_write_24 (sc, offset, addr) 223 struct ie_softc *sc; 224 int offset, addr; 225 { 226 bus_space_write_4(sc->bt, sc->bh, offset, addr + 227 (u_long) sc->sc_maddr - (u_long) sc->sc_iobase); 228 bus_space_barrier(sc->bt, sc->bh, offset, 4, BUS_SPACE_BARRIER_WRITE); 229 } 230 231 int 232 ai_match(parent, cf, aux) 233 struct device *parent; 234 struct cfdata *cf; 235 void *aux; 236 { 237 int rv = 0; 238 u_int8_t val, type; 239 bus_size_t memsize; 240 bus_space_tag_t iot; 241 bus_space_handle_t ioh; 242 struct isa_attach_args * const ia = aux; 243 struct ai_softc asc; 244 245 246 /* Punt if wildcarded port, IRQ or memory address */ 247 if (ia->ia_irq == ISACF_IRQ_DEFAULT || 248 ia->ia_maddr == ISACF_IOMEM_DEFAULT || 249 ia->ia_iobase == ISACF_PORT_DEFAULT) { 250 DPRINTF(( 251 "ai_match: wildcarded IRQ, IOAddr, or memAddr, skipping\n")); 252 return (0); 253 } 254 255 iot = ia->ia_iot; 256 257 /* 258 * This probe is horribly bad, but I have no info on this card other 259 * than the former driver, and it was just as bad! 260 */ 261 if (bus_space_map(iot, ia->ia_iobase, 262 AI_IOSIZE, 0, &ioh) != 0) { 263 264 DPRINTF(("ai_match: cannot map %d IO ports @ 0x%x\n", 265 AI_IOSIZE, ia->ia_iobase)); 266 return (0); 267 } 268 269 val = bus_space_read_1(iot, ioh, AI_REVISION); 270 271 type = SL_BOARD(val); 272 if (type != SL10_BOARD && type != EN100_BOARD && 273 type != SLFIBER_BOARD) { 274 DPRINTF(("ai_match: unknown board code 0x%02x @ 0x%x\n", 275 type, ia->ia_iobase)); 276 goto out; 277 } 278 279 /* 280 * Fill in just about enough of our local `ai_softc' for 281 * ai_find_mem_size() to do its job. 282 */ 283 memset(&asc, 0, sizeof asc); 284 asc.sc_regt = iot; 285 asc.sc_regh = ioh; 286 287 if ((memsize = ai_find_mem_size(&asc,ia->ia_memt,ia->ia_maddr)) == 0) { 288 DPRINTF(("ai_match: cannot size memory of board @ 0x%x\n", 289 ia->ia_iobase)); 290 goto out; 291 } 292 293 if (!ia->ia_msize) 294 ia->ia_msize = memsize; 295 else if (ia->ia_msize != memsize) { 296 DPRINTF(( 297 "ai_match: memsize of board @ 0x%x doesn't match config\n", 298 ia->ia_iobase)); 299 goto out; 300 } 301 302 rv = 1; 303 ia->ia_msize = memsize; 304 ia->ia_iosize = AI_IOSIZE; 305 DPRINTF(("ai_match: found board @ 0x%x\n", ia->ia_iobase)); 306 307 out: 308 bus_space_unmap(iot, ioh, AI_IOSIZE); 309 return rv; 310 } 311 312 void 313 ai_attach(parent, self, aux) 314 struct device *parent; 315 struct device *self; 316 void *aux; 317 { 318 struct ai_softc *asc = (void *)self; 319 struct ie_softc *sc = &asc->sc_ie; 320 struct isa_attach_args *ia = aux; 321 322 u_int8_t val = 0; 323 bus_space_handle_t ioh, memh; 324 u_int8_t ethaddr[ETHER_ADDR_LEN]; 325 char name[80]; 326 327 if (bus_space_map(ia->ia_iot, ia->ia_iobase, 328 ia->ia_iosize, 0, &ioh) != 0) { 329 DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n", 330 sc->sc_dev.dv_xname, 331 ia->ia_iobase, ia->ia_iobase + ia->ia_iosize - 1)); 332 return; 333 } 334 335 if (bus_space_map(ia->ia_memt, ia->ia_maddr, 336 ia->ia_msize, 0, &memh) != 0) { 337 DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n", 338 sc->sc_dev.dv_xname, 339 ia->ia_maddr, ia->ia_maddr + ia->ia_msize - 1)); 340 bus_space_unmap(ia->ia_iot, ioh, ia->ia_iosize); 341 return; 342 } 343 344 asc->sc_regt = ia->ia_iot; 345 asc->sc_regh = ioh; 346 347 sc->hwinit = NULL; 348 sc->intrhook = NULL; 349 sc->hwreset = ai_reset; 350 sc->chan_attn = ai_atten; 351 352 sc->ie_bus_barrier = NULL; 353 354 sc->memcopyin = ai_copyin; 355 sc->memcopyout = ai_copyout; 356 sc->ie_bus_read16 = ai_read_16; 357 sc->ie_bus_write16 = ai_write_16; 358 sc->ie_bus_write24 = ai_write_24; 359 360 sc->do_xmitnopchain = 0; 361 362 sc->sc_mediachange = NULL; 363 sc->sc_mediastatus = NULL; 364 365 sc->bt = ia->ia_memt; 366 sc->bh = memh; 367 368 /* Map i/o space. */ 369 sc->sc_msize = ia->ia_msize; 370 sc->sc_maddr = (void *)memh; 371 sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24); 372 373 /* set up pointers to important on-card control structures */ 374 sc->iscp = 0; 375 sc->scb = IE_ISCP_SZ; 376 sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24); 377 378 sc->buf_area = sc->scb + IE_SCB_SZ; 379 sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ; 380 381 /* zero card memory */ 382 bus_space_set_region_1(sc->bt, sc->bh, 0, 0, sc->sc_msize); 383 384 /* set card to 16-bit bus mode */ 385 bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp), 0); 386 387 /* set up pointers to key structures */ 388 ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp); 389 ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb); 390 ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp); 391 392 /* flush setup of pointers, check if chip answers */ 393 bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize, 394 BUS_SPACE_BARRIER_WRITE); 395 if (!i82586_proberam(sc)) { 396 DPRINTF(("\n%s: can't talk to i82586!\n", 397 sc->sc_dev.dv_xname)); 398 bus_space_unmap(ia->ia_iot, ioh, ia->ia_iosize); 399 bus_space_unmap(ia->ia_memt, memh, ia->ia_msize); 400 return; 401 } 402 403 val = bus_space_read_1(asc->sc_regt, asc->sc_regh, AI_REVISION); 404 asc->card_rev = SL_REV(val); 405 asc->card_type = SL_BOARD(val) - 1; 406 sprintf(name, "%s, rev. %d", 407 ai_names[asc->card_type], asc->card_rev); 408 409 i82586_attach(sc, name, ethaddr, NULL, 0, 0); 410 411 asc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 412 IPL_NET, i82586_intr, sc); 413 if (asc->sc_ih == NULL) { 414 DPRINTF(("\n%s: can't establish interrupt\n", 415 sc->sc_dev.dv_xname)); 416 } 417 } 418 419 /* 420 * Divine the memory size of this board. 421 * Better hope there's nothing important hiding just below the card... 422 */ 423 static int 424 ai_find_mem_size(asc, memt, maddr) 425 struct ai_softc* asc; 426 bus_space_tag_t memt; 427 bus_size_t maddr; 428 { 429 int size; 430 bus_space_handle_t memh; 431 struct ie_softc* sc = &asc->sc_ie; 432 433 for (size = 65536; size >= 16384; size -= 16384) { 434 if (bus_space_map(memt, maddr, size, 0, &memh) == 0) { 435 size = check_ie_present(sc, memt, maddr, size); 436 bus_space_unmap(memt, memh, size); 437 438 if (size != 0) 439 return size; 440 } 441 } 442 443 return (0); 444 } 445 446 /* 447 * Check to see if there's an 82586 out there. 448 */ 449 static int 450 check_ie_present(sc, memt, memh, size) 451 struct ie_softc* sc; 452 bus_space_tag_t memt; 453 bus_space_handle_t memh; 454 bus_size_t size; 455 { 456 sc->hwreset = ai_reset; 457 sc->chan_attn = ai_atten; 458 sc->ie_bus_read16 = ai_read_16; 459 sc->ie_bus_write16 = ai_write_16; 460 461 sc->bt = memt; 462 sc->bh = memh; 463 sc->sc_iobase = (char *)memh + size - (1 << 24); 464 465 sc->scp = size + IE_SCP_ADDR - (1 << 24); 466 bus_space_set_region_1(memt, memh, (u_long) sc->scp, 0, IE_SCP_SZ); 467 468 sc->iscp = 0; 469 bus_space_set_region_1(memt, memh, (u_long) sc->iscp, 0, IE_ISCP_SZ); 470 471 sc->scb = IE_ISCP_SZ; 472 bus_space_set_region_1(memt, memh, sc->scb, 0, IE_SCB_SZ); 473 474 /* set card to 16-bit bus mode */ 475 bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp), 0); 476 477 /* set up pointers to key structures */ 478 ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp); 479 ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb); 480 ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp); 481 482 /* flush setup of pointers, check if chip answers */ 483 bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize, 484 BUS_SPACE_BARRIER_WRITE); 485 486 if (!i82586_proberam(sc)) 487 return (0); 488 489 return (size); 490 } 491 492 struct cfattach ai_ca = { 493 sizeof(struct ai_softc), ai_match, ai_attach 494 }; 495