at_hdmac.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  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) {
  407. dma_pool_free(atdma->memset_pool, desc->memset_vaddr,
  408. desc->memset_paddr);
  409. desc->memset = 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=0x%08x, dest=0x%08x, 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: d0x%x s0x%x 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. /**
  757. * atc_prep_dma_memset - prepare a memcpy operation
  758. * @chan: the channel to prepare operation on
  759. * @dest: operation virtual destination address
  760. * @value: value to set memory buffer to
  761. * @len: operation length
  762. * @flags: tx descriptor status flags
  763. */
  764. static struct dma_async_tx_descriptor *
  765. atc_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  766. size_t len, unsigned long flags)
  767. {
  768. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  769. struct at_dma *atdma = to_at_dma(chan->device);
  770. struct at_desc *desc = NULL;
  771. size_t xfer_count;
  772. u32 ctrla;
  773. u32 ctrlb;
  774. dev_vdbg(chan2dev(chan), "%s: d0x%x v0x%x l0x%zx f0x%lx\n", __func__,
  775. dest, value, len, flags);
  776. if (unlikely(!len)) {
  777. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  778. return NULL;
  779. }
  780. if (!is_dma_fill_aligned(chan->device, dest, 0, len)) {
  781. dev_dbg(chan2dev(chan), "%s: buffer is not aligned\n",
  782. __func__);
  783. return NULL;
  784. }
  785. xfer_count = len >> 2;
  786. if (xfer_count > ATC_BTSIZE_MAX) {
  787. dev_err(chan2dev(chan), "%s: buffer is too big\n",
  788. __func__);
  789. return NULL;
  790. }
  791. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  792. | ATC_SRC_ADDR_MODE_FIXED
  793. | ATC_DST_ADDR_MODE_INCR
  794. | ATC_FC_MEM2MEM;
  795. ctrla = ATC_SRC_WIDTH(2) |
  796. ATC_DST_WIDTH(2);
  797. desc = atc_desc_get(atchan);
  798. if (!desc) {
  799. dev_err(chan2dev(chan), "%s: can't get a descriptor\n",
  800. __func__);
  801. return NULL;
  802. }
  803. desc->memset_vaddr = dma_pool_alloc(atdma->memset_pool, GFP_ATOMIC,
  804. &desc->memset_paddr);
  805. if (!desc->memset_vaddr) {
  806. dev_err(chan2dev(chan), "%s: couldn't allocate buffer\n",
  807. __func__);
  808. goto err_put_desc;
  809. }
  810. *desc->memset_vaddr = value;
  811. desc->memset = true;
  812. desc->lli.saddr = desc->memset_paddr;
  813. desc->lli.daddr = dest;
  814. desc->lli.ctrla = ctrla | xfer_count;
  815. desc->lli.ctrlb = ctrlb;
  816. desc->txd.cookie = -EBUSY;
  817. desc->len = len;
  818. desc->total_len = len;
  819. /* set end-of-link on the descriptor */
  820. set_desc_eol(desc);
  821. desc->txd.flags = flags;
  822. return &desc->txd;
  823. err_put_desc:
  824. atc_desc_put(atchan, desc);
  825. return NULL;
  826. }
  827. /**
  828. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  829. * @chan: DMA channel
  830. * @sgl: scatterlist to transfer to/from
  831. * @sg_len: number of entries in @scatterlist
  832. * @direction: DMA direction
  833. * @flags: tx descriptor status flags
  834. * @context: transaction context (ignored)
  835. */
  836. static struct dma_async_tx_descriptor *
  837. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  838. unsigned int sg_len, enum dma_transfer_direction direction,
  839. unsigned long flags, void *context)
  840. {
  841. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  842. struct at_dma_slave *atslave = chan->private;
  843. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  844. struct at_desc *first = NULL;
  845. struct at_desc *prev = NULL;
  846. u32 ctrla;
  847. u32 ctrlb;
  848. dma_addr_t reg;
  849. unsigned int reg_width;
  850. unsigned int mem_width;
  851. unsigned int i;
  852. struct scatterlist *sg;
  853. size_t total_len = 0;
  854. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  855. sg_len,
  856. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  857. flags);
  858. if (unlikely(!atslave || !sg_len)) {
  859. dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
  860. return NULL;
  861. }
  862. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  863. | ATC_DCSIZE(sconfig->dst_maxburst);
  864. ctrlb = ATC_IEN;
  865. switch (direction) {
  866. case DMA_MEM_TO_DEV:
  867. reg_width = convert_buswidth(sconfig->dst_addr_width);
  868. ctrla |= ATC_DST_WIDTH(reg_width);
  869. ctrlb |= ATC_DST_ADDR_MODE_FIXED
  870. | ATC_SRC_ADDR_MODE_INCR
  871. | ATC_FC_MEM2PER
  872. | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
  873. reg = sconfig->dst_addr;
  874. for_each_sg(sgl, sg, sg_len, i) {
  875. struct at_desc *desc;
  876. u32 len;
  877. u32 mem;
  878. desc = atc_desc_get(atchan);
  879. if (!desc)
  880. goto err_desc_get;
  881. mem = sg_dma_address(sg);
  882. len = sg_dma_len(sg);
  883. if (unlikely(!len)) {
  884. dev_dbg(chan2dev(chan),
  885. "prep_slave_sg: sg(%d) data length is zero\n", i);
  886. goto err;
  887. }
  888. mem_width = 2;
  889. if (unlikely(mem & 3 || len & 3))
  890. mem_width = 0;
  891. desc->lli.saddr = mem;
  892. desc->lli.daddr = reg;
  893. desc->lli.ctrla = ctrla
  894. | ATC_SRC_WIDTH(mem_width)
  895. | len >> mem_width;
  896. desc->lli.ctrlb = ctrlb;
  897. desc->len = len;
  898. atc_desc_chain(&first, &prev, desc);
  899. total_len += len;
  900. }
  901. break;
  902. case DMA_DEV_TO_MEM:
  903. reg_width = convert_buswidth(sconfig->src_addr_width);
  904. ctrla |= ATC_SRC_WIDTH(reg_width);
  905. ctrlb |= ATC_DST_ADDR_MODE_INCR
  906. | ATC_SRC_ADDR_MODE_FIXED
  907. | ATC_FC_PER2MEM
  908. | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
  909. reg = sconfig->src_addr;
  910. for_each_sg(sgl, sg, sg_len, i) {
  911. struct at_desc *desc;
  912. u32 len;
  913. u32 mem;
  914. desc = atc_desc_get(atchan);
  915. if (!desc)
  916. goto err_desc_get;
  917. mem = sg_dma_address(sg);
  918. len = sg_dma_len(sg);
  919. if (unlikely(!len)) {
  920. dev_dbg(chan2dev(chan),
  921. "prep_slave_sg: sg(%d) data length is zero\n", i);
  922. goto err;
  923. }
  924. mem_width = 2;
  925. if (unlikely(mem & 3 || len & 3))
  926. mem_width = 0;
  927. desc->lli.saddr = reg;
  928. desc->lli.daddr = mem;
  929. desc->lli.ctrla = ctrla
  930. | ATC_DST_WIDTH(mem_width)
  931. | len >> reg_width;
  932. desc->lli.ctrlb = ctrlb;
  933. desc->len = len;
  934. atc_desc_chain(&first, &prev, desc);
  935. total_len += len;
  936. }
  937. break;
  938. default:
  939. return NULL;
  940. }
  941. /* set end-of-link to the last link descriptor of list*/
  942. set_desc_eol(prev);
  943. /* First descriptor of the chain embedds additional information */
  944. first->txd.cookie = -EBUSY;
  945. first->total_len = total_len;
  946. /* first link descriptor of list is responsible of flags */
  947. first->txd.flags = flags; /* client is in control of this ack */
  948. return &first->txd;
  949. err_desc_get:
  950. dev_err(chan2dev(chan), "not enough descriptors available\n");
  951. err:
  952. atc_desc_put(atchan, first);
  953. return NULL;
  954. }
  955. /**
  956. * atc_prep_dma_sg - prepare memory to memory scather-gather operation
  957. * @chan: the channel to prepare operation on
  958. * @dst_sg: destination scatterlist
  959. * @dst_nents: number of destination scatterlist entries
  960. * @src_sg: source scatterlist
  961. * @src_nents: number of source scatterlist entries
  962. * @flags: tx descriptor status flags
  963. */
  964. static struct dma_async_tx_descriptor *
  965. atc_prep_dma_sg(struct dma_chan *chan,
  966. struct scatterlist *dst_sg, unsigned int dst_nents,
  967. struct scatterlist *src_sg, unsigned int src_nents,
  968. unsigned long flags)
  969. {
  970. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  971. struct at_desc *desc = NULL;
  972. struct at_desc *first = NULL;
  973. struct at_desc *prev = NULL;
  974. unsigned int src_width;
  975. unsigned int dst_width;
  976. size_t xfer_count;
  977. u32 ctrla;
  978. u32 ctrlb;
  979. size_t dst_len = 0, src_len = 0;
  980. dma_addr_t dst = 0, src = 0;
  981. size_t len = 0, total_len = 0;
  982. if (unlikely(dst_nents == 0 || src_nents == 0))
  983. return NULL;
  984. if (unlikely(dst_sg == NULL || src_sg == NULL))
  985. return NULL;
  986. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  987. | ATC_SRC_ADDR_MODE_INCR
  988. | ATC_DST_ADDR_MODE_INCR
  989. | ATC_FC_MEM2MEM;
  990. /*
  991. * loop until there is either no more source or no more destination
  992. * scatterlist entry
  993. */
  994. while (true) {
  995. /* prepare the next transfer */
  996. if (dst_len == 0) {
  997. /* no more destination scatterlist entries */
  998. if (!dst_sg || !dst_nents)
  999. break;
  1000. dst = sg_dma_address(dst_sg);
  1001. dst_len = sg_dma_len(dst_sg);
  1002. dst_sg = sg_next(dst_sg);
  1003. dst_nents--;
  1004. }
  1005. if (src_len == 0) {
  1006. /* no more source scatterlist entries */
  1007. if (!src_sg || !src_nents)
  1008. break;
  1009. src = sg_dma_address(src_sg);
  1010. src_len = sg_dma_len(src_sg);
  1011. src_sg = sg_next(src_sg);
  1012. src_nents--;
  1013. }
  1014. len = min_t(size_t, src_len, dst_len);
  1015. if (len == 0)
  1016. continue;
  1017. /* take care for the alignment */
  1018. src_width = dst_width = atc_get_xfer_width(src, dst, len);
  1019. ctrla = ATC_SRC_WIDTH(src_width) |
  1020. ATC_DST_WIDTH(dst_width);
  1021. /*
  1022. * The number of transfers to set up refer to the source width
  1023. * that depends on the alignment.
  1024. */
  1025. xfer_count = len >> src_width;
  1026. if (xfer_count > ATC_BTSIZE_MAX) {
  1027. xfer_count = ATC_BTSIZE_MAX;
  1028. len = ATC_BTSIZE_MAX << src_width;
  1029. }
  1030. /* create the transfer */
  1031. desc = atc_desc_get(atchan);
  1032. if (!desc)
  1033. goto err_desc_get;
  1034. desc->lli.saddr = src;
  1035. desc->lli.daddr = dst;
  1036. desc->lli.ctrla = ctrla | xfer_count;
  1037. desc->lli.ctrlb = ctrlb;
  1038. desc->txd.cookie = 0;
  1039. desc->len = len;
  1040. atc_desc_chain(&first, &prev, desc);
  1041. /* update the lengths and addresses for the next loop cycle */
  1042. dst_len -= len;
  1043. src_len -= len;
  1044. dst += len;
  1045. src += len;
  1046. total_len += len;
  1047. }
  1048. /* First descriptor of the chain embedds additional information */
  1049. first->txd.cookie = -EBUSY;
  1050. first->total_len = total_len;
  1051. /* set end-of-link to the last link descriptor of list*/
  1052. set_desc_eol(desc);
  1053. first->txd.flags = flags; /* client is in control of this ack */
  1054. return &first->txd;
  1055. err_desc_get:
  1056. atc_desc_put(atchan, first);
  1057. return NULL;
  1058. }
  1059. /**
  1060. * atc_dma_cyclic_check_values
  1061. * Check for too big/unaligned periods and unaligned DMA buffer
  1062. */
  1063. static int
  1064. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  1065. size_t period_len)
  1066. {
  1067. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  1068. goto err_out;
  1069. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1070. goto err_out;
  1071. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1072. goto err_out;
  1073. return 0;
  1074. err_out:
  1075. return -EINVAL;
  1076. }
  1077. /**
  1078. * atc_dma_cyclic_fill_desc - Fill one period descriptor
  1079. */
  1080. static int
  1081. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  1082. unsigned int period_index, dma_addr_t buf_addr,
  1083. unsigned int reg_width, size_t period_len,
  1084. enum dma_transfer_direction direction)
  1085. {
  1086. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1087. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1088. u32 ctrla;
  1089. /* prepare common CRTLA value */
  1090. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  1091. | ATC_DCSIZE(sconfig->dst_maxburst)
  1092. | ATC_DST_WIDTH(reg_width)
  1093. | ATC_SRC_WIDTH(reg_width)
  1094. | period_len >> reg_width;
  1095. switch (direction) {
  1096. case DMA_MEM_TO_DEV:
  1097. desc->lli.saddr = buf_addr + (period_len * period_index);
  1098. desc->lli.daddr = sconfig->dst_addr;
  1099. desc->lli.ctrla = ctrla;
  1100. desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
  1101. | ATC_SRC_ADDR_MODE_INCR
  1102. | ATC_FC_MEM2PER
  1103. | ATC_SIF(atchan->mem_if)
  1104. | ATC_DIF(atchan->per_if);
  1105. desc->len = period_len;
  1106. break;
  1107. case DMA_DEV_TO_MEM:
  1108. desc->lli.saddr = sconfig->src_addr;
  1109. desc->lli.daddr = buf_addr + (period_len * period_index);
  1110. desc->lli.ctrla = ctrla;
  1111. desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
  1112. | ATC_SRC_ADDR_MODE_FIXED
  1113. | ATC_FC_PER2MEM
  1114. | ATC_SIF(atchan->per_if)
  1115. | ATC_DIF(atchan->mem_if);
  1116. desc->len = period_len;
  1117. break;
  1118. default:
  1119. return -EINVAL;
  1120. }
  1121. return 0;
  1122. }
  1123. /**
  1124. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  1125. * @chan: the DMA channel to prepare
  1126. * @buf_addr: physical DMA address where the buffer starts
  1127. * @buf_len: total number of bytes for the entire buffer
  1128. * @period_len: number of bytes for each period
  1129. * @direction: transfer direction, to or from device
  1130. * @flags: tx descriptor status flags
  1131. */
  1132. static struct dma_async_tx_descriptor *
  1133. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1134. size_t period_len, enum dma_transfer_direction direction,
  1135. unsigned long flags)
  1136. {
  1137. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1138. struct at_dma_slave *atslave = chan->private;
  1139. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1140. struct at_desc *first = NULL;
  1141. struct at_desc *prev = NULL;
  1142. unsigned long was_cyclic;
  1143. unsigned int reg_width;
  1144. unsigned int periods = buf_len / period_len;
  1145. unsigned int i;
  1146. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
  1147. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  1148. buf_addr,
  1149. periods, buf_len, period_len);
  1150. if (unlikely(!atslave || !buf_len || !period_len)) {
  1151. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  1152. return NULL;
  1153. }
  1154. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  1155. if (was_cyclic) {
  1156. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  1157. return NULL;
  1158. }
  1159. if (unlikely(!is_slave_direction(direction)))
  1160. goto err_out;
  1161. if (sconfig->direction == DMA_MEM_TO_DEV)
  1162. reg_width = convert_buswidth(sconfig->dst_addr_width);
  1163. else
  1164. reg_width = convert_buswidth(sconfig->src_addr_width);
  1165. /* Check for too big/unaligned periods and unaligned DMA buffer */
  1166. if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
  1167. goto err_out;
  1168. /* build cyclic linked list */
  1169. for (i = 0; i < periods; i++) {
  1170. struct at_desc *desc;
  1171. desc = atc_desc_get(atchan);
  1172. if (!desc)
  1173. goto err_desc_get;
  1174. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  1175. reg_width, period_len, direction))
  1176. goto err_desc_get;
  1177. atc_desc_chain(&first, &prev, desc);
  1178. }
  1179. /* lets make a cyclic list */
  1180. prev->lli.dscr = first->txd.phys;
  1181. /* First descriptor of the chain embedds additional information */
  1182. first->txd.cookie = -EBUSY;
  1183. first->total_len = buf_len;
  1184. return &first->txd;
  1185. err_desc_get:
  1186. dev_err(chan2dev(chan), "not enough descriptors available\n");
  1187. atc_desc_put(atchan, first);
  1188. err_out:
  1189. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  1190. return NULL;
  1191. }
  1192. static int atc_config(struct dma_chan *chan,
  1193. struct dma_slave_config *sconfig)
  1194. {
  1195. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1196. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1197. /* Check if it is chan is configured for slave transfers */
  1198. if (!chan->private)
  1199. return -EINVAL;
  1200. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  1201. convert_burst(&atchan->dma_sconfig.src_maxburst);
  1202. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  1203. return 0;
  1204. }
  1205. static int atc_pause(struct dma_chan *chan)
  1206. {
  1207. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1208. struct at_dma *atdma = to_at_dma(chan->device);
  1209. int chan_id = atchan->chan_common.chan_id;
  1210. unsigned long flags;
  1211. LIST_HEAD(list);
  1212. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1213. spin_lock_irqsave(&atchan->lock, flags);
  1214. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  1215. set_bit(ATC_IS_PAUSED, &atchan->status);
  1216. spin_unlock_irqrestore(&atchan->lock, flags);
  1217. return 0;
  1218. }
  1219. static int atc_resume(struct dma_chan *chan)
  1220. {
  1221. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1222. struct at_dma *atdma = to_at_dma(chan->device);
  1223. int chan_id = atchan->chan_common.chan_id;
  1224. unsigned long flags;
  1225. LIST_HEAD(list);
  1226. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1227. if (!atc_chan_is_paused(atchan))
  1228. return 0;
  1229. spin_lock_irqsave(&atchan->lock, flags);
  1230. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  1231. clear_bit(ATC_IS_PAUSED, &atchan->status);
  1232. spin_unlock_irqrestore(&atchan->lock, flags);
  1233. return 0;
  1234. }
  1235. static int atc_terminate_all(struct dma_chan *chan)
  1236. {
  1237. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1238. struct at_dma *atdma = to_at_dma(chan->device);
  1239. int chan_id = atchan->chan_common.chan_id;
  1240. struct at_desc *desc, *_desc;
  1241. unsigned long flags;
  1242. LIST_HEAD(list);
  1243. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1244. /*
  1245. * This is only called when something went wrong elsewhere, so
  1246. * we don't really care about the data. Just disable the
  1247. * channel. We still have to poll the channel enable bit due
  1248. * to AHB/HSB limitations.
  1249. */
  1250. spin_lock_irqsave(&atchan->lock, flags);
  1251. /* disabling channel: must also remove suspend state */
  1252. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  1253. /* confirm that this channel is disabled */
  1254. while (dma_readl(atdma, CHSR) & atchan->mask)
  1255. cpu_relax();
  1256. /* active_list entries will end up before queued entries */
  1257. list_splice_init(&atchan->queue, &list);
  1258. list_splice_init(&atchan->active_list, &list);
  1259. /* Flush all pending and queued descriptors */
  1260. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  1261. atc_chain_complete(atchan, desc);
  1262. clear_bit(ATC_IS_PAUSED, &atchan->status);
  1263. /* if channel dedicated to cyclic operations, free it */
  1264. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  1265. spin_unlock_irqrestore(&atchan->lock, flags);
  1266. return 0;
  1267. }
  1268. /**
  1269. * atc_tx_status - poll for transaction completion
  1270. * @chan: DMA channel
  1271. * @cookie: transaction identifier to check status of
  1272. * @txstate: if not %NULL updated with transaction state
  1273. *
  1274. * If @txstate is passed in, upon return it reflect the driver
  1275. * internal state and can be used with dma_async_is_complete() to check
  1276. * the status of multiple cookies without re-checking hardware state.
  1277. */
  1278. static enum dma_status
  1279. atc_tx_status(struct dma_chan *chan,
  1280. dma_cookie_t cookie,
  1281. struct dma_tx_state *txstate)
  1282. {
  1283. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1284. unsigned long flags;
  1285. enum dma_status ret;
  1286. int bytes = 0;
  1287. ret = dma_cookie_status(chan, cookie, txstate);
  1288. if (ret == DMA_COMPLETE)
  1289. return ret;
  1290. /*
  1291. * There's no point calculating the residue if there's
  1292. * no txstate to store the value.
  1293. */
  1294. if (!txstate)
  1295. return DMA_ERROR;
  1296. spin_lock_irqsave(&atchan->lock, flags);
  1297. /* Get number of bytes left in the active transactions */
  1298. bytes = atc_get_bytes_left(chan, cookie);
  1299. spin_unlock_irqrestore(&atchan->lock, flags);
  1300. if (unlikely(bytes < 0)) {
  1301. dev_vdbg(chan2dev(chan), "get residual bytes error\n");
  1302. return DMA_ERROR;
  1303. } else {
  1304. dma_set_residue(txstate, bytes);
  1305. }
  1306. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
  1307. ret, cookie, bytes);
  1308. return ret;
  1309. }
  1310. /**
  1311. * atc_issue_pending - try to finish work
  1312. * @chan: target DMA channel
  1313. */
  1314. static void atc_issue_pending(struct dma_chan *chan)
  1315. {
  1316. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1317. unsigned long flags;
  1318. dev_vdbg(chan2dev(chan), "issue_pending\n");
  1319. /* Not needed for cyclic transfers */
  1320. if (atc_chan_is_cyclic(atchan))
  1321. return;
  1322. spin_lock_irqsave(&atchan->lock, flags);
  1323. atc_advance_work(atchan);
  1324. spin_unlock_irqrestore(&atchan->lock, flags);
  1325. }
  1326. /**
  1327. * atc_alloc_chan_resources - allocate resources for DMA channel
  1328. * @chan: allocate descriptor resources for this channel
  1329. * @client: current client requesting the channel be ready for requests
  1330. *
  1331. * return - the number of allocated descriptors
  1332. */
  1333. static int atc_alloc_chan_resources(struct dma_chan *chan)
  1334. {
  1335. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1336. struct at_dma *atdma = to_at_dma(chan->device);
  1337. struct at_desc *desc;
  1338. struct at_dma_slave *atslave;
  1339. unsigned long flags;
  1340. int i;
  1341. u32 cfg;
  1342. LIST_HEAD(tmp_list);
  1343. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  1344. /* ASSERT: channel is idle */
  1345. if (atc_chan_is_enabled(atchan)) {
  1346. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  1347. return -EIO;
  1348. }
  1349. cfg = ATC_DEFAULT_CFG;
  1350. atslave = chan->private;
  1351. if (atslave) {
  1352. /*
  1353. * We need controller-specific data to set up slave
  1354. * transfers.
  1355. */
  1356. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  1357. /* if cfg configuration specified take it instead of default */
  1358. if (atslave->cfg)
  1359. cfg = atslave->cfg;
  1360. }
  1361. /* have we already been set up?
  1362. * reconfigure channel but no need to reallocate descriptors */
  1363. if (!list_empty(&atchan->free_list))
  1364. return atchan->descs_allocated;
  1365. /* Allocate initial pool of descriptors */
  1366. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1367. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  1368. if (!desc) {
  1369. dev_err(atdma->dma_common.dev,
  1370. "Only %d initial descriptors\n", i);
  1371. break;
  1372. }
  1373. list_add_tail(&desc->desc_node, &tmp_list);
  1374. }
  1375. spin_lock_irqsave(&atchan->lock, flags);
  1376. atchan->descs_allocated = i;
  1377. list_splice(&tmp_list, &atchan->free_list);
  1378. dma_cookie_init(chan);
  1379. spin_unlock_irqrestore(&atchan->lock, flags);
  1380. /* channel parameters */
  1381. channel_writel(atchan, CFG, cfg);
  1382. dev_dbg(chan2dev(chan),
  1383. "alloc_chan_resources: allocated %d descriptors\n",
  1384. atchan->descs_allocated);
  1385. return atchan->descs_allocated;
  1386. }
  1387. /**
  1388. * atc_free_chan_resources - free all channel resources
  1389. * @chan: DMA channel
  1390. */
  1391. static void atc_free_chan_resources(struct dma_chan *chan)
  1392. {
  1393. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1394. struct at_dma *atdma = to_at_dma(chan->device);
  1395. struct at_desc *desc, *_desc;
  1396. LIST_HEAD(list);
  1397. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  1398. atchan->descs_allocated);
  1399. /* ASSERT: channel is idle */
  1400. BUG_ON(!list_empty(&atchan->active_list));
  1401. BUG_ON(!list_empty(&atchan->queue));
  1402. BUG_ON(atc_chan_is_enabled(atchan));
  1403. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  1404. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  1405. list_del(&desc->desc_node);
  1406. /* free link descriptor */
  1407. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  1408. }
  1409. list_splice_init(&atchan->free_list, &list);
  1410. atchan->descs_allocated = 0;
  1411. atchan->status = 0;
  1412. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  1413. }
  1414. #ifdef CONFIG_OF
  1415. static bool at_dma_filter(struct dma_chan *chan, void *slave)
  1416. {
  1417. struct at_dma_slave *atslave = slave;
  1418. if (atslave->dma_dev == chan->device->dev) {
  1419. chan->private = atslave;
  1420. return true;
  1421. } else {
  1422. return false;
  1423. }
  1424. }
  1425. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1426. struct of_dma *of_dma)
  1427. {
  1428. struct dma_chan *chan;
  1429. struct at_dma_chan *atchan;
  1430. struct at_dma_slave *atslave;
  1431. dma_cap_mask_t mask;
  1432. unsigned int per_id;
  1433. struct platform_device *dmac_pdev;
  1434. if (dma_spec->args_count != 2)
  1435. return NULL;
  1436. dmac_pdev = of_find_device_by_node(dma_spec->np);
  1437. dma_cap_zero(mask);
  1438. dma_cap_set(DMA_SLAVE, mask);
  1439. atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
  1440. if (!atslave)
  1441. return NULL;
  1442. atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
  1443. /*
  1444. * We can fill both SRC_PER and DST_PER, one of these fields will be
  1445. * ignored depending on DMA transfer direction.
  1446. */
  1447. per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
  1448. atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
  1449. | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
  1450. /*
  1451. * We have to translate the value we get from the device tree since
  1452. * the half FIFO configuration value had to be 0 to keep backward
  1453. * compatibility.
  1454. */
  1455. switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
  1456. case AT91_DMA_CFG_FIFOCFG_ALAP:
  1457. atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
  1458. break;
  1459. case AT91_DMA_CFG_FIFOCFG_ASAP:
  1460. atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
  1461. break;
  1462. case AT91_DMA_CFG_FIFOCFG_HALF:
  1463. default:
  1464. atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
  1465. }
  1466. atslave->dma_dev = &dmac_pdev->dev;
  1467. chan = dma_request_channel(mask, at_dma_filter, atslave);
  1468. if (!chan)
  1469. return NULL;
  1470. atchan = to_at_dma_chan(chan);
  1471. atchan->per_if = dma_spec->args[0] & 0xff;
  1472. atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
  1473. return chan;
  1474. }
  1475. #else
  1476. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1477. struct of_dma *of_dma)
  1478. {
  1479. return NULL;
  1480. }
  1481. #endif
  1482. /*-- Module Management -----------------------------------------------*/
  1483. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  1484. static struct at_dma_platform_data at91sam9rl_config = {
  1485. .nr_channels = 2,
  1486. };
  1487. static struct at_dma_platform_data at91sam9g45_config = {
  1488. .nr_channels = 8,
  1489. };
  1490. #if defined(CONFIG_OF)
  1491. static const struct of_device_id atmel_dma_dt_ids[] = {
  1492. {
  1493. .compatible = "atmel,at91sam9rl-dma",
  1494. .data = &at91sam9rl_config,
  1495. }, {
  1496. .compatible = "atmel,at91sam9g45-dma",
  1497. .data = &at91sam9g45_config,
  1498. }, {
  1499. /* sentinel */
  1500. }
  1501. };
  1502. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1503. #endif
  1504. static const struct platform_device_id atdma_devtypes[] = {
  1505. {
  1506. .name = "at91sam9rl_dma",
  1507. .driver_data = (unsigned long) &at91sam9rl_config,
  1508. }, {
  1509. .name = "at91sam9g45_dma",
  1510. .driver_data = (unsigned long) &at91sam9g45_config,
  1511. }, {
  1512. /* sentinel */
  1513. }
  1514. };
  1515. static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
  1516. struct platform_device *pdev)
  1517. {
  1518. if (pdev->dev.of_node) {
  1519. const struct of_device_id *match;
  1520. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1521. if (match == NULL)
  1522. return NULL;
  1523. return match->data;
  1524. }
  1525. return (struct at_dma_platform_data *)
  1526. platform_get_device_id(pdev)->driver_data;
  1527. }
  1528. /**
  1529. * at_dma_off - disable DMA controller
  1530. * @atdma: the Atmel HDAMC device
  1531. */
  1532. static void at_dma_off(struct at_dma *atdma)
  1533. {
  1534. dma_writel(atdma, EN, 0);
  1535. /* disable all interrupts */
  1536. dma_writel(atdma, EBCIDR, -1L);
  1537. /* confirm that all channels are disabled */
  1538. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1539. cpu_relax();
  1540. }
  1541. static int __init at_dma_probe(struct platform_device *pdev)
  1542. {
  1543. struct resource *io;
  1544. struct at_dma *atdma;
  1545. size_t size;
  1546. int irq;
  1547. int err;
  1548. int i;
  1549. const struct at_dma_platform_data *plat_dat;
  1550. /* setup platform data for each SoC */
  1551. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1552. dma_cap_set(DMA_SG, at91sam9rl_config.cap_mask);
  1553. dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
  1554. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1555. dma_cap_set(DMA_MEMSET, at91sam9g45_config.cap_mask);
  1556. dma_cap_set(DMA_PRIVATE, at91sam9g45_config.cap_mask);
  1557. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1558. dma_cap_set(DMA_SG, at91sam9g45_config.cap_mask);
  1559. /* get DMA parameters from controller type */
  1560. plat_dat = at_dma_get_driver_data(pdev);
  1561. if (!plat_dat)
  1562. return -ENODEV;
  1563. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1564. if (!io)
  1565. return -EINVAL;
  1566. irq = platform_get_irq(pdev, 0);
  1567. if (irq < 0)
  1568. return irq;
  1569. size = sizeof(struct at_dma);
  1570. size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
  1571. atdma = kzalloc(size, GFP_KERNEL);
  1572. if (!atdma)
  1573. return -ENOMEM;
  1574. /* discover transaction capabilities */
  1575. atdma->dma_common.cap_mask = plat_dat->cap_mask;
  1576. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1577. size = resource_size(io);
  1578. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1579. err = -EBUSY;
  1580. goto err_kfree;
  1581. }
  1582. atdma->regs = ioremap(io->start, size);
  1583. if (!atdma->regs) {
  1584. err = -ENOMEM;
  1585. goto err_release_r;
  1586. }
  1587. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1588. if (IS_ERR(atdma->clk)) {
  1589. err = PTR_ERR(atdma->clk);
  1590. goto err_clk;
  1591. }
  1592. err = clk_prepare_enable(atdma->clk);
  1593. if (err)
  1594. goto err_clk_prepare;
  1595. /* force dma off, just in case */
  1596. at_dma_off(atdma);
  1597. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1598. if (err)
  1599. goto err_irq;
  1600. platform_set_drvdata(pdev, atdma);
  1601. /* create a pool of consistent memory blocks for hardware descriptors */
  1602. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1603. &pdev->dev, sizeof(struct at_desc),
  1604. 4 /* word alignment */, 0);
  1605. if (!atdma->dma_desc_pool) {
  1606. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1607. err = -ENOMEM;
  1608. goto err_desc_pool_create;
  1609. }
  1610. /* create a pool of consistent memory blocks for memset blocks */
  1611. atdma->memset_pool = dma_pool_create("at_hdmac_memset_pool",
  1612. &pdev->dev, sizeof(int), 4, 0);
  1613. if (!atdma->memset_pool) {
  1614. dev_err(&pdev->dev, "No memory for memset dma pool\n");
  1615. err = -ENOMEM;
  1616. goto err_memset_pool_create;
  1617. }
  1618. /* clear any pending interrupt */
  1619. while (dma_readl(atdma, EBCISR))
  1620. cpu_relax();
  1621. /* initialize channels related values */
  1622. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1623. for (i = 0; i < plat_dat->nr_channels; i++) {
  1624. struct at_dma_chan *atchan = &atdma->chan[i];
  1625. atchan->mem_if = AT_DMA_MEM_IF;
  1626. atchan->per_if = AT_DMA_PER_IF;
  1627. atchan->chan_common.device = &atdma->dma_common;
  1628. dma_cookie_init(&atchan->chan_common);
  1629. list_add_tail(&atchan->chan_common.device_node,
  1630. &atdma->dma_common.channels);
  1631. atchan->ch_regs = atdma->regs + ch_regs(i);
  1632. spin_lock_init(&atchan->lock);
  1633. atchan->mask = 1 << i;
  1634. INIT_LIST_HEAD(&atchan->active_list);
  1635. INIT_LIST_HEAD(&atchan->queue);
  1636. INIT_LIST_HEAD(&atchan->free_list);
  1637. tasklet_init(&atchan->tasklet, atc_tasklet,
  1638. (unsigned long)atchan);
  1639. atc_enable_chan_irq(atdma, i);
  1640. }
  1641. /* set base routines */
  1642. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1643. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1644. atdma->dma_common.device_tx_status = atc_tx_status;
  1645. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1646. atdma->dma_common.dev = &pdev->dev;
  1647. /* set prep routines based on capability */
  1648. if (dma_has_cap(DMA_INTERLEAVE, atdma->dma_common.cap_mask))
  1649. atdma->dma_common.device_prep_interleaved_dma = atc_prep_dma_interleaved;
  1650. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1651. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1652. if (dma_has_cap(DMA_MEMSET, atdma->dma_common.cap_mask)) {
  1653. atdma->dma_common.device_prep_dma_memset = atc_prep_dma_memset;
  1654. atdma->dma_common.fill_align = DMAENGINE_ALIGN_4_BYTES;
  1655. }
  1656. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1657. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1658. /* controller can do slave DMA: can trigger cyclic transfers */
  1659. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1660. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1661. atdma->dma_common.device_config = atc_config;
  1662. atdma->dma_common.device_pause = atc_pause;
  1663. atdma->dma_common.device_resume = atc_resume;
  1664. atdma->dma_common.device_terminate_all = atc_terminate_all;
  1665. atdma->dma_common.src_addr_widths = ATC_DMA_BUSWIDTHS;
  1666. atdma->dma_common.dst_addr_widths = ATC_DMA_BUSWIDTHS;
  1667. atdma->dma_common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1668. atdma->dma_common.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1669. }
  1670. if (dma_has_cap(DMA_SG, atdma->dma_common.cap_mask))
  1671. atdma->dma_common.device_prep_dma_sg = atc_prep_dma_sg;
  1672. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1673. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s%s%s), %d channels\n",
  1674. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1675. dma_has_cap(DMA_MEMSET, atdma->dma_common.cap_mask) ? "set " : "",
  1676. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1677. dma_has_cap(DMA_SG, atdma->dma_common.cap_mask) ? "sg-cpy " : "",
  1678. plat_dat->nr_channels);
  1679. dma_async_device_register(&atdma->dma_common);
  1680. /*
  1681. * Do not return an error if the dmac node is not present in order to
  1682. * not break the existing way of requesting channel with
  1683. * dma_request_channel().
  1684. */
  1685. if (pdev->dev.of_node) {
  1686. err = of_dma_controller_register(pdev->dev.of_node,
  1687. at_dma_xlate, atdma);
  1688. if (err) {
  1689. dev_err(&pdev->dev, "could not register of_dma_controller\n");
  1690. goto err_of_dma_controller_register;
  1691. }
  1692. }
  1693. return 0;
  1694. err_of_dma_controller_register:
  1695. dma_async_device_unregister(&atdma->dma_common);
  1696. dma_pool_destroy(atdma->memset_pool);
  1697. err_memset_pool_create:
  1698. dma_pool_destroy(atdma->dma_desc_pool);
  1699. err_desc_pool_create:
  1700. free_irq(platform_get_irq(pdev, 0), atdma);
  1701. err_irq:
  1702. clk_disable_unprepare(atdma->clk);
  1703. err_clk_prepare:
  1704. clk_put(atdma->clk);
  1705. err_clk:
  1706. iounmap(atdma->regs);
  1707. atdma->regs = NULL;
  1708. err_release_r:
  1709. release_mem_region(io->start, size);
  1710. err_kfree:
  1711. kfree(atdma);
  1712. return err;
  1713. }
  1714. static int at_dma_remove(struct platform_device *pdev)
  1715. {
  1716. struct at_dma *atdma = platform_get_drvdata(pdev);
  1717. struct dma_chan *chan, *_chan;
  1718. struct resource *io;
  1719. at_dma_off(atdma);
  1720. dma_async_device_unregister(&atdma->dma_common);
  1721. dma_pool_destroy(atdma->memset_pool);
  1722. dma_pool_destroy(atdma->dma_desc_pool);
  1723. free_irq(platform_get_irq(pdev, 0), atdma);
  1724. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1725. device_node) {
  1726. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1727. /* Disable interrupts */
  1728. atc_disable_chan_irq(atdma, chan->chan_id);
  1729. tasklet_kill(&atchan->tasklet);
  1730. list_del(&chan->device_node);
  1731. }
  1732. clk_disable_unprepare(atdma->clk);
  1733. clk_put(atdma->clk);
  1734. iounmap(atdma->regs);
  1735. atdma->regs = NULL;
  1736. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1737. release_mem_region(io->start, resource_size(io));
  1738. kfree(atdma);
  1739. return 0;
  1740. }
  1741. static void at_dma_shutdown(struct platform_device *pdev)
  1742. {
  1743. struct at_dma *atdma = platform_get_drvdata(pdev);
  1744. at_dma_off(platform_get_drvdata(pdev));
  1745. clk_disable_unprepare(atdma->clk);
  1746. }
  1747. static int at_dma_prepare(struct device *dev)
  1748. {
  1749. struct platform_device *pdev = to_platform_device(dev);
  1750. struct at_dma *atdma = platform_get_drvdata(pdev);
  1751. struct dma_chan *chan, *_chan;
  1752. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1753. device_node) {
  1754. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1755. /* wait for transaction completion (except in cyclic case) */
  1756. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1757. return -EAGAIN;
  1758. }
  1759. return 0;
  1760. }
  1761. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1762. {
  1763. struct dma_chan *chan = &atchan->chan_common;
  1764. /* Channel should be paused by user
  1765. * do it anyway even if it is not done already */
  1766. if (!atc_chan_is_paused(atchan)) {
  1767. dev_warn(chan2dev(chan),
  1768. "cyclic channel not paused, should be done by channel user\n");
  1769. atc_pause(chan);
  1770. }
  1771. /* now preserve additional data for cyclic operations */
  1772. /* next descriptor address in the cyclic list */
  1773. atchan->save_dscr = channel_readl(atchan, DSCR);
  1774. vdbg_dump_regs(atchan);
  1775. }
  1776. static int at_dma_suspend_noirq(struct device *dev)
  1777. {
  1778. struct platform_device *pdev = to_platform_device(dev);
  1779. struct at_dma *atdma = platform_get_drvdata(pdev);
  1780. struct dma_chan *chan, *_chan;
  1781. /* preserve data */
  1782. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1783. device_node) {
  1784. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1785. if (atc_chan_is_cyclic(atchan))
  1786. atc_suspend_cyclic(atchan);
  1787. atchan->save_cfg = channel_readl(atchan, CFG);
  1788. }
  1789. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1790. /* disable DMA controller */
  1791. at_dma_off(atdma);
  1792. clk_disable_unprepare(atdma->clk);
  1793. return 0;
  1794. }
  1795. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1796. {
  1797. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1798. /* restore channel status for cyclic descriptors list:
  1799. * next descriptor in the cyclic list at the time of suspend */
  1800. channel_writel(atchan, SADDR, 0);
  1801. channel_writel(atchan, DADDR, 0);
  1802. channel_writel(atchan, CTRLA, 0);
  1803. channel_writel(atchan, CTRLB, 0);
  1804. channel_writel(atchan, DSCR, atchan->save_dscr);
  1805. dma_writel(atdma, CHER, atchan->mask);
  1806. /* channel pause status should be removed by channel user
  1807. * We cannot take the initiative to do it here */
  1808. vdbg_dump_regs(atchan);
  1809. }
  1810. static int at_dma_resume_noirq(struct device *dev)
  1811. {
  1812. struct platform_device *pdev = to_platform_device(dev);
  1813. struct at_dma *atdma = platform_get_drvdata(pdev);
  1814. struct dma_chan *chan, *_chan;
  1815. /* bring back DMA controller */
  1816. clk_prepare_enable(atdma->clk);
  1817. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1818. /* clear any pending interrupt */
  1819. while (dma_readl(atdma, EBCISR))
  1820. cpu_relax();
  1821. /* restore saved data */
  1822. dma_writel(atdma, EBCIER, atdma->save_imr);
  1823. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1824. device_node) {
  1825. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1826. channel_writel(atchan, CFG, atchan->save_cfg);
  1827. if (atc_chan_is_cyclic(atchan))
  1828. atc_resume_cyclic(atchan);
  1829. }
  1830. return 0;
  1831. }
  1832. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1833. .prepare = at_dma_prepare,
  1834. .suspend_noirq = at_dma_suspend_noirq,
  1835. .resume_noirq = at_dma_resume_noirq,
  1836. };
  1837. static struct platform_driver at_dma_driver = {
  1838. .remove = at_dma_remove,
  1839. .shutdown = at_dma_shutdown,
  1840. .id_table = atdma_devtypes,
  1841. .driver = {
  1842. .name = "at_hdmac",
  1843. .pm = &at_dma_dev_pm_ops,
  1844. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1845. },
  1846. };
  1847. static int __init at_dma_init(void)
  1848. {
  1849. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1850. }
  1851. subsys_initcall(at_dma_init);
  1852. static void __exit at_dma_exit(void)
  1853. {
  1854. platform_driver_unregister(&at_dma_driver);
  1855. }
  1856. module_exit(at_dma_exit);
  1857. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1858. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1859. MODULE_LICENSE("GPL");
  1860. MODULE_ALIAS("platform:at_hdmac");