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