esp.c revision 1.57 1 1.57 rin /* $NetBSD: esp.c,v 1.57 2019/07/23 07:52:53 rin 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.35 lukem
79 1.35 lukem #include <sys/cdefs.h>
80 1.57 rin __KERNEL_RCSID(0, "$NetBSD: esp.c,v 1.57 2019/07/23 07:52:53 rin Exp $");
81 1.1 briggs
82 1.1 briggs #include <sys/types.h>
83 1.1 briggs #include <sys/param.h>
84 1.57 rin #include <sys/buf.h>
85 1.57 rin #include <sys/bus.h>
86 1.1 briggs #include <sys/device.h>
87 1.1 briggs
88 1.11 bouyer #include <dev/scsipi/scsiconf.h>
89 1.1 briggs
90 1.7 briggs #include <dev/ic/ncr53c9xreg.h>
91 1.7 briggs #include <dev/ic/ncr53c9xvar.h>
92 1.7 briggs
93 1.57 rin #include <machine/cpu.h>
94 1.1 briggs #include <machine/viareg.h>
95 1.1 briggs
96 1.15 scottr #include <mac68k/obio/espvar.h>
97 1.15 scottr #include <mac68k/obio/obiovar.h>
98 1.3 briggs
99 1.57 rin static int espmatch(device_t, cfdata_t, void *);
100 1.57 rin static void espattach(device_t, device_t, void *);
101 1.1 briggs
102 1.1 briggs /* Linkup to the rest of the kernel */
103 1.50 tsutsui CFATTACH_DECL_NEW(esp, sizeof(struct esp_softc),
104 1.34 thorpej espmatch, espattach, NULL, NULL);
105 1.1 briggs
106 1.7 briggs /*
107 1.7 briggs * Functions and the switch for the MI code.
108 1.7 briggs */
109 1.57 rin static uint8_t esp_read_reg(struct ncr53c9x_softc *, int);
110 1.57 rin static void esp_write_reg(struct ncr53c9x_softc *, int, uint8_t);
111 1.57 rin static int esp_dma_isintr(struct ncr53c9x_softc *);
112 1.57 rin static void esp_dma_reset(struct ncr53c9x_softc *);
113 1.57 rin static int esp_dma_intr(struct ncr53c9x_softc *);
114 1.57 rin static int esp_dma_setup(struct ncr53c9x_softc *, uint8_t **, size_t *,
115 1.57 rin int, size_t *);
116 1.57 rin static void esp_dma_go(struct ncr53c9x_softc *);
117 1.57 rin static void esp_dma_stop(struct ncr53c9x_softc *);
118 1.57 rin static int esp_dma_isactive(struct ncr53c9x_softc *);
119 1.57 rin static void esp_quick_write_reg(struct ncr53c9x_softc *, int, uint8_t);
120 1.57 rin static int esp_quick_dma_intr(struct ncr53c9x_softc *);
121 1.57 rin static int esp_quick_dma_setup(struct ncr53c9x_softc *, uint8_t **,
122 1.57 rin size_t *, int, size_t *);
123 1.57 rin static void esp_quick_dma_go(struct ncr53c9x_softc *);
124 1.36 chs
125 1.57 rin static void esp_intr(void *);
126 1.57 rin static void esp_dualbus_intr(void *);
127 1.57 rin
128 1.57 rin static int esp_dafb_have_dreq(struct esp_softc *);
129 1.57 rin static int esp_iosb_have_dreq(struct esp_softc *);
130 1.36 chs int (*esp_have_dreq)(struct esp_softc *);
131 1.7 briggs
132 1.57 rin static struct esp_softc *esp0, *esp1;
133 1.57 rin
134 1.57 rin static struct ncr53c9x_glue esp_glue = {
135 1.57 rin .gl_read_reg = esp_read_reg,
136 1.57 rin .gl_write_reg = esp_write_reg,
137 1.57 rin .gl_dma_isintr = esp_dma_isintr,
138 1.57 rin .gl_dma_reset = esp_dma_reset,
139 1.57 rin .gl_dma_intr = esp_dma_intr,
140 1.57 rin .gl_dma_setup = esp_dma_setup,
141 1.57 rin .gl_dma_go = esp_dma_go,
142 1.57 rin .gl_dma_stop = esp_dma_stop,
143 1.57 rin .gl_dma_isactive = esp_dma_isactive,
144 1.57 rin .gl_clear_latched_intr = NULL,
145 1.7 briggs };
146 1.7 briggs
147 1.57 rin static int
148 1.50 tsutsui espmatch(device_t parent, cfdata_t cf, void *aux)
149 1.1 briggs {
150 1.50 tsutsui struct obio_attach_args *oa = aux;
151 1.12 briggs
152 1.57 rin if (oa->oa_addr == 0 && mac68k_machine.scsi96)
153 1.38 chs return 1;
154 1.57 rin if (oa->oa_addr == 1 && mac68k_machine.scsi96_2)
155 1.38 chs return 1;
156 1.38 chs return 0;
157 1.1 briggs }
158 1.1 briggs
159 1.1 briggs /*
160 1.1 briggs * Attach this instance, and then all the sub-devices
161 1.1 briggs */
162 1.57 rin static void
163 1.50 tsutsui espattach(device_t parent, device_t self, void *aux)
164 1.1 briggs {
165 1.50 tsutsui struct esp_softc *esc = device_private(self);
166 1.12 briggs struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
167 1.50 tsutsui struct obio_attach_args *oa = aux;
168 1.57 rin bus_addr_t addr;
169 1.57 rin unsigned long reg_offset;
170 1.12 briggs int quick = 0;
171 1.57 rin uint8_t irq_mask; /* mask for clearing IRQ */
172 1.50 tsutsui extern vaddr_t SCSIBase;
173 1.50 tsutsui
174 1.50 tsutsui sc->sc_dev = self;
175 1.12 briggs
176 1.12 briggs reg_offset = SCSIBase - IOBase;
177 1.37 chs
178 1.12 briggs /*
179 1.12 briggs * For Wombat, Primus and Optimus motherboards, DREQ is
180 1.12 briggs * visible on bit 0 of the IOSB's emulated VIA2 vIFR (and
181 1.12 briggs * the scsi registers are offset 0x1000 bytes from IOBase).
182 1.12 briggs *
183 1.12 briggs * For the Q700/900/950 it's at f9800024 for bus 0 and
184 1.12 briggs * f9800028 for bus 1 (900/950). For these machines, that is also
185 1.12 briggs * a (12-bit) configuration register for DAFB's control of the
186 1.12 briggs * pseudo-DMA timing. The default value is 0x1d1.
187 1.12 briggs */
188 1.39 chs if (oa->oa_addr == 0) {
189 1.12 briggs if (reg_offset == 0x10000) {
190 1.12 briggs quick = 1;
191 1.12 briggs esp_have_dreq = esp_iosb_have_dreq;
192 1.57 rin } else if (reg_offset == 0x18000)
193 1.12 briggs quick = 0;
194 1.57 rin else {
195 1.57 rin addr = 0xf9800024;
196 1.57 rin goto dafb_dreq;
197 1.12 briggs }
198 1.12 briggs } else {
199 1.57 rin bus_space_tag_t bst;
200 1.57 rin bus_space_handle_t bsh;
201 1.57 rin
202 1.57 rin addr = 0xf9800028;
203 1.57 rin
204 1.57 rin dafb_dreq: bst = oa->oa_tag;
205 1.57 rin if (bus_space_map(bst, addr, 4, 0, &bsh))
206 1.57 rin aprint_error(": failed to map 4 at 0x%lx.\n", addr);
207 1.57 rin else {
208 1.12 briggs quick = 1;
209 1.57 rin esp_have_dreq = esp_dafb_have_dreq;
210 1.57 rin esc->sc_dreqreg = (volatile uint32_t *)
211 1.57 rin bus_space_vaddr(bst, bsh);
212 1.57 rin *esc->sc_dreqreg = 0x1d1;
213 1.12 briggs }
214 1.12 briggs }
215 1.57 rin
216 1.12 briggs if (quick) {
217 1.12 briggs esp_glue.gl_write_reg = esp_quick_write_reg;
218 1.12 briggs esp_glue.gl_dma_intr = esp_quick_dma_intr;
219 1.12 briggs esp_glue.gl_dma_setup = esp_quick_dma_setup;
220 1.12 briggs esp_glue.gl_dma_go = esp_quick_dma_go;
221 1.12 briggs }
222 1.1 briggs
223 1.1 briggs /*
224 1.7 briggs * Set up the glue for MI code early; we use some of it here.
225 1.1 briggs */
226 1.7 briggs sc->sc_glue = &esp_glue;
227 1.1 briggs
228 1.1 briggs /*
229 1.7 briggs * Save the regs
230 1.1 briggs */
231 1.39 chs if (oa->oa_addr == 0) {
232 1.16 briggs esp0 = esc;
233 1.2 briggs
234 1.50 tsutsui esc->sc_reg = (volatile uint8_t *)SCSIBase;
235 1.23 briggs via2_register_irq(VIA2_SCSIIRQ, esp_intr, esc);
236 1.57 rin irq_mask = V2IF_SCSIIRQ;
237 1.2 briggs if (reg_offset == 0x10000) {
238 1.26 briggs /* From the Q650 developer's note */
239 1.2 briggs sc->sc_freq = 16500000;
240 1.57 rin } else
241 1.2 briggs sc->sc_freq = 25000000;
242 1.12 briggs
243 1.57 rin if (quick)
244 1.50 tsutsui aprint_normal(" (quick)");
245 1.1 briggs } else {
246 1.16 briggs esp1 = esc;
247 1.16 briggs
248 1.50 tsutsui esc->sc_reg = (volatile uint8_t *)SCSIBase + 0x402;
249 1.23 briggs via2_register_irq(VIA2_SCSIIRQ, esp_dualbus_intr, NULL);
250 1.57 rin irq_mask = 0;
251 1.2 briggs sc->sc_freq = 25000000;
252 1.12 briggs
253 1.57 rin if (quick)
254 1.12 briggs printf(" (quick)");
255 1.1 briggs }
256 1.7 briggs
257 1.50 tsutsui aprint_normal(": address %p", esc->sc_reg);
258 1.1 briggs
259 1.1 briggs sc->sc_id = 7;
260 1.1 briggs
261 1.44 lukem /* gimme MHz */
262 1.1 briggs sc->sc_freq /= 1000000;
263 1.1 briggs
264 1.1 briggs /*
265 1.1 briggs * It is necessary to try to load the 2nd config register here,
266 1.1 briggs * to find out what rev the esp chip is, else the esp_reset
267 1.1 briggs * will not set up the defaults correctly.
268 1.1 briggs */
269 1.13 briggs sc->sc_cfg1 = sc->sc_id; /* | NCRCFG1_PARENB; */
270 1.7 briggs sc->sc_cfg2 = NCRCFG2_SCSI2;
271 1.3 briggs sc->sc_cfg3 = 0;
272 1.7 briggs sc->sc_rev = NCR_VARIANT_NCR53C96;
273 1.1 briggs
274 1.1 briggs /*
275 1.1 briggs * This is the value used to start sync negotiations
276 1.7 briggs * Note that the NCR register "SYNCTP" is programmed
277 1.1 briggs * in "clocks per byte", and has a minimum value of 4.
278 1.1 briggs * The SCSI period used in negotiation is one-fourth
279 1.1 briggs * of the time (in nanoseconds) needed to transfer one byte.
280 1.1 briggs * Since the chip's clock is given in MHz, we have the following
281 1.1 briggs * formula: 4 * period = (1000 / freq) * 4
282 1.1 briggs */
283 1.1 briggs sc->sc_minsync = 1000 / sc->sc_freq;
284 1.1 briggs
285 1.26 briggs /* We need this to fit into the TCR... */
286 1.26 briggs sc->sc_maxxfer = 64 * 1024;
287 1.26 briggs
288 1.48 tsutsui switch (current_mac_model->machineid) {
289 1.48 tsutsui case MACH_MACQ630:
290 1.48 tsutsui /* XXX on LC630 64k xfer causes timeout error */
291 1.48 tsutsui sc->sc_maxxfer = 63 * 1024;
292 1.48 tsutsui break;
293 1.48 tsutsui }
294 1.48 tsutsui
295 1.26 briggs if (!quick) {
296 1.26 briggs sc->sc_minsync = 0; /* No synchronous xfers w/o DMA */
297 1.26 briggs sc->sc_maxxfer = 8 * 1024;
298 1.26 briggs }
299 1.1 briggs
300 1.1 briggs /*
301 1.7 briggs * Configure interrupts.
302 1.1 briggs */
303 1.57 rin if (irq_mask) {
304 1.16 briggs via2_reg(vPCR) = 0x22;
305 1.57 rin via2_reg(vIFR) = irq_mask;
306 1.57 rin via2_reg(vIER) = 0x80 | irq_mask;
307 1.16 briggs }
308 1.24 thorpej
309 1.24 thorpej /*
310 1.24 thorpej * Now try to attach all the sub-devices
311 1.24 thorpej */
312 1.29 bouyer sc->sc_adapter.adapt_minphys = minphys;
313 1.29 bouyer sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
314 1.29 bouyer ncr53c9x_attach(sc);
315 1.1 briggs }
316 1.1 briggs
317 1.1 briggs /*
318 1.7 briggs * Glue functions.
319 1.1 briggs */
320 1.1 briggs
321 1.57 rin static uint8_t
322 1.37 chs esp_read_reg(struct ncr53c9x_softc *sc, int reg)
323 1.1 briggs {
324 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
325 1.1 briggs
326 1.23 briggs return esc->sc_reg[reg * 16];
327 1.1 briggs }
328 1.1 briggs
329 1.57 rin static void
330 1.50 tsutsui esp_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
331 1.1 briggs {
332 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
333 1.50 tsutsui uint8_t v = val;
334 1.1 briggs
335 1.7 briggs if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA)) {
336 1.7 briggs v = NCRCMD_TRANS;
337 1.1 briggs }
338 1.7 briggs esc->sc_reg[reg * 16] = v;
339 1.1 briggs }
340 1.1 briggs
341 1.57 rin static void
342 1.37 chs esp_dma_stop(struct ncr53c9x_softc *sc)
343 1.12 briggs {
344 1.12 briggs }
345 1.12 briggs
346 1.57 rin static int
347 1.37 chs esp_dma_isactive(struct ncr53c9x_softc *sc)
348 1.12 briggs {
349 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
350 1.12 briggs
351 1.12 briggs return esc->sc_active;
352 1.12 briggs }
353 1.12 briggs
354 1.57 rin static int
355 1.37 chs esp_dma_isintr(struct ncr53c9x_softc *sc)
356 1.1 briggs {
357 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
358 1.1 briggs
359 1.57 rin return esc->sc_reg[NCR_STAT * 16] & NCRSTAT_INT;
360 1.1 briggs }
361 1.1 briggs
362 1.57 rin static void
363 1.37 chs esp_dma_reset(struct ncr53c9x_softc *sc)
364 1.1 briggs {
365 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
366 1.1 briggs
367 1.7 briggs esc->sc_active = 0;
368 1.7 briggs esc->sc_tc = 0;
369 1.1 briggs }
370 1.1 briggs
371 1.57 rin static int
372 1.37 chs esp_dma_intr(struct ncr53c9x_softc *sc)
373 1.1 briggs {
374 1.22 briggs struct esp_softc *esc = (struct esp_softc *)sc;
375 1.57 rin volatile uint8_t *cmdreg, *intrreg, *statreg, *fiforeg;
376 1.50 tsutsui uint8_t *p;
377 1.22 briggs u_int espphase, espstat, espintr;
378 1.22 briggs int cnt, s;
379 1.1 briggs
380 1.7 briggs if (esc->sc_active == 0) {
381 1.7 briggs printf("dma_intr--inactive DMA\n");
382 1.7 briggs return -1;
383 1.1 briggs }
384 1.1 briggs
385 1.7 briggs if ((sc->sc_espintr & NCRINTR_BS) == 0) {
386 1.7 briggs esc->sc_active = 0;
387 1.7 briggs return 0;
388 1.1 briggs }
389 1.1 briggs
390 1.30 briggs cnt = *esc->sc_dmalen;
391 1.30 briggs if (*esc->sc_dmalen == 0) {
392 1.7 briggs printf("data interrupt, but no count left.");
393 1.1 briggs }
394 1.1 briggs
395 1.7 briggs p = *esc->sc_dmaaddr;
396 1.7 briggs espphase = sc->sc_phase;
397 1.50 tsutsui espstat = (u_int)sc->sc_espstat;
398 1.50 tsutsui espintr = (u_int)sc->sc_espintr;
399 1.7 briggs cmdreg = esc->sc_reg + NCR_CMD * 16;
400 1.7 briggs fiforeg = esc->sc_reg + NCR_FIFO * 16;
401 1.7 briggs statreg = esc->sc_reg + NCR_STAT * 16;
402 1.7 briggs intrreg = esc->sc_reg + NCR_INTR * 16;
403 1.7 briggs do {
404 1.7 briggs if (esc->sc_datain) {
405 1.7 briggs *p++ = *fiforeg;
406 1.7 briggs cnt--;
407 1.7 briggs if (espphase == DATA_IN_PHASE) {
408 1.7 briggs *cmdreg = NCRCMD_TRANS;
409 1.7 briggs } else {
410 1.7 briggs esc->sc_active = 0;
411 1.7 briggs }
412 1.7 briggs } else {
413 1.7 briggs if ( (espphase == DATA_OUT_PHASE)
414 1.7 briggs || (espphase == MESSAGE_OUT_PHASE)) {
415 1.7 briggs *fiforeg = *p++;
416 1.7 briggs cnt--;
417 1.7 briggs *cmdreg = NCRCMD_TRANS;
418 1.7 briggs } else {
419 1.7 briggs esc->sc_active = 0;
420 1.7 briggs }
421 1.1 briggs }
422 1.1 briggs
423 1.7 briggs if (esc->sc_active) {
424 1.7 briggs while (!(*statreg & 0x80));
425 1.22 briggs s = splhigh();
426 1.7 briggs espstat = *statreg;
427 1.7 briggs espintr = *intrreg;
428 1.7 briggs espphase = (espintr & NCRINTR_DIS)
429 1.7 briggs ? /* Disconnected */ BUSFREE_PHASE
430 1.7 briggs : espstat & PHASE_MASK;
431 1.22 briggs splx(s);
432 1.1 briggs }
433 1.7 briggs } while (esc->sc_active && (espintr & NCRINTR_BS));
434 1.7 briggs sc->sc_phase = espphase;
435 1.57 rin sc->sc_espstat = (uint8_t)espstat;
436 1.57 rin sc->sc_espintr = (uint8_t)espintr;
437 1.7 briggs *esc->sc_dmaaddr = p;
438 1.30 briggs *esc->sc_dmalen = cnt;
439 1.1 briggs
440 1.30 briggs if (*esc->sc_dmalen == 0) {
441 1.7 briggs esc->sc_tc = NCRSTAT_TC;
442 1.1 briggs }
443 1.7 briggs sc->sc_espstat |= esc->sc_tc;
444 1.7 briggs return 0;
445 1.1 briggs }
446 1.1 briggs
447 1.57 rin static int
448 1.50 tsutsui esp_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
449 1.50 tsutsui int datain, size_t *dmasize)
450 1.1 briggs {
451 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
452 1.1 briggs
453 1.50 tsutsui esc->sc_dmaaddr = addr;
454 1.12 briggs esc->sc_dmalen = len;
455 1.7 briggs esc->sc_datain = datain;
456 1.7 briggs esc->sc_dmasize = *dmasize;
457 1.7 briggs esc->sc_tc = 0;
458 1.1 briggs
459 1.7 briggs return 0;
460 1.1 briggs }
461 1.1 briggs
462 1.57 rin static void
463 1.37 chs esp_dma_go(struct ncr53c9x_softc *sc)
464 1.1 briggs {
465 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
466 1.1 briggs
467 1.7 briggs if (esc->sc_datain == 0) {
468 1.7 briggs esc->sc_reg[NCR_FIFO * 16] = **esc->sc_dmaaddr;
469 1.12 briggs (*esc->sc_dmalen)--;
470 1.7 briggs (*esc->sc_dmaaddr)++;
471 1.1 briggs }
472 1.7 briggs esc->sc_active = 1;
473 1.1 briggs }
474 1.1 briggs
475 1.57 rin static void
476 1.57 rin esp_quick_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
477 1.1 briggs {
478 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
479 1.12 briggs
480 1.23 briggs esc->sc_reg[reg * 16] = val;
481 1.1 briggs }
482 1.1 briggs
483 1.26 briggs #if DEBUG
484 1.26 briggs int mac68k_esp_debug=0;
485 1.26 briggs #endif
486 1.26 briggs
487 1.57 rin static int
488 1.37 chs esp_quick_dma_intr(struct ncr53c9x_softc *sc)
489 1.12 briggs {
490 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
491 1.12 briggs int trans=0, resid=0;
492 1.12 briggs
493 1.12 briggs if (esc->sc_active == 0)
494 1.32 provos panic("dma_intr--inactive DMA");
495 1.12 briggs
496 1.12 briggs esc->sc_active = 0;
497 1.12 briggs
498 1.12 briggs if (esc->sc_dmasize == 0) {
499 1.12 briggs int res;
500 1.12 briggs
501 1.26 briggs res = NCR_READ_REG(sc, NCR_TCL);
502 1.26 briggs res += NCR_READ_REG(sc, NCR_TCM) << 8;
503 1.28 briggs /* This can happen in the case of a TRPAD operation */
504 1.28 briggs /* Pretend that it was complete */
505 1.28 briggs sc->sc_espstat |= NCRSTAT_TC;
506 1.28 briggs #if DEBUG
507 1.28 briggs if (mac68k_esp_debug) {
508 1.28 briggs printf("dmaintr: DMA xfer of zero xferred %d\n",
509 1.28 briggs 65536 - res);
510 1.28 briggs }
511 1.28 briggs #endif
512 1.12 briggs return 0;
513 1.12 briggs }
514 1.12 briggs
515 1.12 briggs if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
516 1.28 briggs if (esc->sc_datain == 0) {
517 1.28 briggs resid = NCR_READ_REG(sc, NCR_FFLAG) & 0x1f;
518 1.28 briggs #if DEBUG
519 1.28 briggs if (mac68k_esp_debug) {
520 1.28 briggs printf("Write FIFO residual %d bytes\n", resid);
521 1.28 briggs }
522 1.28 briggs #endif
523 1.28 briggs }
524 1.12 briggs resid += NCR_READ_REG(sc, NCR_TCL);
525 1.12 briggs resid += NCR_READ_REG(sc, NCR_TCM) << 8;
526 1.12 briggs if (resid == 0)
527 1.12 briggs resid = 65536;
528 1.12 briggs }
529 1.12 briggs
530 1.12 briggs trans = esc->sc_dmasize - resid;
531 1.12 briggs if (trans < 0) {
532 1.12 briggs printf("dmaintr: trans < 0????");
533 1.26 briggs trans = *esc->sc_dmalen;
534 1.12 briggs }
535 1.12 briggs
536 1.12 briggs NCR_DMA(("dmaintr: trans %d, resid %d.\n", trans, resid));
537 1.26 briggs #if DEBUG
538 1.26 briggs if (mac68k_esp_debug) {
539 1.26 briggs printf("eqd_intr: trans %d, resid %d.\n", trans, resid);
540 1.26 briggs }
541 1.26 briggs #endif
542 1.12 briggs *esc->sc_dmaaddr += trans;
543 1.12 briggs *esc->sc_dmalen -= trans;
544 1.12 briggs
545 1.12 briggs return 0;
546 1.12 briggs }
547 1.12 briggs
548 1.57 rin static int
549 1.50 tsutsui esp_quick_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
550 1.37 chs int datain, size_t *dmasize)
551 1.12 briggs {
552 1.12 briggs struct esp_softc *esc = (struct esp_softc *)sc;
553 1.12 briggs
554 1.50 tsutsui esc->sc_dmaaddr = addr;
555 1.12 briggs esc->sc_dmalen = len;
556 1.12 briggs
557 1.26 briggs if (*len & 1) {
558 1.13 briggs esc->sc_pad = 1;
559 1.13 briggs } else {
560 1.13 briggs esc->sc_pad = 0;
561 1.13 briggs }
562 1.12 briggs
563 1.12 briggs esc->sc_datain = datain;
564 1.12 briggs esc->sc_dmasize = *dmasize;
565 1.12 briggs
566 1.26 briggs #if DIAGNOSTIC
567 1.26 briggs if (esc->sc_dmasize == 0) {
568 1.28 briggs /* This can happen in the case of a TRPAD operation */
569 1.26 briggs }
570 1.26 briggs #endif
571 1.26 briggs #if DEBUG
572 1.26 briggs if (mac68k_esp_debug) {
573 1.26 briggs printf("eqd_setup: addr %lx, len %lx, in? %d, dmasize %lx\n",
574 1.26 briggs (long) *addr, (long) *len, datain, (long) esc->sc_dmasize);
575 1.26 briggs }
576 1.26 briggs #endif
577 1.26 briggs
578 1.12 briggs return 0;
579 1.12 briggs }
580 1.12 briggs
581 1.57 rin static int
582 1.37 chs esp_dafb_have_dreq(struct esp_softc *esc)
583 1.12 briggs {
584 1.50 tsutsui
585 1.57 rin return *esc->sc_dreqreg & 0x200;
586 1.12 briggs }
587 1.12 briggs
588 1.57 rin static int
589 1.37 chs esp_iosb_have_dreq(struct esp_softc *esc)
590 1.12 briggs {
591 1.50 tsutsui
592 1.50 tsutsui return via2_reg(vIFR) & V2IF_SCSIDRQ;
593 1.12 briggs }
594 1.12 briggs
595 1.50 tsutsui static volatile int espspl = -1;
596 1.12 briggs
597 1.26 briggs /*
598 1.26 briggs * Apple "DMA" is weird.
599 1.26 briggs *
600 1.26 briggs * Basically, the CPU acts like the DMA controller. The DREQ/ off the
601 1.26 briggs * chip goes to a register that we've mapped at attach time (on the
602 1.26 briggs * IOSB or DAFB, depending on the machine). Apple also provides some
603 1.26 briggs * space for which the memory controller handshakes data to/from the
604 1.26 briggs * NCR chip with the DACK/ line. This space appears to be mapped over
605 1.26 briggs * and over, every 4 bytes, but only the lower 16 bits are valid (but
606 1.26 briggs * reading the upper 16 bits will handshake DACK/ just fine, so if you
607 1.57 rin * read *uint16_t++ = *uint16_t++ in a loop, you'll get
608 1.26 briggs * <databyte><databyte>0xff0xff<databyte><databyte>0xff0xff...
609 1.26 briggs *
610 1.26 briggs * When you're attempting to read or write memory to this DACK/ed space,
611 1.26 briggs * and the NCR is not ready for some timeout period, the system will
612 1.26 briggs * generate a bus error. This might be for one of several reasons:
613 1.26 briggs *
614 1.26 briggs * 1) (on write) The FIFO is full and is not draining.
615 1.26 briggs * 2) (on read) The FIFO is empty and is not filling.
616 1.26 briggs * 3) An interrupt condition has occurred.
617 1.26 briggs * 4) Anything else?
618 1.26 briggs *
619 1.26 briggs * So if a bus error occurs, we first turn off the nofault bus error handler,
620 1.26 briggs * then we check for an interrupt (which would render the first two
621 1.26 briggs * possibilities moot). If there's no interrupt, check for a DREQ/. If we
622 1.26 briggs * have that, then attempt to resume stuffing (or unstuffing) the FIFO. If
623 1.26 briggs * neither condition holds, pause briefly and check again.
624 1.26 briggs *
625 1.26 briggs * NOTE!!! In order to make allowances for the hardware structure of
626 1.26 briggs * the mac, spl values in here are hardcoded!!!!!!!!!
627 1.26 briggs * This is done to allow serial interrupts to get in during
628 1.26 briggs * scsi transfers. This is ugly.
629 1.26 briggs */
630 1.57 rin static void
631 1.37 chs esp_quick_dma_go(struct ncr53c9x_softc *sc)
632 1.1 briggs {
633 1.7 briggs struct esp_softc *esc = (struct esp_softc *)sc;
634 1.26 briggs extern long mac68k_a2_fromfault;
635 1.12 briggs extern int *nofault;
636 1.12 briggs label_t faultbuf;
637 1.50 tsutsui uint16_t volatile *pdma;
638 1.50 tsutsui uint16_t *addr;
639 1.26 briggs int len, res;
640 1.50 tsutsui uint16_t cnt32, cnt2;
641 1.50 tsutsui volatile uint8_t *statreg;
642 1.12 briggs
643 1.12 briggs esc->sc_active = 1;
644 1.12 briggs
645 1.26 briggs espspl = splhigh();
646 1.26 briggs
647 1.50 tsutsui addr = (uint16_t *)*esc->sc_dmaaddr;
648 1.26 briggs len = esc->sc_dmasize;
649 1.12 briggs
650 1.12 briggs restart_dmago:
651 1.26 briggs #if DEBUG
652 1.26 briggs if (mac68k_esp_debug) {
653 1.26 briggs printf("eqdg: a %lx, l %lx, in? %d ... ",
654 1.26 briggs (long) addr, (long) len, esc->sc_datain);
655 1.26 briggs }
656 1.26 briggs #endif
657 1.50 tsutsui nofault = (int *)&faultbuf;
658 1.50 tsutsui if (setjmp((label_t *)nofault)) {
659 1.50 tsutsui int i = 0;
660 1.12 briggs
661 1.50 tsutsui nofault = NULL;
662 1.26 briggs #if DEBUG
663 1.26 briggs if (mac68k_esp_debug) {
664 1.26 briggs printf("be\n");
665 1.26 briggs }
666 1.26 briggs #endif
667 1.26 briggs /*
668 1.26 briggs * Bus error...
669 1.26 briggs * So, we first check for an interrupt. If we have
670 1.26 briggs * one, go handle it. Next we check for DREQ/. If
671 1.26 briggs * we have it, then we restart the transfer. If
672 1.26 briggs * neither, then loop until we get one or the other.
673 1.26 briggs */
674 1.12 briggs statreg = esc->sc_reg + NCR_STAT * 16;
675 1.12 briggs for (;;) {
676 1.26 briggs spl2(); /* Give serial a chance... */
677 1.26 briggs splhigh(); /* That's enough... */
678 1.26 briggs
679 1.12 briggs if (*statreg & 0x80) {
680 1.12 briggs goto gotintr;
681 1.12 briggs }
682 1.12 briggs
683 1.12 briggs if (esp_have_dreq(esc)) {
684 1.26 briggs /*
685 1.28 briggs * Get the remaining length from the address
686 1.26 briggs * differential.
687 1.26 briggs */
688 1.50 tsutsui addr = (uint16_t *)mac68k_a2_fromfault;
689 1.26 briggs len = esc->sc_dmasize -
690 1.50 tsutsui ((long)addr - (long)*esc->sc_dmaaddr);
691 1.26 briggs
692 1.26 briggs if (esc->sc_datain == 0) {
693 1.26 briggs /*
694 1.26 briggs * Let the FIFO drain before we read
695 1.26 briggs * the transfer count.
696 1.26 briggs * Do we need to do this?
697 1.26 briggs * Can we do this?
698 1.26 briggs */
699 1.26 briggs while (NCR_READ_REG(sc, NCR_FFLAG)
700 1.26 briggs & 0x1f);
701 1.26 briggs /*
702 1.26 briggs * Get the length from the transfer
703 1.26 briggs * counters.
704 1.26 briggs */
705 1.26 briggs res = NCR_READ_REG(sc, NCR_TCL);
706 1.26 briggs res += NCR_READ_REG(sc, NCR_TCM) << 8;
707 1.26 briggs /*
708 1.26 briggs * If they don't agree,
709 1.26 briggs * adjust accordingly.
710 1.26 briggs */
711 1.26 briggs while (res > len) {
712 1.26 briggs len+=2; addr--;
713 1.26 briggs }
714 1.26 briggs if (res != len) {
715 1.50 tsutsui panic("%s: res %d != len %d",
716 1.50 tsutsui __func__, res, len);
717 1.26 briggs }
718 1.26 briggs }
719 1.12 briggs break;
720 1.12 briggs }
721 1.12 briggs
722 1.12 briggs DELAY(1);
723 1.26 briggs if (i++ > 1000000)
724 1.50 tsutsui panic("%s: Bus error, but no condition! Argh!",
725 1.50 tsutsui __func__);
726 1.12 briggs }
727 1.12 briggs goto restart_dmago;
728 1.12 briggs }
729 1.12 briggs
730 1.26 briggs len &= ~1;
731 1.26 briggs
732 1.12 briggs statreg = esc->sc_reg + NCR_STAT * 16;
733 1.50 tsutsui pdma = (volatile uint16_t *)(esc->sc_reg + 0x100);
734 1.1 briggs
735 1.26 briggs /*
736 1.26 briggs * These loops are unrolled into assembly for two reasons:
737 1.26 briggs * 1) We can make sure that they are as efficient as possible, and
738 1.26 briggs * 2) (more importantly) we need the address that we are reading
739 1.26 briggs * from or writing to to be in a2.
740 1.26 briggs */
741 1.26 briggs cnt32 = len / 32;
742 1.26 briggs cnt2 = (len % 32) / 2;
743 1.12 briggs if (esc->sc_datain == 0) {
744 1.26 briggs /* while (cnt32--) { 16 instances of *pdma = *addr++; } */
745 1.26 briggs /* while (cnt2--) { *pdma = *addr++; } */
746 1.42 perry __asm volatile (
747 1.31 thorpej " movl %1, %%a2 \n"
748 1.31 thorpej " movl %2, %%a3 \n"
749 1.31 thorpej " movw %3, %%d2 \n"
750 1.31 thorpej " cmpw #0, %%d2 \n"
751 1.31 thorpej " beq 2f \n"
752 1.31 thorpej " subql #1, %%d2 \n"
753 1.31 thorpej "1: movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
754 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
755 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
756 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
757 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
758 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
759 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
760 1.31 thorpej " movw %%a2@+,%%a3@; movw %%a2@+,%%a3@ \n"
761 1.31 thorpej " movw #8704,%%sr \n"
762 1.31 thorpej " movw #9728,%%sr \n"
763 1.31 thorpej " dbra %%d2, 1b \n"
764 1.31 thorpej "2: movw %4, %%d2 \n"
765 1.31 thorpej " cmpw #0, %%d2 \n"
766 1.31 thorpej " beq 4f \n"
767 1.31 thorpej " subql #1, %%d2 \n"
768 1.31 thorpej "3: movw %%a2@+,%%a3@ \n"
769 1.31 thorpej " dbra %%d2, 3b \n"
770 1.31 thorpej "4: movl %%a2, %0"
771 1.26 briggs : "=g" (addr)
772 1.26 briggs : "0" (addr), "g" (pdma), "g" (cnt32), "g" (cnt2)
773 1.26 briggs : "a2", "a3", "d2");
774 1.13 briggs if (esc->sc_pad) {
775 1.50 tsutsui volatile uint8_t *c;
776 1.50 tsutsui c = (volatile uint8_t *) addr;
777 1.26 briggs /* Wait for DREQ */
778 1.26 briggs while (!esp_have_dreq(esc)) {
779 1.26 briggs if (*statreg & 0x80) {
780 1.50 tsutsui nofault = NULL;
781 1.26 briggs goto gotintr;
782 1.26 briggs }
783 1.26 briggs }
784 1.50 tsutsui *(volatile int8_t *)pdma = *c;
785 1.13 briggs }
786 1.12 briggs } else {
787 1.26 briggs /* while (cnt32--) { 16 instances of *addr++ = *pdma; } */
788 1.26 briggs /* while (cnt2--) { *addr++ = *pdma; } */
789 1.42 perry __asm volatile (
790 1.31 thorpej " movl %1, %%a2 \n"
791 1.31 thorpej " movl %2, %%a3 \n"
792 1.31 thorpej " movw %3, %%d2 \n"
793 1.31 thorpej " cmpw #0, %%d2 \n"
794 1.31 thorpej " beq 6f \n"
795 1.31 thorpej " subql #1, %%d2 \n"
796 1.31 thorpej "5: movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
797 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
798 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
799 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
800 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
801 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
802 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
803 1.31 thorpej " movw %%a3@,%%a2@+; movw %%a3@,%%a2@+ \n"
804 1.31 thorpej " movw #8704,%%sr \n"
805 1.31 thorpej " movw #9728,%%sr \n"
806 1.31 thorpej " dbra %%d2, 5b \n"
807 1.31 thorpej "6: movw %4, %%d2 \n"
808 1.31 thorpej " cmpw #0, %%d2 \n"
809 1.31 thorpej " beq 8f \n"
810 1.31 thorpej " subql #1, %%d2 \n"
811 1.31 thorpej "7: movw %%a3@,%%a2@+ \n"
812 1.31 thorpej " dbra %%d2, 7b \n"
813 1.31 thorpej "8: movl %%a2, %0"
814 1.26 briggs : "=g" (addr)
815 1.26 briggs : "0" (addr), "g" (pdma), "g" (cnt32), "g" (cnt2)
816 1.26 briggs : "a2", "a3", "d2");
817 1.13 briggs if (esc->sc_pad) {
818 1.50 tsutsui volatile uint8_t *c;
819 1.50 tsutsui c = (volatile int8_t *)addr;
820 1.26 briggs /* Wait for DREQ */
821 1.26 briggs while (!esp_have_dreq(esc)) {
822 1.26 briggs if (*statreg & 0x80) {
823 1.50 tsutsui nofault = NULL;
824 1.26 briggs goto gotintr;
825 1.26 briggs }
826 1.26 briggs }
827 1.50 tsutsui *c = *(volatile uint8_t *)pdma;
828 1.12 briggs }
829 1.12 briggs }
830 1.12 briggs
831 1.50 tsutsui nofault = NULL;
832 1.12 briggs
833 1.26 briggs /*
834 1.26 briggs * If we have not received an interrupt yet, we should shortly,
835 1.26 briggs * and we can't prevent it, so return and wait for it.
836 1.26 briggs */
837 1.12 briggs if ((*statreg & 0x80) == 0) {
838 1.26 briggs #if DEBUG
839 1.26 briggs if (mac68k_esp_debug) {
840 1.26 briggs printf("g.\n");
841 1.26 briggs }
842 1.26 briggs #endif
843 1.50 tsutsui if (espspl != -1)
844 1.50 tsutsui splx(espspl);
845 1.50 tsutsui espspl = -1;
846 1.12 briggs return;
847 1.12 briggs }
848 1.12 briggs
849 1.12 briggs gotintr:
850 1.26 briggs #if DEBUG
851 1.26 briggs if (mac68k_esp_debug) {
852 1.26 briggs printf("g!\n");
853 1.26 briggs }
854 1.26 briggs #endif
855 1.51 hauke /*
856 1.51 hauke * We have been called from the MI ncr53c9x_intr() handler,
857 1.51 hauke * which protects itself against multiple invocation with a
858 1.55 rmind * lock. Follow the example of ncr53c9x_poll().
859 1.51 hauke */
860 1.54 uebayasi mutex_exit(&sc->sc_lock);
861 1.12 briggs ncr53c9x_intr(sc);
862 1.54 uebayasi mutex_enter(&sc->sc_lock);
863 1.50 tsutsui if (espspl != -1)
864 1.50 tsutsui splx(espspl);
865 1.50 tsutsui espspl = -1;
866 1.16 briggs }
867 1.16 briggs
868 1.57 rin static void
869 1.37 chs esp_intr(void *sc)
870 1.23 briggs {
871 1.23 briggs struct esp_softc *esc = (struct esp_softc *)sc;
872 1.23 briggs
873 1.26 briggs if (esc->sc_reg[NCR_STAT * 16] & 0x80) {
874 1.50 tsutsui ncr53c9x_intr((struct ncr53c9x_softc *)esp0);
875 1.26 briggs }
876 1.23 briggs }
877 1.23 briggs
878 1.57 rin static void
879 1.37 chs esp_dualbus_intr(void *sc)
880 1.16 briggs {
881 1.26 briggs if (esp0 && (esp0->sc_reg[NCR_STAT * 16] & 0x80)) {
882 1.50 tsutsui ncr53c9x_intr((struct ncr53c9x_softc *)esp0);
883 1.26 briggs }
884 1.22 briggs
885 1.26 briggs if (esp1 && (esp1->sc_reg[NCR_STAT * 16] & 0x80)) {
886 1.50 tsutsui ncr53c9x_intr((struct ncr53c9x_softc *)esp1);
887 1.26 briggs }
888 1.1 briggs }
889