ep93xx_dma.c 38 KB

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