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. return 0;
  290. }
  291. static inline u32 m2p_channel_state(struct ep93xx_dma_chan *edmac)
  292. {
  293. return (readl(edmac->regs + M2P_STATUS) >> 4) & 0x3;
  294. }
  295. static void m2p_hw_shutdown(struct ep93xx_dma_chan *edmac)
  296. {
  297. u32 control;
  298. control = readl(edmac->regs + M2P_CONTROL);
  299. control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
  300. m2p_set_control(edmac, control);
  301. while (m2p_channel_state(edmac) >= M2P_STATE_ON)
  302. cpu_relax();
  303. m2p_set_control(edmac, 0);
  304. while (m2p_channel_state(edmac) == M2P_STATE_STALL)
  305. cpu_relax();
  306. }
  307. static void m2p_fill_desc(struct ep93xx_dma_chan *edmac)
  308. {
  309. struct ep93xx_dma_desc *desc;
  310. u32 bus_addr;
  311. desc = ep93xx_dma_get_active(edmac);
  312. if (!desc) {
  313. dev_warn(chan2dev(edmac), "M2P: empty descriptor list\n");
  314. return;
  315. }
  316. if (ep93xx_dma_chan_direction(&edmac->chan) == DMA_MEM_TO_DEV)
  317. bus_addr = desc->src_addr;
  318. else
  319. bus_addr = desc->dst_addr;
  320. if (edmac->buffer == 0) {
  321. writel(desc->size, edmac->regs + M2P_MAXCNT0);
  322. writel(bus_addr, edmac->regs + M2P_BASE0);
  323. } else {
  324. writel(desc->size, edmac->regs + M2P_MAXCNT1);
  325. writel(bus_addr, edmac->regs + M2P_BASE1);
  326. }
  327. edmac->buffer ^= 1;
  328. }
  329. static void m2p_hw_submit(struct ep93xx_dma_chan *edmac)
  330. {
  331. u32 control = readl(edmac->regs + M2P_CONTROL);
  332. m2p_fill_desc(edmac);
  333. control |= M2P_CONTROL_STALLINT;
  334. if (ep93xx_dma_advance_active(edmac)) {
  335. m2p_fill_desc(edmac);
  336. control |= M2P_CONTROL_NFBINT;
  337. }
  338. m2p_set_control(edmac, control);
  339. }
  340. static int m2p_hw_interrupt(struct ep93xx_dma_chan *edmac)
  341. {
  342. u32 irq_status = readl(edmac->regs + M2P_INTERRUPT);
  343. u32 control;
  344. if (irq_status & M2P_INTERRUPT_ERROR) {
  345. struct ep93xx_dma_desc *desc = ep93xx_dma_get_active(edmac);
  346. /* Clear the error interrupt */
  347. writel(1, edmac->regs + M2P_INTERRUPT);
  348. /*
  349. * It seems that there is no easy way of reporting errors back
  350. * to client so we just report the error here and continue as
  351. * usual.
  352. *
  353. * Revisit this when there is a mechanism to report back the
  354. * errors.
  355. */
  356. dev_err(chan2dev(edmac),
  357. "DMA transfer failed! Details:\n"
  358. "\tcookie : %d\n"
  359. "\tsrc_addr : 0x%08x\n"
  360. "\tdst_addr : 0x%08x\n"
  361. "\tsize : %zu\n",
  362. desc->txd.cookie, desc->src_addr, desc->dst_addr,
  363. desc->size);
  364. }
  365. /*
  366. * Even latest E2 silicon revision sometimes assert STALL interrupt
  367. * instead of NFB. Therefore we treat them equally, basing on the
  368. * amount of data we still have to transfer.
  369. */
  370. if (!(irq_status & (M2P_INTERRUPT_STALL | M2P_INTERRUPT_NFB)))
  371. return INTERRUPT_UNKNOWN;
  372. if (ep93xx_dma_advance_active(edmac)) {
  373. m2p_fill_desc(edmac);
  374. return INTERRUPT_NEXT_BUFFER;
  375. }
  376. /* Disable interrupts */
  377. control = readl(edmac->regs + M2P_CONTROL);
  378. control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
  379. m2p_set_control(edmac, control);
  380. return INTERRUPT_DONE;
  381. }
  382. /*
  383. * M2M DMA implementation
  384. */
  385. static int m2m_hw_setup(struct ep93xx_dma_chan *edmac)
  386. {
  387. const struct ep93xx_dma_data *data = edmac->chan.private;
  388. u32 control = 0;
  389. if (!data) {
  390. /* This is memcpy channel, nothing to configure */
  391. writel(control, edmac->regs + M2M_CONTROL);
  392. return 0;
  393. }
  394. switch (data->port) {
  395. case EP93XX_DMA_SSP:
  396. /*
  397. * This was found via experimenting - anything less than 5
  398. * causes the channel to perform only a partial transfer which
  399. * leads to problems since we don't get DONE interrupt then.
  400. */
  401. control = (5 << M2M_CONTROL_PWSC_SHIFT);
  402. control |= M2M_CONTROL_NO_HDSK;
  403. if (data->direction == DMA_MEM_TO_DEV) {
  404. control |= M2M_CONTROL_DAH;
  405. control |= M2M_CONTROL_TM_TX;
  406. control |= M2M_CONTROL_RSS_SSPTX;
  407. } else {
  408. control |= M2M_CONTROL_SAH;
  409. control |= M2M_CONTROL_TM_RX;
  410. control |= M2M_CONTROL_RSS_SSPRX;
  411. }
  412. break;
  413. case EP93XX_DMA_IDE:
  414. /*
  415. * This IDE part is totally untested. Values below are taken
  416. * from the EP93xx Users's Guide and might not be correct.
  417. */
  418. if (data->direction == DMA_MEM_TO_DEV) {
  419. /* Worst case from the UG */
  420. control = (3 << M2M_CONTROL_PWSC_SHIFT);
  421. control |= M2M_CONTROL_DAH;
  422. control |= M2M_CONTROL_TM_TX;
  423. } else {
  424. control = (2 << M2M_CONTROL_PWSC_SHIFT);
  425. control |= M2M_CONTROL_SAH;
  426. control |= M2M_CONTROL_TM_RX;
  427. }
  428. control |= M2M_CONTROL_NO_HDSK;
  429. control |= M2M_CONTROL_RSS_IDE;
  430. control |= M2M_CONTROL_PW_16;
  431. break;
  432. default:
  433. return -EINVAL;
  434. }
  435. writel(control, edmac->regs + M2M_CONTROL);
  436. return 0;
  437. }
  438. static void m2m_hw_shutdown(struct ep93xx_dma_chan *edmac)
  439. {
  440. /* Just disable the channel */
  441. writel(0, edmac->regs + M2M_CONTROL);
  442. }
  443. static void m2m_fill_desc(struct ep93xx_dma_chan *edmac)
  444. {
  445. struct ep93xx_dma_desc *desc;
  446. desc = ep93xx_dma_get_active(edmac);
  447. if (!desc) {
  448. dev_warn(chan2dev(edmac), "M2M: empty descriptor list\n");
  449. return;
  450. }
  451. if (edmac->buffer == 0) {
  452. writel(desc->src_addr, edmac->regs + M2M_SAR_BASE0);
  453. writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE0);
  454. writel(desc->size, edmac->regs + M2M_BCR0);
  455. } else {
  456. writel(desc->src_addr, edmac->regs + M2M_SAR_BASE1);
  457. writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE1);
  458. writel(desc->size, edmac->regs + M2M_BCR1);
  459. }
  460. edmac->buffer ^= 1;
  461. }
  462. static void m2m_hw_submit(struct ep93xx_dma_chan *edmac)
  463. {
  464. struct ep93xx_dma_data *data = edmac->chan.private;
  465. u32 control = readl(edmac->regs + M2M_CONTROL);
  466. /*
  467. * Since we allow clients to configure PW (peripheral width) we always
  468. * clear PW bits here and then set them according what is given in
  469. * the runtime configuration.
  470. */
  471. control &= ~M2M_CONTROL_PW_MASK;
  472. control |= edmac->runtime_ctrl;
  473. m2m_fill_desc(edmac);
  474. control |= M2M_CONTROL_DONEINT;
  475. if (ep93xx_dma_advance_active(edmac)) {
  476. m2m_fill_desc(edmac);
  477. control |= M2M_CONTROL_NFBINT;
  478. }
  479. /*
  480. * Now we can finally enable the channel. For M2M channel this must be
  481. * done _after_ the BCRx registers are programmed.
  482. */
  483. control |= M2M_CONTROL_ENABLE;
  484. writel(control, edmac->regs + M2M_CONTROL);
  485. if (!data) {
  486. /*
  487. * For memcpy channels the software trigger must be asserted
  488. * in order to start the memcpy operation.
  489. */
  490. control |= M2M_CONTROL_START;
  491. writel(control, edmac->regs + M2M_CONTROL);
  492. }
  493. }
  494. /*
  495. * According to EP93xx User's Guide, we should receive DONE interrupt when all
  496. * M2M DMA controller transactions complete normally. This is not always the
  497. * case - sometimes EP93xx M2M DMA asserts DONE interrupt when the DMA channel
  498. * is still running (channel Buffer FSM in DMA_BUF_ON state, and channel
  499. * Control FSM in DMA_MEM_RD state, observed at least in IDE-DMA operation).
  500. * In effect, disabling the channel when only DONE bit is set could stop
  501. * currently running DMA transfer. To avoid this, we use Buffer FSM and
  502. * Control FSM to check current state of DMA channel.
  503. */
  504. static int m2m_hw_interrupt(struct ep93xx_dma_chan *edmac)
  505. {
  506. u32 status = readl(edmac->regs + M2M_STATUS);
  507. u32 ctl_fsm = status & M2M_STATUS_CTL_MASK;
  508. u32 buf_fsm = status & M2M_STATUS_BUF_MASK;
  509. bool done = status & M2M_STATUS_DONE;
  510. bool last_done;
  511. u32 control;
  512. struct ep93xx_dma_desc *desc;
  513. /* Accept only DONE and NFB interrupts */
  514. if (!(readl(edmac->regs + M2M_INTERRUPT) & M2M_INTERRUPT_MASK))
  515. return INTERRUPT_UNKNOWN;
  516. if (done) {
  517. /* Clear the DONE bit */
  518. writel(0, edmac->regs + M2M_INTERRUPT);
  519. }
  520. /*
  521. * Check whether we are done with descriptors or not. This, together
  522. * with DMA channel state, determines action to take in interrupt.
  523. */
  524. desc = ep93xx_dma_get_active(edmac);
  525. last_done = !desc || desc->txd.cookie;
  526. /*
  527. * Use M2M DMA Buffer FSM and Control FSM to check current state of
  528. * DMA channel. Using DONE and NFB bits from channel status register
  529. * or bits from channel interrupt register is not reliable.
  530. */
  531. if (!last_done &&
  532. (buf_fsm == M2M_STATUS_BUF_NO ||
  533. buf_fsm == M2M_STATUS_BUF_ON)) {
  534. /*
  535. * Two buffers are ready for update when Buffer FSM is in
  536. * DMA_NO_BUF state. Only one buffer can be prepared without
  537. * disabling the channel or polling the DONE bit.
  538. * To simplify things, always prepare only one buffer.
  539. */
  540. if (ep93xx_dma_advance_active(edmac)) {
  541. m2m_fill_desc(edmac);
  542. if (done && !edmac->chan.private) {
  543. /* Software trigger for memcpy channel */
  544. control = readl(edmac->regs + M2M_CONTROL);
  545. control |= M2M_CONTROL_START;
  546. writel(control, edmac->regs + M2M_CONTROL);
  547. }
  548. return INTERRUPT_NEXT_BUFFER;
  549. } else {
  550. last_done = true;
  551. }
  552. }
  553. /*
  554. * Disable the channel only when Buffer FSM is in DMA_NO_BUF state
  555. * and Control FSM is in DMA_STALL state.
  556. */
  557. if (last_done &&
  558. buf_fsm == M2M_STATUS_BUF_NO &&
  559. ctl_fsm == M2M_STATUS_CTL_STALL) {
  560. /* Disable interrupts and the channel */
  561. control = readl(edmac->regs + M2M_CONTROL);
  562. control &= ~(M2M_CONTROL_DONEINT | M2M_CONTROL_NFBINT
  563. | M2M_CONTROL_ENABLE);
  564. writel(control, edmac->regs + M2M_CONTROL);
  565. return INTERRUPT_DONE;
  566. }
  567. /*
  568. * Nothing to do this time.
  569. */
  570. return INTERRUPT_NEXT_BUFFER;
  571. }
  572. /*
  573. * DMA engine API implementation
  574. */
  575. static struct ep93xx_dma_desc *
  576. ep93xx_dma_desc_get(struct ep93xx_dma_chan *edmac)
  577. {
  578. struct ep93xx_dma_desc *desc, *_desc;
  579. struct ep93xx_dma_desc *ret = NULL;
  580. unsigned long flags;
  581. spin_lock_irqsave(&edmac->lock, flags);
  582. list_for_each_entry_safe(desc, _desc, &edmac->free_list, node) {
  583. if (async_tx_test_ack(&desc->txd)) {
  584. list_del_init(&desc->node);
  585. /* Re-initialize the descriptor */
  586. desc->src_addr = 0;
  587. desc->dst_addr = 0;
  588. desc->size = 0;
  589. desc->complete = false;
  590. desc->txd.cookie = 0;
  591. desc->txd.callback = NULL;
  592. desc->txd.callback_param = NULL;
  593. ret = desc;
  594. break;
  595. }
  596. }
  597. spin_unlock_irqrestore(&edmac->lock, flags);
  598. return ret;
  599. }
  600. static void ep93xx_dma_desc_put(struct ep93xx_dma_chan *edmac,
  601. struct ep93xx_dma_desc *desc)
  602. {
  603. if (desc) {
  604. unsigned long flags;
  605. spin_lock_irqsave(&edmac->lock, flags);
  606. list_splice_init(&desc->tx_list, &edmac->free_list);
  607. list_add(&desc->node, &edmac->free_list);
  608. spin_unlock_irqrestore(&edmac->lock, flags);
  609. }
  610. }
  611. /**
  612. * ep93xx_dma_advance_work - start processing the next pending transaction
  613. * @edmac: channel
  614. *
  615. * If we have pending transactions queued and we are currently idling, this
  616. * function takes the next queued transaction from the @edmac->queue and
  617. * pushes it to the hardware for execution.
  618. */
  619. static void ep93xx_dma_advance_work(struct ep93xx_dma_chan *edmac)
  620. {
  621. struct ep93xx_dma_desc *new;
  622. unsigned long flags;
  623. spin_lock_irqsave(&edmac->lock, flags);
  624. if (!list_empty(&edmac->active) || list_empty(&edmac->queue)) {
  625. spin_unlock_irqrestore(&edmac->lock, flags);
  626. return;
  627. }
  628. /* Take the next descriptor from the pending queue */
  629. new = list_first_entry(&edmac->queue, struct ep93xx_dma_desc, node);
  630. list_del_init(&new->node);
  631. ep93xx_dma_set_active(edmac, new);
  632. /* Push it to the hardware */
  633. edmac->edma->hw_submit(edmac);
  634. spin_unlock_irqrestore(&edmac->lock, flags);
  635. }
  636. static void ep93xx_dma_tasklet(unsigned long data)
  637. {
  638. struct ep93xx_dma_chan *edmac = (struct ep93xx_dma_chan *)data;
  639. struct ep93xx_dma_desc *desc, *d;
  640. dma_async_tx_callback callback = NULL;
  641. void *callback_param = NULL;
  642. LIST_HEAD(list);
  643. spin_lock_irq(&edmac->lock);
  644. /*
  645. * If dma_terminate_all() was called before we get to run, the active
  646. * list has become empty. If that happens we aren't supposed to do
  647. * anything more than call ep93xx_dma_advance_work().
  648. */
  649. desc = ep93xx_dma_get_active(edmac);
  650. if (desc) {
  651. if (desc->complete) {
  652. /* mark descriptor complete for non cyclic case only */
  653. if (!test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
  654. dma_cookie_complete(&desc->txd);
  655. list_splice_init(&edmac->active, &list);
  656. }
  657. callback = desc->txd.callback;
  658. callback_param = desc->txd.callback_param;
  659. }
  660. spin_unlock_irq(&edmac->lock);
  661. /* Pick up the next descriptor from the queue */
  662. ep93xx_dma_advance_work(edmac);
  663. /* Now we can release all the chained descriptors */
  664. list_for_each_entry_safe(desc, d, &list, node) {
  665. dma_descriptor_unmap(&desc->txd);
  666. ep93xx_dma_desc_put(edmac, desc);
  667. }
  668. if (callback)
  669. callback(callback_param);
  670. }
  671. static irqreturn_t ep93xx_dma_interrupt(int irq, void *dev_id)
  672. {
  673. struct ep93xx_dma_chan *edmac = dev_id;
  674. struct ep93xx_dma_desc *desc;
  675. irqreturn_t ret = IRQ_HANDLED;
  676. spin_lock(&edmac->lock);
  677. desc = ep93xx_dma_get_active(edmac);
  678. if (!desc) {
  679. dev_warn(chan2dev(edmac),
  680. "got interrupt while active list is empty\n");
  681. spin_unlock(&edmac->lock);
  682. return IRQ_NONE;
  683. }
  684. switch (edmac->edma->hw_interrupt(edmac)) {
  685. case INTERRUPT_DONE:
  686. desc->complete = true;
  687. tasklet_schedule(&edmac->tasklet);
  688. break;
  689. case INTERRUPT_NEXT_BUFFER:
  690. if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
  691. tasklet_schedule(&edmac->tasklet);
  692. break;
  693. default:
  694. dev_warn(chan2dev(edmac), "unknown interrupt!\n");
  695. ret = IRQ_NONE;
  696. break;
  697. }
  698. spin_unlock(&edmac->lock);
  699. return ret;
  700. }
  701. /**
  702. * ep93xx_dma_tx_submit - set the prepared descriptor(s) to be executed
  703. * @tx: descriptor to be executed
  704. *
  705. * Function will execute given descriptor on the hardware or if the hardware
  706. * is busy, queue the descriptor to be executed later on. Returns cookie which
  707. * can be used to poll the status of the descriptor.
  708. */
  709. static dma_cookie_t ep93xx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  710. {
  711. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(tx->chan);
  712. struct ep93xx_dma_desc *desc;
  713. dma_cookie_t cookie;
  714. unsigned long flags;
  715. spin_lock_irqsave(&edmac->lock, flags);
  716. cookie = dma_cookie_assign(tx);
  717. desc = container_of(tx, struct ep93xx_dma_desc, txd);
  718. /*
  719. * If nothing is currently prosessed, we push this descriptor
  720. * directly to the hardware. Otherwise we put the descriptor
  721. * to the pending queue.
  722. */
  723. if (list_empty(&edmac->active)) {
  724. ep93xx_dma_set_active(edmac, desc);
  725. edmac->edma->hw_submit(edmac);
  726. } else {
  727. list_add_tail(&desc->node, &edmac->queue);
  728. }
  729. spin_unlock_irqrestore(&edmac->lock, flags);
  730. return cookie;
  731. }
  732. /**
  733. * ep93xx_dma_alloc_chan_resources - allocate resources for the channel
  734. * @chan: channel to allocate resources
  735. *
  736. * Function allocates necessary resources for the given DMA channel and
  737. * returns number of allocated descriptors for the channel. Negative errno
  738. * is returned in case of failure.
  739. */
  740. static int ep93xx_dma_alloc_chan_resources(struct dma_chan *chan)
  741. {
  742. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  743. struct ep93xx_dma_data *data = chan->private;
  744. const char *name = dma_chan_name(chan);
  745. int ret, i;
  746. /* Sanity check the channel parameters */
  747. if (!edmac->edma->m2m) {
  748. if (!data)
  749. return -EINVAL;
  750. if (data->port < EP93XX_DMA_I2S1 ||
  751. data->port > EP93XX_DMA_IRDA)
  752. return -EINVAL;
  753. if (data->direction != ep93xx_dma_chan_direction(chan))
  754. return -EINVAL;
  755. } else {
  756. if (data) {
  757. switch (data->port) {
  758. case EP93XX_DMA_SSP:
  759. case EP93XX_DMA_IDE:
  760. if (!is_slave_direction(data->direction))
  761. return -EINVAL;
  762. break;
  763. default:
  764. return -EINVAL;
  765. }
  766. }
  767. }
  768. if (data && data->name)
  769. name = data->name;
  770. ret = clk_enable(edmac->clk);
  771. if (ret)
  772. return ret;
  773. ret = request_irq(edmac->irq, ep93xx_dma_interrupt, 0, name, edmac);
  774. if (ret)
  775. goto fail_clk_disable;
  776. spin_lock_irq(&edmac->lock);
  777. dma_cookie_init(&edmac->chan);
  778. ret = edmac->edma->hw_setup(edmac);
  779. spin_unlock_irq(&edmac->lock);
  780. if (ret)
  781. goto fail_free_irq;
  782. for (i = 0; i < DMA_MAX_CHAN_DESCRIPTORS; i++) {
  783. struct ep93xx_dma_desc *desc;
  784. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  785. if (!desc) {
  786. dev_warn(chan2dev(edmac), "not enough descriptors\n");
  787. break;
  788. }
  789. INIT_LIST_HEAD(&desc->tx_list);
  790. dma_async_tx_descriptor_init(&desc->txd, chan);
  791. desc->txd.flags = DMA_CTRL_ACK;
  792. desc->txd.tx_submit = ep93xx_dma_tx_submit;
  793. ep93xx_dma_desc_put(edmac, desc);
  794. }
  795. return i;
  796. fail_free_irq:
  797. free_irq(edmac->irq, edmac);
  798. fail_clk_disable:
  799. clk_disable(edmac->clk);
  800. return ret;
  801. }
  802. /**
  803. * ep93xx_dma_free_chan_resources - release resources for the channel
  804. * @chan: channel
  805. *
  806. * Function releases all the resources allocated for the given channel.
  807. * The channel must be idle when this is called.
  808. */
  809. static void ep93xx_dma_free_chan_resources(struct dma_chan *chan)
  810. {
  811. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  812. struct ep93xx_dma_desc *desc, *d;
  813. unsigned long flags;
  814. LIST_HEAD(list);
  815. BUG_ON(!list_empty(&edmac->active));
  816. BUG_ON(!list_empty(&edmac->queue));
  817. spin_lock_irqsave(&edmac->lock, flags);
  818. edmac->edma->hw_shutdown(edmac);
  819. edmac->runtime_addr = 0;
  820. edmac->runtime_ctrl = 0;
  821. edmac->buffer = 0;
  822. list_splice_init(&edmac->free_list, &list);
  823. spin_unlock_irqrestore(&edmac->lock, flags);
  824. list_for_each_entry_safe(desc, d, &list, node)
  825. kfree(desc);
  826. clk_disable(edmac->clk);
  827. free_irq(edmac->irq, edmac);
  828. }
  829. /**
  830. * ep93xx_dma_prep_dma_memcpy - prepare a memcpy DMA operation
  831. * @chan: channel
  832. * @dest: destination bus address
  833. * @src: source bus address
  834. * @len: size of the transaction
  835. * @flags: flags for the descriptor
  836. *
  837. * Returns a valid DMA descriptor or %NULL in case of failure.
  838. */
  839. static struct dma_async_tx_descriptor *
  840. ep93xx_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
  841. dma_addr_t src, size_t len, unsigned long flags)
  842. {
  843. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  844. struct ep93xx_dma_desc *desc, *first;
  845. size_t bytes, offset;
  846. first = NULL;
  847. for (offset = 0; offset < len; offset += bytes) {
  848. desc = ep93xx_dma_desc_get(edmac);
  849. if (!desc) {
  850. dev_warn(chan2dev(edmac), "couln't get descriptor\n");
  851. goto fail;
  852. }
  853. bytes = min_t(size_t, len - offset, DMA_MAX_CHAN_BYTES);
  854. desc->src_addr = src + offset;
  855. desc->dst_addr = dest + offset;
  856. desc->size = bytes;
  857. if (!first)
  858. first = desc;
  859. else
  860. list_add_tail(&desc->node, &first->tx_list);
  861. }
  862. first->txd.cookie = -EBUSY;
  863. first->txd.flags = flags;
  864. return &first->txd;
  865. fail:
  866. ep93xx_dma_desc_put(edmac, first);
  867. return NULL;
  868. }
  869. /**
  870. * ep93xx_dma_prep_slave_sg - prepare a slave DMA operation
  871. * @chan: channel
  872. * @sgl: list of buffers to transfer
  873. * @sg_len: number of entries in @sgl
  874. * @dir: direction of tha DMA transfer
  875. * @flags: flags for the descriptor
  876. * @context: operation context (ignored)
  877. *
  878. * Returns a valid DMA descriptor or %NULL in case of failure.
  879. */
  880. static struct dma_async_tx_descriptor *
  881. ep93xx_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  882. unsigned int sg_len, enum dma_transfer_direction dir,
  883. unsigned long flags, void *context)
  884. {
  885. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  886. struct ep93xx_dma_desc *desc, *first;
  887. struct scatterlist *sg;
  888. int i;
  889. if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
  890. dev_warn(chan2dev(edmac),
  891. "channel was configured with different direction\n");
  892. return NULL;
  893. }
  894. if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
  895. dev_warn(chan2dev(edmac),
  896. "channel is already used for cyclic transfers\n");
  897. return NULL;
  898. }
  899. first = NULL;
  900. for_each_sg(sgl, sg, sg_len, i) {
  901. size_t sg_len = sg_dma_len(sg);
  902. if (sg_len > DMA_MAX_CHAN_BYTES) {
  903. dev_warn(chan2dev(edmac), "too big transfer size %d\n",
  904. sg_len);
  905. goto fail;
  906. }
  907. desc = ep93xx_dma_desc_get(edmac);
  908. if (!desc) {
  909. dev_warn(chan2dev(edmac), "couln't get descriptor\n");
  910. goto fail;
  911. }
  912. if (dir == DMA_MEM_TO_DEV) {
  913. desc->src_addr = sg_dma_address(sg);
  914. desc->dst_addr = edmac->runtime_addr;
  915. } else {
  916. desc->src_addr = edmac->runtime_addr;
  917. desc->dst_addr = sg_dma_address(sg);
  918. }
  919. desc->size = sg_len;
  920. if (!first)
  921. first = desc;
  922. else
  923. list_add_tail(&desc->node, &first->tx_list);
  924. }
  925. first->txd.cookie = -EBUSY;
  926. first->txd.flags = flags;
  927. return &first->txd;
  928. fail:
  929. ep93xx_dma_desc_put(edmac, first);
  930. return NULL;
  931. }
  932. /**
  933. * ep93xx_dma_prep_dma_cyclic - prepare a cyclic DMA operation
  934. * @chan: channel
  935. * @dma_addr: DMA mapped address of the buffer
  936. * @buf_len: length of the buffer (in bytes)
  937. * @period_len: length of a single period
  938. * @dir: direction of the operation
  939. * @flags: tx descriptor status flags
  940. *
  941. * Prepares a descriptor for cyclic DMA operation. This means that once the
  942. * descriptor is submitted, we will be submitting in a @period_len sized
  943. * buffers and calling callback once the period has been elapsed. Transfer
  944. * terminates only when client calls dmaengine_terminate_all() for this
  945. * channel.
  946. *
  947. * Returns a valid DMA descriptor or %NULL in case of failure.
  948. */
  949. static struct dma_async_tx_descriptor *
  950. ep93xx_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
  951. size_t buf_len, size_t period_len,
  952. enum dma_transfer_direction dir, unsigned long flags)
  953. {
  954. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  955. struct ep93xx_dma_desc *desc, *first;
  956. size_t offset = 0;
  957. if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
  958. dev_warn(chan2dev(edmac),
  959. "channel was configured with different direction\n");
  960. return NULL;
  961. }
  962. if (test_and_set_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
  963. dev_warn(chan2dev(edmac),
  964. "channel is already used for cyclic transfers\n");
  965. return NULL;
  966. }
  967. if (period_len > DMA_MAX_CHAN_BYTES) {
  968. dev_warn(chan2dev(edmac), "too big period length %d\n",
  969. period_len);
  970. return NULL;
  971. }
  972. /* Split the buffer into period size chunks */
  973. first = NULL;
  974. for (offset = 0; offset < buf_len; offset += period_len) {
  975. desc = ep93xx_dma_desc_get(edmac);
  976. if (!desc) {
  977. dev_warn(chan2dev(edmac), "couln't get descriptor\n");
  978. goto fail;
  979. }
  980. if (dir == DMA_MEM_TO_DEV) {
  981. desc->src_addr = dma_addr + offset;
  982. desc->dst_addr = edmac->runtime_addr;
  983. } else {
  984. desc->src_addr = edmac->runtime_addr;
  985. desc->dst_addr = dma_addr + offset;
  986. }
  987. desc->size = period_len;
  988. if (!first)
  989. first = desc;
  990. else
  991. list_add_tail(&desc->node, &first->tx_list);
  992. }
  993. first->txd.cookie = -EBUSY;
  994. return &first->txd;
  995. fail:
  996. ep93xx_dma_desc_put(edmac, first);
  997. return NULL;
  998. }
  999. /**
  1000. * ep93xx_dma_terminate_all - terminate all transactions
  1001. * @chan: channel
  1002. *
  1003. * Stops all DMA transactions. All descriptors are put back to the
  1004. * @edmac->free_list and callbacks are _not_ called.
  1005. */
  1006. static int ep93xx_dma_terminate_all(struct dma_chan *chan)
  1007. {
  1008. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  1009. struct ep93xx_dma_desc *desc, *_d;
  1010. unsigned long flags;
  1011. LIST_HEAD(list);
  1012. spin_lock_irqsave(&edmac->lock, flags);
  1013. /* First we disable and flush the DMA channel */
  1014. edmac->edma->hw_shutdown(edmac);
  1015. clear_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags);
  1016. list_splice_init(&edmac->active, &list);
  1017. list_splice_init(&edmac->queue, &list);
  1018. /*
  1019. * We then re-enable the channel. This way we can continue submitting
  1020. * the descriptors by just calling ->hw_submit() again.
  1021. */
  1022. edmac->edma->hw_setup(edmac);
  1023. spin_unlock_irqrestore(&edmac->lock, flags);
  1024. list_for_each_entry_safe(desc, _d, &list, node)
  1025. ep93xx_dma_desc_put(edmac, desc);
  1026. return 0;
  1027. }
  1028. static int ep93xx_dma_slave_config(struct dma_chan *chan,
  1029. struct dma_slave_config *config)
  1030. {
  1031. struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
  1032. enum dma_slave_buswidth width;
  1033. unsigned long flags;
  1034. u32 addr, ctrl;
  1035. if (!edmac->edma->m2m)
  1036. return -EINVAL;
  1037. switch (config->direction) {
  1038. case DMA_DEV_TO_MEM:
  1039. width = config->src_addr_width;
  1040. addr = config->src_addr;
  1041. break;
  1042. case DMA_MEM_TO_DEV:
  1043. width = config->dst_addr_width;
  1044. addr = config->dst_addr;
  1045. break;
  1046. default:
  1047. return -EINVAL;
  1048. }
  1049. switch (width) {
  1050. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1051. ctrl = 0;
  1052. break;
  1053. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1054. ctrl = M2M_CONTROL_PW_16;
  1055. break;
  1056. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1057. ctrl = M2M_CONTROL_PW_32;
  1058. break;
  1059. default:
  1060. return -EINVAL;
  1061. }
  1062. spin_lock_irqsave(&edmac->lock, flags);
  1063. edmac->runtime_addr = addr;
  1064. edmac->runtime_ctrl = ctrl;
  1065. spin_unlock_irqrestore(&edmac->lock, flags);
  1066. return 0;
  1067. }
  1068. /**
  1069. * ep93xx_dma_tx_status - check if a transaction is completed
  1070. * @chan: channel
  1071. * @cookie: transaction specific cookie
  1072. * @state: state of the transaction is stored here if given
  1073. *
  1074. * This function can be used to query state of a given transaction.
  1075. */
  1076. static enum dma_status ep93xx_dma_tx_status(struct dma_chan *chan,
  1077. dma_cookie_t cookie,
  1078. struct dma_tx_state *state)
  1079. {
  1080. return dma_cookie_status(chan, cookie, state);
  1081. }
  1082. /**
  1083. * ep93xx_dma_issue_pending - push pending transactions to the hardware
  1084. * @chan: channel
  1085. *
  1086. * When this function is called, all pending transactions are pushed to the
  1087. * hardware and executed.
  1088. */
  1089. static void ep93xx_dma_issue_pending(struct dma_chan *chan)
  1090. {
  1091. ep93xx_dma_advance_work(to_ep93xx_dma_chan(chan));
  1092. }
  1093. static int __init ep93xx_dma_probe(struct platform_device *pdev)
  1094. {
  1095. struct ep93xx_dma_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1096. struct ep93xx_dma_engine *edma;
  1097. struct dma_device *dma_dev;
  1098. size_t edma_size;
  1099. int ret, i;
  1100. edma_size = pdata->num_channels * sizeof(struct ep93xx_dma_chan);
  1101. edma = kzalloc(sizeof(*edma) + edma_size, GFP_KERNEL);
  1102. if (!edma)
  1103. return -ENOMEM;
  1104. dma_dev = &edma->dma_dev;
  1105. edma->m2m = platform_get_device_id(pdev)->driver_data;
  1106. edma->num_channels = pdata->num_channels;
  1107. INIT_LIST_HEAD(&dma_dev->channels);
  1108. for (i = 0; i < pdata->num_channels; i++) {
  1109. const struct ep93xx_dma_chan_data *cdata = &pdata->channels[i];
  1110. struct ep93xx_dma_chan *edmac = &edma->channels[i];
  1111. edmac->chan.device = dma_dev;
  1112. edmac->regs = cdata->base;
  1113. edmac->irq = cdata->irq;
  1114. edmac->edma = edma;
  1115. edmac->clk = clk_get(NULL, cdata->name);
  1116. if (IS_ERR(edmac->clk)) {
  1117. dev_warn(&pdev->dev, "failed to get clock for %s\n",
  1118. cdata->name);
  1119. continue;
  1120. }
  1121. spin_lock_init(&edmac->lock);
  1122. INIT_LIST_HEAD(&edmac->active);
  1123. INIT_LIST_HEAD(&edmac->queue);
  1124. INIT_LIST_HEAD(&edmac->free_list);
  1125. tasklet_init(&edmac->tasklet, ep93xx_dma_tasklet,
  1126. (unsigned long)edmac);
  1127. list_add_tail(&edmac->chan.device_node,
  1128. &dma_dev->channels);
  1129. }
  1130. dma_cap_zero(dma_dev->cap_mask);
  1131. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  1132. dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
  1133. dma_dev->dev = &pdev->dev;
  1134. dma_dev->device_alloc_chan_resources = ep93xx_dma_alloc_chan_resources;
  1135. dma_dev->device_free_chan_resources = ep93xx_dma_free_chan_resources;
  1136. dma_dev->device_prep_slave_sg = ep93xx_dma_prep_slave_sg;
  1137. dma_dev->device_prep_dma_cyclic = ep93xx_dma_prep_dma_cyclic;
  1138. dma_dev->device_config = ep93xx_dma_slave_config;
  1139. dma_dev->device_terminate_all = ep93xx_dma_terminate_all;
  1140. dma_dev->device_issue_pending = ep93xx_dma_issue_pending;
  1141. dma_dev->device_tx_status = ep93xx_dma_tx_status;
  1142. dma_set_max_seg_size(dma_dev->dev, DMA_MAX_CHAN_BYTES);
  1143. if (edma->m2m) {
  1144. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  1145. dma_dev->device_prep_dma_memcpy = ep93xx_dma_prep_dma_memcpy;
  1146. edma->hw_setup = m2m_hw_setup;
  1147. edma->hw_shutdown = m2m_hw_shutdown;
  1148. edma->hw_submit = m2m_hw_submit;
  1149. edma->hw_interrupt = m2m_hw_interrupt;
  1150. } else {
  1151. dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
  1152. edma->hw_setup = m2p_hw_setup;
  1153. edma->hw_shutdown = m2p_hw_shutdown;
  1154. edma->hw_submit = m2p_hw_submit;
  1155. edma->hw_interrupt = m2p_hw_interrupt;
  1156. }
  1157. ret = dma_async_device_register(dma_dev);
  1158. if (unlikely(ret)) {
  1159. for (i = 0; i < edma->num_channels; i++) {
  1160. struct ep93xx_dma_chan *edmac = &edma->channels[i];
  1161. if (!IS_ERR_OR_NULL(edmac->clk))
  1162. clk_put(edmac->clk);
  1163. }
  1164. kfree(edma);
  1165. } else {
  1166. dev_info(dma_dev->dev, "EP93xx M2%s DMA ready\n",
  1167. edma->m2m ? "M" : "P");
  1168. }
  1169. return ret;
  1170. }
  1171. static const struct platform_device_id ep93xx_dma_driver_ids[] = {
  1172. { "ep93xx-dma-m2p", 0 },
  1173. { "ep93xx-dma-m2m", 1 },
  1174. { },
  1175. };
  1176. static struct platform_driver ep93xx_dma_driver = {
  1177. .driver = {
  1178. .name = "ep93xx-dma",
  1179. },
  1180. .id_table = ep93xx_dma_driver_ids,
  1181. };
  1182. static int __init ep93xx_dma_module_init(void)
  1183. {
  1184. return platform_driver_probe(&ep93xx_dma_driver, ep93xx_dma_probe);
  1185. }
  1186. subsys_initcall(ep93xx_dma_module_init);
  1187. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  1188. MODULE_DESCRIPTION("EP93xx DMA driver");
  1189. MODULE_LICENSE("GPL");