dma.c 27 KB

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