Home | History | Annotate | Line # | Download | only in dev
intio_dmac.c revision 1.30
      1  1.30     isaki /*	$NetBSD: intio_dmac.c,v 1.30 2008/06/25 08:14:59 isaki 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.30     isaki __KERNEL_RCSID(0, "$NetBSD: intio_dmac.c,v 1.30 2008/06/25 08:14:59 isaki 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.30     isaki static int dmac_program_arraychain(device_t, 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.30     isaki static int dmac_match(device_t, cfdata_t, void *);
     78  1.30     isaki static void dmac_attach(device_t, device_t, void *);
     79   1.2   minoura 
     80  1.30     isaki CFATTACH_DECL_NEW(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.30     isaki dmac_match(device_t parent, cfdata_t 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.30     isaki dmac_attach(device_t parent, device_t self, void *aux)
    109   1.2   minoura {
    110  1.30     isaki 	struct dmac_softc *sc = device_private(self);
    111   1.2   minoura 	struct intio_attach_args *ia = aux;
    112  1.30     isaki 	struct intio_softc *intio;
    113   1.2   minoura 	int r;
    114   1.2   minoura 
    115  1.30     isaki 	sc->sc_dev = self;
    116  1.20       chs 	dmac_attached = 1;
    117  1.20       chs 
    118   1.2   minoura 	ia->ia_size = DMAC_CHAN_SIZE * DMAC_NCHAN;
    119  1.25     isaki 	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
    120   1.2   minoura #ifdef DIAGNOSTIC
    121   1.2   minoura 	if (r)
    122  1.26     isaki 		panic("IO map for DMAC corruption??");
    123   1.2   minoura #endif
    124   1.2   minoura 
    125  1.30     isaki 	intio = device_private(parent);
    126  1.30     isaki 	intio->sc_dmac = self;
    127   1.2   minoura 	sc->sc_bst = ia->ia_bst;
    128  1.25     isaki 	bus_space_map(sc->sc_bst, ia->ia_addr, ia->ia_size, 0, &sc->sc_bht);
    129   1.2   minoura 	dmac_init_channels(sc);
    130   1.2   minoura 
    131  1.30     isaki 	aprint_normal(": HD63450 DMAC\n");
    132  1.30     isaki 	aprint_normal_dev(self, "4 channels available.\n");
    133   1.2   minoura }
    134   1.2   minoura 
    135   1.2   minoura static void
    136  1.21       chs dmac_init_channels(struct dmac_softc *sc)
    137   1.2   minoura {
    138   1.2   minoura 	int i;
    139   1.2   minoura 
    140  1.25     isaki 	DPRINTF(3, ("dmac_init_channels\n"));
    141   1.2   minoura 	for (i=0; i<DMAC_NCHAN; i++) {
    142   1.2   minoura 		sc->sc_channels[i].ch_channel = i;
    143   1.2   minoura 		sc->sc_channels[i].ch_name[0] = 0;
    144  1.30     isaki 		sc->sc_channels[i].ch_softc = sc->sc_dev;
    145   1.2   minoura 		bus_space_subregion(sc->sc_bst, sc->sc_bht,
    146   1.2   minoura 				    DMAC_CHAN_SIZE*i, DMAC_CHAN_SIZE,
    147   1.2   minoura 				    &sc->sc_channels[i].ch_bht);
    148   1.8   minoura 		sc->sc_channels[i].ch_xfer.dx_dmamap = 0;
    149   1.9   minoura 		/* reset the status register */
    150   1.9   minoura 		bus_space_write_1(sc->sc_bst, sc->sc_channels[i].ch_bht,
    151   1.9   minoura 				  DMAC_REG_CSR, 0xff);
    152   1.2   minoura 	}
    153   1.2   minoura 
    154   1.2   minoura 	return;
    155   1.2   minoura }
    156   1.2   minoura 
    157   1.2   minoura 
    158   1.2   minoura /*
    159   1.2   minoura  * Channel initialization/deinitialization per user device.
    160   1.2   minoura  */
    161   1.2   minoura struct dmac_channel_stat *
    162  1.30     isaki dmac_alloc_channel(device_t self, int ch, const char *name, int normalv,
    163  1.21       chs     dmac_intr_handler_t normal, void *normalarg, int errorv,
    164  1.21       chs     dmac_intr_handler_t error, void *errorarg)
    165   1.2   minoura {
    166  1.30     isaki 	struct intio_softc *intio = device_private(self);
    167  1.30     isaki 	struct dmac_softc *sc = device_private(intio->sc_dmac);
    168   1.2   minoura 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    169  1.12   minoura #ifdef DMAC_ARRAYCHAIN
    170   1.8   minoura 	int r, dummy;
    171  1.12   minoura #endif
    172   1.2   minoura 
    173  1.30     isaki 	aprint_normal_dev(sc->sc_dev, "allocating ch %d for %s.\n",
    174  1.30     isaki 		ch, name);
    175  1.25     isaki 	DPRINTF(3, ("dmamap=%p\n", (void *)chan->ch_xfer.dx_dmamap));
    176   1.2   minoura #ifdef DIAGNOSTIC
    177   1.2   minoura 	if (ch < 0 || ch >= DMAC_NCHAN)
    178  1.25     isaki 		panic("Invalid DMAC channel.");
    179   1.2   minoura 	if (chan->ch_name[0])
    180  1.25     isaki 		panic("DMAC: channel in use.");
    181   1.2   minoura 	if (strlen(name) > 8)
    182  1.25     isaki 	  	panic("DMAC: wrong user name.");
    183   1.2   minoura #endif
    184   1.2   minoura 
    185   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    186   1.8   minoura 	/* allocate the DMAC arraychaining map */
    187   1.8   minoura 	r = bus_dmamem_alloc(intio->sc_dmat,
    188   1.8   minoura 			     sizeof(struct dmac_sg_array) * DMAC_MAPSIZE,
    189   1.8   minoura 			     4, 0, &chan->ch_seg[0], 1, &dummy,
    190   1.8   minoura 			     BUS_DMA_NOWAIT);
    191   1.8   minoura 	if (r)
    192  1.25     isaki 		panic("DMAC: cannot alloc DMA safe memory");
    193   1.8   minoura 	r = bus_dmamem_map(intio->sc_dmat,
    194   1.8   minoura 			   &chan->ch_seg[0], 1,
    195   1.8   minoura 			   sizeof(struct dmac_sg_array) * DMAC_MAPSIZE,
    196  1.24  christos 			   (void **) &chan->ch_map,
    197   1.8   minoura 			   BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    198   1.8   minoura 	if (r)
    199  1.25     isaki 		panic("DMAC: cannot map DMA safe memory");
    200   1.9   minoura #endif
    201   1.8   minoura 
    202   1.9   minoura 	/* fill the channel status structure by the default values. */
    203   1.2   minoura 	strcpy(chan->ch_name, name);
    204   1.2   minoura 	chan->ch_dcr = (DMAC_DCR_XRM_CSWH | DMAC_DCR_OTYP_EASYNC |
    205   1.2   minoura 			DMAC_DCR_OPS_8BIT);
    206   1.9   minoura 	chan->ch_ocr = (DMAC_OCR_SIZE_BYTE | DMAC_OCR_REQG_EXTERNAL);
    207   1.2   minoura 	chan->ch_normalv = normalv;
    208   1.2   minoura 	chan->ch_errorv = errorv;
    209   1.2   minoura 	chan->ch_normal = normal;
    210   1.2   minoura 	chan->ch_error = error;
    211   1.2   minoura 	chan->ch_normalarg = normalarg;
    212   1.2   minoura 	chan->ch_errorarg = errorarg;
    213   1.8   minoura 	chan->ch_xfer.dx_dmamap = 0;
    214   1.2   minoura 
    215   1.2   minoura 	/* setup the device-specific registers */
    216  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    217  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    218   1.2   minoura 			   DMAC_REG_DCR, chan->ch_dcr);
    219  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR, 0);
    220   1.2   minoura 
    221   1.2   minoura 	/*
    222   1.2   minoura 	 * X68k physical user space is a subset of the kernel space;
    223   1.2   minoura 	 * the memory is always included in the physical user space,
    224   1.2   minoura 	 * while the device is not.
    225   1.2   minoura 	 */
    226  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    227   1.2   minoura 			   DMAC_REG_BFCR, DMAC_FC_USER_DATA);
    228  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    229   1.2   minoura 			   DMAC_REG_MFCR, DMAC_FC_USER_DATA);
    230  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    231   1.2   minoura 			   DMAC_REG_DFCR, DMAC_FC_KERNEL_DATA);
    232   1.2   minoura 
    233   1.2   minoura 	/* setup the interrupt handlers */
    234  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR, normalv);
    235  1.25     isaki 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR, errorv);
    236   1.2   minoura 
    237  1.29     isaki 	intio_intr_establish_ext(normalv, name, "dma", dmac_done, chan);
    238  1.29     isaki 	intio_intr_establish_ext(errorv, name, "dmaerr", dmac_error, chan);
    239   1.2   minoura 
    240   1.2   minoura 	return chan;
    241   1.2   minoura }
    242   1.2   minoura 
    243   1.2   minoura int
    244  1.21       chs dmac_free_channel(struct device *self, int ch, void *channel)
    245   1.2   minoura {
    246  1.25     isaki 	struct intio_softc *intio = (void *)self;
    247  1.25     isaki 	struct dmac_softc *sc = (void *)intio->sc_dmac;
    248   1.2   minoura 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    249   1.2   minoura 
    250  1.25     isaki 	DPRINTF(3, ("dmac_free_channel, %d\n", ch));
    251  1.25     isaki 	DPRINTF(3, ("dmamap=%p\n", (void *)chan->ch_xfer.dx_dmamap));
    252   1.2   minoura 	if (chan != channel)
    253   1.2   minoura 		return -1;
    254   1.2   minoura 	if (ch != chan->ch_channel)
    255   1.2   minoura 		return -1;
    256   1.2   minoura 
    257   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    258  1.25     isaki 	bus_dmamem_unmap(intio->sc_dmat, (void *)chan->ch_map,
    259   1.9   minoura 			 sizeof(struct dmac_sg_array) * DMAC_MAPSIZE);
    260   1.9   minoura 	bus_dmamem_free(intio->sc_dmat, &chan->ch_seg[0], 1);
    261   1.9   minoura #endif
    262   1.2   minoura 	chan->ch_name[0] = 0;
    263   1.2   minoura 	intio_intr_disestablish(chan->ch_normalv, channel);
    264   1.2   minoura 	intio_intr_disestablish(chan->ch_errorv, channel);
    265   1.2   minoura 
    266   1.2   minoura 	return 0;
    267   1.2   minoura }
    268   1.2   minoura 
    269   1.2   minoura /*
    270   1.2   minoura  * Initialization / deinitialization per transfer.
    271   1.2   minoura  */
    272   1.2   minoura struct dmac_dma_xfer *
    273  1.21       chs dmac_alloc_xfer(struct dmac_channel_stat *chan, bus_dma_tag_t dmat,
    274  1.21       chs     bus_dmamap_t dmamap)
    275   1.2   minoura {
    276   1.8   minoura 	struct dmac_dma_xfer *xf = &chan->ch_xfer;
    277   1.2   minoura 
    278  1.25     isaki 	DPRINTF(3, ("dmac_alloc_xfer\n"));
    279   1.8   minoura 	xf->dx_channel = chan;
    280   1.8   minoura 	xf->dx_dmamap = dmamap;
    281   1.8   minoura 	xf->dx_tag = dmat;
    282   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    283   1.9   minoura 	xf->dx_array = chan->ch_map;
    284   1.9   minoura 	xf->dx_done = 0;
    285   1.9   minoura #endif
    286  1.11   minoura 	xf->dx_nextoff = xf->dx_nextsize = -1;
    287   1.9   minoura 	return xf;
    288   1.9   minoura }
    289   1.9   minoura 
    290   1.9   minoura int
    291  1.21       chs dmac_load_xfer(struct device *self, struct dmac_dma_xfer *xf)
    292   1.9   minoura {
    293  1.21       chs 	struct dmac_softc *sc = (void *)self;
    294   1.9   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    295   1.9   minoura 
    296  1.25     isaki 	DPRINTF(3, ("dmac_load_xfer\n"));
    297   1.9   minoura 
    298   1.9   minoura 	xf->dx_ocr &= ~DMAC_OCR_CHAIN_MASK;
    299   1.9   minoura 	if (xf->dx_dmamap->dm_nsegs == 1)
    300   1.9   minoura 		xf->dx_ocr |= DMAC_OCR_CHAIN_DISABLED;
    301   1.9   minoura 	else {
    302   1.9   minoura 		xf->dx_ocr |= DMAC_OCR_CHAIN_ARRAY;
    303   1.9   minoura 		xf->dx_nextoff = ~0;
    304   1.9   minoura 		xf->dx_nextsize = ~0;
    305   1.9   minoura 	}
    306   1.9   minoura 
    307   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    308   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR, xf->dx_scr);
    309   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    310   1.9   minoura 			  DMAC_REG_OCR, (xf->dx_ocr | chan->ch_ocr));
    311   1.9   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    312   1.9   minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    313   1.9   minoura 
    314   1.9   minoura 	return 0;
    315   1.9   minoura }
    316   1.9   minoura 
    317   1.9   minoura struct dmac_dma_xfer *
    318  1.21       chs dmac_prepare_xfer(struct dmac_channel_stat *chan, bus_dma_tag_t dmat,
    319  1.21       chs     bus_dmamap_t dmamap, int dir, int scr, void *dar)
    320   1.9   minoura {
    321   1.9   minoura 	struct dmac_dma_xfer *xf;
    322  1.25     isaki 	struct dmac_softc *sc = (struct dmac_softc *)chan->ch_softc;
    323   1.9   minoura 
    324   1.9   minoura 	xf = dmac_alloc_xfer(chan, dmat, dmamap);
    325   1.9   minoura 
    326   1.8   minoura 	xf->dx_ocr = dir & DMAC_OCR_DIR_MASK;
    327   1.8   minoura 	xf->dx_scr = scr & (DMAC_SCR_MAC_MASK|DMAC_SCR_DAC_MASK);
    328   1.8   minoura 	xf->dx_device = dar;
    329   1.9   minoura 
    330  1.30     isaki 	dmac_load_xfer(sc->sc_dev, xf);
    331   1.2   minoura 
    332   1.8   minoura 	return xf;
    333   1.2   minoura }
    334   1.2   minoura 
    335   1.2   minoura #ifdef DMAC_DEBUG
    336   1.2   minoura static struct dmac_channel_stat *debugchan = 0;
    337   1.2   minoura #endif
    338   1.2   minoura 
    339   1.2   minoura /*
    340   1.2   minoura  * Do the actual transfer.
    341   1.2   minoura  */
    342   1.2   minoura int
    343  1.21       chs dmac_start_xfer(struct device *self, struct dmac_dma_xfer *xf)
    344   1.2   minoura {
    345   1.9   minoura 	return dmac_start_xfer_offset(self, xf, 0, 0);
    346   1.9   minoura }
    347   1.9   minoura 
    348   1.9   minoura int
    349  1.21       chs dmac_start_xfer_offset(struct device *self, struct dmac_dma_xfer *xf,
    350  1.21       chs     u_int offset, u_int size)
    351   1.9   minoura {
    352  1.25     isaki 	struct dmac_softc *sc = (void *)self;
    353   1.2   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    354   1.9   minoura 	struct x68k_bus_dmamap *dmamap = xf->dx_dmamap;
    355  1.12   minoura 	int go = DMAC_CCR_STR|DMAC_CCR_INT;
    356  1.12   minoura #ifdef DMAC_ARRAYCHAIN
    357  1.12   minoura 	int c;
    358  1.12   minoura #endif
    359   1.2   minoura 
    360  1.25     isaki 	DPRINTF(3, ("dmac_start_xfer\n"));
    361   1.2   minoura #ifdef DMAC_DEBUG
    362   1.2   minoura 	debugchan=chan;
    363   1.2   minoura #endif
    364   1.2   minoura 
    365   1.9   minoura 	if (size == 0) {
    366   1.9   minoura #ifdef DIAGNOSTIC
    367   1.9   minoura 		if (offset != 0)
    368  1.25     isaki 			panic("dmac_start_xfer_offset: invalid offset %x",
    369   1.9   minoura 			       offset);
    370   1.9   minoura #endif
    371   1.9   minoura 		size = dmamap->dm_mapsize;
    372   1.9   minoura 	}
    373   1.9   minoura 
    374   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    375   1.9   minoura #ifdef DIAGNOSTIC
    376   1.9   minoura 	if (xf->dx_done)
    377   1.9   minoura 		panic("dmac_start_xfer: DMA transfer in progress");
    378   1.9   minoura #endif
    379   1.9   minoura #endif
    380  1.25     isaki 	DPRINTF(3, ("First program:\n"));
    381   1.9   minoura #ifdef DIAGNOSTIC
    382   1.9   minoura 	if ((offset >= dmamap->dm_mapsize) ||
    383   1.9   minoura 	    (offset + size > dmamap->dm_mapsize))
    384  1.25     isaki 		panic("dmac_start_xfer_offset: invalid offset: "
    385  1.14     isaki 			"offset=%d, size=%d, mapsize=%ld",
    386   1.9   minoura 		       offset, size, dmamap->dm_mapsize);
    387   1.9   minoura #endif
    388   1.8   minoura 	/* program DMAC in single block mode or array chainning mode */
    389   1.9   minoura 	if (dmamap->dm_nsegs == 1) {
    390   1.8   minoura 		DPRINTF(3, ("single block mode\n"));
    391   1.9   minoura #ifdef DIAGNOSTIC
    392   1.9   minoura 		if (dmamap->dm_mapsize != dmamap->dm_segs[0].ds_len)
    393  1.25     isaki 			panic("dmac_start_xfer_offset: dmamap curruption");
    394   1.9   minoura #endif
    395   1.9   minoura 		if (offset == xf->dx_nextoff &&
    396   1.9   minoura 		    size == xf->dx_nextsize) {
    397   1.9   minoura 			/* Use continued operation */
    398   1.9   minoura 			go |=  DMAC_CCR_CNT;
    399   1.9   minoura 			xf->dx_nextoff += size;
    400   1.9   minoura 		} else {
    401   1.9   minoura 			bus_space_write_4(sc->sc_bst, chan->ch_bht,
    402   1.9   minoura 					  DMAC_REG_MAR,
    403   1.9   minoura 					  (int) dmamap->dm_segs[0].ds_addr
    404   1.9   minoura 					  + offset);
    405   1.9   minoura 			bus_space_write_2(sc->sc_bst, chan->ch_bht,
    406   1.9   minoura 					  DMAC_REG_MTCR, (int) size);
    407   1.9   minoura 			xf->dx_nextoff = offset;
    408   1.9   minoura 			xf->dx_nextsize = size;
    409   1.9   minoura 		}
    410   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    411   1.8   minoura 		xf->dx_done = 1;
    412   1.9   minoura #endif
    413   1.8   minoura 	} else {
    414   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    415   1.9   minoura 		c = dmac_program_arraychain(self, xf, offset, size);
    416   1.8   minoura 		bus_space_write_4(sc->sc_bst, chan->ch_bht,
    417   1.8   minoura 				  DMAC_REG_BAR, (int) chan->ch_seg[0].ds_addr);
    418   1.8   minoura 		bus_space_write_2(sc->sc_bst, chan->ch_bht,
    419   1.8   minoura 				  DMAC_REG_BTCR, c);
    420   1.9   minoura #else
    421  1.25     isaki 		panic("DMAC: unexpected use of arraychaining mode");
    422   1.9   minoura #endif
    423   1.8   minoura 	}
    424   1.2   minoura 
    425   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    426   1.2   minoura 
    427   1.2   minoura 	/* START!! */
    428  1.25     isaki 	DDUMPREGS(3, ("first start\n"));
    429  1.18     isaki 
    430   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    431   1.2   minoura #if defined(M68040) || defined(M68060)
    432   1.9   minoura 	/* flush data cache for the map */
    433   1.9   minoura 	if (dmamap->dm_nsegs != 1 && mmutype == MMU_68040)
    434  1.24  christos 		dma_cachectl((void *) xf->dx_array,
    435   1.9   minoura 			     sizeof(struct dmac_sg_array) * c);
    436   1.9   minoura #endif
    437   1.2   minoura #endif
    438   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR, go);
    439   1.9   minoura 
    440   1.9   minoura 	if (xf->dx_nextoff != ~0) {
    441   1.9   minoura 		bus_space_write_4(sc->sc_bst, chan->ch_bht,
    442   1.9   minoura 				  DMAC_REG_BAR, xf->dx_nextoff);
    443   1.9   minoura 		bus_space_write_2(sc->sc_bst, chan->ch_bht,
    444   1.9   minoura 				  DMAC_REG_BTCR, xf->dx_nextsize);
    445   1.9   minoura 	}
    446   1.2   minoura 
    447   1.2   minoura 	return 0;
    448   1.2   minoura }
    449   1.2   minoura 
    450   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    451   1.2   minoura static int
    452  1.21       chs dmac_program_arraychain(struct device *self, struct dmac_dma_xfer *xf,
    453  1.21       chs     u_int offset, u_int size)
    454   1.2   minoura {
    455   1.2   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    456   1.2   minoura 	int ch = chan->ch_channel;
    457   1.2   minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    458   1.2   minoura 	int i, j;
    459   1.2   minoura 
    460   1.9   minoura 	/* XXX not yet!! */
    461   1.9   minoura 	if (offset != 0 || size != map->dm_mapsize)
    462  1.25     isaki 		panic("dmac_program_arraychain: unsupported offset/size");
    463   1.9   minoura 
    464  1.25     isaki 	DPRINTF(3, ("dmac_program_arraychain\n"));
    465   1.2   minoura 	for (i=0, j=xf->dx_done; i<DMAC_MAPSIZE && j<map->dm_nsegs;
    466   1.2   minoura 	     i++, j++) {
    467   1.8   minoura 		xf->dx_array[i].da_addr = map->dm_segs[j].ds_addr;
    468   1.2   minoura #ifdef DIAGNOSTIC
    469   1.9   minoura 		if (map->dm_segs[j].ds_len > DMAC_MAXSEGSZ)
    470  1.25     isaki 			panic("dmac_program_arraychain: wrong map: %ld",
    471   1.9   minoura 			       map->dm_segs[j].ds_len);
    472   1.2   minoura #endif
    473   1.8   minoura 		xf->dx_array[i].da_count = map->dm_segs[j].ds_len;
    474   1.2   minoura 	}
    475   1.2   minoura 	xf->dx_done = j;
    476   1.2   minoura 
    477   1.2   minoura 	return i;
    478   1.2   minoura }
    479   1.9   minoura #endif
    480   1.2   minoura 
    481   1.2   minoura /*
    482   1.2   minoura  * interrupt handlers.
    483   1.2   minoura  */
    484   1.2   minoura static int
    485  1.21       chs dmac_done(void *arg)
    486   1.2   minoura {
    487   1.2   minoura 	struct dmac_channel_stat *chan = arg;
    488  1.25     isaki 	struct dmac_softc *sc = (void *)chan->ch_softc;
    489  1.12   minoura #ifdef DMAC_ARRAYCHAIN
    490   1.8   minoura 	struct dmac_dma_xfer *xf = &chan->ch_xfer;
    491   1.2   minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    492   1.2   minoura 	int c;
    493  1.12   minoura #endif
    494   1.2   minoura 
    495  1.25     isaki 	DPRINTF(3, ("dmac_done\n"));
    496   1.9   minoura 
    497   1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    498   1.2   minoura 
    499   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    500   1.2   minoura 	if (xf->dx_done == map->dm_nsegs) {
    501   1.9   minoura 		xf->dx_done = 0;
    502   1.9   minoura #endif
    503   1.2   minoura 		/* Done */
    504  1.25     isaki 		return (*chan->ch_normal)(chan->ch_normalarg);
    505   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    506   1.2   minoura 	}
    507   1.9   minoura #endif
    508   1.2   minoura 
    509   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    510   1.2   minoura 	/* Continue transfer */
    511  1.25     isaki 	DPRINTF(3, ("reprograming\n"));
    512  1.30     isaki 	c = dmac_program_arraychain(sc->sc_dev, xf, 0, map->dm_mapsize);
    513   1.2   minoura 
    514   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    515   1.2   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    516   1.2   minoura 			  DMAC_REG_BAR, (int) chan->ch_map);
    517   1.2   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    518   1.2   minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    519   1.9   minoura 	bus_space_write_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR, c);
    520   1.2   minoura 
    521   1.2   minoura 	/* START!! */
    522  1.25     isaki 	DDUMPREGS(3, ("restart\n"));
    523   1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    524   1.2   minoura 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    525   1.2   minoura 
    526   1.2   minoura 	return 1;
    527   1.9   minoura #endif
    528   1.2   minoura }
    529   1.2   minoura 
    530   1.2   minoura static int
    531  1.21       chs dmac_error(void *arg)
    532   1.2   minoura {
    533   1.2   minoura 	struct dmac_channel_stat *chan = arg;
    534  1.25     isaki 	struct dmac_softc *sc = (void *)chan->ch_softc;
    535   1.2   minoura 
    536  1.25     isaki 	printf("DMAC transfer error CSR=%02x, CER=%02x\n",
    537   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    538   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER));
    539  1.18     isaki 	DDUMPREGS(3, ("registers were:\n"));
    540   1.2   minoura 
    541   1.9   minoura 	/* Clear the status bits */
    542   1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    543   1.2   minoura 
    544   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    545   1.9   minoura 	chan->ch_xfer.dx_done = 0;
    546   1.9   minoura #endif
    547   1.9   minoura 
    548  1.25     isaki 	return (*chan->ch_error)(chan->ch_errorarg);
    549   1.2   minoura }
    550   1.2   minoura 
    551   1.9   minoura int
    552  1.21       chs dmac_abort_xfer(struct device *self, struct dmac_dma_xfer *xf)
    553   1.9   minoura {
    554  1.25     isaki 	struct dmac_softc *sc = (void *)self;
    555   1.9   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    556   1.9   minoura 
    557   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR,
    558   1.9   minoura 			  DMAC_CCR_INT | DMAC_CCR_HLT);
    559  1.10   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    560  1.11   minoura 	xf->dx_nextoff = xf->dx_nextsize = -1;
    561   1.9   minoura 
    562   1.9   minoura 	return 0;
    563   1.9   minoura }
    564   1.2   minoura 
    565   1.2   minoura #ifdef DMAC_DEBUG
    566   1.2   minoura static int
    567   1.2   minoura dmac_dump_regs(void)
    568   1.2   minoura {
    569   1.2   minoura 	struct dmac_channel_stat *chan = debugchan;
    570   1.2   minoura 	struct dmac_softc *sc;
    571   1.2   minoura 
    572  1.13     isaki 	if ((chan == 0) || (dmacdebug & 0xf0))
    573  1.13     isaki 		return 0;
    574  1.25     isaki 	sc = (void *)chan->ch_softc;
    575   1.2   minoura 
    576  1.25     isaki 	printf("DMAC channel %d registers\n", chan->ch_channel);
    577  1.25     isaki 	printf("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x, "
    578   1.2   minoura 		"CCR=%02x, CPR=%02x, GCR=%02x\n",
    579   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    580   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER),
    581   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR),
    582   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR),
    583   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR),
    584   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR),
    585   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR),
    586   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR));
    587  1.25     isaki 	printf("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x, "
    588   1.2   minoura 		"MFCR=%02x, BFCR=%02x\n",
    589   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR),
    590   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR),
    591   1.2   minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR),
    592   1.2   minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR),
    593   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR),
    594   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR),
    595   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR));
    596  1.25     isaki 	printf("DAR=%08x, MAR=%08x, BAR=%08x\n",
    597   1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR),
    598   1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR),
    599   1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR));
    600   1.2   minoura 
    601   1.2   minoura 	return 0;
    602   1.2   minoura }
    603   1.2   minoura #endif
    604