at_hdmac.c 58 KB

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