bcm2835_dmac.c revision 1.16 1 1.16 skrll /* $NetBSD: bcm2835_dmac.c,v 1.16 2017/12/10 21:38:26 skrll 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.16 skrll __KERNEL_RCSID(0, "$NetBSD: bcm2835_dmac.c,v 1.16 2017/12/10 21:38:26 skrll 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/bcm2835reg.h>
43 1.1 jmcneill #include <arm/broadcom/bcm2835_intr.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <arm/broadcom/bcm2835_dmac.h>
46 1.1 jmcneill
47 1.16 skrll #include <dev/fdt/fdtvar.h>
48 1.16 skrll
49 1.16 skrll #include <arm/fdt/arm_fdtvar.h>
50 1.16 skrll
51 1.5 jmcneill #define BCM_DMAC_CHANNELMASK 0x00000fff
52 1.1 jmcneill
53 1.1 jmcneill struct bcm_dmac_softc;
54 1.1 jmcneill
55 1.1 jmcneill struct bcm_dmac_channel {
56 1.1 jmcneill struct bcm_dmac_softc *ch_sc;
57 1.1 jmcneill void *ch_ih;
58 1.1 jmcneill uint8_t ch_index;
59 1.13 mlelstv void (*ch_callback)(uint32_t, uint32_t, void *);
60 1.1 jmcneill void *ch_callbackarg;
61 1.1 jmcneill uint32_t ch_debug;
62 1.1 jmcneill };
63 1.1 jmcneill
64 1.1 jmcneill #define DMAC_CHANNEL_TYPE(ch) \
65 1.1 jmcneill (((ch)->ch_debug & DMAC_DEBUG_LITE) ? \
66 1.1 jmcneill BCM_DMAC_TYPE_LITE : BCM_DMAC_TYPE_NORMAL)
67 1.1 jmcneill #define DMAC_CHANNEL_USED(ch) \
68 1.1 jmcneill ((ch)->ch_callback != NULL)
69 1.1 jmcneill
70 1.1 jmcneill struct bcm_dmac_softc {
71 1.1 jmcneill device_t sc_dev;
72 1.1 jmcneill bus_space_tag_t sc_iot;
73 1.1 jmcneill bus_space_handle_t sc_ioh;
74 1.16 skrll int sc_phandle;
75 1.16 skrll
76 1.1 jmcneill kmutex_t sc_lock;
77 1.1 jmcneill struct bcm_dmac_channel *sc_channels;
78 1.1 jmcneill int sc_nchannels;
79 1.1 jmcneill uint32_t sc_channelmask;
80 1.1 jmcneill };
81 1.1 jmcneill
82 1.1 jmcneill #define DMAC_READ(sc, reg) \
83 1.1 jmcneill bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
84 1.1 jmcneill #define DMAC_WRITE(sc, reg, val) \
85 1.1 jmcneill bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
86 1.1 jmcneill
87 1.1 jmcneill static int bcm_dmac_match(device_t, cfdata_t, void *);
88 1.1 jmcneill static void bcm_dmac_attach(device_t, device_t, void *);
89 1.1 jmcneill
90 1.1 jmcneill static int bcm_dmac_intr(void *);
91 1.1 jmcneill
92 1.1 jmcneill #if defined(DDB)
93 1.1 jmcneill void bcm_dmac_dump_regs(void);
94 1.1 jmcneill #endif
95 1.1 jmcneill
96 1.16 skrll CFATTACH_DECL_NEW(bcmdmac_fdt, sizeof(struct bcm_dmac_softc),
97 1.1 jmcneill bcm_dmac_match, bcm_dmac_attach, NULL, NULL);
98 1.1 jmcneill
99 1.1 jmcneill static int
100 1.1 jmcneill bcm_dmac_match(device_t parent, cfdata_t cf, void *aux)
101 1.1 jmcneill {
102 1.16 skrll const char * const compatible[] = {
103 1.16 skrll "brcm,bcm2835-dma",
104 1.16 skrll NULL
105 1.16 skrll };
106 1.16 skrll struct fdt_attach_args * const faa = aux;
107 1.1 jmcneill
108 1.16 skrll return of_match_compatible(faa->faa_phandle, compatible);
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.1 jmcneill static void
112 1.1 jmcneill bcm_dmac_attach(device_t parent, device_t self, void *aux)
113 1.1 jmcneill {
114 1.1 jmcneill struct bcm_dmac_softc *sc = device_private(self);
115 1.4 jmcneill const prop_dictionary_t cfg = device_properties(self);
116 1.16 skrll struct fdt_attach_args * const faa = aux;
117 1.1 jmcneill struct bcm_dmac_channel *ch;
118 1.1 jmcneill uint32_t val;
119 1.1 jmcneill int index;
120 1.1 jmcneill
121 1.16 skrll const int phandle = faa->faa_phandle;
122 1.16 skrll
123 1.1 jmcneill sc->sc_dev = self;
124 1.16 skrll sc->sc_iot = faa->faa_bst;
125 1.16 skrll sc->sc_phandle = phandle;
126 1.16 skrll
127 1.16 skrll bus_addr_t addr;
128 1.16 skrll bus_size_t size;
129 1.1 jmcneill
130 1.16 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
131 1.16 skrll aprint_error(": missing 'reg' property\n");
132 1.16 skrll return;
133 1.16 skrll }
134 1.16 skrll
135 1.16 skrll if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
136 1.1 jmcneill aprint_error(": unable to map device\n");
137 1.1 jmcneill return;
138 1.1 jmcneill }
139 1.1 jmcneill
140 1.3 jmcneill prop_dictionary_get_uint32(cfg, "chanmask", &sc->sc_channelmask);
141 1.3 jmcneill sc->sc_channelmask &= BCM_DMAC_CHANNELMASK;
142 1.1 jmcneill
143 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
144 1.1 jmcneill
145 1.3 jmcneill sc->sc_nchannels = 31 - __builtin_clz(sc->sc_channelmask);
146 1.1 jmcneill sc->sc_channels = kmem_alloc(
147 1.1 jmcneill sizeof(*sc->sc_channels) * sc->sc_nchannels, KM_SLEEP);
148 1.1 jmcneill
149 1.1 jmcneill aprint_normal(":");
150 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
151 1.1 jmcneill ch = &sc->sc_channels[index];
152 1.1 jmcneill ch->ch_sc = sc;
153 1.1 jmcneill ch->ch_index = index;
154 1.1 jmcneill ch->ch_callback = NULL;
155 1.1 jmcneill ch->ch_callbackarg = NULL;
156 1.8 jmcneill ch->ch_ih = NULL;
157 1.2 jmcneill if ((__BIT(index) & sc->sc_channelmask) == 0)
158 1.1 jmcneill continue;
159 1.1 jmcneill
160 1.1 jmcneill aprint_normal(" DMA%d", index);
161 1.1 jmcneill
162 1.1 jmcneill ch->ch_debug = DMAC_READ(sc, DMAC_DEBUG(index));
163 1.1 jmcneill
164 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(index));
165 1.1 jmcneill val |= DMAC_CS_RESET;
166 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(index), val);
167 1.1 jmcneill }
168 1.1 jmcneill aprint_normal("\n");
169 1.1 jmcneill aprint_naive("\n");
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill static int
173 1.1 jmcneill bcm_dmac_intr(void *priv)
174 1.1 jmcneill {
175 1.1 jmcneill struct bcm_dmac_channel *ch = priv;
176 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
177 1.13 mlelstv uint32_t cs, ce;
178 1.1 jmcneill
179 1.1 jmcneill cs = DMAC_READ(sc, DMAC_CS(ch->ch_index));
180 1.13 mlelstv DMAC_WRITE(sc, DMAC_CS(ch->ch_index), cs);
181 1.13 mlelstv cs &= DMAC_CS_INT | DMAC_CS_END | DMAC_CS_ERROR;
182 1.1 jmcneill
183 1.13 mlelstv ce = DMAC_READ(sc, DMAC_DEBUG(ch->ch_index));
184 1.13 mlelstv ce &= DMAC_DEBUG_READ_ERROR | DMAC_DEBUG_FIFO_ERROR
185 1.13 mlelstv | DMAC_DEBUG_READ_LAST_NOT_SET_ERROR;
186 1.13 mlelstv DMAC_WRITE(sc, DMAC_DEBUG(ch->ch_index), ce);
187 1.1 jmcneill
188 1.1 jmcneill if (ch->ch_callback)
189 1.13 mlelstv ch->ch_callback(cs, ce, ch->ch_callbackarg);
190 1.1 jmcneill
191 1.1 jmcneill return 1;
192 1.1 jmcneill }
193 1.1 jmcneill
194 1.1 jmcneill struct bcm_dmac_channel *
195 1.13 mlelstv bcm_dmac_alloc(enum bcm_dmac_type type, int ipl,
196 1.13 mlelstv void (*cb)(uint32_t, uint32_t, void *), void *cbarg)
197 1.1 jmcneill {
198 1.1 jmcneill struct bcm_dmac_softc *sc;
199 1.1 jmcneill struct bcm_dmac_channel *ch = NULL;
200 1.1 jmcneill device_t dev;
201 1.1 jmcneill int index;
202 1.1 jmcneill
203 1.1 jmcneill dev = device_find_by_driver_unit("bcmdmac", 0);
204 1.1 jmcneill if (dev == NULL)
205 1.1 jmcneill return NULL;
206 1.1 jmcneill sc = device_private(dev);
207 1.1 jmcneill
208 1.1 jmcneill mutex_enter(&sc->sc_lock);
209 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
210 1.1 jmcneill if ((sc->sc_channelmask & __BIT(index)) == 0)
211 1.1 jmcneill continue;
212 1.1 jmcneill if (DMAC_CHANNEL_TYPE(&sc->sc_channels[index]) != type)
213 1.1 jmcneill continue;
214 1.1 jmcneill if (DMAC_CHANNEL_USED(&sc->sc_channels[index]))
215 1.1 jmcneill continue;
216 1.1 jmcneill
217 1.1 jmcneill ch = &sc->sc_channels[index];
218 1.1 jmcneill ch->ch_callback = cb;
219 1.1 jmcneill ch->ch_callbackarg = cbarg;
220 1.1 jmcneill break;
221 1.1 jmcneill }
222 1.1 jmcneill mutex_exit(&sc->sc_lock);
223 1.1 jmcneill
224 1.7 jmcneill if (ch == NULL)
225 1.7 jmcneill return NULL;
226 1.7 jmcneill
227 1.7 jmcneill KASSERT(ch->ch_ih == NULL);
228 1.16 skrll
229 1.16 skrll const int phandle = sc->sc_phandle;
230 1.16 skrll char intrstr[128];
231 1.16 skrll
232 1.16 skrll if (!fdtbus_intr_str(phandle, ch->ch_index, intrstr, sizeof(intrstr))) {
233 1.16 skrll aprint_error(": failed to decode interrupt\n");
234 1.16 skrll return NULL;
235 1.16 skrll }
236 1.16 skrll
237 1.16 skrll ch->ch_ih = fdtbus_intr_establish(phandle, ch->ch_index, ipl, 0,
238 1.16 skrll bcm_dmac_intr, ch);
239 1.6 jakllsch if (ch->ch_ih == NULL) {
240 1.6 jakllsch aprint_error_dev(sc->sc_dev,
241 1.16 skrll "failed to establish interrupt for DMA%d and %s\n", ch->ch_index,
242 1.16 skrll intrstr);
243 1.7 jmcneill ch->ch_callback = NULL;
244 1.7 jmcneill ch->ch_callbackarg = NULL;
245 1.9 jmcneill ch = NULL;
246 1.6 jakllsch }
247 1.6 jakllsch
248 1.1 jmcneill return ch;
249 1.1 jmcneill }
250 1.1 jmcneill
251 1.1 jmcneill void
252 1.1 jmcneill bcm_dmac_free(struct bcm_dmac_channel *ch)
253 1.1 jmcneill {
254 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
255 1.1 jmcneill uint32_t val;
256 1.1 jmcneill
257 1.1 jmcneill bcm_dmac_halt(ch);
258 1.1 jmcneill
259 1.14 mlelstv /* reset chip */
260 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
261 1.1 jmcneill val |= DMAC_CS_RESET;
262 1.1 jmcneill val &= ~DMAC_CS_ACTIVE;
263 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
264 1.1 jmcneill
265 1.1 jmcneill mutex_enter(&sc->sc_lock);
266 1.16 skrll fdtbus_intr_disestablish(sc->sc_phandle, ch->ch_ih);
267 1.7 jmcneill ch->ch_ih = NULL;
268 1.1 jmcneill ch->ch_callback = NULL;
269 1.1 jmcneill ch->ch_callbackarg = NULL;
270 1.1 jmcneill mutex_exit(&sc->sc_lock);
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill void
274 1.1 jmcneill bcm_dmac_set_conblk_addr(struct bcm_dmac_channel *ch, bus_addr_t addr)
275 1.1 jmcneill {
276 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
277 1.1 jmcneill
278 1.1 jmcneill DMAC_WRITE(sc, DMAC_CONBLK_AD(ch->ch_index), addr);
279 1.1 jmcneill }
280 1.1 jmcneill
281 1.1 jmcneill int
282 1.1 jmcneill bcm_dmac_transfer(struct bcm_dmac_channel *ch)
283 1.1 jmcneill {
284 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
285 1.1 jmcneill uint32_t val;
286 1.1 jmcneill
287 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
288 1.1 jmcneill if (val & DMAC_CS_ACTIVE)
289 1.1 jmcneill return EBUSY;
290 1.1 jmcneill
291 1.1 jmcneill val |= DMAC_CS_ACTIVE;
292 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
293 1.1 jmcneill
294 1.1 jmcneill return 0;
295 1.1 jmcneill }
296 1.1 jmcneill
297 1.1 jmcneill void
298 1.1 jmcneill bcm_dmac_halt(struct bcm_dmac_channel *ch)
299 1.1 jmcneill {
300 1.12 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
301 1.14 mlelstv uint32_t val;
302 1.14 mlelstv
303 1.14 mlelstv /* pause DMA */
304 1.14 mlelstv val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
305 1.14 mlelstv val &= ~DMAC_CS_ACTIVE;
306 1.14 mlelstv DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
307 1.14 mlelstv
308 1.14 mlelstv /* wait for paused state ? */
309 1.12 jmcneill
310 1.14 mlelstv /* end descriptor chain */
311 1.14 mlelstv DMAC_WRITE(sc, DMAC_NEXTCONBK(ch->ch_index), 0);
312 1.14 mlelstv
313 1.14 mlelstv /* resume DMA that then stops */
314 1.14 mlelstv val |= DMAC_CS_ACTIVE | DMAC_CS_ABORT;
315 1.14 mlelstv DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill #if defined(DDB)
319 1.1 jmcneill void
320 1.1 jmcneill bcm_dmac_dump_regs(void)
321 1.1 jmcneill {
322 1.1 jmcneill struct bcm_dmac_softc *sc;
323 1.1 jmcneill device_t dev;
324 1.1 jmcneill int index;
325 1.1 jmcneill
326 1.1 jmcneill dev = device_find_by_driver_unit("bcmdmac", 0);
327 1.1 jmcneill if (dev == NULL)
328 1.1 jmcneill return;
329 1.1 jmcneill sc = device_private(dev);
330 1.1 jmcneill
331 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
332 1.1 jmcneill if ((sc->sc_channelmask & __BIT(index)) == 0)
333 1.1 jmcneill continue;
334 1.1 jmcneill printf("%d_CS: %08X\n", index,
335 1.1 jmcneill DMAC_READ(sc, DMAC_CS(index)));
336 1.1 jmcneill printf("%d_CONBLK_AD: %08X\n", index,
337 1.1 jmcneill DMAC_READ(sc, DMAC_CONBLK_AD(index)));
338 1.1 jmcneill printf("%d_DEBUG: %08X\n", index,
339 1.1 jmcneill DMAC_READ(sc, DMAC_DEBUG(index)));
340 1.1 jmcneill }
341 1.1 jmcneill }
342 1.1 jmcneill #endif
343