Home | History | Annotate | Line # | Download | only in dev
      1  1.29      rin /*	$NetBSD: if_le_obio.c,v 1.29 2022/05/29 10:45:05 rin Exp $	*/
      2   1.1       pk 
      3   1.1       pk /*-
      4   1.4  mycroft  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1       pk  * All rights reserved.
      6   1.1       pk  *
      7   1.1       pk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.5       pk  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
      9   1.5       pk  * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
     10   1.1       pk  *
     11   1.1       pk  * Redistribution and use in source and binary forms, with or without
     12   1.1       pk  * modification, are permitted provided that the following conditions
     13   1.1       pk  * are met:
     14   1.1       pk  * 1. Redistributions of source code must retain the above copyright
     15   1.1       pk  *    notice, this list of conditions and the following disclaimer.
     16   1.1       pk  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       pk  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       pk  *    documentation and/or other materials provided with the distribution.
     19   1.1       pk  *
     20   1.1       pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1       pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1       pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1       pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1       pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1       pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1       pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1       pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1       pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1       pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1       pk  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1       pk  */
     32  1.19    lukem 
     33  1.19    lukem #include <sys/cdefs.h>
     34  1.29      rin __KERNEL_RCSID(0, "$NetBSD: if_le_obio.c,v 1.29 2022/05/29 10:45:05 rin Exp $");
     35   1.1       pk 
     36   1.1       pk #include <sys/param.h>
     37   1.1       pk #include <sys/systm.h>
     38   1.1       pk #include <sys/device.h>
     39   1.1       pk 
     40  1.18  thorpej #include <uvm/uvm_extern.h>
     41  1.18  thorpej 
     42   1.1       pk #include <net/if.h>
     43   1.1       pk #include <net/if_ether.h>
     44   1.1       pk #include <net/if_media.h>
     45   1.1       pk 
     46  1.27   dyoung #include <sys/bus.h>
     47   1.8       pk #include <machine/intr.h>
     48   1.1       pk #include <machine/autoconf.h>
     49   1.1       pk 
     50   1.1       pk #include <dev/ic/lancereg.h>
     51   1.1       pk #include <dev/ic/lancevar.h>
     52   1.1       pk #include <dev/ic/am7990reg.h>
     53   1.1       pk #include <dev/ic/am7990var.h>
     54   1.1       pk 
     55   1.1       pk /*
     56   1.1       pk  * LANCE registers.
     57   1.1       pk  */
     58   1.5       pk #define LEREG1_RDP	0	/* Register Data port */
     59   1.5       pk #define LEREG1_RAP	2	/* Register Address port */
     60   1.1       pk 
     61   1.1       pk struct	le_softc {
     62   1.5       pk 	struct	am7990_softc	sc_am7990;	/* glue to MI code */
     63   1.5       pk 	bus_space_tag_t		sc_bustag;
     64   1.5       pk 	bus_dma_tag_t		sc_dmatag;
     65   1.7       pk 	bus_dmamap_t		sc_dmamap;
     66   1.5       pk 	bus_space_handle_t	sc_reg;		/* LANCE registers */
     67   1.1       pk };
     68   1.1       pk 
     69   1.1       pk #define MEMSIZE 0x4000		/* LANCE memory size */
     70   1.1       pk 
     71   1.1       pk /*
     72   1.1       pk  * Media types supported.
     73   1.1       pk  */
     74   1.1       pk static int lemedia[] = {
     75  1.28  msaitoh 	IFM_ETHER | IFM_10_T,
     76   1.1       pk };
     77  1.28  msaitoh #define NLEMEDIA	__arraycount(lemedia)
     78   1.1       pk 
     79  1.25  tsutsui static int	lematch_obio(device_t, cfdata_t, void *);
     80  1.25  tsutsui static void	leattach_obio(device_t, device_t, void *);
     81  1.23      uwe 
     82  1.25  tsutsui CFATTACH_DECL_NEW(le_obio, sizeof(struct le_softc),
     83  1.16  thorpej     lematch_obio, leattach_obio, NULL, NULL);
     84   1.1       pk 
     85   1.1       pk 
     86  1.22      uwe static void lewrcsr(struct lance_softc *, uint16_t, uint16_t);
     87  1.22      uwe static uint16_t lerdcsr(struct lance_softc *, uint16_t);
     88   1.1       pk 
     89   1.1       pk static void
     90  1.22      uwe lewrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
     91   1.1       pk {
     92   1.5       pk 	struct le_softc *lesc = (struct le_softc *)sc;
     93  1.20       pk 	bus_space_tag_t t = lesc->sc_bustag;
     94  1.20       pk 	bus_space_handle_t h = lesc->sc_reg;
     95   1.1       pk 
     96  1.20       pk 	bus_space_write_2(t, h, LEREG1_RAP, port);
     97  1.20       pk 	bus_space_write_2(t, h, LEREG1_RDP, val);
     98   1.1       pk }
     99   1.1       pk 
    100  1.22      uwe static uint16_t
    101  1.22      uwe lerdcsr(struct lance_softc *sc, uint16_t port)
    102   1.1       pk {
    103   1.5       pk 	struct le_softc *lesc = (struct le_softc *)sc;
    104  1.20       pk 	bus_space_tag_t t = lesc->sc_bustag;
    105  1.20       pk 	bus_space_handle_t h = lesc->sc_reg;
    106   1.1       pk 
    107  1.20       pk 	bus_space_write_2(t, h, LEREG1_RAP, port);
    108  1.28  msaitoh 	return bus_space_read_2(t, h, LEREG1_RDP);
    109   1.1       pk }
    110   1.1       pk 
    111  1.23      uwe static int
    112  1.25  tsutsui lematch_obio(device_t parent, cfdata_t cf, void *aux)
    113   1.1       pk {
    114   1.1       pk 	union obio_attach_args *uoba = aux;
    115   1.1       pk 	struct obio4_attach_args *oba;
    116   1.1       pk 
    117   1.1       pk 	if (uoba->uoba_isobio4 == 0)
    118  1.28  msaitoh 		return 0;
    119   1.1       pk 
    120   1.1       pk 	oba = &uoba->uoba_oba4;
    121  1.28  msaitoh 	return bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    122   1.1       pk 				2,	/* probe size */
    123   1.1       pk 				0,	/* offset */
    124   1.1       pk 				0,	/* flags */
    125  1.28  msaitoh 				NULL, NULL);
    126   1.1       pk }
    127   1.1       pk 
    128  1.23      uwe static void
    129  1.25  tsutsui leattach_obio(device_t parent, device_t self, void *aux)
    130   1.1       pk {
    131   1.1       pk 	union obio_attach_args *uoba = aux;
    132   1.1       pk 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    133  1.25  tsutsui 	struct le_softc *lesc = device_private(self);
    134   1.1       pk 	struct lance_softc *sc = &lesc->sc_am7990.lsc;
    135   1.3       pk 	bus_dma_segment_t seg;
    136   1.7       pk 	bus_dma_tag_t dmatag;
    137   1.3       pk 	int rseg;
    138   1.7       pk 	int error;
    139   1.1       pk 
    140  1.25  tsutsui 	sc->sc_dev = self;
    141   1.1       pk 	lesc->sc_bustag = oba->oba_bustag;
    142   1.7       pk 	lesc->sc_dmatag = dmatag = oba->oba_dmatag;
    143   1.1       pk 
    144  1.13       pk 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    145  1.28  msaitoh 	    2 * sizeof(uint16_t), 0, &lesc->sc_reg) != 0) {
    146  1.25  tsutsui 		aprint_error(": cannot map registers\n");
    147   1.1       pk 		return;
    148   1.1       pk 	}
    149   1.1       pk 
    150   1.7       pk 	/* Get a DMA handle */
    151   1.7       pk 	if ((error = bus_dmamap_create(dmatag, MEMSIZE, 1, MEMSIZE, 0,
    152  1.28  msaitoh 	    BUS_DMA_NOWAIT|BUS_DMA_24BIT, &lesc->sc_dmamap)) != 0) {
    153  1.25  tsutsui 		aprint_error(": DMA map create error %d\n", error);
    154  1.29      rin 		goto bad_bsunmap;
    155   1.7       pk 	}
    156   1.7       pk 
    157   1.7       pk 	/* Allocate DMA buffer */
    158  1.18  thorpej 	if ((error = bus_dmamem_alloc(dmatag, MEMSIZE, PAGE_SIZE, 0,
    159  1.28  msaitoh 	    &seg, 1, &rseg, BUS_DMA_NOWAIT | BUS_DMA_24BIT)) != 0) {
    160  1.25  tsutsui 		aprint_error(": DMA memory allocation error %d\n", error);
    161  1.29      rin 		goto bad_destroy;
    162   1.3       pk 	}
    163   1.7       pk 	/* Map DMA buffer into kernel space */
    164   1.7       pk 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, MEMSIZE,
    165  1.28  msaitoh 	    (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    166  1.25  tsutsui 		aprint_error(": DMA memory map error %d\n", error);
    167  1.29      rin 		goto bad_free;
    168   1.3       pk 	}
    169   1.7       pk 	/* Load DMA buffer */
    170   1.9  thorpej 	if ((error = bus_dmamap_load(dmatag, lesc->sc_dmamap,
    171  1.28  msaitoh 	    sc->sc_mem, MEMSIZE, NULL, BUS_DMA_NOWAIT)) != 0) {
    172  1.25  tsutsui 		aprint_error(": DMA buffer map load error %d\n", error);
    173  1.29      rin 		goto bad_unmap;
    174   1.7       pk 	}
    175   1.7       pk 
    176   1.7       pk 	sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr & 0xffffff;
    177   1.1       pk 	sc->sc_memsize = MEMSIZE;
    178   1.1       pk 	sc->sc_conf3 = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
    179   1.1       pk 
    180   1.1       pk 	sc->sc_supmedia = lemedia;
    181   1.1       pk 	sc->sc_nsupmedia = NLEMEDIA;
    182   1.1       pk 	sc->sc_defaultmedia = lemedia[0];
    183   1.1       pk 
    184  1.21       pk 	prom_getether(0, sc->sc_enaddr);
    185   1.1       pk 
    186   1.1       pk 	sc->sc_copytodesc = lance_copytobuf_contig;
    187   1.1       pk 	sc->sc_copyfromdesc = lance_copyfrombuf_contig;
    188   1.1       pk 	sc->sc_copytobuf = lance_copytobuf_contig;
    189   1.1       pk 	sc->sc_copyfrombuf = lance_copyfrombuf_contig;
    190   1.1       pk 	sc->sc_zerobuf = lance_zerobuf_contig;
    191   1.1       pk 
    192   1.1       pk 	sc->sc_rdcsr = lerdcsr;
    193   1.1       pk 	sc->sc_wrcsr = lewrcsr;
    194   1.1       pk 
    195   1.1       pk 	am7990_config(&lesc->sc_am7990);
    196   1.1       pk 
    197   1.1       pk 	/* Install interrupt */
    198  1.17       pk 	(void)bus_intr_establish(lesc->sc_bustag, oba->oba_pri, IPL_NET,
    199  1.28  msaitoh 	    am7990_intr, sc);
    200  1.29      rin 
    201  1.29      rin 	return;
    202  1.29      rin 
    203  1.29      rin  bad_unmap:
    204  1.29      rin 	bus_dmamem_unmap(dmatag, (void *)sc->sc_mem, MEMSIZE);
    205  1.29      rin  bad_free:
    206  1.29      rin 	bus_dmamem_free(lesc->sc_dmatag, &seg, rseg);
    207  1.29      rin  bad_destroy:
    208  1.29      rin 	bus_dmamap_destroy(dmatag, lesc->sc_dmamap);
    209  1.29      rin  bad_bsunmap:
    210  1.29      rin 	bus_space_unmap(oba->oba_bustag, lesc->sc_reg, 2 * sizeof(uint16_t));
    211   1.1       pk }
    212