atari5380.c revision 1.17 1 1.17 leo /* $NetBSD: atari5380.c,v 1.17 1996/08/16 08:25:46 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Leo Weppelman.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo #include <sys/param.h>
34 1.1 leo #include <sys/systm.h>
35 1.1 leo #include <sys/kernel.h>
36 1.1 leo #include <sys/device.h>
37 1.1 leo #include <sys/buf.h>
38 1.1 leo #include <scsi/scsi_all.h>
39 1.1 leo #include <scsi/scsi_message.h>
40 1.1 leo #include <scsi/scsiconf.h>
41 1.1 leo
42 1.11 leo #include <atari/atari/stalloc.h>
43 1.11 leo
44 1.1 leo /*
45 1.1 leo * Include the driver definitions
46 1.1 leo */
47 1.1 leo #include <atari/dev/ncr5380reg.h>
48 1.1 leo
49 1.1 leo #include <machine/stdarg.h>
50 1.1 leo #include <machine/iomap.h>
51 1.1 leo #include <machine/mfp.h>
52 1.1 leo
53 1.1 leo #if defined(FALCON_SCSI)
54 1.1 leo #include <machine/dma.h>
55 1.1 leo #endif
56 1.1 leo
57 1.1 leo /*
58 1.1 leo * Set the various driver options
59 1.1 leo */
60 1.1 leo #define NREQ 18 /* Size of issue queue */
61 1.1 leo #define AUTO_SENSE 1 /* Automatically issue a request-sense */
62 1.1 leo
63 1.2 leo #define DRNAME ncrscsi /* used in various prints */
64 1.1 leo #undef DBG_SEL /* Show the selection process */
65 1.1 leo #undef DBG_REQ /* Show enqueued/ready requests */
66 1.2 leo #undef DBG_ERR_RET /* Show requests with != 0 return code */
67 1.1 leo #undef DBG_NOWRITE /* Do not allow writes to the targets */
68 1.1 leo #undef DBG_PIO /* Show the polled-I/O process */
69 1.1 leo #undef DBG_INF /* Show information transfer process */
70 1.1 leo #define DBG_NOSTATIC /* No static functions, all in DDB trace*/
71 1.14 leo #define DBG_PID 15 /* Keep track of driver */
72 1.1 leo #define REAL_DMA /* Use DMA if sensible */
73 1.17 leo #if defined(FALCON_SCSI)
74 1.1 leo #define REAL_DMA_POLL 1 /* 1: Poll for end of DMA-transfer */
75 1.1 leo #else
76 1.1 leo #define REAL_DMA_POLL 0 /* 1: Poll for end of DMA-transfer */
77 1.1 leo #endif
78 1.1 leo #undef USE_PDMA /* Use special pdma-transfer function */
79 1.4 leo #define MIN_PHYS 65536 /*BARF!!!!*/
80 1.1 leo
81 1.1 leo /*
82 1.8 leo * Include more driver definitions
83 1.8 leo */
84 1.8 leo #include <atari/dev/ncr5380var.h>
85 1.8 leo
86 1.8 leo
87 1.8 leo /*
88 1.8 leo * This is crap, but because the interrupts now run at MFP spl-level (6),
89 1.8 leo * splbio() is not enough at some places. The code should be checked to
90 1.8 leo * find out where splhigh() is needed and where splbio() should be used.
91 1.8 leo * Now that I use this interrupt sceme, the spl values are fake!
92 1.8 leo */
93 1.8 leo #undef splbio()
94 1.8 leo #define splbio() splhigh()
95 1.8 leo
96 1.8 leo /*
97 1.1 leo * The atari specific driver options
98 1.1 leo */
99 1.1 leo #undef NO_TTRAM_DMA /* Do not use DMA to TT-ram. This */
100 1.1 leo /* fails on older atari's */
101 1.1 leo #define ENABLE_NCR5380(sc) cur_softc = sc;
102 1.1 leo
103 1.9 leo static u_char *alloc_bounceb __P((u_long));
104 1.9 leo static void free_bounceb __P((u_char *));
105 1.10 thorpej static int machine_match __P((struct device *, void *, void *,
106 1.9 leo struct cfdriver *));
107 1.9 leo void scsi_ctrl __P((int));
108 1.9 leo void scsi_dma __P((int));
109 1.9 leo
110 1.3 leo /*
111 1.3 leo * Functions that do nothing on the atari
112 1.3 leo */
113 1.3 leo #define pdma_ready() 0
114 1.1 leo
115 1.1 leo #if defined(TT_SCSI)
116 1.1 leo /*
117 1.1 leo * Define all the things we need of the DMA-controller
118 1.1 leo */
119 1.1 leo #define SCSI_DMA ((struct scsi_dma *)AD_SCSI_DMA)
120 1.1 leo #define SCSI_5380 ((struct scsi_5380 *)AD_NCR5380)
121 1.1 leo
122 1.1 leo struct scsi_dma {
123 1.1 leo volatile u_char s_dma_ptr[8]; /* use only the odd bytes */
124 1.1 leo volatile u_char s_dma_cnt[8]; /* use only the odd bytes */
125 1.1 leo volatile u_char s_dma_res[4]; /* data residue register */
126 1.1 leo volatile u_char s_dma_gap; /* not used */
127 1.1 leo volatile u_char s_dma_ctrl; /* control register */
128 1.1 leo };
129 1.1 leo
130 1.1 leo #define set_scsi_dma(addr, val) (void)( \
131 1.1 leo { \
132 1.1 leo u_char *address = (u_char*)addr+1; \
133 1.1 leo u_long nval = (u_long)val; \
134 1.1 leo __asm("movepl %0, %1@(0)": :"d" (nval), "a" (address)); \
135 1.1 leo })
136 1.1 leo
137 1.1 leo #define get_scsi_dma(addr, res) ( \
138 1.1 leo { \
139 1.1 leo u_char *address = (u_char*)addr+1; \
140 1.1 leo u_long nval; \
141 1.1 leo __asm("movepl %1@(0), %0": "=d" (nval) : "a" (address)); \
142 1.1 leo res = (u_long)nval; \
143 1.1 leo })
144 1.1 leo
145 1.1 leo /*
146 1.1 leo * Defines for TT-DMA control register
147 1.1 leo */
148 1.1 leo #define SD_BUSERR 0x80 /* 1 = transfer caused bus error*/
149 1.1 leo #define SD_ZERO 0x40 /* 1 = byte counter is zero */
150 1.1 leo #define SD_ENABLE 0x02 /* 1 = Enable DMA */
151 1.1 leo #define SD_OUT 0x01 /* Direction: memory to SCSI */
152 1.1 leo #define SD_IN 0x00 /* Direction: SCSI to memory */
153 1.1 leo
154 1.1 leo /*
155 1.1 leo * Define the 5380 register set
156 1.1 leo */
157 1.1 leo struct scsi_5380 {
158 1.1 leo volatile u_char scsi_5380[16]; /* use only the odd bytes */
159 1.1 leo };
160 1.1 leo #endif /* TT_SCSI */
161 1.1 leo
162 1.1 leo /**********************************************
163 1.1 leo * Variables present for both TT and Falcon. *
164 1.1 leo **********************************************/
165 1.1 leo
166 1.1 leo /*
167 1.1 leo * Softc of currently active controller (a bit of fake; we only have one)
168 1.1 leo */
169 1.1 leo static struct ncr_softc *cur_softc;
170 1.1 leo
171 1.1 leo #if defined(TT_SCSI) && !defined(FALCON_SCSI)
172 1.1 leo /*
173 1.1 leo * We can be more efficient for some functions when only TT_SCSI is selected
174 1.1 leo */
175 1.1 leo #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1]
176 1.1 leo #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
177 1.1 leo
178 1.1 leo #define scsi_mach_init(sc) scsi_tt_init(sc)
179 1.1 leo #define scsi_ienable() scsi_tt_ienable()
180 1.1 leo #define scsi_idisable() scsi_tt_idisable()
181 1.1 leo #define scsi_clr_ipend() scsi_tt_clr_ipend()
182 1.7 leo #define scsi_ipending() (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
183 1.1 leo #define scsi_dma_setup(r,p,m) scsi_tt_dmasetup(r, p, m)
184 1.1 leo #define wrong_dma_range(r,d) tt_wrong_dma_range(r, d)
185 1.1 leo #define poll_edma(reqp) tt_poll_edma(reqp)
186 1.1 leo #define get_dma_result(r, b) tt_get_dma_result(r, b)
187 1.16 leo #define can_access_5380() 1
188 1.1 leo
189 1.1 leo #define fair_to_keep_dma() 1
190 1.1 leo #define claimed_dma() 1
191 1.1 leo #define reconsider_dma()
192 1.1 leo
193 1.1 leo #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
194 1.1 leo
195 1.1 leo #if defined(TT_SCSI)
196 1.1 leo
197 1.1 leo /*
198 1.9 leo * Prototype functions defined below
199 1.9 leo */
200 1.9 leo #ifdef NO_TTRAM_DMA
201 1.9 leo static int tt_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
202 1.9 leo #endif
203 1.9 leo static void scsi_tt_init __P((struct ncr_softc *));
204 1.9 leo static u_char get_tt_5380_reg __P((u_short));
205 1.9 leo static void set_tt_5380_reg __P((u_short, u_short));
206 1.9 leo static void scsi_tt_dmasetup __P((SC_REQ *, u_int, u_char));
207 1.9 leo static int tt_poll_edma __P((SC_REQ *));
208 1.9 leo static u_char *ptov __P((SC_REQ *, u_long*));
209 1.9 leo static int tt_get_dma_result __P((SC_REQ *, u_long *));
210 1.9 leo void scsi_tt_ienable __P((void));
211 1.9 leo void scsi_tt_idisable __P((void));
212 1.9 leo void scsi_tt_clr_ipend __P((void));
213 1.9 leo
214 1.9 leo /*
215 1.1 leo * Define these too, so we can use them locally...
216 1.1 leo */
217 1.1 leo #define GET_TT_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1]
218 1.1 leo #define SET_TT_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
219 1.1 leo
220 1.1 leo #ifdef NO_TTRAM_DMA
221 1.9 leo static int
222 1.9 leo tt_wrong_dma_range(reqp, dm)
223 1.1 leo SC_REQ *reqp;
224 1.1 leo struct dma_chain *dm;
225 1.1 leo {
226 1.1 leo if (dm->dm_addr & 0xff000000) {
227 1.1 leo reqp->dr_flag |= DRIVER_BOUNCING;
228 1.1 leo return(1);
229 1.1 leo }
230 1.1 leo return(0);
231 1.1 leo }
232 1.1 leo #else
233 1.1 leo #define tt_wrong_dma_range(reqp, dm) 0
234 1.1 leo #endif
235 1.1 leo
236 1.9 leo static void
237 1.9 leo scsi_tt_init(struct ncr_softc *sc)
238 1.1 leo {
239 1.1 leo /*
240 1.1 leo * Enable SCSI-related interrupts
241 1.1 leo */
242 1.1 leo MFP2->mf_aer |= 0x80; /* SCSI IRQ goes HIGH!!!!! */
243 1.1 leo
244 1.1 leo MFP2->mf_ierb |= IB_SCDM; /* SCSI-dma interrupts */
245 1.1 leo MFP2->mf_iprb &= ~IB_SCDM;
246 1.1 leo MFP2->mf_imrb |= IB_SCDM;
247 1.1 leo
248 1.1 leo MFP2->mf_iera |= IA_SCSI; /* SCSI-5380 interrupts */
249 1.1 leo MFP2->mf_ipra &= ~IA_SCSI;
250 1.1 leo MFP2->mf_imra |= IA_SCSI;
251 1.1 leo
252 1.1 leo /*
253 1.1 leo * LWP: DMA transfers to TT-ram causes data to be garbeled
254 1.1 leo * without notice on some revisons of the TT-mainboard.
255 1.1 leo * When program's generate misterious Segmentations faults,
256 1.1 leo * try turning on NO_TTRAM_DMA.
257 1.1 leo */
258 1.1 leo #ifdef NO_TTRAM_DMA
259 1.1 leo printf(": DMA to TT-RAM is disabled!");
260 1.1 leo #endif
261 1.1 leo }
262 1.1 leo
263 1.9 leo static u_char
264 1.9 leo get_tt_5380_reg(u_short rnum)
265 1.1 leo {
266 1.1 leo return(SCSI_5380->scsi_5380[(rnum << 1) | 1]);
267 1.1 leo }
268 1.1 leo
269 1.9 leo static void
270 1.9 leo set_tt_5380_reg(u_short rnum, u_short val)
271 1.1 leo {
272 1.1 leo SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
273 1.1 leo }
274 1.1 leo
275 1.9 leo extern __inline__ void
276 1.9 leo scsi_tt_ienable(void)
277 1.1 leo {
278 1.1 leo int sps = splbio();
279 1.1 leo MFP2->mf_ierb |= IB_SCDM;
280 1.1 leo MFP2->mf_iera |= IA_SCSI;
281 1.1 leo splx(sps);
282 1.1 leo }
283 1.1 leo
284 1.9 leo extern __inline__ void
285 1.9 leo scsi_tt_idisable(void)
286 1.1 leo {
287 1.1 leo int sps = splbio();
288 1.1 leo MFP2->mf_ierb &= ~IB_SCDM;
289 1.1 leo MFP2->mf_iera &= ~IA_SCSI;
290 1.1 leo splx(sps);
291 1.1 leo }
292 1.1 leo
293 1.9 leo extern __inline__ void
294 1.9 leo scsi_tt_clr_ipend(void)
295 1.1 leo {
296 1.1 leo int tmp;
297 1.1 leo
298 1.1 leo SCSI_DMA->s_dma_ctrl = 0;
299 1.1 leo tmp = GET_TT_REG(NCR5380_IRCV);
300 1.1 leo }
301 1.1 leo
302 1.9 leo static void
303 1.9 leo scsi_tt_dmasetup(SC_REQ *reqp, u_int phase, u_char mode)
304 1.1 leo {
305 1.1 leo if (PH_IN(phase)) {
306 1.1 leo SCSI_DMA->s_dma_ctrl = SD_IN;
307 1.1 leo set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
308 1.1 leo set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
309 1.1 leo SET_TT_REG(NCR5380_ICOM, 0);
310 1.1 leo SET_TT_REG(NCR5380_MODE, mode);
311 1.1 leo SCSI_DMA->s_dma_ctrl = SD_ENABLE;
312 1.1 leo SET_TT_REG(NCR5380_IRCV, 0);
313 1.1 leo }
314 1.1 leo else {
315 1.1 leo SCSI_DMA->s_dma_ctrl = SD_OUT;
316 1.1 leo set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
317 1.1 leo set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
318 1.1 leo SET_TT_REG(NCR5380_MODE, mode);
319 1.1 leo SET_TT_REG(NCR5380_ICOM, SC_ADTB);
320 1.1 leo SET_TT_REG(NCR5380_DMSTAT, 0);
321 1.1 leo SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT;
322 1.1 leo }
323 1.1 leo }
324 1.1 leo
325 1.1 leo static int
326 1.8 leo tt_poll_edma(SC_REQ *reqp)
327 1.1 leo {
328 1.1 leo u_char dmstat, dmastat;
329 1.1 leo int timeout = 9000; /* XXX */
330 1.1 leo
331 1.1 leo /*
332 1.1 leo * We wait here until the DMA has finished. This can be
333 1.1 leo * achieved by checking the following conditions:
334 1.1 leo * - 5380:
335 1.1 leo * - End of DMA flag is set
336 1.1 leo * - We lost BSY (error!!)
337 1.1 leo * - A phase mismatch has occured (partial transfer)
338 1.1 leo * - DMA-controller:
339 1.1 leo * - A bus error occurred (Kernel error!!)
340 1.1 leo * - All bytes are transferred
341 1.1 leo * If one of the terminating conditions was met, we call
342 1.1 leo * 'dma_ready' to check errors and perform the bookkeeping.
343 1.1 leo */
344 1.1 leo
345 1.1 leo for (;;) {
346 1.1 leo delay(20);
347 1.1 leo if (--timeout <= 0) {
348 1.1 leo ncr_tprint(reqp, "timeout on polled transfer\n");
349 1.11 leo reqp->xs->error = XS_TIMEOUT;
350 1.1 leo return(0);
351 1.1 leo }
352 1.1 leo dmstat = GET_TT_REG(NCR5380_DMSTAT);
353 1.1 leo dmastat = SCSI_DMA->s_dma_ctrl;
354 1.1 leo if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET))
355 1.1 leo break;
356 1.1 leo if (!(dmstat & SC_PHS_MTCH))
357 1.1 leo break;
358 1.1 leo if (dmastat & (SD_BUSERR|SD_ZERO))
359 1.1 leo break;
360 1.1 leo }
361 1.1 leo return(1);
362 1.1 leo }
363 1.1 leo
364 1.2 leo /*
365 1.2 leo * Convert physical DMA address to a virtual address.
366 1.2 leo */
367 1.2 leo static u_char *
368 1.8 leo ptov(SC_REQ *reqp, u_long *phaddr)
369 1.2 leo {
370 1.2 leo struct dma_chain *dm;
371 1.2 leo u_char *vaddr;
372 1.2 leo
373 1.2 leo dm = reqp->dm_chain;
374 1.2 leo vaddr = reqp->xdata_ptr;
375 1.2 leo for(; dm < reqp->dm_cur; dm++)
376 1.2 leo vaddr += dm->dm_count;
377 1.2 leo vaddr += (u_long)phaddr - dm->dm_addr;
378 1.2 leo return(vaddr);
379 1.2 leo }
380 1.2 leo
381 1.1 leo static int
382 1.8 leo tt_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
383 1.1 leo {
384 1.1 leo int dmastat, dmstat;
385 1.1 leo u_char *byte_p;
386 1.1 leo u_long leftover;
387 1.1 leo
388 1.1 leo dmastat = SCSI_DMA->s_dma_ctrl;
389 1.1 leo dmstat = GET_TT_REG(NCR5380_DMSTAT);
390 1.1 leo get_scsi_dma(SCSI_DMA->s_dma_cnt, leftover);
391 1.1 leo get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)byte_p);
392 1.1 leo
393 1.1 leo if (dmastat & SD_BUSERR) {
394 1.1 leo /*
395 1.1 leo * The DMA-controller seems to access 8 bytes beyond
396 1.1 leo * it's limits on output. Therefore check also the byte
397 1.1 leo * count. If it's zero, ignore the bus error.
398 1.1 leo */
399 1.1 leo if (leftover != 0) {
400 1.1 leo ncr_tprint(reqp,
401 1.1 leo "SCSI-DMA buserror - accessing 0x%x\n", byte_p);
402 1.1 leo reqp->xs->error = XS_DRIVER_STUFFUP;
403 1.1 leo }
404 1.1 leo }
405 1.1 leo
406 1.1 leo /*
407 1.1 leo * We handle the following special condition below:
408 1.1 leo * -- The device disconnects in the middle of a write operation --
409 1.1 leo * In this case, the 5380 has already pre-fetched the next byte from
410 1.1 leo * the DMA-controller before the phase mismatch occurs. Therefore,
411 1.1 leo * leftover is 1 too low.
412 1.1 leo * This does not always happen! Therefore, we only do this when
413 1.1 leo * leftover is odd. This assumes that DMA transfers are _even_! This
414 1.1 leo * is normally the case on disks and types but might not always be.
415 1.1 leo * XXX: Check if ACK is consistently high on these occasions LWP
416 1.1 leo */
417 1.1 leo if ((leftover & 1) && !(dmstat & SC_PHS_MTCH) && PH_OUT(reqp->phase))
418 1.1 leo leftover++;
419 1.1 leo
420 1.1 leo /*
421 1.1 leo * Check if there are some 'restbytes' left in the DMA-controller.
422 1.1 leo */
423 1.1 leo if (((u_long)byte_p & 3) && PH_IN(reqp->phase)) {
424 1.1 leo u_char *p, *q;
425 1.1 leo
426 1.8 leo p = ptov(reqp, (u_long *)((u_long)byte_p & ~3));
427 1.1 leo q = (u_char*)&(SCSI_DMA->s_dma_res);
428 1.2 leo switch ((u_long)byte_p & 3) {
429 1.1 leo case 3: *p++ = *q++;
430 1.1 leo case 2: *p++ = *q++;
431 1.1 leo case 1: *p++ = *q++;
432 1.1 leo }
433 1.1 leo }
434 1.1 leo *bytes_left = leftover;
435 1.1 leo return ((dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0);
436 1.1 leo }
437 1.1 leo
438 1.1 leo #endif /* defined(TT_SCSI) */
439 1.1 leo
440 1.1 leo #if defined(FALCON_SCSI) && !defined(TT_SCSI)
441 1.1 leo
442 1.1 leo #define GET_5380_REG(rnum) get_falcon_5380_reg(rnum)
443 1.1 leo #define SET_5380_REG(rnum,val) set_falcon_5380_reg(rnum, val)
444 1.1 leo #define scsi_mach_init(sc) scsi_falcon_init(sc)
445 1.1 leo #define scsi_ienable() scsi_falcon_ienable()
446 1.1 leo #define scsi_idisable() scsi_falcon_idisable()
447 1.1 leo #define scsi_clr_ipend() scsi_falcon_clr_ipend()
448 1.7 leo #define scsi_ipending() scsi_falcon_ipending()
449 1.1 leo #define scsi_dma_setup(r,p,m) scsi_falcon_dmasetup(r, p, m)
450 1.1 leo #define wrong_dma_range(r,d) falcon_wrong_dma_range(r, d)
451 1.1 leo #define poll_edma(reqp) falcon_poll_edma(reqp)
452 1.1 leo #define get_dma_result(r, b) falcon_get_dma_result(r, b)
453 1.16 leo #define can_access_5380() falcon_can_access_5380()
454 1.1 leo
455 1.1 leo #define fair_to_keep_dma() (!st_dmawanted())
456 1.1 leo #define claimed_dma() falcon_claimed_dma()
457 1.1 leo #define reconsider_dma() falcon_reconsider_dma()
458 1.1 leo
459 1.1 leo #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */
460 1.1 leo
461 1.1 leo #if defined(FALCON_SCSI)
462 1.1 leo
463 1.9 leo /*
464 1.9 leo * Prototype functions defined below
465 1.9 leo */
466 1.9 leo static void scsi_falcon_init __P((struct ncr_softc *));
467 1.9 leo static u_char get_falcon_5380_reg __P((u_short));
468 1.9 leo static void set_falcon_5380_reg __P((u_short, u_short));
469 1.9 leo static int falcon_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
470 1.9 leo static void fal1_dma __P((u_int, u_int, SC_REQ *));
471 1.9 leo static void scsi_falcon_dmasetup __P((SC_REQ *, u_int, u_char));
472 1.9 leo static int falcon_poll_edma __P((SC_REQ *));
473 1.9 leo static int falcon_get_dma_result __P((SC_REQ *, u_long *));
474 1.16 leo int falcon_can_access_5380 __P((void));
475 1.13 leo void scsi_falcon_clr_ipend __P((void));
476 1.13 leo void scsi_falcon_idisable __P((void));
477 1.9 leo void scsi_falcon_ienable __P((void));
478 1.13 leo int scsi_falcon_ipending __P((void));
479 1.13 leo int falcon_claimed_dma __P((void));
480 1.13 leo void falcon_reconsider_dma __P((void));
481 1.1 leo
482 1.9 leo static void
483 1.9 leo scsi_falcon_init(sc)
484 1.1 leo struct ncr_softc *sc;
485 1.1 leo {
486 1.1 leo /*
487 1.1 leo * Enable disk related interrupts
488 1.1 leo */
489 1.1 leo MFP->mf_ierb |= IB_DINT;
490 1.1 leo MFP->mf_iprb &= ~IB_DINT;
491 1.1 leo MFP->mf_imrb |= IB_DINT;
492 1.1 leo }
493 1.1 leo
494 1.9 leo static u_char
495 1.9 leo get_falcon_5380_reg(rnum)
496 1.1 leo u_short rnum;
497 1.1 leo {
498 1.1 leo DMA->dma_mode = DMA_SCSI + rnum;
499 1.1 leo return(DMA->dma_data);
500 1.1 leo }
501 1.1 leo
502 1.9 leo static void
503 1.9 leo set_falcon_5380_reg(rnum, val)
504 1.1 leo u_short rnum, val;
505 1.1 leo {
506 1.1 leo DMA->dma_mode = DMA_SCSI + rnum;
507 1.1 leo DMA->dma_data = val;
508 1.1 leo }
509 1.1 leo
510 1.9 leo extern __inline__ void
511 1.9 leo scsi_falcon_ienable()
512 1.1 leo {
513 1.1 leo MFP->mf_ierb |= IB_DINT;
514 1.1 leo }
515 1.1 leo
516 1.9 leo extern __inline__ void
517 1.9 leo scsi_falcon_idisable()
518 1.1 leo {
519 1.1 leo MFP->mf_ierb &= ~IB_DINT;
520 1.1 leo }
521 1.1 leo
522 1.9 leo extern __inline__ void
523 1.9 leo scsi_falcon_clr_ipend()
524 1.1 leo {
525 1.1 leo int tmp;
526 1.1 leo
527 1.1 leo tmp = get_falcon_5380_reg(NCR5380_IRCV);
528 1.15 leo rem_sicallback((si_farg)ncr_ctrl_intr);
529 1.1 leo }
530 1.1 leo
531 1.9 leo extern __inline__ int
532 1.9 leo scsi_falcon_ipending()
533 1.7 leo {
534 1.13 leo if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
535 1.13 leo /*
536 1.13 leo * XXX: When DMA is running, we are only allowed to
537 1.13 leo * check the 5380 when DMA _might_ be finished.
538 1.13 leo */
539 1.13 leo if (MFP->mf_gpip & IO_DINT)
540 1.13 leo return (0); /* XXX: Actually: we're not allowed to check */
541 1.16 leo
542 1.16 leo /* LWP: 28-06, must be a dma interrupt! should the
543 1.16 leo * ST-DMA unit be taken out of dma mode?????
544 1.16 leo */
545 1.16 leo DMA->dma_mode = 0x90;
546 1.16 leo
547 1.13 leo }
548 1.13 leo return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET);
549 1.7 leo }
550 1.7 leo
551 1.9 leo static int
552 1.9 leo falcon_wrong_dma_range(reqp, dm)
553 1.1 leo SC_REQ *reqp;
554 1.1 leo struct dma_chain *dm;
555 1.1 leo {
556 1.1 leo /*
557 1.1 leo * Do not allow chains yet! See also comment with
558 1.1 leo * falcon_poll_edma() !!!
559 1.1 leo */
560 1.1 leo if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
561 1.1 leo reqp->dr_flag |= DRIVER_BOUNCING;
562 1.1 leo return(1);
563 1.1 leo }
564 1.2 leo /*
565 1.2 leo * Never allow DMA to happen on a Falcon when the transfer
566 1.2 leo * size is no multiple of 512. This is the transfer unit of the
567 1.2 leo * ST DMA-controller.
568 1.2 leo */
569 1.2 leo if(dm->dm_count & 511)
570 1.2 leo return(1);
571 1.1 leo return(0);
572 1.1 leo }
573 1.1 leo
574 1.1 leo static int falcon_lock = 0;
575 1.1 leo
576 1.9 leo extern __inline__ int
577 1.9 leo falcon_claimed_dma()
578 1.1 leo {
579 1.11 leo if (falcon_lock != DMA_LOCK_GRANT) {
580 1.11 leo if (falcon_lock == DMA_LOCK_REQ) {
581 1.1 leo /*
582 1.1 leo * DMA access is being claimed.
583 1.1 leo */
584 1.1 leo return(0);
585 1.1 leo }
586 1.15 leo if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
587 1.15 leo cur_softc, &falcon_lock, 1))
588 1.1 leo return(0);
589 1.1 leo }
590 1.1 leo return(1);
591 1.1 leo }
592 1.1 leo
593 1.9 leo extern __inline__ void
594 1.9 leo falcon_reconsider_dma()
595 1.1 leo {
596 1.1 leo if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
597 1.1 leo /*
598 1.1 leo * No need to keep DMA locked by us as we are not currently
599 1.1 leo * connected and no disconnected jobs are pending.
600 1.1 leo */
601 1.15 leo rem_sicallback((si_farg)ncr_ctrl_intr);
602 1.15 leo st_dmafree(cur_softc, &falcon_lock);
603 1.1 leo }
604 1.1 leo
605 1.1 leo if (!falcon_lock && (issue_q != NULL)) {
606 1.1 leo /*
607 1.1 leo * We must (re)claim DMA access as there are jobs
608 1.1 leo * waiting in the issue queue.
609 1.1 leo */
610 1.15 leo st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
611 1.15 leo cur_softc, &falcon_lock, 0);
612 1.1 leo }
613 1.1 leo }
614 1.1 leo
615 1.9 leo static void
616 1.9 leo fal1_dma(dir, nsects, reqp)
617 1.1 leo u_int dir, nsects;
618 1.1 leo SC_REQ *reqp;
619 1.1 leo {
620 1.1 leo dir <<= 8;
621 1.1 leo st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr);
622 1.1 leo DMA->dma_mode = 0x90 | dir;
623 1.1 leo DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
624 1.1 leo DMA->dma_mode = 0x90 | dir;
625 1.1 leo DMA->dma_data = nsects;
626 1.7 leo delay(2); /* _really_ needed (Thomas Gerner) */
627 1.1 leo DMA->dma_mode = 0x10 | dir;
628 1.1 leo }
629 1.1 leo
630 1.9 leo static void
631 1.9 leo scsi_falcon_dmasetup(reqp, phase, mode)
632 1.1 leo SC_REQ *reqp;
633 1.1 leo u_int phase;
634 1.1 leo u_char mode;
635 1.1 leo {
636 1.1 leo int nsects = reqp->dm_cur->dm_count / 512; /* XXX */
637 1.1 leo
638 1.1 leo /*
639 1.1 leo * XXX: We should probably clear the fifo before putting the
640 1.1 leo * 5380 into DMA-mode.
641 1.1 leo */
642 1.1 leo if (PH_IN(phase)) {
643 1.1 leo set_falcon_5380_reg(NCR5380_ICOM, 0);
644 1.1 leo set_falcon_5380_reg(NCR5380_MODE, mode);
645 1.1 leo set_falcon_5380_reg(NCR5380_IRCV, 0);
646 1.1 leo fal1_dma(0, nsects, reqp);
647 1.1 leo }
648 1.1 leo else {
649 1.1 leo set_falcon_5380_reg(NCR5380_MODE, mode);
650 1.1 leo set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
651 1.1 leo set_falcon_5380_reg(NCR5380_DMSTAT, 0);
652 1.1 leo fal1_dma(1, nsects, reqp);
653 1.1 leo }
654 1.1 leo }
655 1.1 leo
656 1.1 leo static int
657 1.1 leo falcon_poll_edma(reqp)
658 1.1 leo SC_REQ *reqp;
659 1.1 leo {
660 1.1 leo int timeout = 9000; /* XXX */
661 1.1 leo
662 1.1 leo /*
663 1.1 leo * Because of the Falcon hardware, it is impossible to reach
664 1.1 leo * the 5380 while doing DMA-transfers. So we have to rely on
665 1.1 leo * the interrupt line to determine if DMA-has finished. the
666 1.1 leo * DMA-controller itself will never fire an interrupt. This means
667 1.1 leo * that 'broken-up' DMA transfers are not (yet) possible on the
668 1.1 leo * Falcon.
669 1.1 leo */
670 1.1 leo for (;;) {
671 1.1 leo delay(20);
672 1.1 leo if (--timeout <= 0) {
673 1.1 leo ncr_tprint(reqp, "Timeout on polled transfer\n");
674 1.11 leo reqp->xs->error = XS_TIMEOUT;
675 1.1 leo return(0);
676 1.1 leo }
677 1.1 leo if (!(MFP->mf_gpip & IO_DINT))
678 1.1 leo break;
679 1.1 leo }
680 1.1 leo return(1);
681 1.1 leo }
682 1.1 leo
683 1.1 leo static int
684 1.1 leo falcon_get_dma_result(reqp, bytes_left)
685 1.1 leo SC_REQ *reqp;
686 1.1 leo u_long *bytes_left;
687 1.1 leo {
688 1.1 leo int rv = 0;
689 1.1 leo int st_dmastat;
690 1.1 leo u_long bytes_done;
691 1.1 leo
692 1.1 leo /*
693 1.1 leo * Select sector counter register first (See Atari docu.)
694 1.1 leo */
695 1.1 leo DMA->dma_mode = 0x90;
696 1.1 leo if (!(st_dmastat = DMA->dma_stat) & 0x01) {
697 1.1 leo /*
698 1.1 leo * Misc. DMA-error according to Atari...
699 1.1 leo */
700 1.1 leo ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
701 1.1 leo st_dmaaddr_get());
702 1.1 leo reqp->xs->error = XS_DRIVER_STUFFUP;
703 1.1 leo rv = 1;
704 1.1 leo }
705 1.16 leo /*
706 1.16 leo * Because we NEVER start DMA on the Falcon when the data size
707 1.16 leo * is not a multiple of 512 bytes, we can safely round down the
708 1.16 leo * byte count on writes. We need to because in case of a disconnect,
709 1.16 leo * the DMA has already prefetched the next couple of bytes.
710 1.16 leo * On read, these byte counts are an error. They are logged and
711 1.16 leo * should be handled by the mi-part of the driver.
712 1.16 leo * NOTE: We formerly did this by using the 'byte-count-zero' bit
713 1.16 leo * of the DMA controller, but this didn't seem to work???
714 1.16 leo * [lwp 29/06/96]
715 1.16 leo */
716 1.16 leo bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
717 1.16 leo if (bytes_done & 511) {
718 1.16 leo if (PH_IN(reqp->phase)) {
719 1.16 leo ncr_tprint(reqp, "Byte count on read not a multiple "
720 1.16 leo "of 512 (%ld)\n", bytes_done);
721 1.1 leo }
722 1.16 leo bytes_done &= ~511;
723 1.1 leo }
724 1.16 leo if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0)
725 1.1 leo rv = 1;
726 1.1 leo return(rv);
727 1.1 leo }
728 1.1 leo
729 1.16 leo static int
730 1.16 leo falcon_can_access_5380()
731 1.16 leo {
732 1.16 leo if (connected && (connected->dr_flag & DRIVER_IN_DMA)
733 1.16 leo && (MFP->mf_gpip & IO_DINT))
734 1.16 leo return(0);
735 1.16 leo return(1);
736 1.16 leo }
737 1.16 leo
738 1.1 leo #endif /* defined(FALCON_SCSI) */
739 1.1 leo
740 1.1 leo #if defined(TT_SCSI) && defined(FALCON_SCSI)
741 1.1 leo /*
742 1.1 leo * Define some functions to support _both_ TT and Falcon SCSI
743 1.1 leo */
744 1.1 leo
745 1.1 leo /*
746 1.9 leo * The prototypes first...
747 1.9 leo */
748 1.9 leo static void scsi_mach_init __P((struct ncr_softc *));
749 1.9 leo void scsi_ienable __P((void));
750 1.9 leo void scsi_idisable __P((void));
751 1.9 leo void scsi_clr_ipend __P((void));
752 1.9 leo int scsi_ipending __P((void));
753 1.9 leo void scsi_dma_setup __P((SC_REQ *, u_int, u_char));
754 1.9 leo int wrong_dma_range __P((SC_REQ *, struct dma_chain *));
755 1.9 leo int poll_edma __P((SC_REQ *));
756 1.9 leo int get_dma_result __P((SC_REQ *, u_long *));
757 1.16 leo int can_access_5380 __P((void));
758 1.9 leo
759 1.9 leo /*
760 1.1 leo * Register access will be done through the following 2 function pointers.
761 1.1 leo */
762 1.9 leo static u_char (*get_5380_reg) __P((u_short));
763 1.9 leo static void (*set_5380_reg) __P((u_short, u_short));
764 1.1 leo
765 1.1 leo #define GET_5380_REG (*get_5380_reg)
766 1.1 leo #define SET_5380_REG (*set_5380_reg)
767 1.1 leo
768 1.9 leo static void
769 1.9 leo scsi_mach_init(sc)
770 1.1 leo struct ncr_softc *sc;
771 1.1 leo {
772 1.1 leo if (machineid & ATARI_FALCON) {
773 1.1 leo get_5380_reg = get_falcon_5380_reg;
774 1.1 leo set_5380_reg = set_falcon_5380_reg;
775 1.1 leo scsi_falcon_init(sc);
776 1.1 leo }
777 1.1 leo else {
778 1.1 leo get_5380_reg = get_tt_5380_reg;
779 1.1 leo set_5380_reg = set_tt_5380_reg;
780 1.1 leo scsi_tt_init(sc);
781 1.1 leo }
782 1.1 leo }
783 1.1 leo
784 1.9 leo extern __inline__ void
785 1.9 leo scsi_ienable()
786 1.1 leo {
787 1.1 leo if (machineid & ATARI_FALCON)
788 1.1 leo scsi_falcon_ienable();
789 1.1 leo else scsi_tt_ienable();
790 1.1 leo }
791 1.1 leo
792 1.9 leo extern __inline__ void
793 1.9 leo scsi_idisable()
794 1.1 leo {
795 1.1 leo if (machineid & ATARI_FALCON)
796 1.1 leo scsi_falcon_idisable();
797 1.1 leo else scsi_tt_idisable();
798 1.1 leo }
799 1.1 leo
800 1.9 leo extern __inline__ void
801 1.9 leo scsi_clr_ipend()
802 1.1 leo {
803 1.1 leo if (machineid & ATARI_FALCON)
804 1.1 leo scsi_falcon_clr_ipend();
805 1.1 leo else scsi_tt_clr_ipend();
806 1.7 leo }
807 1.7 leo
808 1.9 leo extern __inline__ int
809 1.9 leo scsi_ipending()
810 1.7 leo {
811 1.7 leo if (machineid & ATARI_FALCON)
812 1.7 leo return(scsi_falcon_ipending());
813 1.7 leo else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
814 1.1 leo }
815 1.1 leo
816 1.9 leo extern __inline__ void
817 1.9 leo scsi_dma_setup(reqp, phase, mbase)
818 1.1 leo SC_REQ *reqp;
819 1.1 leo u_int phase;
820 1.1 leo u_char mbase;
821 1.1 leo {
822 1.1 leo if (machineid & ATARI_FALCON)
823 1.1 leo scsi_falcon_dmasetup(reqp, phase, mbase);
824 1.1 leo else scsi_tt_dmasetup(reqp, phase, mbase);
825 1.1 leo }
826 1.1 leo
827 1.9 leo extern __inline__ int
828 1.9 leo wrong_dma_range(reqp, dm)
829 1.1 leo SC_REQ *reqp;
830 1.1 leo struct dma_chain *dm;
831 1.1 leo {
832 1.1 leo if (machineid & ATARI_FALCON)
833 1.1 leo return(falcon_wrong_dma_range(reqp, dm));
834 1.1 leo else return(tt_wrong_dma_range(reqp, dm));
835 1.1 leo }
836 1.1 leo
837 1.9 leo extern __inline__ int
838 1.9 leo poll_edma(reqp)
839 1.1 leo SC_REQ *reqp;
840 1.1 leo {
841 1.1 leo if (machineid & ATARI_FALCON)
842 1.1 leo return(falcon_poll_edma(reqp));
843 1.1 leo else return(tt_poll_edma(reqp));
844 1.1 leo }
845 1.1 leo
846 1.9 leo extern __inline__ int
847 1.9 leo get_dma_result(reqp, bytes_left)
848 1.1 leo SC_REQ *reqp;
849 1.1 leo u_long *bytes_left;
850 1.1 leo {
851 1.1 leo if (machineid & ATARI_FALCON)
852 1.1 leo return(falcon_get_dma_result(reqp, bytes_left));
853 1.1 leo else return(tt_get_dma_result(reqp, bytes_left));
854 1.1 leo }
855 1.1 leo
856 1.16 leo extern __inline__ int
857 1.16 leo can_access_5380()
858 1.16 leo {
859 1.16 leo if (machineid & ATARI_FALCON)
860 1.16 leo return(falcon_can_access_5380());
861 1.16 leo return(1);
862 1.16 leo }
863 1.16 leo
864 1.1 leo /*
865 1.1 leo * Locking stuff. All turns into NOP's on the TT.
866 1.1 leo */
867 1.1 leo #define fair_to_keep_dma() ((machineid & ATARI_FALCON) ? \
868 1.1 leo !st_dmawanted() : 1)
869 1.1 leo #define claimed_dma() ((machineid & ATARI_FALCON) ? \
870 1.1 leo falcon_claimed_dma() : 1)
871 1.1 leo #define reconsider_dma() { \
872 1.1 leo if(machineid & ATARI_FALCON) \
873 1.1 leo falcon_reconsider_dma();\
874 1.1 leo }
875 1.1 leo #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
876 1.1 leo
877 1.1 leo /**********************************************
878 1.1 leo * Functions present for both TT and Falcon. *
879 1.1 leo **********************************************/
880 1.1 leo /*
881 1.1 leo * Our autoconfig matching function
882 1.1 leo */
883 1.1 leo static int
884 1.10 thorpej machine_match(struct device *pdp, void *match, void *auxp,
885 1.8 leo struct cfdriver *cd)
886 1.1 leo {
887 1.10 thorpej struct cfdata *cdp = match;
888 1.10 thorpej
889 1.1 leo if (strcmp(auxp, cd->cd_name))
890 1.1 leo return(0);
891 1.1 leo if (cdp->cf_unit != 0) /* Only one unit */
892 1.1 leo return(0);
893 1.1 leo return(1);
894 1.1 leo }
895 1.1 leo
896 1.1 leo /*
897 1.1 leo * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
898 1.1 leo * pool. Allocation here is both contiguous and in the lower 16Mb of
899 1.1 leo * the address space. Thus being DMA-able for all controllers.
900 1.1 leo */
901 1.1 leo static u_char *
902 1.8 leo alloc_bounceb(u_long len)
903 1.1 leo {
904 1.12 leo void *tmp;
905 1.1 leo
906 1.1 leo return((u_char *)alloc_stmem(len, &tmp));
907 1.1 leo }
908 1.1 leo
909 1.1 leo static void
910 1.8 leo free_bounceb(u_char *bounceb)
911 1.1 leo {
912 1.1 leo free_stmem(bounceb);
913 1.1 leo }
914 1.1 leo
915 1.1 leo /*
916 1.1 leo * 5380 interrupt.
917 1.1 leo */
918 1.8 leo void
919 1.8 leo scsi_ctrl(int sr)
920 1.1 leo {
921 1.1 leo if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
922 1.1 leo scsi_idisable();
923 1.1 leo if (!BASEPRI(sr))
924 1.12 leo add_sicallback((si_farg)ncr_ctrl_intr,
925 1.12 leo (void *)cur_softc, 0);
926 1.1 leo else {
927 1.1 leo spl1();
928 1.1 leo ncr_ctrl_intr(cur_softc);
929 1.16 leo spl0();
930 1.1 leo }
931 1.1 leo }
932 1.1 leo }
933 1.1 leo
934 1.1 leo /*
935 1.1 leo * DMA controller interrupt
936 1.1 leo */
937 1.8 leo void
938 1.8 leo scsi_dma(int sr)
939 1.1 leo {
940 1.1 leo SC_REQ *reqp;
941 1.1 leo
942 1.1 leo if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
943 1.1 leo scsi_idisable();
944 1.1 leo if (!BASEPRI(sr))
945 1.12 leo add_sicallback((si_farg)ncr_dma_intr,
946 1.12 leo (void *)cur_softc, 0);
947 1.1 leo else {
948 1.1 leo spl1();
949 1.1 leo ncr_dma_intr(cur_softc);
950 1.16 leo spl0();
951 1.1 leo }
952 1.1 leo }
953 1.1 leo }
954 1.1 leo
955 1.1 leo /*
956 1.1 leo * Last but not least... Include the general driver code
957 1.1 leo */
958 1.6 mycroft #include <atari/dev/ncr5380.c>
959