dma.c 43 KB

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