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