si_sebuf.c revision 1.8.8.1 1 1.8.8.1 bouyer /* $NetBSD: si_sebuf.c,v 1.8.8.1 2000/11/20 20:27:53 bouyer Exp $ */
2 1.1 gwr
3 1.1 gwr /*-
4 1.1 gwr * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.1 gwr * by Gordon W. Ross.
9 1.1 gwr *
10 1.1 gwr * Redistribution and use in source and binary forms, with or without
11 1.1 gwr * modification, are permitted provided that the following conditions
12 1.1 gwr * are met:
13 1.1 gwr * 1. Redistributions of source code must retain the above copyright
14 1.1 gwr * notice, this list of conditions and the following disclaimer.
15 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 gwr * notice, this list of conditions and the following disclaimer in the
17 1.1 gwr * documentation and/or other materials provided with the distribution.
18 1.1 gwr * 3. All advertising materials mentioning features or use of this software
19 1.1 gwr * must display the following acknowledgement:
20 1.1 gwr * This product includes software developed by the NetBSD
21 1.1 gwr * Foundation, Inc. and its contributors.
22 1.1 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 gwr * contributors may be used to endorse or promote products derived
24 1.1 gwr * from this software without specific prior written permission.
25 1.1 gwr *
26 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 gwr */
38 1.1 gwr
39 1.1 gwr /*
40 1.1 gwr * Sun3/E SCSI driver (machine-dependent portion).
41 1.1 gwr * The machine-independent parts are in ncr5380sbc.c
42 1.1 gwr *
43 1.1 gwr * XXX - Mostly from the si driver. Merge?
44 1.1 gwr */
45 1.1 gwr
46 1.1 gwr #include <sys/param.h>
47 1.1 gwr #include <sys/systm.h>
48 1.1 gwr #include <sys/errno.h>
49 1.1 gwr #include <sys/kernel.h>
50 1.1 gwr #include <sys/malloc.h>
51 1.1 gwr #include <sys/device.h>
52 1.1 gwr #include <sys/buf.h>
53 1.1 gwr #include <sys/proc.h>
54 1.1 gwr #include <sys/user.h>
55 1.1 gwr
56 1.1 gwr #include <dev/scsipi/scsi_all.h>
57 1.1 gwr #include <dev/scsipi/scsipi_all.h>
58 1.1 gwr #include <dev/scsipi/scsipi_debug.h>
59 1.1 gwr #include <dev/scsipi/scsiconf.h>
60 1.1 gwr
61 1.1 gwr #include <machine/autoconf.h>
62 1.1 gwr
63 1.5 gwr /* #define DEBUG XXX */
64 1.1 gwr
65 1.1 gwr #include <dev/ic/ncr5380reg.h>
66 1.1 gwr #include <dev/ic/ncr5380var.h>
67 1.1 gwr
68 1.1 gwr #include "sereg.h"
69 1.1 gwr #include "sevar.h"
70 1.1 gwr
71 1.1 gwr /*
72 1.1 gwr * Transfers smaller than this are done using PIO
73 1.1 gwr * (on assumption they're not worth DMA overhead)
74 1.1 gwr */
75 1.1 gwr #define MIN_DMA_LEN 128
76 1.1 gwr
77 1.1 gwr /*
78 1.1 gwr * Transfers lager than 65535 bytes need to be split-up.
79 1.1 gwr * (Some of the FIFO logic has only 16 bits counters.)
80 1.1 gwr * Make the size an integer multiple of the page size
81 1.1 gwr * to avoid buf/cluster remap problems. (paranoid?)
82 1.1 gwr */
83 1.1 gwr #define MAX_DMA_LEN 0xE000
84 1.1 gwr
85 1.1 gwr /*
86 1.1 gwr * This structure is used to keep track of mapped DMA requests.
87 1.1 gwr */
88 1.1 gwr struct se_dma_handle {
89 1.1 gwr int dh_flags;
90 1.1 gwr #define SIDH_BUSY 1 /* This DH is in use */
91 1.1 gwr #define SIDH_OUT 2 /* DMA does data out (write) */
92 1.1 gwr u_char * dh_addr; /* KVA of start of buffer */
93 1.1 gwr int dh_maplen; /* Length of KVA mapping. */
94 1.1 gwr long dh_dma; /* Offset in DMA buffer. */
95 1.1 gwr };
96 1.1 gwr
97 1.1 gwr /*
98 1.1 gwr * The first structure member has to be the ncr5380_softc
99 1.1 gwr * so we can just cast to go back and fourth between them.
100 1.1 gwr */
101 1.1 gwr struct se_softc {
102 1.1 gwr struct ncr5380_softc ncr_sc;
103 1.1 gwr volatile struct se_regs *sc_regs;
104 1.1 gwr int sc_adapter_type;
105 1.1 gwr int sc_adapter_iv; /* int. vec */
106 1.1 gwr int sc_options; /* options for this instance */
107 1.1 gwr int sc_reqlen; /* requested transfer length */
108 1.1 gwr struct se_dma_handle *sc_dma;
109 1.1 gwr /* DMA command block for the OBIO controller. */
110 1.1 gwr void *sc_dmacmd;
111 1.1 gwr };
112 1.1 gwr
113 1.1 gwr /* Options for disconnect/reselect, DMA, and interrupts. */
114 1.1 gwr #define SE_NO_DISCONNECT 0xff
115 1.1 gwr #define SE_NO_PARITY_CHK 0xff00
116 1.1 gwr #define SE_FORCE_POLLING 0x10000
117 1.1 gwr #define SE_DISABLE_DMA 0x20000
118 1.1 gwr
119 1.1 gwr void se_dma_alloc __P((struct ncr5380_softc *));
120 1.1 gwr void se_dma_free __P((struct ncr5380_softc *));
121 1.1 gwr void se_dma_poll __P((struct ncr5380_softc *));
122 1.1 gwr
123 1.1 gwr void se_dma_setup __P((struct ncr5380_softc *));
124 1.1 gwr void se_dma_start __P((struct ncr5380_softc *));
125 1.1 gwr void se_dma_eop __P((struct ncr5380_softc *));
126 1.1 gwr void se_dma_stop __P((struct ncr5380_softc *));
127 1.1 gwr
128 1.1 gwr void se_intr_on __P((struct ncr5380_softc *));
129 1.1 gwr void se_intr_off __P((struct ncr5380_softc *));
130 1.1 gwr
131 1.1 gwr static int se_intr __P((void *));
132 1.1 gwr static void se_reset __P((struct ncr5380_softc *));
133 1.1 gwr
134 1.1 gwr /*
135 1.1 gwr * New-style autoconfig attachment
136 1.1 gwr */
137 1.1 gwr
138 1.1 gwr static int se_match __P((struct device *, struct cfdata *, void *));
139 1.1 gwr static void se_attach __P((struct device *, struct device *, void *));
140 1.1 gwr
141 1.1 gwr struct cfattach si_sebuf_ca = {
142 1.1 gwr sizeof(struct se_softc), se_match, se_attach
143 1.1 gwr };
144 1.1 gwr
145 1.1 gwr static void se_minphys __P((struct buf *));
146 1.1 gwr
147 1.1 gwr /* Options for disconnect/reselect, DMA, and interrupts. */
148 1.4 gwr int se_options = SE_DISABLE_DMA | SE_FORCE_POLLING | 0xff;
149 1.1 gwr
150 1.1 gwr /* How long to wait for DMA before declaring an error. */
151 1.1 gwr int se_dma_intr_timo = 500; /* ticks (sec. X 100) */
152 1.1 gwr
153 1.1 gwr int se_debug = 0;
154 1.1 gwr #ifdef DEBUG
155 1.1 gwr static int se_link_flags = 0 /* | SDEV_DB2 */ ;
156 1.1 gwr #endif
157 1.1 gwr
158 1.1 gwr
159 1.1 gwr static int
160 1.1 gwr se_match(parent, cf, args)
161 1.1 gwr struct device *parent;
162 1.1 gwr struct cfdata *cf;
163 1.1 gwr void *args;
164 1.1 gwr {
165 1.1 gwr struct sebuf_attach_args *aa = args;
166 1.1 gwr
167 1.1 gwr /* Match by name. */
168 1.1 gwr if (strcmp(aa->name, "se"))
169 1.1 gwr return (0);
170 1.1 gwr
171 1.2 gwr /* Anyting else to check? */
172 1.1 gwr
173 1.1 gwr return (1);
174 1.1 gwr }
175 1.1 gwr
176 1.1 gwr static void
177 1.1 gwr se_attach(parent, self, args)
178 1.1 gwr struct device *parent, *self;
179 1.1 gwr void *args;
180 1.1 gwr {
181 1.1 gwr struct se_softc *sc = (struct se_softc *) self;
182 1.1 gwr struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
183 1.1 gwr struct cfdata *cf = self->dv_cfdata;
184 1.1 gwr struct sebuf_attach_args *aa = args;
185 1.1 gwr volatile struct se_regs *regs;
186 1.1 gwr int i;
187 1.1 gwr
188 1.1 gwr /* Get options from config flags if specified. */
189 1.1 gwr if (cf->cf_flags)
190 1.1 gwr sc->sc_options = cf->cf_flags;
191 1.1 gwr else
192 1.1 gwr sc->sc_options = se_options;
193 1.1 gwr
194 1.1 gwr printf(": options=0x%x\n", sc->sc_options);
195 1.1 gwr
196 1.1 gwr sc->sc_adapter_type = aa->ca.ca_bustype;
197 1.1 gwr sc->sc_adapter_iv = aa->ca.ca_intvec;
198 1.1 gwr sc->sc_regs = regs = aa->regs;
199 1.1 gwr
200 1.1 gwr /*
201 1.1 gwr * MD function pointers used by the MI code.
202 1.1 gwr */
203 1.1 gwr ncr_sc->sc_pio_out = ncr5380_pio_out;
204 1.1 gwr ncr_sc->sc_pio_in = ncr5380_pio_in;
205 1.1 gwr
206 1.1 gwr #if 0 /* XXX - not yet... */
207 1.1 gwr ncr_sc->sc_dma_alloc = se_dma_alloc;
208 1.1 gwr ncr_sc->sc_dma_free = se_dma_free;
209 1.1 gwr ncr_sc->sc_dma_setup = se_dma_setup;
210 1.1 gwr ncr_sc->sc_dma_start = se_dma_start;
211 1.1 gwr ncr_sc->sc_dma_poll = se_dma_poll;
212 1.1 gwr ncr_sc->sc_dma_eop = se_dma_eop;
213 1.1 gwr ncr_sc->sc_dma_stop = se_dma_stop;
214 1.1 gwr ncr_sc->sc_intr_on = se_intr_on;
215 1.1 gwr ncr_sc->sc_intr_off = se_intr_off;
216 1.1 gwr #endif /* XXX */
217 1.1 gwr
218 1.1 gwr /* Attach interrupt handler. */
219 1.1 gwr isr_add_vectored(se_intr, (void *)sc,
220 1.1 gwr aa->ca.ca_intpri, aa->ca.ca_intvec);
221 1.1 gwr
222 1.1 gwr /* Reset the hardware. */
223 1.1 gwr se_reset(ncr_sc);
224 1.1 gwr
225 1.1 gwr /* Do the common attach stuff. */
226 1.1 gwr
227 1.1 gwr /*
228 1.1 gwr * Support the "options" (config file flags).
229 1.1 gwr * Disconnect/reselect is a per-target mask.
230 1.1 gwr * Interrupts and DMA are per-controller.
231 1.1 gwr */
232 1.1 gwr ncr_sc->sc_no_disconnect =
233 1.1 gwr (sc->sc_options & SE_NO_DISCONNECT);
234 1.1 gwr ncr_sc->sc_parity_disable =
235 1.1 gwr (sc->sc_options & SE_NO_PARITY_CHK) >> 8;
236 1.1 gwr if (sc->sc_options & SE_FORCE_POLLING)
237 1.1 gwr ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
238 1.1 gwr
239 1.1 gwr #if 1 /* XXX - Temporary */
240 1.1 gwr /* XXX - In case we think DMA is completely broken... */
241 1.1 gwr if (sc->sc_options & SE_DISABLE_DMA) {
242 1.1 gwr /* Override this function pointer. */
243 1.1 gwr ncr_sc->sc_dma_alloc = NULL;
244 1.1 gwr }
245 1.1 gwr #endif
246 1.1 gwr ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
247 1.1 gwr
248 1.1 gwr #ifdef DEBUG
249 1.1 gwr if (se_debug)
250 1.1 gwr printf("se: Set TheSoftC=%p TheRegs=%p\n", sc, regs);
251 1.1 gwr ncr_sc->sc_link.flags |= se_link_flags;
252 1.1 gwr #endif
253 1.1 gwr
254 1.1 gwr /*
255 1.1 gwr * Initialize fields used by the MI code
256 1.1 gwr */
257 1.1 gwr ncr_sc->sci_r0 = ®s->ncrregs[0];
258 1.1 gwr ncr_sc->sci_r1 = ®s->ncrregs[1];
259 1.1 gwr ncr_sc->sci_r2 = ®s->ncrregs[2];
260 1.1 gwr ncr_sc->sci_r3 = ®s->ncrregs[3];
261 1.1 gwr ncr_sc->sci_r4 = ®s->ncrregs[4];
262 1.1 gwr ncr_sc->sci_r5 = ®s->ncrregs[5];
263 1.1 gwr ncr_sc->sci_r6 = ®s->ncrregs[6];
264 1.1 gwr ncr_sc->sci_r7 = ®s->ncrregs[7];
265 1.1 gwr
266 1.8.8.1 bouyer ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
267 1.8.8.1 bouyer
268 1.1 gwr /*
269 1.1 gwr * Allocate DMA handles.
270 1.1 gwr */
271 1.1 gwr i = SCI_OPENINGS * sizeof(struct se_dma_handle);
272 1.1 gwr sc->sc_dma = (struct se_dma_handle *)
273 1.1 gwr malloc(i, M_DEVBUF, M_WAITOK);
274 1.1 gwr if (sc->sc_dma == NULL)
275 1.1 gwr panic("se: dma_malloc failed\n");
276 1.1 gwr for (i = 0; i < SCI_OPENINGS; i++)
277 1.1 gwr sc->sc_dma[i].dh_flags = 0;
278 1.1 gwr
279 1.8.8.1 bouyer ncr_sc->sc_link.scsipi_scsi.adapter_target = 7;
280 1.8.8.1 bouyer ncr_sc->sc_adapter.scsipi_minphys = se_minphys;
281 1.8.8.1 bouyer
282 1.1 gwr /*
283 1.1 gwr * Initialize se board itself.
284 1.1 gwr */
285 1.8.8.1 bouyer ncr5380_attach(ncr_sc);
286 1.1 gwr }
287 1.1 gwr
288 1.1 gwr static void
289 1.1 gwr se_reset(struct ncr5380_softc *ncr_sc)
290 1.1 gwr {
291 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
292 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
293 1.1 gwr
294 1.1 gwr #ifdef DEBUG
295 1.1 gwr if (se_debug) {
296 1.1 gwr printf("se_reset\n");
297 1.1 gwr }
298 1.1 gwr #endif
299 1.1 gwr
300 1.1 gwr /* The reset bits in the CSR are active low. */
301 1.1 gwr se->se_csr = 0;
302 1.1 gwr delay(10);
303 1.1 gwr se->se_csr = SE_CSR_SCSI_RES /* | SE_CSR_INTR_EN */ ;
304 1.1 gwr delay(10);
305 1.1 gwr
306 1.1 gwr /* Make sure the DMA engine is stopped. */
307 1.1 gwr se->dma_addr = 0;
308 1.1 gwr se->dma_cntr = 0;
309 1.1 gwr se->se_ivec = sc->sc_adapter_iv;
310 1.1 gwr }
311 1.1 gwr
312 1.1 gwr /*
313 1.1 gwr * This is called when the bus is going idle,
314 1.1 gwr * so we want to enable the SBC interrupts.
315 1.1 gwr * That is controlled by the DMA enable!
316 1.1 gwr * Who would have guessed!
317 1.1 gwr * What a NASTY trick!
318 1.1 gwr */
319 1.1 gwr void
320 1.1 gwr se_intr_on(ncr_sc)
321 1.1 gwr struct ncr5380_softc *ncr_sc;
322 1.1 gwr {
323 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
324 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
325 1.1 gwr
326 1.1 gwr /* receive mode should be safer */
327 1.1 gwr se->se_csr &= ~SE_CSR_SEND;
328 1.1 gwr
329 1.1 gwr /* Clear the count so nothing happens. */
330 1.1 gwr se->dma_cntr = 0;
331 1.1 gwr
332 1.1 gwr /* Clear the start address too. (paranoid?) */
333 1.1 gwr se->dma_addr = 0;
334 1.1 gwr
335 1.1 gwr /* Finally, enable the DMA engine. */
336 1.1 gwr se->se_csr |= SE_CSR_INTR_EN;
337 1.1 gwr }
338 1.1 gwr
339 1.1 gwr /*
340 1.1 gwr * This is called when the bus is idle and we are
341 1.1 gwr * about to start playing with the SBC chip.
342 1.1 gwr */
343 1.1 gwr void
344 1.1 gwr se_intr_off(ncr_sc)
345 1.1 gwr struct ncr5380_softc *ncr_sc;
346 1.1 gwr {
347 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
348 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
349 1.1 gwr
350 1.1 gwr se->se_csr &= ~SE_CSR_INTR_EN;
351 1.1 gwr }
352 1.1 gwr
353 1.1 gwr /*
354 1.1 gwr * This function is called during the COMMAND or MSG_IN phase
355 1.1 gwr * that preceeds a DATA_IN or DATA_OUT phase, in case we need
356 1.1 gwr * to setup the DMA engine before the bus enters a DATA phase.
357 1.1 gwr *
358 1.1 gwr * On the VME version, setup the start addres, but clear the
359 1.1 gwr * count (to make sure it stays idle) and set that later.
360 1.1 gwr * XXX: The VME adapter appears to suppress SBC interrupts
361 1.1 gwr * when the FIFO is not empty or the FIFO count is non-zero!
362 1.1 gwr * XXX: Need to copy data into the DMA buffer...
363 1.1 gwr */
364 1.1 gwr void
365 1.1 gwr se_dma_setup(ncr_sc)
366 1.1 gwr struct ncr5380_softc *ncr_sc;
367 1.1 gwr {
368 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
369 1.1 gwr struct sci_req *sr = ncr_sc->sc_current;
370 1.1 gwr struct se_dma_handle *dh = sr->sr_dma_hand;
371 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
372 1.1 gwr long data_pa;
373 1.1 gwr int xlen;
374 1.1 gwr
375 1.1 gwr /*
376 1.1 gwr * Get the DMA mapping for this segment.
377 1.1 gwr * XXX - Should separate allocation and mapin.
378 1.1 gwr */
379 1.1 gwr data_pa = 0; /* XXX se_dma_kvtopa(dh->dh_dma); */
380 1.1 gwr data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
381 1.1 gwr if (data_pa & 1)
382 1.1 gwr panic("se_dma_start: bad pa=0x%lx", data_pa);
383 1.1 gwr xlen = ncr_sc->sc_datalen;
384 1.1 gwr xlen &= ~1; /* XXX: necessary? */
385 1.1 gwr sc->sc_reqlen = xlen; /* XXX: or less? */
386 1.1 gwr
387 1.1 gwr #ifdef DEBUG
388 1.1 gwr if (se_debug & 2) {
389 1.1 gwr printf("se_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
390 1.1 gwr dh, data_pa, xlen);
391 1.1 gwr }
392 1.1 gwr #endif
393 1.1 gwr
394 1.1 gwr /* Set direction (send/recv) */
395 1.1 gwr if (dh->dh_flags & SIDH_OUT) {
396 1.1 gwr se->se_csr |= SE_CSR_SEND;
397 1.1 gwr } else {
398 1.1 gwr se->se_csr &= ~SE_CSR_SEND;
399 1.1 gwr }
400 1.1 gwr
401 1.1 gwr /* Load the start address. */
402 1.1 gwr se->dma_addr = (ushort)(data_pa & 0xFFFF);
403 1.1 gwr
404 1.1 gwr /*
405 1.1 gwr * Keep the count zero or it may start early!
406 1.1 gwr */
407 1.1 gwr se->dma_cntr = 0;
408 1.1 gwr }
409 1.1 gwr
410 1.1 gwr
411 1.1 gwr void
412 1.1 gwr se_dma_start(ncr_sc)
413 1.1 gwr struct ncr5380_softc *ncr_sc;
414 1.1 gwr {
415 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
416 1.1 gwr struct sci_req *sr = ncr_sc->sc_current;
417 1.1 gwr struct se_dma_handle *dh = sr->sr_dma_hand;
418 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
419 1.1 gwr int s, xlen;
420 1.1 gwr
421 1.1 gwr xlen = sc->sc_reqlen;
422 1.1 gwr
423 1.1 gwr /* This MAY be time critical (not sure). */
424 1.1 gwr s = splhigh();
425 1.1 gwr
426 1.1 gwr se->dma_cntr = (ushort)(xlen & 0xFFFF);
427 1.1 gwr
428 1.1 gwr /*
429 1.1 gwr * Acknowledge the phase change. (After DMA setup!)
430 1.1 gwr * Put the SBIC into DMA mode, and start the transfer.
431 1.1 gwr */
432 1.1 gwr if (dh->dh_flags & SIDH_OUT) {
433 1.1 gwr *ncr_sc->sci_tcmd = PHASE_DATA_OUT;
434 1.1 gwr SCI_CLR_INTR(ncr_sc);
435 1.1 gwr *ncr_sc->sci_icmd = SCI_ICMD_DATA;
436 1.1 gwr *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
437 1.1 gwr *ncr_sc->sci_dma_send = 0; /* start it */
438 1.1 gwr } else {
439 1.1 gwr *ncr_sc->sci_tcmd = PHASE_DATA_IN;
440 1.1 gwr SCI_CLR_INTR(ncr_sc);
441 1.1 gwr *ncr_sc->sci_icmd = 0;
442 1.1 gwr *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
443 1.1 gwr *ncr_sc->sci_irecv = 0; /* start it */
444 1.1 gwr }
445 1.1 gwr
446 1.1 gwr /* Let'er rip! */
447 1.1 gwr se->se_csr |= SE_CSR_INTR_EN;
448 1.1 gwr
449 1.1 gwr splx(s);
450 1.1 gwr ncr_sc->sc_state |= NCR_DOINGDMA;
451 1.1 gwr
452 1.1 gwr #ifdef DEBUG
453 1.1 gwr if (se_debug & 2) {
454 1.1 gwr printf("se_dma_start: started, flags=0x%x\n",
455 1.1 gwr ncr_sc->sc_state);
456 1.1 gwr }
457 1.1 gwr #endif
458 1.1 gwr }
459 1.1 gwr
460 1.1 gwr
461 1.1 gwr void
462 1.1 gwr se_dma_eop(ncr_sc)
463 1.1 gwr struct ncr5380_softc *ncr_sc;
464 1.1 gwr {
465 1.1 gwr
466 1.1 gwr /* Not needed - DMA was stopped prior to examining sci_csr */
467 1.1 gwr }
468 1.1 gwr
469 1.1 gwr
470 1.1 gwr void
471 1.1 gwr se_dma_stop(ncr_sc)
472 1.1 gwr struct ncr5380_softc *ncr_sc;
473 1.1 gwr {
474 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
475 1.1 gwr struct sci_req *sr = ncr_sc->sc_current;
476 1.1 gwr struct se_dma_handle *dh = sr->sr_dma_hand;
477 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
478 1.1 gwr int resid, ntrans;
479 1.1 gwr
480 1.1 gwr if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
481 1.1 gwr #ifdef DEBUG
482 1.1 gwr printf("se_dma_stop: dma not running\n");
483 1.1 gwr #endif
484 1.1 gwr return;
485 1.1 gwr }
486 1.1 gwr ncr_sc->sc_state &= ~NCR_DOINGDMA;
487 1.1 gwr
488 1.1 gwr /* First, halt the DMA engine. */
489 1.1 gwr se->se_csr &= ~SE_CSR_INTR_EN; /* VME only */
490 1.1 gwr
491 1.1 gwr /* Set an impossible phase to prevent data movement? */
492 1.1 gwr *ncr_sc->sci_tcmd = PHASE_INVALID;
493 1.1 gwr
494 1.1 gwr /* Note that timeout may have set the error flag. */
495 1.1 gwr if (ncr_sc->sc_state & NCR_ABORTING)
496 1.1 gwr goto out;
497 1.1 gwr
498 1.1 gwr /* XXX: Wait for DMA to actually finish? */
499 1.1 gwr
500 1.1 gwr /*
501 1.1 gwr * Now try to figure out how much actually transferred
502 1.1 gwr */
503 1.1 gwr resid = se->dma_cntr & 0xFFFF;
504 1.1 gwr if (dh->dh_flags & SIDH_OUT)
505 1.1 gwr if ((resid > 0) && (resid < sc->sc_reqlen))
506 1.1 gwr resid++;
507 1.1 gwr ntrans = sc->sc_reqlen - resid;
508 1.1 gwr
509 1.1 gwr #ifdef DEBUG
510 1.1 gwr if (se_debug & 2) {
511 1.1 gwr printf("se_dma_stop: resid=0x%x ntrans=0x%x\n",
512 1.1 gwr resid, ntrans);
513 1.1 gwr }
514 1.1 gwr #endif
515 1.1 gwr
516 1.1 gwr if (ntrans < MIN_DMA_LEN) {
517 1.1 gwr printf("se: fifo count: 0x%x\n", resid);
518 1.1 gwr ncr_sc->sc_state |= NCR_ABORTING;
519 1.1 gwr goto out;
520 1.1 gwr }
521 1.1 gwr if (ntrans > ncr_sc->sc_datalen)
522 1.1 gwr panic("se_dma_stop: excess transfer");
523 1.1 gwr
524 1.1 gwr /* Adjust data pointer */
525 1.1 gwr ncr_sc->sc_dataptr += ntrans;
526 1.1 gwr ncr_sc->sc_datalen -= ntrans;
527 1.1 gwr
528 1.1 gwr out:
529 1.1 gwr se->dma_addr = 0;
530 1.1 gwr se->dma_cntr = 0;
531 1.1 gwr
532 1.1 gwr /* Put SBIC back in PIO mode. */
533 1.1 gwr *ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
534 1.1 gwr *ncr_sc->sci_icmd = 0;
535 1.1 gwr }
536 1.1 gwr
537 1.1 gwr /*****************************************************************/
538 1.1 gwr
539 1.1 gwr static void
540 1.1 gwr se_minphys(struct buf *bp)
541 1.1 gwr {
542 1.8 gwr
543 1.8 gwr if (bp->b_bcount > MAX_DMA_LEN)
544 1.1 gwr bp->b_bcount = MAX_DMA_LEN;
545 1.8 gwr
546 1.1 gwr return (minphys(bp));
547 1.1 gwr }
548 1.1 gwr
549 1.1 gwr
550 1.1 gwr int
551 1.1 gwr se_intr(void *arg)
552 1.1 gwr {
553 1.1 gwr struct se_softc *sc = arg;
554 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
555 1.1 gwr int dma_error, claimed;
556 1.1 gwr u_short csr;
557 1.1 gwr
558 1.1 gwr claimed = 0;
559 1.1 gwr dma_error = 0;
560 1.1 gwr
561 1.1 gwr /* SBC interrupt? DMA interrupt? */
562 1.1 gwr csr = se->se_csr;
563 1.1 gwr NCR_TRACE("se_intr: csr=0x%x\n", csr);
564 1.1 gwr
565 1.1 gwr if (csr & SE_CSR_SBC_IP) {
566 1.1 gwr claimed = ncr5380_intr(&sc->ncr_sc);
567 1.1 gwr #ifdef DEBUG
568 1.1 gwr if (!claimed) {
569 1.1 gwr printf("se_intr: spurious from SBC\n");
570 1.1 gwr }
571 1.1 gwr #endif
572 1.1 gwr /* Yes, we DID cause this interrupt. */
573 1.1 gwr claimed = 1;
574 1.1 gwr }
575 1.1 gwr
576 1.1 gwr return (claimed);
577 1.1 gwr }
578 1.1 gwr
579 1.1 gwr
580 1.1 gwr /*****************************************************************
581 1.1 gwr * Common functions for DMA
582 1.1 gwr ****************************************************************/
583 1.1 gwr
584 1.1 gwr /*
585 1.1 gwr * Allocate a DMA handle and put it in sc->sc_dma. Prepare
586 1.1 gwr * for DMA transfer. On the Sun3/E, this means we have to
587 1.1 gwr * allocate space in the DMA buffer for this transfer.
588 1.1 gwr */
589 1.1 gwr void
590 1.1 gwr se_dma_alloc(ncr_sc)
591 1.1 gwr struct ncr5380_softc *ncr_sc;
592 1.1 gwr {
593 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
594 1.1 gwr struct sci_req *sr = ncr_sc->sc_current;
595 1.1 gwr struct scsipi_xfer *xs = sr->sr_xs;
596 1.1 gwr struct se_dma_handle *dh;
597 1.1 gwr int i, xlen;
598 1.1 gwr u_long addr;
599 1.1 gwr
600 1.1 gwr #ifdef DIAGNOSTIC
601 1.1 gwr if (sr->sr_dma_hand != NULL)
602 1.1 gwr panic("se_dma_alloc: already have DMA handle");
603 1.1 gwr #endif
604 1.1 gwr
605 1.1 gwr addr = (u_long) ncr_sc->sc_dataptr;
606 1.1 gwr xlen = ncr_sc->sc_datalen;
607 1.1 gwr
608 1.1 gwr /* If the DMA start addr is misaligned then do PIO */
609 1.1 gwr if ((addr & 1) || (xlen & 1)) {
610 1.1 gwr printf("se_dma_alloc: misaligned.\n");
611 1.1 gwr return;
612 1.1 gwr }
613 1.1 gwr
614 1.1 gwr /* Make sure our caller checked sc_min_dma_len. */
615 1.1 gwr if (xlen < MIN_DMA_LEN)
616 1.1 gwr panic("se_dma_alloc: xlen=0x%x\n", xlen);
617 1.1 gwr
618 1.1 gwr /*
619 1.1 gwr * Never attempt single transfers of more than 63k, because
620 1.1 gwr * our count register may be only 16 bits (an OBIO adapter).
621 1.1 gwr * This should never happen since already bounded by minphys().
622 1.1 gwr * XXX - Should just segment these...
623 1.1 gwr */
624 1.1 gwr if (xlen > MAX_DMA_LEN) {
625 1.1 gwr printf("se_dma_alloc: excessive xlen=0x%x\n", xlen);
626 1.1 gwr ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
627 1.1 gwr }
628 1.1 gwr
629 1.1 gwr /* Find free DMA handle. Guaranteed to find one since we have
630 1.1 gwr as many DMA handles as the driver has processes. */
631 1.1 gwr for (i = 0; i < SCI_OPENINGS; i++) {
632 1.1 gwr if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
633 1.1 gwr goto found;
634 1.1 gwr }
635 1.1 gwr panic("se: no free DMA handles.");
636 1.1 gwr found:
637 1.1 gwr
638 1.1 gwr dh = &sc->sc_dma[i];
639 1.1 gwr dh->dh_flags = SIDH_BUSY;
640 1.1 gwr
641 1.1 gwr /* Copy the "write" flag for convenience. */
642 1.8.8.1 bouyer if (xs->xs_control & XS_CTL_DATA_OUT)
643 1.1 gwr dh->dh_flags |= SIDH_OUT;
644 1.1 gwr
645 1.1 gwr dh->dh_addr = (u_char*) addr;
646 1.1 gwr dh->dh_maplen = xlen;
647 1.1 gwr dh->dh_dma = 0; /* XXX - Allocate space in DMA buffer. */
648 1.1 gwr /* XXX: dh->dh_dma = alloc(xlen) */
649 1.1 gwr if (!dh->dh_dma) {
650 1.1 gwr /* Can't remap segment */
651 1.1 gwr printf("se_dma_alloc: can't remap %p/0x%x\n",
652 1.1 gwr dh->dh_addr, dh->dh_maplen);
653 1.1 gwr dh->dh_flags = 0;
654 1.1 gwr return;
655 1.1 gwr }
656 1.1 gwr
657 1.1 gwr /* success */
658 1.1 gwr sr->sr_dma_hand = dh;
659 1.1 gwr
660 1.1 gwr return;
661 1.1 gwr }
662 1.1 gwr
663 1.1 gwr
664 1.1 gwr void
665 1.1 gwr se_dma_free(ncr_sc)
666 1.1 gwr struct ncr5380_softc *ncr_sc;
667 1.1 gwr {
668 1.1 gwr struct sci_req *sr = ncr_sc->sc_current;
669 1.1 gwr struct se_dma_handle *dh = sr->sr_dma_hand;
670 1.1 gwr
671 1.1 gwr #ifdef DIAGNOSTIC
672 1.1 gwr if (dh == NULL)
673 1.1 gwr panic("se_dma_free: no DMA handle");
674 1.1 gwr #endif
675 1.1 gwr
676 1.1 gwr if (ncr_sc->sc_state & NCR_DOINGDMA)
677 1.1 gwr panic("se_dma_free: free while in progress");
678 1.1 gwr
679 1.1 gwr if (dh->dh_flags & SIDH_BUSY) {
680 1.1 gwr /* XXX: Should separate allocation and mapping. */
681 1.1 gwr /* XXX: Give back the DMA space. */
682 1.1 gwr /* XXX: free((caddr_t)dh->dh_dma, dh->dh_maplen); */
683 1.1 gwr dh->dh_dma = 0;
684 1.1 gwr dh->dh_flags = 0;
685 1.1 gwr }
686 1.1 gwr sr->sr_dma_hand = NULL;
687 1.1 gwr }
688 1.1 gwr
689 1.1 gwr
690 1.1 gwr #define CSR_MASK SE_CSR_SBC_IP
691 1.1 gwr #define POLL_TIMO 50000 /* X100 = 5 sec. */
692 1.1 gwr
693 1.1 gwr /*
694 1.1 gwr * Poll (spin-wait) for DMA completion.
695 1.1 gwr * Called right after xx_dma_start(), and
696 1.1 gwr * xx_dma_stop() will be called next.
697 1.1 gwr * Same for either VME or OBIO.
698 1.1 gwr */
699 1.1 gwr void
700 1.1 gwr se_dma_poll(ncr_sc)
701 1.1 gwr struct ncr5380_softc *ncr_sc;
702 1.1 gwr {
703 1.1 gwr struct se_softc *sc = (struct se_softc *)ncr_sc;
704 1.1 gwr struct sci_req *sr = ncr_sc->sc_current;
705 1.1 gwr volatile struct se_regs *se = sc->sc_regs;
706 1.1 gwr int tmo;
707 1.1 gwr
708 1.1 gwr /* Make sure DMA started successfully. */
709 1.1 gwr if (ncr_sc->sc_state & NCR_ABORTING)
710 1.1 gwr return;
711 1.1 gwr
712 1.1 gwr /*
713 1.1 gwr * XXX: The Sun driver waits for ~SE_CSR_DMA_ACTIVE here
714 1.1 gwr * XXX: (on obio) or even worse (on vme) a 10mS. delay!
715 1.1 gwr * XXX: I really doubt that is necessary...
716 1.1 gwr */
717 1.1 gwr
718 1.1 gwr /* Wait for any "dma complete" or error bits. */
719 1.1 gwr tmo = POLL_TIMO;
720 1.1 gwr for (;;) {
721 1.1 gwr if (se->se_csr & CSR_MASK)
722 1.1 gwr break;
723 1.1 gwr if (--tmo <= 0) {
724 1.1 gwr printf("se: DMA timeout (while polling)\n");
725 1.1 gwr /* Indicate timeout as MI code would. */
726 1.1 gwr sr->sr_flags |= SR_OVERDUE;
727 1.1 gwr break;
728 1.1 gwr }
729 1.1 gwr delay(100);
730 1.1 gwr }
731 1.1 gwr NCR_TRACE("se_dma_poll: waited %d\n",
732 1.1 gwr POLL_TIMO - tmo);
733 1.1 gwr
734 1.1 gwr #ifdef DEBUG
735 1.1 gwr if (se_debug & 2) {
736 1.1 gwr printf("se_dma_poll: done, csr=0x%x\n", se->se_csr);
737 1.1 gwr }
738 1.1 gwr #endif
739 1.1 gwr }
740 1.1 gwr
741