Home | History | Annotate | Line # | Download | only in nubus
if_ae_nubus.c revision 1.26
      1 /*	$NetBSD: if_ae_nubus.c,v 1.26 1998/05/02 16:45:30 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 			rv = 1;
    105 			break;
    106 		case DP8390_VENDOR_DAYNA:
    107 		case DP8390_VENDOR_FOCUS:
    108 			rv = UNSUPP;
    109 			break;
    110 		default:
    111 			break;
    112 		}
    113 	}
    114 
    115 	bus_space_unmap(na->na_tag, bsh, NBMEMSIZE);
    116 
    117 	return rv;
    118 }
    119 
    120 /*
    121  * Install interface into kernel networking data structures
    122  */
    123 static void
    124 ae_nubus_attach(parent, self, aux)
    125 	struct device *parent, *self;
    126 	void   *aux;
    127 {
    128 	struct dp8390_softc *sc = (struct dp8390_softc *)self;
    129 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    130 #ifdef DEBUG
    131 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    132 #endif
    133 	bus_space_tag_t bst;
    134 	bus_space_handle_t bsh;
    135 	int i, success;
    136 	char *cardtype;
    137 
    138 	bst = na->na_tag;
    139 	if (bus_space_map(bst, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
    140 	    0, &bsh)) {
    141 		printf(": can't map memory space\n");
    142 		return;
    143 	}
    144 
    145 	sc->sc_regt = sc->sc_buft = bst;
    146 	sc->sc_flags = self->dv_cfdata->cf_flags;
    147 
    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 (ae_nb_card_vendor(bst, bsh, na)) {
    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", cardtype, sc->mem_size / 1024);
    354 
    355 	if (dp8390_config(sc, NULL, 0, 0)) {
    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 void
    365 ae_nubus_intr(arg)
    366 	void *arg;
    367 {
    368 	struct dp8390_softc *sc = (struct dp8390_softc *)arg;
    369 
    370 	(void)dp8390_intr(sc);
    371 }
    372 
    373 static int
    374 ae_nb_card_vendor(bst, bsh, na)
    375 	bus_space_tag_t bst;
    376 	bus_space_handle_t bsh;
    377 	struct nubus_attach_args *na;
    378 {
    379 	int vendor;
    380 
    381 	switch (na->drsw) {
    382 	case NUBUS_DRSW_3COM:
    383 		switch (na->drhw) {
    384 		case NUBUS_DRHW_APPLE_SN:
    385 		case NUBUS_DRHW_APPLE_SNT:
    386 			vendor = DP8390_VENDOR_UNKNOWN;
    387 			break;
    388 		default:
    389 			vendor = DP8390_VENDOR_APPLE;
    390 			break;
    391 		}
    392 		break;
    393 	case NUBUS_DRSW_APPLE:
    394 	case NUBUS_DRSW_DAYNA2:
    395 	case NUBUS_DRSW_TECHWORKS:
    396 		vendor = DP8390_VENDOR_APPLE;
    397 		break;
    398 	case NUBUS_DRSW_ASANTE:
    399 		vendor = DP8390_VENDOR_ASANTE;
    400 		break;
    401 	case NUBUS_DRSW_FARALLON:
    402 		vendor = DP8390_VENDOR_FARALLON;
    403 		break;
    404 	case NUBUS_DRSW_FOCUS:
    405 		vendor = DP8390_VENDOR_FOCUS;
    406 		break;
    407 	case NUBUS_DRSW_GATOR:
    408 		switch (na->drhw) {
    409 		default:
    410 		case NUBUS_DRHW_INTERLAN:
    411 			vendor = DP8390_VENDOR_INTERLAN;
    412 			break;
    413 		case NUBUS_DRHW_KINETICS:
    414 			if (strncmp(nubus_get_card_name(bst, bsh, na->fmt),
    415 			    "EtherPort", 9) == 0)
    416 				vendor = DP8390_VENDOR_KINETICS;
    417 			else
    418 				vendor = DP8390_VENDOR_DAYNA;
    419 			break;
    420 		}
    421 		break;
    422 	default:
    423 		vendor = DP8390_VENDOR_UNKNOWN;
    424 	}
    425 	return vendor;
    426 }
    427 
    428 static int
    429 ae_nb_get_enaddr(bst, bsh, na, ep)
    430 	bus_space_tag_t bst;
    431 	bus_space_handle_t bsh;
    432 	struct nubus_attach_args *na;
    433 	u_int8_t *ep;
    434 {
    435 	nubus_dir dir;
    436 	nubus_dirent dirent;
    437 
    438 	/*
    439 	 * XXX - note hardwired resource IDs here (0x80); these are
    440 	 * assumed to be used by all cards, but should be fixed when
    441 	 * we find out more about Ethernet card resources.
    442 	 */
    443 	nubus_get_main_dir(na->fmt, &dir);
    444 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    445 		return 1;
    446 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    447 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    448 		return 1;
    449 	if (nubus_get_ind_data(bst, bsh,
    450 	    na->fmt, &dirent, ep, ETHER_ADDR_LEN) <= 0)
    451 		return 1;
    452 
    453 	return 0;
    454 }
    455 
    456 #ifdef DEBUG
    457 static void
    458 ae_nb_watchdog(ifp)
    459 	struct ifnet *ifp;
    460 {
    461 	struct dp8390_softc *sc = ifp->if_softc;
    462 
    463 /*
    464  * This is a kludge!  The via code seems to miss slot interrupts
    465  * sometimes.  This kludges around that by calling the handler
    466  * by hand if the watchdog is activated. -- XXX (akb)
    467  */
    468 	(*via2itab[1])((void *)1);
    469 
    470 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    471 	++ifp->if_oerrors;
    472 
    473 	dp8390_reset(sc);
    474 }
    475 #endif
    476