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