Home | History | Annotate | Line # | Download | only in nubus
if_ae_nubus.c revision 1.22
      1 /*	$NetBSD: if_ae_nubus.c,v 1.22 1997/10/15 16:58:21 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1997 Scott Reynolds
      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  * Some parts are derived from code adapted for MacBSD by Brad Parker
     31  * <brad (at) fcr.com>.
     32  *
     33  * Currently supports:
     34  *	Apple NB Ethernet Card
     35  *	Apple NB Ethernet Card II
     36  *	Interlan A310 NuBus Ethernet card
     37  *	Cayman Systems GatorCard
     38  *	Asante MacCon II/E
     39  *	Kinetics EtherPort SE/30
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/device.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/malloc.h>
     47 #include <sys/socket.h>
     48 #include <sys/syslog.h>
     49 #include <sys/systm.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_ether.h>
     53 
     54 #include <machine/bus.h>
     55 #include <machine/viareg.h>
     56 
     57 #include <dev/ic/dp8390reg.h>
     58 #include <dev/ic/dp8390var.h>
     59 #include <mac68k/dev/nubus.h>
     60 #include <mac68k/dev/if_aevar.h>
     61 #include <mac68k/dev/if_aereg.h>
     62 
     63 static int	ae_nubus_match __P((struct device *, struct cfdata *, void *));
     64 static void	ae_nubus_attach __P((struct device *, struct device *, void *));
     65 static int	ae_nb_card_vendor __P((bus_space_tag_t, bus_space_handle_t,
     66 		    struct nubus_attach_args *));
     67 static int	ae_nb_get_enaddr __P((bus_space_tag_t, bus_space_handle_t,
     68 		    struct nubus_attach_args *, u_int8_t *));
     69 #ifdef DEBUG
     70 static void	ae_nb_watchdog __P((struct ifnet *));
     71 #endif
     72 
     73 void		ae_nubus_intr __P((void *, int));
     74 
     75 struct cfattach ae_nubus_ca = {
     76 	sizeof(struct dp8390_softc), ae_nubus_match, ae_nubus_attach
     77 };
     78 
     79 static int
     80 ae_nubus_match(parent, cf, aux)
     81 	struct device *parent;
     82 	struct cfdata *cf;
     83 	void *aux;
     84 {
     85 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
     86 	bus_space_handle_t bsh;
     87 	int rv;
     88 
     89 	if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
     90 	    0, &bsh))
     91 		return (0);
     92 
     93 	rv = 0;
     94 
     95 	if (na->category == NUBUS_CATEGORY_NETWORK &&
     96 	    na->type == NUBUS_TYPE_ETHERNET) {
     97 		switch (ae_nb_card_vendor(na->na_tag, bsh, na)) {
     98 		case DP8390_VENDOR_APPLE:
     99 		case DP8390_VENDOR_ASANTE:
    100 		case DP8390_VENDOR_FARALLON:
    101 		case DP8390_VENDOR_INTERLAN:
    102 		case DP8390_VENDOR_KINETICS:
    103 			rv = 1;
    104 			break;
    105 		case DP8390_VENDOR_DAYNA:
    106 		case DP8390_VENDOR_FOCUS:
    107 			rv = UNSUPP;
    108 			break;
    109 		default:
    110 			break;
    111 		}
    112 	}
    113 
    114 	bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
    115 
    116 	return rv;
    117 }
    118 
    119 /*
    120  * Install interface into kernel networking data structures
    121  */
    122 static void
    123 ae_nubus_attach(parent, self, aux)
    124 	struct device *parent, *self;
    125 	void   *aux;
    126 {
    127 	struct dp8390_softc *sc = (struct dp8390_softc *)self;
    128 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    129 #ifdef DEBUG
    130 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    131 #endif
    132 	bus_space_tag_t bst;
    133 	bus_space_handle_t bsh;
    134 	int i, success;
    135 	char *cardtype;
    136 
    137 	bst = na->na_tag;
    138 	if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
    139 	    0, &bsh)) {
    140 		printf(": can't map memory space\n");
    141 		return;
    142 	}
    143 
    144 	sc->sc_regt = sc->sc_buft = bst;
    145 	sc->sc_flags = self->dv_cfdata->cf_flags;
    146 
    147 	sc->vendor = ae_nb_card_vendor(bst, bsh, na);
    148 	cardtype = nubus_get_card_name(bst, bsh, na->fmt);
    149 
    150 	sc->is790 = 0;
    151 
    152 	sc->mem_start = 0;
    153 	sc->mem_size = 0;
    154 
    155 	success = 0;
    156 
    157 	switch (sc->vendor) {
    158 	case DP8390_VENDOR_APPLE:	/* Apple-compatible cards */
    159 	case DP8390_VENDOR_ASANTE:
    160 		/* Map register offsets */
    161 		for (i = 0; i < 16; i++) /* reverse order, longword aligned */
    162 			sc->sc_reg_map[i] = (15 - i) << 2;
    163 
    164 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    165 		if (bus_space_subregion(bst, bsh,
    166 		    AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    167 			printf(": failed to map register space\n");
    168 			break;
    169 		}
    170 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    171 		    AE_DATA_OFFSET)) == 0) {
    172 			printf(": failed to determine size of RAM.\n");
    173 			break;
    174 		}
    175 		if (bus_space_subregion(bst, bsh,
    176 		    AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    177 			printf(": failed to map register space\n");
    178 			break;
    179 		}
    180 #ifdef AE_OLD_GET_ENADDR
    181 		/* Get station address from on-board ROM */
    182 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    183 			sc->sc_enaddr[i] =
    184 			    bus_space_read_1(bst, bsh, (AE_ROM_OFFSET + i * 2));
    185 #else
    186 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    187 			printf(": can't find MAC address\n");
    188 			break;
    189 		}
    190 #endif
    191 
    192 		success = 1;
    193 		break;
    194 
    195 	case DP8390_VENDOR_DAYNA:
    196 		/* Map register offsets */
    197 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    198 			sc->sc_reg_map[i] = i << 2;
    199 
    200 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    201 		if (bus_space_subregion(bst, bsh,
    202 		    DP_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    203 			printf(": failed to map register space\n");
    204 			break;
    205 		}
    206 		sc->mem_size = 8192;
    207 		if (bus_space_subregion(bst, bsh,
    208 		    DP_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    209 			printf(": failed to map register space\n");
    210 			break;
    211 		}
    212 #ifdef AE_OLD_GET_ENADDR
    213 		/* Get station address from on-board ROM */
    214 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    215 			sc->sc_enaddr[i] =
    216 			    bus_space_read_1(bst, bsh, (DP_ROM_OFFSET + i * 2));
    217 #else
    218 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    219 			printf(": can't find MAC address\n");
    220 			break;
    221 		}
    222 #endif
    223 
    224 		printf(": unsupported Dayna hardware\n");
    225 		break;
    226 
    227 	case DP8390_VENDOR_FARALLON:
    228 		/* Map register offsets */
    229 		for (i = 0; i < 16; i++) /* reverse order, longword aligned */
    230 			sc->sc_reg_map[i] = (15 - i) << 2;
    231 
    232 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    233 		if (bus_space_subregion(bst, bsh,
    234 		    AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    235 			printf(": failed to map register space\n");
    236 			break;
    237 		}
    238 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    239 		    AE_DATA_OFFSET)) == 0) {
    240 			printf(": failed to determine size of RAM.\n");
    241 			break;
    242 		}
    243 		if (bus_space_subregion(bst, bsh,
    244 		    AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    245 			printf(": failed to map register space\n");
    246 			break;
    247 		}
    248 #ifdef AE_OLD_GET_ENADDR
    249 		/* Get station address from on-board ROM */
    250 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    251 			sc->sc_enaddr[i] =
    252 			    bus_space_read_1(bst, bsh, (FE_ROM_OFFSET + i));
    253 #else
    254 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    255 			printf(": can't find MAC address\n");
    256 			break;
    257 		}
    258 #endif
    259 
    260 		success = 1;
    261 		break;
    262 
    263 	case DP8390_VENDOR_FOCUS:
    264 		printf(": unsupported Focus hardware\n");
    265 		break;
    266 
    267 	case DP8390_VENDOR_INTERLAN:
    268 		/* Map register offsets */
    269 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    270 			sc->sc_reg_map[i] = i << 2;
    271 
    272 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    273 		if (bus_space_subregion(bst, bsh,
    274 		    GC_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    275 			printf(": failed to map register space\n");
    276 			break;
    277 		}
    278 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    279 		    GC_DATA_OFFSET)) == 0) {
    280 			printf(": failed to determine size of RAM.\n");
    281 			break;
    282 		}
    283 		if (bus_space_subregion(bst, bsh,
    284 		    GC_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    285 			printf(": failed to map register space\n");
    286 			break;
    287 		}
    288 
    289 		/* reset the NIC chip */
    290 		bus_space_write_1(bst, bsh, GC_RESET_OFFSET, 0);
    291 
    292 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    293 			/* Fall back to snarf directly from ROM.  Ick. */
    294 			for (i = 0; i < ETHER_ADDR_LEN; ++i)
    295 				sc->sc_enaddr[i] =
    296 				    bus_space_read_1(bst, bsh,
    297 				    (GC_ROM_OFFSET + i * 4));
    298 		}
    299 
    300 		success = 1;
    301 		break;
    302 
    303 	case DP8390_VENDOR_KINETICS:
    304 		/* Map register offsets */
    305 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    306 			sc->sc_reg_map[i] = i << 2;
    307 
    308 		if (bus_space_subregion(bst, bsh,
    309 		    KE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    310 			printf(": failed to map register space\n");
    311 			break;
    312 		}
    313 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    314 		    KE_DATA_OFFSET)) == 0) {
    315 			printf(": failed to determine size of RAM.\n");
    316 			break;
    317 		}
    318 		if (bus_space_subregion(bst, bsh,
    319 		    KE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    320 			printf(": failed to map register space\n");
    321 			break;
    322 		}
    323 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    324 			printf(": can't find MAC address\n");
    325 			break;
    326 		}
    327 
    328 		success = 1;
    329 		break;
    330 
    331 	default:
    332 		break;
    333 	}
    334 
    335 	if (!success) {
    336 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    337 		return;
    338 	}
    339 
    340 	/*
    341 	 * Override test_mem and write_mbuf functions; other defaults
    342 	 * already work properly.
    343 	 */
    344 	sc->test_mem = ae_test_mem;
    345 	sc->write_mbuf = ae_write_mbuf;
    346 #ifdef DEBUG
    347 	ifp->if_watchdog = ae_nb_watchdog;	/* Override watchdog */
    348 #endif
    349 
    350 	/* Interface is always enabled. */
    351 	sc->sc_enabled = 1;
    352 
    353 	printf(": %s, %dKB memory\n", cardname, sc->mem_size / 1024);
    354 
    355 	if (dp8390_config(sc)) {
    356 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    357 		return;
    358 	}
    359 
    360 	/* make sure interrupts are vectored to us */
    361 	add_nubus_intr(na->slot, ae_nubus_intr, sc);
    362 }
    363 
    364 /* ARGSUSED */
    365 void
    366 ae_nubus_intr(arg, slot)
    367 	void *arg;
    368 	int slot;
    369 {
    370 
    371 	(void) dp8390_intr(sc);
    372 }
    373 
    374 static int
    375 ae_nb_card_vendor(bst, bsh, na)
    376 	bus_space_tag_t bst;
    377 	bus_space_handle_t bsh;
    378 	struct nubus_attach_args *na;
    379 {
    380 	int vendor;
    381 
    382 	switch (na->drsw) {
    383 	case NUBUS_DRSW_3COM:
    384 		switch (na->drhw) {
    385 		case NUBUS_DRHW_APPLE_SN:
    386 		case NUBUS_DRHW_APPLE_SNT:
    387 			vendor = DP8390_VENDOR_UNKNOWN;
    388 			break;
    389 		default:
    390 			vendor = DP8390_VENDOR_APPLE;
    391 			break;
    392 		}
    393 		break;
    394 	case NUBUS_DRSW_APPLE:
    395 	case NUBUS_DRSW_DAYNA2:
    396 	case NUBUS_DRSW_TECHWORKS:
    397 		vendor = DP8390_VENDOR_APPLE;
    398 		break;
    399 	case NUBUS_DRSW_ASANTE:
    400 		vendor = DP8390_VENDOR_ASANTE;
    401 		break;
    402 	case NUBUS_DRSW_FARALLON:
    403 		vendor = DP8390_VENDOR_FARALLON;
    404 		break;
    405 	case NUBUS_DRSW_FOCUS:
    406 		vendor = DP8390_VENDOR_FOCUS;
    407 		break;
    408 	case NUBUS_DRSW_GATOR:
    409 		switch (na->drhw) {
    410 		default:
    411 		case NUBUS_DRHW_INTERLAN:
    412 			vendor = DP8390_VENDOR_INTERLAN;
    413 			break;
    414 		case NUBUS_DRHW_KINETICS:
    415 			if (strncmp(nubus_get_card_name(bst, bsh, na->fmt),
    416 			    "EtherPort", 9) == 0)
    417 				vendor = DP8390_VENDOR_KINETICS;
    418 			else
    419 				vendor = DP8390_VENDOR_DAYNA;
    420 			break;
    421 		}
    422 		break;
    423 	default:
    424 		vendor = DP8390_VENDOR_UNKNOWN;
    425 	}
    426 	return vendor;
    427 }
    428 
    429 static int
    430 ae_nb_get_enaddr(bst, bsh, na, ep)
    431 	bus_space_tag_t bst;
    432 	bus_space_handle_t bsh;
    433 	struct nubus_attach_args *na;
    434 	u_int8_t *ep;
    435 {
    436 	nubus_dir dir;
    437 	nubus_dirent dirent;
    438 
    439 	/*
    440 	 * XXX - note hardwired resource IDs here (0x80); these are
    441 	 * assumed to be used by all cards, but should be fixed when
    442 	 * we find out more about Ethernet card resources.
    443 	 */
    444 	nubus_get_main_dir(na->fmt, &dir);
    445 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    446 		return 1;
    447 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    448 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    449 		return 1;
    450 	if (nubus_get_ind_data(bst, bsh,
    451 	    na->fmt, &dirent, ep, ETHER_ADDR_LEN) <= 0)
    452 		return 1;
    453 
    454 	return 0;
    455 }
    456 
    457 #ifdef DEBUG
    458 static void
    459 ae_nb_watchdog(ifp)
    460 	struct ifnet *ifp;
    461 {
    462 	struct dp8390_softc *sc = ifp->if_softc;
    463 
    464 /*
    465  * This is a kludge!  The via code seems to miss slot interrupts
    466  * sometimes.  This kludges around that by calling the handler
    467  * by hand if the watchdog is activated. -- XXX (akb)
    468  */
    469 	(*via2itab[1])((void *) 1);
    470 
    471 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    472 	++ifp->if_oerrors;
    473 
    474 	dp8390_reset(sc);
    475 }
    476 #endif
    477