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