Home | History | Annotate | Line # | Download | only in sbd
if_le_sbdio.c revision 1.2
      1 /*	$NetBSD: if_le_sbdio.c,v 1.2 2007/03/04 05:59:47 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass and Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: if_le_sbdio.c,v 1.2 2007/03/04 05:59:47 christos Exp $");
     41 
     42 #include "opt_inet.h"
     43 #include "bpfilter.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/syslog.h>
     49 #include <sys/socket.h>
     50 #include <sys/device.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_media.h>
     55 
     56 #ifdef INET
     57 #include <netinet/in.h>
     58 #include <netinet/if_inarp.h>
     59 #endif
     60 
     61 #include <machine/bus.h>
     62 #include <machine/intr.h>
     63 #include <machine/sbdiovar.h>
     64 #include <machine/sbdvar.h>	/* for ether_addr() */
     65 
     66 #include <dev/ic/lancereg.h>
     67 #include <dev/ic/lancevar.h>
     68 #include <dev/ic/am7990reg.h>
     69 #include <dev/ic/am7990var.h>
     70 
     71 #define	LEREG1_RDP	0	/* offset to lance data register */
     72 #define	LEREG1_RAP	6	/* offset to lance address regsiter */
     73 #define	LE_MEMSIZE	(64 * 1024)
     74 
     75 struct le_sbdio_softc {
     76 	struct am7990_softc sc_am7990;
     77 
     78 	bus_space_tag_t sc_bst;
     79 	bus_space_handle_t sc_bsh;
     80 	bus_dma_tag_t sc_dmat;
     81 	bus_dmamap_t sc_dmamap;
     82 };
     83 
     84 int le_sbdio_match(struct device *, struct cfdata *, void *);
     85 void le_sbdio_attach(struct device *, struct device *, void *);
     86 static void le_sbdio_wrcsr(struct lance_softc *, uint16_t, uint16_t);
     87 static uint16_t le_sbdio_rdcsr(struct lance_softc *, uint16_t);
     88 
     89 CFATTACH_DECL(le_sbdio, sizeof(struct le_sbdio_softc),
     90     le_sbdio_match, le_sbdio_attach, NULL, NULL);
     91 
     92 int
     93 le_sbdio_match(struct device *parent, struct cfdata *cf, void *aux)
     94 {
     95 	struct sbdio_attach_args *sa = aux;
     96 
     97 	return strcmp(sa->sa_name, "lance") ? 0 : 1;
     98 }
     99 
    100 void
    101 le_sbdio_attach(struct device *parent, struct device *self, void *aux)
    102 {
    103 	struct sbdio_attach_args *sa = aux;
    104 	struct le_sbdio_softc *lesc = (void *)self;
    105 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    106 	bus_dma_segment_t seg;
    107 	int rseg;
    108 
    109 	lesc->sc_dmat = sa->sa_dmat;
    110 	lesc->sc_bst  = sa->sa_bust;
    111 
    112 	if (bus_space_map(lesc->sc_bst, sa->sa_addr1, 8 /* XXX */,
    113 	    BUS_SPACE_MAP_LINEAR, &lesc->sc_bsh) != 0) {
    114 		printf(": cannot map registers\n");
    115 		return;
    116 	}
    117 
    118 	/* Allocate DMA memory for the chip. */
    119 	if (bus_dmamem_alloc(lesc->sc_dmat, LE_MEMSIZE, 0, 0, &seg, 1, &rseg,
    120 	    BUS_DMA_NOWAIT) != 0) {
    121 		printf(": can't allocate DMA memory\n");
    122 		return;
    123 	}
    124 	if (bus_dmamem_map(lesc->sc_dmat, &seg, rseg, LE_MEMSIZE,
    125 	    (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0) {
    126 		printf(": can't map DMA memory\n");
    127 		return;
    128 	}
    129 	if (bus_dmamap_create(lesc->sc_dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
    130 	    0, BUS_DMA_NOWAIT, &lesc->sc_dmamap) != 0) {
    131 		printf(": can't create DMA map\n");
    132 		return;
    133 	}
    134 	if (bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmamap, sc->sc_mem,
    135 	    LE_MEMSIZE, NULL, BUS_DMA_NOWAIT) != 0) {
    136 		printf(": can't load DMA map\n");
    137 		return;
    138 	}
    139 
    140 	sc->sc_memsize = LE_MEMSIZE;
    141 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
    142 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_BCON;
    143 	(*platform.ether_addr)(sc->sc_enaddr);
    144 
    145 	sc->sc_copytodesc = lance_copytobuf_contig;
    146 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    147 	sc->sc_copytobuf = lance_copytobuf_contig;
    148 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    149 	sc->sc_zerobuf = lance_zerobuf_contig;
    150 #ifdef LEDEBUG
    151 	sc->sc_debug = 0xff;
    152 #endif
    153 	sc->sc_rdcsr = le_sbdio_rdcsr;
    154 	sc->sc_wrcsr = le_sbdio_wrcsr;
    155 
    156 	printf(" at %p irq %d", (void *)sa->sa_addr1, sa->sa_irq);
    157 
    158 	am7990_config(&lesc->sc_am7990);
    159 	intr_establish(sa->sa_irq, am7990_intr, self);
    160 }
    161 
    162 static void
    163 le_sbdio_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    164 {
    165 	struct le_sbdio_softc *lesc = (void *)sc;
    166 
    167 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
    168 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP, val);
    169 }
    170 
    171 static uint16_t
    172 le_sbdio_rdcsr(struct lance_softc *sc, uint16_t port)
    173 {
    174 	struct le_sbdio_softc *lesc = (void *)sc;
    175 
    176 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
    177 	return bus_space_read_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP);
    178 }
    179