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