xref: /netbsd-src/sys/arch/amiga/dev/ahsc.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: ahsc.c,v 1.32 2003/05/03 18:10:42 wiz 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 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: ahsc.c,v 1.32 2003/05/03 18:10:42 wiz Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #include <dev/scsipi/scsi_all.h>
47 #include <dev/scsipi/scsipi_all.h>
48 #include <dev/scsipi/scsiconf.h>
49 #include <amiga/amiga/custom.h>
50 #include <amiga/amiga/cc.h>
51 #include <amiga/amiga/cfdev.h>
52 #include <amiga/amiga/device.h>
53 #include <amiga/amiga/isr.h>
54 #include <amiga/dev/dmavar.h>
55 #include <amiga/dev/sbicreg.h>
56 #include <amiga/dev/sbicvar.h>
57 #include <amiga/dev/ahscreg.h>
58 #include <amiga/dev/zbusvar.h>
59 
60 #include <machine/cpu.h>
61 
62 void ahscattach(struct device *, struct device *, void *);
63 int ahscmatch(struct device *, struct cfdata *, void *);
64 
65 void ahsc_enintr(struct sbic_softc *);
66 void ahsc_dmastop(struct sbic_softc *);
67 int ahsc_dmanext(struct sbic_softc *);
68 int ahsc_dmaintr(void *);
69 int ahsc_dmago(struct sbic_softc *, char *, int, int);
70 
71 #ifdef DEBUG
72 void ahsc_dump(void);
73 #endif
74 
75 #ifdef DEBUG
76 int	ahsc_dmadebug = 0;
77 #endif
78 
79 CFATTACH_DECL(ahsc, sizeof(struct sbic_softc),
80     ahscmatch, ahscattach, NULL, NULL);
81 
82 /*
83  * if we are an A3000 we are here.
84  */
85 int
86 ahscmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
87 {
88 	char *mbusstr;
89 
90 	mbusstr = auxp;
91 	if (is_a3000() && matchname(auxp, "ahsc"))
92 		return(1);
93 	return(0);
94 }
95 
96 void
97 ahscattach(struct device *pdp, struct device *dp, void *auxp)
98 {
99 	volatile struct sdmac *rp;
100 	struct sbic_softc *sc = (struct sbic_softc *)dp;
101 	struct cfdev *cdp, *ecdp;
102 	struct scsipi_adapter *adapt = &sc->sc_adapter;
103 	struct scsipi_channel *chan = &sc->sc_channel;
104 
105 	ecdp = &cfdev[ncfdev];
106 
107 	for (cdp = cfdev; cdp < ecdp; cdp++) {
108 		if (cdp->rom.manid == 8738 &&
109 		    cdp->rom.prodid == 35)
110 				break;
111 	}
112 
113 	sc->sc_cregs = rp = ztwomap(0xdd0000);
114 	/*
115 	 * disable ints and reset bank register
116 	 */
117 	rp->CNTR = CNTR_PDMD;
118 	rp->DAWR = DAWR_AHSC;
119 	sc->sc_enintr = ahsc_enintr;
120 	sc->sc_dmago = ahsc_dmago;
121 	sc->sc_dmanext = ahsc_dmanext;
122 	sc->sc_dmastop = ahsc_dmastop;
123 	sc->sc_dmacmd = 0;
124 
125 	/*
126 	 * everything is a valid DMA address
127 	 */
128 	sc->sc_dmamask = 0;
129 
130 	if (cdp < ecdp) {
131 		sc->sc_sbic.sbic_asr_p =  ((vu_char *)rp + 0x43);
132 		sc->sc_sbic.sbic_value_p =  ((vu_char *)rp + 0x47);
133 		printf(": modified for Apollo cpu board\n");
134 	} else {
135 		sc->sc_sbic.sbic_asr_p =  ((vu_char *)rp + 0x41);
136 		sc->sc_sbic.sbic_value_p =  ((vu_char *)rp + 0x43);
137 		printf("\n");
138 	}
139 
140 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143;
141 
142 	/*
143 	 * Fill in the scsipi_adapter.
144 	 */
145 	memset(adapt, 0, sizeof(*adapt));
146 	adapt->adapt_dev = &sc->sc_dev;
147 	adapt->adapt_nchannels = 1;
148 	adapt->adapt_openings = 7;
149 	adapt->adapt_max_periph = 1;
150 	adapt->adapt_request = sbic_scsipi_request;
151 	adapt->adapt_minphys = sbic_minphys;
152 
153 	/*
154 	 * Fill in the scsipi_channel.
155 	 */
156 	memset(chan, 0, sizeof(*chan));
157 	chan->chan_adapter = adapt;
158 	chan->chan_bustype = &scsi_bustype;
159 	chan->chan_channel = 0;
160 	chan->chan_ntargets = 8;
161 	chan->chan_nluns = 8;
162 	chan->chan_id = 7;
163 
164 	sbicinit(sc);
165 
166 	sc->sc_isr.isr_intr = ahsc_dmaintr;
167 	sc->sc_isr.isr_arg = sc;
168 	sc->sc_isr.isr_ipl = 2;
169 	add_isr (&sc->sc_isr);
170 
171 	/*
172 	 * attach all scsi units on us
173 	 */
174 	config_found(dp, chan, scsiprint);
175 }
176 
177 void
178 ahsc_enintr(struct sbic_softc *dev)
179 {
180 	volatile struct sdmac *sdp;
181 
182 	sdp = dev->sc_cregs;
183 
184 	dev->sc_flags |= SBICF_INTR;
185 	sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
186 }
187 
188 int
189 ahsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags)
190 {
191 	volatile struct sdmac *sdp;
192 
193 	sdp = dev->sc_cregs;
194 	/*
195 	 * Set up the command word based on flags
196 	 */
197 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
198 	if ((flags & DMAGO_READ) == 0)
199 		dev->sc_dmacmd |= CNTR_DDIR;
200 #ifdef DEBUG
201 	if (ahsc_dmadebug & DDB_IO)
202 		printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd);
203 #endif
204 
205 	dev->sc_flags |= SBICF_INTR;
206 	sdp->CNTR = dev->sc_dmacmd;
207 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
208 	sdp->ST_DMA = 1;
209 
210 	return(dev->sc_tcnt);
211 }
212 
213 void
214 ahsc_dmastop(struct sbic_softc *dev)
215 {
216 	volatile struct sdmac *sdp;
217 	int s;
218 
219 	sdp = dev->sc_cregs;
220 
221 #ifdef DEBUG
222 	if (ahsc_dmadebug & DDB_FOLLOW)
223 		printf("ahsc_dmastop()\n");
224 #endif
225 	if (dev->sc_dmacmd) {
226 		s = splbio();
227 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
228 			/*
229 			 * only FLUSH if terminal count not enabled,
230 			 * and reading from peripheral
231 			 */
232 			sdp->FLUSH = 1;
233 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
234 				;
235 		}
236 		/*
237 		 * clear possible interrupt and stop DMA
238 		 */
239 		sdp->CINT = 1;
240 		sdp->SP_DMA = 1;
241 		dev->sc_dmacmd = 0;
242 		splx(s);
243 	}
244 }
245 
246 int
247 ahsc_dmaintr(void *arg)
248 {
249 	struct sbic_softc *dev = arg;
250 	volatile struct sdmac *sdp;
251 	int stat, found;
252 
253 	sdp = dev->sc_cregs;
254 	stat = sdp->ISTR;
255 
256 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
257 		return (0);
258 
259 #ifdef DEBUG
260 	if (ahsc_dmadebug & DDB_FOLLOW)
261 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
262 #endif
263 
264 	/*
265 	 * both, SCSI and DMA interrupts arrive here. I chose
266 	 * arbitrarily that DMA interrupts should have higher
267 	 * precedence than SCSI interrupts.
268 	 */
269 	found = 0;
270 	if (stat & ISTR_E_INT) {
271 		++found;
272 
273 		sdp->CINT = 1;	/* clear possible interrupt */
274 
275 		/*
276 		 * check for SCSI ints in the same go and
277 		 * eventually save an interrupt
278 		 */
279 	}
280 
281 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
282 		found += sbicintr(dev);
283 	return(found);
284 }
285 
286 
287 int
288 ahsc_dmanext(struct sbic_softc *dev)
289 {
290 	volatile struct sdmac *sdp;
291 
292 	sdp = dev->sc_cregs;
293 
294 	if (dev->sc_cur > dev->sc_last) {
295 		/* shouldn't happen !! */
296 		printf("ahsc_dmanext at end !!!\n");
297 		ahsc_dmastop(dev);
298 		return(0);
299 	}
300 	if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
301 		  /*
302 		   * only FLUSH if terminal count not enabled,
303 		   * and reading from peripheral
304 		   */
305 		sdp->FLUSH = 1;
306 		while ((sdp->ISTR & ISTR_FE_FLG) == 0)
307 			;
308 	}
309 	/*
310 	 * clear possible interrupt and stop DMA
311 	 */
312 	sdp->CINT = 1;	/* clear possible interrupt */
313 	sdp->SP_DMA = 1;	/* stop DMA */
314 	sdp->CNTR = dev->sc_dmacmd;
315 	sdp->ACR = (u_int)dev->sc_cur->dc_addr;
316 	sdp->ST_DMA = 1;
317 
318 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
319 	return(dev->sc_tcnt);
320 }
321 
322 #ifdef DEBUG
323 void
324 ahsc_dump(void)
325 {
326 	extern struct cfdriver ahsc_cd;
327 	int i;
328 
329 	for (i = 0; i < ahsc_cd.cd_ndevs; ++i)
330 		if (ahsc_cd.cd_devs[i])
331 			sbic_dump(ahsc_cd.cd_devs[i]);
332 }
333 #endif
334