dma.c 31 KB

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