esp.c revision 1.28 1 1.28 briggs /* $NetBSD: esp.c,v 1.28 2001/01/18 03:43:18 briggs Exp $ */
2 1.1 briggs
3 1.1 briggs /*
4 1.10 briggs * Copyright (c) 1997 Jason R. Thorpe.
5 1.10 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.10 briggs * This product includes software developed for the NetBSD Project
18 1.10 briggs * by Jason R. Thorpe.
19 1.1 briggs * 4. The name of the author may not be used to endorse or promote products
20 1.1 briggs * derived from this software without specific prior written permission.
21 1.1 briggs *
22 1.1 briggs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 briggs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 briggs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 briggs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 briggs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 briggs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 briggs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 briggs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 briggs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 briggs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 briggs */
33 1.1 briggs
34 1.1 briggs /*
35 1.1 briggs * Copyright (c) 1994 Peter Galbavy
36 1.1 briggs * All rights reserved.
37 1.1 briggs *
38 1.1 briggs * Redistribution and use in source and binary forms, with or without
39 1.1 briggs * modification, are permitted provided that the following conditions
40 1.1 briggs * are met:
41 1.1 briggs * 1. Redistributions of source code must retain the above copyright
42 1.1 briggs * notice, this list of conditions and the following disclaimer.
43 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
44 1.1 briggs * notice, this list of conditions and the following disclaimer in the
45 1.1 briggs * documentation and/or other materials provided with the distribution.
46 1.1 briggs * 3. All advertising materials mentioning features or use of this software
47 1.1 briggs * must display the following acknowledgement:
48 1.1 briggs * This product includes software developed by Peter Galbavy
49 1.1 briggs * 4. The name of the author may not be used to endorse or promote products
50 1.1 briggs * derived from this software without specific prior written permission.
51 1.1 briggs *
52 1.1 briggs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 1.1 briggs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 1.1 briggs * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 1.1 briggs * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
56 1.1 briggs * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57 1.1 briggs * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
58 1.1 briggs * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 briggs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
60 1.1 briggs * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
61 1.1 briggs * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 1.1 briggs * POSSIBILITY OF SUCH DAMAGE.
63 1.1 briggs */
64 1.1 briggs
65 1.1 briggs /*
66 1.1 briggs * Based on aic6360 by Jarle Greipsland
67 1.1 briggs *
68 1.1 briggs * Acknowledgements: Many of the algorithms used in this driver are
69 1.1 briggs * inspired by the work of Julian Elischer (julian (at) tfs.com) and
70 1.1 briggs * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu). Thanks a million!
71 1.10 briggs */
72 1.10 briggs
73 1.10 briggs /*
74 1.10 briggs * Initial m68k mac support from Allen Briggs <briggs (at) macbsd.com>
75 1.10 briggs * (basically consisting of the match, a bit of the attach, and the
76 1.10 briggs * "DMA" glue functions).
77 1.1 briggs */
78 1.1 briggs
79 1.1 briggs #include <sys/types.h>
80 1.1 briggs #include <sys/param.h>
81 1.1 briggs #include <sys/systm.h>
82 1.1 briggs #include <sys/kernel.h>
83 1.1 briggs #include <sys/errno.h>
84 1.1 briggs #include <sys/ioctl.h>
85 1.1 briggs #include <sys/device.h>
86 1.1 briggs #include <sys/buf.h>
87 1.1 briggs #include <sys/proc.h>
88 1.1 briggs #include <sys/user.h>
89 1.1 briggs #include <sys/queue.h>
90 1.1 briggs
91 1.11 bouyer #include <dev/scsipi/scsi_all.h>
92 1.11 bouyer #include <dev/scsipi/scsipi_all.h>
93 1.11 bouyer #include <dev/scsipi/scsiconf.h>
94 1.11 bouyer #include <dev/scsipi/scsi_message.h>
95 1.1 briggs
96 1.1 briggs #include <machine/cpu.h>
97 1.12 briggs #include <machine/bus.h>
98 1.1 briggs #include <machine/param.h>
99 1.1 briggs
100 1.7 briggs #include <dev/ic/ncr53c9xreg.h>
101 1.7 briggs #include <dev/ic/ncr53c9xvar.h>
102 1.7 briggs
103 1.1 briggs #include <machine/viareg.h>
104 1.1 briggs
105 1.15 scottr #include <mac68k/obio/espvar.h>
106 1.15 scottr #include <mac68k/obio/obiovar.h>
107 1.3 briggs
108 1.7 briggs void espattach __P((struct device *, struct device *, void *));
109 1.9 scottr int espmatch __P((struct device *, struct cfdata *, void *));
110 1.1 briggs
111 1.1 briggs /* Linkup to the rest of the kernel */
112 1.1 briggs struct cfattach esp_ca = {
113 1.1 briggs sizeof(struct esp_softc), espmatch, espattach
114 1.1 briggs };
115 1.1 briggs
116 1.7 briggs /*
117 1.7 briggs * Functions and the switch for the MI code.
118 1.7 briggs */
119 1.7 briggs u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
120 1.7 briggs void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
121 1.7 briggs int esp_dma_isintr __P((struct ncr53c9x_softc *));
122 1.7 briggs void esp_dma_reset __P((struct ncr53c9x_softc *));
123 1.7 briggs int esp_dma_intr __P((struct ncr53c9x_softc *));
124 1.7 briggs int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
125 1.7 briggs size_t *, int, size_t *));
126 1.7 briggs void esp_dma_go __P((struct ncr53c9x_softc *));
127 1.7 briggs void esp_dma_stop __P((struct ncr53c9x_softc *));
128 1.7 briggs int esp_dma_isactive __P((struct ncr53c9x_softc *));
129 1.12 briggs void esp_quick_write_reg __P((struct ncr53c9x_softc *, int, u_char));
130 1.12 briggs int esp_quick_dma_intr __P((struct ncr53c9x_softc *));
131 1.12 briggs int esp_quick_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
132 1.12 briggs size_t *, int, size_t *));
133 1.12 briggs void esp_quick_dma_go __P((struct ncr53c9x_softc *));
134 1.12 briggs
135 1.23 briggs void esp_intr __P((void *sc));
136 1.23 briggs void esp_dualbus_intr __P((void *sc));
137 1.16 briggs static struct esp_softc *esp0 = NULL, *esp1 = NULL;
138 1.16 briggs
139 1.12 briggs static __inline__ int esp_dafb_have_dreq __P((struct esp_softc *esc));
140 1.12 briggs static __inline__ int esp_iosb_have_dreq __P((struct esp_softc *esc));
141 1.12 briggs int (*esp_have_dreq) __P((struct esp_softc *esc));
142 1.7 briggs
143 1.7 briggs struct ncr53c9x_glue esp_glue = {
144 1.7 briggs esp_read_reg,
145 1.7 briggs esp_write_reg,
146 1.7 briggs esp_dma_isintr,
147 1.7 briggs esp_dma_reset,
148 1.7 briggs esp_dma_intr,
149 1.7 briggs esp_dma_setup,
150 1.7 briggs esp_dma_go,
151 1.7 briggs esp_dma_stop,
152 1.7 briggs esp_dma_isactive,
153 1.7 briggs NULL, /* gl_clear_latched_intr */
154 1.7 briggs };
155 1.7 briggs
156 1.1 briggs int
157 1.9 scottr espmatch(parent, cf, aux)
158 1.1 briggs struct device *parent;
159 1.6 scottr struct cfdata *cf;
160 1.6 scottr void *aux;
161 1.1 briggs {
162 1.12 briggs int found = 0;
163 1.12 briggs
164 1.12 briggs if ((cf->cf_unit == 0) && mac68k_machine.scsi96) {
165 1.12 briggs found = 1;
166 1.12 briggs }
167 1.12 briggs if ((cf->cf_unit == 1) && mac68k_machine.scsi96_2) {
168 1.12 briggs found = 1;
169 1.12 briggs }
170 1.12 briggs
171 1.12 briggs return found;
172 1.1 briggs }
173 1.1 briggs
174 1.1 briggs /*
175 1.1 briggs * Attach this instance, and then all the sub-devices
176 1.1 briggs */
177 1.1 briggs void
178 1.1 briggs espattach(parent, self, aux)
179 1.1 briggs struct device *parent, *self;
180 1.1 briggs void *aux;
181 1.1 briggs {
182 1.12 briggs struct obio_attach_args *oa = (struct obio_attach_args *)aux;
183 1.20 scottr extern vaddr_t SCSIBase;
184 1.12 briggs struct esp_softc *esc = (void *)self;
185 1.12 briggs struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
186 1.12 briggs int quick = 0;
187 1.12 briggs unsigned long reg_offset;
188 1.12 briggs
189 1.12 briggs reg_offset = SCSIBase - IOBase;
190 1.12 briggs esc->sc_tag = oa->oa_tag;
191 1.12 briggs /*
192 1.12 briggs * For Wombat, Primus and Optimus motherboards, DREQ is
193 1.12 briggs * visible on bit 0 of the IOSB's emulated VIA2 vIFR (and
194 1.12 briggs * the scsi registers are offset 0x1000 bytes from IOBase).
195 1.12 briggs *
196 1.12 briggs * For the Q700/900/950 it's at f9800024 for bus 0 and
197 1.12 briggs * f9800028 for bus 1 (900/950). For these machines, that is also
198 1.12 briggs * a (12-bit) configuration register for DAFB's control of the
199 1.12 briggs * pseudo-DMA timing. The default value is 0x1d1.
200 1.12 briggs */
201 1.12 briggs esp_have_dreq = esp_dafb_have_dreq;
202 1.12 briggs if (sc->sc_dev.dv_unit == 0) {
203 1.12 briggs if (reg_offset == 0x10000) {
204 1.12 briggs quick = 1;
205 1.12 briggs esp_have_dreq = esp_iosb_have_dreq;
206 1.12 briggs } else if (reg_offset == 0x18000) {
207 1.12 briggs quick = 0;
208 1.12 briggs } else {
209 1.12 briggs if (bus_space_map(esc->sc_tag, 0xf9800024,
210 1.12 briggs 4, 0, &esc->sc_bsh)) {
211 1.12 briggs printf("failed to map 4 at 0xf9800024.\n");
212 1.12 briggs } else {
213 1.12 briggs quick = 1;
214 1.12 briggs bus_space_write_4(esc->sc_tag,
215 1.12 briggs esc->sc_bsh, 0, 0x1d1);
216 1.12 briggs }
217 1.12 briggs }
218 1.12 briggs } else {
219 1.12 briggs if (bus_space_map(esc->sc_tag, 0xf9800028,
220 1.12 briggs 4, 0, &esc->sc_bsh)) {
221 1.12 briggs printf("failed to map 4 at 0xf9800028.\n");
222 1.12 briggs } else {
223 1.12 briggs quick = 1;
224 1.12 briggs bus_space_write_4(esc->sc_tag, esc->sc_bsh, 0, 0x1d1);
225 1.12 briggs }
226 1.12 briggs }
227 1.12 briggs if (quick) {
228 1.12 briggs esp_glue.gl_write_reg = esp_quick_write_reg;
229 1.12 briggs esp_glue.gl_dma_intr = esp_quick_dma_intr;
230 1.12 briggs esp_glue.gl_dma_setup = esp_quick_dma_setup;
231 1.12 briggs esp_glue.gl_dma_go = esp_quick_dma_go;
232 1.12 briggs }
233 1.1 briggs
234 1.1 briggs /*
235 1.7 briggs * Set up the glue for MI code early; we use some of it here.
236 1.1 briggs */
237 1.7 briggs sc->sc_glue = &esp_glue;
238 1.1 briggs
239 1.1 briggs /*
240 1.7 briggs * Save the regs
241 1.1 briggs */
242 1.1 briggs if (sc->sc_dev.dv_unit == 0) {
243 1.16 briggs esp0 = esc;
244 1.2 briggs
245 1.7 briggs esc->sc_reg = (volatile u_char *) SCSIBase;
246 1.23 briggs via2_register_irq(VIA2_SCSIIRQ, esp_intr, esc);
247 1.7 briggs esc->irq_mask = V2IF_SCSIIRQ;
248 1.2 briggs if (reg_offset == 0x10000) {
249 1.26 briggs /* From the Q650 developer's note */
250 1.2 briggs sc->sc_freq = 16500000;
251 1.2 briggs } else {
252 1.2 briggs sc->sc_freq = 25000000;
253 1.2 briggs }
254 1.12 briggs
255 1.12 briggs if (esp_glue.gl_dma_go == esp_quick_dma_go) {
256 1.12 briggs printf(" (quick)");
257 1.12 briggs }
258 1.1 briggs } else {
259 1.16 briggs esp1 = esc;
260 1.16 briggs
261 1.7 briggs esc->sc_reg = (volatile u_char *) SCSIBase + 0x402;
262 1.23 briggs via2_register_irq(VIA2_SCSIIRQ, esp_dualbus_intr, NULL);
263 1.16 briggs esc->irq_mask = 0;
264 1.2 briggs sc->sc_freq = 25000000;
265 1.12 briggs
266 1.12 briggs if (esp_glue.gl_dma_go == esp_quick_dma_go) {
267 1.12 briggs printf(" (quick)");
268 1.12 briggs }
269 1.1 briggs }
270 1.7 briggs
271 1.7 briggs printf(": address %p", esc->sc_reg);
272 1.1 briggs
273 1.1 briggs sc->sc_id = 7;
274 1.1 briggs
275 1.1 briggs /* gimme Mhz */
276 1.1 briggs sc->sc_freq /= 1000000;
277 1.1 briggs
278 1.1 briggs /*
279 1.1 briggs * It is necessary to try to load the 2nd config register here,
280 1.1 briggs * to find out what rev the esp chip is, else the esp_reset
281 1.1 briggs * will not set up the defaults correctly.
282 1.1 briggs */
283 1.13 briggs sc->sc_cfg1 = sc->sc_id; /* | NCRCFG1_PARENB; */
284 1.7 briggs sc->sc_cfg2 = NCRCFG2_SCSI2;
285 1.3 briggs sc->sc_cfg3 = 0;
286 1.7 briggs sc->sc_rev = NCR_VARIANT_NCR53C96;
287 1.1 briggs
288 1.1 briggs /*
289 1.1 briggs * This is the value used to start sync negotiations
290 1.7 briggs * Note that the NCR register "SYNCTP" is programmed
291 1.1 briggs * in "clocks per byte", and has a minimum value of 4.
292 1.1 briggs * The SCSI period used in negotiation is one-fourth
293 1.1 briggs * of the time (in nanoseconds) needed to transfer one byte.
294 1.1 briggs * Since the chip's clock is given in MHz, we have the following
295 1.1 briggs * formula: 4 * period = (1000 / freq) * 4
296 1.1 briggs */
297 1.1 briggs sc->sc_minsync = 1000 / sc->sc_freq;
298 1.1 briggs
299 1.26 briggs /* We need this to fit into the TCR... */
300 1.26 briggs sc->sc_maxxfer = 64 * 1024;
301 1.26 briggs
302 1.26 briggs if (!quick) {
303 1.26 briggs sc->sc_minsync = 0; /* No synchronous xfers w/o DMA */
304 1.26 briggs sc->sc_maxxfer = 8 * 1024;
305 1.26 briggs }
306 1.1 briggs
307 1.1 briggs /*
308 1.7 briggs * Configure interrupts.
309 1.1 briggs */
310 1.16 briggs if (esc->irq_mask) {
311 1.16 briggs via2_reg(vPCR) = 0x22;
312 1.16 briggs via2_reg(vIFR) = esc->irq_mask;
313 1.16 briggs via2_reg(vIER) = 0x80 | esc->irq_mask;
314 1.16 briggs }
315 1.24 thorpej
316 1.24 thorpej /*
317 1.24 thorpej * Now try to attach all the sub-devices
318 1.24 thorpej */
319 1.25 nisimura ncr53c9x_attach(sc, NULL, NULL);
320 1.1 briggs }
321 1.1 briggs
322 1.1 briggs /*
323 1.7 briggs * Glue functions.
324 1.1 briggs */
325 1.1 briggs
326 1.7 briggs u_char
327 1.7 briggs esp_read_reg(sc, reg)
328 1.7 briggs struct ncr53c9x_softc *sc;
329 1.7 briggs int reg;
330 1.1 briggs {
331 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
332 1.1 briggs
333 1.23 briggs return esc->sc_reg[reg * 16];
334 1.1 briggs }
335 1.1 briggs
336 1.1 briggs void
337 1.7 briggs esp_write_reg(sc, reg, val)
338 1.7 briggs struct ncr53c9x_softc *sc;
339 1.7 briggs int reg;
340 1.7 briggs u_char val;
341 1.1 briggs {
342 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
343 1.21 briggs u_char v = val;
344 1.1 briggs
345 1.7 briggs if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA)) {
346 1.7 briggs v = NCRCMD_TRANS;
347 1.1 briggs }
348 1.7 briggs esc->sc_reg[reg * 16] = v;
349 1.1 briggs }
350 1.1 briggs
351 1.12 briggs void
352 1.12 briggs esp_dma_stop(sc)
353 1.12 briggs struct ncr53c9x_softc *sc;
354 1.12 briggs {
355 1.12 briggs }
356 1.12 briggs
357 1.12 briggs int
358 1.12 briggs esp_dma_isactive(sc)
359 1.12 briggs struct ncr53c9x_softc *sc;
360 1.12 briggs {
361 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
362 1.12 briggs
363 1.12 briggs return esc->sc_active;
364 1.12 briggs }
365 1.12 briggs
366 1.7 briggs int
367 1.7 briggs esp_dma_isintr(sc)
368 1.7 briggs struct ncr53c9x_softc *sc;
369 1.1 briggs {
370 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
371 1.1 briggs
372 1.7 briggs return esc->sc_reg[NCR_STAT * 16] & 0x80;
373 1.1 briggs }
374 1.1 briggs
375 1.1 briggs void
376 1.7 briggs esp_dma_reset(sc)
377 1.7 briggs struct ncr53c9x_softc *sc;
378 1.1 briggs {
379 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
380 1.1 briggs
381 1.7 briggs esc->sc_active = 0;
382 1.7 briggs esc->sc_tc = 0;
383 1.1 briggs }
384 1.1 briggs
385 1.7 briggs int
386 1.7 briggs esp_dma_intr(sc)
387 1.7 briggs struct ncr53c9x_softc *sc;
388 1.1 briggs {
389 1.22 briggs struct esp_softc *esc = (struct esp_softc *)sc;
390 1.7 briggs volatile u_char *cmdreg, *intrreg, *statreg, *fiforeg;
391 1.22 briggs u_char *p;
392 1.22 briggs u_int espphase, espstat, espintr;
393 1.22 briggs int cnt, s;
394 1.1 briggs
395 1.7 briggs if (esc->sc_active == 0) {
396 1.7 briggs printf("dma_intr--inactive DMA\n");
397 1.7 briggs return -1;
398 1.1 briggs }
399 1.1 briggs
400 1.7 briggs if ((sc->sc_espintr & NCRINTR_BS) == 0) {
401 1.7 briggs esc->sc_active = 0;
402 1.7 briggs return 0;
403 1.1 briggs }
404 1.1 briggs
405 1.26 briggs cnt = esc->sc_dmasize;
406 1.26 briggs if (esc->sc_dmasize == 0) {
407 1.7 briggs printf("data interrupt, but no count left.");
408 1.1 briggs }
409 1.1 briggs
410 1.7 briggs p = *esc->sc_dmaaddr;
411 1.7 briggs espphase = sc->sc_phase;
412 1.7 briggs espstat = (u_int) sc->sc_espstat;
413 1.7 briggs espintr = (u_int) sc->sc_espintr;
414 1.7 briggs cmdreg = esc->sc_reg + NCR_CMD * 16;
415 1.7 briggs fiforeg = esc->sc_reg + NCR_FIFO * 16;
416 1.7 briggs statreg = esc->sc_reg + NCR_STAT * 16;
417 1.7 briggs intrreg = esc->sc_reg + NCR_INTR * 16;
418 1.7 briggs do {
419 1.7 briggs if (esc->sc_datain) {
420 1.7 briggs *p++ = *fiforeg;
421 1.7 briggs cnt--;
422 1.7 briggs if (espphase == DATA_IN_PHASE) {
423 1.7 briggs *cmdreg = NCRCMD_TRANS;
424 1.7 briggs } else {
425 1.7 briggs esc->sc_active = 0;
426 1.7 briggs }
427 1.7 briggs } else {
428 1.7 briggs if ( (espphase == DATA_OUT_PHASE)
429 1.7 briggs || (espphase == MESSAGE_OUT_PHASE)) {
430 1.7 briggs *fiforeg = *p++;
431 1.7 briggs cnt--;
432 1.7 briggs *cmdreg = NCRCMD_TRANS;
433 1.7 briggs } else {
434 1.7 briggs esc->sc_active = 0;
435 1.7 briggs }
436 1.1 briggs }
437 1.1 briggs
438 1.7 briggs if (esc->sc_active) {
439 1.7 briggs while (!(*statreg & 0x80));
440 1.22 briggs s = splhigh();
441 1.7 briggs espstat = *statreg;
442 1.7 briggs espintr = *intrreg;
443 1.7 briggs espphase = (espintr & NCRINTR_DIS)
444 1.7 briggs ? /* Disconnected */ BUSFREE_PHASE
445 1.7 briggs : espstat & PHASE_MASK;
446 1.22 briggs splx(s);
447 1.1 briggs }
448 1.7 briggs } while (esc->sc_active && (espintr & NCRINTR_BS));
449 1.7 briggs sc->sc_phase = espphase;
450 1.7 briggs sc->sc_espstat = (u_char) espstat;
451 1.7 briggs sc->sc_espintr = (u_char) espintr;
452 1.7 briggs *esc->sc_dmaaddr = p;
453 1.26 briggs esc->sc_dmasize = cnt;
454 1.1 briggs
455 1.26 briggs if (esc->sc_dmasize == 0) {
456 1.7 briggs esc->sc_tc = NCRSTAT_TC;
457 1.1 briggs }
458 1.7 briggs sc->sc_espstat |= esc->sc_tc;
459 1.7 briggs return 0;
460 1.1 briggs }
461 1.1 briggs
462 1.1 briggs int
463 1.7 briggs esp_dma_setup(sc, addr, len, datain, dmasize)
464 1.7 briggs struct ncr53c9x_softc *sc;
465 1.7 briggs caddr_t *addr;
466 1.7 briggs size_t *len;
467 1.7 briggs int datain;
468 1.7 briggs size_t *dmasize;
469 1.1 briggs {
470 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
471 1.1 briggs
472 1.7 briggs esc->sc_dmaaddr = addr;
473 1.12 briggs esc->sc_dmalen = len;
474 1.7 briggs esc->sc_datain = datain;
475 1.7 briggs esc->sc_dmasize = *dmasize;
476 1.7 briggs esc->sc_tc = 0;
477 1.1 briggs
478 1.7 briggs return 0;
479 1.1 briggs }
480 1.1 briggs
481 1.1 briggs void
482 1.7 briggs esp_dma_go(sc)
483 1.7 briggs struct ncr53c9x_softc *sc;
484 1.1 briggs {
485 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
486 1.1 briggs
487 1.7 briggs if (esc->sc_datain == 0) {
488 1.7 briggs esc->sc_reg[NCR_FIFO * 16] = **esc->sc_dmaaddr;
489 1.12 briggs (*esc->sc_dmalen)--;
490 1.7 briggs (*esc->sc_dmaaddr)++;
491 1.1 briggs }
492 1.7 briggs esc->sc_active = 1;
493 1.1 briggs }
494 1.1 briggs
495 1.1 briggs void
496 1.12 briggs esp_quick_write_reg(sc, reg, val)
497 1.7 briggs struct ncr53c9x_softc *sc;
498 1.12 briggs int reg;
499 1.12 briggs u_char val;
500 1.1 briggs {
501 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
502 1.12 briggs
503 1.23 briggs esc->sc_reg[reg * 16] = val;
504 1.1 briggs }
505 1.1 briggs
506 1.26 briggs #if DEBUG
507 1.26 briggs int mac68k_esp_debug=0;
508 1.26 briggs #endif
509 1.26 briggs
510 1.1 briggs int
511 1.12 briggs esp_quick_dma_intr(sc)
512 1.12 briggs struct ncr53c9x_softc *sc;
513 1.12 briggs {
514 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
515 1.12 briggs int trans=0, resid=0;
516 1.12 briggs
517 1.12 briggs if (esc->sc_active == 0)
518 1.12 briggs panic("dma_intr--inactive DMA\n");
519 1.12 briggs
520 1.12 briggs esc->sc_active = 0;
521 1.12 briggs
522 1.12 briggs if (esc->sc_dmasize == 0) {
523 1.12 briggs int res;
524 1.12 briggs
525 1.26 briggs res = NCR_READ_REG(sc, NCR_TCL);
526 1.26 briggs res += NCR_READ_REG(sc, NCR_TCM) << 8;
527 1.28 briggs /* This can happen in the case of a TRPAD operation */
528 1.28 briggs /* Pretend that it was complete */
529 1.28 briggs sc->sc_espstat |= NCRSTAT_TC;
530 1.28 briggs #if DEBUG
531 1.28 briggs if (mac68k_esp_debug) {
532 1.28 briggs printf("dmaintr: DMA xfer of zero xferred %d\n",
533 1.28 briggs 65536 - res);
534 1.28 briggs }
535 1.28 briggs #endif
536 1.12 briggs return 0;
537 1.12 briggs }
538 1.12 briggs
539 1.12 briggs if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
540 1.28 briggs if (esc->sc_datain == 0) {
541 1.28 briggs resid = NCR_READ_REG(sc, NCR_FFLAG) & 0x1f;
542 1.28 briggs #if DEBUG
543 1.28 briggs if (mac68k_esp_debug) {
544 1.28 briggs printf("Write FIFO residual %d bytes\n", resid);
545 1.28 briggs }
546 1.28 briggs #endif
547 1.28 briggs }
548 1.12 briggs resid += NCR_READ_REG(sc, NCR_TCL);
549 1.12 briggs resid += NCR_READ_REG(sc, NCR_TCM) << 8;
550 1.12 briggs if (resid == 0)
551 1.12 briggs resid = 65536;
552 1.12 briggs }
553 1.12 briggs
554 1.12 briggs trans = esc->sc_dmasize - resid;
555 1.12 briggs if (trans < 0) {
556 1.12 briggs printf("dmaintr: trans < 0????");
557 1.26 briggs trans = *esc->sc_dmalen;
558 1.12 briggs }
559 1.12 briggs
560 1.12 briggs NCR_DMA(("dmaintr: trans %d, resid %d.\n", trans, resid));
561 1.26 briggs #if DEBUG
562 1.26 briggs if (mac68k_esp_debug) {
563 1.26 briggs printf("eqd_intr: trans %d, resid %d.\n", trans, resid);
564 1.26 briggs }
565 1.26 briggs #endif
566 1.12 briggs *esc->sc_dmaaddr += trans;
567 1.12 briggs *esc->sc_dmalen -= trans;
568 1.12 briggs
569 1.12 briggs return 0;
570 1.12 briggs }
571 1.12 briggs
572 1.12 briggs int
573 1.12 briggs esp_quick_dma_setup(sc, addr, len, datain, dmasize)
574 1.12 briggs struct ncr53c9x_softc *sc;
575 1.12 briggs caddr_t *addr;
576 1.12 briggs size_t *len;
577 1.12 briggs int datain;
578 1.12 briggs size_t *dmasize;
579 1.12 briggs {
580 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
581 1.12 briggs
582 1.12 briggs esc->sc_dmaaddr = addr;
583 1.12 briggs esc->sc_dmalen = len;
584 1.12 briggs
585 1.26 briggs if (*len & 1) {
586 1.13 briggs esc->sc_pad = 1;
587 1.13 briggs } else {
588 1.13 briggs esc->sc_pad = 0;
589 1.13 briggs }
590 1.12 briggs
591 1.12 briggs esc->sc_datain = datain;
592 1.12 briggs esc->sc_dmasize = *dmasize;
593 1.12 briggs
594 1.26 briggs #if DIAGNOSTIC
595 1.26 briggs if (esc->sc_dmasize == 0) {
596 1.28 briggs /* This can happen in the case of a TRPAD operation */
597 1.26 briggs }
598 1.26 briggs #endif
599 1.26 briggs #if DEBUG
600 1.26 briggs if (mac68k_esp_debug) {
601 1.26 briggs printf("eqd_setup: addr %lx, len %lx, in? %d, dmasize %lx\n",
602 1.26 briggs (long) *addr, (long) *len, datain, (long) esc->sc_dmasize);
603 1.26 briggs }
604 1.26 briggs #endif
605 1.26 briggs
606 1.12 briggs return 0;
607 1.12 briggs }
608 1.12 briggs
609 1.12 briggs static __inline__ int
610 1.12 briggs esp_dafb_have_dreq(esc)
611 1.12 briggs struct esp_softc *esc;
612 1.12 briggs {
613 1.26 briggs return (*(volatile u_int32_t *)(esc->sc_bsh.base) & 0x200);
614 1.12 briggs }
615 1.12 briggs
616 1.12 briggs static __inline__ int
617 1.12 briggs esp_iosb_have_dreq(esc)
618 1.12 briggs struct esp_softc *esc;
619 1.12 briggs {
620 1.12 briggs return (via2_reg(vIFR) & V2IF_SCSIDRQ);
621 1.12 briggs }
622 1.12 briggs
623 1.26 briggs static volatile int espspl=-1;
624 1.12 briggs
625 1.26 briggs /*
626 1.26 briggs * Apple "DMA" is weird.
627 1.26 briggs *
628 1.26 briggs * Basically, the CPU acts like the DMA controller. The DREQ/ off the
629 1.26 briggs * chip goes to a register that we've mapped at attach time (on the
630 1.26 briggs * IOSB or DAFB, depending on the machine). Apple also provides some
631 1.26 briggs * space for which the memory controller handshakes data to/from the
632 1.26 briggs * NCR chip with the DACK/ line. This space appears to be mapped over
633 1.26 briggs * and over, every 4 bytes, but only the lower 16 bits are valid (but
634 1.26 briggs * reading the upper 16 bits will handshake DACK/ just fine, so if you
635 1.26 briggs * read *u_int16_t++ = *u_int16_t++ in a loop, you'll get
636 1.26 briggs * <databyte><databyte>0xff0xff<databyte><databyte>0xff0xff...
637 1.26 briggs *
638 1.26 briggs * When you're attempting to read or write memory to this DACK/ed space,
639 1.26 briggs * and the NCR is not ready for some timeout period, the system will
640 1.26 briggs * generate a bus error. This might be for one of several reasons:
641 1.26 briggs *
642 1.26 briggs * 1) (on write) The FIFO is full and is not draining.
643 1.26 briggs * 2) (on read) The FIFO is empty and is not filling.
644 1.26 briggs * 3) An interrupt condition has occurred.
645 1.26 briggs * 4) Anything else?
646 1.26 briggs *
647 1.26 briggs * So if a bus error occurs, we first turn off the nofault bus error handler,
648 1.26 briggs * then we check for an interrupt (which would render the first two
649 1.26 briggs * possibilities moot). If there's no interrupt, check for a DREQ/. If we
650 1.26 briggs * have that, then attempt to resume stuffing (or unstuffing) the FIFO. If
651 1.26 briggs * neither condition holds, pause briefly and check again.
652 1.26 briggs *
653 1.26 briggs * NOTE!!! In order to make allowances for the hardware structure of
654 1.26 briggs * the mac, spl values in here are hardcoded!!!!!!!!!
655 1.26 briggs * This is done to allow serial interrupts to get in during
656 1.26 briggs * scsi transfers. This is ugly.
657 1.26 briggs */
658 1.12 briggs void
659 1.12 briggs esp_quick_dma_go(sc)
660 1.7 briggs struct ncr53c9x_softc *sc;
661 1.1 briggs {
662 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
663 1.26 briggs extern long mac68k_a2_fromfault;
664 1.12 briggs extern int *nofault;
665 1.12 briggs label_t faultbuf;
666 1.12 briggs u_int16_t volatile *pdma;
667 1.26 briggs u_int16_t *addr;
668 1.26 briggs int len, res;
669 1.26 briggs u_short cnt32, cnt2;
670 1.12 briggs u_char volatile *statreg;
671 1.12 briggs
672 1.12 briggs esc->sc_active = 1;
673 1.12 briggs
674 1.26 briggs espspl = splhigh();
675 1.26 briggs
676 1.26 briggs addr = (u_int16_t *) *esc->sc_dmaaddr;
677 1.26 briggs len = esc->sc_dmasize;
678 1.12 briggs
679 1.12 briggs restart_dmago:
680 1.26 briggs #if DEBUG
681 1.26 briggs if (mac68k_esp_debug) {
682 1.26 briggs printf("eqdg: a %lx, l %lx, in? %d ... ",
683 1.26 briggs (long) addr, (long) len, esc->sc_datain);
684 1.26 briggs }
685 1.26 briggs #endif
686 1.12 briggs nofault = (int *) &faultbuf;
687 1.12 briggs if (setjmp((label_t *) nofault)) {
688 1.12 briggs int i=0;
689 1.12 briggs
690 1.12 briggs nofault = (int *) 0;
691 1.26 briggs #if DEBUG
692 1.26 briggs if (mac68k_esp_debug) {
693 1.26 briggs printf("be\n");
694 1.26 briggs }
695 1.26 briggs #endif
696 1.26 briggs /*
697 1.26 briggs * Bus error...
698 1.26 briggs * So, we first check for an interrupt. If we have
699 1.26 briggs * one, go handle it. Next we check for DREQ/. If
700 1.26 briggs * we have it, then we restart the transfer. If
701 1.26 briggs * neither, then loop until we get one or the other.
702 1.26 briggs */
703 1.12 briggs statreg = esc->sc_reg + NCR_STAT * 16;
704 1.12 briggs for (;;) {
705 1.26 briggs spl2(); /* Give serial a chance... */
706 1.26 briggs splhigh(); /* That's enough... */
707 1.26 briggs
708 1.12 briggs if (*statreg & 0x80) {
709 1.12 briggs goto gotintr;
710 1.12 briggs }
711 1.12 briggs
712 1.12 briggs if (esp_have_dreq(esc)) {
713 1.26 briggs /*
714 1.28 briggs * Get the remaining length from the address
715 1.26 briggs * differential.
716 1.26 briggs */
717 1.26 briggs addr = (u_int16_t *) mac68k_a2_fromfault;
718 1.26 briggs len = esc->sc_dmasize -
719 1.26 briggs ((long) addr - (long) *esc->sc_dmaaddr);
720 1.26 briggs
721 1.26 briggs if (esc->sc_datain == 0) {
722 1.26 briggs /*
723 1.26 briggs * Let the FIFO drain before we read
724 1.26 briggs * the transfer count.
725 1.26 briggs * Do we need to do this?
726 1.26 briggs * Can we do this?
727 1.26 briggs */
728 1.26 briggs while (NCR_READ_REG(sc, NCR_FFLAG)
729 1.26 briggs & 0x1f);
730 1.26 briggs /*
731 1.26 briggs * Get the length from the transfer
732 1.26 briggs * counters.
733 1.26 briggs */
734 1.26 briggs res = NCR_READ_REG(sc, NCR_TCL);
735 1.26 briggs res += NCR_READ_REG(sc, NCR_TCM) << 8;
736 1.26 briggs /*
737 1.26 briggs * If they don't agree,
738 1.26 briggs * adjust accordingly.
739 1.26 briggs */
740 1.26 briggs while (res > len) {
741 1.26 briggs len+=2; addr--;
742 1.26 briggs }
743 1.26 briggs if (res != len) {
744 1.26 briggs panic("esp_quick_dma_go: res %d != len %d\n",
745 1.26 briggs res, len);
746 1.26 briggs }
747 1.26 briggs }
748 1.12 briggs break;
749 1.12 briggs }
750 1.12 briggs
751 1.12 briggs DELAY(1);
752 1.26 briggs if (i++ > 1000000)
753 1.26 briggs panic("esp_dma_go: Bus error, but no condition! Argh!");
754 1.12 briggs }
755 1.12 briggs goto restart_dmago;
756 1.12 briggs }
757 1.12 briggs
758 1.26 briggs len &= ~1;
759 1.26 briggs
760 1.12 briggs statreg = esc->sc_reg + NCR_STAT * 16;
761 1.12 briggs pdma = (u_int16_t *) (esc->sc_reg + 0x100);
762 1.1 briggs
763 1.26 briggs /*
764 1.26 briggs * These loops are unrolled into assembly for two reasons:
765 1.26 briggs * 1) We can make sure that they are as efficient as possible, and
766 1.26 briggs * 2) (more importantly) we need the address that we are reading
767 1.26 briggs * from or writing to to be in a2.
768 1.26 briggs */
769 1.26 briggs cnt32 = len / 32;
770 1.26 briggs cnt2 = (len % 32) / 2;
771 1.12 briggs if (esc->sc_datain == 0) {
772 1.26 briggs /* while (cnt32--) { 16 instances of *pdma = *addr++; } */
773 1.26 briggs /* while (cnt2--) { *pdma = *addr++; } */
774 1.26 briggs __asm __volatile ("
775 1.26 briggs movl %1, %%a2
776 1.26 briggs movl %2, %%a3
777 1.26 briggs movw %3, %%d2
778 1.26 briggs cmpw #0, %%d2
779 1.26 briggs beq 2f
780 1.26 briggs subql #1, %%d2
781 1.26 briggs 1: movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
782 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
783 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
784 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
785 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
786 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
787 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
788 1.26 briggs movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
789 1.26 briggs movw #8704,%%sr
790 1.26 briggs movw #9728,%%sr
791 1.26 briggs dbra %%d2, 1b
792 1.26 briggs 2: movw %4, %%d2
793 1.26 briggs cmpw #0, %%d2
794 1.26 briggs beq 4f
795 1.26 briggs subql #1, %%d2
796 1.26 briggs 3: movw %%a2@+,%%a3@
797 1.26 briggs dbra %%d2, 3b
798 1.26 briggs 4: movl %%a2, %0"
799 1.26 briggs : "=g" (addr)
800 1.26 briggs : "0" (addr), "g" (pdma), "g" (cnt32), "g" (cnt2)
801 1.26 briggs : "a2", "a3", "d2");
802 1.13 briggs if (esc->sc_pad) {
803 1.13 briggs unsigned char *c;
804 1.26 briggs c = (unsigned char *) addr;
805 1.26 briggs /* Wait for DREQ */
806 1.26 briggs while (!esp_have_dreq(esc)) {
807 1.26 briggs if (*statreg & 0x80) {
808 1.26 briggs nofault = (int *) 0;
809 1.26 briggs goto gotintr;
810 1.26 briggs }
811 1.26 briggs }
812 1.26 briggs *(unsigned char *)pdma = *c;
813 1.13 briggs }
814 1.12 briggs } else {
815 1.26 briggs /* while (cnt32--) { 16 instances of *addr++ = *pdma; } */
816 1.26 briggs /* while (cnt2--) { *addr++ = *pdma; } */
817 1.26 briggs __asm __volatile ("
818 1.26 briggs movl %1, %%a2
819 1.26 briggs movl %2, %%a3
820 1.26 briggs movw %3, %%d2
821 1.26 briggs cmpw #0, %%d2
822 1.26 briggs beq 6f
823 1.26 briggs subql #1, %%d2
824 1.26 briggs 5: movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
825 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
826 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
827 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
828 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
829 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
830 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
831 1.26 briggs movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
832 1.26 briggs movw #8704,%%sr
833 1.26 briggs movw #9728,%%sr
834 1.26 briggs dbra %%d2, 5b
835 1.26 briggs 6: movw %4, %%d2
836 1.26 briggs cmpw #0, %%d2
837 1.26 briggs beq 8f
838 1.26 briggs subql #1, %%d2
839 1.26 briggs 7: movw %%a3@,%%a2@+
840 1.26 briggs dbra %%d2, 7b
841 1.26 briggs 8: movl %%a2, %0"
842 1.26 briggs : "=g" (addr)
843 1.26 briggs : "0" (addr), "g" (pdma), "g" (cnt32), "g" (cnt2)
844 1.26 briggs : "a2", "a3", "d2");
845 1.13 briggs if (esc->sc_pad) {
846 1.13 briggs unsigned char *c;
847 1.26 briggs c = (unsigned char *) addr;
848 1.26 briggs /* Wait for DREQ */
849 1.26 briggs while (!esp_have_dreq(esc)) {
850 1.26 briggs if (*statreg & 0x80) {
851 1.26 briggs nofault = (int *) 0;
852 1.26 briggs goto gotintr;
853 1.26 briggs }
854 1.26 briggs }
855 1.26 briggs *c = *(unsigned char *)pdma;
856 1.12 briggs }
857 1.12 briggs }
858 1.12 briggs
859 1.12 briggs nofault = (int *) 0;
860 1.12 briggs
861 1.26 briggs /*
862 1.26 briggs * If we have not received an interrupt yet, we should shortly,
863 1.26 briggs * and we can't prevent it, so return and wait for it.
864 1.26 briggs */
865 1.12 briggs if ((*statreg & 0x80) == 0) {
866 1.26 briggs #if DEBUG
867 1.26 briggs if (mac68k_esp_debug) {
868 1.26 briggs printf("g.\n");
869 1.26 briggs }
870 1.26 briggs #endif
871 1.12 briggs if (espspl != -1) splx(espspl); espspl = -1;
872 1.12 briggs return;
873 1.12 briggs }
874 1.12 briggs
875 1.12 briggs gotintr:
876 1.26 briggs #if DEBUG
877 1.26 briggs if (mac68k_esp_debug) {
878 1.26 briggs printf("g!\n");
879 1.26 briggs }
880 1.26 briggs #endif
881 1.12 briggs ncr53c9x_intr(sc);
882 1.12 briggs if (espspl != -1) splx(espspl); espspl = -1;
883 1.16 briggs }
884 1.16 briggs
885 1.23 briggs void
886 1.23 briggs esp_intr(sc)
887 1.23 briggs void *sc;
888 1.23 briggs {
889 1.23 briggs struct esp_softc *esc = (struct esp_softc *)sc;
890 1.23 briggs
891 1.26 briggs if (esc->sc_reg[NCR_STAT * 16] & 0x80) {
892 1.26 briggs ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
893 1.26 briggs }
894 1.23 briggs }
895 1.23 briggs
896 1.23 briggs void
897 1.16 briggs esp_dualbus_intr(sc)
898 1.23 briggs void *sc;
899 1.16 briggs {
900 1.26 briggs if (esp0 && (esp0->sc_reg[NCR_STAT * 16] & 0x80)) {
901 1.26 briggs ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
902 1.26 briggs }
903 1.22 briggs
904 1.26 briggs if (esp1 && (esp1->sc_reg[NCR_STAT * 16] & 0x80)) {
905 1.26 briggs ncr53c9x_intr((struct ncr53c9x_softc *) esp1);
906 1.26 briggs }
907 1.1 briggs }
908