Home | History | Annotate | Line # | Download | only in isa
if_ai.c revision 1.13
      1 /*	$NetBSD: if_ai.c,v 1.13 2001/11/26 23:31:00 fredette 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.13 2001/11/26 23:31:00 fredette 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 <machine/cpu.h>
     57 #include <machine/bus.h>
     58 #include <machine/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 __P((struct ie_softc *, int));
     93 static void 	ai_atten __P((struct ie_softc *, int));
     94 
     95 static void	ai_copyin __P((struct ie_softc *, void *, int, size_t));
     96 static void	ai_copyout __P((struct ie_softc *, const void *, int, size_t));
     97 
     98 static u_int16_t ai_read_16 __P((struct ie_softc *, int));
     99 static void	ai_write_16 __P((struct ie_softc *, int, u_int16_t));
    100 static void	ai_write_24 __P((struct ie_softc *, int, int));
    101 
    102 /* Local support functions */
    103 static int 	check_ie_present __P((struct ie_softc*, bus_space_tag_t,
    104 					bus_space_handle_t, bus_size_t));
    105 static int	ai_find_mem_size __P((struct ai_softc*, bus_space_tag_t,
    106 					bus_size_t));
    107 
    108 int ai_match __P((struct device *, struct cfdata *, void *));
    109 void ai_attach __P((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(sc, why)
    139 	struct ie_softc *sc;
    140 	int why;
    141 {
    142     struct ai_softc* asc = (struct ai_softc *) sc;
    143     bus_space_write_1(asc->sc_regt, asc->sc_regh, AI_ATTN, 0);
    144 }
    145 
    146 static void
    147 ai_copyin (sc, dst, offset, size)
    148         struct ie_softc *sc;
    149         void *dst;
    150         int offset;
    151         size_t size;
    152 {
    153 	int dribble;
    154 	u_int8_t* bptr = dst;
    155 
    156 	bus_space_barrier(sc->bt, sc->bh, offset, size,
    157 			  BUS_SPACE_BARRIER_READ);
    158 
    159 	if (offset % 2) {
    160 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
    161 		offset++; bptr++; size--;
    162 	}
    163 
    164 	dribble = size % 2;
    165 	bus_space_read_region_2(sc->bt, sc->bh, offset, (u_int16_t *) bptr,
    166 				size >> 1);
    167 
    168 	if (dribble) {
    169 		bptr += size - 1;
    170 		offset += size - 1;
    171 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
    172 	}
    173 }
    174 
    175 static void
    176 ai_copyout (sc, src, offset, size)
    177         struct ie_softc *sc;
    178         const void *src;
    179         int offset;
    180         size_t size;
    181 {
    182 	int dribble;
    183 	int osize = size;
    184 	int ooffset = offset;
    185 	const u_int8_t* bptr = src;
    186 
    187 	if (offset % 2) {
    188 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
    189 		offset++; bptr++; size--;
    190 	}
    191 
    192 	dribble = size % 2;
    193 	bus_space_write_region_2(sc->bt, sc->bh, offset, (u_int16_t *)bptr,
    194 				 size >> 1);
    195 	if (dribble) {
    196 		bptr += size - 1;
    197 		offset += size - 1;
    198 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
    199 	}
    200 
    201 	bus_space_barrier(sc->bt, sc->bh, ooffset, osize,
    202 			  BUS_SPACE_BARRIER_WRITE);
    203 }
    204 
    205 static u_int16_t
    206 ai_read_16 (sc, offset)
    207         struct ie_softc *sc;
    208         int offset;
    209 {
    210 	bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_READ);
    211         return bus_space_read_2(sc->bt, sc->bh, offset);
    212 }
    213 
    214 static void
    215 ai_write_16 (sc, offset, value)
    216         struct ie_softc *sc;
    217         int offset;
    218         u_int16_t value;
    219 {
    220         bus_space_write_2(sc->bt, sc->bh, offset, value);
    221 	bus_space_barrier(sc->bt, sc->bh, offset, 2, BUS_SPACE_BARRIER_WRITE);
    222 }
    223 
    224 static void
    225 ai_write_24 (sc, offset, addr)
    226         struct ie_softc *sc;
    227         int offset, addr;
    228 {
    229         bus_space_write_4(sc->bt, sc->bh, offset, addr +
    230                                 (u_long) sc->sc_maddr - (u_long) sc->sc_iobase);
    231 	bus_space_barrier(sc->bt, sc->bh, offset, 4, BUS_SPACE_BARRIER_WRITE);
    232 }
    233 
    234 int
    235 ai_match(parent, cf, aux)
    236 	struct device *parent;
    237 	struct cfdata *cf;
    238 	void *aux;
    239 {
    240 	int rv = 0;
    241 	u_int8_t val, type;
    242 	bus_size_t memsize;
    243 	bus_space_tag_t iot;
    244 	bus_space_handle_t ioh;
    245 	struct isa_attach_args * const ia = aux;
    246 	struct ai_softc asc;
    247 
    248 
    249 	/* Punt if wildcarded port, IRQ or memory address */
    250 	if (ia->ia_irq == ISACF_IRQ_DEFAULT ||
    251 	    ia->ia_maddr == ISACF_IOMEM_DEFAULT  ||
    252             ia->ia_iobase == ISACF_PORT_DEFAULT) {
    253 		DPRINTF((
    254 		 "ai_match: wildcarded IRQ, IOAddr, or memAddr, skipping\n"));
    255 		return (0);
    256 	}
    257 
    258 	iot = ia->ia_iot;
    259 
    260 	/*
    261 	 * This probe is horribly bad, but I have no info on this card other
    262 	 * than the former driver, and it was just as bad!
    263 	 */
    264 	if (bus_space_map(iot, ia->ia_iobase,
    265 			  AI_IOSIZE, 0, &ioh) != 0) {
    266 
    267 		DPRINTF(("ai_match: cannot map %d IO ports @ 0x%x\n",
    268 			 AI_IOSIZE, ia->ia_iobase));
    269 		return (0);
    270 	}
    271 
    272 	val = bus_space_read_1(iot, ioh, AI_REVISION);
    273 
    274 	type = SL_BOARD(val);
    275 	if (type != SL10_BOARD && type != EN100_BOARD &&
    276 	    type != SLFIBER_BOARD) {
    277 		DPRINTF(("ai_match: unknown board code 0x%02x @ 0x%x\n",
    278 			 type, ia->ia_iobase));
    279 		goto out;
    280 	}
    281 
    282 	/*
    283 	 * Fill in just about enough of our local `ai_softc' for
    284 	 * ai_find_mem_size() to do its job.
    285 	 */
    286 	memset(&asc, 0, sizeof asc);
    287 	asc.sc_regt = iot;
    288 	asc.sc_regh = ioh;
    289 
    290 	if ((memsize = ai_find_mem_size(&asc,ia->ia_memt,ia->ia_maddr)) == 0) {
    291 		DPRINTF(("ai_match: cannot size memory of board @ 0x%x\n",
    292 			 ia->ia_iobase));
    293 		goto out;
    294 	}
    295 
    296 	if (!ia->ia_msize)
    297 		ia->ia_msize = memsize;
    298 	else if (ia->ia_msize != memsize) {
    299 		DPRINTF((
    300 		   "ai_match: memsize of board @ 0x%x doesn't match config\n",
    301 		   ia->ia_iobase));
    302 		goto out;
    303 	}
    304 
    305 	rv = 1;
    306 	ia->ia_msize = memsize;
    307 	ia->ia_iosize = AI_IOSIZE;
    308 	DPRINTF(("ai_match: found board @ 0x%x\n", ia->ia_iobase));
    309 
    310 out:
    311 	bus_space_unmap(iot, ioh, AI_IOSIZE);
    312 	return rv;
    313 }
    314 
    315 void
    316 ai_attach(parent, self, aux)
    317 	struct device *parent;
    318 	struct device *self;
    319 	void   *aux;
    320 {
    321 	struct ai_softc *asc = (void *)self;
    322 	struct ie_softc *sc = &asc->sc_ie;
    323 	struct isa_attach_args *ia = aux;
    324 
    325 	u_int8_t val = 0;
    326 	bus_space_handle_t ioh, memh;
    327 	u_int8_t ethaddr[ETHER_ADDR_LEN];
    328 	char name[80];
    329 
    330 	if (bus_space_map(ia->ia_iot, ia->ia_iobase,
    331 			  ia->ia_iosize, 0, &ioh) != 0) {
    332 		DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n",
    333 			 sc->sc_dev.dv_xname,
    334 		         ia->ia_iobase, ia->ia_iobase + ia->ia_iosize - 1));
    335 		return;
    336 	}
    337 
    338 	if (bus_space_map(ia->ia_memt, ia->ia_maddr,
    339 			  ia->ia_msize, 0, &memh) != 0) {
    340 		DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n",
    341 			 sc->sc_dev.dv_xname,
    342 			 ia->ia_maddr, ia->ia_maddr + ia->ia_msize - 1));
    343 		bus_space_unmap(ia->ia_iot, ioh, ia->ia_iosize);
    344 		return;
    345 	}
    346 
    347 	asc->sc_regt = ia->ia_iot;
    348 	asc->sc_regh = ioh;
    349 
    350 	sc->hwinit = NULL;
    351 	sc->intrhook = NULL;
    352 	sc->hwreset = ai_reset;
    353 	sc->chan_attn = ai_atten;
    354 
    355 	sc->ie_bus_barrier = NULL;
    356 
    357 	sc->memcopyin = ai_copyin;
    358 	sc->memcopyout = ai_copyout;
    359 	sc->ie_bus_read16 = ai_read_16;
    360 	sc->ie_bus_write16 = ai_write_16;
    361 	sc->ie_bus_write24 = ai_write_24;
    362 
    363 	sc->do_xmitnopchain = 0;
    364 
    365 	sc->sc_mediachange = NULL;
    366 	sc->sc_mediastatus = NULL;
    367 
    368 	sc->bt = ia->ia_memt;
    369 	sc->bh = memh;
    370 
    371 	/* Map i/o space. */
    372 	sc->sc_msize = ia->ia_msize;
    373 	sc->sc_maddr = (void *)memh;
    374 	sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24);
    375 
    376 	/* set up pointers to important on-card control structures */
    377 	sc->iscp = 0;
    378 	sc->scb = IE_ISCP_SZ;
    379 	sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24);
    380 
    381 	sc->buf_area = sc->scb + IE_SCB_SZ;
    382 	sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
    383 
    384 	/* zero card memory */
    385 	bus_space_set_region_1(sc->bt, sc->bh, 0, 0, sc->sc_msize);
    386 
    387 	/* set card to 16-bit bus mode */
    388 	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
    389 			  IE_SYSBUS_16BIT);
    390 
    391 	/* set up pointers to key structures */
    392 	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
    393 	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
    394 	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
    395 
    396 	/* flush setup of pointers, check if chip answers */
    397 	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
    398 			  BUS_SPACE_BARRIER_WRITE);
    399 	if (!i82586_proberam(sc)) {
    400 		DPRINTF(("\n%s: can't talk to i82586!\n",
    401 			sc->sc_dev.dv_xname));
    402 		bus_space_unmap(ia->ia_iot, ioh, ia->ia_iosize);
    403 		bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
    404 		return;
    405 	}
    406 
    407 	val = bus_space_read_1(asc->sc_regt, asc->sc_regh, AI_REVISION);
    408 	asc->card_rev = SL_REV(val);
    409 	asc->card_type = SL_BOARD(val) - 1;
    410 	sprintf(name, "%s, rev. %d",
    411 		ai_names[asc->card_type], asc->card_rev);
    412 
    413 	i82586_attach(sc, name, ethaddr, NULL, 0, 0);
    414 
    415 	asc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    416 					IPL_NET, i82586_intr, sc);
    417 	if (asc->sc_ih == NULL) {
    418 		DPRINTF(("\n%s: can't establish interrupt\n",
    419 			sc->sc_dev.dv_xname));
    420 	}
    421 }
    422 
    423 /*
    424  * Divine the memory size of this board.
    425  * Better hope there's nothing important hiding just below the card...
    426  */
    427 static int
    428 ai_find_mem_size(asc, memt, maddr)
    429 	struct ai_softc* asc;
    430 	bus_space_tag_t memt;
    431 	bus_size_t maddr;
    432 {
    433 	int size;
    434 	bus_space_handle_t memh;
    435 	struct ie_softc* sc = &asc->sc_ie;
    436 
    437 	for (size = 65536; size >= 16384; size -= 16384) {
    438 		if (bus_space_map(memt, maddr, size, 0, &memh) == 0) {
    439 			size = check_ie_present(sc, memt, maddr, size);
    440 			bus_space_unmap(memt, memh, size);
    441 
    442 			if (size != 0)
    443 				return size;
    444 		}
    445 	}
    446 
    447 	return (0);
    448 }
    449 
    450 /*
    451  * Check to see if there's an 82586 out there.
    452  */
    453 static int
    454 check_ie_present(sc, memt, memh, size)
    455 	struct ie_softc* sc;
    456 	bus_space_tag_t memt;
    457 	bus_space_handle_t memh;
    458 	bus_size_t size;
    459 {
    460 	sc->hwreset = ai_reset;
    461 	sc->chan_attn = ai_atten;
    462 	sc->ie_bus_read16 = ai_read_16;
    463 	sc->ie_bus_write16 = ai_write_16;
    464 
    465 	sc->bt = memt;
    466 	sc->bh = memh;
    467 	sc->sc_iobase = (char *)memh + size - (1 << 24);
    468 
    469 	sc->scp = size + IE_SCP_ADDR - (1 << 24);
    470 	bus_space_set_region_1(memt, memh, (u_long) sc->scp, 0, IE_SCP_SZ);
    471 
    472 	sc->iscp = 0;
    473 	bus_space_set_region_1(memt, memh, (u_long) sc->iscp, 0, IE_ISCP_SZ);
    474 
    475 	sc->scb = IE_ISCP_SZ;
    476 	bus_space_set_region_1(memt, memh, sc->scb, 0, IE_SCB_SZ);
    477 
    478 	/* set card to 16-bit bus mode */
    479 	bus_space_write_1(sc->bt, sc->bh, IE_SCP_BUS_USE((u_long)sc->scp),
    480 			  IE_SYSBUS_16BIT);
    481 
    482 	/* set up pointers to key structures */
    483 	ai_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
    484 	ai_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
    485 	ai_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
    486 
    487 	/* flush setup of pointers, check if chip answers */
    488 	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
    489 			  BUS_SPACE_BARRIER_WRITE);
    490 
    491 	if (!i82586_proberam(sc))
    492 		return (0);
    493 
    494 	return (size);
    495 }
    496 
    497 struct cfattach ai_ca = {
    498 	sizeof(struct ai_softc), ai_match, ai_attach
    499 };
    500