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