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