dma.c 33 KB

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