Linux 2.6.13-rc4
[linux-2.6/next.git] / arch / arm / mach-s3c2410 / dma.c
blob65feaf20d23e842d1d46ba88ed674f4942558c7a
1 /* linux/arch/arm/mach-bast/dma.c
3 * (c) 2003-2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2410 DMA core
8 * http://www.simtec.co.uk/products/EB2410ITX/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * Changelog:
15 * 27-Feb-2005 BJD Added kmem cache for dma descriptors
16 * 18-Nov-2004 BJD Removed error for loading onto stopped channel
17 * 10-Nov-2004 BJD Ensure all external symbols exported for modules
18 * 10-Nov-2004 BJD Use sys_device and sysdev_class for power management
19 * 08-Aug-2004 BJD Apply rmk's suggestions
20 * 21-Jul-2004 BJD Ported to linux 2.6
21 * 12-Jul-2004 BJD Finished re-write and change of API
22 * 06-Jul-2004 BJD Rewrote dma code to try and cope with various problems
23 * 23-May-2003 BJD Created file
24 * 19-Aug-2003 BJD Cleanup, header fix, added URL
26 * This file is based on the Sangwook Lee/Samsung patches, re-written due
27 * to various ommisions from the code (such as flexible dma configuration)
28 * for use with the BAST system board.
30 * The re-write is pretty much complete, and should be good enough for any
31 * possible DMA function
34 #include <linux/config.h>
36 #ifdef CONFIG_S3C2410_DMA_DEBUG
37 #define DEBUG
38 #endif
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/sched.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <linux/sysdev.h>
46 #include <linux/slab.h>
47 #include <linux/errno.h>
48 #include <linux/delay.h>
50 #include <asm/system.h>
51 #include <asm/irq.h>
52 #include <asm/hardware.h>
53 #include <asm/io.h>
54 #include <asm/dma.h>
56 #include <asm/mach/dma.h>
57 #include <asm/arch/map.h>
59 /* io map for dma */
60 static void __iomem *dma_base;
61 static kmem_cache_t *dma_kmem;
63 /* dma channel state information */
64 s3c2410_dma_chan_t s3c2410_chans[S3C2410_DMA_CHANNELS];
66 /* debugging functions */
68 #define BUF_MAGIC (0xcafebabe)
70 #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
72 #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
74 #if 1
75 #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
76 #else
77 static inline void
78 dma_wrreg(s3c2410_dma_chan_t *chan, int reg, unsigned long val)
80 pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
81 writel(val, dma_regaddr(chan, reg));
84 #endif
86 #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
88 /* captured register state for debug */
90 struct s3c2410_dma_regstate {
91 unsigned long dcsrc;
92 unsigned long disrc;
93 unsigned long dstat;
94 unsigned long dcon;
95 unsigned long dmsktrig;
98 #ifdef CONFIG_S3C2410_DMA_DEBUG
100 /* dmadbg_showregs
102 * simple debug routine to print the current state of the dma registers
105 static void
106 dmadbg_capture(s3c2410_dma_chan_t *chan, struct s3c2410_dma_regstate *regs)
108 regs->dcsrc = dma_rdreg(chan, S3C2410_DMA_DCSRC);
109 regs->disrc = dma_rdreg(chan, S3C2410_DMA_DISRC);
110 regs->dstat = dma_rdreg(chan, S3C2410_DMA_DSTAT);
111 regs->dcon = dma_rdreg(chan, S3C2410_DMA_DCON);
112 regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
115 static void
116 dmadbg_showregs(const char *fname, int line, s3c2410_dma_chan_t *chan,
117 struct s3c2410_dma_regstate *regs)
119 printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
120 chan->number, fname, line,
121 regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
122 regs->dcon);
125 static void
126 dmadbg_showchan(const char *fname, int line, s3c2410_dma_chan_t *chan)
128 struct s3c2410_dma_regstate state;
130 dmadbg_capture(chan, &state);
132 printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
133 chan->number, fname, line, chan->load_state,
134 chan->curr, chan->next, chan->end);
136 dmadbg_showregs(fname, line, chan, &state);
139 #define dbg_showregs(chan) dmadbg_showregs(__FUNCTION__, __LINE__, (chan))
140 #define dbg_showchan(chan) dmadbg_showchan(__FUNCTION__, __LINE__, (chan))
141 #else
142 #define dbg_showregs(chan) do { } while(0)
143 #define dbg_showchan(chan) do { } while(0)
144 #endif /* CONFIG_S3C2410_DMA_DEBUG */
146 #define check_channel(chan) \
147 do { if ((chan) >= S3C2410_DMA_CHANNELS) { \
148 printk(KERN_ERR "%s: invalid channel %d\n", __FUNCTION__, (chan)); \
149 return -EINVAL; \
150 } } while(0)
153 /* s3c2410_dma_stats_timeout
155 * Update DMA stats from timeout info
158 static void
159 s3c2410_dma_stats_timeout(s3c2410_dma_stats_t *stats, int val)
161 if (stats == NULL)
162 return;
164 if (val > stats->timeout_longest)
165 stats->timeout_longest = val;
166 if (val < stats->timeout_shortest)
167 stats->timeout_shortest = val;
169 stats->timeout_avg += val;
172 /* s3c2410_dma_waitforload
174 * wait for the DMA engine to load a buffer, and update the state accordingly
177 static int
178 s3c2410_dma_waitforload(s3c2410_dma_chan_t *chan, int line)
180 int timeout = chan->load_timeout;
181 int took;
183 if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
184 printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
185 return 0;
188 if (chan->stats != NULL)
189 chan->stats->loads++;
191 while (--timeout > 0) {
192 if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
193 took = chan->load_timeout - timeout;
195 s3c2410_dma_stats_timeout(chan->stats, took);
197 switch (chan->load_state) {
198 case S3C2410_DMALOAD_1LOADED:
199 chan->load_state = S3C2410_DMALOAD_1RUNNING;
200 break;
202 default:
203 printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
206 return 1;
210 if (chan->stats != NULL) {
211 chan->stats->timeout_failed++;
214 return 0;
219 /* s3c2410_dma_loadbuffer
221 * load a buffer, and update the channel state
224 static inline int
225 s3c2410_dma_loadbuffer(s3c2410_dma_chan_t *chan,
226 s3c2410_dma_buf_t *buf)
228 unsigned long reload;
230 pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
231 buf, (unsigned long)buf->data, buf->size);
233 if (buf == NULL) {
234 dmawarn("buffer is NULL\n");
235 return -EINVAL;
238 /* check the state of the channel before we do anything */
240 if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
241 dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
244 if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
245 dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
248 /* it would seem sensible if we are the last buffer to not bother
249 * with the auto-reload bit, so that the DMA engine will not try
250 * and load another transfer after this one has finished...
252 if (chan->load_state == S3C2410_DMALOAD_NONE) {
253 pr_debug("load_state is none, checking for noreload (next=%p)\n",
254 buf->next);
255 reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
256 } else {
257 pr_debug("load_state is %d => autoreload\n", chan->load_state);
258 reload = S3C2410_DCON_AUTORELOAD;
261 writel(buf->data, chan->addr_reg);
263 dma_wrreg(chan, S3C2410_DMA_DCON,
264 chan->dcon | reload | (buf->size/chan->xfer_unit));
266 chan->next = buf->next;
268 /* update the state of the channel */
270 switch (chan->load_state) {
271 case S3C2410_DMALOAD_NONE:
272 chan->load_state = S3C2410_DMALOAD_1LOADED;
273 break;
275 case S3C2410_DMALOAD_1RUNNING:
276 chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
277 break;
279 default:
280 dmawarn("dmaload: unknown state %d in loadbuffer\n",
281 chan->load_state);
282 break;
285 return 0;
288 /* s3c2410_dma_call_op
290 * small routine to call the op routine with the given op if it has been
291 * registered
294 static void
295 s3c2410_dma_call_op(s3c2410_dma_chan_t *chan, s3c2410_chan_op_t op)
297 if (chan->op_fn != NULL) {
298 (chan->op_fn)(chan, op);
302 /* s3c2410_dma_buffdone
304 * small wrapper to check if callback routine needs to be called, and
305 * if so, call it
308 static inline void
309 s3c2410_dma_buffdone(s3c2410_dma_chan_t *chan, s3c2410_dma_buf_t *buf,
310 s3c2410_dma_buffresult_t result)
312 pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
313 chan->callback_fn, buf, buf->id, buf->size, result);
315 if (chan->callback_fn != NULL) {
316 (chan->callback_fn)(chan, buf->id, buf->size, result);
320 /* s3c2410_dma_start
322 * start a dma channel going
325 static int s3c2410_dma_start(s3c2410_dma_chan_t *chan)
327 unsigned long tmp;
328 unsigned long flags;
330 pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
332 local_irq_save(flags);
334 if (chan->state == S3C2410_DMA_RUNNING) {
335 pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
336 local_irq_restore(flags);
337 return 0;
340 chan->state = S3C2410_DMA_RUNNING;
342 /* check wether there is anything to load, and if not, see
343 * if we can find anything to load
346 if (chan->load_state == S3C2410_DMALOAD_NONE) {
347 if (chan->next == NULL) {
348 printk(KERN_ERR "dma%d: channel has nothing loaded\n",
349 chan->number);
350 chan->state = S3C2410_DMA_IDLE;
351 local_irq_restore(flags);
352 return -EINVAL;
355 s3c2410_dma_loadbuffer(chan, chan->next);
358 dbg_showchan(chan);
360 /* enable the channel */
362 if (!chan->irq_enabled) {
363 enable_irq(chan->irq);
364 chan->irq_enabled = 1;
367 /* start the channel going */
369 tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
370 tmp &= ~S3C2410_DMASKTRIG_STOP;
371 tmp |= S3C2410_DMASKTRIG_ON;
372 dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
374 pr_debug("wrote %08lx to DMASKTRIG\n", tmp);
376 #if 0
377 /* the dma buffer loads should take care of clearing the AUTO
378 * reloading feature */
379 tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
380 tmp &= ~S3C2410_DCON_NORELOAD;
381 dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
382 #endif
384 s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
386 dbg_showchan(chan);
388 local_irq_restore(flags);
389 return 0;
392 /* s3c2410_dma_canload
394 * work out if we can queue another buffer into the DMA engine
397 static int
398 s3c2410_dma_canload(s3c2410_dma_chan_t *chan)
400 if (chan->load_state == S3C2410_DMALOAD_NONE ||
401 chan->load_state == S3C2410_DMALOAD_1RUNNING)
402 return 1;
404 return 0;
408 /* s3c2410_dma_enqueue
410 * queue an given buffer for dma transfer.
412 * id the device driver's id information for this buffer
413 * data the physical address of the buffer data
414 * size the size of the buffer in bytes
416 * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
417 * is checked, and if set, the channel is started. If this flag isn't set,
418 * then an error will be returned.
420 * It is possible to queue more than one DMA buffer onto a channel at
421 * once, and the code will deal with the re-loading of the next buffer
422 * when necessary.
425 int s3c2410_dma_enqueue(unsigned int channel, void *id,
426 dma_addr_t data, int size)
428 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
429 s3c2410_dma_buf_t *buf;
430 unsigned long flags;
432 check_channel(channel);
434 pr_debug("%s: id=%p, data=%08x, size=%d\n",
435 __FUNCTION__, id, (unsigned int)data, size);
437 buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
438 if (buf == NULL) {
439 pr_debug("%s: out of memory (%ld alloc)\n",
440 __FUNCTION__, sizeof(*buf));
441 return -ENOMEM;
444 pr_debug("%s: new buffer %p\n", __FUNCTION__, buf);
446 //dbg_showchan(chan);
448 buf->next = NULL;
449 buf->data = buf->ptr = data;
450 buf->size = size;
451 buf->id = id;
452 buf->magic = BUF_MAGIC;
454 local_irq_save(flags);
456 if (chan->curr == NULL) {
457 /* we've got nothing loaded... */
458 pr_debug("%s: buffer %p queued onto empty channel\n",
459 __FUNCTION__, buf);
461 chan->curr = buf;
462 chan->end = buf;
463 chan->next = NULL;
464 } else {
465 pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
466 chan->number, __FUNCTION__, buf);
468 if (chan->end == NULL)
469 pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
470 chan->number, __FUNCTION__, chan);
472 chan->end->next = buf;
473 chan->end = buf;
476 /* if necessary, update the next buffer field */
477 if (chan->next == NULL)
478 chan->next = buf;
480 /* check to see if we can load a buffer */
481 if (chan->state == S3C2410_DMA_RUNNING) {
482 if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
483 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
484 printk(KERN_ERR "dma%d: loadbuffer:"
485 "timeout loading buffer\n",
486 chan->number);
487 dbg_showchan(chan);
488 local_irq_restore(flags);
489 return -EINVAL;
493 while (s3c2410_dma_canload(chan) && chan->next != NULL) {
494 s3c2410_dma_loadbuffer(chan, chan->next);
496 } else if (chan->state == S3C2410_DMA_IDLE) {
497 if (chan->flags & S3C2410_DMAF_AUTOSTART) {
498 s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_START);
502 local_irq_restore(flags);
503 return 0;
506 EXPORT_SYMBOL(s3c2410_dma_enqueue);
508 static inline void
509 s3c2410_dma_freebuf(s3c2410_dma_buf_t *buf)
511 int magicok = (buf->magic == BUF_MAGIC);
513 buf->magic = -1;
515 if (magicok) {
516 kmem_cache_free(dma_kmem, buf);
517 } else {
518 printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
522 /* s3c2410_dma_lastxfer
524 * called when the system is out of buffers, to ensure that the channel
525 * is prepared for shutdown.
528 static inline void
529 s3c2410_dma_lastxfer(s3c2410_dma_chan_t *chan)
531 pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
532 chan->number, chan->load_state);
534 switch (chan->load_state) {
535 case S3C2410_DMALOAD_NONE:
536 break;
538 case S3C2410_DMALOAD_1LOADED:
539 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
540 /* flag error? */
541 printk(KERN_ERR "dma%d: timeout waiting for load\n",
542 chan->number);
543 return;
545 break;
547 default:
548 pr_debug("dma%d: lastxfer: unhandled load_state %d with no next",
549 chan->number, chan->load_state);
550 return;
554 /* hopefully this'll shut the damned thing up after the transfer... */
555 dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
559 #define dmadbg2(x...)
561 static irqreturn_t
562 s3c2410_dma_irq(int irq, void *devpw, struct pt_regs *regs)
564 s3c2410_dma_chan_t *chan = (s3c2410_dma_chan_t *)devpw;
565 s3c2410_dma_buf_t *buf;
567 buf = chan->curr;
569 dbg_showchan(chan);
571 /* modify the channel state */
573 switch (chan->load_state) {
574 case S3C2410_DMALOAD_1RUNNING:
575 /* TODO - if we are running only one buffer, we probably
576 * want to reload here, and then worry about the buffer
577 * callback */
579 chan->load_state = S3C2410_DMALOAD_NONE;
580 break;
582 case S3C2410_DMALOAD_1LOADED:
583 /* iirc, we should go back to NONE loaded here, we
584 * had a buffer, and it was never verified as being
585 * loaded.
588 chan->load_state = S3C2410_DMALOAD_NONE;
589 break;
591 case S3C2410_DMALOAD_1LOADED_1RUNNING:
592 /* we'll worry about checking to see if another buffer is
593 * ready after we've called back the owner. This should
594 * ensure we do not wait around too long for the DMA
595 * engine to start the next transfer
598 chan->load_state = S3C2410_DMALOAD_1LOADED;
599 break;
601 case S3C2410_DMALOAD_NONE:
602 printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
603 chan->number);
604 break;
606 default:
607 printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
608 chan->number, chan->load_state);
609 break;
612 if (buf != NULL) {
613 /* update the chain to make sure that if we load any more
614 * buffers when we call the callback function, things should
615 * work properly */
617 chan->curr = buf->next;
618 buf->next = NULL;
620 if (buf->magic != BUF_MAGIC) {
621 printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
622 chan->number, __FUNCTION__, buf);
623 return IRQ_HANDLED;
626 s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
628 /* free resouces */
629 s3c2410_dma_freebuf(buf);
630 } else {
633 if (chan->next != NULL) {
634 unsigned long flags;
636 switch (chan->load_state) {
637 case S3C2410_DMALOAD_1RUNNING:
638 /* don't need to do anything for this state */
639 break;
641 case S3C2410_DMALOAD_NONE:
642 /* can load buffer immediately */
643 break;
645 case S3C2410_DMALOAD_1LOADED:
646 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
647 /* flag error? */
648 printk(KERN_ERR "dma%d: timeout waiting for load\n",
649 chan->number);
650 return IRQ_HANDLED;
653 break;
655 case S3C2410_DMALOAD_1LOADED_1RUNNING:
656 goto no_load;
658 default:
659 printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
660 chan->number, chan->load_state);
661 return IRQ_HANDLED;
664 local_irq_save(flags);
665 s3c2410_dma_loadbuffer(chan, chan->next);
666 local_irq_restore(flags);
667 } else {
668 s3c2410_dma_lastxfer(chan);
670 /* see if we can stop this channel.. */
671 if (chan->load_state == S3C2410_DMALOAD_NONE) {
672 pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
673 chan->number, jiffies);
674 s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
678 no_load:
679 return IRQ_HANDLED;
684 /* s3c2410_request_dma
686 * get control of an dma channel
689 int s3c2410_dma_request(unsigned int channel, s3c2410_dma_client_t *client,
690 void *dev)
692 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
693 unsigned long flags;
694 int err;
696 pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
697 channel, client->name, dev);
699 check_channel(channel);
701 local_irq_save(flags);
703 dbg_showchan(chan);
705 if (chan->in_use) {
706 if (client != chan->client) {
707 printk(KERN_ERR "dma%d: already in use\n", channel);
708 local_irq_restore(flags);
709 return -EBUSY;
710 } else {
711 printk(KERN_ERR "dma%d: client already has channel\n", channel);
715 chan->client = client;
716 chan->in_use = 1;
718 if (!chan->irq_claimed) {
719 pr_debug("dma%d: %s : requesting irq %d\n",
720 channel, __FUNCTION__, chan->irq);
722 err = request_irq(chan->irq, s3c2410_dma_irq, SA_INTERRUPT,
723 client->name, (void *)chan);
725 if (err) {
726 chan->in_use = 0;
727 local_irq_restore(flags);
729 printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
730 client->name, chan->irq, chan->number);
731 return err;
734 chan->irq_claimed = 1;
735 chan->irq_enabled = 1;
738 local_irq_restore(flags);
740 /* need to setup */
742 pr_debug("%s: channel initialised, %p\n", __FUNCTION__, chan);
744 return 0;
747 EXPORT_SYMBOL(s3c2410_dma_request);
749 /* s3c2410_dma_free
751 * release the given channel back to the system, will stop and flush
752 * any outstanding transfers, and ensure the channel is ready for the
753 * next claimant.
755 * Note, although a warning is currently printed if the freeing client
756 * info is not the same as the registrant's client info, the free is still
757 * allowed to go through.
760 int s3c2410_dma_free(dmach_t channel, s3c2410_dma_client_t *client)
762 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
763 unsigned long flags;
765 check_channel(channel);
767 local_irq_save(flags);
770 if (chan->client != client) {
771 printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
772 channel, chan->client, client);
775 /* sort out stopping and freeing the channel */
777 if (chan->state != S3C2410_DMA_IDLE) {
778 pr_debug("%s: need to stop dma channel %p\n",
779 __FUNCTION__, chan);
781 /* possibly flush the channel */
782 s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
785 chan->client = NULL;
786 chan->in_use = 0;
788 if (chan->irq_claimed)
789 free_irq(chan->irq, (void *)chan);
790 chan->irq_claimed = 0;
792 local_irq_restore(flags);
794 return 0;
797 EXPORT_SYMBOL(s3c2410_dma_free);
799 static int s3c2410_dma_dostop(s3c2410_dma_chan_t *chan)
801 unsigned long tmp;
802 unsigned long flags;
804 pr_debug("%s:\n", __FUNCTION__);
806 dbg_showchan(chan);
808 local_irq_save(flags);
810 s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
812 tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
813 tmp |= S3C2410_DMASKTRIG_STOP;
814 dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
816 #if 0
817 /* should also clear interrupts, according to WinCE BSP */
818 tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
819 tmp |= S3C2410_DCON_NORELOAD;
820 dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
821 #endif
823 chan->state = S3C2410_DMA_IDLE;
824 chan->load_state = S3C2410_DMALOAD_NONE;
826 local_irq_restore(flags);
828 return 0;
831 /* s3c2410_dma_flush
833 * stop the channel, and remove all current and pending transfers
836 static int s3c2410_dma_flush(s3c2410_dma_chan_t *chan)
838 s3c2410_dma_buf_t *buf, *next;
839 unsigned long flags;
841 pr_debug("%s:\n", __FUNCTION__);
843 local_irq_save(flags);
845 if (chan->state != S3C2410_DMA_IDLE) {
846 pr_debug("%s: stopping channel...\n", __FUNCTION__ );
847 s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
850 buf = chan->curr;
851 if (buf == NULL)
852 buf = chan->next;
854 chan->curr = chan->next = chan->end = NULL;
856 if (buf != NULL) {
857 for ( ; buf != NULL; buf = next) {
858 next = buf->next;
860 pr_debug("%s: free buffer %p, next %p\n",
861 __FUNCTION__, buf, buf->next);
863 s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
864 s3c2410_dma_freebuf(buf);
868 local_irq_restore(flags);
870 return 0;
875 s3c2410_dma_ctrl(dmach_t channel, s3c2410_chan_op_t op)
877 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
879 check_channel(channel);
881 switch (op) {
882 case S3C2410_DMAOP_START:
883 return s3c2410_dma_start(chan);
885 case S3C2410_DMAOP_STOP:
886 return s3c2410_dma_dostop(chan);
888 case S3C2410_DMAOP_PAUSE:
889 return -ENOENT;
891 case S3C2410_DMAOP_RESUME:
892 return -ENOENT;
894 case S3C2410_DMAOP_FLUSH:
895 return s3c2410_dma_flush(chan);
897 case S3C2410_DMAOP_TIMEOUT:
898 return 0;
902 return -ENOENT; /* unknown, don't bother */
905 EXPORT_SYMBOL(s3c2410_dma_ctrl);
907 /* DMA configuration for each channel
909 * DISRCC -> source of the DMA (AHB,APB)
910 * DISRC -> source address of the DMA
911 * DIDSTC -> destination of the DMA (AHB,APD)
912 * DIDST -> destination address of the DMA
915 /* s3c2410_dma_config
917 * xfersize: size of unit in bytes (1,2,4)
918 * dcon: base value of the DCONx register
921 int s3c2410_dma_config(dmach_t channel,
922 int xferunit,
923 int dcon)
925 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
927 pr_debug("%s: chan=%d, xfer_unit=%d, dcon=%08x\n",
928 __FUNCTION__, channel, xferunit, dcon);
930 check_channel(channel);
932 switch (xferunit) {
933 case 1:
934 dcon |= S3C2410_DCON_BYTE;
935 break;
937 case 2:
938 dcon |= S3C2410_DCON_HALFWORD;
939 break;
941 case 4:
942 dcon |= S3C2410_DCON_WORD;
943 break;
945 default:
946 pr_debug("%s: bad transfer size %d\n", __FUNCTION__, xferunit);
947 return -EINVAL;
950 dcon |= S3C2410_DCON_HWTRIG;
951 dcon |= S3C2410_DCON_INTREQ;
953 pr_debug("%s: dcon now %08x\n", __FUNCTION__, dcon);
955 chan->dcon = dcon;
956 chan->xfer_unit = xferunit;
958 return 0;
961 EXPORT_SYMBOL(s3c2410_dma_config);
963 int s3c2410_dma_setflags(dmach_t channel, unsigned int flags)
965 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
967 check_channel(channel);
969 pr_debug("%s: chan=%p, flags=%08x\n", __FUNCTION__, chan, flags);
971 chan->flags = flags;
973 return 0;
976 EXPORT_SYMBOL(s3c2410_dma_setflags);
979 /* do we need to protect the settings of the fields from
980 * irq?
983 int s3c2410_dma_set_opfn(dmach_t channel, s3c2410_dma_opfn_t rtn)
985 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
987 check_channel(channel);
989 pr_debug("%s: chan=%p, op rtn=%p\n", __FUNCTION__, chan, rtn);
991 chan->op_fn = rtn;
993 return 0;
996 EXPORT_SYMBOL(s3c2410_dma_set_opfn);
998 int s3c2410_dma_set_buffdone_fn(dmach_t channel, s3c2410_dma_cbfn_t rtn)
1000 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
1002 check_channel(channel);
1004 pr_debug("%s: chan=%p, callback rtn=%p\n", __FUNCTION__, chan, rtn);
1006 chan->callback_fn = rtn;
1008 return 0;
1011 EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);
1013 /* s3c2410_dma_devconfig
1015 * configure the dma source/destination hardware type and address
1017 * source: S3C2410_DMASRC_HW: source is hardware
1018 * S3C2410_DMASRC_MEM: source is memory
1020 * hwcfg: the value for xxxSTCn register,
1021 * bit 0: 0=increment pointer, 1=leave pointer
1022 * bit 1: 0=soucre is AHB, 1=soucre is APB
1024 * devaddr: physical address of the source
1027 int s3c2410_dma_devconfig(int channel,
1028 s3c2410_dmasrc_t source,
1029 int hwcfg,
1030 unsigned long devaddr)
1032 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
1034 check_channel(channel);
1036 pr_debug("%s: source=%d, hwcfg=%08x, devaddr=%08lx\n",
1037 __FUNCTION__, (int)source, hwcfg, devaddr);
1039 chan->source = source;
1040 chan->dev_addr = devaddr;
1042 switch (source) {
1043 case S3C2410_DMASRC_HW:
1044 /* source is hardware */
1045 pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
1046 __FUNCTION__, devaddr, hwcfg);
1047 dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
1048 dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
1049 dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
1051 chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
1052 return 0;
1054 case S3C2410_DMASRC_MEM:
1055 /* source is memory */
1056 pr_debug( "%s: mem source, devaddr=%08lx, hwcfg=%d\n",
1057 __FUNCTION__, devaddr, hwcfg);
1058 dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
1059 dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
1060 dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
1062 chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
1063 return 0;
1066 printk(KERN_ERR "dma%d: invalid source type (%d)\n", channel, source);
1067 return -EINVAL;
1070 EXPORT_SYMBOL(s3c2410_dma_devconfig);
1072 /* s3c2410_dma_getposition
1074 * returns the current transfer points for the dma source and destination
1077 int s3c2410_dma_getposition(dmach_t channel, dma_addr_t *src, dma_addr_t *dst)
1079 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
1081 check_channel(channel);
1083 if (src != NULL)
1084 *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
1086 if (dst != NULL)
1087 *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
1089 return 0;
1092 EXPORT_SYMBOL(s3c2410_dma_getposition);
1095 /* system device class */
1097 #ifdef CONFIG_PM
1099 static int s3c2410_dma_suspend(struct sys_device *dev, pm_message_t state)
1101 s3c2410_dma_chan_t *cp = container_of(dev, s3c2410_dma_chan_t, dev);
1103 printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
1105 if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
1106 /* the dma channel is still working, which is probably
1107 * a bad thing to do over suspend/resume. We stop the
1108 * channel and assume that the client is either going to
1109 * retry after resume, or that it is broken.
1112 printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
1113 cp->number);
1115 s3c2410_dma_dostop(cp);
1118 return 0;
1121 static int s3c2410_dma_resume(struct sys_device *dev)
1123 return 0;
1126 #else
1127 #define s3c2410_dma_suspend NULL
1128 #define s3c2410_dma_resume NULL
1129 #endif /* CONFIG_PM */
1131 static struct sysdev_class dma_sysclass = {
1132 set_kset_name("s3c24xx-dma"),
1133 .suspend = s3c2410_dma_suspend,
1134 .resume = s3c2410_dma_resume,
1137 /* kmem cache implementation */
1139 static void s3c2410_dma_cache_ctor(void *p, kmem_cache_t *c, unsigned long f)
1141 memset(p, 0, sizeof(s3c2410_dma_buf_t));
1145 /* initialisation code */
1147 static int __init s3c2410_init_dma(void)
1149 s3c2410_dma_chan_t *cp;
1150 int channel;
1151 int ret;
1153 printk("S3C2410 DMA Driver, (c) 2003-2004 Simtec Electronics\n");
1155 dma_base = ioremap(S3C2410_PA_DMA, 0x200);
1156 if (dma_base == NULL) {
1157 printk(KERN_ERR "dma failed to remap register block\n");
1158 return -ENOMEM;
1161 ret = sysdev_class_register(&dma_sysclass);
1162 if (ret != 0) {
1163 printk(KERN_ERR "dma sysclass registration failed\n");
1164 goto err;
1167 dma_kmem = kmem_cache_create("dma_desc", sizeof(s3c2410_dma_buf_t), 0,
1168 SLAB_HWCACHE_ALIGN,
1169 s3c2410_dma_cache_ctor, NULL);
1171 if (dma_kmem == NULL) {
1172 printk(KERN_ERR "dma failed to make kmem cache\n");
1173 ret = -ENOMEM;
1174 goto err;
1177 for (channel = 0; channel < S3C2410_DMA_CHANNELS; channel++) {
1178 cp = &s3c2410_chans[channel];
1180 memset(cp, 0, sizeof(s3c2410_dma_chan_t));
1182 /* dma channel irqs are in order.. */
1183 cp->number = channel;
1184 cp->irq = channel + IRQ_DMA0;
1185 cp->regs = dma_base + (channel*0x40);
1187 /* point current stats somewhere */
1188 cp->stats = &cp->stats_store;
1189 cp->stats_store.timeout_shortest = LONG_MAX;
1191 /* basic channel configuration */
1193 cp->load_timeout = 1<<18;
1195 /* register system device */
1197 cp->dev.cls = &dma_sysclass;
1198 cp->dev.id = channel;
1199 ret = sysdev_register(&cp->dev);
1201 printk("DMA channel %d at %p, irq %d\n",
1202 cp->number, cp->regs, cp->irq);
1205 return 0;
1207 err:
1208 kmem_cache_destroy(dma_kmem);
1209 iounmap(dma_base);
1210 dma_base = NULL;
1211 return ret;
1214 __initcall(s3c2410_init_dma);