edma.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  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. /*
  746. * free the running request descriptor
  747. * since it is not in any of the vdesc lists
  748. */
  749. edma_desc_free(&echan->edesc->vdesc);
  750. echan->edesc = NULL;
  751. }
  752. vchan_get_all_descriptors(&echan->vchan, &head);
  753. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  754. vchan_dma_desc_free_list(&echan->vchan, &head);
  755. return 0;
  756. }
  757. static void edma_synchronize(struct dma_chan *chan)
  758. {
  759. struct edma_chan *echan = to_edma_chan(chan);
  760. vchan_synchronize(&echan->vchan);
  761. }
  762. static int edma_slave_config(struct dma_chan *chan,
  763. struct dma_slave_config *cfg)
  764. {
  765. struct edma_chan *echan = to_edma_chan(chan);
  766. if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  767. cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  768. return -EINVAL;
  769. memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
  770. return 0;
  771. }
  772. static int edma_dma_pause(struct dma_chan *chan)
  773. {
  774. struct edma_chan *echan = to_edma_chan(chan);
  775. if (!echan->edesc)
  776. return -EINVAL;
  777. edma_pause(echan);
  778. return 0;
  779. }
  780. static int edma_dma_resume(struct dma_chan *chan)
  781. {
  782. struct edma_chan *echan = to_edma_chan(chan);
  783. edma_resume(echan);
  784. return 0;
  785. }
  786. /*
  787. * A PaRAM set configuration abstraction used by other modes
  788. * @chan: Channel who's PaRAM set we're configuring
  789. * @pset: PaRAM set to initialize and setup.
  790. * @src_addr: Source address of the DMA
  791. * @dst_addr: Destination address of the DMA
  792. * @burst: In units of dev_width, how much to send
  793. * @dev_width: How much is the dev_width
  794. * @dma_length: Total length of the DMA transfer
  795. * @direction: Direction of the transfer
  796. */
  797. static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
  798. dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
  799. unsigned int acnt, unsigned int dma_length,
  800. enum dma_transfer_direction direction)
  801. {
  802. struct edma_chan *echan = to_edma_chan(chan);
  803. struct device *dev = chan->device->dev;
  804. struct edmacc_param *param = &epset->param;
  805. int bcnt, ccnt, cidx;
  806. int src_bidx, dst_bidx, src_cidx, dst_cidx;
  807. int absync;
  808. /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
  809. if (!burst)
  810. burst = 1;
  811. /*
  812. * If the maxburst is equal to the fifo width, use
  813. * A-synced transfers. This allows for large contiguous
  814. * buffer transfers using only one PaRAM set.
  815. */
  816. if (burst == 1) {
  817. /*
  818. * For the A-sync case, bcnt and ccnt are the remainder
  819. * and quotient respectively of the division of:
  820. * (dma_length / acnt) by (SZ_64K -1). This is so
  821. * that in case bcnt over flows, we have ccnt to use.
  822. * Note: In A-sync tranfer only, bcntrld is used, but it
  823. * only applies for sg_dma_len(sg) >= SZ_64K.
  824. * In this case, the best way adopted is- bccnt for the
  825. * first frame will be the remainder below. Then for
  826. * every successive frame, bcnt will be SZ_64K-1. This
  827. * is assured as bcntrld = 0xffff in end of function.
  828. */
  829. absync = false;
  830. ccnt = dma_length / acnt / (SZ_64K - 1);
  831. bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
  832. /*
  833. * If bcnt is non-zero, we have a remainder and hence an
  834. * extra frame to transfer, so increment ccnt.
  835. */
  836. if (bcnt)
  837. ccnt++;
  838. else
  839. bcnt = SZ_64K - 1;
  840. cidx = acnt;
  841. } else {
  842. /*
  843. * If maxburst is greater than the fifo address_width,
  844. * use AB-synced transfers where A count is the fifo
  845. * address_width and B count is the maxburst. In this
  846. * case, we are limited to transfers of C count frames
  847. * of (address_width * maxburst) where C count is limited
  848. * to SZ_64K-1. This places an upper bound on the length
  849. * of an SG segment that can be handled.
  850. */
  851. absync = true;
  852. bcnt = burst;
  853. ccnt = dma_length / (acnt * bcnt);
  854. if (ccnt > (SZ_64K - 1)) {
  855. dev_err(dev, "Exceeded max SG segment size\n");
  856. return -EINVAL;
  857. }
  858. cidx = acnt * bcnt;
  859. }
  860. epset->len = dma_length;
  861. if (direction == DMA_MEM_TO_DEV) {
  862. src_bidx = acnt;
  863. src_cidx = cidx;
  864. dst_bidx = 0;
  865. dst_cidx = 0;
  866. epset->addr = src_addr;
  867. } else if (direction == DMA_DEV_TO_MEM) {
  868. src_bidx = 0;
  869. src_cidx = 0;
  870. dst_bidx = acnt;
  871. dst_cidx = cidx;
  872. epset->addr = dst_addr;
  873. } else if (direction == DMA_MEM_TO_MEM) {
  874. src_bidx = acnt;
  875. src_cidx = cidx;
  876. dst_bidx = acnt;
  877. dst_cidx = cidx;
  878. } else {
  879. dev_err(dev, "%s: direction not implemented yet\n", __func__);
  880. return -EINVAL;
  881. }
  882. param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
  883. /* Configure A or AB synchronized transfers */
  884. if (absync)
  885. param->opt |= SYNCDIM;
  886. param->src = src_addr;
  887. param->dst = dst_addr;
  888. param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
  889. param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
  890. param->a_b_cnt = bcnt << 16 | acnt;
  891. param->ccnt = ccnt;
  892. /*
  893. * Only time when (bcntrld) auto reload is required is for
  894. * A-sync case, and in this case, a requirement of reload value
  895. * of SZ_64K-1 only is assured. 'link' is initially set to NULL
  896. * and then later will be populated by edma_execute.
  897. */
  898. param->link_bcntrld = 0xffffffff;
  899. return absync;
  900. }
  901. static struct dma_async_tx_descriptor *edma_prep_slave_sg(
  902. struct dma_chan *chan, struct scatterlist *sgl,
  903. unsigned int sg_len, enum dma_transfer_direction direction,
  904. unsigned long tx_flags, void *context)
  905. {
  906. struct edma_chan *echan = to_edma_chan(chan);
  907. struct device *dev = chan->device->dev;
  908. struct edma_desc *edesc;
  909. dma_addr_t src_addr = 0, dst_addr = 0;
  910. enum dma_slave_buswidth dev_width;
  911. u32 burst;
  912. struct scatterlist *sg;
  913. int i, nslots, ret;
  914. if (unlikely(!echan || !sgl || !sg_len))
  915. return NULL;
  916. if (direction == DMA_DEV_TO_MEM) {
  917. src_addr = echan->cfg.src_addr;
  918. dev_width = echan->cfg.src_addr_width;
  919. burst = echan->cfg.src_maxburst;
  920. } else if (direction == DMA_MEM_TO_DEV) {
  921. dst_addr = echan->cfg.dst_addr;
  922. dev_width = echan->cfg.dst_addr_width;
  923. burst = echan->cfg.dst_maxburst;
  924. } else {
  925. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  926. return NULL;
  927. }
  928. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  929. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  930. return NULL;
  931. }
  932. edesc = kzalloc(sizeof(*edesc) + sg_len * sizeof(edesc->pset[0]),
  933. GFP_ATOMIC);
  934. if (!edesc)
  935. return NULL;
  936. edesc->pset_nr = sg_len;
  937. edesc->residue = 0;
  938. edesc->direction = direction;
  939. edesc->echan = echan;
  940. /* Allocate a PaRAM slot, if needed */
  941. nslots = min_t(unsigned, MAX_NR_SG, sg_len);
  942. for (i = 0; i < nslots; i++) {
  943. if (echan->slot[i] < 0) {
  944. echan->slot[i] =
  945. edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
  946. if (echan->slot[i] < 0) {
  947. kfree(edesc);
  948. dev_err(dev, "%s: Failed to allocate slot\n",
  949. __func__);
  950. return NULL;
  951. }
  952. }
  953. }
  954. /* Configure PaRAM sets for each SG */
  955. for_each_sg(sgl, sg, sg_len, i) {
  956. /* Get address for each SG */
  957. if (direction == DMA_DEV_TO_MEM)
  958. dst_addr = sg_dma_address(sg);
  959. else
  960. src_addr = sg_dma_address(sg);
  961. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  962. dst_addr, burst, dev_width,
  963. sg_dma_len(sg), direction);
  964. if (ret < 0) {
  965. kfree(edesc);
  966. return NULL;
  967. }
  968. edesc->absync = ret;
  969. edesc->residue += sg_dma_len(sg);
  970. if (i == sg_len - 1)
  971. /* Enable completion interrupt */
  972. edesc->pset[i].param.opt |= TCINTEN;
  973. else if (!((i+1) % MAX_NR_SG))
  974. /*
  975. * Enable early completion interrupt for the
  976. * intermediateset. In this case the driver will be
  977. * notified when the paRAM set is submitted to TC. This
  978. * will allow more time to set up the next set of slots.
  979. */
  980. edesc->pset[i].param.opt |= (TCINTEN | TCCMODE);
  981. }
  982. edesc->residue_stat = edesc->residue;
  983. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  984. }
  985. static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
  986. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  987. size_t len, unsigned long tx_flags)
  988. {
  989. int ret, nslots;
  990. struct edma_desc *edesc;
  991. struct device *dev = chan->device->dev;
  992. struct edma_chan *echan = to_edma_chan(chan);
  993. unsigned int width, pset_len;
  994. if (unlikely(!echan || !len))
  995. return NULL;
  996. if (len < SZ_64K) {
  997. /*
  998. * Transfer size less than 64K can be handled with one paRAM
  999. * slot and with one burst.
  1000. * ACNT = length
  1001. */
  1002. width = len;
  1003. pset_len = len;
  1004. nslots = 1;
  1005. } else {
  1006. /*
  1007. * Transfer size bigger than 64K will be handled with maximum of
  1008. * two paRAM slots.
  1009. * slot1: (full_length / 32767) times 32767 bytes bursts.
  1010. * ACNT = 32767, length1: (full_length / 32767) * 32767
  1011. * slot2: the remaining amount of data after slot1.
  1012. * ACNT = full_length - length1, length2 = ACNT
  1013. *
  1014. * When the full_length is multibple of 32767 one slot can be
  1015. * used to complete the transfer.
  1016. */
  1017. width = SZ_32K - 1;
  1018. pset_len = rounddown(len, width);
  1019. /* One slot is enough for lengths multiple of (SZ_32K -1) */
  1020. if (unlikely(pset_len == len))
  1021. nslots = 1;
  1022. else
  1023. nslots = 2;
  1024. }
  1025. edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
  1026. GFP_ATOMIC);
  1027. if (!edesc)
  1028. return NULL;
  1029. edesc->pset_nr = nslots;
  1030. edesc->residue = edesc->residue_stat = len;
  1031. edesc->direction = DMA_MEM_TO_MEM;
  1032. edesc->echan = echan;
  1033. ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
  1034. width, pset_len, DMA_MEM_TO_MEM);
  1035. if (ret < 0) {
  1036. kfree(edesc);
  1037. return NULL;
  1038. }
  1039. edesc->absync = ret;
  1040. edesc->pset[0].param.opt |= ITCCHEN;
  1041. if (nslots == 1) {
  1042. /* Enable transfer complete interrupt */
  1043. edesc->pset[0].param.opt |= TCINTEN;
  1044. } else {
  1045. /* Enable transfer complete chaining for the first slot */
  1046. edesc->pset[0].param.opt |= TCCHEN;
  1047. if (echan->slot[1] < 0) {
  1048. echan->slot[1] = edma_alloc_slot(echan->ecc,
  1049. EDMA_SLOT_ANY);
  1050. if (echan->slot[1] < 0) {
  1051. kfree(edesc);
  1052. dev_err(dev, "%s: Failed to allocate slot\n",
  1053. __func__);
  1054. return NULL;
  1055. }
  1056. }
  1057. dest += pset_len;
  1058. src += pset_len;
  1059. pset_len = width = len % (SZ_32K - 1);
  1060. ret = edma_config_pset(chan, &edesc->pset[1], src, dest, 1,
  1061. width, pset_len, DMA_MEM_TO_MEM);
  1062. if (ret < 0) {
  1063. kfree(edesc);
  1064. return NULL;
  1065. }
  1066. edesc->pset[1].param.opt |= ITCCHEN;
  1067. edesc->pset[1].param.opt |= TCINTEN;
  1068. }
  1069. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  1070. }
  1071. static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
  1072. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1073. size_t period_len, enum dma_transfer_direction direction,
  1074. unsigned long tx_flags)
  1075. {
  1076. struct edma_chan *echan = to_edma_chan(chan);
  1077. struct device *dev = chan->device->dev;
  1078. struct edma_desc *edesc;
  1079. dma_addr_t src_addr, dst_addr;
  1080. enum dma_slave_buswidth dev_width;
  1081. bool use_intermediate = false;
  1082. u32 burst;
  1083. int i, ret, nslots;
  1084. if (unlikely(!echan || !buf_len || !period_len))
  1085. return NULL;
  1086. if (direction == DMA_DEV_TO_MEM) {
  1087. src_addr = echan->cfg.src_addr;
  1088. dst_addr = buf_addr;
  1089. dev_width = echan->cfg.src_addr_width;
  1090. burst = echan->cfg.src_maxburst;
  1091. } else if (direction == DMA_MEM_TO_DEV) {
  1092. src_addr = buf_addr;
  1093. dst_addr = echan->cfg.dst_addr;
  1094. dev_width = echan->cfg.dst_addr_width;
  1095. burst = echan->cfg.dst_maxburst;
  1096. } else {
  1097. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  1098. return NULL;
  1099. }
  1100. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  1101. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  1102. return NULL;
  1103. }
  1104. if (unlikely(buf_len % period_len)) {
  1105. dev_err(dev, "Period should be multiple of Buffer length\n");
  1106. return NULL;
  1107. }
  1108. nslots = (buf_len / period_len) + 1;
  1109. /*
  1110. * Cyclic DMA users such as audio cannot tolerate delays introduced
  1111. * by cases where the number of periods is more than the maximum
  1112. * number of SGs the EDMA driver can handle at a time. For DMA types
  1113. * such as Slave SGs, such delays are tolerable and synchronized,
  1114. * but the synchronization is difficult to achieve with Cyclic and
  1115. * cannot be guaranteed, so we error out early.
  1116. */
  1117. if (nslots > MAX_NR_SG) {
  1118. /*
  1119. * If the burst and period sizes are the same, we can put
  1120. * the full buffer into a single period and activate
  1121. * intermediate interrupts. This will produce interrupts
  1122. * after each burst, which is also after each desired period.
  1123. */
  1124. if (burst == period_len) {
  1125. period_len = buf_len;
  1126. nslots = 2;
  1127. use_intermediate = true;
  1128. } else {
  1129. return NULL;
  1130. }
  1131. }
  1132. edesc = kzalloc(sizeof(*edesc) + nslots * sizeof(edesc->pset[0]),
  1133. GFP_ATOMIC);
  1134. if (!edesc)
  1135. return NULL;
  1136. edesc->cyclic = 1;
  1137. edesc->pset_nr = nslots;
  1138. edesc->residue = edesc->residue_stat = buf_len;
  1139. edesc->direction = direction;
  1140. edesc->echan = echan;
  1141. dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
  1142. __func__, echan->ch_num, nslots, period_len, buf_len);
  1143. for (i = 0; i < nslots; i++) {
  1144. /* Allocate a PaRAM slot, if needed */
  1145. if (echan->slot[i] < 0) {
  1146. echan->slot[i] =
  1147. edma_alloc_slot(echan->ecc, EDMA_SLOT_ANY);
  1148. if (echan->slot[i] < 0) {
  1149. kfree(edesc);
  1150. dev_err(dev, "%s: Failed to allocate slot\n",
  1151. __func__);
  1152. return NULL;
  1153. }
  1154. }
  1155. if (i == nslots - 1) {
  1156. memcpy(&edesc->pset[i], &edesc->pset[0],
  1157. sizeof(edesc->pset[0]));
  1158. break;
  1159. }
  1160. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  1161. dst_addr, burst, dev_width, period_len,
  1162. direction);
  1163. if (ret < 0) {
  1164. kfree(edesc);
  1165. return NULL;
  1166. }
  1167. if (direction == DMA_DEV_TO_MEM)
  1168. dst_addr += period_len;
  1169. else
  1170. src_addr += period_len;
  1171. dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
  1172. dev_vdbg(dev,
  1173. "\n pset[%d]:\n"
  1174. " chnum\t%d\n"
  1175. " slot\t%d\n"
  1176. " opt\t%08x\n"
  1177. " src\t%08x\n"
  1178. " dst\t%08x\n"
  1179. " abcnt\t%08x\n"
  1180. " ccnt\t%08x\n"
  1181. " bidx\t%08x\n"
  1182. " cidx\t%08x\n"
  1183. " lkrld\t%08x\n",
  1184. i, echan->ch_num, echan->slot[i],
  1185. edesc->pset[i].param.opt,
  1186. edesc->pset[i].param.src,
  1187. edesc->pset[i].param.dst,
  1188. edesc->pset[i].param.a_b_cnt,
  1189. edesc->pset[i].param.ccnt,
  1190. edesc->pset[i].param.src_dst_bidx,
  1191. edesc->pset[i].param.src_dst_cidx,
  1192. edesc->pset[i].param.link_bcntrld);
  1193. edesc->absync = ret;
  1194. /*
  1195. * Enable period interrupt only if it is requested
  1196. */
  1197. if (tx_flags & DMA_PREP_INTERRUPT) {
  1198. edesc->pset[i].param.opt |= TCINTEN;
  1199. /* Also enable intermediate interrupts if necessary */
  1200. if (use_intermediate)
  1201. edesc->pset[i].param.opt |= ITCINTEN;
  1202. }
  1203. }
  1204. /* Place the cyclic channel to highest priority queue */
  1205. if (!echan->tc)
  1206. edma_assign_channel_eventq(echan, EVENTQ_0);
  1207. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  1208. }
  1209. static void edma_completion_handler(struct edma_chan *echan)
  1210. {
  1211. struct device *dev = echan->vchan.chan.device->dev;
  1212. struct edma_desc *edesc;
  1213. spin_lock(&echan->vchan.lock);
  1214. edesc = echan->edesc;
  1215. if (edesc) {
  1216. if (edesc->cyclic) {
  1217. vchan_cyclic_callback(&edesc->vdesc);
  1218. spin_unlock(&echan->vchan.lock);
  1219. return;
  1220. } else if (edesc->processed == edesc->pset_nr) {
  1221. edesc->residue = 0;
  1222. edma_stop(echan);
  1223. vchan_cookie_complete(&edesc->vdesc);
  1224. echan->edesc = NULL;
  1225. dev_dbg(dev, "Transfer completed on channel %d\n",
  1226. echan->ch_num);
  1227. } else {
  1228. dev_dbg(dev, "Sub transfer completed on channel %d\n",
  1229. echan->ch_num);
  1230. edma_pause(echan);
  1231. /* Update statistics for tx_status */
  1232. edesc->residue -= edesc->sg_len;
  1233. edesc->residue_stat = edesc->residue;
  1234. edesc->processed_stat = edesc->processed;
  1235. }
  1236. edma_execute(echan);
  1237. }
  1238. spin_unlock(&echan->vchan.lock);
  1239. }
  1240. /* eDMA interrupt handler */
  1241. static irqreturn_t dma_irq_handler(int irq, void *data)
  1242. {
  1243. struct edma_cc *ecc = data;
  1244. int ctlr;
  1245. u32 sh_ier;
  1246. u32 sh_ipr;
  1247. u32 bank;
  1248. ctlr = ecc->id;
  1249. if (ctlr < 0)
  1250. return IRQ_NONE;
  1251. dev_vdbg(ecc->dev, "dma_irq_handler\n");
  1252. sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 0);
  1253. if (!sh_ipr) {
  1254. sh_ipr = edma_shadow0_read_array(ecc, SH_IPR, 1);
  1255. if (!sh_ipr)
  1256. return IRQ_NONE;
  1257. sh_ier = edma_shadow0_read_array(ecc, SH_IER, 1);
  1258. bank = 1;
  1259. } else {
  1260. sh_ier = edma_shadow0_read_array(ecc, SH_IER, 0);
  1261. bank = 0;
  1262. }
  1263. do {
  1264. u32 slot;
  1265. u32 channel;
  1266. slot = __ffs(sh_ipr);
  1267. sh_ipr &= ~(BIT(slot));
  1268. if (sh_ier & BIT(slot)) {
  1269. channel = (bank << 5) | slot;
  1270. /* Clear the corresponding IPR bits */
  1271. edma_shadow0_write_array(ecc, SH_ICR, bank, BIT(slot));
  1272. edma_completion_handler(&ecc->slave_chans[channel]);
  1273. }
  1274. } while (sh_ipr);
  1275. edma_shadow0_write(ecc, SH_IEVAL, 1);
  1276. return IRQ_HANDLED;
  1277. }
  1278. static void edma_error_handler(struct edma_chan *echan)
  1279. {
  1280. struct edma_cc *ecc = echan->ecc;
  1281. struct device *dev = echan->vchan.chan.device->dev;
  1282. struct edmacc_param p;
  1283. int err;
  1284. if (!echan->edesc)
  1285. return;
  1286. spin_lock(&echan->vchan.lock);
  1287. err = edma_read_slot(ecc, echan->slot[0], &p);
  1288. /*
  1289. * Issue later based on missed flag which will be sure
  1290. * to happen as:
  1291. * (1) we finished transmitting an intermediate slot and
  1292. * edma_execute is coming up.
  1293. * (2) or we finished current transfer and issue will
  1294. * call edma_execute.
  1295. *
  1296. * Important note: issuing can be dangerous here and
  1297. * lead to some nasty recursion when we are in a NULL
  1298. * slot. So we avoid doing so and set the missed flag.
  1299. */
  1300. if (err || (p.a_b_cnt == 0 && p.ccnt == 0)) {
  1301. dev_dbg(dev, "Error on null slot, setting miss\n");
  1302. echan->missed = 1;
  1303. } else {
  1304. /*
  1305. * The slot is already programmed but the event got
  1306. * missed, so its safe to issue it here.
  1307. */
  1308. dev_dbg(dev, "Missed event, TRIGGERING\n");
  1309. edma_clean_channel(echan);
  1310. edma_stop(echan);
  1311. edma_start(echan);
  1312. edma_trigger_channel(echan);
  1313. }
  1314. spin_unlock(&echan->vchan.lock);
  1315. }
  1316. static inline bool edma_error_pending(struct edma_cc *ecc)
  1317. {
  1318. if (edma_read_array(ecc, EDMA_EMR, 0) ||
  1319. edma_read_array(ecc, EDMA_EMR, 1) ||
  1320. edma_read(ecc, EDMA_QEMR) || edma_read(ecc, EDMA_CCERR))
  1321. return true;
  1322. return false;
  1323. }
  1324. /* eDMA error interrupt handler */
  1325. static irqreturn_t dma_ccerr_handler(int irq, void *data)
  1326. {
  1327. struct edma_cc *ecc = data;
  1328. int i, j;
  1329. int ctlr;
  1330. unsigned int cnt = 0;
  1331. unsigned int val;
  1332. ctlr = ecc->id;
  1333. if (ctlr < 0)
  1334. return IRQ_NONE;
  1335. dev_vdbg(ecc->dev, "dma_ccerr_handler\n");
  1336. if (!edma_error_pending(ecc)) {
  1337. /*
  1338. * The registers indicate no pending error event but the irq
  1339. * handler has been called.
  1340. * Ask eDMA to re-evaluate the error registers.
  1341. */
  1342. dev_err(ecc->dev, "%s: Error interrupt without error event!\n",
  1343. __func__);
  1344. edma_write(ecc, EDMA_EEVAL, 1);
  1345. return IRQ_NONE;
  1346. }
  1347. while (1) {
  1348. /* Event missed register(s) */
  1349. for (j = 0; j < 2; j++) {
  1350. unsigned long emr;
  1351. val = edma_read_array(ecc, EDMA_EMR, j);
  1352. if (!val)
  1353. continue;
  1354. dev_dbg(ecc->dev, "EMR%d 0x%08x\n", j, val);
  1355. emr = val;
  1356. for (i = find_next_bit(&emr, 32, 0); i < 32;
  1357. i = find_next_bit(&emr, 32, i + 1)) {
  1358. int k = (j << 5) + i;
  1359. /* Clear the corresponding EMR bits */
  1360. edma_write_array(ecc, EDMA_EMCR, j, BIT(i));
  1361. /* Clear any SER */
  1362. edma_shadow0_write_array(ecc, SH_SECR, j,
  1363. BIT(i));
  1364. edma_error_handler(&ecc->slave_chans[k]);
  1365. }
  1366. }
  1367. val = edma_read(ecc, EDMA_QEMR);
  1368. if (val) {
  1369. dev_dbg(ecc->dev, "QEMR 0x%02x\n", val);
  1370. /* Not reported, just clear the interrupt reason. */
  1371. edma_write(ecc, EDMA_QEMCR, val);
  1372. edma_shadow0_write(ecc, SH_QSECR, val);
  1373. }
  1374. val = edma_read(ecc, EDMA_CCERR);
  1375. if (val) {
  1376. dev_warn(ecc->dev, "CCERR 0x%08x\n", val);
  1377. /* Not reported, just clear the interrupt reason. */
  1378. edma_write(ecc, EDMA_CCERRCLR, val);
  1379. }
  1380. if (!edma_error_pending(ecc))
  1381. break;
  1382. cnt++;
  1383. if (cnt > 10)
  1384. break;
  1385. }
  1386. edma_write(ecc, EDMA_EEVAL, 1);
  1387. return IRQ_HANDLED;
  1388. }
  1389. /* Alloc channel resources */
  1390. static int edma_alloc_chan_resources(struct dma_chan *chan)
  1391. {
  1392. struct edma_chan *echan = to_edma_chan(chan);
  1393. struct edma_cc *ecc = echan->ecc;
  1394. struct device *dev = ecc->dev;
  1395. enum dma_event_q eventq_no = EVENTQ_DEFAULT;
  1396. int ret;
  1397. if (echan->tc) {
  1398. eventq_no = echan->tc->id;
  1399. } else if (ecc->tc_list) {
  1400. /* memcpy channel */
  1401. echan->tc = &ecc->tc_list[ecc->info->default_queue];
  1402. eventq_no = echan->tc->id;
  1403. }
  1404. ret = edma_alloc_channel(echan, eventq_no);
  1405. if (ret)
  1406. return ret;
  1407. echan->slot[0] = edma_alloc_slot(ecc, echan->ch_num);
  1408. if (echan->slot[0] < 0) {
  1409. dev_err(dev, "Entry slot allocation failed for channel %u\n",
  1410. EDMA_CHAN_SLOT(echan->ch_num));
  1411. ret = echan->slot[0];
  1412. goto err_slot;
  1413. }
  1414. /* Set up channel -> slot mapping for the entry slot */
  1415. edma_set_chmap(echan, echan->slot[0]);
  1416. echan->alloced = true;
  1417. dev_dbg(dev, "Got eDMA channel %d for virt channel %d (%s trigger)\n",
  1418. EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id,
  1419. echan->hw_triggered ? "HW" : "SW");
  1420. return 0;
  1421. err_slot:
  1422. edma_free_channel(echan);
  1423. return ret;
  1424. }
  1425. /* Free channel resources */
  1426. static void edma_free_chan_resources(struct dma_chan *chan)
  1427. {
  1428. struct edma_chan *echan = to_edma_chan(chan);
  1429. struct device *dev = echan->ecc->dev;
  1430. int i;
  1431. /* Terminate transfers */
  1432. edma_stop(echan);
  1433. vchan_free_chan_resources(&echan->vchan);
  1434. /* Free EDMA PaRAM slots */
  1435. for (i = 0; i < EDMA_MAX_SLOTS; i++) {
  1436. if (echan->slot[i] >= 0) {
  1437. edma_free_slot(echan->ecc, echan->slot[i]);
  1438. echan->slot[i] = -1;
  1439. }
  1440. }
  1441. /* Set entry slot to the dummy slot */
  1442. edma_set_chmap(echan, echan->ecc->dummy_slot);
  1443. /* Free EDMA channel */
  1444. if (echan->alloced) {
  1445. edma_free_channel(echan);
  1446. echan->alloced = false;
  1447. }
  1448. echan->tc = NULL;
  1449. echan->hw_triggered = false;
  1450. dev_dbg(dev, "Free eDMA channel %d for virt channel %d\n",
  1451. EDMA_CHAN_SLOT(echan->ch_num), chan->chan_id);
  1452. }
  1453. /* Send pending descriptor to hardware */
  1454. static void edma_issue_pending(struct dma_chan *chan)
  1455. {
  1456. struct edma_chan *echan = to_edma_chan(chan);
  1457. unsigned long flags;
  1458. spin_lock_irqsave(&echan->vchan.lock, flags);
  1459. if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
  1460. edma_execute(echan);
  1461. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  1462. }
  1463. /*
  1464. * This limit exists to avoid a possible infinite loop when waiting for proof
  1465. * that a particular transfer is completed. This limit can be hit if there
  1466. * are large bursts to/from slow devices or the CPU is never able to catch
  1467. * the DMA hardware idle. On an AM335x transfering 48 bytes from the UART
  1468. * RX-FIFO, as many as 55 loops have been seen.
  1469. */
  1470. #define EDMA_MAX_TR_WAIT_LOOPS 1000
  1471. static u32 edma_residue(struct edma_desc *edesc)
  1472. {
  1473. bool dst = edesc->direction == DMA_DEV_TO_MEM;
  1474. int loop_count = EDMA_MAX_TR_WAIT_LOOPS;
  1475. struct edma_chan *echan = edesc->echan;
  1476. struct edma_pset *pset = edesc->pset;
  1477. dma_addr_t done, pos;
  1478. int i;
  1479. /*
  1480. * We always read the dst/src position from the first RamPar
  1481. * pset. That's the one which is active now.
  1482. */
  1483. pos = edma_get_position(echan->ecc, echan->slot[0], dst);
  1484. /*
  1485. * "pos" may represent a transfer request that is still being
  1486. * processed by the EDMACC or EDMATC. We will busy wait until
  1487. * any one of the situations occurs:
  1488. * 1. the DMA hardware is idle
  1489. * 2. a new transfer request is setup
  1490. * 3. we hit the loop limit
  1491. */
  1492. while (edma_read(echan->ecc, EDMA_CCSTAT) & EDMA_CCSTAT_ACTV) {
  1493. /* check if a new transfer request is setup */
  1494. if (edma_get_position(echan->ecc,
  1495. echan->slot[0], dst) != pos) {
  1496. break;
  1497. }
  1498. if (!--loop_count) {
  1499. dev_dbg_ratelimited(echan->vchan.chan.device->dev,
  1500. "%s: timeout waiting for PaRAM update\n",
  1501. __func__);
  1502. break;
  1503. }
  1504. cpu_relax();
  1505. }
  1506. /*
  1507. * Cyclic is simple. Just subtract pset[0].addr from pos.
  1508. *
  1509. * We never update edesc->residue in the cyclic case, so we
  1510. * can tell the remaining room to the end of the circular
  1511. * buffer.
  1512. */
  1513. if (edesc->cyclic) {
  1514. done = pos - pset->addr;
  1515. edesc->residue_stat = edesc->residue - done;
  1516. return edesc->residue_stat;
  1517. }
  1518. /*
  1519. * For SG operation we catch up with the last processed
  1520. * status.
  1521. */
  1522. pset += edesc->processed_stat;
  1523. for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
  1524. /*
  1525. * If we are inside this pset address range, we know
  1526. * this is the active one. Get the current delta and
  1527. * stop walking the psets.
  1528. */
  1529. if (pos >= pset->addr && pos < pset->addr + pset->len)
  1530. return edesc->residue_stat - (pos - pset->addr);
  1531. /* Otherwise mark it done and update residue_stat. */
  1532. edesc->processed_stat++;
  1533. edesc->residue_stat -= pset->len;
  1534. }
  1535. return edesc->residue_stat;
  1536. }
  1537. /* Check request completion status */
  1538. static enum dma_status edma_tx_status(struct dma_chan *chan,
  1539. dma_cookie_t cookie,
  1540. struct dma_tx_state *txstate)
  1541. {
  1542. struct edma_chan *echan = to_edma_chan(chan);
  1543. struct virt_dma_desc *vdesc;
  1544. enum dma_status ret;
  1545. unsigned long flags;
  1546. ret = dma_cookie_status(chan, cookie, txstate);
  1547. if (ret == DMA_COMPLETE || !txstate)
  1548. return ret;
  1549. spin_lock_irqsave(&echan->vchan.lock, flags);
  1550. if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
  1551. txstate->residue = edma_residue(echan->edesc);
  1552. else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
  1553. txstate->residue = to_edma_desc(&vdesc->tx)->residue;
  1554. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  1555. return ret;
  1556. }
  1557. static bool edma_is_memcpy_channel(int ch_num, s32 *memcpy_channels)
  1558. {
  1559. if (!memcpy_channels)
  1560. return false;
  1561. while (*memcpy_channels != -1) {
  1562. if (*memcpy_channels == ch_num)
  1563. return true;
  1564. memcpy_channels++;
  1565. }
  1566. return false;
  1567. }
  1568. #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  1569. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  1570. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  1571. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  1572. static void edma_dma_init(struct edma_cc *ecc, bool legacy_mode)
  1573. {
  1574. struct dma_device *s_ddev = &ecc->dma_slave;
  1575. struct dma_device *m_ddev = NULL;
  1576. s32 *memcpy_channels = ecc->info->memcpy_channels;
  1577. int i, j;
  1578. dma_cap_zero(s_ddev->cap_mask);
  1579. dma_cap_set(DMA_SLAVE, s_ddev->cap_mask);
  1580. dma_cap_set(DMA_CYCLIC, s_ddev->cap_mask);
  1581. if (ecc->legacy_mode && !memcpy_channels) {
  1582. dev_warn(ecc->dev,
  1583. "Legacy memcpy is enabled, things might not work\n");
  1584. dma_cap_set(DMA_MEMCPY, s_ddev->cap_mask);
  1585. s_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
  1586. s_ddev->directions = BIT(DMA_MEM_TO_MEM);
  1587. }
  1588. s_ddev->device_prep_slave_sg = edma_prep_slave_sg;
  1589. s_ddev->device_prep_dma_cyclic = edma_prep_dma_cyclic;
  1590. s_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
  1591. s_ddev->device_free_chan_resources = edma_free_chan_resources;
  1592. s_ddev->device_issue_pending = edma_issue_pending;
  1593. s_ddev->device_tx_status = edma_tx_status;
  1594. s_ddev->device_config = edma_slave_config;
  1595. s_ddev->device_pause = edma_dma_pause;
  1596. s_ddev->device_resume = edma_dma_resume;
  1597. s_ddev->device_terminate_all = edma_terminate_all;
  1598. s_ddev->device_synchronize = edma_synchronize;
  1599. s_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
  1600. s_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
  1601. s_ddev->directions |= (BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV));
  1602. s_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1603. s_ddev->dev = ecc->dev;
  1604. INIT_LIST_HEAD(&s_ddev->channels);
  1605. if (memcpy_channels) {
  1606. m_ddev = devm_kzalloc(ecc->dev, sizeof(*m_ddev), GFP_KERNEL);
  1607. ecc->dma_memcpy = m_ddev;
  1608. dma_cap_zero(m_ddev->cap_mask);
  1609. dma_cap_set(DMA_MEMCPY, m_ddev->cap_mask);
  1610. m_ddev->device_prep_dma_memcpy = edma_prep_dma_memcpy;
  1611. m_ddev->device_alloc_chan_resources = edma_alloc_chan_resources;
  1612. m_ddev->device_free_chan_resources = edma_free_chan_resources;
  1613. m_ddev->device_issue_pending = edma_issue_pending;
  1614. m_ddev->device_tx_status = edma_tx_status;
  1615. m_ddev->device_config = edma_slave_config;
  1616. m_ddev->device_pause = edma_dma_pause;
  1617. m_ddev->device_resume = edma_dma_resume;
  1618. m_ddev->device_terminate_all = edma_terminate_all;
  1619. m_ddev->device_synchronize = edma_synchronize;
  1620. m_ddev->src_addr_widths = EDMA_DMA_BUSWIDTHS;
  1621. m_ddev->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
  1622. m_ddev->directions = BIT(DMA_MEM_TO_MEM);
  1623. m_ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1624. m_ddev->dev = ecc->dev;
  1625. INIT_LIST_HEAD(&m_ddev->channels);
  1626. } else if (!ecc->legacy_mode) {
  1627. dev_info(ecc->dev, "memcpy is disabled\n");
  1628. }
  1629. for (i = 0; i < ecc->num_channels; i++) {
  1630. struct edma_chan *echan = &ecc->slave_chans[i];
  1631. echan->ch_num = EDMA_CTLR_CHAN(ecc->id, i);
  1632. echan->ecc = ecc;
  1633. echan->vchan.desc_free = edma_desc_free;
  1634. if (m_ddev && edma_is_memcpy_channel(i, memcpy_channels))
  1635. vchan_init(&echan->vchan, m_ddev);
  1636. else
  1637. vchan_init(&echan->vchan, s_ddev);
  1638. INIT_LIST_HEAD(&echan->node);
  1639. for (j = 0; j < EDMA_MAX_SLOTS; j++)
  1640. echan->slot[j] = -1;
  1641. }
  1642. }
  1643. static int edma_setup_from_hw(struct device *dev, struct edma_soc_info *pdata,
  1644. struct edma_cc *ecc)
  1645. {
  1646. int i;
  1647. u32 value, cccfg;
  1648. s8 (*queue_priority_map)[2];
  1649. /* Decode the eDMA3 configuration from CCCFG register */
  1650. cccfg = edma_read(ecc, EDMA_CCCFG);
  1651. value = GET_NUM_REGN(cccfg);
  1652. ecc->num_region = BIT(value);
  1653. value = GET_NUM_DMACH(cccfg);
  1654. ecc->num_channels = BIT(value + 1);
  1655. value = GET_NUM_QDMACH(cccfg);
  1656. ecc->num_qchannels = value * 2;
  1657. value = GET_NUM_PAENTRY(cccfg);
  1658. ecc->num_slots = BIT(value + 4);
  1659. value = GET_NUM_EVQUE(cccfg);
  1660. ecc->num_tc = value + 1;
  1661. ecc->chmap_exist = (cccfg & CHMAP_EXIST) ? true : false;
  1662. dev_dbg(dev, "eDMA3 CC HW configuration (cccfg: 0x%08x):\n", cccfg);
  1663. dev_dbg(dev, "num_region: %u\n", ecc->num_region);
  1664. dev_dbg(dev, "num_channels: %u\n", ecc->num_channels);
  1665. dev_dbg(dev, "num_qchannels: %u\n", ecc->num_qchannels);
  1666. dev_dbg(dev, "num_slots: %u\n", ecc->num_slots);
  1667. dev_dbg(dev, "num_tc: %u\n", ecc->num_tc);
  1668. dev_dbg(dev, "chmap_exist: %s\n", ecc->chmap_exist ? "yes" : "no");
  1669. /* Nothing need to be done if queue priority is provided */
  1670. if (pdata->queue_priority_mapping)
  1671. return 0;
  1672. /*
  1673. * Configure TC/queue priority as follows:
  1674. * Q0 - priority 0
  1675. * Q1 - priority 1
  1676. * Q2 - priority 2
  1677. * ...
  1678. * The meaning of priority numbers: 0 highest priority, 7 lowest
  1679. * priority. So Q0 is the highest priority queue and the last queue has
  1680. * the lowest priority.
  1681. */
  1682. queue_priority_map = devm_kcalloc(dev, ecc->num_tc + 1, sizeof(s8),
  1683. GFP_KERNEL);
  1684. if (!queue_priority_map)
  1685. return -ENOMEM;
  1686. for (i = 0; i < ecc->num_tc; i++) {
  1687. queue_priority_map[i][0] = i;
  1688. queue_priority_map[i][1] = i;
  1689. }
  1690. queue_priority_map[i][0] = -1;
  1691. queue_priority_map[i][1] = -1;
  1692. pdata->queue_priority_mapping = queue_priority_map;
  1693. /* Default queue has the lowest priority */
  1694. pdata->default_queue = i - 1;
  1695. return 0;
  1696. }
  1697. #if IS_ENABLED(CONFIG_OF)
  1698. static int edma_xbar_event_map(struct device *dev, struct edma_soc_info *pdata,
  1699. size_t sz)
  1700. {
  1701. const char pname[] = "ti,edma-xbar-event-map";
  1702. struct resource res;
  1703. void __iomem *xbar;
  1704. s16 (*xbar_chans)[2];
  1705. size_t nelm = sz / sizeof(s16);
  1706. u32 shift, offset, mux;
  1707. int ret, i;
  1708. xbar_chans = devm_kcalloc(dev, nelm + 2, sizeof(s16), GFP_KERNEL);
  1709. if (!xbar_chans)
  1710. return -ENOMEM;
  1711. ret = of_address_to_resource(dev->of_node, 1, &res);
  1712. if (ret)
  1713. return -ENOMEM;
  1714. xbar = devm_ioremap(dev, res.start, resource_size(&res));
  1715. if (!xbar)
  1716. return -ENOMEM;
  1717. ret = of_property_read_u16_array(dev->of_node, pname, (u16 *)xbar_chans,
  1718. nelm);
  1719. if (ret)
  1720. return -EIO;
  1721. /* Invalidate last entry for the other user of this mess */
  1722. nelm >>= 1;
  1723. xbar_chans[nelm][0] = -1;
  1724. xbar_chans[nelm][1] = -1;
  1725. for (i = 0; i < nelm; i++) {
  1726. shift = (xbar_chans[i][1] & 0x03) << 3;
  1727. offset = xbar_chans[i][1] & 0xfffffffc;
  1728. mux = readl(xbar + offset);
  1729. mux &= ~(0xff << shift);
  1730. mux |= xbar_chans[i][0] << shift;
  1731. writel(mux, (xbar + offset));
  1732. }
  1733. pdata->xbar_chans = (const s16 (*)[2]) xbar_chans;
  1734. return 0;
  1735. }
  1736. static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
  1737. bool legacy_mode)
  1738. {
  1739. struct edma_soc_info *info;
  1740. struct property *prop;
  1741. int sz, ret;
  1742. info = devm_kzalloc(dev, sizeof(struct edma_soc_info), GFP_KERNEL);
  1743. if (!info)
  1744. return ERR_PTR(-ENOMEM);
  1745. if (legacy_mode) {
  1746. prop = of_find_property(dev->of_node, "ti,edma-xbar-event-map",
  1747. &sz);
  1748. if (prop) {
  1749. ret = edma_xbar_event_map(dev, info, sz);
  1750. if (ret)
  1751. return ERR_PTR(ret);
  1752. }
  1753. return info;
  1754. }
  1755. /* Get the list of channels allocated to be used for memcpy */
  1756. prop = of_find_property(dev->of_node, "ti,edma-memcpy-channels", &sz);
  1757. if (prop) {
  1758. const char pname[] = "ti,edma-memcpy-channels";
  1759. size_t nelm = sz / sizeof(s32);
  1760. s32 *memcpy_ch;
  1761. memcpy_ch = devm_kcalloc(dev, nelm + 1, sizeof(s32),
  1762. GFP_KERNEL);
  1763. if (!memcpy_ch)
  1764. return ERR_PTR(-ENOMEM);
  1765. ret = of_property_read_u32_array(dev->of_node, pname,
  1766. (u32 *)memcpy_ch, nelm);
  1767. if (ret)
  1768. return ERR_PTR(ret);
  1769. memcpy_ch[nelm] = -1;
  1770. info->memcpy_channels = memcpy_ch;
  1771. }
  1772. prop = of_find_property(dev->of_node, "ti,edma-reserved-slot-ranges",
  1773. &sz);
  1774. if (prop) {
  1775. const char pname[] = "ti,edma-reserved-slot-ranges";
  1776. u32 (*tmp)[2];
  1777. s16 (*rsv_slots)[2];
  1778. size_t nelm = sz / sizeof(*tmp);
  1779. struct edma_rsv_info *rsv_info;
  1780. int i;
  1781. if (!nelm)
  1782. return info;
  1783. tmp = kcalloc(nelm, sizeof(*tmp), GFP_KERNEL);
  1784. if (!tmp)
  1785. return ERR_PTR(-ENOMEM);
  1786. rsv_info = devm_kzalloc(dev, sizeof(*rsv_info), GFP_KERNEL);
  1787. if (!rsv_info) {
  1788. kfree(tmp);
  1789. return ERR_PTR(-ENOMEM);
  1790. }
  1791. rsv_slots = devm_kcalloc(dev, nelm + 1, sizeof(*rsv_slots),
  1792. GFP_KERNEL);
  1793. if (!rsv_slots) {
  1794. kfree(tmp);
  1795. return ERR_PTR(-ENOMEM);
  1796. }
  1797. ret = of_property_read_u32_array(dev->of_node, pname,
  1798. (u32 *)tmp, nelm * 2);
  1799. if (ret) {
  1800. kfree(tmp);
  1801. return ERR_PTR(ret);
  1802. }
  1803. for (i = 0; i < nelm; i++) {
  1804. rsv_slots[i][0] = tmp[i][0];
  1805. rsv_slots[i][1] = tmp[i][1];
  1806. }
  1807. rsv_slots[nelm][0] = -1;
  1808. rsv_slots[nelm][1] = -1;
  1809. info->rsv = rsv_info;
  1810. info->rsv->rsv_slots = (const s16 (*)[2])rsv_slots;
  1811. kfree(tmp);
  1812. }
  1813. return info;
  1814. }
  1815. static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
  1816. struct of_dma *ofdma)
  1817. {
  1818. struct edma_cc *ecc = ofdma->of_dma_data;
  1819. struct dma_chan *chan = NULL;
  1820. struct edma_chan *echan;
  1821. int i;
  1822. if (!ecc || dma_spec->args_count < 1)
  1823. return NULL;
  1824. for (i = 0; i < ecc->num_channels; i++) {
  1825. echan = &ecc->slave_chans[i];
  1826. if (echan->ch_num == dma_spec->args[0]) {
  1827. chan = &echan->vchan.chan;
  1828. break;
  1829. }
  1830. }
  1831. if (!chan)
  1832. return NULL;
  1833. if (echan->ecc->legacy_mode && dma_spec->args_count == 1)
  1834. goto out;
  1835. if (!echan->ecc->legacy_mode && dma_spec->args_count == 2 &&
  1836. dma_spec->args[1] < echan->ecc->num_tc) {
  1837. echan->tc = &echan->ecc->tc_list[dma_spec->args[1]];
  1838. goto out;
  1839. }
  1840. return NULL;
  1841. out:
  1842. /* The channel is going to be used as HW synchronized */
  1843. echan->hw_triggered = true;
  1844. return dma_get_slave_channel(chan);
  1845. }
  1846. #else
  1847. static struct edma_soc_info *edma_setup_info_from_dt(struct device *dev,
  1848. bool legacy_mode)
  1849. {
  1850. return ERR_PTR(-EINVAL);
  1851. }
  1852. static struct dma_chan *of_edma_xlate(struct of_phandle_args *dma_spec,
  1853. struct of_dma *ofdma)
  1854. {
  1855. return NULL;
  1856. }
  1857. #endif
  1858. static int edma_probe(struct platform_device *pdev)
  1859. {
  1860. struct edma_soc_info *info = pdev->dev.platform_data;
  1861. s8 (*queue_priority_mapping)[2];
  1862. int i, off, ln;
  1863. const s16 (*rsv_slots)[2];
  1864. const s16 (*xbar_chans)[2];
  1865. int irq;
  1866. char *irq_name;
  1867. struct resource *mem;
  1868. struct device_node *node = pdev->dev.of_node;
  1869. struct device *dev = &pdev->dev;
  1870. struct edma_cc *ecc;
  1871. bool legacy_mode = true;
  1872. int ret;
  1873. if (node) {
  1874. const struct of_device_id *match;
  1875. match = of_match_node(edma_of_ids, node);
  1876. if (match && (*(u32 *)match->data) == EDMA_BINDING_TPCC)
  1877. legacy_mode = false;
  1878. info = edma_setup_info_from_dt(dev, legacy_mode);
  1879. if (IS_ERR(info)) {
  1880. dev_err(dev, "failed to get DT data\n");
  1881. return PTR_ERR(info);
  1882. }
  1883. }
  1884. if (!info)
  1885. return -ENODEV;
  1886. pm_runtime_enable(dev);
  1887. ret = pm_runtime_get_sync(dev);
  1888. if (ret < 0) {
  1889. dev_err(dev, "pm_runtime_get_sync() failed\n");
  1890. return ret;
  1891. }
  1892. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  1893. if (ret)
  1894. return ret;
  1895. ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
  1896. if (!ecc)
  1897. return -ENOMEM;
  1898. ecc->dev = dev;
  1899. ecc->id = pdev->id;
  1900. ecc->legacy_mode = legacy_mode;
  1901. /* When booting with DT the pdev->id is -1 */
  1902. if (ecc->id < 0)
  1903. ecc->id = 0;
  1904. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma3_cc");
  1905. if (!mem) {
  1906. dev_dbg(dev, "mem resource not found, using index 0\n");
  1907. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1908. if (!mem) {
  1909. dev_err(dev, "no mem resource?\n");
  1910. return -ENODEV;
  1911. }
  1912. }
  1913. ecc->base = devm_ioremap_resource(dev, mem);
  1914. if (IS_ERR(ecc->base))
  1915. return PTR_ERR(ecc->base);
  1916. platform_set_drvdata(pdev, ecc);
  1917. /* Get eDMA3 configuration from IP */
  1918. ret = edma_setup_from_hw(dev, info, ecc);
  1919. if (ret)
  1920. return ret;
  1921. /* Allocate memory based on the information we got from the IP */
  1922. ecc->slave_chans = devm_kcalloc(dev, ecc->num_channels,
  1923. sizeof(*ecc->slave_chans), GFP_KERNEL);
  1924. if (!ecc->slave_chans)
  1925. return -ENOMEM;
  1926. ecc->slot_inuse = devm_kcalloc(dev, BITS_TO_LONGS(ecc->num_slots),
  1927. sizeof(unsigned long), GFP_KERNEL);
  1928. if (!ecc->slot_inuse)
  1929. return -ENOMEM;
  1930. ecc->default_queue = info->default_queue;
  1931. for (i = 0; i < ecc->num_slots; i++)
  1932. edma_write_slot(ecc, i, &dummy_paramset);
  1933. if (info->rsv) {
  1934. /* Set the reserved slots in inuse list */
  1935. rsv_slots = info->rsv->rsv_slots;
  1936. if (rsv_slots) {
  1937. for (i = 0; rsv_slots[i][0] != -1; i++) {
  1938. off = rsv_slots[i][0];
  1939. ln = rsv_slots[i][1];
  1940. edma_set_bits(off, ln, ecc->slot_inuse);
  1941. }
  1942. }
  1943. }
  1944. /* Clear the xbar mapped channels in unused list */
  1945. xbar_chans = info->xbar_chans;
  1946. if (xbar_chans) {
  1947. for (i = 0; xbar_chans[i][1] != -1; i++) {
  1948. off = xbar_chans[i][1];
  1949. }
  1950. }
  1951. irq = platform_get_irq_byname(pdev, "edma3_ccint");
  1952. if (irq < 0 && node)
  1953. irq = irq_of_parse_and_map(node, 0);
  1954. if (irq >= 0) {
  1955. irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccint",
  1956. dev_name(dev));
  1957. ret = devm_request_irq(dev, irq, dma_irq_handler, 0, irq_name,
  1958. ecc);
  1959. if (ret) {
  1960. dev_err(dev, "CCINT (%d) failed --> %d\n", irq, ret);
  1961. return ret;
  1962. }
  1963. ecc->ccint = irq;
  1964. }
  1965. irq = platform_get_irq_byname(pdev, "edma3_ccerrint");
  1966. if (irq < 0 && node)
  1967. irq = irq_of_parse_and_map(node, 2);
  1968. if (irq >= 0) {
  1969. irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s_ccerrint",
  1970. dev_name(dev));
  1971. ret = devm_request_irq(dev, irq, dma_ccerr_handler, 0, irq_name,
  1972. ecc);
  1973. if (ret) {
  1974. dev_err(dev, "CCERRINT (%d) failed --> %d\n", irq, ret);
  1975. return ret;
  1976. }
  1977. ecc->ccerrint = irq;
  1978. }
  1979. ecc->dummy_slot = edma_alloc_slot(ecc, EDMA_SLOT_ANY);
  1980. if (ecc->dummy_slot < 0) {
  1981. dev_err(dev, "Can't allocate PaRAM dummy slot\n");
  1982. return ecc->dummy_slot;
  1983. }
  1984. queue_priority_mapping = info->queue_priority_mapping;
  1985. if (!ecc->legacy_mode) {
  1986. int lowest_priority = 0;
  1987. struct of_phandle_args tc_args;
  1988. ecc->tc_list = devm_kcalloc(dev, ecc->num_tc,
  1989. sizeof(*ecc->tc_list), GFP_KERNEL);
  1990. if (!ecc->tc_list)
  1991. return -ENOMEM;
  1992. for (i = 0;; i++) {
  1993. ret = of_parse_phandle_with_fixed_args(node, "ti,tptcs",
  1994. 1, i, &tc_args);
  1995. if (ret || i == ecc->num_tc)
  1996. break;
  1997. ecc->tc_list[i].node = tc_args.np;
  1998. ecc->tc_list[i].id = i;
  1999. queue_priority_mapping[i][1] = tc_args.args[0];
  2000. if (queue_priority_mapping[i][1] > lowest_priority) {
  2001. lowest_priority = queue_priority_mapping[i][1];
  2002. info->default_queue = i;
  2003. }
  2004. }
  2005. }
  2006. /* Event queue priority mapping */
  2007. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  2008. edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
  2009. queue_priority_mapping[i][1]);
  2010. for (i = 0; i < ecc->num_region; i++) {
  2011. edma_write_array2(ecc, EDMA_DRAE, i, 0, 0x0);
  2012. edma_write_array2(ecc, EDMA_DRAE, i, 1, 0x0);
  2013. edma_write_array(ecc, EDMA_QRAE, i, 0x0);
  2014. }
  2015. ecc->info = info;
  2016. /* Init the dma device and channels */
  2017. edma_dma_init(ecc, legacy_mode);
  2018. for (i = 0; i < ecc->num_channels; i++) {
  2019. /* Assign all channels to the default queue */
  2020. edma_assign_channel_eventq(&ecc->slave_chans[i],
  2021. info->default_queue);
  2022. /* Set entry slot to the dummy slot */
  2023. edma_set_chmap(&ecc->slave_chans[i], ecc->dummy_slot);
  2024. }
  2025. ecc->dma_slave.filter.map = info->slave_map;
  2026. ecc->dma_slave.filter.mapcnt = info->slavecnt;
  2027. ecc->dma_slave.filter.fn = edma_filter_fn;
  2028. ret = dma_async_device_register(&ecc->dma_slave);
  2029. if (ret) {
  2030. dev_err(dev, "slave ddev registration failed (%d)\n", ret);
  2031. goto err_reg1;
  2032. }
  2033. if (ecc->dma_memcpy) {
  2034. ret = dma_async_device_register(ecc->dma_memcpy);
  2035. if (ret) {
  2036. dev_err(dev, "memcpy ddev registration failed (%d)\n",
  2037. ret);
  2038. dma_async_device_unregister(&ecc->dma_slave);
  2039. goto err_reg1;
  2040. }
  2041. }
  2042. if (node)
  2043. of_dma_controller_register(node, of_edma_xlate, ecc);
  2044. dev_info(dev, "TI EDMA DMA engine driver\n");
  2045. return 0;
  2046. err_reg1:
  2047. edma_free_slot(ecc, ecc->dummy_slot);
  2048. return ret;
  2049. }
  2050. static void edma_cleanupp_vchan(struct dma_device *dmadev)
  2051. {
  2052. struct edma_chan *echan, *_echan;
  2053. list_for_each_entry_safe(echan, _echan,
  2054. &dmadev->channels, vchan.chan.device_node) {
  2055. list_del(&echan->vchan.chan.device_node);
  2056. tasklet_kill(&echan->vchan.task);
  2057. }
  2058. }
  2059. static int edma_remove(struct platform_device *pdev)
  2060. {
  2061. struct device *dev = &pdev->dev;
  2062. struct edma_cc *ecc = dev_get_drvdata(dev);
  2063. devm_free_irq(dev, ecc->ccint, ecc);
  2064. devm_free_irq(dev, ecc->ccerrint, ecc);
  2065. edma_cleanupp_vchan(&ecc->dma_slave);
  2066. if (dev->of_node)
  2067. of_dma_controller_free(dev->of_node);
  2068. dma_async_device_unregister(&ecc->dma_slave);
  2069. if (ecc->dma_memcpy)
  2070. dma_async_device_unregister(ecc->dma_memcpy);
  2071. edma_free_slot(ecc, ecc->dummy_slot);
  2072. return 0;
  2073. }
  2074. #ifdef CONFIG_PM_SLEEP
  2075. static int edma_pm_suspend(struct device *dev)
  2076. {
  2077. struct edma_cc *ecc = dev_get_drvdata(dev);
  2078. struct edma_chan *echan = ecc->slave_chans;
  2079. int i;
  2080. for (i = 0; i < ecc->num_channels; i++) {
  2081. if (echan[i].alloced)
  2082. edma_setup_interrupt(&echan[i], false);
  2083. }
  2084. return 0;
  2085. }
  2086. static int edma_pm_resume(struct device *dev)
  2087. {
  2088. struct edma_cc *ecc = dev_get_drvdata(dev);
  2089. struct edma_chan *echan = ecc->slave_chans;
  2090. int i;
  2091. s8 (*queue_priority_mapping)[2];
  2092. /* re initialize dummy slot to dummy param set */
  2093. edma_write_slot(ecc, ecc->dummy_slot, &dummy_paramset);
  2094. queue_priority_mapping = ecc->info->queue_priority_mapping;
  2095. /* Event queue priority mapping */
  2096. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  2097. edma_assign_priority_to_queue(ecc, queue_priority_mapping[i][0],
  2098. queue_priority_mapping[i][1]);
  2099. for (i = 0; i < ecc->num_channels; i++) {
  2100. if (echan[i].alloced) {
  2101. /* ensure access through shadow region 0 */
  2102. edma_or_array2(ecc, EDMA_DRAE, 0, i >> 5,
  2103. BIT(i & 0x1f));
  2104. edma_setup_interrupt(&echan[i], true);
  2105. /* Set up channel -> slot mapping for the entry slot */
  2106. edma_set_chmap(&echan[i], echan[i].slot[0]);
  2107. }
  2108. }
  2109. return 0;
  2110. }
  2111. #endif
  2112. static const struct dev_pm_ops edma_pm_ops = {
  2113. SET_LATE_SYSTEM_SLEEP_PM_OPS(edma_pm_suspend, edma_pm_resume)
  2114. };
  2115. static struct platform_driver edma_driver = {
  2116. .probe = edma_probe,
  2117. .remove = edma_remove,
  2118. .driver = {
  2119. .name = "edma",
  2120. .pm = &edma_pm_ops,
  2121. .of_match_table = edma_of_ids,
  2122. },
  2123. };
  2124. static int edma_tptc_probe(struct platform_device *pdev)
  2125. {
  2126. pm_runtime_enable(&pdev->dev);
  2127. return pm_runtime_get_sync(&pdev->dev);
  2128. }
  2129. static struct platform_driver edma_tptc_driver = {
  2130. .probe = edma_tptc_probe,
  2131. .driver = {
  2132. .name = "edma3-tptc",
  2133. .of_match_table = edma_tptc_of_ids,
  2134. },
  2135. };
  2136. bool edma_filter_fn(struct dma_chan *chan, void *param)
  2137. {
  2138. bool match = false;
  2139. if (chan->device->dev->driver == &edma_driver.driver) {
  2140. struct edma_chan *echan = to_edma_chan(chan);
  2141. unsigned ch_req = *(unsigned *)param;
  2142. if (ch_req == echan->ch_num) {
  2143. /* The channel is going to be used as HW synchronized */
  2144. echan->hw_triggered = true;
  2145. match = true;
  2146. }
  2147. }
  2148. return match;
  2149. }
  2150. EXPORT_SYMBOL(edma_filter_fn);
  2151. static int edma_init(void)
  2152. {
  2153. int ret;
  2154. ret = platform_driver_register(&edma_tptc_driver);
  2155. if (ret)
  2156. return ret;
  2157. return platform_driver_register(&edma_driver);
  2158. }
  2159. subsys_initcall(edma_init);
  2160. static void __exit edma_exit(void)
  2161. {
  2162. platform_driver_unregister(&edma_driver);
  2163. platform_driver_unregister(&edma_tptc_driver);
  2164. }
  2165. module_exit(edma_exit);
  2166. MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
  2167. MODULE_DESCRIPTION("TI EDMA DMA engine driver");
  2168. MODULE_LICENSE("GPL v2");