ep93xx_dma.c 36 KB

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