sun6i_dma.c revision 1.2 1 1.2 jmcneill /* $NetBSD: sun6i_dma.c,v 1.2 2017/08/06 17:13:15 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2014-2017 Jared 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.2 jmcneill #include "opt_ddb.h"
30 1.2 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: sun6i_dma.c,v 1.2 2017/08/06 17:13:15 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/mutex.h>
40 1.1 jmcneill #include <sys/bitops.h>
41 1.1 jmcneill #include <sys/kmem.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill
45 1.1 jmcneill #define DMA_IRQ_EN_REG0_REG 0x0000
46 1.1 jmcneill #define DMA_IRQ_EN_REG1_REG 0x0004
47 1.1 jmcneill #define DMA_IRQ_EN_REG0_QUEUE_IRQ_EN(n) __BIT(n * 4 + 2)
48 1.1 jmcneill #define DMA_IRQ_EN_REG0_PKG_IRQ_EN(n) __BIT(n * 4 + 1)
49 1.1 jmcneill #define DMA_IRQ_EN_REG0_HLAF_IRQ_EN(n) __BIT(n * 4 + 0)
50 1.1 jmcneill #define DMA_IRQ_EN_REG1_QUEUE_IRQ_EN(n) __BIT((n - 8) * 4 + 2)
51 1.1 jmcneill #define DMA_IRQ_EN_REG1_PKG_IRQ_EN(n) __BIT((n - 8) * 4 + 1)
52 1.1 jmcneill #define DMA_IRQ_EN_REG1_HLAF_IRQ_EN(n) __BIT((n - 8) * 4 + 0)
53 1.1 jmcneill #define DMA_IRQ_PEND_REG0_REG 0x0010
54 1.1 jmcneill #define DMA_IRQ_PEND_REG1_REG 0x0014
55 1.1 jmcneill #define DMA_IRQ_QUEUE_MASK 0x4444444444444444ULL
56 1.1 jmcneill #define DMA_IRQ_PKG_MASK 0x2222222222222222ULL
57 1.1 jmcneill #define DMA_IRQ_HF_MASK 0x1111111111111111ULL
58 1.1 jmcneill #define DMA_STA_REG 0x0030
59 1.1 jmcneill #define DMA_EN_REG(n) (0x0100 + (n) * 0x40 + 0x00)
60 1.1 jmcneill #define DMA_EN_EN __BIT(0)
61 1.1 jmcneill #define DMA_PAU_REG(n) (0x0100 + (n) * 0x40 + 0x04)
62 1.1 jmcneill #define DMA_PAU_PAUSE __BIT(0)
63 1.1 jmcneill #define DMA_START_ADDR_REG(n) (0x0100 + (n) * 0x40 + 0x08)
64 1.1 jmcneill #define DMA_CFG_REG(n) (0x0100 + (n) * 0x40 + 0x0C)
65 1.1 jmcneill #define DMA_CFG_DEST_DATA_WIDTH __BITS(26,25)
66 1.1 jmcneill #define DMA_CFG_DATA_WIDTH(n) ((n) >> 4)
67 1.1 jmcneill #define DMA_CFG_DEST_BST_LEN __BITS(24,23)
68 1.1 jmcneill #define DMA_CFG_BST_LEN(n) ((n) == 1 ? 0 : (((n) >> 3) + 1))
69 1.1 jmcneill #define DMA_CFG_DEST_ADDR_MODE __BITS(22,21)
70 1.1 jmcneill #define DMA_CFG_ADDR_MODE_LINEAR 0
71 1.1 jmcneill #define DMA_CFG_ADDR_MODE_IO 1
72 1.1 jmcneill #define DMA_CFG_DEST_DRQ_TYPE __BITS(20,16)
73 1.1 jmcneill #define DMA_CFG_DRQ_TYPE_SDRAM 1
74 1.1 jmcneill #define DMA_CFG_SRC_DATA_WIDTH __BITS(10,9)
75 1.1 jmcneill #define DMA_CFG_SRC_BST_LEN __BITS(8,7)
76 1.1 jmcneill #define DMA_CFG_SRC_ADDR_MODE __BITS(6,5)
77 1.1 jmcneill #define DMA_CFG_SRC_DRQ_TYPE __BITS(4,0)
78 1.1 jmcneill #define DMA_CUR_SRC_REG(n) (0x0100 + (n) * 0x40 + 0x10)
79 1.1 jmcneill #define DMA_CUR_DEST_REG(n) (0x0100 + (n) * 0x40 + 0x14)
80 1.1 jmcneill #define DMA_BCNT_LEFT_REG(n) (0x0100 + (n) * 0x40 + 0x18)
81 1.1 jmcneill #define DMA_PARA_REG(n) (0x0100 + (n) * 0x40 + 0x1C)
82 1.1 jmcneill #define DMA_PARA_DATA_BLK_SIZE __BITS(15,8)
83 1.1 jmcneill #define DMA_PARA_WAIT_CYC __BITS(7,0)
84 1.1 jmcneill
85 1.1 jmcneill struct sun6idma_desc {
86 1.1 jmcneill uint32_t dma_config;
87 1.1 jmcneill uint32_t dma_srcaddr;
88 1.1 jmcneill uint32_t dma_dstaddr;
89 1.1 jmcneill uint32_t dma_bcnt;
90 1.1 jmcneill uint32_t dma_para;
91 1.1 jmcneill uint32_t dma_next;
92 1.1 jmcneill #define DMA_NULL 0xfffff800
93 1.1 jmcneill };
94 1.1 jmcneill
95 1.1 jmcneill static const struct of_compat_data compat_data[] = {
96 1.1 jmcneill { "allwinner,sun6i-a31-dma", 16 },
97 1.1 jmcneill { "allwinner,sun8i-a83t-dma", 8 },
98 1.1 jmcneill { "allwinner,sun8i-h3-dma", 12 },
99 1.1 jmcneill { NULL }
100 1.1 jmcneill };
101 1.1 jmcneill
102 1.1 jmcneill struct sun6idma_channel {
103 1.1 jmcneill uint8_t ch_index;
104 1.1 jmcneill void (*ch_callback)(void *);
105 1.1 jmcneill void *ch_callbackarg;
106 1.1 jmcneill u_int ch_portid;
107 1.1 jmcneill
108 1.1 jmcneill bus_dma_segment_t ch_dmasegs[1];
109 1.1 jmcneill bus_dmamap_t ch_dmamap;
110 1.1 jmcneill void *ch_dmadesc;
111 1.1 jmcneill bus_size_t ch_dmadesclen;
112 1.1 jmcneill };
113 1.1 jmcneill
114 1.1 jmcneill struct sun6idma_softc {
115 1.1 jmcneill device_t sc_dev;
116 1.1 jmcneill bus_space_tag_t sc_bst;
117 1.1 jmcneill bus_space_handle_t sc_bsh;
118 1.1 jmcneill bus_dma_tag_t sc_dmat;
119 1.1 jmcneill int sc_phandle;
120 1.1 jmcneill void *sc_ih;
121 1.1 jmcneill
122 1.1 jmcneill kmutex_t sc_lock;
123 1.1 jmcneill
124 1.1 jmcneill struct sun6idma_channel *sc_chan;
125 1.1 jmcneill u_int sc_nchan;
126 1.1 jmcneill };
127 1.1 jmcneill
128 1.1 jmcneill #define DMA_READ(sc, reg) \
129 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
130 1.1 jmcneill #define DMA_WRITE(sc, reg, val) \
131 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
132 1.1 jmcneill
133 1.1 jmcneill static void *
134 1.1 jmcneill sun6idma_acquire(device_t dev, const void *data, size_t len,
135 1.1 jmcneill void (*cb)(void *), void *cbarg)
136 1.1 jmcneill {
137 1.1 jmcneill struct sun6idma_softc *sc = device_private(dev);
138 1.1 jmcneill struct sun6idma_channel *ch = NULL;
139 1.1 jmcneill uint32_t irqen;
140 1.1 jmcneill uint8_t index;
141 1.1 jmcneill
142 1.1 jmcneill if (len != 4)
143 1.1 jmcneill return NULL;
144 1.1 jmcneill
145 1.1 jmcneill const u_int portid = be32dec(data);
146 1.1 jmcneill if (portid > __SHIFTOUT_MASK(DMA_CFG_SRC_DRQ_TYPE))
147 1.1 jmcneill return NULL;
148 1.1 jmcneill
149 1.1 jmcneill mutex_enter(&sc->sc_lock);
150 1.1 jmcneill
151 1.1 jmcneill for (index = 0; index < sc->sc_nchan; index++) {
152 1.1 jmcneill if (sc->sc_chan[index].ch_callback == NULL) {
153 1.1 jmcneill ch = &sc->sc_chan[index];
154 1.1 jmcneill ch->ch_callback = cb;
155 1.1 jmcneill ch->ch_callbackarg = cbarg;
156 1.1 jmcneill ch->ch_portid = portid;
157 1.1 jmcneill
158 1.1 jmcneill irqen = DMA_READ(sc, index < 8 ?
159 1.1 jmcneill DMA_IRQ_EN_REG0_REG :
160 1.1 jmcneill DMA_IRQ_EN_REG1_REG);
161 1.1 jmcneill irqen |= (index < 8 ?
162 1.1 jmcneill DMA_IRQ_EN_REG0_PKG_IRQ_EN(index) :
163 1.1 jmcneill DMA_IRQ_EN_REG1_PKG_IRQ_EN(index));
164 1.1 jmcneill DMA_WRITE(sc, index < 8 ?
165 1.1 jmcneill DMA_IRQ_EN_REG0_REG :
166 1.1 jmcneill DMA_IRQ_EN_REG1_REG, irqen);
167 1.1 jmcneill
168 1.1 jmcneill break;
169 1.1 jmcneill }
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill mutex_exit(&sc->sc_lock);
173 1.1 jmcneill
174 1.1 jmcneill return ch;
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.1 jmcneill static void
178 1.1 jmcneill sun6idma_release(device_t dev, void *priv)
179 1.1 jmcneill {
180 1.1 jmcneill struct sun6idma_softc *sc = device_private(dev);
181 1.1 jmcneill struct sun6idma_channel *ch = priv;
182 1.1 jmcneill uint32_t irqen;
183 1.1 jmcneill uint8_t index = ch->ch_index;
184 1.1 jmcneill
185 1.1 jmcneill mutex_enter(&sc->sc_lock);
186 1.1 jmcneill
187 1.1 jmcneill irqen = DMA_READ(sc, index < 8 ?
188 1.1 jmcneill DMA_IRQ_EN_REG0_REG :
189 1.1 jmcneill DMA_IRQ_EN_REG1_REG);
190 1.1 jmcneill irqen &= ~(index < 8 ?
191 1.1 jmcneill DMA_IRQ_EN_REG0_PKG_IRQ_EN(index) :
192 1.1 jmcneill DMA_IRQ_EN_REG1_PKG_IRQ_EN(index));
193 1.1 jmcneill DMA_WRITE(sc, index < 8 ?
194 1.1 jmcneill DMA_IRQ_EN_REG0_REG :
195 1.1 jmcneill DMA_IRQ_EN_REG1_REG, irqen);
196 1.1 jmcneill
197 1.1 jmcneill ch->ch_callback = NULL;
198 1.1 jmcneill ch->ch_callbackarg = NULL;
199 1.1 jmcneill
200 1.1 jmcneill mutex_exit(&sc->sc_lock);
201 1.1 jmcneill }
202 1.1 jmcneill
203 1.1 jmcneill static int
204 1.1 jmcneill sun6idma_transfer(device_t dev, void *priv, struct fdtbus_dma_req *req)
205 1.1 jmcneill {
206 1.1 jmcneill struct sun6idma_softc *sc = device_private(dev);
207 1.1 jmcneill struct sun6idma_channel *ch = priv;
208 1.1 jmcneill struct sun6idma_desc *desc = ch->ch_dmadesc;
209 1.1 jmcneill uint32_t src, dst, len, cfg, mem_cfg, dev_cfg;
210 1.1 jmcneill uint32_t mem_width, dev_width, mem_burst, dev_burst;
211 1.1 jmcneill
212 1.1 jmcneill if (req->dreq_nsegs != 1)
213 1.1 jmcneill return EINVAL;
214 1.1 jmcneill
215 1.1 jmcneill mem_width = DMA_CFG_DATA_WIDTH(req->dreq_mem_opt.opt_bus_width);
216 1.1 jmcneill dev_width = DMA_CFG_DATA_WIDTH(req->dreq_dev_opt.opt_bus_width);
217 1.2 jmcneill mem_burst = DMA_CFG_BST_LEN(req->dreq_mem_opt.opt_burst_len);
218 1.2 jmcneill dev_burst = DMA_CFG_BST_LEN(req->dreq_dev_opt.opt_burst_len);
219 1.1 jmcneill
220 1.1 jmcneill mem_cfg = __SHIFTIN(mem_width, DMA_CFG_SRC_DATA_WIDTH) |
221 1.1 jmcneill __SHIFTIN(mem_burst, DMA_CFG_SRC_BST_LEN) |
222 1.1 jmcneill __SHIFTIN(DMA_CFG_ADDR_MODE_LINEAR, DMA_CFG_SRC_ADDR_MODE) |
223 1.1 jmcneill __SHIFTIN(DMA_CFG_DRQ_TYPE_SDRAM, DMA_CFG_SRC_DRQ_TYPE);
224 1.1 jmcneill dev_cfg = __SHIFTIN(dev_width, DMA_CFG_SRC_DATA_WIDTH) |
225 1.1 jmcneill __SHIFTIN(dev_burst, DMA_CFG_SRC_BST_LEN) |
226 1.1 jmcneill __SHIFTIN(DMA_CFG_ADDR_MODE_IO, DMA_CFG_SRC_ADDR_MODE) |
227 1.1 jmcneill __SHIFTIN(ch->ch_portid, DMA_CFG_SRC_DRQ_TYPE);
228 1.1 jmcneill
229 1.1 jmcneill if (req->dreq_dir == FDT_DMA_READ) {
230 1.1 jmcneill src = req->dreq_dev_phys;
231 1.1 jmcneill dst = req->dreq_segs[0].ds_addr;
232 1.1 jmcneill cfg = mem_cfg << 16 | dev_cfg;
233 1.1 jmcneill } else {
234 1.1 jmcneill src = req->dreq_segs[0].ds_addr;
235 1.1 jmcneill dst = req->dreq_dev_phys;
236 1.1 jmcneill cfg = dev_cfg << 16 | mem_cfg;
237 1.1 jmcneill }
238 1.1 jmcneill len = req->dreq_segs[0].ds_len;
239 1.1 jmcneill
240 1.1 jmcneill desc->dma_config = htole32(cfg);
241 1.1 jmcneill desc->dma_srcaddr = htole32(src);
242 1.1 jmcneill desc->dma_dstaddr = htole32(dst);
243 1.1 jmcneill desc->dma_bcnt = htole32(len);
244 1.1 jmcneill desc->dma_para = htole32(0);
245 1.1 jmcneill desc->dma_next = htole32(DMA_NULL);
246 1.1 jmcneill
247 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, ch->ch_dmamap, 0, ch->ch_dmadesclen,
248 1.1 jmcneill BUS_DMASYNC_PREWRITE);
249 1.1 jmcneill
250 1.1 jmcneill DMA_WRITE(sc, DMA_START_ADDR_REG(ch->ch_index),
251 1.1 jmcneill ch->ch_dmamap->dm_segs[0].ds_addr);
252 1.1 jmcneill DMA_WRITE(sc, DMA_EN_REG(ch->ch_index), DMA_EN_EN);
253 1.1 jmcneill
254 1.2 jmcneill if ((DMA_READ(sc, DMA_EN_REG(ch->ch_index)) & DMA_EN_EN) == 0) {
255 1.2 jmcneill aprint_error_dev(sc->sc_dev,
256 1.2 jmcneill "DMA Channel %u failed to start\n", ch->ch_index);
257 1.2 jmcneill return EIO;
258 1.2 jmcneill }
259 1.2 jmcneill
260 1.1 jmcneill return 0;
261 1.1 jmcneill }
262 1.1 jmcneill
263 1.1 jmcneill static void
264 1.1 jmcneill sun6idma_halt(device_t dev, void *priv)
265 1.1 jmcneill {
266 1.1 jmcneill struct sun6idma_softc *sc = device_private(dev);
267 1.1 jmcneill struct sun6idma_channel *ch = priv;
268 1.1 jmcneill
269 1.1 jmcneill DMA_WRITE(sc, DMA_EN_REG(ch->ch_index), 0);
270 1.1 jmcneill }
271 1.1 jmcneill
272 1.1 jmcneill static const struct fdtbus_dma_controller_func sun6idma_funcs = {
273 1.1 jmcneill .acquire = sun6idma_acquire,
274 1.1 jmcneill .release = sun6idma_release,
275 1.1 jmcneill .transfer = sun6idma_transfer,
276 1.1 jmcneill .halt = sun6idma_halt
277 1.1 jmcneill };
278 1.1 jmcneill
279 1.1 jmcneill static int
280 1.1 jmcneill sun6idma_intr(void *priv)
281 1.1 jmcneill {
282 1.1 jmcneill struct sun6idma_softc *sc = priv;
283 1.1 jmcneill uint32_t pend0, pend1, bit;
284 1.1 jmcneill uint64_t pend, mask;
285 1.1 jmcneill uint8_t index;
286 1.1 jmcneill
287 1.1 jmcneill pend0 = DMA_READ(sc, DMA_IRQ_PEND_REG0_REG);
288 1.1 jmcneill pend1 = DMA_READ(sc, DMA_IRQ_PEND_REG1_REG);
289 1.1 jmcneill if (!pend0 && !pend1)
290 1.1 jmcneill return 0;
291 1.1 jmcneill
292 1.1 jmcneill DMA_WRITE(sc, DMA_IRQ_PEND_REG0_REG, pend0);
293 1.1 jmcneill DMA_WRITE(sc, DMA_IRQ_PEND_REG1_REG, pend1);
294 1.1 jmcneill
295 1.1 jmcneill pend = pend0 | ((uint64_t)pend1 << 32);
296 1.1 jmcneill
297 1.1 jmcneill while ((bit = ffs64(pend & DMA_IRQ_PKG_MASK)) != 0) {
298 1.1 jmcneill mask = __BIT(bit - 1);
299 1.1 jmcneill pend &= ~mask;
300 1.1 jmcneill index = (bit - 1) / 4;
301 1.1 jmcneill
302 1.1 jmcneill if (sc->sc_chan[index].ch_callback == NULL)
303 1.1 jmcneill continue;
304 1.1 jmcneill sc->sc_chan[index].ch_callback(
305 1.1 jmcneill sc->sc_chan[index].ch_callbackarg);
306 1.1 jmcneill }
307 1.1 jmcneill
308 1.1 jmcneill return 1;
309 1.1 jmcneill }
310 1.1 jmcneill
311 1.1 jmcneill static int
312 1.1 jmcneill sun6idma_match(device_t parent, cfdata_t cf, void *aux)
313 1.1 jmcneill {
314 1.1 jmcneill struct fdt_attach_args * const faa = aux;
315 1.1 jmcneill
316 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
317 1.1 jmcneill }
318 1.1 jmcneill
319 1.1 jmcneill static void
320 1.1 jmcneill sun6idma_attach(device_t parent, device_t self, void *aux)
321 1.1 jmcneill {
322 1.1 jmcneill struct sun6idma_softc * const sc = device_private(self);
323 1.1 jmcneill struct fdt_attach_args * const faa = aux;
324 1.1 jmcneill const int phandle = faa->faa_phandle;
325 1.1 jmcneill const size_t desclen = sizeof(struct sun6idma_desc);
326 1.1 jmcneill struct fdtbus_reset *rst;
327 1.1 jmcneill struct clk *clk;
328 1.1 jmcneill char intrstr[128];
329 1.1 jmcneill bus_addr_t addr;
330 1.1 jmcneill bus_size_t size;
331 1.1 jmcneill int error, nsegs;
332 1.1 jmcneill u_int index;
333 1.1 jmcneill
334 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
335 1.1 jmcneill aprint_error(": couldn't get registers\n");
336 1.1 jmcneill return;
337 1.1 jmcneill }
338 1.1 jmcneill
339 1.1 jmcneill if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL ||
340 1.1 jmcneill clk_enable(clk) != 0) {
341 1.1 jmcneill aprint_error(": couldn't enable clock\n");
342 1.1 jmcneill return;
343 1.1 jmcneill }
344 1.1 jmcneill if ((rst = fdtbus_reset_get_index(phandle, 0)) == NULL ||
345 1.1 jmcneill fdtbus_reset_deassert(rst) != 0) {
346 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
347 1.1 jmcneill return;
348 1.1 jmcneill }
349 1.1 jmcneill
350 1.1 jmcneill sc->sc_dev = self;
351 1.1 jmcneill sc->sc_phandle = phandle;
352 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
353 1.1 jmcneill sc->sc_bst = faa->faa_bst;
354 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
355 1.1 jmcneill aprint_error(": couldn't map registers\n");
356 1.1 jmcneill return;
357 1.1 jmcneill }
358 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
359 1.1 jmcneill
360 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
361 1.1 jmcneill aprint_error(": failed to decode interrupt\n");
362 1.1 jmcneill return;
363 1.1 jmcneill }
364 1.1 jmcneill
365 1.1 jmcneill sc->sc_nchan = of_search_compatible(phandle, compat_data)->data;
366 1.1 jmcneill sc->sc_chan = kmem_alloc(sizeof(*sc->sc_chan) * sc->sc_nchan, KM_SLEEP);
367 1.1 jmcneill
368 1.1 jmcneill aprint_naive("\n");
369 1.1 jmcneill aprint_normal(": DMA controller (%u channels)\n", sc->sc_nchan);
370 1.1 jmcneill
371 1.1 jmcneill DMA_WRITE(sc, DMA_IRQ_EN_REG0_REG, 0);
372 1.1 jmcneill DMA_WRITE(sc, DMA_IRQ_EN_REG1_REG, 0);
373 1.1 jmcneill DMA_WRITE(sc, DMA_IRQ_PEND_REG0_REG, ~0);
374 1.1 jmcneill DMA_WRITE(sc, DMA_IRQ_PEND_REG1_REG, ~0);
375 1.1 jmcneill
376 1.1 jmcneill for (index = 0; index < sc->sc_nchan; index++) {
377 1.1 jmcneill struct sun6idma_channel *ch = &sc->sc_chan[index];
378 1.1 jmcneill ch->ch_index = index;
379 1.1 jmcneill ch->ch_callback = NULL;
380 1.1 jmcneill ch->ch_callbackarg = NULL;
381 1.1 jmcneill ch->ch_dmadesclen = desclen;
382 1.1 jmcneill
383 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, desclen, 0, 0,
384 1.1 jmcneill ch->ch_dmasegs, 1, &nsegs, BUS_DMA_WAITOK);
385 1.1 jmcneill if (error)
386 1.1 jmcneill panic("bus_dmamem_alloc failed: %d", error);
387 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, ch->ch_dmasegs, nsegs,
388 1.1 jmcneill desclen, &ch->ch_dmadesc, BUS_DMA_WAITOK);
389 1.1 jmcneill if (error)
390 1.1 jmcneill panic("bus_dmamem_map failed: %d", error);
391 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, desclen, 1, desclen, 0,
392 1.1 jmcneill BUS_DMA_WAITOK, &ch->ch_dmamap);
393 1.1 jmcneill if (error)
394 1.1 jmcneill panic("bus_dmamap_create failed: %d", error);
395 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, ch->ch_dmamap,
396 1.1 jmcneill ch->ch_dmadesc, desclen, NULL, BUS_DMA_WAITOK);
397 1.1 jmcneill if (error)
398 1.1 jmcneill panic("bus_dmamap_load failed: %d", error);
399 1.1 jmcneill
400 1.1 jmcneill DMA_WRITE(sc, DMA_EN_REG(index), 0);
401 1.1 jmcneill }
402 1.1 jmcneill
403 1.1 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SCHED, FDT_INTR_MPSAFE,
404 1.1 jmcneill sun6idma_intr, sc);
405 1.1 jmcneill if (sc->sc_ih == NULL) {
406 1.1 jmcneill aprint_error_dev(sc->sc_dev,
407 1.1 jmcneill "couldn't establish interrupt on %s\n", intrstr);
408 1.1 jmcneill return;
409 1.1 jmcneill }
410 1.1 jmcneill aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
411 1.1 jmcneill
412 1.1 jmcneill fdtbus_register_dma_controller(self, phandle, &sun6idma_funcs);
413 1.1 jmcneill }
414 1.1 jmcneill
415 1.1 jmcneill CFATTACH_DECL_NEW(sun6i_dma, sizeof(struct sun6idma_softc),
416 1.1 jmcneill sun6idma_match, sun6idma_attach, NULL, NULL);
417 1.2 jmcneill
418 1.2 jmcneill #ifdef DDB
419 1.2 jmcneill void sun6idma_dump(void);
420 1.2 jmcneill
421 1.2 jmcneill void
422 1.2 jmcneill sun6idma_dump(void)
423 1.2 jmcneill {
424 1.2 jmcneill struct sun6idma_softc *sc;
425 1.2 jmcneill device_t dev;
426 1.2 jmcneill u_int index;
427 1.2 jmcneill
428 1.2 jmcneill dev = device_find_by_driver_unit("sun6idma", 0);
429 1.2 jmcneill if (dev == NULL)
430 1.2 jmcneill return;
431 1.2 jmcneill sc = device_private(dev);
432 1.2 jmcneill
433 1.2 jmcneill device_printf(dev, "DMA_IRQ_EN_REG0_REG: %08x\n", DMA_READ(sc, DMA_IRQ_EN_REG0_REG));
434 1.2 jmcneill device_printf(dev, "DMA_IRQ_EN_REG1_REG: %08x\n", DMA_READ(sc, DMA_IRQ_EN_REG1_REG));
435 1.2 jmcneill device_printf(dev, "DMA_IRQ_PEND_REG0_REG: %08x\n", DMA_READ(sc, DMA_IRQ_PEND_REG0_REG));
436 1.2 jmcneill device_printf(dev, "DMA_IRQ_PEND_REG1_REG: %08x\n", DMA_READ(sc, DMA_IRQ_PEND_REG1_REG));
437 1.2 jmcneill device_printf(dev, "DMA_STA_REG: %08x\n", DMA_READ(sc, DMA_STA_REG));
438 1.2 jmcneill
439 1.2 jmcneill for (index = 0; index < sc->sc_nchan; index++) {
440 1.2 jmcneill struct sun6idma_channel *ch = &sc->sc_chan[index];
441 1.2 jmcneill if (ch->ch_callback == NULL)
442 1.2 jmcneill continue;
443 1.2 jmcneill device_printf(dev, " %2d: DMA_EN_REG: %08x\n", index, DMA_READ(sc, DMA_EN_REG(index)));
444 1.2 jmcneill device_printf(dev, " %2d: DMA_PAU_REG: %08x\n", index, DMA_READ(sc, DMA_PAU_REG(index)));
445 1.2 jmcneill device_printf(dev, " %2d: DMA_START_ADDR_REG: %08x\n", index, DMA_READ(sc, DMA_START_ADDR_REG(index)));
446 1.2 jmcneill device_printf(dev, " %2d: DMA_CFG_REG: %08x\n", index, DMA_READ(sc, DMA_CFG_REG(index)));
447 1.2 jmcneill device_printf(dev, " %2d: DMA_CUR_SRC_REG: %08x\n", index, DMA_READ(sc, DMA_CUR_SRC_REG(index)));
448 1.2 jmcneill device_printf(dev, " %2d: DMA_CUR_DEST_REG: %08x\n", index, DMA_READ(sc, DMA_CUR_DEST_REG(index)));
449 1.2 jmcneill device_printf(dev, " %2d: DMA_BCNT_LEFT_REG: %08x\n", index, DMA_READ(sc, DMA_BCNT_LEFT_REG(index)));
450 1.2 jmcneill device_printf(dev, " %2d: DMA_PARA_REG: %08x\n", index, DMA_READ(sc, DMA_PARA_REG(index)));
451 1.2 jmcneill }
452 1.2 jmcneill }
453 1.2 jmcneill #endif
454