dma.c 32 KB

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