dmaengine: omap-dma: provide register definitions
[deliverable/linux.git] / drivers / dma / omap-dma.c
CommitLineData
7bedaa55
RK
1/*
2 * OMAP DMAengine support
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
fa3ad86a 8#include <linux/delay.h>
7bedaa55
RK
9#include <linux/dmaengine.h>
10#include <linux/dma-mapping.h>
11#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/list.h>
15#include <linux/module.h>
16#include <linux/omap-dma.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/spinlock.h>
8d30662a
JH
20#include <linux/of_dma.h>
21#include <linux/of_device.h>
7bedaa55
RK
22
23#include "virt-dma.h"
7d7e1eba 24
7bedaa55
RK
25struct omap_dmadev {
26 struct dma_device ddev;
27 spinlock_t lock;
28 struct tasklet_struct task;
29 struct list_head pending;
1b416c4b 30 struct omap_system_dma_plat_info *plat;
7bedaa55
RK
31};
32
33struct omap_chan {
34 struct virt_dma_chan vc;
35 struct list_head node;
1b416c4b 36 struct omap_system_dma_plat_info *plat;
7bedaa55
RK
37
38 struct dma_slave_config cfg;
39 unsigned dma_sig;
3a774ea9 40 bool cyclic;
2dcdf570 41 bool paused;
7bedaa55
RK
42
43 int dma_ch;
44 struct omap_desc *desc;
45 unsigned sgidx;
46};
47
48struct omap_sg {
49 dma_addr_t addr;
50 uint32_t en; /* number of elements (24-bit) */
51 uint32_t fn; /* number of frames (16-bit) */
52};
53
54struct omap_desc {
55 struct virt_dma_desc vd;
56 enum dma_transfer_direction dir;
57 dma_addr_t dev_addr;
58
7c836bc7 59 int16_t fi; /* for OMAP_DMA_SYNC_PACKET */
9043826d 60 uint8_t es; /* CSDP_DATA_TYPE_xxx */
3ed4d18f 61 uint32_t ccr; /* CCR value */
fa3ad86a 62 uint16_t cicr; /* CICR value */
2f0d13bd 63 uint32_t csdp; /* CSDP value */
7bedaa55
RK
64
65 unsigned sglen;
66 struct omap_sg sg[0];
67};
68
9043826d
RK
69enum {
70 CCR_FS = BIT(5),
71 CCR_READ_PRIORITY = BIT(6),
72 CCR_ENABLE = BIT(7),
73 CCR_AUTO_INIT = BIT(8), /* OMAP1 only */
74 CCR_REPEAT = BIT(9), /* OMAP1 only */
75 CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */
76 CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */
77 CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */
78 CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */
79 CCR_SRC_AMODE_CONSTANT = 0 << 12,
80 CCR_SRC_AMODE_POSTINC = 1 << 12,
81 CCR_SRC_AMODE_SGLIDX = 2 << 12,
82 CCR_SRC_AMODE_DBLIDX = 3 << 12,
83 CCR_DST_AMODE_CONSTANT = 0 << 14,
84 CCR_DST_AMODE_POSTINC = 1 << 14,
85 CCR_DST_AMODE_SGLIDX = 2 << 14,
86 CCR_DST_AMODE_DBLIDX = 3 << 14,
87 CCR_CONSTANT_FILL = BIT(16),
88 CCR_TRANSPARENT_COPY = BIT(17),
89 CCR_BS = BIT(18),
90 CCR_SUPERVISOR = BIT(22),
91 CCR_PREFETCH = BIT(23),
92 CCR_TRIGGER_SRC = BIT(24),
93 CCR_BUFFERING_DISABLE = BIT(25),
94 CCR_WRITE_PRIORITY = BIT(26),
95 CCR_SYNC_ELEMENT = 0,
96 CCR_SYNC_FRAME = CCR_FS,
97 CCR_SYNC_BLOCK = CCR_BS,
98 CCR_SYNC_PACKET = CCR_BS | CCR_FS,
99
100 CSDP_DATA_TYPE_8 = 0,
101 CSDP_DATA_TYPE_16 = 1,
102 CSDP_DATA_TYPE_32 = 2,
103 CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */
104 CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */
105 CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */
106 CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */
107 CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */
108 CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */
109 CSDP_SRC_PACKED = BIT(6),
110 CSDP_SRC_BURST_1 = 0 << 7,
111 CSDP_SRC_BURST_16 = 1 << 7,
112 CSDP_SRC_BURST_32 = 2 << 7,
113 CSDP_SRC_BURST_64 = 3 << 7,
114 CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */
115 CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */
116 CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */
117 CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */
118 CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */
119 CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */
120 CSDP_DST_PACKED = BIT(13),
121 CSDP_DST_BURST_1 = 0 << 14,
122 CSDP_DST_BURST_16 = 1 << 14,
123 CSDP_DST_BURST_32 = 2 << 14,
124 CSDP_DST_BURST_64 = 3 << 14,
125
126 CICR_TOUT_IE = BIT(0), /* OMAP1 only */
127 CICR_DROP_IE = BIT(1),
128 CICR_HALF_IE = BIT(2),
129 CICR_FRAME_IE = BIT(3),
130 CICR_LAST_IE = BIT(4),
131 CICR_BLOCK_IE = BIT(5),
132 CICR_PKT_IE = BIT(7), /* OMAP2+ only */
133 CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */
134 CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */
135 CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */
136 CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */
137 CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */
138
139 CLNK_CTRL_ENABLE_LNK = BIT(15),
140};
141
7bedaa55 142static const unsigned es_bytes[] = {
9043826d
RK
143 [CSDP_DATA_TYPE_8] = 1,
144 [CSDP_DATA_TYPE_16] = 2,
145 [CSDP_DATA_TYPE_32] = 4,
7bedaa55
RK
146};
147
8d30662a
JH
148static struct of_dma_filter_info omap_dma_info = {
149 .filter_fn = omap_dma_filter_fn,
150};
151
7bedaa55
RK
152static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
153{
154 return container_of(d, struct omap_dmadev, ddev);
155}
156
157static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
158{
159 return container_of(c, struct omap_chan, vc.chan);
160}
161
162static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
163{
164 return container_of(t, struct omap_desc, vd.tx);
165}
166
167static void omap_dma_desc_free(struct virt_dma_desc *vd)
168{
169 kfree(container_of(vd, struct omap_desc, vd));
170}
171
fa3ad86a
RK
172static void omap_dma_start(struct omap_chan *c, struct omap_desc *d)
173{
174 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
175 uint32_t val;
176
177 if (__dma_omap15xx(od->plat->dma_attr))
178 c->plat->dma_write(0, CPC, c->dma_ch);
179 else
180 c->plat->dma_write(0, CDAC, c->dma_ch);
181
182 if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) {
183 val = c->plat->dma_read(CLNK_CTRL, c->dma_ch);
184
185 if (dma_omap1())
186 val &= ~(1 << 14);
187
9043826d 188 val |= c->dma_ch | CLNK_CTRL_ENABLE_LNK;
fa3ad86a
RK
189
190 c->plat->dma_write(val, CLNK_CTRL, c->dma_ch);
191 } else if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS)
192 c->plat->dma_write(c->dma_ch, CLNK_CTRL, c->dma_ch);
193
194 /* Clear CSR */
195 if (dma_omap1())
196 c->plat->dma_read(CSR, c->dma_ch);
197 else
198 c->plat->dma_write(~0, CSR, c->dma_ch);
199
200 /* Enable interrupts */
201 c->plat->dma_write(d->cicr, CICR, c->dma_ch);
202
203 val = c->plat->dma_read(CCR, c->dma_ch);
204 if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
9043826d
RK
205 val |= CCR_BUFFERING_DISABLE;
206 val |= CCR_ENABLE;
fa3ad86a
RK
207 mb();
208 c->plat->dma_write(val, CCR, c->dma_ch);
209}
210
211static void omap_dma_stop(struct omap_chan *c)
212{
213 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
214 uint32_t val;
215
216 /* disable irq */
217 c->plat->dma_write(0, CICR, c->dma_ch);
218
219 /* Clear CSR */
220 if (dma_omap1())
221 c->plat->dma_read(CSR, c->dma_ch);
222 else
223 c->plat->dma_write(~0, CSR, c->dma_ch);
224
225 val = c->plat->dma_read(CCR, c->dma_ch);
9043826d 226 if (od->plat->errata & DMA_ERRATA_i541 && val & CCR_TRIGGER_SRC) {
fa3ad86a
RK
227 uint32_t sysconfig;
228 unsigned i;
229
230 sysconfig = c->plat->dma_read(OCP_SYSCONFIG, c->dma_ch);
231 val = sysconfig & ~DMA_SYSCONFIG_MIDLEMODE_MASK;
232 val |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
233 c->plat->dma_write(val, OCP_SYSCONFIG, c->dma_ch);
234
235 val = c->plat->dma_read(CCR, c->dma_ch);
9043826d 236 val &= ~CCR_ENABLE;
fa3ad86a
RK
237 c->plat->dma_write(val, CCR, c->dma_ch);
238
239 /* Wait for sDMA FIFO to drain */
240 for (i = 0; ; i++) {
241 val = c->plat->dma_read(CCR, c->dma_ch);
9043826d 242 if (!(val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE)))
fa3ad86a
RK
243 break;
244
245 if (i > 100)
246 break;
247
248 udelay(5);
249 }
250
9043826d 251 if (val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE))
fa3ad86a
RK
252 dev_err(c->vc.chan.device->dev,
253 "DMA drain did not complete on lch %d\n",
254 c->dma_ch);
255
256 c->plat->dma_write(sysconfig, OCP_SYSCONFIG, c->dma_ch);
257 } else {
9043826d 258 val &= ~CCR_ENABLE;
fa3ad86a
RK
259 c->plat->dma_write(val, CCR, c->dma_ch);
260 }
261
262 mb();
263
264 if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) {
265 val = c->plat->dma_read(CLNK_CTRL, c->dma_ch);
266
267 if (dma_omap1())
268 val |= 1 << 14; /* set the STOP_LNK bit */
269 else
9043826d 270 val &= ~CLNK_CTRL_ENABLE_LNK;
fa3ad86a
RK
271
272 c->plat->dma_write(val, CLNK_CTRL, c->dma_ch);
273 }
274}
275
7bedaa55
RK
276static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d,
277 unsigned idx)
278{
279 struct omap_sg *sg = d->sg + idx;
913a2d0c
RK
280
281 if (d->dir == DMA_DEV_TO_MEM) {
282 c->plat->dma_write(sg->addr, CDSA, c->dma_ch);
283 c->plat->dma_write(0, CDEI, c->dma_ch);
284 c->plat->dma_write(0, CDFI, c->dma_ch);
285 } else {
286 c->plat->dma_write(sg->addr, CSSA, c->dma_ch);
287 c->plat->dma_write(0, CSEI, c->dma_ch);
288 c->plat->dma_write(0, CSFI, c->dma_ch);
289 }
290
291 c->plat->dma_write(sg->en, CEN, c->dma_ch);
292 c->plat->dma_write(sg->fn, CFN, c->dma_ch);
293
fa3ad86a 294 omap_dma_start(c, d);
913a2d0c
RK
295}
296
297static void omap_dma_start_desc(struct omap_chan *c)
298{
299 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
300 struct omap_desc *d;
b9e97822 301
913a2d0c
RK
302 if (!vd) {
303 c->desc = NULL;
304 return;
305 }
306
307 list_del(&vd->node);
308
309 c->desc = d = to_omap_dma_desc(&vd->tx);
310 c->sgidx = 0;
311
3ed4d18f
RK
312 c->plat->dma_write(d->ccr, CCR, c->dma_ch);
313 if (dma_omap1())
314 c->plat->dma_write(d->ccr >> 16, CCR2, c->dma_ch);
b9e97822 315
3ed4d18f 316 if (d->dir == DMA_DEV_TO_MEM) {
913a2d0c
RK
317 c->plat->dma_write(d->dev_addr, CSSA, c->dma_ch);
318 c->plat->dma_write(0, CSEI, c->dma_ch);
319 c->plat->dma_write(d->fi, CSFI, c->dma_ch);
b9e97822 320 } else {
913a2d0c
RK
321 c->plat->dma_write(d->dev_addr, CDSA, c->dma_ch);
322 c->plat->dma_write(0, CDEI, c->dma_ch);
323 c->plat->dma_write(d->fi, CDFI, c->dma_ch);
b9e97822
RK
324 }
325
2f0d13bd 326 c->plat->dma_write(d->csdp, CSDP, c->dma_ch);
b9e97822 327
7bedaa55
RK
328 omap_dma_start_sg(c, d, 0);
329}
330
331static void omap_dma_callback(int ch, u16 status, void *data)
332{
333 struct omap_chan *c = data;
334 struct omap_desc *d;
335 unsigned long flags;
336
337 spin_lock_irqsave(&c->vc.lock, flags);
338 d = c->desc;
339 if (d) {
3a774ea9
RK
340 if (!c->cyclic) {
341 if (++c->sgidx < d->sglen) {
342 omap_dma_start_sg(c, d, c->sgidx);
343 } else {
344 omap_dma_start_desc(c);
345 vchan_cookie_complete(&d->vd);
346 }
7bedaa55 347 } else {
3a774ea9 348 vchan_cyclic_callback(&d->vd);
7bedaa55
RK
349 }
350 }
351 spin_unlock_irqrestore(&c->vc.lock, flags);
352}
353
354/*
355 * This callback schedules all pending channels. We could be more
356 * clever here by postponing allocation of the real DMA channels to
357 * this point, and freeing them when our virtual channel becomes idle.
358 *
359 * We would then need to deal with 'all channels in-use'
360 */
361static void omap_dma_sched(unsigned long data)
362{
363 struct omap_dmadev *d = (struct omap_dmadev *)data;
364 LIST_HEAD(head);
365
366 spin_lock_irq(&d->lock);
367 list_splice_tail_init(&d->pending, &head);
368 spin_unlock_irq(&d->lock);
369
370 while (!list_empty(&head)) {
371 struct omap_chan *c = list_first_entry(&head,
372 struct omap_chan, node);
373
374 spin_lock_irq(&c->vc.lock);
375 list_del_init(&c->node);
376 omap_dma_start_desc(c);
377 spin_unlock_irq(&c->vc.lock);
378 }
379}
380
381static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
382{
383 struct omap_chan *c = to_omap_dma_chan(chan);
384
9e2f7d82 385 dev_dbg(c->vc.chan.device->dev, "allocating channel for %u\n", c->dma_sig);
7bedaa55
RK
386
387 return omap_request_dma(c->dma_sig, "DMA engine",
388 omap_dma_callback, c, &c->dma_ch);
389}
390
391static void omap_dma_free_chan_resources(struct dma_chan *chan)
392{
393 struct omap_chan *c = to_omap_dma_chan(chan);
394
395 vchan_free_chan_resources(&c->vc);
396 omap_free_dma(c->dma_ch);
397
9e2f7d82 398 dev_dbg(c->vc.chan.device->dev, "freeing channel for %u\n", c->dma_sig);
7bedaa55
RK
399}
400
3850e22f
RK
401static size_t omap_dma_sg_size(struct omap_sg *sg)
402{
403 return sg->en * sg->fn;
404}
405
406static size_t omap_dma_desc_size(struct omap_desc *d)
407{
408 unsigned i;
409 size_t size;
410
411 for (size = i = 0; i < d->sglen; i++)
412 size += omap_dma_sg_size(&d->sg[i]);
413
414 return size * es_bytes[d->es];
415}
416
417static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
418{
419 unsigned i;
420 size_t size, es_size = es_bytes[d->es];
421
422 for (size = i = 0; i < d->sglen; i++) {
423 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
424
425 if (size)
426 size += this_size;
427 else if (addr >= d->sg[i].addr &&
428 addr < d->sg[i].addr + this_size)
429 size += d->sg[i].addr + this_size - addr;
430 }
431 return size;
432}
433
3997cab3
RK
434static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c)
435{
436 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
437 dma_addr_t addr;
438
439 if (__dma_omap15xx(od->plat->dma_attr))
440 addr = c->plat->dma_read(CPC, c->dma_ch);
441 else
442 addr = c->plat->dma_read(CSAC, c->dma_ch);
443
444 if (od->plat->errata & DMA_ERRATA_3_3 && addr == 0)
445 addr = c->plat->dma_read(CSAC, c->dma_ch);
446
447 if (!__dma_omap15xx(od->plat->dma_attr)) {
448 /*
449 * CDAC == 0 indicates that the DMA transfer on the channel has
450 * not been started (no data has been transferred so far).
451 * Return the programmed source start address in this case.
452 */
453 if (c->plat->dma_read(CDAC, c->dma_ch))
454 addr = c->plat->dma_read(CSAC, c->dma_ch);
455 else
456 addr = c->plat->dma_read(CSSA, c->dma_ch);
457 }
458
459 if (dma_omap1())
460 addr |= c->plat->dma_read(CSSA, c->dma_ch) & 0xffff0000;
461
462 return addr;
463}
464
465static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c)
466{
467 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
468 dma_addr_t addr;
469
470 if (__dma_omap15xx(od->plat->dma_attr))
471 addr = c->plat->dma_read(CPC, c->dma_ch);
472 else
473 addr = c->plat->dma_read(CDAC, c->dma_ch);
474
475 /*
476 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
477 * read before the DMA controller finished disabling the channel.
478 */
479 if (!__dma_omap15xx(od->plat->dma_attr) && addr == 0) {
480 addr = c->plat->dma_read(CDAC, c->dma_ch);
481 /*
482 * CDAC == 0 indicates that the DMA transfer on the channel has
483 * not been started (no data has been transferred so far).
484 * Return the programmed destination start address in this case.
485 */
486 if (addr == 0)
487 addr = c->plat->dma_read(CDSA, c->dma_ch);
488 }
489
490 if (dma_omap1())
491 addr |= c->plat->dma_read(CDSA, c->dma_ch) & 0xffff0000;
492
493 return addr;
494}
495
7bedaa55
RK
496static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
497 dma_cookie_t cookie, struct dma_tx_state *txstate)
498{
3850e22f
RK
499 struct omap_chan *c = to_omap_dma_chan(chan);
500 struct virt_dma_desc *vd;
501 enum dma_status ret;
502 unsigned long flags;
503
504 ret = dma_cookie_status(chan, cookie, txstate);
7cce5083 505 if (ret == DMA_COMPLETE || !txstate)
3850e22f
RK
506 return ret;
507
508 spin_lock_irqsave(&c->vc.lock, flags);
509 vd = vchan_find_desc(&c->vc, cookie);
510 if (vd) {
511 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
512 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
513 struct omap_desc *d = c->desc;
514 dma_addr_t pos;
515
516 if (d->dir == DMA_MEM_TO_DEV)
3997cab3 517 pos = omap_dma_get_src_pos(c);
3850e22f 518 else if (d->dir == DMA_DEV_TO_MEM)
3997cab3 519 pos = omap_dma_get_dst_pos(c);
3850e22f
RK
520 else
521 pos = 0;
522
523 txstate->residue = omap_dma_desc_size_pos(d, pos);
524 } else {
525 txstate->residue = 0;
526 }
527 spin_unlock_irqrestore(&c->vc.lock, flags);
528
529 return ret;
7bedaa55
RK
530}
531
532static void omap_dma_issue_pending(struct dma_chan *chan)
533{
534 struct omap_chan *c = to_omap_dma_chan(chan);
535 unsigned long flags;
536
537 spin_lock_irqsave(&c->vc.lock, flags);
538 if (vchan_issue_pending(&c->vc) && !c->desc) {
76502469
PU
539 /*
540 * c->cyclic is used only by audio and in this case the DMA need
541 * to be started without delay.
542 */
543 if (!c->cyclic) {
544 struct omap_dmadev *d = to_omap_dma_dev(chan->device);
545 spin_lock(&d->lock);
546 if (list_empty(&c->node))
547 list_add_tail(&c->node, &d->pending);
548 spin_unlock(&d->lock);
549 tasklet_schedule(&d->task);
550 } else {
551 omap_dma_start_desc(c);
552 }
7bedaa55
RK
553 }
554 spin_unlock_irqrestore(&c->vc.lock, flags);
555}
556
557static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
558 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
559 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
560{
561 struct omap_chan *c = to_omap_dma_chan(chan);
562 enum dma_slave_buswidth dev_width;
563 struct scatterlist *sgent;
564 struct omap_desc *d;
565 dma_addr_t dev_addr;
3ed4d18f 566 unsigned i, j = 0, es, en, frame_bytes;
7bedaa55
RK
567 u32 burst;
568
569 if (dir == DMA_DEV_TO_MEM) {
570 dev_addr = c->cfg.src_addr;
571 dev_width = c->cfg.src_addr_width;
572 burst = c->cfg.src_maxburst;
7bedaa55
RK
573 } else if (dir == DMA_MEM_TO_DEV) {
574 dev_addr = c->cfg.dst_addr;
575 dev_width = c->cfg.dst_addr_width;
576 burst = c->cfg.dst_maxburst;
7bedaa55
RK
577 } else {
578 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
579 return NULL;
580 }
581
582 /* Bus width translates to the element size (ES) */
583 switch (dev_width) {
584 case DMA_SLAVE_BUSWIDTH_1_BYTE:
9043826d 585 es = CSDP_DATA_TYPE_8;
7bedaa55
RK
586 break;
587 case DMA_SLAVE_BUSWIDTH_2_BYTES:
9043826d 588 es = CSDP_DATA_TYPE_16;
7bedaa55
RK
589 break;
590 case DMA_SLAVE_BUSWIDTH_4_BYTES:
9043826d 591 es = CSDP_DATA_TYPE_32;
7bedaa55
RK
592 break;
593 default: /* not reached */
594 return NULL;
595 }
596
597 /* Now allocate and setup the descriptor. */
598 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
599 if (!d)
600 return NULL;
601
602 d->dir = dir;
603 d->dev_addr = dev_addr;
604 d->es = es;
3ed4d18f 605
9043826d 606 d->ccr = CCR_SYNC_FRAME;
3ed4d18f 607 if (dir == DMA_DEV_TO_MEM)
9043826d 608 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
3ed4d18f 609 else
9043826d 610 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
3ed4d18f 611
9043826d 612 d->cicr = CICR_DROP_IE | CICR_BLOCK_IE;
2f0d13bd 613 d->csdp = es;
fa3ad86a 614
2f0d13bd 615 if (dma_omap1()) {
3ed4d18f 616 if (__dma_omap16xx(od->plat->dma_attr)) {
9043826d 617 d->ccr |= CCR_OMAP31_DISABLE;
3ed4d18f
RK
618 /* Duplicate what plat-omap/dma.c does */
619 d->ccr |= c->dma_ch + 1;
620 } else {
621 d->ccr |= c->dma_sig & 0x1f;
622 }
623
9043826d 624 d->cicr |= CICR_TOUT_IE;
2f0d13bd
RK
625
626 if (dir == DMA_DEV_TO_MEM)
9043826d 627 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB;
2f0d13bd 628 else
9043826d 629 d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF;
2f0d13bd 630 } else {
3ed4d18f
RK
631 d->ccr |= (c->dma_sig & ~0x1f) << 14;
632 d->ccr |= c->dma_sig & 0x1f;
3ed4d18f
RK
633
634 if (dir == DMA_DEV_TO_MEM)
9043826d 635 d->ccr |= CCR_TRIGGER_SRC;
3ed4d18f 636
9043826d 637 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
2f0d13bd 638 }
7bedaa55
RK
639
640 /*
641 * Build our scatterlist entries: each contains the address,
642 * the number of elements (EN) in each frame, and the number of
643 * frames (FN). Number of bytes for this entry = ES * EN * FN.
644 *
645 * Burst size translates to number of elements with frame sync.
646 * Note: DMA engine defines burst to be the number of dev-width
647 * transfers.
648 */
649 en = burst;
650 frame_bytes = es_bytes[es] * en;
651 for_each_sg(sgl, sgent, sglen, i) {
652 d->sg[j].addr = sg_dma_address(sgent);
653 d->sg[j].en = en;
654 d->sg[j].fn = sg_dma_len(sgent) / frame_bytes;
655 j++;
656 }
657
658 d->sglen = j;
659
660 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
661}
662
3a774ea9
RK
663static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
664 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
ec8b5e48
PU
665 size_t period_len, enum dma_transfer_direction dir, unsigned long flags,
666 void *context)
3a774ea9 667{
fa3ad86a 668 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
3a774ea9
RK
669 struct omap_chan *c = to_omap_dma_chan(chan);
670 enum dma_slave_buswidth dev_width;
671 struct omap_desc *d;
672 dma_addr_t dev_addr;
3ed4d18f 673 unsigned es;
3a774ea9
RK
674 u32 burst;
675
676 if (dir == DMA_DEV_TO_MEM) {
677 dev_addr = c->cfg.src_addr;
678 dev_width = c->cfg.src_addr_width;
679 burst = c->cfg.src_maxburst;
3a774ea9
RK
680 } else if (dir == DMA_MEM_TO_DEV) {
681 dev_addr = c->cfg.dst_addr;
682 dev_width = c->cfg.dst_addr_width;
683 burst = c->cfg.dst_maxburst;
3a774ea9
RK
684 } else {
685 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
686 return NULL;
687 }
688
689 /* Bus width translates to the element size (ES) */
690 switch (dev_width) {
691 case DMA_SLAVE_BUSWIDTH_1_BYTE:
9043826d 692 es = CSDP_DATA_TYPE_8;
3a774ea9
RK
693 break;
694 case DMA_SLAVE_BUSWIDTH_2_BYTES:
9043826d 695 es = CSDP_DATA_TYPE_16;
3a774ea9
RK
696 break;
697 case DMA_SLAVE_BUSWIDTH_4_BYTES:
9043826d 698 es = CSDP_DATA_TYPE_32;
3a774ea9
RK
699 break;
700 default: /* not reached */
701 return NULL;
702 }
703
704 /* Now allocate and setup the descriptor. */
705 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
706 if (!d)
707 return NULL;
708
709 d->dir = dir;
710 d->dev_addr = dev_addr;
711 d->fi = burst;
712 d->es = es;
3a774ea9
RK
713 d->sg[0].addr = buf_addr;
714 d->sg[0].en = period_len / es_bytes[es];
715 d->sg[0].fn = buf_len / period_len;
716 d->sglen = 1;
3ed4d18f
RK
717
718 d->ccr = 0;
719 if (__dma_omap15xx(od->plat->dma_attr))
9043826d 720 d->ccr = CCR_AUTO_INIT | CCR_REPEAT;
3ed4d18f 721 if (dir == DMA_DEV_TO_MEM)
9043826d 722 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
3ed4d18f 723 else
9043826d 724 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
3ed4d18f 725
9043826d 726 d->cicr = CICR_DROP_IE;
fa3ad86a 727 if (flags & DMA_PREP_INTERRUPT)
9043826d 728 d->cicr |= CICR_FRAME_IE;
fa3ad86a 729
2f0d13bd
RK
730 d->csdp = es;
731
732 if (dma_omap1()) {
3ed4d18f 733 if (__dma_omap16xx(od->plat->dma_attr)) {
9043826d 734 d->ccr |= CCR_OMAP31_DISABLE;
3ed4d18f
RK
735 /* Duplicate what plat-omap/dma.c does */
736 d->ccr |= c->dma_ch + 1;
737 } else {
738 d->ccr |= c->dma_sig & 0x1f;
739 }
740
9043826d 741 d->cicr |= CICR_TOUT_IE;
2f0d13bd
RK
742
743 if (dir == DMA_DEV_TO_MEM)
9043826d 744 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI;
2f0d13bd 745 else
9043826d 746 d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF;
2f0d13bd 747 } else {
3ed4d18f
RK
748 d->ccr |= (c->dma_sig & ~0x1f) << 14;
749 d->ccr |= c->dma_sig & 0x1f;
750
751 if (burst)
9043826d
RK
752 d->ccr |= CCR_SYNC_PACKET;
753 else
754 d->ccr |= CCR_SYNC_ELEMENT;
3ed4d18f
RK
755
756 if (dir == DMA_DEV_TO_MEM)
9043826d 757 d->ccr |= CCR_TRIGGER_SRC;
3ed4d18f 758
9043826d 759 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
3a774ea9 760
9043826d 761 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
2f0d13bd
RK
762 }
763
3ed4d18f 764 c->cyclic = true;
3a774ea9 765
2dde5b90 766 return vchan_tx_prep(&c->vc, &d->vd, flags);
3a774ea9
RK
767}
768
7bedaa55
RK
769static int omap_dma_slave_config(struct omap_chan *c, struct dma_slave_config *cfg)
770{
771 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
772 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
773 return -EINVAL;
774
775 memcpy(&c->cfg, cfg, sizeof(c->cfg));
776
777 return 0;
778}
779
780static int omap_dma_terminate_all(struct omap_chan *c)
781{
782 struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device);
783 unsigned long flags;
784 LIST_HEAD(head);
785
786 spin_lock_irqsave(&c->vc.lock, flags);
787
788 /* Prevent this channel being scheduled */
789 spin_lock(&d->lock);
790 list_del_init(&c->node);
791 spin_unlock(&d->lock);
792
793 /*
794 * Stop DMA activity: we assume the callback will not be called
fa3ad86a 795 * after omap_dma_stop() returns (even if it does, it will see
7bedaa55
RK
796 * c->desc is NULL and exit.)
797 */
798 if (c->desc) {
799 c->desc = NULL;
2dcdf570
PU
800 /* Avoid stopping the dma twice */
801 if (!c->paused)
fa3ad86a 802 omap_dma_stop(c);
7bedaa55
RK
803 }
804
3a774ea9
RK
805 if (c->cyclic) {
806 c->cyclic = false;
2dcdf570 807 c->paused = false;
3a774ea9
RK
808 }
809
7bedaa55
RK
810 vchan_get_all_descriptors(&c->vc, &head);
811 spin_unlock_irqrestore(&c->vc.lock, flags);
812 vchan_dma_desc_free_list(&c->vc, &head);
813
814 return 0;
815}
816
817static int omap_dma_pause(struct omap_chan *c)
818{
2dcdf570
PU
819 /* Pause/Resume only allowed with cyclic mode */
820 if (!c->cyclic)
821 return -EINVAL;
822
823 if (!c->paused) {
fa3ad86a 824 omap_dma_stop(c);
2dcdf570
PU
825 c->paused = true;
826 }
827
828 return 0;
7bedaa55
RK
829}
830
831static int omap_dma_resume(struct omap_chan *c)
832{
2dcdf570
PU
833 /* Pause/Resume only allowed with cyclic mode */
834 if (!c->cyclic)
835 return -EINVAL;
836
837 if (c->paused) {
fa3ad86a 838 omap_dma_start(c, c->desc);
2dcdf570
PU
839 c->paused = false;
840 }
841
842 return 0;
7bedaa55
RK
843}
844
845static int omap_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
846 unsigned long arg)
847{
848 struct omap_chan *c = to_omap_dma_chan(chan);
849 int ret;
850
851 switch (cmd) {
852 case DMA_SLAVE_CONFIG:
853 ret = omap_dma_slave_config(c, (struct dma_slave_config *)arg);
854 break;
855
856 case DMA_TERMINATE_ALL:
857 ret = omap_dma_terminate_all(c);
858 break;
859
860 case DMA_PAUSE:
861 ret = omap_dma_pause(c);
862 break;
863
864 case DMA_RESUME:
865 ret = omap_dma_resume(c);
866 break;
867
868 default:
869 ret = -ENXIO;
870 break;
871 }
872
873 return ret;
874}
875
876static int omap_dma_chan_init(struct omap_dmadev *od, int dma_sig)
877{
878 struct omap_chan *c;
879
880 c = kzalloc(sizeof(*c), GFP_KERNEL);
881 if (!c)
882 return -ENOMEM;
883
1b416c4b 884 c->plat = od->plat;
7bedaa55
RK
885 c->dma_sig = dma_sig;
886 c->vc.desc_free = omap_dma_desc_free;
887 vchan_init(&c->vc, &od->ddev);
888 INIT_LIST_HEAD(&c->node);
889
890 od->ddev.chancnt++;
891
892 return 0;
893}
894
895static void omap_dma_free(struct omap_dmadev *od)
896{
897 tasklet_kill(&od->task);
898 while (!list_empty(&od->ddev.channels)) {
899 struct omap_chan *c = list_first_entry(&od->ddev.channels,
900 struct omap_chan, vc.chan.device_node);
901
902 list_del(&c->vc.chan.device_node);
903 tasklet_kill(&c->vc.task);
904 kfree(c);
905 }
7bedaa55
RK
906}
907
908static int omap_dma_probe(struct platform_device *pdev)
909{
910 struct omap_dmadev *od;
911 int rc, i;
912
104fce73 913 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
7bedaa55
RK
914 if (!od)
915 return -ENOMEM;
916
1b416c4b
RK
917 od->plat = omap_get_plat_info();
918 if (!od->plat)
919 return -EPROBE_DEFER;
920
7bedaa55 921 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
3a774ea9 922 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
7bedaa55
RK
923 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
924 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
925 od->ddev.device_tx_status = omap_dma_tx_status;
926 od->ddev.device_issue_pending = omap_dma_issue_pending;
927 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
3a774ea9 928 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
7bedaa55
RK
929 od->ddev.device_control = omap_dma_control;
930 od->ddev.dev = &pdev->dev;
931 INIT_LIST_HEAD(&od->ddev.channels);
932 INIT_LIST_HEAD(&od->pending);
933 spin_lock_init(&od->lock);
934
935 tasklet_init(&od->task, omap_dma_sched, (unsigned long)od);
936
937 for (i = 0; i < 127; i++) {
938 rc = omap_dma_chan_init(od, i);
939 if (rc) {
940 omap_dma_free(od);
941 return rc;
942 }
943 }
944
945 rc = dma_async_device_register(&od->ddev);
946 if (rc) {
947 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
948 rc);
949 omap_dma_free(od);
8d30662a
JH
950 return rc;
951 }
952
953 platform_set_drvdata(pdev, od);
954
955 if (pdev->dev.of_node) {
956 omap_dma_info.dma_cap = od->ddev.cap_mask;
957
958 /* Device-tree DMA controller registration */
959 rc = of_dma_controller_register(pdev->dev.of_node,
960 of_dma_simple_xlate, &omap_dma_info);
961 if (rc) {
962 pr_warn("OMAP-DMA: failed to register DMA controller\n");
963 dma_async_device_unregister(&od->ddev);
964 omap_dma_free(od);
965 }
7bedaa55
RK
966 }
967
968 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
969
970 return rc;
971}
972
973static int omap_dma_remove(struct platform_device *pdev)
974{
975 struct omap_dmadev *od = platform_get_drvdata(pdev);
976
8d30662a
JH
977 if (pdev->dev.of_node)
978 of_dma_controller_free(pdev->dev.of_node);
979
7bedaa55
RK
980 dma_async_device_unregister(&od->ddev);
981 omap_dma_free(od);
982
983 return 0;
984}
985
8d30662a
JH
986static const struct of_device_id omap_dma_match[] = {
987 { .compatible = "ti,omap2420-sdma", },
988 { .compatible = "ti,omap2430-sdma", },
989 { .compatible = "ti,omap3430-sdma", },
990 { .compatible = "ti,omap3630-sdma", },
991 { .compatible = "ti,omap4430-sdma", },
992 {},
993};
994MODULE_DEVICE_TABLE(of, omap_dma_match);
995
7bedaa55
RK
996static struct platform_driver omap_dma_driver = {
997 .probe = omap_dma_probe,
998 .remove = omap_dma_remove,
999 .driver = {
1000 .name = "omap-dma-engine",
1001 .owner = THIS_MODULE,
8d30662a 1002 .of_match_table = of_match_ptr(omap_dma_match),
7bedaa55
RK
1003 },
1004};
1005
1006bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
1007{
1008 if (chan->device->dev->driver == &omap_dma_driver.driver) {
1009 struct omap_chan *c = to_omap_dma_chan(chan);
1010 unsigned req = *(unsigned *)param;
1011
1012 return req == c->dma_sig;
1013 }
1014 return false;
1015}
1016EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
1017
7bedaa55
RK
1018static int omap_dma_init(void)
1019{
be1f9481 1020 return platform_driver_register(&omap_dma_driver);
7bedaa55
RK
1021}
1022subsys_initcall(omap_dma_init);
1023
1024static void __exit omap_dma_exit(void)
1025{
7bedaa55
RK
1026 platform_driver_unregister(&omap_dma_driver);
1027}
1028module_exit(omap_dma_exit);
1029
1030MODULE_AUTHOR("Russell King");
1031MODULE_LICENSE("GPL");
This page took 0.143553 seconds and 5 git commands to generate.