1 /* $NetBSD: intio_dmac.c,v 1.31 2008/06/25 13:30:24 isaki Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Hitachi HD63450 (= Motorola MC68450) DMAC driver for x68k.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: intio_dmac.c,v 1.31 2008/06/25 13:30:24 isaki Exp $");
39 #include "opt_m680x0.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <uvm/uvm_extern.h>
46 #include <machine/bus.h>
47 #include <machine/cpu.h>
48 #include <machine/frame.h>
50 #include <arch/x68k/dev/intiovar.h>
51 #include <arch/x68k/dev/dmacvar.h>
54 #define DPRINTF(n,x) if (dmacdebug>((n)&0x0f)) printf x
55 #define DDUMPREGS(n,x) if (dmacdebug>((n)&0x0f)) {printf x; dmac_dump_regs();}
59 #define DDUMPREGS(n,x)
62 static void dmac_init_channels(struct dmac_softc
*);
63 #ifdef DMAC_ARRAYCHAIN
64 static int dmac_program_arraychain(device_t
, struct dmac_dma_xfer
*,
67 static int dmac_done(void *);
68 static int dmac_error(void *);
71 static int dmac_dump_regs(void);
77 static int dmac_match(device_t
, cfdata_t
, void *);
78 static void dmac_attach(device_t
, device_t
, void *);
80 CFATTACH_DECL_NEW(dmac
, sizeof(struct dmac_softc
),
81 dmac_match
, dmac_attach
, NULL
, NULL
);
83 static int dmac_attached
;
86 dmac_match(device_t parent
, cfdata_t cf
, void *aux
)
88 struct intio_attach_args
*ia
= aux
;
90 if (strcmp(ia
->ia_name
, "dmac") != 0)
95 if (ia
->ia_addr
== INTIOCF_ADDR_DEFAULT
)
96 ia
->ia_addr
= DMAC_ADDR
;
99 if (ia
->ia_addr
!= DMAC_ADDR
)
101 if (ia
->ia_intr
!= INTIOCF_INTR_DEFAULT
)
108 dmac_attach(device_t parent
, device_t self
, void *aux
)
110 struct dmac_softc
*sc
= device_private(self
);
111 struct intio_attach_args
*ia
= aux
;
112 struct intio_softc
*intio
;
118 ia
->ia_size
= DMAC_CHAN_SIZE
* DMAC_NCHAN
;
119 r
= intio_map_allocate_region(parent
, ia
, INTIO_MAP_ALLOCATE
);
122 panic("IO map for DMAC corruption??");
125 intio
= device_private(parent
);
126 intio
->sc_dmac
= self
;
127 sc
->sc_bst
= ia
->ia_bst
;
128 bus_space_map(sc
->sc_bst
, ia
->ia_addr
, ia
->ia_size
, 0, &sc
->sc_bht
);
129 dmac_init_channels(sc
);
131 aprint_normal(": HD63450 DMAC\n");
132 aprint_normal_dev(self
, "4 channels available.\n");
136 dmac_init_channels(struct dmac_softc
*sc
)
140 DPRINTF(3, ("dmac_init_channels\n"));
141 for (i
=0; i
<DMAC_NCHAN
; i
++) {
142 sc
->sc_channels
[i
].ch_channel
= i
;
143 sc
->sc_channels
[i
].ch_name
[0] = 0;
144 sc
->sc_channels
[i
].ch_softc
= sc
;
145 bus_space_subregion(sc
->sc_bst
, sc
->sc_bht
,
146 DMAC_CHAN_SIZE
*i
, DMAC_CHAN_SIZE
,
147 &sc
->sc_channels
[i
].ch_bht
);
148 sc
->sc_channels
[i
].ch_xfer
.dx_dmamap
= 0;
149 /* reset the status register */
150 bus_space_write_1(sc
->sc_bst
, sc
->sc_channels
[i
].ch_bht
,
159 * Channel initialization/deinitialization per user device.
161 struct dmac_channel_stat
*
162 dmac_alloc_channel(device_t self
, int ch
, const char *name
, int normalv
,
163 dmac_intr_handler_t normal
, void *normalarg
, int errorv
,
164 dmac_intr_handler_t error
, void *errorarg
)
166 struct intio_softc
*intio
= device_private(self
);
167 struct dmac_softc
*dmac
= device_private(intio
->sc_dmac
);
168 struct dmac_channel_stat
*chan
= &dmac
->sc_channels
[ch
];
169 #ifdef DMAC_ARRAYCHAIN
173 aprint_normal_dev(dmac
->sc_dev
, "allocating ch %d for %s.\n",
175 DPRINTF(3, ("dmamap=%p\n", (void *)chan
->ch_xfer
.dx_dmamap
));
177 if (ch
< 0 || ch
>= DMAC_NCHAN
)
178 panic("Invalid DMAC channel.");
179 if (chan
->ch_name
[0])
180 panic("DMAC: channel in use.");
181 if (strlen(name
) > 8)
182 panic("DMAC: wrong user name.");
185 #ifdef DMAC_ARRAYCHAIN
186 /* allocate the DMAC arraychaining map */
187 r
= bus_dmamem_alloc(intio
->sc_dmat
,
188 sizeof(struct dmac_sg_array
) * DMAC_MAPSIZE
,
189 4, 0, &chan
->ch_seg
[0], 1, &dummy
,
192 panic("DMAC: cannot alloc DMA safe memory");
193 r
= bus_dmamem_map(intio
->sc_dmat
,
195 sizeof(struct dmac_sg_array
) * DMAC_MAPSIZE
,
196 (void **) &chan
->ch_map
,
197 BUS_DMA_NOWAIT
|BUS_DMA_COHERENT
);
199 panic("DMAC: cannot map DMA safe memory");
202 /* fill the channel status structure by the default values. */
203 strcpy(chan
->ch_name
, name
);
204 chan
->ch_dcr
= (DMAC_DCR_XRM_CSWH
| DMAC_DCR_OTYP_EASYNC
|
206 chan
->ch_ocr
= (DMAC_OCR_SIZE_BYTE
| DMAC_OCR_REQG_EXTERNAL
);
207 chan
->ch_normalv
= normalv
;
208 chan
->ch_errorv
= errorv
;
209 chan
->ch_normal
= normal
;
210 chan
->ch_error
= error
;
211 chan
->ch_normalarg
= normalarg
;
212 chan
->ch_errorarg
= errorarg
;
213 chan
->ch_xfer
.dx_dmamap
= 0;
215 /* setup the device-specific registers */
216 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
217 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
,
218 DMAC_REG_DCR
, chan
->ch_dcr
);
219 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CPR
, 0);
222 * X68k physical user space is a subset of the kernel space;
223 * the memory is always included in the physical user space,
224 * while the device is not.
226 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
,
227 DMAC_REG_BFCR
, DMAC_FC_USER_DATA
);
228 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
,
229 DMAC_REG_MFCR
, DMAC_FC_USER_DATA
);
230 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
,
231 DMAC_REG_DFCR
, DMAC_FC_KERNEL_DATA
);
233 /* setup the interrupt handlers */
234 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_NIVR
, normalv
);
235 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_EIVR
, errorv
);
237 intio_intr_establish_ext(normalv
, name
, "dma", dmac_done
, chan
);
238 intio_intr_establish_ext(errorv
, name
, "dmaerr", dmac_error
, chan
);
244 dmac_free_channel(device_t self
, int ch
, void *channel
)
246 struct intio_softc
*intio
= device_private(self
);
247 struct dmac_softc
*dmac
= device_private(intio
->sc_dmac
);
248 struct dmac_channel_stat
*chan
= &dmac
->sc_channels
[ch
];
250 DPRINTF(3, ("dmac_free_channel, %d\n", ch
));
251 DPRINTF(3, ("dmamap=%p\n", (void *)chan
->ch_xfer
.dx_dmamap
));
254 if (ch
!= chan
->ch_channel
)
257 #ifdef DMAC_ARRAYCHAIN
258 bus_dmamem_unmap(intio
->sc_dmat
, (void *)chan
->ch_map
,
259 sizeof(struct dmac_sg_array
) * DMAC_MAPSIZE
);
260 bus_dmamem_free(intio
->sc_dmat
, &chan
->ch_seg
[0], 1);
262 chan
->ch_name
[0] = 0;
263 intio_intr_disestablish(chan
->ch_normalv
, channel
);
264 intio_intr_disestablish(chan
->ch_errorv
, channel
);
270 * Initialization / deinitialization per transfer.
272 struct dmac_dma_xfer
*
273 dmac_alloc_xfer(struct dmac_channel_stat
*chan
, bus_dma_tag_t dmat
,
276 struct dmac_dma_xfer
*xf
= &chan
->ch_xfer
;
278 DPRINTF(3, ("dmac_alloc_xfer\n"));
279 xf
->dx_channel
= chan
;
280 xf
->dx_dmamap
= dmamap
;
282 #ifdef DMAC_ARRAYCHAIN
283 xf
->dx_array
= chan
->ch_map
;
286 xf
->dx_nextoff
= xf
->dx_nextsize
= -1;
291 dmac_load_xfer(struct dmac_softc
*dmac
, struct dmac_dma_xfer
*xf
)
293 struct dmac_channel_stat
*chan
= xf
->dx_channel
;
295 DPRINTF(3, ("dmac_load_xfer\n"));
297 xf
->dx_ocr
&= ~DMAC_OCR_CHAIN_MASK
;
298 if (xf
->dx_dmamap
->dm_nsegs
== 1)
299 xf
->dx_ocr
|= DMAC_OCR_CHAIN_DISABLED
;
301 xf
->dx_ocr
|= DMAC_OCR_CHAIN_ARRAY
;
303 xf
->dx_nextsize
= ~0;
306 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
307 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_SCR
, xf
->dx_scr
);
308 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
,
309 DMAC_REG_OCR
, (xf
->dx_ocr
| chan
->ch_ocr
));
310 bus_space_write_4(dmac
->sc_bst
, chan
->ch_bht
,
311 DMAC_REG_DAR
, (int) xf
->dx_device
);
316 struct dmac_dma_xfer
*
317 dmac_prepare_xfer(struct dmac_channel_stat
*chan
, bus_dma_tag_t dmat
,
318 bus_dmamap_t dmamap
, int dir
, int scr
, void *dar
)
320 struct dmac_dma_xfer
*xf
;
321 struct dmac_softc
*dmac
= chan
->ch_softc
;
323 xf
= dmac_alloc_xfer(chan
, dmat
, dmamap
);
325 xf
->dx_ocr
= dir
& DMAC_OCR_DIR_MASK
;
326 xf
->dx_scr
= scr
& (DMAC_SCR_MAC_MASK
|DMAC_SCR_DAC_MASK
);
329 dmac_load_xfer(dmac
, xf
);
335 static struct dmac_channel_stat
*debugchan
= 0;
339 * Do the actual transfer.
342 dmac_start_xfer(struct dmac_softc
*dmac
, struct dmac_dma_xfer
*xf
)
344 return dmac_start_xfer_offset(dmac
, xf
, 0, 0);
348 dmac_start_xfer_offset(struct dmac_softc
*dmac
, struct dmac_dma_xfer
*xf
,
349 u_int offset
, u_int size
)
351 struct dmac_channel_stat
*chan
= xf
->dx_channel
;
352 struct x68k_bus_dmamap
*dmamap
= xf
->dx_dmamap
;
353 int go
= DMAC_CCR_STR
|DMAC_CCR_INT
;
354 #ifdef DMAC_ARRAYCHAIN
358 DPRINTF(3, ("dmac_start_xfer\n"));
366 panic("dmac_start_xfer_offset: invalid offset %x",
369 size
= dmamap
->dm_mapsize
;
372 #ifdef DMAC_ARRAYCHAIN
375 panic("dmac_start_xfer: DMA transfer in progress");
378 DPRINTF(3, ("First program:\n"));
380 if ((offset
>= dmamap
->dm_mapsize
) ||
381 (offset
+ size
> dmamap
->dm_mapsize
))
382 panic("dmac_start_xfer_offset: invalid offset: "
383 "offset=%d, size=%d, mapsize=%ld",
384 offset
, size
, dmamap
->dm_mapsize
);
386 /* program DMAC in single block mode or array chainning mode */
387 if (dmamap
->dm_nsegs
== 1) {
388 DPRINTF(3, ("single block mode\n"));
390 if (dmamap
->dm_mapsize
!= dmamap
->dm_segs
[0].ds_len
)
391 panic("dmac_start_xfer_offset: dmamap curruption");
393 if (offset
== xf
->dx_nextoff
&&
394 size
== xf
->dx_nextsize
) {
395 /* Use continued operation */
397 xf
->dx_nextoff
+= size
;
399 bus_space_write_4(dmac
->sc_bst
, chan
->ch_bht
,
401 (int) dmamap
->dm_segs
[0].ds_addr
403 bus_space_write_2(dmac
->sc_bst
, chan
->ch_bht
,
404 DMAC_REG_MTCR
, (int) size
);
405 xf
->dx_nextoff
= offset
;
406 xf
->dx_nextsize
= size
;
408 #ifdef DMAC_ARRAYCHAIN
412 #ifdef DMAC_ARRAYCHAIN
413 c
= dmac_program_arraychain(self
, xf
, offset
, size
);
414 bus_space_write_4(dmac
->sc_bst
, chan
->ch_bht
,
415 DMAC_REG_BAR
, (int) chan
->ch_seg
[0].ds_addr
);
416 bus_space_write_2(dmac
->sc_bst
, chan
->ch_bht
,
419 panic("DMAC: unexpected use of arraychaining mode");
423 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
426 DDUMPREGS(3, ("first start\n"));
428 #ifdef DMAC_ARRAYCHAIN
429 #if defined(M68040) || defined(M68060)
430 /* flush data cache for the map */
431 if (dmamap
->dm_nsegs
!= 1 && mmutype
== MMU_68040
)
432 dma_cachectl((void *) xf
->dx_array
,
433 sizeof(struct dmac_sg_array
) * c
);
436 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CCR
, go
);
438 if (xf
->dx_nextoff
!= ~0) {
439 bus_space_write_4(dmac
->sc_bst
, chan
->ch_bht
,
440 DMAC_REG_BAR
, xf
->dx_nextoff
);
441 bus_space_write_2(dmac
->sc_bst
, chan
->ch_bht
,
442 DMAC_REG_BTCR
, xf
->dx_nextsize
);
448 #ifdef DMAC_ARRAYCHAIN
450 dmac_program_arraychain(device_t self
, struct dmac_dma_xfer
*xf
,
451 u_int offset
, u_int size
)
453 struct dmac_channel_stat
*chan
= xf
->dx_channel
;
454 int ch
= chan
->ch_channel
;
455 struct x68k_bus_dmamap
*map
= xf
->dx_dmamap
;
459 if (offset
!= 0 || size
!= map
->dm_mapsize
)
460 panic("dmac_program_arraychain: unsupported offset/size");
462 DPRINTF(3, ("dmac_program_arraychain\n"));
463 for (i
=0, j
=xf
->dx_done
; i
<DMAC_MAPSIZE
&& j
<map
->dm_nsegs
;
465 xf
->dx_array
[i
].da_addr
= map
->dm_segs
[j
].ds_addr
;
467 if (map
->dm_segs
[j
].ds_len
> DMAC_MAXSEGSZ
)
468 panic("dmac_program_arraychain: wrong map: %ld",
469 map
->dm_segs
[j
].ds_len
);
471 xf
->dx_array
[i
].da_count
= map
->dm_segs
[j
].ds_len
;
480 * interrupt handlers.
485 struct dmac_channel_stat
*chan
= arg
;
486 struct dmac_softc
*sc
= chan
->ch_softc
;
487 #ifdef DMAC_ARRAYCHAIN
488 struct dmac_dma_xfer
*xf
= &chan
->ch_xfer
;
489 struct x68k_bus_dmamap
*map
= xf
->dx_dmamap
;
493 DPRINTF(3, ("dmac_done\n"));
495 bus_space_write_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
497 #ifdef DMAC_ARRAYCHAIN
498 if (xf
->dx_done
== map
->dm_nsegs
) {
502 return (*chan
->ch_normal
)(chan
->ch_normalarg
);
503 #ifdef DMAC_ARRAYCHAIN
507 #ifdef DMAC_ARRAYCHAIN
508 /* Continue transfer */
509 DPRINTF(3, ("reprograming\n"));
510 c
= dmac_program_arraychain(sc
->sc_dev
, xf
, 0, map
->dm_mapsize
);
512 bus_space_write_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
513 bus_space_write_4(sc
->sc_bst
, chan
->ch_bht
,
514 DMAC_REG_BAR
, (int) chan
->ch_map
);
515 bus_space_write_4(sc
->sc_bst
, chan
->ch_bht
,
516 DMAC_REG_DAR
, (int) xf
->dx_device
);
517 bus_space_write_2(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_BTCR
, c
);
520 DDUMPREGS(3, ("restart\n"));
521 bus_space_write_1(sc
->sc_bst
, chan
->ch_bht
,
522 DMAC_REG_CCR
, DMAC_CCR_STR
|DMAC_CCR_INT
);
529 dmac_error(void *arg
)
531 struct dmac_channel_stat
*chan
= arg
;
532 struct dmac_softc
*sc
= chan
->ch_softc
;
534 printf("DMAC transfer error CSR=%02x, CER=%02x\n",
535 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
),
536 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CER
));
537 DDUMPREGS(3, ("registers were:\n"));
539 /* Clear the status bits */
540 bus_space_write_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
542 #ifdef DMAC_ARRAYCHAIN
543 chan
->ch_xfer
.dx_done
= 0;
546 return (*chan
->ch_error
)(chan
->ch_errorarg
);
550 dmac_abort_xfer(struct dmac_softc
*dmac
, struct dmac_dma_xfer
*xf
)
552 struct dmac_channel_stat
*chan
= xf
->dx_channel
;
554 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CCR
,
555 DMAC_CCR_INT
| DMAC_CCR_HLT
);
556 bus_space_write_1(dmac
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
, 0xff);
557 xf
->dx_nextoff
= xf
->dx_nextsize
= -1;
566 struct dmac_channel_stat
*chan
= debugchan
;
567 struct dmac_softc
*sc
;
569 if ((chan
== 0) || (dmacdebug
& 0xf0))
573 printf("DMAC channel %d registers\n", chan
->ch_channel
);
574 printf("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x, "
575 "CCR=%02x, CPR=%02x, GCR=%02x\n",
576 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CSR
),
577 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CER
),
578 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_DCR
),
579 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_OCR
),
580 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_SCR
),
581 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CCR
),
582 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_CPR
),
583 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_GCR
));
584 printf("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x, "
585 "MFCR=%02x, BFCR=%02x\n",
586 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_NIVR
),
587 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_EIVR
),
588 bus_space_read_2(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_MTCR
),
589 bus_space_read_2(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_BTCR
),
590 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_DFCR
),
591 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_MFCR
),
592 bus_space_read_1(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_BFCR
));
593 printf("DAR=%08x, MAR=%08x, BAR=%08x\n",
594 bus_space_read_4(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_DAR
),
595 bus_space_read_4(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_MAR
),
596 bus_space_read_4(sc
->sc_bst
, chan
->ch_bht
, DMAC_REG_BAR
));