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