ep93xx_dma.c 39 KB

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