dma.c revision 1.14 1 1.14 lukem /* $NetBSD: dma.c,v 1.14 2003/07/15 03:36:14 lukem 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.14 lukem
33 1.14 lukem #include <sys/cdefs.h>
34 1.14 lukem __KERNEL_RCSID(0, "$NetBSD: dma.c,v 1.14 2003/07/15 03:36:14 lukem Exp $");
35 1.1 jeremy
36 1.1 jeremy #include <sys/types.h>
37 1.1 jeremy #include <sys/param.h>
38 1.1 jeremy #include <sys/systm.h>
39 1.1 jeremy #include <sys/kernel.h>
40 1.1 jeremy #include <sys/errno.h>
41 1.1 jeremy #include <sys/device.h>
42 1.1 jeremy #include <sys/malloc.h>
43 1.1 jeremy
44 1.1 jeremy #include <machine/autoconf.h>
45 1.1 jeremy #include <machine/dvma.h>
46 1.1 jeremy
47 1.6 bouyer #include <dev/scsipi/scsi_all.h>
48 1.6 bouyer #include <dev/scsipi/scsipi_all.h>
49 1.6 bouyer #include <dev/scsipi/scsiconf.h>
50 1.1 jeremy
51 1.4 gwr #include <dev/ic/ncr53c9xreg.h>
52 1.4 gwr #include <dev/ic/ncr53c9xvar.h>
53 1.4 gwr
54 1.8 gwr #include <sun3/dev/dmareg.h>
55 1.8 gwr #include <sun3/dev/dmavar.h>
56 1.1 jeremy
57 1.10 gwr #define MAX_DMA_SZ 0x01000000 /* 16MB */
58 1.10 gwr
59 1.10 gwr static int dmamatch __P((struct device *, struct cfdata *, void *));
60 1.10 gwr static void dmaattach __P((struct device *, struct device *, void *));
61 1.10 gwr
62 1.12 thorpej CFATTACH_DECL(dma, sizeof(struct dma_softc),
63 1.13 thorpej dmamatch, dmaattach, NULL, NULL);
64 1.10 gwr
65 1.10 gwr extern struct cfdriver dma_cd;
66 1.10 gwr
67 1.10 gwr static int
68 1.10 gwr dmamatch(parent, cf, aux)
69 1.10 gwr struct device *parent;
70 1.10 gwr struct cfdata *cf;
71 1.1 jeremy void *aux;
72 1.1 jeremy {
73 1.10 gwr struct confargs *ca = aux;
74 1.1 jeremy
75 1.1 jeremy /*
76 1.10 gwr * Check for the DMA registers.
77 1.1 jeremy */
78 1.10 gwr if (bus_peek(ca->ca_bustype, ca->ca_paddr, 4) == -1)
79 1.10 gwr return (0);
80 1.10 gwr
81 1.10 gwr /* If default ipl, fill it in. */
82 1.10 gwr if (ca->ca_intpri == -1)
83 1.10 gwr ca->ca_intpri = 2;
84 1.1 jeremy
85 1.10 gwr return (1);
86 1.10 gwr }
87 1.1 jeremy
88 1.10 gwr static void
89 1.10 gwr dmaattach(parent, self, aux)
90 1.10 gwr struct device *parent, *self;
91 1.10 gwr void *aux;
92 1.10 gwr {
93 1.10 gwr struct confargs *ca = aux;
94 1.10 gwr struct dma_softc *sc = (void *)self;
95 1.10 gwr int id;
96 1.4 gwr
97 1.4 gwr #if 0
98 1.4 gwr /* indirect functions */
99 1.4 gwr sc->intr = espdmaintr;
100 1.10 gwr sc->setup = dma_setup;
101 1.4 gwr sc->reset = dma_reset;
102 1.4 gwr #endif
103 1.4 gwr
104 1.10 gwr /*
105 1.10 gwr * Map in the registers.
106 1.10 gwr */
107 1.10 gwr sc->sc_regs = bus_mapin(ca->ca_bustype, ca->ca_paddr,
108 1.10 gwr sizeof(struct dma_regs));
109 1.10 gwr sc->sc_rev = DMACSR(sc) & D_DEV_ID;
110 1.10 gwr id = (sc->sc_rev >> 28) & 0xf;
111 1.10 gwr printf(": rev %d\n", id);
112 1.4 gwr
113 1.10 gwr /*
114 1.10 gwr * Make sure the DMA chip is supported revision.
115 1.10 gwr * The Sun3/80 used only the old rev zero chip,
116 1.10 gwr * so the initialization has been simplified.
117 1.10 gwr */
118 1.1 jeremy switch (sc->sc_rev) {
119 1.1 jeremy case DMAREV_0:
120 1.1 jeremy case DMAREV_1:
121 1.1 jeremy break;
122 1.1 jeremy default:
123 1.10 gwr panic("unsupported dma rev");
124 1.1 jeremy }
125 1.4 gwr }
126 1.1 jeremy
127 1.10 gwr /*
128 1.10 gwr * This is called by espattach to get our softc.
129 1.10 gwr */
130 1.10 gwr struct dma_softc *
131 1.10 gwr espdmafind(int unit)
132 1.10 gwr {
133 1.10 gwr if (unit < 0 || unit >= dma_cd.cd_ndevs ||
134 1.10 gwr dma_cd.cd_devs[unit] == NULL)
135 1.10 gwr panic("no dma");
136 1.10 gwr return (dma_cd.cd_devs[unit]);
137 1.10 gwr }
138 1.1 jeremy
139 1.1 jeremy #define DMAWAIT(SC, COND, MSG, DONTPANIC) do if (COND) { \
140 1.10 gwr int count = 100000; \
141 1.10 gwr while ((COND) && --count > 0) DELAY(5); \
142 1.1 jeremy if (count == 0) { \
143 1.10 gwr printf("%s: line %d: CSR = 0x%x\n", \
144 1.10 gwr __FILE__, __LINE__, DMACSR(SC)); \
145 1.1 jeremy if (DONTPANIC) \
146 1.1 jeremy printf(MSG); \
147 1.1 jeremy else \
148 1.1 jeremy panic(MSG); \
149 1.1 jeremy } \
150 1.1 jeremy } while (0)
151 1.1 jeremy
152 1.1 jeremy #define DMA_DRAIN(sc, dontpanic) do { \
153 1.1 jeremy /* \
154 1.1 jeremy * DMA rev0 & rev1: we are not allowed to touch the DMA "flush" \
155 1.1 jeremy * and "drain" bits while it is still thinking about a \
156 1.1 jeremy * request. \
157 1.1 jeremy * other revs: D_R_PEND bit reads as 0 \
158 1.1 jeremy */ \
159 1.10 gwr DMAWAIT(sc, DMACSR(sc) & D_R_PEND, "R_PEND", dontpanic); \
160 1.1 jeremy /* \
161 1.10 gwr * Select drain bit (always rev 0,1) \
162 1.1 jeremy * also clears errors and D_TC flag \
163 1.1 jeremy */ \
164 1.10 gwr DMACSR(sc) |= D_DRAIN; \
165 1.1 jeremy /* \
166 1.1 jeremy * Wait for draining to finish \
167 1.1 jeremy */ \
168 1.10 gwr DMAWAIT(sc, DMACSR(sc) & D_PACKCNT, "DRAINING", dontpanic); \
169 1.10 gwr } while(0)
170 1.10 gwr
171 1.10 gwr #define DMA_FLUSH(sc, dontpanic) do { \
172 1.10 gwr /* \
173 1.10 gwr * DMA rev0 & rev1: we are not allowed to touch the DMA "flush" \
174 1.10 gwr * and "drain" bits while it is still thinking about a \
175 1.10 gwr * request. \
176 1.10 gwr * other revs: D_R_PEND bit reads as 0 \
177 1.10 gwr */ \
178 1.10 gwr DMAWAIT(sc, DMACSR(sc) & D_R_PEND, "R_PEND", dontpanic); \
179 1.10 gwr DMACSR(sc) &= ~(D_WRITE|D_EN_DMA); \
180 1.10 gwr DMACSR(sc) |= D_FLUSH; \
181 1.1 jeremy } while(0)
182 1.1 jeremy
183 1.1 jeremy void
184 1.1 jeremy dma_reset(sc)
185 1.1 jeremy struct dma_softc *sc;
186 1.1 jeremy {
187 1.10 gwr
188 1.10 gwr DMA_FLUSH(sc, 1);
189 1.10 gwr DMACSR(sc) |= D_RESET; /* reset DMA */
190 1.10 gwr DELAY(200); /* what should this be ? */
191 1.1 jeremy /*DMAWAIT1(sc); why was this here? */
192 1.10 gwr DMACSR(sc) &= ~D_RESET; /* de-assert reset line */
193 1.10 gwr DELAY(5); /* allow a few ticks to settle */
194 1.1 jeremy
195 1.10 gwr /*
196 1.10 gwr * Get transfer burst size from (?) and plug it into the
197 1.10 gwr * controller registers. This is needed on the Sun4m...
198 1.10 gwr * Do we need it too? Apparently not, because the 3/80
199 1.10 gwr * always has the old, REV zero DMA chip.
200 1.10 gwr */
201 1.10 gwr DMACSR(sc) |= D_INT_EN; /* enable interrupts */
202 1.1 jeremy
203 1.10 gwr sc->sc_active = 0;
204 1.1 jeremy }
205 1.1 jeremy
206 1.1 jeremy
207 1.10 gwr #define DMAMAX(a) (MAX_DMA_SZ - ((a) & (MAX_DMA_SZ-1)))
208 1.1 jeremy
209 1.1 jeremy /*
210 1.1 jeremy * setup a dma transfer
211 1.1 jeremy */
212 1.1 jeremy int
213 1.1 jeremy dma_setup(sc, addr, len, datain, dmasize)
214 1.1 jeremy struct dma_softc *sc;
215 1.1 jeremy caddr_t *addr;
216 1.1 jeremy size_t *len;
217 1.1 jeremy int datain;
218 1.1 jeremy size_t *dmasize; /* IN-OUT */
219 1.1 jeremy {
220 1.10 gwr u_int32_t csr;
221 1.1 jeremy
222 1.10 gwr DMA_FLUSH(sc, 0);
223 1.1 jeremy
224 1.1 jeremy #if 0
225 1.1 jeremy DMACSR(sc) &= ~D_INT_EN;
226 1.1 jeremy #endif
227 1.1 jeremy sc->sc_dmaaddr = addr;
228 1.1 jeremy sc->sc_dmalen = len;
229 1.1 jeremy
230 1.4 gwr NCR_DMA(("%s: start %d@%p,%d\n", sc->sc_dev.dv_xname,
231 1.1 jeremy *sc->sc_dmalen, *sc->sc_dmaaddr, datain ? 1 : 0));
232 1.1 jeremy
233 1.1 jeremy /*
234 1.1 jeremy * the rules say we cannot transfer more than the limit
235 1.1 jeremy * of this DMA chip (64k for old and 16Mb for new),
236 1.1 jeremy * and we cannot cross a 16Mb boundary.
237 1.1 jeremy */
238 1.1 jeremy *dmasize = sc->sc_dmasize =
239 1.1 jeremy min(*dmasize, DMAMAX((size_t) *sc->sc_dmaaddr));
240 1.1 jeremy
241 1.4 gwr NCR_DMA(("dma_setup: dmasize = %d\n", sc->sc_dmasize));
242 1.1 jeremy
243 1.1 jeremy /* Program the DMA address */
244 1.1 jeremy if (sc->sc_dmasize) {
245 1.1 jeremy /*
246 1.1 jeremy * Use dvma mapin routines to map the buffer into DVMA space.
247 1.1 jeremy */
248 1.1 jeremy sc->sc_dvmaaddr = *sc->sc_dmaaddr;
249 1.1 jeremy sc->sc_dvmakaddr = dvma_mapin(sc->sc_dvmaaddr,
250 1.4 gwr sc->sc_dmasize, 0);
251 1.1 jeremy if (sc->sc_dvmakaddr == NULL)
252 1.1 jeremy panic("dma: cannot allocate DVMA address");
253 1.1 jeremy sc->sc_dmasaddr = dvma_kvtopa(sc->sc_dvmakaddr, BUS_OBIO);
254 1.1 jeremy DMADDR(sc) = sc->sc_dmasaddr;
255 1.10 gwr } else {
256 1.10 gwr /* XXX: What is this about? -gwr */
257 1.10 gwr DMADDR(sc) = (u_int32_t) *sc->sc_dmaaddr;
258 1.10 gwr }
259 1.10 gwr
260 1.10 gwr /* We never have DMAREV_ESC. */
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 /*
275 1.1 jeremy * Pseudo (chained) interrupt from the esp driver to kick the
276 1.10 gwr * current running DMA transfer. I am relying on espintr() to
277 1.1 jeremy * pickup and clean errors for now
278 1.1 jeremy *
279 1.1 jeremy * return 1 if it was a DMA continue.
280 1.1 jeremy */
281 1.1 jeremy int
282 1.1 jeremy espdmaintr(sc)
283 1.1 jeremy struct dma_softc *sc;
284 1.1 jeremy {
285 1.4 gwr struct ncr53c9x_softc *nsc = sc->sc_esp;
286 1.1 jeremy char bits[64];
287 1.1 jeremy int trans, resid;
288 1.10 gwr u_int32_t csr;
289 1.10 gwr
290 1.1 jeremy csr = DMACSR(sc);
291 1.1 jeremy
292 1.10 gwr NCR_DMA(("%s: intr: addr 0x%x, csr %s\n",
293 1.4 gwr sc->sc_dev.dv_xname, DMADDR(sc),
294 1.4 gwr bitmask_snprintf(csr, DMACSRBITS, bits, sizeof(bits))));
295 1.1 jeremy
296 1.1 jeremy if (csr & D_ERR_PEND) {
297 1.1 jeremy DMACSR(sc) &= ~D_EN_DMA; /* Stop DMA */
298 1.10 gwr DMACSR(sc) |= D_FLUSH;
299 1.1 jeremy printf("%s: error: csr=%s\n", sc->sc_dev.dv_xname,
300 1.1 jeremy bitmask_snprintf(csr, DMACSRBITS, bits, sizeof(bits)));
301 1.10 gwr return (-1);
302 1.1 jeremy }
303 1.1 jeremy
304 1.1 jeremy /* This is an "assertion" :) */
305 1.1 jeremy if (sc->sc_active == 0)
306 1.1 jeremy panic("dmaintr: DMA wasn't active");
307 1.1 jeremy
308 1.1 jeremy DMA_DRAIN(sc, 0);
309 1.1 jeremy
310 1.1 jeremy /* DMA has stopped */
311 1.1 jeremy DMACSR(sc) &= ~D_EN_DMA;
312 1.1 jeremy sc->sc_active = 0;
313 1.1 jeremy
314 1.1 jeremy if (sc->sc_dmasize == 0) {
315 1.1 jeremy /* A "Transfer Pad" operation completed */
316 1.4 gwr NCR_DMA(("dmaintr: discarded %d bytes (tcl=%d, tcm=%d)\n",
317 1.4 gwr NCR_READ_REG(nsc, NCR_TCL) |
318 1.4 gwr (NCR_READ_REG(nsc, NCR_TCM) << 8),
319 1.4 gwr NCR_READ_REG(nsc, NCR_TCL),
320 1.4 gwr NCR_READ_REG(nsc, NCR_TCM)));
321 1.1 jeremy return 0;
322 1.1 jeremy }
323 1.1 jeremy
324 1.1 jeremy resid = 0;
325 1.1 jeremy /*
326 1.1 jeremy * If a transfer onto the SCSI bus gets interrupted by the device
327 1.1 jeremy * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
328 1.1 jeremy * as residual since the ESP counter registers get decremented as
329 1.1 jeremy * bytes are clocked into the FIFO.
330 1.1 jeremy */
331 1.1 jeremy if (!(csr & D_WRITE) &&
332 1.4 gwr (resid = (NCR_READ_REG(nsc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
333 1.4 gwr NCR_DMA(("dmaintr: empty esp FIFO of %d ", resid));
334 1.1 jeremy }
335 1.1 jeremy
336 1.4 gwr if ((nsc->sc_espstat & NCRSTAT_TC) == 0) {
337 1.1 jeremy /*
338 1.1 jeremy * `Terminal count' is off, so read the residue
339 1.1 jeremy * out of the ESP counter registers.
340 1.1 jeremy */
341 1.4 gwr resid += (NCR_READ_REG(nsc, NCR_TCL) |
342 1.4 gwr (NCR_READ_REG(nsc, NCR_TCM) << 8) |
343 1.4 gwr ((nsc->sc_cfg2 & NCRCFG2_FE)
344 1.4 gwr ? (NCR_READ_REG(nsc, NCR_TCH) << 16)
345 1.1 jeremy : 0));
346 1.1 jeremy
347 1.1 jeremy if (resid == 0 && sc->sc_dmasize == 65536 &&
348 1.4 gwr (nsc->sc_cfg2 & NCRCFG2_FE) == 0)
349 1.1 jeremy /* A transfer of 64K is encoded as `TCL=TCM=0' */
350 1.1 jeremy resid = 65536;
351 1.1 jeremy }
352 1.1 jeremy
353 1.1 jeremy trans = sc->sc_dmasize - resid;
354 1.1 jeremy if (trans < 0) { /* transferred < 0 ? */
355 1.10 gwr #if 0
356 1.5 jeremy /*
357 1.5 jeremy * This situation can happen in perfectly normal operation
358 1.5 jeremy * if the ESP is reselected while using DMA to select
359 1.5 jeremy * another target. As such, don't print the warning.
360 1.5 jeremy */
361 1.1 jeremy printf("%s: xfer (%d) > req (%d)\n",
362 1.1 jeremy sc->sc_dev.dv_xname, trans, sc->sc_dmasize);
363 1.5 jeremy #endif
364 1.1 jeremy trans = sc->sc_dmasize;
365 1.1 jeremy }
366 1.1 jeremy
367 1.4 gwr NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
368 1.4 gwr NCR_READ_REG(nsc, NCR_TCL),
369 1.4 gwr NCR_READ_REG(nsc, NCR_TCM),
370 1.4 gwr (nsc->sc_cfg2 & NCRCFG2_FE)
371 1.4 gwr ? NCR_READ_REG(nsc, NCR_TCH) : 0,
372 1.1 jeremy trans, resid));
373 1.1 jeremy
374 1.1 jeremy #ifdef SUN3X_470_EVENTUALLY
375 1.1 jeremy if (csr & D_WRITE)
376 1.1 jeremy cache_flush(*sc->sc_dmaaddr, trans);
377 1.1 jeremy #endif
378 1.1 jeremy
379 1.1 jeremy if (sc->sc_dvmakaddr)
380 1.1 jeremy dvma_mapout(sc->sc_dvmakaddr, sc->sc_dmasize);
381 1.1 jeremy
382 1.1 jeremy *sc->sc_dmalen -= trans;
383 1.1 jeremy *sc->sc_dmaaddr += trans;
384 1.1 jeremy
385 1.1 jeremy #if 0 /* this is not normal operation just yet */
386 1.1 jeremy if (*sc->sc_dmalen == 0 ||
387 1.4 gwr nsc->sc_phase != nsc->sc_prevphase)
388 1.1 jeremy return 0;
389 1.1 jeremy
390 1.1 jeremy /* and again */
391 1.1 jeremy dma_start(sc, sc->sc_dmaaddr, sc->sc_dmalen, DMACSR(sc) & D_WRITE);
392 1.1 jeremy return 1;
393 1.1 jeremy #endif
394 1.1 jeremy return 0;
395 1.1 jeremy }
396