sw.c revision 1.3.4.3 1 1.3.4.3 bouyer /* $NetBSD: sw.c,v 1.3.4.3 2001/01/22 18:26:20 bouyer Exp $ */
2 1.3.4.2 bouyer
3 1.3.4.2 bouyer /*-
4 1.3.4.2 bouyer * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.3.4.2 bouyer * All rights reserved.
6 1.3.4.2 bouyer *
7 1.3.4.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 bouyer * by Adam Glass, David Jones, Gordon W. Ross, and Jason R. Thorpe.
9 1.3.4.2 bouyer *
10 1.3.4.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 bouyer * modification, are permitted provided that the following conditions
12 1.3.4.2 bouyer * are met:
13 1.3.4.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.3.4.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.3.4.2 bouyer * must display the following acknowledgement:
20 1.3.4.2 bouyer * This product includes software developed by the NetBSD
21 1.3.4.2 bouyer * Foundation, Inc. and its contributors.
22 1.3.4.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.4.2 bouyer * contributors may be used to endorse or promote products derived
24 1.3.4.2 bouyer * from this software without specific prior written permission.
25 1.3.4.2 bouyer *
26 1.3.4.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.4.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.4.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.4.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.4.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.4.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.4.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.4.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.4.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.4.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.4.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.3.4.2 bouyer */
38 1.3.4.2 bouyer
39 1.3.4.2 bouyer /*
40 1.3.4.2 bouyer * This file contains only the machine-dependent parts of the
41 1.3.4.2 bouyer * Sun4 SCSI driver. (Autoconfig stuff and DMA functions.)
42 1.3.4.2 bouyer * The machine-independent parts are in ncr5380sbc.c
43 1.3.4.2 bouyer *
44 1.3.4.2 bouyer * Supported hardware includes:
45 1.3.4.2 bouyer * Sun "SCSI Weird" on OBIO (sw: Sun 4/100-series)
46 1.3.4.2 bouyer * Sun SCSI-3 on VME (si: Sun 4/200-series, others)
47 1.3.4.2 bouyer *
48 1.3.4.2 bouyer * The VME variant has a bit to enable or disable the DMA engine,
49 1.3.4.2 bouyer * but that bit also gates the interrupt line from the NCR5380!
50 1.3.4.2 bouyer * Therefore, in order to get any interrupt from the 5380, (i.e.
51 1.3.4.2 bouyer * for reselect) one must clear the DMA engine transfer count and
52 1.3.4.2 bouyer * then enable DMA. This has the further complication that you
53 1.3.4.2 bouyer * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
54 1.3.4.2 bouyer * we have to turn DMA back off before we even look at the 5380.
55 1.3.4.2 bouyer *
56 1.3.4.2 bouyer * What wonderfully whacky hardware this is!
57 1.3.4.2 bouyer *
58 1.3.4.2 bouyer * David Jones wrote the initial version of this module for NetBSD/sun3,
59 1.3.4.2 bouyer * which included support for the VME adapter only. (no reselection).
60 1.3.4.2 bouyer *
61 1.3.4.2 bouyer * Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
62 1.3.4.2 bouyer * both the VME and OBIO code to support disconnect/reselect.
63 1.3.4.2 bouyer * (Required figuring out the hardware "features" noted above.)
64 1.3.4.2 bouyer *
65 1.3.4.2 bouyer * The autoconfiguration boilerplate came from Adam Glass.
66 1.3.4.2 bouyer *
67 1.3.4.2 bouyer * Jason R. Thorpe ported the autoconfiguration and VME portions to
68 1.3.4.2 bouyer * NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
69 1.3.4.2 bouyer * a wacky OBIO variant of the VME SCSI-3. Many thanks to Chuck Cranor
70 1.3.4.2 bouyer * for lots of helpful tips and suggestions. Thanks also to Paul Kranenburg
71 1.3.4.2 bouyer * and Chris Torek for bits of insight needed along the way. Thanks to
72 1.3.4.2 bouyer * David Gilbert and Andrew Gillham who risked filesystem life-and-limb
73 1.3.4.2 bouyer * for the sake of testing. Andrew Gillham helped work out the bugs
74 1.3.4.2 bouyer * the 4/100 DMA code.
75 1.3.4.2 bouyer */
76 1.3.4.2 bouyer
77 1.3.4.2 bouyer /*
78 1.3.4.2 bouyer * NOTE: support for the 4/100 "SCSI Weird" is not complete! DMA
79 1.3.4.2 bouyer * works, but interrupts (and, thus, reselection) don't. I don't know
80 1.3.4.2 bouyer * why, and I don't have a machine to test this on further.
81 1.3.4.2 bouyer *
82 1.3.4.2 bouyer * DMA, DMA completion interrupts, and reselection work fine on my
83 1.3.4.2 bouyer * 4/260 with modern SCSI-II disks attached. I've had reports of
84 1.3.4.2 bouyer * reselection failing on Sun Shoebox-type configurations where
85 1.3.4.2 bouyer * there are multiple non-SCSI devices behind Emulex or Adaptec
86 1.3.4.2 bouyer * bridges. These devices pre-date the SCSI-I spec, and might not
87 1.3.4.2 bouyer * bahve the way the 5380 code expects. For this reason, only
88 1.3.4.2 bouyer * DMA is enabled by default in this driver.
89 1.3.4.2 bouyer *
90 1.3.4.2 bouyer * Jason R. Thorpe <thorpej (at) NetBSD.ORG>
91 1.3.4.2 bouyer * December 8, 1995
92 1.3.4.2 bouyer */
93 1.3.4.2 bouyer
94 1.3.4.2 bouyer #include "opt_ddb.h"
95 1.3.4.2 bouyer
96 1.3.4.2 bouyer #include <sys/types.h>
97 1.3.4.2 bouyer #include <sys/param.h>
98 1.3.4.2 bouyer #include <sys/systm.h>
99 1.3.4.2 bouyer #include <sys/kernel.h>
100 1.3.4.2 bouyer #include <sys/malloc.h>
101 1.3.4.2 bouyer #include <sys/errno.h>
102 1.3.4.2 bouyer #include <sys/device.h>
103 1.3.4.2 bouyer #include <sys/buf.h>
104 1.3.4.2 bouyer
105 1.3.4.2 bouyer #include <machine/bus.h>
106 1.3.4.2 bouyer #include <machine/intr.h>
107 1.3.4.2 bouyer #include <machine/autoconf.h>
108 1.3.4.2 bouyer
109 1.3.4.2 bouyer #include <dev/scsipi/scsi_all.h>
110 1.3.4.2 bouyer #include <dev/scsipi/scsipi_all.h>
111 1.3.4.2 bouyer #include <dev/scsipi/scsipi_debug.h>
112 1.3.4.2 bouyer #include <dev/scsipi/scsiconf.h>
113 1.3.4.2 bouyer
114 1.3.4.2 bouyer #ifndef DDB
115 1.3.4.2 bouyer #define Debugger()
116 1.3.4.2 bouyer #endif
117 1.3.4.2 bouyer
118 1.3.4.2 bouyer #ifndef DEBUG
119 1.3.4.2 bouyer #define DEBUG XXX
120 1.3.4.2 bouyer #endif
121 1.3.4.2 bouyer
122 1.3.4.2 bouyer #define COUNT_SW_LEFTOVERS XXX /* See sw DMA completion code */
123 1.3.4.2 bouyer
124 1.3.4.2 bouyer #include <dev/ic/ncr5380reg.h>
125 1.3.4.2 bouyer #include <dev/ic/ncr5380var.h>
126 1.3.4.2 bouyer
127 1.3.4.2 bouyer #include <sparc/dev/swreg.h>
128 1.3.4.2 bouyer
129 1.3.4.2 bouyer /*
130 1.3.4.2 bouyer * Transfers smaller than this are done using PIO
131 1.3.4.2 bouyer * (on assumption they're not worth DMA overhead)
132 1.3.4.2 bouyer */
133 1.3.4.2 bouyer #define MIN_DMA_LEN 128
134 1.3.4.2 bouyer
135 1.3.4.2 bouyer /*
136 1.3.4.2 bouyer * Transfers lager than 65535 bytes need to be split-up.
137 1.3.4.2 bouyer * (Some of the FIFO logic has only 16 bits counters.)
138 1.3.4.2 bouyer * Make the size an integer multiple of the page size
139 1.3.4.2 bouyer * to avoid buf/cluster remap problems. (paranoid?)
140 1.3.4.2 bouyer */
141 1.3.4.2 bouyer #define MAX_DMA_LEN 0xE000
142 1.3.4.2 bouyer
143 1.3.4.2 bouyer #ifdef DEBUG
144 1.3.4.2 bouyer int sw_debug = 0;
145 1.3.4.2 bouyer #endif
146 1.3.4.2 bouyer
147 1.3.4.2 bouyer /*
148 1.3.4.2 bouyer * This structure is used to keep track of mapped DMA requests.
149 1.3.4.2 bouyer */
150 1.3.4.2 bouyer struct sw_dma_handle {
151 1.3.4.2 bouyer int dh_flags;
152 1.3.4.2 bouyer #define SIDH_BUSY 0x01 /* This DH is in use */
153 1.3.4.2 bouyer #define SIDH_OUT 0x02 /* DMA does data out (write) */
154 1.3.4.2 bouyer u_char *dh_addr; /* KVA of start of buffer */
155 1.3.4.2 bouyer int dh_maplen; /* Original data length */
156 1.3.4.2 bouyer long dh_startingpa; /* PA of buffer; for "sw" */
157 1.3.4.2 bouyer bus_dmamap_t dh_dmamap;
158 1.3.4.2 bouyer #define dh_dvma dh_dmamap->dm_segs[0].ds_addr /* VA of buffer in DVMA space */
159 1.3.4.2 bouyer };
160 1.3.4.2 bouyer
161 1.3.4.2 bouyer /*
162 1.3.4.2 bouyer * The first structure member has to be the ncr5380_softc
163 1.3.4.2 bouyer * so we can just cast to go back and fourth between them.
164 1.3.4.2 bouyer */
165 1.3.4.2 bouyer struct sw_softc {
166 1.3.4.2 bouyer struct ncr5380_softc ncr_sc;
167 1.3.4.2 bouyer bus_space_tag_t sc_bustag; /* bus tags */
168 1.3.4.2 bouyer bus_dma_tag_t sc_dmatag;
169 1.3.4.2 bouyer
170 1.3.4.2 bouyer struct sw_dma_handle *sc_dma;
171 1.3.4.2 bouyer int sc_xlen; /* length of current DMA segment. */
172 1.3.4.2 bouyer int sc_options; /* options for this instance. */
173 1.3.4.2 bouyer };
174 1.3.4.2 bouyer
175 1.3.4.2 bouyer /*
176 1.3.4.2 bouyer * Options. By default, DMA is enabled and DMA completion interrupts
177 1.3.4.2 bouyer * and reselect are disabled. You may enable additional features
178 1.3.4.2 bouyer * the `flags' directive in your kernel's configuration file.
179 1.3.4.2 bouyer *
180 1.3.4.2 bouyer * Alternatively, you can patch your kernel with DDB or some other
181 1.3.4.2 bouyer * mechanism. The sc_options member of the softc is OR'd with
182 1.3.4.2 bouyer * the value in sw_options.
183 1.3.4.2 bouyer *
184 1.3.4.2 bouyer * On the "sw", interrupts (and thus) reselection don't work, so they're
185 1.3.4.2 bouyer * disabled by default. DMA is still a little dangerous, too.
186 1.3.4.2 bouyer *
187 1.3.4.2 bouyer * Note, there's a separate sw_options to make life easier.
188 1.3.4.2 bouyer */
189 1.3.4.2 bouyer #define SW_ENABLE_DMA 0x01 /* Use DMA (maybe polled) */
190 1.3.4.2 bouyer #define SW_DMA_INTR 0x02 /* DMA completion interrupts */
191 1.3.4.2 bouyer #define SW_DO_RESELECT 0x04 /* Allow disconnect/reselect */
192 1.3.4.2 bouyer #define SW_OPTIONS_MASK (SW_ENABLE_DMA|SW_DMA_INTR|SW_DO_RESELECT)
193 1.3.4.2 bouyer #define SW_OPTIONS_BITS "\10\3RESELECT\2DMA_INTR\1DMA"
194 1.3.4.2 bouyer int sw_options = SW_ENABLE_DMA;
195 1.3.4.2 bouyer
196 1.3.4.2 bouyer static int sw_match __P((struct device *, struct cfdata *, void *));
197 1.3.4.2 bouyer static void sw_attach __P((struct device *, struct device *, void *));
198 1.3.4.2 bouyer static int sw_intr __P((void *));
199 1.3.4.2 bouyer static void sw_reset_adapter __P((struct ncr5380_softc *));
200 1.3.4.2 bouyer static void sw_minphys __P((struct buf *));
201 1.3.4.2 bouyer
202 1.3.4.2 bouyer void sw_dma_alloc __P((struct ncr5380_softc *));
203 1.3.4.2 bouyer void sw_dma_free __P((struct ncr5380_softc *));
204 1.3.4.2 bouyer void sw_dma_poll __P((struct ncr5380_softc *));
205 1.3.4.2 bouyer
206 1.3.4.2 bouyer void sw_dma_setup __P((struct ncr5380_softc *));
207 1.3.4.2 bouyer void sw_dma_start __P((struct ncr5380_softc *));
208 1.3.4.2 bouyer void sw_dma_eop __P((struct ncr5380_softc *));
209 1.3.4.2 bouyer void sw_dma_stop __P((struct ncr5380_softc *));
210 1.3.4.2 bouyer
211 1.3.4.2 bouyer void sw_intr_on __P((struct ncr5380_softc *));
212 1.3.4.2 bouyer void sw_intr_off __P((struct ncr5380_softc *));
213 1.3.4.2 bouyer
214 1.3.4.2 bouyer /* Shorthand bus space access */
215 1.3.4.2 bouyer #define SWREG_READ(sc, index) \
216 1.3.4.2 bouyer bus_space_read_4((sc)->sc_regt, (sc)->sc_regh, index)
217 1.3.4.2 bouyer #define SWREG_WRITE(sc, index, v) \
218 1.3.4.2 bouyer bus_space_write_4((sc)->sc_regt, (sc)->sc_regh, index, v)
219 1.3.4.2 bouyer
220 1.3.4.2 bouyer
221 1.3.4.2 bouyer /* The Sun "SCSI Weird" 4/100 obio controller. */
222 1.3.4.2 bouyer struct cfattach sw_ca = {
223 1.3.4.2 bouyer sizeof(struct sw_softc), sw_match, sw_attach
224 1.3.4.2 bouyer };
225 1.3.4.2 bouyer
226 1.3.4.2 bouyer static int
227 1.3.4.2 bouyer sw_match(parent, cf, aux)
228 1.3.4.2 bouyer struct device *parent;
229 1.3.4.2 bouyer struct cfdata *cf;
230 1.3.4.2 bouyer void *aux;
231 1.3.4.2 bouyer {
232 1.3.4.2 bouyer union obio_attach_args *uoba = aux;
233 1.3.4.2 bouyer struct obio4_attach_args *oba;
234 1.3.4.2 bouyer
235 1.3.4.2 bouyer /* Nothing but a Sun 4/100 is going to have these devices. */
236 1.3.4.2 bouyer if (cpuinfo.cpu_type != CPUTYP_4_100)
237 1.3.4.2 bouyer return (0);
238 1.3.4.2 bouyer
239 1.3.4.2 bouyer if (uoba->uoba_isobio4 == 0)
240 1.3.4.2 bouyer return (0);
241 1.3.4.2 bouyer
242 1.3.4.2 bouyer /* Make sure there is something there... */
243 1.3.4.2 bouyer oba = &uoba->uoba_oba4;
244 1.3.4.2 bouyer return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
245 1.3.4.2 bouyer 1, /* probe size */
246 1.3.4.2 bouyer 1, /* offset */
247 1.3.4.2 bouyer 0, /* flags */
248 1.3.4.2 bouyer NULL, NULL));
249 1.3.4.2 bouyer }
250 1.3.4.2 bouyer
251 1.3.4.2 bouyer static void
252 1.3.4.2 bouyer sw_attach(parent, self, aux)
253 1.3.4.2 bouyer struct device *parent, *self;
254 1.3.4.2 bouyer void *aux;
255 1.3.4.2 bouyer {
256 1.3.4.2 bouyer struct sw_softc *sc = (struct sw_softc *) self;
257 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
258 1.3.4.2 bouyer union obio_attach_args *uoba = aux;
259 1.3.4.2 bouyer struct obio4_attach_args *oba = &uoba->uoba_oba4;
260 1.3.4.2 bouyer bus_space_handle_t bh;
261 1.3.4.2 bouyer char bits[64];
262 1.3.4.2 bouyer int i;
263 1.3.4.2 bouyer
264 1.3.4.2 bouyer sc->sc_dmatag = oba->oba_dmatag;
265 1.3.4.2 bouyer
266 1.3.4.2 bouyer /* Map the controller registers. */
267 1.3.4.2 bouyer if (obio_bus_map(oba->oba_bustag, oba->oba_paddr,
268 1.3.4.2 bouyer 0,
269 1.3.4.2 bouyer SWREG_BANK_SZ,
270 1.3.4.2 bouyer BUS_SPACE_MAP_LINEAR,
271 1.3.4.2 bouyer 0, &bh) != 0) {
272 1.3.4.2 bouyer printf("%s: cannot map registers\n", self->dv_xname);
273 1.3.4.2 bouyer return;
274 1.3.4.2 bouyer }
275 1.3.4.2 bouyer
276 1.3.4.2 bouyer ncr_sc->sc_regt = oba->oba_bustag;
277 1.3.4.2 bouyer ncr_sc->sc_regh = bh;
278 1.3.4.2 bouyer
279 1.3.4.2 bouyer sc->sc_options = sw_options;
280 1.3.4.2 bouyer
281 1.3.4.2 bouyer ncr_sc->sc_dma_setup = sw_dma_setup;
282 1.3.4.2 bouyer ncr_sc->sc_dma_start = sw_dma_start;
283 1.3.4.2 bouyer ncr_sc->sc_dma_eop = sw_dma_stop;
284 1.3.4.2 bouyer ncr_sc->sc_dma_stop = sw_dma_stop;
285 1.3.4.2 bouyer ncr_sc->sc_intr_on = sw_intr_on;
286 1.3.4.2 bouyer ncr_sc->sc_intr_off = sw_intr_off;
287 1.3.4.2 bouyer
288 1.3.4.2 bouyer /*
289 1.3.4.2 bouyer * Establish interrupt channel.
290 1.3.4.2 bouyer * Default interrupt priority always is 3. At least, that's
291 1.3.4.2 bouyer * what my board seems to be at. --thorpej
292 1.3.4.2 bouyer */
293 1.3.4.2 bouyer if (oba->oba_pri == -1)
294 1.3.4.2 bouyer oba->oba_pri = 3;
295 1.3.4.2 bouyer
296 1.3.4.2 bouyer (void)bus_intr_establish(oba->oba_bustag, oba->oba_pri, IPL_BIO, 0,
297 1.3.4.2 bouyer sw_intr, sc);
298 1.3.4.2 bouyer
299 1.3.4.2 bouyer printf(" pri %d\n", oba->oba_pri);
300 1.3.4.2 bouyer
301 1.3.4.2 bouyer
302 1.3.4.2 bouyer /*
303 1.3.4.2 bouyer * Pull in the options flags. Allow the user to completely
304 1.3.4.2 bouyer * override the default values.
305 1.3.4.2 bouyer */
306 1.3.4.2 bouyer if ((ncr_sc->sc_dev.dv_cfdata->cf_flags & SW_OPTIONS_MASK) != 0)
307 1.3.4.2 bouyer sc->sc_options =
308 1.3.4.2 bouyer (ncr_sc->sc_dev.dv_cfdata->cf_flags & SW_OPTIONS_MASK);
309 1.3.4.2 bouyer
310 1.3.4.2 bouyer /*
311 1.3.4.2 bouyer * Initialize fields used by the MI code
312 1.3.4.2 bouyer */
313 1.3.4.2 bouyer
314 1.3.4.2 bouyer /* NCR5380 register bank offsets */
315 1.3.4.2 bouyer ncr_sc->sci_r0 = 0;
316 1.3.4.2 bouyer ncr_sc->sci_r1 = 1;
317 1.3.4.2 bouyer ncr_sc->sci_r2 = 2;
318 1.3.4.2 bouyer ncr_sc->sci_r3 = 3;
319 1.3.4.2 bouyer ncr_sc->sci_r4 = 4;
320 1.3.4.2 bouyer ncr_sc->sci_r5 = 5;
321 1.3.4.2 bouyer ncr_sc->sci_r6 = 6;
322 1.3.4.2 bouyer ncr_sc->sci_r7 = 7;
323 1.3.4.2 bouyer
324 1.3.4.2 bouyer ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
325 1.3.4.2 bouyer
326 1.3.4.2 bouyer /*
327 1.3.4.2 bouyer * MD function pointers used by the MI code.
328 1.3.4.2 bouyer */
329 1.3.4.2 bouyer ncr_sc->sc_pio_out = ncr5380_pio_out;
330 1.3.4.2 bouyer ncr_sc->sc_pio_in = ncr5380_pio_in;
331 1.3.4.2 bouyer ncr_sc->sc_dma_alloc = sw_dma_alloc;
332 1.3.4.2 bouyer ncr_sc->sc_dma_free = sw_dma_free;
333 1.3.4.2 bouyer ncr_sc->sc_dma_poll = sw_dma_poll;
334 1.3.4.2 bouyer
335 1.3.4.2 bouyer ncr_sc->sc_flags = 0;
336 1.3.4.2 bouyer if ((sc->sc_options & SW_DO_RESELECT) == 0)
337 1.3.4.2 bouyer ncr_sc->sc_no_disconnect = 0xFF;
338 1.3.4.2 bouyer if ((sc->sc_options & SW_DMA_INTR) == 0)
339 1.3.4.2 bouyer ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
340 1.3.4.2 bouyer ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
341 1.3.4.2 bouyer
342 1.3.4.2 bouyer
343 1.3.4.2 bouyer /*
344 1.3.4.2 bouyer * Allocate DMA handles.
345 1.3.4.2 bouyer */
346 1.3.4.2 bouyer i = SCI_OPENINGS * sizeof(struct sw_dma_handle);
347 1.3.4.2 bouyer sc->sc_dma = (struct sw_dma_handle *)malloc(i, M_DEVBUF, M_NOWAIT);
348 1.3.4.2 bouyer if (sc->sc_dma == NULL)
349 1.3.4.2 bouyer panic("sw: dma handle malloc failed\n");
350 1.3.4.2 bouyer
351 1.3.4.2 bouyer for (i = 0; i < SCI_OPENINGS; i++) {
352 1.3.4.2 bouyer sc->sc_dma[i].dh_flags = 0;
353 1.3.4.2 bouyer
354 1.3.4.2 bouyer /* Allocate a DMA handle */
355 1.3.4.2 bouyer if (bus_dmamap_create(
356 1.3.4.2 bouyer sc->sc_dmatag, /* tag */
357 1.3.4.2 bouyer MAXPHYS, /* size */
358 1.3.4.2 bouyer 1, /* nsegments */
359 1.3.4.2 bouyer MAXPHYS, /* maxsegsz */
360 1.3.4.2 bouyer 0, /* boundary */
361 1.3.4.2 bouyer BUS_DMA_NOWAIT,
362 1.3.4.2 bouyer &sc->sc_dma[i].dh_dmamap) != 0) {
363 1.3.4.2 bouyer
364 1.3.4.2 bouyer printf("%s: DMA buffer map create error\n",
365 1.3.4.2 bouyer ncr_sc->sc_dev.dv_xname);
366 1.3.4.2 bouyer return;
367 1.3.4.2 bouyer }
368 1.3.4.2 bouyer }
369 1.3.4.2 bouyer
370 1.3.4.2 bouyer if (sc->sc_options) {
371 1.3.4.2 bouyer printf("%s: options=%s\n", ncr_sc->sc_dev.dv_xname,
372 1.3.4.2 bouyer bitmask_snprintf(sc->sc_options, SW_OPTIONS_BITS,
373 1.3.4.2 bouyer bits, sizeof(bits)));
374 1.3.4.2 bouyer }
375 1.3.4.2 bouyer
376 1.3.4.3 bouyer ncr_sc->sc_channel.chan_id = 7;
377 1.3.4.3 bouyer ncr_sc->sc_adapter.adapt_minphys = sw_minphys;
378 1.3.4.2 bouyer
379 1.3.4.2 bouyer /* Initialize sw board */
380 1.3.4.2 bouyer sw_reset_adapter(ncr_sc);
381 1.3.4.2 bouyer
382 1.3.4.2 bouyer /* Attach the ncr5380 chip driver */
383 1.3.4.2 bouyer ncr5380_attach(ncr_sc);
384 1.3.4.2 bouyer }
385 1.3.4.2 bouyer
386 1.3.4.2 bouyer static void
387 1.3.4.2 bouyer sw_minphys(struct buf *bp)
388 1.3.4.2 bouyer {
389 1.3.4.2 bouyer if (bp->b_bcount > MAX_DMA_LEN) {
390 1.3.4.2 bouyer #ifdef DEBUG
391 1.3.4.2 bouyer if (sw_debug) {
392 1.3.4.2 bouyer printf("sw_minphys len = 0x%x.\n", MAX_DMA_LEN);
393 1.3.4.2 bouyer Debugger();
394 1.3.4.2 bouyer }
395 1.3.4.2 bouyer #endif
396 1.3.4.2 bouyer bp->b_bcount = MAX_DMA_LEN;
397 1.3.4.2 bouyer }
398 1.3.4.3 bouyer minphys(bp);
399 1.3.4.2 bouyer }
400 1.3.4.2 bouyer
401 1.3.4.2 bouyer #define CSR_WANT (SW_CSR_SBC_IP | SW_CSR_DMA_IP | \
402 1.3.4.2 bouyer SW_CSR_DMA_CONFLICT | SW_CSR_DMA_BUS_ERR )
403 1.3.4.2 bouyer
404 1.3.4.2 bouyer static int
405 1.3.4.2 bouyer sw_intr(void *arg)
406 1.3.4.2 bouyer {
407 1.3.4.2 bouyer struct sw_softc *sc = arg;
408 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)arg;
409 1.3.4.2 bouyer int dma_error, claimed;
410 1.3.4.2 bouyer u_short csr;
411 1.3.4.2 bouyer
412 1.3.4.2 bouyer claimed = 0;
413 1.3.4.2 bouyer dma_error = 0;
414 1.3.4.2 bouyer
415 1.3.4.2 bouyer /* SBC interrupt? DMA interrupt? */
416 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
417 1.3.4.2 bouyer
418 1.3.4.2 bouyer NCR_TRACE("sw_intr: csr=0x%x\n", csr);
419 1.3.4.2 bouyer
420 1.3.4.2 bouyer if (csr & SW_CSR_DMA_CONFLICT) {
421 1.3.4.2 bouyer dma_error |= SW_CSR_DMA_CONFLICT;
422 1.3.4.2 bouyer printf("sw_intr: DMA conflict\n");
423 1.3.4.2 bouyer }
424 1.3.4.2 bouyer if (csr & SW_CSR_DMA_BUS_ERR) {
425 1.3.4.2 bouyer dma_error |= SW_CSR_DMA_BUS_ERR;
426 1.3.4.2 bouyer printf("sw_intr: DMA bus error\n");
427 1.3.4.2 bouyer }
428 1.3.4.2 bouyer if (dma_error) {
429 1.3.4.2 bouyer if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
430 1.3.4.2 bouyer sc->ncr_sc.sc_state |= NCR_ABORTING;
431 1.3.4.2 bouyer /* Make sure we will call the main isr. */
432 1.3.4.2 bouyer csr |= SW_CSR_DMA_IP;
433 1.3.4.2 bouyer }
434 1.3.4.2 bouyer
435 1.3.4.2 bouyer if (csr & (SW_CSR_SBC_IP | SW_CSR_DMA_IP)) {
436 1.3.4.2 bouyer claimed = ncr5380_intr(&sc->ncr_sc);
437 1.3.4.2 bouyer #ifdef DEBUG
438 1.3.4.2 bouyer if (!claimed) {
439 1.3.4.2 bouyer printf("sw_intr: spurious from SBC\n");
440 1.3.4.2 bouyer if (sw_debug & 4) {
441 1.3.4.2 bouyer Debugger(); /* XXX */
442 1.3.4.2 bouyer }
443 1.3.4.2 bouyer }
444 1.3.4.2 bouyer #endif
445 1.3.4.2 bouyer }
446 1.3.4.2 bouyer
447 1.3.4.2 bouyer return (claimed);
448 1.3.4.2 bouyer }
449 1.3.4.2 bouyer
450 1.3.4.2 bouyer
451 1.3.4.2 bouyer static void
452 1.3.4.2 bouyer sw_reset_adapter(struct ncr5380_softc *ncr_sc)
453 1.3.4.2 bouyer {
454 1.3.4.2 bouyer
455 1.3.4.2 bouyer #ifdef DEBUG
456 1.3.4.2 bouyer if (sw_debug) {
457 1.3.4.2 bouyer printf("sw_reset_adapter\n");
458 1.3.4.2 bouyer }
459 1.3.4.2 bouyer #endif
460 1.3.4.2 bouyer
461 1.3.4.2 bouyer /*
462 1.3.4.2 bouyer * The reset bits in the CSR are active low.
463 1.3.4.2 bouyer */
464 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, 0);
465 1.3.4.2 bouyer delay(10);
466 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, SW_CSR_SCSI_RES);
467 1.3.4.2 bouyer
468 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, 0);
469 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, 0);
470 1.3.4.2 bouyer delay(10);
471 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, SW_CSR_SCSI_RES | SW_CSR_INTR_EN);
472 1.3.4.2 bouyer
473 1.3.4.2 bouyer SCI_CLR_INTR(ncr_sc);
474 1.3.4.2 bouyer }
475 1.3.4.2 bouyer
476 1.3.4.2 bouyer
477 1.3.4.2 bouyer /*****************************************************************
478 1.3.4.2 bouyer * Common functions for DMA
479 1.3.4.2 bouyer ****************************************************************/
480 1.3.4.2 bouyer
481 1.3.4.2 bouyer /*
482 1.3.4.2 bouyer * Allocate a DMA handle and put it in sc->sc_dma. Prepare
483 1.3.4.2 bouyer * for DMA transfer. On the Sun4, this means mapping the buffer
484 1.3.4.2 bouyer * into DVMA space.
485 1.3.4.2 bouyer */
486 1.3.4.2 bouyer void
487 1.3.4.2 bouyer sw_dma_alloc(ncr_sc)
488 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
489 1.3.4.2 bouyer {
490 1.3.4.2 bouyer struct sw_softc *sc = (struct sw_softc *)ncr_sc;
491 1.3.4.2 bouyer struct sci_req *sr = ncr_sc->sc_current;
492 1.3.4.2 bouyer struct scsipi_xfer *xs = sr->sr_xs;
493 1.3.4.2 bouyer struct sw_dma_handle *dh;
494 1.3.4.2 bouyer int i, xlen;
495 1.3.4.2 bouyer u_long addr;
496 1.3.4.2 bouyer
497 1.3.4.2 bouyer #ifdef DIAGNOSTIC
498 1.3.4.2 bouyer if (sr->sr_dma_hand != NULL)
499 1.3.4.2 bouyer panic("sw_dma_alloc: already have DMA handle");
500 1.3.4.2 bouyer #endif
501 1.3.4.2 bouyer
502 1.3.4.2 bouyer #if 1 /* XXX - Temporary */
503 1.3.4.2 bouyer /* XXX - In case we think DMA is completely broken... */
504 1.3.4.2 bouyer if ((sc->sc_options & SW_ENABLE_DMA) == 0)
505 1.3.4.2 bouyer return;
506 1.3.4.2 bouyer #endif
507 1.3.4.2 bouyer
508 1.3.4.2 bouyer addr = (u_long) ncr_sc->sc_dataptr;
509 1.3.4.2 bouyer xlen = ncr_sc->sc_datalen;
510 1.3.4.2 bouyer
511 1.3.4.2 bouyer /* If the DMA start addr is misaligned then do PIO */
512 1.3.4.2 bouyer if ((addr & 1) || (xlen & 1)) {
513 1.3.4.2 bouyer printf("sw_dma_alloc: misaligned.\n");
514 1.3.4.2 bouyer return;
515 1.3.4.2 bouyer }
516 1.3.4.2 bouyer
517 1.3.4.2 bouyer /* Make sure our caller checked sc_min_dma_len. */
518 1.3.4.2 bouyer if (xlen < MIN_DMA_LEN)
519 1.3.4.2 bouyer panic("sw_dma_alloc: xlen=0x%x\n", xlen);
520 1.3.4.2 bouyer
521 1.3.4.2 bouyer /* Find free DMA handle. Guaranteed to find one since we have
522 1.3.4.2 bouyer as many DMA handles as the driver has processes. */
523 1.3.4.2 bouyer for (i = 0; i < SCI_OPENINGS; i++) {
524 1.3.4.2 bouyer if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
525 1.3.4.2 bouyer goto found;
526 1.3.4.2 bouyer }
527 1.3.4.2 bouyer panic("sw: no free DMA handles.");
528 1.3.4.2 bouyer
529 1.3.4.2 bouyer found:
530 1.3.4.2 bouyer dh = &sc->sc_dma[i];
531 1.3.4.2 bouyer dh->dh_flags = SIDH_BUSY;
532 1.3.4.2 bouyer dh->dh_addr = (u_char *)addr;
533 1.3.4.2 bouyer dh->dh_maplen = xlen;
534 1.3.4.2 bouyer
535 1.3.4.2 bouyer /* Copy the "write" flag for convenience. */
536 1.3.4.2 bouyer if ((xs->xs_control & XS_CTL_DATA_OUT) != 0)
537 1.3.4.2 bouyer dh->dh_flags |= SIDH_OUT;
538 1.3.4.2 bouyer
539 1.3.4.2 bouyer /*
540 1.3.4.2 bouyer * Double-map the buffer into DVMA space. If we can't re-map
541 1.3.4.2 bouyer * the buffer, we print a warning and fall back to PIO mode.
542 1.3.4.2 bouyer *
543 1.3.4.2 bouyer * NOTE: it is not safe to sleep here!
544 1.3.4.2 bouyer */
545 1.3.4.2 bouyer if (bus_dmamap_load(sc->sc_dmatag, dh->dh_dmamap,
546 1.3.4.2 bouyer (caddr_t)addr, xlen, NULL, BUS_DMA_NOWAIT) != 0) {
547 1.3.4.2 bouyer /* Can't remap segment */
548 1.3.4.2 bouyer printf("sw_dma_alloc: can't remap 0x%lx/0x%x, doing PIO\n",
549 1.3.4.2 bouyer addr, dh->dh_maplen);
550 1.3.4.2 bouyer dh->dh_flags = 0;
551 1.3.4.2 bouyer return;
552 1.3.4.2 bouyer }
553 1.3.4.2 bouyer bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap, addr, xlen,
554 1.3.4.2 bouyer (dh->dh_flags & SIDH_OUT)
555 1.3.4.2 bouyer ? BUS_DMASYNC_PREWRITE
556 1.3.4.2 bouyer : BUS_DMASYNC_PREREAD);
557 1.3.4.2 bouyer
558 1.3.4.2 bouyer /* success */
559 1.3.4.2 bouyer sr->sr_dma_hand = dh;
560 1.3.4.2 bouyer
561 1.3.4.2 bouyer return;
562 1.3.4.2 bouyer }
563 1.3.4.2 bouyer
564 1.3.4.2 bouyer
565 1.3.4.2 bouyer void
566 1.3.4.2 bouyer sw_dma_free(ncr_sc)
567 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
568 1.3.4.2 bouyer {
569 1.3.4.2 bouyer struct sw_softc *sc = (struct sw_softc *)ncr_sc;
570 1.3.4.2 bouyer struct sci_req *sr = ncr_sc->sc_current;
571 1.3.4.2 bouyer struct sw_dma_handle *dh = sr->sr_dma_hand;
572 1.3.4.2 bouyer
573 1.3.4.2 bouyer #ifdef DIAGNOSTIC
574 1.3.4.2 bouyer if (dh == NULL)
575 1.3.4.2 bouyer panic("sw_dma_free: no DMA handle");
576 1.3.4.2 bouyer #endif
577 1.3.4.2 bouyer
578 1.3.4.2 bouyer if (ncr_sc->sc_state & NCR_DOINGDMA)
579 1.3.4.2 bouyer panic("sw_dma_free: free while in progress");
580 1.3.4.2 bouyer
581 1.3.4.2 bouyer if (dh->dh_flags & SIDH_BUSY) {
582 1.3.4.2 bouyer /* Give back the DVMA space. */
583 1.3.4.2 bouyer bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap,
584 1.3.4.2 bouyer dh->dh_dvma, dh->dh_maplen,
585 1.3.4.2 bouyer (dh->dh_flags & SIDH_OUT)
586 1.3.4.2 bouyer ? BUS_DMASYNC_POSTWRITE
587 1.3.4.2 bouyer : BUS_DMASYNC_POSTREAD);
588 1.3.4.2 bouyer bus_dmamap_unload(sc->sc_dmatag, dh->dh_dmamap);
589 1.3.4.2 bouyer dh->dh_flags = 0;
590 1.3.4.2 bouyer }
591 1.3.4.2 bouyer sr->sr_dma_hand = NULL;
592 1.3.4.2 bouyer }
593 1.3.4.2 bouyer
594 1.3.4.2 bouyer
595 1.3.4.2 bouyer /*
596 1.3.4.2 bouyer * Poll (spin-wait) for DMA completion.
597 1.3.4.2 bouyer * Called right after xx_dma_start(), and
598 1.3.4.2 bouyer * xx_dma_stop() will be called next.
599 1.3.4.2 bouyer * Same for either VME or OBIO.
600 1.3.4.2 bouyer */
601 1.3.4.2 bouyer void
602 1.3.4.2 bouyer sw_dma_poll(ncr_sc)
603 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
604 1.3.4.2 bouyer {
605 1.3.4.2 bouyer struct sci_req *sr = ncr_sc->sc_current;
606 1.3.4.2 bouyer int tmo, csr_mask, csr;
607 1.3.4.2 bouyer
608 1.3.4.2 bouyer /* Make sure DMA started successfully. */
609 1.3.4.2 bouyer if (ncr_sc->sc_state & NCR_ABORTING)
610 1.3.4.2 bouyer return;
611 1.3.4.2 bouyer
612 1.3.4.2 bouyer csr_mask = SW_CSR_SBC_IP | SW_CSR_DMA_IP |
613 1.3.4.2 bouyer SW_CSR_DMA_CONFLICT | SW_CSR_DMA_BUS_ERR;
614 1.3.4.2 bouyer
615 1.3.4.2 bouyer tmo = 50000; /* X100 = 5 sec. */
616 1.3.4.2 bouyer for (;;) {
617 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
618 1.3.4.2 bouyer if (csr & csr_mask)
619 1.3.4.2 bouyer break;
620 1.3.4.2 bouyer if (--tmo <= 0) {
621 1.3.4.2 bouyer printf("%s: DMA timeout (while polling)\n",
622 1.3.4.2 bouyer ncr_sc->sc_dev.dv_xname);
623 1.3.4.2 bouyer /* Indicate timeout as MI code would. */
624 1.3.4.2 bouyer sr->sr_flags |= SR_OVERDUE;
625 1.3.4.2 bouyer break;
626 1.3.4.2 bouyer }
627 1.3.4.2 bouyer delay(100);
628 1.3.4.2 bouyer }
629 1.3.4.2 bouyer
630 1.3.4.2 bouyer #ifdef DEBUG
631 1.3.4.2 bouyer if (sw_debug) {
632 1.3.4.2 bouyer printf("sw_dma_poll: done, csr=0x%x\n", csr);
633 1.3.4.2 bouyer }
634 1.3.4.2 bouyer #endif
635 1.3.4.2 bouyer }
636 1.3.4.2 bouyer
637 1.3.4.2 bouyer
638 1.3.4.2 bouyer /*
639 1.3.4.2 bouyer * This is called when the bus is going idle,
640 1.3.4.2 bouyer * so we want to enable the SBC interrupts.
641 1.3.4.2 bouyer * That is controlled by the DMA enable!
642 1.3.4.2 bouyer * Who would have guessed!
643 1.3.4.2 bouyer * What a NASTY trick!
644 1.3.4.2 bouyer *
645 1.3.4.2 bouyer * XXX THIS MIGHT NOT WORK RIGHT!
646 1.3.4.2 bouyer */
647 1.3.4.2 bouyer void
648 1.3.4.2 bouyer sw_intr_on(ncr_sc)
649 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
650 1.3.4.2 bouyer {
651 1.3.4.2 bouyer u_int32_t csr;
652 1.3.4.2 bouyer
653 1.3.4.2 bouyer sw_dma_setup(ncr_sc);
654 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
655 1.3.4.2 bouyer csr |= SW_CSR_DMA_EN; /* XXX - this bit is for vme only?! */
656 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
657 1.3.4.2 bouyer }
658 1.3.4.2 bouyer
659 1.3.4.2 bouyer /*
660 1.3.4.2 bouyer * This is called when the bus is idle and we are
661 1.3.4.2 bouyer * about to start playing with the SBC chip.
662 1.3.4.2 bouyer *
663 1.3.4.2 bouyer * XXX THIS MIGHT NOT WORK RIGHT!
664 1.3.4.2 bouyer */
665 1.3.4.2 bouyer void
666 1.3.4.2 bouyer sw_intr_off(ncr_sc)
667 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
668 1.3.4.2 bouyer {
669 1.3.4.2 bouyer u_int32_t csr;
670 1.3.4.2 bouyer
671 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
672 1.3.4.2 bouyer csr &= ~SW_CSR_DMA_EN;
673 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
674 1.3.4.2 bouyer }
675 1.3.4.2 bouyer
676 1.3.4.2 bouyer
677 1.3.4.2 bouyer /*
678 1.3.4.2 bouyer * This function is called during the COMMAND or MSG_IN phase
679 1.3.4.2 bouyer * that preceeds a DATA_IN or DATA_OUT phase, in case we need
680 1.3.4.2 bouyer * to setup the DMA engine before the bus enters a DATA phase.
681 1.3.4.2 bouyer *
682 1.3.4.2 bouyer * On the OBIO version we just clear the DMA count and address
683 1.3.4.2 bouyer * here (to make sure it stays idle) and do the real setup
684 1.3.4.2 bouyer * later, in dma_start.
685 1.3.4.2 bouyer */
686 1.3.4.2 bouyer void
687 1.3.4.2 bouyer sw_dma_setup(ncr_sc)
688 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
689 1.3.4.2 bouyer {
690 1.3.4.2 bouyer u_int32_t csr;
691 1.3.4.2 bouyer
692 1.3.4.2 bouyer /* No FIFO to reset on "sw". */
693 1.3.4.2 bouyer
694 1.3.4.2 bouyer /* Set direction (assume recv here) */
695 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
696 1.3.4.2 bouyer csr &= ~SW_CSR_SEND;
697 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
698 1.3.4.2 bouyer
699 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, 0);
700 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, 0);
701 1.3.4.2 bouyer }
702 1.3.4.2 bouyer
703 1.3.4.2 bouyer
704 1.3.4.2 bouyer void
705 1.3.4.2 bouyer sw_dma_start(ncr_sc)
706 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
707 1.3.4.2 bouyer {
708 1.3.4.2 bouyer struct sw_softc *sc = (struct sw_softc *)ncr_sc;
709 1.3.4.2 bouyer struct sci_req *sr = ncr_sc->sc_current;
710 1.3.4.2 bouyer struct sw_dma_handle *dh = sr->sr_dma_hand;
711 1.3.4.2 bouyer u_long dva;
712 1.3.4.2 bouyer int xlen, adj, adjlen;
713 1.3.4.2 bouyer u_int mode;
714 1.3.4.2 bouyer u_int32_t csr;
715 1.3.4.2 bouyer
716 1.3.4.2 bouyer /*
717 1.3.4.2 bouyer * Get the DVMA mapping for this segment.
718 1.3.4.2 bouyer */
719 1.3.4.2 bouyer dva = (u_long)(dh->dh_dvma);
720 1.3.4.2 bouyer if (dva & 1)
721 1.3.4.2 bouyer panic("sw_dma_start: bad dva=0x%lx", dva);
722 1.3.4.2 bouyer
723 1.3.4.2 bouyer xlen = ncr_sc->sc_datalen;
724 1.3.4.2 bouyer xlen &= ~1;
725 1.3.4.2 bouyer sc->sc_xlen = xlen; /* XXX: or less... */
726 1.3.4.2 bouyer
727 1.3.4.2 bouyer #ifdef DEBUG
728 1.3.4.2 bouyer if (sw_debug & 2) {
729 1.3.4.2 bouyer printf("sw_dma_start: dh=%p, dva=0x%lx, xlen=%d\n",
730 1.3.4.2 bouyer dh, dva, xlen);
731 1.3.4.2 bouyer }
732 1.3.4.2 bouyer #endif
733 1.3.4.2 bouyer
734 1.3.4.2 bouyer /*
735 1.3.4.2 bouyer * Set up the DMA controller.
736 1.3.4.2 bouyer * Note that (dh->dh_len < sc_datalen)
737 1.3.4.2 bouyer */
738 1.3.4.2 bouyer
739 1.3.4.2 bouyer /* Set direction (send/recv) */
740 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
741 1.3.4.2 bouyer if (dh->dh_flags & SIDH_OUT) {
742 1.3.4.2 bouyer csr |= SW_CSR_SEND;
743 1.3.4.2 bouyer } else {
744 1.3.4.2 bouyer csr &= ~SW_CSR_SEND;
745 1.3.4.2 bouyer }
746 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
747 1.3.4.2 bouyer
748 1.3.4.2 bouyer /*
749 1.3.4.2 bouyer * The "sw" needs longword aligned transfers. We
750 1.3.4.2 bouyer * detect a shortword aligned transfer here, and adjust the
751 1.3.4.2 bouyer * DMA transfer by 2 bytes. These two bytes are read/written
752 1.3.4.2 bouyer * in PIO mode just before the DMA is started.
753 1.3.4.2 bouyer */
754 1.3.4.2 bouyer adj = 0;
755 1.3.4.2 bouyer if (dva & 2) {
756 1.3.4.2 bouyer adj = 2;
757 1.3.4.2 bouyer #ifdef DEBUG
758 1.3.4.2 bouyer if (sw_debug & 2)
759 1.3.4.2 bouyer printf("sw_dma_start: adjusted up %d bytes\n", adj);
760 1.3.4.2 bouyer #endif
761 1.3.4.2 bouyer }
762 1.3.4.2 bouyer
763 1.3.4.2 bouyer /* We have to frob the address on the "sw". */
764 1.3.4.2 bouyer dh->dh_startingpa = (dva | 0xF00000);
765 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, (u_int)(dh->dh_startingpa + adj));
766 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, xlen - adj);
767 1.3.4.2 bouyer
768 1.3.4.2 bouyer /*
769 1.3.4.2 bouyer * Acknowledge the phase change. (After DMA setup!)
770 1.3.4.2 bouyer * Put the SBIC into DMA mode, and start the transfer.
771 1.3.4.2 bouyer */
772 1.3.4.2 bouyer if (dh->dh_flags & SIDH_OUT) {
773 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
774 1.3.4.2 bouyer if (adj) {
775 1.3.4.2 bouyer adjlen = ncr5380_pio_out(ncr_sc, PHASE_DATA_OUT,
776 1.3.4.2 bouyer adj, dh->dh_addr);
777 1.3.4.2 bouyer if (adjlen != adj)
778 1.3.4.2 bouyer printf("%s: bad outgoing adj, %d != %d\n",
779 1.3.4.2 bouyer ncr_sc->sc_dev.dv_xname, adjlen, adj);
780 1.3.4.2 bouyer }
781 1.3.4.2 bouyer SCI_CLR_INTR(ncr_sc);
782 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
783 1.3.4.2 bouyer mode = NCR5380_READ(ncr_sc, sci_mode);
784 1.3.4.2 bouyer mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
785 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_mode, mode);
786 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_dma_send, 0); /* start it */
787 1.3.4.2 bouyer } else {
788 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
789 1.3.4.2 bouyer if (adj) {
790 1.3.4.2 bouyer adjlen = ncr5380_pio_in(ncr_sc, PHASE_DATA_IN,
791 1.3.4.2 bouyer adj, dh->dh_addr);
792 1.3.4.2 bouyer if (adjlen != adj)
793 1.3.4.2 bouyer printf("%s: bad incoming adj, %d != %d\n",
794 1.3.4.2 bouyer ncr_sc->sc_dev.dv_xname, adjlen, adj);
795 1.3.4.2 bouyer }
796 1.3.4.2 bouyer SCI_CLR_INTR(ncr_sc);
797 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_icmd, 0);
798 1.3.4.2 bouyer mode = NCR5380_READ(ncr_sc, sci_mode);
799 1.3.4.2 bouyer mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
800 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_mode, mode);
801 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_irecv, 0); /* start it */
802 1.3.4.2 bouyer }
803 1.3.4.2 bouyer
804 1.3.4.2 bouyer /* Let'er rip! */
805 1.3.4.2 bouyer csr |= SW_CSR_DMA_EN;
806 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
807 1.3.4.2 bouyer
808 1.3.4.2 bouyer ncr_sc->sc_state |= NCR_DOINGDMA;
809 1.3.4.2 bouyer
810 1.3.4.2 bouyer #ifdef DEBUG
811 1.3.4.2 bouyer if (sw_debug & 2) {
812 1.3.4.2 bouyer printf("sw_dma_start: started, flags=0x%x\n",
813 1.3.4.2 bouyer ncr_sc->sc_state);
814 1.3.4.2 bouyer }
815 1.3.4.2 bouyer #endif
816 1.3.4.2 bouyer }
817 1.3.4.2 bouyer
818 1.3.4.2 bouyer
819 1.3.4.2 bouyer void
820 1.3.4.2 bouyer sw_dma_eop(ncr_sc)
821 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
822 1.3.4.2 bouyer {
823 1.3.4.2 bouyer
824 1.3.4.2 bouyer /* Not needed - DMA was stopped prior to examining sci_csr */
825 1.3.4.2 bouyer }
826 1.3.4.2 bouyer
827 1.3.4.2 bouyer #if (defined(DEBUG) || defined(DIAGNOSTIC)) && !defined(COUNT_SW_LEFTOVERS)
828 1.3.4.2 bouyer #define COUNT_SW_LEFTOVERS
829 1.3.4.2 bouyer #endif
830 1.3.4.2 bouyer #ifdef COUNT_SW_LEFTOVERS
831 1.3.4.2 bouyer /*
832 1.3.4.2 bouyer * Let's find out how often these occur. Read these with DDB from time
833 1.3.4.2 bouyer * to time.
834 1.3.4.2 bouyer */
835 1.3.4.2 bouyer int sw_3_leftover = 0;
836 1.3.4.2 bouyer int sw_2_leftover = 0;
837 1.3.4.2 bouyer int sw_1_leftover = 0;
838 1.3.4.2 bouyer int sw_0_leftover = 0;
839 1.3.4.2 bouyer #endif
840 1.3.4.2 bouyer
841 1.3.4.2 bouyer void
842 1.3.4.2 bouyer sw_dma_stop(ncr_sc)
843 1.3.4.2 bouyer struct ncr5380_softc *ncr_sc;
844 1.3.4.2 bouyer {
845 1.3.4.2 bouyer struct sci_req *sr = ncr_sc->sc_current;
846 1.3.4.2 bouyer struct sw_dma_handle *dh = sr->sr_dma_hand;
847 1.3.4.2 bouyer int ntrans = 0, dva;
848 1.3.4.2 bouyer u_int mode;
849 1.3.4.2 bouyer u_int32_t csr;
850 1.3.4.2 bouyer
851 1.3.4.2 bouyer if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
852 1.3.4.2 bouyer #ifdef DEBUG
853 1.3.4.2 bouyer printf("sw_dma_stop: dma not running\n");
854 1.3.4.2 bouyer #endif
855 1.3.4.2 bouyer return;
856 1.3.4.2 bouyer }
857 1.3.4.2 bouyer ncr_sc->sc_state &= ~NCR_DOINGDMA;
858 1.3.4.2 bouyer
859 1.3.4.2 bouyer /* First, halt the DMA engine. */
860 1.3.4.2 bouyer csr = SWREG_READ(ncr_sc, SWREG_CSR);
861 1.3.4.2 bouyer csr &= ~SW_CSR_DMA_EN;
862 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_CSR, csr);
863 1.3.4.2 bouyer
864 1.3.4.2 bouyer /*
865 1.3.4.2 bouyer * XXX HARDWARE BUG!
866 1.3.4.2 bouyer * Apparently, some early 4/100 SCSI controllers had a hardware
867 1.3.4.2 bouyer * bug that caused the controller to do illegal memory access.
868 1.3.4.2 bouyer * We see this as SW_CSR_DMA_BUS_ERR (makes sense). To work around
869 1.3.4.2 bouyer * this, we simply need to clean up after ourselves ... there will
870 1.3.4.2 bouyer * be as many as 3 bytes left over. Since we clean up "left-over"
871 1.3.4.2 bouyer * bytes on every read anyway, we just continue to chug along
872 1.3.4.2 bouyer * if SW_CSR_DMA_BUS_ERR is asserted. (This was probably worked
873 1.3.4.2 bouyer * around in hardware later with the "left-over byte" indicator
874 1.3.4.2 bouyer * in the VME controller.)
875 1.3.4.2 bouyer */
876 1.3.4.2 bouyer #if 0
877 1.3.4.2 bouyer if (csr & (SW_CSR_DMA_CONFLICT | SW_CSR_DMA_BUS_ERR)) {
878 1.3.4.2 bouyer #else
879 1.3.4.2 bouyer if (csr & (SW_CSR_DMA_CONFLICT)) {
880 1.3.4.2 bouyer #endif
881 1.3.4.2 bouyer printf("sw: DMA error, csr=0x%x, reset\n", csr);
882 1.3.4.2 bouyer sr->sr_xs->error = XS_DRIVER_STUFFUP;
883 1.3.4.2 bouyer ncr_sc->sc_state |= NCR_ABORTING;
884 1.3.4.2 bouyer sw_reset_adapter(ncr_sc);
885 1.3.4.2 bouyer }
886 1.3.4.2 bouyer
887 1.3.4.2 bouyer /* Note that timeout may have set the error flag. */
888 1.3.4.2 bouyer if (ncr_sc->sc_state & NCR_ABORTING)
889 1.3.4.2 bouyer goto out;
890 1.3.4.2 bouyer
891 1.3.4.2 bouyer /*
892 1.3.4.2 bouyer * Now try to figure out how much actually transferred
893 1.3.4.2 bouyer *
894 1.3.4.2 bouyer * The "sw" doesn't have a FIFO or a bcr, so we've stored
895 1.3.4.2 bouyer * the starting PA of the transfer in the DMA handle,
896 1.3.4.2 bouyer * and subtract it from the ending PA left in the dma_addr
897 1.3.4.2 bouyer * register.
898 1.3.4.2 bouyer */
899 1.3.4.2 bouyer dva = SWREG_READ(ncr_sc, SWREG_DMA_ADDR);
900 1.3.4.2 bouyer ntrans = (dva - dh->dh_startingpa);
901 1.3.4.2 bouyer
902 1.3.4.2 bouyer #ifdef DEBUG
903 1.3.4.2 bouyer if (sw_debug & 2) {
904 1.3.4.2 bouyer printf("sw_dma_stop: ntrans=0x%x\n", ntrans);
905 1.3.4.2 bouyer }
906 1.3.4.2 bouyer #endif
907 1.3.4.2 bouyer
908 1.3.4.2 bouyer if (ntrans > ncr_sc->sc_datalen)
909 1.3.4.2 bouyer panic("sw_dma_stop: excess transfer");
910 1.3.4.2 bouyer
911 1.3.4.2 bouyer /* Adjust data pointer */
912 1.3.4.2 bouyer ncr_sc->sc_dataptr += ntrans;
913 1.3.4.2 bouyer ncr_sc->sc_datalen -= ntrans;
914 1.3.4.2 bouyer
915 1.3.4.2 bouyer /*
916 1.3.4.2 bouyer * After a read, we may need to clean-up
917 1.3.4.2 bouyer * "Left-over bytes" (yuck!) The "sw" doesn't
918 1.3.4.2 bouyer * have a "left-over" indicator, so we have to so
919 1.3.4.2 bouyer * this no matter what. Ick.
920 1.3.4.2 bouyer */
921 1.3.4.2 bouyer if ((dh->dh_flags & SIDH_OUT) == 0) {
922 1.3.4.2 bouyer char *cp = ncr_sc->sc_dataptr;
923 1.3.4.2 bouyer u_int32_t bpr;
924 1.3.4.2 bouyer
925 1.3.4.2 bouyer bpr = SWREG_READ(ncr_sc, SWREG_BPR);
926 1.3.4.2 bouyer
927 1.3.4.2 bouyer switch (dva & 3) {
928 1.3.4.2 bouyer case 3:
929 1.3.4.2 bouyer cp[0] = (bpr & 0xff000000) >> 24;
930 1.3.4.2 bouyer cp[1] = (bpr & 0x00ff0000) >> 16;
931 1.3.4.2 bouyer cp[2] = (bpr & 0x0000ff00) >> 8;
932 1.3.4.2 bouyer #ifdef COUNT_SW_LEFTOVERS
933 1.3.4.2 bouyer ++sw_3_leftover;
934 1.3.4.2 bouyer #endif
935 1.3.4.2 bouyer break;
936 1.3.4.2 bouyer
937 1.3.4.2 bouyer case 2:
938 1.3.4.2 bouyer cp[0] = (bpr & 0xff000000) >> 24;
939 1.3.4.2 bouyer cp[1] = (bpr & 0x00ff0000) >> 16;
940 1.3.4.2 bouyer #ifdef COUNT_SW_LEFTOVERS
941 1.3.4.2 bouyer ++sw_2_leftover;
942 1.3.4.2 bouyer #endif
943 1.3.4.2 bouyer break;
944 1.3.4.2 bouyer
945 1.3.4.2 bouyer case 1:
946 1.3.4.2 bouyer cp[0] = (bpr & 0xff000000) >> 24;
947 1.3.4.2 bouyer #ifdef COUNT_SW_LEFTOVERS
948 1.3.4.2 bouyer ++sw_1_leftover;
949 1.3.4.2 bouyer #endif
950 1.3.4.2 bouyer break;
951 1.3.4.2 bouyer
952 1.3.4.2 bouyer #ifdef COUNT_SW_LEFTOVERS
953 1.3.4.2 bouyer default:
954 1.3.4.2 bouyer ++sw_0_leftover;
955 1.3.4.2 bouyer break;
956 1.3.4.2 bouyer #endif
957 1.3.4.2 bouyer }
958 1.3.4.2 bouyer }
959 1.3.4.2 bouyer
960 1.3.4.2 bouyer out:
961 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_ADDR, 0);
962 1.3.4.2 bouyer SWREG_WRITE(ncr_sc, SWREG_DMA_CNT, 0);
963 1.3.4.2 bouyer
964 1.3.4.2 bouyer /* Put SBIC back in PIO mode. */
965 1.3.4.2 bouyer mode = NCR5380_READ(ncr_sc, sci_mode);
966 1.3.4.2 bouyer mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
967 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_mode, mode);
968 1.3.4.2 bouyer NCR5380_WRITE(ncr_sc, sci_icmd, 0);
969 1.3.4.2 bouyer
970 1.3.4.2 bouyer #ifdef DEBUG
971 1.3.4.2 bouyer if (sw_debug & 2) {
972 1.3.4.2 bouyer printf("sw_dma_stop: ntrans=0x%x\n", ntrans);
973 1.3.4.2 bouyer }
974 1.3.4.2 bouyer #endif
975 1.3.4.2 bouyer }
976