dma.c 34 KB

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