dma.c 34 KB

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