dma.c 32 KB

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