Home | History | Annotate | Line # | Download | only in dev
intio_dmac.c revision 1.5.2.1
      1  1.5.2.1   bouyer /*	$NetBSD: intio_dmac.c,v 1.5.2.1 2000/11/20 20:29:57 bouyer Exp $	*/
      2      1.2  minoura 
      3      1.2  minoura /*-
      4      1.2  minoura  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5      1.2  minoura  * All rights reserved.
      6      1.2  minoura  *
      7      1.2  minoura  * This code is derived from software contributed to The NetBSD Foundation
      8      1.2  minoura  * by Minoura Makoto.
      9      1.2  minoura  *
     10      1.2  minoura  * Redistribution and use in source and binary forms, with or without
     11      1.2  minoura  * modification, are permitted provided that the following conditions
     12      1.2  minoura  * are met:
     13      1.2  minoura  * 1. Redistributions of source code must retain the above copyright
     14      1.2  minoura  *    notice, this list of conditions and the following disclaimer.
     15      1.2  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.2  minoura  *    notice, this list of conditions and the following disclaimer in the
     17      1.2  minoura  *    documentation and/or other materials provided with the distribution.
     18      1.2  minoura  * 3. All advertising materials mentioning features or use of this software
     19      1.2  minoura  *    must display the following acknowledgement:
     20      1.2  minoura  *	This product includes software developed by the NetBSD
     21      1.2  minoura  *	Foundation, Inc. and its contributors.
     22      1.2  minoura  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.2  minoura  *    contributors may be used to endorse or promote products derived
     24      1.2  minoura  *    from this software without specific prior written permission.
     25      1.2  minoura  *
     26      1.2  minoura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.2  minoura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.2  minoura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.2  minoura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.2  minoura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.2  minoura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.2  minoura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.2  minoura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.2  minoura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.2  minoura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.2  minoura  * POSSIBILITY OF SUCH DAMAGE.
     37      1.2  minoura  */
     38      1.2  minoura 
     39      1.2  minoura /*
     40      1.2  minoura  * Hitachi HD63450 (= Motorola MC68450) DMAC driver for x68k.
     41      1.2  minoura  */
     42      1.2  minoura 
     43  1.5.2.1   bouyer #include "opt_m680x0.h"
     44  1.5.2.1   bouyer 
     45      1.2  minoura #include <sys/param.h>
     46      1.2  minoura #include <sys/systm.h>
     47      1.2  minoura #include <sys/device.h>
     48      1.2  minoura #include <sys/malloc.h>
     49      1.2  minoura #include <sys/extent.h>
     50  1.5.2.1   bouyer #include <uvm/uvm_extern.h>	/* XXX needed? */
     51      1.2  minoura 
     52      1.2  minoura #include <machine/bus.h>
     53      1.2  minoura #include <machine/cpu.h>
     54      1.2  minoura #include <machine/frame.h>
     55      1.2  minoura 
     56      1.2  minoura #include <arch/x68k/dev/intiovar.h>
     57      1.2  minoura #include <arch/x68k/dev/dmacvar.h>
     58      1.2  minoura 
     59      1.2  minoura #ifdef DMAC_DEBUG
     60      1.2  minoura #define DPRINTF(n,x)	if (dmacdebug>(n)&0x0f) printf x
     61      1.2  minoura #define DDUMPREGS(n,x)	if (dmacdebug>(n)&0x0f) {printf x; dmac_dump_regs();}
     62      1.2  minoura int dmacdebug = 0;
     63      1.2  minoura #else
     64      1.2  minoura #define DPRINTF(n,x)
     65      1.2  minoura #define DDUMPREGS(n,x)
     66      1.2  minoura #endif
     67      1.2  minoura 
     68      1.2  minoura static void dmac_init_channels __P((struct dmac_softc*));
     69      1.2  minoura static int dmac_program_arraychain __P((struct device*, struct dmac_dma_xfer*));
     70      1.2  minoura static int dmac_done __P((void*));
     71      1.2  minoura static int dmac_error __P((void*));
     72      1.2  minoura 
     73      1.3  minoura #ifdef DMAC_DEBUG
     74      1.2  minoura static int dmac_dump_regs __P((void));
     75      1.3  minoura #endif
     76      1.2  minoura 
     77      1.2  minoura /*
     78      1.2  minoura  * autoconf stuff
     79      1.2  minoura  */
     80      1.2  minoura static int dmac_match __P((struct device *, struct cfdata *, void *));
     81      1.2  minoura static void dmac_attach __P((struct device *, struct device *, void *));
     82      1.2  minoura 
     83      1.2  minoura struct cfattach dmac_ca = {
     84      1.2  minoura 	sizeof(struct dmac_softc), dmac_match, dmac_attach
     85      1.2  minoura };
     86      1.2  minoura 
     87      1.2  minoura static int
     88      1.2  minoura dmac_match(parent, cf, aux)
     89      1.2  minoura 	struct device *parent;
     90      1.2  minoura 	struct cfdata *cf;
     91      1.2  minoura 	void *aux;
     92      1.2  minoura {
     93      1.2  minoura 	struct intio_attach_args *ia = aux;
     94      1.2  minoura 
     95      1.2  minoura 	if (strcmp (ia->ia_name, "dmac") != 0)
     96      1.2  minoura 		return (0);
     97      1.2  minoura 	if (cf->cf_unit != 0)
     98      1.2  minoura 		return (0);
     99      1.2  minoura 
    100      1.2  minoura 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    101      1.2  minoura 		ia->ia_addr = DMAC_ADDR;
    102      1.2  minoura 
    103      1.2  minoura 	/* fixed address */
    104      1.2  minoura 	if (ia->ia_addr != DMAC_ADDR)
    105      1.2  minoura 		return (0);
    106      1.2  minoura 	if (ia->ia_intr != INTIOCF_INTR_DEFAULT)
    107      1.2  minoura 		return (0);
    108      1.2  minoura 
    109      1.2  minoura 	return 1;
    110      1.2  minoura }
    111      1.2  minoura 
    112      1.2  minoura static void
    113      1.2  minoura dmac_attach(parent, self, aux)
    114      1.2  minoura 	struct device *parent, *self;
    115      1.2  minoura 	void *aux;
    116      1.2  minoura {
    117      1.2  minoura 	struct dmac_softc *sc = (struct dmac_softc *)self;
    118      1.2  minoura 	struct intio_attach_args *ia = aux;
    119      1.2  minoura 	int r;
    120      1.2  minoura 
    121      1.2  minoura 	ia->ia_size = DMAC_CHAN_SIZE * DMAC_NCHAN;
    122      1.2  minoura 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
    123      1.2  minoura #ifdef DIAGNOSTIC
    124      1.2  minoura 	if (r)
    125      1.2  minoura 		panic ("IO map for DMAC corruption??");
    126      1.2  minoura #endif
    127      1.2  minoura 
    128      1.2  minoura 	((struct intio_softc*) parent)->sc_dmac = self;
    129      1.2  minoura 	sc->sc_bst = ia->ia_bst;
    130      1.2  minoura 	bus_space_map (sc->sc_bst, ia->ia_addr, ia->ia_size, 0, &sc->sc_bht);
    131      1.2  minoura 	dmac_init_channels(sc);
    132      1.2  minoura 
    133      1.2  minoura 	printf (": HD63450 DMAC\n%s: 4 channels available.\n", self->dv_xname);
    134      1.2  minoura }
    135      1.2  minoura 
    136      1.2  minoura #define DMAC_MAPSIZE 64
    137      1.2  minoura /* Allocate statically in order to make sure the DMAC can reach the maps. */
    138      1.2  minoura static struct dmac_sg_array dmac_map[DMAC_NCHAN][DMAC_MAPSIZE];
    139      1.2  minoura 
    140      1.2  minoura static void
    141      1.2  minoura dmac_init_channels(sc)
    142      1.2  minoura 	struct dmac_softc *sc;
    143      1.2  minoura {
    144      1.2  minoura 	int i;
    145      1.2  minoura 	pmap_t pmap = pmap_kernel();
    146      1.2  minoura 
    147      1.2  minoura 	for (i=0; i<DMAC_NCHAN; i++) {
    148      1.2  minoura 		sc->sc_channels[i].ch_channel = i;
    149      1.2  minoura 		sc->sc_channels[i].ch_name[0] = 0;
    150      1.2  minoura 		sc->sc_channels[i].ch_softc = &sc->sc_dev;
    151      1.5  thorpej 		(void) pmap_extract(pmap, (vaddr_t) &dmac_map[i],
    152      1.5  thorpej 		   (paddr_t *) &sc->sc_channels[i].ch_map);
    153      1.2  minoura 		bus_space_subregion(sc->sc_bst, sc->sc_bht,
    154      1.2  minoura 				    DMAC_CHAN_SIZE*i, DMAC_CHAN_SIZE,
    155      1.2  minoura 				    &sc->sc_channels[i].ch_bht);
    156      1.2  minoura 	}
    157      1.2  minoura 
    158      1.2  minoura 	return;
    159      1.2  minoura }
    160      1.2  minoura 
    161      1.2  minoura 
    162      1.2  minoura /*
    163      1.2  minoura  * Channel initialization/deinitialization per user device.
    164      1.2  minoura  */
    165      1.2  minoura struct dmac_channel_stat *
    166      1.2  minoura dmac_alloc_channel(self, ch, name,
    167      1.2  minoura 		   normalv, normal, normalarg,
    168      1.2  minoura 		   errorv, error, errorarg)
    169      1.2  minoura 	struct device *self;
    170      1.2  minoura 	int ch;
    171      1.2  minoura 	char *name;
    172      1.2  minoura 	int normalv, errorv;
    173      1.2  minoura 	dmac_intr_handler_t normal, error;
    174      1.2  minoura 	void *normalarg, *errorarg;
    175      1.2  minoura {
    176      1.2  minoura 	struct intio_softc *intio = (void*) self;
    177      1.2  minoura 	struct dmac_softc *sc = (void*) intio->sc_dmac;
    178      1.2  minoura 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    179      1.2  minoura 	char intrname[16];
    180      1.2  minoura 
    181      1.2  minoura #ifdef DIAGNOSTIC
    182      1.2  minoura 	if (ch < 0 || ch >= DMAC_NCHAN)
    183      1.2  minoura 		panic ("Invalid DMAC channel.");
    184      1.2  minoura 	if (chan->ch_name[0])
    185      1.2  minoura 		panic ("DMAC: channel in use.");
    186      1.2  minoura 	if (strlen(name) > 8)
    187      1.2  minoura 	  	panic ("DMAC: wrong user name.");
    188      1.2  minoura #endif
    189      1.2  minoura 
    190      1.2  minoura 	/* fill the channel status structure. */
    191      1.2  minoura 	strcpy(chan->ch_name, name);
    192      1.2  minoura 	chan->ch_dcr = (DMAC_DCR_XRM_CSWH | DMAC_DCR_OTYP_EASYNC |
    193      1.2  minoura 			DMAC_DCR_OPS_8BIT);
    194      1.2  minoura 	chan->ch_ocr = (DMAC_OCR_SIZE_BYTE_NOPACK | DMAC_OCR_CHAIN_ARRAY |
    195      1.2  minoura 			DMAC_OCR_REQG_EXTERNAL);
    196      1.2  minoura 	chan->ch_normalv = normalv;
    197      1.2  minoura 	chan->ch_errorv = errorv;
    198      1.2  minoura 	chan->ch_normal = normal;
    199      1.2  minoura 	chan->ch_error = error;
    200      1.2  minoura 	chan->ch_normalarg = normalarg;
    201      1.2  minoura 	chan->ch_errorarg = errorarg;
    202      1.2  minoura 	chan->ch_xfer_in_progress = 0;
    203      1.2  minoura 
    204      1.2  minoura 	/* setup the device-specific registers */
    205      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    206      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    207      1.2  minoura 			   DMAC_REG_DCR, chan->ch_dcr);
    208      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CPR, 0);
    209      1.2  minoura 
    210      1.2  minoura 	/*
    211      1.2  minoura 	 * X68k physical user space is a subset of the kernel space;
    212      1.2  minoura 	 * the memory is always included in the physical user space,
    213      1.2  minoura 	 * while the device is not.
    214      1.2  minoura 	 */
    215      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    216      1.2  minoura 			   DMAC_REG_BFCR, DMAC_FC_USER_DATA);
    217      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    218      1.2  minoura 			   DMAC_REG_MFCR, DMAC_FC_USER_DATA);
    219      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    220      1.2  minoura 			   DMAC_REG_DFCR, DMAC_FC_KERNEL_DATA);
    221      1.2  minoura 
    222      1.2  minoura 	/* setup the interrupt handlers */
    223      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR, normalv);
    224      1.2  minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR, errorv);
    225      1.2  minoura 
    226      1.2  minoura 	strcpy(intrname, name);
    227      1.2  minoura 	strcat(intrname, "dma");
    228      1.2  minoura 	intio_intr_establish (normalv, intrname, dmac_done, chan);
    229      1.2  minoura 
    230      1.2  minoura 	strcpy(intrname, name);
    231      1.2  minoura 	strcat(intrname, "dmaerr");
    232      1.2  minoura 	intio_intr_establish (errorv, intrname, dmac_error, chan);
    233      1.2  minoura 
    234      1.2  minoura 	return chan;
    235      1.2  minoura }
    236      1.2  minoura 
    237      1.2  minoura int
    238      1.2  minoura dmac_free_channel(self, ch, channel)
    239      1.2  minoura 	struct device *self;
    240      1.2  minoura 	int ch;
    241      1.2  minoura 	void *channel;
    242      1.2  minoura {
    243      1.2  minoura 	struct dmac_softc *sc = (void*) self;
    244      1.2  minoura 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    245      1.2  minoura 
    246      1.2  minoura 	if (chan != channel)
    247      1.2  minoura 		return -1;
    248      1.2  minoura 	if (ch != chan->ch_channel)
    249      1.2  minoura 		return -1;
    250      1.2  minoura #if DIAGNOSTIC
    251      1.2  minoura 	if (chan->ch_xfer_in_progress)
    252      1.2  minoura 		panic ("dmac_free_channel: DMA transfer in progress");
    253      1.2  minoura #endif
    254      1.2  minoura 
    255      1.2  minoura 	chan->ch_name[0] = 0;
    256      1.2  minoura 	intio_intr_disestablish(chan->ch_normalv, channel);
    257      1.2  minoura 	intio_intr_disestablish(chan->ch_errorv, channel);
    258      1.2  minoura 
    259      1.2  minoura 	return 0;
    260      1.2  minoura }
    261      1.2  minoura 
    262      1.2  minoura /*
    263      1.2  minoura  * Initialization / deinitialization per transfer.
    264      1.2  minoura  */
    265      1.2  minoura struct dmac_dma_xfer *
    266      1.2  minoura dmac_prepare_xfer (chan, dmat, dmamap, dir, scr, dar)
    267      1.2  minoura 	struct dmac_channel_stat *chan;
    268      1.2  minoura 	bus_dma_tag_t dmat;
    269      1.2  minoura 	bus_dmamap_t dmamap;
    270      1.2  minoura 	int dir, scr;
    271      1.2  minoura 	void *dar;
    272      1.2  minoura {
    273      1.2  minoura 	struct dmac_dma_xfer *r = malloc (sizeof (struct dmac_dma_xfer),
    274      1.2  minoura 					  M_DEVBUF, M_WAITOK);
    275      1.2  minoura 
    276      1.2  minoura 	r->dx_channel = chan;
    277      1.2  minoura 	r->dx_dmamap = dmamap;
    278      1.2  minoura 	r->dx_tag = dmat;
    279      1.2  minoura 	r->dx_ocr = dir & DMAC_OCR_DIR_MASK;
    280      1.2  minoura 	r->dx_scr = scr & (DMAC_SCR_MAC_MASK|DMAC_SCR_DAC_MASK);
    281      1.2  minoura 	r->dx_device = dar;
    282      1.2  minoura 	r->dx_done = 0;
    283      1.2  minoura 
    284      1.2  minoura 	return r;
    285      1.2  minoura }
    286      1.2  minoura 
    287      1.2  minoura #ifdef DMAC_DEBUG
    288      1.2  minoura static struct dmac_channel_stat *debugchan = 0;
    289      1.2  minoura #endif
    290      1.2  minoura 
    291      1.2  minoura #ifdef DMAC_DEBUG
    292      1.2  minoura static u_int8_t dcsr, dcer, ddcr, docr, dscr, dccr, dcpr, dgcr,
    293      1.2  minoura   dnivr, deivr, ddfcr, dmfcr, dbfcr;
    294      1.2  minoura static u_int16_t dmtcr, dbtcr;
    295      1.2  minoura static u_int32_t ddar, dmar, dbar;
    296      1.2  minoura #endif
    297      1.2  minoura /*
    298      1.2  minoura  * Do the actual transfer.
    299      1.2  minoura  */
    300      1.2  minoura int
    301      1.2  minoura dmac_start_xfer(self, xf)
    302      1.2  minoura 	struct device *self;
    303      1.2  minoura 	struct dmac_dma_xfer *xf;
    304      1.2  minoura {
    305      1.2  minoura 	struct dmac_softc *sc = (void*) self;
    306      1.2  minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    307      1.2  minoura 	int c;
    308      1.2  minoura 
    309      1.2  minoura 
    310      1.2  minoura #ifdef DMAC_DEBUG
    311      1.2  minoura 	debugchan=chan;
    312      1.2  minoura #endif
    313      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    314      1.2  minoura 			  DMAC_REG_OCR, (xf->dx_ocr | chan->ch_ocr));
    315      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    316      1.2  minoura 			  DMAC_REG_SCR, xf->dx_scr);
    317      1.2  minoura 
    318      1.2  minoura 	/* program DMAC in array chainning mode */
    319      1.2  minoura 	xf->dx_done = 0;
    320      1.2  minoura 	DPRINTF (3, ("First program:\n"));
    321      1.2  minoura 	c = dmac_program_arraychain(self, xf);
    322      1.2  minoura 
    323      1.2  minoura 	/* setup the address/count registers */
    324      1.2  minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    325      1.2  minoura 			  DMAC_REG_BAR, (int) chan->ch_map);
    326      1.2  minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    327      1.2  minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    328      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    329      1.2  minoura 			  DMAC_REG_CSR, 0xff);
    330      1.2  minoura 	bus_space_write_2(sc->sc_bst, chan->ch_bht,
    331      1.2  minoura 			  DMAC_REG_BTCR, c);
    332      1.2  minoura 
    333      1.2  minoura 	/* START!! */
    334      1.2  minoura 	DDUMPREGS (3, ("first start\n"));
    335      1.2  minoura #ifdef DMAC_DEBUG
    336      1.2  minoura 	dcsr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR);
    337      1.2  minoura 	dcer = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER);
    338      1.2  minoura 	ddcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR);
    339      1.2  minoura 	docr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR);
    340      1.2  minoura 	dscr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR);
    341      1.2  minoura 	dccr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR);
    342      1.2  minoura 	dcpr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR);
    343      1.2  minoura 	dgcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR);
    344      1.2  minoura 	dnivr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR);
    345      1.2  minoura 	deivr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR);
    346      1.2  minoura 	ddfcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR);
    347      1.2  minoura 	dmfcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR);
    348      1.2  minoura 	dbfcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR);
    349      1.2  minoura 	dmtcr = bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR);
    350      1.2  minoura 	dbtcr = bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR);
    351      1.2  minoura 	ddar = bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR);
    352      1.2  minoura 	dmar = bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR);
    353      1.2  minoura 	dbar = bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR);
    354      1.2  minoura #endif
    355      1.2  minoura #if defined(M68040) || defined(M68060)
    356      1.2  minoura 	if (mmutype == MMU_68040)
    357      1.2  minoura 		dma_cachectl((caddr_t) &dmac_map[chan->ch_channel],
    358      1.2  minoura 			     sizeof(struct dmac_sg_array)*DMAC_MAPSIZE);
    359      1.2  minoura #endif
    360      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    361      1.2  minoura 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    362      1.2  minoura 	chan->ch_xfer_in_progress = xf;
    363      1.2  minoura 
    364      1.2  minoura 	return 0;
    365      1.2  minoura }
    366      1.2  minoura 
    367      1.2  minoura static int
    368      1.2  minoura dmac_program_arraychain(self, xf)
    369      1.2  minoura 	struct device *self;
    370      1.2  minoura 	struct dmac_dma_xfer *xf;
    371      1.2  minoura {
    372      1.2  minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    373      1.2  minoura 	int ch = chan->ch_channel;
    374      1.2  minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    375      1.2  minoura 	int i, j;
    376      1.2  minoura 
    377      1.2  minoura 	for (i=0, j=xf->dx_done; i<DMAC_MAPSIZE && j<map->dm_nsegs;
    378      1.2  minoura 	     i++, j++) {
    379      1.2  minoura 		dmac_map[ch][i].da_addr = map->dm_segs[j].ds_addr;
    380      1.2  minoura #ifdef DIAGNOSTIC
    381      1.2  minoura 		if (map->dm_segs[j].ds_len > 0xff00)
    382      1.4  minoura 			panic ("dmac_program_arraychain: wrong map: %ld", map->dm_segs[j].ds_len);
    383      1.2  minoura #endif
    384      1.2  minoura 		dmac_map[ch][i].da_count = map->dm_segs[j].ds_len;
    385      1.2  minoura 	}
    386      1.2  minoura 	xf->dx_done = j;
    387      1.2  minoura 
    388      1.2  minoura 	return i;
    389      1.2  minoura }
    390      1.2  minoura 
    391      1.2  minoura /*
    392      1.2  minoura  * interrupt handlers.
    393      1.2  minoura  */
    394      1.2  minoura static int
    395      1.2  minoura dmac_done(arg)
    396      1.2  minoura 	void *arg;
    397      1.2  minoura {
    398      1.2  minoura 	struct dmac_channel_stat *chan = arg;
    399      1.2  minoura 	struct dmac_softc *sc = (void*) chan->ch_softc;
    400      1.2  minoura 	struct dmac_dma_xfer *xf = chan->ch_xfer_in_progress;
    401      1.2  minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    402      1.2  minoura 	int c;
    403      1.2  minoura 
    404      1.2  minoura 	DPRINTF (3, ("dmac_done\n"));
    405      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    406      1.2  minoura 
    407      1.2  minoura 	if (xf->dx_done == map->dm_nsegs) {
    408      1.2  minoura 		/* Done */
    409      1.2  minoura 		chan->ch_xfer_in_progress = 0;
    410      1.2  minoura 		return (*chan->ch_normal) (chan->ch_normalarg);
    411      1.2  minoura 	}
    412      1.2  minoura 
    413      1.2  minoura 	/* Continue transfer */
    414      1.2  minoura 	DPRINTF (3, ("reprograming\n"));
    415      1.2  minoura 	c = dmac_program_arraychain (&sc->sc_dev, xf);
    416      1.2  minoura 
    417      1.2  minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    418      1.2  minoura 			  DMAC_REG_BAR, (int) chan->ch_map);
    419      1.2  minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    420      1.2  minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    421      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    422      1.2  minoura 			  DMAC_REG_CSR, 0xff);
    423      1.2  minoura 	bus_space_write_2(sc->sc_bst, chan->ch_bht,
    424      1.2  minoura 			  DMAC_REG_BTCR, c);
    425      1.2  minoura 
    426      1.2  minoura 	/* START!! */
    427      1.2  minoura 	DDUMPREGS (3, ("restart\n"));
    428      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    429      1.2  minoura 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    430      1.2  minoura 
    431      1.2  minoura 	return 1;
    432      1.2  minoura }
    433      1.2  minoura 
    434      1.2  minoura static int
    435      1.2  minoura dmac_error(arg)
    436      1.2  minoura 	void *arg;
    437      1.2  minoura {
    438      1.2  minoura 	struct dmac_channel_stat *chan = arg;
    439      1.2  minoura 	struct dmac_softc *sc = (void*) chan->ch_softc;
    440      1.2  minoura 
    441      1.2  minoura 	printf ("DMAC transfer error CSR=%02x, CER=%02x\n",
    442      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    443      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER));
    444      1.2  minoura 	DPRINTF(5, ("registers were:\n"));
    445      1.2  minoura #ifdef DMAC_DEBUG
    446      1.2  minoura 	if ((dmacdebug & 0x0f) > 5) {
    447      1.2  minoura 		printf ("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x,"
    448      1.2  minoura 			"CCR=%02x, CPR=%02x, GCR=%02x\n",
    449      1.2  minoura 			dcsr, dcer, ddcr, docr, dscr, dccr, dcpr, dgcr);
    450      1.2  minoura 		printf ("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, "
    451      1.2  minoura 			"DFCR=%02x, MFCR=%02x, BFCR=%02x\n",
    452      1.2  minoura 			dnivr, deivr, dmtcr, dbtcr, ddfcr, dmfcr, dbfcr);
    453      1.2  minoura 		printf ("DAR=%08x, MAR=%08x, BAR=%08x\n",
    454      1.2  minoura 			ddar, dmar, dbar);
    455      1.2  minoura 	}
    456      1.2  minoura #endif
    457      1.2  minoura 
    458      1.2  minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    459      1.2  minoura 	DDUMPREGS(3, ("dmac_error\n"));
    460      1.2  minoura 
    461      1.2  minoura 	return (*chan->ch_error) (chan->ch_errorarg);
    462      1.2  minoura }
    463      1.2  minoura 
    464      1.2  minoura 
    465      1.2  minoura #ifdef DMAC_DEBUG
    466      1.2  minoura static int
    467      1.2  minoura dmac_dump_regs(void)
    468      1.2  minoura {
    469      1.2  minoura 	struct dmac_channel_stat *chan = debugchan;
    470      1.2  minoura 	struct dmac_softc *sc;
    471      1.2  minoura 
    472      1.2  minoura 	if ((chan == 0) || (dmacdebug & 0xf0)) return;
    473      1.2  minoura 	sc = (void*) chan->ch_softc;
    474      1.2  minoura 
    475      1.2  minoura 	printf ("DMAC channel %d registers\n", chan->ch_channel);
    476      1.2  minoura 	printf ("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x,"
    477      1.2  minoura 		"CCR=%02x, CPR=%02x, GCR=%02x\n",
    478      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    479      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER),
    480      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR),
    481      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR),
    482      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR),
    483      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR),
    484      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR),
    485      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR));
    486      1.2  minoura 	printf ("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x,"
    487      1.2  minoura 		"MFCR=%02x, BFCR=%02x\n",
    488      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR),
    489      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR),
    490      1.2  minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR),
    491      1.2  minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR),
    492      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR),
    493      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR),
    494      1.2  minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR));
    495      1.2  minoura 	printf ("DAR=%08x, MAR=%08x, BAR=%08x\n",
    496      1.2  minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR),
    497      1.2  minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR),
    498      1.2  minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR));
    499      1.2  minoura 
    500      1.2  minoura 	return 0;
    501      1.2  minoura }
    502      1.2  minoura #endif
    503