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