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