Home | History | Annotate | Line # | Download | only in apbus
dmac3.c revision 1.6.10.1
      1  1.6.10.1     yamt /*	$NetBSD: dmac3.c,v 1.6.10.1 2005/02/12 18:17:37 yamt 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.10.1 2005/02/12 18:17:37 yamt 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.6.10.1     yamt int dmac3_match(struct device *, struct cfdata *, void *);
     71  1.6.10.1     yamt void dmac3_attach(struct device *, struct device *, void *);
     72       1.1   tsubai 
     73  1.6.10.1     yamt extern paddr_t kvtophys(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.6.10.1     yamt dmac3_match(struct device *parent, struct cfdata *cf, void *aux)
     80       1.1   tsubai {
     81       1.1   tsubai 	struct apbus_attach_args *apa = aux;
     82       1.1   tsubai 
     83       1.1   tsubai 	if (strcmp(apa->apa_name, "dmac3") == 0)
     84       1.1   tsubai 		return 1;
     85       1.1   tsubai 
     86       1.1   tsubai 	return 0;
     87       1.1   tsubai }
     88       1.1   tsubai 
     89       1.1   tsubai void
     90  1.6.10.1     yamt dmac3_attach(struct device *parent, struct device *self, void *aux)
     91       1.1   tsubai {
     92       1.1   tsubai 	struct dmac3_softc *sc = (void *)self;
     93       1.1   tsubai 	struct apbus_attach_args *apa = aux;
     94       1.1   tsubai 	struct dmac3reg *reg;
     95       1.1   tsubai 
     96       1.1   tsubai 	static paddr_t dmamap = DMAC3_PAGEMAP;
     97       1.1   tsubai 	static vaddr_t dmaaddr = 0;
     98       1.1   tsubai 
     99       1.1   tsubai 	reg = (void *)apa->apa_hwbase;
    100       1.1   tsubai 	sc->sc_reg = reg;
    101       1.1   tsubai 	sc->sc_ctlnum = apa->apa_ctlnum;
    102       1.1   tsubai 	sc->sc_dmamap = (int *)dmamap;
    103       1.1   tsubai 	sc->sc_dmaaddr = dmaaddr;
    104       1.1   tsubai 	dmamap += 0x1000;
    105       1.1   tsubai 	dmaaddr += 0x200000;
    106       1.1   tsubai 
    107       1.1   tsubai 	sc->sc_conf = DMAC3_CONF_PCEN | DMAC3_CONF_DCEN | DMAC3_CONF_FASTACCESS;
    108       1.1   tsubai 
    109       1.1   tsubai 	dmac3_reset(sc);
    110       1.1   tsubai 
    111       1.1   tsubai 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    112       1.1   tsubai 	printf(": ctlnum = %d, map = %p, va = %lx",
    113       1.1   tsubai 	       apa->apa_ctlnum, sc->sc_dmamap, sc->sc_dmaaddr);
    114       1.1   tsubai 	printf("\n");
    115       1.1   tsubai }
    116       1.1   tsubai 
    117       1.1   tsubai void *
    118  1.6.10.1     yamt dmac3_link(int ctlnum)
    119       1.1   tsubai {
    120       1.1   tsubai 	struct dmac3_softc *sc;
    121       1.1   tsubai 	struct device *dv;
    122       1.1   tsubai 
    123       1.1   tsubai 	for (dv = alldevs.tqh_first; dv; dv = dv->dv_list.tqe_next) {
    124       1.1   tsubai 		if (strncmp(dv->dv_xname, "dmac", 4) == 0) {
    125       1.1   tsubai 			sc = (void *)dv;
    126       1.1   tsubai 			if (sc->sc_ctlnum == ctlnum)
    127       1.1   tsubai 				return sc;
    128       1.1   tsubai 		}
    129       1.1   tsubai 	}
    130       1.1   tsubai 	return NULL;
    131       1.1   tsubai }
    132       1.1   tsubai 
    133       1.1   tsubai void
    134  1.6.10.1     yamt dmac3_reset(struct dmac3_softc *sc)
    135       1.1   tsubai {
    136       1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    137       1.1   tsubai 
    138       1.1   tsubai 	reg->csr = DMAC3_CSR_RESET;
    139       1.1   tsubai 	reg->csr = 0;
    140       1.1   tsubai 	reg->intr = DMAC3_INTR_EOPIE | DMAC3_INTR_INTEN;
    141       1.1   tsubai 	reg->conf = sc->sc_conf;
    142       1.1   tsubai }
    143       1.1   tsubai 
    144       1.1   tsubai void
    145  1.6.10.1     yamt dmac3_start(struct dmac3_softc *sc, vaddr_t addr, int len, int direction)
    146       1.1   tsubai {
    147       1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    148       1.1   tsubai 	paddr_t pa;
    149       1.1   tsubai 	vaddr_t start, end, v;
    150       1.1   tsubai 	u_int *p;
    151       1.1   tsubai 
    152       1.1   tsubai 	if (reg->csr & DMAC3_CSR_ENABLE)
    153       1.1   tsubai 		dmac3_reset(sc);
    154       1.1   tsubai 
    155       1.1   tsubai 	start = mips_trunc_page(addr);
    156       1.1   tsubai 	end   = mips_round_page(addr + len);
    157       1.1   tsubai 	p = sc->sc_dmamap;
    158       1.5  thorpej 	for (v = start; v < end; v += PAGE_SIZE) {
    159       1.1   tsubai 		pa = kvtophys(v);
    160       1.5  thorpej 		mips_dcache_wbinv_range(MIPS_PHYS_TO_KSEG0(pa), PAGE_SIZE);
    161       1.1   tsubai 		*p++ = 0;
    162       1.1   tsubai 		*p++ = (pa >> PGSHIFT) | 0xc0000000;
    163       1.1   tsubai 	}
    164       1.1   tsubai 	*p++ = 0;
    165       1.1   tsubai 	*p++ = 0x003fffff;
    166       1.1   tsubai 
    167       1.1   tsubai 	addr &= PGOFSET;
    168       1.1   tsubai 	addr += sc->sc_dmaaddr;
    169       1.1   tsubai 
    170       1.1   tsubai 	reg->len = len;
    171       1.1   tsubai 	reg->addr = addr;
    172       1.1   tsubai 	reg->intr = DMAC3_INTR_EOPIE | DMAC3_INTR_INTEN;
    173       1.1   tsubai 	reg->csr = DMAC3_CSR_ENABLE | direction | BURST_MODE | APAD_MODE;
    174       1.1   tsubai }
    175       1.1   tsubai 
    176       1.1   tsubai int
    177  1.6.10.1     yamt dmac3_intr(void *v)
    178       1.1   tsubai {
    179       1.1   tsubai 	struct dmac3_softc *sc = v;
    180       1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    181       1.1   tsubai 	int intr, conf, rv = 1;
    182       1.1   tsubai 
    183       1.1   tsubai 	intr = reg->intr;
    184       1.1   tsubai 	if ((intr & DMAC3_INTR_INT) == 0)
    185       1.1   tsubai 		return 0;
    186       1.1   tsubai 
    187       1.1   tsubai 	/* clear interrupt */
    188       1.1   tsubai 	conf = reg->conf;
    189       1.1   tsubai 	reg->conf = conf;
    190       1.1   tsubai 	reg->intr = intr;
    191       1.1   tsubai 
    192       1.1   tsubai 	if (intr & DMAC3_INTR_PERR) {
    193       1.1   tsubai 		printf("%s: intr = 0x%x\n", sc->sc_dev.dv_xname, intr);
    194       1.1   tsubai 		rv = -1;
    195       1.1   tsubai 	}
    196       1.1   tsubai 
    197       1.1   tsubai 	if (conf & (DMAC3_CONF_IPER | DMAC3_CONF_MPER | DMAC3_CONF_DERR)) {
    198       1.1   tsubai 		printf("%s: conf = 0x%x\n", sc->sc_dev.dv_xname, conf);
    199       1.1   tsubai 		if (conf & DMAC3_CONF_DERR) {
    200       1.1   tsubai 			printf("DMA address = 0x%x\n", reg->addr);
    201       1.1   tsubai 			printf("resetting DMA...\n");
    202       1.1   tsubai 			dmac3_reset(sc);
    203       1.1   tsubai 		}
    204       1.1   tsubai 	}
    205       1.1   tsubai 
    206       1.1   tsubai 	return rv;
    207       1.1   tsubai }
    208       1.1   tsubai 
    209       1.1   tsubai void
    210  1.6.10.1     yamt dmac3_misc(struct dmac3_softc *sc, int cmd)
    211       1.1   tsubai {
    212       1.1   tsubai 	struct dmac3reg *reg = sc->sc_reg;
    213       1.1   tsubai 	int conf;
    214       1.1   tsubai 
    215       1.1   tsubai 	conf = DMAC3_CONF_PCEN | DMAC3_CONF_DCEN | cmd;
    216       1.1   tsubai 	sc->sc_conf = conf;
    217       1.1   tsubai 	reg->conf = conf;
    218       1.1   tsubai }
    219