1 /*	$OpenBSD: dma_sbus.c,v 1.11 2003/07/03 20:36:07 jason Exp $	*/
2 /*	$NetBSD: dma_sbus.c,v 1.5 2000/07/09 20:57:42 pk Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Paul Kranenburg.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1994 Peter Galbavy.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/errno.h>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 
72 #include <machine/bus.h>
73 #include <machine/intr.h>
74 #include <machine/autoconf.h>
75 
76 #include <dev/sbus/sbusvar.h>
77 
78 #include <dev/ic/lsi64854reg.h>
79 #include <dev/ic/lsi64854var.h>
80 
81 #include <scsi/scsi_all.h>
82 #include <scsi/scsiconf.h>
83 
84 #include <dev/ic/ncr53c9xreg.h>
85 #include <dev/ic/ncr53c9xvar.h>
86 
87 struct dma_softc {
88 	struct lsi64854_softc	sc_lsi64854;	/* base device */
89 	struct sbusdev	sc_sd;			/* sbus device */
90 };
91 
92 int	dmamatch_sbus(struct device *, void *, void *);
93 void	dmaattach_sbus(struct device *, struct device *, void *);
94 
95 int	dmaprint_sbus(void *, const char *);
96 
97 void	*dmabus_intr_establish(
98 		bus_space_tag_t,
99 		bus_space_tag_t,
100 		int,			/*bus interrupt priority*/
101 		int,			/*`device class' level*/
102 		int,			/*flags*/
103 		int (*)(void *),	/*handler*/
104 		void *,			/*handler arg*/
105 		const char *);		/*what*/
106 
107 static	bus_space_tag_t dma_alloc_bustag(struct dma_softc *sc);
108 
109 struct cfattach dma_sbus_ca = {
110 	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
111 };
112 
113 struct cfattach ledma_ca = {
114 	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
115 };
116 
117 struct cfdriver ledma_cd = {
118 	NULL, "ledma", DV_DULL
119 };
120 
121 struct cfdriver dma_cd = {
122 	NULL, "dma", DV_DULL
123 };
124 
125 int
dmaprint_sbus(void * aux,const char * busname)126 dmaprint_sbus(void *aux, const char *busname)
127 {
128 	struct sbus_attach_args *sa = aux;
129 	bus_space_tag_t t = sa->sa_bustag;
130 	struct dma_softc *sc = t->cookie;
131 
132 	sa->sa_bustag = sc->sc_lsi64854.sc_bustag;	/* XXX */
133 	sbus_print(aux, busname);	/* XXX */
134 	sa->sa_bustag = t;		/* XXX */
135 	return (UNCONF);
136 }
137 
138 int
dmamatch_sbus(struct device * parent,void * vcf,void * aux)139 dmamatch_sbus(struct device *parent, void *vcf, void *aux)
140 {
141 	struct cfdata *cf = vcf;
142 	struct sbus_attach_args *sa = aux;
143 
144 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
145 		strcmp("espdma", sa->sa_name) == 0);
146 }
147 
148 void
dmaattach_sbus(parent,self,aux)149 dmaattach_sbus(parent, self, aux)
150 	struct device *parent, *self;
151 	void *aux;
152 {
153 	struct sbus_attach_args *sa = aux;
154 	struct dma_softc *dsc = (void *)self;
155 	struct lsi64854_softc *sc = &dsc->sc_lsi64854;
156 	bus_space_handle_t bh;
157 	bus_space_tag_t sbt;
158 	int sbusburst, burst;
159 	int node;
160 
161 	node = sa->sa_node;
162 
163 	sc->sc_bustag = sa->sa_bustag;
164 	sc->sc_dmatag = sa->sa_dmatag;
165 
166 	/* Map registers */
167 	if (sa->sa_npromvaddrs != 0) {
168 		if (sbus_bus_map(sa->sa_bustag, 0,
169 		    sa->sa_promvaddrs[0],
170 		    sa->sa_size,		/* ???? */
171 		    BUS_SPACE_MAP_PROMADDRESS,
172 		    0, &bh) != 0) {
173 			printf("%s: cannot map registers\n", self->dv_xname);
174 			return;
175 		}
176 	} else if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
177 	    sa->sa_offset,
178 	    sa->sa_size,
179 	    0, 0, &bh) != 0) {
180 		printf("%s: cannot map registers\n", self->dv_xname);
181 		return;
182 	}
183 	sc->sc_regs = bh;
184 
185 	/*
186 	 * Get transfer burst size from PROM and plug it into the
187 	 * controller registers. This is needed on the Sun4m; do
188 	 * others need it too?
189 	 */
190 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
191 	if (sbusburst == 0)
192 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
193 
194 	burst = getpropint(node,"burst-sizes", -1);
195 	if (burst == -1)
196 		/* take SBus burst sizes */
197 		burst = sbusburst;
198 
199 	/* Clamp at parent's burst sizes */
200 	burst &= sbusburst;
201 	sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
202 		       (burst & SBUS_BURST_16) ? 16 : 0;
203 
204 	if (sc->sc_dev.dv_cfdata->cf_attach == &ledma_ca) {
205 		char *cabletype;
206 		u_int32_t csr;
207 		/*
208 		 * Check to see which cable type is currently active and
209 		 * set the appropriate bit in the ledma csr so that it
210 		 * gets used. If we didn't netboot, the PROM won't have
211 		 * the "cable-selection" property; default to TP and then
212 		 * the user can change it via a "media" option to ifconfig.
213 		 */
214 		cabletype = getpropstring(node, "cable-selection");
215 		csr = L64854_GCSR(sc);
216 		if (strcmp(cabletype, "tpe") == 0) {
217 			csr |= E_TP_AUI;
218 		} else if (strcmp(cabletype, "aui") == 0) {
219 			csr &= ~E_TP_AUI;
220 		} else {
221 			/* assume TP if nothing there */
222 			csr |= E_TP_AUI;
223 		}
224 		L64854_SCSR(sc, csr);
225 		delay(20000);	/* manual says we need a 20ms delay */
226 		sc->sc_channel = L64854_CHANNEL_ENET;
227 	} else {
228 		sc->sc_channel = L64854_CHANNEL_SCSI;
229 	}
230 
231 	sbus_establish(&dsc->sc_sd, &sc->sc_dev);
232 	sbt = dma_alloc_bustag(dsc);
233 	lsi64854_attach(sc);
234 
235 	/* Attach children */
236 	for (node = firstchild(sa->sa_node); node; node = nextsibling(node)) {
237 		struct sbus_attach_args sa;
238 		sbus_setup_attach_args((struct sbus_softc *)parent,
239 				       sbt, sc->sc_dmatag, node, &sa);
240 		(void) config_found(&sc->sc_dev, (void *)&sa, dmaprint_sbus);
241 		sbus_destroy_attach_args(&sa);
242 	}
243 }
244 
245 void *
dmabus_intr_establish(bus_space_tag_t t,bus_space_tag_t t0,int pri,int level,int flags,int (* handler)(void *),void * arg,const char * what)246 dmabus_intr_establish(
247 	bus_space_tag_t t,
248 	bus_space_tag_t t0,
249 	int pri,
250 	int level,
251 	int flags,
252 	int (*handler)(void *),
253 	void *arg,
254 	const char *what)
255 {
256 	struct lsi64854_softc *sc = t->cookie;
257 
258 	/* XXX - for now only le; do esp later */
259 	if (sc->sc_channel == L64854_CHANNEL_ENET) {
260 		sc->sc_intrchain = handler;
261 		sc->sc_intrchainarg = arg;
262 		handler = lsi64854_enet_intr;
263 		arg = sc;
264 	}
265 
266 	for (t = t->parent; t; t = t->parent) {
267 		if (t->sparc_intr_establish != NULL)
268 			return ((*t->sparc_intr_establish)
269 				(t, t0, pri, level, flags, handler, arg, what));
270 
271 	}
272 
273 	panic("dmabus_intr_establish: no handler found");
274 
275 	return (NULL);
276 }
277 
278 bus_space_tag_t
dma_alloc_bustag(struct dma_softc * sc)279 dma_alloc_bustag(struct dma_softc *sc)
280 {
281 	struct sparc_bus_space_tag *sbt;
282 
283 	sbt = malloc(sizeof(*sbt), M_DEVBUF, M_NOWAIT);
284 	if (sbt == NULL)
285 		return (NULL);
286 
287 	bzero(sbt, sizeof *sbt);
288 	sbt->cookie = sc;
289 	sbt->parent = sc->sc_lsi64854.sc_bustag;
290 	sbt->asi = sbt->parent->asi;
291 	sbt->sasi = sbt->parent->sasi;
292 	sbt->sparc_intr_establish = dmabus_intr_establish;
293 	return (sbt);
294 }
295