at_hdmac.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  1. /*
  2. * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems)
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. *
  12. * This supports the Atmel AHB DMA Controller found in several Atmel SoCs.
  13. * The only Atmel DMA Controller that is not covered by this driver is the one
  14. * found on AT91SAM9263.
  15. */
  16. #include <dt-bindings/dma/at91.h>
  17. #include <linux/clk.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/dmapool.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_dma.h>
  28. #include "at_hdmac_regs.h"
  29. #include "dmaengine.h"
  30. /*
  31. * Glossary
  32. * --------
  33. *
  34. * at_hdmac : Name of the ATmel AHB DMA Controller
  35. * at_dma_ / atdma : ATmel DMA controller entity related
  36. * atc_ / atchan : ATmel DMA Channel entity related
  37. */
  38. #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
  39. #define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
  40. |ATC_DIF(AT_DMA_MEM_IF))
  41. #define ATC_DMA_BUSWIDTHS\
  42. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  43. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  44. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  45. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  46. #define ATC_MAX_DSCR_TRIALS 10
  47. /*
  48. * Initial number of descriptors to allocate for each channel. This could
  49. * be increased during dma usage.
  50. */
  51. static unsigned int init_nr_desc_per_channel = 64;
  52. module_param(init_nr_desc_per_channel, uint, 0644);
  53. MODULE_PARM_DESC(init_nr_desc_per_channel,
  54. "initial descriptors per channel (default: 64)");
  55. /* prototypes */
  56. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
  57. static void atc_issue_pending(struct dma_chan *chan);
  58. /*----------------------------------------------------------------------*/
  59. static inline unsigned int atc_get_xfer_width(dma_addr_t src, dma_addr_t dst,
  60. size_t len)
  61. {
  62. unsigned int width;
  63. if (!((src | dst | len) & 3))
  64. width = 2;
  65. else if (!((src | dst | len) & 1))
  66. width = 1;
  67. else
  68. width = 0;
  69. return width;
  70. }
  71. static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
  72. {
  73. return list_first_entry(&atchan->active_list,
  74. struct at_desc, desc_node);
  75. }
  76. static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
  77. {
  78. return list_first_entry(&atchan->queue,
  79. struct at_desc, desc_node);
  80. }
  81. /**
  82. * atc_alloc_descriptor - allocate and return an initialized descriptor
  83. * @chan: the channel to allocate descriptors for
  84. * @gfp_flags: GFP allocation flags
  85. *
  86. * Note: The ack-bit is positioned in the descriptor flag at creation time
  87. * to make initial allocation more convenient. This bit will be cleared
  88. * and control will be given to client at usage time (during
  89. * preparation functions).
  90. */
  91. static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
  92. gfp_t gfp_flags)
  93. {
  94. struct at_desc *desc = NULL;
  95. struct at_dma *atdma = to_at_dma(chan->device);
  96. dma_addr_t phys;
  97. desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
  98. if (desc) {
  99. memset(desc, 0, sizeof(struct at_desc));
  100. INIT_LIST_HEAD(&desc->tx_list);
  101. dma_async_tx_descriptor_init(&desc->txd, chan);
  102. /* txd.flags will be overwritten in prep functions */
  103. desc->txd.flags = DMA_CTRL_ACK;
  104. desc->txd.tx_submit = atc_tx_submit;
  105. desc->txd.phys = phys;
  106. }
  107. return desc;
  108. }
  109. /**
  110. * atc_desc_get - get an unused descriptor from free_list
  111. * @atchan: channel we want a new descriptor for
  112. */
  113. static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
  114. {
  115. struct at_desc *desc, *_desc;
  116. struct at_desc *ret = NULL;
  117. unsigned long flags;
  118. unsigned int i = 0;
  119. LIST_HEAD(tmp_list);
  120. spin_lock_irqsave(&atchan->lock, flags);
  121. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  122. i++;
  123. if (async_tx_test_ack(&desc->txd)) {
  124. list_del(&desc->desc_node);
  125. ret = desc;
  126. break;
  127. }
  128. dev_dbg(chan2dev(&atchan->chan_common),
  129. "desc %p not ACKed\n", desc);
  130. }
  131. spin_unlock_irqrestore(&atchan->lock, flags);
  132. dev_vdbg(chan2dev(&atchan->chan_common),
  133. "scanned %u descriptors on freelist\n", i);
  134. /* no more descriptor available in initial pool: create one more */
  135. if (!ret) {
  136. ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
  137. if (ret) {
  138. spin_lock_irqsave(&atchan->lock, flags);
  139. atchan->descs_allocated++;
  140. spin_unlock_irqrestore(&atchan->lock, flags);
  141. } else {
  142. dev_err(chan2dev(&atchan->chan_common),
  143. "not enough descriptors available\n");
  144. }
  145. }
  146. return ret;
  147. }
  148. /**
  149. * atc_desc_put - move a descriptor, including any children, to the free list
  150. * @atchan: channel we work on
  151. * @desc: descriptor, at the head of a chain, to move to free list
  152. */
  153. static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
  154. {
  155. if (desc) {
  156. struct at_desc *child;
  157. unsigned long flags;
  158. spin_lock_irqsave(&atchan->lock, flags);
  159. list_for_each_entry(child, &desc->tx_list, desc_node)
  160. dev_vdbg(chan2dev(&atchan->chan_common),
  161. "moving child desc %p to freelist\n",
  162. child);
  163. list_splice_init(&desc->tx_list, &atchan->free_list);
  164. dev_vdbg(chan2dev(&atchan->chan_common),
  165. "moving desc %p to freelist\n", desc);
  166. list_add(&desc->desc_node, &atchan->free_list);
  167. spin_unlock_irqrestore(&atchan->lock, flags);
  168. }
  169. }
  170. /**
  171. * atc_desc_chain - build chain adding a descriptor
  172. * @first: address of first descriptor of the chain
  173. * @prev: address of previous descriptor of the chain
  174. * @desc: descriptor to queue
  175. *
  176. * Called from prep_* functions
  177. */
  178. static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
  179. struct at_desc *desc)
  180. {
  181. if (!(*first)) {
  182. *first = desc;
  183. } else {
  184. /* inform the HW lli about chaining */
  185. (*prev)->lli.dscr = desc->txd.phys;
  186. /* insert the link descriptor to the LD ring */
  187. list_add_tail(&desc->desc_node,
  188. &(*first)->tx_list);
  189. }
  190. *prev = desc;
  191. }
  192. /**
  193. * atc_dostart - starts the DMA engine for real
  194. * @atchan: the channel we want to start
  195. * @first: first descriptor in the list we want to begin with
  196. *
  197. * Called with atchan->lock held and bh disabled
  198. */
  199. static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
  200. {
  201. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  202. /* ASSERT: channel is idle */
  203. if (atc_chan_is_enabled(atchan)) {
  204. dev_err(chan2dev(&atchan->chan_common),
  205. "BUG: Attempted to start non-idle channel\n");
  206. dev_err(chan2dev(&atchan->chan_common),
  207. " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
  208. channel_readl(atchan, SADDR),
  209. channel_readl(atchan, DADDR),
  210. channel_readl(atchan, CTRLA),
  211. channel_readl(atchan, CTRLB),
  212. channel_readl(atchan, DSCR));
  213. /* The tasklet will hopefully advance the queue... */
  214. return;
  215. }
  216. vdbg_dump_regs(atchan);
  217. channel_writel(atchan, SADDR, 0);
  218. channel_writel(atchan, DADDR, 0);
  219. channel_writel(atchan, CTRLA, 0);
  220. channel_writel(atchan, CTRLB, 0);
  221. channel_writel(atchan, DSCR, first->txd.phys);
  222. channel_writel(atchan, SPIP, ATC_SPIP_HOLE(first->src_hole) |
  223. ATC_SPIP_BOUNDARY(first->boundary));
  224. channel_writel(atchan, DPIP, ATC_DPIP_HOLE(first->dst_hole) |
  225. ATC_DPIP_BOUNDARY(first->boundary));
  226. dma_writel(atdma, CHER, atchan->mask);
  227. vdbg_dump_regs(atchan);
  228. }
  229. /*
  230. * atc_get_desc_by_cookie - get the descriptor of a cookie
  231. * @atchan: the DMA channel
  232. * @cookie: the cookie to get the descriptor for
  233. */
  234. static struct at_desc *atc_get_desc_by_cookie(struct at_dma_chan *atchan,
  235. dma_cookie_t cookie)
  236. {
  237. struct at_desc *desc, *_desc;
  238. list_for_each_entry_safe(desc, _desc, &atchan->queue, desc_node) {
  239. if (desc->txd.cookie == cookie)
  240. return desc;
  241. }
  242. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  243. if (desc->txd.cookie == cookie)
  244. return desc;
  245. }
  246. return NULL;
  247. }
  248. /**
  249. * atc_calc_bytes_left - calculates the number of bytes left according to the
  250. * value read from CTRLA.
  251. *
  252. * @current_len: the number of bytes left before reading CTRLA
  253. * @ctrla: the value of CTRLA
  254. */
  255. static inline int atc_calc_bytes_left(int current_len, u32 ctrla)
  256. {
  257. u32 btsize = (ctrla & ATC_BTSIZE_MAX);
  258. u32 src_width = ATC_REG_TO_SRC_WIDTH(ctrla);
  259. /*
  260. * According to the datasheet, when reading the Control A Register
  261. * (ctrla), the Buffer Transfer Size (btsize) bitfield refers to the
  262. * number of transfers completed on the Source Interface.
  263. * So btsize is always a number of source width transfers.
  264. */
  265. return current_len - (btsize << src_width);
  266. }
  267. /**
  268. * atc_get_bytes_left - get the number of bytes residue for a cookie
  269. * @chan: DMA channel
  270. * @cookie: transaction identifier to check status of
  271. */
  272. static int atc_get_bytes_left(struct dma_chan *chan, dma_cookie_t cookie)
  273. {
  274. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  275. struct at_desc *desc_first = atc_first_active(atchan);
  276. struct at_desc *desc;
  277. int ret;
  278. u32 ctrla, dscr, trials;
  279. /*
  280. * If the cookie doesn't match to the currently running transfer then
  281. * we can return the total length of the associated DMA transfer,
  282. * because it is still queued.
  283. */
  284. desc = atc_get_desc_by_cookie(atchan, cookie);
  285. if (desc == NULL)
  286. return -EINVAL;
  287. else if (desc != desc_first)
  288. return desc->total_len;
  289. /* cookie matches to the currently running transfer */
  290. ret = desc_first->total_len;
  291. if (desc_first->lli.dscr) {
  292. /* hardware linked list transfer */
  293. /*
  294. * Calculate the residue by removing the length of the child
  295. * descriptors already transferred from the total length.
  296. * To get the current child descriptor we can use the value of
  297. * the channel's DSCR register and compare it against the value
  298. * of the hardware linked list structure of each child
  299. * descriptor.
  300. *
  301. * The CTRLA register provides us with the amount of data
  302. * already read from the source for the current child
  303. * descriptor. So we can compute a more accurate residue by also
  304. * removing the number of bytes corresponding to this amount of
  305. * data.
  306. *
  307. * However, the DSCR and CTRLA registers cannot be read both
  308. * atomically. Hence a race condition may occur: the first read
  309. * register may refer to one child descriptor whereas the second
  310. * read may refer to a later child descriptor in the list
  311. * because of the DMA transfer progression inbetween the two
  312. * reads.
  313. *
  314. * One solution could have been to pause the DMA transfer, read
  315. * the DSCR and CTRLA then resume the DMA transfer. Nonetheless,
  316. * this approach presents some drawbacks:
  317. * - If the DMA transfer is paused, RX overruns or TX underruns
  318. * are more likey to occur depending on the system latency.
  319. * Taking the USART driver as an example, it uses a cyclic DMA
  320. * transfer to read data from the Receive Holding Register
  321. * (RHR) to avoid RX overruns since the RHR is not protected
  322. * by any FIFO on most Atmel SoCs. So pausing the DMA transfer
  323. * to compute the residue would break the USART driver design.
  324. * - The atc_pause() function masks interrupts but we'd rather
  325. * avoid to do so for system latency purpose.
  326. *
  327. * Then we'd rather use another solution: the DSCR is read a
  328. * first time, the CTRLA is read in turn, next the DSCR is read
  329. * a second time. If the two consecutive read values of the DSCR
  330. * are the same then we assume both refers to the very same
  331. * child descriptor as well as the CTRLA value read inbetween
  332. * does. For cyclic tranfers, the assumption is that a full loop
  333. * is "not so fast".
  334. * If the two DSCR values are different, we read again the CTRLA
  335. * then the DSCR till two consecutive read values from DSCR are
  336. * equal or till the maxium trials is reach.
  337. * This algorithm is very unlikely not to find a stable value for
  338. * DSCR.
  339. */
  340. dscr = channel_readl(atchan, DSCR);
  341. rmb(); /* ensure DSCR is read before CTRLA */
  342. ctrla = channel_readl(atchan, CTRLA);
  343. for (trials = 0; trials < ATC_MAX_DSCR_TRIALS; ++trials) {
  344. u32 new_dscr;
  345. rmb(); /* ensure DSCR is read after CTRLA */
  346. new_dscr = channel_readl(atchan, DSCR);
  347. /*
  348. * If the DSCR register value has not changed inside the
  349. * DMA controller since the previous read, we assume
  350. * that both the dscr and ctrla values refers to the
  351. * very same descriptor.
  352. */
  353. if (likely(new_dscr == dscr))
  354. break;
  355. /*
  356. * DSCR has changed inside the DMA controller, so the
  357. * previouly read value of CTRLA may refer to an already
  358. * processed descriptor hence could be outdated.
  359. * We need to update ctrla to match the current
  360. * descriptor.
  361. */
  362. dscr = new_dscr;
  363. rmb(); /* ensure DSCR is read before CTRLA */
  364. ctrla = channel_readl(atchan, CTRLA);
  365. }
  366. if (unlikely(trials >= ATC_MAX_DSCR_TRIALS))
  367. return -ETIMEDOUT;
  368. /* for the first descriptor we can be more accurate */
  369. if (desc_first->lli.dscr == dscr)
  370. return atc_calc_bytes_left(ret, ctrla);
  371. ret -= desc_first->len;
  372. list_for_each_entry(desc, &desc_first->tx_list, desc_node) {
  373. if (desc->lli.dscr == dscr)
  374. break;
  375. ret -= desc->len;
  376. }
  377. /*
  378. * For the current descriptor in the chain we can calculate
  379. * the remaining bytes using the channel's register.
  380. */
  381. ret = atc_calc_bytes_left(ret, ctrla);
  382. } else {
  383. /* single transfer */
  384. ctrla = channel_readl(atchan, CTRLA);
  385. ret = atc_calc_bytes_left(ret, ctrla);
  386. }
  387. return ret;
  388. }
  389. /**
  390. * atc_chain_complete - finish work for one transaction chain
  391. * @atchan: channel we work on
  392. * @desc: descriptor at the head of the chain we want do complete
  393. *
  394. * Called with atchan->lock held and bh disabled */
  395. static void
  396. atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
  397. {
  398. struct dma_async_tx_descriptor *txd = &desc->txd;
  399. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  400. dev_vdbg(chan2dev(&atchan->chan_common),
  401. "descriptor %u complete\n", txd->cookie);
  402. /* mark the descriptor as complete for non cyclic cases only */
  403. if (!atc_chan_is_cyclic(atchan))
  404. dma_cookie_complete(txd);
  405. /* If the transfer was a memset, free our temporary buffer */
  406. if (desc->memset_buffer) {
  407. dma_pool_free(atdma->memset_pool, desc->memset_vaddr,
  408. desc->memset_paddr);
  409. desc->memset_buffer = false;
  410. }
  411. /* move children to free_list */
  412. list_splice_init(&desc->tx_list, &atchan->free_list);
  413. /* move myself to free_list */
  414. list_move(&desc->desc_node, &atchan->free_list);
  415. dma_descriptor_unmap(txd);
  416. /* for cyclic transfers,
  417. * no need to replay callback function while stopping */
  418. if (!atc_chan_is_cyclic(atchan)) {
  419. dma_async_tx_callback callback = txd->callback;
  420. void *param = txd->callback_param;
  421. /*
  422. * The API requires that no submissions are done from a
  423. * callback, so we don't need to drop the lock here
  424. */
  425. if (callback)
  426. callback(param);
  427. }
  428. dma_run_dependencies(txd);
  429. }
  430. /**
  431. * atc_complete_all - finish work for all transactions
  432. * @atchan: channel to complete transactions for
  433. *
  434. * Eventually submit queued descriptors if any
  435. *
  436. * Assume channel is idle while calling this function
  437. * Called with atchan->lock held and bh disabled
  438. */
  439. static void atc_complete_all(struct at_dma_chan *atchan)
  440. {
  441. struct at_desc *desc, *_desc;
  442. LIST_HEAD(list);
  443. dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
  444. /*
  445. * Submit queued descriptors ASAP, i.e. before we go through
  446. * the completed ones.
  447. */
  448. if (!list_empty(&atchan->queue))
  449. atc_dostart(atchan, atc_first_queued(atchan));
  450. /* empty active_list now it is completed */
  451. list_splice_init(&atchan->active_list, &list);
  452. /* empty queue list by moving descriptors (if any) to active_list */
  453. list_splice_init(&atchan->queue, &atchan->active_list);
  454. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  455. atc_chain_complete(atchan, desc);
  456. }
  457. /**
  458. * atc_advance_work - at the end of a transaction, move forward
  459. * @atchan: channel where the transaction ended
  460. *
  461. * Called with atchan->lock held and bh disabled
  462. */
  463. static void atc_advance_work(struct at_dma_chan *atchan)
  464. {
  465. dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
  466. if (atc_chan_is_enabled(atchan))
  467. return;
  468. if (list_empty(&atchan->active_list) ||
  469. list_is_singular(&atchan->active_list)) {
  470. atc_complete_all(atchan);
  471. } else {
  472. atc_chain_complete(atchan, atc_first_active(atchan));
  473. /* advance work */
  474. atc_dostart(atchan, atc_first_active(atchan));
  475. }
  476. }
  477. /**
  478. * atc_handle_error - handle errors reported by DMA controller
  479. * @atchan: channel where error occurs
  480. *
  481. * Called with atchan->lock held and bh disabled
  482. */
  483. static void atc_handle_error(struct at_dma_chan *atchan)
  484. {
  485. struct at_desc *bad_desc;
  486. struct at_desc *child;
  487. /*
  488. * The descriptor currently at the head of the active list is
  489. * broked. Since we don't have any way to report errors, we'll
  490. * just have to scream loudly and try to carry on.
  491. */
  492. bad_desc = atc_first_active(atchan);
  493. list_del_init(&bad_desc->desc_node);
  494. /* As we are stopped, take advantage to push queued descriptors
  495. * in active_list */
  496. list_splice_init(&atchan->queue, atchan->active_list.prev);
  497. /* Try to restart the controller */
  498. if (!list_empty(&atchan->active_list))
  499. atc_dostart(atchan, atc_first_active(atchan));
  500. /*
  501. * KERN_CRITICAL may seem harsh, but since this only happens
  502. * when someone submits a bad physical address in a
  503. * descriptor, we should consider ourselves lucky that the
  504. * controller flagged an error instead of scribbling over
  505. * random memory locations.
  506. */
  507. dev_crit(chan2dev(&atchan->chan_common),
  508. "Bad descriptor submitted for DMA!\n");
  509. dev_crit(chan2dev(&atchan->chan_common),
  510. " cookie: %d\n", bad_desc->txd.cookie);
  511. atc_dump_lli(atchan, &bad_desc->lli);
  512. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  513. atc_dump_lli(atchan, &child->lli);
  514. /* Pretend the descriptor completed successfully */
  515. atc_chain_complete(atchan, bad_desc);
  516. }
  517. /**
  518. * atc_handle_cyclic - at the end of a period, run callback function
  519. * @atchan: channel used for cyclic operations
  520. *
  521. * Called with atchan->lock held and bh disabled
  522. */
  523. static void atc_handle_cyclic(struct at_dma_chan *atchan)
  524. {
  525. struct at_desc *first = atc_first_active(atchan);
  526. struct dma_async_tx_descriptor *txd = &first->txd;
  527. dma_async_tx_callback callback = txd->callback;
  528. void *param = txd->callback_param;
  529. dev_vdbg(chan2dev(&atchan->chan_common),
  530. "new cyclic period llp 0x%08x\n",
  531. channel_readl(atchan, DSCR));
  532. if (callback)
  533. callback(param);
  534. }
  535. /*-- IRQ & Tasklet ---------------------------------------------------*/
  536. static void atc_tasklet(unsigned long data)
  537. {
  538. struct at_dma_chan *atchan = (struct at_dma_chan *)data;
  539. unsigned long flags;
  540. spin_lock_irqsave(&atchan->lock, flags);
  541. if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
  542. atc_handle_error(atchan);
  543. else if (atc_chan_is_cyclic(atchan))
  544. atc_handle_cyclic(atchan);
  545. else
  546. atc_advance_work(atchan);
  547. spin_unlock_irqrestore(&atchan->lock, flags);
  548. }
  549. static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
  550. {
  551. struct at_dma *atdma = (struct at_dma *)dev_id;
  552. struct at_dma_chan *atchan;
  553. int i;
  554. u32 status, pending, imr;
  555. int ret = IRQ_NONE;
  556. do {
  557. imr = dma_readl(atdma, EBCIMR);
  558. status = dma_readl(atdma, EBCISR);
  559. pending = status & imr;
  560. if (!pending)
  561. break;
  562. dev_vdbg(atdma->dma_common.dev,
  563. "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
  564. status, imr, pending);
  565. for (i = 0; i < atdma->dma_common.chancnt; i++) {
  566. atchan = &atdma->chan[i];
  567. if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
  568. if (pending & AT_DMA_ERR(i)) {
  569. /* Disable channel on AHB error */
  570. dma_writel(atdma, CHDR,
  571. AT_DMA_RES(i) | atchan->mask);
  572. /* Give information to tasklet */
  573. set_bit(ATC_IS_ERROR, &atchan->status);
  574. }
  575. tasklet_schedule(&atchan->tasklet);
  576. ret = IRQ_HANDLED;
  577. }
  578. }
  579. } while (pending);
  580. return ret;
  581. }
  582. /*-- DMA Engine API --------------------------------------------------*/
  583. /**
  584. * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
  585. * @desc: descriptor at the head of the transaction chain
  586. *
  587. * Queue chain if DMA engine is working already
  588. *
  589. * Cookie increment and adding to active_list or queue must be atomic
  590. */
  591. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
  592. {
  593. struct at_desc *desc = txd_to_at_desc(tx);
  594. struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
  595. dma_cookie_t cookie;
  596. unsigned long flags;
  597. spin_lock_irqsave(&atchan->lock, flags);
  598. cookie = dma_cookie_assign(tx);
  599. if (list_empty(&atchan->active_list)) {
  600. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  601. desc->txd.cookie);
  602. atc_dostart(atchan, desc);
  603. list_add_tail(&desc->desc_node, &atchan->active_list);
  604. } else {
  605. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  606. desc->txd.cookie);
  607. list_add_tail(&desc->desc_node, &atchan->queue);
  608. }
  609. spin_unlock_irqrestore(&atchan->lock, flags);
  610. return cookie;
  611. }
  612. /**
  613. * atc_prep_dma_interleaved - prepare memory to memory interleaved operation
  614. * @chan: the channel to prepare operation on
  615. * @xt: Interleaved transfer template
  616. * @flags: tx descriptor status flags
  617. */
  618. static struct dma_async_tx_descriptor *
  619. atc_prep_dma_interleaved(struct dma_chan *chan,
  620. struct dma_interleaved_template *xt,
  621. unsigned long flags)
  622. {
  623. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  624. struct data_chunk *first = xt->sgl;
  625. struct at_desc *desc = NULL;
  626. size_t xfer_count;
  627. unsigned int dwidth;
  628. u32 ctrla;
  629. u32 ctrlb;
  630. size_t len = 0;
  631. int i;
  632. if (unlikely(!xt || xt->numf != 1 || !xt->frame_size))
  633. return NULL;
  634. dev_info(chan2dev(chan),
  635. "%s: src=%pad, dest=%pad, numf=%d, frame_size=%d, flags=0x%lx\n",
  636. __func__, &xt->src_start, &xt->dst_start, xt->numf,
  637. xt->frame_size, flags);
  638. /*
  639. * The controller can only "skip" X bytes every Y bytes, so we
  640. * need to make sure we are given a template that fit that
  641. * description, ie a template with chunks that always have the
  642. * same size, with the same ICGs.
  643. */
  644. for (i = 0; i < xt->frame_size; i++) {
  645. struct data_chunk *chunk = xt->sgl + i;
  646. if ((chunk->size != xt->sgl->size) ||
  647. (dmaengine_get_dst_icg(xt, chunk) != dmaengine_get_dst_icg(xt, first)) ||
  648. (dmaengine_get_src_icg(xt, chunk) != dmaengine_get_src_icg(xt, first))) {
  649. dev_err(chan2dev(chan),
  650. "%s: the controller can transfer only identical chunks\n",
  651. __func__);
  652. return NULL;
  653. }
  654. len += chunk->size;
  655. }
  656. dwidth = atc_get_xfer_width(xt->src_start,
  657. xt->dst_start, len);
  658. xfer_count = len >> dwidth;
  659. if (xfer_count > ATC_BTSIZE_MAX) {
  660. dev_err(chan2dev(chan), "%s: buffer is too big\n", __func__);
  661. return NULL;
  662. }
  663. ctrla = ATC_SRC_WIDTH(dwidth) |
  664. ATC_DST_WIDTH(dwidth);
  665. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  666. | ATC_SRC_ADDR_MODE_INCR
  667. | ATC_DST_ADDR_MODE_INCR
  668. | ATC_SRC_PIP
  669. | ATC_DST_PIP
  670. | ATC_FC_MEM2MEM;
  671. /* create the transfer */
  672. desc = atc_desc_get(atchan);
  673. if (!desc) {
  674. dev_err(chan2dev(chan),
  675. "%s: couldn't allocate our descriptor\n", __func__);
  676. return NULL;
  677. }
  678. desc->lli.saddr = xt->src_start;
  679. desc->lli.daddr = xt->dst_start;
  680. desc->lli.ctrla = ctrla | xfer_count;
  681. desc->lli.ctrlb = ctrlb;
  682. desc->boundary = first->size >> dwidth;
  683. desc->dst_hole = (dmaengine_get_dst_icg(xt, first) >> dwidth) + 1;
  684. desc->src_hole = (dmaengine_get_src_icg(xt, first) >> dwidth) + 1;
  685. desc->txd.cookie = -EBUSY;
  686. desc->total_len = desc->len = len;
  687. /* set end-of-link to the last link descriptor of list*/
  688. set_desc_eol(desc);
  689. desc->txd.flags = flags; /* client is in control of this ack */
  690. return &desc->txd;
  691. }
  692. /**
  693. * atc_prep_dma_memcpy - prepare a memcpy operation
  694. * @chan: the channel to prepare operation on
  695. * @dest: operation virtual destination address
  696. * @src: operation virtual source address
  697. * @len: operation length
  698. * @flags: tx descriptor status flags
  699. */
  700. static struct dma_async_tx_descriptor *
  701. atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  702. size_t len, unsigned long flags)
  703. {
  704. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  705. struct at_desc *desc = NULL;
  706. struct at_desc *first = NULL;
  707. struct at_desc *prev = NULL;
  708. size_t xfer_count;
  709. size_t offset;
  710. unsigned int src_width;
  711. unsigned int dst_width;
  712. u32 ctrla;
  713. u32 ctrlb;
  714. dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d%pad s%pad l0x%zx f0x%lx\n",
  715. &dest, &src, len, flags);
  716. if (unlikely(!len)) {
  717. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  718. return NULL;
  719. }
  720. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  721. | ATC_SRC_ADDR_MODE_INCR
  722. | ATC_DST_ADDR_MODE_INCR
  723. | ATC_FC_MEM2MEM;
  724. /*
  725. * We can be a lot more clever here, but this should take care
  726. * of the most common optimization.
  727. */
  728. src_width = dst_width = atc_get_xfer_width(src, dest, len);
  729. ctrla = ATC_SRC_WIDTH(src_width) |
  730. ATC_DST_WIDTH(dst_width);
  731. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  732. xfer_count = min_t(size_t, (len - offset) >> src_width,
  733. ATC_BTSIZE_MAX);
  734. desc = atc_desc_get(atchan);
  735. if (!desc)
  736. goto err_desc_get;
  737. desc->lli.saddr = src + offset;
  738. desc->lli.daddr = dest + offset;
  739. desc->lli.ctrla = ctrla | xfer_count;
  740. desc->lli.ctrlb = ctrlb;
  741. desc->txd.cookie = 0;
  742. desc->len = xfer_count << src_width;
  743. atc_desc_chain(&first, &prev, desc);
  744. }
  745. /* First descriptor of the chain embedds additional information */
  746. first->txd.cookie = -EBUSY;
  747. first->total_len = len;
  748. /* set end-of-link to the last link descriptor of list*/
  749. set_desc_eol(desc);
  750. first->txd.flags = flags; /* client is in control of this ack */
  751. return &first->txd;
  752. err_desc_get:
  753. atc_desc_put(atchan, first);
  754. return NULL;
  755. }
  756. static struct at_desc *atc_create_memset_desc(struct dma_chan *chan,
  757. dma_addr_t psrc,
  758. dma_addr_t pdst,
  759. size_t len)
  760. {
  761. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  762. struct at_desc *desc;
  763. size_t xfer_count;
  764. u32 ctrla = ATC_SRC_WIDTH(2) | ATC_DST_WIDTH(2);
  765. u32 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
  766. ATC_SRC_ADDR_MODE_FIXED |
  767. ATC_DST_ADDR_MODE_INCR |
  768. ATC_FC_MEM2MEM;
  769. xfer_count = len >> 2;
  770. if (xfer_count > ATC_BTSIZE_MAX) {
  771. dev_err(chan2dev(chan), "%s: buffer is too big\n",
  772. __func__);
  773. return NULL;
  774. }
  775. desc = atc_desc_get(atchan);
  776. if (!desc) {
  777. dev_err(chan2dev(chan), "%s: can't get a descriptor\n",
  778. __func__);
  779. return NULL;
  780. }
  781. desc->lli.saddr = psrc;
  782. desc->lli.daddr = pdst;
  783. desc->lli.ctrla = ctrla | xfer_count;
  784. desc->lli.ctrlb = ctrlb;
  785. desc->txd.cookie = 0;
  786. desc->len = len;
  787. return desc;
  788. }
  789. /**
  790. * atc_prep_dma_memset - prepare a memcpy operation
  791. * @chan: the channel to prepare operation on
  792. * @dest: operation virtual destination address
  793. * @value: value to set memory buffer to
  794. * @len: operation length
  795. * @flags: tx descriptor status flags
  796. */
  797. static struct dma_async_tx_descriptor *
  798. atc_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  799. size_t len, unsigned long flags)
  800. {
  801. struct at_dma *atdma = to_at_dma(chan->device);
  802. struct at_desc *desc;
  803. void __iomem *vaddr;
  804. dma_addr_t paddr;
  805. dev_vdbg(chan2dev(chan), "%s: d%pad v0x%x l0x%zx f0x%lx\n", __func__,
  806. &dest, value, len, flags);
  807. if (unlikely(!len)) {
  808. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  809. return NULL;
  810. }
  811. if (!is_dma_fill_aligned(chan->device, dest, 0, len)) {
  812. dev_dbg(chan2dev(chan), "%s: buffer is not aligned\n",
  813. __func__);
  814. return NULL;
  815. }
  816. vaddr = dma_pool_alloc(atdma->memset_pool, GFP_ATOMIC, &paddr);
  817. if (!vaddr) {
  818. dev_err(chan2dev(chan), "%s: couldn't allocate buffer\n",
  819. __func__);
  820. return NULL;
  821. }
  822. *(u32*)vaddr = value;
  823. desc = atc_create_memset_desc(chan, paddr, dest, len);
  824. if (!desc) {
  825. dev_err(chan2dev(chan), "%s: couldn't get a descriptor\n",
  826. __func__);
  827. goto err_free_buffer;
  828. }
  829. desc->memset_paddr = paddr;
  830. desc->memset_vaddr = vaddr;
  831. desc->memset_buffer = true;
  832. desc->txd.cookie = -EBUSY;
  833. desc->total_len = len;
  834. /* set end-of-link on the descriptor */
  835. set_desc_eol(desc);
  836. desc->txd.flags = flags;
  837. return &desc->txd;
  838. err_free_buffer:
  839. dma_pool_free(atdma->memset_pool, vaddr, paddr);
  840. return NULL;
  841. }
  842. static struct dma_async_tx_descriptor *
  843. atc_prep_dma_memset_sg(struct dma_chan *chan,
  844. struct scatterlist *sgl,
  845. unsigned int sg_len, int value,
  846. unsigned long flags)
  847. {
  848. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  849. struct at_dma *atdma = to_at_dma(chan->device);
  850. struct at_desc *desc = NULL, *first = NULL, *prev = NULL;
  851. struct scatterlist *sg;
  852. void __iomem *vaddr;
  853. dma_addr_t paddr;
  854. size_t total_len = 0;
  855. int i;
  856. dev_vdbg(chan2dev(chan), "%s: v0x%x l0x%zx f0x%lx\n", __func__,
  857. value, sg_len, flags);
  858. if (unlikely(!sgl || !sg_len)) {
  859. dev_dbg(chan2dev(chan), "%s: scatterlist is empty!\n",
  860. __func__);
  861. return NULL;
  862. }
  863. vaddr = dma_pool_alloc(atdma->memset_pool, GFP_ATOMIC, &paddr);
  864. if (!vaddr) {
  865. dev_err(chan2dev(chan), "%s: couldn't allocate buffer\n",
  866. __func__);
  867. return NULL;
  868. }
  869. *(u32*)vaddr = value;
  870. for_each_sg(sgl, sg, sg_len, i) {
  871. dma_addr_t dest = sg_dma_address(sg);
  872. size_t len = sg_dma_len(sg);
  873. dev_vdbg(chan2dev(chan), "%s: d%pad, l0x%zx\n",
  874. __func__, &dest, len);
  875. if (!is_dma_fill_aligned(chan->device, dest, 0, len)) {
  876. dev_err(chan2dev(chan), "%s: buffer is not aligned\n",
  877. __func__);
  878. goto err_put_desc;
  879. }
  880. desc = atc_create_memset_desc(chan, paddr, dest, len);
  881. if (!desc)
  882. goto err_put_desc;
  883. atc_desc_chain(&first, &prev, desc);
  884. total_len += len;
  885. }
  886. /*
  887. * Only set the buffer pointers on the last descriptor to
  888. * avoid free'ing while we have our transfer still going
  889. */
  890. desc->memset_paddr = paddr;
  891. desc->memset_vaddr = vaddr;
  892. desc->memset_buffer = true;
  893. first->txd.cookie = -EBUSY;
  894. first->total_len = total_len;
  895. /* set end-of-link on the descriptor */
  896. set_desc_eol(desc);
  897. first->txd.flags = flags;
  898. return &first->txd;
  899. err_put_desc:
  900. atc_desc_put(atchan, first);
  901. return NULL;
  902. }
  903. /**
  904. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  905. * @chan: DMA channel
  906. * @sgl: scatterlist to transfer to/from
  907. * @sg_len: number of entries in @scatterlist
  908. * @direction: DMA direction
  909. * @flags: tx descriptor status flags
  910. * @context: transaction context (ignored)
  911. */
  912. static struct dma_async_tx_descriptor *
  913. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  914. unsigned int sg_len, enum dma_transfer_direction direction,
  915. unsigned long flags, void *context)
  916. {
  917. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  918. struct at_dma_slave *atslave = chan->private;
  919. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  920. struct at_desc *first = NULL;
  921. struct at_desc *prev = NULL;
  922. u32 ctrla;
  923. u32 ctrlb;
  924. dma_addr_t reg;
  925. unsigned int reg_width;
  926. unsigned int mem_width;
  927. unsigned int i;
  928. struct scatterlist *sg;
  929. size_t total_len = 0;
  930. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  931. sg_len,
  932. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  933. flags);
  934. if (unlikely(!atslave || !sg_len)) {
  935. dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
  936. return NULL;
  937. }
  938. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  939. | ATC_DCSIZE(sconfig->dst_maxburst);
  940. ctrlb = ATC_IEN;
  941. switch (direction) {
  942. case DMA_MEM_TO_DEV:
  943. reg_width = convert_buswidth(sconfig->dst_addr_width);
  944. ctrla |= ATC_DST_WIDTH(reg_width);
  945. ctrlb |= ATC_DST_ADDR_MODE_FIXED
  946. | ATC_SRC_ADDR_MODE_INCR
  947. | ATC_FC_MEM2PER
  948. | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
  949. reg = sconfig->dst_addr;
  950. for_each_sg(sgl, sg, sg_len, i) {
  951. struct at_desc *desc;
  952. u32 len;
  953. u32 mem;
  954. desc = atc_desc_get(atchan);
  955. if (!desc)
  956. goto err_desc_get;
  957. mem = sg_dma_address(sg);
  958. len = sg_dma_len(sg);
  959. if (unlikely(!len)) {
  960. dev_dbg(chan2dev(chan),
  961. "prep_slave_sg: sg(%d) data length is zero\n", i);
  962. goto err;
  963. }
  964. mem_width = 2;
  965. if (unlikely(mem & 3 || len & 3))
  966. mem_width = 0;
  967. desc->lli.saddr = mem;
  968. desc->lli.daddr = reg;
  969. desc->lli.ctrla = ctrla
  970. | ATC_SRC_WIDTH(mem_width)
  971. | len >> mem_width;
  972. desc->lli.ctrlb = ctrlb;
  973. desc->len = len;
  974. atc_desc_chain(&first, &prev, desc);
  975. total_len += len;
  976. }
  977. break;
  978. case DMA_DEV_TO_MEM:
  979. reg_width = convert_buswidth(sconfig->src_addr_width);
  980. ctrla |= ATC_SRC_WIDTH(reg_width);
  981. ctrlb |= ATC_DST_ADDR_MODE_INCR
  982. | ATC_SRC_ADDR_MODE_FIXED
  983. | ATC_FC_PER2MEM
  984. | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
  985. reg = sconfig->src_addr;
  986. for_each_sg(sgl, sg, sg_len, i) {
  987. struct at_desc *desc;
  988. u32 len;
  989. u32 mem;
  990. desc = atc_desc_get(atchan);
  991. if (!desc)
  992. goto err_desc_get;
  993. mem = sg_dma_address(sg);
  994. len = sg_dma_len(sg);
  995. if (unlikely(!len)) {
  996. dev_dbg(chan2dev(chan),
  997. "prep_slave_sg: sg(%d) data length is zero\n", i);
  998. goto err;
  999. }
  1000. mem_width = 2;
  1001. if (unlikely(mem & 3 || len & 3))
  1002. mem_width = 0;
  1003. desc->lli.saddr = reg;
  1004. desc->lli.daddr = mem;
  1005. desc->lli.ctrla = ctrla
  1006. | ATC_DST_WIDTH(mem_width)
  1007. | len >> reg_width;
  1008. desc->lli.ctrlb = ctrlb;
  1009. desc->len = len;
  1010. atc_desc_chain(&first, &prev, desc);
  1011. total_len += len;
  1012. }
  1013. break;
  1014. default:
  1015. return NULL;
  1016. }
  1017. /* set end-of-link to the last link descriptor of list*/
  1018. set_desc_eol(prev);
  1019. /* First descriptor of the chain embedds additional information */
  1020. first->txd.cookie = -EBUSY;
  1021. first->total_len = total_len;
  1022. /* first link descriptor of list is responsible of flags */
  1023. first->txd.flags = flags; /* client is in control of this ack */
  1024. return &first->txd;
  1025. err_desc_get:
  1026. dev_err(chan2dev(chan), "not enough descriptors available\n");
  1027. err:
  1028. atc_desc_put(atchan, first);
  1029. return NULL;
  1030. }
  1031. /**
  1032. * atc_prep_dma_sg - prepare memory to memory scather-gather operation
  1033. * @chan: the channel to prepare operation on
  1034. * @dst_sg: destination scatterlist
  1035. * @dst_nents: number of destination scatterlist entries
  1036. * @src_sg: source scatterlist
  1037. * @src_nents: number of source scatterlist entries
  1038. * @flags: tx descriptor status flags
  1039. */
  1040. static struct dma_async_tx_descriptor *
  1041. atc_prep_dma_sg(struct dma_chan *chan,
  1042. struct scatterlist *dst_sg, unsigned int dst_nents,
  1043. struct scatterlist *src_sg, unsigned int src_nents,
  1044. unsigned long flags)
  1045. {
  1046. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1047. struct at_desc *desc = NULL;
  1048. struct at_desc *first = NULL;
  1049. struct at_desc *prev = NULL;
  1050. unsigned int src_width;
  1051. unsigned int dst_width;
  1052. size_t xfer_count;
  1053. u32 ctrla;
  1054. u32 ctrlb;
  1055. size_t dst_len = 0, src_len = 0;
  1056. dma_addr_t dst = 0, src = 0;
  1057. size_t len = 0, total_len = 0;
  1058. if (unlikely(dst_nents == 0 || src_nents == 0))
  1059. return NULL;
  1060. if (unlikely(dst_sg == NULL || src_sg == NULL))
  1061. return NULL;
  1062. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  1063. | ATC_SRC_ADDR_MODE_INCR
  1064. | ATC_DST_ADDR_MODE_INCR
  1065. | ATC_FC_MEM2MEM;
  1066. /*
  1067. * loop until there is either no more source or no more destination
  1068. * scatterlist entry
  1069. */
  1070. while (true) {
  1071. /* prepare the next transfer */
  1072. if (dst_len == 0) {
  1073. /* no more destination scatterlist entries */
  1074. if (!dst_sg || !dst_nents)
  1075. break;
  1076. dst = sg_dma_address(dst_sg);
  1077. dst_len = sg_dma_len(dst_sg);
  1078. dst_sg = sg_next(dst_sg);
  1079. dst_nents--;
  1080. }
  1081. if (src_len == 0) {
  1082. /* no more source scatterlist entries */
  1083. if (!src_sg || !src_nents)
  1084. break;
  1085. src = sg_dma_address(src_sg);
  1086. src_len = sg_dma_len(src_sg);
  1087. src_sg = sg_next(src_sg);
  1088. src_nents--;
  1089. }
  1090. len = min_t(size_t, src_len, dst_len);
  1091. if (len == 0)
  1092. continue;
  1093. /* take care for the alignment */
  1094. src_width = dst_width = atc_get_xfer_width(src, dst, len);
  1095. ctrla = ATC_SRC_WIDTH(src_width) |
  1096. ATC_DST_WIDTH(dst_width);
  1097. /*
  1098. * The number of transfers to set up refer to the source width
  1099. * that depends on the alignment.
  1100. */
  1101. xfer_count = len >> src_width;
  1102. if (xfer_count > ATC_BTSIZE_MAX) {
  1103. xfer_count = ATC_BTSIZE_MAX;
  1104. len = ATC_BTSIZE_MAX << src_width;
  1105. }
  1106. /* create the transfer */
  1107. desc = atc_desc_get(atchan);
  1108. if (!desc)
  1109. goto err_desc_get;
  1110. desc->lli.saddr = src;
  1111. desc->lli.daddr = dst;
  1112. desc->lli.ctrla = ctrla | xfer_count;
  1113. desc->lli.ctrlb = ctrlb;
  1114. desc->txd.cookie = 0;
  1115. desc->len = len;
  1116. atc_desc_chain(&first, &prev, desc);
  1117. /* update the lengths and addresses for the next loop cycle */
  1118. dst_len -= len;
  1119. src_len -= len;
  1120. dst += len;
  1121. src += len;
  1122. total_len += len;
  1123. }
  1124. /* First descriptor of the chain embedds additional information */
  1125. first->txd.cookie = -EBUSY;
  1126. first->total_len = total_len;
  1127. /* set end-of-link to the last link descriptor of list*/
  1128. set_desc_eol(desc);
  1129. first->txd.flags = flags; /* client is in control of this ack */
  1130. return &first->txd;
  1131. err_desc_get:
  1132. atc_desc_put(atchan, first);
  1133. return NULL;
  1134. }
  1135. /**
  1136. * atc_dma_cyclic_check_values
  1137. * Check for too big/unaligned periods and unaligned DMA buffer
  1138. */
  1139. static int
  1140. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  1141. size_t period_len)
  1142. {
  1143. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  1144. goto err_out;
  1145. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1146. goto err_out;
  1147. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1148. goto err_out;
  1149. return 0;
  1150. err_out:
  1151. return -EINVAL;
  1152. }
  1153. /**
  1154. * atc_dma_cyclic_fill_desc - Fill one period descriptor
  1155. */
  1156. static int
  1157. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  1158. unsigned int period_index, dma_addr_t buf_addr,
  1159. unsigned int reg_width, size_t period_len,
  1160. enum dma_transfer_direction direction)
  1161. {
  1162. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1163. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1164. u32 ctrla;
  1165. /* prepare common CRTLA value */
  1166. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  1167. | ATC_DCSIZE(sconfig->dst_maxburst)
  1168. | ATC_DST_WIDTH(reg_width)
  1169. | ATC_SRC_WIDTH(reg_width)
  1170. | period_len >> reg_width;
  1171. switch (direction) {
  1172. case DMA_MEM_TO_DEV:
  1173. desc->lli.saddr = buf_addr + (period_len * period_index);
  1174. desc->lli.daddr = sconfig->dst_addr;
  1175. desc->lli.ctrla = ctrla;
  1176. desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
  1177. | ATC_SRC_ADDR_MODE_INCR
  1178. | ATC_FC_MEM2PER
  1179. | ATC_SIF(atchan->mem_if)
  1180. | ATC_DIF(atchan->per_if);
  1181. desc->len = period_len;
  1182. break;
  1183. case DMA_DEV_TO_MEM:
  1184. desc->lli.saddr = sconfig->src_addr;
  1185. desc->lli.daddr = buf_addr + (period_len * period_index);
  1186. desc->lli.ctrla = ctrla;
  1187. desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
  1188. | ATC_SRC_ADDR_MODE_FIXED
  1189. | ATC_FC_PER2MEM
  1190. | ATC_SIF(atchan->per_if)
  1191. | ATC_DIF(atchan->mem_if);
  1192. desc->len = period_len;
  1193. break;
  1194. default:
  1195. return -EINVAL;
  1196. }
  1197. return 0;
  1198. }
  1199. /**
  1200. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  1201. * @chan: the DMA channel to prepare
  1202. * @buf_addr: physical DMA address where the buffer starts
  1203. * @buf_len: total number of bytes for the entire buffer
  1204. * @period_len: number of bytes for each period
  1205. * @direction: transfer direction, to or from device
  1206. * @flags: tx descriptor status flags
  1207. */
  1208. static struct dma_async_tx_descriptor *
  1209. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1210. size_t period_len, enum dma_transfer_direction direction,
  1211. unsigned long flags)
  1212. {
  1213. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1214. struct at_dma_slave *atslave = chan->private;
  1215. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1216. struct at_desc *first = NULL;
  1217. struct at_desc *prev = NULL;
  1218. unsigned long was_cyclic;
  1219. unsigned int reg_width;
  1220. unsigned int periods = buf_len / period_len;
  1221. unsigned int i;
  1222. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@%pad - %d (%d/%d)\n",
  1223. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  1224. &buf_addr,
  1225. periods, buf_len, period_len);
  1226. if (unlikely(!atslave || !buf_len || !period_len)) {
  1227. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  1228. return NULL;
  1229. }
  1230. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  1231. if (was_cyclic) {
  1232. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  1233. return NULL;
  1234. }
  1235. if (unlikely(!is_slave_direction(direction)))
  1236. goto err_out;
  1237. if (sconfig->direction == DMA_MEM_TO_DEV)
  1238. reg_width = convert_buswidth(sconfig->dst_addr_width);
  1239. else
  1240. reg_width = convert_buswidth(sconfig->src_addr_width);
  1241. /* Check for too big/unaligned periods and unaligned DMA buffer */
  1242. if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
  1243. goto err_out;
  1244. /* build cyclic linked list */
  1245. for (i = 0; i < periods; i++) {
  1246. struct at_desc *desc;
  1247. desc = atc_desc_get(atchan);
  1248. if (!desc)
  1249. goto err_desc_get;
  1250. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  1251. reg_width, period_len, direction))
  1252. goto err_desc_get;
  1253. atc_desc_chain(&first, &prev, desc);
  1254. }
  1255. /* lets make a cyclic list */
  1256. prev->lli.dscr = first->txd.phys;
  1257. /* First descriptor of the chain embedds additional information */
  1258. first->txd.cookie = -EBUSY;
  1259. first->total_len = buf_len;
  1260. return &first->txd;
  1261. err_desc_get:
  1262. dev_err(chan2dev(chan), "not enough descriptors available\n");
  1263. atc_desc_put(atchan, first);
  1264. err_out:
  1265. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  1266. return NULL;
  1267. }
  1268. static int atc_config(struct dma_chan *chan,
  1269. struct dma_slave_config *sconfig)
  1270. {
  1271. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1272. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1273. /* Check if it is chan is configured for slave transfers */
  1274. if (!chan->private)
  1275. return -EINVAL;
  1276. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  1277. convert_burst(&atchan->dma_sconfig.src_maxburst);
  1278. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  1279. return 0;
  1280. }
  1281. static int atc_pause(struct dma_chan *chan)
  1282. {
  1283. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1284. struct at_dma *atdma = to_at_dma(chan->device);
  1285. int chan_id = atchan->chan_common.chan_id;
  1286. unsigned long flags;
  1287. LIST_HEAD(list);
  1288. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1289. spin_lock_irqsave(&atchan->lock, flags);
  1290. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  1291. set_bit(ATC_IS_PAUSED, &atchan->status);
  1292. spin_unlock_irqrestore(&atchan->lock, flags);
  1293. return 0;
  1294. }
  1295. static int atc_resume(struct dma_chan *chan)
  1296. {
  1297. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1298. struct at_dma *atdma = to_at_dma(chan->device);
  1299. int chan_id = atchan->chan_common.chan_id;
  1300. unsigned long flags;
  1301. LIST_HEAD(list);
  1302. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1303. if (!atc_chan_is_paused(atchan))
  1304. return 0;
  1305. spin_lock_irqsave(&atchan->lock, flags);
  1306. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  1307. clear_bit(ATC_IS_PAUSED, &atchan->status);
  1308. spin_unlock_irqrestore(&atchan->lock, flags);
  1309. return 0;
  1310. }
  1311. static int atc_terminate_all(struct dma_chan *chan)
  1312. {
  1313. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1314. struct at_dma *atdma = to_at_dma(chan->device);
  1315. int chan_id = atchan->chan_common.chan_id;
  1316. struct at_desc *desc, *_desc;
  1317. unsigned long flags;
  1318. LIST_HEAD(list);
  1319. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1320. /*
  1321. * This is only called when something went wrong elsewhere, so
  1322. * we don't really care about the data. Just disable the
  1323. * channel. We still have to poll the channel enable bit due
  1324. * to AHB/HSB limitations.
  1325. */
  1326. spin_lock_irqsave(&atchan->lock, flags);
  1327. /* disabling channel: must also remove suspend state */
  1328. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  1329. /* confirm that this channel is disabled */
  1330. while (dma_readl(atdma, CHSR) & atchan->mask)
  1331. cpu_relax();
  1332. /* active_list entries will end up before queued entries */
  1333. list_splice_init(&atchan->queue, &list);
  1334. list_splice_init(&atchan->active_list, &list);
  1335. /* Flush all pending and queued descriptors */
  1336. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  1337. atc_chain_complete(atchan, desc);
  1338. clear_bit(ATC_IS_PAUSED, &atchan->status);
  1339. /* if channel dedicated to cyclic operations, free it */
  1340. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  1341. spin_unlock_irqrestore(&atchan->lock, flags);
  1342. return 0;
  1343. }
  1344. /**
  1345. * atc_tx_status - poll for transaction completion
  1346. * @chan: DMA channel
  1347. * @cookie: transaction identifier to check status of
  1348. * @txstate: if not %NULL updated with transaction state
  1349. *
  1350. * If @txstate is passed in, upon return it reflect the driver
  1351. * internal state and can be used with dma_async_is_complete() to check
  1352. * the status of multiple cookies without re-checking hardware state.
  1353. */
  1354. static enum dma_status
  1355. atc_tx_status(struct dma_chan *chan,
  1356. dma_cookie_t cookie,
  1357. struct dma_tx_state *txstate)
  1358. {
  1359. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1360. unsigned long flags;
  1361. enum dma_status ret;
  1362. int bytes = 0;
  1363. ret = dma_cookie_status(chan, cookie, txstate);
  1364. if (ret == DMA_COMPLETE)
  1365. return ret;
  1366. /*
  1367. * There's no point calculating the residue if there's
  1368. * no txstate to store the value.
  1369. */
  1370. if (!txstate)
  1371. return DMA_ERROR;
  1372. spin_lock_irqsave(&atchan->lock, flags);
  1373. /* Get number of bytes left in the active transactions */
  1374. bytes = atc_get_bytes_left(chan, cookie);
  1375. spin_unlock_irqrestore(&atchan->lock, flags);
  1376. if (unlikely(bytes < 0)) {
  1377. dev_vdbg(chan2dev(chan), "get residual bytes error\n");
  1378. return DMA_ERROR;
  1379. } else {
  1380. dma_set_residue(txstate, bytes);
  1381. }
  1382. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
  1383. ret, cookie, bytes);
  1384. return ret;
  1385. }
  1386. /**
  1387. * atc_issue_pending - try to finish work
  1388. * @chan: target DMA channel
  1389. */
  1390. static void atc_issue_pending(struct dma_chan *chan)
  1391. {
  1392. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1393. unsigned long flags;
  1394. dev_vdbg(chan2dev(chan), "issue_pending\n");
  1395. /* Not needed for cyclic transfers */
  1396. if (atc_chan_is_cyclic(atchan))
  1397. return;
  1398. spin_lock_irqsave(&atchan->lock, flags);
  1399. atc_advance_work(atchan);
  1400. spin_unlock_irqrestore(&atchan->lock, flags);
  1401. }
  1402. /**
  1403. * atc_alloc_chan_resources - allocate resources for DMA channel
  1404. * @chan: allocate descriptor resources for this channel
  1405. * @client: current client requesting the channel be ready for requests
  1406. *
  1407. * return - the number of allocated descriptors
  1408. */
  1409. static int atc_alloc_chan_resources(struct dma_chan *chan)
  1410. {
  1411. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1412. struct at_dma *atdma = to_at_dma(chan->device);
  1413. struct at_desc *desc;
  1414. struct at_dma_slave *atslave;
  1415. unsigned long flags;
  1416. int i;
  1417. u32 cfg;
  1418. LIST_HEAD(tmp_list);
  1419. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  1420. /* ASSERT: channel is idle */
  1421. if (atc_chan_is_enabled(atchan)) {
  1422. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  1423. return -EIO;
  1424. }
  1425. cfg = ATC_DEFAULT_CFG;
  1426. atslave = chan->private;
  1427. if (atslave) {
  1428. /*
  1429. * We need controller-specific data to set up slave
  1430. * transfers.
  1431. */
  1432. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  1433. /* if cfg configuration specified take it instead of default */
  1434. if (atslave->cfg)
  1435. cfg = atslave->cfg;
  1436. }
  1437. /* have we already been set up?
  1438. * reconfigure channel but no need to reallocate descriptors */
  1439. if (!list_empty(&atchan->free_list))
  1440. return atchan->descs_allocated;
  1441. /* Allocate initial pool of descriptors */
  1442. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1443. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  1444. if (!desc) {
  1445. dev_err(atdma->dma_common.dev,
  1446. "Only %d initial descriptors\n", i);
  1447. break;
  1448. }
  1449. list_add_tail(&desc->desc_node, &tmp_list);
  1450. }
  1451. spin_lock_irqsave(&atchan->lock, flags);
  1452. atchan->descs_allocated = i;
  1453. list_splice(&tmp_list, &atchan->free_list);
  1454. dma_cookie_init(chan);
  1455. spin_unlock_irqrestore(&atchan->lock, flags);
  1456. /* channel parameters */
  1457. channel_writel(atchan, CFG, cfg);
  1458. dev_dbg(chan2dev(chan),
  1459. "alloc_chan_resources: allocated %d descriptors\n",
  1460. atchan->descs_allocated);
  1461. return atchan->descs_allocated;
  1462. }
  1463. /**
  1464. * atc_free_chan_resources - free all channel resources
  1465. * @chan: DMA channel
  1466. */
  1467. static void atc_free_chan_resources(struct dma_chan *chan)
  1468. {
  1469. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1470. struct at_dma *atdma = to_at_dma(chan->device);
  1471. struct at_desc *desc, *_desc;
  1472. LIST_HEAD(list);
  1473. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  1474. atchan->descs_allocated);
  1475. /* ASSERT: channel is idle */
  1476. BUG_ON(!list_empty(&atchan->active_list));
  1477. BUG_ON(!list_empty(&atchan->queue));
  1478. BUG_ON(atc_chan_is_enabled(atchan));
  1479. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  1480. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  1481. list_del(&desc->desc_node);
  1482. /* free link descriptor */
  1483. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  1484. }
  1485. list_splice_init(&atchan->free_list, &list);
  1486. atchan->descs_allocated = 0;
  1487. atchan->status = 0;
  1488. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  1489. }
  1490. #ifdef CONFIG_OF
  1491. static bool at_dma_filter(struct dma_chan *chan, void *slave)
  1492. {
  1493. struct at_dma_slave *atslave = slave;
  1494. if (atslave->dma_dev == chan->device->dev) {
  1495. chan->private = atslave;
  1496. return true;
  1497. } else {
  1498. return false;
  1499. }
  1500. }
  1501. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1502. struct of_dma *of_dma)
  1503. {
  1504. struct dma_chan *chan;
  1505. struct at_dma_chan *atchan;
  1506. struct at_dma_slave *atslave;
  1507. dma_cap_mask_t mask;
  1508. unsigned int per_id;
  1509. struct platform_device *dmac_pdev;
  1510. if (dma_spec->args_count != 2)
  1511. return NULL;
  1512. dmac_pdev = of_find_device_by_node(dma_spec->np);
  1513. dma_cap_zero(mask);
  1514. dma_cap_set(DMA_SLAVE, mask);
  1515. atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
  1516. if (!atslave)
  1517. return NULL;
  1518. atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
  1519. /*
  1520. * We can fill both SRC_PER and DST_PER, one of these fields will be
  1521. * ignored depending on DMA transfer direction.
  1522. */
  1523. per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
  1524. atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
  1525. | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
  1526. /*
  1527. * We have to translate the value we get from the device tree since
  1528. * the half FIFO configuration value had to be 0 to keep backward
  1529. * compatibility.
  1530. */
  1531. switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
  1532. case AT91_DMA_CFG_FIFOCFG_ALAP:
  1533. atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
  1534. break;
  1535. case AT91_DMA_CFG_FIFOCFG_ASAP:
  1536. atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
  1537. break;
  1538. case AT91_DMA_CFG_FIFOCFG_HALF:
  1539. default:
  1540. atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
  1541. }
  1542. atslave->dma_dev = &dmac_pdev->dev;
  1543. chan = dma_request_channel(mask, at_dma_filter, atslave);
  1544. if (!chan)
  1545. return NULL;
  1546. atchan = to_at_dma_chan(chan);
  1547. atchan->per_if = dma_spec->args[0] & 0xff;
  1548. atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
  1549. return chan;
  1550. }
  1551. #else
  1552. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1553. struct of_dma *of_dma)
  1554. {
  1555. return NULL;
  1556. }
  1557. #endif
  1558. /*-- Module Management -----------------------------------------------*/
  1559. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  1560. static struct at_dma_platform_data at91sam9rl_config = {
  1561. .nr_channels = 2,
  1562. };
  1563. static struct at_dma_platform_data at91sam9g45_config = {
  1564. .nr_channels = 8,
  1565. };
  1566. #if defined(CONFIG_OF)
  1567. static const struct of_device_id atmel_dma_dt_ids[] = {
  1568. {
  1569. .compatible = "atmel,at91sam9rl-dma",
  1570. .data = &at91sam9rl_config,
  1571. }, {
  1572. .compatible = "atmel,at91sam9g45-dma",
  1573. .data = &at91sam9g45_config,
  1574. }, {
  1575. /* sentinel */
  1576. }
  1577. };
  1578. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1579. #endif
  1580. static const struct platform_device_id atdma_devtypes[] = {
  1581. {
  1582. .name = "at91sam9rl_dma",
  1583. .driver_data = (unsigned long) &at91sam9rl_config,
  1584. }, {
  1585. .name = "at91sam9g45_dma",
  1586. .driver_data = (unsigned long) &at91sam9g45_config,
  1587. }, {
  1588. /* sentinel */
  1589. }
  1590. };
  1591. static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
  1592. struct platform_device *pdev)
  1593. {
  1594. if (pdev->dev.of_node) {
  1595. const struct of_device_id *match;
  1596. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1597. if (match == NULL)
  1598. return NULL;
  1599. return match->data;
  1600. }
  1601. return (struct at_dma_platform_data *)
  1602. platform_get_device_id(pdev)->driver_data;
  1603. }
  1604. /**
  1605. * at_dma_off - disable DMA controller
  1606. * @atdma: the Atmel HDAMC device
  1607. */
  1608. static void at_dma_off(struct at_dma *atdma)
  1609. {
  1610. dma_writel(atdma, EN, 0);
  1611. /* disable all interrupts */
  1612. dma_writel(atdma, EBCIDR, -1L);
  1613. /* confirm that all channels are disabled */
  1614. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1615. cpu_relax();
  1616. }
  1617. static int __init at_dma_probe(struct platform_device *pdev)
  1618. {
  1619. struct resource *io;
  1620. struct at_dma *atdma;
  1621. size_t size;
  1622. int irq;
  1623. int err;
  1624. int i;
  1625. const struct at_dma_platform_data *plat_dat;
  1626. /* setup platform data for each SoC */
  1627. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1628. dma_cap_set(DMA_SG, at91sam9rl_config.cap_mask);
  1629. dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
  1630. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1631. dma_cap_set(DMA_MEMSET, at91sam9g45_config.cap_mask);
  1632. dma_cap_set(DMA_MEMSET_SG, at91sam9g45_config.cap_mask);
  1633. dma_cap_set(DMA_PRIVATE, at91sam9g45_config.cap_mask);
  1634. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1635. dma_cap_set(DMA_SG, at91sam9g45_config.cap_mask);
  1636. /* get DMA parameters from controller type */
  1637. plat_dat = at_dma_get_driver_data(pdev);
  1638. if (!plat_dat)
  1639. return -ENODEV;
  1640. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1641. if (!io)
  1642. return -EINVAL;
  1643. irq = platform_get_irq(pdev, 0);
  1644. if (irq < 0)
  1645. return irq;
  1646. size = sizeof(struct at_dma);
  1647. size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
  1648. atdma = kzalloc(size, GFP_KERNEL);
  1649. if (!atdma)
  1650. return -ENOMEM;
  1651. /* discover transaction capabilities */
  1652. atdma->dma_common.cap_mask = plat_dat->cap_mask;
  1653. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1654. size = resource_size(io);
  1655. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1656. err = -EBUSY;
  1657. goto err_kfree;
  1658. }
  1659. atdma->regs = ioremap(io->start, size);
  1660. if (!atdma->regs) {
  1661. err = -ENOMEM;
  1662. goto err_release_r;
  1663. }
  1664. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1665. if (IS_ERR(atdma->clk)) {
  1666. err = PTR_ERR(atdma->clk);
  1667. goto err_clk;
  1668. }
  1669. err = clk_prepare_enable(atdma->clk);
  1670. if (err)
  1671. goto err_clk_prepare;
  1672. /* force dma off, just in case */
  1673. at_dma_off(atdma);
  1674. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1675. if (err)
  1676. goto err_irq;
  1677. platform_set_drvdata(pdev, atdma);
  1678. /* create a pool of consistent memory blocks for hardware descriptors */
  1679. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1680. &pdev->dev, sizeof(struct at_desc),
  1681. 4 /* word alignment */, 0);
  1682. if (!atdma->dma_desc_pool) {
  1683. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1684. err = -ENOMEM;
  1685. goto err_desc_pool_create;
  1686. }
  1687. /* create a pool of consistent memory blocks for memset blocks */
  1688. atdma->memset_pool = dma_pool_create("at_hdmac_memset_pool",
  1689. &pdev->dev, sizeof(int), 4, 0);
  1690. if (!atdma->memset_pool) {
  1691. dev_err(&pdev->dev, "No memory for memset dma pool\n");
  1692. err = -ENOMEM;
  1693. goto err_memset_pool_create;
  1694. }
  1695. /* clear any pending interrupt */
  1696. while (dma_readl(atdma, EBCISR))
  1697. cpu_relax();
  1698. /* initialize channels related values */
  1699. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1700. for (i = 0; i < plat_dat->nr_channels; i++) {
  1701. struct at_dma_chan *atchan = &atdma->chan[i];
  1702. atchan->mem_if = AT_DMA_MEM_IF;
  1703. atchan->per_if = AT_DMA_PER_IF;
  1704. atchan->chan_common.device = &atdma->dma_common;
  1705. dma_cookie_init(&atchan->chan_common);
  1706. list_add_tail(&atchan->chan_common.device_node,
  1707. &atdma->dma_common.channels);
  1708. atchan->ch_regs = atdma->regs + ch_regs(i);
  1709. spin_lock_init(&atchan->lock);
  1710. atchan->mask = 1 << i;
  1711. INIT_LIST_HEAD(&atchan->active_list);
  1712. INIT_LIST_HEAD(&atchan->queue);
  1713. INIT_LIST_HEAD(&atchan->free_list);
  1714. tasklet_init(&atchan->tasklet, atc_tasklet,
  1715. (unsigned long)atchan);
  1716. atc_enable_chan_irq(atdma, i);
  1717. }
  1718. /* set base routines */
  1719. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1720. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1721. atdma->dma_common.device_tx_status = atc_tx_status;
  1722. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1723. atdma->dma_common.dev = &pdev->dev;
  1724. /* set prep routines based on capability */
  1725. if (dma_has_cap(DMA_INTERLEAVE, atdma->dma_common.cap_mask))
  1726. atdma->dma_common.device_prep_interleaved_dma = atc_prep_dma_interleaved;
  1727. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1728. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1729. if (dma_has_cap(DMA_MEMSET, atdma->dma_common.cap_mask)) {
  1730. atdma->dma_common.device_prep_dma_memset = atc_prep_dma_memset;
  1731. atdma->dma_common.device_prep_dma_memset_sg = atc_prep_dma_memset_sg;
  1732. atdma->dma_common.fill_align = DMAENGINE_ALIGN_4_BYTES;
  1733. }
  1734. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1735. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1736. /* controller can do slave DMA: can trigger cyclic transfers */
  1737. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1738. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1739. atdma->dma_common.device_config = atc_config;
  1740. atdma->dma_common.device_pause = atc_pause;
  1741. atdma->dma_common.device_resume = atc_resume;
  1742. atdma->dma_common.device_terminate_all = atc_terminate_all;
  1743. atdma->dma_common.src_addr_widths = ATC_DMA_BUSWIDTHS;
  1744. atdma->dma_common.dst_addr_widths = ATC_DMA_BUSWIDTHS;
  1745. atdma->dma_common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1746. atdma->dma_common.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1747. }
  1748. if (dma_has_cap(DMA_SG, atdma->dma_common.cap_mask))
  1749. atdma->dma_common.device_prep_dma_sg = atc_prep_dma_sg;
  1750. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1751. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s%s%s), %d channels\n",
  1752. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1753. dma_has_cap(DMA_MEMSET, atdma->dma_common.cap_mask) ? "set " : "",
  1754. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1755. dma_has_cap(DMA_SG, atdma->dma_common.cap_mask) ? "sg-cpy " : "",
  1756. plat_dat->nr_channels);
  1757. dma_async_device_register(&atdma->dma_common);
  1758. /*
  1759. * Do not return an error if the dmac node is not present in order to
  1760. * not break the existing way of requesting channel with
  1761. * dma_request_channel().
  1762. */
  1763. if (pdev->dev.of_node) {
  1764. err = of_dma_controller_register(pdev->dev.of_node,
  1765. at_dma_xlate, atdma);
  1766. if (err) {
  1767. dev_err(&pdev->dev, "could not register of_dma_controller\n");
  1768. goto err_of_dma_controller_register;
  1769. }
  1770. }
  1771. return 0;
  1772. err_of_dma_controller_register:
  1773. dma_async_device_unregister(&atdma->dma_common);
  1774. dma_pool_destroy(atdma->memset_pool);
  1775. err_memset_pool_create:
  1776. dma_pool_destroy(atdma->dma_desc_pool);
  1777. err_desc_pool_create:
  1778. free_irq(platform_get_irq(pdev, 0), atdma);
  1779. err_irq:
  1780. clk_disable_unprepare(atdma->clk);
  1781. err_clk_prepare:
  1782. clk_put(atdma->clk);
  1783. err_clk:
  1784. iounmap(atdma->regs);
  1785. atdma->regs = NULL;
  1786. err_release_r:
  1787. release_mem_region(io->start, size);
  1788. err_kfree:
  1789. kfree(atdma);
  1790. return err;
  1791. }
  1792. static int at_dma_remove(struct platform_device *pdev)
  1793. {
  1794. struct at_dma *atdma = platform_get_drvdata(pdev);
  1795. struct dma_chan *chan, *_chan;
  1796. struct resource *io;
  1797. at_dma_off(atdma);
  1798. dma_async_device_unregister(&atdma->dma_common);
  1799. dma_pool_destroy(atdma->memset_pool);
  1800. dma_pool_destroy(atdma->dma_desc_pool);
  1801. free_irq(platform_get_irq(pdev, 0), atdma);
  1802. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1803. device_node) {
  1804. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1805. /* Disable interrupts */
  1806. atc_disable_chan_irq(atdma, chan->chan_id);
  1807. tasklet_kill(&atchan->tasklet);
  1808. list_del(&chan->device_node);
  1809. }
  1810. clk_disable_unprepare(atdma->clk);
  1811. clk_put(atdma->clk);
  1812. iounmap(atdma->regs);
  1813. atdma->regs = NULL;
  1814. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1815. release_mem_region(io->start, resource_size(io));
  1816. kfree(atdma);
  1817. return 0;
  1818. }
  1819. static void at_dma_shutdown(struct platform_device *pdev)
  1820. {
  1821. struct at_dma *atdma = platform_get_drvdata(pdev);
  1822. at_dma_off(platform_get_drvdata(pdev));
  1823. clk_disable_unprepare(atdma->clk);
  1824. }
  1825. static int at_dma_prepare(struct device *dev)
  1826. {
  1827. struct platform_device *pdev = to_platform_device(dev);
  1828. struct at_dma *atdma = platform_get_drvdata(pdev);
  1829. struct dma_chan *chan, *_chan;
  1830. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1831. device_node) {
  1832. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1833. /* wait for transaction completion (except in cyclic case) */
  1834. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1835. return -EAGAIN;
  1836. }
  1837. return 0;
  1838. }
  1839. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1840. {
  1841. struct dma_chan *chan = &atchan->chan_common;
  1842. /* Channel should be paused by user
  1843. * do it anyway even if it is not done already */
  1844. if (!atc_chan_is_paused(atchan)) {
  1845. dev_warn(chan2dev(chan),
  1846. "cyclic channel not paused, should be done by channel user\n");
  1847. atc_pause(chan);
  1848. }
  1849. /* now preserve additional data for cyclic operations */
  1850. /* next descriptor address in the cyclic list */
  1851. atchan->save_dscr = channel_readl(atchan, DSCR);
  1852. vdbg_dump_regs(atchan);
  1853. }
  1854. static int at_dma_suspend_noirq(struct device *dev)
  1855. {
  1856. struct platform_device *pdev = to_platform_device(dev);
  1857. struct at_dma *atdma = platform_get_drvdata(pdev);
  1858. struct dma_chan *chan, *_chan;
  1859. /* preserve data */
  1860. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1861. device_node) {
  1862. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1863. if (atc_chan_is_cyclic(atchan))
  1864. atc_suspend_cyclic(atchan);
  1865. atchan->save_cfg = channel_readl(atchan, CFG);
  1866. }
  1867. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1868. /* disable DMA controller */
  1869. at_dma_off(atdma);
  1870. clk_disable_unprepare(atdma->clk);
  1871. return 0;
  1872. }
  1873. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1874. {
  1875. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1876. /* restore channel status for cyclic descriptors list:
  1877. * next descriptor in the cyclic list at the time of suspend */
  1878. channel_writel(atchan, SADDR, 0);
  1879. channel_writel(atchan, DADDR, 0);
  1880. channel_writel(atchan, CTRLA, 0);
  1881. channel_writel(atchan, CTRLB, 0);
  1882. channel_writel(atchan, DSCR, atchan->save_dscr);
  1883. dma_writel(atdma, CHER, atchan->mask);
  1884. /* channel pause status should be removed by channel user
  1885. * We cannot take the initiative to do it here */
  1886. vdbg_dump_regs(atchan);
  1887. }
  1888. static int at_dma_resume_noirq(struct device *dev)
  1889. {
  1890. struct platform_device *pdev = to_platform_device(dev);
  1891. struct at_dma *atdma = platform_get_drvdata(pdev);
  1892. struct dma_chan *chan, *_chan;
  1893. /* bring back DMA controller */
  1894. clk_prepare_enable(atdma->clk);
  1895. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1896. /* clear any pending interrupt */
  1897. while (dma_readl(atdma, EBCISR))
  1898. cpu_relax();
  1899. /* restore saved data */
  1900. dma_writel(atdma, EBCIER, atdma->save_imr);
  1901. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1902. device_node) {
  1903. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1904. channel_writel(atchan, CFG, atchan->save_cfg);
  1905. if (atc_chan_is_cyclic(atchan))
  1906. atc_resume_cyclic(atchan);
  1907. }
  1908. return 0;
  1909. }
  1910. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1911. .prepare = at_dma_prepare,
  1912. .suspend_noirq = at_dma_suspend_noirq,
  1913. .resume_noirq = at_dma_resume_noirq,
  1914. };
  1915. static struct platform_driver at_dma_driver = {
  1916. .remove = at_dma_remove,
  1917. .shutdown = at_dma_shutdown,
  1918. .id_table = atdma_devtypes,
  1919. .driver = {
  1920. .name = "at_hdmac",
  1921. .pm = &at_dma_dev_pm_ops,
  1922. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1923. },
  1924. };
  1925. static int __init at_dma_init(void)
  1926. {
  1927. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1928. }
  1929. subsys_initcall(at_dma_init);
  1930. static void __exit at_dma_exit(void)
  1931. {
  1932. platform_driver_unregister(&at_dma_driver);
  1933. }
  1934. module_exit(at_dma_exit);
  1935. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1936. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1937. MODULE_LICENSE("GPL");
  1938. MODULE_ALIAS("platform:at_hdmac");