Home | History | Annotate | Line # | Download | only in ofisa
if_cs_ofisa.c revision 1.17.2.1
      1 /*	$NetBSD: if_cs_ofisa.c,v 1.17.2.1 2008/05/18 12:34:18 yamt 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_cs_ofisa.c,v 1.17.2.1 2008/05/18 12:34:18 yamt Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/socket.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 
     42 #include "rnd.h"
     43 #if NRND > 0
     44 #include <sys/rnd.h>
     45 #endif
     46 
     47 #include <net/if.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 #ifdef INET
     51 #include <netinet/in.h>
     52 #include <netinet/if_inarp.h>
     53 #endif
     54 
     55 #include <sys/bus.h>
     56 #include <sys/intr.h>
     57 
     58 #include <dev/ofw/openfirm.h>
     59 #include <dev/isa/isavar.h>
     60 #include <dev/ofisa/ofisavar.h>
     61 
     62 #include <dev/ic/cs89x0reg.h>
     63 #include <dev/ic/cs89x0var.h>
     64 #include <dev/isa/cs89x0isavar.h>
     65 
     66 int	cs_ofisa_match(struct device *, struct cfdata *, void *);
     67 void	cs_ofisa_attach(struct device *, struct device *, void *);
     68 
     69 CFATTACH_DECL(cs_ofisa, sizeof(struct cs_softc_isa),
     70     cs_ofisa_match, cs_ofisa_attach, NULL, NULL);
     71 
     72 int
     73 cs_ofisa_match(parent, cf, aux)
     74 	struct device *parent;
     75 	struct cfdata *cf;
     76 	void *aux;
     77 {
     78 	struct ofisa_attach_args *aa = aux;
     79 	static const char *const compatible_strings[] = {
     80 		"CRUS,CS8900",
     81 		/* XXX CS8920, CS8920M? */
     82 		/* XXX PNP names? */
     83 		NULL,
     84 	};
     85 	int rv = 0;
     86 
     87 	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1)
     88 		rv = 5;
     89 #ifdef _CS_OFISA_MD_MATCH
     90 	if (rv == 0)
     91 		rv = cs_ofisa_md_match(parent, cf, aux);
     92 #endif
     93 	return (rv);
     94 }
     95 
     96 void
     97 cs_ofisa_attach(parent, self, aux)
     98 	struct device *parent, *self;
     99 	void *aux;
    100 {
    101 	struct cs_softc *sc = device_private(self);
    102 	struct cs_softc_isa *isc = (void *)sc;
    103 	struct ofisa_attach_args *aa = aux;
    104 	struct ofisa_reg_desc reg[2];
    105 	struct ofisa_intr_desc intr;
    106 	struct ofisa_dma_desc dma;
    107 	int i, n, *media, nmedia, defmedia;
    108 	bus_addr_t io_addr, mem_addr;
    109 	char *model = NULL;
    110 	const char *message = NULL;
    111 	u_int8_t enaddr[6];
    112 
    113 	isc->sc_ic = aa->ic;
    114 	sc->sc_iot = aa->iot;
    115 	sc->sc_memt = aa->memt;
    116 
    117 	/*
    118 	 * We're living on an OFW.  We have to ask the OFW what our
    119 	 * registers and interrupts properties look like.
    120 	 *
    121 	 * We expect:
    122 	 *
    123 	 *	1 i/o register region
    124 	 *	0 or 1 memory region
    125 	 *	1 interrupt
    126 	 *	0 or 1 DMA channel
    127 	 */
    128 
    129 	io_addr = mem_addr = -1;
    130 
    131 	n = ofisa_reg_get(aa->oba.oba_phandle, reg, 2);
    132 #ifdef _CS_OFISA_MD_REG_FIXUP
    133 	n = cs_ofisa_md_reg_fixup(parent, self, aux, reg, 2, n);
    134 #endif
    135 	if (n < 1 || n > 2) {
    136 		printf(": error getting register data\n");
    137 		return;
    138 	}
    139 
    140 	for (i = 0; i < n; i++) {
    141 		if (reg[i].type == OFISA_REG_TYPE_IO) {
    142 			if (io_addr != (bus_addr_t) -1) {
    143 				printf(": multiple I/O regions\n");
    144 				return;
    145 			}
    146 			if (reg[i].len != CS8900_IOSIZE) {
    147 				printf(": weird register size (%lu, expected %d)\n",
    148 				    (unsigned long)reg[i].len, CS8900_IOSIZE);
    149 				return;
    150 			}
    151 			io_addr = reg[i].addr;
    152 		} else {
    153 			if (mem_addr != (bus_addr_t) -1) {
    154 				printf(": multiple memory regions\n");
    155 				return;
    156 			}
    157 			if (reg[i].len != CS8900_MEMSIZE) {
    158 				printf(": weird register size (%lu, expected %d)\n",
    159 				    (unsigned long)reg[i].len, CS8900_MEMSIZE);
    160 				return;
    161 			}
    162 			mem_addr = reg[i].addr;
    163 		}
    164 	}
    165 
    166 	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
    167 #ifdef _CS_OFISA_MD_INTR_FIXUP
    168 	n = cs_ofisa_md_intr_fixup(parent, self, aux, &intr, 1, n);
    169 #endif
    170 	if (n != 1) {
    171 		printf(": error getting interrupt data\n");
    172 		return;
    173 	}
    174 	sc->sc_irq = intr.irq;
    175 
    176 	if (CS8900_IRQ_ISVALID(sc->sc_irq) == 0) {
    177 		printf(": invalid IRQ %d\n", sc->sc_irq);
    178 		return;
    179 	}
    180 
    181 	isc->sc_drq = -1;
    182 	n = ofisa_dma_get(aa->oba.oba_phandle, &dma, 1);
    183 #ifdef _CS_OFISA_MD_DMA_FIXUP
    184 	n = cs_ofisa_md_dma_fixup(parent, self, aux, &dma, 1, n);
    185 #endif
    186 	if (n == 1)
    187 		isc->sc_drq = dma.drq;
    188 
    189 	if (io_addr == (bus_addr_t) -1) {
    190 		printf(": no I/O space\n");
    191 		return;
    192 	}
    193 	if (bus_space_map(sc->sc_iot, io_addr, CS8900_IOSIZE, 0,
    194 	    &sc->sc_ioh)) {
    195 		printf(": unable to map register space\n");
    196 		return;
    197 	}
    198 
    199 	if (mem_addr != (bus_addr_t) -1) {
    200 		if (bus_space_map(sc->sc_memt, mem_addr, CS8900_MEMSIZE, 0,
    201 		    &sc->sc_memh)) {
    202 			message = "unable to map memory space";
    203 		} else {
    204 			sc->sc_cfgflags |= CFGFLG_MEM_MODE;
    205 			sc->sc_pktpgaddr = mem_addr;
    206 		}
    207 	}
    208 
    209 	/* Dig MAC address out of the firmware. */
    210 	if (OF_getprop(aa->oba.oba_phandle, "mac-address", enaddr,
    211 	    sizeof(enaddr)) < 0) {
    212 		printf(": unable to get Ethernet address\n");
    213 		return;
    214 	}
    215 
    216 	/* Dig media out of the firmware. */
    217 	media = of_network_decode_media(aa->oba.oba_phandle, &nmedia,
    218 	    &defmedia);
    219 #ifdef _CS_OFISA_MD_MEDIA_FIXUP
    220 	media = cs_ofisa_md_media_fixup(parent, self, aux, media, &nmedia,
    221 	    &defmedia);
    222 #endif
    223 	if (media == NULL) {
    224 		printf(": unable to get media information\n");
    225 		return;
    226 	}
    227 
    228 	n = OF_getproplen(aa->oba.oba_phandle, "model");
    229 	if (n > 0) {
    230 		model = alloca(n);
    231 		if (OF_getprop(aa->oba.oba_phandle, "model", model, n) != n)
    232 			model = NULL;	/* Safe; alloca is on-stack */
    233 	}
    234 	if (model != NULL)
    235 		printf(": %s\n", model);
    236 	else
    237 		printf("\n");
    238 
    239 	if (message != NULL)
    240 		printf("%s: %s\n", device_xname(&sc->sc_dev), message);
    241 
    242 	if (defmedia == -1) {
    243 		aprint_error_dev(&sc->sc_dev, "unable to get default media\n");
    244 		defmedia = media[0];	/* XXX What to do? */
    245 	}
    246 
    247 	sc->sc_ih = isa_intr_establish(isc->sc_ic, sc->sc_irq, intr.share,
    248 	    IPL_NET, cs_intr, sc);
    249 	if (sc->sc_ih == NULL) {
    250 		aprint_error_dev(&sc->sc_dev, "unable to establish interrupt\n");
    251 		return;
    252 	}
    253 
    254 #ifdef _CS_OFISA_MD_CFGFLAGS_FIXUP
    255 	sc->sc_cfgflags |= cs_ofisa_md_cfgflags_fixup(parent, self, aux);
    256 #endif
    257 
    258 	sc->sc_dma_chipinit = cs_isa_dma_chipinit;
    259 	sc->sc_dma_attach = cs_isa_dma_attach;
    260 	sc->sc_dma_process_rx = cs_process_rx_dma;
    261 
    262 	cs_attach(sc, enaddr, media, nmedia, defmedia);
    263 
    264 	/* This is malloc'd. */
    265 	free(media, M_DEVBUF);
    266 }
    267