sbc.c revision 1.36 1 1.36 jonathan /* $NetBSD: sbc.c,v 1.36 1998/07/04 22:18:27 jonathan Exp $ */
2 1.1 scottr
3 1.1 scottr /*
4 1.19 scottr * Copyright (C) 1996 Scott Reynolds. All rights reserved.
5 1.1 scottr *
6 1.1 scottr * Redistribution and use in source and binary forms, with or without
7 1.1 scottr * modification, are permitted provided that the following conditions
8 1.1 scottr * are met:
9 1.1 scottr * 1. Redistributions of source code must retain the above copyright
10 1.1 scottr * notice, this list of conditions and the following disclaimer.
11 1.1 scottr * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 scottr * notice, this list of conditions and the following disclaimer in the
13 1.1 scottr * documentation and/or other materials provided with the distribution.
14 1.32 scottr * 3. The name of the author may not be used to endorse or promote products
15 1.19 scottr * derived from this software without specific prior written permission
16 1.1 scottr *
17 1.19 scottr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 scottr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 scottr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.19 scottr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 scottr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 scottr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 scottr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 scottr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 scottr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1 scottr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 scottr */
28 1.1 scottr
29 1.1 scottr /*
30 1.1 scottr * This file contains only the machine-dependent parts of the mac68k
31 1.1 scottr * NCR 5380 SCSI driver. (Autoconfig stuff and PDMA functions.)
32 1.1 scottr * The machine-independent parts are in ncr5380sbc.c
33 1.1 scottr *
34 1.1 scottr * Supported hardware includes:
35 1.1 scottr * Macintosh II family 5380-based controller
36 1.1 scottr *
37 1.1 scottr * Credits, history:
38 1.1 scottr *
39 1.1 scottr * Scott Reynolds wrote this module, based on work by Allen Briggs
40 1.9 scottr * (mac68k), Gordon W. Ross and David Jones (sun3), and Leo Weppelman
41 1.9 scottr * (atari). Thanks to Allen for supplying crucial interpretation of the
42 1.9 scottr * NetBSD/mac68k 1.1 'ncrscsi' driver. Also, Allen, Gordon, and Jason
43 1.9 scottr * Thorpe all helped to refine this code, and were considerable sources
44 1.9 scottr * of moral support.
45 1.1 scottr */
46 1.36 jonathan #include "opt_ddb.h"
47 1.1 scottr
48 1.1 scottr #include <sys/types.h>
49 1.1 scottr #include <sys/param.h>
50 1.1 scottr #include <sys/systm.h>
51 1.1 scottr #include <sys/kernel.h>
52 1.1 scottr #include <sys/errno.h>
53 1.1 scottr #include <sys/device.h>
54 1.1 scottr #include <sys/buf.h>
55 1.1 scottr #include <sys/proc.h>
56 1.1 scottr #include <sys/user.h>
57 1.1 scottr
58 1.30 bouyer #include <dev/scsipi/scsi_all.h>
59 1.30 bouyer #include <dev/scsipi/scsipi_all.h>
60 1.30 bouyer #include <dev/scsipi/scsipi_debug.h>
61 1.30 bouyer #include <dev/scsipi/scsiconf.h>
62 1.1 scottr
63 1.1 scottr #include <dev/ic/ncr5380reg.h>
64 1.1 scottr #include <dev/ic/ncr5380var.h>
65 1.1 scottr
66 1.8 scottr #include <machine/cpu.h>
67 1.1 scottr #include <machine/viareg.h>
68 1.1 scottr
69 1.29 scottr #include <mac68k/dev/sbcreg.h>
70 1.29 scottr #include <mac68k/dev/sbcvar.h>
71 1.36 jonathan
72 1.36 jonathan /* SBC_DEBUG -- relies on DDB */
73 1.36 jonathan #ifdef SBC_DEBUG
74 1.36 jonathan # define SBC_DB_INTR 0x01
75 1.36 jonathan # define SBC_DB_DMA 0x02
76 1.36 jonathan # define SBC_DB_REG 0x04
77 1.36 jonathan # define SBC_DB_BREAK 0x08
78 1.36 jonathan # ifndef DDB
79 1.36 jonathan # define Debugger() printf("Debug: sbc.c:%d\n", __LINE__)
80 1.36 jonathan # endif
81 1.36 jonathan # define SBC_BREAK \
82 1.36 jonathan do { if (sbc_debug & SBC_DB_BREAK) Debugger(); } while (0)
83 1.36 jonathan #else
84 1.36 jonathan # define SBC_BREAK
85 1.36 jonathan #endif
86 1.36 jonathan
87 1.1 scottr
88 1.22 scottr int sbc_debug = 0 /* | SBC_DB_INTR | SBC_DB_DMA */;
89 1.22 scottr int sbc_link_flags = 0 /* | SDEV_DB2 */;
90 1.24 scottr int sbc_options = 0 /* | SBC_PDMA */;
91 1.1 scottr
92 1.30 bouyer struct scsipi_adapter sbc_ops = {
93 1.1 scottr ncr5380_scsi_cmd, /* scsi_cmd() */
94 1.35 scottr minphys, /* scsi_minphys() */
95 1.1 scottr NULL, /* open_target_lu() */
96 1.1 scottr NULL, /* close_target_lu() */
97 1.1 scottr };
98 1.1 scottr
99 1.1 scottr /* This is copied from julian's bt driver */
100 1.1 scottr /* "so we have a default dev struct for our link struct." */
101 1.30 bouyer struct scsipi_device sbc_dev = {
102 1.1 scottr NULL, /* Use default error handler. */
103 1.1 scottr NULL, /* Use default start handler. */
104 1.1 scottr NULL, /* Use default async handler. */
105 1.1 scottr NULL, /* Use default "done" routine. */
106 1.1 scottr };
107 1.1 scottr
108 1.31 scottr extern label_t *nofault;
109 1.31 scottr extern caddr_t m68k_fault_addr;
110 1.31 scottr
111 1.28 scottr static int sbc_wait_busy __P((struct ncr5380_softc *));
112 1.22 scottr static int sbc_ready __P((struct ncr5380_softc *));
113 1.28 scottr static int sbc_wait_dreq __P((struct ncr5380_softc *));
114 1.1 scottr
115 1.1 scottr
116 1.1 scottr /***
117 1.1 scottr * General support for Mac-specific SCSI logic.
118 1.1 scottr ***/
119 1.1 scottr
120 1.28 scottr /* These are used in the following inline functions. */
121 1.28 scottr int sbc_wait_busy_timo = 1000 * 5000; /* X2 = 10 S. */
122 1.28 scottr int sbc_ready_timo = 1000 * 5000; /* X2 = 10 S. */
123 1.28 scottr int sbc_wait_dreq_timo = 1000 * 5000; /* X2 = 10 S. */
124 1.28 scottr
125 1.28 scottr /* Return zero on success. */
126 1.28 scottr static __inline__ int
127 1.28 scottr sbc_wait_busy(sc)
128 1.28 scottr struct ncr5380_softc *sc;
129 1.28 scottr {
130 1.28 scottr int timo = sbc_wait_busy_timo;
131 1.28 scottr for (;;) {
132 1.28 scottr if (SCI_BUSY(sc)) {
133 1.28 scottr timo = 0; /* return 0 */
134 1.28 scottr break;
135 1.28 scottr }
136 1.28 scottr if (--timo < 0)
137 1.28 scottr break; /* return -1 */
138 1.28 scottr delay(2);
139 1.28 scottr }
140 1.28 scottr return (timo);
141 1.28 scottr }
142 1.28 scottr
143 1.28 scottr static __inline__ int
144 1.28 scottr sbc_ready(sc)
145 1.28 scottr struct ncr5380_softc *sc;
146 1.28 scottr {
147 1.28 scottr int timo = sbc_ready_timo;
148 1.28 scottr
149 1.28 scottr for (;;) {
150 1.28 scottr if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
151 1.28 scottr == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
152 1.28 scottr timo = 0;
153 1.28 scottr break;
154 1.28 scottr }
155 1.28 scottr if (((*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0)
156 1.28 scottr || (SCI_BUSY(sc) == 0)) {
157 1.28 scottr timo = -1;
158 1.28 scottr break;
159 1.28 scottr }
160 1.28 scottr if (--timo < 0)
161 1.28 scottr break; /* return -1 */
162 1.28 scottr delay(2);
163 1.28 scottr }
164 1.28 scottr return (timo);
165 1.28 scottr }
166 1.28 scottr
167 1.28 scottr static __inline__ int
168 1.28 scottr sbc_wait_dreq(sc)
169 1.28 scottr struct ncr5380_softc *sc;
170 1.28 scottr {
171 1.28 scottr int timo = sbc_wait_dreq_timo;
172 1.28 scottr
173 1.28 scottr for (;;) {
174 1.28 scottr if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
175 1.28 scottr == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
176 1.28 scottr timo = 0;
177 1.28 scottr break;
178 1.28 scottr }
179 1.28 scottr if (--timo < 0)
180 1.28 scottr break; /* return -1 */
181 1.28 scottr delay(2);
182 1.28 scottr }
183 1.28 scottr return (timo);
184 1.28 scottr }
185 1.28 scottr
186 1.1 scottr void
187 1.1 scottr sbc_irq_intr(p)
188 1.1 scottr void *p;
189 1.1 scottr {
190 1.23 scottr struct ncr5380_softc *ncr_sc = p;
191 1.33 scottr struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
192 1.23 scottr int claimed = 0;
193 1.33 scottr extern int cold;
194 1.1 scottr
195 1.1 scottr /* How we ever arrive here without IRQ set is a mystery... */
196 1.1 scottr if (*ncr_sc->sci_csr & SCI_CSR_INT) {
197 1.3 scottr #ifdef SBC_DEBUG
198 1.3 scottr if (sbc_debug & SBC_DB_INTR)
199 1.3 scottr decode_5380_intr(ncr_sc);
200 1.3 scottr #endif
201 1.33 scottr if (!cold)
202 1.33 scottr claimed = ncr5380_intr(ncr_sc);
203 1.1 scottr if (!claimed) {
204 1.1 scottr if (((*ncr_sc->sci_csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT)
205 1.33 scottr && ((*ncr_sc->sci_bus_csr & ~SCI_BUS_RST) == 0)) {
206 1.1 scottr SCI_CLR_INTR(ncr_sc); /* RST interrupt */
207 1.33 scottr if (sc->sc_clrintr)
208 1.33 scottr (*sc->sc_clrintr)(ncr_sc);
209 1.33 scottr }
210 1.1 scottr #ifdef SBC_DEBUG
211 1.1 scottr else {
212 1.13 christos printf("%s: spurious intr\n",
213 1.1 scottr ncr_sc->sc_dev.dv_xname);
214 1.3 scottr SBC_BREAK;
215 1.1 scottr }
216 1.1 scottr #endif
217 1.1 scottr }
218 1.1 scottr }
219 1.1 scottr }
220 1.1 scottr
221 1.3 scottr #ifdef SBC_DEBUG
222 1.3 scottr void
223 1.3 scottr decode_5380_intr(ncr_sc)
224 1.3 scottr struct ncr5380_softc *ncr_sc;
225 1.3 scottr {
226 1.28 scottr u_int8_t csr = *ncr_sc->sci_csr;
227 1.28 scottr u_int8_t bus_csr = *ncr_sc->sci_bus_csr;
228 1.3 scottr
229 1.3 scottr if (((csr & ~(SCI_CSR_PHASE_MATCH | SCI_CSR_ATN)) == SCI_CSR_INT) &&
230 1.3 scottr ((bus_csr & ~(SCI_BUS_MSG | SCI_BUS_CD | SCI_BUS_IO | SCI_BUS_DBP)) == SCI_BUS_SEL)) {
231 1.3 scottr if (csr & SCI_BUS_IO)
232 1.13 christos printf("%s: reselect\n", ncr_sc->sc_dev.dv_xname);
233 1.3 scottr else
234 1.13 christos printf("%s: select\n", ncr_sc->sc_dev.dv_xname);
235 1.3 scottr } else if (((csr & ~SCI_CSR_ACK) == (SCI_CSR_DONE | SCI_CSR_INT)) &&
236 1.3 scottr ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
237 1.13 christos printf("%s: dma eop\n", ncr_sc->sc_dev.dv_xname);
238 1.3 scottr else if (((csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT) &&
239 1.3 scottr ((bus_csr & ~SCI_BUS_RST) == 0))
240 1.13 christos printf("%s: bus reset\n", ncr_sc->sc_dev.dv_xname);
241 1.3 scottr else if (((csr & ~(SCI_CSR_DREQ | SCI_CSR_ATN | SCI_CSR_ACK)) == (SCI_CSR_PERR | SCI_CSR_INT | SCI_CSR_PHASE_MATCH)) &&
242 1.3 scottr ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
243 1.13 christos printf("%s: parity error\n", ncr_sc->sc_dev.dv_xname);
244 1.3 scottr else if (((csr & ~SCI_CSR_ATN) == SCI_CSR_INT) &&
245 1.3 scottr ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_REQ | SCI_BUS_SEL)) == (SCI_BUS_BSY | SCI_BUS_REQ)))
246 1.13 christos printf("%s: phase mismatch\n", ncr_sc->sc_dev.dv_xname);
247 1.3 scottr else if (((csr & ~SCI_CSR_PHASE_MATCH) == (SCI_CSR_INT | SCI_CSR_DISC)) &&
248 1.3 scottr (bus_csr == 0))
249 1.13 christos printf("%s: disconnect\n", ncr_sc->sc_dev.dv_xname);
250 1.3 scottr else
251 1.13 christos printf("%s: unknown intr: csr=%x, bus_csr=%x\n",
252 1.3 scottr ncr_sc->sc_dev.dv_xname, csr, bus_csr);
253 1.3 scottr }
254 1.3 scottr #endif
255 1.1 scottr
256 1.8 scottr
257 1.1 scottr /***
258 1.1 scottr * The following code implements polled PDMA.
259 1.1 scottr ***/
260 1.1 scottr
261 1.23 scottr int
262 1.23 scottr sbc_pdma_in(ncr_sc, phase, datalen, data)
263 1.23 scottr struct ncr5380_softc *ncr_sc;
264 1.28 scottr int phase;
265 1.28 scottr int datalen;
266 1.23 scottr u_char *data;
267 1.23 scottr {
268 1.23 scottr struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
269 1.24 scottr volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
270 1.24 scottr volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
271 1.23 scottr int resid, s;
272 1.23 scottr
273 1.28 scottr if (datalen < ncr_sc->sc_min_dma_len ||
274 1.28 scottr (sc->sc_options & SBC_PDMA) == 0)
275 1.28 scottr return ncr5380_pio_in(ncr_sc, phase, datalen, data);
276 1.28 scottr
277 1.23 scottr s = splbio();
278 1.28 scottr if (sbc_wait_busy(ncr_sc)) {
279 1.28 scottr splx(s);
280 1.28 scottr return 0;
281 1.28 scottr }
282 1.28 scottr
283 1.23 scottr *ncr_sc->sci_mode |= SCI_MODE_DMA;
284 1.23 scottr *ncr_sc->sci_irecv = 0;
285 1.1 scottr
286 1.28 scottr #define R4 *((u_int32_t *)data)++ = *long_data
287 1.28 scottr #define R1 *((u_int8_t *)data)++ = *byte_data
288 1.23 scottr for (resid = datalen; resid >= 128; resid -= 128) {
289 1.28 scottr if (sbc_ready(ncr_sc))
290 1.23 scottr goto interrupt;
291 1.23 scottr R4; R4; R4; R4; R4; R4; R4; R4;
292 1.23 scottr R4; R4; R4; R4; R4; R4; R4; R4;
293 1.23 scottr R4; R4; R4; R4; R4; R4; R4; R4;
294 1.28 scottr R4; R4; R4; R4; R4; R4; R4; R4; /* 128 */
295 1.23 scottr }
296 1.23 scottr while (resid) {
297 1.28 scottr if (sbc_ready(ncr_sc))
298 1.23 scottr goto interrupt;
299 1.23 scottr R1;
300 1.23 scottr resid--;
301 1.1 scottr }
302 1.23 scottr #undef R4
303 1.23 scottr #undef R1
304 1.1 scottr
305 1.23 scottr interrupt:
306 1.1 scottr SCI_CLR_INTR(ncr_sc);
307 1.1 scottr *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
308 1.28 scottr *ncr_sc->sci_icmd = 0;
309 1.23 scottr splx(s);
310 1.28 scottr return (datalen - resid);
311 1.1 scottr }
312 1.1 scottr
313 1.22 scottr int
314 1.23 scottr sbc_pdma_out(ncr_sc, phase, datalen, data)
315 1.1 scottr struct ncr5380_softc *ncr_sc;
316 1.28 scottr int phase;
317 1.28 scottr int datalen;
318 1.1 scottr u_char *data;
319 1.1 scottr {
320 1.1 scottr struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
321 1.24 scottr volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
322 1.24 scottr volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
323 1.31 scottr label_t faultbuf;
324 1.28 scottr int resid, s;
325 1.28 scottr u_int8_t icmd;
326 1.23 scottr
327 1.31 scottr #if 1
328 1.31 scottr /* Work around lame gcc initialization bug */
329 1.31 scottr (void)&data;
330 1.31 scottr #endif
331 1.31 scottr
332 1.28 scottr if (datalen < ncr_sc->sc_min_dma_len ||
333 1.28 scottr (sc->sc_options & SBC_PDMA) == 0)
334 1.24 scottr return ncr5380_pio_out(ncr_sc, phase, datalen, data);
335 1.24 scottr
336 1.23 scottr s = splbio();
337 1.28 scottr if (sbc_wait_busy(ncr_sc)) {
338 1.28 scottr splx(s);
339 1.28 scottr return 0;
340 1.28 scottr }
341 1.28 scottr
342 1.23 scottr icmd = *(ncr_sc->sci_icmd) & SCI_ICMD_RMASK;
343 1.23 scottr *ncr_sc->sci_icmd = icmd | SCI_ICMD_DATA;
344 1.23 scottr *ncr_sc->sci_mode |= SCI_MODE_DMA;
345 1.23 scottr *ncr_sc->sci_dma_send = 0;
346 1.23 scottr
347 1.31 scottr /*
348 1.31 scottr * Setup for a possible bus error caused by SCSI controller
349 1.31 scottr * switching out of DATA OUT before we're done with the
350 1.31 scottr * current transfer. (See comment before sbc_drq_intr().)
351 1.31 scottr */
352 1.31 scottr nofault = &faultbuf;
353 1.31 scottr
354 1.31 scottr if (setjmp(nofault)) {
355 1.31 scottr printf("buf = 0x%lx, fault = 0x%lx\n",
356 1.31 scottr (u_long)sc->sc_drq_addr, (u_long)m68k_fault_addr);
357 1.31 scottr panic("Unexpected bus error in sbc_pdma_out()");
358 1.31 scottr }
359 1.31 scottr
360 1.28 scottr #define W1 *byte_data = *((u_int8_t *)data)++
361 1.28 scottr #define W4 *long_data = *((u_int32_t *)data)++
362 1.28 scottr for (resid = datalen; resid >= 64; resid -= 64) {
363 1.28 scottr if (sbc_ready(ncr_sc))
364 1.23 scottr goto interrupt;
365 1.23 scottr W1;
366 1.28 scottr if (sbc_ready(ncr_sc))
367 1.23 scottr goto interrupt;
368 1.23 scottr W1;
369 1.28 scottr if (sbc_ready(ncr_sc))
370 1.23 scottr goto interrupt;
371 1.23 scottr W1;
372 1.28 scottr if (sbc_ready(ncr_sc))
373 1.23 scottr goto interrupt;
374 1.23 scottr W1;
375 1.28 scottr if (sbc_ready(ncr_sc))
376 1.23 scottr goto interrupt;
377 1.23 scottr W4; W4; W4; W4;
378 1.23 scottr W4; W4; W4; W4;
379 1.23 scottr W4; W4; W4; W4;
380 1.23 scottr W4; W4; W4;
381 1.23 scottr }
382 1.28 scottr while (resid) {
383 1.28 scottr if (sbc_ready(ncr_sc))
384 1.23 scottr goto interrupt;
385 1.23 scottr W1;
386 1.28 scottr resid--;
387 1.23 scottr }
388 1.23 scottr #undef W1
389 1.23 scottr #undef W4
390 1.28 scottr if (sbc_wait_dreq(ncr_sc))
391 1.28 scottr printf("%s: timeout waiting for DREQ.\n",
392 1.28 scottr ncr_sc->sc_dev.dv_xname);
393 1.28 scottr
394 1.28 scottr *byte_data = 0;
395 1.28 scottr goto done;
396 1.1 scottr
397 1.28 scottr interrupt:
398 1.28 scottr if ((*ncr_sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0) {
399 1.28 scottr *ncr_sc->sci_icmd = icmd & ~SCI_ICMD_DATA;
400 1.28 scottr --resid;
401 1.1 scottr }
402 1.1 scottr
403 1.28 scottr done:
404 1.1 scottr SCI_CLR_INTR(ncr_sc);
405 1.1 scottr *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
406 1.23 scottr *ncr_sc->sci_icmd = icmd;
407 1.23 scottr splx(s);
408 1.23 scottr return (datalen - resid);
409 1.1 scottr }
410 1.1 scottr
411 1.1 scottr
412 1.1 scottr /***
413 1.1 scottr * The following code implements interrupt-driven PDMA.
414 1.1 scottr ***/
415 1.1 scottr
416 1.1 scottr /*
417 1.1 scottr * This is the meat of the PDMA transfer.
418 1.1 scottr * When we get here, we shove data as fast as the mac can take it.
419 1.1 scottr * We depend on several things:
420 1.1 scottr * * All macs after the Mac Plus that have a 5380 chip should have a general
421 1.1 scottr * logic IC that handshakes data for blind transfers.
422 1.1 scottr * * If the SCSI controller finishes sending/receiving data before we do,
423 1.1 scottr * the same general logic IC will generate a /BERR for us in short order.
424 1.1 scottr * * The fault address for said /BERR minus the base address for the
425 1.1 scottr * transfer will be the amount of data that was actually written.
426 1.1 scottr *
427 1.1 scottr * We use the nofault flag and the setjmp/longjmp in locore.s so we can
428 1.1 scottr * detect and handle the bus error for early termination of a command.
429 1.1 scottr * This is usually caused by a disconnecting target.
430 1.1 scottr */
431 1.1 scottr void
432 1.1 scottr sbc_drq_intr(p)
433 1.1 scottr void *p;
434 1.1 scottr {
435 1.24 scottr struct sbc_softc *sc = (struct sbc_softc *)p;
436 1.24 scottr struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)p;
437 1.23 scottr struct sci_req *sr = ncr_sc->sc_current;
438 1.23 scottr struct sbc_pdma_handle *dh = sr->sr_dma_hand;
439 1.23 scottr label_t faultbuf;
440 1.23 scottr volatile u_int32_t *long_drq;
441 1.23 scottr u_int32_t *long_data;
442 1.23 scottr volatile u_int8_t *drq;
443 1.23 scottr u_int8_t *data;
444 1.23 scottr int count, dcount, resid;
445 1.23 scottr u_int8_t tmp;
446 1.26 scottr
447 1.26 scottr /* Work around lame gcc initialization bug */
448 1.26 scottr (void)&drq;
449 1.1 scottr
450 1.1 scottr /*
451 1.1 scottr * If we're not ready to xfer data, or have no more, just return.
452 1.1 scottr */
453 1.3 scottr if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0 || dh->dh_len == 0)
454 1.1 scottr return;
455 1.1 scottr
456 1.1 scottr #ifdef SBC_DEBUG
457 1.1 scottr if (sbc_debug & SBC_DB_INTR)
458 1.13 christos printf("%s: drq intr, dh_len=0x%x, dh_flags=0x%x\n",
459 1.1 scottr ncr_sc->sc_dev.dv_xname, dh->dh_len, dh->dh_flags);
460 1.1 scottr #endif
461 1.1 scottr
462 1.1 scottr /*
463 1.1 scottr * Setup for a possible bus error caused by SCSI controller
464 1.1 scottr * switching out of DATA-IN/OUT before we're done with the
465 1.1 scottr * current transfer.
466 1.1 scottr */
467 1.31 scottr nofault = &faultbuf;
468 1.1 scottr
469 1.33 scottr if (setjmp(nofault)) {
470 1.31 scottr nofault = (label_t *)0;
471 1.8 scottr if ((dh->dh_flags & SBC_DH_DONE) == 0) {
472 1.27 scottr count = (( (u_long)m68k_fault_addr
473 1.24 scottr - (u_long)sc->sc_drq_addr));
474 1.8 scottr
475 1.8 scottr if ((count < 0) || (count > dh->dh_len)) {
476 1.13 christos printf("%s: complete=0x%x (pending 0x%x)\n",
477 1.8 scottr ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
478 1.8 scottr panic("something is wrong");
479 1.8 scottr }
480 1.1 scottr
481 1.8 scottr dh->dh_addr += count;
482 1.8 scottr dh->dh_len -= count;
483 1.18 scottr } else
484 1.18 scottr count = 0;
485 1.8 scottr
486 1.1 scottr #ifdef SBC_DEBUG
487 1.1 scottr if (sbc_debug & SBC_DB_INTR)
488 1.13 christos printf("%s: drq /berr, complete=0x%x (pending 0x%x)\n",
489 1.8 scottr ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
490 1.1 scottr #endif
491 1.27 scottr m68k_fault_addr = 0;
492 1.3 scottr
493 1.1 scottr return;
494 1.1 scottr }
495 1.1 scottr
496 1.1 scottr if (dh->dh_flags & SBC_DH_OUT) { /* Data Out */
497 1.26 scottr dcount = 0;
498 1.26 scottr
499 1.1 scottr /*
500 1.1 scottr * Get the source address aligned.
501 1.1 scottr */
502 1.6 scottr resid =
503 1.24 scottr count = min(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
504 1.1 scottr if (count && count < 4) {
505 1.24 scottr drq = (volatile u_int8_t *)sc->sc_drq_addr;
506 1.24 scottr data = (u_int8_t *)dh->dh_addr;
507 1.8 scottr
508 1.1 scottr #define W1 *drq++ = *data++
509 1.1 scottr while (count) {
510 1.1 scottr W1; count--;
511 1.1 scottr }
512 1.1 scottr #undef W1
513 1.1 scottr dh->dh_addr += resid;
514 1.1 scottr dh->dh_len -= resid;
515 1.1 scottr }
516 1.1 scottr
517 1.1 scottr /*
518 1.8 scottr * Start the transfer.
519 1.1 scottr */
520 1.1 scottr while (dh->dh_len) {
521 1.1 scottr dcount = count = min(dh->dh_len, MAX_DMA_LEN);
522 1.24 scottr long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
523 1.24 scottr long_data = (u_int32_t *)dh->dh_addr;
524 1.1 scottr
525 1.1 scottr #define W4 *long_drq++ = *long_data++
526 1.1 scottr while (count >= 64) {
527 1.1 scottr W4; W4; W4; W4; W4; W4; W4; W4;
528 1.1 scottr W4; W4; W4; W4; W4; W4; W4; W4; /* 64 */
529 1.1 scottr count -= 64;
530 1.1 scottr }
531 1.1 scottr while (count >= 4) {
532 1.1 scottr W4; count -= 4;
533 1.1 scottr }
534 1.1 scottr #undef W4
535 1.24 scottr data = (u_int8_t *)long_data;
536 1.24 scottr drq = (u_int8_t *)long_drq;
537 1.8 scottr
538 1.7 scottr #define W1 *drq++ = *data++
539 1.7 scottr while (count) {
540 1.7 scottr W1; count--;
541 1.7 scottr }
542 1.7 scottr #undef W1
543 1.7 scottr dh->dh_len -= dcount;
544 1.7 scottr dh->dh_addr += dcount;
545 1.7 scottr }
546 1.8 scottr dh->dh_flags |= SBC_DH_DONE;
547 1.7 scottr
548 1.7 scottr /*
549 1.8 scottr * XXX -- Read a byte from the SBC to trigger a /BERR.
550 1.8 scottr * This seems to be necessary for us to notice that
551 1.8 scottr * the target has disconnected. Ick. 06 jun 1996 (sr)
552 1.7 scottr */
553 1.26 scottr if (dcount >= MAX_DMA_LEN)
554 1.24 scottr drq = (volatile u_int8_t *)sc->sc_drq_addr;
555 1.8 scottr tmp = *drq;
556 1.1 scottr } else { /* Data In */
557 1.1 scottr /*
558 1.1 scottr * Get the dest address aligned.
559 1.1 scottr */
560 1.6 scottr resid =
561 1.24 scottr count = min(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
562 1.1 scottr if (count && count < 4) {
563 1.24 scottr data = (u_int8_t *)dh->dh_addr;
564 1.24 scottr drq = (volatile u_int8_t *)sc->sc_drq_addr;
565 1.8 scottr
566 1.1 scottr #define R1 *data++ = *drq++
567 1.1 scottr while (count) {
568 1.1 scottr R1; count--;
569 1.1 scottr }
570 1.1 scottr #undef R1
571 1.1 scottr dh->dh_addr += resid;
572 1.1 scottr dh->dh_len -= resid;
573 1.1 scottr }
574 1.1 scottr
575 1.1 scottr /*
576 1.8 scottr * Start the transfer.
577 1.1 scottr */
578 1.1 scottr while (dh->dh_len) {
579 1.1 scottr dcount = count = min(dh->dh_len, MAX_DMA_LEN);
580 1.24 scottr long_data = (u_int32_t *)dh->dh_addr;
581 1.24 scottr long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
582 1.1 scottr
583 1.1 scottr #define R4 *long_data++ = *long_drq++
584 1.8 scottr while (count >= 64) {
585 1.1 scottr R4; R4; R4; R4; R4; R4; R4; R4;
586 1.1 scottr R4; R4; R4; R4; R4; R4; R4; R4; /* 64 */
587 1.8 scottr count -= 64;
588 1.1 scottr }
589 1.1 scottr while (count >= 4) {
590 1.1 scottr R4; count -= 4;
591 1.1 scottr }
592 1.1 scottr #undef R4
593 1.24 scottr data = (u_int8_t *)long_data;
594 1.24 scottr drq = (volatile u_int8_t *)long_drq;
595 1.8 scottr
596 1.1 scottr #define R1 *data++ = *drq++
597 1.1 scottr while (count) {
598 1.1 scottr R1; count--;
599 1.1 scottr }
600 1.1 scottr #undef R1
601 1.1 scottr dh->dh_len -= dcount;
602 1.1 scottr dh->dh_addr += dcount;
603 1.1 scottr }
604 1.8 scottr dh->dh_flags |= SBC_DH_DONE;
605 1.1 scottr }
606 1.1 scottr
607 1.1 scottr /*
608 1.1 scottr * OK. No bus error occurred above. Clear the nofault flag
609 1.1 scottr * so we no longer short-circuit bus errors.
610 1.1 scottr */
611 1.31 scottr nofault = (label_t *)0;
612 1.7 scottr
613 1.7 scottr #ifdef SBC_DEBUG
614 1.7 scottr if (sbc_debug & (SBC_DB_REG | SBC_DB_INTR))
615 1.13 christos printf("%s: drq intr complete: csr=0x%x, bus_csr=0x%x\n",
616 1.7 scottr ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
617 1.7 scottr *ncr_sc->sci_bus_csr);
618 1.7 scottr #endif
619 1.1 scottr }
620 1.1 scottr
621 1.1 scottr void
622 1.1 scottr sbc_dma_alloc(ncr_sc)
623 1.1 scottr struct ncr5380_softc *ncr_sc;
624 1.1 scottr {
625 1.24 scottr struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
626 1.1 scottr struct sci_req *sr = ncr_sc->sc_current;
627 1.30 bouyer struct scsipi_xfer *xs = sr->sr_xs;
628 1.1 scottr struct sbc_pdma_handle *dh;
629 1.1 scottr int i, xlen;
630 1.1 scottr
631 1.6 scottr #ifdef DIAGNOSTIC
632 1.1 scottr if (sr->sr_dma_hand != NULL)
633 1.1 scottr panic("sbc_dma_alloc: already have PDMA handle");
634 1.1 scottr #endif
635 1.1 scottr
636 1.1 scottr /* Polled transfers shouldn't allocate a PDMA handle. */
637 1.1 scottr if (sr->sr_flags & SR_IMMED)
638 1.1 scottr return;
639 1.1 scottr
640 1.1 scottr xlen = ncr_sc->sc_datalen;
641 1.1 scottr
642 1.1 scottr /* Make sure our caller checked sc_min_dma_len. */
643 1.1 scottr if (xlen < MIN_DMA_LEN)
644 1.1 scottr panic("sbc_dma_alloc: len=0x%x\n", xlen);
645 1.1 scottr
646 1.1 scottr /*
647 1.1 scottr * Find free PDMA handle. Guaranteed to find one since we
648 1.1 scottr * have as many PDMA handles as the driver has processes.
649 1.1 scottr * (instances?)
650 1.1 scottr */
651 1.1 scottr for (i = 0; i < SCI_OPENINGS; i++) {
652 1.1 scottr if ((sc->sc_pdma[i].dh_flags & SBC_DH_BUSY) == 0)
653 1.1 scottr goto found;
654 1.1 scottr }
655 1.1 scottr panic("sbc: no free PDMA handles");
656 1.1 scottr found:
657 1.1 scottr dh = &sc->sc_pdma[i];
658 1.1 scottr dh->dh_flags = SBC_DH_BUSY;
659 1.1 scottr dh->dh_addr = ncr_sc->sc_dataptr;
660 1.1 scottr dh->dh_len = xlen;
661 1.1 scottr
662 1.1 scottr /* Copy the 'write' flag for convenience. */
663 1.1 scottr if (xs->flags & SCSI_DATA_OUT)
664 1.1 scottr dh->dh_flags |= SBC_DH_OUT;
665 1.1 scottr
666 1.1 scottr sr->sr_dma_hand = dh;
667 1.1 scottr }
668 1.1 scottr
669 1.1 scottr void
670 1.1 scottr sbc_dma_free(ncr_sc)
671 1.1 scottr struct ncr5380_softc *ncr_sc;
672 1.1 scottr {
673 1.1 scottr struct sci_req *sr = ncr_sc->sc_current;
674 1.1 scottr struct sbc_pdma_handle *dh = sr->sr_dma_hand;
675 1.1 scottr
676 1.6 scottr #ifdef DIAGNOSTIC
677 1.1 scottr if (sr->sr_dma_hand == NULL)
678 1.1 scottr panic("sbc_dma_free: no DMA handle");
679 1.1 scottr #endif
680 1.1 scottr
681 1.1 scottr if (ncr_sc->sc_state & NCR_DOINGDMA)
682 1.1 scottr panic("sbc_dma_free: free while in progress");
683 1.1 scottr
684 1.1 scottr if (dh->dh_flags & SBC_DH_BUSY) {
685 1.1 scottr dh->dh_flags = 0;
686 1.1 scottr dh->dh_addr = NULL;
687 1.1 scottr dh->dh_len = 0;
688 1.1 scottr }
689 1.1 scottr sr->sr_dma_hand = NULL;
690 1.1 scottr }
691 1.1 scottr
692 1.1 scottr void
693 1.1 scottr sbc_dma_poll(ncr_sc)
694 1.1 scottr struct ncr5380_softc *ncr_sc;
695 1.1 scottr {
696 1.1 scottr struct sci_req *sr = ncr_sc->sc_current;
697 1.1 scottr
698 1.3 scottr /*
699 1.3 scottr * We shouldn't arrive here; if SR_IMMED is set, then
700 1.3 scottr * dma_alloc() should have refused to allocate a handle
701 1.3 scottr * for the transfer. This forces the polled PDMA code
702 1.3 scottr * to handle the request...
703 1.3 scottr */
704 1.6 scottr #ifdef SBC_DEBUG
705 1.1 scottr if (sbc_debug & SBC_DB_DMA)
706 1.13 christos printf("%s: lost DRQ interrupt?\n", ncr_sc->sc_dev.dv_xname);
707 1.1 scottr #endif
708 1.3 scottr sr->sr_flags |= SR_OVERDUE;
709 1.1 scottr }
710 1.1 scottr
711 1.1 scottr void
712 1.1 scottr sbc_dma_setup(ncr_sc)
713 1.1 scottr struct ncr5380_softc *ncr_sc;
714 1.1 scottr {
715 1.1 scottr /* Not needed; we don't have real DMA */
716 1.1 scottr }
717 1.1 scottr
718 1.1 scottr void
719 1.1 scottr sbc_dma_start(ncr_sc)
720 1.1 scottr struct ncr5380_softc *ncr_sc;
721 1.1 scottr {
722 1.24 scottr struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
723 1.1 scottr struct sci_req *sr = ncr_sc->sc_current;
724 1.1 scottr struct sbc_pdma_handle *dh = sr->sr_dma_hand;
725 1.1 scottr
726 1.1 scottr /*
727 1.7 scottr * Match bus phase, clear pending interrupts, set DMA mode, and
728 1.7 scottr * assert data bus (for writing only), then start the transfer.
729 1.1 scottr */
730 1.1 scottr if (dh->dh_flags & SBC_DH_OUT) {
731 1.1 scottr *ncr_sc->sci_tcmd = PHASE_DATA_OUT;
732 1.1 scottr SCI_CLR_INTR(ncr_sc);
733 1.22 scottr if (sc->sc_clrintr)
734 1.22 scottr (*sc->sc_clrintr)(ncr_sc);
735 1.1 scottr *ncr_sc->sci_mode |= SCI_MODE_DMA;
736 1.1 scottr *ncr_sc->sci_icmd = SCI_ICMD_DATA;
737 1.1 scottr *ncr_sc->sci_dma_send = 0;
738 1.1 scottr } else {
739 1.1 scottr *ncr_sc->sci_tcmd = PHASE_DATA_IN;
740 1.1 scottr SCI_CLR_INTR(ncr_sc);
741 1.22 scottr if (sc->sc_clrintr)
742 1.22 scottr (*sc->sc_clrintr)(ncr_sc);
743 1.1 scottr *ncr_sc->sci_mode |= SCI_MODE_DMA;
744 1.1 scottr *ncr_sc->sci_icmd = 0;
745 1.1 scottr *ncr_sc->sci_irecv = 0;
746 1.1 scottr }
747 1.3 scottr ncr_sc->sc_state |= NCR_DOINGDMA;
748 1.1 scottr
749 1.6 scottr #ifdef SBC_DEBUG
750 1.1 scottr if (sbc_debug & SBC_DB_DMA)
751 1.13 christos printf("%s: PDMA started, va=%p, len=0x%x\n",
752 1.1 scottr ncr_sc->sc_dev.dv_xname, dh->dh_addr, dh->dh_len);
753 1.1 scottr #endif
754 1.1 scottr }
755 1.1 scottr
756 1.1 scottr void
757 1.1 scottr sbc_dma_eop(ncr_sc)
758 1.1 scottr struct ncr5380_softc *ncr_sc;
759 1.1 scottr {
760 1.1 scottr /* Not used; the EOP pin is wired high (GMFH, pp. 389-390) */
761 1.1 scottr }
762 1.1 scottr
763 1.1 scottr void
764 1.1 scottr sbc_dma_stop(ncr_sc)
765 1.1 scottr struct ncr5380_softc *ncr_sc;
766 1.1 scottr {
767 1.24 scottr struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
768 1.1 scottr struct sci_req *sr = ncr_sc->sc_current;
769 1.1 scottr struct sbc_pdma_handle *dh = sr->sr_dma_hand;
770 1.23 scottr int ntrans;
771 1.1 scottr
772 1.1 scottr if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
773 1.1 scottr #ifdef SBC_DEBUG
774 1.1 scottr if (sbc_debug & SBC_DB_DMA)
775 1.13 christos printf("%s: dma_stop: DMA not running\n",
776 1.1 scottr ncr_sc->sc_dev.dv_xname);
777 1.1 scottr #endif
778 1.1 scottr return;
779 1.1 scottr }
780 1.1 scottr ncr_sc->sc_state &= ~NCR_DOINGDMA;
781 1.1 scottr
782 1.3 scottr if ((ncr_sc->sc_state & NCR_ABORTING) == 0) {
783 1.1 scottr ntrans = ncr_sc->sc_datalen - dh->dh_len;
784 1.1 scottr
785 1.1 scottr #ifdef SBC_DEBUG
786 1.1 scottr if (sbc_debug & SBC_DB_DMA)
787 1.13 christos printf("%s: dma_stop: ntrans=0x%x\n",
788 1.1 scottr ncr_sc->sc_dev.dv_xname, ntrans);
789 1.1 scottr #endif
790 1.1 scottr
791 1.1 scottr if (ntrans > ncr_sc->sc_datalen)
792 1.1 scottr panic("sbc_dma_stop: excess transfer\n");
793 1.1 scottr
794 1.1 scottr /* Adjust data pointer */
795 1.1 scottr ncr_sc->sc_dataptr += ntrans;
796 1.1 scottr ncr_sc->sc_datalen -= ntrans;
797 1.1 scottr
798 1.1 scottr /* Clear any pending interrupts. */
799 1.1 scottr SCI_CLR_INTR(ncr_sc);
800 1.22 scottr if (sc->sc_clrintr)
801 1.22 scottr (*sc->sc_clrintr)(ncr_sc);
802 1.1 scottr }
803 1.1 scottr
804 1.1 scottr /* Put SBIC back into PIO mode. */
805 1.1 scottr *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
806 1.1 scottr *ncr_sc->sci_icmd = 0;
807 1.1 scottr
808 1.1 scottr #ifdef SBC_DEBUG
809 1.3 scottr if (sbc_debug & SBC_DB_REG)
810 1.13 christos printf("%s: dma_stop: csr=0x%x, bus_csr=0x%x\n",
811 1.1 scottr ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
812 1.1 scottr *ncr_sc->sci_bus_csr);
813 1.1 scottr #endif
814 1.1 scottr }
815