dma.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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 (%ld 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. if (chan->irq_claimed)
  613. free_irq(chan->irq, (void *)chan);
  614. chan->irq_claimed = 0;
  615. local_irq_restore(flags);
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(s3c2410_dma_free);
  619. static int s3c2410_dma_dostop(s3c2410_dma_chan_t *chan)
  620. {
  621. unsigned long tmp;
  622. unsigned long flags;
  623. pr_debug("%s:\n", __FUNCTION__);
  624. dbg_showchan(chan);
  625. local_irq_save(flags);
  626. s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
  627. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  628. tmp |= S3C2410_DMASKTRIG_STOP;
  629. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  630. #if 0
  631. /* should also clear interrupts, according to WinCE BSP */
  632. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  633. tmp |= S3C2410_DCON_NORELOAD;
  634. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  635. #endif
  636. chan->state = S3C2410_DMA_IDLE;
  637. chan->load_state = S3C2410_DMALOAD_NONE;
  638. local_irq_restore(flags);
  639. return 0;
  640. }
  641. /* s3c2410_dma_flush
  642. *
  643. * stop the channel, and remove all current and pending transfers
  644. */
  645. static int s3c2410_dma_flush(s3c2410_dma_chan_t *chan)
  646. {
  647. s3c2410_dma_buf_t *buf, *next;
  648. unsigned long flags;
  649. pr_debug("%s:\n", __FUNCTION__);
  650. local_irq_save(flags);
  651. if (chan->state != S3C2410_DMA_IDLE) {
  652. pr_debug("%s: stopping channel...\n", __FUNCTION__ );
  653. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
  654. }
  655. buf = chan->curr;
  656. if (buf == NULL)
  657. buf = chan->next;
  658. chan->curr = chan->next = chan->end = NULL;
  659. if (buf != NULL) {
  660. for ( ; buf != NULL; buf = next) {
  661. next = buf->next;
  662. pr_debug("%s: free buffer %p, next %p\n",
  663. __FUNCTION__, buf, buf->next);
  664. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
  665. s3c2410_dma_freebuf(buf);
  666. }
  667. }
  668. local_irq_restore(flags);
  669. return 0;
  670. }
  671. int
  672. s3c2410_dma_ctrl(dmach_t channel, s3c2410_chan_op_t op)
  673. {
  674. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  675. check_channel(channel);
  676. switch (op) {
  677. case S3C2410_DMAOP_START:
  678. return s3c2410_dma_start(chan);
  679. case S3C2410_DMAOP_STOP:
  680. return s3c2410_dma_dostop(chan);
  681. case S3C2410_DMAOP_PAUSE:
  682. return -ENOENT;
  683. case S3C2410_DMAOP_RESUME:
  684. return -ENOENT;
  685. case S3C2410_DMAOP_FLUSH:
  686. return s3c2410_dma_flush(chan);
  687. case S3C2410_DMAOP_TIMEOUT:
  688. return 0;
  689. }
  690. return -ENOENT; /* unknown, don't bother */
  691. }
  692. EXPORT_SYMBOL(s3c2410_dma_ctrl);
  693. /* DMA configuration for each channel
  694. *
  695. * DISRCC -> source of the DMA (AHB,APB)
  696. * DISRC -> source address of the DMA
  697. * DIDSTC -> destination of the DMA (AHB,APD)
  698. * DIDST -> destination address of the DMA
  699. */
  700. /* s3c2410_dma_config
  701. *
  702. * xfersize: size of unit in bytes (1,2,4)
  703. * dcon: base value of the DCONx register
  704. */
  705. int s3c2410_dma_config(dmach_t channel,
  706. int xferunit,
  707. int dcon)
  708. {
  709. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  710. pr_debug("%s: chan=%d, xfer_unit=%d, dcon=%08x\n",
  711. __FUNCTION__, channel, xferunit, dcon);
  712. check_channel(channel);
  713. switch (xferunit) {
  714. case 1:
  715. dcon |= S3C2410_DCON_BYTE;
  716. break;
  717. case 2:
  718. dcon |= S3C2410_DCON_HALFWORD;
  719. break;
  720. case 4:
  721. dcon |= S3C2410_DCON_WORD;
  722. break;
  723. default:
  724. pr_debug("%s: bad transfer size %d\n", __FUNCTION__, xferunit);
  725. return -EINVAL;
  726. }
  727. dcon |= S3C2410_DCON_HWTRIG;
  728. dcon |= S3C2410_DCON_INTREQ;
  729. pr_debug("%s: dcon now %08x\n", __FUNCTION__, dcon);
  730. chan->dcon = dcon;
  731. chan->xfer_unit = xferunit;
  732. return 0;
  733. }
  734. EXPORT_SYMBOL(s3c2410_dma_config);
  735. int s3c2410_dma_setflags(dmach_t channel, unsigned int flags)
  736. {
  737. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  738. check_channel(channel);
  739. pr_debug("%s: chan=%p, flags=%08x\n", __FUNCTION__, chan, flags);
  740. chan->flags = flags;
  741. return 0;
  742. }
  743. EXPORT_SYMBOL(s3c2410_dma_setflags);
  744. /* do we need to protect the settings of the fields from
  745. * irq?
  746. */
  747. int s3c2410_dma_set_opfn(dmach_t channel, s3c2410_dma_opfn_t rtn)
  748. {
  749. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  750. check_channel(channel);
  751. pr_debug("%s: chan=%p, op rtn=%p\n", __FUNCTION__, chan, rtn);
  752. chan->op_fn = rtn;
  753. return 0;
  754. }
  755. EXPORT_SYMBOL(s3c2410_dma_set_opfn);
  756. int s3c2410_dma_set_buffdone_fn(dmach_t channel, s3c2410_dma_cbfn_t rtn)
  757. {
  758. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  759. check_channel(channel);
  760. pr_debug("%s: chan=%p, callback rtn=%p\n", __FUNCTION__, chan, rtn);
  761. chan->callback_fn = rtn;
  762. return 0;
  763. }
  764. EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);
  765. /* s3c2410_dma_devconfig
  766. *
  767. * configure the dma source/destination hardware type and address
  768. *
  769. * source: S3C2410_DMASRC_HW: source is hardware
  770. * S3C2410_DMASRC_MEM: source is memory
  771. *
  772. * hwcfg: the value for xxxSTCn register,
  773. * bit 0: 0=increment pointer, 1=leave pointer
  774. * bit 1: 0=soucre is AHB, 1=soucre is APB
  775. *
  776. * devaddr: physical address of the source
  777. */
  778. int s3c2410_dma_devconfig(int channel,
  779. s3c2410_dmasrc_t source,
  780. int hwcfg,
  781. unsigned long devaddr)
  782. {
  783. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  784. check_channel(channel);
  785. pr_debug("%s: source=%d, hwcfg=%08x, devaddr=%08lx\n",
  786. __FUNCTION__, (int)source, hwcfg, devaddr);
  787. chan->source = source;
  788. chan->dev_addr = devaddr;
  789. switch (source) {
  790. case S3C2410_DMASRC_HW:
  791. /* source is hardware */
  792. pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
  793. __FUNCTION__, devaddr, hwcfg);
  794. dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
  795. dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
  796. dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
  797. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
  798. return 0;
  799. case S3C2410_DMASRC_MEM:
  800. /* source is memory */
  801. pr_debug( "%s: mem source, devaddr=%08lx, hwcfg=%d\n",
  802. __FUNCTION__, devaddr, hwcfg);
  803. dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
  804. dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
  805. dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
  806. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
  807. return 0;
  808. }
  809. printk(KERN_ERR "dma%d: invalid source type (%d)\n", channel, source);
  810. return -EINVAL;
  811. }
  812. EXPORT_SYMBOL(s3c2410_dma_devconfig);
  813. /* s3c2410_dma_getposition
  814. *
  815. * returns the current transfer points for the dma source and destination
  816. */
  817. int s3c2410_dma_getposition(dmach_t channel, dma_addr_t *src, dma_addr_t *dst)
  818. {
  819. s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
  820. check_channel(channel);
  821. if (src != NULL)
  822. *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  823. if (dst != NULL)
  824. *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
  825. return 0;
  826. }
  827. EXPORT_SYMBOL(s3c2410_dma_getposition);
  828. /* system device class */
  829. #ifdef CONFIG_PM
  830. static int s3c2410_dma_suspend(struct sys_device *dev, pm_message_t state)
  831. {
  832. s3c2410_dma_chan_t *cp = container_of(dev, s3c2410_dma_chan_t, dev);
  833. printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
  834. if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
  835. /* the dma channel is still working, which is probably
  836. * a bad thing to do over suspend/resume. We stop the
  837. * channel and assume that the client is either going to
  838. * retry after resume, or that it is broken.
  839. */
  840. printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
  841. cp->number);
  842. s3c2410_dma_dostop(cp);
  843. }
  844. return 0;
  845. }
  846. static int s3c2410_dma_resume(struct sys_device *dev)
  847. {
  848. return 0;
  849. }
  850. #else
  851. #define s3c2410_dma_suspend NULL
  852. #define s3c2410_dma_resume NULL
  853. #endif /* CONFIG_PM */
  854. static struct sysdev_class dma_sysclass = {
  855. set_kset_name("s3c24xx-dma"),
  856. .suspend = s3c2410_dma_suspend,
  857. .resume = s3c2410_dma_resume,
  858. };
  859. /* kmem cache implementation */
  860. static void s3c2410_dma_cache_ctor(void *p, kmem_cache_t *c, unsigned long f)
  861. {
  862. memset(p, 0, sizeof(s3c2410_dma_buf_t));
  863. }
  864. /* initialisation code */
  865. static int __init s3c2410_init_dma(void)
  866. {
  867. s3c2410_dma_chan_t *cp;
  868. int channel;
  869. int ret;
  870. printk("S3C2410 DMA Driver, (c) 2003-2004 Simtec Electronics\n");
  871. dma_base = ioremap(S3C2410_PA_DMA, 0x200);
  872. if (dma_base == NULL) {
  873. printk(KERN_ERR "dma failed to remap register block\n");
  874. return -ENOMEM;
  875. }
  876. ret = sysdev_class_register(&dma_sysclass);
  877. if (ret != 0) {
  878. printk(KERN_ERR "dma sysclass registration failed\n");
  879. goto err;
  880. }
  881. dma_kmem = kmem_cache_create("dma_desc", sizeof(s3c2410_dma_buf_t), 0,
  882. SLAB_HWCACHE_ALIGN,
  883. s3c2410_dma_cache_ctor, NULL);
  884. if (dma_kmem == NULL) {
  885. printk(KERN_ERR "dma failed to make kmem cache\n");
  886. ret = -ENOMEM;
  887. goto err;
  888. }
  889. for (channel = 0; channel < S3C2410_DMA_CHANNELS; channel++) {
  890. cp = &s3c2410_chans[channel];
  891. memset(cp, 0, sizeof(s3c2410_dma_chan_t));
  892. /* dma channel irqs are in order.. */
  893. cp->number = channel;
  894. cp->irq = channel + IRQ_DMA0;
  895. cp->regs = dma_base + (channel*0x40);
  896. /* point current stats somewhere */
  897. cp->stats = &cp->stats_store;
  898. cp->stats_store.timeout_shortest = LONG_MAX;
  899. /* basic channel configuration */
  900. cp->load_timeout = 1<<18;
  901. /* register system device */
  902. cp->dev.cls = &dma_sysclass;
  903. cp->dev.id = channel;
  904. ret = sysdev_register(&cp->dev);
  905. printk("DMA channel %d at %p, irq %d\n",
  906. cp->number, cp->regs, cp->irq);
  907. }
  908. return 0;
  909. err:
  910. kmem_cache_destroy(dma_kmem);
  911. iounmap(dma_base);
  912. dma_base = NULL;
  913. return ret;
  914. }
  915. __initcall(s3c2410_init_dma);