intio_dmac.c revision 1.11.8.4 1 1.11.8.4 nathanw /* $NetBSD: intio_dmac.c,v 1.11.8.4 2002/10/18 02:40:45 nathanw Exp $ */
2 1.11.8.2 nathanw
3 1.11.8.2 nathanw /*-
4 1.11.8.2 nathanw * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.11.8.2 nathanw * All rights reserved.
6 1.11.8.2 nathanw *
7 1.11.8.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.11.8.2 nathanw * by Minoura Makoto.
9 1.11.8.2 nathanw *
10 1.11.8.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.11.8.2 nathanw * modification, are permitted provided that the following conditions
12 1.11.8.2 nathanw * are met:
13 1.11.8.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.11.8.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.11.8.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.11.8.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.11.8.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.11.8.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.11.8.2 nathanw * must display the following acknowledgement:
20 1.11.8.2 nathanw * This product includes software developed by the NetBSD
21 1.11.8.2 nathanw * Foundation, Inc. and its contributors.
22 1.11.8.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.11.8.2 nathanw * contributors may be used to endorse or promote products derived
24 1.11.8.2 nathanw * from this software without specific prior written permission.
25 1.11.8.2 nathanw *
26 1.11.8.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.11.8.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.11.8.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.11.8.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.11.8.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.11.8.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.11.8.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.11.8.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.11.8.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.11.8.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.11.8.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.11.8.2 nathanw */
38 1.11.8.2 nathanw
39 1.11.8.2 nathanw /*
40 1.11.8.2 nathanw * Hitachi HD63450 (= Motorola MC68450) DMAC driver for x68k.
41 1.11.8.2 nathanw */
42 1.11.8.2 nathanw
43 1.11.8.2 nathanw #include "opt_m680x0.h"
44 1.11.8.2 nathanw
45 1.11.8.2 nathanw #include <sys/param.h>
46 1.11.8.2 nathanw #include <sys/systm.h>
47 1.11.8.2 nathanw #include <sys/device.h>
48 1.11.8.2 nathanw #include <uvm/uvm_extern.h>
49 1.11.8.2 nathanw
50 1.11.8.2 nathanw #include <machine/bus.h>
51 1.11.8.2 nathanw #include <machine/cpu.h>
52 1.11.8.2 nathanw #include <machine/frame.h>
53 1.11.8.2 nathanw
54 1.11.8.2 nathanw #include <arch/x68k/dev/intiovar.h>
55 1.11.8.2 nathanw #include <arch/x68k/dev/dmacvar.h>
56 1.11.8.2 nathanw
57 1.11.8.2 nathanw #ifdef DMAC_DEBUG
58 1.11.8.3 nathanw #define DPRINTF(n,x) if (dmacdebug>((n)&0x0f)) printf x
59 1.11.8.3 nathanw #define DDUMPREGS(n,x) if (dmacdebug>((n)&0x0f)) {printf x; dmac_dump_regs();}
60 1.11.8.2 nathanw int dmacdebug = 0;
61 1.11.8.2 nathanw #else
62 1.11.8.2 nathanw #define DPRINTF(n,x)
63 1.11.8.2 nathanw #define DDUMPREGS(n,x)
64 1.11.8.2 nathanw #endif
65 1.11.8.2 nathanw
66 1.11.8.2 nathanw static void dmac_init_channels __P((struct dmac_softc*));
67 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
68 1.11.8.2 nathanw static int dmac_program_arraychain __P((struct device*, struct dmac_dma_xfer*,
69 1.11.8.2 nathanw u_int, u_int));
70 1.11.8.2 nathanw #endif
71 1.11.8.2 nathanw static int dmac_done __P((void*));
72 1.11.8.2 nathanw static int dmac_error __P((void*));
73 1.11.8.2 nathanw
74 1.11.8.2 nathanw #ifdef DMAC_DEBUG
75 1.11.8.2 nathanw static int dmac_dump_regs __P((void));
76 1.11.8.2 nathanw #endif
77 1.11.8.2 nathanw
78 1.11.8.2 nathanw /*
79 1.11.8.2 nathanw * autoconf stuff
80 1.11.8.2 nathanw */
81 1.11.8.2 nathanw static int dmac_match __P((struct device *, struct cfdata *, void *));
82 1.11.8.2 nathanw static void dmac_attach __P((struct device *, struct device *, void *));
83 1.11.8.2 nathanw
84 1.11.8.4 nathanw CFATTACH_DECL(dmac, sizeof(struct dmac_softc),
85 1.11.8.4 nathanw dmac_match, dmac_attach, NULL, NULL);
86 1.11.8.2 nathanw
87 1.11.8.2 nathanw static int
88 1.11.8.2 nathanw dmac_match(parent, cf, aux)
89 1.11.8.2 nathanw struct device *parent;
90 1.11.8.2 nathanw struct cfdata *cf;
91 1.11.8.2 nathanw void *aux;
92 1.11.8.2 nathanw {
93 1.11.8.2 nathanw struct intio_attach_args *ia = aux;
94 1.11.8.2 nathanw
95 1.11.8.2 nathanw if (strcmp (ia->ia_name, "dmac") != 0)
96 1.11.8.2 nathanw return (0);
97 1.11.8.2 nathanw if (cf->cf_unit != 0)
98 1.11.8.2 nathanw return (0);
99 1.11.8.2 nathanw
100 1.11.8.2 nathanw if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
101 1.11.8.2 nathanw ia->ia_addr = DMAC_ADDR;
102 1.11.8.2 nathanw
103 1.11.8.2 nathanw /* fixed address */
104 1.11.8.2 nathanw if (ia->ia_addr != DMAC_ADDR)
105 1.11.8.2 nathanw return (0);
106 1.11.8.2 nathanw if (ia->ia_intr != INTIOCF_INTR_DEFAULT)
107 1.11.8.2 nathanw return (0);
108 1.11.8.2 nathanw
109 1.11.8.2 nathanw return 1;
110 1.11.8.2 nathanw }
111 1.11.8.2 nathanw
112 1.11.8.2 nathanw static void
113 1.11.8.2 nathanw dmac_attach(parent, self, aux)
114 1.11.8.2 nathanw struct device *parent, *self;
115 1.11.8.2 nathanw void *aux;
116 1.11.8.2 nathanw {
117 1.11.8.2 nathanw struct dmac_softc *sc = (struct dmac_softc *)self;
118 1.11.8.2 nathanw struct intio_attach_args *ia = aux;
119 1.11.8.2 nathanw int r;
120 1.11.8.2 nathanw
121 1.11.8.2 nathanw ia->ia_size = DMAC_CHAN_SIZE * DMAC_NCHAN;
122 1.11.8.2 nathanw r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
123 1.11.8.2 nathanw #ifdef DIAGNOSTIC
124 1.11.8.2 nathanw if (r)
125 1.11.8.2 nathanw panic ("IO map for DMAC corruption??");
126 1.11.8.2 nathanw #endif
127 1.11.8.2 nathanw
128 1.11.8.2 nathanw ((struct intio_softc*) parent)->sc_dmac = self;
129 1.11.8.2 nathanw sc->sc_bst = ia->ia_bst;
130 1.11.8.2 nathanw bus_space_map (sc->sc_bst, ia->ia_addr, ia->ia_size, 0, &sc->sc_bht);
131 1.11.8.2 nathanw dmac_init_channels(sc);
132 1.11.8.2 nathanw
133 1.11.8.2 nathanw printf (": HD63450 DMAC\n%s: 4 channels available.\n", self->dv_xname);
134 1.11.8.2 nathanw }
135 1.11.8.2 nathanw
136 1.11.8.2 nathanw static void
137 1.11.8.2 nathanw dmac_init_channels(sc)
138 1.11.8.2 nathanw struct dmac_softc *sc;
139 1.11.8.2 nathanw {
140 1.11.8.2 nathanw int i;
141 1.11.8.2 nathanw
142 1.11.8.2 nathanw DPRINTF (3, ("dmac_init_channels\n"));
143 1.11.8.2 nathanw for (i=0; i<DMAC_NCHAN; i++) {
144 1.11.8.2 nathanw sc->sc_channels[i].ch_channel = i;
145 1.11.8.2 nathanw sc->sc_channels[i].ch_name[0] = 0;
146 1.11.8.2 nathanw sc->sc_channels[i].ch_softc = &sc->sc_dev;
147 1.11.8.2 nathanw bus_space_subregion(sc->sc_bst, sc->sc_bht,
148 1.11.8.2 nathanw DMAC_CHAN_SIZE*i, DMAC_CHAN_SIZE,
149 1.11.8.2 nathanw &sc->sc_channels[i].ch_bht);
150 1.11.8.2 nathanw sc->sc_channels[i].ch_xfer.dx_dmamap = 0;
151 1.11.8.2 nathanw /* reset the status register */
152 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, sc->sc_channels[i].ch_bht,
153 1.11.8.2 nathanw DMAC_REG_CSR, 0xff);
154 1.11.8.2 nathanw }
155 1.11.8.2 nathanw
156 1.11.8.2 nathanw return;
157 1.11.8.2 nathanw }
158 1.11.8.2 nathanw
159 1.11.8.2 nathanw
160 1.11.8.2 nathanw /*
161 1.11.8.2 nathanw * Channel initialization/deinitialization per user device.
162 1.11.8.2 nathanw */
163 1.11.8.2 nathanw struct dmac_channel_stat *
164 1.11.8.2 nathanw dmac_alloc_channel(self, ch, name,
165 1.11.8.2 nathanw normalv, normal, normalarg,
166 1.11.8.2 nathanw errorv, error, errorarg)
167 1.11.8.2 nathanw struct device *self;
168 1.11.8.2 nathanw int ch;
169 1.11.8.2 nathanw char *name;
170 1.11.8.2 nathanw int normalv, errorv;
171 1.11.8.2 nathanw dmac_intr_handler_t normal, error;
172 1.11.8.2 nathanw void *normalarg, *errorarg;
173 1.11.8.2 nathanw {
174 1.11.8.2 nathanw struct intio_softc *intio = (void*) self;
175 1.11.8.2 nathanw struct dmac_softc *sc = (void*) intio->sc_dmac;
176 1.11.8.2 nathanw struct dmac_channel_stat *chan = &sc->sc_channels[ch];
177 1.11.8.2 nathanw char intrname[16];
178 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
179 1.11.8.2 nathanw int r, dummy;
180 1.11.8.2 nathanw #endif
181 1.11.8.2 nathanw
182 1.11.8.2 nathanw printf ("%s: allocating ch %d for %s.\n",
183 1.11.8.2 nathanw sc->sc_dev.dv_xname, ch, name);
184 1.11.8.2 nathanw DPRINTF (3, ("dmamap=%p\n", (void*) chan->ch_xfer.dx_dmamap));
185 1.11.8.2 nathanw #ifdef DIAGNOSTIC
186 1.11.8.2 nathanw if (ch < 0 || ch >= DMAC_NCHAN)
187 1.11.8.2 nathanw panic ("Invalid DMAC channel.");
188 1.11.8.2 nathanw if (chan->ch_name[0])
189 1.11.8.2 nathanw panic ("DMAC: channel in use.");
190 1.11.8.2 nathanw if (strlen(name) > 8)
191 1.11.8.2 nathanw panic ("DMAC: wrong user name.");
192 1.11.8.2 nathanw #endif
193 1.11.8.2 nathanw
194 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
195 1.11.8.2 nathanw /* allocate the DMAC arraychaining map */
196 1.11.8.2 nathanw r = bus_dmamem_alloc(intio->sc_dmat,
197 1.11.8.2 nathanw sizeof(struct dmac_sg_array) * DMAC_MAPSIZE,
198 1.11.8.2 nathanw 4, 0, &chan->ch_seg[0], 1, &dummy,
199 1.11.8.2 nathanw BUS_DMA_NOWAIT);
200 1.11.8.2 nathanw if (r)
201 1.11.8.2 nathanw panic ("DMAC: cannot alloc DMA safe memory");
202 1.11.8.2 nathanw r = bus_dmamem_map(intio->sc_dmat,
203 1.11.8.2 nathanw &chan->ch_seg[0], 1,
204 1.11.8.2 nathanw sizeof(struct dmac_sg_array) * DMAC_MAPSIZE,
205 1.11.8.2 nathanw (caddr_t*) &chan->ch_map,
206 1.11.8.2 nathanw BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
207 1.11.8.2 nathanw if (r)
208 1.11.8.2 nathanw panic ("DMAC: cannot map DMA safe memory");
209 1.11.8.2 nathanw #endif
210 1.11.8.2 nathanw
211 1.11.8.2 nathanw /* fill the channel status structure by the default values. */
212 1.11.8.2 nathanw strcpy(chan->ch_name, name);
213 1.11.8.2 nathanw chan->ch_dcr = (DMAC_DCR_XRM_CSWH | DMAC_DCR_OTYP_EASYNC |
214 1.11.8.2 nathanw DMAC_DCR_OPS_8BIT);
215 1.11.8.2 nathanw chan->ch_ocr = (DMAC_OCR_SIZE_BYTE | DMAC_OCR_REQG_EXTERNAL);
216 1.11.8.2 nathanw chan->ch_normalv = normalv;
217 1.11.8.2 nathanw chan->ch_errorv = errorv;
218 1.11.8.2 nathanw chan->ch_normal = normal;
219 1.11.8.2 nathanw chan->ch_error = error;
220 1.11.8.2 nathanw chan->ch_normalarg = normalarg;
221 1.11.8.2 nathanw chan->ch_errorarg = errorarg;
222 1.11.8.2 nathanw chan->ch_xfer.dx_dmamap = 0;
223 1.11.8.2 nathanw
224 1.11.8.2 nathanw /* setup the device-specific registers */
225 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
226 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht,
227 1.11.8.2 nathanw DMAC_REG_DCR, chan->ch_dcr);
228 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CPR, 0);
229 1.11.8.2 nathanw
230 1.11.8.2 nathanw /*
231 1.11.8.2 nathanw * X68k physical user space is a subset of the kernel space;
232 1.11.8.2 nathanw * the memory is always included in the physical user space,
233 1.11.8.2 nathanw * while the device is not.
234 1.11.8.2 nathanw */
235 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht,
236 1.11.8.2 nathanw DMAC_REG_BFCR, DMAC_FC_USER_DATA);
237 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht,
238 1.11.8.2 nathanw DMAC_REG_MFCR, DMAC_FC_USER_DATA);
239 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht,
240 1.11.8.2 nathanw DMAC_REG_DFCR, DMAC_FC_KERNEL_DATA);
241 1.11.8.2 nathanw
242 1.11.8.2 nathanw /* setup the interrupt handlers */
243 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR, normalv);
244 1.11.8.2 nathanw bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR, errorv);
245 1.11.8.2 nathanw
246 1.11.8.2 nathanw strcpy(intrname, name);
247 1.11.8.2 nathanw strcat(intrname, "dma");
248 1.11.8.2 nathanw intio_intr_establish (normalv, intrname, dmac_done, chan);
249 1.11.8.2 nathanw
250 1.11.8.2 nathanw strcpy(intrname, name);
251 1.11.8.2 nathanw strcat(intrname, "dmaerr");
252 1.11.8.2 nathanw intio_intr_establish (errorv, intrname, dmac_error, chan);
253 1.11.8.2 nathanw
254 1.11.8.2 nathanw return chan;
255 1.11.8.2 nathanw }
256 1.11.8.2 nathanw
257 1.11.8.2 nathanw int
258 1.11.8.2 nathanw dmac_free_channel(self, ch, channel)
259 1.11.8.2 nathanw struct device *self;
260 1.11.8.2 nathanw int ch;
261 1.11.8.2 nathanw void *channel;
262 1.11.8.2 nathanw {
263 1.11.8.2 nathanw struct intio_softc *intio = (void*) self;
264 1.11.8.2 nathanw struct dmac_softc *sc = (void*) intio->sc_dmac;
265 1.11.8.2 nathanw struct dmac_channel_stat *chan = &sc->sc_channels[ch];
266 1.11.8.2 nathanw
267 1.11.8.2 nathanw DPRINTF (3, ("dmac_free_channel, %d\n", ch));
268 1.11.8.2 nathanw DPRINTF (3, ("dmamap=%p\n", (void*) chan->ch_xfer.dx_dmamap));
269 1.11.8.2 nathanw if (chan != channel)
270 1.11.8.2 nathanw return -1;
271 1.11.8.2 nathanw if (ch != chan->ch_channel)
272 1.11.8.2 nathanw return -1;
273 1.11.8.2 nathanw
274 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
275 1.11.8.2 nathanw bus_dmamem_unmap(intio->sc_dmat, (caddr_t) chan->ch_map,
276 1.11.8.2 nathanw sizeof(struct dmac_sg_array) * DMAC_MAPSIZE);
277 1.11.8.2 nathanw bus_dmamem_free(intio->sc_dmat, &chan->ch_seg[0], 1);
278 1.11.8.2 nathanw #endif
279 1.11.8.2 nathanw chan->ch_name[0] = 0;
280 1.11.8.2 nathanw intio_intr_disestablish(chan->ch_normalv, channel);
281 1.11.8.2 nathanw intio_intr_disestablish(chan->ch_errorv, channel);
282 1.11.8.2 nathanw
283 1.11.8.2 nathanw return 0;
284 1.11.8.2 nathanw }
285 1.11.8.2 nathanw
286 1.11.8.2 nathanw /*
287 1.11.8.2 nathanw * Initialization / deinitialization per transfer.
288 1.11.8.2 nathanw */
289 1.11.8.2 nathanw struct dmac_dma_xfer *
290 1.11.8.2 nathanw dmac_alloc_xfer (chan, dmat, dmamap)
291 1.11.8.2 nathanw struct dmac_channel_stat *chan;
292 1.11.8.2 nathanw bus_dma_tag_t dmat;
293 1.11.8.2 nathanw bus_dmamap_t dmamap;
294 1.11.8.2 nathanw {
295 1.11.8.2 nathanw struct dmac_dma_xfer *xf = &chan->ch_xfer;
296 1.11.8.2 nathanw
297 1.11.8.2 nathanw DPRINTF (3, ("dmac_alloc_xfer\n"));
298 1.11.8.2 nathanw xf->dx_channel = chan;
299 1.11.8.2 nathanw xf->dx_dmamap = dmamap;
300 1.11.8.2 nathanw xf->dx_tag = dmat;
301 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
302 1.11.8.2 nathanw xf->dx_array = chan->ch_map;
303 1.11.8.2 nathanw xf->dx_done = 0;
304 1.11.8.2 nathanw #endif
305 1.11.8.2 nathanw xf->dx_nextoff = xf->dx_nextsize = -1;
306 1.11.8.2 nathanw return xf;
307 1.11.8.2 nathanw }
308 1.11.8.2 nathanw
309 1.11.8.2 nathanw int
310 1.11.8.2 nathanw dmac_load_xfer (self, xf)
311 1.11.8.2 nathanw struct device *self;
312 1.11.8.2 nathanw struct dmac_dma_xfer *xf;
313 1.11.8.2 nathanw {
314 1.11.8.2 nathanw struct dmac_softc *sc = (void*) self;
315 1.11.8.2 nathanw struct dmac_channel_stat *chan = xf->dx_channel;
316 1.11.8.2 nathanw
317 1.11.8.2 nathanw DPRINTF (3, ("dmac_load_xfer\n"));
318 1.11.8.2 nathanw
319 1.11.8.2 nathanw xf->dx_ocr &= ~DMAC_OCR_CHAIN_MASK;
320 1.11.8.2 nathanw if (xf->dx_dmamap->dm_nsegs == 1)
321 1.11.8.2 nathanw xf->dx_ocr |= DMAC_OCR_CHAIN_DISABLED;
322 1.11.8.2 nathanw else {
323 1.11.8.2 nathanw xf->dx_ocr |= DMAC_OCR_CHAIN_ARRAY;
324 1.11.8.2 nathanw xf->dx_nextoff = ~0;
325 1.11.8.2 nathanw xf->dx_nextsize = ~0;
326 1.11.8.2 nathanw }
327 1.11.8.2 nathanw
328 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
329 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR, xf->dx_scr);
330 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht,
331 1.11.8.2 nathanw DMAC_REG_OCR, (xf->dx_ocr | chan->ch_ocr));
332 1.11.8.2 nathanw bus_space_write_4(sc->sc_bst, chan->ch_bht,
333 1.11.8.2 nathanw DMAC_REG_DAR, (int) xf->dx_device);
334 1.11.8.2 nathanw
335 1.11.8.2 nathanw return 0;
336 1.11.8.2 nathanw }
337 1.11.8.2 nathanw
338 1.11.8.2 nathanw struct dmac_dma_xfer *
339 1.11.8.2 nathanw dmac_prepare_xfer (chan, dmat, dmamap, dir, scr, dar)
340 1.11.8.2 nathanw struct dmac_channel_stat *chan;
341 1.11.8.2 nathanw bus_dma_tag_t dmat;
342 1.11.8.2 nathanw bus_dmamap_t dmamap;
343 1.11.8.2 nathanw int dir, scr;
344 1.11.8.2 nathanw void *dar;
345 1.11.8.2 nathanw {
346 1.11.8.2 nathanw struct dmac_dma_xfer *xf;
347 1.11.8.2 nathanw struct dmac_softc *sc = (struct dmac_softc*) chan->ch_softc;
348 1.11.8.2 nathanw
349 1.11.8.2 nathanw xf = dmac_alloc_xfer(chan, dmat, dmamap);
350 1.11.8.2 nathanw
351 1.11.8.2 nathanw xf->dx_ocr = dir & DMAC_OCR_DIR_MASK;
352 1.11.8.2 nathanw xf->dx_scr = scr & (DMAC_SCR_MAC_MASK|DMAC_SCR_DAC_MASK);
353 1.11.8.2 nathanw xf->dx_device = dar;
354 1.11.8.2 nathanw
355 1.11.8.2 nathanw dmac_load_xfer(&sc->sc_dev, xf);
356 1.11.8.2 nathanw
357 1.11.8.2 nathanw return xf;
358 1.11.8.2 nathanw }
359 1.11.8.2 nathanw
360 1.11.8.2 nathanw #ifdef DMAC_DEBUG
361 1.11.8.2 nathanw static struct dmac_channel_stat *debugchan = 0;
362 1.11.8.2 nathanw #endif
363 1.11.8.2 nathanw
364 1.11.8.2 nathanw /*
365 1.11.8.2 nathanw * Do the actual transfer.
366 1.11.8.2 nathanw */
367 1.11.8.2 nathanw int
368 1.11.8.2 nathanw dmac_start_xfer(self, xf)
369 1.11.8.2 nathanw struct device *self;
370 1.11.8.2 nathanw struct dmac_dma_xfer *xf;
371 1.11.8.2 nathanw {
372 1.11.8.2 nathanw return dmac_start_xfer_offset(self, xf, 0, 0);
373 1.11.8.2 nathanw }
374 1.11.8.2 nathanw
375 1.11.8.2 nathanw int
376 1.11.8.2 nathanw dmac_start_xfer_offset(self, xf, offset, size)
377 1.11.8.2 nathanw struct device *self;
378 1.11.8.2 nathanw struct dmac_dma_xfer *xf;
379 1.11.8.2 nathanw u_int offset;
380 1.11.8.2 nathanw u_int size;
381 1.11.8.2 nathanw {
382 1.11.8.2 nathanw struct dmac_softc *sc = (void*) self;
383 1.11.8.2 nathanw struct dmac_channel_stat *chan = xf->dx_channel;
384 1.11.8.2 nathanw struct x68k_bus_dmamap *dmamap = xf->dx_dmamap;
385 1.11.8.2 nathanw int go = DMAC_CCR_STR|DMAC_CCR_INT;
386 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
387 1.11.8.2 nathanw int c;
388 1.11.8.2 nathanw #endif
389 1.11.8.2 nathanw
390 1.11.8.2 nathanw DPRINTF (3, ("dmac_start_xfer\n"));
391 1.11.8.2 nathanw #ifdef DMAC_DEBUG
392 1.11.8.2 nathanw debugchan=chan;
393 1.11.8.2 nathanw #endif
394 1.11.8.2 nathanw
395 1.11.8.2 nathanw if (size == 0) {
396 1.11.8.2 nathanw #ifdef DIAGNOSTIC
397 1.11.8.2 nathanw if (offset != 0)
398 1.11.8.2 nathanw panic ("dmac_start_xfer_offset: invalid offset %x",
399 1.11.8.2 nathanw offset);
400 1.11.8.2 nathanw #endif
401 1.11.8.2 nathanw size = dmamap->dm_mapsize;
402 1.11.8.2 nathanw }
403 1.11.8.2 nathanw
404 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
405 1.11.8.2 nathanw #ifdef DIAGNOSTIC
406 1.11.8.2 nathanw if (xf->dx_done)
407 1.11.8.2 nathanw panic("dmac_start_xfer: DMA transfer in progress");
408 1.11.8.2 nathanw #endif
409 1.11.8.2 nathanw #endif
410 1.11.8.2 nathanw DPRINTF (3, ("First program:\n"));
411 1.11.8.2 nathanw #ifdef DIAGNOSTIC
412 1.11.8.2 nathanw if ((offset >= dmamap->dm_mapsize) ||
413 1.11.8.2 nathanw (offset + size > dmamap->dm_mapsize))
414 1.11.8.2 nathanw panic ("dmac_start_xfer_offset: invalid offset: "
415 1.11.8.3 nathanw "offset=%d, size=%d, mapsize=%ld",
416 1.11.8.2 nathanw offset, size, dmamap->dm_mapsize);
417 1.11.8.2 nathanw #endif
418 1.11.8.2 nathanw /* program DMAC in single block mode or array chainning mode */
419 1.11.8.2 nathanw if (dmamap->dm_nsegs == 1) {
420 1.11.8.2 nathanw DPRINTF(3, ("single block mode\n"));
421 1.11.8.2 nathanw #ifdef DIAGNOSTIC
422 1.11.8.2 nathanw if (dmamap->dm_mapsize != dmamap->dm_segs[0].ds_len)
423 1.11.8.2 nathanw panic ("dmac_start_xfer_offset: dmamap curruption");
424 1.11.8.2 nathanw #endif
425 1.11.8.2 nathanw if (offset == xf->dx_nextoff &&
426 1.11.8.2 nathanw size == xf->dx_nextsize) {
427 1.11.8.2 nathanw /* Use continued operation */
428 1.11.8.2 nathanw go |= DMAC_CCR_CNT;
429 1.11.8.2 nathanw xf->dx_nextoff += size;
430 1.11.8.2 nathanw } else {
431 1.11.8.2 nathanw bus_space_write_4(sc->sc_bst, chan->ch_bht,
432 1.11.8.2 nathanw DMAC_REG_MAR,
433 1.11.8.2 nathanw (int) dmamap->dm_segs[0].ds_addr
434 1.11.8.2 nathanw + offset);
435 1.11.8.2 nathanw bus_space_write_2(sc->sc_bst, chan->ch_bht,
436 1.11.8.2 nathanw DMAC_REG_MTCR, (int) size);
437 1.11.8.2 nathanw xf->dx_nextoff = offset;
438 1.11.8.2 nathanw xf->dx_nextsize = size;
439 1.11.8.2 nathanw }
440 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
441 1.11.8.2 nathanw xf->dx_done = 1;
442 1.11.8.2 nathanw #endif
443 1.11.8.2 nathanw } else {
444 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
445 1.11.8.2 nathanw c = dmac_program_arraychain(self, xf, offset, size);
446 1.11.8.2 nathanw bus_space_write_4(sc->sc_bst, chan->ch_bht,
447 1.11.8.2 nathanw DMAC_REG_BAR, (int) chan->ch_seg[0].ds_addr);
448 1.11.8.2 nathanw bus_space_write_2(sc->sc_bst, chan->ch_bht,
449 1.11.8.2 nathanw DMAC_REG_BTCR, c);
450 1.11.8.2 nathanw #else
451 1.11.8.2 nathanw panic ("DMAC: unexpected use of arraychaining mode");
452 1.11.8.2 nathanw #endif
453 1.11.8.2 nathanw }
454 1.11.8.2 nathanw
455 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
456 1.11.8.2 nathanw
457 1.11.8.2 nathanw /* START!! */
458 1.11.8.2 nathanw DDUMPREGS (3, ("first start\n"));
459 1.11.8.4 nathanw
460 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
461 1.11.8.2 nathanw #if defined(M68040) || defined(M68060)
462 1.11.8.2 nathanw /* flush data cache for the map */
463 1.11.8.2 nathanw if (dmamap->dm_nsegs != 1 && mmutype == MMU_68040)
464 1.11.8.2 nathanw dma_cachectl((caddr_t) xf->dx_array,
465 1.11.8.2 nathanw sizeof(struct dmac_sg_array) * c);
466 1.11.8.2 nathanw #endif
467 1.11.8.2 nathanw #endif
468 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR, go);
469 1.11.8.2 nathanw
470 1.11.8.2 nathanw if (xf->dx_nextoff != ~0) {
471 1.11.8.2 nathanw bus_space_write_4(sc->sc_bst, chan->ch_bht,
472 1.11.8.2 nathanw DMAC_REG_BAR, xf->dx_nextoff);
473 1.11.8.2 nathanw bus_space_write_2(sc->sc_bst, chan->ch_bht,
474 1.11.8.2 nathanw DMAC_REG_BTCR, xf->dx_nextsize);
475 1.11.8.2 nathanw }
476 1.11.8.2 nathanw
477 1.11.8.2 nathanw return 0;
478 1.11.8.2 nathanw }
479 1.11.8.2 nathanw
480 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
481 1.11.8.2 nathanw static int
482 1.11.8.2 nathanw dmac_program_arraychain(self, xf, offset, size)
483 1.11.8.2 nathanw struct device *self;
484 1.11.8.2 nathanw struct dmac_dma_xfer *xf;
485 1.11.8.2 nathanw u_int offset;
486 1.11.8.2 nathanw u_int size;
487 1.11.8.2 nathanw {
488 1.11.8.2 nathanw struct dmac_channel_stat *chan = xf->dx_channel;
489 1.11.8.2 nathanw int ch = chan->ch_channel;
490 1.11.8.2 nathanw struct x68k_bus_dmamap *map = xf->dx_dmamap;
491 1.11.8.2 nathanw int i, j;
492 1.11.8.2 nathanw
493 1.11.8.2 nathanw /* XXX not yet!! */
494 1.11.8.2 nathanw if (offset != 0 || size != map->dm_mapsize)
495 1.11.8.2 nathanw panic ("dmac_program_arraychain: unsupported offset/size");
496 1.11.8.2 nathanw
497 1.11.8.2 nathanw DPRINTF (3, ("dmac_program_arraychain\n"));
498 1.11.8.2 nathanw for (i=0, j=xf->dx_done; i<DMAC_MAPSIZE && j<map->dm_nsegs;
499 1.11.8.2 nathanw i++, j++) {
500 1.11.8.2 nathanw xf->dx_array[i].da_addr = map->dm_segs[j].ds_addr;
501 1.11.8.2 nathanw #ifdef DIAGNOSTIC
502 1.11.8.2 nathanw if (map->dm_segs[j].ds_len > DMAC_MAXSEGSZ)
503 1.11.8.2 nathanw panic ("dmac_program_arraychain: wrong map: %ld",
504 1.11.8.2 nathanw map->dm_segs[j].ds_len);
505 1.11.8.2 nathanw #endif
506 1.11.8.2 nathanw xf->dx_array[i].da_count = map->dm_segs[j].ds_len;
507 1.11.8.2 nathanw }
508 1.11.8.2 nathanw xf->dx_done = j;
509 1.11.8.2 nathanw
510 1.11.8.2 nathanw return i;
511 1.11.8.2 nathanw }
512 1.11.8.2 nathanw #endif
513 1.11.8.2 nathanw
514 1.11.8.2 nathanw /*
515 1.11.8.2 nathanw * interrupt handlers.
516 1.11.8.2 nathanw */
517 1.11.8.2 nathanw static int
518 1.11.8.2 nathanw dmac_done(arg)
519 1.11.8.2 nathanw void *arg;
520 1.11.8.2 nathanw {
521 1.11.8.2 nathanw struct dmac_channel_stat *chan = arg;
522 1.11.8.2 nathanw struct dmac_softc *sc = (void*) chan->ch_softc;
523 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
524 1.11.8.2 nathanw struct dmac_dma_xfer *xf = &chan->ch_xfer;
525 1.11.8.2 nathanw struct x68k_bus_dmamap *map = xf->dx_dmamap;
526 1.11.8.2 nathanw int c;
527 1.11.8.2 nathanw #endif
528 1.11.8.2 nathanw
529 1.11.8.2 nathanw DPRINTF (3, ("dmac_done\n"));
530 1.11.8.2 nathanw
531 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
532 1.11.8.2 nathanw
533 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
534 1.11.8.2 nathanw if (xf->dx_done == map->dm_nsegs) {
535 1.11.8.2 nathanw xf->dx_done = 0;
536 1.11.8.2 nathanw #endif
537 1.11.8.2 nathanw /* Done */
538 1.11.8.2 nathanw return (*chan->ch_normal) (chan->ch_normalarg);
539 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
540 1.11.8.2 nathanw }
541 1.11.8.2 nathanw #endif
542 1.11.8.2 nathanw
543 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
544 1.11.8.2 nathanw /* Continue transfer */
545 1.11.8.2 nathanw DPRINTF (3, ("reprograming\n"));
546 1.11.8.2 nathanw c = dmac_program_arraychain (&sc->sc_dev, xf, 0, map->dm_mapsize);
547 1.11.8.2 nathanw
548 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
549 1.11.8.2 nathanw bus_space_write_4(sc->sc_bst, chan->ch_bht,
550 1.11.8.2 nathanw DMAC_REG_BAR, (int) chan->ch_map);
551 1.11.8.2 nathanw bus_space_write_4(sc->sc_bst, chan->ch_bht,
552 1.11.8.2 nathanw DMAC_REG_DAR, (int) xf->dx_device);
553 1.11.8.2 nathanw bus_space_write_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR, c);
554 1.11.8.2 nathanw
555 1.11.8.2 nathanw /* START!! */
556 1.11.8.2 nathanw DDUMPREGS (3, ("restart\n"));
557 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht,
558 1.11.8.2 nathanw DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
559 1.11.8.2 nathanw
560 1.11.8.2 nathanw return 1;
561 1.11.8.2 nathanw #endif
562 1.11.8.2 nathanw }
563 1.11.8.2 nathanw
564 1.11.8.2 nathanw static int
565 1.11.8.2 nathanw dmac_error(arg)
566 1.11.8.2 nathanw void *arg;
567 1.11.8.2 nathanw {
568 1.11.8.2 nathanw struct dmac_channel_stat *chan = arg;
569 1.11.8.2 nathanw struct dmac_softc *sc = (void*) chan->ch_softc;
570 1.11.8.2 nathanw
571 1.11.8.2 nathanw printf ("DMAC transfer error CSR=%02x, CER=%02x\n",
572 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
573 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER));
574 1.11.8.4 nathanw DDUMPREGS(3, ("registers were:\n"));
575 1.11.8.2 nathanw
576 1.11.8.2 nathanw /* Clear the status bits */
577 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
578 1.11.8.2 nathanw
579 1.11.8.2 nathanw #ifdef DMAC_ARRAYCHAIN
580 1.11.8.2 nathanw chan->ch_xfer.dx_done = 0;
581 1.11.8.2 nathanw #endif
582 1.11.8.2 nathanw
583 1.11.8.2 nathanw return (*chan->ch_error) (chan->ch_errorarg);
584 1.11.8.2 nathanw }
585 1.11.8.2 nathanw
586 1.11.8.2 nathanw int
587 1.11.8.2 nathanw dmac_abort_xfer(self, xf)
588 1.11.8.2 nathanw struct device *self;
589 1.11.8.2 nathanw struct dmac_dma_xfer *xf;
590 1.11.8.2 nathanw {
591 1.11.8.2 nathanw struct dmac_softc *sc = (void*) self;
592 1.11.8.2 nathanw struct dmac_channel_stat *chan = xf->dx_channel;
593 1.11.8.2 nathanw
594 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR,
595 1.11.8.2 nathanw DMAC_CCR_INT | DMAC_CCR_HLT);
596 1.11.8.2 nathanw bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
597 1.11.8.2 nathanw xf->dx_nextoff = xf->dx_nextsize = -1;
598 1.11.8.2 nathanw
599 1.11.8.2 nathanw return 0;
600 1.11.8.2 nathanw }
601 1.11.8.2 nathanw
602 1.11.8.2 nathanw #ifdef DMAC_DEBUG
603 1.11.8.2 nathanw static int
604 1.11.8.2 nathanw dmac_dump_regs(void)
605 1.11.8.2 nathanw {
606 1.11.8.2 nathanw struct dmac_channel_stat *chan = debugchan;
607 1.11.8.2 nathanw struct dmac_softc *sc;
608 1.11.8.2 nathanw
609 1.11.8.3 nathanw if ((chan == 0) || (dmacdebug & 0xf0))
610 1.11.8.3 nathanw return 0;
611 1.11.8.2 nathanw sc = (void*) chan->ch_softc;
612 1.11.8.2 nathanw
613 1.11.8.2 nathanw printf ("DMAC channel %d registers\n", chan->ch_channel);
614 1.11.8.4 nathanw printf ("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x, "
615 1.11.8.2 nathanw "CCR=%02x, CPR=%02x, GCR=%02x\n",
616 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
617 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER),
618 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR),
619 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR),
620 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR),
621 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR),
622 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR),
623 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR));
624 1.11.8.4 nathanw printf ("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x, "
625 1.11.8.2 nathanw "MFCR=%02x, BFCR=%02x\n",
626 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR),
627 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR),
628 1.11.8.2 nathanw bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR),
629 1.11.8.2 nathanw bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR),
630 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR),
631 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR),
632 1.11.8.2 nathanw bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR));
633 1.11.8.2 nathanw printf ("DAR=%08x, MAR=%08x, BAR=%08x\n",
634 1.11.8.2 nathanw bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR),
635 1.11.8.2 nathanw bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR),
636 1.11.8.2 nathanw bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR));
637 1.11.8.2 nathanw
638 1.11.8.2 nathanw return 0;
639 1.11.8.2 nathanw }
640 1.11.8.2 nathanw #endif
641