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