Home | History | Annotate | Line # | Download | only in sbd
if_le_sbdio.c revision 1.4
      1 /*	$NetBSD: if_le_sbdio.c,v 1.4 2008/04/04 12:25:06 tsutsui 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.4 2008/04/04 12:25:06 tsutsui 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(device_t, struct cfdata *, void *);
     85 void le_sbdio_attach(device_t, 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_NEW(le_sbdio, sizeof(struct le_sbdio_softc),
     90     le_sbdio_match, le_sbdio_attach, NULL, NULL);
     91 
     92 int
     93 le_sbdio_match(device_t parent, cfdata_t 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(device_t parent, device_t self, void *aux)
    102 {
    103 	struct le_sbdio_softc *lesc = device_private(self);
    104 	struct sbdio_attach_args *sa = aux;
    105 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    106 	bus_dma_segment_t seg;
    107 	int rseg;
    108 
    109 	sc->sc_dev = self;
    110 	lesc->sc_dmat = sa->sa_dmat;
    111 	lesc->sc_bst  = sa->sa_bust;
    112 
    113 	if (bus_space_map(lesc->sc_bst, sa->sa_addr1, 8 /* XXX */,
    114 	    BUS_SPACE_MAP_LINEAR, &lesc->sc_bsh) != 0) {
    115 		aprint_error(": cannot map registers\n");
    116 		return;
    117 	}
    118 
    119 	/* Allocate DMA memory for the chip. */
    120 	if (bus_dmamem_alloc(lesc->sc_dmat, LE_MEMSIZE, 0, 0, &seg, 1, &rseg,
    121 	    BUS_DMA_NOWAIT) != 0) {
    122 		aprint_error(": can't allocate DMA memory\n");
    123 		return;
    124 	}
    125 	if (bus_dmamem_map(lesc->sc_dmat, &seg, rseg, LE_MEMSIZE,
    126 	    (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0) {
    127 		aprint_error(": can't map DMA memory\n");
    128 		return;
    129 	}
    130 	if (bus_dmamap_create(lesc->sc_dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
    131 	    0, BUS_DMA_NOWAIT, &lesc->sc_dmamap) != 0) {
    132 		aprint_error(": can't create DMA map\n");
    133 		return;
    134 	}
    135 	if (bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmamap, sc->sc_mem,
    136 	    LE_MEMSIZE, NULL, BUS_DMA_NOWAIT) != 0) {
    137 		aprint_error(": can't load DMA map\n");
    138 		return;
    139 	}
    140 
    141 	sc->sc_memsize = LE_MEMSIZE;
    142 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
    143 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_BCON;
    144 	(*platform.ether_addr)(sc->sc_enaddr);
    145 
    146 	sc->sc_copytodesc = lance_copytobuf_contig;
    147 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    148 	sc->sc_copytobuf = lance_copytobuf_contig;
    149 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    150 	sc->sc_zerobuf = lance_zerobuf_contig;
    151 #ifdef LEDEBUG
    152 	sc->sc_debug = 0xff;
    153 #endif
    154 	sc->sc_rdcsr = le_sbdio_rdcsr;
    155 	sc->sc_wrcsr = le_sbdio_wrcsr;
    156 
    157 	am7990_config(&lesc->sc_am7990);
    158 	intr_establish(sa->sa_irq, am7990_intr, sc);
    159 }
    160 
    161 static void
    162 le_sbdio_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    163 {
    164 	struct le_sbdio_softc *lesc = (struct le_sbdio_softc *)sc;
    165 
    166 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
    167 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP, val);
    168 }
    169 
    170 static uint16_t
    171 le_sbdio_rdcsr(struct lance_softc *sc, uint16_t port)
    172 {
    173 	struct le_sbdio_softc *lesc = (struct le_sbdio_softc *)sc;
    174 
    175 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
    176 	return bus_space_read_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP);
    177 }
    178