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