dma.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * EDMA3 support for DaVinci
  3. *
  4. * Copyright (C) 2006-2009 Texas Instruments.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/io.h>
  26. #include <linux/slab.h>
  27. #include <mach/edma.h>
  28. /* Offsets matching "struct edmacc_param" */
  29. #define PARM_OPT 0x00
  30. #define PARM_SRC 0x04
  31. #define PARM_A_B_CNT 0x08
  32. #define PARM_DST 0x0c
  33. #define PARM_SRC_DST_BIDX 0x10
  34. #define PARM_LINK_BCNTRLD 0x14
  35. #define PARM_SRC_DST_CIDX 0x18
  36. #define PARM_CCNT 0x1c
  37. #define PARM_SIZE 0x20
  38. /* Offsets for EDMA CC global channel registers and their shadows */
  39. #define SH_ER 0x00 /* 64 bits */
  40. #define SH_ECR 0x08 /* 64 bits */
  41. #define SH_ESR 0x10 /* 64 bits */
  42. #define SH_CER 0x18 /* 64 bits */
  43. #define SH_EER 0x20 /* 64 bits */
  44. #define SH_EECR 0x28 /* 64 bits */
  45. #define SH_EESR 0x30 /* 64 bits */
  46. #define SH_SER 0x38 /* 64 bits */
  47. #define SH_SECR 0x40 /* 64 bits */
  48. #define SH_IER 0x50 /* 64 bits */
  49. #define SH_IECR 0x58 /* 64 bits */
  50. #define SH_IESR 0x60 /* 64 bits */
  51. #define SH_IPR 0x68 /* 64 bits */
  52. #define SH_ICR 0x70 /* 64 bits */
  53. #define SH_IEVAL 0x78
  54. #define SH_QER 0x80
  55. #define SH_QEER 0x84
  56. #define SH_QEECR 0x88
  57. #define SH_QEESR 0x8c
  58. #define SH_QSER 0x90
  59. #define SH_QSECR 0x94
  60. #define SH_SIZE 0x200
  61. /* Offsets for EDMA CC global registers */
  62. #define EDMA_REV 0x0000
  63. #define EDMA_CCCFG 0x0004
  64. #define EDMA_QCHMAP 0x0200 /* 8 registers */
  65. #define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
  66. #define EDMA_QDMAQNUM 0x0260
  67. #define EDMA_QUETCMAP 0x0280
  68. #define EDMA_QUEPRI 0x0284
  69. #define EDMA_EMR 0x0300 /* 64 bits */
  70. #define EDMA_EMCR 0x0308 /* 64 bits */
  71. #define EDMA_QEMR 0x0310
  72. #define EDMA_QEMCR 0x0314
  73. #define EDMA_CCERR 0x0318
  74. #define EDMA_CCERRCLR 0x031c
  75. #define EDMA_EEVAL 0x0320
  76. #define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
  77. #define EDMA_QRAE 0x0380 /* 4 registers */
  78. #define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
  79. #define EDMA_QSTAT 0x0600 /* 2 registers */
  80. #define EDMA_QWMTHRA 0x0620
  81. #define EDMA_QWMTHRB 0x0624
  82. #define EDMA_CCSTAT 0x0640
  83. #define EDMA_M 0x1000 /* global channel registers */
  84. #define EDMA_ECR 0x1008
  85. #define EDMA_ECRH 0x100C
  86. #define EDMA_SHADOW0 0x2000 /* 4 regions shadowing global channels */
  87. #define EDMA_PARM 0x4000 /* 128 param entries */
  88. #define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
  89. #define EDMA_DCHMAP 0x0100 /* 64 registers */
  90. #define CHMAP_EXIST BIT(24)
  91. #define EDMA_MAX_DMACH 64
  92. #define EDMA_MAX_PARAMENTRY 512
  93. /*****************************************************************************/
  94. static void __iomem *edmacc_regs_base[EDMA_MAX_CC];
  95. static inline unsigned int edma_read(unsigned ctlr, int offset)
  96. {
  97. return (unsigned int)__raw_readl(edmacc_regs_base[ctlr] + offset);
  98. }
  99. static inline void edma_write(unsigned ctlr, int offset, int val)
  100. {
  101. __raw_writel(val, edmacc_regs_base[ctlr] + offset);
  102. }
  103. static inline void edma_modify(unsigned ctlr, int offset, unsigned and,
  104. unsigned or)
  105. {
  106. unsigned val = edma_read(ctlr, offset);
  107. val &= and;
  108. val |= or;
  109. edma_write(ctlr, offset, val);
  110. }
  111. static inline void edma_and(unsigned ctlr, int offset, unsigned and)
  112. {
  113. unsigned val = edma_read(ctlr, offset);
  114. val &= and;
  115. edma_write(ctlr, offset, val);
  116. }
  117. static inline void edma_or(unsigned ctlr, int offset, unsigned or)
  118. {
  119. unsigned val = edma_read(ctlr, offset);
  120. val |= or;
  121. edma_write(ctlr, offset, val);
  122. }
  123. static inline unsigned int edma_read_array(unsigned ctlr, int offset, int i)
  124. {
  125. return edma_read(ctlr, offset + (i << 2));
  126. }
  127. static inline void edma_write_array(unsigned ctlr, int offset, int i,
  128. unsigned val)
  129. {
  130. edma_write(ctlr, offset + (i << 2), val);
  131. }
  132. static inline void edma_modify_array(unsigned ctlr, int offset, int i,
  133. unsigned and, unsigned or)
  134. {
  135. edma_modify(ctlr, offset + (i << 2), and, or);
  136. }
  137. static inline void edma_or_array(unsigned ctlr, int offset, int i, unsigned or)
  138. {
  139. edma_or(ctlr, offset + (i << 2), or);
  140. }
  141. static inline void edma_or_array2(unsigned ctlr, int offset, int i, int j,
  142. unsigned or)
  143. {
  144. edma_or(ctlr, offset + ((i*2 + j) << 2), or);
  145. }
  146. static inline void edma_write_array2(unsigned ctlr, int offset, int i, int j,
  147. unsigned val)
  148. {
  149. edma_write(ctlr, offset + ((i*2 + j) << 2), val);
  150. }
  151. static inline unsigned int edma_shadow0_read(unsigned ctlr, int offset)
  152. {
  153. return edma_read(ctlr, EDMA_SHADOW0 + offset);
  154. }
  155. static inline unsigned int edma_shadow0_read_array(unsigned ctlr, int offset,
  156. int i)
  157. {
  158. return edma_read(ctlr, EDMA_SHADOW0 + offset + (i << 2));
  159. }
  160. static inline void edma_shadow0_write(unsigned ctlr, int offset, unsigned val)
  161. {
  162. edma_write(ctlr, EDMA_SHADOW0 + offset, val);
  163. }
  164. static inline void edma_shadow0_write_array(unsigned ctlr, int offset, int i,
  165. unsigned val)
  166. {
  167. edma_write(ctlr, EDMA_SHADOW0 + offset + (i << 2), val);
  168. }
  169. static inline unsigned int edma_parm_read(unsigned ctlr, int offset,
  170. int param_no)
  171. {
  172. return edma_read(ctlr, EDMA_PARM + offset + (param_no << 5));
  173. }
  174. static inline void edma_parm_write(unsigned ctlr, int offset, int param_no,
  175. unsigned val)
  176. {
  177. edma_write(ctlr, EDMA_PARM + offset + (param_no << 5), val);
  178. }
  179. static inline void edma_parm_modify(unsigned ctlr, int offset, int param_no,
  180. unsigned and, unsigned or)
  181. {
  182. edma_modify(ctlr, EDMA_PARM + offset + (param_no << 5), and, or);
  183. }
  184. static inline void edma_parm_and(unsigned ctlr, int offset, int param_no,
  185. unsigned and)
  186. {
  187. edma_and(ctlr, EDMA_PARM + offset + (param_no << 5), and);
  188. }
  189. static inline void edma_parm_or(unsigned ctlr, int offset, int param_no,
  190. unsigned or)
  191. {
  192. edma_or(ctlr, EDMA_PARM + offset + (param_no << 5), or);
  193. }
  194. static inline void set_bits(int offset, int len, unsigned long *p)
  195. {
  196. for (; len > 0; len--)
  197. set_bit(offset + (len - 1), p);
  198. }
  199. static inline void clear_bits(int offset, int len, unsigned long *p)
  200. {
  201. for (; len > 0; len--)
  202. clear_bit(offset + (len - 1), p);
  203. }
  204. /*****************************************************************************/
  205. /* actual number of DMA channels and slots on this silicon */
  206. struct edma {
  207. /* how many dma resources of each type */
  208. unsigned num_channels;
  209. unsigned num_region;
  210. unsigned num_slots;
  211. unsigned num_tc;
  212. unsigned num_cc;
  213. enum dma_event_q default_queue;
  214. /* list of channels with no even trigger; terminated by "-1" */
  215. const s8 *noevent;
  216. /* The edma_inuse bit for each PaRAM slot is clear unless the
  217. * channel is in use ... by ARM or DSP, for QDMA, or whatever.
  218. */
  219. DECLARE_BITMAP(edma_inuse, EDMA_MAX_PARAMENTRY);
  220. /* The edma_unused bit for each channel is clear unless
  221. * it is not being used on this platform. It uses a bit
  222. * of SOC-specific initialization code.
  223. */
  224. DECLARE_BITMAP(edma_unused, EDMA_MAX_DMACH);
  225. unsigned irq_res_start;
  226. unsigned irq_res_end;
  227. struct dma_interrupt_data {
  228. void (*callback)(unsigned channel, unsigned short ch_status,
  229. void *data);
  230. void *data;
  231. } intr_data[EDMA_MAX_DMACH];
  232. };
  233. static struct edma *edma_cc[EDMA_MAX_CC];
  234. static int arch_num_cc;
  235. /* dummy param set used to (re)initialize parameter RAM slots */
  236. static const struct edmacc_param dummy_paramset = {
  237. .link_bcntrld = 0xffff,
  238. .ccnt = 1,
  239. };
  240. /*****************************************************************************/
  241. static void map_dmach_queue(unsigned ctlr, unsigned ch_no,
  242. enum dma_event_q queue_no)
  243. {
  244. int bit = (ch_no & 0x7) * 4;
  245. /* default to low priority queue */
  246. if (queue_no == EVENTQ_DEFAULT)
  247. queue_no = edma_cc[ctlr]->default_queue;
  248. queue_no &= 7;
  249. edma_modify_array(ctlr, EDMA_DMAQNUM, (ch_no >> 3),
  250. ~(0x7 << bit), queue_no << bit);
  251. }
  252. static void __init map_queue_tc(unsigned ctlr, int queue_no, int tc_no)
  253. {
  254. int bit = queue_no * 4;
  255. edma_modify(ctlr, EDMA_QUETCMAP, ~(0x7 << bit), ((tc_no & 0x7) << bit));
  256. }
  257. static void __init assign_priority_to_queue(unsigned ctlr, int queue_no,
  258. int priority)
  259. {
  260. int bit = queue_no * 4;
  261. edma_modify(ctlr, EDMA_QUEPRI, ~(0x7 << bit),
  262. ((priority & 0x7) << bit));
  263. }
  264. /**
  265. * map_dmach_param - Maps channel number to param entry number
  266. *
  267. * This maps the dma channel number to param entry numberter. In
  268. * other words using the DMA channel mapping registers a param entry
  269. * can be mapped to any channel
  270. *
  271. * Callers are responsible for ensuring the channel mapping logic is
  272. * included in that particular EDMA variant (Eg : dm646x)
  273. *
  274. */
  275. static void __init map_dmach_param(unsigned ctlr)
  276. {
  277. int i;
  278. for (i = 0; i < EDMA_MAX_DMACH; i++)
  279. edma_write_array(ctlr, EDMA_DCHMAP , i , (i << 5));
  280. }
  281. static inline void
  282. setup_dma_interrupt(unsigned lch,
  283. void (*callback)(unsigned channel, u16 ch_status, void *data),
  284. void *data)
  285. {
  286. unsigned ctlr;
  287. ctlr = EDMA_CTLR(lch);
  288. lch = EDMA_CHAN_SLOT(lch);
  289. if (!callback)
  290. edma_shadow0_write_array(ctlr, SH_IECR, lch >> 5,
  291. BIT(lch & 0x1f));
  292. edma_cc[ctlr]->intr_data[lch].callback = callback;
  293. edma_cc[ctlr]->intr_data[lch].data = data;
  294. if (callback) {
  295. edma_shadow0_write_array(ctlr, SH_ICR, lch >> 5,
  296. BIT(lch & 0x1f));
  297. edma_shadow0_write_array(ctlr, SH_IESR, lch >> 5,
  298. BIT(lch & 0x1f));
  299. }
  300. }
  301. static int irq2ctlr(int irq)
  302. {
  303. if (irq >= edma_cc[0]->irq_res_start && irq <= edma_cc[0]->irq_res_end)
  304. return 0;
  305. else if (irq >= edma_cc[1]->irq_res_start &&
  306. irq <= edma_cc[1]->irq_res_end)
  307. return 1;
  308. return -1;
  309. }
  310. /******************************************************************************
  311. *
  312. * DMA interrupt handler
  313. *
  314. *****************************************************************************/
  315. static irqreturn_t dma_irq_handler(int irq, void *data)
  316. {
  317. int ctlr;
  318. u32 sh_ier;
  319. u32 sh_ipr;
  320. u32 bank;
  321. ctlr = irq2ctlr(irq);
  322. if (ctlr < 0)
  323. return IRQ_NONE;
  324. dev_dbg(data, "dma_irq_handler\n");
  325. sh_ipr = edma_shadow0_read_array(ctlr, SH_IPR, 0);
  326. if (!sh_ipr) {
  327. sh_ipr = edma_shadow0_read_array(ctlr, SH_IPR, 1);
  328. if (!sh_ipr)
  329. return IRQ_NONE;
  330. sh_ier = edma_shadow0_read_array(ctlr, SH_IER, 1);
  331. bank = 1;
  332. } else {
  333. sh_ier = edma_shadow0_read_array(ctlr, SH_IER, 0);
  334. bank = 0;
  335. }
  336. do {
  337. u32 slot;
  338. u32 channel;
  339. dev_dbg(data, "IPR%d %08x\n", bank, sh_ipr);
  340. slot = __ffs(sh_ipr);
  341. sh_ipr &= ~(BIT(slot));
  342. if (sh_ier & BIT(slot)) {
  343. channel = (bank << 5) | slot;
  344. /* Clear the corresponding IPR bits */
  345. edma_shadow0_write_array(ctlr, SH_ICR, bank,
  346. BIT(slot));
  347. if (edma_cc[ctlr]->intr_data[channel].callback)
  348. edma_cc[ctlr]->intr_data[channel].callback(
  349. channel, DMA_COMPLETE,
  350. edma_cc[ctlr]->intr_data[channel].data);
  351. }
  352. } while (sh_ipr);
  353. edma_shadow0_write(ctlr, SH_IEVAL, 1);
  354. return IRQ_HANDLED;
  355. }
  356. /******************************************************************************
  357. *
  358. * DMA error interrupt handler
  359. *
  360. *****************************************************************************/
  361. static irqreturn_t dma_ccerr_handler(int irq, void *data)
  362. {
  363. int i;
  364. int ctlr;
  365. unsigned int cnt = 0;
  366. ctlr = irq2ctlr(irq);
  367. if (ctlr < 0)
  368. return IRQ_NONE;
  369. dev_dbg(data, "dma_ccerr_handler\n");
  370. if ((edma_read_array(ctlr, EDMA_EMR, 0) == 0) &&
  371. (edma_read_array(ctlr, EDMA_EMR, 1) == 0) &&
  372. (edma_read(ctlr, EDMA_QEMR) == 0) &&
  373. (edma_read(ctlr, EDMA_CCERR) == 0))
  374. return IRQ_NONE;
  375. while (1) {
  376. int j = -1;
  377. if (edma_read_array(ctlr, EDMA_EMR, 0))
  378. j = 0;
  379. else if (edma_read_array(ctlr, EDMA_EMR, 1))
  380. j = 1;
  381. if (j >= 0) {
  382. dev_dbg(data, "EMR%d %08x\n", j,
  383. edma_read_array(ctlr, EDMA_EMR, j));
  384. for (i = 0; i < 32; i++) {
  385. int k = (j << 5) + i;
  386. if (edma_read_array(ctlr, EDMA_EMR, j) &
  387. BIT(i)) {
  388. /* Clear the corresponding EMR bits */
  389. edma_write_array(ctlr, EDMA_EMCR, j,
  390. BIT(i));
  391. /* Clear any SER */
  392. edma_shadow0_write_array(ctlr, SH_SECR,
  393. j, BIT(i));
  394. if (edma_cc[ctlr]->intr_data[k].
  395. callback) {
  396. edma_cc[ctlr]->intr_data[k].
  397. callback(k,
  398. DMA_CC_ERROR,
  399. edma_cc[ctlr]->intr_data
  400. [k].data);
  401. }
  402. }
  403. }
  404. } else if (edma_read(ctlr, EDMA_QEMR)) {
  405. dev_dbg(data, "QEMR %02x\n",
  406. edma_read(ctlr, EDMA_QEMR));
  407. for (i = 0; i < 8; i++) {
  408. if (edma_read(ctlr, EDMA_QEMR) & BIT(i)) {
  409. /* Clear the corresponding IPR bits */
  410. edma_write(ctlr, EDMA_QEMCR, BIT(i));
  411. edma_shadow0_write(ctlr, SH_QSECR,
  412. BIT(i));
  413. /* NOTE: not reported!! */
  414. }
  415. }
  416. } else if (edma_read(ctlr, EDMA_CCERR)) {
  417. dev_dbg(data, "CCERR %08x\n",
  418. edma_read(ctlr, EDMA_CCERR));
  419. /* FIXME: CCERR.BIT(16) ignored! much better
  420. * to just write CCERRCLR with CCERR value...
  421. */
  422. for (i = 0; i < 8; i++) {
  423. if (edma_read(ctlr, EDMA_CCERR) & BIT(i)) {
  424. /* Clear the corresponding IPR bits */
  425. edma_write(ctlr, EDMA_CCERRCLR, BIT(i));
  426. /* NOTE: not reported!! */
  427. }
  428. }
  429. }
  430. if ((edma_read_array(ctlr, EDMA_EMR, 0) == 0) &&
  431. (edma_read_array(ctlr, EDMA_EMR, 1) == 0) &&
  432. (edma_read(ctlr, EDMA_QEMR) == 0) &&
  433. (edma_read(ctlr, EDMA_CCERR) == 0))
  434. break;
  435. cnt++;
  436. if (cnt > 10)
  437. break;
  438. }
  439. edma_write(ctlr, EDMA_EEVAL, 1);
  440. return IRQ_HANDLED;
  441. }
  442. /******************************************************************************
  443. *
  444. * Transfer controller error interrupt handlers
  445. *
  446. *****************************************************************************/
  447. #define tc_errs_handled false /* disabled as long as they're NOPs */
  448. static irqreturn_t dma_tc0err_handler(int irq, void *data)
  449. {
  450. dev_dbg(data, "dma_tc0err_handler\n");
  451. return IRQ_HANDLED;
  452. }
  453. static irqreturn_t dma_tc1err_handler(int irq, void *data)
  454. {
  455. dev_dbg(data, "dma_tc1err_handler\n");
  456. return IRQ_HANDLED;
  457. }
  458. static int reserve_contiguous_slots(int ctlr, unsigned int id,
  459. unsigned int num_slots,
  460. unsigned int start_slot)
  461. {
  462. int i, j;
  463. unsigned int count = num_slots;
  464. int stop_slot = start_slot;
  465. DECLARE_BITMAP(tmp_inuse, EDMA_MAX_PARAMENTRY);
  466. for (i = start_slot; i < edma_cc[ctlr]->num_slots; ++i) {
  467. j = EDMA_CHAN_SLOT(i);
  468. if (!test_and_set_bit(j, edma_cc[ctlr]->edma_inuse)) {
  469. /* Record our current beginning slot */
  470. if (count == num_slots)
  471. stop_slot = i;
  472. count--;
  473. set_bit(j, tmp_inuse);
  474. if (count == 0)
  475. break;
  476. } else {
  477. clear_bit(j, tmp_inuse);
  478. if (id == EDMA_CONT_PARAMS_FIXED_EXACT) {
  479. stop_slot = i;
  480. break;
  481. } else {
  482. count = num_slots;
  483. }
  484. }
  485. }
  486. /*
  487. * We have to clear any bits that we set
  488. * if we run out parameter RAM slots, i.e we do find a set
  489. * of contiguous parameter RAM slots but do not find the exact number
  490. * requested as we may reach the total number of parameter RAM slots
  491. */
  492. if (i == edma_cc[ctlr]->num_slots)
  493. stop_slot = i;
  494. j = start_slot;
  495. for_each_set_bit_from(j, tmp_inuse, stop_slot)
  496. clear_bit(j, edma_cc[ctlr]->edma_inuse);
  497. if (count)
  498. return -EBUSY;
  499. for (j = i - num_slots + 1; j <= i; ++j)
  500. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(j),
  501. &dummy_paramset, PARM_SIZE);
  502. return EDMA_CTLR_CHAN(ctlr, i - num_slots + 1);
  503. }
  504. static int prepare_unused_channel_list(struct device *dev, void *data)
  505. {
  506. struct platform_device *pdev = to_platform_device(dev);
  507. int i, ctlr;
  508. for (i = 0; i < pdev->num_resources; i++) {
  509. if ((pdev->resource[i].flags & IORESOURCE_DMA) &&
  510. (int)pdev->resource[i].start >= 0) {
  511. ctlr = EDMA_CTLR(pdev->resource[i].start);
  512. clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
  513. edma_cc[ctlr]->edma_unused);
  514. }
  515. }
  516. return 0;
  517. }
  518. /*-----------------------------------------------------------------------*/
  519. static bool unused_chan_list_done;
  520. /* Resource alloc/free: dma channels, parameter RAM slots */
  521. /**
  522. * edma_alloc_channel - allocate DMA channel and paired parameter RAM
  523. * @channel: specific channel to allocate; negative for "any unmapped channel"
  524. * @callback: optional; to be issued on DMA completion or errors
  525. * @data: passed to callback
  526. * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
  527. * Controller (TC) executes requests using this channel. Use
  528. * EVENTQ_DEFAULT unless you really need a high priority queue.
  529. *
  530. * This allocates a DMA channel and its associated parameter RAM slot.
  531. * The parameter RAM is initialized to hold a dummy transfer.
  532. *
  533. * Normal use is to pass a specific channel number as @channel, to make
  534. * use of hardware events mapped to that channel. When the channel will
  535. * be used only for software triggering or event chaining, channels not
  536. * mapped to hardware events (or mapped to unused events) are preferable.
  537. *
  538. * DMA transfers start from a channel using edma_start(), or by
  539. * chaining. When the transfer described in that channel's parameter RAM
  540. * slot completes, that slot's data may be reloaded through a link.
  541. *
  542. * DMA errors are only reported to the @callback associated with the
  543. * channel driving that transfer, but transfer completion callbacks can
  544. * be sent to another channel under control of the TCC field in
  545. * the option word of the transfer's parameter RAM set. Drivers must not
  546. * use DMA transfer completion callbacks for channels they did not allocate.
  547. * (The same applies to TCC codes used in transfer chaining.)
  548. *
  549. * Returns the number of the channel, else negative errno.
  550. */
  551. int edma_alloc_channel(int channel,
  552. void (*callback)(unsigned channel, u16 ch_status, void *data),
  553. void *data,
  554. enum dma_event_q eventq_no)
  555. {
  556. unsigned i, done = 0, ctlr = 0;
  557. int ret = 0;
  558. if (!unused_chan_list_done) {
  559. /*
  560. * Scan all the platform devices to find out the EDMA channels
  561. * used and clear them in the unused list, making the rest
  562. * available for ARM usage.
  563. */
  564. ret = bus_for_each_dev(&platform_bus_type, NULL, NULL,
  565. prepare_unused_channel_list);
  566. if (ret < 0)
  567. return ret;
  568. unused_chan_list_done = true;
  569. }
  570. if (channel >= 0) {
  571. ctlr = EDMA_CTLR(channel);
  572. channel = EDMA_CHAN_SLOT(channel);
  573. }
  574. if (channel < 0) {
  575. for (i = 0; i < arch_num_cc; i++) {
  576. channel = 0;
  577. for (;;) {
  578. channel = find_next_bit(edma_cc[i]->edma_unused,
  579. edma_cc[i]->num_channels,
  580. channel);
  581. if (channel == edma_cc[i]->num_channels)
  582. break;
  583. if (!test_and_set_bit(channel,
  584. edma_cc[i]->edma_inuse)) {
  585. done = 1;
  586. ctlr = i;
  587. break;
  588. }
  589. channel++;
  590. }
  591. if (done)
  592. break;
  593. }
  594. if (!done)
  595. return -ENOMEM;
  596. } else if (channel >= edma_cc[ctlr]->num_channels) {
  597. return -EINVAL;
  598. } else if (test_and_set_bit(channel, edma_cc[ctlr]->edma_inuse)) {
  599. return -EBUSY;
  600. }
  601. /* ensure access through shadow region 0 */
  602. edma_or_array2(ctlr, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
  603. /* ensure no events are pending */
  604. edma_stop(EDMA_CTLR_CHAN(ctlr, channel));
  605. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(channel),
  606. &dummy_paramset, PARM_SIZE);
  607. if (callback)
  608. setup_dma_interrupt(EDMA_CTLR_CHAN(ctlr, channel),
  609. callback, data);
  610. map_dmach_queue(ctlr, channel, eventq_no);
  611. return EDMA_CTLR_CHAN(ctlr, channel);
  612. }
  613. EXPORT_SYMBOL(edma_alloc_channel);
  614. /**
  615. * edma_free_channel - deallocate DMA channel
  616. * @channel: dma channel returned from edma_alloc_channel()
  617. *
  618. * This deallocates the DMA channel and associated parameter RAM slot
  619. * allocated by edma_alloc_channel().
  620. *
  621. * Callers are responsible for ensuring the channel is inactive, and
  622. * will not be reactivated by linking, chaining, or software calls to
  623. * edma_start().
  624. */
  625. void edma_free_channel(unsigned channel)
  626. {
  627. unsigned ctlr;
  628. ctlr = EDMA_CTLR(channel);
  629. channel = EDMA_CHAN_SLOT(channel);
  630. if (channel >= edma_cc[ctlr]->num_channels)
  631. return;
  632. setup_dma_interrupt(channel, NULL, NULL);
  633. /* REVISIT should probably take out of shadow region 0 */
  634. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(channel),
  635. &dummy_paramset, PARM_SIZE);
  636. clear_bit(channel, edma_cc[ctlr]->edma_inuse);
  637. }
  638. EXPORT_SYMBOL(edma_free_channel);
  639. /**
  640. * edma_alloc_slot - allocate DMA parameter RAM
  641. * @slot: specific slot to allocate; negative for "any unused slot"
  642. *
  643. * This allocates a parameter RAM slot, initializing it to hold a
  644. * dummy transfer. Slots allocated using this routine have not been
  645. * mapped to a hardware DMA channel, and will normally be used by
  646. * linking to them from a slot associated with a DMA channel.
  647. *
  648. * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
  649. * slots may be allocated on behalf of DSP firmware.
  650. *
  651. * Returns the number of the slot, else negative errno.
  652. */
  653. int edma_alloc_slot(unsigned ctlr, int slot)
  654. {
  655. if (!edma_cc[ctlr])
  656. return -EINVAL;
  657. if (slot >= 0)
  658. slot = EDMA_CHAN_SLOT(slot);
  659. if (slot < 0) {
  660. slot = edma_cc[ctlr]->num_channels;
  661. for (;;) {
  662. slot = find_next_zero_bit(edma_cc[ctlr]->edma_inuse,
  663. edma_cc[ctlr]->num_slots, slot);
  664. if (slot == edma_cc[ctlr]->num_slots)
  665. return -ENOMEM;
  666. if (!test_and_set_bit(slot, edma_cc[ctlr]->edma_inuse))
  667. break;
  668. }
  669. } else if (slot < edma_cc[ctlr]->num_channels ||
  670. slot >= edma_cc[ctlr]->num_slots) {
  671. return -EINVAL;
  672. } else if (test_and_set_bit(slot, edma_cc[ctlr]->edma_inuse)) {
  673. return -EBUSY;
  674. }
  675. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  676. &dummy_paramset, PARM_SIZE);
  677. return EDMA_CTLR_CHAN(ctlr, slot);
  678. }
  679. EXPORT_SYMBOL(edma_alloc_slot);
  680. /**
  681. * edma_free_slot - deallocate DMA parameter RAM
  682. * @slot: parameter RAM slot returned from edma_alloc_slot()
  683. *
  684. * This deallocates the parameter RAM slot allocated by edma_alloc_slot().
  685. * Callers are responsible for ensuring the slot is inactive, and will
  686. * not be activated.
  687. */
  688. void edma_free_slot(unsigned slot)
  689. {
  690. unsigned ctlr;
  691. ctlr = EDMA_CTLR(slot);
  692. slot = EDMA_CHAN_SLOT(slot);
  693. if (slot < edma_cc[ctlr]->num_channels ||
  694. slot >= edma_cc[ctlr]->num_slots)
  695. return;
  696. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  697. &dummy_paramset, PARM_SIZE);
  698. clear_bit(slot, edma_cc[ctlr]->edma_inuse);
  699. }
  700. EXPORT_SYMBOL(edma_free_slot);
  701. /**
  702. * edma_alloc_cont_slots- alloc contiguous parameter RAM slots
  703. * The API will return the starting point of a set of
  704. * contiguous parameter RAM slots that have been requested
  705. *
  706. * @id: can only be EDMA_CONT_PARAMS_ANY or EDMA_CONT_PARAMS_FIXED_EXACT
  707. * or EDMA_CONT_PARAMS_FIXED_NOT_EXACT
  708. * @count: number of contiguous Paramter RAM slots
  709. * @slot - the start value of Parameter RAM slot that should be passed if id
  710. * is EDMA_CONT_PARAMS_FIXED_EXACT or EDMA_CONT_PARAMS_FIXED_NOT_EXACT
  711. *
  712. * If id is EDMA_CONT_PARAMS_ANY then the API starts looking for a set of
  713. * contiguous Parameter RAM slots from parameter RAM 64 in the case of
  714. * DaVinci SOCs and 32 in the case of DA8xx SOCs.
  715. *
  716. * If id is EDMA_CONT_PARAMS_FIXED_EXACT then the API starts looking for a
  717. * set of contiguous parameter RAM slots from the "slot" that is passed as an
  718. * argument to the API.
  719. *
  720. * If id is EDMA_CONT_PARAMS_FIXED_NOT_EXACT then the API initially tries
  721. * starts looking for a set of contiguous parameter RAMs from the "slot"
  722. * that is passed as an argument to the API. On failure the API will try to
  723. * find a set of contiguous Parameter RAM slots from the remaining Parameter
  724. * RAM slots
  725. */
  726. int edma_alloc_cont_slots(unsigned ctlr, unsigned int id, int slot, int count)
  727. {
  728. /*
  729. * The start slot requested should be greater than
  730. * the number of channels and lesser than the total number
  731. * of slots
  732. */
  733. if ((id != EDMA_CONT_PARAMS_ANY) &&
  734. (slot < edma_cc[ctlr]->num_channels ||
  735. slot >= edma_cc[ctlr]->num_slots))
  736. return -EINVAL;
  737. /*
  738. * The number of parameter RAM slots requested cannot be less than 1
  739. * and cannot be more than the number of slots minus the number of
  740. * channels
  741. */
  742. if (count < 1 || count >
  743. (edma_cc[ctlr]->num_slots - edma_cc[ctlr]->num_channels))
  744. return -EINVAL;
  745. switch (id) {
  746. case EDMA_CONT_PARAMS_ANY:
  747. return reserve_contiguous_slots(ctlr, id, count,
  748. edma_cc[ctlr]->num_channels);
  749. case EDMA_CONT_PARAMS_FIXED_EXACT:
  750. case EDMA_CONT_PARAMS_FIXED_NOT_EXACT:
  751. return reserve_contiguous_slots(ctlr, id, count, slot);
  752. default:
  753. return -EINVAL;
  754. }
  755. }
  756. EXPORT_SYMBOL(edma_alloc_cont_slots);
  757. /**
  758. * edma_free_cont_slots - deallocate DMA parameter RAM slots
  759. * @slot: first parameter RAM of a set of parameter RAM slots to be freed
  760. * @count: the number of contiguous parameter RAM slots to be freed
  761. *
  762. * This deallocates the parameter RAM slots allocated by
  763. * edma_alloc_cont_slots.
  764. * Callers/applications need to keep track of sets of contiguous
  765. * parameter RAM slots that have been allocated using the edma_alloc_cont_slots
  766. * API.
  767. * Callers are responsible for ensuring the slots are inactive, and will
  768. * not be activated.
  769. */
  770. int edma_free_cont_slots(unsigned slot, int count)
  771. {
  772. unsigned ctlr, slot_to_free;
  773. int i;
  774. ctlr = EDMA_CTLR(slot);
  775. slot = EDMA_CHAN_SLOT(slot);
  776. if (slot < edma_cc[ctlr]->num_channels ||
  777. slot >= edma_cc[ctlr]->num_slots ||
  778. count < 1)
  779. return -EINVAL;
  780. for (i = slot; i < slot + count; ++i) {
  781. ctlr = EDMA_CTLR(i);
  782. slot_to_free = EDMA_CHAN_SLOT(i);
  783. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot_to_free),
  784. &dummy_paramset, PARM_SIZE);
  785. clear_bit(slot_to_free, edma_cc[ctlr]->edma_inuse);
  786. }
  787. return 0;
  788. }
  789. EXPORT_SYMBOL(edma_free_cont_slots);
  790. /*-----------------------------------------------------------------------*/
  791. /* Parameter RAM operations (i) -- read/write partial slots */
  792. /**
  793. * edma_set_src - set initial DMA source address in parameter RAM slot
  794. * @slot: parameter RAM slot being configured
  795. * @src_port: physical address of source (memory, controller FIFO, etc)
  796. * @addressMode: INCR, except in very rare cases
  797. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  798. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  799. *
  800. * Note that the source address is modified during the DMA transfer
  801. * according to edma_set_src_index().
  802. */
  803. void edma_set_src(unsigned slot, dma_addr_t src_port,
  804. enum address_mode mode, enum fifo_width width)
  805. {
  806. unsigned ctlr;
  807. ctlr = EDMA_CTLR(slot);
  808. slot = EDMA_CHAN_SLOT(slot);
  809. if (slot < edma_cc[ctlr]->num_slots) {
  810. unsigned int i = edma_parm_read(ctlr, PARM_OPT, slot);
  811. if (mode) {
  812. /* set SAM and program FWID */
  813. i = (i & ~(EDMA_FWID)) | (SAM | ((width & 0x7) << 8));
  814. } else {
  815. /* clear SAM */
  816. i &= ~SAM;
  817. }
  818. edma_parm_write(ctlr, PARM_OPT, slot, i);
  819. /* set the source port address
  820. in source register of param structure */
  821. edma_parm_write(ctlr, PARM_SRC, slot, src_port);
  822. }
  823. }
  824. EXPORT_SYMBOL(edma_set_src);
  825. /**
  826. * edma_set_dest - set initial DMA destination address in parameter RAM slot
  827. * @slot: parameter RAM slot being configured
  828. * @dest_port: physical address of destination (memory, controller FIFO, etc)
  829. * @addressMode: INCR, except in very rare cases
  830. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  831. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  832. *
  833. * Note that the destination address is modified during the DMA transfer
  834. * according to edma_set_dest_index().
  835. */
  836. void edma_set_dest(unsigned slot, dma_addr_t dest_port,
  837. enum address_mode mode, enum fifo_width width)
  838. {
  839. unsigned ctlr;
  840. ctlr = EDMA_CTLR(slot);
  841. slot = EDMA_CHAN_SLOT(slot);
  842. if (slot < edma_cc[ctlr]->num_slots) {
  843. unsigned int i = edma_parm_read(ctlr, PARM_OPT, slot);
  844. if (mode) {
  845. /* set DAM and program FWID */
  846. i = (i & ~(EDMA_FWID)) | (DAM | ((width & 0x7) << 8));
  847. } else {
  848. /* clear DAM */
  849. i &= ~DAM;
  850. }
  851. edma_parm_write(ctlr, PARM_OPT, slot, i);
  852. /* set the destination port address
  853. in dest register of param structure */
  854. edma_parm_write(ctlr, PARM_DST, slot, dest_port);
  855. }
  856. }
  857. EXPORT_SYMBOL(edma_set_dest);
  858. /**
  859. * edma_get_position - returns the current transfer points
  860. * @slot: parameter RAM slot being examined
  861. * @src: pointer to source port position
  862. * @dst: pointer to destination port position
  863. *
  864. * Returns current source and destination addresses for a particular
  865. * parameter RAM slot. Its channel should not be active when this is called.
  866. */
  867. void edma_get_position(unsigned slot, dma_addr_t *src, dma_addr_t *dst)
  868. {
  869. struct edmacc_param temp;
  870. unsigned ctlr;
  871. ctlr = EDMA_CTLR(slot);
  872. slot = EDMA_CHAN_SLOT(slot);
  873. edma_read_slot(EDMA_CTLR_CHAN(ctlr, slot), &temp);
  874. if (src != NULL)
  875. *src = temp.src;
  876. if (dst != NULL)
  877. *dst = temp.dst;
  878. }
  879. EXPORT_SYMBOL(edma_get_position);
  880. /**
  881. * edma_set_src_index - configure DMA source address indexing
  882. * @slot: parameter RAM slot being configured
  883. * @src_bidx: byte offset between source arrays in a frame
  884. * @src_cidx: byte offset between source frames in a block
  885. *
  886. * Offsets are specified to support either contiguous or discontiguous
  887. * memory transfers, or repeated access to a hardware register, as needed.
  888. * When accessing hardware registers, both offsets are normally zero.
  889. */
  890. void edma_set_src_index(unsigned slot, s16 src_bidx, s16 src_cidx)
  891. {
  892. unsigned ctlr;
  893. ctlr = EDMA_CTLR(slot);
  894. slot = EDMA_CHAN_SLOT(slot);
  895. if (slot < edma_cc[ctlr]->num_slots) {
  896. edma_parm_modify(ctlr, PARM_SRC_DST_BIDX, slot,
  897. 0xffff0000, src_bidx);
  898. edma_parm_modify(ctlr, PARM_SRC_DST_CIDX, slot,
  899. 0xffff0000, src_cidx);
  900. }
  901. }
  902. EXPORT_SYMBOL(edma_set_src_index);
  903. /**
  904. * edma_set_dest_index - configure DMA destination address indexing
  905. * @slot: parameter RAM slot being configured
  906. * @dest_bidx: byte offset between destination arrays in a frame
  907. * @dest_cidx: byte offset between destination frames in a block
  908. *
  909. * Offsets are specified to support either contiguous or discontiguous
  910. * memory transfers, or repeated access to a hardware register, as needed.
  911. * When accessing hardware registers, both offsets are normally zero.
  912. */
  913. void edma_set_dest_index(unsigned slot, s16 dest_bidx, s16 dest_cidx)
  914. {
  915. unsigned ctlr;
  916. ctlr = EDMA_CTLR(slot);
  917. slot = EDMA_CHAN_SLOT(slot);
  918. if (slot < edma_cc[ctlr]->num_slots) {
  919. edma_parm_modify(ctlr, PARM_SRC_DST_BIDX, slot,
  920. 0x0000ffff, dest_bidx << 16);
  921. edma_parm_modify(ctlr, PARM_SRC_DST_CIDX, slot,
  922. 0x0000ffff, dest_cidx << 16);
  923. }
  924. }
  925. EXPORT_SYMBOL(edma_set_dest_index);
  926. /**
  927. * edma_set_transfer_params - configure DMA transfer parameters
  928. * @slot: parameter RAM slot being configured
  929. * @acnt: how many bytes per array (at least one)
  930. * @bcnt: how many arrays per frame (at least one)
  931. * @ccnt: how many frames per block (at least one)
  932. * @bcnt_rld: used only for A-Synchronized transfers; this specifies
  933. * the value to reload into bcnt when it decrements to zero
  934. * @sync_mode: ASYNC or ABSYNC
  935. *
  936. * See the EDMA3 documentation to understand how to configure and link
  937. * transfers using the fields in PaRAM slots. If you are not doing it
  938. * all at once with edma_write_slot(), you will use this routine
  939. * plus two calls each for source and destination, setting the initial
  940. * address and saying how to index that address.
  941. *
  942. * An example of an A-Synchronized transfer is a serial link using a
  943. * single word shift register. In that case, @acnt would be equal to
  944. * that word size; the serial controller issues a DMA synchronization
  945. * event to transfer each word, and memory access by the DMA transfer
  946. * controller will be word-at-a-time.
  947. *
  948. * An example of an AB-Synchronized transfer is a device using a FIFO.
  949. * In that case, @acnt equals the FIFO width and @bcnt equals its depth.
  950. * The controller with the FIFO issues DMA synchronization events when
  951. * the FIFO threshold is reached, and the DMA transfer controller will
  952. * transfer one frame to (or from) the FIFO. It will probably use
  953. * efficient burst modes to access memory.
  954. */
  955. void edma_set_transfer_params(unsigned slot,
  956. u16 acnt, u16 bcnt, u16 ccnt,
  957. u16 bcnt_rld, enum sync_dimension sync_mode)
  958. {
  959. unsigned ctlr;
  960. ctlr = EDMA_CTLR(slot);
  961. slot = EDMA_CHAN_SLOT(slot);
  962. if (slot < edma_cc[ctlr]->num_slots) {
  963. edma_parm_modify(ctlr, PARM_LINK_BCNTRLD, slot,
  964. 0x0000ffff, bcnt_rld << 16);
  965. if (sync_mode == ASYNC)
  966. edma_parm_and(ctlr, PARM_OPT, slot, ~SYNCDIM);
  967. else
  968. edma_parm_or(ctlr, PARM_OPT, slot, SYNCDIM);
  969. /* Set the acount, bcount, ccount registers */
  970. edma_parm_write(ctlr, PARM_A_B_CNT, slot, (bcnt << 16) | acnt);
  971. edma_parm_write(ctlr, PARM_CCNT, slot, ccnt);
  972. }
  973. }
  974. EXPORT_SYMBOL(edma_set_transfer_params);
  975. /**
  976. * edma_link - link one parameter RAM slot to another
  977. * @from: parameter RAM slot originating the link
  978. * @to: parameter RAM slot which is the link target
  979. *
  980. * The originating slot should not be part of any active DMA transfer.
  981. */
  982. void edma_link(unsigned from, unsigned to)
  983. {
  984. unsigned ctlr_from, ctlr_to;
  985. ctlr_from = EDMA_CTLR(from);
  986. from = EDMA_CHAN_SLOT(from);
  987. ctlr_to = EDMA_CTLR(to);
  988. to = EDMA_CHAN_SLOT(to);
  989. if (from >= edma_cc[ctlr_from]->num_slots)
  990. return;
  991. if (to >= edma_cc[ctlr_to]->num_slots)
  992. return;
  993. edma_parm_modify(ctlr_from, PARM_LINK_BCNTRLD, from, 0xffff0000,
  994. PARM_OFFSET(to));
  995. }
  996. EXPORT_SYMBOL(edma_link);
  997. /**
  998. * edma_unlink - cut link from one parameter RAM slot
  999. * @from: parameter RAM slot originating the link
  1000. *
  1001. * The originating slot should not be part of any active DMA transfer.
  1002. * Its link is set to 0xffff.
  1003. */
  1004. void edma_unlink(unsigned from)
  1005. {
  1006. unsigned ctlr;
  1007. ctlr = EDMA_CTLR(from);
  1008. from = EDMA_CHAN_SLOT(from);
  1009. if (from >= edma_cc[ctlr]->num_slots)
  1010. return;
  1011. edma_parm_or(ctlr, PARM_LINK_BCNTRLD, from, 0xffff);
  1012. }
  1013. EXPORT_SYMBOL(edma_unlink);
  1014. /*-----------------------------------------------------------------------*/
  1015. /* Parameter RAM operations (ii) -- read/write whole parameter sets */
  1016. /**
  1017. * edma_write_slot - write parameter RAM data for slot
  1018. * @slot: number of parameter RAM slot being modified
  1019. * @param: data to be written into parameter RAM slot
  1020. *
  1021. * Use this to assign all parameters of a transfer at once. This
  1022. * allows more efficient setup of transfers than issuing multiple
  1023. * calls to set up those parameters in small pieces, and provides
  1024. * complete control over all transfer options.
  1025. */
  1026. void edma_write_slot(unsigned slot, const struct edmacc_param *param)
  1027. {
  1028. unsigned ctlr;
  1029. ctlr = EDMA_CTLR(slot);
  1030. slot = EDMA_CHAN_SLOT(slot);
  1031. if (slot >= edma_cc[ctlr]->num_slots)
  1032. return;
  1033. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot), param,
  1034. PARM_SIZE);
  1035. }
  1036. EXPORT_SYMBOL(edma_write_slot);
  1037. /**
  1038. * edma_read_slot - read parameter RAM data from slot
  1039. * @slot: number of parameter RAM slot being copied
  1040. * @param: where to store copy of parameter RAM data
  1041. *
  1042. * Use this to read data from a parameter RAM slot, perhaps to
  1043. * save them as a template for later reuse.
  1044. */
  1045. void edma_read_slot(unsigned slot, struct edmacc_param *param)
  1046. {
  1047. unsigned ctlr;
  1048. ctlr = EDMA_CTLR(slot);
  1049. slot = EDMA_CHAN_SLOT(slot);
  1050. if (slot >= edma_cc[ctlr]->num_slots)
  1051. return;
  1052. memcpy_fromio(param, edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  1053. PARM_SIZE);
  1054. }
  1055. EXPORT_SYMBOL(edma_read_slot);
  1056. /*-----------------------------------------------------------------------*/
  1057. /* Various EDMA channel control operations */
  1058. /**
  1059. * edma_pause - pause dma on a channel
  1060. * @channel: on which edma_start() has been called
  1061. *
  1062. * This temporarily disables EDMA hardware events on the specified channel,
  1063. * preventing them from triggering new transfers on its behalf
  1064. */
  1065. void edma_pause(unsigned channel)
  1066. {
  1067. unsigned ctlr;
  1068. ctlr = EDMA_CTLR(channel);
  1069. channel = EDMA_CHAN_SLOT(channel);
  1070. if (channel < edma_cc[ctlr]->num_channels) {
  1071. unsigned int mask = BIT(channel & 0x1f);
  1072. edma_shadow0_write_array(ctlr, SH_EECR, channel >> 5, mask);
  1073. }
  1074. }
  1075. EXPORT_SYMBOL(edma_pause);
  1076. /**
  1077. * edma_resume - resumes dma on a paused channel
  1078. * @channel: on which edma_pause() has been called
  1079. *
  1080. * This re-enables EDMA hardware events on the specified channel.
  1081. */
  1082. void edma_resume(unsigned channel)
  1083. {
  1084. unsigned ctlr;
  1085. ctlr = EDMA_CTLR(channel);
  1086. channel = EDMA_CHAN_SLOT(channel);
  1087. if (channel < edma_cc[ctlr]->num_channels) {
  1088. unsigned int mask = BIT(channel & 0x1f);
  1089. edma_shadow0_write_array(ctlr, SH_EESR, channel >> 5, mask);
  1090. }
  1091. }
  1092. EXPORT_SYMBOL(edma_resume);
  1093. /**
  1094. * edma_start - start dma on a channel
  1095. * @channel: channel being activated
  1096. *
  1097. * Channels with event associations will be triggered by their hardware
  1098. * events, and channels without such associations will be triggered by
  1099. * software. (At this writing there is no interface for using software
  1100. * triggers except with channels that don't support hardware triggers.)
  1101. *
  1102. * Returns zero on success, else negative errno.
  1103. */
  1104. int edma_start(unsigned channel)
  1105. {
  1106. unsigned ctlr;
  1107. ctlr = EDMA_CTLR(channel);
  1108. channel = EDMA_CHAN_SLOT(channel);
  1109. if (channel < edma_cc[ctlr]->num_channels) {
  1110. int j = channel >> 5;
  1111. unsigned int mask = BIT(channel & 0x1f);
  1112. /* EDMA channels without event association */
  1113. if (test_bit(channel, edma_cc[ctlr]->edma_unused)) {
  1114. pr_debug("EDMA: ESR%d %08x\n", j,
  1115. edma_shadow0_read_array(ctlr, SH_ESR, j));
  1116. edma_shadow0_write_array(ctlr, SH_ESR, j, mask);
  1117. return 0;
  1118. }
  1119. /* EDMA channel with event association */
  1120. pr_debug("EDMA: ER%d %08x\n", j,
  1121. edma_shadow0_read_array(ctlr, SH_ER, j));
  1122. /* Clear any pending event or error */
  1123. edma_write_array(ctlr, EDMA_ECR, j, mask);
  1124. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1125. /* Clear any SER */
  1126. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1127. edma_shadow0_write_array(ctlr, SH_EESR, j, mask);
  1128. pr_debug("EDMA: EER%d %08x\n", j,
  1129. edma_shadow0_read_array(ctlr, SH_EER, j));
  1130. return 0;
  1131. }
  1132. return -EINVAL;
  1133. }
  1134. EXPORT_SYMBOL(edma_start);
  1135. /**
  1136. * edma_stop - stops dma on the channel passed
  1137. * @channel: channel being deactivated
  1138. *
  1139. * When @lch is a channel, any active transfer is paused and
  1140. * all pending hardware events are cleared. The current transfer
  1141. * may not be resumed, and the channel's Parameter RAM should be
  1142. * reinitialized before being reused.
  1143. */
  1144. void edma_stop(unsigned channel)
  1145. {
  1146. unsigned ctlr;
  1147. ctlr = EDMA_CTLR(channel);
  1148. channel = EDMA_CHAN_SLOT(channel);
  1149. if (channel < edma_cc[ctlr]->num_channels) {
  1150. int j = channel >> 5;
  1151. unsigned int mask = BIT(channel & 0x1f);
  1152. edma_shadow0_write_array(ctlr, SH_EECR, j, mask);
  1153. edma_shadow0_write_array(ctlr, SH_ECR, j, mask);
  1154. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1155. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1156. pr_debug("EDMA: EER%d %08x\n", j,
  1157. edma_shadow0_read_array(ctlr, SH_EER, j));
  1158. /* REVISIT: consider guarding against inappropriate event
  1159. * chaining by overwriting with dummy_paramset.
  1160. */
  1161. }
  1162. }
  1163. EXPORT_SYMBOL(edma_stop);
  1164. /******************************************************************************
  1165. *
  1166. * It cleans ParamEntry qand bring back EDMA to initial state if media has
  1167. * been removed before EDMA has finished.It is usedful for removable media.
  1168. * Arguments:
  1169. * ch_no - channel no
  1170. *
  1171. * Return: zero on success, or corresponding error no on failure
  1172. *
  1173. * FIXME this should not be needed ... edma_stop() should suffice.
  1174. *
  1175. *****************************************************************************/
  1176. void edma_clean_channel(unsigned channel)
  1177. {
  1178. unsigned ctlr;
  1179. ctlr = EDMA_CTLR(channel);
  1180. channel = EDMA_CHAN_SLOT(channel);
  1181. if (channel < edma_cc[ctlr]->num_channels) {
  1182. int j = (channel >> 5);
  1183. unsigned int mask = BIT(channel & 0x1f);
  1184. pr_debug("EDMA: EMR%d %08x\n", j,
  1185. edma_read_array(ctlr, EDMA_EMR, j));
  1186. edma_shadow0_write_array(ctlr, SH_ECR, j, mask);
  1187. /* Clear the corresponding EMR bits */
  1188. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1189. /* Clear any SER */
  1190. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1191. edma_write(ctlr, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
  1192. }
  1193. }
  1194. EXPORT_SYMBOL(edma_clean_channel);
  1195. /*
  1196. * edma_clear_event - clear an outstanding event on the DMA channel
  1197. * Arguments:
  1198. * channel - channel number
  1199. */
  1200. void edma_clear_event(unsigned channel)
  1201. {
  1202. unsigned ctlr;
  1203. ctlr = EDMA_CTLR(channel);
  1204. channel = EDMA_CHAN_SLOT(channel);
  1205. if (channel >= edma_cc[ctlr]->num_channels)
  1206. return;
  1207. if (channel < 32)
  1208. edma_write(ctlr, EDMA_ECR, BIT(channel));
  1209. else
  1210. edma_write(ctlr, EDMA_ECRH, BIT(channel - 32));
  1211. }
  1212. EXPORT_SYMBOL(edma_clear_event);
  1213. /*-----------------------------------------------------------------------*/
  1214. static int __init edma_probe(struct platform_device *pdev)
  1215. {
  1216. struct edma_soc_info **info = pdev->dev.platform_data;
  1217. const s8 (*queue_priority_mapping)[2];
  1218. const s8 (*queue_tc_mapping)[2];
  1219. int i, j, off, ln, found = 0;
  1220. int status = -1;
  1221. const s16 (*rsv_chans)[2];
  1222. const s16 (*rsv_slots)[2];
  1223. int irq[EDMA_MAX_CC] = {0, 0};
  1224. int err_irq[EDMA_MAX_CC] = {0, 0};
  1225. struct resource *r[EDMA_MAX_CC] = {NULL};
  1226. resource_size_t len[EDMA_MAX_CC];
  1227. char res_name[10];
  1228. char irq_name[10];
  1229. if (!info)
  1230. return -ENODEV;
  1231. for (j = 0; j < EDMA_MAX_CC; j++) {
  1232. sprintf(res_name, "edma_cc%d", j);
  1233. r[j] = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1234. res_name);
  1235. if (!r[j] || !info[j]) {
  1236. if (found)
  1237. break;
  1238. else
  1239. return -ENODEV;
  1240. } else {
  1241. found = 1;
  1242. }
  1243. len[j] = resource_size(r[j]);
  1244. r[j] = request_mem_region(r[j]->start, len[j],
  1245. dev_name(&pdev->dev));
  1246. if (!r[j]) {
  1247. status = -EBUSY;
  1248. goto fail1;
  1249. }
  1250. edmacc_regs_base[j] = ioremap(r[j]->start, len[j]);
  1251. if (!edmacc_regs_base[j]) {
  1252. status = -EBUSY;
  1253. goto fail1;
  1254. }
  1255. edma_cc[j] = kzalloc(sizeof(struct edma), GFP_KERNEL);
  1256. if (!edma_cc[j]) {
  1257. status = -ENOMEM;
  1258. goto fail1;
  1259. }
  1260. edma_cc[j]->num_channels = min_t(unsigned, info[j]->n_channel,
  1261. EDMA_MAX_DMACH);
  1262. edma_cc[j]->num_slots = min_t(unsigned, info[j]->n_slot,
  1263. EDMA_MAX_PARAMENTRY);
  1264. edma_cc[j]->num_cc = min_t(unsigned, info[j]->n_cc,
  1265. EDMA_MAX_CC);
  1266. edma_cc[j]->default_queue = info[j]->default_queue;
  1267. dev_dbg(&pdev->dev, "DMA REG BASE ADDR=%p\n",
  1268. edmacc_regs_base[j]);
  1269. for (i = 0; i < edma_cc[j]->num_slots; i++)
  1270. memcpy_toio(edmacc_regs_base[j] + PARM_OFFSET(i),
  1271. &dummy_paramset, PARM_SIZE);
  1272. /* Mark all channels as unused */
  1273. memset(edma_cc[j]->edma_unused, 0xff,
  1274. sizeof(edma_cc[j]->edma_unused));
  1275. if (info[j]->rsv) {
  1276. /* Clear the reserved channels in unused list */
  1277. rsv_chans = info[j]->rsv->rsv_chans;
  1278. if (rsv_chans) {
  1279. for (i = 0; rsv_chans[i][0] != -1; i++) {
  1280. off = rsv_chans[i][0];
  1281. ln = rsv_chans[i][1];
  1282. clear_bits(off, ln,
  1283. edma_cc[j]->edma_unused);
  1284. }
  1285. }
  1286. /* Set the reserved slots in inuse list */
  1287. rsv_slots = info[j]->rsv->rsv_slots;
  1288. if (rsv_slots) {
  1289. for (i = 0; rsv_slots[i][0] != -1; i++) {
  1290. off = rsv_slots[i][0];
  1291. ln = rsv_slots[i][1];
  1292. set_bits(off, ln,
  1293. edma_cc[j]->edma_inuse);
  1294. }
  1295. }
  1296. }
  1297. sprintf(irq_name, "edma%d", j);
  1298. irq[j] = platform_get_irq_byname(pdev, irq_name);
  1299. edma_cc[j]->irq_res_start = irq[j];
  1300. status = request_irq(irq[j], dma_irq_handler, 0, "edma",
  1301. &pdev->dev);
  1302. if (status < 0) {
  1303. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  1304. irq[j], status);
  1305. goto fail;
  1306. }
  1307. sprintf(irq_name, "edma%d_err", j);
  1308. err_irq[j] = platform_get_irq_byname(pdev, irq_name);
  1309. edma_cc[j]->irq_res_end = err_irq[j];
  1310. status = request_irq(err_irq[j], dma_ccerr_handler, 0,
  1311. "edma_error", &pdev->dev);
  1312. if (status < 0) {
  1313. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  1314. err_irq[j], status);
  1315. goto fail;
  1316. }
  1317. for (i = 0; i < edma_cc[j]->num_channels; i++)
  1318. map_dmach_queue(j, i, info[j]->default_queue);
  1319. queue_tc_mapping = info[j]->queue_tc_mapping;
  1320. queue_priority_mapping = info[j]->queue_priority_mapping;
  1321. /* Event queue to TC mapping */
  1322. for (i = 0; queue_tc_mapping[i][0] != -1; i++)
  1323. map_queue_tc(j, queue_tc_mapping[i][0],
  1324. queue_tc_mapping[i][1]);
  1325. /* Event queue priority mapping */
  1326. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  1327. assign_priority_to_queue(j,
  1328. queue_priority_mapping[i][0],
  1329. queue_priority_mapping[i][1]);
  1330. /* Map the channel to param entry if channel mapping logic
  1331. * exist
  1332. */
  1333. if (edma_read(j, EDMA_CCCFG) & CHMAP_EXIST)
  1334. map_dmach_param(j);
  1335. for (i = 0; i < info[j]->n_region; i++) {
  1336. edma_write_array2(j, EDMA_DRAE, i, 0, 0x0);
  1337. edma_write_array2(j, EDMA_DRAE, i, 1, 0x0);
  1338. edma_write_array(j, EDMA_QRAE, i, 0x0);
  1339. }
  1340. arch_num_cc++;
  1341. }
  1342. if (tc_errs_handled) {
  1343. status = request_irq(IRQ_TCERRINT0, dma_tc0err_handler, 0,
  1344. "edma_tc0", &pdev->dev);
  1345. if (status < 0) {
  1346. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  1347. IRQ_TCERRINT0, status);
  1348. return status;
  1349. }
  1350. status = request_irq(IRQ_TCERRINT, dma_tc1err_handler, 0,
  1351. "edma_tc1", &pdev->dev);
  1352. if (status < 0) {
  1353. dev_dbg(&pdev->dev, "request_irq %d --> %d\n",
  1354. IRQ_TCERRINT, status);
  1355. return status;
  1356. }
  1357. }
  1358. return 0;
  1359. fail:
  1360. for (i = 0; i < EDMA_MAX_CC; i++) {
  1361. if (err_irq[i])
  1362. free_irq(err_irq[i], &pdev->dev);
  1363. if (irq[i])
  1364. free_irq(irq[i], &pdev->dev);
  1365. }
  1366. fail1:
  1367. for (i = 0; i < EDMA_MAX_CC; i++) {
  1368. if (r[i])
  1369. release_mem_region(r[i]->start, len[i]);
  1370. if (edmacc_regs_base[i])
  1371. iounmap(edmacc_regs_base[i]);
  1372. kfree(edma_cc[i]);
  1373. }
  1374. return status;
  1375. }
  1376. static struct platform_driver edma_driver = {
  1377. .driver.name = "edma",
  1378. };
  1379. static int __init edma_init(void)
  1380. {
  1381. return platform_driver_probe(&edma_driver, edma_probe);
  1382. }
  1383. arch_initcall(edma_init);