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