edma.c 64 KB

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