| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 | 
							- /* linux/arch/arm/plat-s3c24xx/dma.c
 
-  *
 
-  * Copyright 2003-2006 Simtec Electronics
 
-  *	Ben Dooks <ben@simtec.co.uk>
 
-  *
 
-  * S3C2410 DMA core
 
-  *
 
-  * http://armlinux.simtec.co.uk/
 
-  *
 
-  * This program is free software; you can redistribute it and/or modify
 
-  * it under the terms of the GNU General Public License version 2 as
 
-  * published by the Free Software Foundation.
 
- */
 
- #ifdef CONFIG_S3C2410_DMA_DEBUG
 
- #define DEBUG
 
- #endif
 
- #include <linux/module.h>
 
- #include <linux/init.h>
 
- #include <linux/sched.h>
 
- #include <linux/spinlock.h>
 
- #include <linux/interrupt.h>
 
- #include <linux/syscore_ops.h>
 
- #include <linux/slab.h>
 
- #include <linux/errno.h>
 
- #include <linux/io.h>
 
- #include <asm/irq.h>
 
- #include <mach/hardware.h>
 
- #include <mach/dma.h>
 
- #include <mach/map.h>
 
- #include <plat/dma-s3c24xx.h>
 
- #include <plat/regs-dma.h>
 
- /* io map for dma */
 
- static void __iomem *dma_base;
 
- static struct kmem_cache *dma_kmem;
 
- static int dma_channels;
 
- static struct s3c24xx_dma_selection dma_sel;
 
- /* debugging functions */
 
- #define BUF_MAGIC (0xcafebabe)
 
- #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
 
- #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
 
- #if 1
 
- #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
 
- #else
 
- static inline void
 
- dma_wrreg(struct s3c2410_dma_chan *chan, int reg, unsigned long val)
 
- {
 
- 	pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
 
- 	writel(val, dma_regaddr(chan, reg));
 
- }
 
- #endif
 
- #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
 
- /* captured register state for debug */
 
- struct s3c2410_dma_regstate {
 
- 	unsigned long         dcsrc;
 
- 	unsigned long         disrc;
 
- 	unsigned long         dstat;
 
- 	unsigned long         dcon;
 
- 	unsigned long         dmsktrig;
 
- };
 
- #ifdef CONFIG_S3C2410_DMA_DEBUG
 
- /* dmadbg_showregs
 
-  *
 
-  * simple debug routine to print the current state of the dma registers
 
- */
 
- static void
 
- dmadbg_capture(struct s3c2410_dma_chan *chan, struct s3c2410_dma_regstate *regs)
 
- {
 
- 	regs->dcsrc    = dma_rdreg(chan, S3C2410_DMA_DCSRC);
 
- 	regs->disrc    = dma_rdreg(chan, S3C2410_DMA_DISRC);
 
- 	regs->dstat    = dma_rdreg(chan, S3C2410_DMA_DSTAT);
 
- 	regs->dcon     = dma_rdreg(chan, S3C2410_DMA_DCON);
 
- 	regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
 
- }
 
- static void
 
- dmadbg_dumpregs(const char *fname, int line, struct s3c2410_dma_chan *chan,
 
- 		 struct s3c2410_dma_regstate *regs)
 
- {
 
- 	printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
 
- 	       chan->number, fname, line,
 
- 	       regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
 
- 	       regs->dcon);
 
- }
 
- static void
 
- dmadbg_showchan(const char *fname, int line, struct s3c2410_dma_chan *chan)
 
- {
 
- 	struct s3c2410_dma_regstate state;
 
- 	dmadbg_capture(chan, &state);
 
- 	printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
 
- 	       chan->number, fname, line, chan->load_state,
 
- 	       chan->curr, chan->next, chan->end);
 
- 	dmadbg_dumpregs(fname, line, chan, &state);
 
- }
 
- static void
 
- dmadbg_showregs(const char *fname, int line, struct s3c2410_dma_chan *chan)
 
- {
 
- 	struct s3c2410_dma_regstate state;
 
- 	dmadbg_capture(chan, &state);
 
- 	dmadbg_dumpregs(fname, line, chan, &state);
 
- }
 
- #define dbg_showregs(chan) dmadbg_showregs(__func__, __LINE__, (chan))
 
- #define dbg_showchan(chan) dmadbg_showchan(__func__, __LINE__, (chan))
 
- #else
 
- #define dbg_showregs(chan) do { } while(0)
 
- #define dbg_showchan(chan) do { } while(0)
 
- #endif /* CONFIG_S3C2410_DMA_DEBUG */
 
- /* s3c2410_dma_stats_timeout
 
-  *
 
-  * Update DMA stats from timeout info
 
- */
 
- static void
 
- s3c2410_dma_stats_timeout(struct s3c2410_dma_stats *stats, int val)
 
- {
 
- 	if (stats == NULL)
 
- 		return;
 
- 	if (val > stats->timeout_longest)
 
- 		stats->timeout_longest = val;
 
- 	if (val < stats->timeout_shortest)
 
- 		stats->timeout_shortest = val;
 
- 	stats->timeout_avg += val;
 
- }
 
- /* s3c2410_dma_waitforload
 
-  *
 
-  * wait for the DMA engine to load a buffer, and update the state accordingly
 
- */
 
- static int
 
- s3c2410_dma_waitforload(struct s3c2410_dma_chan *chan, int line)
 
- {
 
- 	int timeout = chan->load_timeout;
 
- 	int took;
 
- 	if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
 
- 		printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
 
- 		return 0;
 
- 	}
 
- 	if (chan->stats != NULL)
 
- 		chan->stats->loads++;
 
- 	while (--timeout > 0) {
 
- 		if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
 
- 			took = chan->load_timeout - timeout;
 
- 			s3c2410_dma_stats_timeout(chan->stats, took);
 
- 			switch (chan->load_state) {
 
- 			case S3C2410_DMALOAD_1LOADED:
 
- 				chan->load_state = S3C2410_DMALOAD_1RUNNING;
 
- 				break;
 
- 			default:
 
- 				printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
 
- 			}
 
- 			return 1;
 
- 		}
 
- 	}
 
- 	if (chan->stats != NULL) {
 
- 		chan->stats->timeout_failed++;
 
- 	}
 
- 	return 0;
 
- }
 
- /* s3c2410_dma_loadbuffer
 
-  *
 
-  * load a buffer, and update the channel state
 
- */
 
- static inline int
 
- s3c2410_dma_loadbuffer(struct s3c2410_dma_chan *chan,
 
- 		       struct s3c2410_dma_buf *buf)
 
- {
 
- 	unsigned long reload;
 
- 	if (buf == NULL) {
 
- 		dmawarn("buffer is NULL\n");
 
- 		return -EINVAL;
 
- 	}
 
- 	pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
 
- 		 buf, (unsigned long)buf->data, buf->size);
 
- 	/* check the state of the channel before we do anything */
 
- 	if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
 
- 		dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
 
- 	}
 
- 	if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
 
- 		dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
 
- 	}
 
- 	/* it would seem sensible if we are the last buffer to not bother
 
- 	 * with the auto-reload bit, so that the DMA engine will not try
 
- 	 * and load another transfer after this one has finished...
 
- 	 */
 
- 	if (chan->load_state == S3C2410_DMALOAD_NONE) {
 
- 		pr_debug("load_state is none, checking for noreload (next=%p)\n",
 
- 			 buf->next);
 
- 		reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
 
- 	} else {
 
- 		//pr_debug("load_state is %d => autoreload\n", chan->load_state);
 
- 		reload = S3C2410_DCON_AUTORELOAD;
 
- 	}
 
- 	if ((buf->data & 0xf0000000) != 0x30000000) {
 
- 		dmawarn("dmaload: buffer is %p\n", (void *)buf->data);
 
- 	}
 
- 	writel(buf->data, chan->addr_reg);
 
- 	dma_wrreg(chan, S3C2410_DMA_DCON,
 
- 		  chan->dcon | reload | (buf->size/chan->xfer_unit));
 
- 	chan->next = buf->next;
 
- 	/* update the state of the channel */
 
- 	switch (chan->load_state) {
 
- 	case S3C2410_DMALOAD_NONE:
 
- 		chan->load_state = S3C2410_DMALOAD_1LOADED;
 
- 		break;
 
- 	case S3C2410_DMALOAD_1RUNNING:
 
- 		chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
 
- 		break;
 
- 	default:
 
- 		dmawarn("dmaload: unknown state %d in loadbuffer\n",
 
- 			chan->load_state);
 
- 		break;
 
- 	}
 
- 	return 0;
 
- }
 
- /* s3c2410_dma_call_op
 
-  *
 
-  * small routine to call the op routine with the given op if it has been
 
-  * registered
 
- */
 
- static void
 
- s3c2410_dma_call_op(struct s3c2410_dma_chan *chan, enum s3c2410_chan_op op)
 
- {
 
- 	if (chan->op_fn != NULL) {
 
- 		(chan->op_fn)(chan, op);
 
- 	}
 
- }
 
- /* s3c2410_dma_buffdone
 
-  *
 
-  * small wrapper to check if callback routine needs to be called, and
 
-  * if so, call it
 
- */
 
- static inline void
 
- s3c2410_dma_buffdone(struct s3c2410_dma_chan *chan, struct s3c2410_dma_buf *buf,
 
- 		     enum s3c2410_dma_buffresult result)
 
- {
 
- #if 0
 
- 	pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
 
- 		 chan->callback_fn, buf, buf->id, buf->size, result);
 
- #endif
 
- 	if (chan->callback_fn != NULL) {
 
- 		(chan->callback_fn)(chan, buf->id, buf->size, result);
 
- 	}
 
- }
 
- /* s3c2410_dma_start
 
-  *
 
-  * start a dma channel going
 
- */
 
- static int s3c2410_dma_start(struct s3c2410_dma_chan *chan)
 
- {
 
- 	unsigned long tmp;
 
- 	unsigned long flags;
 
- 	pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
 
- 	local_irq_save(flags);
 
- 	if (chan->state == S3C2410_DMA_RUNNING) {
 
- 		pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
 
- 		local_irq_restore(flags);
 
- 		return 0;
 
- 	}
 
- 	chan->state = S3C2410_DMA_RUNNING;
 
- 	/* check whether there is anything to load, and if not, see
 
- 	 * if we can find anything to load
 
- 	 */
 
- 	if (chan->load_state == S3C2410_DMALOAD_NONE) {
 
- 		if (chan->next == NULL) {
 
- 			printk(KERN_ERR "dma%d: channel has nothing loaded\n",
 
- 			       chan->number);
 
- 			chan->state = S3C2410_DMA_IDLE;
 
- 			local_irq_restore(flags);
 
- 			return -EINVAL;
 
- 		}
 
- 		s3c2410_dma_loadbuffer(chan, chan->next);
 
- 	}
 
- 	dbg_showchan(chan);
 
- 	/* enable the channel */
 
- 	if (!chan->irq_enabled) {
 
- 		enable_irq(chan->irq);
 
- 		chan->irq_enabled = 1;
 
- 	}
 
- 	/* start the channel going */
 
- 	tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
 
- 	tmp &= ~S3C2410_DMASKTRIG_STOP;
 
- 	tmp |= S3C2410_DMASKTRIG_ON;
 
- 	dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
 
- 	pr_debug("dma%d: %08lx to DMASKTRIG\n", chan->number, tmp);
 
- #if 0
 
- 	/* the dma buffer loads should take care of clearing the AUTO
 
- 	 * reloading feature */
 
- 	tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
 
- 	tmp &= ~S3C2410_DCON_NORELOAD;
 
- 	dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
 
- #endif
 
- 	s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
 
- 	dbg_showchan(chan);
 
- 	/* if we've only loaded one buffer onto the channel, then chec
 
- 	 * to see if we have another, and if so, try and load it so when
 
- 	 * the first buffer is finished, the new one will be loaded onto
 
- 	 * the channel */
 
- 	if (chan->next != NULL) {
 
- 		if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
 
- 			if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
 
- 				pr_debug("%s: buff not yet loaded, no more todo\n",
 
- 					 __func__);
 
- 			} else {
 
- 				chan->load_state = S3C2410_DMALOAD_1RUNNING;
 
- 				s3c2410_dma_loadbuffer(chan, chan->next);
 
- 			}
 
- 		} else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
 
- 			s3c2410_dma_loadbuffer(chan, chan->next);
 
- 		}
 
- 	}
 
- 	local_irq_restore(flags);
 
- 	return 0;
 
- }
 
- /* s3c2410_dma_canload
 
-  *
 
-  * work out if we can queue another buffer into the DMA engine
 
- */
 
- static int
 
- s3c2410_dma_canload(struct s3c2410_dma_chan *chan)
 
- {
 
- 	if (chan->load_state == S3C2410_DMALOAD_NONE ||
 
- 	    chan->load_state == S3C2410_DMALOAD_1RUNNING)
 
- 		return 1;
 
- 	return 0;
 
- }
 
- /* s3c2410_dma_enqueue
 
-  *
 
-  * queue an given buffer for dma transfer.
 
-  *
 
-  * id         the device driver's id information for this buffer
 
-  * data       the physical address of the buffer data
 
-  * size       the size of the buffer in bytes
 
-  *
 
-  * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
 
-  * is checked, and if set, the channel is started. If this flag isn't set,
 
-  * then an error will be returned.
 
-  *
 
-  * It is possible to queue more than one DMA buffer onto a channel at
 
-  * once, and the code will deal with the re-loading of the next buffer
 
-  * when necessary.
 
- */
 
- int s3c2410_dma_enqueue(enum dma_ch channel, void *id,
 
- 			dma_addr_t data, int size)
 
- {
 
- 	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
 
- 	struct s3c2410_dma_buf *buf;
 
- 	unsigned long flags;
 
- 	if (chan == NULL)
 
- 		return -EINVAL;
 
- 	pr_debug("%s: id=%p, data=%08x, size=%d\n",
 
- 		 __func__, id, (unsigned int)data, size);
 
- 	buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
 
- 	if (buf == NULL) {
 
- 		pr_debug("%s: out of memory (%ld alloc)\n",
 
- 			 __func__, (long)sizeof(*buf));
 
- 		return -ENOMEM;
 
- 	}
 
- 	//pr_debug("%s: new buffer %p\n", __func__, buf);
 
- 	//dbg_showchan(chan);
 
- 	buf->next  = NULL;
 
- 	buf->data  = buf->ptr = data;
 
- 	buf->size  = size;
 
- 	buf->id    = id;
 
- 	buf->magic = BUF_MAGIC;
 
- 	local_irq_save(flags);
 
- 	if (chan->curr == NULL) {
 
- 		/* we've got nothing loaded... */
 
- 		pr_debug("%s: buffer %p queued onto empty channel\n",
 
- 			 __func__, buf);
 
- 		chan->curr = buf;
 
- 		chan->end  = buf;
 
- 		chan->next = NULL;
 
- 	} else {
 
- 		pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
 
- 			 chan->number, __func__, buf);
 
- 		if (chan->end == NULL) {
 
- 			pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
 
- 				 chan->number, __func__, chan);
 
- 		} else {
 
- 			chan->end->next = buf;
 
- 			chan->end = buf;
 
- 		}
 
- 	}
 
- 	/* if necessary, update the next buffer field */
 
- 	if (chan->next == NULL)
 
- 		chan->next = buf;
 
- 	/* check to see if we can load a buffer */
 
- 	if (chan->state == S3C2410_DMA_RUNNING) {
 
- 		if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
 
- 			if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
 
- 				printk(KERN_ERR "dma%d: loadbuffer:"
 
- 				       "timeout loading buffer\n",
 
- 				       chan->number);
 
- 				dbg_showchan(chan);
 
- 				local_irq_restore(flags);
 
- 				return -EINVAL;
 
- 			}
 
- 		}
 
- 		while (s3c2410_dma_canload(chan) && chan->next != NULL) {
 
- 			s3c2410_dma_loadbuffer(chan, chan->next);
 
- 		}
 
- 	} else if (chan->state == S3C2410_DMA_IDLE) {
 
- 		if (chan->flags & S3C2410_DMAF_AUTOSTART) {
 
- 			s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
 
- 					 S3C2410_DMAOP_START);
 
- 		}
 
- 	}
 
- 	local_irq_restore(flags);
 
- 	return 0;
 
- }
 
- EXPORT_SYMBOL(s3c2410_dma_enqueue);
 
- static inline void
 
- s3c2410_dma_freebuf(struct s3c2410_dma_buf *buf)
 
- {
 
- 	int magicok = (buf->magic == BUF_MAGIC);
 
- 	buf->magic = -1;
 
- 	if (magicok) {
 
- 		kmem_cache_free(dma_kmem, buf);
 
- 	} else {
 
- 		printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
 
- 	}
 
- }
 
- /* s3c2410_dma_lastxfer
 
-  *
 
-  * called when the system is out of buffers, to ensure that the channel
 
-  * is prepared for shutdown.
 
- */
 
- static inline void
 
- s3c2410_dma_lastxfer(struct s3c2410_dma_chan *chan)
 
- {
 
- #if 0
 
- 	pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
 
- 		 chan->number, chan->load_state);
 
- #endif
 
- 	switch (chan->load_state) {
 
- 	case S3C2410_DMALOAD_NONE:
 
- 		break;
 
- 	case S3C2410_DMALOAD_1LOADED:
 
- 		if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
 
- 				/* flag error? */
 
- 			printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
 
- 			       chan->number, __func__);
 
- 			return;
 
- 		}
 
- 		break;
 
- 	case S3C2410_DMALOAD_1LOADED_1RUNNING:
 
- 		/* I believe in this case we do not have anything to do
 
- 		 * until the next buffer comes along, and we turn off the
 
- 		 * reload */
 
- 		return;
 
- 	default:
 
- 		pr_debug("dma%d: lastxfer: unhandled load_state %d with no next\n",
 
- 			 chan->number, chan->load_state);
 
- 		return;
 
- 	}
 
- 	/* hopefully this'll shut the damned thing up after the transfer... */
 
- 	dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
 
- }
 
- #define dmadbg2(x...)
 
- static irqreturn_t
 
 
  |