Home | History | Annotate | Line # | Download | only in sbus
      1 /*	$NetBSD: dma_sbus.c,v 1.41 2026/01/02 23:33:58 nia 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 Paul Kranenburg.
      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) 1994 Peter Galbavy.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. The name of the author may not be used to endorse or promote products
     44  *    derived from this software without specific prior written permission.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __KERNEL_RCSID(0, "$NetBSD: dma_sbus.c,v 1.41 2026/01/02 23:33:58 nia Exp $");
     60 
     61 #include <sys/param.h>
     62 #include <sys/systm.h>
     63 #include <sys/kernel.h>
     64 #include <sys/errno.h>
     65 #include <sys/device.h>
     66 
     67 #include <sys/bus.h>
     68 #include <sys/intr.h>
     69 #include <machine/autoconf.h>
     70 
     71 #include <dev/sbus/sbusvar.h>
     72 
     73 #include <dev/ic/lsi64854reg.h>
     74 #include <dev/ic/lsi64854var.h>
     75 
     76 struct dma_softc {
     77 	struct lsi64854_softc	sc_lsi64854;	/* base device */
     78 	/* possible sbus specific stuff here */
     79 };
     80 
     81 int	dmamatch_sbus(device_t, cfdata_t, void *);
     82 void	dmaattach_sbus(device_t, device_t, void *);
     83 
     84 int	dmaprint_sbus(void *, const char *);
     85 
     86 void	*dmabus_intr_establish(
     87 		bus_space_tag_t,
     88 		int,			/*bus interrupt priority*/
     89 		int,			/*`device class' level*/
     90 		int (*)(void *),	/*handler*/
     91 		void *,			/*handler arg*/
     92 		void (*) (void));	/*optional fast trap handler*/
     93 
     94 CFATTACH_DECL_NEW(dma_sbus, sizeof(struct dma_softc),
     95     dmamatch_sbus, dmaattach_sbus, NULL, NULL);
     96 
     97 CFATTACH_DECL_NEW(ledma, sizeof(struct dma_softc),
     98     dmamatch_sbus, dmaattach_sbus, NULL, NULL);
     99 
    100 int
    101 dmaprint_sbus(void *aux, const char *busname)
    102 {
    103 	struct sbus_attach_args *sa = aux;
    104 	bus_space_tag_t t = sa->sa_bustag;
    105 	struct dma_softc *sc = t->cookie;
    106 
    107 	sa->sa_bustag = sc->sc_lsi64854.sc_bustag;	/* XXX */
    108 	sbus_print(aux, busname);	/* XXX */
    109 	sa->sa_bustag = t;		/* XXX */
    110 	return UNCONF;
    111 }
    112 
    113 int
    114 dmamatch_sbus(device_t parent, cfdata_t cf, void *aux)
    115 {
    116 	struct sbus_attach_args *sa = aux;
    117 
    118 	return strcmp(cf->cf_name, sa->sa_name) == 0 ||
    119 	    strcmp("espdma", sa->sa_name) == 0;
    120 }
    121 
    122 void
    123 dmaattach_sbus(device_t parent, device_t self, void *aux)
    124 {
    125 	struct dma_softc *dsc = device_private(self);
    126 	struct lsi64854_softc *sc = &dsc->sc_lsi64854;
    127 	struct sbus_attach_args *sa = aux;
    128 	struct sbus_softc *sbsc = device_private(parent);
    129 	bus_space_tag_t sbt;
    130 	int sbusburst, burst;
    131 	int node;
    132 
    133 	node = sa->sa_node;
    134 
    135 	sc->sc_dev = self;
    136 	sc->sc_bustag = sa->sa_bustag;
    137 	sc->sc_dmatag = sa->sa_dmatag;
    138 
    139 	/* Map registers */
    140 	if (sa->sa_npromvaddrs) {
    141 		sbus_promaddr_to_handle(sa->sa_bustag,
    142 		    sa->sa_promvaddrs[0], &sc->sc_regs);
    143 	} else {
    144 		if (sbus_bus_map(sa->sa_bustag,
    145 		    sa->sa_slot, sa->sa_offset, sa->sa_size,
    146 		    0, &sc->sc_regs) != 0) {
    147 			aprint_error(": cannot map registers\n");
    148 			return;
    149 		}
    150 	}
    151 
    152 	/*
    153 	 * Get transfer burst size from PROM and plug it into the
    154 	 * controller registers. This is needed on the Sun4m; do
    155 	 * others need it too?
    156 	 */
    157 	sbusburst = sbsc->sc_burst;
    158 	if (sbusburst == 0)
    159 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
    160 
    161 	burst = prom_getpropint(node,"burst-sizes", -1);
    162 	if (burst == -1)
    163 		/* take SBus burst sizes */
    164 		burst = sbusburst;
    165 
    166 	/* Clamp at parent's burst sizes */
    167 	burst &= sbusburst;
    168 	sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
    169 		       (burst & SBUS_BURST_16) ? 16 : 0;
    170 
    171 	if (device_is_a(self, "ledma")) {
    172 		char *cabletype;
    173 		uint32_t csr;
    174 		/*
    175 		 * Check to see which cable type is currently active and
    176 		 * set the appropriate bit in the ledma csr so that it
    177 		 * gets used. If we didn't netboot, the PROM won't have
    178 		 * the "cable-selection" property; default to TP and then
    179 		 * the user can change it via a "media" option to ifconfig.
    180 		 */
    181 		cabletype = prom_getpropstring(node, "cable-selection");
    182 		csr = L64854_GCSR(sc);
    183 		if (strcmp(cabletype, "tpe") == 0) {
    184 			csr |= E_TP_AUI;
    185 		} else if (strcmp(cabletype, "aui") == 0) {
    186 			csr &= ~E_TP_AUI;
    187 		} else {
    188 			/* assume TP if nothing there */
    189 			csr |= E_TP_AUI;
    190 		}
    191 		L64854_SCSR(sc, csr);
    192 		delay(20000);	/* manual says we need a 20ms delay */
    193 		sc->sc_channel = L64854_CHANNEL_ENET;
    194 	} else {
    195 		sc->sc_channel = L64854_CHANNEL_SCSI;
    196 	}
    197 
    198 	if ((sbt = bus_space_tag_alloc(sc->sc_bustag, dsc)) == NULL) {
    199 		aprint_error(": out of memory\n");
    200 		return;
    201 	}
    202 	sbt->sparc_intr_establish = dmabus_intr_establish;
    203 	lsi64854_attach(sc);
    204 
    205 	/* Attach children */
    206 	devhandle_t selfh = device_handle(self);
    207 	for (node = firstchild(sa->sa_node); node; node = nextsibling(node)) {
    208 		struct sbus_attach_args sax;
    209 		sbus_setup_attach_args(sbsc, sbt, sc->sc_dmatag, node, &sax);
    210 		(void)config_found(self, (void *)&sax, dmaprint_sbus,
    211 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
    212 		sbus_destroy_attach_args(&sax);
    213 	}
    214 }
    215 
    216 void *
    217 dmabus_intr_establish(bus_space_tag_t t, int pri, int level,
    218     int (*handler)(void *), void *arg, void (*fastvec)(void))
    219 {
    220 	struct lsi64854_softc *sc = t->cookie;
    221 
    222 	/* XXX - for now only le; do esp later */
    223 	if (sc->sc_channel == L64854_CHANNEL_ENET) {
    224 		sc->sc_intrchain = handler;
    225 		sc->sc_intrchainarg = arg;
    226 		handler = lsi64854_enet_intr;
    227 		arg = sc;
    228 	}
    229 	return bus_intr_establish(sc->sc_bustag, pri, level, handler, arg);
    230 }
    231