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