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 wether 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. chan->end->next = buf;
  376. chan->end = buf;
  377. }
  378. /* if necessary, update the next buffer field */
  379. if (chan->next == NULL)
  380. chan->next = buf;
  381. /* check to see if we can load a buffer */
  382. if (chan->state == S3C2410_DMA_RUNNING) {
  383. if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
  384. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  385. printk(KERN_ERR "dma%d: loadbuffer:"
  386. "timeout loading buffer\n",
  387. chan->number);
  388. dbg_showchan(chan);
  389. local_irq_restore(flags);
  390. return -EINVAL;
  391. }
  392. }
  393. while (s3c2410_dma_canload(chan) && chan->next != NULL) {
  394. s3c2410_dma_loadbuffer(chan, chan->next);
  395. }
  396. } else if (chan->state == S3C2410_DMA_IDLE) {
  397. if (chan->flags & S3C2410_DMAF_AUTOSTART) {
  398. s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
  399. S3C2410_DMAOP_START);
  400. }
  401. }
  402. local_irq_restore(flags);
  403. return 0;
  404. }
  405. EXPORT_SYMBOL(s3c2410_dma_enqueue);
  406. static inline void
  407. s3c2410_dma_freebuf(struct s3c2410_dma_buf *buf)
  408. {
  409. int magicok = (buf->magic == BUF_MAGIC);
  410. buf->magic = -1;
  411. if (magicok) {
  412. kmem_cache_free(dma_kmem, buf);
  413. } else {
  414. printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
  415. }
  416. }
  417. /* s3c2410_dma_lastxfer
  418. *
  419. * called when the system is out of buffers, to ensure that the channel
  420. * is prepared for shutdown.
  421. */
  422. static inline void
  423. s3c2410_dma_lastxfer(struct s3c2410_dma_chan *chan)
  424. {
  425. #if 0
  426. pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
  427. chan->number, chan->load_state);
  428. #endif
  429. switch (chan->load_state) {
  430. case S3C2410_DMALOAD_NONE:
  431. break;
  432. case S3C2410_DMALOAD_1LOADED:
  433. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  434. /* flag error? */
  435. printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
  436. chan->number, __func__);
  437. return;
  438. }
  439. break;
  440. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  441. /* I believe in this case we do not have anything to do
  442. * until the next buffer comes along, and we turn off the
  443. * reload */
  444. return;
  445. default:
  446. pr_debug("dma%d: lastxfer: unhandled load_state %d with no next\n",
  447. chan->number, chan->load_state);
  448. return;
  449. }
  450. /* hopefully this'll shut the damned thing up after the transfer... */
  451. dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
  452. }
  453. #define dmadbg2(x...)
  454. static irqreturn_t
  455. s3c2410_dma_irq(int irq, void *devpw)
  456. {
  457. struct s3c2410_dma_chan *chan = (struct s3c2410_dma_chan *)devpw;
  458. struct s3c2410_dma_buf *buf;
  459. buf = chan->curr;
  460. dbg_showchan(chan);
  461. /* modify the channel state */
  462. switch (chan->load_state) {
  463. case S3C2410_DMALOAD_1RUNNING:
  464. /* TODO - if we are running only one buffer, we probably
  465. * want to reload here, and then worry about the buffer
  466. * callback */
  467. chan->load_state = S3C2410_DMALOAD_NONE;
  468. break;
  469. case S3C2410_DMALOAD_1LOADED:
  470. /* iirc, we should go back to NONE loaded here, we
  471. * had a buffer, and it was never verified as being
  472. * loaded.
  473. */
  474. chan->load_state = S3C2410_DMALOAD_NONE;
  475. break;
  476. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  477. /* we'll worry about checking to see if another buffer is
  478. * ready after we've called back the owner. This should
  479. * ensure we do not wait around too long for the DMA
  480. * engine to start the next transfer
  481. */
  482. chan->load_state = S3C2410_DMALOAD_1LOADED;
  483. break;
  484. case S3C2410_DMALOAD_NONE:
  485. printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
  486. chan->number);
  487. break;
  488. default:
  489. printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
  490. chan->number, chan->load_state);
  491. break;
  492. }
  493. if (buf != NULL) {
  494. /* update the chain to make sure that if we load any more
  495. * buffers when we call the callback function, things should
  496. * work properly */
  497. chan->curr = buf->next;
  498. buf->next = NULL;
  499. if (buf->magic != BUF_MAGIC) {
  500. printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
  501. chan->number, __func__, buf);
  502. return IRQ_HANDLED;
  503. }
  504. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
  505. /* free resouces */
  506. s3c2410_dma_freebuf(buf);
  507. } else {
  508. }
  509. /* only reload if the channel is still running... our buffer done
  510. * routine may have altered the state by requesting the dma channel
  511. * to stop or shutdown... */
  512. /* todo: check that when the channel is shut-down from inside this
  513. * function, we cope with unsetting reload, etc */
  514. if (chan->next != NULL && chan->state != S3C2410_DMA_IDLE) {
  515. unsigned long flags;
  516. switch (chan->load_state) {
  517. case S3C2410_DMALOAD_1RUNNING:
  518. /* don't need to do anything for this state */
  519. break;
  520. case S3C2410_DMALOAD_NONE:
  521. /* can load buffer immediately */
  522. break;
  523. case S3C2410_DMALOAD_1LOADED:
  524. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  525. /* flag error? */
  526. printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
  527. chan->number, __func__);
  528. return IRQ_HANDLED;
  529. }
  530. break;
  531. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  532. goto no_load;
  533. default:
  534. printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
  535. chan->number, chan->load_state);
  536. return IRQ_HANDLED;
  537. }
  538. local_irq_save(flags);
  539. s3c2410_dma_loadbuffer(chan, chan->next);
  540. local_irq_restore(flags);
  541. } else {
  542. s3c2410_dma_lastxfer(chan);
  543. /* see if we can stop this channel.. */
  544. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  545. pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
  546. chan->number, jiffies);
  547. s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
  548. S3C2410_DMAOP_STOP);
  549. }
  550. }
  551. no_load:
  552. return IRQ_HANDLED;
  553. }
  554. static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel);
  555. /* s3c2410_request_dma
  556. *
  557. * get control of an dma channel
  558. */
  559. int s3c2410_dma_request(enum dma_ch channel,
  560. struct s3c2410_dma_client *client,
  561. void *dev)
  562. {
  563. struct s3c2410_dma_chan *chan;
  564. unsigned long flags;
  565. int err;
  566. pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
  567. channel, client->name, dev);
  568. local_irq_save(flags);
  569. chan = s3c2410_dma_map_channel(channel);
  570. if (chan == NULL) {
  571. local_irq_restore(flags);
  572. return -EBUSY;
  573. }
  574. dbg_showchan(chan);
  575. chan->client = client;
  576. chan->in_use = 1;
  577. if (!chan->irq_claimed) {
  578. pr_debug("dma%d: %s : requesting irq %d\n",
  579. channel, __func__, chan->irq);
  580. chan->irq_claimed = 1;
  581. local_irq_restore(flags);
  582. err = request_irq(chan->irq, s3c2410_dma_irq, IRQF_DISABLED,
  583. client->name, (void *)chan);
  584. local_irq_save(flags);
  585. if (err) {
  586. chan->in_use = 0;
  587. chan->irq_claimed = 0;
  588. local_irq_restore(flags);
  589. printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
  590. client->name, chan->irq, chan->number);
  591. return err;
  592. }
  593. chan->irq_enabled = 1;
  594. }
  595. local_irq_restore(flags);
  596. /* need to setup */
  597. pr_debug("%s: channel initialised, %p\n", __func__, chan);
  598. return chan->number | DMACH_LOW_LEVEL;
  599. }
  600. EXPORT_SYMBOL(s3c2410_dma_request);
  601. /* s3c2410_dma_free
  602. *
  603. * release the given channel back to the system, will stop and flush
  604. * any outstanding transfers, and ensure the channel is ready for the
  605. * next claimant.
  606. *
  607. * Note, although a warning is currently printed if the freeing client
  608. * info is not the same as the registrant's client info, the free is still
  609. * allowed to go through.
  610. */
  611. int s3c2410_dma_free(enum dma_ch channel, struct s3c2410_dma_client *client)
  612. {
  613. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  614. unsigned long flags;
  615. if (chan == NULL)
  616. return -EINVAL;
  617. local_irq_save(flags);
  618. if (chan->client != client) {
  619. printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
  620. channel, chan->client, client);
  621. }
  622. /* sort out stopping and freeing the channel */
  623. if (chan->state != S3C2410_DMA_IDLE) {
  624. pr_debug("%s: need to stop dma channel %p\n",
  625. __func__, chan);
  626. /* possibly flush the channel */
  627. s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
  628. }
  629. chan->client = NULL;
  630. chan->in_use = 0;
  631. if (chan->irq_claimed)
  632. free_irq(chan->irq, (void *)chan);
  633. chan->irq_claimed = 0;
  634. if (!(channel & DMACH_LOW_LEVEL))
  635. s3c_dma_chan_map[channel] = NULL;
  636. local_irq_restore(flags);
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(s3c2410_dma_free);
  640. static int s3c2410_dma_dostop(struct s3c2410_dma_chan *chan)
  641. {
  642. unsigned long flags;
  643. unsigned long tmp;
  644. pr_debug("%s:\n", __func__);
  645. dbg_showchan(chan);
  646. local_irq_save(flags);
  647. s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
  648. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  649. tmp |= S3C2410_DMASKTRIG_STOP;
  650. //tmp &= ~S3C2410_DMASKTRIG_ON;
  651. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  652. #if 0
  653. /* should also clear interrupts, according to WinCE BSP */
  654. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  655. tmp |= S3C2410_DCON_NORELOAD;
  656. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  657. #endif
  658. /* should stop do this, or should we wait for flush? */
  659. chan->state = S3C2410_DMA_IDLE;
  660. chan->load_state = S3C2410_DMALOAD_NONE;
  661. local_irq_restore(flags);
  662. return 0;
  663. }
  664. static void s3c2410_dma_waitforstop(struct s3c2410_dma_chan *chan)
  665. {
  666. unsigned long tmp;
  667. unsigned int timeout = 0x10000;
  668. while (timeout-- > 0) {
  669. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  670. if (!(tmp & S3C2410_DMASKTRIG_ON))
  671. return;
  672. }
  673. pr_debug("dma%d: failed to stop?\n", chan->number);
  674. }
  675. /* s3c2410_dma_flush
  676. *
  677. * stop the channel, and remove all current and pending transfers
  678. */
  679. static int s3c2410_dma_flush(struct s3c2410_dma_chan *chan)
  680. {
  681. struct s3c2410_dma_buf *buf, *next;
  682. unsigned long flags;
  683. pr_debug("%s: chan %p (%d)\n", __func__, chan, chan->number);
  684. dbg_showchan(chan);
  685. local_irq_save(flags);
  686. if (chan->state != S3C2410_DMA_IDLE) {
  687. pr_debug("%s: stopping channel...\n", __func__ );
  688. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
  689. }
  690. buf = chan->curr;
  691. if (buf == NULL)
  692. buf = chan->next;
  693. chan->curr = chan->next = chan->end = NULL;
  694. if (buf != NULL) {
  695. for ( ; buf != NULL; buf = next) {
  696. next = buf->next;
  697. pr_debug("%s: free buffer %p, next %p\n",
  698. __func__, buf, buf->next);
  699. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
  700. s3c2410_dma_freebuf(buf);
  701. }
  702. }
  703. dbg_showregs(chan);
  704. s3c2410_dma_waitforstop(chan);
  705. #if 0
  706. /* should also clear interrupts, according to WinCE BSP */
  707. {
  708. unsigned long tmp;
  709. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  710. tmp |= S3C2410_DCON_NORELOAD;
  711. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  712. }
  713. #endif
  714. dbg_showregs(chan);
  715. local_irq_restore(flags);
  716. return 0;
  717. }
  718. static int s3c2410_dma_started(struct s3c2410_dma_chan *chan)
  719. {
  720. unsigned long flags;
  721. local_irq_save(flags);
  722. dbg_showchan(chan);
  723. /* if we've only loaded one buffer onto the channel, then chec
  724. * to see if we have another, and if so, try and load it so when
  725. * the first buffer is finished, the new one will be loaded onto
  726. * the channel */
  727. if (chan->next != NULL) {
  728. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  729. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  730. pr_debug("%s: buff not yet loaded, no more todo\n",
  731. __func__);
  732. } else {
  733. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  734. s3c2410_dma_loadbuffer(chan, chan->next);
  735. }
  736. } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
  737. s3c2410_dma_loadbuffer(chan, chan->next);
  738. }
  739. }
  740. local_irq_restore(flags);
  741. return 0;
  742. }
  743. int
  744. s3c2410_dma_ctrl(enum dma_ch channel, enum s3c2410_chan_op op)
  745. {
  746. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  747. if (chan == NULL)
  748. return -EINVAL;
  749. switch (op) {
  750. case S3C2410_DMAOP_START:
  751. return s3c2410_dma_start(chan);
  752. case S3C2410_DMAOP_STOP:
  753. return s3c2410_dma_dostop(chan);
  754. case S3C2410_DMAOP_PAUSE:
  755. case S3C2410_DMAOP_RESUME:
  756. return -ENOENT;
  757. case S3C2410_DMAOP_FLUSH:
  758. return s3c2410_dma_flush(chan);
  759. case S3C2410_DMAOP_STARTED:
  760. return s3c2410_dma_started(chan);
  761. case S3C2410_DMAOP_TIMEOUT:
  762. return 0;
  763. }
  764. return -ENOENT; /* unknown, don't bother */
  765. }
  766. EXPORT_SYMBOL(s3c2410_dma_ctrl);
  767. /* DMA configuration for each channel
  768. *
  769. * DISRCC -> source of the DMA (AHB,APB)
  770. * DISRC -> source address of the DMA
  771. * DIDSTC -> destination of the DMA (AHB,APD)
  772. * DIDST -> destination address of the DMA
  773. */
  774. /* s3c2410_dma_config
  775. *
  776. * xfersize: size of unit in bytes (1,2,4)
  777. */
  778. int s3c2410_dma_config(enum dma_ch channel,
  779. int xferunit)
  780. {
  781. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  782. unsigned int dcon;
  783. pr_debug("%s: chan=%d, xfer_unit=%d\n", __func__, channel, xferunit);
  784. if (chan == NULL)
  785. return -EINVAL;
  786. dcon = chan->dcon & dma_sel.dcon_mask;
  787. pr_debug("%s: dcon is %08x\n", __func__, dcon);
  788. switch (chan->req_ch) {
  789. case DMACH_I2S_IN:
  790. case DMACH_I2S_OUT:
  791. case DMACH_PCM_IN:
  792. case DMACH_PCM_OUT:
  793. case DMACH_MIC_IN:
  794. default:
  795. dcon |= S3C2410_DCON_HANDSHAKE;
  796. dcon |= S3C2410_DCON_SYNC_PCLK;
  797. break;
  798. case DMACH_SDI:
  799. /* note, ensure if need HANDSHAKE or not */
  800. dcon |= S3C2410_DCON_SYNC_PCLK;
  801. break;
  802. case DMACH_XD0:
  803. case DMACH_XD1:
  804. dcon |= S3C2410_DCON_HANDSHAKE;
  805. dcon |= S3C2410_DCON_SYNC_HCLK;
  806. break;
  807. }
  808. switch (xferunit) {
  809. case 1:
  810. dcon |= S3C2410_DCON_BYTE;
  811. break;
  812. case 2:
  813. dcon |= S3C2410_DCON_HALFWORD;
  814. break;
  815. case 4:
  816. dcon |= S3C2410_DCON_WORD;
  817. break;
  818. default:
  819. pr_debug("%s: bad transfer size %d\n", __func__, xferunit);
  820. return -EINVAL;
  821. }
  822. dcon |= S3C2410_DCON_HWTRIG;
  823. dcon |= S3C2410_DCON_INTREQ;
  824. pr_debug("%s: dcon now %08x\n", __func__, dcon);
  825. chan->dcon = dcon;
  826. chan->xfer_unit = xferunit;
  827. return 0;
  828. }
  829. EXPORT_SYMBOL(s3c2410_dma_config);
  830. /* s3c2410_dma_devconfig
  831. *
  832. * configure the dma source/destination hardware type and address
  833. *
  834. * source: DMA_FROM_DEVICE: source is hardware
  835. * DMA_TO_DEVICE: source is memory
  836. *
  837. * devaddr: physical address of the source
  838. */
  839. int s3c2410_dma_devconfig(enum dma_ch channel,
  840. enum dma_data_direction source,
  841. unsigned long devaddr)
  842. {
  843. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  844. unsigned int hwcfg;
  845. if (chan == NULL)
  846. return -EINVAL;
  847. pr_debug("%s: source=%d, devaddr=%08lx\n",
  848. __func__, (int)source, devaddr);
  849. chan->source = source;
  850. chan->dev_addr = devaddr;
  851. switch (chan->req_ch) {
  852. case DMACH_XD0:
  853. case DMACH_XD1:
  854. hwcfg = 0; /* AHB */
  855. break;
  856. default:
  857. hwcfg = S3C2410_DISRCC_APB;
  858. }
  859. /* always assume our peripheral desintation is a fixed
  860. * address in memory. */
  861. hwcfg |= S3C2410_DISRCC_INC;
  862. switch (source) {
  863. case DMA_FROM_DEVICE:
  864. /* source is hardware */
  865. pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
  866. __func__, devaddr, hwcfg);
  867. dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
  868. dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
  869. dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
  870. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
  871. break;
  872. case DMA_TO_DEVICE:
  873. /* source is memory */
  874. pr_debug("%s: mem source, devaddr=%08lx, hwcfg=%d\n",
  875. __func__, devaddr, hwcfg);
  876. dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
  877. dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
  878. dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
  879. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
  880. break;
  881. default:
  882. printk(KERN_ERR "dma%d: invalid source type (%d)\n",
  883. channel, source);
  884. return -EINVAL;
  885. }
  886. if (dma_sel.direction != NULL)
  887. (dma_sel.direction)(chan, chan->map, source);
  888. return 0;
  889. }
  890. EXPORT_SYMBOL(s3c2410_dma_devconfig);
  891. /* s3c2410_dma_getposition
  892. *
  893. * returns the current transfer points for the dma source and destination
  894. */
  895. int s3c2410_dma_getposition(enum dma_ch channel, dma_addr_t *src, dma_addr_t *dst)
  896. {
  897. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  898. if (chan == NULL)
  899. return -EINVAL;
  900. if (src != NULL)
  901. *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  902. if (dst != NULL)
  903. *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
  904. return 0;
  905. }
  906. EXPORT_SYMBOL(s3c2410_dma_getposition);
  907. /* system core operations */
  908. #ifdef CONFIG_PM
  909. static void s3c2410_dma_suspend_chan(struct s3c2410_dma_chan *cp)
  910. {
  911. printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
  912. if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
  913. /* the dma channel is still working, which is probably
  914. * a bad thing to do over suspend/resume. We stop the
  915. * channel and assume that the client is either going to
  916. * retry after resume, or that it is broken.
  917. */
  918. printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
  919. cp->number);
  920. s3c2410_dma_dostop(cp);
  921. }
  922. }
  923. static int s3c2410_dma_suspend(void)
  924. {
  925. struct s3c2410_dma_chan *cp = s3c2410_chans;
  926. int channel;
  927. for (channel = 0; channel < dma_channels; cp++, channel++)
  928. s3c2410_dma_suspend_chan(cp);
  929. return 0;
  930. }
  931. static void s3c2410_dma_resume_chan(struct s3c2410_dma_chan *cp)
  932. {
  933. unsigned int no = cp->number | DMACH_LOW_LEVEL;
  934. /* restore channel's hardware configuration */
  935. if (!cp->in_use)
  936. return;
  937. printk(KERN_INFO "dma%d: restoring configuration\n", cp->number);
  938. s3c2410_dma_config(no, cp->xfer_unit);
  939. s3c2410_dma_devconfig(no, cp->source, cp->dev_addr);
  940. /* re-select the dma source for this channel */
  941. if (cp->map != NULL)
  942. dma_sel.select(cp, cp->map);
  943. }
  944. static void s3c2410_dma_resume(void)
  945. {
  946. struct s3c2410_dma_chan *cp = s3c2410_chans + dma_channels - 1;
  947. int channel;
  948. for (channel = dma_channels - 1; channel >= 0; cp--, channel--)
  949. s3c2410_dma_resume_chan(cp);
  950. }
  951. #else
  952. #define s3c2410_dma_suspend NULL
  953. #define s3c2410_dma_resume NULL
  954. #endif /* CONFIG_PM */
  955. struct syscore_ops dma_syscore_ops = {
  956. .suspend = s3c2410_dma_suspend,
  957. .resume = s3c2410_dma_resume,
  958. };
  959. /* kmem cache implementation */
  960. static void s3c2410_dma_cache_ctor(void *p)
  961. {
  962. memset(p, 0, sizeof(struct s3c2410_dma_buf));
  963. }
  964. /* initialisation code */
  965. static int __init s3c24xx_dma_syscore_init(void)
  966. {
  967. register_syscore_ops(&dma_syscore_ops);
  968. return 0;
  969. }
  970. late_initcall(s3c24xx_dma_syscore_init);
  971. int __init s3c24xx_dma_init(unsigned int channels, unsigned int irq,
  972. unsigned int stride)
  973. {
  974. struct s3c2410_dma_chan *cp;
  975. int channel;
  976. int ret;
  977. printk("S3C24XX DMA Driver, Copyright 2003-2006 Simtec Electronics\n");
  978. dma_channels = channels;
  979. dma_base = ioremap(S3C24XX_PA_DMA, stride * channels);
  980. if (dma_base == NULL) {
  981. printk(KERN_ERR "dma failed to remap register block\n");
  982. return -ENOMEM;
  983. }
  984. dma_kmem = kmem_cache_create("dma_desc",
  985. sizeof(struct s3c2410_dma_buf), 0,
  986. SLAB_HWCACHE_ALIGN,
  987. s3c2410_dma_cache_ctor);
  988. if (dma_kmem == NULL) {
  989. printk(KERN_ERR "dma failed to make kmem cache\n");
  990. ret = -ENOMEM;
  991. goto err;
  992. }
  993. for (channel = 0; channel < channels; channel++) {
  994. cp = &s3c2410_chans[channel];
  995. memset(cp, 0, sizeof(struct s3c2410_dma_chan));
  996. /* dma channel irqs are in order.. */
  997. cp->number = channel;
  998. cp->irq = channel + irq;
  999. cp->regs = dma_base + (channel * stride);
  1000. /* point current stats somewhere */
  1001. cp->stats = &cp->stats_store;
  1002. cp->stats_store.timeout_shortest = LONG_MAX;
  1003. /* basic channel configuration */
  1004. cp->load_timeout = 1<<18;
  1005. printk("DMA channel %d at %p, irq %d\n",
  1006. cp->number, cp->regs, cp->irq);
  1007. }
  1008. return 0;
  1009. err:
  1010. kmem_cache_destroy(dma_kmem);
  1011. iounmap(dma_base);
  1012. dma_base = NULL;
  1013. return ret;
  1014. }
  1015. int __init s3c2410_dma_init(void)
  1016. {
  1017. return s3c24xx_dma_init(4, IRQ_DMA0, 0x40);
  1018. }
  1019. static inline int is_channel_valid(unsigned int channel)
  1020. {
  1021. return (channel & DMA_CH_VALID);
  1022. }
  1023. static struct s3c24xx_dma_order *dma_order;
  1024. /* s3c2410_dma_map_channel()
  1025. *
  1026. * turn the virtual channel number into a real, and un-used hardware
  1027. * channel.
  1028. *
  1029. * first, try the dma ordering given to us by either the relevant
  1030. * dma code, or the board. Then just find the first usable free
  1031. * channel
  1032. */
  1033. static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel)
  1034. {
  1035. struct s3c24xx_dma_order_ch *ord = NULL;
  1036. struct s3c24xx_dma_map *ch_map;
  1037. struct s3c2410_dma_chan *dmach;
  1038. int ch;
  1039. if (dma_sel.map == NULL || channel > dma_sel.map_size)
  1040. return NULL;
  1041. ch_map = dma_sel.map + channel;
  1042. /* first, try the board mapping */
  1043. if (dma_order) {
  1044. ord = &dma_order->channels[channel];
  1045. for (ch = 0; ch < dma_channels; ch++) {
  1046. int tmp;
  1047. if (!is_channel_valid(ord->list[ch]))
  1048. continue;
  1049. tmp = ord->list[ch] & ~DMA_CH_VALID;
  1050. if (s3c2410_chans[tmp].in_use == 0) {
  1051. ch = tmp;
  1052. goto found;
  1053. }
  1054. }
  1055. if (ord->flags & DMA_CH_NEVER)
  1056. return NULL;
  1057. }
  1058. /* second, search the channel map for first free */
  1059. for (ch = 0; ch < dma_channels; ch++) {
  1060. if (!is_channel_valid(ch_map->channels[ch]))
  1061. continue;
  1062. if (s3c2410_chans[ch].in_use == 0) {
  1063. printk("mapped channel %d to %d\n", channel, ch);
  1064. break;
  1065. }
  1066. }
  1067. if (ch >= dma_channels)
  1068. return NULL;
  1069. /* update our channel mapping */
  1070. found:
  1071. dmach = &s3c2410_chans[ch];
  1072. dmach->map = ch_map;
  1073. dmach->req_ch = channel;
  1074. s3c_dma_chan_map[channel] = dmach;
  1075. /* select the channel */
  1076. (dma_sel.select)(dmach, ch_map);
  1077. return dmach;
  1078. }
  1079. static int s3c24xx_dma_check_entry(struct s3c24xx_dma_map *map, int ch)
  1080. {
  1081. return 0;
  1082. }
  1083. int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)
  1084. {
  1085. struct s3c24xx_dma_map *nmap;
  1086. size_t map_sz = sizeof(*nmap) * sel->map_size;
  1087. int ptr;
  1088. nmap = kmemdup(sel->map, map_sz, GFP_KERNEL);
  1089. if (nmap == NULL)
  1090. return -ENOMEM;
  1091. memcpy(&dma_sel, sel, sizeof(*sel));
  1092. dma_sel.map = nmap;
  1093. for (ptr = 0; ptr < sel->map_size; ptr++)
  1094. s3c24xx_dma_check_entry(nmap+ptr, ptr);
  1095. return 0;
  1096. }
  1097. int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)
  1098. {
  1099. struct s3c24xx_dma_order *nord = dma_order;
  1100. if (nord == NULL)
  1101. nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);
  1102. if (nord == NULL) {
  1103. printk(KERN_ERR "no memory to store dma channel order\n");
  1104. return -ENOMEM;
  1105. }
  1106. dma_order = nord;
  1107. memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));
  1108. return 0;
  1109. }