esp.c revision 1.16 1 /* $NetBSD: esp.c,v 1.16 1999/02/02 12:46:13 dbj Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1994 Peter Galbavy
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Peter Galbavy
55 * 4. The name of the author may not be used to endorse or promote products
56 * derived from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
62 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
63 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
64 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
66 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
67 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * Based on aic6360 by Jarle Greipsland
73 *
74 * Acknowledgements: Many of the algorithms used in this driver are
75 * inspired by the work of Julian Elischer (julian (at) tfs.com) and
76 * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu). Thanks a million!
77 */
78
79 /*
80 * Grabbed from the sparc port at revision 1.73 for the NeXT.
81 * Darrin B. Jewell <dbj (at) netbsd.org> Sat Jul 4 15:41:32 1998
82 */
83
84 #include <sys/types.h>
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/errno.h>
89 #include <sys/ioctl.h>
90 #include <sys/device.h>
91 #include <sys/buf.h>
92 #include <sys/proc.h>
93 #include <sys/user.h>
94 #include <sys/queue.h>
95
96 #include <dev/scsipi/scsi_all.h>
97 #include <dev/scsipi/scsipi_all.h>
98 #include <dev/scsipi/scsiconf.h>
99 #include <dev/scsipi/scsi_message.h>
100
101 #include <machine/bus.h>
102 #include <machine/autoconf.h>
103 #include <machine/cpu.h>
104
105 #include <dev/ic/ncr53c9xreg.h>
106 #include <dev/ic/ncr53c9xvar.h>
107
108 #include <next68k/next68k/isr.h>
109
110 #include <next68k/dev/nextdmareg.h>
111 #include <next68k/dev/nextdmavar.h>
112
113 #include "espreg.h"
114 #include "espvar.h"
115
116 #if 1
117 #define ESP_DEBUG
118 #endif
119
120 #ifdef ESP_DEBUG
121 int esp_debug = 0;
122 #define DPRINTF(x) if (esp_debug) printf x;
123 #else
124 #define DPRINTF(x)
125 #endif
126
127
128 void espattach_intio __P((struct device *, struct device *, void *));
129 int espmatch_intio __P((struct device *, struct cfdata *, void *));
130
131 /* DMA callbacks */
132 bus_dmamap_t esp_dmacb_continue __P((void *arg));
133 void esp_dmacb_completed __P((bus_dmamap_t map, void *arg));
134 void esp_dmacb_shutdown __P((void *arg));
135
136 /* Linkup to the rest of the kernel */
137 struct cfattach esp_ca = {
138 sizeof(struct esp_softc), espmatch_intio, espattach_intio
139 };
140
141 struct scsipi_device esp_dev = {
142 NULL, /* Use default error handler */
143 NULL, /* have a queue, served by this */
144 NULL, /* have no async handler */
145 NULL, /* Use default 'done' routine */
146 };
147
148 /*
149 * Functions and the switch for the MI code.
150 */
151 u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
152 void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
153 int esp_dma_isintr __P((struct ncr53c9x_softc *));
154 void esp_dma_reset __P((struct ncr53c9x_softc *));
155 int esp_dma_intr __P((struct ncr53c9x_softc *));
156 int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
157 size_t *, int, size_t *));
158 void esp_dma_go __P((struct ncr53c9x_softc *));
159 void esp_dma_stop __P((struct ncr53c9x_softc *));
160 int esp_dma_isactive __P((struct ncr53c9x_softc *));
161
162 struct ncr53c9x_glue esp_glue = {
163 esp_read_reg,
164 esp_write_reg,
165 esp_dma_isintr,
166 esp_dma_reset,
167 esp_dma_intr,
168 esp_dma_setup,
169 esp_dma_go,
170 esp_dma_stop,
171 esp_dma_isactive,
172 NULL, /* gl_clear_latched_intr */
173 };
174
175 #ifdef ESP_DEBUG
176 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
177 static void
178 esp_hex_dump(unsigned char *pkt, size_t len)
179 {
180 size_t i, j;
181
182 printf("0000: ");
183 for(i=0; i<len; i++) {
184 printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
185 if ((i+1) % 16 == 0) {
186 printf(" %c", '"');
187 for(j=0; j<16; j++)
188 printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
189 printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
190 XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
191 }
192 }
193 printf("\n");
194 }
195 #endif
196
197 int
198 espmatch_intio(parent, cf, aux)
199 struct device *parent;
200 struct cfdata *cf;
201 void *aux;
202 {
203 /* should probably probe here */
204 /* Should also probably set up data from config */
205
206 return(1);
207 }
208
209 void
210 espattach_intio(parent, self, aux)
211 struct device *parent, *self;
212 void *aux;
213 {
214 struct esp_softc *esc = (void *)self;
215 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
216
217 esc->sc_bst = NEXT68K_INTIO_BUS_SPACE;
218 if (bus_space_map(esc->sc_bst, NEXT_P_SCSI,
219 ESP_DEVICE_SIZE, 0, &esc->sc_bsh)) {
220 panic("\n%s: can't map ncr53c90 registers",
221 sc->sc_dev.dv_xname);
222 }
223
224 sc->sc_id = 7;
225 sc->sc_freq = 20; /* Mhz */
226
227 /*
228 * Set up glue for MI code early; we use some of it here.
229 */
230 sc->sc_glue = &esp_glue;
231
232 /*
233 * XXX More of this should be in ncr53c9x_attach(), but
234 * XXX should we really poke around the chip that much in
235 * XXX the MI code? Think about this more...
236 */
237
238 /*
239 * It is necessary to try to load the 2nd config register here,
240 * to find out what rev the esp chip is, else the ncr53c9x_reset
241 * will not set up the defaults correctly.
242 */
243 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
244 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
245 sc->sc_cfg3 = NCRCFG3_CDB;
246 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
247
248 if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
249 (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
250 sc->sc_rev = NCR_VARIANT_ESP100;
251 } else {
252 sc->sc_cfg2 = NCRCFG2_SCSI2;
253 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
254 sc->sc_cfg3 = 0;
255 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
256 sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
257 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
258 if (NCR_READ_REG(sc, NCR_CFG3) !=
259 (NCRCFG3_CDB | NCRCFG3_FCLK)) {
260 sc->sc_rev = NCR_VARIANT_ESP100A;
261 } else {
262 /* NCRCFG2_FE enables > 64K transfers */
263 sc->sc_cfg2 |= NCRCFG2_FE;
264 sc->sc_cfg3 = 0;
265 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
266 sc->sc_rev = NCR_VARIANT_ESP200;
267 }
268 }
269
270 /*
271 * XXX minsync and maxxfer _should_ be set up in MI code,
272 * XXX but it appears to have some dependency on what sort
273 * XXX of DMA we're hooked up to, etc.
274 */
275
276 /*
277 * This is the value used to start sync negotiations
278 * Note that the NCR register "SYNCTP" is programmed
279 * in "clocks per byte", and has a minimum value of 4.
280 * The SCSI period used in negotiation is one-fourth
281 * of the time (in nanoseconds) needed to transfer one byte.
282 * Since the chip's clock is given in MHz, we have the following
283 * formula: 4 * period = (1000 / freq) * 4
284 */
285 sc->sc_minsync = 1000 / sc->sc_freq;
286
287 /*
288 * Alas, we must now modify the value a bit, because it's
289 * only valid when can switch on FASTCLK and FASTSCSI bits
290 * in config register 3...
291 */
292 switch (sc->sc_rev) {
293 case NCR_VARIANT_ESP100:
294 sc->sc_maxxfer = 64 * 1024;
295 sc->sc_minsync = 0; /* No synch on old chip? */
296 break;
297
298 case NCR_VARIANT_ESP100A:
299 sc->sc_maxxfer = 64 * 1024;
300 /* Min clocks/byte is 5 */
301 sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
302 break;
303
304 case NCR_VARIANT_ESP200:
305 sc->sc_maxxfer = 16 * 1024 * 1024;
306 /* XXX - do actually set FAST* bits */
307 break;
308 }
309
310 /* @@@ Some ESP_DCTL bits probably need setting */
311 NCR_WRITE_REG(sc, ESP_DCTL,
312 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_RESET);
313 DELAY(10);
314 NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_20MHZ | ESPDCTL_INTENB);
315 DELAY(10);
316
317 /* Set up SCSI DMA */
318 {
319 esc->sc_scsi_dma.nd_bst = NEXT68K_INTIO_BUS_SPACE;
320
321 if (bus_space_map(esc->sc_scsi_dma.nd_bst, NEXT_P_SCSI_CSR,
322 sizeof(struct dma_dev),0, &esc->sc_scsi_dma.nd_bsh)) {
323 panic("\n%s: can't map scsi DMA registers",
324 sc->sc_dev.dv_xname);
325 }
326
327 esc->sc_scsi_dma.nd_intr = NEXT_I_SCSI_DMA;
328 esc->sc_scsi_dma.nd_shutdown_cb = &esp_dmacb_shutdown;
329 esc->sc_scsi_dma.nd_continue_cb = &esp_dmacb_continue;
330 esc->sc_scsi_dma.nd_completed_cb = &esp_dmacb_completed;
331 esc->sc_scsi_dma.nd_cb_arg = sc;
332 nextdma_config(&esc->sc_scsi_dma);
333 nextdma_init(&esc->sc_scsi_dma);
334
335 {
336 int error;
337 if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
338 sc->sc_maxxfer, 1, sc->sc_maxxfer,
339 0, BUS_DMA_ALLOCNOW, &esc->sc_dmamap)) != 0) {
340 panic("%s: can't create i/o DMA map, error = %d",
341 sc->sc_dev.dv_xname,error);
342 }
343 }
344
345 {
346 int error;
347 if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
348 ESP_DMA_MAXTAIL, 1, ESP_DMA_MAXTAIL,
349 0, BUS_DMA_ALLOCNOW, &esc->sc_tail_dmamap)) != 0) {
350 panic("%s: can't create tail i/o DMA map, error = %d",
351 sc->sc_dev.dv_xname,error);
352 }
353 }
354 }
355
356 #if 0
357 /* Turn on target selection using the `dma' method */
358 ncr53c9x_dmaselect = 1;
359 #else
360 ncr53c9x_dmaselect = 0;
361 #endif
362
363 esc->sc_datain = -1;
364 esc->sc_slop_bgn_addr = 0;
365 esc->sc_slop_bgn_size = 0;
366 esc->sc_slop_end_addr = 0;
367 esc->sc_slop_end_size = 0;
368 esc->sc_dmamap_loaded = 0;
369 esc->sc_tail = 0;
370 esc->sc_tail_size = 0;
371
372 /* Establish interrupt channel */
373 isrlink_autovec((int(*)__P((void*)))ncr53c9x_intr, sc,
374 NEXT_I_IPL(NEXT_I_SCSI), 0);
375 INTR_ENABLE(NEXT_I_SCSI);
376
377 /* register interrupt stats */
378 evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
379
380 /* Do the common parts of attachment. */
381 sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
382 sc->sc_adapter.scsipi_minphys = minphys;
383 ncr53c9x_attach(sc, &esp_dev);
384 }
385
386 /*
387 * Glue functions.
388 */
389
390 u_char
391 esp_read_reg(sc, reg)
392 struct ncr53c9x_softc *sc;
393 int reg;
394 {
395 struct esp_softc *esc = (struct esp_softc *)sc;
396
397 return(bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg));
398 }
399
400 void
401 esp_write_reg(sc, reg, val)
402 struct ncr53c9x_softc *sc;
403 int reg;
404 u_char val;
405 {
406 struct esp_softc *esc = (struct esp_softc *)sc;
407
408 bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg, val);
409 }
410
411 int
412 esp_dma_isintr(sc)
413 struct ncr53c9x_softc *sc;
414 {
415 struct esp_softc *esc = (struct esp_softc *)sc;
416
417 int r = (INTR_OCCURRED(NEXT_I_SCSI));
418
419 if (r) {
420 DPRINTF(("esp_dma_isintr = 0x%b\n",
421 (*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS));
422
423 if (esp_dma_isactive(sc)) {
424 if (esc->sc_datain) {
425 NCR_WRITE_REG(sc, ESP_DCTL,
426 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD | ESPDCTL_FLUSH);
427 NCR_WRITE_REG(sc, ESP_DCTL,
428 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
429 } else {
430 NCR_WRITE_REG(sc, ESP_DCTL,
431 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_FLUSH);
432 NCR_WRITE_REG(sc, ESP_DCTL,
433 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
434 }
435 {
436 int nr;
437 nr = nextdma_intr(&esc->sc_scsi_dma);
438 if (nr) {
439 DPRINTF(("nextma_intr = %d\n",nr));
440 }
441 }
442 return 0;
443 }
444
445 /* Clear the DMAMOD bit in the DCTL register, since if this
446 * routine returns true, then the ncr53c9x_intr handler will
447 * be called and needs access to the scsi registers.
448 */
449 if (esc->sc_datain) {
450 NCR_WRITE_REG(sc, ESP_DCTL,
451 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
452 } else {
453 NCR_WRITE_REG(sc, ESP_DCTL,
454 ESPDCTL_20MHZ | ESPDCTL_INTENB);
455 }
456
457 }
458
459 return (r);
460 }
461
462 void
463 esp_dma_reset(sc)
464 struct ncr53c9x_softc *sc;
465 {
466 struct esp_softc *esc = (struct esp_softc *)sc;
467
468 DPRINTF(("esp dma reset\n"));
469
470 #ifdef ESP_DEBUG
471 if (esp_debug) {
472 printf(" *intrstat = 0x%b\n",
473 (*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS);
474 printf(" *intrmask = 0x%b\n",
475 (*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),NEXT_INTR_BITS);
476 }
477 #endif
478
479
480 /* Clear the DMAMOD bit in the DCTL register: */
481 if (esc->sc_datain) {
482 NCR_WRITE_REG(sc, ESP_DCTL,
483 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
484 } else {
485 NCR_WRITE_REG(sc, ESP_DCTL,
486 ESPDCTL_20MHZ | ESPDCTL_INTENB);
487 }
488
489 nextdma_reset(&esc->sc_scsi_dma);
490
491 if (esc->sc_dmamap_loaded) {
492 /* fixing this is slightly complicated since multiple maps may be loaded,
493 * and the esc->sc_dmamap_loaded variable only indicates the most recent one.
494 */
495 panic("invoking completed callbacks upon esp_dma_reset is not yet implemented");
496
497 esp_dmacb_completed(esc->sc_dmamap,sc);
498 esp_dmacb_completed(esc->sc_tail_dmamap,sc);
499 }
500
501 esp_dmacb_shutdown(sc); /* this will clean up */
502 }
503
504 int
505 esp_dma_intr(sc)
506 struct ncr53c9x_softc *sc;
507 {
508 int trans;
509 int resid;
510 int datain;
511 struct esp_softc *esc = (struct esp_softc *)sc;
512
513 datain = esc->sc_datain;
514
515 DPRINTF(("esp_dma_intr resetting dma\n"));
516
517 /* If the dma hasn't finished when we are in a scsi
518 * interrupt. Then, "Houston, we have a problem."
519 * Stop DMA and figure out how many bytes were transferred
520 */
521 esp_dma_reset(sc);
522
523 resid = 0;
524
525 /*
526 * If a transfer onto the SCSI bus gets interrupted by the device
527 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
528 * as residual since the ESP counter registers get decremented as
529 * bytes are clocked into the FIFO.
530 */
531
532 if (! datain) {
533 resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
534 if (resid) {
535 NCR_DMA(("dmaintr: empty esp FIFO of %d ", resid));
536 NCRCMD(sc, NCRCMD_FLUSH);
537 DELAY(1);
538 }
539 }
540
541 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
542 /*
543 * `Terminal count' is off, so read the residue
544 * out of the ESP counter registers.
545 */
546 resid += (NCR_READ_REG(sc, NCR_TCL) |
547 (NCR_READ_REG(sc, NCR_TCM) << 8) |
548 ((sc->sc_cfg2 & NCRCFG2_FE)
549 ? (NCR_READ_REG(sc, NCR_TCH) << 16)
550 : 0));
551
552 if (resid == 0 && esc->sc_dmasize == 65536 &&
553 (sc->sc_cfg2 & NCRCFG2_FE) == 0)
554 /* A transfer of 64K is encoded as `TCL=TCM=0' */
555 resid = 65536;
556 }
557
558 trans = esc->sc_dmasize - resid;
559 if (trans < 0) { /* transferred < 0 ? */
560 #if 0
561 /*
562 * This situation can happen in perfectly normal operation
563 * if the ESP is reselected while using DMA to select
564 * another target. As such, don't print the warning.
565 */
566 printf("%s: xfer (%d) > req (%d)\n",
567 esc->sc_dev.dv_xname, trans, esc->sc_dmasize);
568 #endif
569 trans = esc->sc_dmasize;
570 }
571
572 NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
573 NCR_READ_REG(sc, NCR_TCL),
574 NCR_READ_REG(sc, NCR_TCM),
575 (sc->sc_cfg2 & NCRCFG2_FE)
576 ? NCR_READ_REG(sc, NCR_TCH) : 0,
577 trans, resid));
578
579 #ifdef ESP_DEBUG
580 if (esp_debug) esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
581 #endif
582
583 *esc->sc_dmalen -= trans;
584 *esc->sc_dmaaddr += trans;
585
586 return 0;
587 }
588
589 int
590 esp_dma_setup(sc, addr, len, datain, dmasize)
591 struct ncr53c9x_softc *sc;
592 caddr_t *addr;
593 size_t *len;
594 int datain;
595 size_t *dmasize;
596 {
597 struct esp_softc *esc = (struct esp_softc *)sc;
598
599 #ifdef DIAGNOSTIC
600 /* if this is a read DMA, pre-fill the buffer with 0xdeadbeef
601 * to identify bogus reads
602 */
603 if (datain) {
604 int *v = (int *)(*addr);
605 int i;
606 for(i=0;i<((*len)/4);i++) v[i] = 0xdeadbeef;
607 }
608 #endif
609
610 DPRINTF(("esp_dma_setup(0x%08lx,0x%08lx,0x%08lx)\n",*addr,*len,*dmasize));
611
612 #ifdef DIAGNOSTIC /* @@@ this is ok sometimes. verify that we handle it ok
613 * and then remove this check
614 */
615 if (*len != *dmasize) {
616 panic("esp dmalen != size");
617 }
618 #endif
619
620 #ifdef DIAGNOSTIC
621 if ((esc->sc_datain != -1) ||
622 (esc->sc_dmamap->dm_mapsize != 0) ||
623 (esc->sc_dmamap_loaded != 0)) {
624 panic("%s: map already loaded in esp_dma_setup\n"
625 "\tdatain = %d\n\tmapsize=%d\n\tloaed = %d",
626 sc->sc_dev.dv_xname,esc->sc_datain,esc->sc_dmamap->dm_mapsize,
627 esc->sc_dmamap_loaded);
628 }
629 #endif
630
631 /* Save these in case we have to abort DMA */
632 esc->sc_datain = datain;
633 esc->sc_dmaaddr = addr;
634 esc->sc_dmalen = len;
635 esc->sc_dmasize = *dmasize;
636
637 /* Deal with DMA alignment issues, by stuffing the FIFO.
638 * This assumes that if bus_dmamap_load is given an aligned
639 * buffer, then it will generate aligned hardware addresses
640 * to give to the device. Perhaps that is not a good assumption,
641 * but it is probably true. [dbj (at) netbsd.org:19980719.0135EDT]
642 */
643 {
644 int slop_bgn_size; /* # bytes to be fifo'd at beginning */
645 int slop_end_size; /* # bytes to be fifo'd at end */
646
647 {
648 u_long bgn = (u_long)(*esc->sc_dmaaddr);
649 u_long end = (u_long)(*esc->sc_dmaaddr+esc->sc_dmasize);
650
651 slop_bgn_size = DMA_BEGINALIGNMENT-(bgn % DMA_BEGINALIGNMENT);
652 if (slop_bgn_size == DMA_BEGINALIGNMENT) slop_bgn_size = 0;
653 slop_end_size = end % DMA_ENDALIGNMENT;
654 }
655
656 /* Check to make sure we haven't counted extra slop
657 * as would happen for a very short dma buffer, also
658 * for short buffers, just stuff the entire thing in the tail
659 */
660 if ((slop_bgn_size+slop_end_size >= esc->sc_dmasize) ||
661 (esc->sc_dmasize <= ESP_DMA_MAXTAIL)) {
662 slop_bgn_size = 0;
663 slop_end_size = esc->sc_dmasize;
664 } else {
665 panic("Chaining DMA interrupts are currently broken.\n"); /* @@@ */
666 }
667
668 esc->sc_slop_bgn_addr = *esc->sc_dmaaddr;
669 esc->sc_slop_bgn_size = slop_bgn_size;
670 esc->sc_slop_end_addr = (*esc->sc_dmaaddr+esc->sc_dmasize)-slop_end_size;
671 esc->sc_slop_end_size = slop_end_size;
672 }
673
674 /* Load the normal DMA map */
675 if (esc->sc_dmasize-(esc->sc_slop_bgn_size+esc->sc_slop_end_size)) {
676 int error;
677 error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
678 esc->sc_dmamap,
679 *esc->sc_dmaaddr+esc->sc_slop_bgn_size,
680 esc->sc_dmasize-(esc->sc_slop_bgn_size+esc->sc_slop_end_size),
681 NULL, BUS_DMA_NOWAIT);
682 if (error) {
683 panic("%s: can't load dma map. error = %d",
684 sc->sc_dev.dv_xname, error);
685 }
686 }
687
688 /* Now set up the tail dma buffer, including alignment. */
689 if (esc->sc_slop_end_size) {
690 esc->sc_tail = DMA_ENDALIGN(caddr_t,esc->sc_tailbuf+esc->sc_slop_end_size)-esc->sc_slop_end_size;
691 /* If the beginning of the tail is not correctly aligned,
692 * we have no choice but to align the start, which might then unalign the end.
693 */
694 esc->sc_tail = DMA_ALIGN(caddr_t,esc->sc_tail);
695 /* So therefore, we change the tail size to be end aligned again. */
696 esc->sc_tail_size = DMA_ENDALIGN(caddr_t,esc->sc_tail+esc->sc_slop_end_size)-esc->sc_tail;
697
698 {
699 int error;
700 error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
701 esc->sc_tail_dmamap,
702 esc->sc_tail, esc->sc_tail_size,
703 NULL, BUS_DMA_NOWAIT);
704 if (error) {
705 panic("%s: can't load dma map. error = %d",
706 sc->sc_dev.dv_xname, error);
707 }
708 }
709 } else {
710 esc->sc_tail = 0;
711 esc->sc_tail_size = 0;
712 }
713
714 return (0);
715 }
716
717 void
718 esp_dma_go(sc)
719 struct ncr53c9x_softc *sc;
720 {
721 struct esp_softc *esc = (struct esp_softc *)sc;
722
723 DPRINTF(("esp_dma_go(datain = %d)\n",esc->sc_datain));
724
725 DPRINTF(("\tbgn slop = %d\n\tend slop = %d\n\tmapsize = %d\n",
726 esc->sc_slop_bgn_size,esc->sc_slop_end_size,
727 esc->sc_dmamap->dm_mapsize));
728
729 #ifdef DIAGNOSTIC
730 {
731 int n = NCR_READ_REG(sc, NCR_FFLAG);
732 DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
733 }
734 #endif
735
736 if (esc->sc_datain) {
737 int i;
738 for(i=0;i<esc->sc_slop_bgn_size;i++) {
739 esc->sc_slop_bgn_addr[i]=NCR_READ_REG(sc, NCR_FIFO);
740 }
741 } else {
742 int i;
743 for(i=0;i<esc->sc_slop_bgn_size;i++) {
744 NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_slop_bgn_addr[i]);
745 }
746 }
747
748 #ifdef DIAGNOSTIC
749 {
750 int n = NCR_READ_REG(sc, NCR_FFLAG);
751 DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
752 }
753 #endif
754
755 #if defined(DIAGNOSTIC)
756 if ((esc->sc_dmamap->dm_mapsize == 0) && (esc->sc_tail_dmamap->dm_mapsize == 0)) {
757 panic("%s: No DMA requested!");
758 }
759 #endif
760
761 /* if we are a dma write cycle, copy the end slop */
762 if (esc->sc_datain == 0) {
763 memcpy(esc->sc_tail,esc->sc_slop_end_addr,esc->sc_slop_end_size);
764 }
765
766 nextdma_start(&esc->sc_scsi_dma,
767 (esc->sc_datain ? DMACSR_READ : DMACSR_WRITE));
768
769 if (esc->sc_datain) {
770 NCR_WRITE_REG(sc, ESP_DCTL,
771 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
772 } else {
773 NCR_WRITE_REG(sc, ESP_DCTL,
774 ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
775 }
776 }
777
778 void
779 esp_dma_stop(sc)
780 struct ncr53c9x_softc *sc;
781 {
782 panic("Not yet implemented");
783 }
784
785 int
786 esp_dma_isactive(sc)
787 struct ncr53c9x_softc *sc;
788 {
789 struct esp_softc *esc = (struct esp_softc *)sc;
790 int r = !nextdma_finished(&esc->sc_scsi_dma);
791 DPRINTF(("esp_dma_isactive = %d\n",r));
792 return(r);
793 }
794
795 /****************************************************************/
796
797 /* Internal dma callback routines */
798 bus_dmamap_t
799 esp_dmacb_continue(arg)
800 void *arg;
801 {
802 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
803 struct esp_softc *esc = (struct esp_softc *)sc;
804
805 DPRINTF(("esp dma continue\n"));
806
807 #ifdef DIAGNOSTIC
808 if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
809 panic("%s: map not loaded in dma continue callback, datain = %d",
810 sc->sc_dev.dv_xname,esc->sc_datain);
811 }
812 #endif
813 switch(esc->sc_dmamap_loaded) {
814 case 0:
815 if (esc->sc_dmamap->dm_mapsize) {
816 bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap,
817 0, esc->sc_dmamap->dm_mapsize,
818 (esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
819 esc->sc_dmamap_loaded = 1;
820 DPRINTF(("Loading primary map\n"));
821 return(esc->sc_dmamap);
822 }
823 /* Fallthrough */
824 case 1:
825 if (esc->sc_tail_dmamap->dm_mapsize) {
826 bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
827 0, esc->sc_tail_dmamap->dm_mapsize,
828 (esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
829 esc->sc_dmamap_loaded = 2;
830 DPRINTF(("Loading tail map\n"));
831 return(esc->sc_tail_dmamap);
832 }
833 /* Fallthrough */
834 case 2:
835 DPRINTF(("Not loading map\n"));
836 return(0);
837 default:
838 panic("%s: Unexpected sc_dmamap_loaded (%d) in continue_cb",
839 sc->sc_dev.dv_xname,esc->sc_dmamap_loaded);
840 }
841 }
842
843
844 void
845 esp_dmacb_completed(map, arg)
846 bus_dmamap_t map;
847 void *arg;
848 {
849 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
850 struct esp_softc *esc = (struct esp_softc *)sc;
851
852 DPRINTF(("esp dma completed\n"));
853
854 #ifdef DIAGNOSTIC
855 if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
856 panic("%s: map not loaded in dma completed callback, datain = %d, loaded = %d",
857 sc->sc_dev.dv_xname,esc->sc_datain,esc->sc_dmamap_loaded);
858 }
859 if ((map != esc->sc_dmamap) && (map != esc->sc_tail_dmamap)) {
860 panic("%s: unexpected completed map", sc->sc_dev.dv_xname);
861 }
862 #endif
863
864 bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, map,
865 0, map->dm_mapsize,
866 (esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
867
868 }
869
870 void
871 esp_dmacb_shutdown(arg)
872 void *arg;
873 {
874 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
875 struct esp_softc *esc = (struct esp_softc *)sc;
876
877 DPRINTF(("esp dma shutdown\n"));
878
879 /* Stuff the end slop into fifo */
880
881 #ifdef ESP_DEBUG
882 if (esp_debug) {
883
884 int n = NCR_READ_REG(sc, NCR_FFLAG);
885 DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
886
887 NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d\n",
888 NCR_READ_REG(sc, NCR_TCL),
889 NCR_READ_REG(sc, NCR_TCM),
890 (sc->sc_cfg2 & NCRCFG2_FE)
891 ? NCR_READ_REG(sc, NCR_TCH) : 0));
892 }
893 #endif
894
895 if (esc->sc_dmamap->dm_mapsize) {
896 bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap);
897 }
898 if (esc->sc_tail_dmamap->dm_mapsize) {
899 bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap);
900 }
901
902 if (esc->sc_datain == 1) {
903 memcpy(esc->sc_slop_end_addr,esc->sc_tail,esc->sc_slop_end_size);
904 }
905
906 #ifdef ESP_DEBUG
907 if (esp_debug) esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
908 #endif
909
910 esc->sc_datain = -1;
911 esc->sc_slop_bgn_addr = 0;
912 esc->sc_slop_bgn_size = 0;
913 esc->sc_slop_end_addr = 0;
914 esc->sc_slop_end_size = 0;
915 esc->sc_dmamap_loaded = 0;
916 esc->sc_tail = 0;
917 esc->sc_tail_size = 0;
918 }
919