dma.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /* linux/arch/arm/mach-bast/dma.c
  2. *
  3. * (c) 2003-2005 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 DMA core
  7. *
  8. * http://www.simtec.co.uk/products/EB2410ITX/
  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. * Changelog:
  15. * 27-Feb-2005 BJD Added kmem cache for dma descriptors
  16. * 18-Nov-2004 BJD Removed error for loading onto stopped channel
  17. * 10-Nov-2004 BJD Ensure all external symbols exported for modules
  18. * 10-Nov-2004 BJD Use sys_device and sysdev_class for power management
  19. * 08-Aug-2004 BJD Apply rmk's suggestions
  20. * 21-Jul-2004 BJD Ported to linux 2.6
  21. * 12-Jul-2004 BJD Finished re-write and change of API
  22. * 06-Jul-2004 BJD Rewrote dma code to try and cope with various problems
  23. * 23-May-2003 BJD Created file
  24. * 19-Aug-2003 BJD Cleanup, header fix, added URL
  25. *
  26. * This file is based on the Sangwook Lee/Samsung patches, re-written due
  27. * to various ommisions from the code (such as flexible dma configuration)
  28. * for use with the BAST system board.
  29. *
  30. * The re-write is pretty much complete, and should be good enough for any
  31. * possible DMA function
  32. */
  33. #include <linux/config.h>
  34. #ifdef CONFIG_S3C2410_DMA_DEBUG
  35. #define DEBUG
  36. #endif
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/sched.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/sysdev.h>
  43. #include <linux/slab.h>
  44. #include <linux/errno.h>
  45. #include <linux/delay.h>
  46. #include <asm/system.h>
  47. #include <asm/irq.h>
  48. #include <asm/hardware.h>
  49. #include <asm/io.h>
  50. #include <asm/dma.h>
  51. #include <asm/mach/dma.h>
  52. #include <asm/arch/map.h>
  53. /* io map for dma */
  54. static void __iomem *dma_base;
  55. static kmem_cache_t *dma_kmem;
  56. /* dma channel state information */
  57. s3c2410_dma_chan_t s3c2410_chans[S3C2410_DMA_CHANNELS];
  58. /* debugging functions */
  59. #define BUF_MAGIC (0xcafebabe)
  60. #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
  61. #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
  62. #if 1
  63. #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
  64. #else
  65. static inline void
  66. dma_wrreg(s3c2410_dma_chan_t *chan, int reg, unsigned long val)
  67. {
  68. pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
  69. writel(val, dma_regaddr(chan, reg));
  70. }
  71. #endif
  72. #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
  73. /* captured register state for debug */
  74. struct s3c2410_dma_regstate {
  75. unsigned long dcsrc;
  76. unsigned long disrc;
  77. unsigned long dstat;
  78. unsigned long dcon;
  79. unsigned long dmsktrig;
  80. };
  81. #ifdef CONFIG_S3C2410_DMA_DEBUG
  82. /* dmadbg_showregs
  83. *
  84. * simple debug routine to print the current state of the dma registers
  85. */
  86. static void
  87. dmadbg_capture(s3c2410_dma_chan_t *chan, struct s3c2410_dma_regstate *regs)
  88. {
  89. regs->dcsrc = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  90. regs->disrc = dma_rdreg(chan, S3C2410_DMA_DISRC);
  91. regs->dstat = dma_rdreg(chan, S3C2410_DMA_DSTAT);
  92. regs->dcon = dma_rdreg(chan, S3C2410_DMA_DCON);
  93. regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  94. }
  95. static void
  96. dmadbg_showregs(const char *fname, int line, s3c2410_dma_chan_t *chan,
  97. struct s3c2410_dma_regstate *regs)
  98. {
  99. printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
  100. chan->number, fname, line,
  101. regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
  102. regs->dcon);
  103. }
  104. static void
  105. dmadbg_showchan(const char *fname, int line, s3c2410_dma_chan_t *chan)
  106. {
  107. struct s3c2410_dma_regstate state;
  108. dmadbg_capture(chan, &state);
  109. printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
  110. chan->number, fname, line, chan->load_state,
  111. chan->curr, chan->next, chan->end);
  112. dmadbg_showregs(fname, line, chan, &state);
  113. }
  114. #define dbg_showregs(chan) dmadbg_showregs(__FUNCTION__, __LINE__, (chan))
  115. #define dbg_showchan(chan) dmadbg_showchan(__FUNCTION__, __LINE__, (chan))
  116. #else
  117. #define dbg_showregs(chan) do { } while(0)
  118. #define dbg_showchan(chan) do { } while(0)
  119. #endif /* CONFIG_S3C2410_DMA_DEBUG */
  120. #define check_channel(chan) \
  121. do { if ((chan) >= S3C2410_DMA_CHANNELS) { \
  122. printk(KERN_ERR "%s: invalid channel %d\n", __FUNCTION__, (chan)); \
  123. return -EINVAL; \
  124. } } while(0)
  125. /* s3c2410_dma_stats_timeout
  126. *
  127. * Update DMA stats from timeout info
  128. */
  129. static void
  130. s3c2410_dma_stats_timeout(s3c2410_dma_stats_t *stats, int val)
  131. {
  132. if (stats == NULL)
  133. return;
  134. if (val > stats->timeout_longest)
  135. stats->timeout_longest = val;
  136. if (val < stats->timeout_shortest)
  137. stats->timeout_shortest = val;
  138. stats->timeout_avg += val;
  139. }
  140. /* s3c2410_dma_waitforload
  141. *
  142. * wait for the DMA engine to load a buffer, and update the state accordingly
  143. */
  144. static int
  145. s3c2410_dma_waitforload(s3c2410_dma_chan_t *chan, int line)
  146. {
  147. int timeout = chan->load_timeout;
  148. int took;
  149. if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
  150. printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
  151. return 0;
  152. }
  153. if (chan->stats != NULL)
  154. chan->stats->loads++;
  155. while (--timeout > 0) {
  156. if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
  157. took = chan->load_timeout - timeout;
  158. s3c2410_dma_stats_timeout(chan->stats, took);
  159. switch (chan->load_state) {
  160. case S3C2410_DMALOAD_1LOADED:
  161. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  162. break;
  163. default:
  164. printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
  165. }
  166. return 1;
  167. }
  168. }
  169. if (chan->stats != NULL) {
  170. chan->stats->timeout_failed++;
  171. }
  172. return 0;
  173. }
  174. /* s3c2410_dma_loadbuffer
  175. *
  176. * load a buffer, and update the channel state
  177. */
  178. static inline int
  179. s3c2410_dma_loadbuffer(s3c2410_dma_chan_t *chan,
  180. s3c2410_dma_buf_t *buf)
  181. {
  182. unsigned long reload;
  183. pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
  184. buf, (unsigned long)buf->data, buf->size);
  185. if (buf == NULL) {
  186. dmawarn("buffer is NULL\n");
  187. return -EINVAL;
  188. }
  189. /* check the state of the channel before we do anything */
  190. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  191. dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
  192. }
  193. if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
  194. dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
  195. }
  196. /* it would seem sensible if we are the last buffer to not bother
  197. * with the auto-reload bit, so that the DMA engine will not try
  198. * and load another transfer after this one has finished...
  199. */
  200. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  201. pr_debug("load_state is none, checking for noreload (next=%p)\n",
  202. buf->next);
  203. reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
  204. } else {
  205. pr_debug("load_state is %d => autoreload\n", chan->load_state);
  206. reload = S3C2410_DCON_AUTORELOAD;
  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(s3c2410_dma_chan_t *chan, s3c2410_chan_op_t 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(s3c2410_dma_chan_t *chan, s3c2410_dma_buf_t *buf,
  246. s3c2410_dma_buffresult_t result)
  247. {
  248. pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
  249. chan->callback_fn, buf, buf->id, buf->size, result);
  250. if (chan->callback_fn != NULL) {
  251. (chan->callback_fn)(chan, buf->id, buf->size, result);
  252. }
  253. }
  254. /* s3c2410_dma_start
  255. *
  256. * start a dma channel going
  257. */
  258. static int s3c2410_dma_start(s3c2410_dma_chan_t *chan)
  259. {
  260. unsigned long tmp;
  261. unsigned long flags;
  262. pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
  263. local_irq_save(flags);
  264. if (chan->state == S3C2410_DMA_RUNNING) {
  265. pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
  266. local_irq_restore(flags);
  267. return 0;
  268. }
  269. chan->state = S3C2410_DMA_RUNNING;
  270. /* check wether there is anything to load, and if not, see
  271. * if we can find anything to load
  272. */
  273. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  274. if (chan->next == NULL) {
  275. printk(KERN_ERR "dma%d: channel has nothing loaded\n",
  276. chan->number);
  277. chan->state = S3C2410_DMA_IDLE;
  278. local_irq_restore(flags);
  279. return -EINVAL;
  280. }
  281. s3c2410_dma_loadbuffer(chan, chan->next);
  282. }
  283. dbg_showchan(chan);
  284. /* enable the channel */
  285. if (!chan->irq_enabled) {
  286. enable_irq(chan->irq);
  287. chan->irq_enabled = 1;
  288. }
  289. /* start the channel going */
  290. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  291. tmp &= ~S3C2410_DMASKTRIG_STOP;
  292. tmp |= S3C2410_DMASKTRIG_ON;
  293. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  294. pr_debug("wrote %08lx to DMASKTRIG\n", tmp);
  295. #if 0
  296. /* the dma buffer loads should take care of clearing the AUTO
  297. * reloading feature */
  298. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  299. tmp &= ~S3C2410_DCON_NORELOAD;
  300. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  301. #endif
  302. s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
  303. dbg_showchan(chan);
  304. local_irq_restore(flags);
  305. return 0;
  306. }
  307. /* s3c2410_dma_canload
  308. *
  309. * work out if we can queue another buffer into the DMA engine
  310. */
  311. static int
  312. s3c2410_dma_canload(s3c2410_dma_chan_t *chan)
  313. {
  314. if (chan->load_state == S3C2410_DMALOAD_NONE ||
  315. chan->load_state == S3C2410_DMALOAD_1RUNNING)
  316. return 1;
  317. return 0;
  318. }
  319. /* s3c2410_dma_enqueue
  320. *
  321. * queue an given buffer for dma transfer.
  322. *
  323. * id the device driver's id information for this buffer
  324. * data the physical address of the buffer data
  325. * size the size of the buffer in bytes
  326. *
  327. * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
  328. * is checked, and if set, the channel is started. If this flag isn't set,
  329. * then an error will be returned.
  330. *
  331. * It is possible to queue more than one DMA buffer onto a channel at
  332. * once, and the code will deal with the re-loading of the next buffer
  333. * when necessary.
  334. */
  335. int s3c2410_dma_enqueue(unsigned int channel, void *id,
  336. dma_addr_t data, int size)
  337. {
  338. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  339. s3c2410_dma_buf_t *buf;
  340. unsigned long flags;
  341. check_channel(channel);
  342. pr_debug("%s: id=%p, data=%08x, size=%d\n",
  343. __FUNCTION__, id, (unsigned int)data, size);
  344. buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
  345. if (buf == NULL) {
  346. pr_debug("%s: out of memory (%d alloc)\n",
  347. __FUNCTION__, sizeof(*buf));
  348. return -ENOMEM;
  349. }
  350. pr_debug("%s: new buffer %p\n", __FUNCTION__, buf);
  351. //dbg_showchan(chan);
  352. buf->next = NULL;
  353. buf->data = buf->ptr = data;
  354. buf->size = size;
  355. buf->id = id;
  356. buf->magic = BUF_MAGIC;
  357. local_irq_save(flags);
  358. if (chan->curr == NULL) {
  359. /* we've got nothing loaded... */
  360. pr_debug("%s: buffer %p queued onto empty channel\n",
  361. __FUNCTION__, buf);
  362. chan->curr = buf;
  363. chan->end = buf;
  364. chan->next = NULL;
  365. } else {
  366. pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
  367. chan->number, __FUNCTION__, buf);
  368. if (chan->end == NULL)
  369. pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
  370. chan->number, __FUNCTION__, chan);
  371. chan->end->next = buf;
  372. chan->end = buf;
  373. }
  374. /* if necessary, update the next buffer field */
  375. if (chan->next == NULL)
  376. chan->next = buf;
  377. /* check to see if we can load a buffer */
  378. if (chan->state == S3C2410_DMA_RUNNING) {
  379. if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
  380. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  381. printk(KERN_ERR "dma%d: loadbuffer:"
  382. "timeout loading buffer\n",
  383. chan->number);
  384. dbg_showchan(chan);
  385. local_irq_restore(flags);
  386. return -EINVAL;
  387. }
  388. }
  389. while (s3c2410_dma_canload(chan) && chan->next != NULL) {
  390. s3c2410_dma_loadbuffer(chan, chan->next);
  391. }
  392. } else if (chan->state == S3C2410_DMA_IDLE) {
  393. if (chan->flags & S3C2410_DMAF_AUTOSTART) {
  394. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_START);
  395. }
  396. }
  397. local_irq_restore(flags);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(s3c2410_dma_enqueue);
  401. static inline void
  402. s3c2410_dma_freebuf(s3c2410_dma_buf_t *buf)
  403. {
  404. int magicok = (buf->magic == BUF_MAGIC);
  405. buf->magic = -1;
  406. if (magicok) {
  407. kmem_cache_free(dma_kmem, buf);
  408. } else {
  409. printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
  410. }
  411. }
  412. /* s3c2410_dma_lastxfer
  413. *
  414. * called when the system is out of buffers, to ensure that the channel
  415. * is prepared for shutdown.
  416. */
  417. static inline void
  418. s3c2410_dma_lastxfer(s3c2410_dma_chan_t *chan)
  419. {
  420. pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
  421. chan->number, chan->load_state);
  422. switch (chan->load_state) {
  423. case S3C2410_DMALOAD_NONE:
  424. break;
  425. case S3C2410_DMALOAD_1LOADED:
  426. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  427. /* flag error? */
  428. printk(KERN_ERR "dma%d: timeout waiting for load\n",
  429. chan->number);
  430. return;
  431. }
  432. break;
  433. default:
  434. pr_debug("dma%d: lastxfer: unhandled load_state %d with no next",
  435. chan->number, chan->load_state);
  436. return;
  437. }
  438. /* hopefully this'll shut the damned thing up after the transfer... */
  439. dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
  440. }
  441. #define dmadbg2(x...)
  442. static irqreturn_t
  443. s3c2410_dma_irq(int irq, void *devpw, struct pt_regs *regs)
  444. {
  445. s3c2410_dma_chan_t *chan = (s3c2410_dma_chan_t *)devpw;
  446. s3c2410_dma_buf_t *buf;
  447. buf = chan->curr;
  448. dbg_showchan(chan);
  449. /* modify the channel state */
  450. switch (chan->load_state) {
  451. case S3C2410_DMALOAD_1RUNNING:
  452. /* TODO - if we are running only one buffer, we probably
  453. * want to reload here, and then worry about the buffer
  454. * callback */
  455. chan->load_state = S3C2410_DMALOAD_NONE;
  456. break;
  457. case S3C2410_DMALOAD_1LOADED:
  458. /* iirc, we should go back to NONE loaded here, we
  459. * had a buffer, and it was never verified as being
  460. * loaded.
  461. */
  462. chan->load_state = S3C2410_DMALOAD_NONE;
  463. break;
  464. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  465. /* we'll worry about checking to see if another buffer is
  466. * ready after we've called back the owner. This should
  467. * ensure we do not wait around too long for the DMA
  468. * engine to start the next transfer
  469. */
  470. chan->load_state = S3C2410_DMALOAD_1LOADED;
  471. break;
  472. case S3C2410_DMALOAD_NONE:
  473. printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
  474. chan->number);
  475. break;
  476. default:
  477. printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
  478. chan->number, chan->load_state);
  479. break;
  480. }
  481. if (buf != NULL) {
  482. /* update the chain to make sure that if we load any more
  483. * buffers when we call the callback function, things should
  484. * work properly */
  485. chan->curr = buf->next;
  486. buf->next = NULL;
  487. if (buf->magic != BUF_MAGIC) {
  488. printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
  489. chan->number, __FUNCTION__, buf);
  490. return IRQ_HANDLED;
  491. }
  492. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
  493. /* free resouces */
  494. s3c2410_dma_freebuf(buf);
  495. } else {
  496. }
  497. if (chan->next != NULL) {
  498. unsigned long flags;
  499. switch (chan->load_state) {
  500. case S3C2410_DMALOAD_1RUNNING:
  501. /* don't need to do anything for this state */
  502. break;
  503. case S3C2410_DMALOAD_NONE:
  504. /* can load buffer immediately */
  505. break;
  506. case S3C2410_DMALOAD_1LOADED:
  507. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  508. /* flag error? */
  509. printk(KERN_ERR "dma%d: timeout waiting for load\n",
  510. chan->number);
  511. return IRQ_HANDLED;
  512. }
  513. break;
  514. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  515. goto no_load;
  516. default:
  517. printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
  518. chan->number, chan->load_state);
  519. return IRQ_HANDLED;
  520. }
  521. local_irq_save(flags);
  522. s3c2410_dma_loadbuffer(chan, chan->next);
  523. local_irq_restore(flags);
  524. } else {
  525. s3c2410_dma_lastxfer(chan);
  526. /* see if we can stop this channel.. */
  527. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  528. pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
  529. chan->number, jiffies);
  530. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
  531. }
  532. }
  533. no_load:
  534. return IRQ_HANDLED;
  535. }
  536. /* s3c2410_request_dma
  537. *
  538. * get control of an dma channel
  539. */
  540. int s3c2410_dma_request(unsigned int channel, s3c2410_dma_client_t *client,
  541. void *dev)
  542. {
  543. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  544. unsigned long flags;
  545. int err;
  546. pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
  547. channel, client->name, dev);
  548. check_channel(channel);
  549. local_irq_save(flags);
  550. dbg_showchan(chan);
  551. if (chan->in_use) {
  552. if (client != chan->client) {
  553. printk(KERN_ERR "dma%d: already in use\n", channel);
  554. local_irq_restore(flags);
  555. return -EBUSY;
  556. } else {
  557. printk(KERN_ERR "dma%d: client already has channel\n", channel);
  558. }
  559. }
  560. chan->client = client;
  561. chan->in_use = 1;
  562. if (!chan->irq_claimed) {
  563. pr_debug("dma%d: %s : requesting irq %d\n",
  564. channel, __FUNCTION__, chan->irq);
  565. err = request_irq(chan->irq, s3c2410_dma_irq, SA_INTERRUPT,
  566. client->name, (void *)chan);
  567. if (err) {
  568. chan->in_use = 0;
  569. local_irq_restore(flags);
  570. printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
  571. client->name, chan->irq, chan->number);
  572. return err;
  573. }
  574. chan->irq_claimed = 1;
  575. chan->irq_enabled = 1;
  576. }
  577. local_irq_restore(flags);
  578. /* need to setup */
  579. pr_debug("%s: channel initialised, %p\n", __FUNCTION__, chan);
  580. return 0;
  581. }
  582. EXPORT_SYMBOL(s3c2410_dma_request);
  583. /* s3c2410_dma_free
  584. *
  585. * release the given channel back to the system, will stop and flush
  586. * any outstanding transfers, and ensure the channel is ready for the
  587. * next claimant.
  588. *
  589. * Note, although a warning is currently printed if the freeing client
  590. * info is not the same as the registrant's client info, the free is still
  591. * allowed to go through.
  592. */
  593. int s3c2410_dma_free(dmach_t channel, s3c2410_dma_client_t *client)
  594. {
  595. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  596. unsigned long flags;
  597. check_channel(channel);
  598. local_irq_save(flags);
  599. if (chan->client != client) {
  600. printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
  601. channel, chan->client, client);
  602. }
  603. /* sort out stopping and freeing the channel */
  604. if (chan->state != S3C2410_DMA_IDLE) {
  605. pr_debug("%s: need to stop dma channel %p\n",
  606. __FUNCTION__, chan);
  607. /* possibly flush the channel */
  608. s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
  609. }
  610. chan->client = NULL;
  611. chan->in_use = 0;
  612. local_irq_restore(flags);
  613. return 0;
  614. }
  615. EXPORT_SYMBOL(s3c2410_dma_free);
  616. static int s3c2410_dma_dostop(s3c2410_dma_chan_t *chan)
  617. {
  618. unsigned long tmp;
  619. unsigned long flags;
  620. pr_debug("%s:\n", __FUNCTION__);
  621. dbg_showchan(chan);
  622. local_irq_save(flags);
  623. s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
  624. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  625. tmp |= S3C2410_DMASKTRIG_STOP;
  626. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  627. #if 0
  628. /* should also clear interrupts, according to WinCE BSP */
  629. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  630. tmp |= S3C2410_DCON_NORELOAD;
  631. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  632. #endif
  633. chan->state = S3C2410_DMA_IDLE;
  634. chan->load_state = S3C2410_DMALOAD_NONE;
  635. local_irq_restore(flags);
  636. return 0;
  637. }
  638. /* s3c2410_dma_flush
  639. *
  640. * stop the channel, and remove all current and pending transfers
  641. */
  642. static int s3c2410_dma_flush(s3c2410_dma_chan_t *chan)
  643. {
  644. s3c2410_dma_buf_t *buf, *next;
  645. unsigned long flags;
  646. pr_debug("%s:\n", __FUNCTION__);
  647. local_irq_save(flags);
  648. if (chan->state != S3C2410_DMA_IDLE) {
  649. pr_debug("%s: stopping channel...\n", __FUNCTION__ );
  650. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
  651. }
  652. buf = chan->curr;
  653. if (buf == NULL)
  654. buf = chan->next;
  655. chan->curr = chan->next = chan->end = NULL;
  656. if (buf != NULL) {
  657. for ( ; buf != NULL; buf = next) {
  658. next = buf->next;
  659. pr_debug("%s: free buffer %p, next %p\n",
  660. __FUNCTION__, buf, buf->next);
  661. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
  662. s3c2410_dma_freebuf(buf);
  663. }
  664. }
  665. local_irq_restore(flags);
  666. return 0;
  667. }
  668. int
  669. s3c2410_dma_ctrl(dmach_t channel, s3c2410_chan_op_t op)
  670. {
  671. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  672. check_channel(channel);
  673. switch (op) {
  674. case S3C2410_DMAOP_START:
  675. return s3c2410_dma_start(chan);
  676. case S3C2410_DMAOP_STOP:
  677. return s3c2410_dma_dostop(chan);
  678. case S3C2410_DMAOP_PAUSE:
  679. return -ENOENT;
  680. case S3C2410_DMAOP_RESUME:
  681. return -ENOENT;
  682. case S3C2410_DMAOP_FLUSH:
  683. return s3c2410_dma_flush(chan);
  684. case S3C2410_DMAOP_TIMEOUT:
  685. return 0;
  686. }
  687. return -ENOENT; /* unknown, don't bother */
  688. }
  689. EXPORT_SYMBOL(s3c2410_dma_ctrl);
  690. /* DMA configuration for each channel
  691. *
  692. * DISRCC -> source of the DMA (AHB,APB)
  693. * DISRC -> source address of the DMA
  694. * DIDSTC -> destination of the DMA (AHB,APD)
  695. * DIDST -> destination address of the DMA
  696. */
  697. /* s3c2410_dma_config
  698. *
  699. * xfersize: size of unit in bytes (1,2,4)
  700. * dcon: base value of the DCONx register
  701. */
  702. int s3c2410_dma_config(dmach_t channel,
  703. int xferunit,
  704. int dcon)
  705. {
  706. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  707. pr_debug("%s: chan=%d, xfer_unit=%d, dcon=%08x\n",
  708. __FUNCTION__, channel, xferunit, dcon);
  709. check_channel(channel);
  710. switch (xferunit) {
  711. case 1:
  712. dcon |= S3C2410_DCON_BYTE;
  713. break;
  714. case 2:
  715. dcon |= S3C2410_DCON_HALFWORD;
  716. break;
  717. case 4:
  718. dcon |= S3C2410_DCON_WORD;
  719. break;
  720. default:
  721. pr_debug("%s: bad transfer size %d\n", __FUNCTION__, xferunit);
  722. return -EINVAL;
  723. }
  724. dcon |= S3C2410_DCON_HWTRIG;
  725. dcon |= S3C2410_DCON_INTREQ;
  726. pr_debug("%s: dcon now %08x\n", __FUNCTION__, dcon);
  727. chan->dcon = dcon;
  728. chan->xfer_unit = xferunit;
  729. return 0;
  730. }
  731. EXPORT_SYMBOL(s3c2410_dma_config);
  732. int s3c2410_dma_setflags(dmach_t channel, unsigned int flags)
  733. {
  734. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  735. check_channel(channel);
  736. pr_debug("%s: chan=%p, flags=%08x\n", __FUNCTION__, chan, flags);
  737. chan->flags = flags;
  738. return 0;
  739. }
  740. EXPORT_SYMBOL(s3c2410_dma_setflags);
  741. /* do we need to protect the settings of the fields from
  742. * irq?
  743. */
  744. int s3c2410_dma_set_opfn(dmach_t channel, s3c2410_dma_opfn_t rtn)
  745. {
  746. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  747. check_channel(channel);
  748. pr_debug("%s: chan=%p, op rtn=%p\n", __FUNCTION__, chan, rtn);
  749. chan->op_fn = rtn;
  750. return 0;
  751. }
  752. EXPORT_SYMBOL(s3c2410_dma_set_opfn);
  753. int s3c2410_dma_set_buffdone_fn(dmach_t channel, s3c2410_dma_cbfn_t rtn)
  754. {
  755. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  756. check_channel(channel);
  757. pr_debug("%s: chan=%p, callback rtn=%p\n", __FUNCTION__, chan, rtn);
  758. chan->callback_fn = rtn;
  759. return 0;
  760. }
  761. EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);
  762. /* s3c2410_dma_devconfig
  763. *
  764. * configure the dma source/destination hardware type and address
  765. *
  766. * source: S3C2410_DMASRC_HW: source is hardware
  767. * S3C2410_DMASRC_MEM: source is memory
  768. *
  769. * hwcfg: the value for xxxSTCn register,
  770. * bit 0: 0=increment pointer, 1=leave pointer
  771. * bit 1: 0=soucre is AHB, 1=soucre is APB
  772. *
  773. * devaddr: physical address of the source
  774. */
  775. int s3c2410_dma_devconfig(int channel,
  776. s3c2410_dmasrc_t source,
  777. int hwcfg,
  778. unsigned long devaddr)
  779. {
  780. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  781. check_channel(channel);
  782. pr_debug("%s: source=%d, hwcfg=%08x, devaddr=%08lx\n",
  783. __FUNCTION__, (int)source, hwcfg, devaddr);
  784. chan->source = source;
  785. chan->dev_addr = devaddr;
  786. switch (source) {
  787. case S3C2410_DMASRC_HW:
  788. /* source is hardware */
  789. pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
  790. __FUNCTION__, devaddr, hwcfg);
  791. dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
  792. dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
  793. dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
  794. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
  795. return 0;
  796. case S3C2410_DMASRC_MEM:
  797. /* source is memory */
  798. pr_debug( "%s: mem source, devaddr=%08lx, hwcfg=%d\n",
  799. __FUNCTION__, devaddr, hwcfg);
  800. dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
  801. dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
  802. dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
  803. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
  804. return 0;
  805. }
  806. printk(KERN_ERR "dma%d: invalid source type (%d)\n", channel, source);
  807. return -EINVAL;
  808. }
  809. EXPORT_SYMBOL(s3c2410_dma_devconfig);
  810. /* s3c2410_dma_getposition
  811. *
  812. * returns the current transfer points for the dma source and destination
  813. */
  814. int s3c2410_dma_getposition(dmach_t channel, dma_addr_t *src, dma_addr_t *dst)
  815. {
  816. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  817. check_channel(channel);
  818. if (src != NULL)
  819. *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  820. if (dst != NULL)
  821. *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
  822. return 0;
  823. }
  824. EXPORT_SYMBOL(s3c2410_dma_getposition);
  825. /* system device class */
  826. #ifdef CONFIG_PM
  827. static int s3c2410_dma_suspend(struct sys_device *dev, pm_message_t state)
  828. {
  829. s3c2410_dma_chan_t *cp = container_of(dev, s3c2410_dma_chan_t, dev);
  830. printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
  831. if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
  832. /* the dma channel is still working, which is probably
  833. * a bad thing to do over suspend/resume. We stop the
  834. * channel and assume that the client is either going to
  835. * retry after resume, or that it is broken.
  836. */
  837. printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
  838. cp->number);
  839. s3c2410_dma_dostop(cp);
  840. }
  841. return 0;
  842. }
  843. static int s3c2410_dma_resume(struct sys_device *dev)
  844. {
  845. return 0;
  846. }
  847. #else
  848. #define s3c2410_dma_suspend NULL
  849. #define s3c2410_dma_resume NULL
  850. #endif /* CONFIG_PM */
  851. static struct sysdev_class dma_sysclass = {
  852. set_kset_name("s3c24xx-dma"),
  853. .suspend = s3c2410_dma_suspend,
  854. .resume = s3c2410_dma_resume,
  855. };
  856. /* kmem cache implementation */
  857. static void s3c2410_dma_cache_ctor(void *p, kmem_cache_t *c, unsigned long f)
  858. {
  859. memset(p, 0, sizeof(s3c2410_dma_buf_t));
  860. }
  861. /* initialisation code */
  862. static int __init s3c2410_init_dma(void)
  863. {
  864. s3c2410_dma_chan_t *cp;
  865. int channel;
  866. int ret;
  867. printk("S3C2410 DMA Driver, (c) 2003-2004 Simtec Electronics\n");
  868. dma_base = ioremap(S3C2410_PA_DMA, 0x200);
  869. if (dma_base == NULL) {
  870. printk(KERN_ERR "dma failed to remap register block\n");
  871. return -ENOMEM;
  872. }
  873. ret = sysdev_class_register(&dma_sysclass);
  874. if (ret != 0) {
  875. printk(KERN_ERR "dma sysclass registration failed\n");
  876. goto err;
  877. }
  878. dma_kmem = kmem_cache_create("dma_desc", sizeof(s3c2410_dma_buf_t), 0,
  879. SLAB_HWCACHE_ALIGN,
  880. s3c2410_dma_cache_ctor, NULL);
  881. if (dma_kmem == NULL) {
  882. printk(KERN_ERR "dma failed to make kmem cache\n");
  883. ret = -ENOMEM;
  884. goto err;
  885. }
  886. for (channel = 0; channel < S3C2410_DMA_CHANNELS; channel++) {
  887. cp = &s3c2410_chans[channel];
  888. memset(cp, 0, sizeof(s3c2410_dma_chan_t));
  889. /* dma channel irqs are in order.. */
  890. cp->number = channel;
  891. cp->irq = channel + IRQ_DMA0;
  892. cp->regs = dma_base + (channel*0x40);
  893. /* point current stats somewhere */
  894. cp->stats = &cp->stats_store;
  895. cp->stats_store.timeout_shortest = LONG_MAX;
  896. /* basic channel configuration */
  897. cp->load_timeout = 1<<18;
  898. /* register system device */
  899. cp->dev.cls = &dma_sysclass;
  900. cp->dev.id = channel;
  901. ret = sysdev_register(&cp->dev);
  902. printk("DMA channel %d at %p, irq %d\n",
  903. cp->number, cp->regs, cp->irq);
  904. }
  905. return 0;
  906. err:
  907. kmem_cache_destroy(dma_kmem);
  908. iounmap(dma_base);
  909. dma_base = NULL;
  910. return ret;
  911. }
  912. __initcall(s3c2410_init_dma);