dma.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /* linux/arch/arm/mach-s3c2410/dma.c
  2. *
  3. * (c) 2003-2005,2006 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 DMA core
  7. *
  8. * http://armlinux.simtec.co.uk/
  9. *
  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.
  13. */
  14. #ifdef CONFIG_S3C2410_DMA_DEBUG
  15. #define DEBUG
  16. #endif
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/sysdev.h>
  23. #include <linux/slab.h>
  24. #include <linux/errno.h>
  25. #include <linux/delay.h>
  26. #include <asm/system.h>
  27. #include <asm/irq.h>
  28. #include <asm/hardware.h>
  29. #include <asm/io.h>
  30. #include <asm/dma.h>
  31. #include <asm/mach/dma.h>
  32. #include <asm/arch/map.h>
  33. #include "dma.h"
  34. /* io map for dma */
  35. static void __iomem *dma_base;
  36. static kmem_cache_t *dma_kmem;
  37. struct s3c24xx_dma_selection dma_sel;
  38. /* dma channel state information */
  39. struct s3c2410_dma_chan s3c2410_chans[S3C2410_DMA_CHANNELS];
  40. /* debugging functions */
  41. #define BUF_MAGIC (0xcafebabe)
  42. #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
  43. #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
  44. #if 1
  45. #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
  46. #else
  47. static inline void
  48. dma_wrreg(struct s3c2410_dma_chan *chan, int reg, unsigned long val)
  49. {
  50. pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
  51. writel(val, dma_regaddr(chan, reg));
  52. }
  53. #endif
  54. #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
  55. /* captured register state for debug */
  56. struct s3c2410_dma_regstate {
  57. unsigned long dcsrc;
  58. unsigned long disrc;
  59. unsigned long dstat;
  60. unsigned long dcon;
  61. unsigned long dmsktrig;
  62. };
  63. #ifdef CONFIG_S3C2410_DMA_DEBUG
  64. /* dmadbg_showregs
  65. *
  66. * simple debug routine to print the current state of the dma registers
  67. */
  68. static void
  69. dmadbg_capture(struct s3c2410_dma_chan *chan, struct s3c2410_dma_regstate *regs)
  70. {
  71. regs->dcsrc = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  72. regs->disrc = dma_rdreg(chan, S3C2410_DMA_DISRC);
  73. regs->dstat = dma_rdreg(chan, S3C2410_DMA_DSTAT);
  74. regs->dcon = dma_rdreg(chan, S3C2410_DMA_DCON);
  75. regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  76. }
  77. static void
  78. dmadbg_dumpregs(const char *fname, int line, struct s3c2410_dma_chan *chan,
  79. struct s3c2410_dma_regstate *regs)
  80. {
  81. printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
  82. chan->number, fname, line,
  83. regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
  84. regs->dcon);
  85. }
  86. static void
  87. dmadbg_showchan(const char *fname, int line, struct s3c2410_dma_chan *chan)
  88. {
  89. struct s3c2410_dma_regstate state;
  90. dmadbg_capture(chan, &state);
  91. printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
  92. chan->number, fname, line, chan->load_state,
  93. chan->curr, chan->next, chan->end);
  94. dmadbg_dumpregs(fname, line, chan, &state);
  95. }
  96. static void
  97. dmadbg_showregs(const char *fname, int line, struct s3c2410_dma_chan *chan)
  98. {
  99. struct s3c2410_dma_regstate state;
  100. dmadbg_capture(chan, &state);
  101. dmadbg_dumpregs(fname, line, chan, &state);
  102. }
  103. #define dbg_showregs(chan) dmadbg_showregs(__FUNCTION__, __LINE__, (chan))
  104. #define dbg_showchan(chan) dmadbg_showchan(__FUNCTION__, __LINE__, (chan))
  105. #else
  106. #define dbg_showregs(chan) do { } while(0)
  107. #define dbg_showchan(chan) do { } while(0)
  108. #endif /* CONFIG_S3C2410_DMA_DEBUG */
  109. static struct s3c2410_dma_chan *dma_chan_map[DMACH_MAX];
  110. /* lookup_dma_channel
  111. *
  112. * change the dma channel number given into a real dma channel id
  113. */
  114. static struct s3c2410_dma_chan *lookup_dma_channel(unsigned int channel)
  115. {
  116. if (channel & DMACH_LOW_LEVEL)
  117. return &s3c2410_chans[channel & ~DMACH_LOW_LEVEL];
  118. else
  119. return dma_chan_map[channel];
  120. }
  121. /* s3c2410_dma_stats_timeout
  122. *
  123. * Update DMA stats from timeout info
  124. */
  125. static void
  126. s3c2410_dma_stats_timeout(struct s3c2410_dma_stats *stats, int val)
  127. {
  128. if (stats == NULL)
  129. return;
  130. if (val > stats->timeout_longest)
  131. stats->timeout_longest = val;
  132. if (val < stats->timeout_shortest)
  133. stats->timeout_shortest = val;
  134. stats->timeout_avg += val;
  135. }
  136. /* s3c2410_dma_waitforload
  137. *
  138. * wait for the DMA engine to load a buffer, and update the state accordingly
  139. */
  140. static int
  141. s3c2410_dma_waitforload(struct s3c2410_dma_chan *chan, int line)
  142. {
  143. int timeout = chan->load_timeout;
  144. int took;
  145. if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
  146. printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
  147. return 0;
  148. }
  149. if (chan->stats != NULL)
  150. chan->stats->loads++;
  151. while (--timeout > 0) {
  152. if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
  153. took = chan->load_timeout - timeout;
  154. s3c2410_dma_stats_timeout(chan->stats, took);
  155. switch (chan->load_state) {
  156. case S3C2410_DMALOAD_1LOADED:
  157. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  158. break;
  159. default:
  160. printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
  161. }
  162. return 1;
  163. }
  164. }
  165. if (chan->stats != NULL) {
  166. chan->stats->timeout_failed++;
  167. }
  168. return 0;
  169. }
  170. /* s3c2410_dma_loadbuffer
  171. *
  172. * load a buffer, and update the channel state
  173. */
  174. static inline int
  175. s3c2410_dma_loadbuffer(struct s3c2410_dma_chan *chan,
  176. struct s3c2410_dma_buf *buf)
  177. {
  178. unsigned long reload;
  179. pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
  180. buf, (unsigned long)buf->data, buf->size);
  181. if (buf == NULL) {
  182. dmawarn("buffer is NULL\n");
  183. return -EINVAL;
  184. }
  185. /* check the state of the channel before we do anything */
  186. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  187. dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
  188. }
  189. if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
  190. dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
  191. }
  192. /* it would seem sensible if we are the last buffer to not bother
  193. * with the auto-reload bit, so that the DMA engine will not try
  194. * and load another transfer after this one has finished...
  195. */
  196. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  197. pr_debug("load_state is none, checking for noreload (next=%p)\n",
  198. buf->next);
  199. reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
  200. } else {
  201. //pr_debug("load_state is %d => autoreload\n", chan->load_state);
  202. reload = S3C2410_DCON_AUTORELOAD;
  203. }
  204. if ((buf->data & 0xf0000000) != 0x30000000) {
  205. dmawarn("dmaload: buffer is %p\n", (void *)buf->data);
  206. }
  207. writel(buf->data, chan->addr_reg);
  208. dma_wrreg(chan, S3C2410_DMA_DCON,
  209. chan->dcon | reload | (buf->size/chan->xfer_unit));
  210. chan->next = buf->next;
  211. /* update the state of the channel */
  212. switch (chan->load_state) {
  213. case S3C2410_DMALOAD_NONE:
  214. chan->load_state = S3C2410_DMALOAD_1LOADED;
  215. break;
  216. case S3C2410_DMALOAD_1RUNNING:
  217. chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
  218. break;
  219. default:
  220. dmawarn("dmaload: unknown state %d in loadbuffer\n",
  221. chan->load_state);
  222. break;
  223. }
  224. return 0;
  225. }
  226. /* s3c2410_dma_call_op
  227. *
  228. * small routine to call the op routine with the given op if it has been
  229. * registered
  230. */
  231. static void
  232. s3c2410_dma_call_op(struct s3c2410_dma_chan *chan, enum s3c2410_chan_op op)
  233. {
  234. if (chan->op_fn != NULL) {
  235. (chan->op_fn)(chan, op);
  236. }
  237. }
  238. /* s3c2410_dma_buffdone
  239. *
  240. * small wrapper to check if callback routine needs to be called, and
  241. * if so, call it
  242. */
  243. static inline void
  244. s3c2410_dma_buffdone(struct s3c2410_dma_chan *chan, struct s3c2410_dma_buf *buf,
  245. enum s3c2410_dma_buffresult result)
  246. {
  247. #if 0
  248. pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
  249. chan->callback_fn, buf, buf->id, buf->size, result);
  250. #endif
  251. if (chan->callback_fn != NULL) {
  252. (chan->callback_fn)(chan, buf->id, buf->size, result);
  253. }
  254. }
  255. /* s3c2410_dma_start
  256. *
  257. * start a dma channel going
  258. */
  259. static int s3c2410_dma_start(struct s3c2410_dma_chan *chan)
  260. {
  261. unsigned long tmp;
  262. unsigned long flags;
  263. pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
  264. local_irq_save(flags);
  265. if (chan->state == S3C2410_DMA_RUNNING) {
  266. pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
  267. local_irq_restore(flags);
  268. return 0;
  269. }
  270. chan->state = S3C2410_DMA_RUNNING;
  271. /* check wether there is anything to load, and if not, see
  272. * if we can find anything to load
  273. */
  274. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  275. if (chan->next == NULL) {
  276. printk(KERN_ERR "dma%d: channel has nothing loaded\n",
  277. chan->number);
  278. chan->state = S3C2410_DMA_IDLE;
  279. local_irq_restore(flags);
  280. return -EINVAL;
  281. }
  282. s3c2410_dma_loadbuffer(chan, chan->next);
  283. }
  284. dbg_showchan(chan);
  285. /* enable the channel */
  286. if (!chan->irq_enabled) {
  287. enable_irq(chan->irq);
  288. chan->irq_enabled = 1;
  289. }
  290. /* start the channel going */
  291. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  292. tmp &= ~S3C2410_DMASKTRIG_STOP;
  293. tmp |= S3C2410_DMASKTRIG_ON;
  294. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  295. pr_debug("dma%d: %08lx to DMASKTRIG\n", chan->number, tmp);
  296. #if 0
  297. /* the dma buffer loads should take care of clearing the AUTO
  298. * reloading feature */
  299. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  300. tmp &= ~S3C2410_DCON_NORELOAD;
  301. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  302. #endif
  303. s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
  304. dbg_showchan(chan);
  305. /* if we've only loaded one buffer onto the channel, then chec
  306. * to see if we have another, and if so, try and load it so when
  307. * the first buffer is finished, the new one will be loaded onto
  308. * the channel */
  309. if (chan->next != NULL) {
  310. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  311. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  312. pr_debug("%s: buff not yet loaded, no more todo\n",
  313. __FUNCTION__);
  314. } else {
  315. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  316. s3c2410_dma_loadbuffer(chan, chan->next);
  317. }
  318. } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
  319. s3c2410_dma_loadbuffer(chan, chan->next);
  320. }
  321. }
  322. local_irq_restore(flags);
  323. return 0;
  324. }
  325. /* s3c2410_dma_canload
  326. *
  327. * work out if we can queue another buffer into the DMA engine
  328. */
  329. static int
  330. s3c2410_dma_canload(struct s3c2410_dma_chan *chan)
  331. {
  332. if (chan->load_state == S3C2410_DMALOAD_NONE ||
  333. chan->load_state == S3C2410_DMALOAD_1RUNNING)
  334. return 1;
  335. return 0;
  336. }
  337. /* s3c2410_dma_enqueue
  338. *
  339. * queue an given buffer for dma transfer.
  340. *
  341. * id the device driver's id information for this buffer
  342. * data the physical address of the buffer data
  343. * size the size of the buffer in bytes
  344. *
  345. * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
  346. * is checked, and if set, the channel is started. If this flag isn't set,
  347. * then an error will be returned.
  348. *
  349. * It is possible to queue more than one DMA buffer onto a channel at
  350. * once, and the code will deal with the re-loading of the next buffer
  351. * when necessary.
  352. */
  353. int s3c2410_dma_enqueue(unsigned int channel, void *id,
  354. dma_addr_t data, int size)
  355. {
  356. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  357. struct s3c2410_dma_buf *buf;
  358. unsigned long flags;
  359. if (chan == NULL)
  360. return -EINVAL;
  361. pr_debug("%s: id=%p, data=%08x, size=%d\n",
  362. __FUNCTION__, id, (unsigned int)data, size);
  363. buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
  364. if (buf == NULL) {
  365. pr_debug("%s: out of memory (%ld alloc)\n",
  366. __FUNCTION__, (long)sizeof(*buf));
  367. return -ENOMEM;
  368. }
  369. //pr_debug("%s: new buffer %p\n", __FUNCTION__, buf);
  370. //dbg_showchan(chan);
  371. buf->next = NULL;
  372. buf->data = buf->ptr = data;
  373. buf->size = size;
  374. buf->id = id;
  375. buf->magic = BUF_MAGIC;
  376. local_irq_save(flags);
  377. if (chan->curr == NULL) {
  378. /* we've got nothing loaded... */
  379. pr_debug("%s: buffer %p queued onto empty channel\n",
  380. __FUNCTION__, buf);
  381. chan->curr = buf;
  382. chan->end = buf;
  383. chan->next = NULL;
  384. } else {
  385. pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
  386. chan->number, __FUNCTION__, buf);
  387. if (chan->end == NULL)
  388. pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
  389. chan->number, __FUNCTION__, chan);
  390. chan->end->next = buf;
  391. chan->end = buf;
  392. }
  393. /* if necessary, update the next buffer field */
  394. if (chan->next == NULL)
  395. chan->next = buf;
  396. /* check to see if we can load a buffer */
  397. if (chan->state == S3C2410_DMA_RUNNING) {
  398. if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
  399. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  400. printk(KERN_ERR "dma%d: loadbuffer:"
  401. "timeout loading buffer\n",
  402. chan->number);
  403. dbg_showchan(chan);
  404. local_irq_restore(flags);
  405. return -EINVAL;
  406. }
  407. }
  408. while (s3c2410_dma_canload(chan) && chan->next != NULL) {
  409. s3c2410_dma_loadbuffer(chan, chan->next);
  410. }
  411. } else if (chan->state == S3C2410_DMA_IDLE) {
  412. if (chan->flags & S3C2410_DMAF_AUTOSTART) {
  413. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_START);
  414. }
  415. }
  416. local_irq_restore(flags);
  417. return 0;
  418. }
  419. EXPORT_SYMBOL(s3c2410_dma_enqueue);
  420. static inline void
  421. s3c2410_dma_freebuf(struct s3c2410_dma_buf *buf)
  422. {
  423. int magicok = (buf->magic == BUF_MAGIC);
  424. buf->magic = -1;
  425. if (magicok) {
  426. kmem_cache_free(dma_kmem, buf);
  427. } else {
  428. printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
  429. }
  430. }
  431. /* s3c2410_dma_lastxfer
  432. *
  433. * called when the system is out of buffers, to ensure that the channel
  434. * is prepared for shutdown.
  435. */
  436. static inline void
  437. s3c2410_dma_lastxfer(struct s3c2410_dma_chan *chan)
  438. {
  439. #if 0
  440. pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
  441. chan->number, chan->load_state);
  442. #endif
  443. switch (chan->load_state) {
  444. case S3C2410_DMALOAD_NONE:
  445. break;
  446. case S3C2410_DMALOAD_1LOADED:
  447. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  448. /* flag error? */
  449. printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
  450. chan->number, __FUNCTION__);
  451. return;
  452. }
  453. break;
  454. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  455. /* I belive in this case we do not have anything to do
  456. * until the next buffer comes along, and we turn off the
  457. * reload */
  458. return;
  459. default:
  460. pr_debug("dma%d: lastxfer: unhandled load_state %d with no next\n",
  461. chan->number, chan->load_state);
  462. return;
  463. }
  464. /* hopefully this'll shut the damned thing up after the transfer... */
  465. dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
  466. }
  467. #define dmadbg2(x...)
  468. static irqreturn_t
  469. s3c2410_dma_irq(int irq, void *devpw)
  470. {
  471. struct s3c2410_dma_chan *chan = (struct s3c2410_dma_chan *)devpw;
  472. struct s3c2410_dma_buf *buf;
  473. buf = chan->curr;
  474. dbg_showchan(chan);
  475. /* modify the channel state */
  476. switch (chan->load_state) {
  477. case S3C2410_DMALOAD_1RUNNING:
  478. /* TODO - if we are running only one buffer, we probably
  479. * want to reload here, and then worry about the buffer
  480. * callback */
  481. chan->load_state = S3C2410_DMALOAD_NONE;
  482. break;
  483. case S3C2410_DMALOAD_1LOADED:
  484. /* iirc, we should go back to NONE loaded here, we
  485. * had a buffer, and it was never verified as being
  486. * loaded.
  487. */
  488. chan->load_state = S3C2410_DMALOAD_NONE;
  489. break;
  490. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  491. /* we'll worry about checking to see if another buffer is
  492. * ready after we've called back the owner. This should
  493. * ensure we do not wait around too long for the DMA
  494. * engine to start the next transfer
  495. */
  496. chan->load_state = S3C2410_DMALOAD_1LOADED;
  497. break;
  498. case S3C2410_DMALOAD_NONE:
  499. printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
  500. chan->number);
  501. break;
  502. default:
  503. printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
  504. chan->number, chan->load_state);
  505. break;
  506. }
  507. if (buf != NULL) {
  508. /* update the chain to make sure that if we load any more
  509. * buffers when we call the callback function, things should
  510. * work properly */
  511. chan->curr = buf->next;
  512. buf->next = NULL;
  513. if (buf->magic != BUF_MAGIC) {
  514. printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
  515. chan->number, __FUNCTION__, buf);
  516. return IRQ_HANDLED;
  517. }
  518. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
  519. /* free resouces */
  520. s3c2410_dma_freebuf(buf);
  521. } else {
  522. }
  523. /* only reload if the channel is still running... our buffer done
  524. * routine may have altered the state by requesting the dma channel
  525. * to stop or shutdown... */
  526. /* todo: check that when the channel is shut-down from inside this
  527. * function, we cope with unsetting reload, etc */
  528. if (chan->next != NULL && chan->state != S3C2410_DMA_IDLE) {
  529. unsigned long flags;
  530. switch (chan->load_state) {
  531. case S3C2410_DMALOAD_1RUNNING:
  532. /* don't need to do anything for this state */
  533. break;
  534. case S3C2410_DMALOAD_NONE:
  535. /* can load buffer immediately */
  536. break;
  537. case S3C2410_DMALOAD_1LOADED:
  538. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  539. /* flag error? */
  540. printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
  541. chan->number, __FUNCTION__);
  542. return IRQ_HANDLED;
  543. }
  544. break;
  545. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  546. goto no_load;
  547. default:
  548. printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
  549. chan->number, chan->load_state);
  550. return IRQ_HANDLED;
  551. }
  552. local_irq_save(flags);
  553. s3c2410_dma_loadbuffer(chan, chan->next);
  554. local_irq_restore(flags);
  555. } else {
  556. s3c2410_dma_lastxfer(chan);
  557. /* see if we can stop this channel.. */
  558. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  559. pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
  560. chan->number, jiffies);
  561. s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
  562. S3C2410_DMAOP_STOP);
  563. }
  564. }
  565. no_load:
  566. return IRQ_HANDLED;
  567. }
  568. static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel);
  569. /* s3c2410_request_dma
  570. *
  571. * get control of an dma channel
  572. */
  573. int s3c2410_dma_request(unsigned int channel,
  574. struct s3c2410_dma_client *client,
  575. void *dev)
  576. {
  577. struct s3c2410_dma_chan *chan;
  578. unsigned long flags;
  579. int err;
  580. pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
  581. channel, client->name, dev);
  582. local_irq_save(flags);
  583. chan = s3c2410_dma_map_channel(channel);
  584. if (chan == NULL) {
  585. local_irq_restore(flags);
  586. return -EBUSY;
  587. }
  588. dbg_showchan(chan);
  589. chan->client = client;
  590. chan->in_use = 1;
  591. if (!chan->irq_claimed) {
  592. pr_debug("dma%d: %s : requesting irq %d\n",
  593. channel, __FUNCTION__, chan->irq);
  594. chan->irq_claimed = 1;
  595. local_irq_restore(flags);
  596. err = request_irq(chan->irq, s3c2410_dma_irq, IRQF_DISABLED,
  597. client->name, (void *)chan);
  598. local_irq_save(flags);
  599. if (err) {
  600. chan->in_use = 0;
  601. chan->irq_claimed = 0;
  602. local_irq_restore(flags);
  603. printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
  604. client->name, chan->irq, chan->number);
  605. return err;
  606. }
  607. chan->irq_enabled = 1;
  608. }
  609. local_irq_restore(flags);
  610. /* need to setup */
  611. pr_debug("%s: channel initialised, %p\n", __FUNCTION__, chan);
  612. return 0;
  613. }
  614. EXPORT_SYMBOL(s3c2410_dma_request);
  615. /* s3c2410_dma_free
  616. *
  617. * release the given channel back to the system, will stop and flush
  618. * any outstanding transfers, and ensure the channel is ready for the
  619. * next claimant.
  620. *
  621. * Note, although a warning is currently printed if the freeing client
  622. * info is not the same as the registrant's client info, the free is still
  623. * allowed to go through.
  624. */
  625. int s3c2410_dma_free(dmach_t channel, struct s3c2410_dma_client *client)
  626. {
  627. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  628. unsigned long flags;
  629. if (chan == NULL)
  630. return -EINVAL;
  631. local_irq_save(flags);
  632. if (chan->client != client) {
  633. printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
  634. channel, chan->client, client);
  635. }
  636. /* sort out stopping and freeing the channel */
  637. if (chan->state != S3C2410_DMA_IDLE) {
  638. pr_debug("%s: need to stop dma channel %p\n",
  639. __FUNCTION__, chan);
  640. /* possibly flush the channel */
  641. s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
  642. }
  643. chan->client = NULL;
  644. chan->in_use = 0;
  645. if (chan->irq_claimed)
  646. free_irq(chan->irq, (void *)chan);
  647. chan->irq_claimed = 0;
  648. if (!(channel & DMACH_LOW_LEVEL))
  649. dma_chan_map[channel] = NULL;
  650. local_irq_restore(flags);
  651. return 0;
  652. }
  653. EXPORT_SYMBOL(s3c2410_dma_free);
  654. static int s3c2410_dma_dostop(struct s3c2410_dma_chan *chan)
  655. {
  656. unsigned long flags;
  657. unsigned long tmp;
  658. pr_debug("%s:\n", __FUNCTION__);
  659. dbg_showchan(chan);
  660. local_irq_save(flags);
  661. s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
  662. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  663. tmp |= S3C2410_DMASKTRIG_STOP;
  664. //tmp &= ~S3C2410_DMASKTRIG_ON;
  665. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  666. #if 0
  667. /* should also clear interrupts, according to WinCE BSP */
  668. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  669. tmp |= S3C2410_DCON_NORELOAD;
  670. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  671. #endif
  672. /* should stop do this, or should we wait for flush? */
  673. chan->state = S3C2410_DMA_IDLE;
  674. chan->load_state = S3C2410_DMALOAD_NONE;
  675. local_irq_restore(flags);
  676. return 0;
  677. }
  678. void s3c2410_dma_waitforstop(struct s3c2410_dma_chan *chan)
  679. {
  680. unsigned long tmp;
  681. unsigned int timeout = 0x10000;
  682. while (timeout-- > 0) {
  683. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  684. if (!(tmp & S3C2410_DMASKTRIG_ON))
  685. return;
  686. }
  687. pr_debug("dma%d: failed to stop?\n", chan->number);
  688. }
  689. /* s3c2410_dma_flush
  690. *
  691. * stop the channel, and remove all current and pending transfers
  692. */
  693. static int s3c2410_dma_flush(struct s3c2410_dma_chan *chan)
  694. {
  695. struct s3c2410_dma_buf *buf, *next;
  696. unsigned long flags;
  697. pr_debug("%s: chan %p (%d)\n", __FUNCTION__, chan, chan->number);
  698. dbg_showchan(chan);
  699. local_irq_save(flags);
  700. if (chan->state != S3C2410_DMA_IDLE) {
  701. pr_debug("%s: stopping channel...\n", __FUNCTION__ );
  702. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
  703. }
  704. buf = chan->curr;
  705. if (buf == NULL)
  706. buf = chan->next;
  707. chan->curr = chan->next = chan->end = NULL;
  708. if (buf != NULL) {
  709. for ( ; buf != NULL; buf = next) {
  710. next = buf->next;
  711. pr_debug("%s: free buffer %p, next %p\n",
  712. __FUNCTION__, buf, buf->next);
  713. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
  714. s3c2410_dma_freebuf(buf);
  715. }
  716. }
  717. dbg_showregs(chan);
  718. s3c2410_dma_waitforstop(chan);
  719. #if 0
  720. /* should also clear interrupts, according to WinCE BSP */
  721. {
  722. unsigned long tmp;
  723. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  724. tmp |= S3C2410_DCON_NORELOAD;
  725. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  726. }
  727. #endif
  728. dbg_showregs(chan);
  729. local_irq_restore(flags);
  730. return 0;
  731. }
  732. int
  733. s3c2410_dma_started(struct s3c2410_dma_chan *chan)
  734. {
  735. unsigned long flags;
  736. local_irq_save(flags);
  737. dbg_showchan(chan);
  738. /* if we've only loaded one buffer onto the channel, then chec
  739. * to see if we have another, and if so, try and load it so when
  740. * the first buffer is finished, the new one will be loaded onto
  741. * the channel */
  742. if (chan->next != NULL) {
  743. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  744. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  745. pr_debug("%s: buff not yet loaded, no more todo\n",
  746. __FUNCTION__);
  747. } else {
  748. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  749. s3c2410_dma_loadbuffer(chan, chan->next);
  750. }
  751. } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
  752. s3c2410_dma_loadbuffer(chan, chan->next);
  753. }
  754. }
  755. local_irq_restore(flags);
  756. return 0;
  757. }
  758. int
  759. s3c2410_dma_ctrl(dmach_t channel, enum s3c2410_chan_op op)
  760. {
  761. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  762. if (chan == NULL)
  763. return -EINVAL;
  764. switch (op) {
  765. case S3C2410_DMAOP_START:
  766. return s3c2410_dma_start(chan);
  767. case S3C2410_DMAOP_STOP:
  768. return s3c2410_dma_dostop(chan);
  769. case S3C2410_DMAOP_PAUSE:
  770. case S3C2410_DMAOP_RESUME:
  771. return -ENOENT;
  772. case S3C2410_DMAOP_FLUSH:
  773. return s3c2410_dma_flush(chan);
  774. case S3C2410_DMAOP_STARTED:
  775. return s3c2410_dma_started(chan);
  776. case S3C2410_DMAOP_TIMEOUT:
  777. return 0;
  778. }
  779. return -ENOENT; /* unknown, don't bother */
  780. }
  781. EXPORT_SYMBOL(s3c2410_dma_ctrl);
  782. /* DMA configuration for each channel
  783. *
  784. * DISRCC -> source of the DMA (AHB,APB)
  785. * DISRC -> source address of the DMA
  786. * DIDSTC -> destination of the DMA (AHB,APD)
  787. * DIDST -> destination address of the DMA
  788. */
  789. /* s3c2410_dma_config
  790. *
  791. * xfersize: size of unit in bytes (1,2,4)
  792. * dcon: base value of the DCONx register
  793. */
  794. int s3c2410_dma_config(dmach_t channel,
  795. int xferunit,
  796. int dcon)
  797. {
  798. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  799. pr_debug("%s: chan=%d, xfer_unit=%d, dcon=%08x\n",
  800. __FUNCTION__, channel, xferunit, dcon);
  801. if (chan == NULL)
  802. return -EINVAL;
  803. printk("Initial dcon is %08x\n", dcon);
  804. dcon |= chan->dcon & dma_sel.dcon_mask;
  805. printk("New dcon is %08x\n", dcon);
  806. switch (xferunit) {
  807. case 1:
  808. dcon |= S3C2410_DCON_BYTE;
  809. break;
  810. case 2:
  811. dcon |= S3C2410_DCON_HALFWORD;
  812. break;
  813. case 4:
  814. dcon |= S3C2410_DCON_WORD;
  815. break;
  816. default:
  817. pr_debug("%s: bad transfer size %d\n", __FUNCTION__, xferunit);
  818. return -EINVAL;
  819. }
  820. dcon |= S3C2410_DCON_HWTRIG;
  821. dcon |= S3C2410_DCON_INTREQ;
  822. pr_debug("%s: dcon now %08x\n", __FUNCTION__, dcon);
  823. chan->dcon = dcon;
  824. chan->xfer_unit = xferunit;
  825. return 0;
  826. }
  827. EXPORT_SYMBOL(s3c2410_dma_config);
  828. int s3c2410_dma_setflags(dmach_t channel, unsigned int flags)
  829. {
  830. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  831. if (chan == NULL)
  832. return -EINVAL;
  833. pr_debug("%s: chan=%p, flags=%08x\n", __FUNCTION__, chan, flags);
  834. chan->flags = flags;
  835. return 0;
  836. }
  837. EXPORT_SYMBOL(s3c2410_dma_setflags);
  838. /* do we need to protect the settings of the fields from
  839. * irq?
  840. */
  841. int s3c2410_dma_set_opfn(dmach_t channel, s3c2410_dma_opfn_t rtn)
  842. {
  843. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  844. if (chan == NULL)
  845. return -EINVAL;
  846. pr_debug("%s: chan=%p, op rtn=%p\n", __FUNCTION__, chan, rtn);
  847. chan->op_fn = rtn;
  848. return 0;
  849. }
  850. EXPORT_SYMBOL(s3c2410_dma_set_opfn);
  851. int s3c2410_dma_set_buffdone_fn(dmach_t channel, s3c2410_dma_cbfn_t rtn)
  852. {
  853. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  854. if (chan == NULL)
  855. return -EINVAL;
  856. pr_debug("%s: chan=%p, callback rtn=%p\n", __FUNCTION__, chan, rtn);
  857. chan->callback_fn = rtn;
  858. return 0;
  859. }
  860. EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);
  861. /* s3c2410_dma_devconfig
  862. *
  863. * configure the dma source/destination hardware type and address
  864. *
  865. * source: S3C2410_DMASRC_HW: source is hardware
  866. * S3C2410_DMASRC_MEM: source is memory
  867. *
  868. * hwcfg: the value for xxxSTCn register,
  869. * bit 0: 0=increment pointer, 1=leave pointer
  870. * bit 1: 0=soucre is AHB, 1=soucre is APB
  871. *
  872. * devaddr: physical address of the source
  873. */
  874. int s3c2410_dma_devconfig(int channel,
  875. enum s3c2410_dmasrc source,
  876. int hwcfg,
  877. unsigned long devaddr)
  878. {
  879. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  880. if (chan == NULL)
  881. return -EINVAL;
  882. pr_debug("%s: source=%d, hwcfg=%08x, devaddr=%08lx\n",
  883. __FUNCTION__, (int)source, hwcfg, devaddr);
  884. chan->source = source;
  885. chan->dev_addr = devaddr;
  886. switch (source) {
  887. case S3C2410_DMASRC_HW:
  888. /* source is hardware */
  889. pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
  890. __FUNCTION__, devaddr, hwcfg);
  891. dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
  892. dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
  893. dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
  894. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
  895. return 0;
  896. case S3C2410_DMASRC_MEM:
  897. /* source is memory */
  898. pr_debug( "%s: mem source, devaddr=%08lx, hwcfg=%d\n",
  899. __FUNCTION__, devaddr, hwcfg);
  900. dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
  901. dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
  902. dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
  903. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
  904. return 0;
  905. }
  906. printk(KERN_ERR "dma%d: invalid source type (%d)\n", channel, source);
  907. return -EINVAL;
  908. }
  909. EXPORT_SYMBOL(s3c2410_dma_devconfig);
  910. /* s3c2410_dma_getposition
  911. *
  912. * returns the current transfer points for the dma source and destination
  913. */
  914. int s3c2410_dma_getposition(dmach_t channel, dma_addr_t *src, dma_addr_t *dst)
  915. {
  916. struct s3c2410_dma_chan *chan = lookup_dma_channel(channel);
  917. if (chan == NULL)
  918. return -EINVAL;
  919. if (src != NULL)
  920. *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  921. if (dst != NULL)
  922. *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
  923. return 0;
  924. }
  925. EXPORT_SYMBOL(s3c2410_dma_getposition);
  926. /* system device class */
  927. #ifdef CONFIG_PM
  928. static int s3c2410_dma_suspend(struct sys_device *dev, pm_message_t state)
  929. {
  930. struct s3c2410_dma_chan *cp = container_of(dev, struct s3c2410_dma_chan, dev);
  931. printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
  932. if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
  933. /* the dma channel is still working, which is probably
  934. * a bad thing to do over suspend/resume. We stop the
  935. * channel and assume that the client is either going to
  936. * retry after resume, or that it is broken.
  937. */
  938. printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
  939. cp->number);
  940. s3c2410_dma_dostop(cp);
  941. }
  942. return 0;
  943. }
  944. static int s3c2410_dma_resume(struct sys_device *dev)
  945. {
  946. return 0;
  947. }
  948. #else
  949. #define s3c2410_dma_suspend NULL
  950. #define s3c2410_dma_resume NULL
  951. #endif /* CONFIG_PM */
  952. struct sysdev_class dma_sysclass = {
  953. set_kset_name("s3c24xx-dma"),
  954. .suspend = s3c2410_dma_suspend,
  955. .resume = s3c2410_dma_resume,
  956. };
  957. /* kmem cache implementation */
  958. static void s3c2410_dma_cache_ctor(void *p, kmem_cache_t *c, unsigned long f)
  959. {
  960. memset(p, 0, sizeof(struct s3c2410_dma_buf));
  961. }
  962. /* initialisation code */
  963. static int __init s3c2410_init_dma(void)
  964. {
  965. struct s3c2410_dma_chan *cp;
  966. int channel;
  967. int ret;
  968. printk("S3C24XX DMA Driver, (c) 2003-2004,2006 Simtec Electronics\n");
  969. dma_base = ioremap(S3C24XX_PA_DMA, 0x200);
  970. if (dma_base == NULL) {
  971. printk(KERN_ERR "dma failed to remap register block\n");
  972. return -ENOMEM;
  973. }
  974. printk("Registering sysclass\n");
  975. ret = sysdev_class_register(&dma_sysclass);
  976. if (ret != 0) {
  977. printk(KERN_ERR "dma sysclass registration failed\n");
  978. goto err;
  979. }
  980. dma_kmem = kmem_cache_create("dma_desc", sizeof(struct s3c2410_dma_buf), 0,
  981. SLAB_HWCACHE_ALIGN,
  982. s3c2410_dma_cache_ctor, NULL);
  983. if (dma_kmem == NULL) {
  984. printk(KERN_ERR "dma failed to make kmem cache\n");
  985. ret = -ENOMEM;
  986. goto err;
  987. }
  988. for (channel = 0; channel < S3C2410_DMA_CHANNELS; channel++) {
  989. cp = &s3c2410_chans[channel];
  990. memset(cp, 0, sizeof(struct s3c2410_dma_chan));
  991. /* dma channel irqs are in order.. */
  992. cp->number = channel;
  993. cp->irq = channel + IRQ_DMA0;
  994. cp->regs = dma_base + (channel*0x40);
  995. /* point current stats somewhere */
  996. cp->stats = &cp->stats_store;
  997. cp->stats_store.timeout_shortest = LONG_MAX;
  998. /* basic channel configuration */
  999. cp->load_timeout = 1<<18;
  1000. /* register system device */
  1001. cp->dev.cls = &dma_sysclass;
  1002. cp->dev.id = channel;
  1003. ret = sysdev_register(&cp->dev);
  1004. printk("DMA channel %d at %p, irq %d\n",
  1005. cp->number, cp->regs, cp->irq);
  1006. }
  1007. return 0;
  1008. err:
  1009. kmem_cache_destroy(dma_kmem);
  1010. iounmap(dma_base);
  1011. dma_base = NULL;
  1012. return ret;
  1013. }
  1014. core_initcall(s3c2410_init_dma);
  1015. static inline int is_channel_valid(unsigned int channel)
  1016. {
  1017. return (channel & DMA_CH_VALID);
  1018. }
  1019. /* s3c2410_dma_map_channel()
  1020. *
  1021. * turn the virtual channel number into a real, and un-used hardware
  1022. * channel.
  1023. *
  1024. * currently this code uses first-free channel from the specified harware
  1025. * map, not taking into account anything that the board setup code may
  1026. * have to say about the likely peripheral set to be in use.
  1027. */
  1028. struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel)
  1029. {
  1030. struct s3c24xx_dma_map *ch_map;
  1031. struct s3c2410_dma_chan *dmach;
  1032. int ch;
  1033. if (dma_sel.map == NULL || channel > dma_sel.map_size)
  1034. return NULL;
  1035. ch_map = dma_sel.map + channel;
  1036. for (ch = 0; ch < S3C2410_DMA_CHANNELS; ch++) {
  1037. if (!is_channel_valid(ch_map->channels[ch]))
  1038. continue;
  1039. if (s3c2410_chans[ch].in_use == 0) {
  1040. printk("mapped channel %d to %d\n", channel, ch);
  1041. break;
  1042. }
  1043. }
  1044. if (ch >= S3C2410_DMA_CHANNELS)
  1045. return NULL;
  1046. /* update our channel mapping */
  1047. dmach = &s3c2410_chans[ch];
  1048. dma_chan_map[channel] = dmach;
  1049. /* select the channel */
  1050. (dma_sel.select)(dmach, ch_map);
  1051. return dmach;
  1052. }
  1053. static void s3c24xx_dma_show_ch(struct s3c24xx_dma_map *map, int ch)
  1054. {
  1055. /* show the channel configuration */
  1056. printk("%2d: %20s, channels %c%c%c%c\n", ch, map->name,
  1057. (is_channel_valid(map->channels[0]) ? '0' : '-'),
  1058. (is_channel_valid(map->channels[1]) ? '1' : '-'),
  1059. (is_channel_valid(map->channels[2]) ? '2' : '-'),
  1060. (is_channel_valid(map->channels[3]) ? '3' : '-'));
  1061. }
  1062. static int s3c24xx_dma_check_entry(struct s3c24xx_dma_map *map, int ch)
  1063. {
  1064. if (1)
  1065. s3c24xx_dma_show_ch(map, ch);
  1066. return 0;
  1067. }
  1068. int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)
  1069. {
  1070. struct s3c24xx_dma_map *nmap;
  1071. size_t map_sz = sizeof(*nmap) * sel->map_size;
  1072. int ptr;
  1073. nmap = kmalloc(map_sz, GFP_KERNEL);
  1074. if (nmap == NULL)
  1075. return -ENOMEM;
  1076. memcpy(nmap, sel->map, map_sz);
  1077. memcpy(&dma_sel, sel, sizeof(*sel));
  1078. dma_sel.map = nmap;
  1079. for (ptr = 0; ptr < sel->map_size; ptr++)
  1080. s3c24xx_dma_check_entry(nmap+ptr, ptr);
  1081. return 0;
  1082. }