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