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