xref: /netbsd-src/sys/arch/amiga/dev/atzsc.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: atzsc.c,v 1.35 2003/08/07 16:26:40 agc Exp $ */
2 
3 /*
4  * Copyright (c) 1982, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)dma.c
32  */
33 
34 /*
35  * Copyright (c) 1994 Christian E. Hopps
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)dma.c
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: atzsc.c,v 1.35 2003/08/07 16:26:40 agc Exp $");
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/device.h>
75 #include <dev/scsipi/scsi_all.h>
76 #include <dev/scsipi/scsipi_all.h>
77 #include <dev/scsipi/scsiconf.h>
78 #include <amiga/amiga/custom.h>
79 #include <amiga/amiga/cc.h>
80 #include <amiga/amiga/device.h>
81 #include <amiga/amiga/isr.h>
82 #include <amiga/dev/dmavar.h>
83 #include <amiga/dev/sbicreg.h>
84 #include <amiga/dev/sbicvar.h>
85 #include <amiga/dev/atzscreg.h>
86 #include <amiga/dev/zbusvar.h>
87 
88 void atzscattach(struct device *, struct device *, void *);
89 int atzscmatch(struct device *, struct cfdata *, void *);
90 
91 void atzsc_enintr(struct sbic_softc *);
92 void atzsc_dmastop(struct sbic_softc *);
93 int atzsc_dmanext(struct sbic_softc *);
94 int atzsc_dmaintr(void *);
95 int atzsc_dmago(struct sbic_softc *, char *, int, int);
96 
97 #ifdef DEBUG
98 void atzsc_dump(void);
99 #endif
100 
101 #ifdef DEBUG
102 int	atzsc_dmadebug = 0;
103 #endif
104 
105 CFATTACH_DECL(atzsc, sizeof(struct sbic_softc),
106     atzscmatch, atzscattach, NULL, NULL);
107 
108 /*
109  * if we are an A3000 we are here.
110  */
111 int
112 atzscmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
113 {
114 	struct zbus_args *zap;
115 
116 	zap = auxp;
117 
118 	/*
119 	 * Check manufacturer and product id.
120 	 * I was informed that older boards can be 2 also.
121 	 */
122 	if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2))
123 		return(1);
124 	else
125 		return(0);
126 }
127 
128 void
129 atzscattach(struct device *pdp, struct device *dp, void *auxp)
130 {
131 	volatile struct sdmac *rp;
132 	struct sbic_softc *sc = (struct sbic_softc *)dp;
133 	struct zbus_args *zap;
134 	struct scsipi_adapter *adapt = &sc->sc_adapter;
135 	struct scsipi_channel *chan = &sc->sc_channel;
136 
137 	zap = auxp;
138 
139 	sc->sc_cregs = rp = zap->va;
140 	/*
141 	 * disable ints and reset bank register
142 	 */
143 	rp->CNTR = CNTR_PDMD;
144 	rp->DAWR = DAWR_ATZSC;
145 	sc->sc_enintr = atzsc_enintr;
146 	sc->sc_dmago = atzsc_dmago;
147 	sc->sc_dmanext = atzsc_dmanext;
148 	sc->sc_dmastop = atzsc_dmastop;
149 	sc->sc_dmacmd = 0;
150 
151 	/*
152 	 * only 24 bit mem.
153 	 */
154 	sc->sc_flags |= SBICF_BADDMA;
155 	sc->sc_dmamask = ~0x00ffffff;
156 #if 0
157 	/*
158 	 * If the users kva space is not ztwo try and allocate a bounce buffer.
159 	 * XXX this needs to change if we move to multiple memory segments.
160 	 */
161 	if (kvtop(sc) & sc->sc_dmamask) {
162 		sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS * 8); /* XXX */
163 		if (isztwomem(sc->sc_dmabuffer))
164 			printf(" bounce pa 0x%x", kvtop(sc->sc_dmabuffer));
165 		else if (sc->sc_dmabuffer)
166 			printf(" bounce pa 0x%x",
167 			    PREP_DMA_MEM(sc->sc_dmabuffer));
168 	}
169 #endif
170 	sc->sc_sbic.sbic_asr_p = (volatile unsigned char *)rp + 0x91;
171 	sc->sc_sbic.sbic_value_p = (volatile unsigned char *)rp + 0x93;
172 
173 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77;
174 
175 	printf(": dmamask 0x%lx\n", ~sc->sc_dmamask);
176 
177 	/*
178 	 * Fill in the scsipi_adapter.
179 	 */
180 	memset(adapt, 0, sizeof(*adapt));
181 	adapt->adapt_dev = &sc->sc_dev;
182 	adapt->adapt_nchannels = 1;
183 	adapt->adapt_openings = 7;
184 	adapt->adapt_max_periph = 1;
185 	adapt->adapt_request = sbic_scsipi_request;
186 	adapt->adapt_minphys = sbic_minphys;
187 
188 	/*
189 	 * Fill in the scsipi_channel.
190 	 */
191 	memset(chan, 0, sizeof(*chan));
192 	chan->chan_adapter = adapt;
193 	chan->chan_bustype = &scsi_bustype;
194 	chan->chan_channel = 0;
195 	chan->chan_ntargets = 8;
196 	chan->chan_nluns = 8;
197 	chan->chan_id = 7;
198 
199 	sbicinit(sc);
200 
201 	sc->sc_isr.isr_intr = atzsc_dmaintr;
202 	sc->sc_isr.isr_arg = sc;
203 	sc->sc_isr.isr_ipl = 2;
204 	add_isr (&sc->sc_isr);
205 
206 	/*
207 	 * attach all scsi units on us
208 	 */
209 	config_found(dp, chan, scsiprint);
210 }
211 
212 void
213 atzsc_enintr(struct sbic_softc *dev)
214 {
215 	volatile struct sdmac *sdp;
216 
217 	sdp = dev->sc_cregs;
218 
219 	dev->sc_flags |= SBICF_INTR;
220 	sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
221 }
222 
223 int
224 atzsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags)
225 {
226 	volatile struct sdmac *sdp;
227 
228 	sdp = dev->sc_cregs;
229 	/*
230 	 * Set up the command word based on flags
231 	 */
232 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
233 	if ((flags & DMAGO_READ) == 0)
234 		dev->sc_dmacmd |= CNTR_DDIR;
235 #ifdef DEBUG
236 	if (atzsc_dmadebug & DDB_IO)
237 		printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd);
238 #endif
239 
240 	dev->sc_flags |= SBICF_INTR;
241 	sdp->CNTR = dev->sc_dmacmd;
242 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
243 	sdp->ST_DMA = 1;
244 
245 	return(dev->sc_tcnt);
246 }
247 
248 void
249 atzsc_dmastop(struct sbic_softc *dev)
250 {
251 	volatile struct sdmac *sdp;
252 	int s;
253 
254 	sdp = dev->sc_cregs;
255 
256 #ifdef DEBUG
257 	if (atzsc_dmadebug & DDB_FOLLOW)
258 		printf("atzsc_dmastop()\n");
259 #endif
260 	if (dev->sc_dmacmd) {
261 		s = splbio();
262 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
263 			/*
264 			 * only FLUSH if terminal count not enabled,
265 			 * and reading from peripheral
266 			 */
267 			sdp->FLUSH = 1;
268 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
269 				;
270 		}
271 		/*
272 		 * clear possible interrupt and stop DMA
273 		 */
274 		sdp->CINT = 1;
275 		sdp->SP_DMA = 1;
276 		dev->sc_dmacmd = 0;
277 		splx(s);
278 	}
279 }
280 
281 int
282 atzsc_dmaintr(void *arg)
283 {
284 	struct sbic_softc *dev = arg;
285 	volatile struct sdmac *sdp;
286 	int stat, found;
287 
288 	sdp = dev->sc_cregs;
289 	stat = sdp->ISTR;
290 
291 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
292 		return (0);
293 
294 #ifdef DEBUG
295 	if (atzsc_dmadebug & DDB_FOLLOW)
296 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
297 #endif
298 
299 	/*
300 	 * both, SCSI and DMA interrupts arrive here. I chose
301 	 * arbitrarily that DMA interrupts should have higher
302 	 * precedence than SCSI interrupts.
303 	 */
304 	found = 0;
305 	if (stat & ISTR_E_INT) {
306 		found++;
307 
308 		sdp->CINT = 1;	/* clear possible interrupt */
309 
310 		/*
311 		 * check for SCSI ints in the same go and
312 		 * eventually save an interrupt
313 		 */
314 	}
315 
316 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
317 		found += sbicintr(dev);
318 	return(found);
319 }
320 
321 
322 int
323 atzsc_dmanext(struct sbic_softc *dev)
324 {
325 	volatile struct sdmac *sdp;
326 
327 	sdp = dev->sc_cregs;
328 
329 	if (dev->sc_cur > dev->sc_last) {
330 		/* shouldn't happen !! */
331 		printf("atzsc_dmanext at end !!!\n");
332 		atzsc_dmastop(dev);
333 		return(0);
334 	}
335 	if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
336 		  /*
337 		   * only FLUSH if terminal count not enabled,
338 		   * and reading from peripheral
339 		   */
340 		sdp->FLUSH = 1;
341 		while ((sdp->ISTR & ISTR_FE_FLG) == 0)
342 			;
343 	}
344 	/*
345 	 * clear possible interrupt and stop DMA
346 	 */
347 	sdp->CINT = 1;	/* clear possible interrupt */
348 	sdp->SP_DMA = 1;	/* stop DMA */
349 	sdp->CNTR = dev->sc_dmacmd;
350 	sdp->ACR = (u_int)dev->sc_cur->dc_addr;
351 	sdp->ST_DMA = 1;
352 
353 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
354 	return(dev->sc_tcnt);
355 }
356 
357 #ifdef DEBUG
358 void
359 atzsc_dump(void)
360 {
361 	extern struct cfdriver atzsc_cd;
362 	int i;
363 
364 	for (i = 0; i < atzsc_cd.cd_ndevs; ++i)
365 		if (atzsc_cd.cd_devs[i])
366 			sbic_dump(atzsc_cd.cd_devs[i]);
367 }
368 #endif
369