atzsc.c revision 1.30 1 /* $NetBSD: atzsc.c,v 1.30 2002/01/26 13:40:53 aymeric 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 <dev/scsipi/scsi_all.h>
43 #include <dev/scsipi/scsipi_all.h>
44 #include <dev/scsipi/scsiconf.h>
45 #include <amiga/amiga/custom.h>
46 #include <amiga/amiga/cc.h>
47 #include <amiga/amiga/device.h>
48 #include <amiga/amiga/isr.h>
49 #include <amiga/dev/dmavar.h>
50 #include <amiga/dev/sbicreg.h>
51 #include <amiga/dev/sbicvar.h>
52 #include <amiga/dev/atzscreg.h>
53 #include <amiga/dev/zbusvar.h>
54
55 void atzscattach(struct device *, struct device *, void *);
56 int atzscmatch(struct device *, struct cfdata *, void *);
57
58 void atzsc_enintr(struct sbic_softc *);
59 void atzsc_dmastop(struct sbic_softc *);
60 int atzsc_dmanext(struct sbic_softc *);
61 int atzsc_dmaintr(void *);
62 int atzsc_dmago(struct sbic_softc *, char *, int, int);
63
64 #ifdef DEBUG
65 void atzsc_dump(void);
66 #endif
67
68 #ifdef DEBUG
69 int atzsc_dmadebug = 0;
70 #endif
71
72 struct cfattach atzsc_ca = {
73 sizeof(struct sbic_softc), atzscmatch, atzscattach
74 };
75
76 /*
77 * if we are an A3000 we are here.
78 */
79 int
80 atzscmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
81 {
82 struct zbus_args *zap;
83
84 zap = auxp;
85
86 /*
87 * Check manufacturer and product id.
88 * I was informed that older boards can be 2 also.
89 */
90 if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2))
91 return(1);
92 else
93 return(0);
94 }
95
96 void
97 atzscattach(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 zbus_args *zap;
102 struct scsipi_adapter *adapt = &sc->sc_adapter;
103 struct scsipi_channel *chan = &sc->sc_channel;
104
105 zap = auxp;
106
107 sc->sc_cregs = rp = zap->va;
108 /*
109 * disable ints and reset bank register
110 */
111 rp->CNTR = CNTR_PDMD;
112 rp->DAWR = DAWR_ATZSC;
113 sc->sc_enintr = atzsc_enintr;
114 sc->sc_dmago = atzsc_dmago;
115 sc->sc_dmanext = atzsc_dmanext;
116 sc->sc_dmastop = atzsc_dmastop;
117 sc->sc_dmacmd = 0;
118
119 /*
120 * only 24 bit mem.
121 */
122 sc->sc_flags |= SBICF_BADDMA;
123 sc->sc_dmamask = ~0x00ffffff;
124 #if 0
125 /*
126 * If the users kva space is not ztwo try and allocate a bounce buffer.
127 * XXX this needs to change if we move to multiple memory segments.
128 */
129 if (kvtop(sc) & sc->sc_dmamask) {
130 sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS * 8); /* XXX */
131 if (isztwomem(sc->sc_dmabuffer))
132 printf(" bounce pa 0x%x", kvtop(sc->sc_dmabuffer));
133 else if (sc->sc_dmabuffer)
134 printf(" bounce pa 0x%x",
135 PREP_DMA_MEM(sc->sc_dmabuffer));
136 }
137 #endif
138 sc->sc_sbic.sbic_asr_p = (volatile unsigned char *)rp + 0x91;
139 sc->sc_sbic.sbic_value_p = (volatile unsigned char *)rp + 0x93;
140
141 sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77;
142
143 printf(": dmamask 0x%lx\n", ~sc->sc_dmamask);
144
145 /*
146 * Fill in the scsipi_adapter.
147 */
148 memset(adapt, 0, sizeof(*adapt));
149 adapt->adapt_dev = &sc->sc_dev;
150 adapt->adapt_nchannels = 1;
151 adapt->adapt_openings = 7;
152 adapt->adapt_max_periph = 1;
153 adapt->adapt_request = sbic_scsipi_request;
154 adapt->adapt_minphys = sbic_minphys;
155
156 /*
157 * Fill in the scsipi_channel.
158 */
159 memset(chan, 0, sizeof(*chan));
160 chan->chan_adapter = adapt;
161 chan->chan_bustype = &scsi_bustype;
162 chan->chan_channel = 0;
163 chan->chan_ntargets = 8;
164 chan->chan_nluns = 8;
165 chan->chan_id = 7;
166
167 sbicinit(sc);
168
169 sc->sc_isr.isr_intr = atzsc_dmaintr;
170 sc->sc_isr.isr_arg = sc;
171 sc->sc_isr.isr_ipl = 2;
172 add_isr (&sc->sc_isr);
173
174 /*
175 * attach all scsi units on us
176 */
177 config_found(dp, chan, scsiprint);
178 }
179
180 void
181 atzsc_enintr(struct sbic_softc *dev)
182 {
183 volatile struct sdmac *sdp;
184
185 sdp = dev->sc_cregs;
186
187 dev->sc_flags |= SBICF_INTR;
188 sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
189 }
190
191 int
192 atzsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags)
193 {
194 volatile struct sdmac *sdp;
195
196 sdp = dev->sc_cregs;
197 /*
198 * Set up the command word based on flags
199 */
200 dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
201 if ((flags & DMAGO_READ) == 0)
202 dev->sc_dmacmd |= CNTR_DDIR;
203 #ifdef DEBUG
204 if (atzsc_dmadebug & DDB_IO)
205 printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd);
206 #endif
207
208 dev->sc_flags |= SBICF_INTR;
209 sdp->CNTR = dev->sc_dmacmd;
210 sdp->ACR = (u_int) dev->sc_cur->dc_addr;
211 sdp->ST_DMA = 1;
212
213 return(dev->sc_tcnt);
214 }
215
216 void
217 atzsc_dmastop(struct sbic_softc *dev)
218 {
219 volatile struct sdmac *sdp;
220 int s;
221
222 sdp = dev->sc_cregs;
223
224 #ifdef DEBUG
225 if (atzsc_dmadebug & DDB_FOLLOW)
226 printf("atzsc_dmastop()\n");
227 #endif
228 if (dev->sc_dmacmd) {
229 s = splbio();
230 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
231 /*
232 * only FLUSH if terminal count not enabled,
233 * and reading from peripheral
234 */
235 sdp->FLUSH = 1;
236 while ((sdp->ISTR & ISTR_FE_FLG) == 0)
237 ;
238 }
239 /*
240 * clear possible interrupt and stop dma
241 */
242 sdp->CINT = 1;
243 sdp->SP_DMA = 1;
244 dev->sc_dmacmd = 0;
245 splx(s);
246 }
247 }
248
249 int
250 atzsc_dmaintr(void *arg)
251 {
252 struct sbic_softc *dev = arg;
253 volatile struct sdmac *sdp;
254 int stat, found;
255
256 sdp = dev->sc_cregs;
257 stat = sdp->ISTR;
258
259 if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
260 return (0);
261
262 #ifdef DEBUG
263 if (atzsc_dmadebug & DDB_FOLLOW)
264 printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
265 #endif
266
267 /*
268 * both, SCSI and DMA interrupts arrive here. I chose
269 * arbitrarily that DMA interrupts should have higher
270 * precedence than SCSI interrupts.
271 */
272 found = 0;
273 if (stat & ISTR_E_INT) {
274 found++;
275
276 sdp->CINT = 1; /* clear possible interrupt */
277
278 /*
279 * check for SCSI ints in the same go and
280 * eventually save an interrupt
281 */
282 }
283
284 if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
285 found += sbicintr(dev);
286 return(found);
287 }
288
289
290 int
291 atzsc_dmanext(struct sbic_softc *dev)
292 {
293 volatile struct sdmac *sdp;
294
295 sdp = dev->sc_cregs;
296
297 if (dev->sc_cur > dev->sc_last) {
298 /* shouldn't happen !! */
299 printf("atzsc_dmanext at end !!!\n");
300 atzsc_dmastop(dev);
301 return(0);
302 }
303 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
304 /*
305 * only FLUSH if terminal count not enabled,
306 * and reading from peripheral
307 */
308 sdp->FLUSH = 1;
309 while ((sdp->ISTR & ISTR_FE_FLG) == 0)
310 ;
311 }
312 /*
313 * clear possible interrupt and stop dma
314 */
315 sdp->CINT = 1; /* clear possible interrupt */
316 sdp->SP_DMA = 1; /* stop dma */
317 sdp->CNTR = dev->sc_dmacmd;
318 sdp->ACR = (u_int)dev->sc_cur->dc_addr;
319 sdp->ST_DMA = 1;
320
321 dev->sc_tcnt = dev->sc_cur->dc_count << 1;
322 return(dev->sc_tcnt);
323 }
324
325 #ifdef DEBUG
326 void
327 atzsc_dump(void)
328 {
329 extern struct cfdriver atzsc_cd;
330 int i;
331
332 for (i = 0; i < atzsc_cd.cd_ndevs; ++i)
333 if (atzsc_cd.cd_devs[i])
334 sbic_dump(atzsc_cd.cd_devs[i]);
335 }
336 #endif
337