dma.c 33 KB

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