atzsc.c revision 1.10 1 /* $NetBSD: atzsc.c,v 1.10 1995/01/05 07:22:34 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/dev/dmavar.h>
48 #include <amiga/dev/sbicreg.h>
49 #include <amiga/dev/sbicvar.h>
50 #include <amiga/dev/atzscreg.h>
51 #include <amiga/dev/zbusvar.h>
52
53 int atzscprint __P((void *auxp, char *));
54 void atzscattach __P((struct device *, struct device *, void *));
55 int atzscmatch __P((struct device *, struct cfdata *, void *));
56
57 void atzsc_dmafree __P((struct sbic_softc *));
58 void atzsc_dmastop __P((struct sbic_softc *));
59 int atzsc_dmanext __P((struct sbic_softc *));
60 int atzsc_dmaintr __P((void));
61 int atzsc_dmago __P((struct sbic_softc *, char *, int, int));
62
63 struct scsi_adapter atzsc_scsiswitch = {
64 sbic_scsicmd,
65 sbic_minphys,
66 0, /* no lun support */
67 0, /* no lun support */
68 };
69
70 struct scsi_device atzsc_scsidev = {
71 NULL, /* use default error handler */
72 NULL, /* do not have a start functio */
73 NULL, /* have no async handler */
74 NULL, /* Use default done routine */
75 };
76
77
78 #ifdef DEBUG
79 void atzsc_dmatimeout __P((void *));
80 int atzsc_dmadebug = 0;
81 #endif
82
83 struct cfdriver atzsccd = {
84 NULL, "atzsc", (cfmatch_t)atzscmatch, atzscattach,
85 DV_DULL, sizeof(struct sbic_softc), NULL, 0 };
86
87 /*
88 * if we are an A3000 we are here.
89 */
90 int
91 atzscmatch(pdp, cdp, auxp)
92 struct device *pdp;
93 struct cfdata *cdp;
94 void *auxp;
95 {
96 struct zbus_args *zap;
97
98 zap = auxp;
99
100 /*
101 * Check manufacturer and product id.
102 * I was informed that older boards can be 2 also.
103 */
104 if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2))
105 return(1);
106 else
107 return(0);
108 }
109
110 void
111 atzscattach(pdp, dp, auxp)
112 struct device *pdp, *dp;
113 void *auxp;
114 {
115 volatile struct sdmac *rp;
116 struct sbic_softc *sc;
117 struct zbus_args *zap;
118
119 zap = auxp;
120
121 sc = (struct sbic_softc *)dp;
122 sc->sc_cregs = rp = zap->va;
123 /*
124 * disable ints and reset bank register
125 */
126 rp->CNTR = CNTR_PDMD;
127 rp->DAWR = DAWR_ATZSC;
128 sc->sc_dmafree = atzsc_dmafree;
129 sc->sc_dmago = atzsc_dmago;
130 sc->sc_dmanext = atzsc_dmanext;
131 sc->sc_dmastop = atzsc_dmastop;
132 sc->sc_dmacmd = 0;
133
134 #ifdef DEBUG
135 /* make sure timeout is really not needed */
136 timeout(atzsc_dmatimeout, 0, 30 * hz);
137 #endif
138 /*
139 * only 24 bit mem.
140 */
141 sc->sc_flags |= SBICF_BADDMA;
142 sc->sc_dmamask = ~0x00ffffff;
143 /*
144 * If the users kva space is not ztwo try and allocate a bounce buffer.
145 * XXX this needs to change if we move to multiple memory segments.
146 */
147 if (kvtop(sc) & sc->sc_dmamask) {
148 sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS);
149 if (isztwomem(sc->sc_dmabuffer))
150 printf(" bounce pa 0x%x", ztwopa(sc->sc_dmabuffer));
151 else if (sc->sc_dmabuffer)
152 printf(" bounce pa 0x%x",
153 PREP_DMA_MEM(sc->sc_dmabuffer));
154 }
155 sc->sc_sbicp = (sbic_regmap_p) ((int)rp + 0x91);
156 sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77;
157
158 printf(": dmamask 0x%x\n", ~sc->sc_dmamask);
159
160 sbicreset(sc);
161
162 sc->sc_link.adapter_softc = sc;
163 sc->sc_link.adapter_target = 7;
164 sc->sc_link.adapter = &atzsc_scsiswitch;
165 sc->sc_link.device = &atzsc_scsidev;
166 sc->sc_link.openings = 1;
167 TAILQ_INIT(&sc->sc_xslist);
168
169 custom.intreq = INTF_PORTS;
170 custom.intena = INTF_SETCLR | INTF_PORTS;
171
172 /*
173 * attach all scsi units on us
174 */
175 config_found(dp, &sc->sc_link, atzscprint);
176 }
177
178 /*
179 * print diag if pnp is NULL else just extra
180 */
181 int
182 atzscprint(auxp, pnp)
183 void *auxp;
184 char *pnp;
185 {
186 if (pnp == NULL)
187 return(UNCONF);
188 return(QUIET);
189 }
190
191
192 void
193 atzsc_dmafree(dev)
194 struct sbic_softc *dev;
195 {
196 volatile struct sdmac *sdp;
197 int s;
198
199 sdp = dev->sc_cregs;
200
201 s = splbio();
202 #ifdef DEBUG
203 dev->sc_dmatimo = 0;
204 #endif
205 if (dev->sc_dmacmd) {
206 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
207 /*
208 * only FLUSH if terminal count not enabled,
209 * and reading from peripheral
210 */
211 sdp->FLUSH = 1;
212 while ((sdp->ISTR & ISTR_FE_FLG) == 0)
213 ;
214 }
215 /*
216 * clear possible interrupt and stop dma
217 */
218 sdp->CINT = 1;
219 sdp->SP_DMA = 1;
220 dev->sc_dmacmd = 0;
221 }
222 /*
223 * disable interrupts
224 */
225 sdp->CNTR = CNTR_PDMD; /* disable interrupts from dma/scsi */
226 dev->sc_flags &= ~SBICF_INTR;
227 splx(s);
228 }
229
230 int
231 atzsc_dmago(dev, addr, count, flags)
232 struct sbic_softc *dev;
233 char *addr;
234 int count, flags;
235 {
236 volatile struct sdmac *sdp;
237
238 sdp = dev->sc_cregs;
239 /*
240 * Set up the command word based on flags
241 */
242 dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
243 if ((flags & DMAGO_READ) == 0)
244 dev->sc_dmacmd |= CNTR_DDIR;
245 #ifdef DEBUG
246 if (atzsc_dmadebug & DDB_IO)
247 printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd);
248 dev->sc_dmatimo = 1;
249 #endif
250
251 dev->sc_flags |= SBICF_INTR;
252 sdp->CNTR = dev->sc_dmacmd;
253 sdp->ACR = (u_int) dev->sc_cur->dc_addr;
254 sdp->ST_DMA = 1;
255
256 return(dev->sc_tcnt);
257 }
258
259 void
260 atzsc_dmastop(dev)
261 struct sbic_softc *dev;
262 {
263 volatile struct sdmac *sdp;
264 int s;
265
266 sdp = dev->sc_cregs;
267
268 #ifdef DEBUG
269 if (atzsc_dmadebug & DDB_FOLLOW)
270 printf("atzsc_dmastop()\n");
271 dev->sc_dmatimo = 0;
272 #endif
273 if (dev->sc_dmacmd) {
274 s = splbio();
275 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
276 /*
277 * only FLUSH if terminal count not enabled,
278 * and reading from peripheral
279 */
280 sdp->FLUSH = 1;
281 while ((sdp->ISTR & ISTR_FE_FLG) == 0)
282 ;
283 }
284 /*
285 * clear possible interrupt and stop dma
286 */
287 sdp->CINT = 1;
288 sdp->SP_DMA = 1;
289 dev->sc_dmacmd = 0;
290 splx(s);
291 }
292 }
293
294 int
295 atzsc_dmaintr()
296 {
297 volatile struct sdmac *sdp;
298 struct sbic_softc *dev;
299 int i, stat, found;
300
301 found = 0;
302 for (i = 0; i < atzsccd.cd_ndevs; i++) {
303 dev = atzsccd.cd_devs[i];
304 if (dev == NULL)
305 continue;
306 sdp = dev->sc_cregs;
307 stat = sdp->ISTR;
308
309 if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
310 continue;
311
312 #ifdef DEBUG
313 if (atzsc_dmadebug & DDB_FOLLOW)
314 printf("atzsc_dmaintr (%d, 0x%x)\n", i, stat);
315 #endif
316
317 /*
318 * both, SCSI and DMA interrupts arrive here. I chose
319 * arbitrarily that DMA interrupts should have higher
320 * precedence than SCSI interrupts.
321 */
322 if (stat & ISTR_E_INT) {
323 found++;
324
325 sdp->CINT = 1; /* clear possible interrupt */
326
327 /*
328 * check for SCSI ints in the same go and
329 * eventually save an interrupt
330 */
331 }
332
333 if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
334 found += sbicintr(dev);
335 }
336 return(found);
337 }
338
339
340 int
341 atzsc_dmanext(dev)
342 struct sbic_softc *dev;
343 {
344 volatile struct sdmac *sdp;
345 int i, stat;
346
347 sdp = dev->sc_cregs;
348
349 if (dev->sc_cur > dev->sc_last) {
350 /* shouldn't happen !! */
351 printf("atzsc_dmanext at end !!!\n");
352 atzsc_dmastop(dev);
353 return(0);
354 }
355 #ifdef DEBUG
356 dev->sc_dmatimo = 1;
357 #endif
358 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
359 /*
360 * only FLUSH if terminal count not enabled,
361 * and reading from peripheral
362 */
363 sdp->FLUSH = 1;
364 while ((sdp->ISTR & ISTR_FE_FLG) == 0)
365 ;
366 }
367 /*
368 * clear possible interrupt and stop dma
369 */
370 sdp->CINT = 1; /* clear possible interrupt */
371 sdp->SP_DMA = 1; /* stop dma */
372 sdp->CNTR = dev->sc_dmacmd;
373 sdp->ACR = (u_int)dev->sc_cur->dc_addr;
374 sdp->ST_DMA = 1;
375
376 dev->sc_tcnt = dev->sc_cur->dc_count << 1;
377 return(dev->sc_tcnt);
378 }
379
380 #ifdef DEBUG
381 /*ARGSUSED*/
382 void
383 atzsc_dmatimeout(arg)
384 void *arg;
385 {
386 struct sbic_softc *dev;
387 int i, s;
388
389 for (i = 0; i < atzsccd.cd_ndevs; i++) {
390 dev = atzsccd.cd_devs[i];
391 if (dev == NULL)
392 continue;
393
394 s = splbio();
395 if (dev->sc_dmatimo) {
396 if (dev->sc_dmatimo > 1)
397 printf("atzsc_dma%d: timeout #%d\n",
398 dev->sc_dev.dv_unit, dev->sc_dmatimo - 1);
399 dev->sc_dmatimo++;
400 }
401 splx(s);
402 }
403 timeout(atzsc_dmatimeout, 0, 30 * hz);
404 }
405 #endif
406