Home | History | Annotate | Line # | Download | only in apbus
dmac3.c revision 1.6
      1  1.6    lukem /*	$NetBSD: dmac3.c,v 1.6 2003/07/15 02:59:28 lukem Exp $	*/
      2  1.1   tsubai 
      3  1.1   tsubai /*-
      4  1.1   tsubai  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  1.1   tsubai  *
      6  1.1   tsubai  * Redistribution and use in source and binary forms, with or without
      7  1.1   tsubai  * modification, are permitted provided that the following conditions
      8  1.1   tsubai  * are met:
      9  1.1   tsubai  * 1. Redistributions of source code must retain the above copyright
     10  1.1   tsubai  *    notice, this list of conditions and the following disclaimer.
     11  1.1   tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   tsubai  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   tsubai  *    documentation and/or other materials provided with the distribution.
     14  1.1   tsubai  * 3. The name of the author may not be used to endorse or promote products
     15  1.1   tsubai  *    derived from this software without specific prior written permission.
     16  1.1   tsubai  *
     17  1.1   tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1   tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1   tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1   tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1   tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1   tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1   tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1   tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1   tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1   tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1   tsubai  */
     28  1.6    lukem 
     29  1.6    lukem #include <sys/cdefs.h>
     30  1.6    lukem __KERNEL_RCSID(0, "$NetBSD: dmac3.c,v 1.6 2003/07/15 02:59:28 lukem Exp $");
     31  1.1   tsubai 
     32  1.1   tsubai #include <sys/param.h>
     33  1.1   tsubai #include <sys/device.h>
     34  1.1   tsubai #include <sys/kernel.h>
     35  1.1   tsubai #include <sys/systm.h>
     36  1.1   tsubai 
     37  1.1   tsubai #include <uvm/uvm_extern.h>
     38  1.1   tsubai 
     39  1.1   tsubai #include <machine/locore.h>
     40  1.1   tsubai 
     41  1.1   tsubai #include <newsmips/apbus/apbusvar.h>
     42  1.1   tsubai #include <newsmips/apbus/dmac3reg.h>
     43  1.1   tsubai 
     44  1.2  thorpej #include <mips/cache.h>
     45  1.2  thorpej 
     46  1.1   tsubai #define DMA_BURST
     47  1.1   tsubai #define DMA_APAD_OFF
     48  1.1   tsubai 
     49  1.1   tsubai #ifdef DMA_APAD_OFF
     50  1.1   tsubai # define APAD_MODE	0
     51  1.1   tsubai #else
     52  1.1   tsubai # define APAD_MODE	DMAC3_CSR_APAD
     53  1.1   tsubai #endif
     54  1.1   tsubai 
     55  1.1   tsubai #ifdef DMA_BURST
     56  1.1   tsubai # define BURST_MODE	(DMAC3_CSR_DBURST | DMAC3_CSR_MBURST)
     57  1.1   tsubai #else
     58  1.1   tsubai # define BURST_MODE	0
     59  1.1   tsubai #endif
     60  1.1   tsubai 
     61  1.1   tsubai struct dmac3_softc {
     62  1.1   tsubai 	struct device sc_dev;
     63  1.1   tsubai 	struct dmac3reg *sc_reg;
     64  1.1   tsubai 	vaddr_t sc_dmaaddr;
     65  1.1   tsubai 	int *sc_dmamap;
     66  1.1   tsubai 	int sc_conf;
     67  1.1   tsubai 	int sc_ctlnum;
     68  1.1   tsubai };
     69  1.1   tsubai 
     70  1.1   tsubai int dmac3_match __P((struct device *, struct cfdata *, void *));
     71  1.1   tsubai void dmac3_attach __P((struct device *, struct device *, void *));
     72  1.1   tsubai 
     73  1.1   tsubai paddr_t kvtophys __P((vaddr_t));
     74  1.1   tsubai 
     75  1.4  thorpej CFATTACH_DECL(dmac, sizeof(struct dmac3_softc),
     76  1.4  thorpej     dmac3_match, dmac3_attach, NULL, NULL);
     77  1.1   tsubai 
     78  1.1   tsubai int
     79  1.1   tsubai dmac3_match(parent, cf, aux)
     80  1.1   tsubai 	struct device *parent;
     81  1.1   tsubai 	struct cfdata *cf;
     82  1.1   tsubai 	void *aux;
     83  1.1   tsubai {
     84  1.1   tsubai 	struct apbus_attach_args *apa = aux;
     85  1.1   tsubai 
     86  1.1   tsubai 	if (strcmp(apa->apa_name, "dmac3") == 0)
     87  1.1   tsubai 		return 1;
     88  1.1   tsubai 
     89  1.1   tsubai 	return 0;
     90  1.1   tsubai }
     91  1.1   tsubai 
     92  1.1   tsubai void
     93  1.1   tsubai dmac3_attach(parent, self, aux)
     94  1.1   tsubai 	struct device *parent, *self;
     95  1.1   tsubai 	void *aux;
     96  1.1   tsubai {
     97  1.1   tsubai 	struct dmac3_softc *sc = (void *)self;
     98  1.1   tsubai 	struct apbus_attach_args *apa = aux;
     99  1.1   tsubai 	struct dmac3reg *reg;
    100  1.1   tsubai 
    101  1.1   tsubai 	static paddr_t dmamap = DMAC3_PAGEMAP;
    102  1.1   tsubai 	static vaddr_t dmaaddr = 0;
    103  1.1   tsubai 
    104  1.1   tsubai 	reg = (void *)apa->apa_hwbase;
    105  1.1   tsubai 	sc->sc_reg = reg;
    106  1.1   tsubai 	sc->sc_ctlnum = apa->apa_ctlnum;
    107  1.1   tsubai 	sc->sc_dmamap = (int *)dmamap;
    108  1.1   tsubai 	sc->sc_dmaaddr = dmaaddr;
    109  1.1   tsubai 	dmamap += 0x1000;
    110  1.1   tsubai 	dmaaddr += 0x200000;
    111  1.1   tsubai 
    112  1.1   tsubai 	sc->sc_conf = DMAC3_CONF_PCEN | DMAC3_CONF_DCEN | DMAC3_CONF_FASTACCESS;
    113  1.1   tsubai 
    114  1.1   tsubai 	dmac3_reset(sc);
    115  1.1   tsubai 
    116  1.1   tsubai 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    117  1.1   tsubai 	printf(": ctlnum = %d, map = %p, va = %lx",
    118  1.1   tsubai 	       apa->apa_ctlnum, sc->sc_dmamap, sc->sc_dmaaddr);
    119  1.1   tsubai 	printf("\n");
    120  1.1   tsubai }
    121  1.1   tsubai 
    122  1.1   tsubai void *
    123  1.1   tsubai dmac3_link(ctlnum)
    124  1.1   tsubai 	int ctlnum;
    125  1.1   tsubai {
    126  1.1   tsubai 	struct dmac3_softc *sc;
    127  1.1   tsubai 	struct device *dv;
    128  1.1   tsubai 
    129  1.1   tsubai 	for (dv = alldevs.tqh_first; dv; dv = dv->dv_list.tqe_next) {
    130  1.1   tsubai 		if (strncmp(dv->dv_xname, "dmac", 4) == 0) {
    131  1.1   tsubai 			sc = (void *)dv;
    132  1.1   tsubai 			if (sc->sc_ctlnum == ctlnum)
    133  1.1   tsubai 				return sc;
    134  1.1   tsubai 		}
    135  1.1   tsubai 	}
    136  1.1   tsubai 	return NULL;
    137  1.1   tsubai }
    138  1.1   tsubai 
    139  1.1   tsubai void
    140  1.1   tsubai dmac3_reset(sc)
    141  1.1   tsubai 	struct dmac3_softc *sc;
    142  1.1   tsubai {
    143  1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    144  1.1   tsubai 
    145  1.1   tsubai 	reg->csr = DMAC3_CSR_RESET;
    146  1.1   tsubai 	reg->csr = 0;
    147  1.1   tsubai 	reg->intr = DMAC3_INTR_EOPIE | DMAC3_INTR_INTEN;
    148  1.1   tsubai 	reg->conf = sc->sc_conf;
    149  1.1   tsubai }
    150  1.1   tsubai 
    151  1.1   tsubai void
    152  1.1   tsubai dmac3_start(sc, addr, len, direction)
    153  1.1   tsubai 	struct dmac3_softc *sc;
    154  1.1   tsubai 	vaddr_t addr;
    155  1.1   tsubai 	int len, direction;
    156  1.1   tsubai {
    157  1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    158  1.1   tsubai 	paddr_t pa;
    159  1.1   tsubai 	vaddr_t start, end, v;
    160  1.1   tsubai 	u_int *p;
    161  1.1   tsubai 
    162  1.1   tsubai 	if (reg->csr & DMAC3_CSR_ENABLE)
    163  1.1   tsubai 		dmac3_reset(sc);
    164  1.1   tsubai 
    165  1.1   tsubai 	start = mips_trunc_page(addr);
    166  1.1   tsubai 	end   = mips_round_page(addr + len);
    167  1.1   tsubai 	p = sc->sc_dmamap;
    168  1.5  thorpej 	for (v = start; v < end; v += PAGE_SIZE) {
    169  1.1   tsubai 		pa = kvtophys(v);
    170  1.5  thorpej 		mips_dcache_wbinv_range(MIPS_PHYS_TO_KSEG0(pa), PAGE_SIZE);
    171  1.1   tsubai 		*p++ = 0;
    172  1.1   tsubai 		*p++ = (pa >> PGSHIFT) | 0xc0000000;
    173  1.1   tsubai 	}
    174  1.1   tsubai 	*p++ = 0;
    175  1.1   tsubai 	*p++ = 0x003fffff;
    176  1.1   tsubai 
    177  1.1   tsubai 	addr &= PGOFSET;
    178  1.1   tsubai 	addr += sc->sc_dmaaddr;
    179  1.1   tsubai 
    180  1.1   tsubai 	reg->len = len;
    181  1.1   tsubai 	reg->addr = addr;
    182  1.1   tsubai 	reg->intr = DMAC3_INTR_EOPIE | DMAC3_INTR_INTEN;
    183  1.1   tsubai 	reg->csr = DMAC3_CSR_ENABLE | direction | BURST_MODE | APAD_MODE;
    184  1.1   tsubai }
    185  1.1   tsubai 
    186  1.1   tsubai int
    187  1.1   tsubai dmac3_intr(v)
    188  1.1   tsubai 	void *v;
    189  1.1   tsubai {
    190  1.1   tsubai 	struct dmac3_softc *sc = v;
    191  1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    192  1.1   tsubai 	int intr, conf, rv = 1;
    193  1.1   tsubai 
    194  1.1   tsubai 	intr = reg->intr;
    195  1.1   tsubai 	if ((intr & DMAC3_INTR_INT) == 0)
    196  1.1   tsubai 		return 0;
    197  1.1   tsubai 
    198  1.1   tsubai 	/* clear interrupt */
    199  1.1   tsubai 	conf = reg->conf;
    200  1.1   tsubai 	reg->conf = conf;
    201  1.1   tsubai 	reg->intr = intr;
    202  1.1   tsubai 
    203  1.1   tsubai 	if (intr & DMAC3_INTR_PERR) {
    204  1.1   tsubai 		printf("%s: intr = 0x%x\n", sc->sc_dev.dv_xname, intr);
    205  1.1   tsubai 		rv = -1;
    206  1.1   tsubai 	}
    207  1.1   tsubai 
    208  1.1   tsubai 	if (conf & (DMAC3_CONF_IPER | DMAC3_CONF_MPER | DMAC3_CONF_DERR)) {
    209  1.1   tsubai 		printf("%s: conf = 0x%x\n", sc->sc_dev.dv_xname, conf);
    210  1.1   tsubai 		if (conf & DMAC3_CONF_DERR) {
    211  1.1   tsubai 			printf("DMA address = 0x%x\n", reg->addr);
    212  1.1   tsubai 			printf("resetting DMA...\n");
    213  1.1   tsubai 			dmac3_reset(sc);
    214  1.1   tsubai 		}
    215  1.1   tsubai 	}
    216  1.1   tsubai 
    217  1.1   tsubai 	return rv;
    218  1.1   tsubai }
    219  1.1   tsubai 
    220  1.1   tsubai void
    221  1.1   tsubai dmac3_misc(sc, cmd)
    222  1.1   tsubai 	struct dmac3_softc *sc;
    223  1.1   tsubai 	int cmd;
    224  1.1   tsubai {
    225  1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    226  1.1   tsubai 	int conf;
    227  1.1   tsubai 
    228  1.1   tsubai 	conf = DMAC3_CONF_PCEN | DMAC3_CONF_DCEN | cmd;
    229  1.1   tsubai 	sc->sc_conf = conf;
    230  1.1   tsubai 	reg->conf = conf;
    231  1.1   tsubai }
    232