Home | History | Annotate | Line # | Download | only in sbd
if_le_sbdio.c revision 1.4.2.1
      1 /*	$NetBSD: if_le_sbdio.c,v 1.4.2.1 2008/05/18 12:31:54 yamt 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_le_sbdio.c,v 1.4.2.1 2008/05/18 12:31:54 yamt Exp $");
     34 
     35 #include "opt_inet.h"
     36 #include "bpfilter.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/syslog.h>
     42 #include <sys/socket.h>
     43 #include <sys/device.h>
     44 
     45 #include <net/if.h>
     46 #include <net/if_ether.h>
     47 #include <net/if_media.h>
     48 
     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 #include <machine/sbdiovar.h>
     57 #include <machine/sbdvar.h>	/* for ether_addr() */
     58 
     59 #include <dev/ic/lancereg.h>
     60 #include <dev/ic/lancevar.h>
     61 #include <dev/ic/am7990reg.h>
     62 #include <dev/ic/am7990var.h>
     63 
     64 #define	LEREG1_RDP	0	/* offset to lance data register */
     65 #define	LEREG1_RAP	6	/* offset to lance address regsiter */
     66 #define	LE_MEMSIZE	(64 * 1024)
     67 
     68 struct le_sbdio_softc {
     69 	struct am7990_softc sc_am7990;
     70 
     71 	bus_space_tag_t sc_bst;
     72 	bus_space_handle_t sc_bsh;
     73 	bus_dma_tag_t sc_dmat;
     74 	bus_dmamap_t sc_dmamap;
     75 };
     76 
     77 int le_sbdio_match(device_t, struct cfdata *, void *);
     78 void le_sbdio_attach(device_t, struct device *, void *);
     79 static void le_sbdio_wrcsr(struct lance_softc *, uint16_t, uint16_t);
     80 static uint16_t le_sbdio_rdcsr(struct lance_softc *, uint16_t);
     81 
     82 CFATTACH_DECL_NEW(le_sbdio, sizeof(struct le_sbdio_softc),
     83     le_sbdio_match, le_sbdio_attach, NULL, NULL);
     84 
     85 int
     86 le_sbdio_match(device_t parent, cfdata_t cf, void *aux)
     87 {
     88 	struct sbdio_attach_args *sa = aux;
     89 
     90 	return strcmp(sa->sa_name, "lance") ? 0 : 1;
     91 }
     92 
     93 void
     94 le_sbdio_attach(device_t parent, device_t self, void *aux)
     95 {
     96 	struct le_sbdio_softc *lesc = device_private(self);
     97 	struct sbdio_attach_args *sa = aux;
     98 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
     99 	bus_dma_segment_t seg;
    100 	int rseg;
    101 
    102 	sc->sc_dev = self;
    103 	lesc->sc_dmat = sa->sa_dmat;
    104 	lesc->sc_bst  = sa->sa_bust;
    105 
    106 	if (bus_space_map(lesc->sc_bst, sa->sa_addr1, 8 /* XXX */,
    107 	    BUS_SPACE_MAP_LINEAR, &lesc->sc_bsh) != 0) {
    108 		aprint_error(": cannot map registers\n");
    109 		return;
    110 	}
    111 
    112 	/* Allocate DMA memory for the chip. */
    113 	if (bus_dmamem_alloc(lesc->sc_dmat, LE_MEMSIZE, 0, 0, &seg, 1, &rseg,
    114 	    BUS_DMA_NOWAIT) != 0) {
    115 		aprint_error(": can't allocate DMA memory\n");
    116 		return;
    117 	}
    118 	if (bus_dmamem_map(lesc->sc_dmat, &seg, rseg, LE_MEMSIZE,
    119 	    (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0) {
    120 		aprint_error(": can't map DMA memory\n");
    121 		return;
    122 	}
    123 	if (bus_dmamap_create(lesc->sc_dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
    124 	    0, BUS_DMA_NOWAIT, &lesc->sc_dmamap) != 0) {
    125 		aprint_error(": can't create DMA map\n");
    126 		return;
    127 	}
    128 	if (bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmamap, sc->sc_mem,
    129 	    LE_MEMSIZE, NULL, BUS_DMA_NOWAIT) != 0) {
    130 		aprint_error(": can't load DMA map\n");
    131 		return;
    132 	}
    133 
    134 	sc->sc_memsize = LE_MEMSIZE;
    135 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
    136 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_BCON;
    137 	(*platform.ether_addr)(sc->sc_enaddr);
    138 
    139 	sc->sc_copytodesc = lance_copytobuf_contig;
    140 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    141 	sc->sc_copytobuf = lance_copytobuf_contig;
    142 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    143 	sc->sc_zerobuf = lance_zerobuf_contig;
    144 #ifdef LEDEBUG
    145 	sc->sc_debug = 0xff;
    146 #endif
    147 	sc->sc_rdcsr = le_sbdio_rdcsr;
    148 	sc->sc_wrcsr = le_sbdio_wrcsr;
    149 
    150 	am7990_config(&lesc->sc_am7990);
    151 	intr_establish(sa->sa_irq, am7990_intr, sc);
    152 }
    153 
    154 static void
    155 le_sbdio_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
    156 {
    157 	struct le_sbdio_softc *lesc = (struct le_sbdio_softc *)sc;
    158 
    159 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
    160 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP, val);
    161 }
    162 
    163 static uint16_t
    164 le_sbdio_rdcsr(struct lance_softc *sc, uint16_t port)
    165 {
    166 	struct le_sbdio_softc *lesc = (struct le_sbdio_softc *)sc;
    167 
    168 	bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
    169 	return bus_space_read_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP);
    170 }
    171