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