mac68k5380.c revision 1.3 1 /* $NetBSD: mac68k5380.c,v 1.3 1995/09/02 05:36:22 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 /*
56 * Set the various driver options
57 */
58 #define NREQ 18 /* Size of issue queue */
59 #define AUTO_SENSE 1 /* Automatically issue a request-sense */
60
61 #define DRNAME ncrscsi /* used in various prints */
62 #undef DBG_SEL /* Show the selection process */
63 #undef DBG_REQ /* Show enqueued/ready requests */
64 #undef DBG_NOWRITE /* Do not allow writes to the targets */
65 #undef DBG_PIO /* Show the polled-I/O process */
66 #undef DBG_INF /* Show information transfer process */
67 #define DBG_NOSTATIC /* No static functions, all in DDB trace*/
68 #define DBG_PID /* Keep track of driver */
69 #undef REAL_DMA /* Use DMA if sensible */
70 #define fair_to_keep_dma() 1
71 #define claimed_dma() 1
72 #define reconsider_dma()
73 #define USE_PDMA 1 /* Use special pdma-transfer function */
74
75 #define ENABLE_NCR5380(sc) cur_softc = sc;
76
77 /*
78 * softc of currently active controller (well, we only have one for now).
79 */
80
81 static struct ncr_softc *cur_softc;
82
83 struct scsi_5380 {
84 volatile u_char scsi_5380[8*16]; /* 8 regs, 1 every 16th byte. */
85 };
86
87 extern vm_offset_t SCSIBase;
88 static volatile u_char *ncr = (volatile u_char *) 0x10000;
89 static volatile u_char *ncr_5380_with_drq = (volatile u_char *) 0x6000;
90 static volatile u_char *ncr_5380_without_drq = (volatile u_char *) 0x12000;
91
92 #define SCSI_5380 ((struct scsi_5380 *) ncr)
93 #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[((rnum)<<4)]
94 #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[((rnum)<<4)] = (val))
95
96 static __inline__ void
97 scsi_clr_ipend()
98 {
99 int tmp;
100
101 tmp = GET_5380_REG(NCR5380_IRCV);
102 }
103
104 extern __inline__ void
105 scsi_ienable()
106 {
107 int s;
108
109 s = splhigh();
110 if (VIA2 == VIA2OFF)
111 via_reg(VIA2, vIER) = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
112 else
113 via_reg(VIA2, rIER) = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
114 splx(s);
115 }
116
117 extern __inline__ void
118 scsi_idisable()
119 {
120 int s;
121
122 s = splhigh();
123 if (VIA2 == VIA2OFF)
124 via_reg(VIA2, vIER) = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
125 else
126 via_reg(VIA2, rIER) = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
127 splx(s);
128 }
129
130 static void
131 scsi_mach_init(sc)
132 struct ncr_softc *sc;
133 {
134 static int initted = 0;
135
136 if (initted++)
137 panic("scsi_mach_init called again.\n");
138
139 ncr = (volatile u_char *)
140 (SCSIBase + (u_long) ncr);
141 ncr_5380_with_drq = (volatile u_char *)
142 (SCSIBase + (u_int) ncr_5380_with_drq);
143 ncr_5380_without_drq = (volatile u_char *)
144 (SCSIBase + (u_int) ncr_5380_without_drq);
145 }
146
147 static int
148 machine_match(pdp, cdp, auxp, cd)
149 struct device *pdp;
150 struct cfdata *cdp;
151 void *auxp;
152 struct cfdriver *cd;
153 {
154 if (matchbyname(pdp, cdp, auxp) == 0)
155 return 0;
156 if (!mac68k_machine.scsi80)
157 return 0;
158 if (cdp->cf_unit != 0)
159 return 0;
160 return 1;
161 }
162
163 #if USE_PDMA
164 int pdma_5380_dir = 0;
165 void (*pdma_xfer_fun)(void) = NULL;
166
167 volatile int pdma_5380_pending = 0;
168 volatile u_char *pending_5380_data;
169 volatile u_long pending_5380_count;
170
171 #define DEBUG 1 /* Maybe we try with this off eventually. */
172 #if DEBUG
173 int pdma_5380_sends = 0;
174 int pdma_5380_bytes = 0;
175
176 volatile char *pdma_5380_state="";
177 void
178 pdma_stat()
179 {
180 printf("PDMA SCSI: %d xfers completed for %d bytes, pending = %d.\n",
181 pdma_5380_sends, pdma_5380_bytes, pdma_5380_pending);
182 printf("xfer fun = 0x%x, pdma_5380_dir = %d.\n",
183 pdma_xfer_fun, pdma_5380_dir);
184 printf("datap = 0x%x, remainder = %d.\n",
185 pending_5380_data, pending_5380_count);
186 printf("pdma_5380_state = %s.\n", pdma_5380_state);
187 }
188 #endif
189 #endif
190
191 void
192 pdma_cleanup(void)
193 {
194 SC_REQ *reqp = connected;
195 int bytes, s;
196
197 s = splbio();
198
199 pdma_5380_pending = 0;
200 pdma_xfer_fun = NULL;
201
202 #if DEBUG
203 pdma_5380_state = "in pdma_cleanup().";
204 pdma_5380_sends++;
205 pdma_5380_bytes+=(reqp->xdata_len - pending_5380_count);
206 #endif
207
208 /*
209 * Update pointers.
210 */
211 reqp->xdata_ptr += reqp->xdata_len - pending_5380_count;
212 reqp->xdata_len = pending_5380_count;
213
214 /*
215 * Reset DMA mode.
216 */
217 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
218
219 /*
220 * Tell interrupt functions that DMA has ended.
221 */
222 reqp->dr_flag &= ~DRIVER_IN_DMA;
223
224 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
225 SET_5380_REG(NCR5380_ICOM, 0);
226
227 splx(s);
228
229 /*
230 * Back for more punishment.
231 */
232 run_main(cur_softc);
233 }
234
235 static int
236 scsi_main_irq()
237 {
238 if (pdma_xfer_fun) {
239 #if DEBUG
240 pdma_5380_state = "got irq interrupt in xfer.";
241 #endif
242 /*
243 * If Mr. IRQ isn't set one might wonder how we got
244 * here. It does happen, though.
245 */
246 if (!(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)) {
247 return 0;
248 }
249 /*
250 * For a phase mis-match, ATN is a "don't care," IRQ is 1 and
251 * all other bits in the Bus & Status Register are 0. Also,
252 * the current SCSI Bus Status Register has a 1 for BSY and
253 * REQ. Since we're just checking that this interrupt isn't a
254 * reselection or a reset, we just check for either.
255 */
256 if ( ((GET_5380_REG(NCR5380_DMSTAT) & (0xff & ~SC_ATN_STAT))
257 == SC_IRQ_SET)
258 && (GET_5380_REG(NCR5380_IDSTAT) & (SC_S_BSY|SC_S_REQ))) {
259 pdma_cleanup();
260 return 1;
261 } else {
262 scsi_show();
263 panic("Spurious interrupt during PDMA xfer.\n");
264 }
265 }
266 return 0;
267 }
268
269 void
270 ncr5380_irq_intr(void)
271 {
272 if (scsi_main_irq()) {
273 return;
274 }
275 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
276 scsi_idisable();
277 ncr_ctrl_intr(cur_softc);
278 }
279 }
280
281 void
282 ncr5380_drq_intr(void)
283 {
284 #if USE_PDMA
285 if (pdma_xfer_fun) {
286 #if DEBUG
287 pdma_5380_state = "got drq interrupt.";
288 #endif
289 pdma_xfer_fun();
290 }
291 #endif
292 }
293
294 #if USE_PDMA
295 static void
296 pdma_xfer_in()
297 {
298 /*
299 * Can we "unroll" this any? I don't think so--in fact, I
300 * question the safety of using long word transfers. The
301 * device could theoretically disconnect at any time.
302 * The long word xfer is controlled by the Mac's circuitry,
303 * and we can't know how much it transferred if the device
304 * decides to disconnect on us.
305 * If it does disconnect in the middle of a long xfer, it
306 * should get a bus error--we might be able to derive from
307 * that bus error where the transaction stopped, but I
308 * don't want to think about that...
309 */
310 while (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
311 if (pending_5380_count) {
312 *((u_char *) pending_5380_data)++ = *ncr_5380_with_drq;
313 pending_5380_count --;
314 } else {
315 #if DEBUG
316 pdma_5380_state = "done in xfer in.";
317 #endif
318 SET_5380_REG(NCR5380_MODE,
319 GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
320 return;
321 }
322 #if DEBUG
323 pdma_5380_state = "handled drq interrupt.";
324 #endif
325 }
326
327 /*
328 * Macroed for readability.
329 */
330 #define DONE ( (GET_5380_REG(NCR5380_DMSTAT) & SC_ACK_STAT) \
331 || (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) )
332
333 static void
334 pdma_xfer_out()
335 {
336 /*
337 * See comment on pdma_xfer_in(), above.
338 */
339 while (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
340 if (pending_5380_count) {
341 *ncr_5380_with_drq = *((u_char *) pending_5380_data)++;
342 pending_5380_count --;
343 } else {
344 #if DEBUG
345 pdma_5380_state = "done in xfer out--waiting.";
346 #endif
347 while (!DONE);
348 #if DEBUG
349 pdma_5380_state = "done in xfer out--really done.";
350 #endif
351 pdma_cleanup();
352 return;
353 }
354 #if DEBUG
355 pdma_5380_state = "handled drq interrupt.";
356 #endif
357 }
358
359 #define SCSI_TIMEOUT_VAL 10000000
360
361 static int
362 transfer_pdma(phasep, data, count)
363 u_char *phasep;
364 u_char *data;
365 u_long *count;
366 {
367 SC_REQ *reqp = connected;
368 int len = *count, i, scsi_timeout = SCSI_TIMEOUT_VAL;
369 int s, err;
370
371 if (pdma_5380_pending) {
372 panic("ncrscsi: transfer_pdma called when operation already "
373 "pending.\n");
374 }
375 #if DEBUG
376 pdma_5380_state = "in transfer_pdma.";
377 #endif
378
379 scsi_idisable();
380
381 /*
382 * Don't bother with PDMA for short transfers or if we can't sleep.
383 */
384 if ((reqp->dr_flag & DRIVER_NOINT) || (*count < 128)) {
385 #if DEBUG
386 pdma_5380_state = "using transfer_pio.";
387 #endif
388 transfer_pio(phasep, data, count);
389 return -1;
390 }
391
392 switch (*phasep) {
393 default:
394 panic("Unexpected phase in transfer_pdma.\n");
395 case PH_DATAOUT:
396 pdma_5380_dir = 1;
397 break;
398 case PH_DATAIN:
399 pdma_5380_dir = 2;
400 break;
401 }
402
403 /*
404 * Match phases with target.
405 */
406 SET_5380_REG(NCR5380_TCOM, *phasep);
407
408 /*
409 * We are probably already at spl2(), so this is likely a no-op.
410 * Paranoia.
411 */
412 s = splbio();
413
414 /*
415 * Clear pending interrupts.
416 */
417 scsi_clr_ipend();
418
419 /*
420 * Wait until target asserts BSY.
421 */
422 while ( ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
423 ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
424 ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
425 (--scsi_timeout) );
426 if (!scsi_timeout) {
427 #if DIAGNOSTIC
428 printf("scsi timeout: waiting for BSY in %s.\n",
429 (pdma_5380_dir == 1) ? "pdma_out" : "pdma_in");
430 #endif
431 goto scsi_timeout_error;
432 }
433
434 /*
435 * Tell the driver that we're in DMA mode.
436 */
437 reqp->dr_flag |= DRIVER_IN_DMA;
438
439 /*
440 * Set DMA mode and assert data bus.
441 */
442 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) | SC_M_DMA);
443 SET_5380_REG(NCR5380_ICOM, GET_5380_REG(NCR5380_ICOM) | SC_ADTB);
444
445 /*
446 * Load static/volatile values for DRQ interrupt handlers.
447 */
448 pending_5380_data = (volatile u_char *) data;
449 pending_5380_count = len;
450
451 #if DEBUG
452 pdma_5380_state = "wait for interrupt.";
453 #endif
454
455 /*
456 * Set the transfer function to be called on DRQ interrupts.
457 * And note that we're waiting.
458 */
459 pdma_xfer_fun = (pdma_5380_dir == 1) ? pdma_xfer_out : pdma_xfer_in;
460 pdma_5380_pending = 1;
461
462 /*
463 * Initiate the DMA transaction--sending or receiving.
464 */
465 if (pdma_5380_dir == 1) {
466 SET_5380_REG(NCR5380_DMSTAT, 0);
467 } else {
468 SET_5380_REG(NCR5380_IRCV, 0);
469 }
470
471 /*
472 * Now that we're set up, enable interrupts and drop processor
473 * priority back down.
474 */
475 scsi_ienable();
476 splx(s);
477 return 0;
478
479 scsi_timeout_error:
480 /*
481 * Clear the DMA mode.
482 */
483 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
484 return -1;
485 }
486 #endif /* if USE_PDMA */
487
488 /* Include general routines. */
489 #include <atari/dev/ncr5380.c>
490