Home | History | Annotate | Line # | Download | only in if
if_le_vsbus.c revision 1.23
      1 /*	$NetBSD: if_le_vsbus.c,v 1.23 2008/04/04 12:25:07 tsutsui 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 Charles M. Hannum.
      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 /*-
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Ralph Campbell and Rick Macklem.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: if_le_vsbus.c,v 1.23 2008/04/04 12:25:07 tsutsui Exp $");
     75 
     76 #include "opt_inet.h"
     77 #include "bpfilter.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/syslog.h>
     81 #include <sys/socket.h>
     82 #include <sys/device.h>
     83 #include <sys/reboot.h>
     84 
     85 #include <uvm/uvm_extern.h>
     86 
     87 #include <net/if.h>
     88 #include <net/if_ether.h>
     89 #include <net/if_media.h>
     90 
     91 #ifdef INET
     92 #include <netinet/in.h>
     93 #include <netinet/if_inarp.h>
     94 #endif
     95 
     96 #include <machine/cpu.h>
     97 #include <machine/sid.h>
     98 #include <machine/scb.h>
     99 #include <machine/bus.h>
    100 #include <machine/vsbus.h>
    101 
    102 #include <dev/ic/lancereg.h>
    103 #include <dev/ic/lancevar.h>
    104 #include <dev/ic/am7990reg.h>
    105 #include <dev/ic/am7990var.h>
    106 
    107 #include "ioconf.h"
    108 
    109 struct le_softc {
    110 	struct	am7990_softc sc_am7990; /* Must be first */
    111 	struct	evcnt sc_intrcnt;
    112 	bus_dmamap_t sc_dm;
    113 	volatile uint16_t *sc_rap;
    114 	volatile uint16_t *sc_rdp;
    115 };
    116 
    117 static	int	le_vsbus_match(device_t, cfdata_t, void *);
    118 static	void	le_vsbus_attach(device_t, device_t, void *);
    119 static	void	lewrcsr(struct lance_softc *, uint16_t, uint16_t);
    120 static	uint16_t lerdcsr(struct lance_softc *, uint16_t);
    121 
    122 CFATTACH_DECL_NEW(le_vsbus, sizeof(struct le_softc),
    123     le_vsbus_match, le_vsbus_attach, NULL, NULL);
    124 
    125 void
    126 lewrcsr(struct lance_softc *ls, uint16_t port, uint16_t val)
    127 {
    128 	struct le_softc * const sc = (void *)ls;
    129 
    130 	*sc->sc_rap = port;
    131 	*sc->sc_rdp = val;
    132 }
    133 
    134 uint16_t
    135 lerdcsr(struct lance_softc *ls, uint16_t port)
    136 {
    137 	struct le_softc * const sc = (void *)ls;
    138 
    139 	*sc->sc_rap = port;
    140 	return *sc->sc_rdp;
    141 }
    142 
    143 static int
    144 le_vsbus_match(device_t parent, cfdata_t cf, void *aux)
    145 {
    146 	struct vsbus_attach_args * const va = aux;
    147 	volatile uint16_t *rdp, *rap;
    148 	struct leinit initblock;
    149 	bus_dmamap_t map;
    150 	int i;
    151 	int rv = 0;
    152 	int error;
    153 
    154 	if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_53)
    155 		return 0;
    156 
    157 	error = bus_dmamap_create(va->va_dmat, sizeof(initblock), 1,
    158 	    sizeof(initblock), 0, BUS_DMA_NOWAIT, &map);
    159 	if (error) {
    160 		return 0;
    161 	}
    162 
    163 	error = bus_dmamap_load(va->va_dmat, map, &initblock,
    164 	    sizeof(initblock), NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    165 	if (error) {
    166 		bus_dmamap_destroy(va->va_dmat, map);
    167 		return 0;
    168 	}
    169 
    170 	memset(&initblock, 0, sizeof(initblock));
    171 
    172 	rdp = (uint16_t *)va->va_addr;
    173 	rap = rdp + 2;
    174 
    175 	/* Make sure the chip is stopped. */
    176 	*rap = LE_CSR0;
    177 	*rdp = LE_C0_STOP;
    178 	DELAY(100);
    179 	*rap = LE_CSR1;
    180 	*rdp = map->dm_segs->ds_addr & 0xffff;
    181 	*rap = LE_CSR2;
    182 	*rdp = (map->dm_segs->ds_addr >> 16) & 0xffff;
    183 	*rap = LE_CSR0;
    184 	*rdp = LE_C0_INIT|LE_C0_INEA;
    185 
    186 	/* Wait for initialization to finish. */
    187 	for (i = 100; i >= 0; i--) {
    188 		DELAY(1000);
    189 		/* Should have interrupted by now */
    190 		if (*rdp & LE_C0_IDON)
    191 			rv = 1;
    192 	}
    193 	*rap = LE_CSR0;
    194 	*rdp = LE_C0_STOP;
    195 
    196 	bus_dmamap_unload(va->va_dmat, map);
    197 	bus_dmamap_destroy(va->va_dmat, map);
    198 	return rv;
    199 }
    200 
    201 static void
    202 le_vsbus_attach(device_t parent, device_t self, void *aux)
    203 {
    204 	struct vsbus_attach_args * const va = aux;
    205 	struct le_softc * const sc = device_private(self);
    206 	bus_dma_segment_t seg;
    207 	int *lance_addr;
    208 	int i, err, rseg;
    209 
    210 	sc->sc_am7990.lsc.sc_dev = self;
    211 	sc->sc_rdp = (uint16_t *) vax_map_physmem(NI_BASE, 1);
    212 	sc->sc_rap = sc->sc_rdp + 2;
    213 
    214 	/*
    215 	 * MD functions.
    216 	 */
    217 	sc->sc_am7990.lsc.sc_rdcsr = lerdcsr;
    218 	sc->sc_am7990.lsc.sc_wrcsr = lewrcsr;
    219 	sc->sc_am7990.lsc.sc_nocarrier = NULL;
    220 
    221 	scb_vecalloc(va->va_cvec, (void (*)(void *)) am7990_intr, sc,
    222 		SCB_ISTACK, &sc->sc_intrcnt);
    223 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    224 		device_xname(self), "intr");
    225 
    226 	/*
    227 	 * Allocate a (DMA-safe) block for all descriptors and buffers.
    228 	 */
    229 
    230 #define ALLOCSIZ (64 * 1024)
    231 	err = bus_dmamem_alloc(va->va_dmat, ALLOCSIZ, PAGE_SIZE, 0,
    232 	    &seg, 1, &rseg, BUS_DMA_NOWAIT);
    233 	if (err) {
    234 		aprint_error(": unable to alloc buffer block: err %d\n", err);
    235 		return;
    236 	}
    237 	err = bus_dmamem_map(va->va_dmat, &seg, rseg, ALLOCSIZ,
    238 	    (void **)&sc->sc_am7990.lsc.sc_mem,
    239 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    240 	if (err) {
    241 		aprint_error(": unable to map buffer block: err %d\n", err);
    242 		bus_dmamem_free(va->va_dmat, &seg, rseg);
    243 		return;
    244 	}
    245 	bus_dmamap_create(va->va_dmat, ALLOCSIZ, rseg, ALLOCSIZ,
    246 	    0, BUS_DMA_NOWAIT, &sc->sc_dm);
    247 	if (err) {
    248 		aprint_error(": unable to create DMA map: err %d\n", err);
    249 		bus_dmamem_free(va->va_dmat, &seg, rseg);
    250 		return;
    251 	}
    252 	err = bus_dmamap_load(va->va_dmat, sc->sc_dm, sc->sc_am7990.lsc.sc_mem,
    253 	    ALLOCSIZ, NULL, BUS_DMA_NOWAIT);
    254 	if (err) {
    255 		aprint_error(": unable to load DMA map: err %d\n", err);
    256 		bus_dmamap_destroy(va->va_dmat, sc->sc_dm);
    257 		bus_dmamem_free(va->va_dmat, &seg, rseg);
    258 		return;
    259 	}
    260 	aprint_normal(" buf 0x%lx-0x%lx", sc->sc_dm->dm_segs->ds_addr,
    261 	    sc->sc_dm->dm_segs->ds_addr + sc->sc_dm->dm_segs->ds_len - 1);
    262 	sc->sc_am7990.lsc.sc_addr = sc->sc_dm->dm_segs->ds_addr & 0xffffff;
    263 	sc->sc_am7990.lsc.sc_memsize = sc->sc_dm->dm_segs->ds_len;
    264 
    265 	sc->sc_am7990.lsc.sc_copytodesc = lance_copytobuf_contig;
    266 	sc->sc_am7990.lsc.sc_copyfromdesc = lance_copyfrombuf_contig;
    267 	sc->sc_am7990.lsc.sc_copytobuf = lance_copytobuf_contig;
    268 	sc->sc_am7990.lsc.sc_copyfrombuf = lance_copyfrombuf_contig;
    269 	sc->sc_am7990.lsc.sc_zerobuf = lance_zerobuf_contig;
    270 
    271 #ifdef LEDEBUG
    272 	sc->sc_am7990.lsc.sc_debug = 1;
    273 #endif
    274 	/*
    275 	 * Get the ethernet address out of rom
    276 	 */
    277 	lance_addr = (int *)vax_map_physmem(NI_ADDR, 1);
    278 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    279 		sc->sc_am7990.lsc.sc_enaddr[i] = (u_char)lance_addr[i];
    280 	vax_unmap_physmem((vaddr_t)lance_addr, 1);
    281 
    282 	bcopy(device_xname(self), sc->sc_am7990.lsc.sc_ethercom.ec_if.if_xname,
    283 	    IFNAMSIZ);
    284 
    285 	/* Prettier printout */
    286 	aprint_normal("\n%s", device_xname(self));
    287 
    288 	am7990_config(&sc->sc_am7990);
    289 }
    290