Home | History | Annotate | Line # | Download | only in dev
intio_dmac.c revision 1.1.2.1
      1 /*	$NetBSD: intio_dmac.c,v 1.1.2.1 1999/01/30 15:07:41 minoura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Minoura Makoto.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Hitachi HD63450 (= Motorola MC68450) DMAC driver for x68k.
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 #include <sys/extent.h>
     48 #include <vm/vm.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/cpu.h>
     52 #include <machine/frame.h>
     53 
     54 #include <arch/x68k/dev/intiovar.h>
     55 #include <arch/x68k/dev/dmacvar.h>
     56 
     57 #define DMAC_DEBUG
     58 
     59 #ifdef DMAC_DEBUG
     60 #define DPRINTF(n,x)	if (dmacdebug>(n)&0x0f) printf x
     61 #define DDUMPREGS(n,x)	if (dmacdebug>(n)&0x0f) {printf x; dmac_dump_regs();}
     62 int dmacdebug = 0;
     63 #else
     64 #define DPRINTF(n,x)
     65 #define DDUMPREGS(n,x)
     66 #endif
     67 
     68 static void dmac_init_channels __P((struct dmac_softc*));
     69 static int dmac_program_arraychain __P((struct device*, struct dmac_dma_xfer*));
     70 static int dmac_done __P((void*));
     71 static int dmac_error __P((void*));
     72 
     73 static int dmac_dump_regs __P((void));
     74 
     75 /*
     76  * autoconf stuff
     77  */
     78 static int dmac_match __P((struct device *, struct cfdata *, void *));
     79 static void dmac_attach __P((struct device *, struct device *, void *));
     80 
     81 struct cfattach dmac_ca = {
     82 	sizeof(struct dmac_softc), dmac_match, dmac_attach
     83 };
     84 
     85 static int
     86 dmac_match(parent, cf, aux)
     87 	struct device *parent;
     88 	struct cfdata *cf;
     89 	void *aux;
     90 {
     91 	struct intio_attach_args *ia = aux;
     92 
     93 	if (strcmp (ia->ia_name, "dmac") != 0)
     94 		return (0);
     95 	if (cf->cf_unit != 0)
     96 		return (0);
     97 
     98 	/* fixed address */
     99 	if (ia->ia_addr != DMAC_ADDR)
    100 		return (0);
    101 	if (ia->ia_intr != -1)
    102 		return (0);
    103 
    104 	return 1;
    105 }
    106 
    107 static void
    108 dmac_attach(parent, self, aux)
    109 	struct device *parent, *self;
    110 	void *aux;
    111 {
    112 	struct dmac_softc *sc = (struct dmac_softc *)self;
    113 	struct intio_attach_args *ia = aux;
    114 	int r;
    115 
    116 	ia->ia_size = DMAC_CHAN_SIZE * DMAC_NCHAN;
    117 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
    118 #ifdef DIAGNOSTIC
    119 	if (r)
    120 		panic ("IO map for DMAC corruption??");
    121 #endif
    122 
    123 	((struct intio_softc*) parent)->sc_dmac = self;
    124 	sc->sc_bst = ia->ia_bst;
    125 	bus_space_map (sc->sc_bst, ia->ia_addr, ia->ia_size, 0, &sc->sc_bht);
    126 	dmac_init_channels(sc);
    127 
    128 	printf (": HD63450 DMAC\n%s: 4 channels available.\n", self->dv_xname);
    129 }
    130 
    131 #define DMAC_MAPSIZE 64
    132 /* Allocate statically in order to make sure the DMAC can reach the maps. */
    133 static struct dmac_sg_array dmac_map[DMAC_NCHAN][DMAC_MAPSIZE];
    134 
    135 static void
    136 dmac_init_channels(sc)
    137 	struct dmac_softc *sc;
    138 {
    139 	int i;
    140 	pmap_t pmap = pmap_kernel();
    141 
    142 	for (i=0; i<DMAC_NCHAN; i++) {
    143 		sc->sc_channels[i].ch_channel = i;
    144 		sc->sc_channels[i].ch_name[0] = 0;
    145 		sc->sc_channels[i].ch_softc = &sc->sc_dev;
    146 		sc->sc_channels[i].ch_map =
    147 		  (void*) pmap_extract (pmap, (vaddr_t) &dmac_map[i]);
    148 		bus_space_subregion(sc->sc_bst, sc->sc_bht,
    149 				    DMAC_CHAN_SIZE*i, DMAC_CHAN_SIZE,
    150 				    &sc->sc_channels[i].ch_bht);
    151 	}
    152 
    153 	return;
    154 }
    155 
    156 
    157 /*
    158  * Channel initialization/deinitialization per user device.
    159  */
    160 struct dmac_channel_stat *
    161 dmac_alloc_channel(self, ch, name, normalv, normal, errorv, error)
    162 	struct device *self;
    163 	int ch;
    164 	char *name;
    165 	int normalv, errorv;
    166 	dmac_intr_handler_t normal, error;
    167 {
    168 	struct intio_softc *intio = (void*) self;
    169 	struct dmac_softc *sc = (void*) intio->sc_dmac;
    170 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    171 	char intrname[16];
    172 
    173 #ifdef DIAGNOSTIC
    174 	if (ch < 0 || ch >= DMAC_NCHAN)
    175 		panic ("Invalid DMAC channel.");
    176 	if (chan->ch_name[0])
    177 		panic ("DMAC: channel in use.");
    178 	if (strlen(name) > 8)
    179 	  	panic ("DMAC: wrong user name.");
    180 #endif
    181 
    182 	/* fill the channel status structure. */
    183 	strcpy(chan->ch_name, name);
    184 	chan->ch_dcr = (DMAC_DCR_XRM_CSWOH | DMAC_DCR_OTYP_EASYNC |
    185 			DMAC_DCR_OPS_8BIT);
    186 	chan->ch_ocr = (DMAC_OCR_SIZE_BYTE | DMAC_OCR_CHAIN_ARRAY |
    187 			DMAC_OCR_REQG_EXTERNAL);
    188 	chan->ch_normalv = normalv;
    189 	chan->ch_errorv = errorv;
    190 	chan->ch_normal = normal;
    191 	chan->ch_error = error;
    192 	chan->ch_xfer_in_progress = 0;
    193 
    194 	/* setup the device-specific registers */
    195 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    196 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    197 			   DMAC_REG_DCR, chan->ch_dcr);
    198 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_CPR, 0);
    199 
    200 	/*
    201 	 * X68k physical user space is a subset of the kernel space;
    202 	 * the memory is always included in the physical user space,
    203 	 * while the device is not.
    204 	 */
    205 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    206 			   DMAC_REG_BFCR, DMAC_FC_USER_DATA);
    207 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    208 			   DMAC_REG_MFCR, DMAC_FC_USER_DATA);
    209 	bus_space_write_1 (sc->sc_bst, chan->ch_bht,
    210 			   DMAC_REG_DFCR, DMAC_FC_KERNEL_DATA);
    211 
    212 	/* setup the interrupt handlers */
    213 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR, normalv);
    214 	bus_space_write_1 (sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR, errorv);
    215 
    216 	strcpy(intrname, name);
    217 	strcat(intrname, "dma");
    218 	intio_intr_establish (normalv, intrname, dmac_done, chan);
    219 
    220 	strcpy(intrname, name);
    221 	strcat(intrname, "dmaerr");
    222 	intio_intr_establish (errorv, intrname, dmac_error, chan);
    223 
    224 	return chan;
    225 }
    226 
    227 int
    228 dmac_free_channel(self, ch, channel)
    229 	struct device *self;
    230 	int ch;
    231 	void *channel;
    232 {
    233 	struct dmac_softc *sc = (void*) self;
    234 	struct dmac_channel_stat *chan = &sc->sc_channels[ch];
    235 
    236 	if (chan != channel)
    237 		return -1;
    238 	if (ch != chan->ch_channel)
    239 		return -1;
    240 #if DIAGNOSTIC
    241 	if (chan->ch_xfer_in_progress)
    242 		panic ("dmac_free_channel: DMA transfer in progress");
    243 #endif
    244 
    245 	chan->ch_name[0] = 0;
    246 	intio_intr_disestablish(chan->ch_normalv, channel);
    247 	intio_intr_disestablish(chan->ch_errorv, channel);
    248 
    249 	return 0;
    250 }
    251 
    252 /*
    253  * Initialization / deinitialization per transfer.
    254  */
    255 struct dmac_dma_xfer *
    256 dmac_prepare_xfer (chan, dmat, dmamap, dir, scr, dar)
    257 	struct dmac_channel_stat *chan;
    258 	bus_dma_tag_t dmat;
    259 	bus_dmamap_t dmamap;
    260 	int dir, scr;
    261 	void *dar;
    262 {
    263 	struct dmac_dma_xfer *r = malloc (sizeof (struct dmac_dma_xfer),
    264 					  M_DEVBUF, M_WAITOK);
    265 
    266 	r->dx_channel = chan;
    267 	r->dx_dmamap = dmamap;
    268 	r->dx_tag = dmat;
    269 	r->dx_ocr = dir & DMAC_OCR_DIR_MASK;
    270 	r->dx_scr = scr & (DMAC_SCR_MAC_MASK|DMAC_SCR_DAC_MASK);
    271 	r->dx_device = (void*) pmap_extract (pmap_kernel(), (vaddr_t) dar);
    272 	r->dx_done = 0;
    273 
    274 	return r;
    275 }
    276 
    277 #ifdef DMAC_DEBUG
    278 static struct dmac_channel_stat *debugchan = 0;
    279 #endif
    280 
    281 /*
    282  * Do the actual transfer.
    283  */
    284 int
    285 dmac_start_xfer(self, xf)
    286 	struct device *self;
    287 	struct dmac_dma_xfer *xf;
    288 {
    289 	struct dmac_softc *sc = (void*) self;
    290 	struct dmac_channel_stat *chan = xf->dx_channel;
    291 	int c;
    292 
    293 
    294 #ifdef DMAC_DEBUG
    295 	debugchan=chan;
    296 #endif
    297 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    298 			  DMAC_REG_OCR, (xf->dx_ocr | chan->ch_ocr));
    299 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    300 			  DMAC_REG_SCR, xf->dx_scr);
    301 
    302 	/* program DMAC in array chainning mode */
    303 	xf->dx_done = 0;
    304 	DPRINTF (3, ("First program:\n"));
    305 	c = dmac_program_arraychain(self, xf);
    306 
    307 	/* setup the address/count registers */
    308 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    309 			  DMAC_REG_BAR, (int) chan->ch_map);
    310 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    311 			  DMAC_REG_DAR, (int) xf->dx_device);
    312 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    313 			  DMAC_REG_CSR, 0xff);
    314 	bus_space_write_2(sc->sc_bst, chan->ch_bht,
    315 			  DMAC_REG_BTCR, c);
    316 
    317 	/* START!! */
    318 #if defined(M68040) || defined(M68060)
    319 	if (mmutype == MMU_68040)
    320 		DCIA();		/* XXX: granularity */
    321 #endif
    322 	DDUMPREGS (3, ("first start\n"));
    323 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    324 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    325 	chan->ch_xfer_in_progress = xf;
    326 
    327 	return 0;
    328 }
    329 
    330 static int
    331 dmac_program_arraychain(self, xf)
    332 	struct device *self;
    333 	struct dmac_dma_xfer *xf;
    334 {
    335 	struct dmac_softc *sc = (void*) self;
    336 	struct dmac_channel_stat *chan = xf->dx_channel;
    337 	int ch = chan->ch_channel;
    338 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    339 	int i, j;
    340 
    341 	for (i=0, j=xf->dx_done; i<DMAC_MAPSIZE && j<map->dm_nsegs;
    342 	     i++, j++) {
    343 		dmac_map[ch][i].da_addr = (void*) map->dm_segs[j].ds_addr;
    344 		if (map->dm_segs[j].ds_len > 0xff00)
    345 		printf ("dmac_program_arraychain: wrong map: %d\n", map->dm_segs[j].ds_len);
    346 		dmac_map[ch][i].da_count = map->dm_segs[j].ds_len;
    347 	}
    348 	xf->dx_done = j;
    349 
    350 	return i;
    351 }
    352 
    353 /*
    354  * interrupt handlers.
    355  */
    356 static int
    357 dmac_done(arg)
    358 	void *arg;
    359 {
    360 	struct dmac_channel_stat *chan = arg;
    361 	struct dmac_softc *sc = (void*) chan->ch_softc;
    362 	struct dmac_dma_xfer *xf = chan->ch_xfer_in_progress;
    363 	struct x68k_bus_dmamap *map = xf->dx_dmamap;
    364 	int c;
    365 
    366 	DPRINTF (3, ("dmac_done\n"));
    367 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    368 #if defined(M68040) || defined(M68060)
    369 	if (mmutype == MMU_68040)
    370 		DCIA();		/* XXX: granularity */
    371 #endif
    372 
    373 	if (xf->dx_done == map->dm_nsegs) {
    374 		/* Done */
    375 		chan->ch_xfer_in_progress = 0;
    376 		return (*chan->ch_normal) (arg);
    377 	}
    378 
    379 	/* Continue transfer */
    380 	DPRINTF (3, ("reprograming\n"));
    381 	c = dmac_program_arraychain (&sc->sc_dev, xf);
    382 
    383 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    384 			  DMAC_REG_BAR, (int) chan->ch_map);
    385 	bus_space_write_4(sc->sc_bst, chan->ch_bht,
    386 			  DMAC_REG_DAR, (int) xf->dx_device);
    387 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    388 			  DMAC_REG_CSR, 0xff);
    389 	bus_space_write_2(sc->sc_bst, chan->ch_bht,
    390 			  DMAC_REG_BTCR, c);
    391 
    392 	/* START!! */
    393 	DDUMPREGS (3, ("restart\n"));
    394 	bus_space_write_1(sc->sc_bst, chan->ch_bht,
    395 			  DMAC_REG_CCR, DMAC_CCR_STR|DMAC_CCR_INT);
    396 
    397 	return 1;
    398 }
    399 
    400 static int
    401 dmac_error(arg)
    402 	void *arg;
    403 {
    404 	struct dmac_channel_stat *chan = arg;
    405 	struct dmac_softc *sc = (void*) chan->ch_softc;
    406 
    407 	DDUMPREGS(3, ("dmac_error\n"));
    408 	bus_space_write_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR, 0xff);
    409 	DDUMPREGS(3, ("dmac_error\n"));
    410 
    411 	return (*chan->ch_error) (arg);
    412 }
    413 
    414 
    415 #ifdef DMAC_DEBUG
    416 static int
    417 dmac_dump_regs(void)
    418 {
    419 	struct dmac_channel_stat *chan = debugchan;
    420 	struct dmac_softc *sc;
    421 
    422 	if ((chan == 0) || (dmacdebug & 0xf0)) return;
    423 	sc = (void*) chan->ch_softc;
    424 
    425 	printf ("DMAC channel %d registers\n");
    426 	printf ("CSR=%02x, CER=%02x, DCR=%02x, OCR=%02x, SCR=%02x,"
    427 		"CCR=%02x, CPR=%02x, GCR=%02x\n",
    428 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CSR),
    429 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CER),
    430 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DCR),
    431 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_OCR),
    432 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_SCR),
    433 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CCR),
    434 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_CPR),
    435 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_GCR));
    436 	printf ("NIVR=%02x, EIVR=%02x, MTCR=%04x, BTCR=%04x, DFCR=%02x,"
    437 		"MFCR=%02x, BFCR=%02x\n",
    438 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_NIVR),
    439 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_EIVR),
    440 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_MTCR),
    441 		bus_space_read_2(sc->sc_bst, chan->ch_bht, DMAC_REG_BTCR),
    442 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_DFCR),
    443 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_MFCR),
    444 		bus_space_read_1(sc->sc_bst, chan->ch_bht, DMAC_REG_BFCR));
    445 	printf ("DAR=%08x, MAR=%08x, BAR=%08x\n",
    446 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_DAR),
    447 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_MAR),
    448 		bus_space_read_4(sc->sc_bst, chan->ch_bht, DMAC_REG_BAR));
    449 
    450 	return 0;
    451 }
    452 #endif
    453