Home | History | Annotate | Line # | Download | only in apbus
dmac3.c revision 1.1
      1  1.1  tsubai /*	$NetBSD: dmac3.c,v 1.1 2000/10/30 10:07:35 tsubai 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.1  tsubai 
     29  1.1  tsubai #include <sys/param.h>
     30  1.1  tsubai #include <sys/device.h>
     31  1.1  tsubai #include <sys/kernel.h>
     32  1.1  tsubai #include <sys/systm.h>
     33  1.1  tsubai 
     34  1.1  tsubai #include <uvm/uvm_extern.h>
     35  1.1  tsubai 
     36  1.1  tsubai #include <machine/locore.h>
     37  1.1  tsubai 
     38  1.1  tsubai #include <newsmips/apbus/apbusvar.h>
     39  1.1  tsubai #include <newsmips/apbus/dmac3reg.h>
     40  1.1  tsubai 
     41  1.1  tsubai #define DMA_BURST
     42  1.1  tsubai #define DMA_APAD_OFF
     43  1.1  tsubai 
     44  1.1  tsubai #ifdef DMA_APAD_OFF
     45  1.1  tsubai # define APAD_MODE	0
     46  1.1  tsubai #else
     47  1.1  tsubai # define APAD_MODE	DMAC3_CSR_APAD
     48  1.1  tsubai #endif
     49  1.1  tsubai 
     50  1.1  tsubai #ifdef DMA_BURST
     51  1.1  tsubai # define BURST_MODE	(DMAC3_CSR_DBURST | DMAC3_CSR_MBURST)
     52  1.1  tsubai #else
     53  1.1  tsubai # define BURST_MODE	0
     54  1.1  tsubai #endif
     55  1.1  tsubai 
     56  1.1  tsubai struct dmac3_softc {
     57  1.1  tsubai 	struct device sc_dev;
     58  1.1  tsubai 	struct dmac3reg *sc_reg;
     59  1.1  tsubai 	vaddr_t sc_dmaaddr;
     60  1.1  tsubai 	int *sc_dmamap;
     61  1.1  tsubai 	int sc_conf;
     62  1.1  tsubai 	int sc_ctlnum;
     63  1.1  tsubai };
     64  1.1  tsubai 
     65  1.1  tsubai int dmac3_match __P((struct device *, struct cfdata *, void *));
     66  1.1  tsubai void dmac3_attach __P((struct device *, struct device *, void *));
     67  1.1  tsubai 
     68  1.1  tsubai paddr_t kvtophys __P((vaddr_t));
     69  1.1  tsubai 
     70  1.1  tsubai struct cfattach dmac_ca = {
     71  1.1  tsubai 	sizeof(struct dmac3_softc), dmac3_match, dmac3_attach
     72  1.1  tsubai };
     73  1.1  tsubai 
     74  1.1  tsubai int
     75  1.1  tsubai dmac3_match(parent, cf, aux)
     76  1.1  tsubai 	struct device *parent;
     77  1.1  tsubai 	struct cfdata *cf;
     78  1.1  tsubai 	void *aux;
     79  1.1  tsubai {
     80  1.1  tsubai 	struct apbus_attach_args *apa = aux;
     81  1.1  tsubai 
     82  1.1  tsubai 	if (strcmp(apa->apa_name, "dmac3") == 0)
     83  1.1  tsubai 		return 1;
     84  1.1  tsubai 
     85  1.1  tsubai 	return 0;
     86  1.1  tsubai }
     87  1.1  tsubai 
     88  1.1  tsubai void
     89  1.1  tsubai dmac3_attach(parent, self, aux)
     90  1.1  tsubai 	struct device *parent, *self;
     91  1.1  tsubai 	void *aux;
     92  1.1  tsubai {
     93  1.1  tsubai 	struct dmac3_softc *sc = (void *)self;
     94  1.1  tsubai 	struct apbus_attach_args *apa = aux;
     95  1.1  tsubai 	struct dmac3reg *reg;
     96  1.1  tsubai 
     97  1.1  tsubai 	static paddr_t dmamap = DMAC3_PAGEMAP;
     98  1.1  tsubai 	static vaddr_t dmaaddr = 0;
     99  1.1  tsubai 
    100  1.1  tsubai 	reg = (void *)apa->apa_hwbase;
    101  1.1  tsubai 	sc->sc_reg = reg;
    102  1.1  tsubai 	sc->sc_ctlnum = apa->apa_ctlnum;
    103  1.1  tsubai 	sc->sc_dmamap = (int *)dmamap;
    104  1.1  tsubai 	sc->sc_dmaaddr = dmaaddr;
    105  1.1  tsubai 	dmamap += 0x1000;
    106  1.1  tsubai 	dmaaddr += 0x200000;
    107  1.1  tsubai 
    108  1.1  tsubai 	sc->sc_conf = DMAC3_CONF_PCEN | DMAC3_CONF_DCEN | DMAC3_CONF_FASTACCESS;
    109  1.1  tsubai 
    110  1.1  tsubai 	dmac3_reset(sc);
    111  1.1  tsubai 
    112  1.1  tsubai 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    113  1.1  tsubai 	printf(": ctlnum = %d, map = %p, va = %lx",
    114  1.1  tsubai 	       apa->apa_ctlnum, sc->sc_dmamap, sc->sc_dmaaddr);
    115  1.1  tsubai 	printf("\n");
    116  1.1  tsubai }
    117  1.1  tsubai 
    118  1.1  tsubai void *
    119  1.1  tsubai dmac3_link(ctlnum)
    120  1.1  tsubai 	int ctlnum;
    121  1.1  tsubai {
    122  1.1  tsubai 	struct dmac3_softc *sc;
    123  1.1  tsubai 	struct device *dv;
    124  1.1  tsubai 
    125  1.1  tsubai 	for (dv = alldevs.tqh_first; dv; dv = dv->dv_list.tqe_next) {
    126  1.1  tsubai 		if (strncmp(dv->dv_xname, "dmac", 4) == 0) {
    127  1.1  tsubai 			sc = (void *)dv;
    128  1.1  tsubai 			if (sc->sc_ctlnum == ctlnum)
    129  1.1  tsubai 				return sc;
    130  1.1  tsubai 		}
    131  1.1  tsubai 	}
    132  1.1  tsubai 	return NULL;
    133  1.1  tsubai }
    134  1.1  tsubai 
    135  1.1  tsubai void
    136  1.1  tsubai dmac3_reset(sc)
    137  1.1  tsubai 	struct dmac3_softc *sc;
    138  1.1  tsubai {
    139  1.1  tsubai 	struct dmac3reg *reg = sc->sc_reg;
    140  1.1  tsubai 
    141  1.1  tsubai 	reg->csr = DMAC3_CSR_RESET;
    142  1.1  tsubai 	reg->csr = 0;
    143  1.1  tsubai 	reg->intr = DMAC3_INTR_EOPIE | DMAC3_INTR_INTEN;
    144  1.1  tsubai 	reg->conf = sc->sc_conf;
    145  1.1  tsubai }
    146  1.1  tsubai 
    147  1.1  tsubai void
    148  1.1  tsubai dmac3_start(sc, addr, len, direction)
    149  1.1  tsubai 	struct dmac3_softc *sc;
    150  1.1  tsubai 	vaddr_t addr;
    151  1.1  tsubai 	int len, direction;
    152  1.1  tsubai {
    153  1.1  tsubai 	struct dmac3reg *reg = sc->sc_reg;
    154  1.1  tsubai 	paddr_t pa;
    155  1.1  tsubai 	vaddr_t start, end, v;
    156  1.1  tsubai 	u_int *p;
    157  1.1  tsubai 
    158  1.1  tsubai 	if (reg->csr & DMAC3_CSR_ENABLE)
    159  1.1  tsubai 		dmac3_reset(sc);
    160  1.1  tsubai 
    161  1.1  tsubai 	start = mips_trunc_page(addr);
    162  1.1  tsubai 	end   = mips_round_page(addr + len);
    163  1.1  tsubai 	p = sc->sc_dmamap;
    164  1.1  tsubai 	for (v = start; v < end; v += NBPG) {
    165  1.1  tsubai 		pa = kvtophys(v);
    166  1.1  tsubai 		MachFlushDCache(MIPS_PHYS_TO_KSEG0(pa), NBPG);
    167  1.1  tsubai 		*p++ = 0;
    168  1.1  tsubai 		*p++ = (pa >> PGSHIFT) | 0xc0000000;
    169  1.1  tsubai 	}
    170  1.1  tsubai 	*p++ = 0;
    171  1.1  tsubai 	*p++ = 0x003fffff;
    172  1.1  tsubai 
    173  1.1  tsubai 	addr &= PGOFSET;
    174  1.1  tsubai 	addr += sc->sc_dmaaddr;
    175  1.1  tsubai 
    176  1.1  tsubai 	reg->len = len;
    177  1.1  tsubai 	reg->addr = addr;
    178  1.1  tsubai 	reg->intr = DMAC3_INTR_EOPIE | DMAC3_INTR_INTEN;
    179  1.1  tsubai 	reg->csr = DMAC3_CSR_ENABLE | direction | BURST_MODE | APAD_MODE;
    180  1.1  tsubai }
    181  1.1  tsubai 
    182  1.1  tsubai int
    183  1.1  tsubai dmac3_intr(v)
    184  1.1  tsubai 	void *v;
    185  1.1  tsubai {
    186  1.1  tsubai 	struct dmac3_softc *sc = v;
    187  1.1  tsubai 	struct dmac3reg *reg = sc->sc_reg;
    188  1.1  tsubai 	int intr, conf, rv = 1;
    189  1.1  tsubai 
    190  1.1  tsubai 	intr = reg->intr;
    191  1.1  tsubai 	if ((intr & DMAC3_INTR_INT) == 0)
    192  1.1  tsubai 		return 0;
    193  1.1  tsubai 
    194  1.1  tsubai 	/* clear interrupt */
    195  1.1  tsubai 	conf = reg->conf;
    196  1.1  tsubai 	reg->conf = conf;
    197  1.1  tsubai 	reg->intr = intr;
    198  1.1  tsubai 
    199  1.1  tsubai 	if (intr & DMAC3_INTR_PERR) {
    200  1.1  tsubai 		printf("%s: intr = 0x%x\n", sc->sc_dev.dv_xname, intr);
    201  1.1  tsubai 		rv = -1;
    202  1.1  tsubai 	}
    203  1.1  tsubai 
    204  1.1  tsubai 	if (conf & (DMAC3_CONF_IPER | DMAC3_CONF_MPER | DMAC3_CONF_DERR)) {
    205  1.1  tsubai 		printf("%s: conf = 0x%x\n", sc->sc_dev.dv_xname, conf);
    206  1.1  tsubai 		if (conf & DMAC3_CONF_DERR) {
    207  1.1  tsubai 			printf("DMA address = 0x%x\n", reg->addr);
    208  1.1  tsubai 			printf("resetting DMA...\n");
    209  1.1  tsubai 			dmac3_reset(sc);
    210  1.1  tsubai 		}
    211  1.1  tsubai 	}
    212  1.1  tsubai 
    213  1.1  tsubai 	return rv;
    214  1.1  tsubai }
    215  1.1  tsubai 
    216  1.1  tsubai void
    217  1.1  tsubai dmac3_misc(sc, cmd)
    218  1.1  tsubai 	struct dmac3_softc *sc;
    219  1.1  tsubai 	int cmd;
    220  1.1  tsubai {
    221  1.1  tsubai 	struct dmac3reg *reg = sc->sc_reg;
    222  1.1  tsubai 	int conf;
    223  1.1  tsubai 
    224  1.1  tsubai 	conf = DMAC3_CONF_PCEN | DMAC3_CONF_DCEN | cmd;
    225  1.1  tsubai 	sc->sc_conf = conf;
    226  1.1  tsubai 	reg->conf = conf;
    227  1.1  tsubai }
    228