at_hdmac.c 62 KB

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