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