dma.c revision 1.1 1 1.1 jeremy /* $NetBSD: dma.c,v 1.1 1997/02/24 01:45:13 jeremy Exp $ */
2 1.1 jeremy
3 1.1 jeremy /*
4 1.1 jeremy * Copyright (c) 1994 Paul Kranenburg. All rights reserved.
5 1.1 jeremy * Copyright (c) 1994 Peter Galbavy. All rights reserved.
6 1.1 jeremy *
7 1.1 jeremy * Redistribution and use in source and binary forms, with or without
8 1.1 jeremy * modification, are permitted provided that the following conditions
9 1.1 jeremy * are met:
10 1.1 jeremy * 1. Redistributions of source code must retain the above copyright
11 1.1 jeremy * notice, this list of conditions and the following disclaimer.
12 1.1 jeremy * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jeremy * notice, this list of conditions and the following disclaimer in the
14 1.1 jeremy * documentation and/or other materials provided with the distribution.
15 1.1 jeremy * 3. All advertising materials mentioning features or use of this software
16 1.1 jeremy * must display the following acknowledgement:
17 1.1 jeremy * This product includes software developed by Peter Galbavy.
18 1.1 jeremy * 4. The name of the author may not be used to endorse or promote products
19 1.1 jeremy * derived from this software without specific prior written permission.
20 1.1 jeremy *
21 1.1 jeremy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 jeremy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 jeremy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 jeremy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 jeremy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 jeremy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 jeremy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 jeremy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 jeremy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 jeremy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 jeremy */
32 1.1 jeremy
33 1.1 jeremy #include <sys/types.h>
34 1.1 jeremy #include <sys/param.h>
35 1.1 jeremy #include <sys/systm.h>
36 1.1 jeremy #include <sys/kernel.h>
37 1.1 jeremy #include <sys/errno.h>
38 1.1 jeremy #include <sys/ioctl.h>
39 1.1 jeremy #include <sys/device.h>
40 1.1 jeremy #include <sys/malloc.h>
41 1.1 jeremy #include <sys/buf.h>
42 1.1 jeremy #include <sys/proc.h>
43 1.1 jeremy #include <sys/user.h>
44 1.1 jeremy
45 1.1 jeremy #include <machine/autoconf.h>
46 1.1 jeremy #include <machine/dvma.h>
47 1.1 jeremy
48 1.1 jeremy #include <scsi/scsi_all.h>
49 1.1 jeremy #include <scsi/scsiconf.h>
50 1.1 jeremy
51 1.1 jeremy #include <sun3x/dev/dmareg.h>
52 1.1 jeremy #include <sun3x/dev/dmavar.h>
53 1.1 jeremy #include <sun3x/dev/espreg.h>
54 1.1 jeremy #include <sun3x/dev/espvar.h>
55 1.1 jeremy
56 1.1 jeremy void dmaattach __P((struct device *, struct device *, void *));
57 1.1 jeremy void dma_reset __P((struct dma_softc *));
58 1.1 jeremy void dma_enintr __P((struct dma_softc *));
59 1.1 jeremy int dma_isintr __P((struct dma_softc *));
60 1.1 jeremy int espdmaintr __P((struct dma_softc *));
61 1.1 jeremy int dma_setup __P((struct dma_softc *, caddr_t *, size_t *,
62 1.1 jeremy int, size_t *));
63 1.1 jeremy void dma_go __P((struct dma_softc *));
64 1.1 jeremy
65 1.1 jeremy #if 0
66 1.1 jeremy struct cfattach dma_ca = {
67 1.1 jeremy sizeof(struct dma_softc), dmamatch, dmaattach
68 1.1 jeremy };
69 1.1 jeremy #endif /* 0 */
70 1.1 jeremy
71 1.1 jeremy struct cfdriver dma_cd = {
72 1.1 jeremy NULL, "dma", DV_DULL
73 1.1 jeremy };
74 1.1 jeremy
75 1.1 jeremy /*
76 1.1 jeremy * Pseudo-attach function. Called from the esp driver during its
77 1.1 jeremy * attach function.
78 1.1 jeremy */
79 1.1 jeremy void
80 1.1 jeremy dmaattach(parent, self, aux)
81 1.1 jeremy struct device *parent, *self;
82 1.1 jeremy void *aux;
83 1.1 jeremy {
84 1.1 jeremy struct dma_softc *sc = (void *)self;
85 1.1 jeremy
86 1.1 jeremy /*
87 1.1 jeremy * The esp driver has filled in the virtual address used to
88 1.1 jeremy * address the dma registers at this point. Normally we would
89 1.1 jeremy * map them in here and assign them ourselves.
90 1.1 jeremy *
91 1.1 jeremy * It has also filled itself in to our sc->sc_esp register.
92 1.1 jeremy */
93 1.1 jeremy
94 1.1 jeremy /*
95 1.1 jeremy * Get transfer burst size from PROM and plug it into the
96 1.1 jeremy * controller registers. This is needed on the Sun4m; do
97 1.1 jeremy * others need it too?
98 1.1 jeremy *
99 1.1 jeremy * Sun3x works ok (so far) without it.
100 1.1 jeremy */
101 1.1 jeremy
102 1.1 jeremy printf(": rev ");
103 1.1 jeremy sc->sc_rev = sc->sc_regs->csr & D_DEV_ID;
104 1.1 jeremy switch (sc->sc_rev) {
105 1.1 jeremy case DMAREV_0:
106 1.1 jeremy printf("0");
107 1.1 jeremy break;
108 1.1 jeremy case DMAREV_ESC:
109 1.1 jeremy printf("esc");
110 1.1 jeremy break;
111 1.1 jeremy case DMAREV_1:
112 1.1 jeremy printf("1");
113 1.1 jeremy break;
114 1.1 jeremy case DMAREV_PLUS:
115 1.1 jeremy printf("1+");
116 1.1 jeremy break;
117 1.1 jeremy case DMAREV_2:
118 1.1 jeremy printf("2");
119 1.1 jeremy break;
120 1.1 jeremy default:
121 1.1 jeremy printf("unknown (0x%x)", sc->sc_rev);
122 1.1 jeremy }
123 1.1 jeremy printf("\n");
124 1.1 jeremy
125 1.1 jeremy /* indirect functions */
126 1.1 jeremy sc->intr = espdmaintr;
127 1.1 jeremy sc->enintr = dma_enintr;
128 1.1 jeremy sc->isintr = dma_isintr;
129 1.1 jeremy sc->reset = dma_reset;
130 1.1 jeremy sc->setup = dma_setup;
131 1.1 jeremy sc->go = dma_go;
132 1.1 jeremy }
133 1.1 jeremy
134 1.1 jeremy #define DMAWAIT(SC, COND, MSG, DONTPANIC) do if (COND) { \
135 1.1 jeremy int count = 500000; \
136 1.1 jeremy while ((COND) && --count > 0) DELAY(1); \
137 1.1 jeremy if (count == 0) { \
138 1.1 jeremy printf("%s: line %d: CSR = %lx\n", __FILE__, __LINE__, \
139 1.1 jeremy (SC)->sc_regs->csr); \
140 1.1 jeremy if (DONTPANIC) \
141 1.1 jeremy printf(MSG); \
142 1.1 jeremy else \
143 1.1 jeremy panic(MSG); \
144 1.1 jeremy } \
145 1.1 jeremy } while (0)
146 1.1 jeremy
147 1.1 jeremy #define DMA_DRAIN(sc, dontpanic) do { \
148 1.1 jeremy /* \
149 1.1 jeremy * DMA rev0 & rev1: we are not allowed to touch the DMA "flush" \
150 1.1 jeremy * and "drain" bits while it is still thinking about a \
151 1.1 jeremy * request. \
152 1.1 jeremy * other revs: D_R_PEND bit reads as 0 \
153 1.1 jeremy */ \
154 1.1 jeremy DMAWAIT(sc, sc->sc_regs->csr & D_R_PEND, "R_PEND", dontpanic); \
155 1.1 jeremy /* \
156 1.1 jeremy * Select drain bit based on revision \
157 1.1 jeremy * also clears errors and D_TC flag \
158 1.1 jeremy */ \
159 1.1 jeremy if (sc->sc_rev == DMAREV_1 || sc->sc_rev == DMAREV_0) \
160 1.1 jeremy DMACSR(sc) |= D_DRAIN; \
161 1.1 jeremy else \
162 1.1 jeremy DMACSR(sc) |= D_INVALIDATE; \
163 1.1 jeremy /* \
164 1.1 jeremy * Wait for draining to finish \
165 1.1 jeremy * rev0 & rev1 call this PACKCNT \
166 1.1 jeremy */ \
167 1.1 jeremy DMAWAIT(sc, sc->sc_regs->csr & D_DRAINING, "DRAINING", dontpanic);\
168 1.1 jeremy } while(0)
169 1.1 jeremy
170 1.1 jeremy void
171 1.1 jeremy dma_reset(sc)
172 1.1 jeremy struct dma_softc *sc;
173 1.1 jeremy {
174 1.1 jeremy DMA_DRAIN(sc, 1);
175 1.1 jeremy DMACSR(sc) &= ~D_EN_DMA; /* Stop DMA */
176 1.1 jeremy DMACSR(sc) |= D_RESET; /* reset DMA */
177 1.1 jeremy DELAY(200); /* what should this be ? */
178 1.1 jeremy /*DMAWAIT1(sc); why was this here? */
179 1.1 jeremy DMACSR(sc) &= ~D_RESET; /* de-assert reset line */
180 1.1 jeremy DMACSR(sc) |= D_INT_EN; /* enable interrupts */
181 1.1 jeremy if (sc->sc_rev > DMAREV_1) /* XXX - needed in 3x? */
182 1.1 jeremy DMACSR(sc) |= D_FASTER;
183 1.1 jeremy
184 1.1 jeremy sc->sc_active = 0; /* and of course we aren't */
185 1.1 jeremy }
186 1.1 jeremy
187 1.1 jeremy
188 1.1 jeremy void
189 1.1 jeremy dma_enintr(sc)
190 1.1 jeremy struct dma_softc *sc;
191 1.1 jeremy {
192 1.1 jeremy sc->sc_regs->csr |= D_INT_EN;
193 1.1 jeremy }
194 1.1 jeremy
195 1.1 jeremy int
196 1.1 jeremy dma_isintr(sc)
197 1.1 jeremy struct dma_softc *sc;
198 1.1 jeremy {
199 1.1 jeremy return (sc->sc_regs->csr & (D_INT_PEND|D_ERR_PEND));
200 1.1 jeremy }
201 1.1 jeremy
202 1.1 jeremy #define DMAMAX(a) (0x01000000 - ((a) & 0x00ffffff))
203 1.1 jeremy
204 1.1 jeremy
205 1.1 jeremy /*
206 1.1 jeremy * setup a dma transfer
207 1.1 jeremy */
208 1.1 jeremy int
209 1.1 jeremy dma_setup(sc, addr, len, datain, dmasize)
210 1.1 jeremy struct dma_softc *sc;
211 1.1 jeremy caddr_t *addr;
212 1.1 jeremy size_t *len;
213 1.1 jeremy int datain;
214 1.1 jeremy size_t *dmasize; /* IN-OUT */
215 1.1 jeremy {
216 1.1 jeremy u_long csr;
217 1.1 jeremy
218 1.1 jeremy DMA_DRAIN(sc, 0);
219 1.1 jeremy
220 1.1 jeremy #if 0
221 1.1 jeremy DMACSR(sc) &= ~D_INT_EN;
222 1.1 jeremy #endif
223 1.1 jeremy sc->sc_dmaaddr = addr;
224 1.1 jeremy sc->sc_dmalen = len;
225 1.1 jeremy
226 1.1 jeremy ESP_DMA(("%s: start %d@%p,%d\n", sc->sc_dev.dv_xname,
227 1.1 jeremy *sc->sc_dmalen, *sc->sc_dmaaddr, datain ? 1 : 0));
228 1.1 jeremy
229 1.1 jeremy /*
230 1.1 jeremy * the rules say we cannot transfer more than the limit
231 1.1 jeremy * of this DMA chip (64k for old and 16Mb for new),
232 1.1 jeremy * and we cannot cross a 16Mb boundary.
233 1.1 jeremy */
234 1.1 jeremy *dmasize = sc->sc_dmasize =
235 1.1 jeremy min(*dmasize, DMAMAX((size_t) *sc->sc_dmaaddr));
236 1.1 jeremy
237 1.1 jeremy ESP_DMA(("dma_setup: dmasize = %d\n", sc->sc_dmasize));
238 1.1 jeremy
239 1.1 jeremy /* Program the DMA address */
240 1.1 jeremy if (sc->sc_dmasize) {
241 1.1 jeremy /*
242 1.1 jeremy * Use dvma mapin routines to map the buffer into DVMA space.
243 1.1 jeremy */
244 1.1 jeremy sc->sc_dvmaaddr = *sc->sc_dmaaddr;
245 1.1 jeremy sc->sc_dvmakaddr = dvma_mapin(sc->sc_dvmaaddr,
246 1.1 jeremy sc->sc_dmasize, 0);
247 1.1 jeremy if (sc->sc_dvmakaddr == NULL)
248 1.1 jeremy panic("dma: cannot allocate DVMA address");
249 1.1 jeremy sc->sc_dmasaddr = dvma_kvtopa(sc->sc_dvmakaddr, BUS_OBIO);
250 1.1 jeremy DMADDR(sc) = sc->sc_dmasaddr;
251 1.1 jeremy } else
252 1.1 jeremy DMADDR(sc) = (u_long) *sc->sc_dmaaddr;
253 1.1 jeremy
254 1.1 jeremy if (sc->sc_rev == DMAREV_ESC) {
255 1.1 jeremy /* DMA ESC chip bug work-around */
256 1.1 jeremy register long bcnt = sc->sc_dmasize;
257 1.1 jeremy register long eaddr = bcnt + (long)*sc->sc_dmaaddr;
258 1.1 jeremy if ((eaddr & PGOFSET) != 0)
259 1.1 jeremy bcnt = roundup(bcnt, NBPG);
260 1.1 jeremy DMACNT(sc) = bcnt;
261 1.1 jeremy }
262 1.1 jeremy /* Setup DMA control register */
263 1.1 jeremy csr = DMACSR(sc);
264 1.1 jeremy if (datain)
265 1.1 jeremy csr |= D_WRITE;
266 1.1 jeremy else
267 1.1 jeremy csr &= ~D_WRITE;
268 1.1 jeremy csr |= D_INT_EN;
269 1.1 jeremy DMACSR(sc) = csr;
270 1.1 jeremy
271 1.1 jeremy return 0;
272 1.1 jeremy }
273 1.1 jeremy
274 1.1 jeremy void
275 1.1 jeremy dma_go(sc)
276 1.1 jeremy struct dma_softc *sc;
277 1.1 jeremy {
278 1.1 jeremy
279 1.1 jeremy /* Start DMA */
280 1.1 jeremy DMACSR(sc) |= D_EN_DMA;
281 1.1 jeremy sc->sc_active = 1;
282 1.1 jeremy }
283 1.1 jeremy
284 1.1 jeremy /*
285 1.1 jeremy * Pseudo (chained) interrupt from the esp driver to kick the
286 1.1 jeremy * current running DMA transfer. I am replying on espintr() to
287 1.1 jeremy * pickup and clean errors for now
288 1.1 jeremy *
289 1.1 jeremy * return 1 if it was a DMA continue.
290 1.1 jeremy */
291 1.1 jeremy int
292 1.1 jeremy espdmaintr(sc)
293 1.1 jeremy struct dma_softc *sc;
294 1.1 jeremy {
295 1.1 jeremy char bits[64];
296 1.1 jeremy int trans, resid;
297 1.1 jeremy u_long csr;
298 1.1 jeremy csr = DMACSR(sc);
299 1.1 jeremy
300 1.1 jeremy ESP_DMA(("%s: intr: addr %x, csr %s\n", sc->sc_dev.dv_xname,
301 1.1 jeremy DMADDR(sc), bitmask_snprintf(csr, DMACSRBITS, bits,
302 1.1 jeremy sizeof(bits))));
303 1.1 jeremy
304 1.1 jeremy if (csr & D_ERR_PEND) {
305 1.1 jeremy DMACSR(sc) &= ~D_EN_DMA; /* Stop DMA */
306 1.1 jeremy DMACSR(sc) |= D_INVALIDATE;
307 1.1 jeremy printf("%s: error: csr=%s\n", sc->sc_dev.dv_xname,
308 1.1 jeremy bitmask_snprintf(csr, DMACSRBITS, bits, sizeof(bits)));
309 1.1 jeremy return -1;
310 1.1 jeremy }
311 1.1 jeremy
312 1.1 jeremy /* This is an "assertion" :) */
313 1.1 jeremy if (sc->sc_active == 0)
314 1.1 jeremy panic("dmaintr: DMA wasn't active");
315 1.1 jeremy
316 1.1 jeremy DMA_DRAIN(sc, 0);
317 1.1 jeremy
318 1.1 jeremy /* DMA has stopped */
319 1.1 jeremy DMACSR(sc) &= ~D_EN_DMA;
320 1.1 jeremy sc->sc_active = 0;
321 1.1 jeremy
322 1.1 jeremy if (sc->sc_dmasize == 0) {
323 1.1 jeremy /* A "Transfer Pad" operation completed */
324 1.1 jeremy ESP_DMA(("dmaintr: discarded %d bytes (tcl=%d, tcm=%d)\n",
325 1.1 jeremy ESP_READ_REG(sc->sc_esp, ESP_TCL) |
326 1.1 jeremy (ESP_READ_REG(sc->sc_esp, ESP_TCM) << 8),
327 1.1 jeremy ESP_READ_REG(sc->sc_esp, ESP_TCL),
328 1.1 jeremy ESP_READ_REG(sc->sc_esp, ESP_TCM)));
329 1.1 jeremy return 0;
330 1.1 jeremy }
331 1.1 jeremy
332 1.1 jeremy resid = 0;
333 1.1 jeremy /*
334 1.1 jeremy * If a transfer onto the SCSI bus gets interrupted by the device
335 1.1 jeremy * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
336 1.1 jeremy * as residual since the ESP counter registers get decremented as
337 1.1 jeremy * bytes are clocked into the FIFO.
338 1.1 jeremy */
339 1.1 jeremy if (!(csr & D_WRITE) &&
340 1.1 jeremy (resid = (ESP_READ_REG(sc->sc_esp, ESP_FFLAG) & ESPFIFO_FF)) != 0) {
341 1.1 jeremy ESP_DMA(("dmaintr: empty esp FIFO of %d ", resid));
342 1.1 jeremy ESPCMD(sc->sc_esp, ESPCMD_FLUSH);
343 1.1 jeremy }
344 1.1 jeremy
345 1.1 jeremy if ((sc->sc_esp->sc_espstat & ESPSTAT_TC) == 0) {
346 1.1 jeremy /*
347 1.1 jeremy * `Terminal count' is off, so read the residue
348 1.1 jeremy * out of the ESP counter registers.
349 1.1 jeremy */
350 1.1 jeremy resid += ( ESP_READ_REG(sc->sc_esp, ESP_TCL) |
351 1.1 jeremy (ESP_READ_REG(sc->sc_esp, ESP_TCM) << 8) |
352 1.1 jeremy ((sc->sc_esp->sc_cfg2 & ESPCFG2_FE)
353 1.1 jeremy ? (ESP_READ_REG(sc->sc_esp, ESP_TCH) << 16)
354 1.1 jeremy : 0));
355 1.1 jeremy
356 1.1 jeremy if (resid == 0 && sc->sc_dmasize == 65536 &&
357 1.1 jeremy (sc->sc_esp->sc_cfg2 & ESPCFG2_FE) == 0)
358 1.1 jeremy /* A transfer of 64K is encoded as `TCL=TCM=0' */
359 1.1 jeremy resid = 65536;
360 1.1 jeremy }
361 1.1 jeremy
362 1.1 jeremy trans = sc->sc_dmasize - resid;
363 1.1 jeremy if (trans < 0) { /* transferred < 0 ? */
364 1.1 jeremy printf("%s: xfer (%d) > req (%d)\n",
365 1.1 jeremy sc->sc_dev.dv_xname, trans, sc->sc_dmasize);
366 1.1 jeremy trans = sc->sc_dmasize;
367 1.1 jeremy }
368 1.1 jeremy
369 1.1 jeremy ESP_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
370 1.1 jeremy ESP_READ_REG(sc->sc_esp, ESP_TCL),
371 1.1 jeremy ESP_READ_REG(sc->sc_esp, ESP_TCM),
372 1.1 jeremy (sc->sc_esp->sc_cfg2 & ESPCFG2_FE)
373 1.1 jeremy ? ESP_READ_REG(sc->sc_esp, ESP_TCH) : 0,
374 1.1 jeremy trans, resid));
375 1.1 jeremy
376 1.1 jeremy #ifdef SUN3X_470_EVENTUALLY
377 1.1 jeremy if (csr & D_WRITE)
378 1.1 jeremy cache_flush(*sc->sc_dmaaddr, trans);
379 1.1 jeremy #endif
380 1.1 jeremy
381 1.1 jeremy if (sc->sc_dvmakaddr)
382 1.1 jeremy dvma_mapout(sc->sc_dvmakaddr, sc->sc_dmasize);
383 1.1 jeremy
384 1.1 jeremy *sc->sc_dmalen -= trans;
385 1.1 jeremy *sc->sc_dmaaddr += trans;
386 1.1 jeremy
387 1.1 jeremy #if 0 /* this is not normal operation just yet */
388 1.1 jeremy if (*sc->sc_dmalen == 0 ||
389 1.1 jeremy sc->sc_esp->sc_phase != sc->sc_esp->sc_prevphase)
390 1.1 jeremy return 0;
391 1.1 jeremy
392 1.1 jeremy /* and again */
393 1.1 jeremy dma_start(sc, sc->sc_dmaaddr, sc->sc_dmalen, DMACSR(sc) & D_WRITE);
394 1.1 jeremy return 1;
395 1.1 jeremy #endif
396 1.1 jeremy return 0;
397 1.1 jeremy }
398