Home | History | Annotate | Line # | Download | only in iomd
iomd_dma.c revision 1.2
      1  1.2  thorpej /* 	$NetBSD: iomd_dma.c,v 1.2 2001/11/27 01:03:52 thorpej Exp $	*/
      2  1.1  reinoud 
      3  1.1  reinoud /*
      4  1.1  reinoud  * Copyright (c) 1995 Scott Stevens
      5  1.1  reinoud  * All rights reserved.
      6  1.1  reinoud  *
      7  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8  1.1  reinoud  * modification, are permitted provided that the following conditions
      9  1.1  reinoud  * are met:
     10  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12  1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     15  1.1  reinoud  * 3. All advertising materials mentioning features or use of this software
     16  1.1  reinoud  *    must display the following acknowledgement:
     17  1.1  reinoud  *	This product includes software developed by Scott Stevens.
     18  1.1  reinoud  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  reinoud  *    derived from this software without specific prior written permission.
     20  1.1  reinoud  *
     21  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  reinoud  *
     32  1.1  reinoud  * RiscBSD kernel project
     33  1.1  reinoud  *
     34  1.1  reinoud  * dma.c
     35  1.1  reinoud  *
     36  1.1  reinoud  * Created      : 15/03/97
     37  1.1  reinoud  */
     38  1.1  reinoud 
     39  1.1  reinoud #define DMA_DEBUG
     40  1.1  reinoud #include <sys/param.h>
     41  1.1  reinoud #include <sys/systm.h>
     42  1.1  reinoud #include <sys/kernel.h>
     43  1.1  reinoud 
     44  1.1  reinoud #include <uvm/uvm_extern.h>
     45  1.1  reinoud 
     46  1.2  thorpej #include <machine/intr.h>
     47  1.1  reinoud #include <machine/pmap.h>
     48  1.1  reinoud #include <arm/iomd/iomdreg.h>
     49  1.1  reinoud #include <arm/iomd/iomdvar.h>
     50  1.1  reinoud #include <arm/iomd/iomd_dma.h>
     51  1.1  reinoud 
     52  1.1  reinoud 
     53  1.1  reinoud /*
     54  1.1  reinoud  * Only for non ARM7500 machines but the kernel could be booted on a different machine
     55  1.1  reinoud  */
     56  1.1  reinoud 
     57  1.1  reinoud static struct dma_ctrl ctrl[6];
     58  1.1  reinoud 
     59  1.1  reinoud void dma_dumpdc __P((struct dma_ctrl *));
     60  1.1  reinoud 
     61  1.1  reinoud void
     62  1.1  reinoud dma_go(dp)
     63  1.1  reinoud 	struct dma_ctrl *dp;
     64  1.1  reinoud {
     65  1.1  reinoud #ifdef DMA_DEBUG
     66  1.1  reinoud 	printf("dma_go()\n");
     67  1.1  reinoud #endif
     68  1.1  reinoud 	if(dp->dc_flags & DMA_FL_READY) {
     69  1.1  reinoud 		dp->dc_flags = DMA_FL_ACTIVE;
     70  1.1  reinoud 		enable_irq(IRQ_DMACH0 + dp->dc_channel);
     71  1.1  reinoud 	}
     72  1.1  reinoud 	else
     73  1.1  reinoud 		panic("dma not ready");
     74  1.1  reinoud }
     75  1.1  reinoud 
     76  1.1  reinoud int
     77  1.1  reinoud dma_reset(dp)
     78  1.1  reinoud 	struct dma_ctrl *dp;
     79  1.1  reinoud {
     80  1.1  reinoud #ifdef DMA_DEBUG
     81  1.1  reinoud 	printf("dma_reset()\n");
     82  1.1  reinoud 	dma_dumpdc(dp);
     83  1.1  reinoud #endif
     84  1.1  reinoud 	*dp->dc_cr = DMA_CR_CLEAR;
     85  1.1  reinoud 	dp->dc_flags = 0;
     86  1.1  reinoud 	disable_irq(IRQ_DMACH0 + dp->dc_channel);
     87  1.1  reinoud 	return(0);
     88  1.1  reinoud }
     89  1.1  reinoud 
     90  1.1  reinoud /*
     91  1.1  reinoud  * Setup dma transfer, prior to the dma_go call
     92  1.1  reinoud  */
     93  1.1  reinoud int
     94  1.1  reinoud dma_setup(dp, start, len, readp)
     95  1.1  reinoud 	struct dma_ctrl *dp;
     96  1.1  reinoud 	int readp;
     97  1.1  reinoud 	u_char *start;
     98  1.1  reinoud 	int len;
     99  1.1  reinoud {
    100  1.1  reinoud #ifdef DMA_DEBUG
    101  1.1  reinoud 	printf("dma_setup(start = %p, len = 0x%08x, readp = %d\n", start, len, readp);
    102  1.1  reinoud #endif
    103  1.1  reinoud 	if(((u_int)start & (dp->dc_dmasize - 1)) ||
    104  1.1  reinoud 	   (len & (dp->dc_dmasize - 1))) {
    105  1.1  reinoud 		printf("dma_setup: unaligned DMA, %p (0x%08x)\n",
    106  1.1  reinoud 			start, len);
    107  1.1  reinoud 	}
    108  1.1  reinoud 	*dp->dc_cr = DMA_CR_CLEAR | DMA_CR_ENABLE | (readp?DMA_CR_DIR:0) | dp->dc_dmasize;
    109  1.1  reinoud 	*dp->dc_cr = DMA_CR_ENABLE | (readp?DMA_CR_DIR:0) | dp->dc_dmasize;
    110  1.1  reinoud 
    111  1.1  reinoud 	dp->dc_nextaddr = start;
    112  1.1  reinoud 	dp->dc_len = len;
    113  1.1  reinoud 
    114  1.1  reinoud 	dp->dc_flags = DMA_FL_READY;
    115  1.1  reinoud 	return(0);
    116  1.1  reinoud }
    117  1.1  reinoud 
    118  1.1  reinoud /*
    119  1.1  reinoud  * return true if DMA is active
    120  1.1  reinoud  */
    121  1.1  reinoud int
    122  1.1  reinoud dma_isactive(dp)
    123  1.1  reinoud 	struct dma_ctrl *dp;
    124  1.1  reinoud {
    125  1.1  reinoud 	return(dp->dc_flags & DMA_FL_ACTIVE);
    126  1.1  reinoud }
    127  1.1  reinoud 
    128  1.1  reinoud /*
    129  1.1  reinoud  * return true if interrupt pending
    130  1.1  reinoud  */
    131  1.1  reinoud int
    132  1.1  reinoud dma_isintr(dp)
    133  1.1  reinoud 	struct dma_ctrl *dp;
    134  1.1  reinoud {
    135  1.1  reinoud #ifdef DMA_DEBUG
    136  1.1  reinoud /*	printf("dma_isintr() returning %d\n", *dp->dc_st & DMA_ST_INT);*/
    137  1.1  reinoud #endif
    138  1.1  reinoud 	return(*dp->dc_st & DMA_ST_INT);
    139  1.1  reinoud }
    140  1.1  reinoud 
    141  1.1  reinoud int
    142  1.1  reinoud dma_intr(dp)
    143  1.1  reinoud 	struct dma_ctrl *dp;
    144  1.1  reinoud {
    145  1.1  reinoud 	u_char status = (*dp->dc_st) & DMA_ST_MASK;
    146  1.1  reinoud 	vm_offset_t cur;
    147  1.1  reinoud 	int len;
    148  1.1  reinoud 	int bufap = 0;
    149  1.1  reinoud 
    150  1.1  reinoud #ifdef DMA_DEBUG
    151  1.1  reinoud 	printf("dma_intr() status = 0x%02x\n", status);
    152  1.1  reinoud #endif
    153  1.1  reinoud 
    154  1.1  reinoud 	if(!(dp->dc_flags & DMA_FL_ACTIVE)) {
    155  1.1  reinoud 		/* interrupt whilst not active */
    156  1.1  reinoud 		/* ie. last buffer programmed */
    157  1.1  reinoud 		dma_reset(dp);
    158  1.1  reinoud 		return(0);
    159  1.1  reinoud 	}
    160  1.1  reinoud 
    161  1.1  reinoud 	switch(status) {
    162  1.1  reinoud 	case DMA_ST_OVER | DMA_ST_INT:
    163  1.1  reinoud 	case DMA_ST_OVER | DMA_ST_INT | DMA_ST_CHAN:
    164  1.1  reinoud 		/* idle, either first buffer or finished */
    165  1.1  reinoud 		if(status & DMA_ST_CHAN) {
    166  1.1  reinoud 			/* fill buffer B */
    167  1.1  reinoud 			bufap = 0;
    168  1.1  reinoud 			goto fill;
    169  1.1  reinoud 		}
    170  1.1  reinoud 		else {
    171  1.1  reinoud 			/* fill buffer A */
    172  1.1  reinoud 			bufap = 1;
    173  1.1  reinoud 			goto fill;
    174  1.1  reinoud 		}
    175  1.1  reinoud 		break;
    176  1.1  reinoud 	case DMA_ST_INT:
    177  1.1  reinoud 	case DMA_ST_INT | DMA_ST_CHAN:
    178  1.1  reinoud 		/* buffer ready */
    179  1.1  reinoud 		if(status & DMA_ST_CHAN) {
    180  1.1  reinoud 			/* fill buffer A */
    181  1.1  reinoud 			bufap = 1;
    182  1.1  reinoud 			goto fill;
    183  1.1  reinoud 		}
    184  1.1  reinoud 		else {
    185  1.1  reinoud 			/* fill buffer B */
    186  1.1  reinoud 			bufap = 0;
    187  1.1  reinoud 			goto fill;
    188  1.1  reinoud 		}
    189  1.1  reinoud 		break;
    190  1.1  reinoud 	default:
    191  1.1  reinoud 		/* Shouldn't be here */
    192  1.1  reinoud #ifdef DMA_DEBUG
    193  1.1  reinoud 		printf("DMA ch %d bad status [%x]\n", dp->dc_channel, status);
    194  1.1  reinoud 		dma_dumpdc(dp);
    195  1.1  reinoud #endif
    196  1.1  reinoud 		break;
    197  1.1  reinoud 	}
    198  1.1  reinoud 
    199  1.1  reinoud /*	return(0);*/
    200  1.1  reinoud /* XXX */
    201  1.1  reinoud #define	PHYS(x, y)	pmap_extract(pmap_kernel(), (vaddr_t)x, (paddr_t *)(y))
    202  1.1  reinoud fill:
    203  1.1  reinoud #ifdef DMA_DEBUG
    204  1.1  reinoud 	printf("fill:\n");
    205  1.1  reinoud #endif
    206  1.1  reinoud 	if (dp->dc_len == 0) goto done;
    207  1.1  reinoud 	PHYS(dp->dc_nextaddr, &cur);
    208  1.1  reinoud 	len = NBPG - (cur & PGOFSET);
    209  1.1  reinoud 	if (len > dp->dc_len) {
    210  1.1  reinoud 		/* Last buffer */
    211  1.1  reinoud 		len = dp->dc_len;
    212  1.1  reinoud 	}
    213  1.1  reinoud 
    214  1.1  reinoud #ifdef DMA_DEBUG
    215  1.1  reinoud 	dma_dumpdc(dp);
    216  1.1  reinoud /*	ptsc_dump_mem(dp->dc_nextaddr, len);*/
    217  1.1  reinoud #endif
    218  1.1  reinoud /*
    219  1.1  reinoud  * Flush the cache for this address
    220  1.1  reinoud  */
    221  1.1  reinoud 	cpu_cache_purgeD_rng((vm_offset_t)dp->dc_nextaddr, len);
    222  1.1  reinoud 
    223  1.1  reinoud 	dp->dc_nextaddr += len;
    224  1.1  reinoud 	dp->dc_len -= len;
    225  1.1  reinoud 
    226  1.1  reinoud 	if(bufap) {
    227  1.1  reinoud 		*dp->dc_cura = (u_int)cur;
    228  1.1  reinoud 		*dp->dc_enda = ((u_int)cur + len - dp->dc_dmasize) |
    229  1.1  reinoud 				(dp->dc_len == 0 ? DMA_END_STOP : 0);
    230  1.1  reinoud 		if (dp->dc_len == 0) {
    231  1.1  reinoud 			/* Last buffer, fill other buffer with garbage */
    232  1.1  reinoud 			*dp->dc_endb = (u_int)cur;
    233  1.1  reinoud 		}
    234  1.1  reinoud 	}
    235  1.1  reinoud 	else {
    236  1.1  reinoud 		*dp->dc_curb = (u_int)cur;
    237  1.1  reinoud 		*dp->dc_endb = ((u_int)cur + len - dp->dc_dmasize) |
    238  1.1  reinoud 				(dp->dc_len == 0 ? DMA_END_STOP : 0);
    239  1.1  reinoud 		if (dp->dc_len == 0) {
    240  1.1  reinoud 			/* Last buffer, fill other buffer with garbage */
    241  1.1  reinoud 			*dp->dc_enda = (u_int)cur;
    242  1.1  reinoud 		}
    243  1.1  reinoud 	}
    244  1.1  reinoud #ifdef DMA_DEBUG
    245  1.1  reinoud 	dma_dumpdc(dp);
    246  1.1  reinoud /*	ptsc_dump_mem(dp->dc_nextaddr - len, len);*/
    247  1.1  reinoud 	printf("about to return\n");
    248  1.1  reinoud #endif
    249  1.1  reinoud 	return(1);
    250  1.1  reinoud done:
    251  1.1  reinoud #ifdef DMA_DEBUG
    252  1.1  reinoud 	printf("done:\n");
    253  1.1  reinoud #endif
    254  1.1  reinoud 	dp->dc_flags = 0;
    255  1.1  reinoud 	*dp->dc_cr = 0;
    256  1.1  reinoud 	disable_irq(IRQ_DMACH0 + dp->dc_channel);
    257  1.1  reinoud #ifdef DMA_DEBUG
    258  1.1  reinoud 	printf("about to return\n");
    259  1.1  reinoud #endif
    260  1.1  reinoud 	return(1);
    261  1.1  reinoud }
    262  1.1  reinoud 
    263  1.1  reinoud void
    264  1.1  reinoud dma_dumpdc(dc)
    265  1.1  reinoud 	struct dma_ctrl *dc;
    266  1.1  reinoud {
    267  1.1  reinoud 	printf("\ndc:\t%p\n"
    268  1.1  reinoud 		"dc_channel:\t%p=0x%08x\tdc_flags:\t%p=0x%08x\n"
    269  1.1  reinoud 		"dc_cura:\t%p=0x%08x\tdc_enda:\t%p=0x%08x\n"
    270  1.1  reinoud 		"dc_curb:\t%p=0x%08x\tdc_endb:\t%p=0x%08x\n"
    271  1.1  reinoud 		"dc_cr:\t%p=0x%02x\t\tdc_st:\t%p=0x%02x\n"
    272  1.1  reinoud 		"dc_nextaddr:\t%p=0x%08x\tdc_len:\t%p=0x%08x\n",
    273  1.1  reinoud 		dc,
    274  1.1  reinoud 		&dc->dc_channel, (int)dc->dc_channel,
    275  1.1  reinoud 		&dc->dc_flags, (int)dc->dc_flags,
    276  1.1  reinoud 		dc->dc_cura, (int)*dc->dc_cura,
    277  1.1  reinoud 		dc->dc_enda, (int)*dc->dc_enda,
    278  1.1  reinoud 		dc->dc_curb, (int)*dc->dc_curb,
    279  1.1  reinoud 		dc->dc_endb, (int)*dc->dc_endb,
    280  1.1  reinoud 		dc->dc_cr, (int)*dc->dc_cr,
    281  1.1  reinoud 		dc->dc_st, (int)(*dc->dc_st) & DMA_ST_MASK,
    282  1.1  reinoud 		&dc->dc_nextaddr, (int)dc->dc_nextaddr,
    283  1.1  reinoud 		&dc->dc_len, dc->dc_len);
    284  1.1  reinoud }
    285  1.1  reinoud 
    286  1.1  reinoud struct dma_ctrl *
    287  1.1  reinoud dma_init(ch, extp, dmasize, ipl)
    288  1.1  reinoud 	int ch;
    289  1.1  reinoud 	int extp;
    290  1.1  reinoud 	int dmasize;
    291  1.1  reinoud 	int ipl;
    292  1.1  reinoud {
    293  1.1  reinoud 	struct dma_ctrl *dp = &ctrl[ch];
    294  1.1  reinoud 	int offset = ch * 0x20;
    295  1.1  reinoud 	volatile u_char *dmaext = (volatile u_char *)(IOMD_ADDRESS(IOMD_DMAEXT));
    296  1.1  reinoud 
    297  1.1  reinoud 	printf("Initialising DMA channel %d\n", ch);
    298  1.1  reinoud 
    299  1.1  reinoud 	dp->dc_channel = ch;
    300  1.1  reinoud 	dp->dc_flags = 0;
    301  1.1  reinoud 	dp->dc_dmasize = dmasize;
    302  1.1  reinoud 	dp->dc_cura = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0CURA) + offset);
    303  1.1  reinoud 	dp->dc_enda = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0ENDA) + offset);
    304  1.1  reinoud 	dp->dc_curb = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0CURB) + offset);
    305  1.1  reinoud 	dp->dc_endb = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0ENDB) + offset);
    306  1.1  reinoud 	dp->dc_cr = (volatile u_char *)(IOMD_ADDRESS(IOMD_IO0CR) + offset);
    307  1.1  reinoud 	dp->dc_st = (volatile u_char *)(IOMD_ADDRESS(IOMD_IO0ST) + offset);
    308  1.1  reinoud 
    309  1.1  reinoud 	if (extp)
    310  1.1  reinoud 		*dmaext |= (1 << ch);
    311  1.1  reinoud 
    312  1.1  reinoud 	printf("about to claim interrupt\n");
    313  1.1  reinoud 
    314  1.1  reinoud 	dp->dc_ih.ih_func = dma_intr;
    315  1.1  reinoud 	dp->dc_ih.ih_arg = dp;
    316  1.1  reinoud 	dp->dc_ih.ih_level = ipl;
    317  1.1  reinoud 	dp->dc_ih.ih_name = "dma";
    318  1.1  reinoud 	dp->dc_ih.ih_maskaddr = (u_int) IOMD_ADDRESS(IOMD_DMARQ);
    319  1.1  reinoud 	dp->dc_ih.ih_maskbits = (1 << ch);
    320  1.1  reinoud 
    321  1.1  reinoud 	if (irq_claim(IRQ_DMACH0 + ch, &dp->dc_ih))
    322  1.1  reinoud 		panic("Cannot install DMA IRQ handler\n");
    323  1.1  reinoud 
    324  1.1  reinoud 	return(dp);
    325  1.1  reinoud }
    326  1.1  reinoud 
    327