Home | History | Annotate | Line # | Download | only in nubus
if_ae_nubus.c revision 1.23
      1 /*	$NetBSD: if_ae_nubus.c,v 1.23 1997/10/17 00:24:47 briggs 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 	cardtype = nubus_get_card_name(bst, bsh, na->fmt);
    148 
    149 	sc->is790 = 0;
    150 
    151 	sc->mem_start = 0;
    152 	sc->mem_size = 0;
    153 
    154 	success = 0;
    155 
    156 	switch (ae_nb_card_vendor(bst, bsh, na)) {
    157 	case DP8390_VENDOR_APPLE:	/* Apple-compatible cards */
    158 	case DP8390_VENDOR_ASANTE:
    159 		/* Map register offsets */
    160 		for (i = 0; i < 16; i++) /* reverse order, longword aligned */
    161 			sc->sc_reg_map[i] = (15 - i) << 2;
    162 
    163 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    164 		if (bus_space_subregion(bst, bsh,
    165 		    AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    166 			printf(": failed to map register space\n");
    167 			break;
    168 		}
    169 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    170 		    AE_DATA_OFFSET)) == 0) {
    171 			printf(": failed to determine size of RAM.\n");
    172 			break;
    173 		}
    174 		if (bus_space_subregion(bst, bsh,
    175 		    AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    176 			printf(": failed to map register space\n");
    177 			break;
    178 		}
    179 #ifdef AE_OLD_GET_ENADDR
    180 		/* Get station address from on-board ROM */
    181 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    182 			sc->sc_enaddr[i] =
    183 			    bus_space_read_1(bst, bsh, (AE_ROM_OFFSET + i * 2));
    184 #else
    185 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    186 			printf(": can't find MAC address\n");
    187 			break;
    188 		}
    189 #endif
    190 
    191 		success = 1;
    192 		break;
    193 
    194 	case DP8390_VENDOR_DAYNA:
    195 		/* Map register offsets */
    196 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    197 			sc->sc_reg_map[i] = i << 2;
    198 
    199 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    200 		if (bus_space_subregion(bst, bsh,
    201 		    DP_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    202 			printf(": failed to map register space\n");
    203 			break;
    204 		}
    205 		sc->mem_size = 8192;
    206 		if (bus_space_subregion(bst, bsh,
    207 		    DP_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    208 			printf(": failed to map register space\n");
    209 			break;
    210 		}
    211 #ifdef AE_OLD_GET_ENADDR
    212 		/* Get station address from on-board ROM */
    213 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    214 			sc->sc_enaddr[i] =
    215 			    bus_space_read_1(bst, bsh, (DP_ROM_OFFSET + i * 2));
    216 #else
    217 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    218 			printf(": can't find MAC address\n");
    219 			break;
    220 		}
    221 #endif
    222 
    223 		printf(": unsupported Dayna hardware\n");
    224 		break;
    225 
    226 	case DP8390_VENDOR_FARALLON:
    227 		/* Map register offsets */
    228 		for (i = 0; i < 16; i++) /* reverse order, longword aligned */
    229 			sc->sc_reg_map[i] = (15 - i) << 2;
    230 
    231 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    232 		if (bus_space_subregion(bst, bsh,
    233 		    AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    234 			printf(": failed to map register space\n");
    235 			break;
    236 		}
    237 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    238 		    AE_DATA_OFFSET)) == 0) {
    239 			printf(": failed to determine size of RAM.\n");
    240 			break;
    241 		}
    242 		if (bus_space_subregion(bst, bsh,
    243 		    AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    244 			printf(": failed to map register space\n");
    245 			break;
    246 		}
    247 #ifdef AE_OLD_GET_ENADDR
    248 		/* Get station address from on-board ROM */
    249 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    250 			sc->sc_enaddr[i] =
    251 			    bus_space_read_1(bst, bsh, (FE_ROM_OFFSET + i));
    252 #else
    253 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    254 			printf(": can't find MAC address\n");
    255 			break;
    256 		}
    257 #endif
    258 
    259 		success = 1;
    260 		break;
    261 
    262 	case DP8390_VENDOR_FOCUS:
    263 		printf(": unsupported Focus hardware\n");
    264 		break;
    265 
    266 	case DP8390_VENDOR_INTERLAN:
    267 		/* Map register offsets */
    268 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    269 			sc->sc_reg_map[i] = i << 2;
    270 
    271 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    272 		if (bus_space_subregion(bst, bsh,
    273 		    GC_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    274 			printf(": failed to map register space\n");
    275 			break;
    276 		}
    277 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    278 		    GC_DATA_OFFSET)) == 0) {
    279 			printf(": failed to determine size of RAM.\n");
    280 			break;
    281 		}
    282 		if (bus_space_subregion(bst, bsh,
    283 		    GC_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    284 			printf(": failed to map register space\n");
    285 			break;
    286 		}
    287 
    288 		/* reset the NIC chip */
    289 		bus_space_write_1(bst, bsh, GC_RESET_OFFSET, 0);
    290 
    291 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    292 			/* Fall back to snarf directly from ROM.  Ick. */
    293 			for (i = 0; i < ETHER_ADDR_LEN; ++i)
    294 				sc->sc_enaddr[i] =
    295 				    bus_space_read_1(bst, bsh,
    296 				    (GC_ROM_OFFSET + i * 4));
    297 		}
    298 
    299 		success = 1;
    300 		break;
    301 
    302 	case DP8390_VENDOR_KINETICS:
    303 		/* Map register offsets */
    304 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    305 			sc->sc_reg_map[i] = i << 2;
    306 
    307 		if (bus_space_subregion(bst, bsh,
    308 		    KE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    309 			printf(": failed to map register space\n");
    310 			break;
    311 		}
    312 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    313 		    KE_DATA_OFFSET)) == 0) {
    314 			printf(": failed to determine size of RAM.\n");
    315 			break;
    316 		}
    317 		if (bus_space_subregion(bst, bsh,
    318 		    KE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    319 			printf(": failed to map register space\n");
    320 			break;
    321 		}
    322 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    323 			printf(": can't find MAC address\n");
    324 			break;
    325 		}
    326 
    327 		success = 1;
    328 		break;
    329 
    330 	default:
    331 		break;
    332 	}
    333 
    334 	if (!success) {
    335 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    336 		return;
    337 	}
    338 
    339 	/*
    340 	 * Override test_mem and write_mbuf functions; other defaults
    341 	 * already work properly.
    342 	 */
    343 	sc->test_mem = ae_test_mem;
    344 	sc->write_mbuf = ae_write_mbuf;
    345 #ifdef DEBUG
    346 	ifp->if_watchdog = ae_nb_watchdog;	/* Override watchdog */
    347 #endif
    348 
    349 	/* Interface is always enabled. */
    350 	sc->sc_enabled = 1;
    351 
    352 	printf(": %s, %dKB memory\n", cardtype, sc->mem_size / 1024);
    353 
    354 	if (dp8390_config(sc)) {
    355 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    356 		return;
    357 	}
    358 
    359 	/* make sure interrupts are vectored to us */
    360 	add_nubus_intr(na->slot, ae_nubus_intr, sc);
    361 }
    362 
    363 /* ARGSUSED */
    364 void
    365 ae_nubus_intr(arg, slot)
    366 	void *arg;
    367 	int slot;
    368 {
    369 	struct dp8390_softc *sc = (struct dp8390_softc *)arg;
    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