Home | History | Annotate | Line # | Download | only in nubus
if_ae_nubus.c revision 1.19
      1 /*	$NetBSD: if_ae_nubus.c,v 1.19 1997/08/11 22:53:32 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Scott Reynolds for
     18  *      the NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Some parts are derived from code adapted for MacBSD by Brad Parker
     35  * <brad (at) fcr.com>.
     36  *
     37  * Currently supports:
     38  *	Apple NB Ethernet Card
     39  *	Apple NB Ethernet Card II
     40  *	Interlan A310 NuBus Ethernet card
     41  *	Cayman Systems GatorCard
     42  *	Asante MacCon II/E
     43  *	Kinetics EtherPort SE/30
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/device.h>
     48 #include <sys/errno.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/malloc.h>
     51 #include <sys/socket.h>
     52 #include <sys/syslog.h>
     53 #include <sys/systm.h>
     54 
     55 #include <net/if.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/dev/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 struct cfattach ae_nubus_ca = {
     78 	sizeof(struct dp8390_softc), ae_nubus_match, ae_nubus_attach
     79 };
     80 
     81 static int
     82 ae_nubus_match(parent, cf, aux)
     83 	struct device *parent;
     84 	struct cfdata *cf;
     85 	void *aux;
     86 {
     87 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
     88 	bus_space_handle_t bsh;
     89 	int rv;
     90 
     91 	if (bus_space_map(na->na_tag, NUBUS_SLOT2PA(na->slot), NBMEMSIZE,
     92 	    0, &bsh))
     93 		return (0);
     94 
     95 	rv = 0;
     96 
     97 	if (na->category == NUBUS_CATEGORY_NETWORK &&
     98 	    na->type == NUBUS_TYPE_ETHERNET) {
     99 		switch (ae_nb_card_vendor(na->na_tag, bsh, na)) {
    100 		case DP8390_VENDOR_APPLE:
    101 		case DP8390_VENDOR_ASANTE:
    102 		case DP8390_VENDOR_FARALLON:
    103 		case DP8390_VENDOR_INTERLAN:
    104 		case DP8390_VENDOR_KINETICS:
    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 	sc->vendor = ae_nb_card_vendor(bst, bsh, na);
    150 	sc->type = 0;
    151 	cardtype = nubus_get_card_name(bst, bsh, na->fmt);
    152 	i = strlen(cardtype);
    153 	if ((sc->type_str = malloc((u_long)(i + 1), M_DEVBUF, M_NOWAIT))) {
    154 		strncpy(sc->type_str, cardtype, i);
    155 		sc->type_str[i] = '\0';
    156 	}
    157 
    158 	sc->is790 = 0;
    159 
    160 	sc->mem_start = 0;
    161 	sc->mem_size = 0;
    162 
    163 	success = 0;
    164 
    165 	switch (sc->vendor) {
    166 	case DP8390_VENDOR_APPLE:	/* Apple-compatible cards */
    167 	case DP8390_VENDOR_ASANTE:
    168 		/* Map register offsets */
    169 		for (i = 0; i < 16; i++) /* reverse order, longword aligned */
    170 			sc->sc_reg_map[i] = (15 - i) << 2;
    171 
    172 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    173 		if (bus_space_subregion(bst, bsh,
    174 		    AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    175 			printf(": failed to map register space\n");
    176 			break;
    177 		}
    178 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    179 		    AE_DATA_OFFSET)) == 0) {
    180 			printf(": failed to determine size of RAM.\n");
    181 			break;
    182 		}
    183 		if (bus_space_subregion(bst, bsh,
    184 		    AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    185 			printf(": failed to map register space\n");
    186 			break;
    187 		}
    188 #ifdef AE_OLD_GET_ENADDR
    189 		/* Get station address from on-board ROM */
    190 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    191 			sc->sc_enaddr[i] =
    192 			    bus_space_read_1(bst, bsh, (AE_ROM_OFFSET + i * 2));
    193 #else
    194 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    195 			printf(": can't find MAC address\n");
    196 			break;
    197 		}
    198 #endif
    199 
    200 		success = 1;
    201 		break;
    202 
    203 	case DP8390_VENDOR_DAYNA:
    204 		/* Map register offsets */
    205 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    206 			sc->sc_reg_map[i] = i << 2;
    207 
    208 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    209 		if (bus_space_subregion(bst, bsh,
    210 		    DP_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    211 			printf(": failed to map register space\n");
    212 			break;
    213 		}
    214 		sc->mem_size = 8192;
    215 		if (bus_space_subregion(bst, bsh,
    216 		    DP_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    217 			printf(": failed to map register space\n");
    218 			break;
    219 		}
    220 #ifdef AE_OLD_GET_ENADDR
    221 		/* Get station address from on-board ROM */
    222 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    223 			sc->sc_enaddr[i] =
    224 			    bus_space_read_1(bst, bsh, (DP_ROM_OFFSET + i * 2));
    225 #else
    226 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    227 			printf(": can't find MAC address\n");
    228 			break;
    229 		}
    230 #endif
    231 
    232 		printf(": unsupported Dayna hardware\n");
    233 		break;
    234 
    235 	case DP8390_VENDOR_FARALLON:
    236 		/* Map register offsets */
    237 		for (i = 0; i < 16; i++) /* reverse order, longword aligned */
    238 			sc->sc_reg_map[i] = (15 - i) << 2;
    239 
    240 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    241 		if (bus_space_subregion(bst, bsh,
    242 		    AE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    243 			printf(": failed to map register space\n");
    244 			break;
    245 		}
    246 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    247 		    AE_DATA_OFFSET)) == 0) {
    248 			printf(": failed to determine size of RAM.\n");
    249 			break;
    250 		}
    251 		if (bus_space_subregion(bst, bsh,
    252 		    AE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    253 			printf(": failed to map register space\n");
    254 			break;
    255 		}
    256 #ifdef AE_OLD_GET_ENADDR
    257 		/* Get station address from on-board ROM */
    258 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    259 			sc->sc_enaddr[i] =
    260 			    bus_space_read_1(bst, bsh, (FE_ROM_OFFSET + i));
    261 #else
    262 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    263 			printf(": can't find MAC address\n");
    264 			break;
    265 		}
    266 #endif
    267 
    268 		success = 1;
    269 		break;
    270 
    271 	case DP8390_VENDOR_FOCUS:
    272 		printf(": unsupported Focus hardware\n");
    273 		break;
    274 
    275 	case DP8390_VENDOR_INTERLAN:
    276 		/* Map register offsets */
    277 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    278 			sc->sc_reg_map[i] = i << 2;
    279 
    280 		sc->dcr_reg = (ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    281 		if (bus_space_subregion(bst, bsh,
    282 		    GC_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    283 			printf(": failed to map register space\n");
    284 			break;
    285 		}
    286 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    287 		    GC_DATA_OFFSET)) == 0) {
    288 			printf(": failed to determine size of RAM.\n");
    289 			break;
    290 		}
    291 		if (bus_space_subregion(bst, bsh,
    292 		    GC_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    293 			printf(": failed to map register space\n");
    294 			break;
    295 		}
    296 
    297 		/* reset the NIC chip */
    298 		bus_space_write_1(bst, bsh, GC_RESET_OFFSET, 0);
    299 
    300 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    301 			/* Fall back to snarf directly from ROM.  Ick. */
    302 			for (i = 0; i < ETHER_ADDR_LEN; ++i)
    303 				sc->sc_enaddr[i] =
    304 				    bus_space_read_1(bst, bsh,
    305 				    (GC_ROM_OFFSET + i * 4));
    306 		}
    307 
    308 		success = 1;
    309 		break;
    310 
    311 	case DP8390_VENDOR_KINETICS:
    312 		/* Map register offsets */
    313 		for (i = 0; i < 16; i++) /* normal order, longword aligned */
    314 			sc->sc_reg_map[i] = i << 2;
    315 
    316 		if (bus_space_subregion(bst, bsh,
    317 		    KE_REG_OFFSET, AE_REG_SIZE, &sc->sc_regh)) {
    318 			printf(": failed to map register space\n");
    319 			break;
    320 		}
    321 		if ((sc->mem_size = ae_size_card_memory(bst, bsh,
    322 		    KE_DATA_OFFSET)) == 0) {
    323 			printf(": failed to determine size of RAM.\n");
    324 			break;
    325 		}
    326 		if (bus_space_subregion(bst, bsh,
    327 		    KE_DATA_OFFSET, sc->mem_size, &sc->sc_bufh)) {
    328 			printf(": failed to map register space\n");
    329 			break;
    330 		}
    331 		if (ae_nb_get_enaddr(bst, bsh, na, sc->sc_enaddr)) {
    332 			printf(": can't find MAC address\n");
    333 			break;
    334 		}
    335 
    336 		success = 1;
    337 		break;
    338 
    339 	default:
    340 		break;
    341 	}
    342 
    343 	if (!success) {
    344 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    345 		return;
    346 	}
    347 
    348 	/*
    349 	 * Override test_mem and write_mbuf functions; other defaults
    350 	 * already work properly.
    351 	 */
    352 	sc->test_mem = ae_test_mem;
    353 	sc->write_mbuf = ae_write_mbuf;
    354 #ifdef DEBUG
    355 	ifp->if_watchdog = ae_nb_watchdog;	/* Override watchdog */
    356 #endif
    357 
    358 	if (dp8390_config(sc)) {
    359 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    360 		return;
    361 	}
    362 
    363 	printf("%dKB memory\n", sc->mem_size / 1024);
    364 
    365 	/* make sure interrupts are vectored to us */
    366 	add_nubus_intr(na->slot, dp8390_intr, sc);
    367 }
    368 
    369 static int
    370 ae_nb_card_vendor(bst, bsh, na)
    371 	bus_space_tag_t bst;
    372 	bus_space_handle_t bsh;
    373 	struct nubus_attach_args *na;
    374 {
    375 	int vendor;
    376 
    377 	switch (na->drsw) {
    378 	case NUBUS_DRSW_3COM:
    379 		switch (na->drhw) {
    380 		case NUBUS_DRHW_APPLE_SN:
    381 		case NUBUS_DRHW_APPLE_SNT:
    382 			vendor = DP8390_VENDOR_UNKNOWN;
    383 			break;
    384 		default:
    385 			vendor = DP8390_VENDOR_APPLE;
    386 			break;
    387 		}
    388 		break;
    389 	case NUBUS_DRSW_APPLE:
    390 	case NUBUS_DRSW_TECHWORKS:
    391 		vendor = DP8390_VENDOR_APPLE;
    392 		break;
    393 	case NUBUS_DRSW_ASANTE:
    394 		vendor = DP8390_VENDOR_ASANTE;
    395 		break;
    396 	case NUBUS_DRSW_FARALLON:
    397 		vendor = DP8390_VENDOR_FARALLON;
    398 		break;
    399 	case NUBUS_DRSW_FOCUS:
    400 		vendor = DP8390_VENDOR_FOCUS;
    401 		break;
    402 	case NUBUS_DRSW_GATOR:
    403 		switch (na->drhw) {
    404 		default:
    405 		case NUBUS_DRHW_INTERLAN:
    406 			vendor = DP8390_VENDOR_INTERLAN;
    407 			break;
    408 		case NUBUS_DRHW_KINETICS:
    409 			if (strncmp(nubus_get_card_name(bst, bsh, na->fmt),
    410 			    "EtherPort", 9) == 0)
    411 				vendor = DP8390_VENDOR_KINETICS;
    412 			else
    413 				vendor = DP8390_VENDOR_DAYNA;
    414 			break;
    415 		}
    416 		break;
    417 	default:
    418 		vendor = DP8390_VENDOR_UNKNOWN;
    419 	}
    420 	return vendor;
    421 }
    422 
    423 static int
    424 ae_nb_get_enaddr(bst, bsh, na, ep)
    425 	bus_space_tag_t bst;
    426 	bus_space_handle_t bsh;
    427 	struct nubus_attach_args *na;
    428 	u_int8_t *ep;
    429 {
    430 	nubus_dir dir;
    431 	nubus_dirent dirent;
    432 
    433 	/*
    434 	 * XXX - note hardwired resource IDs here (0x80); these are
    435 	 * assumed to be used by all cards, but should be fixed when
    436 	 * we find out more about Ethernet card resources.
    437 	 */
    438 	nubus_get_main_dir(na->fmt, &dir);
    439 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    440 		return 1;
    441 	nubus_get_dir_from_rsrc(na->fmt, &dirent, &dir);
    442 	if (nubus_find_rsrc(bst, bsh, na->fmt, &dir, 0x80, &dirent) <= 0)
    443 		return 1;
    444 	if (nubus_get_ind_data(bst, bsh,
    445 	    na->fmt, &dirent, ep, ETHER_ADDR_LEN) <= 0)
    446 		return 1;
    447 
    448 	return 0;
    449 }
    450 
    451 #ifdef DEBUG
    452 static void
    453 ae_nb_watchdog(ifp)
    454 	struct ifnet *ifp;
    455 {
    456 	struct dp8390_softc *sc = ifp->if_softc;
    457 
    458 /*
    459  * This is a kludge!  The via code seems to miss slot interrupts
    460  * sometimes.  This kludges around that by calling the handler
    461  * by hand if the watchdog is activated. -- XXX (akb)
    462  */
    463 	(*via2itab[1])((void *) 1);
    464 
    465 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    466 	++ifp->if_oerrors;
    467 
    468 	dp8390_reset(sc);
    469 }
    470 #endif
    471