Home | History | Annotate | Line # | Download | only in dev
intio_dmac.c revision 1.11.2.3
      1  1.11.2.3  jdolecek /*	$NetBSD: intio_dmac.c,v 1.11.2.3 2002/10/10 18:37:34 jdolecek 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.6   minoura 
     43       1.6   minoura #include "opt_m680x0.h"
     44       1.2   minoura 
     45       1.2   minoura #include <sys/param.h>
     46       1.2   minoura #include <sys/systm.h>
     47       1.2   minoura #include <sys/device.h>
     48       1.8   minoura #include <uvm/uvm_extern.h>
     49       1.2   minoura 
     50       1.2   minoura #include <machine/bus.h>
     51       1.2   minoura #include <machine/cpu.h>
     52       1.2   minoura #include <machine/frame.h>
     53       1.2   minoura 
     54       1.2   minoura #include <arch/x68k/dev/intiovar.h>
     55       1.2   minoura #include <arch/x68k/dev/dmacvar.h>
     56       1.2   minoura 
     57       1.2   minoura #ifdef DMAC_DEBUG
     58  1.11.2.2  jdolecek #define DPRINTF(n,x)	if (dmacdebug>((n)&0x0f)) printf x
     59  1.11.2.2  jdolecek #define DDUMPREGS(n,x)	if (dmacdebug>((n)&0x0f)) {printf x; dmac_dump_regs();}
     60       1.2   minoura int dmacdebug = 0;
     61       1.2   minoura #else
     62       1.2   minoura #define DPRINTF(n,x)
     63       1.2   minoura #define DDUMPREGS(n,x)
     64       1.2   minoura #endif
     65       1.2   minoura 
     66       1.2   minoura static void dmac_init_channels __P((struct dmac_softc*));
     67       1.9   minoura #ifdef DMAC_ARRAYCHAIN
     68       1.9   minoura static int dmac_program_arraychain __P((struct device*, struct dmac_dma_xfer*,
     69       1.9   minoura 					u_int, u_int));
     70       1.9   minoura #endif
     71       1.2   minoura static int dmac_done __P((void*));
     72       1.2   minoura static int dmac_error __P((void*));
     73       1.2   minoura 
     74       1.3   minoura #ifdef DMAC_DEBUG
     75       1.2   minoura static int dmac_dump_regs __P((void));
     76       1.3   minoura #endif
     77       1.2   minoura 
     78       1.2   minoura /*
     79       1.2   minoura  * autoconf stuff
     80       1.2   minoura  */
     81       1.2   minoura static int dmac_match __P((struct device *, struct cfdata *, void *));
     82       1.2   minoura static void dmac_attach __P((struct device *, struct device *, void *));
     83       1.2   minoura 
     84  1.11.2.3  jdolecek CFATTACH_DECL(dmac, sizeof(struct dmac_softc),
     85  1.11.2.3  jdolecek     dmac_match, dmac_attach, NULL, NULL);
     86       1.2   minoura 
     87       1.2   minoura static int
     88       1.2   minoura dmac_match(parent, cf, aux)
     89       1.2   minoura 	struct device *parent;
     90       1.2   minoura 	struct cfdata *cf;
     91       1.2   minoura 	void *aux;
     92       1.2   minoura {
     93       1.2   minoura 	struct intio_attach_args *ia = aux;
     94       1.2   minoura 
     95       1.2   minoura 	if (strcmp (ia->ia_name, "dmac") != 0)
     96       1.2   minoura 		return (0);
     97       1.2   minoura 	if (cf->cf_unit != 0)
     98       1.2   minoura 		return (0);
     99       1.2   minoura 
    100       1.2   minoura 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    101       1.2   minoura 		ia->ia_addr = DMAC_ADDR;
    102       1.2   minoura 
    103       1.2   minoura 	/* fixed address */
    104       1.2   minoura 	if (ia->ia_addr != DMAC_ADDR)
    105       1.2   minoura 		return (0);
    106       1.2   minoura 	if (ia->ia_intr != INTIOCF_INTR_DEFAULT)
    107       1.2   minoura 		return (0);
    108       1.2   minoura 
    109       1.2   minoura 	return 1;
    110       1.2   minoura }
    111       1.2   minoura 
    112       1.2   minoura static void
    113       1.2   minoura dmac_attach(parent, self, aux)
    114       1.2   minoura 	struct device *parent, *self;
    115       1.2   minoura 	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.2   minoura 	ia->ia_size = DMAC_CHAN_SIZE * DMAC_NCHAN;
    122       1.2   minoura 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
    123       1.2   minoura #ifdef DIAGNOSTIC
    124       1.2   minoura 	if (r)
    125       1.2   minoura 		panic ("IO map for DMAC corruption??");
    126       1.2   minoura #endif
    127       1.2   minoura 
    128       1.2   minoura 	((struct intio_softc*) parent)->sc_dmac = self;
    129       1.2   minoura 	sc->sc_bst = ia->ia_bst;
    130       1.2   minoura 	bus_space_map (sc->sc_bst, ia->ia_addr, ia->ia_size, 0, &sc->sc_bht);
    131       1.2   minoura 	dmac_init_channels(sc);
    132       1.2   minoura 
    133       1.2   minoura 	printf (": HD63450 DMAC\n%s: 4 channels available.\n", self->dv_xname);
    134       1.2   minoura }
    135       1.2   minoura 
    136       1.2   minoura static void
    137       1.2   minoura dmac_init_channels(sc)
    138       1.2   minoura 	struct dmac_softc *sc;
    139       1.2   minoura {
    140       1.2   minoura 	int i;
    141       1.2   minoura 
    142       1.8   minoura 	DPRINTF (3, ("dmac_init_channels\n"));
    143       1.2   minoura 	for (i=0; i<DMAC_NCHAN; i++) {
    144       1.2   minoura 		sc->sc_channels[i].ch_channel = i;
    145       1.2   minoura 		sc->sc_channels[i].ch_name[0] = 0;
    146       1.2   minoura 		sc->sc_channels[i].ch_softc = &sc->sc_dev;
    147       1.2   minoura 		bus_space_subregion(sc->sc_bst, sc->sc_bht,
    148       1.2   minoura 				    DMAC_CHAN_SIZE*i, DMAC_CHAN_SIZE,
    149       1.2   minoura 				    &sc->sc_channels[i].ch_bht);
    150       1.8   minoura 		sc->sc_channels[i].ch_xfer.dx_dmamap = 0;
    151       1.9   minoura 		/* reset the status register */
    152       1.9   minoura 		bus_space_write_1(sc->sc_bst, sc->sc_channels[i].ch_bht,
    153       1.9   minoura 				  DMAC_REG_CSR, 0xff);
    154       1.2   minoura 	}
    155       1.2   minoura 
    156       1.2   minoura 	return;
    157       1.2   minoura }
    158       1.2   minoura 
    159       1.2   minoura 
    160       1.2   minoura /*
    161       1.2   minoura  * Channel initialization/deinitialization per user device.
    162       1.2   minoura  */
    163       1.2   minoura struct dmac_channel_stat *
    164       1.2   minoura dmac_alloc_channel(self, ch, name,
    165       1.2   minoura 		   normalv, normal, normalarg,
    166       1.2   minoura 		   errorv, error, errorarg)
    167       1.2   minoura 	struct device *self;
    168       1.2   minoura 	int ch;
    169       1.2   minoura 	char *name;
    170       1.2   minoura 	int normalv, errorv;
    171       1.2   minoura 	dmac_intr_handler_t normal, error;
    172       1.2   minoura 	void *normalarg, *errorarg;
    173       1.2   minoura {
    174       1.2   minoura 	struct intio_softc *intio = (void*) self;
    175       1.2   minoura 	struct dmac_softc *sc = (void*) intio->sc_dmac;
    176       1.2   minoura 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    177       1.2   minoura 	char intrname[16];
    178  1.11.2.1   thorpej #ifdef DMAC_ARRAYCHAIN
    179       1.8   minoura 	int r, dummy;
    180  1.11.2.1   thorpej #endif
    181       1.2   minoura 
    182       1.9   minoura 	printf ("%s: allocating ch %d for %s.\n",
    183       1.9   minoura 		sc->sc_dev.dv_xname, ch, name);
    184       1.8   minoura 	DPRINTF (3, ("dmamap=%p\n", (void*) chan->ch_xfer.dx_dmamap));
    185       1.2   minoura #ifdef DIAGNOSTIC
    186       1.2   minoura 	if (ch < 0 || ch >= DMAC_NCHAN)
    187       1.2   minoura 		panic ("Invalid DMAC channel.");
    188       1.2   minoura 	if (chan->ch_name[0])
    189       1.2   minoura 		panic ("DMAC: channel in use.");
    190       1.2   minoura 	if (strlen(name) > 8)
    191       1.2   minoura 	  	panic ("DMAC: wrong user name.");
    192       1.2   minoura #endif
    193       1.2   minoura 
    194       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    195       1.8   minoura 	/* allocate the DMAC arraychaining map */
    196       1.8   minoura 	r = bus_dmamem_alloc(intio->sc_dmat,
    197       1.8   minoura 			     sizeof(struct dmac_sg_array) * DMAC_MAPSIZE,
    198       1.8   minoura 			     4, 0, &chan->ch_seg[0], 1, &dummy,
    199       1.8   minoura 			     BUS_DMA_NOWAIT);
    200       1.8   minoura 	if (r)
    201       1.8   minoura 		panic ("DMAC: cannot alloc DMA safe memory");
    202       1.8   minoura 	r = bus_dmamem_map(intio->sc_dmat,
    203       1.8   minoura 			   &chan->ch_seg[0], 1,
    204       1.8   minoura 			   sizeof(struct dmac_sg_array) * DMAC_MAPSIZE,
    205       1.8   minoura 			   (caddr_t*) &chan->ch_map,
    206       1.8   minoura 			   BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    207       1.8   minoura 	if (r)
    208       1.8   minoura 		panic ("DMAC: cannot map DMA safe memory");
    209       1.9   minoura #endif
    210       1.8   minoura 
    211       1.9   minoura 	/* fill the channel status structure by the default values. */
    212       1.2   minoura 	strcpy(chan->ch_name, name);
    213       1.2   minoura 	chan->ch_dcr = (DMAC_DCR_XRM_CSWH | DMAC_DCR_OTYP_EASYNC |
    214       1.2   minoura 			DMAC_DCR_OPS_8BIT);
    215       1.9   minoura 	chan->ch_ocr = (DMAC_OCR_SIZE_BYTE | DMAC_OCR_REQG_EXTERNAL);
    216       1.2   minoura 	chan->ch_normalv = normalv;
    217       1.2   minoura 	chan->ch_errorv = errorv;
    218       1.2   minoura 	chan->ch_normal = normal;
    219       1.2   minoura 	chan->ch_error = error;
    220       1.2   minoura 	chan->ch_normalarg = normalarg;
    221       1.2   minoura 	chan->ch_errorarg = errorarg;
    222       1.8   minoura 	chan->ch_xfer.dx_dmamap = 0;
    223       1.2   minoura 
    224       1.2   minoura 	/* setup the device-specific registers */
    225       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    226       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    227       1.2   minoura 			   DMAC_REG_DCR, chan->ch_dcr);
    228       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CPR, 0);
    229       1.2   minoura 
    230       1.2   minoura 	/*
    231       1.2   minoura 	 * X68k physical user space is a subset of the kernel space;
    232       1.2   minoura 	 * the memory is always included in the physical user space,
    233       1.2   minoura 	 * while the device is not.
    234       1.2   minoura 	 */
    235       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    236       1.2   minoura 			   DMAC_REG_BFCR, DMAC_FC_USER_DATA);
    237       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    238       1.2   minoura 			   DMAC_REG_MFCR, DMAC_FC_USER_DATA);
    239       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    240       1.2   minoura 			   DMAC_REG_DFCR, DMAC_FC_KERNEL_DATA);
    241       1.2   minoura 
    242       1.2   minoura 	/* setup the interrupt handlers */
    243       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR, normalv);
    244       1.2   minoura 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR, errorv);
    245       1.2   minoura 
    246       1.2   minoura 	strcpy(intrname, name);
    247       1.2   minoura 	strcat(intrname, "dma");
    248       1.2   minoura 	intio_intr_establish (normalv, intrname, dmac_done, chan);
    249       1.2   minoura 
    250       1.2   minoura 	strcpy(intrname, name);
    251       1.2   minoura 	strcat(intrname, "dmaerr");
    252       1.2   minoura 	intio_intr_establish (errorv, intrname, dmac_error, chan);
    253       1.2   minoura 
    254       1.2   minoura 	return chan;
    255       1.2   minoura }
    256       1.2   minoura 
    257       1.2   minoura int
    258       1.2   minoura dmac_free_channel(self, ch, channel)
    259       1.2   minoura 	struct device *self;
    260       1.2   minoura 	int ch;
    261       1.2   minoura 	void *channel;
    262       1.2   minoura {
    263       1.9   minoura 	struct intio_softc *intio = (void*) self;
    264       1.9   minoura 	struct dmac_softc *sc = (void*) intio->sc_dmac;
    265       1.2   minoura 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    266       1.2   minoura 
    267       1.8   minoura 	DPRINTF (3, ("dmac_free_channel, %d\n", ch));
    268       1.8   minoura 	DPRINTF (3, ("dmamap=%p\n", (void*) chan->ch_xfer.dx_dmamap));
    269       1.2   minoura 	if (chan != channel)
    270       1.2   minoura 		return -1;
    271       1.2   minoura 	if (ch != chan->ch_channel)
    272       1.2   minoura 		return -1;
    273       1.2   minoura 
    274       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    275       1.9   minoura 	bus_dmamem_unmap(intio->sc_dmat, (caddr_t) chan->ch_map,
    276       1.9   minoura 			 sizeof(struct dmac_sg_array) * DMAC_MAPSIZE);
    277       1.9   minoura 	bus_dmamem_free(intio->sc_dmat, &chan->ch_seg[0], 1);
    278       1.9   minoura #endif
    279       1.2   minoura 	chan->ch_name[0] = 0;
    280       1.2   minoura 	intio_intr_disestablish(chan->ch_normalv, channel);
    281       1.2   minoura 	intio_intr_disestablish(chan->ch_errorv, channel);
    282       1.2   minoura 
    283       1.2   minoura 	return 0;
    284       1.2   minoura }
    285       1.2   minoura 
    286       1.2   minoura /*
    287       1.2   minoura  * Initialization / deinitialization per transfer.
    288       1.2   minoura  */
    289       1.2   minoura struct dmac_dma_xfer *
    290       1.9   minoura dmac_alloc_xfer (chan, dmat, dmamap)
    291       1.2   minoura 	struct dmac_channel_stat *chan;
    292       1.2   minoura 	bus_dma_tag_t dmat;
    293       1.2   minoura 	bus_dmamap_t dmamap;
    294       1.2   minoura {
    295       1.8   minoura 	struct dmac_dma_xfer *xf = &chan->ch_xfer;
    296       1.2   minoura 
    297       1.9   minoura 	DPRINTF (3, ("dmac_alloc_xfer\n"));
    298       1.8   minoura 	xf->dx_channel = chan;
    299       1.8   minoura 	xf->dx_dmamap = dmamap;
    300       1.8   minoura 	xf->dx_tag = dmat;
    301       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    302       1.9   minoura 	xf->dx_array = chan->ch_map;
    303       1.9   minoura 	xf->dx_done = 0;
    304       1.9   minoura #endif
    305      1.11   minoura 	xf->dx_nextoff = xf->dx_nextsize = -1;
    306       1.9   minoura 	return xf;
    307       1.9   minoura }
    308       1.9   minoura 
    309       1.9   minoura int
    310       1.9   minoura dmac_load_xfer (self, xf)
    311       1.9   minoura 	struct device *self;
    312       1.9   minoura 	struct dmac_dma_xfer *xf;
    313       1.9   minoura {
    314       1.9   minoura 	struct dmac_softc *sc = (void*) self;
    315       1.9   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    316       1.9   minoura 
    317       1.9   minoura 	DPRINTF (3, ("dmac_load_xfer\n"));
    318       1.9   minoura 
    319       1.9   minoura 	xf->dx_ocr &= ~DMAC_OCR_CHAIN_MASK;
    320       1.9   minoura 	if (xf->dx_dmamap->dm_nsegs == 1)
    321       1.9   minoura 		xf->dx_ocr |= DMAC_OCR_CHAIN_DISABLED;
    322       1.9   minoura 	else {
    323       1.9   minoura 		xf->dx_ocr |= DMAC_OCR_CHAIN_ARRAY;
    324       1.9   minoura 		xf->dx_nextoff = ~0;
    325       1.9   minoura 		xf->dx_nextsize = ~0;
    326       1.9   minoura 	}
    327       1.9   minoura 
    328       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    329       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR, xf->dx_scr);
    330       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    331       1.9   minoura 			  DMAC_REG_OCR, (xf->dx_ocr | chan->ch_ocr));
    332       1.9   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    333       1.9   minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    334       1.9   minoura 
    335       1.9   minoura 	return 0;
    336       1.9   minoura }
    337       1.9   minoura 
    338       1.9   minoura struct dmac_dma_xfer *
    339       1.9   minoura dmac_prepare_xfer (chan, dmat, dmamap, dir, scr, dar)
    340       1.9   minoura 	struct dmac_channel_stat *chan;
    341       1.9   minoura 	bus_dma_tag_t dmat;
    342       1.9   minoura 	bus_dmamap_t dmamap;
    343       1.9   minoura 	int dir, scr;
    344       1.9   minoura 	void *dar;
    345       1.9   minoura {
    346       1.9   minoura 	struct dmac_dma_xfer *xf;
    347       1.9   minoura 	struct dmac_softc *sc = (struct dmac_softc*) chan->ch_softc;
    348       1.9   minoura 
    349       1.9   minoura 	xf = dmac_alloc_xfer(chan, dmat, dmamap);
    350       1.9   minoura 
    351       1.8   minoura 	xf->dx_ocr = dir & DMAC_OCR_DIR_MASK;
    352       1.8   minoura 	xf->dx_scr = scr & (DMAC_SCR_MAC_MASK|DMAC_SCR_DAC_MASK);
    353       1.8   minoura 	xf->dx_device = dar;
    354       1.9   minoura 
    355       1.9   minoura 	dmac_load_xfer(&sc->sc_dev, xf);
    356       1.2   minoura 
    357       1.8   minoura 	return xf;
    358       1.2   minoura }
    359       1.2   minoura 
    360       1.2   minoura #ifdef DMAC_DEBUG
    361       1.2   minoura static struct dmac_channel_stat *debugchan = 0;
    362       1.2   minoura #endif
    363       1.2   minoura 
    364       1.2   minoura #ifdef DMAC_DEBUG
    365       1.2   minoura static u_int8_t dcsr, dcer, ddcr, docr, dscr, dccr, dcpr, dgcr,
    366       1.2   minoura   dnivr, deivr, ddfcr, dmfcr, dbfcr;
    367       1.2   minoura static u_int16_t dmtcr, dbtcr;
    368       1.2   minoura static u_int32_t ddar, dmar, dbar;
    369       1.2   minoura #endif
    370       1.2   minoura /*
    371       1.2   minoura  * Do the actual transfer.
    372       1.2   minoura  */
    373       1.2   minoura int
    374       1.2   minoura dmac_start_xfer(self, xf)
    375       1.2   minoura 	struct device *self;
    376       1.2   minoura 	struct dmac_dma_xfer *xf;
    377       1.2   minoura {
    378       1.9   minoura 	return dmac_start_xfer_offset(self, xf, 0, 0);
    379       1.9   minoura }
    380       1.9   minoura 
    381       1.9   minoura int
    382       1.9   minoura dmac_start_xfer_offset(self, xf, offset, size)
    383       1.9   minoura 	struct device *self;
    384       1.9   minoura 	struct dmac_dma_xfer *xf;
    385       1.9   minoura 	u_int offset;
    386       1.9   minoura 	u_int size;
    387       1.9   minoura {
    388       1.2   minoura 	struct dmac_softc *sc = (void*) self;
    389       1.2   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    390       1.9   minoura 	struct x68k_bus_dmamap *dmamap = xf->dx_dmamap;
    391  1.11.2.1   thorpej 	int go = DMAC_CCR_STR|DMAC_CCR_INT;
    392  1.11.2.1   thorpej #ifdef DMAC_ARRAYCHAIN
    393  1.11.2.1   thorpej 	int c;
    394  1.11.2.1   thorpej #endif
    395       1.2   minoura 
    396       1.8   minoura 	DPRINTF (3, ("dmac_start_xfer\n"));
    397       1.2   minoura #ifdef DMAC_DEBUG
    398       1.2   minoura 	debugchan=chan;
    399       1.2   minoura #endif
    400       1.2   minoura 
    401       1.9   minoura 	if (size == 0) {
    402       1.9   minoura #ifdef DIAGNOSTIC
    403       1.9   minoura 		if (offset != 0)
    404       1.9   minoura 			panic ("dmac_start_xfer_offset: invalid offset %x",
    405       1.9   minoura 			       offset);
    406       1.9   minoura #endif
    407       1.9   minoura 		size = dmamap->dm_mapsize;
    408       1.9   minoura 	}
    409       1.9   minoura 
    410       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    411       1.9   minoura #ifdef DIAGNOSTIC
    412       1.9   minoura 	if (xf->dx_done)
    413       1.9   minoura 		panic("dmac_start_xfer: DMA transfer in progress");
    414       1.9   minoura #endif
    415       1.9   minoura #endif
    416       1.2   minoura 	DPRINTF (3, ("First program:\n"));
    417       1.9   minoura #ifdef DIAGNOSTIC
    418       1.9   minoura 	if ((offset >= dmamap->dm_mapsize) ||
    419       1.9   minoura 	    (offset + size > dmamap->dm_mapsize))
    420       1.9   minoura 		panic ("dmac_start_xfer_offset: invalid offset: "
    421  1.11.2.2  jdolecek 			"offset=%d, size=%d, mapsize=%ld",
    422       1.9   minoura 		       offset, size, dmamap->dm_mapsize);
    423       1.9   minoura #endif
    424       1.8   minoura 	/* program DMAC in single block mode or array chainning mode */
    425       1.9   minoura 	if (dmamap->dm_nsegs == 1) {
    426       1.8   minoura 		DPRINTF(3, ("single block mode\n"));
    427       1.9   minoura #ifdef DIAGNOSTIC
    428       1.9   minoura 		if (dmamap->dm_mapsize != dmamap->dm_segs[0].ds_len)
    429       1.9   minoura 			panic ("dmac_start_xfer_offset: dmamap curruption");
    430       1.9   minoura #endif
    431       1.9   minoura 		if (offset == xf->dx_nextoff &&
    432       1.9   minoura 		    size == xf->dx_nextsize) {
    433       1.9   minoura 			/* Use continued operation */
    434       1.9   minoura 			go |=  DMAC_CCR_CNT;
    435       1.9   minoura 			xf->dx_nextoff += size;
    436       1.9   minoura 		} else {
    437       1.9   minoura 			bus_space_write_4(sc->sc_bst, chan->ch_bht,
    438       1.9   minoura 					  DMAC_REG_MAR,
    439       1.9   minoura 					  (int) dmamap->dm_segs[0].ds_addr
    440       1.9   minoura 					  + offset);
    441       1.9   minoura 			bus_space_write_2(sc->sc_bst, chan->ch_bht,
    442       1.9   minoura 					  DMAC_REG_MTCR, (int) size);
    443       1.9   minoura 			xf->dx_nextoff = offset;
    444       1.9   minoura 			xf->dx_nextsize = size;
    445       1.9   minoura 		}
    446       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    447       1.8   minoura 		xf->dx_done = 1;
    448       1.9   minoura #endif
    449       1.8   minoura 	} else {
    450       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    451       1.9   minoura 		c = dmac_program_arraychain(self, xf, offset, size);
    452       1.8   minoura 		bus_space_write_4(sc->sc_bst, chan->ch_bht,
    453       1.8   minoura 				  DMAC_REG_BAR, (int) chan->ch_seg[0].ds_addr);
    454       1.8   minoura 		bus_space_write_2(sc->sc_bst, chan->ch_bht,
    455       1.8   minoura 				  DMAC_REG_BTCR, c);
    456       1.9   minoura #else
    457       1.9   minoura 		panic ("DMAC: unexpected use of arraychaining mode");
    458       1.9   minoura #endif
    459       1.8   minoura 	}
    460       1.2   minoura 
    461       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    462       1.2   minoura 
    463       1.2   minoura 	/* START!! */
    464       1.2   minoura 	DDUMPREGS (3, ("first start\n"));
    465       1.2   minoura #ifdef DMAC_DEBUG
    466       1.2   minoura 	dcsr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR);
    467       1.2   minoura 	dcer = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER);
    468       1.2   minoura 	ddcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR);
    469       1.2   minoura 	docr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR);
    470       1.2   minoura 	dscr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR);
    471       1.2   minoura 	dccr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR);
    472       1.2   minoura 	dcpr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR);
    473       1.2   minoura 	dgcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR);
    474       1.2   minoura 	dnivr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR);
    475       1.2   minoura 	deivr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR);
    476       1.2   minoura 	ddfcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR);
    477       1.2   minoura 	dmfcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR);
    478       1.2   minoura 	dbfcr = bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR);
    479       1.2   minoura 	dmtcr = bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR);
    480       1.2   minoura 	dbtcr = bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR);
    481       1.2   minoura 	ddar = bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR);
    482       1.2   minoura 	dmar = bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR);
    483       1.2   minoura 	dbar = bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR);
    484       1.2   minoura #endif
    485       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    486       1.2   minoura #if defined(M68040) || defined(M68060)
    487       1.9   minoura 	/* flush data cache for the map */
    488       1.9   minoura 	if (dmamap->dm_nsegs != 1 && mmutype == MMU_68040)
    489       1.9   minoura 		dma_cachectl((caddr_t) xf->dx_array,
    490       1.9   minoura 			     sizeof(struct dmac_sg_array) * c);
    491       1.9   minoura #endif
    492       1.2   minoura #endif
    493       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR, go);
    494       1.9   minoura 
    495       1.9   minoura 	if (xf->dx_nextoff != ~0) {
    496       1.9   minoura 		bus_space_write_4(sc->sc_bst, chan->ch_bht,
    497       1.9   minoura 				  DMAC_REG_BAR, xf->dx_nextoff);
    498       1.9   minoura 		bus_space_write_2(sc->sc_bst, chan->ch_bht,
    499       1.9   minoura 				  DMAC_REG_BTCR, xf->dx_nextsize);
    500       1.9   minoura 	}
    501       1.2   minoura 
    502       1.2   minoura 	return 0;
    503       1.2   minoura }
    504       1.2   minoura 
    505       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    506       1.2   minoura static int
    507       1.9   minoura dmac_program_arraychain(self, xf, offset, size)
    508       1.2   minoura 	struct device *self;
    509       1.2   minoura 	struct dmac_dma_xfer *xf;
    510       1.9   minoura 	u_int offset;
    511       1.9   minoura 	u_int size;
    512       1.2   minoura {
    513       1.2   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    514       1.2   minoura 	int ch = chan->ch_channel;
    515       1.2   minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    516       1.2   minoura 	int i, j;
    517       1.2   minoura 
    518       1.9   minoura 	/* XXX not yet!! */
    519       1.9   minoura 	if (offset != 0 || size != map->dm_mapsize)
    520       1.9   minoura 		panic ("dmac_program_arraychain: unsupported offset/size");
    521       1.9   minoura 
    522       1.8   minoura 	DPRINTF (3, ("dmac_program_arraychain\n"));
    523       1.2   minoura 	for (i=0, j=xf->dx_done; i<DMAC_MAPSIZE && j<map->dm_nsegs;
    524       1.2   minoura 	     i++, j++) {
    525       1.8   minoura 		xf->dx_array[i].da_addr = map->dm_segs[j].ds_addr;
    526       1.2   minoura #ifdef DIAGNOSTIC
    527       1.9   minoura 		if (map->dm_segs[j].ds_len > DMAC_MAXSEGSZ)
    528       1.9   minoura 			panic ("dmac_program_arraychain: wrong map: %ld",
    529       1.9   minoura 			       map->dm_segs[j].ds_len);
    530       1.2   minoura #endif
    531       1.8   minoura 		xf->dx_array[i].da_count = map->dm_segs[j].ds_len;
    532       1.2   minoura 	}
    533       1.2   minoura 	xf->dx_done = j;
    534       1.2   minoura 
    535       1.2   minoura 	return i;
    536       1.2   minoura }
    537       1.9   minoura #endif
    538       1.2   minoura 
    539       1.2   minoura /*
    540       1.2   minoura  * interrupt handlers.
    541       1.2   minoura  */
    542       1.2   minoura static int
    543       1.2   minoura dmac_done(arg)
    544       1.2   minoura 	void *arg;
    545       1.2   minoura {
    546       1.2   minoura 	struct dmac_channel_stat *chan = arg;
    547       1.2   minoura 	struct dmac_softc *sc = (void*) chan->ch_softc;
    548  1.11.2.1   thorpej #ifdef DMAC_ARRAYCHAIN
    549       1.8   minoura 	struct dmac_dma_xfer *xf = &chan->ch_xfer;
    550       1.2   minoura 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    551       1.2   minoura 	int c;
    552  1.11.2.1   thorpej #endif
    553       1.2   minoura 
    554       1.2   minoura 	DPRINTF (3, ("dmac_done\n"));
    555       1.9   minoura 
    556       1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    557       1.2   minoura 
    558       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    559       1.2   minoura 	if (xf->dx_done == map->dm_nsegs) {
    560       1.9   minoura 		xf->dx_done = 0;
    561       1.9   minoura #endif
    562       1.2   minoura 		/* Done */
    563       1.2   minoura 		return (*chan->ch_normal) (chan->ch_normalarg);
    564       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    565       1.2   minoura 	}
    566       1.9   minoura #endif
    567       1.2   minoura 
    568       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    569       1.2   minoura 	/* Continue transfer */
    570       1.2   minoura 	DPRINTF (3, ("reprograming\n"));
    571       1.9   minoura 	c = dmac_program_arraychain (&sc->sc_dev, xf, 0, map->dm_mapsize);
    572       1.2   minoura 
    573       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    574       1.2   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    575       1.2   minoura 			  DMAC_REG_BAR, (int) chan->ch_map);
    576       1.2   minoura 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    577       1.2   minoura 			  DMAC_REG_DAR, (int) xf->dx_device);
    578       1.9   minoura 	bus_space_write_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR, c);
    579       1.2   minoura 
    580       1.2   minoura 	/* START!! */
    581       1.2   minoura 	DDUMPREGS (3, ("restart\n"));
    582       1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    583       1.2   minoura 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    584       1.2   minoura 
    585       1.2   minoura 	return 1;
    586       1.9   minoura #endif
    587       1.2   minoura }
    588       1.2   minoura 
    589       1.2   minoura static int
    590       1.2   minoura dmac_error(arg)
    591       1.2   minoura 	void *arg;
    592       1.2   minoura {
    593       1.2   minoura 	struct dmac_channel_stat *chan = arg;
    594       1.2   minoura 	struct dmac_softc *sc = (void*) chan->ch_softc;
    595       1.2   minoura 
    596       1.2   minoura 	printf ("DMAC transfer error CSR=%02x, CER=%02x\n",
    597       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    598       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER));
    599       1.2   minoura 	DPRINTF(5, ("registers were:\n"));
    600       1.2   minoura #ifdef DMAC_DEBUG
    601       1.2   minoura 	if ((dmacdebug & 0x0f) > 5) {
    602       1.9   minoura 		printf ("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x, "
    603       1.2   minoura 			"CCR=%02x, CPR=%02x, GCR=%02x\n",
    604       1.2   minoura 			dcsr, dcer, ddcr, docr, dscr, dccr, dcpr, dgcr);
    605       1.2   minoura 		printf ("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, "
    606       1.2   minoura 			"DFCR=%02x, MFCR=%02x, BFCR=%02x\n",
    607       1.2   minoura 			dnivr, deivr, dmtcr, dbtcr, ddfcr, dmfcr, dbfcr);
    608       1.2   minoura 		printf ("DAR=%08x, MAR=%08x, BAR=%08x\n",
    609       1.2   minoura 			ddar, dmar, dbar);
    610       1.2   minoura 	}
    611       1.2   minoura #endif
    612       1.2   minoura 
    613       1.9   minoura 	/* Clear the status bits */
    614       1.2   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    615       1.2   minoura 	DDUMPREGS(3, ("dmac_error\n"));
    616       1.2   minoura 
    617       1.9   minoura #ifdef DMAC_ARRAYCHAIN
    618       1.9   minoura 	chan->ch_xfer.dx_done = 0;
    619       1.9   minoura #endif
    620       1.9   minoura 
    621       1.2   minoura 	return (*chan->ch_error) (chan->ch_errorarg);
    622       1.2   minoura }
    623       1.2   minoura 
    624       1.9   minoura int
    625       1.9   minoura dmac_abort_xfer(self, xf)
    626       1.9   minoura 	struct device *self;
    627       1.9   minoura 	struct dmac_dma_xfer *xf;
    628       1.9   minoura {
    629       1.9   minoura 	struct dmac_softc *sc = (void*) self;
    630       1.9   minoura 	struct dmac_channel_stat *chan = xf->dx_channel;
    631       1.9   minoura 
    632       1.9   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR,
    633       1.9   minoura 			  DMAC_CCR_INT | DMAC_CCR_HLT);
    634      1.10   minoura 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    635      1.11   minoura 	xf->dx_nextoff = xf->dx_nextsize = -1;
    636       1.9   minoura 
    637       1.9   minoura 	return 0;
    638       1.9   minoura }
    639       1.2   minoura 
    640       1.2   minoura #ifdef DMAC_DEBUG
    641       1.2   minoura static int
    642       1.2   minoura dmac_dump_regs(void)
    643       1.2   minoura {
    644       1.2   minoura 	struct dmac_channel_stat *chan = debugchan;
    645       1.2   minoura 	struct dmac_softc *sc;
    646       1.2   minoura 
    647  1.11.2.2  jdolecek 	if ((chan == 0) || (dmacdebug & 0xf0))
    648  1.11.2.2  jdolecek 		return 0;
    649       1.2   minoura 	sc = (void*) chan->ch_softc;
    650       1.2   minoura 
    651       1.2   minoura 	printf ("DMAC channel %d registers\n", chan->ch_channel);
    652       1.2   minoura 	printf ("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x,"
    653       1.2   minoura 		"CCR=%02x, CPR=%02x, GCR=%02x\n",
    654       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    655       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER),
    656       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR),
    657       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR),
    658       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR),
    659       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR),
    660       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR),
    661       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR));
    662       1.2   minoura 	printf ("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x,"
    663       1.2   minoura 		"MFCR=%02x, BFCR=%02x\n",
    664       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR),
    665       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR),
    666       1.2   minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR),
    667       1.2   minoura 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR),
    668       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR),
    669       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR),
    670       1.2   minoura 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR));
    671       1.2   minoura 	printf ("DAR=%08x, MAR=%08x, BAR=%08x\n",
    672       1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR),
    673       1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR),
    674       1.2   minoura 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR));
    675       1.2   minoura 
    676       1.2   minoura 	return 0;
    677       1.2   minoura }
    678       1.2   minoura #endif
    679