xref: /netbsd-src/sys/arch/amiga/dev/ahsc.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: ahsc.c,v 1.8 1995/02/12 19:19:01 chopps Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Christian E. Hopps
5  * Copyright (c) 1982, 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)dma.c
37  */
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <scsi/scsi_all.h>
43 #include <scsi/scsiconf.h>
44 #include <amiga/amiga/custom.h>
45 #include <amiga/amiga/cc.h>
46 #include <amiga/amiga/device.h>
47 #include <amiga/amiga/isr.h>
48 #include <amiga/dev/dmavar.h>
49 #include <amiga/dev/sbicreg.h>
50 #include <amiga/dev/sbicvar.h>
51 #include <amiga/dev/ahscreg.h>
52 #include <amiga/dev/zbusvar.h>
53 
54 int ahscprint __P((void *auxp, char *));
55 void ahscattach __P((struct device *, struct device *, void *));
56 int ahscmatch __P((struct device *, struct cfdata *, void *));
57 
58 void ahsc_dmafree __P((struct sbic_softc *));
59 void ahsc_dmastop __P((struct sbic_softc *));
60 int ahsc_dmanext __P((struct sbic_softc *));
61 int ahsc_dmaintr __P((struct sbic_softc *));
62 int ahsc_dmago __P((struct sbic_softc *, char *, int, int));
63 
64 struct scsi_adapter ahsc_scsiswitch = {
65 	sbic_scsicmd,
66 	sbic_minphys,
67 	0,			/* no lun support */
68 	0,			/* no lun support */
69 };
70 
71 struct scsi_device ahsc_scsidev = {
72 	NULL,		/* use default error handler */
73 	NULL,		/* do not have a start functio */
74 	NULL,		/* have no async handler */
75 	NULL,		/* Use default done routine */
76 };
77 
78 
79 #ifdef DEBUG
80 void	ahsc_dmatimeout __P((void *));
81 int	ahsc_dmadebug = 0;
82 #endif
83 
84 struct cfdriver ahsccd = {
85 	NULL, "ahsc", (cfmatch_t)ahscmatch, ahscattach,
86 	DV_DULL, sizeof(struct sbic_softc), NULL, 0 };
87 
88 /*
89  * if we are an A3000 we are here.
90  */
91 int
92 ahscmatch(pdp, cdp, auxp)
93 	struct device *pdp;
94 	struct cfdata *cdp;
95 	void *auxp;
96 {
97 	char *mbusstr;
98 
99 	mbusstr = auxp;
100 	if (is_a3000() && matchname(auxp, "ahsc"))
101 		return(1);
102 	return(0);
103 }
104 
105 void
106 ahscattach(pdp, dp, auxp)
107 	struct device *pdp, *dp;
108 	void *auxp;
109 {
110 	volatile struct sdmac *rp;
111 	struct sbic_softc *sc;
112 
113 	printf("\n");
114 
115 	sc = (struct sbic_softc *)dp;
116 	sc->sc_cregs = rp = ztwomap(0xdd0000);
117 	/*
118 	 * disable ints and reset bank register
119 	 */
120 	rp->CNTR = CNTR_PDMD;
121 	rp->DAWR = DAWR_AHSC;
122 	sc->sc_dmafree = ahsc_dmafree;
123 	sc->sc_dmago = ahsc_dmago;
124 	sc->sc_dmanext = ahsc_dmanext;
125 	sc->sc_dmastop = ahsc_dmastop;
126 	sc->sc_dmacmd = 0;
127 
128 #ifdef DEBUG
129 	/* make sure timeout is really not needed */
130 	timeout(ahsc_dmatimeout, 0, 30 * hz);
131 #endif
132 	/*
133 	 * eveything is a valid dma address
134 	 */
135 	sc->sc_dmamask = 0;
136 	sc->sc_sbicp = (sbic_regmap_p) ((int)rp + 0x41);
137 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143;
138 
139 	sbicreset(sc);
140 
141 	sc->sc_link.adapter_softc = sc;
142 	sc->sc_link.adapter_target = 7;
143 	sc->sc_link.adapter = &ahsc_scsiswitch;
144 	sc->sc_link.device = &ahsc_scsidev;
145 	sc->sc_link.openings = 1;
146 	TAILQ_INIT(&sc->sc_xslist);
147 
148 	sc->sc_isr.isr_intr = ahsc_dmaintr;
149 	sc->sc_isr.isr_arg = sc;
150 	sc->sc_isr.isr_ipl = 2;
151 	add_isr (&sc->sc_isr);
152 
153 	/*
154 	 * attach all scsi units on us
155 	 */
156 	config_found(dp, &sc->sc_link, ahscprint);
157 }
158 
159 /*
160  * print diag if pnp is NULL else just extra
161  */
162 int
163 ahscprint(auxp, pnp)
164 	void *auxp;
165 	char *pnp;
166 {
167 	if (pnp == NULL)
168 		return(UNCONF);
169 	return(QUIET);
170 }
171 
172 
173 void
174 ahsc_dmafree(dev)
175 	struct sbic_softc *dev;
176 {
177 	volatile struct sdmac *sdp;
178 	int s;
179 
180 	sdp = dev->sc_cregs;
181 
182 	s = splbio();
183 #ifdef DEBUG
184 	dev->sc_dmatimo = 0;
185 #endif
186 	if (dev->sc_dmacmd) {
187 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
188 			/*
189 			 * only FLUSH if terminal count not enabled,
190 			 * and reading from peripheral
191 			 */
192 			sdp->FLUSH = 1;
193 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
194 				;
195 		}
196 		/*
197 		 * clear possible interrupt and stop dma
198 		 */
199 		sdp->CINT = 1;
200 		sdp->SP_DMA = 1;
201 		dev->sc_dmacmd = 0;
202 	}
203 	/*
204 	 * disable interrupts
205 	 */
206 	sdp->CNTR = CNTR_PDMD;	/* disable interrupts from dma/scsi */
207 	dev->sc_flags &= ~SBICF_INTR;
208 	splx(s);
209 }
210 
211 int
212 ahsc_dmago(dev, addr, count, flags)
213 	struct sbic_softc *dev;
214 	char *addr;
215 	int count, flags;
216 {
217 	volatile struct sdmac *sdp;
218 
219 	sdp = dev->sc_cregs;
220 	/*
221 	 * Set up the command word based on flags
222 	 */
223 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
224 	if ((flags & DMAGO_READ) == 0)
225 		dev->sc_dmacmd |= CNTR_DDIR;
226 #ifdef DEBUG
227 	if (ahsc_dmadebug & DDB_IO)
228 		printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd);
229 	dev->sc_dmatimo = 1;
230 #endif
231 
232 	dev->sc_flags |= SBICF_INTR;
233 	sdp->CNTR = dev->sc_dmacmd;
234 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
235 	sdp->ST_DMA = 1;
236 
237 	return(dev->sc_tcnt);
238 }
239 
240 void
241 ahsc_dmastop(dev)
242 	struct sbic_softc *dev;
243 {
244 	volatile struct sdmac *sdp;
245 	int s;
246 
247 	sdp = dev->sc_cregs;
248 
249 #ifdef DEBUG
250 	if (ahsc_dmadebug & DDB_FOLLOW)
251 		printf("ahsc_dmastop()\n");
252 	dev->sc_dmatimo = 0;
253 #endif
254 	if (dev->sc_dmacmd) {
255 		s = splbio();
256 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
257 			/*
258 			 * only FLUSH if terminal count not enabled,
259 			 * and reading from peripheral
260 			 */
261 			sdp->FLUSH = 1;
262 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
263 				;
264 		}
265 		/*
266 		 * clear possible interrupt and stop dma
267 		 */
268 		sdp->CINT = 1;
269 		sdp->SP_DMA = 1;
270 		dev->sc_dmacmd = 0;
271 		splx(s);
272 	}
273 }
274 
275 int
276 ahsc_dmaintr(dev)
277 	struct sbic_softc *dev;
278 {
279 	volatile struct sdmac *sdp;
280 	int stat, found;
281 
282 	sdp = dev->sc_cregs;
283 	stat = sdp->ISTR;
284 
285 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
286 		return (0);
287 
288 #ifdef DEBUG
289 	if (ahsc_dmadebug & DDB_FOLLOW)
290 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
291 #endif
292 
293 	/*
294 	 * both, SCSI and DMA interrupts arrive here. I chose
295 	 * arbitrarily that DMA interrupts should have higher
296 	 * precedence than SCSI interrupts.
297 	 */
298 	found = 0;
299 	if (stat & ISTR_E_INT) {
300 		++found;
301 
302 		sdp->CINT = 1;	/* clear possible interrupt */
303 
304 		/*
305 		 * check for SCSI ints in the same go and
306 		 * eventually save an interrupt
307 		 */
308 	}
309 
310 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
311 		found += sbicintr(dev);
312 	return(found);
313 }
314 
315 
316 int
317 ahsc_dmanext(dev)
318 	struct sbic_softc *dev;
319 {
320 	volatile struct sdmac *sdp;
321 	int i, stat;
322 
323 	sdp = dev->sc_cregs;
324 
325 	if (dev->sc_cur > dev->sc_last) {
326 		/* shouldn't happen !! */
327 		printf("ahsc_dmanext at end !!!\n");
328 		ahsc_dmastop(dev);
329 		return(0);
330 	}
331 #ifdef DEBUG
332 	dev->sc_dmatimo = 1;
333 #endif
334 	if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
335 		  /*
336 		   * only FLUSH if terminal count not enabled,
337 		   * and reading from peripheral
338 		   */
339 		sdp->FLUSH = 1;
340 		while ((sdp->ISTR & ISTR_FE_FLG) == 0)
341 			;
342         }
343 	/*
344 	 * clear possible interrupt and stop dma
345 	 */
346 	sdp->CINT = 1;	/* clear possible interrupt */
347 	sdp->SP_DMA = 1;	/* stop dma */
348 	sdp->CNTR = dev->sc_dmacmd;
349 	sdp->ACR = (u_int)dev->sc_cur->dc_addr;
350 	sdp->ST_DMA = 1;
351 
352 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
353 	return(dev->sc_tcnt);
354 }
355 
356 #ifdef DEBUG
357 /*ARGSUSED*/
358 void
359 ahsc_dmatimeout(arg)
360 	void *arg;
361 {
362 	struct sbic_softc *dev;
363 	int i, s;
364 
365 	for (i = 0; i < ahsccd.cd_ndevs; i++) {
366 		dev = ahsccd.cd_devs[i];
367 		if (dev == NULL)
368 			continue;
369 
370 		s = splbio();
371 		if (dev->sc_dmatimo) {
372 			if (dev->sc_dmatimo > 1)
373 				printf("ahsc_dma%d: timeout #%d\n",
374 				    dev->sc_dev.dv_unit, dev->sc_dmatimo - 1);
375 			dev->sc_dmatimo++;
376 		}
377 		splx(s);
378 	}
379 	timeout(ahsc_dmatimeout, 0, 30 * hz);
380 }
381 #endif
382