dma.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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/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-s3c24xx.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 believe 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(enum dma_ch 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(enum dma_ch 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(enum dma_ch 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(enum dma_ch 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\n", __func__, channel, xferunit);
  785. if (chan == NULL)
  786. return -EINVAL;
  787. dcon = chan->dcon & dma_sel.dcon_mask;
  788. pr_debug("%s: dcon is %08x\n", __func__, dcon);
  789. switch (chan->req_ch) {
  790. case DMACH_I2S_IN:
  791. case DMACH_I2S_OUT:
  792. case DMACH_PCM_IN:
  793. case DMACH_PCM_OUT:
  794. case DMACH_MIC_IN:
  795. default:
  796. dcon |= S3C2410_DCON_HANDSHAKE;
  797. dcon |= S3C2410_DCON_SYNC_PCLK;
  798. break;
  799. case DMACH_SDI:
  800. /* note, ensure if need HANDSHAKE or not */
  801. dcon |= S3C2410_DCON_SYNC_PCLK;
  802. break;
  803. case DMACH_XD0:
  804. case DMACH_XD1:
  805. dcon |= S3C2410_DCON_HANDSHAKE;
  806. dcon |= S3C2410_DCON_SYNC_HCLK;
  807. break;
  808. }
  809. switch (xferunit) {
  810. case 1:
  811. dcon |= S3C2410_DCON_BYTE;
  812. break;
  813. case 2:
  814. dcon |= S3C2410_DCON_HALFWORD;
  815. break;
  816. case 4:
  817. dcon |= S3C2410_DCON_WORD;
  818. break;
  819. default:
  820. pr_debug("%s: bad transfer size %d\n", __func__, xferunit);
  821. return -EINVAL;
  822. }
  823. dcon |= S3C2410_DCON_HWTRIG;
  824. dcon |= S3C2410_DCON_INTREQ;
  825. pr_debug("%s: dcon now %08x\n", __func__, dcon);
  826. chan->dcon = dcon;
  827. chan->xfer_unit = xferunit;
  828. return 0;
  829. }
  830. EXPORT_SYMBOL(s3c2410_dma_config);
  831. /* s3c2410_dma_devconfig
  832. *
  833. * configure the dma source/destination hardware type and address
  834. *
  835. * source: S3C2410_DMASRC_HW: source is hardware
  836. * S3C2410_DMASRC_MEM: source is memory
  837. *
  838. * devaddr: physical address of the source
  839. */
  840. int s3c2410_dma_devconfig(enum dma_ch channel,
  841. enum s3c2410_dmasrc source,
  842. unsigned long devaddr)
  843. {
  844. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  845. unsigned int hwcfg;
  846. if (chan == NULL)
  847. return -EINVAL;
  848. pr_debug("%s: source=%d, devaddr=%08lx\n",
  849. __func__, (int)source, devaddr);
  850. chan->source = source;
  851. chan->dev_addr = devaddr;
  852. switch (chan->req_ch) {
  853. case DMACH_XD0:
  854. case DMACH_XD1:
  855. hwcfg = 0; /* AHB */
  856. break;
  857. default:
  858. hwcfg = S3C2410_DISRCC_APB;
  859. }
  860. /* always assume our peripheral desintation is a fixed
  861. * address in memory. */
  862. hwcfg |= S3C2410_DISRCC_INC;
  863. switch (source) {
  864. case S3C2410_DMASRC_HW:
  865. /* source is hardware */
  866. pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
  867. __func__, devaddr, hwcfg);
  868. dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);
  869. dma_wrreg(chan, S3C2410_DMA_DISRC, devaddr);
  870. dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));
  871. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);
  872. break;
  873. case S3C2410_DMASRC_MEM:
  874. /* source is memory */
  875. pr_debug("%s: mem source, devaddr=%08lx, hwcfg=%d\n",
  876. __func__, devaddr, hwcfg);
  877. dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
  878. dma_wrreg(chan, S3C2410_DMA_DIDST, devaddr);
  879. dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);
  880. chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
  881. break;
  882. default:
  883. printk(KERN_ERR "dma%d: invalid source type (%d)\n",
  884. channel, source);
  885. return -EINVAL;
  886. }
  887. if (dma_sel.direction != NULL)
  888. (dma_sel.direction)(chan, chan->map, source);
  889. return 0;
  890. }
  891. EXPORT_SYMBOL(s3c2410_dma_devconfig);
  892. /* s3c2410_dma_getposition
  893. *
  894. * returns the current transfer points for the dma source and destination
  895. */
  896. int s3c2410_dma_getposition(enum dma_ch channel, dma_addr_t *src, dma_addr_t *dst)
  897. {
  898. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  899. if (chan == NULL)
  900. return -EINVAL;
  901. if (src != NULL)
  902. *src = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  903. if (dst != NULL)
  904. *dst = dma_rdreg(chan, S3C2410_DMA_DCDST);
  905. return 0;
  906. }
  907. EXPORT_SYMBOL(s3c2410_dma_getposition);
  908. /* system core operations */
  909. #ifdef CONFIG_PM
  910. static void s3c2410_dma_suspend_chan(struct s3c2410_dma_chan *cp)
  911. {
  912. printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
  913. if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
  914. /* the dma channel is still working, which is probably
  915. * a bad thing to do over suspend/resume. We stop the
  916. * channel and assume that the client is either going to
  917. * retry after resume, or that it is broken.
  918. */
  919. printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
  920. cp->number);
  921. s3c2410_dma_dostop(cp);
  922. }
  923. }
  924. static int s3c2410_dma_suspend(void)
  925. {
  926. struct s3c2410_dma_chan *cp = s3c2410_chans;
  927. int channel;
  928. for (channel = 0; channel < dma_channels; cp++, channel++)
  929. s3c2410_dma_suspend_chan(cp);
  930. return 0;
  931. }
  932. static void s3c2410_dma_resume_chan(struct s3c2410_dma_chan *cp)
  933. {
  934. unsigned int no = cp->number | DMACH_LOW_LEVEL;
  935. /* restore channel's hardware configuration */
  936. if (!cp->in_use)
  937. return;
  938. printk(KERN_INFO "dma%d: restoring configuration\n", cp->number);
  939. s3c2410_dma_config(no, cp->xfer_unit);
  940. s3c2410_dma_devconfig(no, cp->source, cp->dev_addr);
  941. /* re-select the dma source for this channel */
  942. if (cp->map != NULL)
  943. dma_sel.select(cp, cp->map);
  944. }
  945. static void s3c2410_dma_resume(void)
  946. {
  947. struct s3c2410_dma_chan *cp = s3c2410_chans + dma_channels - 1;
  948. int channel;
  949. for (channel = dma_channels - 1; channel >= 0; cp++, channel--)
  950. s3c2410_dma_resume_chan(cp);
  951. }
  952. #else
  953. #define s3c2410_dma_suspend NULL
  954. #define s3c2410_dma_resume NULL
  955. #endif /* CONFIG_PM */
  956. struct syscore_ops dma_syscore_ops = {
  957. .suspend = s3c2410_dma_suspend,
  958. .resume = s3c2410_dma_resume,
  959. };
  960. /* kmem cache implementation */
  961. static void s3c2410_dma_cache_ctor(void *p)
  962. {
  963. memset(p, 0, sizeof(struct s3c2410_dma_buf));
  964. }
  965. /* initialisation code */
  966. static int __init s3c24xx_dma_syscore_init(void)
  967. {
  968. register_syscore_ops(&dma_syscore_ops);
  969. return 0;
  970. }
  971. late_initcall(s3c24xx_dma_syscore_init);
  972. int __init s3c24xx_dma_init(unsigned int channels, unsigned int irq,
  973. unsigned int stride)
  974. {
  975. struct s3c2410_dma_chan *cp;
  976. int channel;
  977. int ret;
  978. printk("S3C24XX DMA Driver, Copyright 2003-2006 Simtec Electronics\n");
  979. dma_channels = channels;
  980. dma_base = ioremap(S3C24XX_PA_DMA, stride * channels);
  981. if (dma_base == NULL) {
  982. printk(KERN_ERR "dma failed to remap register block\n");
  983. return -ENOMEM;
  984. }
  985. dma_kmem = kmem_cache_create("dma_desc",
  986. sizeof(struct s3c2410_dma_buf), 0,
  987. SLAB_HWCACHE_ALIGN,
  988. s3c2410_dma_cache_ctor);
  989. if (dma_kmem == NULL) {
  990. printk(KERN_ERR "dma failed to make kmem cache\n");
  991. ret = -ENOMEM;
  992. goto err;
  993. }
  994. for (channel = 0; channel < channels; channel++) {
  995. cp = &s3c2410_chans[channel];
  996. memset(cp, 0, sizeof(struct s3c2410_dma_chan));
  997. /* dma channel irqs are in order.. */
  998. cp->number = channel;
  999. cp->irq = channel + irq;
  1000. cp->regs = dma_base + (channel * stride);
  1001. /* point current stats somewhere */
  1002. cp->stats = &cp->stats_store;
  1003. cp->stats_store.timeout_shortest = LONG_MAX;
  1004. /* basic channel configuration */
  1005. cp->load_timeout = 1<<18;
  1006. printk("DMA channel %d at %p, irq %d\n",
  1007. cp->number, cp->regs, cp->irq);
  1008. }
  1009. return 0;
  1010. err:
  1011. kmem_cache_destroy(dma_kmem);
  1012. iounmap(dma_base);
  1013. dma_base = NULL;
  1014. return ret;
  1015. }
  1016. int __init s3c2410_dma_init(void)
  1017. {
  1018. return s3c24xx_dma_init(4, IRQ_DMA0, 0x40);
  1019. }
  1020. static inline int is_channel_valid(unsigned int channel)
  1021. {
  1022. return (channel & DMA_CH_VALID);
  1023. }
  1024. static struct s3c24xx_dma_order *dma_order;
  1025. /* s3c2410_dma_map_channel()
  1026. *
  1027. * turn the virtual channel number into a real, and un-used hardware
  1028. * channel.
  1029. *
  1030. * first, try the dma ordering given to us by either the relevant
  1031. * dma code, or the board. Then just find the first usable free
  1032. * channel
  1033. */
  1034. static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel)
  1035. {
  1036. struct s3c24xx_dma_order_ch *ord = NULL;
  1037. struct s3c24xx_dma_map *ch_map;
  1038. struct s3c2410_dma_chan *dmach;
  1039. int ch;
  1040. if (dma_sel.map == NULL || channel > dma_sel.map_size)
  1041. return NULL;
  1042. ch_map = dma_sel.map + channel;
  1043. /* first, try the board mapping */
  1044. if (dma_order) {
  1045. ord = &dma_order->channels[channel];
  1046. for (ch = 0; ch < dma_channels; ch++) {
  1047. int tmp;
  1048. if (!is_channel_valid(ord->list[ch]))
  1049. continue;
  1050. tmp = ord->list[ch] & ~DMA_CH_VALID;
  1051. if (s3c2410_chans[tmp].in_use == 0) {
  1052. ch = tmp;
  1053. goto found;
  1054. }
  1055. }
  1056. if (ord->flags & DMA_CH_NEVER)
  1057. return NULL;
  1058. }
  1059. /* second, search the channel map for first free */
  1060. for (ch = 0; ch < dma_channels; ch++) {
  1061. if (!is_channel_valid(ch_map->channels[ch]))
  1062. continue;
  1063. if (s3c2410_chans[ch].in_use == 0) {
  1064. printk("mapped channel %d to %d\n", channel, ch);
  1065. break;
  1066. }
  1067. }
  1068. if (ch >= dma_channels)
  1069. return NULL;
  1070. /* update our channel mapping */
  1071. found:
  1072. dmach = &s3c2410_chans[ch];
  1073. dmach->map = ch_map;
  1074. dmach->req_ch = channel;
  1075. s3c_dma_chan_map[channel] = dmach;
  1076. /* select the channel */
  1077. (dma_sel.select)(dmach, ch_map);
  1078. return dmach;
  1079. }
  1080. static int s3c24xx_dma_check_entry(struct s3c24xx_dma_map *map, int ch)
  1081. {
  1082. return 0;
  1083. }
  1084. int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)
  1085. {
  1086. struct s3c24xx_dma_map *nmap;
  1087. size_t map_sz = sizeof(*nmap) * sel->map_size;
  1088. int ptr;
  1089. nmap = kmalloc(map_sz, GFP_KERNEL);
  1090. if (nmap == NULL)
  1091. return -ENOMEM;
  1092. memcpy(nmap, sel->map, map_sz);
  1093. memcpy(&dma_sel, sel, sizeof(*sel));
  1094. dma_sel.map = nmap;
  1095. for (ptr = 0; ptr < sel->map_size; ptr++)
  1096. s3c24xx_dma_check_entry(nmap+ptr, ptr);
  1097. return 0;
  1098. }
  1099. int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)
  1100. {
  1101. struct s3c24xx_dma_order *nord = dma_order;
  1102. if (nord == NULL)
  1103. nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);
  1104. if (nord == NULL) {
  1105. printk(KERN_ERR "no memory to store dma channel order\n");
  1106. return -ENOMEM;
  1107. }
  1108. dma_order = nord;
  1109. memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));
  1110. return 0;
  1111. }