dma.c 43 KB

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