mac68k5380.c revision 1.1 1 /* $NetBSD: mac68k5380.c,v 1.1 1995/09/01 03:43:49 briggs Exp $ */
2
3 /*
4 * Copyright (c) 1995 Allen Briggs
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Allen Briggs
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Derived from atari5380.c for the mac68k port of NetBSD.
33 *
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/syslog.h>
41 #include <sys/buf.h>
42 #include <scsi/scsi_all.h>
43 #include <scsi/scsi_message.h>
44 #include <scsi/scsiconf.h>
45
46 /*
47 * Include the driver definitions
48 */
49 #include <atari/dev/ncr5380reg.h>
50
51 #include <machine/stdarg.h>
52
53 #include "../mac68k/via.h"
54
55 #undef splbio()
56 #define splbio() splhigh()
57
58 /*
59 * Set the various driver options
60 */
61 #define NREQ 18 /* Size of issue queue */
62 #define AUTO_SENSE 1 /* Automatically issue a request-sense */
63
64 #define DRNAME ncrscsi /* used in various prints */
65 #undef DBG_SEL /* Show the selection process */
66 #undef DBG_REQ /* Show enqueued/ready requests */
67 #undef DBG_NOWRITE /* Do not allow writes to the targets */
68 #undef DBG_PIO /* Show the polled-I/O process */
69 #undef DBG_INF /* Show information transfer process */
70 #define DBG_NOSTATIC /* No static functions, all in DDB trace*/
71 #define DBG_PID /* Keep track of driver */
72 #undef REAL_DMA /* Use DMA if sensible */
73 #define fair_to_keep_dma() 1
74 #define claimed_dma() 1
75 #define reconsider_dma()
76 #define USE_PDMA 1 /* Use special pdma-transfer function */
77
78 #define ENABLE_NCR5380(sc) cur_softc = sc;
79
80 /*
81 * softc of currently active controller (well, we only have one for now).
82 */
83
84 static struct ncr_softc *cur_softc;
85
86 struct scsi_5380 {
87 volatile u_char scsi_5380[8*16]; /* 8 regs, 1 every 16th byte. */
88 };
89
90 extern vm_offset_t SCSIBase;
91 static volatile u_char *ncr = (volatile u_char *) 0x10000;
92 static volatile u_char *ncr_5380_with_drq = (volatile u_char *) 0x6000;
93 static volatile u_char *ncr_5380_without_drq = (volatile u_char *) 0x12000;
94
95 #define SCSI_5380 ((struct scsi_5380 *) ncr)
96 #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[((rnum)<<4)]
97 #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[((rnum)<<4)] = (val))
98
99 static __inline__ void
100 scsi_clr_ipend()
101 {
102 int tmp;
103
104 tmp = GET_5380_REG(NCR5380_IRCV);
105 }
106
107 extern __inline__ void
108 scsi_ienable()
109 {
110 int s;
111
112 s = splhigh();
113 if (VIA2 == VIA2OFF)
114 via_reg(VIA2, vIER) = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
115 else
116 via_reg(VIA2, rIER) = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
117 splx(s);
118 }
119
120 extern __inline__ void
121 scsi_idisable()
122 {
123 int s;
124
125 s = splhigh();
126 if (VIA2 == VIA2OFF)
127 via_reg(VIA2, vIER) = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
128 else
129 via_reg(VIA2, rIER) = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
130 splx(s);
131 }
132
133 static void
134 scsi_mach_init(sc)
135 struct ncr_softc *sc;
136 {
137 static int initted = 0;
138
139 if (initted++)
140 panic("scsi_mach_init called again.\n");
141
142 ncr = (volatile u_char *)
143 (SCSIBase + (u_long) ncr);
144 ncr_5380_with_drq = (volatile u_char *)
145 (SCSIBase + (u_int) ncr_5380_with_drq);
146 ncr_5380_without_drq = (volatile u_char *)
147 (SCSIBase + (u_int) ncr_5380_without_drq);
148 }
149
150 static int
151 machine_match(pdp, cdp, auxp, cd)
152 struct device *pdp;
153 struct cfdata *cdp;
154 void *auxp;
155 struct cfdriver *cd;
156 {
157 if (matchbyname(pdp, cdp, auxp) == 0)
158 return 0;
159 if (!mac68k_machine.scsi80)
160 return 0;
161 if (cdp->cf_unit != 0)
162 return 0;
163 return 1;
164 }
165
166 #if USE_PDMA
167 int pdma_5380_dir = 0;
168 int (*pdma_xfer_fun)(void) = NULL;
169 int pdma_5380_sleeping = 0;
170
171 volatile int pdma_5380_pending = 0;
172 volatile u_char *pending_5380_data;
173 volatile u_long pending_5380_count;
174
175 #define DEBUG 1 /* Maybe we try with this off eventually. */
176 #if DEBUG
177 int pdma_5380_sends = 0;
178
179 volatile char *pdma_5380_state="";
180 void
181 pdma_stat()
182 {
183 printf("PDMA SCSI: %d xfers completed, pending = %d.\n",
184 pdma_5380_sends, pdma_5380_pending);
185 printf("xfer fun = 0x%x, pdma_5380_dir = %d.\n",
186 pdma_xfer_fun, pdma_5380_dir);
187 printf("datap = 0x%x, remainder = %d.\n",
188 pending_5380_data, pending_5380_count);
189 printf("pdma_5380_state = %s.\n", pdma_5380_state);
190 }
191 #endif
192 #endif
193
194 void
195 ncr5380_irq_intr(void)
196 {
197 if (pdma_xfer_fun) {
198 #if DEBUG
199 pdma_5380_state = "got irq interrupt in xfer.";
200 #endif
201 /*
202 * If Mr. IRQ isn't set one might wonder how we got
203 * here. It does happen, though.
204 */
205 if (!(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)) {
206 return;
207 }
208 /*
209 * For a phase mis-match, ATN is a "don't care," IRQ is 1 and
210 * all other bits in the Bus & Status Register are 0. Also,
211 * the current SCSI Bus Status Register has a 1 for BSY and
212 * REQ. Since we're just checking that this interrupt isn't a
213 * reselection or a reset, we just check for either.
214 */
215 if ( ((GET_5380_REG(NCR5380_DMSTAT) & (0xff & ~SC_ATN_STAT))
216 == SC_IRQ_SET)
217 && (GET_5380_REG(NCR5380_IDSTAT) & (SC_S_BSY|SC_S_REQ))) {
218 scsi_idisable();
219 scsi_clr_ipend();
220 pdma_5380_pending = 0;
221 if (pdma_5380_sleeping) wakeup(pdma_xfer_fun);
222 return;
223 }
224 scsi_show();
225 panic("Spurious interrupt during PDMA xfer.\n");
226 }
227 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
228 scsi_idisable();
229 ncr_ctrl_intr(cur_softc);
230 }
231 }
232
233 void
234 ncr5380_drq_intr(void)
235 {
236 #if USE_PDMA
237 if (pdma_xfer_fun) {
238 #if DEBUG
239 pdma_5380_state = "got drq interrupt.";
240 #endif
241 if (pdma_xfer_fun())
242 #if DEBUG
243 pdma_5380_state = "handled drq interrupt.";
244 #endif
245 }
246 #endif
247 }
248
249 #if USE_PDMA
250 static int
251 pdma_xfer_in()
252 {
253 /*
254 * Can we "unroll" this any? I don't think so--in fact, I
255 * question the safety of using long word transfers. The
256 * device could theoretically disconnect at any time.
257 * The long word xfer is controlled by the Mac's circuitry,
258 * and we can't know how much it transferred if the device
259 * decides to disconnect on us.
260 * If it does disconnect in the middle of a long xfer, it
261 * should get a bus error--lovely.
262 */
263 while ( (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
264 && (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
265 && (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ) )
266 if (pending_5380_count) {
267 *((u_char *) pending_5380_data)++ = *ncr_5380_with_drq;
268 pending_5380_count --;
269 } else {
270 #if DEBUG
271 pdma_5380_state = "done in xfer in.";
272 #endif
273 SET_5380_REG(NCR5380_MODE,
274 GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
275 return 0;
276 }
277 return 1;
278 }
279
280 /*
281 * Macroed for readability.
282 */
283 #define DONE ( (GET_5380_REG(NCR5380_DMSTAT) & SC_ACK_STAT) \
284 || (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) )
285
286 static int
287 pdma_xfer_out()
288 {
289 /*
290 * See comment on pdma_xfer_in(), above.
291 */
292 while ( (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
293 && (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
294 && (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ) )
295 if (pending_5380_count) {
296 *ncr_5380_with_drq = *((u_char *) pending_5380_data)++;
297 pending_5380_count --;
298 } else {
299 scsi_idisable();
300 #if DEBUG
301 pdma_5380_state = "done in xfer out--waiting.";
302 #endif
303 while (!DONE);
304 #if DEBUG
305 pdma_5380_state = "done in xfer out--really done.";
306 #endif
307 pdma_5380_pending = 0;
308 if (pdma_5380_sleeping) wakeup(pdma_xfer_fun);
309 return 0;
310 }
311 return 1;
312 }
313
314 #define SCSI_TIMEOUT_VAL 10000000
315
316 static int
317 transfer_pdma(phasep, data, count)
318 u_char *phasep;
319 u_char *data;
320 u_long *count;
321 {
322 SC_REQ *reqp = connected;
323 int len = *count, i, scsi_timeout = SCSI_TIMEOUT_VAL;
324 int s, err;
325
326 if (pdma_5380_pending) {
327 panic("ncrscsi: transfer_pdma called when operation already "
328 "pending.\n");
329 }
330 #if DEBUG
331 pdma_5380_state = "in transfer_pdma.";
332 #endif
333
334 scsi_idisable();
335
336 if (*count < 128) { /* Don't bother with PDMA for short xfers. */
337 #if DEBUG
338 pdma_5380_state = "using transfer_pio.";
339 #endif
340 return transfer_pio(phasep, data, count);
341 }
342
343 switch (*phasep) {
344 default:
345 panic("Unexpected phase in transfer_pdma.\n");
346 case PH_DATAOUT:
347 pdma_5380_dir = 1;
348 break;
349 case PH_DATAIN:
350 pdma_5380_dir = 2;
351 break;
352 }
353
354 /*
355 * Match phases with target.
356 */
357 SET_5380_REG(NCR5380_TCOM, *phasep);
358 scsi_clr_ipend();
359
360 /*
361 * Wait until target asserts BSY.
362 */
363 while ( ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
364 ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
365 ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
366 (--scsi_timeout) );
367 if (!scsi_timeout) {
368 #if DIAGNOSTIC
369 printf("scsi timeout: waiting for BSY in %s.\n",
370 (pdma_5380_dir == 1) ? "pdma_out" : "pdma_in");
371 #endif
372 goto scsi_timeout_error;
373 }
374
375 /*
376 * Set DMA mode and assert data bus.
377 */
378 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) | SC_M_DMA);
379 SET_5380_REG(NCR5380_ICOM, GET_5380_REG(NCR5380_ICOM) | SC_ADTB);
380
381 /*
382 * Load static/volatile values for DRQ interrupt handlers.
383 */
384 pending_5380_data = (volatile u_char *) data;
385 pending_5380_count = len;
386
387 #if DEBUG
388 pdma_5380_state = "wait for interrupt.";
389 #endif
390
391 /*
392 * Set the transfer function to be called on DRQ interrupts.
393 */
394 pdma_xfer_fun = (pdma_5380_dir == 1) ? pdma_xfer_out : pdma_xfer_in;
395
396 /*
397 * Initiate the DMA transaction--sending or receiving.
398 */
399 if (pdma_5380_dir == 1) {
400 SET_5380_REG(NCR5380_DMSTAT, 0);
401 } else {
402 SET_5380_REG(NCR5380_IRCV, 0);
403 }
404
405 pdma_5380_pending = 1;
406
407 /*
408 * Now that we're set up, enable interrupts and drop processor
409 * priority. We've been running at spl2 because that's what the
410 * VIA2/RBV interrupts are on and we're responding to a selection
411 * interrupt, here.
412 */
413 scsi_ienable();
414 s = spl1(); /* Allow lev 2 and above interrupts--we */
415 /* were probably called from spl2(). */
416 if (reqp->xs->flags & SCSI_NOSLEEP) {
417 while (pdma_5380_pending);
418 } else {
419 pdma_5380_sleeping = 1;
420 if ((err = tsleep(pdma_xfer_fun, PRIBIO,
421 (pdma_5380_dir == 1) ? "pdma_out" : "pdma_in", 0))) {
422 log(LOG_ERR, "pdma_xfer--error %d.\n", err);
423 }
424 pdma_5380_sleeping = 0;
425 }
426 splx(s);
427 scsi_idisable();
428 pdma_xfer_fun = NULL;
429
430 #if DEBUG
431 pdma_5380_state = "back from xfer.";
432 pdma_5380_sends++;
433 #endif
434
435 /*
436 * Send remainder back to m.i. code.
437 */
438 *count = pending_5380_count;
439
440 scsi_timeout_error:
441 /*
442 * Clear the DMA mode.
443 */
444 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
445 return -1;
446 }
447 #endif /* if USE_PDMA */
448
449 /* Include general routines. */
450 #include <atari/dev/ncr5380.c>
451