ep93xx_dma.c 39 KB

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