ep93xx_dma.c 36 KB

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