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