ep93xx_dma.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /*
  2. * Driver for the Cirrus Logic EP93xx DMA Controller
  3. *
  4. * Copyright (C) 2011 Mika Westerberg
  5. *
  6. * DMA M2P implementation is based on the original
  7. * arch/arm/mach-ep93xx/dma-m2p.c which has following copyrights:
  8. *
  9. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  10. * Copyright (C) 2006 Applied Data Systems
  11. * Copyright (C) 2009 Ryan Mallon <rmallon@gmail.com>
  12. *
  13. * This driver is based on dw_dmac and amba-pl08x drivers.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <mach/dma.h>
  28. /* M2P registers */
  29. #define M2P_CONTROL 0x0000
  30. #define M2P_CONTROL_STALLINT BIT(0)
  31. #define M2P_CONTROL_NFBINT BIT(1)
  32. #define M2P_CONTROL_CH_ERROR_INT BIT(3)
  33. #define M2P_CONTROL_ENABLE BIT(4)
  34. #define M2P_CONTROL_ICE BIT(6)
  35. #define M2P_INTERRUPT 0x0004
  36. #define M2P_INTERRUPT_STALL BIT(0)
  37. #define M2P_INTERRUPT_NFB BIT(1)
  38. #define M2P_INTERRUPT_ERROR BIT(3)
  39. #define M2P_PPALLOC 0x0008
  40. #define M2P_STATUS 0x000c
  41. #define M2P_MAXCNT0 0x0020
  42. #define M2P_BASE0 0x0024
  43. #define M2P_MAXCNT1 0x0030
  44. #define M2P_BASE1 0x0034
  45. #define M2P_STATE_IDLE 0
  46. #define M2P_STATE_STALL 1
  47. #define M2P_STATE_ON 2
  48. #define M2P_STATE_NEXT 3
  49. /* M2M registers */
  50. #define M2M_CONTROL 0x0000
  51. #define M2M_CONTROL_DONEINT BIT(2)
  52. #define M2M_CONTROL_ENABLE BIT(3)
  53. #define M2M_CONTROL_START BIT(4)
  54. #define M2M_CONTROL_DAH BIT(11)
  55. #define M2M_CONTROL_SAH BIT(12)
  56. #define M2M_CONTROL_PW_SHIFT 9
  57. #define M2M_CONTROL_PW_8 (0 << M2M_CONTROL_PW_SHIFT)
  58. #define M2M_CONTROL_PW_16 (1 << M2M_CONTROL_PW_SHIFT)
  59. #define M2M_CONTROL_PW_32 (2 << M2M_CONTROL_PW_SHIFT)
  60. #define M2M_CONTROL_PW_MASK (3 << M2M_CONTROL_PW_SHIFT)
  61. #define M2M_CONTROL_TM_SHIFT 13
  62. #define M2M_CONTROL_TM_TX (1 << M2M_CONTROL_TM_SHIFT)
  63. #define M2M_CONTROL_TM_RX (2 << M2M_CONTROL_TM_SHIFT)
  64. #define M2M_CONTROL_RSS_SHIFT 22
  65. #define M2M_CONTROL_RSS_SSPRX (1 << M2M_CONTROL_RSS_SHIFT)
  66. #define M2M_CONTROL_RSS_SSPTX (2 << M2M_CONTROL_RSS_SHIFT)
  67. #define M2M_CONTROL_RSS_IDE (3 << M2M_CONTROL_RSS_SHIFT)
  68. #define M2M_CONTROL_NO_HDSK BIT(24)
  69. #define M2M_CONTROL_PWSC_SHIFT 25
  70. #define M2M_INTERRUPT 0x0004
  71. #define M2M_INTERRUPT_DONEINT BIT(1)
  72. #define M2M_BCR0 0x0010
  73. #define M2M_BCR1 0x0014
  74. #define M2M_SAR_BASE0 0x0018
  75. #define M2M_SAR_BASE1 0x001c
  76. #define M2M_DAR_BASE0 0x002c
  77. #define M2M_DAR_BASE1 0x0030
  78. #define DMA_MAX_CHAN_BYTES 0xffff
  79. #define DMA_MAX_CHAN_DESCRIPTORS 32
  80. struct ep93xx_dma_engine;
  81. /**
  82. * struct ep93xx_dma_desc - EP93xx specific transaction descriptor
  83. * @src_addr: source address of the transaction
  84. * @dst_addr: destination address of the transaction
  85. * @size: size of the transaction (in bytes)
  86. * @complete: this descriptor is completed
  87. * @txd: dmaengine API descriptor
  88. * @tx_list: list of linked descriptors
  89. * @node: link used for putting this into a channel queue
  90. */
  91. struct ep93xx_dma_desc {
  92. u32 src_addr;
  93. u32 dst_addr;
  94. size_t size;
  95. bool complete;
  96. struct dma_async_tx_descriptor txd;
  97. struct list_head tx_list;
  98. struct list_head node;
  99. };
  100. /**
  101. * struct ep93xx_dma_chan - an EP93xx DMA M2P/M2M channel
  102. * @chan: dmaengine API channel
  103. * @edma: pointer to to the engine device
  104. * @regs: memory mapped registers
  105. * @irq: interrupt number of the channel
  106. * @clk: clock used by this channel
  107. * @tasklet: channel specific tasklet used for callbacks
  108. * @lock: lock protecting the fields following
  109. * @flags: flags for the channel
  110. * @buffer: which buffer to use next (0/1)
  111. * @active: flattened chain of descriptors currently being processed
  112. * @queue: pending descriptors which are handled next
  113. * @free_list: list of free descriptors which can be used
  114. * @runtime_addr: physical address currently used as dest/src (M2M only). This
  115. * is set via %DMA_SLAVE_CONFIG before slave operation is
  116. * prepared
  117. * @runtime_ctrl: M2M runtime values for the control register.
  118. *
  119. * As EP93xx DMA controller doesn't support real chained DMA descriptors we
  120. * will have slightly different scheme here: @active points to a head of
  121. * flattened DMA descriptor chain.
  122. *
  123. * @queue holds pending transactions. These are linked through the first
  124. * descriptor in the chain. When a descriptor is moved to the @active queue,
  125. * the first and chained descriptors are flattened into a single list.
  126. *
  127. * @chan.private holds pointer to &struct ep93xx_dma_data which contains
  128. * necessary channel configuration information. For memcpy channels this must
  129. * be %NULL.
  130. */
  131. struct ep93xx_dma_chan {
  132. struct dma_chan chan;
  133. const struct ep93xx_dma_engine *edma;
  134. void __iomem *regs;
  135. int irq;
  136. struct clk *clk;
  137. struct tasklet_struct tasklet;
  138. /* protects the fields following */
  139. spinlock_t lock;
  140. unsigned long flags;
  141. /* Channel is configured for cyclic transfers */
  142. #define EP93XX_DMA_IS_CYCLIC 0
  143. int buffer;
  144. struct list_head active;
  145. struct list_head queue;
  146. struct list_head free_list;
  147. u32 runtime_addr;
  148. u32 runtime_ctrl;
  149. };
  150. /**
  151. * struct ep93xx_dma_engine - the EP93xx DMA engine instance
  152. * @dma_dev: holds the dmaengine device
  153. * @m2m: is this an M2M or M2P device
  154. * @hw_setup: method which sets the channel up for operation
  155. * @hw_shutdown: shuts the channel down and flushes whatever is left
  156. * @hw_submit: pushes active descriptor(s) to the hardware
  157. * @hw_interrupt: handle the interrupt
  158. * @num_channels: number of channels for this instance
  159. * @channels: array of channels
  160. *
  161. * There is one instance of this struct for the M2P channels and one for the
  162. * M2M channels. hw_xxx() methods are used to perform operations which are
  163. * different on M2M and M2P channels. These methods are called with channel
  164. * lock held and interrupts disabled so they cannot sleep.
  165. */
  166. struct ep93xx_dma_engine {
  167. struct dma_device dma_dev;
  168. bool m2m;
  169. int (*hw_setup)(struct ep93xx_dma_chan *);
  170. void (*hw_shutdown)(struct ep93xx_dma_chan *);
  171. void (*hw_submit)(struct ep93xx_dma_chan *);
  172. int (*hw_interrupt)(struct ep93xx_dma_chan *);
  173. #define INTERRUPT_UNKNOWN 0
  174. #define INTERRUPT_DONE 1
  175. #define INTERRUPT_NEXT_BUFFER 2
  176. size_t num_channels;
  177. struct ep93xx_dma_chan channels[];
  178. };
  179. static inline struct device *chan2dev(struct ep93xx_dma_chan *edmac)
  180. {
  181. return &edmac->chan.dev->device;
  182. }
  183. static struct ep93xx_dma_chan *to_ep93xx_dma_chan(struct dma_chan *chan)
  184. {
  185. return container_of(chan, struct ep93xx_dma_chan, chan);
  186. }
  187. /**
  188. * ep93xx_dma_set_active - set new active descriptor chain
  189. * @edmac: channel
  190. * @desc: head of the new active descriptor chain
  191. *
  192. * Sets @desc to be the head of the new active descriptor chain. This is the
  193. * chain which is processed next. The active list must be empty before calling
  194. * this function.
  195. *
  196. * Called with @edmac->lock held and interrupts disabled.
  197. */
  198. static void ep93xx_dma_set_active(struct ep93xx_dma_chan *edmac,
  199. struct ep93xx_dma_desc *desc)
  200. {
  201. BUG_ON(!list_empty(&edmac->active));
  202. list_add_tail(&desc->node, &edmac->active);
  203. /* Flatten the @desc->tx_list chain into @edmac->active list */
  204. while (!list_empty(&desc->tx_list)) {
  205. struct ep93xx_dma_desc *d = list_first_entry(&desc->tx_list,
  206. struct ep93xx_dma_desc, node);
  207. /*
  208. * We copy the callback parameters from the first descriptor
  209. * to all the chained descriptors. This way we can call the
  210. * callback without having to find out the first descriptor in
  211. * the chain. Useful for cyclic transfers.
  212. */
  213. d->txd.callback = desc->txd.callback;
  214. d->txd.callback_param = desc->txd.callback_param;
  215. list_move_tail(&d->node, &edmac->active);
  216. }
  217. }
  218. /* Called with @edmac->lock held and interrupts disabled */
  219. static struct ep93xx_dma_desc *
  220. ep93xx_dma_get_active(struct ep93xx_dma_chan *edmac)
  221. {
  222. if (list_empty(&edmac->active))
  223. return NULL;
  224. return list_first_entry(&edmac->active, struct ep93xx_dma_desc, node);
  225. }
  226. /**
  227. * ep93xx_dma_advance_active - advances to the next active descriptor
  228. * @edmac: channel
  229. *
  230. * Function advances active descriptor to the next in the @edmac->active and
  231. * returns %true if we still have descriptors in the chain to process.
  232. * Otherwise returns %false.
  233. *
  234. * When the channel is in cyclic mode always returns %true.
  235. *
  236. * Called with @edmac->lock held and interrupts disabled.
  237. */
  238. static bool ep93xx_dma_advance_active(struct ep93xx_dma_chan *edmac)
  239. {
  240. struct ep93xx_dma_desc *desc;
  241. list_rotate_left(&edmac->active);
  242. if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
  243. return true;
  244. desc = ep93xx_dma_get_active(edmac);
  245. if (!desc)
  246. return false;
  247. /*
  248. * If txd.cookie is set it means that we are back in the first
  249. * descriptor in the chain and hence done with it.
  250. */
  251. return !desc->txd.cookie;
  252. }
  253. /*
  254. * M2P DMA implementation
  255. */
  256. static void m2p_set_control(struct ep93xx_dma_chan *edmac, u32 control)
  257. {
  258. writel(control, edmac->regs + M2P_CONTROL);
  259. /*
  260. * EP93xx User's Guide states that we must perform a dummy read after
  261. * write to the control register.
  262. */
  263. readl(edmac->regs + M2P_CONTROL);
  264. }
  265. static int m2p_hw_setup(struct ep93xx_dma_chan *edmac)
  266. {
  267. struct ep93xx_dma_data *data = edmac->chan.private;
  268. u32 control;
  269. writel(data->port & 0xf, edmac->regs + M2P_PPALLOC);
  270. control = M2P_CONTROL_CH_ERROR_INT | M2P_CONTROL_ICE
  271. | M2P_CONTROL_ENABLE;
  272. m2p_set_control(edmac, control);
  273. return 0;
  274. }
  275. static inline u32 m2p_channel_state(struct ep93xx_dma_chan *edmac)
  276. {
  277. return (readl(edmac->regs + M2P_STATUS) >> 4) & 0x3;
  278. }
  279. static void m2p_hw_shutdown(struct ep93xx_dma_chan *edmac)
  280. {
  281. u32 control;
  282. control = readl(edmac->regs + M2P_CONTROL);
  283. control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
  284. m2p_set_control(edmac, control);
  285. while (m2p_channel_state(edmac) >= M2P_STATE_ON)
  286. cpu_relax();
  287. m2p_set_control(edmac, 0);
  288. while (m2p_channel_state(edmac) == M2P_STATE_STALL)
  289. cpu_relax();
  290. }
  291. static void m2p_fill_desc(struct ep93xx_dma_chan *edmac)
  292. {
  293. struct ep93xx_dma_desc *desc;
  294. u32 bus_addr;
  295. desc = ep93xx_dma_get_active(edmac);
  296. if (!desc) {
  297. dev_warn(chan2dev(edmac), "M2P: empty descriptor list\n");
  298. return;
  299. }
  300. if (ep93xx_dma_chan_direction(&edmac->chan) == DMA_MEM_TO_DEV)
  301. bus_addr = desc->src_addr;
  302. else
  303. bus_addr = desc->dst_addr;
  304. if (edmac->buffer == 0) {
  305. writel(desc->size, edmac->regs + M2P_MAXCNT0);
  306. writel(bus_addr, edmac->regs + M2P_BASE0);
  307. } else {
  308. writel(desc->size, edmac->regs + M2P_MAXCNT1);
  309. writel(bus_addr, edmac->regs + M2P_BASE1);
  310. }
  311. edmac->buffer ^= 1;
  312. }
  313. static void m2p_hw_submit(struct ep93xx_dma_chan *edmac)
  314. {
  315. u32 control = readl(edmac->regs + M2P_CONTROL);
  316. m2p_fill_desc(edmac);
  317. control |= M2P_CONTROL_STALLINT;
  318. if (ep93xx_dma_advance_active(edmac)) {
  319. m2p_fill_desc(edmac);
  320. control |= M2P_CONTROL_NFBINT;
  321. }
  322. m2p_set_control(edmac, control);
  323. }
  324. static int m2p_hw_interrupt(struct ep93xx_dma_chan *edmac)
  325. {
  326. u32 irq_status = readl(edmac->regs + M2P_INTERRUPT);
  327. u32 control;
  328. if (irq_status & M2P_INTERRUPT_ERROR) {
  329. struct ep93xx_dma_desc *desc = ep93xx_dma_get_active(edmac);
  330. /* Clear the error interrupt */
  331. writel(1, edmac->regs + M2P_INTERRUPT);
  332. /*
  333. * It seems that there is no easy way of reporting errors back
  334. * to client so we just report the error here and continue as
  335. * usual.
  336. *
  337. * Revisit this when there is a mechanism to report back the
  338. * errors.
  339. */
  340. dev_err(chan2dev(edmac),
  341. "DMA transfer failed! Details:\n"
  342. "\tcookie : %d\n"
  343. "\tsrc_addr : 0x%08x\n"
  344. "\tdst_addr : 0x%08x\n"
  345. "\tsize : %zu\n",
  346. desc->txd.cookie, desc->src_addr, desc->dst_addr,
  347. desc->size);
  348. }
  349. switch (irq_status & (M2P_INTERRUPT_STALL | M2P_INTERRUPT_NFB)) {
  350. case M2P_INTERRUPT_STALL:
  351. /* Disable interrupts */
  352. control = readl(edmac->regs + M2P_CONTROL);
  353. control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
  354. m2p_set_control(edmac, control);
  355. return INTERRUPT_DONE;
  356. case M2P_INTERRUPT_NFB:
  357. if (ep93xx_dma_advance_active(edmac))
  358. m2p_fill_desc(edmac);
  359. return INTERRUPT_NEXT_BUFFER;
  360. }
  361. return INTERRUPT_UNKNOWN;
  362. }
  363. /*
  364. * M2M DMA implementation
  365. *
  366. * For the M2M transfers we don't use NFB at all. This is because it simply
  367. * doesn't work well with memcpy transfers. When you submit both buffers it is
  368. * extremely unlikely that you get an NFB interrupt, but it instead reports
  369. * DONE interrupt and both buffers are already transferred which means that we
  370. * weren't able to update the next buffer.
  371. *
  372. * So for now we "simulate" NFB by just submitting buffer after buffer
  373. * without double buffering.
  374. */
  375. static int m2m_hw_setup(struct ep93xx_dma_chan *edmac)
  376. {
  377. const struct ep93xx_dma_data *data = edmac->chan.private;
  378. u32 control = 0;
  379. if (!data) {
  380. /* This is memcpy channel, nothing to configure */
  381. writel(control, edmac->regs + M2M_CONTROL);
  382. return 0;
  383. }
  384. switch (data->port) {
  385. case EP93XX_DMA_SSP:
  386. /*
  387. * This was found via experimenting - anything less than 5
  388. * causes the channel to perform only a partial transfer which
  389. * leads to problems since we don't get DONE interrupt then.
  390. */
  391. control = (5 << M2M_CONTROL_PWSC_SHIFT);
  392. control |= M2M_CONTROL_NO_HDSK;
  393. if (data->direction == DMA_MEM_TO_DEV) {
  394. control |= M2M_CONTROL_DAH;
  395. control |= M2M_CONTROL_TM_TX;
  396. control |= M2M_CONTROL_RSS_SSPTX;
  397. } else {
  398. control |= M2M_CONTROL_SAH;
  399. control |= M2M_CONTROL_TM_RX;
  400. control |= M2M_CONTROL_RSS_SSPRX;
  401. }
  402. break;
  403. case EP93XX_DMA_IDE:
  404. /*
  405. * This IDE part is totally untested. Values below are taken
  406. * from the EP93xx Users's Guide and might not be correct.
  407. */
  408. if (data->direction == DMA_MEM_TO_DEV) {
  409. /* Worst case from the UG */
  410. control = (3 << M2M_CONTROL_PWSC_SHIFT);
  411. control |= M2M_CONTROL_DAH;
  412. control |= M2M_CONTROL_TM_TX;
  413. } else {
  414. control = (2 << M2M_CONTROL_PWSC_SHIFT);
  415. control |= M2M_CONTROL_SAH;
  416. control |= M2M_CONTROL_TM_RX;
  417. }
  418. control |= M2M_CONTROL_NO_HDSK;
  419. control |= M2M_CONTROL_RSS_IDE;
  420. control |= M2M_CONTROL_PW_16;
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. writel(control, edmac->regs + M2M_CONTROL);
  426. return 0;
  427. }
  428. static void m2m_hw_shutdown(struct ep93xx_dma_chan *edmac)
  429. {
  430. /* Just disable the channel */
  431. writel(0, edmac->regs + M2M_CONTROL);
  432. }
  433. static void m2m_fill_desc(struct ep93xx_dma_chan *edmac)
  434. {
  435. struct ep93xx_dma_desc *desc;
  436. desc = ep93xx_dma_get_active(edmac);
  437. if (!desc) {
  438. dev_warn(chan2dev(edmac), "M2M: empty descriptor list\n");
  439. return;
  440. }
  441. if (edmac->buffer == 0) {
  442. writel(desc->src_addr, edmac->regs + M2M_SAR_BASE0);
  443. writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE0);
  444. writel(desc->size, edmac->regs + M2M_BCR0);
  445. } else {
  446. writel(desc->src_addr, edmac->regs + M2M_SAR_BASE1);
  447. writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE1);
  448. writel(desc->size, edmac->regs + M2M_BCR1);
  449. }
  450. edmac->buffer ^= 1;
  451. }
  452. static void m2m_hw_submit(struct ep93xx_dma_chan *edmac)
  453. {
  454. struct ep93xx_dma_data *data = edmac->chan.private;
  455. u32 control = readl(edmac->regs + M2M_CONTROL);
  456. /*
  457. * Since we allow clients to configure PW (peripheral width) we always
  458. * clear PW bits here and then set them according what is given in
  459. * the runtime configuration.
  460. */
  461. control &= ~M2M_CONTROL_PW_MASK;
  462. control |= edmac->runtime_ctrl;
  463. m2m_fill_desc(edmac);
  464. control |= M2M_CONTROL_DONEINT;
  465. /*
  466. * Now we can finally enable the channel. For M2M channel this must be
  467. * done _after_ the BCRx registers are programmed.
  468. */
  469. control |= M2M_CONTROL_ENABLE;
  470. writel(control, edmac->regs + M2M_CONTROL);
  471. if (!data) {
  472. /*
  473. * For memcpy channels the software trigger must be asserted
  474. * in order to start the memcpy operation.
  475. */
  476. control |= M2M_CONTROL_START;
  477. writel(control, edmac->regs + M2M_CONTROL);
  478. }
  479. }
  480. static int m2m_hw_interrupt(struct ep93xx_dma_chan *edmac)
  481. {
  482. u32 control;
  483. if (!(readl(edmac->regs + M2M_INTERRUPT) & M2M_INTERRUPT_DONEINT))
  484. return INTERRUPT_UNKNOWN;
  485. /* Clear the DONE bit */
  486. writel(0, edmac->regs + M2M_INTERRUPT);
  487. /* Disable interrupts and the channel */
  488. control = readl(edmac->regs + M2M_CONTROL);
  489. control &= ~(M2M_CONTROL_DONEINT | M2M_CONTROL_ENABLE);
  490. writel(control, edmac->regs + M2M_CONTROL);
  491. /*
  492. * Since we only get DONE interrupt we have to find out ourselves
  493. * whether there still is something to process. So we try to advance
  494. * the chain an see whether it succeeds.
  495. */
  496. if (ep93xx_dma_advance_active(edmac)) {
  497. edmac->edma->hw_submit(edmac);
  498. return INTERRUPT_NEXT_BUFFER;
  499. }
  500. return INTERRUPT_DONE;
  501. }
  502. /*
  503. * DMA engine API implementation
  504. */
  505. static struct ep93xx_dma_desc *
  506. ep93xx_dma_desc_get(struct ep93xx_dma_chan *edmac)
  507. {
  508. struct ep93xx_dma_desc *desc, *_desc;
  509. struct ep93xx_dma_desc *ret = NULL;
  510. unsigned long flags;
  511. spin_lock_irqsave(&edmac->lock, flags);
  512. list_for_each_entry_safe(desc, _desc, &edmac->free_list, node) {
  513. if (async_tx_test_ack(&desc->txd)) {
  514. list_del_init(&desc->node);
  515. /* Re-initialize the descriptor */
  516. desc->src_addr = 0;
  517. desc->dst_addr = 0;
  518. desc->size = 0;
  519. desc->complete = false;
  520. desc->txd.cookie = 0;
  521. desc->txd.callback = NULL;
  522. desc->txd.callback_param = NULL;
  523. ret = desc;
  524. break;
  525. }
  526. }
  527. spin_unlock_irqrestore(&edmac->lock, flags);
  528. return ret;
  529. }
  530. static void ep93xx_dma_desc_put(struct ep93xx_dma_chan *edmac,
  531. struct ep93xx_dma_desc *desc)
  532. {
  533. if (desc) {
  534. unsigned long flags;
  535. spin_lock_irqsave(&edmac->lock, flags);
  536. list_splice_init(&desc->tx_list, &edmac->free_list);
  537. list_add(&desc->node, &edmac->free_list);
  538. spin_unlock_irqrestore(&edmac->lock, flags);
  539. }
  540. }
  541. /**
  542. * ep93xx_dma_advance_work - start processing the next pending transaction
  543. * @edmac: channel
  544. *
  545. * If we have pending transactions queued and we are currently idling, this
  546. * function takes the next queued transaction from the @edmac->queue and
  547. * pushes it to the hardware for execution.
  548. */
  549. static void ep93xx_dma_advance_work(struct ep93xx_dma_chan *edmac)
  550. {
  551. struct ep93xx_dma_desc *new;
  552. unsigned long flags;
  553. spin_lock_irqsave(&edmac->lock, flags);
  554. if (!list_empty(&edmac->active) || list_empty(&edmac->queue)) {
  555. spin_unlock_irqrestore(&edmac->lock, flags);
  556. return;
  557. }
  558. /* Take the next descriptor from the pending queue */
  559. new = list_first_entry(&edmac->queue, struct ep93xx_dma_desc, node);
  560. list_del_init(&new->node);
  561. ep93xx_dma_set_active(edmac, new);
  562. /* Push it to the hardware */
  563. edmac->edma->hw_submit(edmac);
  564. spin_unlock_irqrestore(&edmac->lock, flags);
  565. }
  566. static void ep93xx_dma_unmap_buffers(struct ep93xx_dma_desc *desc)
  567. {
  568. struct device *dev = desc->txd.chan->device->dev;
  569. if (!(desc->txd.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  570. if (desc->txd.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  571. dma_unmap_single(dev, desc->src_addr, desc->size,
  572. DMA_TO_DEVICE);
  573. else
  574. dma_unmap_page(dev, desc->src_addr, desc->size,
  575. DMA_TO_DEVICE);
  576. }
  577. if (!(desc->txd.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  578. if (desc->txd.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  579. dma_unmap_single(dev, desc->dst_addr, desc->size,
  580. DMA_FROM_DEVICE);
  581. else
  582. dma_unmap_page(dev, desc->dst_addr, desc->size,
  583. DMA_FROM_DEVICE);
  584. }
  585. }
  586. static void ep93xx_dma_tasklet(unsigned long data)
  587. {
  588. struct ep93xx_dma_chan *edmac = (struct ep93xx_dma_chan *)data;
  589. struct ep93xx_dma_desc *desc, *d;
  590. dma_async_tx_callback callback = NULL;
  591. void *callback_param = NULL;
  592. LIST_HEAD(list);
  593. spin_lock_irq(&edmac->lock);
  594. /*
  595. * If dma_terminate_all() was called before we get to run, the active
  596. * list has become empty. If that happens we aren't supposed to do
  597. * anything more than call ep93xx_dma_advance_work().
  598. */
  599. desc = ep93xx_dma_get_active(edmac);
  600. if (desc) {
  601. if (desc->complete) {
  602. edmac->chan.completed_cookie = desc->txd.cookie;
  603. list_splice_init(&edmac->active, &list);
  604. }
  605. callback = desc->txd.callback;
  606. callback_param = desc->txd.callback_param;
  607. }
  608. spin_unlock_irq(&edmac->lock);
  609. /* Pick up the next descriptor from the queue */
  610. ep93xx_dma_advance_work(edmac);
  611. /* Now we can release all the chained descriptors */
  612. list_for_each_entry_safe(desc, d, &list, node) {
  613. /*
  614. * For the memcpy channels the API requires us to unmap the
  615. * buffers unless requested otherwise.
  616. */
  617. if (!edmac->chan.private)
  618. ep93xx_dma_unmap_buffers(desc);
  619. ep93xx_dma_desc_put(edmac, desc);
  620. }
  621. if (callback)
  622. callback(callback_param);
  623. }
  624. static irqreturn_t ep93xx_dma_interrupt(int irq, void *dev_id)
  625. {
  626. struct ep93xx_dma_chan *edmac = dev_id;
  627. struct ep93xx_dma_desc *desc;
  628. irqreturn_t ret = IRQ_HANDLED;
  629. spin_lock(&edmac->lock);
  630. desc = ep93xx_dma_get_active(edmac);
  631. if (!desc) {
  632. dev_warn(chan2dev(edmac),
  633. "got interrupt while active list is empty\n");
  634. spin_unlock(&edmac->lock);
  635. return IRQ_NONE;
  636. }
  637. switch (edmac->edma->hw_interrupt(edmac)) {
  638. case INTERRUPT_DONE:
  639. desc->complete = true;
  640. tasklet_schedule(&edmac->tasklet);
  641. break;
  642. case INTERRUPT_NEXT_BUFFER:
  643. if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
  644. tasklet_schedule(&edmac->tasklet);
  645. break;
  646. default:
  647. dev_warn(chan2dev(edmac), "unknown interrupt!\n");
  648. ret = IRQ_NONE;
  649. break;
  650. }
  651. spin_unlock(&edmac->lock);
  652. return ret;
  653. }
  654. /**
  655. * ep93xx_dma_tx_submit - set the prepared descriptor(s) to be executed
  656. * @tx: descriptor to be executed
  657. *
  658. * Function will execute given descriptor on the hardware or if the hardware
  659. * is busy, queue the descriptor to be executed later on. Returns cookie which
  660. * can be used to poll the status of the descriptor.
  661. */
  662. static dma_cookie_t ep93xx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  663. {
  664. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(tx->chan);
  665. struct ep93xx_dma_desc *desc;
  666. dma_cookie_t cookie;
  667. unsigned long flags;
  668. spin_lock_irqsave(&edmac->lock, flags);
  669. cookie = edmac->chan.cookie;
  670. if (++cookie < 0)
  671. cookie = 1;
  672. desc = container_of(tx, struct ep93xx_dma_desc, txd);
  673. edmac->chan.cookie = cookie;
  674. desc->txd.cookie = cookie;
  675. /*
  676. * If nothing is currently prosessed, we push this descriptor
  677. * directly to the hardware. Otherwise we put the descriptor
  678. * to the pending queue.
  679. */
  680. if (list_empty(&edmac->active)) {
  681. ep93xx_dma_set_active(edmac, desc);
  682. edmac->edma->hw_submit(edmac);
  683. } else {
  684. list_add_tail(&desc->node, &edmac->queue);
  685. }
  686. spin_unlock_irqrestore(&edmac->lock, flags);
  687. return cookie;
  688. }
  689. /**
  690. * ep93xx_dma_alloc_chan_resources - allocate resources for the channel
  691. * @chan: channel to allocate resources
  692. *
  693. * Function allocates necessary resources for the given DMA channel and
  694. * returns number of allocated descriptors for the channel. Negative errno
  695. * is returned in case of failure.
  696. */
  697. static int ep93xx_dma_alloc_chan_resources(struct dma_chan *chan)
  698. {
  699. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  700. struct ep93xx_dma_data *data = chan->private;
  701. const char *name = dma_chan_name(chan);
  702. int ret, i;
  703. /* Sanity check the channel parameters */
  704. if (!edmac->edma->m2m) {
  705. if (!data)
  706. return -EINVAL;
  707. if (data->port < EP93XX_DMA_I2S1 ||
  708. data->port > EP93XX_DMA_IRDA)
  709. return -EINVAL;
  710. if (data->direction != ep93xx_dma_chan_direction(chan))
  711. return -EINVAL;
  712. } else {
  713. if (data) {
  714. switch (data->port) {
  715. case EP93XX_DMA_SSP:
  716. case EP93XX_DMA_IDE:
  717. if (data->direction != DMA_MEM_TO_DEV &&
  718. data->direction != DMA_DEV_TO_MEM)
  719. return -EINVAL;
  720. break;
  721. default:
  722. return -EINVAL;
  723. }
  724. }
  725. }
  726. if (data && data->name)
  727. name = data->name;
  728. ret = clk_enable(edmac->clk);
  729. if (ret)
  730. return ret;
  731. ret = request_irq(edmac->irq, ep93xx_dma_interrupt, 0, name, edmac);
  732. if (ret)
  733. goto fail_clk_disable;
  734. spin_lock_irq(&edmac->lock);
  735. edmac->chan.completed_cookie = 1;
  736. edmac->chan.cookie = 1;
  737. ret = edmac->edma->hw_setup(edmac);
  738. spin_unlock_irq(&edmac->lock);
  739. if (ret)
  740. goto fail_free_irq;
  741. for (i = 0; i < DMA_MAX_CHAN_DESCRIPTORS; i++) {
  742. struct ep93xx_dma_desc *desc;
  743. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  744. if (!desc) {
  745. dev_warn(chan2dev(edmac), "not enough descriptors\n");
  746. break;
  747. }
  748. INIT_LIST_HEAD(&desc->tx_list);
  749. dma_async_tx_descriptor_init(&desc->txd, chan);
  750. desc->txd.flags = DMA_CTRL_ACK;
  751. desc->txd.tx_submit = ep93xx_dma_tx_submit;
  752. ep93xx_dma_desc_put(edmac, desc);
  753. }
  754. return i;
  755. fail_free_irq:
  756. free_irq(edmac->irq, edmac);
  757. fail_clk_disable:
  758. clk_disable(edmac->clk);
  759. return ret;
  760. }
  761. /**
  762. * ep93xx_dma_free_chan_resources - release resources for the channel
  763. * @chan: channel
  764. *
  765. * Function releases all the resources allocated for the given channel.
  766. * The channel must be idle when this is called.
  767. */
  768. static void ep93xx_dma_free_chan_resources(struct dma_chan *chan)
  769. {
  770. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  771. struct ep93xx_dma_desc *desc, *d;
  772. unsigned long flags;
  773. LIST_HEAD(list);
  774. BUG_ON(!list_empty(&edmac->active));
  775. BUG_ON(!list_empty(&edmac->queue));
  776. spin_lock_irqsave(&edmac->lock, flags);
  777. edmac->edma->hw_shutdown(edmac);
  778. edmac->runtime_addr = 0;
  779. edmac->runtime_ctrl = 0;
  780. edmac->buffer = 0;
  781. list_splice_init(&edmac->free_list, &list);
  782. spin_unlock_irqrestore(&edmac->lock, flags);
  783. list_for_each_entry_safe(desc, d, &list, node)
  784. kfree(desc);
  785. clk_disable(edmac->clk);
  786. free_irq(edmac->irq, edmac);
  787. }
  788. /**
  789. * ep93xx_dma_prep_dma_memcpy - prepare a memcpy DMA operation
  790. * @chan: channel
  791. * @dest: destination bus address
  792. * @src: source bus address
  793. * @len: size of the transaction
  794. * @flags: flags for the descriptor
  795. *
  796. * Returns a valid DMA descriptor or %NULL in case of failure.
  797. */
  798. static struct dma_async_tx_descriptor *
  799. ep93xx_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
  800. dma_addr_t src, size_t len, unsigned long flags)
  801. {
  802. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  803. struct ep93xx_dma_desc *desc, *first;
  804. size_t bytes, offset;
  805. first = NULL;
  806. for (offset = 0; offset < len; offset += bytes) {
  807. desc = ep93xx_dma_desc_get(edmac);
  808. if (!desc) {
  809. dev_warn(chan2dev(edmac), "couln't get descriptor\n");
  810. goto fail;
  811. }
  812. bytes = min_t(size_t, len - offset, DMA_MAX_CHAN_BYTES);
  813. desc->src_addr = src + offset;
  814. desc->dst_addr = dest + offset;
  815. desc->size = bytes;
  816. if (!first)
  817. first = desc;
  818. else
  819. list_add_tail(&desc->node, &first->tx_list);
  820. }
  821. first->txd.cookie = -EBUSY;
  822. first->txd.flags = flags;
  823. return &first->txd;
  824. fail:
  825. ep93xx_dma_desc_put(edmac, first);
  826. return NULL;
  827. }
  828. /**
  829. * ep93xx_dma_prep_slave_sg - prepare a slave DMA operation
  830. * @chan: channel
  831. * @sgl: list of buffers to transfer
  832. * @sg_len: number of entries in @sgl
  833. * @dir: direction of tha DMA transfer
  834. * @flags: flags for the descriptor
  835. *
  836. * Returns a valid DMA descriptor or %NULL in case of failure.
  837. */
  838. static struct dma_async_tx_descriptor *
  839. ep93xx_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  840. unsigned int sg_len, enum dma_transfer_direction dir,
  841. unsigned long flags)
  842. {
  843. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  844. struct ep93xx_dma_desc *desc, *first;
  845. struct scatterlist *sg;
  846. int i;
  847. if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
  848. dev_warn(chan2dev(edmac),
  849. "channel was configured with different direction\n");
  850. return NULL;
  851. }
  852. if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
  853. dev_warn(chan2dev(edmac),
  854. "channel is already used for cyclic transfers\n");
  855. return NULL;
  856. }
  857. first = NULL;
  858. for_each_sg(sgl, sg, sg_len, i) {
  859. size_t sg_len = sg_dma_len(sg);
  860. if (sg_len > DMA_MAX_CHAN_BYTES) {
  861. dev_warn(chan2dev(edmac), "too big transfer size %d\n",
  862. sg_len);
  863. goto fail;
  864. }
  865. desc = ep93xx_dma_desc_get(edmac);
  866. if (!desc) {
  867. dev_warn(chan2dev(edmac), "couln't get descriptor\n");
  868. goto fail;
  869. }
  870. if (dir == DMA_MEM_TO_DEV) {
  871. desc->src_addr = sg_dma_address(sg);
  872. desc->dst_addr = edmac->runtime_addr;
  873. } else {
  874. desc->src_addr = edmac->runtime_addr;
  875. desc->dst_addr = sg_dma_address(sg);
  876. }
  877. desc->size = sg_len;
  878. if (!first)
  879. first = desc;
  880. else
  881. list_add_tail(&desc->node, &first->tx_list);
  882. }
  883. first->txd.cookie = -EBUSY;
  884. first->txd.flags = flags;
  885. return &first->txd;
  886. fail:
  887. ep93xx_dma_desc_put(edmac, first);
  888. return NULL;
  889. }
  890. /**
  891. * ep93xx_dma_prep_dma_cyclic - prepare a cyclic DMA operation
  892. * @chan: channel
  893. * @dma_addr: DMA mapped address of the buffer
  894. * @buf_len: length of the buffer (in bytes)
  895. * @period_len: lenght of a single period
  896. * @dir: direction of the operation
  897. *
  898. * Prepares a descriptor for cyclic DMA operation. This means that once the
  899. * descriptor is submitted, we will be submitting in a @period_len sized
  900. * buffers and calling callback once the period has been elapsed. Transfer
  901. * terminates only when client calls dmaengine_terminate_all() for this
  902. * channel.
  903. *
  904. * Returns a valid DMA descriptor or %NULL in case of failure.
  905. */
  906. static struct dma_async_tx_descriptor *
  907. ep93xx_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
  908. size_t buf_len, size_t period_len,
  909. enum dma_transfer_direction dir)
  910. {
  911. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  912. struct ep93xx_dma_desc *desc, *first;
  913. size_t offset = 0;
  914. if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
  915. dev_warn(chan2dev(edmac),
  916. "channel was configured with different direction\n");
  917. return NULL;
  918. }
  919. if (test_and_set_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
  920. dev_warn(chan2dev(edmac),
  921. "channel is already used for cyclic transfers\n");
  922. return NULL;
  923. }
  924. if (period_len > DMA_MAX_CHAN_BYTES) {
  925. dev_warn(chan2dev(edmac), "too big period length %d\n",
  926. period_len);
  927. return NULL;
  928. }
  929. /* Split the buffer into period size chunks */
  930. first = NULL;
  931. for (offset = 0; offset < buf_len; offset += period_len) {
  932. desc = ep93xx_dma_desc_get(edmac);
  933. if (!desc) {
  934. dev_warn(chan2dev(edmac), "couln't get descriptor\n");
  935. goto fail;
  936. }
  937. if (dir == DMA_MEM_TO_DEV) {
  938. desc->src_addr = dma_addr + offset;
  939. desc->dst_addr = edmac->runtime_addr;
  940. } else {
  941. desc->src_addr = edmac->runtime_addr;
  942. desc->dst_addr = dma_addr + offset;
  943. }
  944. desc->size = period_len;
  945. if (!first)
  946. first = desc;
  947. else
  948. list_add_tail(&desc->node, &first->tx_list);
  949. }
  950. first->txd.cookie = -EBUSY;
  951. return &first->txd;
  952. fail:
  953. ep93xx_dma_desc_put(edmac, first);
  954. return NULL;
  955. }
  956. /**
  957. * ep93xx_dma_terminate_all - terminate all transactions
  958. * @edmac: channel
  959. *
  960. * Stops all DMA transactions. All descriptors are put back to the
  961. * @edmac->free_list and callbacks are _not_ called.
  962. */
  963. static int ep93xx_dma_terminate_all(struct ep93xx_dma_chan *edmac)
  964. {
  965. struct ep93xx_dma_desc *desc, *_d;
  966. unsigned long flags;
  967. LIST_HEAD(list);
  968. spin_lock_irqsave(&edmac->lock, flags);
  969. /* First we disable and flush the DMA channel */
  970. edmac->edma->hw_shutdown(edmac);
  971. clear_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags);
  972. list_splice_init(&edmac->active, &list);
  973. list_splice_init(&edmac->queue, &list);
  974. /*
  975. * We then re-enable the channel. This way we can continue submitting
  976. * the descriptors by just calling ->hw_submit() again.
  977. */
  978. edmac->edma->hw_setup(edmac);
  979. spin_unlock_irqrestore(&edmac->lock, flags);
  980. list_for_each_entry_safe(desc, _d, &list, node)
  981. ep93xx_dma_desc_put(edmac, desc);
  982. return 0;
  983. }
  984. static int ep93xx_dma_slave_config(struct ep93xx_dma_chan *edmac,
  985. struct dma_slave_config *config)
  986. {
  987. enum dma_slave_buswidth width;
  988. unsigned long flags;
  989. u32 addr, ctrl;
  990. if (!edmac->edma->m2m)
  991. return -EINVAL;
  992. switch (config->direction) {
  993. case DMA_DEV_TO_MEM:
  994. width = config->src_addr_width;
  995. addr = config->src_addr;
  996. break;
  997. case DMA_MEM_TO_DEV:
  998. width = config->dst_addr_width;
  999. addr = config->dst_addr;
  1000. break;
  1001. default:
  1002. return -EINVAL;
  1003. }
  1004. switch (width) {
  1005. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1006. ctrl = 0;
  1007. break;
  1008. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1009. ctrl = M2M_CONTROL_PW_16;
  1010. break;
  1011. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1012. ctrl = M2M_CONTROL_PW_32;
  1013. break;
  1014. default:
  1015. return -EINVAL;
  1016. }
  1017. spin_lock_irqsave(&edmac->lock, flags);
  1018. edmac->runtime_addr = addr;
  1019. edmac->runtime_ctrl = ctrl;
  1020. spin_unlock_irqrestore(&edmac->lock, flags);
  1021. return 0;
  1022. }
  1023. /**
  1024. * ep93xx_dma_control - manipulate all pending operations on a channel
  1025. * @chan: channel
  1026. * @cmd: control command to perform
  1027. * @arg: optional argument
  1028. *
  1029. * Controls the channel. Function returns %0 in case of success or negative
  1030. * error in case of failure.
  1031. */
  1032. static int ep93xx_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1033. unsigned long arg)
  1034. {
  1035. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  1036. struct dma_slave_config *config;
  1037. switch (cmd) {
  1038. case DMA_TERMINATE_ALL:
  1039. return ep93xx_dma_terminate_all(edmac);
  1040. case DMA_SLAVE_CONFIG:
  1041. config = (struct dma_slave_config *)arg;
  1042. return ep93xx_dma_slave_config(edmac, config);
  1043. default:
  1044. break;
  1045. }
  1046. return -ENOSYS;
  1047. }
  1048. /**
  1049. * ep93xx_dma_tx_status - check if a transaction is completed
  1050. * @chan: channel
  1051. * @cookie: transaction specific cookie
  1052. * @state: state of the transaction is stored here if given
  1053. *
  1054. * This function can be used to query state of a given transaction.
  1055. */
  1056. static enum dma_status ep93xx_dma_tx_status(struct dma_chan *chan,
  1057. dma_cookie_t cookie,
  1058. struct dma_tx_state *state)
  1059. {
  1060. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  1061. dma_cookie_t last_used, last_completed;
  1062. enum dma_status ret;
  1063. unsigned long flags;
  1064. spin_lock_irqsave(&edmac->lock, flags);
  1065. last_used = chan->cookie;
  1066. last_completed = chan->completed_cookie;
  1067. spin_unlock_irqrestore(&edmac->lock, flags);
  1068. ret = dma_async_is_complete(cookie, last_completed, last_used);
  1069. dma_set_tx_state(state, last_completed, last_used, 0);
  1070. return ret;
  1071. }
  1072. /**
  1073. * ep93xx_dma_issue_pending - push pending transactions to the hardware
  1074. * @chan: channel
  1075. *
  1076. * When this function is called, all pending transactions are pushed to the
  1077. * hardware and executed.
  1078. */
  1079. static void ep93xx_dma_issue_pending(struct dma_chan *chan)
  1080. {
  1081. ep93xx_dma_advance_work(to_ep93xx_dma_chan(chan));
  1082. }
  1083. static int __init ep93xx_dma_probe(struct platform_device *pdev)
  1084. {
  1085. struct ep93xx_dma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1086. struct ep93xx_dma_engine *edma;
  1087. struct dma_device *dma_dev;
  1088. size_t edma_size;
  1089. int ret, i;
  1090. edma_size = pdata->num_channels * sizeof(struct ep93xx_dma_chan);
  1091. edma = kzalloc(sizeof(*edma) + edma_size, GFP_KERNEL);
  1092. if (!edma)
  1093. return -ENOMEM;
  1094. dma_dev = &edma->dma_dev;
  1095. edma->m2m = platform_get_device_id(pdev)->driver_data;
  1096. edma->num_channels = pdata->num_channels;
  1097. INIT_LIST_HEAD(&dma_dev->channels);
  1098. for (i = 0; i < pdata->num_channels; i++) {
  1099. const struct ep93xx_dma_chan_data *cdata = &pdata->channels[i];
  1100. struct ep93xx_dma_chan *edmac = &edma->channels[i];
  1101. edmac->chan.device = dma_dev;
  1102. edmac->regs = cdata->base;
  1103. edmac->irq = cdata->irq;
  1104. edmac->edma = edma;
  1105. edmac->clk = clk_get(NULL, cdata->name);
  1106. if (IS_ERR(edmac->clk)) {
  1107. dev_warn(&pdev->dev, "failed to get clock for %s\n",
  1108. cdata->name);
  1109. continue;
  1110. }
  1111. spin_lock_init(&edmac->lock);
  1112. INIT_LIST_HEAD(&edmac->active);
  1113. INIT_LIST_HEAD(&edmac->queue);
  1114. INIT_LIST_HEAD(&edmac->free_list);
  1115. tasklet_init(&edmac->tasklet, ep93xx_dma_tasklet,
  1116. (unsigned long)edmac);
  1117. list_add_tail(&edmac->chan.device_node,
  1118. &dma_dev->channels);
  1119. }
  1120. dma_cap_zero(dma_dev->cap_mask);
  1121. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  1122. dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
  1123. dma_dev->dev = &pdev->dev;
  1124. dma_dev->device_alloc_chan_resources = ep93xx_dma_alloc_chan_resources;
  1125. dma_dev->device_free_chan_resources = ep93xx_dma_free_chan_resources;
  1126. dma_dev->device_prep_slave_sg = ep93xx_dma_prep_slave_sg;
  1127. dma_dev->device_prep_dma_cyclic = ep93xx_dma_prep_dma_cyclic;
  1128. dma_dev->device_control = ep93xx_dma_control;
  1129. dma_dev->device_issue_pending = ep93xx_dma_issue_pending;
  1130. dma_dev->device_tx_status = ep93xx_dma_tx_status;
  1131. dma_set_max_seg_size(dma_dev->dev, DMA_MAX_CHAN_BYTES);
  1132. if (edma->m2m) {
  1133. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  1134. dma_dev->device_prep_dma_memcpy = ep93xx_dma_prep_dma_memcpy;
  1135. edma->hw_setup = m2m_hw_setup;
  1136. edma->hw_shutdown = m2m_hw_shutdown;
  1137. edma->hw_submit = m2m_hw_submit;
  1138. edma->hw_interrupt = m2m_hw_interrupt;
  1139. } else {
  1140. dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
  1141. edma->hw_setup = m2p_hw_setup;
  1142. edma->hw_shutdown = m2p_hw_shutdown;
  1143. edma->hw_submit = m2p_hw_submit;
  1144. edma->hw_interrupt = m2p_hw_interrupt;
  1145. }
  1146. ret = dma_async_device_register(dma_dev);
  1147. if (unlikely(ret)) {
  1148. for (i = 0; i < edma->num_channels; i++) {
  1149. struct ep93xx_dma_chan *edmac = &edma->channels[i];
  1150. if (!IS_ERR_OR_NULL(edmac->clk))
  1151. clk_put(edmac->clk);
  1152. }
  1153. kfree(edma);
  1154. } else {
  1155. dev_info(dma_dev->dev, "EP93xx M2%s DMA ready\n",
  1156. edma->m2m ? "M" : "P");
  1157. }
  1158. return ret;
  1159. }
  1160. static struct platform_device_id ep93xx_dma_driver_ids[] = {
  1161. { "ep93xx-dma-m2p", 0 },
  1162. { "ep93xx-dma-m2m", 1 },
  1163. { },
  1164. };
  1165. static struct platform_driver ep93xx_dma_driver = {
  1166. .driver = {
  1167. .name = "ep93xx-dma",
  1168. },
  1169. .id_table = ep93xx_dma_driver_ids,
  1170. };
  1171. static int __init ep93xx_dma_module_init(void)
  1172. {
  1173. return platform_driver_probe(&ep93xx_dma_driver, ep93xx_dma_probe);
  1174. }
  1175. subsys_initcall(ep93xx_dma_module_init);
  1176. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  1177. MODULE_DESCRIPTION("EP93xx DMA driver");
  1178. MODULE_LICENSE("GPL");