dma.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /* linux/arch/arm/plat-s3c24xx/dma.c
  2. *
  3. * Copyright 2003-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/syscore_ops.h>
  23. #include <linux/slab.h>
  24. #include <linux/errno.h>
  25. #include <linux/io.h>
  26. #include <asm/system.h>
  27. #include <asm/irq.h>
  28. #include <mach/hardware.h>
  29. #include <mach/dma.h>
  30. #include <mach/map.h>
  31. #include <plat/dma-s3c24xx.h>
  32. #include <plat/regs-dma.h>
  33. /* io map for dma */
  34. static void __iomem *dma_base;
  35. static struct kmem_cache *dma_kmem;
  36. static int dma_channels;
  37. static struct s3c24xx_dma_selection dma_sel;
  38. /* debugging functions */
  39. #define BUF_MAGIC (0xcafebabe)
  40. #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
  41. #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
  42. #if 1
  43. #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
  44. #else
  45. static inline void
  46. dma_wrreg(struct s3c2410_dma_chan *chan, int reg, unsigned long val)
  47. {
  48. pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
  49. writel(val, dma_regaddr(chan, reg));
  50. }
  51. #endif
  52. #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
  53. /* captured register state for debug */
  54. struct s3c2410_dma_regstate {
  55. unsigned long dcsrc;
  56. unsigned long disrc;
  57. unsigned long dstat;
  58. unsigned long dcon;
  59. unsigned long dmsktrig;
  60. };
  61. #ifdef CONFIG_S3C2410_DMA_DEBUG
  62. /* dmadbg_showregs
  63. *
  64. * simple debug routine to print the current state of the dma registers
  65. */
  66. static void
  67. dmadbg_capture(struct s3c2410_dma_chan *chan, struct s3c2410_dma_regstate *regs)
  68. {
  69. regs->dcsrc = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  70. regs->disrc = dma_rdreg(chan, S3C2410_DMA_DISRC);
  71. regs->dstat = dma_rdreg(chan, S3C2410_DMA_DSTAT);
  72. regs->dcon = dma_rdreg(chan, S3C2410_DMA_DCON);
  73. regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  74. }
  75. static void
  76. dmadbg_dumpregs(const char *fname, int line, struct s3c2410_dma_chan *chan,
  77. struct s3c2410_dma_regstate *regs)
  78. {
  79. printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
  80. chan->number, fname, line,
  81. regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
  82. regs->dcon);
  83. }
  84. static void
  85. dmadbg_showchan(const char *fname, int line, struct s3c2410_dma_chan *chan)
  86. {
  87. struct s3c2410_dma_regstate state;
  88. dmadbg_capture(chan, &state);
  89. printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
  90. chan->number, fname, line, chan->load_state,
  91. chan->curr, chan->next, chan->end);
  92. dmadbg_dumpregs(fname, line, chan, &state);
  93. }
  94. static void
  95. dmadbg_showregs(const char *fname, int line, struct s3c2410_dma_chan *chan)
  96. {
  97. struct s3c2410_dma_regstate state;
  98. dmadbg_capture(chan, &state);
  99. dmadbg_dumpregs(fname, line, chan, &state);
  100. }
  101. #define dbg_showregs(chan) dmadbg_showregs(__func__, __LINE__, (chan))
  102. #define dbg_showchan(chan) dmadbg_showchan(__func__, __LINE__, (chan))
  103. #else
  104. #define dbg_showregs(chan) do { } while(0)
  105. #define dbg_showchan(chan) do { } while(0)
  106. #endif /* CONFIG_S3C2410_DMA_DEBUG */
  107. /* s3c2410_dma_stats_timeout
  108. *
  109. * Update DMA stats from timeout info
  110. */
  111. static void
  112. s3c2410_dma_stats_timeout(struct s3c2410_dma_stats *stats, int val)
  113. {
  114. if (stats == NULL)
  115. return;
  116. if (val > stats->timeout_longest)
  117. stats->timeout_longest = val;
  118. if (val < stats->timeout_shortest)
  119. stats->timeout_shortest = val;
  120. stats->timeout_avg += val;
  121. }
  122. /* s3c2410_dma_waitforload
  123. *
  124. * wait for the DMA engine to load a buffer, and update the state accordingly
  125. */
  126. static int
  127. s3c2410_dma_waitforload(struct s3c2410_dma_chan *chan, int line)
  128. {
  129. int timeout = chan->load_timeout;
  130. int took;
  131. if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
  132. printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
  133. return 0;
  134. }
  135. if (chan->stats != NULL)
  136. chan->stats->loads++;
  137. while (--timeout > 0) {
  138. if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
  139. took = chan->load_timeout - timeout;
  140. s3c2410_dma_stats_timeout(chan->stats, took);
  141. switch (chan->load_state) {
  142. case S3C2410_DMALOAD_1LOADED:
  143. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  144. break;
  145. default:
  146. printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
  147. }
  148. return 1;
  149. }
  150. }
  151. if (chan->stats != NULL) {
  152. chan->stats->timeout_failed++;
  153. }
  154. return 0;
  155. }
  156. /* s3c2410_dma_loadbuffer
  157. *
  158. * load a buffer, and update the channel state
  159. */
  160. static inline int
  161. s3c2410_dma_loadbuffer(struct s3c2410_dma_chan *chan,
  162. struct s3c2410_dma_buf *buf)
  163. {
  164. unsigned long reload;
  165. if (buf == NULL) {
  166. dmawarn("buffer is NULL\n");
  167. return -EINVAL;
  168. }
  169. pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
  170. buf, (unsigned long)buf->data, buf->size);
  171. /* check the state of the channel before we do anything */
  172. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  173. dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
  174. }
  175. if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
  176. dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
  177. }
  178. /* it would seem sensible if we are the last buffer to not bother
  179. * with the auto-reload bit, so that the DMA engine will not try
  180. * and load another transfer after this one has finished...
  181. */
  182. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  183. pr_debug("load_state is none, checking for noreload (next=%p)\n",
  184. buf->next);
  185. reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
  186. } else {
  187. //pr_debug("load_state is %d => autoreload\n", chan->load_state);
  188. reload = S3C2410_DCON_AUTORELOAD;
  189. }
  190. if ((buf->data & 0xf0000000) != 0x30000000) {
  191. dmawarn("dmaload: buffer is %p\n", (void *)buf->data);
  192. }
  193. writel(buf->data, chan->addr_reg);
  194. dma_wrreg(chan, S3C2410_DMA_DCON,
  195. chan->dcon | reload | (buf->size/chan->xfer_unit));
  196. chan->next = buf->next;
  197. /* update the state of the channel */
  198. switch (chan->load_state) {
  199. case S3C2410_DMALOAD_NONE:
  200. chan->load_state = S3C2410_DMALOAD_1LOADED;
  201. break;
  202. case S3C2410_DMALOAD_1RUNNING:
  203. chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
  204. break;
  205. default:
  206. dmawarn("dmaload: unknown state %d in loadbuffer\n",
  207. chan->load_state);
  208. break;
  209. }
  210. return 0;
  211. }
  212. /* s3c2410_dma_call_op
  213. *
  214. * small routine to call the op routine with the given op if it has been
  215. * registered
  216. */
  217. static void
  218. s3c2410_dma_call_op(struct s3c2410_dma_chan *chan, enum s3c2410_chan_op op)
  219. {
  220. if (chan->op_fn != NULL) {
  221. (chan->op_fn)(chan, op);
  222. }
  223. }
  224. /* s3c2410_dma_buffdone
  225. *
  226. * small wrapper to check if callback routine needs to be called, and
  227. * if so, call it
  228. */
  229. static inline void
  230. s3c2410_dma_buffdone(struct s3c2410_dma_chan *chan, struct s3c2410_dma_buf *buf,
  231. enum s3c2410_dma_buffresult result)
  232. {
  233. #if 0
  234. pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
  235. chan->callback_fn, buf, buf->id, buf->size, result);
  236. #endif
  237. if (chan->callback_fn != NULL) {
  238. (chan->callback_fn)(chan, buf->id, buf->size, result);
  239. }
  240. }
  241. /* s3c2410_dma_start
  242. *
  243. * start a dma channel going
  244. */
  245. static int s3c2410_dma_start(struct s3c2410_dma_chan *chan)
  246. {
  247. unsigned long tmp;
  248. unsigned long flags;
  249. pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
  250. local_irq_save(flags);
  251. if (chan->state == S3C2410_DMA_RUNNING) {
  252. pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
  253. local_irq_restore(flags);
  254. return 0;
  255. }
  256. chan->state = S3C2410_DMA_RUNNING;
  257. /* check wether there is anything to load, and if not, see
  258. * if we can find anything to load
  259. */
  260. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  261. if (chan->next == NULL) {
  262. printk(KERN_ERR "dma%d: channel has nothing loaded\n",
  263. chan->number);
  264. chan->state = S3C2410_DMA_IDLE;
  265. local_irq_restore(flags);
  266. return -EINVAL;
  267. }
  268. s3c2410_dma_loadbuffer(chan, chan->next);
  269. }
  270. dbg_showchan(chan);
  271. /* enable the channel */
  272. if (!chan->irq_enabled) {
  273. enable_irq(chan->irq);
  274. chan->irq_enabled = 1;
  275. }
  276. /* start the channel going */
  277. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  278. tmp &= ~S3C2410_DMASKTRIG_STOP;
  279. tmp |= S3C2410_DMASKTRIG_ON;
  280. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  281. pr_debug("dma%d: %08lx to DMASKTRIG\n", chan->number, tmp);
  282. #if 0
  283. /* the dma buffer loads should take care of clearing the AUTO
  284. * reloading feature */
  285. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  286. tmp &= ~S3C2410_DCON_NORELOAD;
  287. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  288. #endif
  289. s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
  290. dbg_showchan(chan);
  291. /* if we've only loaded one buffer onto the channel, then chec
  292. * to see if we have another, and if so, try and load it so when
  293. * the first buffer is finished, the new one will be loaded onto
  294. * the channel */
  295. if (chan->next != NULL) {
  296. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  297. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  298. pr_debug("%s: buff not yet loaded, no more todo\n",
  299. __func__);
  300. } else {
  301. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  302. s3c2410_dma_loadbuffer(chan, chan->next);
  303. }
  304. } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
  305. s3c2410_dma_loadbuffer(chan, chan->next);
  306. }
  307. }
  308. local_irq_restore(flags);
  309. return 0;
  310. }
  311. /* s3c2410_dma_canload
  312. *
  313. * work out if we can queue another buffer into the DMA engine
  314. */
  315. static int
  316. s3c2410_dma_canload(struct s3c2410_dma_chan *chan)
  317. {
  318. if (chan->load_state == S3C2410_DMALOAD_NONE ||
  319. chan->load_state == S3C2410_DMALOAD_1RUNNING)
  320. return 1;
  321. return 0;
  322. }
  323. /* s3c2410_dma_enqueue
  324. *
  325. * queue an given buffer for dma transfer.
  326. *
  327. * id the device driver's id information for this buffer
  328. * data the physical address of the buffer data
  329. * size the size of the buffer in bytes
  330. *
  331. * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
  332. * is checked, and if set, the channel is started. If this flag isn't set,
  333. * then an error will be returned.
  334. *
  335. * It is possible to queue more than one DMA buffer onto a channel at
  336. * once, and the code will deal with the re-loading of the next buffer
  337. * when necessary.
  338. */
  339. int s3c2410_dma_enqueue(unsigned int channel, void *id,
  340. dma_addr_t data, int size)
  341. {
  342. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  343. struct s3c2410_dma_buf *buf;
  344. unsigned long flags;
  345. if (chan == NULL)
  346. return -EINVAL;
  347. pr_debug("%s: id=%p, data=%08x, size=%d\n",
  348. __func__, id, (unsigned int)data, size);
  349. buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
  350. if (buf == NULL) {
  351. pr_debug("%s: out of memory (%ld alloc)\n",
  352. __func__, (long)sizeof(*buf));
  353. return -ENOMEM;
  354. }
  355. //pr_debug("%s: new buffer %p\n", __func__, buf);
  356. //dbg_showchan(chan);
  357. buf->next = NULL;
  358. buf->data = buf->ptr = data;
  359. buf->size = size;
  360. buf->id = id;
  361. buf->magic = BUF_MAGIC;
  362. local_irq_save(flags);
  363. if (chan->curr == NULL) {
  364. /* we've got nothing loaded... */
  365. pr_debug("%s: buffer %p queued onto empty channel\n",
  366. __func__, buf);
  367. chan->curr = buf;
  368. chan->end = buf;
  369. chan->next = NULL;
  370. } else {
  371. pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
  372. chan->number, __func__, buf);
  373. if (chan->end == NULL)
  374. pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
  375. chan->number, __func__, chan);
  376. chan->end->next = buf;
  377. chan->end = buf;
  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: S3C2410_DMASRC_HW: source is hardware
  836. * S3C2410_DMASRC_MEM: source is memory
  837. *
  838. * devaddr: physical address of the source
  839. */
  840. int s3c2410_dma_devconfig(enum dma_ch channel,
  841. enum s3c2410_dmasrc 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 S3C2410_DMASRC_HW:
  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 S3C2410_DMASRC_MEM:
  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 = kmalloc(map_sz, GFP_KERNEL);
  1090. if (nmap == NULL)
  1091. return -ENOMEM;
  1092. memcpy(nmap, sel->map, map_sz);
  1093. memcpy(&dma_sel, sel, sizeof(*sel));
  1094. dma_sel.map = nmap;
  1095. for (ptr = 0; ptr < sel->map_size; ptr++)
  1096. s3c24xx_dma_check_entry(nmap+ptr, ptr);
  1097. return 0;
  1098. }
  1099. int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)
  1100. {
  1101. struct s3c24xx_dma_order *nord = dma_order;
  1102. if (nord == NULL)
  1103. nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);
  1104. if (nord == NULL) {
  1105. printk(KERN_ERR "no memory to store dma channel order\n");
  1106. return -ENOMEM;
  1107. }
  1108. dma_order = nord;
  1109. memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));
  1110. return 0;
  1111. }