bcm2835_dmac.c revision 1.14 1 1.13 mlelstv /* $NetBSD: bcm2835_dmac.c,v 1.14 2015/08/09 13:07:47 mlelstv Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "opt_ddb.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.13 mlelstv __KERNEL_RCSID(0, "$NetBSD: bcm2835_dmac.c,v 1.14 2015/08/09 13:07:47 mlelstv Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/kmem.h>
40 1.1 jmcneill #include <sys/mutex.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <arm/broadcom/bcm_amba.h>
43 1.1 jmcneill #include <arm/broadcom/bcm2835reg.h>
44 1.1 jmcneill #include <arm/broadcom/bcm2835_intr.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <arm/broadcom/bcm2835_dmac.h>
47 1.1 jmcneill
48 1.5 jmcneill #define BCM_DMAC_CHANNELMASK 0x00000fff
49 1.1 jmcneill
50 1.1 jmcneill struct bcm_dmac_softc;
51 1.1 jmcneill
52 1.1 jmcneill struct bcm_dmac_channel {
53 1.1 jmcneill struct bcm_dmac_softc *ch_sc;
54 1.1 jmcneill void *ch_ih;
55 1.1 jmcneill uint8_t ch_index;
56 1.13 mlelstv void (*ch_callback)(uint32_t, uint32_t, void *);
57 1.1 jmcneill void *ch_callbackarg;
58 1.1 jmcneill uint32_t ch_debug;
59 1.1 jmcneill };
60 1.1 jmcneill
61 1.1 jmcneill #define DMAC_CHANNEL_TYPE(ch) \
62 1.1 jmcneill (((ch)->ch_debug & DMAC_DEBUG_LITE) ? \
63 1.1 jmcneill BCM_DMAC_TYPE_LITE : BCM_DMAC_TYPE_NORMAL)
64 1.1 jmcneill #define DMAC_CHANNEL_USED(ch) \
65 1.1 jmcneill ((ch)->ch_callback != NULL)
66 1.1 jmcneill
67 1.1 jmcneill struct bcm_dmac_softc {
68 1.1 jmcneill device_t sc_dev;
69 1.1 jmcneill bus_space_tag_t sc_iot;
70 1.1 jmcneill bus_space_handle_t sc_ioh;
71 1.1 jmcneill kmutex_t sc_lock;
72 1.1 jmcneill struct bcm_dmac_channel *sc_channels;
73 1.1 jmcneill int sc_nchannels;
74 1.1 jmcneill uint32_t sc_channelmask;
75 1.1 jmcneill };
76 1.1 jmcneill
77 1.1 jmcneill #define DMAC_READ(sc, reg) \
78 1.1 jmcneill bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
79 1.1 jmcneill #define DMAC_WRITE(sc, reg, val) \
80 1.1 jmcneill bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
81 1.1 jmcneill
82 1.1 jmcneill static int bcm_dmac_match(device_t, cfdata_t, void *);
83 1.1 jmcneill static void bcm_dmac_attach(device_t, device_t, void *);
84 1.1 jmcneill
85 1.1 jmcneill static int bcm_dmac_intr(void *);
86 1.1 jmcneill
87 1.1 jmcneill #if defined(DDB)
88 1.1 jmcneill void bcm_dmac_dump_regs(void);
89 1.1 jmcneill #endif
90 1.1 jmcneill
91 1.1 jmcneill CFATTACH_DECL_NEW(bcmdmac_amba, sizeof(struct bcm_dmac_softc),
92 1.1 jmcneill bcm_dmac_match, bcm_dmac_attach, NULL, NULL);
93 1.1 jmcneill
94 1.1 jmcneill static int
95 1.1 jmcneill bcm_dmac_match(device_t parent, cfdata_t cf, void *aux)
96 1.1 jmcneill {
97 1.1 jmcneill struct amba_attach_args *aaa = aux;
98 1.1 jmcneill
99 1.1 jmcneill if (strcmp(aaa->aaa_name, "bcmdmac") != 0)
100 1.1 jmcneill return 0;
101 1.1 jmcneill
102 1.1 jmcneill if (aaa->aaa_addr != BCM2835_DMA0_BASE)
103 1.1 jmcneill return 0;
104 1.1 jmcneill
105 1.1 jmcneill return 1;
106 1.1 jmcneill }
107 1.1 jmcneill
108 1.1 jmcneill static void
109 1.1 jmcneill bcm_dmac_attach(device_t parent, device_t self, void *aux)
110 1.1 jmcneill {
111 1.1 jmcneill struct bcm_dmac_softc *sc = device_private(self);
112 1.4 jmcneill const prop_dictionary_t cfg = device_properties(self);
113 1.1 jmcneill struct bcm_dmac_channel *ch;
114 1.1 jmcneill struct amba_attach_args *aaa = aux;
115 1.1 jmcneill uint32_t val;
116 1.1 jmcneill int index;
117 1.1 jmcneill
118 1.1 jmcneill sc->sc_dev = self;
119 1.1 jmcneill sc->sc_iot = aaa->aaa_iot;
120 1.1 jmcneill
121 1.1 jmcneill if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, aaa->aaa_size, 0,
122 1.1 jmcneill &sc->sc_ioh)) {
123 1.1 jmcneill aprint_error(": unable to map device\n");
124 1.1 jmcneill return;
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.3 jmcneill prop_dictionary_get_uint32(cfg, "chanmask", &sc->sc_channelmask);
128 1.3 jmcneill sc->sc_channelmask &= BCM_DMAC_CHANNELMASK;
129 1.1 jmcneill
130 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
131 1.1 jmcneill
132 1.3 jmcneill sc->sc_nchannels = 31 - __builtin_clz(sc->sc_channelmask);
133 1.1 jmcneill sc->sc_channels = kmem_alloc(
134 1.1 jmcneill sizeof(*sc->sc_channels) * sc->sc_nchannels, KM_SLEEP);
135 1.1 jmcneill if (sc->sc_channels == NULL) {
136 1.1 jmcneill aprint_error(": couldn't allocate channels\n");
137 1.1 jmcneill return;
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.1 jmcneill aprint_normal(":");
141 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
142 1.1 jmcneill ch = &sc->sc_channels[index];
143 1.1 jmcneill ch->ch_sc = sc;
144 1.1 jmcneill ch->ch_index = index;
145 1.1 jmcneill ch->ch_callback = NULL;
146 1.1 jmcneill ch->ch_callbackarg = NULL;
147 1.8 jmcneill ch->ch_ih = NULL;
148 1.2 jmcneill if ((__BIT(index) & sc->sc_channelmask) == 0)
149 1.1 jmcneill continue;
150 1.1 jmcneill
151 1.1 jmcneill aprint_normal(" DMA%d", index);
152 1.1 jmcneill
153 1.1 jmcneill ch->ch_debug = DMAC_READ(sc, DMAC_DEBUG(index));
154 1.1 jmcneill
155 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(index));
156 1.1 jmcneill val |= DMAC_CS_RESET;
157 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(index), val);
158 1.1 jmcneill }
159 1.1 jmcneill aprint_normal("\n");
160 1.1 jmcneill aprint_naive("\n");
161 1.1 jmcneill }
162 1.1 jmcneill
163 1.1 jmcneill static int
164 1.1 jmcneill bcm_dmac_intr(void *priv)
165 1.1 jmcneill {
166 1.1 jmcneill struct bcm_dmac_channel *ch = priv;
167 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
168 1.13 mlelstv uint32_t cs, ce;
169 1.1 jmcneill
170 1.1 jmcneill cs = DMAC_READ(sc, DMAC_CS(ch->ch_index));
171 1.13 mlelstv DMAC_WRITE(sc, DMAC_CS(ch->ch_index), cs);
172 1.13 mlelstv cs &= DMAC_CS_INT | DMAC_CS_END | DMAC_CS_ERROR;
173 1.1 jmcneill
174 1.13 mlelstv ce = DMAC_READ(sc, DMAC_DEBUG(ch->ch_index));
175 1.13 mlelstv ce &= DMAC_DEBUG_READ_ERROR | DMAC_DEBUG_FIFO_ERROR
176 1.13 mlelstv | DMAC_DEBUG_READ_LAST_NOT_SET_ERROR;
177 1.13 mlelstv DMAC_WRITE(sc, DMAC_DEBUG(ch->ch_index), ce);
178 1.1 jmcneill
179 1.1 jmcneill if (ch->ch_callback)
180 1.13 mlelstv ch->ch_callback(cs, ce, ch->ch_callbackarg);
181 1.1 jmcneill
182 1.1 jmcneill return 1;
183 1.1 jmcneill }
184 1.1 jmcneill
185 1.1 jmcneill struct bcm_dmac_channel *
186 1.13 mlelstv bcm_dmac_alloc(enum bcm_dmac_type type, int ipl,
187 1.13 mlelstv void (*cb)(uint32_t, uint32_t, void *), void *cbarg)
188 1.1 jmcneill {
189 1.1 jmcneill struct bcm_dmac_softc *sc;
190 1.1 jmcneill struct bcm_dmac_channel *ch = NULL;
191 1.1 jmcneill device_t dev;
192 1.1 jmcneill int index;
193 1.1 jmcneill
194 1.1 jmcneill dev = device_find_by_driver_unit("bcmdmac", 0);
195 1.1 jmcneill if (dev == NULL)
196 1.1 jmcneill return NULL;
197 1.1 jmcneill sc = device_private(dev);
198 1.1 jmcneill
199 1.1 jmcneill mutex_enter(&sc->sc_lock);
200 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
201 1.1 jmcneill if ((sc->sc_channelmask & __BIT(index)) == 0)
202 1.1 jmcneill continue;
203 1.1 jmcneill if (DMAC_CHANNEL_TYPE(&sc->sc_channels[index]) != type)
204 1.1 jmcneill continue;
205 1.1 jmcneill if (DMAC_CHANNEL_USED(&sc->sc_channels[index]))
206 1.1 jmcneill continue;
207 1.1 jmcneill
208 1.1 jmcneill ch = &sc->sc_channels[index];
209 1.1 jmcneill ch->ch_callback = cb;
210 1.1 jmcneill ch->ch_callbackarg = cbarg;
211 1.1 jmcneill break;
212 1.1 jmcneill }
213 1.1 jmcneill mutex_exit(&sc->sc_lock);
214 1.1 jmcneill
215 1.7 jmcneill if (ch == NULL)
216 1.7 jmcneill return NULL;
217 1.7 jmcneill
218 1.7 jmcneill KASSERT(ch->ch_ih == NULL);
219 1.10 skrll ch->ch_ih = intr_establish(BCM2835_INT_DMA0 + ch->ch_index,
220 1.11 skrll ipl, IST_LEVEL, bcm_dmac_intr, ch);
221 1.6 jakllsch if (ch->ch_ih == NULL) {
222 1.6 jakllsch aprint_error_dev(sc->sc_dev,
223 1.6 jakllsch "failed to establish interrupt for DMA%d\n", ch->ch_index);
224 1.7 jmcneill ch->ch_callback = NULL;
225 1.7 jmcneill ch->ch_callbackarg = NULL;
226 1.9 jmcneill ch = NULL;
227 1.6 jakllsch }
228 1.6 jakllsch
229 1.1 jmcneill return ch;
230 1.1 jmcneill }
231 1.1 jmcneill
232 1.1 jmcneill void
233 1.1 jmcneill bcm_dmac_free(struct bcm_dmac_channel *ch)
234 1.1 jmcneill {
235 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
236 1.1 jmcneill uint32_t val;
237 1.1 jmcneill
238 1.1 jmcneill bcm_dmac_halt(ch);
239 1.1 jmcneill
240 1.14 mlelstv /* reset chip */
241 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
242 1.1 jmcneill val |= DMAC_CS_RESET;
243 1.1 jmcneill val &= ~DMAC_CS_ACTIVE;
244 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
245 1.1 jmcneill
246 1.1 jmcneill mutex_enter(&sc->sc_lock);
247 1.7 jmcneill intr_disestablish(ch->ch_ih);
248 1.7 jmcneill ch->ch_ih = NULL;
249 1.1 jmcneill ch->ch_callback = NULL;
250 1.1 jmcneill ch->ch_callbackarg = NULL;
251 1.1 jmcneill mutex_exit(&sc->sc_lock);
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill void
255 1.1 jmcneill bcm_dmac_set_conblk_addr(struct bcm_dmac_channel *ch, bus_addr_t addr)
256 1.1 jmcneill {
257 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
258 1.1 jmcneill
259 1.1 jmcneill DMAC_WRITE(sc, DMAC_CONBLK_AD(ch->ch_index), addr);
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill int
263 1.1 jmcneill bcm_dmac_transfer(struct bcm_dmac_channel *ch)
264 1.1 jmcneill {
265 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
266 1.1 jmcneill uint32_t val;
267 1.1 jmcneill
268 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
269 1.1 jmcneill if (val & DMAC_CS_ACTIVE)
270 1.1 jmcneill return EBUSY;
271 1.1 jmcneill
272 1.1 jmcneill val |= DMAC_CS_ACTIVE;
273 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
274 1.1 jmcneill
275 1.1 jmcneill return 0;
276 1.1 jmcneill }
277 1.1 jmcneill
278 1.1 jmcneill void
279 1.1 jmcneill bcm_dmac_halt(struct bcm_dmac_channel *ch)
280 1.1 jmcneill {
281 1.12 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
282 1.14 mlelstv uint32_t val;
283 1.14 mlelstv
284 1.14 mlelstv /* pause DMA */
285 1.14 mlelstv val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
286 1.14 mlelstv val &= ~DMAC_CS_ACTIVE;
287 1.14 mlelstv DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
288 1.14 mlelstv
289 1.14 mlelstv /* wait for paused state ? */
290 1.12 jmcneill
291 1.14 mlelstv /* end descriptor chain */
292 1.14 mlelstv DMAC_WRITE(sc, DMAC_NEXTCONBK(ch->ch_index), 0);
293 1.14 mlelstv
294 1.14 mlelstv /* resume DMA that then stops */
295 1.14 mlelstv val |= DMAC_CS_ACTIVE | DMAC_CS_ABORT;
296 1.14 mlelstv DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
297 1.1 jmcneill }
298 1.1 jmcneill
299 1.1 jmcneill #if defined(DDB)
300 1.1 jmcneill void
301 1.1 jmcneill bcm_dmac_dump_regs(void)
302 1.1 jmcneill {
303 1.1 jmcneill struct bcm_dmac_softc *sc;
304 1.1 jmcneill device_t dev;
305 1.1 jmcneill int index;
306 1.1 jmcneill
307 1.1 jmcneill dev = device_find_by_driver_unit("bcmdmac", 0);
308 1.1 jmcneill if (dev == NULL)
309 1.1 jmcneill return;
310 1.1 jmcneill sc = device_private(dev);
311 1.1 jmcneill
312 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
313 1.1 jmcneill if ((sc->sc_channelmask & __BIT(index)) == 0)
314 1.1 jmcneill continue;
315 1.1 jmcneill printf("%d_CS: %08X\n", index,
316 1.1 jmcneill DMAC_READ(sc, DMAC_CS(index)));
317 1.1 jmcneill printf("%d_CONBLK_AD: %08X\n", index,
318 1.1 jmcneill DMAC_READ(sc, DMAC_CONBLK_AD(index)));
319 1.1 jmcneill printf("%d_DEBUG: %08X\n", index,
320 1.1 jmcneill DMAC_READ(sc, DMAC_DEBUG(index)));
321 1.1 jmcneill }
322 1.1 jmcneill }
323 1.1 jmcneill #endif
324