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