memoryOperation.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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/irq.h>
  27. #include <mach/hardware.h>
  28. #include <mach/dma.h>
  29. #include <mach/map.h>
  30. #include <plat/dma-s3c24xx.h>
  31. #include <plat/regs-dma.h>
  32. /* io map for dma */
  33. static void __iomem *dma_base;
  34. static struct kmem_cache *dma_kmem;
  35. static int dma_channels;
  36. static struct s3c24xx_dma_selection dma_sel;
  37. /* debugging functions */
  38. #define BUF_MAGIC (0xcafebabe)
  39. #define dmawarn(fmt...) printk(KERN_DEBUG fmt)
  40. #define dma_regaddr(chan, reg) ((chan)->regs + (reg))
  41. #if 1
  42. #define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
  43. #else
  44. static inline void
  45. dma_wrreg(struct s3c2410_dma_chan *chan, int reg, unsigned long val)
  46. {
  47. pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
  48. writel(val, dma_regaddr(chan, reg));
  49. }
  50. #endif
  51. #define dma_rdreg(chan, reg) readl((chan)->regs + (reg))
  52. /* captured register state for debug */
  53. struct s3c2410_dma_regstate {
  54. unsigned long dcsrc;
  55. unsigned long disrc;
  56. unsigned long dstat;
  57. unsigned long dcon;
  58. unsigned long dmsktrig;
  59. };
  60. #ifdef CONFIG_S3C2410_DMA_DEBUG
  61. /* dmadbg_showregs
  62. *
  63. * simple debug routine to print the current state of the dma registers
  64. */
  65. static void
  66. dmadbg_capture(struct s3c2410_dma_chan *chan, struct s3c2410_dma_regstate *regs)
  67. {
  68. regs->dcsrc = dma_rdreg(chan, S3C2410_DMA_DCSRC);
  69. regs->disrc = dma_rdreg(chan, S3C2410_DMA_DISRC);
  70. regs->dstat = dma_rdreg(chan, S3C2410_DMA_DSTAT);
  71. regs->dcon = dma_rdreg(chan, S3C2410_DMA_DCON);
  72. regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  73. }
  74. static void
  75. dmadbg_dumpregs(const char *fname, int line, struct s3c2410_dma_chan *chan,
  76. struct s3c2410_dma_regstate *regs)
  77. {
  78. printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
  79. chan->number, fname, line,
  80. regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
  81. regs->dcon);
  82. }
  83. static void
  84. dmadbg_showchan(const char *fname, int line, struct s3c2410_dma_chan *chan)
  85. {
  86. struct s3c2410_dma_regstate state;
  87. dmadbg_capture(chan, &state);
  88. printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
  89. chan->number, fname, line, chan->load_state,
  90. chan->curr, chan->next, chan->end);
  91. dmadbg_dumpregs(fname, line, chan, &state);
  92. }
  93. static void
  94. dmadbg_showregs(const char *fname, int line, struct s3c2410_dma_chan *chan)
  95. {
  96. struct s3c2410_dma_regstate state;
  97. dmadbg_capture(chan, &state);
  98. dmadbg_dumpregs(fname, line, chan, &state);
  99. }
  100. #define dbg_showregs(chan) dmadbg_showregs(__func__, __LINE__, (chan))
  101. #define dbg_showchan(chan) dmadbg_showchan(__func__, __LINE__, (chan))
  102. #else
  103. #define dbg_showregs(chan) do { } while(0)
  104. #define dbg_showchan(chan) do { } while(0)
  105. #endif /* CONFIG_S3C2410_DMA_DEBUG */
  106. /* s3c2410_dma_stats_timeout
  107. *
  108. * Update DMA stats from timeout info
  109. */
  110. static void
  111. s3c2410_dma_stats_timeout(struct s3c2410_dma_stats *stats, int val)
  112. {
  113. if (stats == NULL)
  114. return;
  115. if (val > stats->timeout_longest)
  116. stats->timeout_longest = val;
  117. if (val < stats->timeout_shortest)
  118. stats->timeout_shortest = val;
  119. stats->timeout_avg += val;
  120. }
  121. /* s3c2410_dma_waitforload
  122. *
  123. * wait for the DMA engine to load a buffer, and update the state accordingly
  124. */
  125. static int
  126. s3c2410_dma_waitforload(struct s3c2410_dma_chan *chan, int line)
  127. {
  128. int timeout = chan->load_timeout;
  129. int took;
  130. if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
  131. printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
  132. return 0;
  133. }
  134. if (chan->stats != NULL)
  135. chan->stats->loads++;
  136. while (--timeout > 0) {
  137. if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {
  138. took = chan->load_timeout - timeout;
  139. s3c2410_dma_stats_timeout(chan->stats, took);
  140. switch (chan->load_state) {
  141. case S3C2410_DMALOAD_1LOADED:
  142. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  143. break;
  144. default:
  145. printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
  146. }
  147. return 1;
  148. }
  149. }
  150. if (chan->stats != NULL) {
  151. chan->stats->timeout_failed++;
  152. }
  153. return 0;
  154. }
  155. /* s3c2410_dma_loadbuffer
  156. *
  157. * load a buffer, and update the channel state
  158. */
  159. static inline int
  160. s3c2410_dma_loadbuffer(struct s3c2410_dma_chan *chan,
  161. struct s3c2410_dma_buf *buf)
  162. {
  163. unsigned long reload;
  164. if (buf == NULL) {
  165. dmawarn("buffer is NULL\n");
  166. return -EINVAL;
  167. }
  168. pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
  169. buf, (unsigned long)buf->data, buf->size);
  170. /* check the state of the channel before we do anything */
  171. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  172. dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
  173. }
  174. if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {
  175. dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
  176. }
  177. /* it would seem sensible if we are the last buffer to not bother
  178. * with the auto-reload bit, so that the DMA engine will not try
  179. * and load another transfer after this one has finished...
  180. */
  181. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  182. pr_debug("load_state is none, checking for noreload (next=%p)\n",
  183. buf->next);
  184. reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
  185. } else {
  186. //pr_debug("load_state is %d => autoreload\n", chan->load_state);
  187. reload = S3C2410_DCON_AUTORELOAD;
  188. }
  189. if ((buf->data & 0xf0000000) != 0x30000000) {
  190. dmawarn("dmaload: buffer is %p\n", (void *)buf->data);
  191. }
  192. writel(buf->data, chan->addr_reg);
  193. dma_wrreg(chan, S3C2410_DMA_DCON,
  194. chan->dcon | reload | (buf->size/chan->xfer_unit));
  195. chan->next = buf->next;
  196. /* update the state of the channel */
  197. switch (chan->load_state) {
  198. case S3C2410_DMALOAD_NONE:
  199. chan->load_state = S3C2410_DMALOAD_1LOADED;
  200. break;
  201. case S3C2410_DMALOAD_1RUNNING:
  202. chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
  203. break;
  204. default:
  205. dmawarn("dmaload: unknown state %d in loadbuffer\n",
  206. chan->load_state);
  207. break;
  208. }
  209. return 0;
  210. }
  211. /* s3c2410_dma_call_op
  212. *
  213. * small routine to call the op routine with the given op if it has been
  214. * registered
  215. */
  216. static void
  217. s3c2410_dma_call_op(struct s3c2410_dma_chan *chan, enum s3c2410_chan_op op)
  218. {
  219. if (chan->op_fn != NULL) {
  220. (chan->op_fn)(chan, op);
  221. }
  222. }
  223. /* s3c2410_dma_buffdone
  224. *
  225. * small wrapper to check if callback routine needs to be called, and
  226. * if so, call it
  227. */
  228. static inline void
  229. s3c2410_dma_buffdone(struct s3c2410_dma_chan *chan, struct s3c2410_dma_buf *buf,
  230. enum s3c2410_dma_buffresult result)
  231. {
  232. #if 0
  233. pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
  234. chan->callback_fn, buf, buf->id, buf->size, result);
  235. #endif
  236. if (chan->callback_fn != NULL) {
  237. (chan->callback_fn)(chan, buf->id, buf->size, result);
  238. }
  239. }
  240. /* s3c2410_dma_start
  241. *
  242. * start a dma channel going
  243. */
  244. static int s3c2410_dma_start(struct s3c2410_dma_chan *chan)
  245. {
  246. unsigned long tmp;
  247. unsigned long flags;
  248. pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);
  249. local_irq_save(flags);
  250. if (chan->state == S3C2410_DMA_RUNNING) {
  251. pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
  252. local_irq_restore(flags);
  253. return 0;
  254. }
  255. chan->state = S3C2410_DMA_RUNNING;
  256. /* check whether there is anything to load, and if not, see
  257. * if we can find anything to load
  258. */
  259. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  260. if (chan->next == NULL) {
  261. printk(KERN_ERR "dma%d: channel has nothing loaded\n",
  262. chan->number);
  263. chan->state = S3C2410_DMA_IDLE;
  264. local_irq_restore(flags);
  265. return -EINVAL;
  266. }
  267. s3c2410_dma_loadbuffer(chan, chan->next);
  268. }
  269. dbg_showchan(chan);
  270. /* enable the channel */
  271. if (!chan->irq_enabled) {
  272. enable_irq(chan->irq);
  273. chan->irq_enabled = 1;
  274. }
  275. /* start the channel going */
  276. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  277. tmp &= ~S3C2410_DMASKTRIG_STOP;
  278. tmp |= S3C2410_DMASKTRIG_ON;
  279. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  280. pr_debug("dma%d: %08lx to DMASKTRIG\n", chan->number, tmp);
  281. #if 0
  282. /* the dma buffer loads should take care of clearing the AUTO
  283. * reloading feature */
  284. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  285. tmp &= ~S3C2410_DCON_NORELOAD;
  286. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  287. #endif
  288. s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);
  289. dbg_showchan(chan);
  290. /* if we've only loaded one buffer onto the channel, then chec
  291. * to see if we have another, and if so, try and load it so when
  292. * the first buffer is finished, the new one will be loaded onto
  293. * the channel */
  294. if (chan->next != NULL) {
  295. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  296. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  297. pr_debug("%s: buff not yet loaded, no more todo\n",
  298. __func__);
  299. } else {
  300. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  301. s3c2410_dma_loadbuffer(chan, chan->next);
  302. }
  303. } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
  304. s3c2410_dma_loadbuffer(chan, chan->next);
  305. }
  306. }
  307. local_irq_restore(flags);
  308. return 0;
  309. }
  310. /* s3c2410_dma_canload
  311. *
  312. * work out if we can queue another buffer into the DMA engine
  313. */
  314. static int
  315. s3c2410_dma_canload(struct s3c2410_dma_chan *chan)
  316. {
  317. if (chan->load_state == S3C2410_DMALOAD_NONE ||
  318. chan->load_state == S3C2410_DMALOAD_1RUNNING)
  319. return 1;
  320. return 0;
  321. }
  322. /* s3c2410_dma_enqueue
  323. *
  324. * queue an given buffer for dma transfer.
  325. *
  326. * id the device driver's id information for this buffer
  327. * data the physical address of the buffer data
  328. * size the size of the buffer in bytes
  329. *
  330. * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
  331. * is checked, and if set, the channel is started. If this flag isn't set,
  332. * then an error will be returned.
  333. *
  334. * It is possible to queue more than one DMA buffer onto a channel at
  335. * once, and the code will deal with the re-loading of the next buffer
  336. * when necessary.
  337. */
  338. int s3c2410_dma_enqueue(enum dma_ch channel, void *id,
  339. dma_addr_t data, int size)
  340. {
  341. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  342. struct s3c2410_dma_buf *buf;
  343. unsigned long flags;
  344. if (chan == NULL)
  345. return -EINVAL;
  346. pr_debug("%s: id=%p, data=%08x, size=%d\n",
  347. __func__, id, (unsigned int)data, size);
  348. buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
  349. if (buf == NULL) {
  350. pr_debug("%s: out of memory (%ld alloc)\n",
  351. __func__, (long)sizeof(*buf));
  352. return -ENOMEM;
  353. }
  354. //pr_debug("%s: new buffer %p\n", __func__, buf);
  355. //dbg_showchan(chan);
  356. buf->next = NULL;
  357. buf->data = buf->ptr = data;
  358. buf->size = size;
  359. buf->id = id;
  360. buf->magic = BUF_MAGIC;
  361. local_irq_save(flags);
  362. if (chan->curr == NULL) {
  363. /* we've got nothing loaded... */
  364. pr_debug("%s: buffer %p queued onto empty channel\n",
  365. __func__, buf);
  366. chan->curr = buf;
  367. chan->end = buf;
  368. chan->next = NULL;
  369. } else {
  370. pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
  371. chan->number, __func__, buf);
  372. if (chan->end == NULL) {
  373. pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
  374. chan->number, __func__, chan);
  375. } else {
  376. chan->end->next = buf;
  377. chan->end = buf;
  378. }
  379. }
  380. /* if necessary, update the next buffer field */
  381. if (chan->next == NULL)
  382. chan->next = buf;
  383. /* check to see if we can load a buffer */
  384. if (chan->state == S3C2410_DMA_RUNNING) {
  385. if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {
  386. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  387. printk(KERN_ERR "dma%d: loadbuffer:"
  388. "timeout loading buffer\n",
  389. chan->number);
  390. dbg_showchan(chan);
  391. local_irq_restore(flags);
  392. return -EINVAL;
  393. }
  394. }
  395. while (s3c2410_dma_canload(chan) && chan->next != NULL) {
  396. s3c2410_dma_loadbuffer(chan, chan->next);
  397. }
  398. } else if (chan->state == S3C2410_DMA_IDLE) {
  399. if (chan->flags & S3C2410_DMAF_AUTOSTART) {
  400. s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
  401. S3C2410_DMAOP_START);
  402. }
  403. }
  404. local_irq_restore(flags);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL(s3c2410_dma_enqueue);
  408. static inline void
  409. s3c2410_dma_freebuf(struct s3c2410_dma_buf *buf)
  410. {
  411. int magicok = (buf->magic == BUF_MAGIC);
  412. buf->magic = -1;
  413. if (magicok) {
  414. kmem_cache_free(dma_kmem, buf);
  415. } else {
  416. printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
  417. }
  418. }
  419. /* s3c2410_dma_lastxfer
  420. *
  421. * called when the system is out of buffers, to ensure that the channel
  422. * is prepared for shutdown.
  423. */
  424. static inline void
  425. s3c2410_dma_lastxfer(struct s3c2410_dma_chan *chan)
  426. {
  427. #if 0
  428. pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
  429. chan->number, chan->load_state);
  430. #endif
  431. switch (chan->load_state) {
  432. case S3C2410_DMALOAD_NONE:
  433. break;
  434. case S3C2410_DMALOAD_1LOADED:
  435. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  436. /* flag error? */
  437. printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
  438. chan->number, __func__);
  439. return;
  440. }
  441. break;
  442. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  443. /* I believe in this case we do not have anything to do
  444. * until the next buffer comes along, and we turn off the
  445. * reload */
  446. return;
  447. default:
  448. pr_debug("dma%d: lastxfer: unhandled load_state %d with no next\n",
  449. chan->number, chan->load_state);
  450. return;
  451. }
  452. /* hopefully this'll shut the damned thing up after the transfer... */
  453. dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
  454. }
  455. #define dmadbg2(x...)
  456. static irqreturn_t
  457. s3c2410_dma_irq(int irq, void *devpw)
  458. {
  459. struct s3c2410_dma_chan *chan = (struct s3c2410_dma_chan *)devpw;
  460. struct s3c2410_dma_buf *buf;
  461. buf = chan->curr;
  462. dbg_showchan(chan);
  463. /* modify the channel state */
  464. switch (chan->load_state) {
  465. case S3C2410_DMALOAD_1RUNNING:
  466. /* TODO - if we are running only one buffer, we probably
  467. * want to reload here, and then worry about the buffer
  468. * callback */
  469. chan->load_state = S3C2410_DMALOAD_NONE;
  470. break;
  471. case S3C2410_DMALOAD_1LOADED:
  472. /* iirc, we should go back to NONE loaded here, we
  473. * had a buffer, and it was never verified as being
  474. * loaded.
  475. */
  476. chan->load_state = S3C2410_DMALOAD_NONE;
  477. break;
  478. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  479. /* we'll worry about checking to see if another buffer is
  480. * ready after we've called back the owner. This should
  481. * ensure we do not wait around too long for the DMA
  482. * engine to start the next transfer
  483. */
  484. chan->load_state = S3C2410_DMALOAD_1LOADED;
  485. break;
  486. case S3C2410_DMALOAD_NONE:
  487. printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
  488. chan->number);
  489. break;
  490. default:
  491. printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
  492. chan->number, chan->load_state);
  493. break;
  494. }
  495. if (buf != NULL) {
  496. /* update the chain to make sure that if we load any more
  497. * buffers when we call the callback function, things should
  498. * work properly */
  499. chan->curr = buf->next;
  500. buf->next = NULL;
  501. if (buf->magic != BUF_MAGIC) {
  502. printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
  503. chan->number, __func__, buf);
  504. return IRQ_HANDLED;
  505. }
  506. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
  507. /* free resouces */
  508. s3c2410_dma_freebuf(buf);
  509. } else {
  510. }
  511. /* only reload if the channel is still running... our buffer done
  512. * routine may have altered the state by requesting the dma channel
  513. * to stop or shutdown... */
  514. /* todo: check that when the channel is shut-down from inside this
  515. * function, we cope with unsetting reload, etc */
  516. if (chan->next != NULL && chan->state != S3C2410_DMA_IDLE) {
  517. unsigned long flags;
  518. switch (chan->load_state) {
  519. case S3C2410_DMALOAD_1RUNNING:
  520. /* don't need to do anything for this state */
  521. break;
  522. case S3C2410_DMALOAD_NONE:
  523. /* can load buffer immediately */
  524. break;
  525. case S3C2410_DMALOAD_1LOADED:
  526. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  527. /* flag error? */
  528. printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
  529. chan->number, __func__);
  530. return IRQ_HANDLED;
  531. }
  532. break;
  533. case S3C2410_DMALOAD_1LOADED_1RUNNING:
  534. goto no_load;
  535. default:
  536. printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
  537. chan->number, chan->load_state);
  538. return IRQ_HANDLED;
  539. }
  540. local_irq_save(flags);
  541. s3c2410_dma_loadbuffer(chan, chan->next);
  542. local_irq_restore(flags);
  543. } else {
  544. s3c2410_dma_lastxfer(chan);
  545. /* see if we can stop this channel.. */
  546. if (chan->load_state == S3C2410_DMALOAD_NONE) {
  547. pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
  548. chan->number, jiffies);
  549. s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
  550. S3C2410_DMAOP_STOP);
  551. }
  552. }
  553. no_load:
  554. return IRQ_HANDLED;
  555. }
  556. static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel);
  557. /* s3c2410_request_dma
  558. *
  559. * get control of an dma channel
  560. */
  561. int s3c2410_dma_request(enum dma_ch channel,
  562. struct s3c2410_dma_client *client,
  563. void *dev)
  564. {
  565. struct s3c2410_dma_chan *chan;
  566. unsigned long flags;
  567. int err;
  568. pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
  569. channel, client->name, dev);
  570. local_irq_save(flags);
  571. chan = s3c2410_dma_map_channel(channel);
  572. if (chan == NULL) {
  573. local_irq_restore(flags);
  574. return -EBUSY;
  575. }
  576. dbg_showchan(chan);
  577. chan->client = client;
  578. chan->in_use = 1;
  579. if (!chan->irq_claimed) {
  580. pr_debug("dma%d: %s : requesting irq %d\n",
  581. channel, __func__, chan->irq);
  582. chan->irq_claimed = 1;
  583. local_irq_restore(flags);
  584. err = request_irq(chan->irq, s3c2410_dma_irq, IRQF_DISABLED,
  585. client->name, (void *)chan);
  586. local_irq_save(flags);
  587. if (err) {
  588. chan->in_use = 0;
  589. chan->irq_claimed = 0;
  590. local_irq_restore(flags);
  591. printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
  592. client->name, chan->irq, chan->number);
  593. return err;
  594. }
  595. chan->irq_enabled = 1;
  596. }
  597. local_irq_restore(flags);
  598. /* need to setup */
  599. pr_debug("%s: channel initialised, %p\n", __func__, chan);
  600. return chan->number | DMACH_LOW_LEVEL;
  601. }
  602. EXPORT_SYMBOL(s3c2410_dma_request);
  603. /* s3c2410_dma_free
  604. *
  605. * release the given channel back to the system, will stop and flush
  606. * any outstanding transfers, and ensure the channel is ready for the
  607. * next claimant.
  608. *
  609. * Note, although a warning is currently printed if the freeing client
  610. * info is not the same as the registrant's client info, the free is still
  611. * allowed to go through.
  612. */
  613. int s3c2410_dma_free(enum dma_ch channel, struct s3c2410_dma_client *client)
  614. {
  615. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  616. unsigned long flags;
  617. if (chan == NULL)
  618. return -EINVAL;
  619. local_irq_save(flags);
  620. if (chan->client != client) {
  621. printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
  622. channel, chan->client, client);
  623. }
  624. /* sort out stopping and freeing the channel */
  625. if (chan->state != S3C2410_DMA_IDLE) {
  626. pr_debug("%s: need to stop dma channel %p\n",
  627. __func__, chan);
  628. /* possibly flush the channel */
  629. s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
  630. }
  631. chan->client = NULL;
  632. chan->in_use = 0;
  633. if (chan->irq_claimed)
  634. free_irq(chan->irq, (void *)chan);
  635. chan->irq_claimed = 0;
  636. if (!(channel & DMACH_LOW_LEVEL))
  637. s3c_dma_chan_map[channel] = NULL;
  638. local_irq_restore(flags);
  639. return 0;
  640. }
  641. EXPORT_SYMBOL(s3c2410_dma_free);
  642. static int s3c2410_dma_dostop(struct s3c2410_dma_chan *chan)
  643. {
  644. unsigned long flags;
  645. unsigned long tmp;
  646. pr_debug("%s:\n", __func__);
  647. dbg_showchan(chan);
  648. local_irq_save(flags);
  649. s3c2410_dma_call_op(chan, S3C2410_DMAOP_STOP);
  650. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  651. tmp |= S3C2410_DMASKTRIG_STOP;
  652. //tmp &= ~S3C2410_DMASKTRIG_ON;
  653. dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
  654. #if 0
  655. /* should also clear interrupts, according to WinCE BSP */
  656. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  657. tmp |= S3C2410_DCON_NORELOAD;
  658. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  659. #endif
  660. /* should stop do this, or should we wait for flush? */
  661. chan->state = S3C2410_DMA_IDLE;
  662. chan->load_state = S3C2410_DMALOAD_NONE;
  663. local_irq_restore(flags);
  664. return 0;
  665. }
  666. static void s3c2410_dma_waitforstop(struct s3c2410_dma_chan *chan)
  667. {
  668. unsigned long tmp;
  669. unsigned int timeout = 0x10000;
  670. while (timeout-- > 0) {
  671. tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
  672. if (!(tmp & S3C2410_DMASKTRIG_ON))
  673. return;
  674. }
  675. pr_debug("dma%d: failed to stop?\n", chan->number);
  676. }
  677. /* s3c2410_dma_flush
  678. *
  679. * stop the channel, and remove all current and pending transfers
  680. */
  681. static int s3c2410_dma_flush(struct s3c2410_dma_chan *chan)
  682. {
  683. struct s3c2410_dma_buf *buf, *next;
  684. unsigned long flags;
  685. pr_debug("%s: chan %p (%d)\n", __func__, chan, chan->number);
  686. dbg_showchan(chan);
  687. local_irq_save(flags);
  688. if (chan->state != S3C2410_DMA_IDLE) {
  689. pr_debug("%s: stopping channel...\n", __func__ );
  690. s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
  691. }
  692. buf = chan->curr;
  693. if (buf == NULL)
  694. buf = chan->next;
  695. chan->curr = chan->next = chan->end = NULL;
  696. if (buf != NULL) {
  697. for ( ; buf != NULL; buf = next) {
  698. next = buf->next;
  699. pr_debug("%s: free buffer %p, next %p\n",
  700. __func__, buf, buf->next);
  701. s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
  702. s3c2410_dma_freebuf(buf);
  703. }
  704. }
  705. dbg_showregs(chan);
  706. s3c2410_dma_waitforstop(chan);
  707. #if 0
  708. /* should also clear interrupts, according to WinCE BSP */
  709. {
  710. unsigned long tmp;
  711. tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
  712. tmp |= S3C2410_DCON_NORELOAD;
  713. dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
  714. }
  715. #endif
  716. dbg_showregs(chan);
  717. local_irq_restore(flags);
  718. return 0;
  719. }
  720. static int s3c2410_dma_started(struct s3c2410_dma_chan *chan)
  721. {
  722. unsigned long flags;
  723. local_irq_save(flags);
  724. dbg_showchan(chan);
  725. /* if we've only loaded one buffer onto the channel, then chec
  726. * to see if we have another, and if so, try and load it so when
  727. * the first buffer is finished, the new one will be loaded onto
  728. * the channel */
  729. if (chan->next != NULL) {
  730. if (chan->load_state == S3C2410_DMALOAD_1LOADED) {
  731. if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
  732. pr_debug("%s: buff not yet loaded, no more todo\n",
  733. __func__);
  734. } else {
  735. chan->load_state = S3C2410_DMALOAD_1RUNNING;
  736. s3c2410_dma_loadbuffer(chan, chan->next);
  737. }
  738. } else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
  739. s3c2410_dma_loadbuffer(chan, chan->next);
  740. }
  741. }
  742. local_irq_restore(flags);
  743. return 0;
  744. }
  745. int
  746. s3c2410_dma_ctrl(enum dma_ch channel, enum s3c2410_chan_op op)
  747. {
  748. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  749. if (chan == NULL)
  750. return -EINVAL;
  751. switch (op) {
  752. case S3C2410_DMAOP_START:
  753. return s3c2410_dma_start(chan);
  754. case S3C2410_DMAOP_STOP:
  755. return s3c2410_dma_dostop(chan);
  756. case S3C2410_DMAOP_PAUSE:
  757. case S3C2410_DMAOP_RESUME:
  758. return -ENOENT;
  759. case S3C2410_DMAOP_FLUSH:
  760. return s3c2410_dma_flush(chan);
  761. case S3C2410_DMAOP_STARTED:
  762. return s3c2410_dma_started(chan);
  763. case S3C2410_DMAOP_TIMEOUT:
  764. return 0;
  765. }
  766. return -ENOENT; /* unknown, don't bother */
  767. }
  768. EXPORT_SYMBOL(s3c2410_dma_ctrl);
  769. /* DMA configuration for each channel
  770. *
  771. * DISRCC -> source of the DMA (AHB,APB)
  772. * DISRC -> source address of the DMA
  773. * DIDSTC -> destination of the DMA (AHB,APD)
  774. * DIDST -> destination address of the DMA
  775. */
  776. /* s3c2410_dma_config
  777. *
  778. * xfersize: size of unit in bytes (1,2,4)
  779. */
  780. int s3c2410_dma_config(enum dma_ch channel,
  781. int xferunit)
  782. {
  783. struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
  784. unsigned int dcon;
  785. pr_debug("%s: chan=%d, xfer_unit=%d\n", __func__, channel, xferunit);
  786. if (chan == NULL)
  787. return -EINVAL;
  788. dcon = chan->dcon & dma_sel.dcon_mask;
  789. pr_debug("%s: dcon is %08x\n", __func__, dcon);
  790. switch (chan->req_ch) {
  791. case DMACH_I2S_IN:
  792. case DMACH_I2S_OUT:
  793. case DMACH_PCM_IN:
  794. case DMACH_PCM_OUT:
  795. case DMACH_MIC_IN:
  796. default:
  797. dcon |= S3C2410_DCON_HANDSHAKE;
  798. dcon |= S3C2410_DCON_SYNC_PCLK;
  799. break;
  800. case DMACH_SDI:
  801. /* note, ensure if need HANDSHAKE or not */
  802. dcon |= S3C2410_DCON_SYNC_PCLK;
  803. break;
  804. case DMACH_XD0:
  805. case DMACH_XD1:
  806. dcon |= S3C2410_DCON_HANDSHAKE;
  807. dcon |= S3C2410_DCON_SYNC_HCLK;
  808. break;
  809. }
  810. switch (xferunit) {
  811. case 1:
  812. dcon |= S3C2410_DCON_BYTE;
  813. break;
  814. case 2:
  815. dcon |= S3C2410_DCON_HALFWORD;