Home | History | Annotate | Line # | Download | only in ofisa
if_cs_ofisa.c revision 1.1
      1 /*	$NetBSD: if_cs_ofisa.c,v 1.1 1998/07/27 01:26:43 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/socket.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_ether.h>
     48 #include <net/if_media.h>
     49 #ifdef INET
     50 #include <netinet/in.h>
     51 #include <netinet/if_inarp.h>
     52 #endif
     53 
     54 #include <machine/bus.h>
     55 #include <machine/intr.h>
     56 
     57 #include <dev/ofw/openfirm.h>
     58 #include <dev/isa/isavar.h>
     59 #include <dev/ofisa/ofisavar.h>
     60 
     61 #include <dev/isa/cs89x0reg.h>
     62 #include <dev/isa/cs89x0var.h>
     63 
     64 int	cs_ofisa_match __P((struct device *, struct cfdata *, void *));
     65 void	cs_ofisa_attach __P((struct device *, struct device *, void *));
     66 
     67 struct cfattach cs_ofisa_ca = {
     68 	sizeof(struct cs_softc), cs_ofisa_match, cs_ofisa_attach
     69 };
     70 
     71 int
     72 cs_ofisa_match(parent, cf, aux)
     73 	struct device *parent;
     74 	struct cfdata *cf;
     75 	void *aux;
     76 {
     77 	struct ofisa_attach_args *aa = aux;
     78 	const char *compatible_strings[] = {
     79 		"CRUS,CS8900",
     80 		/* XXX CS8920, CS8920M? */
     81 		/* XXX PNP names? */
     82 		NULL,
     83 	};
     84 	int rv = 0;
     85 
     86 	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1)
     87 		rv = 1;
     88 
     89 	return (rv);
     90 }
     91 
     92 void
     93 cs_ofisa_attach(parent, self, aux)
     94 	struct device *parent, *self;
     95 	void *aux;
     96 {
     97 	struct cs_softc *sc = (struct cs_softc *) self;
     98 	struct ofisa_attach_args *aa = aux;
     99 	struct ofisa_reg_desc reg[2];
    100 	struct ofisa_intr_desc intr;
    101 	struct ofisa_dma_desc dma;
    102 	int i, n, *media, nmedia, defmedia;
    103 	bus_addr_t io_addr, mem_addr;
    104 	char *model = NULL;
    105 	const char *message = NULL;
    106 	u_int8_t enaddr[6];
    107 
    108 	sc->sc_ic = aa->ic;
    109 	sc->sc_iot = aa->iot;
    110 	sc->sc_memt = aa->memt;
    111 
    112 	/*
    113 	 * We're living on an OFW.  We have to ask the OFW what our
    114 	 * registers and interrupts properties look like.
    115 	 *
    116 	 * We expect:
    117 	 *
    118 	 *	1 i/o register region
    119 	 *	0 or 1 memory region
    120 	 *	1 interrupt
    121 	 *	0 or 1 dma channel
    122 	 */
    123 
    124 	io_addr = mem_addr = -1;
    125 
    126 	n = ofisa_reg_get(aa->oba.oba_phandle, reg, 2);
    127 	if (n < 1 || n > 2) {
    128 		printf(": error getting register data\n");
    129 		return;
    130 	}
    131 
    132 	for (i = 0; i < n; i++) {
    133 		if (reg[i].type == OFISA_REG_TYPE_IO) {
    134 			if (io_addr != (bus_addr_t) -1) {
    135 				printf(": multiple I/O regions\n");
    136 				return;
    137 			}
    138 			if (reg[i].len != CS8900_IOSIZE) {
    139 				printf(": weird register size (%lu, expected %d)\n",
    140 				    (unsigned long)reg[i].len, CS8900_IOSIZE);
    141 				return;
    142 			}
    143 			io_addr = reg[i].addr;
    144 		} else {
    145 			if (mem_addr != (bus_addr_t) -1) {
    146 				printf(": multiple memory regions\n");
    147 				return;
    148 			}
    149 			if (reg[i].len != CS8900_MEMSIZE) {
    150 				printf(": weird register size (%lu, expected %d)\n",
    151 				    (unsigned long)reg[i].len, CS8900_MEMSIZE);
    152 				return;
    153 			}
    154 			mem_addr = reg[i].addr;
    155 		}
    156 	}
    157 
    158 	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
    159 	if (n != 1) {
    160 		printf(": error getting interrupt data\n");
    161 		return;
    162 	}
    163 	sc->sc_irq = intr.irq;
    164 
    165 	if (CS8900_IRQ_ISVALID(sc->sc_irq) == 0) {
    166 		printf(": invalid IRQ %d\n", sc->sc_irq);
    167 		return;
    168 	}
    169 
    170 	sc->sc_drq = ISACF_DRQ_DEFAULT;
    171 	if (OF_getproplen(aa->oba.oba_phandle, "no-dma") < 0 &&
    172 	    ofisa_dma_get(aa->oba.oba_phandle, &dma, 1) == 1) {
    173 #ifdef SHARK	/* XXX machdep ofw hook */
    174 		sc->sc_drq = 6;
    175 #else
    176 		sc->sc_drq = dma.drq;
    177 #endif
    178 	}
    179 
    180 	if (io_addr == (bus_addr_t) -1) {
    181 		printf(": no I/O space\n");
    182 		return;
    183 	}
    184 	if (bus_space_map(sc->sc_iot, io_addr, CS8900_IOSIZE, 0,
    185 	    &sc->sc_ioh)) {
    186 		printf(": unable to map register space\n");
    187 		return;
    188 	}
    189 
    190 	if (mem_addr != (bus_addr_t) -1) {
    191 		if (bus_space_map(sc->sc_memt, mem_addr, CS8900_MEMSIZE, 0,
    192 		    &sc->sc_memh)) {
    193 			message = "unable to map memory space";
    194 		} else {
    195 			sc->sc_cfgflags |= CFGFLG_MEM_MODE;
    196 			sc->sc_pktpgaddr = mem_addr;
    197 		}
    198 	}
    199 
    200 	/* Dig MAC address out of the firmware. */
    201 	if (OF_getprop(aa->oba.oba_phandle, "mac-address", enaddr,
    202 	    sizeof(enaddr)) < 0) {
    203 		printf(": unable to get Ethernet address\n");
    204 		return;
    205 	}
    206 
    207 	/* Dig media out of the firmware. */
    208 	media = of_network_decode_media(aa->oba.oba_phandle, &nmedia,
    209 	    &defmedia);
    210 	if (media == NULL) {
    211 		printf(": unable to get media information\n");
    212 		return;
    213 	}
    214 
    215 	n = OF_getproplen(aa->oba.oba_phandle, "model");
    216 	if (n > 0) {
    217 		model = alloca(n);
    218 		if (OF_getprop(aa->oba.oba_phandle, "model", model, n) != n)
    219 			model = NULL;	/* Safe; alloca is on-stack */
    220 	}
    221 	if (model != NULL)
    222 		printf(": %s\n", model);
    223 	else
    224 		printf("\n");
    225 
    226 	if (message != NULL)
    227 		printf("%s: %s\n", sc->sc_dev.dv_xname, message);
    228 
    229 	if (defmedia == -1) {
    230 		printf("%s: unable to get default media\n",
    231 		    sc->sc_dev.dv_xname);
    232 		defmedia = media[0];	/* XXX What to do? */
    233 	}
    234 
    235 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, intr.share,
    236 	    IPL_NET, cs_intr, sc);
    237 	if (sc->sc_ih == NULL) {
    238 		printf("%s: unable to establish interrupt\n",
    239 		    sc->sc_dev.dv_xname);
    240 		return;
    241 	}
    242 
    243 #ifdef SHARK	/* XXX machdep ofw hook */
    244 	sc->sc_cfgflags |= CFGFLG_USE_SA|CFGFLG_IOCHRDY|CFGFLG_NOT_EEPROM;
    245 #endif
    246 
    247 	cs_attach(sc, enaddr, media, nmedia, defmedia);
    248 
    249 	/* This is malloc'd. */
    250 	free(media, M_DEVBUF);
    251 }
    252