edma.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. /*
  2. * TI EDMA DMA engine driver
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/dmaengine.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/edma.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/of.h>
  27. #include <linux/of_dma.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/platform_data/edma.h>
  33. #include "../dmaengine.h"
  34. #include "../virt-dma.h"
  35. /* Offsets matching "struct edmacc_param" */
  36. #define PARM_OPT 0x00
  37. #define PARM_SRC 0x04
  38. #define PARM_A_B_CNT 0x08
  39. #define PARM_DST 0x0c
  40. #define PARM_SRC_DST_BIDX 0x10
  41. #define PARM_LINK_BCNTRLD 0x14
  42. #define PARM_SRC_DST_CIDX 0x18
  43. #define PARM_CCNT 0x1c
  44. #define PARM_SIZE 0x20
  45. /* Offsets for EDMA CC global channel registers and their shadows */
  46. #define SH_ER 0x00 /* 64 bits */
  47. #define SH_ECR 0x08 /* 64 bits */
  48. #define SH_ESR 0x10 /* 64 bits */
  49. #define SH_CER 0x18 /* 64 bits */
  50. #define SH_EER 0x20 /* 64 bits */
  51. #define SH_EECR 0x28 /* 64 bits */
  52. #define SH_EESR 0x30 /* 64 bits */
  53. #define SH_SER 0x38 /* 64 bits */
  54. #define SH_SECR 0x40 /* 64 bits */
  55. #define SH_IER 0x50 /* 64 bits */
  56. #define SH_IECR 0x58 /* 64 bits */
  57. #define SH_IESR 0x60 /* 64 bits */
  58. #define SH_IPR 0x68 /* 64 bits */
  59. #define SH_ICR 0x70 /* 64 bits */
  60. #define SH_IEVAL 0x78
  61. #define SH_QER 0x80
  62. #define SH_QEER 0x84
  63. #define SH_QEECR 0x88
  64. #define SH_QEESR 0x8c
  65. #define SH_QSER 0x90
  66. #define SH_QSECR 0x94
  67. #define SH_SIZE 0x200
  68. /* Offsets for EDMA CC global registers */
  69. #define EDMA_REV 0x0000
  70. #define EDMA_CCCFG 0x0004
  71. #define EDMA_QCHMAP 0x0200 /* 8 registers */
  72. #define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
  73. #define EDMA_QDMAQNUM 0x0260
  74. #define EDMA_QUETCMAP 0x0280
  75. #define EDMA_QUEPRI 0x0284
  76. #define EDMA_EMR 0x0300 /* 64 bits */
  77. #define EDMA_EMCR 0x0308 /* 64 bits */
  78. #define EDMA_QEMR 0x0310
  79. #define EDMA_QEMCR 0x0314
  80. #define EDMA_CCERR 0x0318
  81. #define EDMA_CCERRCLR 0x031c
  82. #define EDMA_EEVAL 0x0320
  83. #define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
  84. #define EDMA_QRAE 0x0380 /* 4 registers */
  85. #define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
  86. #define EDMA_QSTAT 0x0600 /* 2 registers */
  87. #define EDMA_QWMTHRA 0x0620
  88. #define EDMA_QWMTHRB 0x0624
  89. #define EDMA_CCSTAT 0x0640
  90. #define EDMA_M 0x1000 /* global channel registers */
  91. #define EDMA_ECR 0x1008
  92. #define EDMA_ECRH 0x100C
  93. #define EDMA_SHADOW0 0x2000 /* 4 shadow regions */
  94. #define EDMA_PARM 0x4000 /* PaRAM entries */
  95. #define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
  96. #define EDMA_DCHMAP 0x0100 /* 64 registers */
  97. /* CCCFG register */
  98. #define GET_NUM_DMACH(x) (x & 0x7) /* bits 0-2 */
  99. #define GET_NUM_QDMACH(x) ((x & 0x70) >> 4) /* bits 4-6 */
  100. #define GET_NUM_PAENTRY(x) ((x & 0x7000) >> 12) /* bits 12-14 */
  101. #define GET_NUM_EVQUE(x) ((x & 0x70000) >> 16) /* bits 16-18 */
  102. #define GET_NUM_REGN(x) ((x & 0x300000) >> 20) /* bits 20-21 */
  103. #define CHMAP_EXIST BIT(24)
  104. /* CCSTAT register */
  105. #define EDMA_CCSTAT_ACTV BIT(4)
  106. /*
  107. * Max of 20 segments per channel to conserve PaRAM slots
  108. * Also note that MAX_NR_SG should be atleast the no.of periods
  109. * that are required for ASoC, otherwise DMA prep calls will
  110. * fail. Today davinci-pcm is the only user of this driver and
  111. * requires atleast 17 slots, so we setup the default to 20.
  112. */
  113. #define MAX_NR_SG 20
  114. #define EDMA_MAX_SLOTS MAX_NR_SG
  115. #define EDMA_DESCRIPTORS 16
  116. #define EDMA_CHANNEL_ANY -1 /* for edma_alloc_channel() */
  117. #define EDMA_SLOT_ANY -1 /* for edma_alloc_slot() */
  118. #define EDMA_CONT_PARAMS_ANY 1001
  119. #define EDMA_CONT_PARAMS_FIXED_EXACT 1002
  120. #define EDMA_CONT_PARAMS_FIXED_NOT_EXACT 1003
  121. /* PaRAM slots are laid out like this */
  122. struct edmacc_param {
  123. u32 opt;
  124. u32 src;
  125. u32 a_b_cnt;
  126. u32 dst;
  127. u32 src_dst_bidx;
  128. u32 link_bcntrld;
  129. u32 src_dst_cidx;
  130. u32 ccnt;
  131. } __packed;
  132. /* fields in edmacc_param.opt */
  133. #define SAM BIT(0)
  134. #define DAM BIT(1)
  135. #define SYNCDIM BIT(2)
  136. #define STATIC BIT(3)
  137. #define EDMA_FWID (0x07 << 8)
  138. #define TCCMODE BIT(11)
  139. #define EDMA_TCC(t) ((t) << 12)
  140. #define TCINTEN BIT(20)
  141. #define ITCINTEN BIT(21)
  142. #define TCCHEN BIT(22)
  143. #define ITCCHEN BIT(23)
  144. struct edma_pset {
  145. u32 len;
  146. dma_addr_t addr;
  147. struct edmacc_param param;
  148. };
  149. struct edma_desc {
  150. struct virt_dma_desc vdesc;
  151. struct list_head node;
  152. enum dma_transfer_direction direction;
  153. int cyclic;
  154. int absync;
  155. int pset_nr;
  156. struct edma_chan *echan;
  157. int processed;
  158. /*
  159. * The following 4 elements are used for residue accounting.
  160. *
  161. * - processed_stat: the number of SG elements we have traversed
  162. * so far to cover accounting. This is updated directly to processed
  163. * during edma_callback and is always <= processed, because processed
  164. * refers to the number of pending transfer (programmed to EDMA
  165. * controller), where as processed_stat tracks number of transfers
  166. * accounted for so far.
  167. *
  168. * - residue: The amount of bytes we have left to transfer for this desc
  169. *
  170. * - residue_stat: The residue in bytes of data we have covered
  171. * so far for accounting. This is updated directly to residue
  172. * during callbacks to keep it current.
  173. *
  174. * - sg_len: Tracks the length of the current intermediate transfer,
  175. * this is required to update the residue during intermediate transfer
  176. * completion callback.
  177. */
  178. int processed_stat;
  179. u32 sg_len;
  180. u32 residue;
  181. u32 residue_stat;
  182. struct edma_pset pset[0];
  183. };
  184. struct edma_cc;
  185. struct edma_tc {
  186. struct device_node *node;
  187. u16 id;
  188. };
  189. struct edma_chan {
  190. struct virt_dma_chan vchan;
  191. struct list_head node;
  192. struct edma_desc *edesc;
  193. struct edma_cc *ecc;
  194. struct edma_tc *tc;
  195. int ch_num;
  196. bool alloced;
  197. bool hw_triggered;
  198. int slot[EDMA_MAX_SLOTS];
  199. int missed;
  200. struct dma_slave_config cfg;
  201. };
  202. struct edma_cc {
  203. struct device *dev;
  204. struct edma_soc_info *info;
  205. void __iomem *base;
  206. int id;
  207. bool legacy_mode;
  208. /* eDMA3 resource information */
  209. unsigned num_channels;
  210. unsigned num_qchannels;
  211. unsigned num_region;
  212. unsigned num_slots;
  213. unsigned num_tc;
  214. bool chmap_exist;
  215. enum dma_event_q default_queue;
  216. unsigned int ccint;
  217. unsigned int ccerrint;
  218. /*
  219. * The slot_inuse bit for each PaRAM slot is clear unless the slot is
  220. * in use by Linux or if it is allocated to be used by DSP.
  221. */
  222. unsigned long *slot_inuse;
  223. struct dma_device dma_slave;
  224. struct dma_device *dma_memcpy;
  225. struct edma_chan *slave_chans;
  226. struct edma_tc *tc_list;
  227. int dummy_slot;
  228. };
  229. /* dummy param set used to (re)initialize parameter RAM slots */
  230. static const struct edmacc_param dummy_paramset = {
  231. .link_bcntrld = 0xffff,
  232. .ccnt = 1,
  233. };
  234. #define EDMA_BINDING_LEGACY 0
  235. #define EDMA_BINDING_TPCC 1
  236. static const u32 edma_binding_type[] = {
  237. [EDMA_BINDING_LEGACY] = EDMA_BINDING_LEGACY,
  238. [EDMA_BINDING_TPCC] = EDMA_BINDING_TPCC,
  239. };
  240. static const struct of_device_id edma_of_ids[] = {
  241. {
  242. .compatible = "ti,edma3",
  243. .data = &edma_binding_type[EDMA_BINDING_LEGACY],
  244. },
  245. {
  246. .compatible = "ti,edma3-tpcc",
  247. .data = &edma_binding_type[EDMA_BINDING_TPCC],
  248. },
  249. {}
  250. };
  251. MODULE_DEVICE_TABLE(of, edma_of_ids);
  252. static const struct of_device_id edma_tptc_of_ids[] = {
  253. { .compatible = "ti,edma3-tptc", },
  254. {}
  255. };
  256. MODULE_DEVICE_TABLE(of, edma_tptc_of_ids);
  257. static inline unsigned int edma_read(struct edma_cc *ecc, int offset)
  258. {
  259. return (unsigned int)__raw_readl(ecc->base + offset);
  260. }
  261. static inline void edma_write(struct edma_cc *ecc, int offset, int val)
  262. {
  263. __raw_writel(val, ecc->base + offset);
  264. }
  265. static inline void edma_modify(struct edma_cc *ecc, int offset, unsigned and,
  266. unsigned or)
  267. {
  268. unsigned val = edma_read(ecc, offset);
  269. val &= and;
  270. val |= or;
  271. edma_write(ecc, offset, val);
  272. }
  273. static inline void edma_and(struct edma_cc *ecc, int offset, unsigned and)
  274. {
  275. unsigned val = edma_read(ecc, offset);
  276. val &= and;
  277. edma_write(ecc, offset, val);
  278. }
  279. static inline void edma_or(struct edma_cc *ecc, int offset, unsigned or)
  280. {
  281. unsigned val = edma_read(ecc, offset);
  282. val |= or;
  283. edma_write(ecc, offset, val);
  284. }
  285. static inline unsigned int edma_read_array(struct edma_cc *ecc, int offset,
  286. int i)
  287. {
  288. return edma_read(ecc, offset + (i << 2));
  289. }
  290. static inline void edma_write_array(struct edma_cc *ecc, int offset, int i,
  291. unsigned val)
  292. {
  293. edma_write(ecc, offset + (i << 2), val);
  294. }
  295. static inline void edma_modify_array(struct edma_cc *ecc, int offset, int i,
  296. unsigned and, unsigned or)
  297. {
  298. edma_modify(ecc, offset + (i << 2), and, or);
  299. }
  300. static inline void edma_or_array(struct edma_cc *ecc, int offset, int i,
  301. unsigned or)
  302. {
  303. edma_or(ecc, offset + (i << 2), or);
  304. }
  305. static inline void edma_or_array2(struct edma_cc *ecc, int offset, int i, int j,
  306. unsigned or)
  307. {
  308. edma_or(ecc, offset + ((i * 2 + j) << 2), or);
  309. }
  310. static inline void edma_write_array2(struct edma_cc *ecc, int offset, int i,
  311. int j, unsigned val)
  312. {
  313. edma_write(ecc, offset + ((i * 2 + j) << 2), val);
  314. }
  315. static inline unsigned int edma_shadow0_read(struct edma_cc *ecc, int offset)
  316. {
  317. return edma_read(ecc, EDMA_SHADOW0 + offset);
  318. }
  319. static inline unsigned int edma_shadow0_read_array(struct edma_cc *ecc,
  320. int offset, int i)
  321. {
  322. return edma_read(ecc, EDMA_SHADOW0 + offset + (i << 2));
  323. }
  324. static inline void edma_shadow0_write(struct edma_cc *ecc, int offset,
  325. unsigned val)
  326. {
  327. edma_write(ecc, EDMA_SHADOW0 + offset, val);
  328. }
  329. static inline void edma_shadow0_write_array(struct edma_cc *ecc, int offset,
  330. int i, unsigned val)
  331. {
  332. edma_write(ecc, EDMA_SHADOW0 + offset + (i << 2), val);
  333. }
  334. static inline unsigned int edma_param_read(struct edma_cc *ecc, int offset,
  335. int param_no)
  336. {
  337. return edma_read(ecc, EDMA_PARM + offset + (param_no << 5));
  338. }
  339. static inline void edma_param_write(struct edma_cc *ecc, int offset,
  340. int param_no, unsigned val)
  341. {
  342. edma_write(ecc, EDMA_PARM + offset + (param_no << 5), val);
  343. }
  344. static inline void edma_param_modify(struct edma_cc *ecc, int offset,
  345. int param_no, unsigned and, unsigned or)
  346. {
  347. edma_modify(ecc, EDMA_PARM + offset + (param_no << 5), and, or);
  348. }
  349. static inline void edma_param_and(struct edma_cc *ecc, int offset, int param_no,
  350. unsigned and)
  351. {
  352. edma_and(ecc, EDMA_PARM + offset + (param_no << 5), and);
  353. }
  354. static inline void edma_param_or(struct edma_cc *ecc, int offset, int param_no,
  355. unsigned or)
  356. {
  357. edma_or(ecc, EDMA_PARM + offset + (param_no << 5), or);
  358. }
  359. static inline void edma_set_bits(int offset, int len, unsigned long *p)
  360. {
  361. for (; len > 0; len--)
  362. set_bit(offset + (len - 1), p);
  363. }
  364. static void edma_assign_priority_to_queue(struct edma_cc *ecc, int queue_no,
  365. int priority)
  366. {
  367. int bit = queue_no * 4;
  368. edma_modify(ecc, EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
  369. }
  370. static void edma_set_chmap(struct edma_chan *echan, int slot)
  371. {
  372. struct edma_cc *ecc = echan->ecc;
  373. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  374. if (ecc->chmap_exist) {
  375. slot = EDMA_CHAN_SLOT(slot);
  376. edma_write_array(ecc, EDMA_DCHMAP, channel, (slot << 5));
  377. }
  378. }
  379. static void edma_setup_interrupt(struct edma_chan *echan, bool enable)
  380. {
  381. struct edma_cc *ecc = echan->ecc;
  382. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  383. if (enable) {
  384. edma_shadow0_write_array(ecc, SH_ICR, channel >> 5,
  385. BIT(channel & 0x1f));
  386. edma_shadow0_write_array(ecc, SH_IESR, channel >> 5,
  387. BIT(channel & 0x1f));
  388. } else {
  389. edma_shadow0_write_array(ecc, SH_IECR, channel >> 5,
  390. BIT(channel & 0x1f));
  391. }
  392. }
  393. /*
  394. * paRAM slot management functions
  395. */
  396. static void edma_write_slot(struct edma_cc *ecc, unsigned slot,
  397. const struct edmacc_param *param)
  398. {
  399. slot = EDMA_CHAN_SLOT(slot);
  400. if (slot >= ecc->num_slots)
  401. return;
  402. memcpy_toio(ecc->base + PARM_OFFSET(slot), param, PARM_SIZE);
  403. }
  404. static int edma_read_slot(struct edma_cc *ecc, unsigned slot,
  405. struct edmacc_param *param)
  406. {
  407. slot = EDMA_CHAN_SLOT(slot);
  408. if (slot >= ecc->num_slots)
  409. return -EINVAL;
  410. memcpy_fromio(param, ecc->base + PARM_OFFSET(slot), PARM_SIZE);
  411. return 0;
  412. }
  413. /**
  414. * edma_alloc_slot - allocate DMA parameter RAM
  415. * @ecc: pointer to edma_cc struct
  416. * @slot: specific slot to allocate; negative for "any unused slot"
  417. *
  418. * This allocates a parameter RAM slot, initializing it to hold a
  419. * dummy transfer. Slots allocated using this routine have not been
  420. * mapped to a hardware DMA channel, and will normally be used by
  421. * linking to them from a slot associated with a DMA channel.
  422. *
  423. * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
  424. * slots may be allocated on behalf of DSP firmware.
  425. *
  426. * Returns the number of the slot, else negative errno.
  427. */
  428. static int edma_alloc_slot(struct edma_cc *ecc, int slot)
  429. {
  430. if (slot >= 0) {
  431. slot = EDMA_CHAN_SLOT(slot);
  432. /* Requesting entry paRAM slot for a HW triggered channel. */
  433. if (ecc->chmap_exist && slot < ecc->num_channels)
  434. slot = EDMA_SLOT_ANY;
  435. }
  436. if (slot < 0) {
  437. if (ecc->chmap_exist)
  438. slot = 0;
  439. else
  440. slot = ecc->num_channels;
  441. for (;;) {
  442. slot = find_next_zero_bit(ecc->slot_inuse,
  443. ecc->num_slots,
  444. slot);
  445. if (slot == ecc->num_slots)
  446. return -ENOMEM;
  447. if (!test_and_set_bit(slot, ecc->slot_inuse))
  448. break;
  449. }
  450. } else if (slot >= ecc->num_slots) {
  451. return -EINVAL;
  452. } else if (test_and_set_bit(slot, ecc->slot_inuse)) {
  453. return -EBUSY;
  454. }
  455. edma_write_slot(ecc, slot, &dummy_paramset);
  456. return EDMA_CTLR_CHAN(ecc->id, slot);
  457. }
  458. static void edma_free_slot(struct edma_cc *ecc, unsigned slot)
  459. {
  460. slot = EDMA_CHAN_SLOT(slot);
  461. if (slot >= ecc->num_slots)
  462. return;
  463. edma_write_slot(ecc, slot, &dummy_paramset);
  464. clear_bit(slot, ecc->slot_inuse);
  465. }
  466. /**
  467. * edma_link - link one parameter RAM slot to another
  468. * @ecc: pointer to edma_cc struct
  469. * @from: parameter RAM slot originating the link
  470. * @to: parameter RAM slot which is the link target
  471. *
  472. * The originating slot should not be part of any active DMA transfer.
  473. */
  474. static void edma_link(struct edma_cc *ecc, unsigned from, unsigned to)
  475. {
  476. if (unlikely(EDMA_CTLR(from) != EDMA_CTLR(to)))
  477. dev_warn(ecc->dev, "Ignoring eDMA instance for linking\n");
  478. from = EDMA_CHAN_SLOT(from);
  479. to = EDMA_CHAN_SLOT(to);
  480. if (from >= ecc->num_slots || to >= ecc->num_slots)
  481. return;
  482. edma_param_modify(ecc, PARM_LINK_BCNTRLD, from, 0xffff0000,
  483. PARM_OFFSET(to));
  484. }
  485. /**
  486. * edma_get_position - returns the current transfer point
  487. * @ecc: pointer to edma_cc struct
  488. * @slot: parameter RAM slot being examined
  489. * @dst: true selects the dest position, false the source
  490. *
  491. * Returns the position of the current active slot
  492. */
  493. static dma_addr_t edma_get_position(struct edma_cc *ecc, unsigned slot,
  494. bool dst)
  495. {
  496. u32 offs;
  497. slot = EDMA_CHAN_SLOT(slot);
  498. offs = PARM_OFFSET(slot);
  499. offs += dst ? PARM_DST : PARM_SRC;
  500. return edma_read(ecc, offs);
  501. }
  502. /*
  503. * Channels with event associations will be triggered by their hardware
  504. * events, and channels without such associations will be triggered by
  505. * software. (At this writing there is no interface for using software
  506. * triggers except with channels that don't support hardware triggers.)
  507. */
  508. static void edma_start(struct edma_chan *echan)
  509. {
  510. struct edma_cc *ecc = echan->ecc;
  511. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  512. int j = (channel >> 5);
  513. unsigned int mask = BIT(channel & 0x1f);
  514. if (!echan->hw_triggered) {
  515. /* EDMA channels without event association */
  516. dev_dbg(ecc->dev, "ESR%d %08x\n", j,
  517. edma_shadow0_read_array(ecc, SH_ESR, j));
  518. edma_shadow0_write_array(ecc, SH_ESR, j, mask);
  519. } else {
  520. /* EDMA channel with event association */
  521. dev_dbg(ecc->dev, "ER%d %08x\n", j,
  522. edma_shadow0_read_array(ecc, SH_ER, j));
  523. /* Clear any pending event or error */
  524. edma_write_array(ecc, EDMA_ECR, j, mask);
  525. edma_write_array(ecc, EDMA_EMCR, j, mask);
  526. /* Clear any SER */
  527. edma_shadow0_write_array(ecc, SH_SECR, j, mask);
  528. edma_shadow0_write_array(ecc, SH_EESR, j, mask);
  529. dev_dbg(ecc->dev, "EER%d %08x\n", j,
  530. edma_shadow0_read_array(ecc, SH_EER, j));
  531. }
  532. }
  533. static void edma_stop(struct edma_chan *echan)
  534. {
  535. struct edma_cc *ecc = echan->ecc;
  536. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  537. int j = (channel >> 5);
  538. unsigned int mask = BIT(channel & 0x1f);
  539. edma_shadow0_write_array(ecc, SH_EECR, j, mask);
  540. edma_shadow0_write_array(ecc, SH_ECR, j, mask);
  541. edma_shadow0_write_array(ecc, SH_SECR, j, mask);
  542. edma_write_array(ecc, EDMA_EMCR, j, mask);
  543. /* clear possibly pending completion interrupt */
  544. edma_shadow0_write_array(ecc, SH_ICR, j, mask);
  545. dev_dbg(ecc->dev, "EER%d %08x\n", j,
  546. edma_shadow0_read_array(ecc, SH_EER, j));
  547. /* REVISIT: consider guarding against inappropriate event
  548. * chaining by overwriting with dummy_paramset.
  549. */
  550. }
  551. /*
  552. * Temporarily disable EDMA hardware events on the specified channel,
  553. * preventing them from triggering new transfers
  554. */
  555. static void edma_pause(struct edma_chan *echan)
  556. {
  557. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  558. unsigned int mask = BIT(channel & 0x1f);
  559. edma_shadow0_write_array(echan->ecc, SH_EECR, channel >> 5, mask);
  560. }
  561. /* Re-enable EDMA hardware events on the specified channel. */
  562. static void edma_resume(struct edma_chan *echan)
  563. {
  564. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  565. unsigned int mask = BIT(channel & 0x1f);
  566. edma_shadow0_write_array(echan->ecc, SH_EESR, channel >> 5, mask);
  567. }
  568. static void edma_trigger_channel(struct edma_chan *echan)
  569. {
  570. struct edma_cc *ecc = echan->ecc;
  571. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  572. unsigned int mask = BIT(channel & 0x1f);
  573. edma_shadow0_write_array(ecc, SH_ESR, (channel >> 5), mask);
  574. dev_dbg(ecc->dev, "ESR%d %08x\n", (channel >> 5),
  575. edma_shadow0_read_array(ecc, SH_ESR, (channel >> 5)));
  576. }
  577. static void edma_clean_channel(struct edma_chan *echan)
  578. {
  579. struct edma_cc *ecc = echan->ecc;
  580. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  581. int j = (channel >> 5);
  582. unsigned int mask = BIT(channel & 0x1f);
  583. dev_dbg(ecc->dev, "EMR%d %08x\n", j, edma_read_array(ecc, EDMA_EMR, j));
  584. edma_shadow0_write_array(ecc, SH_ECR, j, mask);
  585. /* Clear the corresponding EMR bits */
  586. edma_write_array(ecc, EDMA_EMCR, j, mask);
  587. /* Clear any SER */
  588. edma_shadow0_write_array(ecc, SH_SECR, j, mask);
  589. edma_write(ecc, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
  590. }
  591. /* Move channel to a specific event queue */
  592. static void edma_assign_channel_eventq(struct edma_chan *echan,
  593. enum dma_event_q eventq_no)
  594. {
  595. struct edma_cc *ecc = echan->ecc;
  596. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  597. int bit = (channel & 0x7) * 4;
  598. /* default to low priority queue */
  599. if (eventq_no == EVENTQ_DEFAULT)
  600. eventq_no = ecc->default_queue;
  601. if (eventq_no >= ecc->num_tc)
  602. return;
  603. eventq_no &= 7;
  604. edma_modify_array(ecc, EDMA_DMAQNUM, (channel >> 3), ~(0x7 << bit),
  605. eventq_no << bit);
  606. }
  607. static int edma_alloc_channel(struct edma_chan *echan,
  608. enum dma_event_q eventq_no)
  609. {
  610. struct edma_cc *ecc = echan->ecc;
  611. int channel = EDMA_CHAN_SLOT(echan->ch_num);
  612. /* ensure access through shadow region 0 */
  613. edma_or_array2(ecc, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
  614. /* ensure no events are pending */
  615. edma_stop(echan);
  616. edma_setup_interrupt(echan, true);
  617. edma_assign_channel_eventq(echan, eventq_no);
  618. return 0;
  619. }
  620. static void edma_free_channel(struct edma_chan *echan)
  621. {
  622. /* ensure no events are pending */
  623. edma_stop(echan);
  624. /* REVISIT should probably take out of shadow region 0 */
  625. edma_setup_interrupt(echan, false);
  626. }
  627. static inline struct edma_cc *to_edma_cc(struct dma_device *d)
  628. {
  629. return container_of(d, struct edma_cc, dma_slave);
  630. }
  631. static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
  632. {
  633. return container_of(c, struct edma_chan, vchan.chan);
  634. }
  635. static inline struct edma_desc *to_edma_desc(struct dma_async_tx_descriptor *tx)
  636. {
  637. return container_of(tx, struct edma_desc, vdesc.tx);
  638. }
  639. static void edma_desc_free(struct virt_dma_desc *vdesc)
  640. {
  641. kfree(container_of(vdesc, struct edma_desc, vdesc));
  642. }
  643. /* Dispatch a queued descriptor to the controller (caller holds lock) */
  644. static void edma_execute(struct edma_chan *echan)
  645. {
  646. struct edma_cc *ecc = echan->ecc;
  647. struct virt_dma_desc *vdesc;
  648. struct edma_desc *edesc;
  649. struct device *dev = echan->vchan.chan.device->dev;
  650. int i, j, left, nslots;
  651. if (!echan->edesc) {
  652. /* Setup is needed for the first transfer */
  653. vdesc = vchan_next_desc(&echan->vchan);
  654. if (!vdesc)
  655. return;
  656. list_del(&vdesc->node);
  657. echan->edesc = to_edma_desc(&vdesc->tx);
  658. }
  659. edesc = echan->edesc;
  660. /* Find out how many left */
  661. left = edesc->pset_nr - edesc->processed;
  662. nslots = min(MAX_NR_SG, left);
  663. edesc->sg_len = 0;
  664. /* Write descriptor PaRAM set(s) */
  665. for (i = 0; i < nslots; i++) {
  666. j = i + edesc->processed;
  667. edma_write_slot(ecc, echan->slot[i], &edesc->pset[j].param);
  668. edesc->sg_len += edesc->pset[j].len;
  669. dev_vdbg(dev,
  670. "\n pset[%d]:\n"
  671. " chnum\t%d\n"
  672. " slot\t%d\n"
  673. " opt\t%08x\n"
  674. " src\t%08x\n"
  675. " dst\t%08x\n"
  676. " abcnt\t%08x\n"
  677. " ccnt\t%08x\n"
  678. " bidx\t%08x\n"
  679. " cidx\t%08x\n"
  680. " lkrld\t%08x\n",
  681. j, echan->ch_num, echan->slot[i],
  682. edesc->pset[j].param.opt,
  683. edesc->pset[j].param.src,
  684. edesc->pset[j].param.dst,
  685. edesc->pset[j].param.a_b_cnt,
  686. edesc->pset[j].param.ccnt,
  687. edesc->pset[j].param.src_dst_bidx,
  688. edesc->pset[j].param.src_dst_cidx,
  689. edesc->pset[j].param.link_bcntrld);
  690. /* Link to the previous slot if not the last set */
  691. if (i != (nslots - 1))
  692. edma_link(ecc, echan->slot[i], echan->slot[i + 1]);
  693. }
  694. edesc->processed += nslots;
  695. /*
  696. * If this is either the last set in a set of SG-list transactions
  697. * then setup a link to the dummy slot, this results in all future
  698. * events being absorbed and that's OK because we're done
  699. */
  700. if (edesc->processed == edesc->pset_nr) {
  701. if (edesc->cyclic)
  702. edma_link(ecc, echan->slot[nslots - 1], echan->slot[1]);
  703. else
  704. edma_link(ecc, echan->slot[nslots - 1],
  705. echan->ecc->dummy_slot);
  706. }
  707. if (echan->missed) {
  708. /*
  709. * This happens due to setup times between intermediate
  710. * transfers in long SG lists which have to be broken up into
  711. * transfers of MAX_NR_SG
  712. */
  713. dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
  714. edma_clean_channel(echan);
  715. edma_stop(echan);
  716. edma_start(echan);
  717. edma_trigger_channel(echan);
  718. echan->missed = 0;
  719. } else if (edesc->processed <= MAX_NR_SG) {
  720. dev_dbg(dev, "first transfer starting on channel %d\n",
  721. echan->ch_num);
  722. edma_start(echan);
  723. } else {
  724. dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
  725. echan->ch_num, edesc->processed);
  726. edma_resume(echan);
  727. }
  728. }
  729. static int edma_terminate_all(struct dma_chan *chan)
  730. {
  731. struct edma_chan *echan = to_edma_chan(chan);
  732. unsigned long flags;
  733. LIST_HEAD(head);
  734. spin_lock_irqsave(&echan->vchan.lock, flags);
  735. /*
  736. * Stop DMA activity: we assume the callback will not be called
  737. * after edma_dma() returns (even if it does, it will see
  738. * echan->edesc is NULL and exit.)
  739. */
  740. if (echan->edesc) {
  741. edma_stop(echan);
  742. /* Move the cyclic channel back to default queue */
  743. if (!echan->tc && echan->edesc->cyclic)
  744. edma_assign_channel_eventq(echan, EVENTQ_DEFAULT);
  745. vchan_terminate_vdesc(&echan->edesc->vdesc);
  746. echan->edesc = NULL;
  747. }
  748. vchan_get_all_descriptors(&echan->vchan, &head);
  749. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  750. vchan_dma_desc_free_list(&echan->vchan, &head);
  751. return 0;
  752. }
  753. static void edma_synchronize(struct dma_chan *chan)
  754. {
  755. struct edma_chan *echan = to_edma_chan(chan);
  756. vchan_synchronize(&echan->vchan);
  757. }
  758. static int edma_slave_config(struct dma_chan *chan,
  759. struct dma_slave_config *cfg)
  760. {
  761. struct edma_chan *echan = to_edma_chan(chan);
  762. if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  763. cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  764. return -EINVAL;
  765. if (cfg->src_maxburst > chan->device->max_burst ||
  766. cfg->dst_maxburst > chan->device->max_burst)
  767. return -EINVAL;
  768. memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
  769. return 0;
  770. }
  771. static int edma_dma_pause(struct dma_chan *chan)
  772. {
  773. struct edma_chan *echan = to_edma_chan(chan);
  774. if (!echan->edesc)
  775. return -EINVAL;
  776. edma_pause(echan);
  777. return 0;
  778. }
  779. static int edma_dma_resume(struct dma_chan *chan)
  780. {
  781. struct edma_chan *echan = to_edma_chan(chan);
  782. edma_resume(echan);
  783. return 0;
  784. }
  785. /*
  786. * A PaRAM set configuration abstraction used by other modes
  787. * @chan: Channel who's PaRAM set we're configuring
  788. * @pset: PaRAM set to initialize and setup.
  789. * @src_addr: Source address of the DMA
  790. * @dst_addr: Destination address of the DMA
  791. * @burst: In units of dev_width, how much to send
  792. * @dev_width: How much is the dev_width
  793. * @dma_length: Total length of the DMA transfer
  794. * @direction: Direction of the transfer
  795. */
  796. static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
  797. dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
  798. unsigned int acnt, unsigned int dma_length,
  799. enum dma_transfer_direction direction)
  800. {
  801. struct edma_chan *echan = to_edma_chan(chan);
  802. struct device *dev = chan->device->dev;
  803. struct edmacc_param *param = &epset->param;
  804. int bcnt, ccnt, cidx;
  805. int src_bidx, dst_bidx, src_cidx, dst_cidx;
  806. int absync;
  807. /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
  808. if (!burst)
  809. burst = 1;
  810. /*
  811. * If the maxburst is equal to the fifo width, use
  812. * A-synced transfers. This allows for large contiguous
  813. * buffer transfers using only one PaRAM set.
  814. */
  815. if (burst == 1) {
  816. /*
  817. * For the A-sync case, bcnt and ccnt are the remainder
  818. * and quotient respectively of the division of:
  819. * (dma_length / acnt) by (SZ_64K -1). This is so
  820. * that in case bcnt over flows, we have ccnt to use.
  821. * Note: In A-sync tranfer only, bcntrld is used, but it
  822. * only applies for sg_dma_len(sg) >= SZ_64K.
  823. * In this case, the best way adopted is- bccnt for the
  824. * first frame will be the remainder below. Then for
  825. * every successive frame, bcnt will be SZ_64K-1. This
  826. * is assured as bcntrld = 0xffff in end of function.
  827. */
  828. absync = false;
  829. ccnt = dma_length / acnt / (SZ_64K - 1);
  830. bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
  831. /*
  832. * If bcnt is non-zero, we have a remainder and hence an
  833. * extra frame to transfer, so increment ccnt.
  834. */
  835. if (bcnt)
  836. ccnt++;
  837. else
  838. bcnt = SZ_64K - 1;
  839. cidx = acnt;
  840. } else {
  841. /*
  842. * If maxburst is greater than the fifo address_width,
  843. * use AB-synced transfers where A count is the fifo
  844. * address_width and B count is the maxburst. In this
  845. * case, we are limited to transfers of C count frames
  846. * of (address_width * maxburst) where C count is limited
  847. * to SZ_64K-1. This places an upper bound on the length
  848. * of an SG segment that can be handled.
  849. */
  850. absync = true;
  851. bcnt = burst;
  852. ccnt = dma_length / (acnt * bcnt);
  853. if (ccnt > (SZ_64K - 1)) {
  854. dev_err(dev, "Exceeded max SG segment size\n");
  855. return -EINVAL;
  856. }
  857. cidx = acnt * bcnt;
  858. }
  859. epset->len = dma_length;
  860. if (direction == DMA_MEM_TO_DEV) {
  861. src_bidx = acnt;
  862. src_cidx = cidx;
  863. dst_bidx = 0;
  864. dst_cidx = 0;
  865. epset->addr = src_addr;
  866. } else if (direction == DMA_DEV_TO_MEM) {
  867. src_bidx = 0;
  868. src_cidx = 0;
  869. dst_bidx = acnt;
  870. dst_cidx = cidx;
  871. epset->addr = dst_addr;
  872. } else if (direction == DMA_MEM_TO_MEM) {
  873. src_bidx = acnt;
  874. src_cidx = cidx;
  875. dst_bidx = acnt;
  876. dst_cidx = cidx;
  877. } else {
  878. dev_err(dev, "%s: direction not implemented yet\n", __func__);
  879. return -EINVAL;
  880. }
  881. param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
  882. /* Configure A or AB synchronized transfers */
  883. if (absync)
  884. param->opt |= SYNCDIM;
  885. param->src = src_addr;
  886. param->dst = dst_addr;
  887. param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
  888. param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
  889. param->a_b_cnt = bcnt << 16 | acnt;
  890. param->ccnt = ccnt;
  891. /*
  892. * Only time when (bcntrld) auto reload is required is for
  893. * A-sync case, and in this case, a requirement of reload value
  894. * of SZ_64K-1 only is assured. 'link' is initially set to NULL
  895. * and then later will be populated by edma_execute.
  896. */
  897. param->link_bcntrld = 0xffffffff;
  898. return absync;
  899. }
  900. static struct dma_async_tx_descriptor *edma_prep_slave_sg(
  901. struct dma_chan *chan, struct scatterlist *sgl,
  902. unsigned int sg_len, enum dma_transfer_direction direction,
  903. unsigned long tx_flags, void *context)
  904. {
  905. struct edma_chan *echan = to_edma_chan(chan);
  906. struct device *dev = chan->device->dev;
  907. struct edma_desc *edesc;
  908. dma_addr_t src_addr = 0, dst_addr = 0;
  909. enum dma_slave_buswidth dev_width;
  910. u32 burst;
  911. struct scatterlist *sg;
  912. int i, nslots, ret;
  913. if (unlikely(!echan || !sgl || !sg_len))
  914. return NULL;
  915. if (direction == DMA_DEV_TO_MEM) {
  916. src_addr = echan->cfg.src_addr;
  917. dev_width = echan->cfg.src_addr_width;
  918. burst = echan->cfg.src_maxburst;
  919. } else if (direction == DMA_MEM_TO_DEV) {
  920. dst_addr = echan->cfg.dst_addr;
  921. dev_width = echan->cfg.dst_addr_width;
  922. burst = echan->cfg.dst_maxburst;
  923. } else {
  924. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  925. return NULL;
  926. }
  927. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  928. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  929. return NULL;
  930. }
  931. edesc = kzalloc(struct_size(edesc, pset, sg_len), GFP_ATOMIC);
  932. if (!edesc)
  933. return NULL;
  934. edesc->pset_nr = sg_len;
  935. edesc->residue = 0;
  936. edesc->direction = direction;
  937. edesc->echan = echan;
  938. /* Allocate a PaRAM slot, if needed */
  939. nslots = min_t(unsigned, MAX_NR_SG, sg_len);
  940. for (i = 0; i < nslots; i++) {
  941. if (echan->slot[i] < 0) {
  942. echan->slot[i] =
  943. edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
  944. if (echan->slot[i] < 0) {
  945. kfree(edesc);
  946. dev_err(dev, "%s: Failed to allocate slot\n",
  947. __func__);
  948. return NULL;
  949. }
  950. }
  951. }
  952. /* Configure PaRAM sets for each SG */
  953. for_each_sg(sgl, sg, sg_len, i) {
  954. /* Get address for each SG */
  955. if (direction == DMA_DEV_TO_MEM)
  956. dst_addr = sg_dma_address(sg);
  957. else
  958. src_addr = sg_dma_address(sg);
  959. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  960. dst_addr, burst, dev_width,
  961. sg_dma_len(sg), direction);
  962. if (ret < 0) {
  963. kfree(edesc);
  964. return NULL;
  965. }
  966. edesc->absync = ret;
  967. edesc->residue += sg_dma_len(sg);
  968. if (i == sg_len - 1)
  969. /* Enable completion interrupt */
  970. edesc->pset[i].param.opt |= TCINTEN;
  971. else if (!((i+1) % MAX_NR_SG))
  972. /*
  973. * Enable early completion interrupt for the
  974. * intermediateset. In this case the driver will be
  975. * notified when the paRAM set is submitted to TC. This
  976. * will allow more time to set up the next set of slots.
  977. */
  978. edesc->pset[i].param.opt |= (TCINTEN | TCCMODE);
  979. }
  980. edesc->residue_stat = edesc->residue;
  981. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  982. }
  983. static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
  984. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  985. size_t len, unsigned long tx_flags)
  986. {
  987. int ret, nslots;
  988. struct edma_desc *edesc;
  989. struct device *dev = chan->device->dev;
  990. struct edma_chan *echan = to_edma_chan(chan);
  991. unsigned int width, pset_len, array_size;
  992. if (unlikely(!echan || !len))
  993. return NULL;
  994. /* Align the array size (acnt block) with the transfer properties */
  995. switch (__ffs((src | dest | len))) {
  996. case 0:
  997. array_size = SZ_32K - 1;
  998. break;
  999. case 1:
  1000. array_size = SZ_32K - 2;
  1001. break;
  1002. default:
  1003. array_size = SZ_32K - 4;
  1004. break;
  1005. }
  1006. if (len < SZ_64K) {
  1007. /*
  1008. * Transfer size less than 64K can be handled with one paRAM
  1009. * slot and with one burst.
  1010. * ACNT = length
  1011. */
  1012. width = len;
  1013. pset_len = len;
  1014. nslots = 1;
  1015. } else {
  1016. /*
  1017. * Transfer size bigger than 64K will be handled with maximum of
  1018. * two paRAM slots.
  1019. * slot1: (full_length / 32767) times 32767 bytes bursts.
  1020. * ACNT = 32767, length1: (full_length / 32767) * 32767
  1021. * slot2: the remaining amount of data after slot1.
  1022. * ACNT = full_length - length1, length2 = ACNT
  1023. *
  1024. * When the full_length is multibple of 32767 one slot can be
  1025. * used to complete the transfer.
  1026. */
  1027. width = array_size;
  1028. pset_len = rounddown(len, width);
  1029. /* One slot is enough for lengths multiple of (SZ_32K -1) */
  1030. if (unlikely(pset_len == len))
  1031. nslots = 1;
  1032. else
  1033. nslots = 2;
  1034. }
  1035. edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
  1036. if (!edesc)
  1037. return NULL;
  1038. edesc->pset_nr = nslots;
  1039. edesc->residue = edesc->residue_stat = len;
  1040. edesc->direction = DMA_MEM_TO_MEM;
  1041. edesc->echan = echan;
  1042. ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
  1043. width, pset_len, DMA_MEM_TO_MEM);
  1044. if (ret < 0) {
  1045. kfree(edesc);
  1046. return NULL;
  1047. }
  1048. edesc->absync = ret;
  1049. edesc->pset[0].param.opt |= ITCCHEN;
  1050. if (nslots == 1) {
  1051. /* Enable transfer complete interrupt */
  1052. edesc->pset[0].param.opt |= TCINTEN;
  1053. } else {
  1054. /* Enable transfer complete chaining for the first slot */
  1055. edesc->pset[0].param.opt |= TCCHEN;
  1056. if (echan->slot[1] < 0) {
  1057. echan->slot[1] = edma_alloc_slot(echan->ecc,
  1058. EDMA_SLOT_ANY);
  1059. if (echan->slot[1] < 0) {
  1060. kfree(edesc);
  1061. dev_err(dev, "%s: Failed to allocate slot\n",
  1062. __func__);
  1063. return NULL;
  1064. }
  1065. }
  1066. dest += pset_len;
  1067. src += pset_len;
  1068. pset_len = width = len % array_size;
  1069. ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
  1070. width, pset_len, DMA_MEM_TO_MEM);
  1071. if (ret < 0) {
  1072. kfree(edesc);
  1073. return NULL;
  1074. }
  1075. edesc->pset[1].param.opt |= ITCCHEN;
  1076. edesc->pset[1].param.opt |= TCINTEN;
  1077. }
  1078. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  1079. }
  1080. static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
  1081. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1082. size_t period_len, enum dma_transfer_direction direction,
  1083. unsigned long tx_flags)
  1084. {
  1085. struct edma_chan *echan = to_edma_chan(chan);
  1086. struct device *dev = chan->device->dev;
  1087. struct edma_desc *edesc;
  1088. dma_addr_t src_addr, dst_addr;
  1089. enum dma_slave_buswidth dev_width;
  1090. bool use_intermediate = false;
  1091. u32 burst;
  1092. int i, ret, nslots;
  1093. if (unlikely(!echan || !buf_len || !period_len))
  1094. return NULL;
  1095. if (direction == DMA_DEV_TO_MEM) {
  1096. src_addr = echan->cfg.src_addr;
  1097. dst_addr = buf_addr;
  1098. dev_width = echan->cfg.src_addr_width;
  1099. burst = echan->cfg.src_maxburst;
  1100. } else if (direction == DMA_MEM_TO_DEV) {
  1101. src_addr = buf_addr;
  1102. dst_addr = echan->cfg.dst_addr;
  1103. dev_width = echan->cfg.dst_addr_width;
  1104. burst = echan->cfg.dst_maxburst;
  1105. } else {
  1106. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  1107. return NULL;
  1108. }
  1109. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  1110. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  1111. return NULL;
  1112. }
  1113. if (unlikely(buf_len % period_len)) {
  1114. dev_err(dev, "Period should be multiple of Buffer length\n");
  1115. return NULL;
  1116. }
  1117. nslots = (buf_len / period_len) + 1;
  1118. /*
  1119. * Cyclic DMA users such as audio cannot tolerate delays introduced
  1120. * by cases where the number of periods is more than the maximum
  1121. * number of SGs the EDMA driver can handle at a time. For DMA types
  1122. * such as Slave SGs, such delays are tolerable and synchronized,
  1123. * but the synchronization is difficult to achieve with Cyclic and
  1124. * cannot be guaranteed, so we error out early.
  1125. */
  1126. if (nslots > MAX_NR_SG) {
  1127. /*
  1128. * If the burst and period sizes are the same, we can put
  1129. * the full buffer into a single period and activate
  1130. * intermediate interrupts. This will produce interrupts
  1131. * after each burst, which is also after each desired period.
  1132. */
  1133. if (burst == period_len) {
  1134. period_len = buf_len;
  1135. nslots = 2;
  1136. use_intermediate = true;
  1137. } else {
  1138. return NULL;
  1139. }
  1140. }
  1141. edesc = kzalloc(struct_size(edesc, pset, nslots), GFP_ATOMIC);
  1142. if (!edesc)
  1143. return NULL;
  1144. edesc->cyclic = 1;
  1145. edesc->pset_nr = nslots;
  1146. edesc->residue = edesc->residue_stat = buf_len;
  1147. edesc->direction = direction;
  1148. edesc->echan = echan;
  1149. dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
  1150. __func__, echan->ch_num, nslots, period_len, buf_len);
  1151. for (i = 0; i < nslots; i++) {
  1152. /* Allocate a PaRAM slot, if needed */
  1153. if (echan->slot[i] < 0) {
  1154. echan->slot[i] =
  1155. edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
  1156. if (echan->slot[i] < 0) {
  1157. kfree(edesc);
  1158. dev_err(dev, "%s: Failed to allocate slot\n",
  1159. __func__);
  1160. return NULL;
  1161. }
  1162. }
  1163. if (i == nslots - 1) {
  1164. memcpy(&edesc->pset[i], &edesc->pset[0],
  1165. sizeof(edesc->pset[0]));
  1166. break;
  1167. }
  1168. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  1169. dst_addr, burst, dev_width, period_len,
  1170. direction);
  1171. if (ret < 0) {
  1172. kfree(edesc);
  1173. return NULL;
  1174. }
  1175. if (direction == DMA_DEV_TO_MEM)
  1176. dst_addr += period_len;
  1177. else
  1178. src_addr += period_len;
  1179. dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
  1180. dev_vdbg(dev,
  1181. "\n pset[%d]:\n"
  1182. " chnum\t%d\n"
  1183. " slot\t%d\n"
  1184. " opt\t%08x\n"
  1185. " src\t%08x\n"
  1186. " dst\t%08x\n"
  1187. " abcnt\t%08x\n"
  1188. " ccnt\t%08x\n"
  1189. " bidx\t%08x\n"
  1190. " cidx\t%08x\n"
  1191. " lkrld\t%08x\n",
  1192. i, echan->ch_num, echan->slot[i],
  1193. edesc->pset[i].param.opt,
  1194. edesc->pset[i].param.src,
  1195. edesc->pset[i].param.dst,
  1196. edesc->pset[i].param.a_b_cnt,
  1197. edesc->pset[i].param.ccnt,
  1198. edesc->pset[i].param.src_dst_bidx,
  1199. edesc->pset[i].param.src_dst_cidx,
  1200. edesc->pset[i].param.link_bcntrld);
  1201. edesc->absync = ret;
  1202. /*
  1203. * Enable period interrupt only if it is requested
  1204. */
  1205. if (tx_flags & DMA_PREP_INTERRUPT) {
  1206. edesc->pset[i].param.opt |= TCINTEN;
  1207. /* Also enable intermediate interrupts if necessary */
  1208. if (use_intermediate)
  1209. edesc->pset[i].param.opt |= ITCINTEN;
  1210. }
  1211. }
  1212. /* Place the cyclic channel to highest priority queue */
  1213. if (!echan->tc)
  1214. edma_assign_channel_eventq(echan, EVENTQ_0);
  1215. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  1216. }
  1217. static void edma_completion_handler(struct edma_chan *echan)
  1218. {
  1219. struct device *dev = echan->vchan.chan.device->dev;
  1220. struct edma_desc *edesc;
  1221. spin_lock(&echan->vchan.lock);
  1222. edesc = echan->edesc;
  1223. if (edesc) {
  1224. if (edesc->cyclic) {
  1225. vchan_cyclic_callback(&edesc->vdesc);
  1226. spin_unlock(&echan->vchan.lock);
  1227. return;
  1228. } else if (edesc->processed == edesc->pset_nr) {
  1229. edesc->residue = 0;
  1230. edma_stop(echan);
  1231. vchan_cookie_complete(&edesc->vdesc);
  1232. echan->edesc = NULL;
  1233. dev_dbg(dev, "Transfer completed on channel %d\n",
  1234. echan->ch_num);
  1235. } else {
  1236. dev_dbg(dev, "Sub transfer completed on channel %d\n",
  1237. echan->ch_num);
  1238. edma_pause(echan);
  1239. /* Update statistics for tx_status */
  1240. edesc->residue -= edesc->sg_len;
  1241. edesc->residue_stat = edesc->residue;
  1242. edesc->processed_stat = edesc->processed;
  1243. }
  1244. edma_execute(echan);
  1245. }
  1246. spin_unlock(&echan->vchan.lock);
  1247. }
  1248. /* eDMA interrupt handler */
  1249. static irqreturn_t dma_irq_handler(int irq, void *data)
  1250. {
  1251. struct edma_cc *ecc = data;
  1252. int ctlr;
  1253. u32 sh_ier;
  1254. u32 sh_ipr;
  1255. u32 bank;
  1256. ctlr = ecc->id;
  1257. if (ctlr < 0)
  1258. return IRQ_NONE;
  1259. dev_vdbg(ecc->dev, "dma_irq_handler\n");
  1260. sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
  1261. if (!sh_ipr) {
  1262. sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
  1263. if (!sh_ipr)
  1264. return IRQ_NONE;
  1265. sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
  1266. bank = 1;
  1267. } else {
  1268. sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
  1269. bank = 0;
  1270. }
  1271. do {
  1272. u32 slot;
  1273. u32 channel;
  1274. slot = __ffs(sh_ipr);
  1275. sh_ipr &= ~(BIT(slot));
  1276. if (sh_ier & BIT(slot)) {
  1277. channel = (bank << 5) | slot;
  1278. /* Clear the corresponding IPR bits */
  1279. edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
  1280. edma_completion_handler(&ecc->slave_chans[channel]);
  1281. }
  1282. } while (sh_ipr);
  1283. edma_shadow0_write(ecc, SH_IEVAL, 1);
  1284. return IRQ_HANDLED;
  1285. }
  1286. static void edma_error_handler(struct edma_chan *echan)
  1287. {
  1288. struct edma_cc *ecc = echan->ecc;
  1289. struct device *dev = echan->vchan.chan.device->dev;
  1290. struct edmacc_param p;
  1291. int err;
  1292. if (!echan->edesc)
  1293. return;
  1294. spin_lock(&echan->vchan.lock);
  1295. err = edma_read_slot(ecc, echan->slot[0], &p);
  1296. /*
  1297. * Issue later based on missed flag which will be sure
  1298. * to happen as:
  1299. * (1) we finished transmitting an intermediate slot and
  1300. * edma_execute is coming up.
  1301. * (2) or we finished current transfer and issue will
  1302. * call edma_execute.
  1303. *
  1304. * Important note: issuing can be dangerous here and
  1305. * lead to some nasty recursion when we are in a NULL
  1306. * slot. So we avoid doing so and set the missed flag.
  1307. */
  1308. if (err || (p.a_b_cnt == 0 && p.ccnt == 0)) {
  1309. dev_dbg(dev, "Error on null slot, setting miss\n");
  1310. echan->missed = 1;
  1311. } else {
  1312. /*
  1313. * The slot is already programmed but the event got
  1314. * missed, so its safe to issue it here.
  1315. */
  1316. dev_dbg(dev, "Missed event, TRIGGERING\n");
  1317. edma_clean_channel(echan);
  1318. edma_stop(echan);
  1319. edma_start(echan);
  1320. edma_trigger_channel(echan);
  1321. }
  1322. spin_unlock(&echan->vchan.lock);
  1323. }
  1324. static inline bool edma_error_pending(struct edma_cc *ecc)
  1325. {
  1326. if (edma_read_array(ecc, EDMA_EMR, 0) ||
  1327. edma_read_array(ecc, EDMA_EMR, 1) ||
  1328. edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
  1329. return true;
  1330. return false;
  1331. }
  1332. /* eDMA error interrupt handler */
  1333. static irqreturn_t dma_ccerr_handler(int irq, void *data)
  1334. {
  1335. struct edma_cc *ecc = data;
  1336. int i, j;
  1337. int ctlr;
  1338. unsigned int cnt = 0;
  1339. unsigned int val;
  1340. ctlr = ecc->id;
  1341. if (ctlr < 0)
  1342. return IRQ_NONE;
  1343. dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
  1344. if (!edma_error_pending(ecc)) {
  1345. /*
  1346. * The registers indicate no pending error event but the irq
  1347. * handler has been called.
  1348. * Ask eDMA to re-evaluate the error registers.
  1349. */
  1350. dev_err(ecc->dev, "%s: Error interrupt without error event!\n",
  1351. __func__);
  1352. edma_write(ecc, EDMA_EEVAL, 1);
  1353. return IRQ_NONE;
  1354. }
  1355. while (1) {
  1356. /* Event missed register(s) */
  1357. for (j = 0; j < 2; j++) {
  1358. unsigned long emr;
  1359. val = edma_read_array(ecc, EDMA_EMR, j);
  1360. if (!val)
  1361. continue;
  1362. dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
  1363. emr = val;
  1364. for (i = find_next_bit(&emr, 32, 0); i < 32;
  1365. i = find_next_bit(&emr, 32, i + 1)) {
  1366. int k = (j << 5) + i;
  1367. /* Clear the corresponding EMR bits */
  1368. edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
  1369. /* Clear any SER */
  1370. edma_shadow0_write_array(ecc, SH_SECR, j,
  1371. BIT(i));
  1372. edma_error_handler(&ecc->slave_chans[k]);
  1373. }
  1374. }
  1375. val = edma_read(ecc, EDMA_QEMR);
  1376. if (val) {
  1377. dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
  1378. /* Not reported, just clear the interrupt reason. */
  1379. edma_write(ecc, EDMA_QEMCR, val);
  1380. edma_shadow0_write(ecc, SH_QSECR, val);
  1381. }
  1382. val = edma_read(ecc, EDMA_CCERR);
  1383. if (val) {
  1384. dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
  1385. /* Not reported, just clear the interrupt reason. */
  1386. edma_write(ecc, EDMA_CCERRCLR, val);
  1387. }
  1388. if (!edma_error_pending(ecc))
  1389. break;
  1390. cnt++;
  1391. if (cnt > 10)
  1392. break;
  1393. }
  1394. edma_write(ecc, EDMA_EEVAL, 1);
  1395. return IRQ_HANDLED;
  1396. }
  1397. /* Alloc channel resources */
  1398. static int edma_alloc_chan_resources(struct dma_chan *chan)
  1399. {
  1400. struct edma_chan *echan = to_edma_chan(chan);
  1401. struct edma_cc *ecc = echan->ecc;
  1402. struct device *dev = ecc->dev;
  1403. enum dma_event_q eventq_no = EVENTQ_DEFAULT;
  1404. int ret;
  1405. if (echan->tc) {
  1406. eventq_no = echan->tc->id;
  1407. } else if (ecc->tc_list) {
  1408. /* memcpy channel */
  1409. echan->tc = &ecc->tc_list[ecc->info->default_queue];
  1410. eventq_no = echan->tc->id;
  1411. }
  1412. ret = edma_alloc_channel(echan, eventq_no);
  1413. if (ret)
  1414. return ret;
  1415. echan->slot[0] = edma_alloc_slot(ecc, echan->ch_num);
  1416. if (echan->slot[0] < 0) {
  1417. dev_err(dev, "Entry slot allocation failed for channel %u\n",
  1418. EDMA_CHAN_SLOT(echan->ch_num));
  1419. ret = echan->slot[0];
  1420. goto err_slot;
  1421. }
  1422. /* Set up channel -> slot mapping for the entry slot */
  1423. edma_set_chmap(echan, echan->slot[0]);
  1424. echan->alloced = true;
  1425. dev_dbg(dev, "Got eDMA channel %d for virt channel %d (%s trigger)\n",
  1426. EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id,
  1427. echan->hw_triggered ? "HW" : "SW");
  1428. return 0;
  1429. err_slot:
  1430. edma_free_channel(echan);
  1431. return ret;
  1432. }
  1433. /* Free channel resources */
  1434. static void edma_free_chan_resources(struct dma_chan *chan)
  1435. {
  1436. struct edma_chan *echan = to_edma_chan(chan);
  1437. struct device *dev = echan->ecc->dev;
  1438. int i;
  1439. /* Terminate transfers */
  1440. edma_stop(echan);
  1441. vchan_free_chan_resources(&echan->vchan);
  1442. /* Free EDMA PaRAM slots */
  1443. for (i = 0; i < EDMA_MAX_SLOTS; i++) {
  1444. if (echan->slot[i] >= 0) {
  1445. edma_free_slot(echan->ecc, echan->slot[i]);
  1446. echan->slot[i] = -1;
  1447. }
  1448. }
  1449. /* Set entry slot to the dummy slot */
  1450. edma_set_chmap(echan, echan->ecc->dummy_slot);
  1451. /* Free EDMA channel */
  1452. if (echan->alloced) {
  1453. edma_free_channel(echan);
  1454. echan->alloced = false;
  1455. }
  1456. echan->tc = NULL;
  1457. echan->hw_triggered = false;
  1458. dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
  1459. EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
  1460. }
  1461. /* Send pending descriptor to hardware */
  1462. static void edma_issue_pending(struct dma_chan *chan)
  1463. {
  1464. struct edma_chan *echan = to_edma_chan(chan);
  1465. unsigned long flags;
  1466. spin_lock_irqsave(&echan->vchan.lock, flags);
  1467. if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
  1468. edma_execute(echan);
  1469. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  1470. }
  1471. /*
  1472. * This limit exists to avoid a possible infinite loop when waiting for proof
  1473. * that a particular transfer is completed. This limit can be hit if there
  1474. * are large bursts to/from slow devices or the CPU is never able to catch
  1475. * the DMA hardware idle. On an AM335x transfering 48 bytes from the UART
  1476. * RX-FIFO, as many as 55 loops have been seen.
  1477. */
  1478. #define EDMA_MAX_TR_WAIT_LOOPS 1000
  1479. static u32 edma_residue(struct edma_desc *edesc)
  1480. {
  1481. bool dst = edesc->direction == DMA_DEV_TO_MEM;
  1482. int loop_count = EDMA_MAX_TR_WAIT_LOOPS;
  1483. struct edma_chan *echan = edesc->echan;
  1484. struct edma_pset *pset = edesc->pset;
  1485. dma_addr_t done, pos;
  1486. int i;
  1487. /*
  1488. * We always read the dst/src position from the first RamPar
  1489. * pset. That's the one which is active now.
  1490. */
  1491. pos = edma_get_position(echan->ecc, echan->slot[0], dst);
  1492. /*
  1493. * "pos" may represent a transfer request that is still being
  1494. * processed by the EDMACC or EDMATC. We will busy wait until
  1495. * any one of the situations occurs:
  1496. * 1. the DMA hardware is idle
  1497. * 2. a new transfer request is setup
  1498. * 3. we hit the loop limit
  1499. */
  1500. while (edma_read(echan->ecc, EDMA_CCSTAT) & EDMA_CCSTAT_ACTV) {
  1501. /* check if a new transfer request is setup */
  1502. if (edma_get_position(echan->ecc,
  1503. echan->slot[0], dst) != pos) {
  1504. break;
  1505. }
  1506. if (!--loop_count) {
  1507. dev_dbg_ratelimited(echan->vchan.chan.device->dev,
  1508. "%s: timeout waiting for PaRAM update\n",
  1509. __func__);
  1510. break;
  1511. }
  1512. cpu_relax();
  1513. }
  1514. /*
  1515. * Cyclic is simple. Just subtract pset[0].addr from pos.
  1516. *
  1517. * We never update edesc->residue in the cyclic case, so we
  1518. * can tell the remaining room to the end of the circular
  1519. * buffer.
  1520. */
  1521. if (edesc->cyclic) {
  1522. done = pos - pset->addr;
  1523. edesc->residue_stat = edesc->residue - done;
  1524. return edesc->residue_stat;
  1525. }
  1526. /*
  1527. * For SG operation we catch up with the last processed
  1528. * status.
  1529. */
  1530. pset += edesc->processed_stat;
  1531. for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
  1532. /*
  1533. * If we are inside this pset address range, we know
  1534. * this is the active one. Get the current delta and
  1535. * stop walking the psets.
  1536. */
  1537. if (pos >= pset->addr && pos < pset->addr + pset->len)
  1538. return edesc->residue_stat - (pos - pset->addr);
  1539. /* Otherwise mark it done and update residue_stat. */
  1540. edesc->processed_stat++;
  1541. edesc->residue_stat -= pset->len;
  1542. }
  1543. return edesc->residue_stat;
  1544. }
  1545. /* Check request completion status */
  1546. static enum dma_status edma_tx_status(struct dma_chan *chan,
  1547. dma_cookie_t cookie,
  1548. struct dma_tx_state *txstate)
  1549. {
  1550. struct edma_chan *echan = to_edma_chan(chan);
  1551. struct virt_dma_desc *vdesc;
  1552. enum dma_status ret;
  1553. unsigned long flags;
  1554. ret = dma_cookie_status(chan, cookie, txstate);
  1555. if (ret == DMA_COMPLETE || !txstate)
  1556. return ret;
  1557. spin_lock_irqsave(&echan->vchan.lock, flags);
  1558. if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
  1559. txstate->residue = edma_residue(echan->edesc);
  1560. else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
  1561. txstate->residue = to_edma_desc(&vdesc->tx)->residue;
  1562. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  1563. return ret;
  1564. }
  1565. static bool edma_is_memcpy_channel(int ch_num, s32 *memcpy_channels)
  1566. {
  1567. if (!memcpy_channels)
  1568. return false;
  1569. while (*memcpy_channels != -1) {
  1570. if (*memcpy_channels == ch_num)
  1571. return true;
  1572. memcpy_channels++;
  1573. }
  1574. return false;
  1575. }
  1576. #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  1577. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  1578. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  1579. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  1580. static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
  1581. {
  1582. struct dma_device *s_ddev = &ecc->dma_slave;
  1583. struct dma_device *m_ddev = NULL;
  1584. s32 *memcpy_channels = ecc->info->memcpy_channels;
  1585. int i, j;
  1586. dma_cap_zero(s_ddev->cap_mask);
  1587. dma_cap_set(DMA_SLAVE, s_ddev->cap_mask);
  1588. dma_cap_set(DMA_CYCLIC, s_ddev->cap_mask);
  1589. if (ecc->legacy_mode && !memcpy_channels) {
  1590. dev_warn(ecc->dev,
  1591. "Legacy memcpy is enabled, things might not work\n");
  1592. dma_cap_set(DMA_MEMCPY, s_ddev->cap_mask);
  1593. s_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
  1594. s_ddev->directions = BIT(DMA_MEM_TO_MEM);
  1595. }
  1596. s_ddev->device_prep_slave_sg = edma_prep_slave_sg;
  1597. s_ddev->device_prep_dma_cyclic = edma_prep_dma_cyclic;
  1598. s_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
  1599. s_ddev->device_free_chan_resources = edma_free_chan_resources;
  1600. s_ddev->device_issue_pending = edma_issue_pending;
  1601. s_ddev->device_tx_status = edma_tx_status;
  1602. s_ddev->device_config = edma_slave_config;
  1603. s_ddev->device_pause = edma_dma_pause;
  1604. s_ddev->device_resume = edma_dma_resume;
  1605. s_ddev->device_terminate_all = edma_terminate_all;
  1606. s_ddev->device_synchronize = edma_synchronize;
  1607. s_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
  1608. s_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
  1609. s_ddev->directions |= (BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV));
  1610. s_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1611. s_ddev->max_burst = SZ_32K - 1; /* CIDX: 16bit signed */
  1612. s_ddev->dev = ecc->dev;
  1613. INIT_LIST_HEAD(&s_ddev->channels);
  1614. if (memcpy_channels) {
  1615. m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
  1616. if (!m_ddev) {
  1617. dev_warn(ecc->dev, "memcpy is disabled due to OoM\n");
  1618. memcpy_channels = NULL;
  1619. goto ch_setup;
  1620. }
  1621. ecc->dma_memcpy = m_ddev;
  1622. dma_cap_zero(m_ddev->cap_mask);
  1623. dma_cap_set(DMA_MEMCPY, m_ddev->cap_mask);
  1624. m_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
  1625. m_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
  1626. m_ddev->device_free_chan_resources = edma_free_chan_resources;
  1627. m_ddev->device_issue_pending = edma_issue_pending;
  1628. m_ddev->device_tx_status = edma_tx_status;
  1629. m_ddev->device_config = edma_slave_config;
  1630. m_ddev->device_pause = edma_dma_pause;
  1631. m_ddev->device_resume = edma_dma_resume;
  1632. m_ddev->device_terminate_all = edma_terminate_all;
  1633. m_ddev->device_synchronize = edma_synchronize;
  1634. m_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
  1635. m_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
  1636. m_ddev->directions = BIT(DMA_MEM_TO_MEM);
  1637. m_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1638. m_ddev->dev = ecc->dev;
  1639. INIT_LIST_HEAD(&m_ddev->channels);
  1640. } else if (!ecc->legacy_mode) {
  1641. dev_info(ecc->dev, "memcpy is disabled\n");
  1642. }
  1643. ch_setup:
  1644. for (i = 0; i < ecc->num_channels; i++) {
  1645. struct edma_chan *echan = &ecc->slave_chans[i];
  1646. echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
  1647. echan->ecc = ecc;
  1648. echan->vchan.desc_free = edma_desc_free;
  1649. if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
  1650. vchan_init(&echan->vchan, m_ddev);
  1651. else
  1652. vchan_init(&echan->vchan, s_ddev);
  1653. INIT_LIST_HEAD(&echan->node);
  1654. for (j = 0; j < EDMA_MAX_SLOTS; j++)
  1655. echan->slot[j] = -1;
  1656. }
  1657. }
  1658. static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
  1659. struct edma_cc *ecc)
  1660. {
  1661. int i;
  1662. u32 value, cccfg;
  1663. s8 (*queue_priority_map)[2];
  1664. /* Decode the eDMA3 configuration from CCCFG register */
  1665. cccfg = edma_read(ecc, EDMA_CCCFG);
  1666. value = GET_NUM_REGN(cccfg);
  1667. ecc->num_region = BIT(value);
  1668. value = GET_NUM_DMACH(cccfg);
  1669. ecc->num_channels = BIT(value + 1);
  1670. value = GET_NUM_QDMACH(cccfg);
  1671. ecc->num_qchannels = value * 2;
  1672. value = GET_NUM_PAENTRY(cccfg);
  1673. ecc->num_slots = BIT(value + 4);
  1674. value = GET_NUM_EVQUE(cccfg);
  1675. ecc->num_tc = value + 1;
  1676. ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
  1677. dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
  1678. dev_dbg(dev, "num_region: %u\n", ecc->num_region);
  1679. dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
  1680. dev_dbg(dev, "num_qchannels: %u\n", ecc->num_qchannels);
  1681. dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
  1682. dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
  1683. dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
  1684. /* Nothing need to be done if queue priority is provided */
  1685. if (pdata->queue_priority_mapping)
  1686. return 0;
  1687. /*
  1688. * Configure TC/queue priority as follows:
  1689. * Q0 - priority 0
  1690. * Q1 - priority 1
  1691. * Q2 - priority 2
  1692. * ...
  1693. * The meaning of priority numbers: 0 highest priority, 7 lowest
  1694. * priority. So Q0 is the highest priority queue and the last queue has
  1695. * the lowest priority.
  1696. */
  1697. queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
  1698. GFP_KERNEL);
  1699. if (!queue_priority_map)
  1700. return -ENOMEM;
  1701. for (i = 0; i < ecc->num_tc; i++) {
  1702. queue_priority_map[i][0] = i;
  1703. queue_priority_map[i][1] = i;
  1704. }
  1705. queue_priority_map[i][0] = -1;
  1706. queue_priority_map[i][1] = -1;
  1707. pdata->queue_priority_mapping = queue_priority_map;
  1708. /* Default queue has the lowest priority */
  1709. pdata->default_queue = i - 1;
  1710. return 0;
  1711. }
  1712. #if IS_ENABLED(CONFIG_OF)
  1713. static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
  1714. size_t sz)
  1715. {
  1716. const char pname[] = "ti,edma-xbar-event-map";
  1717. struct resource res;
  1718. void __iomem *xbar;
  1719. s16 (*xbar_chans)[2];
  1720. size_t nelm = sz / sizeof(s16);
  1721. u32 shift, offset, mux;
  1722. int ret, i;
  1723. xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
  1724. if (!xbar_chans)
  1725. return -ENOMEM;
  1726. ret = of_address_to_resource(dev->of_node, 1, &res);
  1727. if (ret)
  1728. return -ENOMEM;
  1729. xbar = devm_ioremap(dev, res.start, resource_size(&res));
  1730. if (!xbar)
  1731. return -ENOMEM;
  1732. ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
  1733. nelm);
  1734. if (ret)
  1735. return -EIO;
  1736. /* Invalidate last entry for the other user of this mess */
  1737. nelm >>= 1;
  1738. xbar_chans[nelm][0] = -1;
  1739. xbar_chans[nelm][1] = -1;
  1740. for (i = 0; i < nelm; i++) {
  1741. shift = (xbar_chans[i][1] & 0x03) << 3;
  1742. offset = xbar_chans[i][1] & 0xfffffffc;
  1743. mux = readl(xbar + offset);
  1744. mux &= ~(0xff << shift);
  1745. mux |= xbar_chans[i][0] << shift;
  1746. writel(mux, (xbar + offset));
  1747. }
  1748. pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
  1749. return 0;
  1750. }
  1751. static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
  1752. bool legacy_mode)
  1753. {
  1754. struct edma_soc_info *info;
  1755. struct property *prop;
  1756. int sz, ret;
  1757. info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
  1758. if (!info)
  1759. return ERR_PTR(-ENOMEM);
  1760. if (legacy_mode) {
  1761. prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
  1762. &sz);
  1763. if (prop) {
  1764. ret = edma_xbar_event_map(dev, info, sz);
  1765. if (ret)
  1766. return ERR_PTR(ret);
  1767. }
  1768. return info;
  1769. }
  1770. /* Get the list of channels allocated to be used for memcpy */
  1771. prop = of_find_property(dev->of_node, "ti,edma-memcpy-channels", &sz);
  1772. if (prop) {
  1773. const char pname[] = "ti,edma-memcpy-channels";
  1774. size_t nelm = sz / sizeof(s32);
  1775. s32 *memcpy_ch;
  1776. memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s32),
  1777. GFP_KERNEL);
  1778. if (!memcpy_ch)
  1779. return ERR_PTR(-ENOMEM);
  1780. ret = of_property_read_u32_array(dev->of_node, pname,
  1781. (u32 *)memcpy_ch, nelm);
  1782. if (ret)
  1783. return ERR_PTR(ret);
  1784. memcpy_ch[nelm] = -1;
  1785. info->memcpy_channels = memcpy_ch;
  1786. }
  1787. prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
  1788. &sz);
  1789. if (prop) {
  1790. const char pname[] = "ti,edma-reserved-slot-ranges";
  1791. u32 (*tmp)[2];
  1792. s16 (*rsv_slots)[2];
  1793. size_t nelm = sz / sizeof(*tmp);
  1794. struct edma_rsv_info *rsv_info;
  1795. int i;
  1796. if (!nelm)
  1797. return info;
  1798. tmp = kcalloc(nelm, sizeof(*tmp), GFP_KERNEL);
  1799. if (!tmp)
  1800. return ERR_PTR(-ENOMEM);
  1801. rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
  1802. if (!rsv_info) {
  1803. kfree(tmp);
  1804. return ERR_PTR(-ENOMEM);
  1805. }
  1806. rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
  1807. GFP_KERNEL);
  1808. if (!rsv_slots) {
  1809. kfree(tmp);
  1810. return ERR_PTR(-ENOMEM);
  1811. }
  1812. ret = of_property_read_u32_array(dev->of_node, pname,
  1813. (u32 *)tmp, nelm * 2);
  1814. if (ret) {
  1815. kfree(tmp);
  1816. return ERR_PTR(ret);
  1817. }
  1818. for (i = 0; i < nelm; i++) {
  1819. rsv_slots[i][0] = tmp[i][0];
  1820. rsv_slots[i][1] = tmp[i][1];
  1821. }
  1822. rsv_slots[nelm][0] = -1;
  1823. rsv_slots[nelm][1] = -1;
  1824. info->rsv = rsv_info;
  1825. info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
  1826. kfree(tmp);
  1827. }
  1828. return info;
  1829. }
  1830. static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
  1831. struct of_dma *ofdma)
  1832. {
  1833. struct edma_cc *ecc = ofdma->of_dma_data;
  1834. struct dma_chan *chan = NULL;
  1835. struct edma_chan *echan;
  1836. int i;
  1837. if (!ecc || dma_spec->args_count < 1)
  1838. return NULL;
  1839. for (i = 0; i < ecc->num_channels; i++) {
  1840. echan = &ecc->slave_chans[i];
  1841. if (echan->ch_num == dma_spec->args[0]) {
  1842. chan = &echan->vchan.chan;
  1843. break;
  1844. }
  1845. }
  1846. if (!chan)
  1847. return NULL;
  1848. if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
  1849. goto out;
  1850. if (!echan->ecc->legacy_mode && dma_spec->args_count == 2 &&
  1851. dma_spec->args[1] < echan->ecc->num_tc) {
  1852. echan->tc = &echan->ecc->tc_list[dma_spec->args[1]];
  1853. goto out;
  1854. }
  1855. return NULL;
  1856. out:
  1857. /* The channel is going to be used as HW synchronized */
  1858. echan->hw_triggered = true;
  1859. return dma_get_slave_channel(chan);
  1860. }
  1861. #else
  1862. static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
  1863. bool legacy_mode)
  1864. {
  1865. return ERR_PTR(-EINVAL);
  1866. }
  1867. static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
  1868. struct of_dma *ofdma)
  1869. {
  1870. return NULL;
  1871. }
  1872. #endif
  1873. static int edma_probe(struct platform_device *pdev)
  1874. {
  1875. struct edma_soc_info *info = pdev->dev.platform_data;
  1876. s8 (*queue_priority_mapping)[2];
  1877. int i, off, ln;
  1878. const s16 (*rsv_slots)[2];
  1879. const s16 (*xbar_chans)[2];
  1880. int irq;
  1881. char *irq_name;
  1882. struct resource *mem;
  1883. struct device_node *node = pdev->dev.of_node;
  1884. struct device *dev = &pdev->dev;
  1885. struct edma_cc *ecc;
  1886. bool legacy_mode = true;
  1887. int ret;
  1888. if (node) {
  1889. const struct of_device_id *match;
  1890. match = of_match_node(edma_of_ids, node);
  1891. if (match && (*(u32 *)match->data) == EDMA_BINDING_TPCC)
  1892. legacy_mode = false;
  1893. info = edma_setup_info_from_dt(dev, legacy_mode);
  1894. if (IS_ERR(info)) {
  1895. dev_err(dev, "failed to get DT data\n");
  1896. return PTR_ERR(info);
  1897. }
  1898. }
  1899. if (!info)
  1900. return -ENODEV;
  1901. pm_runtime_enable(dev);
  1902. ret = pm_runtime_get_sync(dev);
  1903. if (ret < 0) {
  1904. dev_err(dev, "pm_runtime_get_sync() failed\n");
  1905. return ret;
  1906. }
  1907. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1908. if (ret)
  1909. return ret;
  1910. ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
  1911. if (!ecc)
  1912. return -ENOMEM;
  1913. ecc->dev = dev;
  1914. ecc->id = pdev->id;
  1915. ecc->legacy_mode = legacy_mode;
  1916. /* When booting with DT the pdev->id is -1 */
  1917. if (ecc->id < 0)
  1918. ecc->id = 0;
  1919. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
  1920. if (!mem) {
  1921. dev_dbg(dev, "mem resource not found, using index 0\n");
  1922. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1923. if (!mem) {
  1924. dev_err(dev, "no mem resource?\n");
  1925. return -ENODEV;
  1926. }
  1927. }
  1928. ecc->base = devm_ioremap_resource(dev, mem);
  1929. if (IS_ERR(ecc->base))
  1930. return PTR_ERR(ecc->base);
  1931. platform_set_drvdata(pdev, ecc);
  1932. /* Get eDMA3 configuration from IP */
  1933. ret = edma_setup_from_hw(dev, info, ecc);
  1934. if (ret)
  1935. return ret;
  1936. /* Allocate memory based on the information we got from the IP */
  1937. ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
  1938. sizeof(*ecc->slave_chans), GFP_KERNEL);
  1939. if (!ecc->slave_chans)
  1940. return -ENOMEM;
  1941. ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
  1942. sizeof(unsigned long), GFP_KERNEL);
  1943. if (!ecc->slot_inuse)
  1944. return -ENOMEM;
  1945. ecc->default_queue = info->default_queue;
  1946. if (info->rsv) {
  1947. /* Set the reserved slots in inuse list */
  1948. rsv_slots = info->rsv->rsv_slots;
  1949. if (rsv_slots) {
  1950. for (i = 0; rsv_slots[i][0] != -1; i++) {
  1951. off = rsv_slots[i][0];
  1952. ln = rsv_slots[i][1];
  1953. edma_set_bits(off, ln, ecc->slot_inuse);
  1954. }
  1955. }
  1956. }
  1957. for (i = 0; i < ecc->num_slots; i++) {
  1958. /* Reset only unused - not reserved - paRAM slots */
  1959. if (!test_bit(i, ecc->slot_inuse))
  1960. edma_write_slot(ecc, i, &dummy_paramset);
  1961. }
  1962. /* Clear the xbar mapped channels in unused list */
  1963. xbar_chans = info->xbar_chans;
  1964. if (xbar_chans) {
  1965. for (i = 0; xbar_chans[i][1] != -1; i++) {
  1966. off = xbar_chans[i][1];
  1967. }
  1968. }
  1969. irq = platform_get_irq_byname(pdev, "edma3_ccint");
  1970. if (irq < 0 && node)
  1971. irq = irq_of_parse_and_map(node, 0);
  1972. if (irq >= 0) {
  1973. irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
  1974. dev_name(dev));
  1975. ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
  1976. ecc);
  1977. if (ret) {
  1978. dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
  1979. return ret;
  1980. }
  1981. ecc->ccint = irq;
  1982. }
  1983. irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
  1984. if (irq < 0 && node)
  1985. irq = irq_of_parse_and_map(node, 2);
  1986. if (irq >= 0) {
  1987. irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
  1988. dev_name(dev));
  1989. ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
  1990. ecc);
  1991. if (ret) {
  1992. dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
  1993. return ret;
  1994. }
  1995. ecc->ccerrint = irq;
  1996. }
  1997. ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
  1998. if (ecc->dummy_slot < 0) {
  1999. dev_err(dev, "Can't allocate PaRAM dummy slot\n");
  2000. return ecc->dummy_slot;
  2001. }
  2002. queue_priority_mapping = info->queue_priority_mapping;
  2003. if (!ecc->legacy_mode) {
  2004. int lowest_priority = 0;
  2005. struct of_phandle_args tc_args;
  2006. ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
  2007. sizeof(*ecc->tc_list), GFP_KERNEL);
  2008. if (!ecc->tc_list) {
  2009. ret = -ENOMEM;
  2010. goto err_reg1;
  2011. }
  2012. for (i = 0;; i++) {
  2013. ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
  2014. 1, i, &tc_args);
  2015. if (ret || i == ecc->num_tc)
  2016. break;
  2017. ecc->tc_list[i].node = tc_args.np;
  2018. ecc->tc_list[i].id = i;
  2019. queue_priority_mapping[i][1] = tc_args.args[0];
  2020. if (queue_priority_mapping[i][1] > lowest_priority) {
  2021. lowest_priority = queue_priority_mapping[i][1];
  2022. info->default_queue = i;
  2023. }
  2024. }
  2025. }
  2026. /* Event queue priority mapping */
  2027. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  2028. edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
  2029. queue_priority_mapping[i][1]);
  2030. for (i = 0; i < ecc->num_region; i++) {
  2031. edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
  2032. edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
  2033. edma_write_array(ecc, EDMA_QRAE, i, 0x0);
  2034. }
  2035. ecc->info = info;
  2036. /* Init the dma device and channels */
  2037. edma_dma_init(ecc, legacy_mode);
  2038. for (i = 0; i < ecc->num_channels; i++) {
  2039. /* Assign all channels to the default queue */
  2040. edma_assign_channel_eventq(&ecc->slave_chans[i],
  2041. info->default_queue);
  2042. /* Set entry slot to the dummy slot */
  2043. edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
  2044. }
  2045. ecc->dma_slave.filter.map = info->slave_map;
  2046. ecc->dma_slave.filter.mapcnt = info->slavecnt;
  2047. ecc->dma_slave.filter.fn = edma_filter_fn;
  2048. ret = dma_async_device_register(&ecc->dma_slave);
  2049. if (ret) {
  2050. dev_err(dev, "slave ddev registration failed (%d)\n", ret);
  2051. goto err_reg1;
  2052. }
  2053. if (ecc->dma_memcpy) {
  2054. ret = dma_async_device_register(ecc->dma_memcpy);
  2055. if (ret) {
  2056. dev_err(dev, "memcpy ddev registration failed (%d)\n",
  2057. ret);
  2058. dma_async_device_unregister(&ecc->dma_slave);
  2059. goto err_reg1;
  2060. }
  2061. }
  2062. if (node)
  2063. of_dma_controller_register(node, of_edma_xlate, ecc);
  2064. dev_info(dev, "TI EDMA DMA engine driver\n");
  2065. return 0;
  2066. err_reg1:
  2067. edma_free_slot(ecc, ecc->dummy_slot);
  2068. return ret;
  2069. }
  2070. static void edma_cleanupp_vchan(struct dma_device *dmadev)
  2071. {
  2072. struct edma_chan *echan, *_echan;
  2073. list_for_each_entry_safe(echan, _echan,
  2074. &dmadev->channels, vchan.chan.device_node) {
  2075. list_del(&echan->vchan.chan.device_node);
  2076. tasklet_kill(&echan->vchan.task);
  2077. }
  2078. }
  2079. static int edma_remove(struct platform_device *pdev)
  2080. {
  2081. struct device *dev = &pdev->dev;
  2082. struct edma_cc *ecc = dev_get_drvdata(dev);
  2083. devm_free_irq(dev, ecc->ccint, ecc);
  2084. devm_free_irq(dev, ecc->ccerrint, ecc);
  2085. edma_cleanupp_vchan(&ecc->dma_slave);
  2086. if (dev->of_node)
  2087. of_dma_controller_free(dev->of_node);
  2088. dma_async_device_unregister(&ecc->dma_slave);
  2089. if (ecc->dma_memcpy)
  2090. dma_async_device_unregister(ecc->dma_memcpy);
  2091. edma_free_slot(ecc, ecc->dummy_slot);
  2092. return 0;
  2093. }
  2094. #ifdef CONFIG_PM_SLEEP
  2095. static int edma_pm_suspend(struct device *dev)
  2096. {
  2097. struct edma_cc *ecc = dev_get_drvdata(dev);
  2098. struct edma_chan *echan = ecc->slave_chans;
  2099. int i;
  2100. for (i = 0; i < ecc->num_channels; i++) {
  2101. if (echan[i].alloced)
  2102. edma_setup_interrupt(&echan[i], false);
  2103. }
  2104. return 0;
  2105. }
  2106. static int edma_pm_resume(struct device *dev)
  2107. {
  2108. struct edma_cc *ecc = dev_get_drvdata(dev);
  2109. struct edma_chan *echan = ecc->slave_chans;
  2110. int i;
  2111. s8 (*queue_priority_mapping)[2];
  2112. /* re initialize dummy slot to dummy param set */
  2113. edma_write_slot(ecc, ecc->dummy_slot, &dummy_paramset);
  2114. queue_priority_mapping = ecc->info->queue_priority_mapping;
  2115. /* Event queue priority mapping */
  2116. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  2117. edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
  2118. queue_priority_mapping[i][1]);
  2119. for (i = 0; i < ecc->num_channels; i++) {
  2120. if (echan[i].alloced) {
  2121. /* ensure access through shadow region 0 */
  2122. edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
  2123. BIT(i & 0x1f));
  2124. edma_setup_interrupt(&echan[i], true);
  2125. /* Set up channel -> slot mapping for the entry slot */
  2126. edma_set_chmap(&echan[i], echan[i].slot[0]);
  2127. }
  2128. }
  2129. return 0;
  2130. }
  2131. #endif
  2132. static const struct dev_pm_ops edma_pm_ops = {
  2133. SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
  2134. };
  2135. static struct platform_driver edma_driver = {
  2136. .probe = edma_probe,
  2137. .remove = edma_remove,
  2138. .driver = {
  2139. .name = "edma",
  2140. .pm = &edma_pm_ops,
  2141. .of_match_table = edma_of_ids,
  2142. },
  2143. };
  2144. static int edma_tptc_probe(struct platform_device *pdev)
  2145. {
  2146. pm_runtime_enable(&pdev->dev);
  2147. return pm_runtime_get_sync(&pdev->dev);
  2148. }
  2149. static struct platform_driver edma_tptc_driver = {
  2150. .probe = edma_tptc_probe,
  2151. .driver = {
  2152. .name = "edma3-tptc",
  2153. .of_match_table = edma_tptc_of_ids,
  2154. },
  2155. };
  2156. bool edma_filter_fn(struct dma_chan *chan, void *param)
  2157. {
  2158. bool match = false;
  2159. if (chan->device->dev->driver == &edma_driver.driver) {
  2160. struct edma_chan *echan = to_edma_chan(chan);
  2161. unsigned ch_req = *(unsigned *)param;
  2162. if (ch_req == echan->ch_num) {
  2163. /* The channel is going to be used as HW synchronized */
  2164. echan->hw_triggered = true;
  2165. match = true;
  2166. }
  2167. }
  2168. return match;
  2169. }
  2170. EXPORT_SYMBOL(edma_filter_fn);
  2171. static int edma_init(void)
  2172. {
  2173. int ret;
  2174. ret = platform_driver_register(&edma_tptc_driver);
  2175. if (ret)
  2176. return ret;
  2177. return platform_driver_register(&edma_driver);
  2178. }
  2179. subsys_initcall(edma_init);
  2180. static void __exit edma_exit(void)
  2181. {
  2182. platform_driver_unregister(&edma_driver);
  2183. platform_driver_unregister(&edma_tptc_driver);
  2184. }
  2185. module_exit(edma_exit);
  2186. MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
  2187. MODULE_DESCRIPTION("TI EDMA DMA engine driver");
  2188. MODULE_LICENSE("GPL v2");