bcm2835_dmac.c revision 1.12 1 1.12 jmcneill /* $NetBSD: bcm2835_dmac.c,v 1.12 2015/08/02 16:46:12 jmcneill 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.12 jmcneill __KERNEL_RCSID(0, "$NetBSD: bcm2835_dmac.c,v 1.12 2015/08/02 16:46:12 jmcneill 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.1 jmcneill void (*ch_callback)(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.1 jmcneill uint32_t cs;
169 1.1 jmcneill
170 1.1 jmcneill cs = DMAC_READ(sc, DMAC_CS(ch->ch_index));
171 1.1 jmcneill if (!(cs & DMAC_CS_INTMASK))
172 1.1 jmcneill return 0;
173 1.1 jmcneill
174 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), cs);
175 1.1 jmcneill
176 1.1 jmcneill if (ch->ch_callback)
177 1.1 jmcneill ch->ch_callback(ch->ch_callbackarg);
178 1.1 jmcneill
179 1.1 jmcneill return 1;
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.1 jmcneill struct bcm_dmac_channel *
183 1.6 jakllsch bcm_dmac_alloc(enum bcm_dmac_type type, int ipl, void (*cb)(void *),
184 1.6 jakllsch void *cbarg)
185 1.1 jmcneill {
186 1.1 jmcneill struct bcm_dmac_softc *sc;
187 1.1 jmcneill struct bcm_dmac_channel *ch = NULL;
188 1.1 jmcneill device_t dev;
189 1.1 jmcneill int index;
190 1.1 jmcneill
191 1.1 jmcneill dev = device_find_by_driver_unit("bcmdmac", 0);
192 1.1 jmcneill if (dev == NULL)
193 1.1 jmcneill return NULL;
194 1.1 jmcneill sc = device_private(dev);
195 1.1 jmcneill
196 1.1 jmcneill mutex_enter(&sc->sc_lock);
197 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
198 1.1 jmcneill if ((sc->sc_channelmask & __BIT(index)) == 0)
199 1.1 jmcneill continue;
200 1.1 jmcneill if (DMAC_CHANNEL_TYPE(&sc->sc_channels[index]) != type)
201 1.1 jmcneill continue;
202 1.1 jmcneill if (DMAC_CHANNEL_USED(&sc->sc_channels[index]))
203 1.1 jmcneill continue;
204 1.1 jmcneill
205 1.1 jmcneill ch = &sc->sc_channels[index];
206 1.1 jmcneill ch->ch_callback = cb;
207 1.1 jmcneill ch->ch_callbackarg = cbarg;
208 1.1 jmcneill break;
209 1.1 jmcneill }
210 1.1 jmcneill mutex_exit(&sc->sc_lock);
211 1.1 jmcneill
212 1.7 jmcneill if (ch == NULL)
213 1.7 jmcneill return NULL;
214 1.7 jmcneill
215 1.7 jmcneill KASSERT(ch->ch_ih == NULL);
216 1.10 skrll ch->ch_ih = intr_establish(BCM2835_INT_DMA0 + ch->ch_index,
217 1.11 skrll ipl, IST_LEVEL, bcm_dmac_intr, ch);
218 1.6 jakllsch if (ch->ch_ih == NULL) {
219 1.6 jakllsch aprint_error_dev(sc->sc_dev,
220 1.6 jakllsch "failed to establish interrupt for DMA%d\n", ch->ch_index);
221 1.7 jmcneill ch->ch_callback = NULL;
222 1.7 jmcneill ch->ch_callbackarg = NULL;
223 1.9 jmcneill ch = NULL;
224 1.6 jakllsch }
225 1.6 jakllsch
226 1.1 jmcneill return ch;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill void
230 1.1 jmcneill bcm_dmac_free(struct bcm_dmac_channel *ch)
231 1.1 jmcneill {
232 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
233 1.1 jmcneill uint32_t val;
234 1.1 jmcneill
235 1.1 jmcneill bcm_dmac_halt(ch);
236 1.1 jmcneill
237 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
238 1.1 jmcneill val |= DMAC_CS_RESET;
239 1.1 jmcneill val |= DMAC_CS_ABORT;
240 1.1 jmcneill val &= ~DMAC_CS_ACTIVE;
241 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
242 1.1 jmcneill
243 1.1 jmcneill mutex_enter(&sc->sc_lock);
244 1.7 jmcneill intr_disestablish(ch->ch_ih);
245 1.7 jmcneill ch->ch_ih = NULL;
246 1.1 jmcneill ch->ch_callback = NULL;
247 1.1 jmcneill ch->ch_callbackarg = NULL;
248 1.1 jmcneill mutex_exit(&sc->sc_lock);
249 1.1 jmcneill }
250 1.1 jmcneill
251 1.1 jmcneill void
252 1.1 jmcneill bcm_dmac_set_conblk_addr(struct bcm_dmac_channel *ch, bus_addr_t addr)
253 1.1 jmcneill {
254 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
255 1.1 jmcneill
256 1.1 jmcneill DMAC_WRITE(sc, DMAC_CONBLK_AD(ch->ch_index), addr);
257 1.1 jmcneill }
258 1.1 jmcneill
259 1.1 jmcneill int
260 1.1 jmcneill bcm_dmac_transfer(struct bcm_dmac_channel *ch)
261 1.1 jmcneill {
262 1.1 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
263 1.1 jmcneill uint32_t val;
264 1.1 jmcneill
265 1.1 jmcneill val = DMAC_READ(sc, DMAC_CS(ch->ch_index));
266 1.1 jmcneill if (val & DMAC_CS_ACTIVE)
267 1.1 jmcneill return EBUSY;
268 1.1 jmcneill
269 1.1 jmcneill val |= DMAC_CS_ACTIVE;
270 1.1 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), val);
271 1.1 jmcneill
272 1.1 jmcneill return 0;
273 1.1 jmcneill }
274 1.1 jmcneill
275 1.1 jmcneill void
276 1.1 jmcneill bcm_dmac_halt(struct bcm_dmac_channel *ch)
277 1.1 jmcneill {
278 1.12 jmcneill struct bcm_dmac_softc *sc = ch->ch_sc;
279 1.12 jmcneill
280 1.12 jmcneill DMAC_WRITE(sc, DMAC_CS(ch->ch_index), DMAC_CS_RESET|DMAC_CS_ABORT);
281 1.1 jmcneill bcm_dmac_set_conblk_addr(ch, 0);
282 1.1 jmcneill }
283 1.1 jmcneill
284 1.1 jmcneill #if defined(DDB)
285 1.1 jmcneill void
286 1.1 jmcneill bcm_dmac_dump_regs(void)
287 1.1 jmcneill {
288 1.1 jmcneill struct bcm_dmac_softc *sc;
289 1.1 jmcneill device_t dev;
290 1.1 jmcneill int index;
291 1.1 jmcneill
292 1.1 jmcneill dev = device_find_by_driver_unit("bcmdmac", 0);
293 1.1 jmcneill if (dev == NULL)
294 1.1 jmcneill return;
295 1.1 jmcneill sc = device_private(dev);
296 1.1 jmcneill
297 1.1 jmcneill for (index = 0; index < sc->sc_nchannels; index++) {
298 1.1 jmcneill if ((sc->sc_channelmask & __BIT(index)) == 0)
299 1.1 jmcneill continue;
300 1.1 jmcneill printf("%d_CS: %08X\n", index,
301 1.1 jmcneill DMAC_READ(sc, DMAC_CS(index)));
302 1.1 jmcneill printf("%d_CONBLK_AD: %08X\n", index,
303 1.1 jmcneill DMAC_READ(sc, DMAC_CONBLK_AD(index)));
304 1.1 jmcneill printf("%d_DEBUG: %08X\n", index,
305 1.1 jmcneill DMAC_READ(sc, DMAC_DEBUG(index)));
306 1.1 jmcneill }
307 1.1 jmcneill }
308 1.1 jmcneill #endif
309