ep93xx_dma.c 37 KB

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