Home | History | Annotate | Line # | Download | only in dev
intio_dmac.c revision 1.35
      1  1.35  christos /*	$NetBSD: intio_dmac.c,v 1.35 2014/03/26 08:17:59 christos 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.33       mrg #include "opt_m68k_arch.h"
     37  1.33       mrg 
     38  1.19     lukem #include <sys/cdefs.h>
     39  1.35  christos __KERNEL_RCSID(0, "$NetBSD: intio_dmac.c,v 1.35 2014/03/26 08:17:59 christos Exp $");
     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.35  christos 	int r __diagused;
    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.31     isaki 		sc->sc_channels[i].ch_softc = sc;
    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.32     isaki 	struct dmac_softc *dmac = device_private(intio->sc_dmac);
    168  1.32     isaki 	struct dmac_channel_stat *chan = &dmac->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.32     isaki 	aprint_normal_dev(dmac->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.32     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    217  1.32     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht,
    218   1.2   minoura 			   DMAC_REG_DCR, chan->ch_dcr);
    219  1.32     isaki 	bus_space_write_1(dmac->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.32     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht,
    227   1.2   minoura 			   DMAC_REG_BFCR, DMAC_FC_USER_DATA);
    228  1.32     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht,
    229   1.2   minoura 			   DMAC_REG_MFCR, DMAC_FC_USER_DATA);
    230  1.32     isaki 	bus_space_write_1(dmac->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.32     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_NIVR, normalv);
    235  1.32     isaki 	bus_space_write_1(dmac->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.31     isaki dmac_free_channel(device_t self, int ch, void *channel)
    245   1.2   minoura {
    246  1.31     isaki 	struct intio_softc *intio = device_private(self);
    247  1.31     isaki 	struct dmac_softc *dmac = device_private(intio->sc_dmac);
    248  1.31     isaki 	struct dmac_channel_stat *chan = &dmac->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.31     isaki dmac_load_xfer(struct dmac_softc *dmac, struct dmac_dma_xfer *xf)
    292   1.9   minoura {
    293   1.9   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    294   1.9   minoura 
    295  1.25     isaki 	DPRINTF(3, ("dmac_load_xfer\n"));
    296   1.9   minoura 
    297   1.9   minoura 	xf->dx_ocr &= ~DMAC_OCR_CHAIN_MASK;
    298   1.9   minoura 	if (xf->dx_dmamap->dm_nsegs == 1)
    299   1.9   minoura 		xf->dx_ocr |= DMAC_OCR_CHAIN_DISABLED;
    300   1.9   minoura 	else {
    301   1.9   minoura 		xf->dx_ocr |= DMAC_OCR_CHAIN_ARRAY;
    302   1.9   minoura 		xf->dx_nextoff = ~0;
    303   1.9   minoura 		xf->dx_nextsize = ~0;
    304   1.9   minoura 	}
    305   1.9   minoura 
    306  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    307  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_SCR, xf->dx_scr);
    308  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht,
    309   1.9   minoura 			  DMAC_REG_OCR, (xf->dx_ocr | chan->ch_ocr));
    310  1.31     isaki 	bus_space_write_4(dmac->sc_bst, chan->ch_bht,
    311   1.9   minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    312   1.9   minoura 
    313   1.9   minoura 	return 0;
    314   1.9   minoura }
    315   1.9   minoura 
    316   1.9   minoura struct dmac_dma_xfer *
    317  1.21       chs dmac_prepare_xfer(struct dmac_channel_stat *chan, bus_dma_tag_t dmat,
    318  1.21       chs     bus_dmamap_t dmamap, int dir, int scr, void *dar)
    319   1.9   minoura {
    320   1.9   minoura 	struct dmac_dma_xfer *xf;
    321  1.31     isaki 	struct dmac_softc *dmac = chan->ch_softc;
    322   1.9   minoura 
    323   1.9   minoura 	xf = dmac_alloc_xfer(chan, dmat, dmamap);
    324   1.9   minoura 
    325   1.8   minoura 	xf->dx_ocr = dir & DMAC_OCR_DIR_MASK;
    326   1.8   minoura 	xf->dx_scr = scr & (DMAC_SCR_MAC_MASK|DMAC_SCR_DAC_MASK);
    327   1.8   minoura 	xf->dx_device = dar;
    328   1.9   minoura 
    329  1.31     isaki 	dmac_load_xfer(dmac, xf);
    330   1.2   minoura 
    331   1.8   minoura 	return xf;
    332   1.2   minoura }
    333   1.2   minoura 
    334   1.2   minoura #ifdef DMAC_DEBUG
    335   1.2   minoura static struct dmac_channel_stat *debugchan = 0;
    336   1.2   minoura #endif
    337   1.2   minoura 
    338   1.2   minoura /*
    339   1.2   minoura  * Do the actual transfer.
    340   1.2   minoura  */
    341   1.2   minoura int
    342  1.31     isaki dmac_start_xfer(struct dmac_softc *dmac, struct dmac_dma_xfer *xf)
    343   1.2   minoura {
    344  1.31     isaki 	return dmac_start_xfer_offset(dmac, xf, 0, 0);
    345   1.9   minoura }
    346   1.9   minoura 
    347   1.9   minoura int
    348  1.31     isaki dmac_start_xfer_offset(struct dmac_softc *dmac, struct dmac_dma_xfer *xf,
    349  1.21       chs     u_int offset, u_int size)
    350   1.9   minoura {
    351   1.2   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    352   1.9   minoura 	struct x68k_bus_dmamap *dmamap = xf->dx_dmamap;
    353  1.12   minoura 	int go = DMAC_CCR_STR|DMAC_CCR_INT;
    354  1.12   minoura #ifdef DMAC_ARRAYCHAIN
    355  1.12   minoura 	int c;
    356  1.12   minoura #endif
    357   1.2   minoura 
    358  1.25     isaki 	DPRINTF(3, ("dmac_start_xfer\n"));
    359   1.2   minoura #ifdef DMAC_DEBUG
    360   1.2   minoura 	debugchan=chan;
    361   1.2   minoura #endif
    362   1.2   minoura 
    363   1.9   minoura 	if (size == 0) {
    364   1.9   minoura #ifdef DIAGNOSTIC
    365   1.9   minoura 		if (offset != 0)
    366  1.25     isaki 			panic("dmac_start_xfer_offset: invalid offset %x",
    367   1.9   minoura 			       offset);
    368   1.9   minoura #endif
    369   1.9   minoura 		size = dmamap->dm_mapsize;
    370   1.9   minoura 	}
    371   1.9   minoura 
    372   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    373   1.9   minoura #ifdef DIAGNOSTIC
    374   1.9   minoura 	if (xf->dx_done)
    375   1.9   minoura 		panic("dmac_start_xfer: DMA transfer in progress");
    376   1.9   minoura #endif
    377   1.9   minoura #endif
    378  1.25     isaki 	DPRINTF(3, ("First program:\n"));
    379   1.9   minoura #ifdef DIAGNOSTIC
    380   1.9   minoura 	if ((offset >= dmamap->dm_mapsize) ||
    381   1.9   minoura 	    (offset + size > dmamap->dm_mapsize))
    382  1.25     isaki 		panic("dmac_start_xfer_offset: invalid offset: "
    383  1.14     isaki 			"offset=%d, size=%d, mapsize=%ld",
    384   1.9   minoura 		       offset, size, dmamap->dm_mapsize);
    385   1.9   minoura #endif
    386   1.8   minoura 	/* program DMAC in single block mode or array chainning mode */
    387   1.9   minoura 	if (dmamap->dm_nsegs == 1) {
    388   1.8   minoura 		DPRINTF(3, ("single block mode\n"));
    389   1.9   minoura #ifdef DIAGNOSTIC
    390   1.9   minoura 		if (dmamap->dm_mapsize != dmamap->dm_segs[0].ds_len)
    391  1.25     isaki 			panic("dmac_start_xfer_offset: dmamap curruption");
    392   1.9   minoura #endif
    393   1.9   minoura 		if (offset == xf->dx_nextoff &&
    394   1.9   minoura 		    size == xf->dx_nextsize) {
    395   1.9   minoura 			/* Use continued operation */
    396   1.9   minoura 			go |=  DMAC_CCR_CNT;
    397   1.9   minoura 			xf->dx_nextoff += size;
    398   1.9   minoura 		} else {
    399  1.31     isaki 			bus_space_write_4(dmac->sc_bst, chan->ch_bht,
    400   1.9   minoura 					  DMAC_REG_MAR,
    401   1.9   minoura 					  (int) dmamap->dm_segs[0].ds_addr
    402   1.9   minoura 					  + offset);
    403  1.31     isaki 			bus_space_write_2(dmac->sc_bst, chan->ch_bht,
    404   1.9   minoura 					  DMAC_REG_MTCR, (int) size);
    405   1.9   minoura 			xf->dx_nextoff = offset;
    406   1.9   minoura 			xf->dx_nextsize = size;
    407   1.9   minoura 		}
    408   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    409   1.8   minoura 		xf->dx_done = 1;
    410   1.9   minoura #endif
    411   1.8   minoura 	} else {
    412   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    413   1.9   minoura 		c = dmac_program_arraychain(self, xf, offset, size);
    414  1.31     isaki 		bus_space_write_4(dmac->sc_bst, chan->ch_bht,
    415   1.8   minoura 				  DMAC_REG_BAR, (int) chan->ch_seg[0].ds_addr);
    416  1.31     isaki 		bus_space_write_2(dmac->sc_bst, chan->ch_bht,
    417   1.8   minoura 				  DMAC_REG_BTCR, c);
    418   1.9   minoura #else
    419  1.25     isaki 		panic("DMAC: unexpected use of arraychaining mode");
    420   1.9   minoura #endif
    421   1.8   minoura 	}
    422   1.2   minoura 
    423  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    424   1.2   minoura 
    425   1.2   minoura 	/* START!! */
    426  1.25     isaki 	DDUMPREGS(3, ("first start\n"));
    427  1.18     isaki 
    428   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    429   1.2   minoura #if defined(M68040) || defined(M68060)
    430   1.9   minoura 	/* flush data cache for the map */
    431   1.9   minoura 	if (dmamap->dm_nsegs != 1 && mmutype == MMU_68040)
    432  1.24  christos 		dma_cachectl((void *) xf->dx_array,
    433   1.9   minoura 			     sizeof(struct dmac_sg_array) * c);
    434   1.9   minoura #endif
    435   1.2   minoura #endif
    436  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_CCR, go);
    437   1.9   minoura 
    438   1.9   minoura 	if (xf->dx_nextoff != ~0) {
    439  1.31     isaki 		bus_space_write_4(dmac->sc_bst, chan->ch_bht,
    440   1.9   minoura 				  DMAC_REG_BAR, xf->dx_nextoff);
    441  1.31     isaki 		bus_space_write_2(dmac->sc_bst, chan->ch_bht,
    442   1.9   minoura 				  DMAC_REG_BTCR, xf->dx_nextsize);
    443   1.9   minoura 	}
    444   1.2   minoura 
    445   1.2   minoura 	return 0;
    446   1.2   minoura }
    447   1.2   minoura 
    448   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    449   1.2   minoura static int
    450  1.31     isaki dmac_program_arraychain(device_t self, struct dmac_dma_xfer *xf,
    451  1.21       chs     u_int offset, u_int size)
    452   1.2   minoura {
    453   1.2   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    454   1.2   minoura 	int ch = chan->ch_channel;
    455   1.2   minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    456   1.2   minoura 	int i, j;
    457   1.2   minoura 
    458   1.9   minoura 	/* XXX not yet!! */
    459   1.9   minoura 	if (offset != 0 || size != map->dm_mapsize)
    460  1.25     isaki 		panic("dmac_program_arraychain: unsupported offset/size");
    461   1.9   minoura 
    462  1.25     isaki 	DPRINTF(3, ("dmac_program_arraychain\n"));
    463   1.2   minoura 	for (i=0, j=xf->dx_done; i<DMAC_MAPSIZE && j<map->dm_nsegs;
    464   1.2   minoura 	     i++, j++) {
    465   1.8   minoura 		xf->dx_array[i].da_addr = map->dm_segs[j].ds_addr;
    466   1.2   minoura #ifdef DIAGNOSTIC
    467   1.9   minoura 		if (map->dm_segs[j].ds_len > DMAC_MAXSEGSZ)
    468  1.25     isaki 			panic("dmac_program_arraychain: wrong map: %ld",
    469   1.9   minoura 			       map->dm_segs[j].ds_len);
    470   1.2   minoura #endif
    471   1.8   minoura 		xf->dx_array[i].da_count = map->dm_segs[j].ds_len;
    472   1.2   minoura 	}
    473   1.2   minoura 	xf->dx_done = j;
    474   1.2   minoura 
    475   1.2   minoura 	return i;
    476   1.2   minoura }
    477   1.9   minoura #endif
    478   1.2   minoura 
    479   1.2   minoura /*
    480   1.2   minoura  * interrupt handlers.
    481   1.2   minoura  */
    482   1.2   minoura static int
    483  1.21       chs dmac_done(void *arg)
    484   1.2   minoura {
    485   1.2   minoura 	struct dmac_channel_stat *chan = arg;
    486  1.31     isaki 	struct dmac_softc *sc = chan->ch_softc;
    487  1.12   minoura #ifdef DMAC_ARRAYCHAIN
    488   1.8   minoura 	struct dmac_dma_xfer *xf = &chan->ch_xfer;
    489   1.2   minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    490   1.2   minoura 	int c;
    491  1.12   minoura #endif
    492   1.2   minoura 
    493  1.25     isaki 	DPRINTF(3, ("dmac_done\n"));
    494   1.9   minoura 
    495   1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    496   1.2   minoura 
    497   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    498   1.2   minoura 	if (xf->dx_done == map->dm_nsegs) {
    499   1.9   minoura 		xf->dx_done = 0;
    500   1.9   minoura #endif
    501   1.2   minoura 		/* Done */
    502  1.25     isaki 		return (*chan->ch_normal)(chan->ch_normalarg);
    503   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    504   1.2   minoura 	}
    505   1.9   minoura #endif
    506   1.2   minoura 
    507   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    508   1.2   minoura 	/* Continue transfer */
    509  1.25     isaki 	DPRINTF(3, ("reprograming\n"));
    510  1.30     isaki 	c = dmac_program_arraychain(sc->sc_dev, xf, 0, map->dm_mapsize);
    511   1.2   minoura 
    512   1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    513   1.2   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    514   1.2   minoura 			  DMAC_REG_BAR, (int) chan->ch_map);
    515   1.2   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    516   1.2   minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    517   1.9   minoura 	bus_space_write_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR, c);
    518   1.2   minoura 
    519   1.2   minoura 	/* START!! */
    520  1.25     isaki 	DDUMPREGS(3, ("restart\n"));
    521   1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    522   1.2   minoura 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    523   1.2   minoura 
    524   1.2   minoura 	return 1;
    525   1.9   minoura #endif
    526   1.2   minoura }
    527   1.2   minoura 
    528   1.2   minoura static int
    529  1.21       chs dmac_error(void *arg)
    530   1.2   minoura {
    531   1.2   minoura 	struct dmac_channel_stat *chan = arg;
    532  1.31     isaki 	struct dmac_softc *sc = chan->ch_softc;
    533  1.34   tsutsui 	uint8_t csr, cer;
    534   1.2   minoura 
    535  1.34   tsutsui 	csr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR);
    536  1.34   tsutsui 	cer = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER);
    537  1.34   tsutsui 
    538  1.34   tsutsui #ifndef DMAC_DEBUG
    539  1.34   tsutsui 	/* Software abort (CER=0x11) could happen on normal xfer termination */
    540  1.34   tsutsui 	if (cer != 0x11)
    541  1.34   tsutsui #endif
    542  1.34   tsutsui 	{
    543  1.34   tsutsui 		printf("DMAC transfer error CSR=%02x, CER=%02x\n", csr, cer);
    544  1.34   tsutsui 	}
    545  1.18     isaki 	DDUMPREGS(3, ("registers were:\n"));
    546   1.2   minoura 
    547   1.9   minoura 	/* Clear the status bits */
    548   1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    549   1.2   minoura 
    550   1.9   minoura #ifdef DMAC_ARRAYCHAIN
    551   1.9   minoura 	chan->ch_xfer.dx_done = 0;
    552   1.9   minoura #endif
    553   1.9   minoura 
    554  1.25     isaki 	return (*chan->ch_error)(chan->ch_errorarg);
    555   1.2   minoura }
    556   1.2   minoura 
    557   1.9   minoura int
    558  1.31     isaki dmac_abort_xfer(struct dmac_softc *dmac, struct dmac_dma_xfer *xf)
    559   1.9   minoura {
    560   1.9   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    561   1.9   minoura 
    562  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_CCR,
    563  1.34   tsutsui 			  DMAC_CCR_INT | DMAC_CCR_SAB);
    564  1.31     isaki 	bus_space_write_1(dmac->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    565  1.11   minoura 	xf->dx_nextoff = xf->dx_nextsize = -1;
    566   1.9   minoura 
    567   1.9   minoura 	return 0;
    568   1.9   minoura }
    569   1.2   minoura 
    570   1.2   minoura #ifdef DMAC_DEBUG
    571   1.2   minoura static int
    572   1.2   minoura dmac_dump_regs(void)
    573   1.2   minoura {
    574   1.2   minoura 	struct dmac_channel_stat *chan = debugchan;
    575   1.2   minoura 	struct dmac_softc *sc;
    576   1.2   minoura 
    577  1.13     isaki 	if ((chan == 0) || (dmacdebug & 0xf0))
    578  1.13     isaki 		return 0;
    579  1.31     isaki 	sc = chan->ch_softc;
    580   1.2   minoura 
    581  1.25     isaki 	printf("DMAC channel %d registers\n", chan->ch_channel);
    582  1.25     isaki 	printf("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x, "
    583   1.2   minoura 		"CCR=%02x, CPR=%02x, GCR=%02x\n",
    584   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    585   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER),
    586   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR),
    587   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR),
    588   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR),
    589   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR),
    590   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR),
    591   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR));
    592  1.25     isaki 	printf("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x, "
    593   1.2   minoura 		"MFCR=%02x, BFCR=%02x\n",
    594   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR),
    595   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR),
    596   1.2   minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR),
    597   1.2   minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR),
    598   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR),
    599   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR),
    600   1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR));
    601  1.25     isaki 	printf("DAR=%08x, MAR=%08x, BAR=%08x\n",
    602   1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR),
    603   1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR),
    604   1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR));
    605   1.2   minoura 
    606   1.2   minoura 	return 0;
    607   1.2   minoura }
    608   1.2   minoura #endif
    609