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