Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / arm / plat-s3c24xx / dma.c
blobe75dfd3ec00570745e55f9adb9c1efd84100bfa4
1 /* linux/arch/arm/plat-s3c24xx/dma.c
3 * Copyright (c) 2003-2005,2006 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2410 DMA core
8 * http://armlinux.simtec.co.uk/
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.
16 #ifdef CONFIG_S3C2410_DMA_DEBUG
17 #define DEBUG
18 #endif
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/sysdev.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/delay.h>
30 #include <asm/system.h>
31 #include <asm/irq.h>
32 #include <asm/hardware.h>
33 #include <asm/io.h>
34 #include <asm/dma.h>
36 #include <asm/mach/dma.h>
37 #include <asm/arch/map.h>
39 #include <asm/plat-s3c24xx/dma.h>
41 /* io map for dma */
42 static void __iomem *dma_base;
43 static struct kmem_cache *dma_kmem;
45 static int dma_channels;
47 static struct s3c24xx_dma_selection dma_sel;
49 /* dma channel state information */
50 struct s3c2410_dma_chan s3c2410_chans[S3C2410_DMA_CHANNELS];
52 /* debugging functions */
54 #define BUF_MAGIC (0xcafebabe)
56 #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
58 #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
60 #if 1
61 #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
62 #else
63 static inline void
64 dma_wrreg(struct s3c2410_dma_chan *chan, int reg, unsigned long val)
66 pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
67 writel(val, dma_regaddr(chan, reg));
69 #endif
71 #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
73 /* captured register state for debug */
75 struct s3c2410_dma_regstate {
76 unsigned long dcsrc;
77 unsigned long disrc;
78 unsigned long dstat;
79 unsigned long dcon;
80 unsigned long dmsktrig;
83 #ifdef CONFIG_S3C2410_DMA_DEBUG
85 /* dmadbg_showregs
87 * simple debug routine to print the current state of the dma registers
90 static void
91 dmadbg_capture(struct s3c2410_dma_chan *chan, struct s3c2410_dma_regstate *regs)
93 regs->dcsrc = dma_rdreg(chan, S3C2410_DMA_DCSRC);
94 regs->disrc = dma_rdreg(chan, S3C2410_DMA_DISRC);
95 regs->dstat = dma_rdreg(chan, S3C2410_DMA_DSTAT);
96 regs->dcon = dma_rdreg(chan, S3C2410_DMA_DCON);
97 regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
100 static void
101 dmadbg_dumpregs(const char *fname, int line, struct s3c2410_dma_chan *chan,
102 struct s3c2410_dma_regstate *regs)
104 printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
105 chan->number, fname, line,
106 regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
107 regs->dcon);
110 static void
111 dmadbg_showchan(const char *fname, int line, struct s3c2410_dma_chan *chan)
113 struct s3c2410_dma_regstate state;
115 dmadbg_capture(chan, &state);
117 printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
118 chan->number, fname, line, chan->load_state,
119 chan->curr, chan->next, chan->end);
121 dmadbg_dumpregs(fname, line, chan, &state);
124 static void
125 dmadbg_showregs(const char *fname, int line, struct s3c2410_dma_chan *chan)
127 struct s3c2410_dma_regstate state;
129 dmadbg_capture(chan, &state);
130 dmadbg_dumpregs(fname, line, chan, &state);
133 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
134 #define dbg_showregs(chan) dmadbg_showregs(__FUNCTION__, __LINE__, (chan))
135 #define dbg_showchan(chan) dmadbg_showchan(__FUNCTION__, __LINE__, (chan))
136 =======
137 #define dbg_showregs(chan) dmadbg_showregs(__func__, __LINE__, (chan))
138 #define dbg_showchan(chan) dmadbg_showchan(__func__, __LINE__, (chan))
139 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
140 #else
141 #define dbg_showregs(chan) do { } while(0)
142 #define dbg_showchan(chan) do { } while(0)
143 #endif /* CONFIG_S3C2410_DMA_DEBUG */
145 static struct s3c2410_dma_chan *dma_chan_map[DMACH_MAX];
147 /* lookup_dma_channel
149 * change the dma channel number given into a real dma channel id
152 static struct s3c2410_dma_chan *lookup_dma_channel(unsigned int channel)
154 if (channel & DMACH_LOW_LEVEL)
155 return &s3c2410_chans[channel & ~DMACH_LOW_LEVEL];
156 else
157 return dma_chan_map[channel];
160 /* s3c2410_dma_stats_timeout
162 * Update DMA stats from timeout info
165 static void
166 s3c2410_dma_stats_timeout(struct s3c2410_dma_stats *stats, int val)
168 if (stats == NULL)
169 return;
171 if (val > stats->timeout_longest)
172 stats->timeout_longest = val;
173 if (val < stats->timeout_shortest)
174 stats->timeout_shortest = val;
176 stats->timeout_avg += val;
179 /* s3c2410_dma_waitforload
181 * wait for the DMA engine to load a buffer, and update the state accordingly
184 static int
185 s3c2410_dma_waitforload(struct s3c2410_dma_chan *chan, int line)
187 int timeout = chan->load_timeout;
188 int took;
190 if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
191 printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
192 return 0;
195 if (chan->stats != NULL)
196 chan->stats->loads++;
198 while (--timeout > 0) {
199 if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
200 took = chan->load_timeout - timeout;
202 s3c2410_dma_stats_timeout(chan->stats, took);
204 switch (chan->load_state) {
205 case S3C2410_DMALOAD_1LOADED:
206 chan->load_state = S3C2410_DMALOAD_1RUNNING;
207 break;
209 default:
210 printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
213 return 1;
217 if (chan->stats != NULL) {
218 chan->stats->timeout_failed++;
221 return 0;
226 /* s3c2410_dma_loadbuffer
228 * load a buffer, and update the channel state
231 static inline int
232 s3c2410_dma_loadbuffer(struct s3c2410_dma_chan *chan,
233 struct s3c2410_dma_buf *buf)
235 unsigned long reload;
237 pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
238 buf, (unsigned long)buf->data, buf->size);
240 if (buf == NULL) {
241 dmawarn("buffer is NULL\n");
242 return -EINVAL;
245 /* check the state of the channel before we do anything */
247 if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
248 dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
251 if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
252 dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
255 /* it would seem sensible if we are the last buffer to not bother
256 * with the auto-reload bit, so that the DMA engine will not try
257 * and load another transfer after this one has finished...
259 if (chan->load_state == S3C2410_DMALOAD_NONE) {
260 pr_debug("load_state is none, checking for noreload (next=%p)\n",
261 buf->next);
262 reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
263 } else {
264 //pr_debug("load_state is %d => autoreload\n", chan->load_state);
265 reload = S3C2410_DCON_AUTORELOAD;
268 if ((buf->data & 0xf0000000) != 0x30000000) {
269 dmawarn("dmaload: buffer is %p\n", (void *)buf->data);
272 writel(buf->data, chan->addr_reg);
274 dma_wrreg(chan, S3C2410_DMA_DCON,
275 chan->dcon | reload | (buf->size/chan->xfer_unit));
277 chan->next = buf->next;
279 /* update the state of the channel */
281 switch (chan->load_state) {
282 case S3C2410_DMALOAD_NONE:
283 chan->load_state = S3C2410_DMALOAD_1LOADED;
284 break;
286 case S3C2410_DMALOAD_1RUNNING:
287 chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
288 break;
290 default:
291 dmawarn("dmaload: unknown state %d in loadbuffer\n",
292 chan->load_state);
293 break;
296 return 0;
299 /* s3c2410_dma_call_op
301 * small routine to call the op routine with the given op if it has been
302 * registered
305 static void
306 s3c2410_dma_call_op(struct s3c2410_dma_chan *chan, enum s3c2410_chan_op op)
308 if (chan->op_fn != NULL) {
309 (chan->op_fn)(chan, op);
313 /* s3c2410_dma_buffdone
315 * small wrapper to check if callback routine needs to be called, and
316 * if so, call it
319 static inline void
320 s3c2410_dma_buffdone(struct s3c2410_dma_chan *chan, struct s3c2410_dma_buf *buf,
321 enum s3c2410_dma_buffresult result)
323 #if 0
324 pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
325 chan->callback_fn, buf, buf->id, buf->size, result);
326 #endif
328 if (chan->callback_fn != NULL) {
329 (chan->callback_fn)(chan, buf->id, buf->size, result);
333 /* s3c2410_dma_start
335 * start a dma channel going
338 static int s3c2410_dma_start(struct s3c2410_dma_chan *chan)
340 unsigned long tmp;
341 unsigned long flags;
343 pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
345 local_irq_save(flags);
347 if (chan->state == S3C2410_DMA_RUNNING) {
348 pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
349 local_irq_restore(flags);
350 return 0;
353 chan->state = S3C2410_DMA_RUNNING;
355 /* check wether there is anything to load, and if not, see
356 * if we can find anything to load
359 if (chan->load_state == S3C2410_DMALOAD_NONE) {
360 if (chan->next == NULL) {
361 printk(KERN_ERR "dma%d: channel has nothing loaded\n",
362 chan->number);
363 chan->state = S3C2410_DMA_IDLE;
364 local_irq_restore(flags);
365 return -EINVAL;
368 s3c2410_dma_loadbuffer(chan, chan->next);
371 dbg_showchan(chan);
373 /* enable the channel */
375 if (!chan->irq_enabled) {
376 enable_irq(chan->irq);
377 chan->irq_enabled = 1;
380 /* start the channel going */
382 tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
383 tmp &= ~S3C2410_DMASKTRIG_STOP;
384 tmp |= S3C2410_DMASKTRIG_ON;
385 dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
387 pr_debug("dma%d: %08lx to DMASKTRIG\n", chan->number, tmp);
389 #if 0
390 /* the dma buffer loads should take care of clearing the AUTO
391 * reloading feature */
392 tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
393 tmp &= ~S3C2410_DCON_NORELOAD;
394 dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
395 #endif
397 s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
399 dbg_showchan(chan);
401 /* if we've only loaded one buffer onto the channel, then chec
402 * to see if we have another, and if so, try and load it so when
403 * the first buffer is finished, the new one will be loaded onto
404 * the channel */
406 if (chan->next != NULL) {
407 if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
409 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
410 pr_debug("%s: buff not yet loaded, no more todo\n",
411 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
412 __FUNCTION__);
413 =======
414 __func__);
415 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
416 } else {
417 chan->load_state = S3C2410_DMALOAD_1RUNNING;
418 s3c2410_dma_loadbuffer(chan, chan->next);
421 } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
422 s3c2410_dma_loadbuffer(chan, chan->next);
427 local_irq_restore(flags);
429 return 0;
432 /* s3c2410_dma_canload
434 * work out if we can queue another buffer into the DMA engine
437 static int
438 s3c2410_dma_canload(struct s3c2410_dma_chan *chan)
440 if (chan->load_state == S3C2410_DMALOAD_NONE ||
441 chan->load_state == S3C2410_DMALOAD_1RUNNING)
442 return 1;
444 return 0;
447 /* s3c2410_dma_enqueue
449 * queue an given buffer for dma transfer.
451 * id the device driver's id information for this buffer
452 * data the physical address of the buffer data
453 * size the size of the buffer in bytes
455 * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
456 * is checked, and if set, the channel is started. If this flag isn't set,
457 * then an error will be returned.
459 * It is possible to queue more than one DMA buffer onto a channel at
460 * once, and the code will deal with the re-loading of the next buffer
461 * when necessary.
464 int s3c2410_dma_enqueue(unsigned int channel, void *id,
465 dma_addr_t data, int size)
467 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
468 struct s3c2410_dma_buf *buf;
469 unsigned long flags;
471 if (chan == NULL)
472 return -EINVAL;
474 pr_debug("%s: id=%p, data=%08x, size=%d\n",
475 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
476 __FUNCTION__, id, (unsigned int)data, size);
477 =======
478 __func__, id, (unsigned int)data, size);
479 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
481 buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
482 if (buf == NULL) {
483 pr_debug("%s: out of memory (%ld alloc)\n",
484 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
485 __FUNCTION__, (long)sizeof(*buf));
486 =======
487 __func__, (long)sizeof(*buf));
488 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
489 return -ENOMEM;
492 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
493 //pr_debug("%s: new buffer %p\n", __FUNCTION__, buf);
494 =======
495 //pr_debug("%s: new buffer %p\n", __func__, buf);
496 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
497 //dbg_showchan(chan);
499 buf->next = NULL;
500 buf->data = buf->ptr = data;
501 buf->size = size;
502 buf->id = id;
503 buf->magic = BUF_MAGIC;
505 local_irq_save(flags);
507 if (chan->curr == NULL) {
508 /* we've got nothing loaded... */
509 pr_debug("%s: buffer %p queued onto empty channel\n",
510 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
511 __FUNCTION__, buf);
512 =======
513 __func__, buf);
514 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
516 chan->curr = buf;
517 chan->end = buf;
518 chan->next = NULL;
519 } else {
520 pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
521 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
522 chan->number, __FUNCTION__, buf);
523 =======
524 chan->number, __func__, buf);
525 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
527 if (chan->end == NULL)
528 pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
529 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
530 chan->number, __FUNCTION__, chan);
531 =======
532 chan->number, __func__, chan);
533 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
535 chan->end->next = buf;
536 chan->end = buf;
539 /* if necessary, update the next buffer field */
540 if (chan->next == NULL)
541 chan->next = buf;
543 /* check to see if we can load a buffer */
544 if (chan->state == S3C2410_DMA_RUNNING) {
545 if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
546 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
547 printk(KERN_ERR "dma%d: loadbuffer:"
548 "timeout loading buffer\n",
549 chan->number);
550 dbg_showchan(chan);
551 local_irq_restore(flags);
552 return -EINVAL;
556 while (s3c2410_dma_canload(chan) && chan->next != NULL) {
557 s3c2410_dma_loadbuffer(chan, chan->next);
559 } else if (chan->state == S3C2410_DMA_IDLE) {
560 if (chan->flags & S3C2410_DMAF_AUTOSTART) {
561 s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
562 S3C2410_DMAOP_START);
566 local_irq_restore(flags);
567 return 0;
570 EXPORT_SYMBOL(s3c2410_dma_enqueue);
572 static inline void
573 s3c2410_dma_freebuf(struct s3c2410_dma_buf *buf)
575 int magicok = (buf->magic == BUF_MAGIC);
577 buf->magic = -1;
579 if (magicok) {
580 kmem_cache_free(dma_kmem, buf);
581 } else {
582 printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
586 /* s3c2410_dma_lastxfer
588 * called when the system is out of buffers, to ensure that the channel
589 * is prepared for shutdown.
592 static inline void
593 s3c2410_dma_lastxfer(struct s3c2410_dma_chan *chan)
595 #if 0
596 pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
597 chan->number, chan->load_state);
598 #endif
600 switch (chan->load_state) {
601 case S3C2410_DMALOAD_NONE:
602 break;
604 case S3C2410_DMALOAD_1LOADED:
605 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
606 /* flag error? */
607 printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
608 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
609 chan->number, __FUNCTION__);
610 =======
611 chan->number, __func__);
612 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
613 return;
615 break;
617 case S3C2410_DMALOAD_1LOADED_1RUNNING:
618 /* I belive in this case we do not have anything to do
619 * until the next buffer comes along, and we turn off the
620 * reload */
621 return;
623 default:
624 pr_debug("dma%d: lastxfer: unhandled load_state %d with no next\n",
625 chan->number, chan->load_state);
626 return;
630 /* hopefully this'll shut the damned thing up after the transfer... */
631 dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
635 #define dmadbg2(x...)
637 static irqreturn_t
638 s3c2410_dma_irq(int irq, void *devpw)
640 struct s3c2410_dma_chan *chan = (struct s3c2410_dma_chan *)devpw;
641 struct s3c2410_dma_buf *buf;
643 buf = chan->curr;
645 dbg_showchan(chan);
647 /* modify the channel state */
649 switch (chan->load_state) {
650 case S3C2410_DMALOAD_1RUNNING:
651 /* TODO - if we are running only one buffer, we probably
652 * want to reload here, and then worry about the buffer
653 * callback */
655 chan->load_state = S3C2410_DMALOAD_NONE;
656 break;
658 case S3C2410_DMALOAD_1LOADED:
659 /* iirc, we should go back to NONE loaded here, we
660 * had a buffer, and it was never verified as being
661 * loaded.
664 chan->load_state = S3C2410_DMALOAD_NONE;
665 break;
667 case S3C2410_DMALOAD_1LOADED_1RUNNING:
668 /* we'll worry about checking to see if another buffer is
669 * ready after we've called back the owner. This should
670 * ensure we do not wait around too long for the DMA
671 * engine to start the next transfer
674 chan->load_state = S3C2410_DMALOAD_1LOADED;
675 break;
677 case S3C2410_DMALOAD_NONE:
678 printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
679 chan->number);
680 break;
682 default:
683 printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
684 chan->number, chan->load_state);
685 break;
688 if (buf != NULL) {
689 /* update the chain to make sure that if we load any more
690 * buffers when we call the callback function, things should
691 * work properly */
693 chan->curr = buf->next;
694 buf->next = NULL;
696 if (buf->magic != BUF_MAGIC) {
697 printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
698 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
699 chan->number, __FUNCTION__, buf);
700 =======
701 chan->number, __func__, buf);
702 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
703 return IRQ_HANDLED;
706 s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
708 /* free resouces */
709 s3c2410_dma_freebuf(buf);
710 } else {
713 /* only reload if the channel is still running... our buffer done
714 * routine may have altered the state by requesting the dma channel
715 * to stop or shutdown... */
717 /* todo: check that when the channel is shut-down from inside this
718 * function, we cope with unsetting reload, etc */
720 if (chan->next != NULL && chan->state != S3C2410_DMA_IDLE) {
721 unsigned long flags;
723 switch (chan->load_state) {
724 case S3C2410_DMALOAD_1RUNNING:
725 /* don't need to do anything for this state */
726 break;
728 case S3C2410_DMALOAD_NONE:
729 /* can load buffer immediately */
730 break;
732 case S3C2410_DMALOAD_1LOADED:
733 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
734 /* flag error? */
735 printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
736 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
737 chan->number, __FUNCTION__);
738 =======
739 chan->number, __func__);
740 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
741 return IRQ_HANDLED;
744 break;
746 case S3C2410_DMALOAD_1LOADED_1RUNNING:
747 goto no_load;
749 default:
750 printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
751 chan->number, chan->load_state);
752 return IRQ_HANDLED;
755 local_irq_save(flags);
756 s3c2410_dma_loadbuffer(chan, chan->next);
757 local_irq_restore(flags);
758 } else {
759 s3c2410_dma_lastxfer(chan);
761 /* see if we can stop this channel.. */
762 if (chan->load_state == S3C2410_DMALOAD_NONE) {
763 pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
764 chan->number, jiffies);
765 s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
766 S3C2410_DMAOP_STOP);
770 no_load:
771 return IRQ_HANDLED;
774 static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel);
776 /* s3c2410_request_dma
778 * get control of an dma channel
781 int s3c2410_dma_request(unsigned int channel,
782 struct s3c2410_dma_client *client,
783 void *dev)
785 struct s3c2410_dma_chan *chan;
786 unsigned long flags;
787 int err;
789 pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
790 channel, client->name, dev);
792 local_irq_save(flags);
794 chan = s3c2410_dma_map_channel(channel);
795 if (chan == NULL) {
796 local_irq_restore(flags);
797 return -EBUSY;
800 dbg_showchan(chan);
802 chan->client = client;
803 chan->in_use = 1;
805 if (!chan->irq_claimed) {
806 pr_debug("dma%d: %s : requesting irq %d\n",
807 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
808 channel, __FUNCTION__, chan->irq);
809 =======
810 channel, __func__, chan->irq);
811 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
813 chan->irq_claimed = 1;
814 local_irq_restore(flags);
816 err = request_irq(chan->irq, s3c2410_dma_irq, IRQF_DISABLED,
817 client->name, (void *)chan);
819 local_irq_save(flags);
821 if (err) {
822 chan->in_use = 0;
823 chan->irq_claimed = 0;
824 local_irq_restore(flags);
826 printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
827 client->name, chan->irq, chan->number);
828 return err;
831 chan->irq_enabled = 1;
834 local_irq_restore(flags);
836 /* need to setup */
838 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
839 pr_debug("%s: channel initialised, %p\n", __FUNCTION__, chan);
840 =======
841 pr_debug("%s: channel initialised, %p\n", __func__, chan);
842 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
844 return chan->number | DMACH_LOW_LEVEL;
847 EXPORT_SYMBOL(s3c2410_dma_request);
849 /* s3c2410_dma_free
851 * release the given channel back to the system, will stop and flush
852 * any outstanding transfers, and ensure the channel is ready for the
853 * next claimant.
855 * Note, although a warning is currently printed if the freeing client
856 * info is not the same as the registrant's client info, the free is still
857 * allowed to go through.
860 int s3c2410_dma_free(dmach_t channel, struct s3c2410_dma_client *client)
862 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
863 unsigned long flags;
865 if (chan == NULL)
866 return -EINVAL;
868 local_irq_save(flags);
870 if (chan->client != client) {
871 printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
872 channel, chan->client, client);
875 /* sort out stopping and freeing the channel */
877 if (chan->state != S3C2410_DMA_IDLE) {
878 pr_debug("%s: need to stop dma channel %p\n",
879 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
880 __FUNCTION__, chan);
881 =======
882 __func__, chan);
883 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
885 /* possibly flush the channel */
886 s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
889 chan->client = NULL;
890 chan->in_use = 0;
892 if (chan->irq_claimed)
893 free_irq(chan->irq, (void *)chan);
895 chan->irq_claimed = 0;
897 if (!(channel & DMACH_LOW_LEVEL))
898 dma_chan_map[channel] = NULL;
900 local_irq_restore(flags);
902 return 0;
905 EXPORT_SYMBOL(s3c2410_dma_free);
907 static int s3c2410_dma_dostop(struct s3c2410_dma_chan *chan)
909 unsigned long flags;
910 unsigned long tmp;
912 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
913 pr_debug("%s:\n", __FUNCTION__);
914 =======
915 pr_debug("%s:\n", __func__);
916 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
918 dbg_showchan(chan);
920 local_irq_save(flags);
922 s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
924 tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
925 tmp |= S3C2410_DMASKTRIG_STOP;
926 //tmp &= ~S3C2410_DMASKTRIG_ON;
927 dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
929 #if 0
930 /* should also clear interrupts, according to WinCE BSP */
931 tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
932 tmp |= S3C2410_DCON_NORELOAD;
933 dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
934 #endif
936 /* should stop do this, or should we wait for flush? */
937 chan->state = S3C2410_DMA_IDLE;
938 chan->load_state = S3C2410_DMALOAD_NONE;
940 local_irq_restore(flags);
942 return 0;
945 static void s3c2410_dma_waitforstop(struct s3c2410_dma_chan *chan)
947 unsigned long tmp;
948 unsigned int timeout = 0x10000;
950 while (timeout-- > 0) {
951 tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
953 if (!(tmp & S3C2410_DMASKTRIG_ON))
954 return;
957 pr_debug("dma%d: failed to stop?\n", chan->number);
961 /* s3c2410_dma_flush
963 * stop the channel, and remove all current and pending transfers
966 static int s3c2410_dma_flush(struct s3c2410_dma_chan *chan)
968 struct s3c2410_dma_buf *buf, *next;
969 unsigned long flags;
971 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
972 pr_debug("%s: chan %p (%d)\n", __FUNCTION__, chan, chan->number);
973 =======
974 pr_debug("%s: chan %p (%d)\n", __func__, chan, chan->number);
975 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
977 dbg_showchan(chan);
979 local_irq_save(flags);
981 if (chan->state != S3C2410_DMA_IDLE) {
982 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
983 pr_debug("%s: stopping channel...\n", __FUNCTION__ );
984 =======
985 pr_debug("%s: stopping channel...\n", __func__ );
986 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
987 s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
990 buf = chan->curr;
991 if (buf == NULL)
992 buf = chan->next;
994 chan->curr = chan->next = chan->end = NULL;
996 if (buf != NULL) {
997 for ( ; buf != NULL; buf = next) {
998 next = buf->next;
1000 pr_debug("%s: free buffer %p, next %p\n",
1001 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1002 __FUNCTION__, buf, buf->next);
1003 =======
1004 __func__, buf, buf->next);
1005 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1007 s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
1008 s3c2410_dma_freebuf(buf);
1012 dbg_showregs(chan);
1014 s3c2410_dma_waitforstop(chan);
1016 #if 0
1017 /* should also clear interrupts, according to WinCE BSP */
1019 unsigned long tmp;
1021 tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
1022 tmp |= S3C2410_DCON_NORELOAD;
1023 dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
1025 #endif
1027 dbg_showregs(chan);
1029 local_irq_restore(flags);
1031 return 0;
1034 static int s3c2410_dma_started(struct s3c2410_dma_chan *chan)
1036 unsigned long flags;
1038 local_irq_save(flags);
1040 dbg_showchan(chan);
1042 /* if we've only loaded one buffer onto the channel, then chec
1043 * to see if we have another, and if so, try and load it so when
1044 * the first buffer is finished, the new one will be loaded onto
1045 * the channel */
1047 if (chan->next != NULL) {
1048 if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
1050 if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
1051 pr_debug("%s: buff not yet loaded, no more todo\n",
1052 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1053 __FUNCTION__);
1054 =======
1055 __func__);
1056 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1057 } else {
1058 chan->load_state = S3C2410_DMALOAD_1RUNNING;
1059 s3c2410_dma_loadbuffer(chan, chan->next);
1062 } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
1063 s3c2410_dma_loadbuffer(chan, chan->next);
1068 local_irq_restore(flags);
1070 return 0;
1075 s3c2410_dma_ctrl(dmach_t channel, enum s3c2410_chan_op op)
1077 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1079 if (chan == NULL)
1080 return -EINVAL;
1082 switch (op) {
1083 case S3C2410_DMAOP_START:
1084 return s3c2410_dma_start(chan);
1086 case S3C2410_DMAOP_STOP:
1087 return s3c2410_dma_dostop(chan);
1089 case S3C2410_DMAOP_PAUSE:
1090 case S3C2410_DMAOP_RESUME:
1091 return -ENOENT;
1093 case S3C2410_DMAOP_FLUSH:
1094 return s3c2410_dma_flush(chan);
1096 case S3C2410_DMAOP_STARTED:
1097 return s3c2410_dma_started(chan);
1099 case S3C2410_DMAOP_TIMEOUT:
1100 return 0;
1104 return -ENOENT; /* unknown, don't bother */
1107 EXPORT_SYMBOL(s3c2410_dma_ctrl);
1109 /* DMA configuration for each channel
1111 * DISRCC -> source of the DMA (AHB,APB)
1112 * DISRC -> source address of the DMA
1113 * DIDSTC -> destination of the DMA (AHB,APD)
1114 * DIDST -> destination address of the DMA
1117 /* s3c2410_dma_config
1119 * xfersize: size of unit in bytes (1,2,4)
1120 * dcon: base value of the DCONx register
1123 int s3c2410_dma_config(dmach_t channel,
1124 int xferunit,
1125 int dcon)
1127 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1129 pr_debug("%s: chan=%d, xfer_unit=%d, dcon=%08x\n",
1130 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1131 __FUNCTION__, channel, xferunit, dcon);
1132 =======
1133 __func__, channel, xferunit, dcon);
1134 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1136 if (chan == NULL)
1137 return -EINVAL;
1139 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1140 pr_debug("%s: Initial dcon is %08x\n", __FUNCTION__, dcon);
1141 =======
1142 pr_debug("%s: Initial dcon is %08x\n", __func__, dcon);
1143 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1145 dcon |= chan->dcon & dma_sel.dcon_mask;
1147 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1148 pr_debug("%s: New dcon is %08x\n", __FUNCTION__, dcon);
1149 =======
1150 pr_debug("%s: New dcon is %08x\n", __func__, dcon);
1151 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1153 switch (xferunit) {
1154 case 1:
1155 dcon |= S3C2410_DCON_BYTE;
1156 break;
1158 case 2:
1159 dcon |= S3C2410_DCON_HALFWORD;
1160 break;
1162 case 4:
1163 dcon |= S3C2410_DCON_WORD;
1164 break;
1166 default:
1167 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1168 pr_debug("%s: bad transfer size %d\n", __FUNCTION__, xferunit);
1169 =======
1170 pr_debug("%s: bad transfer size %d\n", __func__, xferunit);
1171 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1172 return -EINVAL;
1175 dcon |= S3C2410_DCON_HWTRIG;
1176 dcon |= S3C2410_DCON_INTREQ;
1178 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1179 pr_debug("%s: dcon now %08x\n", __FUNCTION__, dcon);
1180 =======
1181 pr_debug("%s: dcon now %08x\n", __func__, dcon);
1182 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1184 chan->dcon = dcon;
1185 chan->xfer_unit = xferunit;
1187 return 0;
1190 EXPORT_SYMBOL(s3c2410_dma_config);
1192 int s3c2410_dma_setflags(dmach_t channel, unsigned int flags)
1194 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1196 if (chan == NULL)
1197 return -EINVAL;
1199 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1200 pr_debug("%s: chan=%p, flags=%08x\n", __FUNCTION__, chan, flags);
1201 =======
1202 pr_debug("%s: chan=%p, flags=%08x\n", __func__, chan, flags);
1203 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1205 chan->flags = flags;
1207 return 0;
1210 EXPORT_SYMBOL(s3c2410_dma_setflags);
1213 /* do we need to protect the settings of the fields from
1214 * irq?
1217 int s3c2410_dma_set_opfn(dmach_t channel, s3c2410_dma_opfn_t rtn)
1219 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1221 if (chan == NULL)
1222 return -EINVAL;
1224 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1225 pr_debug("%s: chan=%p, op rtn=%p\n", __FUNCTION__, chan, rtn);
1226 =======
1227 pr_debug("%s: chan=%p, op rtn=%p\n", __func__, chan, rtn);
1228 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1230 chan->op_fn = rtn;
1232 return 0;
1235 EXPORT_SYMBOL(s3c2410_dma_set_opfn);
1237 int s3c2410_dma_set_buffdone_fn(dmach_t channel, s3c2410_dma_cbfn_t rtn)
1239 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1241 if (chan == NULL)
1242 return -EINVAL;
1244 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1245 pr_debug("%s: chan=%p, callback rtn=%p\n", __FUNCTION__, chan, rtn);
1246 =======
1247 pr_debug("%s: chan=%p, callback rtn=%p\n", __func__, chan, rtn);
1248 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1250 chan->callback_fn = rtn;
1252 return 0;
1255 EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);
1257 /* s3c2410_dma_devconfig
1259 * configure the dma source/destination hardware type and address
1261 * source: S3C2410_DMASRC_HW: source is hardware
1262 * S3C2410_DMASRC_MEM: source is memory
1264 * hwcfg: the value for xxxSTCn register,
1265 * bit 0: 0=increment pointer, 1=leave pointer
1266 * bit 1: 0=source is AHB, 1=source is APB
1268 * devaddr: physical address of the source
1271 int s3c2410_dma_devconfig(int channel,
1272 enum s3c2410_dmasrc source,
1273 int hwcfg,
1274 unsigned long devaddr)
1276 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1278 if (chan == NULL)
1279 return -EINVAL;
1281 pr_debug("%s: source=%d, hwcfg=%08x, devaddr=%08lx\n",
1282 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1283 __FUNCTION__, (int)source, hwcfg, devaddr);
1284 =======
1285 __func__, (int)source, hwcfg, devaddr);
1286 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1288 chan->source = source;
1289 chan->dev_addr = devaddr;
1290 chan->hw_cfg = hwcfg;
1292 switch (source) {
1293 case S3C2410_DMASRC_HW:
1294 /* source is hardware */
1295 pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
1296 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1297 __FUNCTION__, devaddr, hwcfg);
1298 =======
1299 __func__, devaddr, hwcfg);
1300 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1301 dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
1302 dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
1303 dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
1305 chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
1306 break;
1308 case S3C2410_DMASRC_MEM:
1309 /* source is memory */
1310 <<<<<<< HEAD:arch/arm/plat-s3c24xx/dma.c
1311 pr_debug( "%s: mem source, devaddr=%08lx, hwcfg=%d\n",
1312 __FUNCTION__, devaddr, hwcfg);
1313 =======
1314 pr_debug("%s: mem source, devaddr=%08lx, hwcfg=%d\n",
1315 __func__, devaddr, hwcfg);
1316 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/plat-s3c24xx/dma.c
1317 dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
1318 dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
1319 dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
1321 chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
1322 break;
1324 default:
1325 printk(KERN_ERR "dma%d: invalid source type (%d)\n",
1326 channel, source);
1328 return -EINVAL;
1331 if (dma_sel.direction != NULL)
1332 (dma_sel.direction)(chan, chan->map, source);
1334 return 0;
1337 EXPORT_SYMBOL(s3c2410_dma_devconfig);
1339 /* s3c2410_dma_getposition
1341 * returns the current transfer points for the dma source and destination
1344 int s3c2410_dma_getposition(dmach_t channel, dma_addr_t *src, dma_addr_t *dst)
1346 struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
1348 if (chan == NULL)
1349 return -EINVAL;
1351 if (src != NULL)
1352 *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
1354 if (dst != NULL)
1355 *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
1357 return 0;
1360 EXPORT_SYMBOL(s3c2410_dma_getposition);
1362 static struct s3c2410_dma_chan *to_dma_chan(struct sys_device *dev)
1364 return container_of(dev, struct s3c2410_dma_chan, dev);
1367 /* system device class */
1369 #ifdef CONFIG_PM
1371 static int s3c2410_dma_suspend(struct sys_device *dev, pm_message_t state)
1373 struct s3c2410_dma_chan *cp = to_dma_chan(dev);
1375 printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
1377 if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
1378 /* the dma channel is still working, which is probably
1379 * a bad thing to do over suspend/resume. We stop the
1380 * channel and assume that the client is either going to
1381 * retry after resume, or that it is broken.
1384 printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
1385 cp->number);
1387 s3c2410_dma_dostop(cp);
1390 return 0;
1393 static int s3c2410_dma_resume(struct sys_device *dev)
1395 struct s3c2410_dma_chan *cp = to_dma_chan(dev);
1396 unsigned int no = cp->number | DMACH_LOW_LEVEL;
1398 /* restore channel's hardware configuration */
1400 if (!cp->in_use)
1401 return 0;
1403 printk(KERN_INFO "dma%d: restoring configuration\n", cp->number);
1405 s3c2410_dma_config(no, cp->xfer_unit, cp->dcon);
1406 s3c2410_dma_devconfig(no, cp->source, cp->hw_cfg, cp->dev_addr);
1408 /* re-select the dma source for this channel */
1410 if (cp->map != NULL)
1411 dma_sel.select(cp, cp->map);
1413 return 0;
1416 #else
1417 #define s3c2410_dma_suspend NULL
1418 #define s3c2410_dma_resume NULL
1419 #endif /* CONFIG_PM */
1421 struct sysdev_class dma_sysclass = {
1422 .name = "s3c24xx-dma",
1423 .suspend = s3c2410_dma_suspend,
1424 .resume = s3c2410_dma_resume,
1427 /* kmem cache implementation */
1429 static void s3c2410_dma_cache_ctor(struct kmem_cache *c, void *p)
1431 memset(p, 0, sizeof(struct s3c2410_dma_buf));
1434 /* initialisation code */
1436 static int __init s3c24xx_dma_sysclass_init(void)
1438 int ret = sysdev_class_register(&dma_sysclass);
1440 if (ret != 0)
1441 printk(KERN_ERR "dma sysclass registration failed\n");
1443 return ret;
1446 core_initcall(s3c24xx_dma_sysclass_init);
1448 static int __init s3c24xx_dma_sysdev_register(void)
1450 struct s3c2410_dma_chan *cp = s3c2410_chans;
1451 int channel, ret;
1453 for (channel = 0; channel < dma_channels; cp++, channel++) {
1454 cp->dev.cls = &dma_sysclass;
1455 cp->dev.id = channel;
1456 ret = sysdev_register(&cp->dev);
1458 if (ret) {
1459 printk(KERN_ERR "error registering dev for dma %d\n",
1460 channel);
1461 return ret;
1465 return 0;
1468 late_initcall(s3c24xx_dma_sysdev_register);
1470 int __init s3c24xx_dma_init(unsigned int channels, unsigned int irq,
1471 unsigned int stride)
1473 struct s3c2410_dma_chan *cp;
1474 int channel;
1475 int ret;
1477 printk("S3C24XX DMA Driver, (c) 2003-2004,2006 Simtec Electronics\n");
1479 dma_channels = channels;
1481 dma_base = ioremap(S3C24XX_PA_DMA, stride * channels);
1482 if (dma_base == NULL) {
1483 printk(KERN_ERR "dma failed to remap register block\n");
1484 return -ENOMEM;
1487 dma_kmem = kmem_cache_create("dma_desc",
1488 sizeof(struct s3c2410_dma_buf), 0,
1489 SLAB_HWCACHE_ALIGN,
1490 s3c2410_dma_cache_ctor);
1492 if (dma_kmem == NULL) {
1493 printk(KERN_ERR "dma failed to make kmem cache\n");
1494 ret = -ENOMEM;
1495 goto err;
1498 for (channel = 0; channel < channels; channel++) {
1499 cp = &s3c2410_chans[channel];
1501 memset(cp, 0, sizeof(struct s3c2410_dma_chan));
1503 /* dma channel irqs are in order.. */
1504 cp->number = channel;
1505 cp->irq = channel + irq;
1506 cp->regs = dma_base + (channel * stride);
1508 /* point current stats somewhere */
1509 cp->stats = &cp->stats_store;
1510 cp->stats_store.timeout_shortest = LONG_MAX;
1512 /* basic channel configuration */
1514 cp->load_timeout = 1<<18;
1516 printk("DMA channel %d at %p, irq %d\n",
1517 cp->number, cp->regs, cp->irq);
1520 return 0;
1522 err:
1523 kmem_cache_destroy(dma_kmem);
1524 iounmap(dma_base);
1525 dma_base = NULL;
1526 return ret;
1529 int __init s3c2410_dma_init(void)
1531 return s3c24xx_dma_init(4, IRQ_DMA0, 0x40);
1534 static inline int is_channel_valid(unsigned int channel)
1536 return (channel & DMA_CH_VALID);
1539 static struct s3c24xx_dma_order *dma_order;
1542 /* s3c2410_dma_map_channel()
1544 * turn the virtual channel number into a real, and un-used hardware
1545 * channel.
1547 * first, try the dma ordering given to us by either the relevant
1548 * dma code, or the board. Then just find the first usable free
1549 * channel
1552 static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel)
1554 struct s3c24xx_dma_order_ch *ord = NULL;
1555 struct s3c24xx_dma_map *ch_map;
1556 struct s3c2410_dma_chan *dmach;
1557 int ch;
1559 if (dma_sel.map == NULL || channel > dma_sel.map_size)
1560 return NULL;
1562 ch_map = dma_sel.map + channel;
1564 /* first, try the board mapping */
1566 if (dma_order) {
1567 ord = &dma_order->channels[channel];
1569 for (ch = 0; ch < dma_channels; ch++) {
1570 if (!is_channel_valid(ord->list[ch]))
1571 continue;
1573 if (s3c2410_chans[ord->list[ch]].in_use == 0) {
1574 ch = ord->list[ch] & ~DMA_CH_VALID;
1575 goto found;
1579 if (ord->flags & DMA_CH_NEVER)
1580 return NULL;
1583 /* second, search the channel map for first free */
1585 for (ch = 0; ch < dma_channels; ch++) {
1586 if (!is_channel_valid(ch_map->channels[ch]))
1587 continue;
1589 if (s3c2410_chans[ch].in_use == 0) {
1590 printk("mapped channel %d to %d\n", channel, ch);
1591 break;
1595 if (ch >= dma_channels)
1596 return NULL;
1598 /* update our channel mapping */
1600 found:
1601 dmach = &s3c2410_chans[ch];
1602 dmach->map = ch_map;
1603 dma_chan_map[channel] = dmach;
1605 /* select the channel */
1607 (dma_sel.select)(dmach, ch_map);
1609 return dmach;
1612 static int s3c24xx_dma_check_entry(struct s3c24xx_dma_map *map, int ch)
1614 return 0;
1617 int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)
1619 struct s3c24xx_dma_map *nmap;
1620 size_t map_sz = sizeof(*nmap) * sel->map_size;
1621 int ptr;
1623 nmap = kmalloc(map_sz, GFP_KERNEL);
1624 if (nmap == NULL)
1625 return -ENOMEM;
1627 memcpy(nmap, sel->map, map_sz);
1628 memcpy(&dma_sel, sel, sizeof(*sel));
1630 dma_sel.map = nmap;
1632 for (ptr = 0; ptr < sel->map_size; ptr++)
1633 s3c24xx_dma_check_entry(nmap+ptr, ptr);
1635 return 0;
1638 int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)
1640 struct s3c24xx_dma_order *nord = dma_order;
1642 if (nord == NULL)
1643 nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);
1645 if (nord == NULL) {
1646 printk(KERN_ERR "no memory to store dma channel order\n");
1647 return -ENOMEM;
1650 dma_order = nord;
1651 memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));
1652 return 0;