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