edma.c 65 KB

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