dma.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. int stop_param = start_param;
  464. DECLARE_BITMAP(tmp_inuse, EDMA_MAX_PARAMENTRY);
  465. for (i = start_param; i < edma_info[ctlr]->num_slots; ++i) {
  466. j = EDMA_CHAN_SLOT(i);
  467. if (!test_and_set_bit(j, edma_info[ctlr]->edma_inuse)) {
  468. /* Record our current beginning slot */
  469. if (count == num_params)
  470. stop_param = i;
  471. count--;
  472. set_bit(j, tmp_inuse);
  473. if (count == 0)
  474. break;
  475. } else {
  476. clear_bit(j, tmp_inuse);
  477. if (id == EDMA_CONT_PARAMS_FIXED_EXACT) {
  478. stop_param = i;
  479. break;
  480. } else
  481. count = num_params;
  482. }
  483. }
  484. /*
  485. * We have to clear any bits that we set
  486. * if we run out parameter RAMs, i.e we do find a set
  487. * of contiguous parameter RAMs but do not find the exact number
  488. * requested as we may reach the total number of parameter RAMs
  489. */
  490. if (i == edma_info[ctlr]->num_slots)
  491. stop_param = i;
  492. for (j = start_param; j < stop_param; j++)
  493. if (test_bit(j, tmp_inuse))
  494. clear_bit(j, edma_info[ctlr]->edma_inuse);
  495. if (count)
  496. return -EBUSY;
  497. for (j = i - num_params + 1; j <= i; ++j)
  498. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(j),
  499. &dummy_paramset, PARM_SIZE);
  500. return EDMA_CTLR_CHAN(ctlr, i - num_params + 1);
  501. }
  502. /*-----------------------------------------------------------------------*/
  503. /* Resource alloc/free: dma channels, parameter RAM slots */
  504. /**
  505. * edma_alloc_channel - allocate DMA channel and paired parameter RAM
  506. * @channel: specific channel to allocate; negative for "any unmapped channel"
  507. * @callback: optional; to be issued on DMA completion or errors
  508. * @data: passed to callback
  509. * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
  510. * Controller (TC) executes requests using this channel. Use
  511. * EVENTQ_DEFAULT unless you really need a high priority queue.
  512. *
  513. * This allocates a DMA channel and its associated parameter RAM slot.
  514. * The parameter RAM is initialized to hold a dummy transfer.
  515. *
  516. * Normal use is to pass a specific channel number as @channel, to make
  517. * use of hardware events mapped to that channel. When the channel will
  518. * be used only for software triggering or event chaining, channels not
  519. * mapped to hardware events (or mapped to unused events) are preferable.
  520. *
  521. * DMA transfers start from a channel using edma_start(), or by
  522. * chaining. When the transfer described in that channel's parameter RAM
  523. * slot completes, that slot's data may be reloaded through a link.
  524. *
  525. * DMA errors are only reported to the @callback associated with the
  526. * channel driving that transfer, but transfer completion callbacks can
  527. * be sent to another channel under control of the TCC field in
  528. * the option word of the transfer's parameter RAM set. Drivers must not
  529. * use DMA transfer completion callbacks for channels they did not allocate.
  530. * (The same applies to TCC codes used in transfer chaining.)
  531. *
  532. * Returns the number of the channel, else negative errno.
  533. */
  534. int edma_alloc_channel(int channel,
  535. void (*callback)(unsigned channel, u16 ch_status, void *data),
  536. void *data,
  537. enum dma_event_q eventq_no)
  538. {
  539. unsigned i, done, ctlr = 0;
  540. if (channel >= 0) {
  541. ctlr = EDMA_CTLR(channel);
  542. channel = EDMA_CHAN_SLOT(channel);
  543. }
  544. if (channel < 0) {
  545. for (i = 0; i < EDMA_MAX_CC; i++) {
  546. channel = 0;
  547. for (;;) {
  548. channel = find_next_bit(edma_info[i]->
  549. edma_noevent,
  550. edma_info[i]->num_channels,
  551. channel);
  552. if (channel == edma_info[i]->num_channels)
  553. return -ENOMEM;
  554. if (!test_and_set_bit(channel,
  555. edma_info[i]->edma_inuse)) {
  556. done = 1;
  557. ctlr = i;
  558. break;
  559. }
  560. channel++;
  561. }
  562. if (done)
  563. break;
  564. }
  565. } else if (channel >= edma_info[ctlr]->num_channels) {
  566. return -EINVAL;
  567. } else if (test_and_set_bit(channel, edma_info[ctlr]->edma_inuse)) {
  568. return -EBUSY;
  569. }
  570. /* ensure access through shadow region 0 */
  571. edma_or_array2(ctlr, EDMA_DRAE, 0, channel >> 5, 1 << (channel & 0x1f));
  572. /* ensure no events are pending */
  573. edma_stop(EDMA_CTLR_CHAN(ctlr, channel));
  574. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(channel),
  575. &dummy_paramset, PARM_SIZE);
  576. if (callback)
  577. setup_dma_interrupt(EDMA_CTLR_CHAN(ctlr, channel),
  578. callback, data);
  579. map_dmach_queue(ctlr, channel, eventq_no);
  580. return channel;
  581. }
  582. EXPORT_SYMBOL(edma_alloc_channel);
  583. /**
  584. * edma_free_channel - deallocate DMA channel
  585. * @channel: dma channel returned from edma_alloc_channel()
  586. *
  587. * This deallocates the DMA channel and associated parameter RAM slot
  588. * allocated by edma_alloc_channel().
  589. *
  590. * Callers are responsible for ensuring the channel is inactive, and
  591. * will not be reactivated by linking, chaining, or software calls to
  592. * edma_start().
  593. */
  594. void edma_free_channel(unsigned channel)
  595. {
  596. unsigned ctlr;
  597. ctlr = EDMA_CTLR(channel);
  598. channel = EDMA_CHAN_SLOT(channel);
  599. if (channel >= edma_info[ctlr]->num_channels)
  600. return;
  601. setup_dma_interrupt(channel, NULL, NULL);
  602. /* REVISIT should probably take out of shadow region 0 */
  603. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(channel),
  604. &dummy_paramset, PARM_SIZE);
  605. clear_bit(channel, edma_info[ctlr]->edma_inuse);
  606. }
  607. EXPORT_SYMBOL(edma_free_channel);
  608. /**
  609. * edma_alloc_slot - allocate DMA parameter RAM
  610. * @slot: specific slot to allocate; negative for "any unused slot"
  611. *
  612. * This allocates a parameter RAM slot, initializing it to hold a
  613. * dummy transfer. Slots allocated using this routine have not been
  614. * mapped to a hardware DMA channel, and will normally be used by
  615. * linking to them from a slot associated with a DMA channel.
  616. *
  617. * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
  618. * slots may be allocated on behalf of DSP firmware.
  619. *
  620. * Returns the number of the slot, else negative errno.
  621. */
  622. int edma_alloc_slot(unsigned ctlr, int slot)
  623. {
  624. if (slot >= 0)
  625. slot = EDMA_CHAN_SLOT(slot);
  626. if (slot < 0) {
  627. slot = edma_info[ctlr]->num_channels;
  628. for (;;) {
  629. slot = find_next_zero_bit(edma_info[ctlr]->edma_inuse,
  630. edma_info[ctlr]->num_slots, slot);
  631. if (slot == edma_info[ctlr]->num_slots)
  632. return -ENOMEM;
  633. if (!test_and_set_bit(slot,
  634. edma_info[ctlr]->edma_inuse))
  635. break;
  636. }
  637. } else if (slot < edma_info[ctlr]->num_channels ||
  638. slot >= edma_info[ctlr]->num_slots) {
  639. return -EINVAL;
  640. } else if (test_and_set_bit(slot, edma_info[ctlr]->edma_inuse)) {
  641. return -EBUSY;
  642. }
  643. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  644. &dummy_paramset, PARM_SIZE);
  645. return EDMA_CTLR_CHAN(ctlr, slot);
  646. }
  647. EXPORT_SYMBOL(edma_alloc_slot);
  648. /**
  649. * edma_free_slot - deallocate DMA parameter RAM
  650. * @slot: parameter RAM slot returned from edma_alloc_slot()
  651. *
  652. * This deallocates the parameter RAM slot allocated by edma_alloc_slot().
  653. * Callers are responsible for ensuring the slot is inactive, and will
  654. * not be activated.
  655. */
  656. void edma_free_slot(unsigned slot)
  657. {
  658. unsigned ctlr;
  659. ctlr = EDMA_CTLR(slot);
  660. slot = EDMA_CHAN_SLOT(slot);
  661. if (slot < edma_info[ctlr]->num_channels ||
  662. slot >= edma_info[ctlr]->num_slots)
  663. return;
  664. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  665. &dummy_paramset, PARM_SIZE);
  666. clear_bit(slot, edma_info[ctlr]->edma_inuse);
  667. }
  668. EXPORT_SYMBOL(edma_free_slot);
  669. /**
  670. * edma_alloc_cont_slots- alloc contiguous parameter RAM slots
  671. * The API will return the starting point of a set of
  672. * contiguous PARAM's that have been requested
  673. *
  674. * @id: can only be EDMA_CONT_PARAMS_ANY or EDMA_CONT_PARAMS_FIXED_EXACT
  675. * or EDMA_CONT_PARAMS_FIXED_NOT_EXACT
  676. * @count: number of contiguous Paramter RAM's
  677. * @param - the start value of Parameter RAM that should be passed if id
  678. * is EDMA_CONT_PARAMS_FIXED_EXACT or EDMA_CONT_PARAMS_FIXED_NOT_EXACT
  679. *
  680. * If id is EDMA_CONT_PARAMS_ANY then the API starts looking for a set of
  681. * contiguous Parameter RAMs from parameter RAM 64 in the case of DaVinci SOCs
  682. * and 32 in the case of Primus
  683. *
  684. * If id is EDMA_CONT_PARAMS_FIXED_EXACT then the API starts looking for a
  685. * set of contiguous parameter RAMs from the "param" that is passed as an
  686. * argument to the API.
  687. *
  688. * If id is EDMA_CONT_PARAMS_FIXED_NOT_EXACT then the API initially tries
  689. * starts looking for a set of contiguous parameter RAMs from the "param"
  690. * that is passed as an argument to the API. On failure the API will try to
  691. * find a set of contiguous Parameter RAMs in the remaining Parameter RAMs
  692. */
  693. int edma_alloc_cont_slots(unsigned ctlr, unsigned int id, int slot, int count)
  694. {
  695. /*
  696. * The start slot requested should be greater than
  697. * the number of channels and lesser than the total number
  698. * of slots
  699. */
  700. if ((id != EDMA_CONT_PARAMS_ANY) &&
  701. (slot < edma_info[ctlr]->num_channels ||
  702. slot >= edma_info[ctlr]->num_slots))
  703. return -EINVAL;
  704. /*
  705. * The number of parameter RAMs requested cannot be less than 1
  706. * and cannot be more than the number of slots minus the number of
  707. * channels
  708. */
  709. if (count < 1 || count >
  710. (edma_info[ctlr]->num_slots - edma_info[ctlr]->num_channels))
  711. return -EINVAL;
  712. switch (id) {
  713. case EDMA_CONT_PARAMS_ANY:
  714. return reserve_contiguous_params(ctlr, id, count,
  715. edma_info[ctlr]->num_channels);
  716. case EDMA_CONT_PARAMS_FIXED_EXACT:
  717. case EDMA_CONT_PARAMS_FIXED_NOT_EXACT:
  718. return reserve_contiguous_params(ctlr, id, count, slot);
  719. default:
  720. return -EINVAL;
  721. }
  722. }
  723. EXPORT_SYMBOL(edma_alloc_cont_slots);
  724. /**
  725. * edma_free_cont_slots - deallocate DMA parameter RAMs
  726. * @slot: first parameter RAM of a set of parameter RAMs to be freed
  727. * @count: the number of contiguous parameter RAMs to be freed
  728. *
  729. * This deallocates the parameter RAM slots allocated by
  730. * edma_alloc_cont_slots.
  731. * Callers/applications need to keep track of sets of contiguous
  732. * parameter RAMs that have been allocated using the edma_alloc_cont_slots
  733. * API.
  734. * Callers are responsible for ensuring the slots are inactive, and will
  735. * not be activated.
  736. */
  737. int edma_free_cont_slots(unsigned slot, int count)
  738. {
  739. unsigned ctlr, slot_to_free;
  740. int i;
  741. ctlr = EDMA_CTLR(slot);
  742. slot = EDMA_CHAN_SLOT(slot);
  743. if (slot < edma_info[ctlr]->num_channels ||
  744. slot >= edma_info[ctlr]->num_slots ||
  745. count < 1)
  746. return -EINVAL;
  747. for (i = slot; i < slot + count; ++i) {
  748. ctlr = EDMA_CTLR(i);
  749. slot_to_free = EDMA_CHAN_SLOT(i);
  750. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot_to_free),
  751. &dummy_paramset, PARM_SIZE);
  752. clear_bit(slot_to_free, edma_info[ctlr]->edma_inuse);
  753. }
  754. return 0;
  755. }
  756. EXPORT_SYMBOL(edma_free_cont_slots);
  757. /*-----------------------------------------------------------------------*/
  758. /* Parameter RAM operations (i) -- read/write partial slots */
  759. /**
  760. * edma_set_src - set initial DMA source address in parameter RAM slot
  761. * @slot: parameter RAM slot being configured
  762. * @src_port: physical address of source (memory, controller FIFO, etc)
  763. * @addressMode: INCR, except in very rare cases
  764. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  765. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  766. *
  767. * Note that the source address is modified during the DMA transfer
  768. * according to edma_set_src_index().
  769. */
  770. void edma_set_src(unsigned slot, dma_addr_t src_port,
  771. enum address_mode mode, enum fifo_width width)
  772. {
  773. unsigned ctlr;
  774. ctlr = EDMA_CTLR(slot);
  775. slot = EDMA_CHAN_SLOT(slot);
  776. if (slot < edma_info[ctlr]->num_slots) {
  777. unsigned int i = edma_parm_read(ctlr, PARM_OPT, slot);
  778. if (mode) {
  779. /* set SAM and program FWID */
  780. i = (i & ~(EDMA_FWID)) | (SAM | ((width & 0x7) << 8));
  781. } else {
  782. /* clear SAM */
  783. i &= ~SAM;
  784. }
  785. edma_parm_write(ctlr, PARM_OPT, slot, i);
  786. /* set the source port address
  787. in source register of param structure */
  788. edma_parm_write(ctlr, PARM_SRC, slot, src_port);
  789. }
  790. }
  791. EXPORT_SYMBOL(edma_set_src);
  792. /**
  793. * edma_set_dest - set initial DMA destination address in parameter RAM slot
  794. * @slot: parameter RAM slot being configured
  795. * @dest_port: physical address of destination (memory, controller FIFO, etc)
  796. * @addressMode: INCR, except in very rare cases
  797. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  798. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  799. *
  800. * Note that the destination address is modified during the DMA transfer
  801. * according to edma_set_dest_index().
  802. */
  803. void edma_set_dest(unsigned slot, dma_addr_t dest_port,
  804. enum address_mode mode, enum fifo_width width)
  805. {
  806. unsigned ctlr;
  807. ctlr = EDMA_CTLR(slot);
  808. slot = EDMA_CHAN_SLOT(slot);
  809. if (slot < edma_info[ctlr]->num_slots) {
  810. unsigned int i = edma_parm_read(ctlr, PARM_OPT, slot);
  811. if (mode) {
  812. /* set DAM and program FWID */
  813. i = (i & ~(EDMA_FWID)) | (DAM | ((width & 0x7) << 8));
  814. } else {
  815. /* clear DAM */
  816. i &= ~DAM;
  817. }
  818. edma_parm_write(ctlr, PARM_OPT, slot, i);
  819. /* set the destination port address
  820. in dest register of param structure */
  821. edma_parm_write(ctlr, PARM_DST, slot, dest_port);
  822. }
  823. }
  824. EXPORT_SYMBOL(edma_set_dest);
  825. /**
  826. * edma_get_position - returns the current transfer points
  827. * @slot: parameter RAM slot being examined
  828. * @src: pointer to source port position
  829. * @dst: pointer to destination port position
  830. *
  831. * Returns current source and destination addresses for a particular
  832. * parameter RAM slot. Its channel should not be active when this is called.
  833. */
  834. void edma_get_position(unsigned slot, dma_addr_t *src, dma_addr_t *dst)
  835. {
  836. struct edmacc_param temp;
  837. unsigned ctlr;
  838. ctlr = EDMA_CTLR(slot);
  839. slot = EDMA_CHAN_SLOT(slot);
  840. edma_read_slot(EDMA_CTLR_CHAN(ctlr, slot), &temp);
  841. if (src != NULL)
  842. *src = temp.src;
  843. if (dst != NULL)
  844. *dst = temp.dst;
  845. }
  846. EXPORT_SYMBOL(edma_get_position);
  847. /**
  848. * edma_set_src_index - configure DMA source address indexing
  849. * @slot: parameter RAM slot being configured
  850. * @src_bidx: byte offset between source arrays in a frame
  851. * @src_cidx: byte offset between source frames in a block
  852. *
  853. * Offsets are specified to support either contiguous or discontiguous
  854. * memory transfers, or repeated access to a hardware register, as needed.
  855. * When accessing hardware registers, both offsets are normally zero.
  856. */
  857. void edma_set_src_index(unsigned slot, s16 src_bidx, s16 src_cidx)
  858. {
  859. unsigned ctlr;
  860. ctlr = EDMA_CTLR(slot);
  861. slot = EDMA_CHAN_SLOT(slot);
  862. if (slot < edma_info[ctlr]->num_slots) {
  863. edma_parm_modify(ctlr, PARM_SRC_DST_BIDX, slot,
  864. 0xffff0000, src_bidx);
  865. edma_parm_modify(ctlr, PARM_SRC_DST_CIDX, slot,
  866. 0xffff0000, src_cidx);
  867. }
  868. }
  869. EXPORT_SYMBOL(edma_set_src_index);
  870. /**
  871. * edma_set_dest_index - configure DMA destination address indexing
  872. * @slot: parameter RAM slot being configured
  873. * @dest_bidx: byte offset between destination arrays in a frame
  874. * @dest_cidx: byte offset between destination frames in a block
  875. *
  876. * Offsets are specified to support either contiguous or discontiguous
  877. * memory transfers, or repeated access to a hardware register, as needed.
  878. * When accessing hardware registers, both offsets are normally zero.
  879. */
  880. void edma_set_dest_index(unsigned slot, s16 dest_bidx, s16 dest_cidx)
  881. {
  882. unsigned ctlr;
  883. ctlr = EDMA_CTLR(slot);
  884. slot = EDMA_CHAN_SLOT(slot);
  885. if (slot < edma_info[ctlr]->num_slots) {
  886. edma_parm_modify(ctlr, PARM_SRC_DST_BIDX, slot,
  887. 0x0000ffff, dest_bidx << 16);
  888. edma_parm_modify(ctlr, PARM_SRC_DST_CIDX, slot,
  889. 0x0000ffff, dest_cidx << 16);
  890. }
  891. }
  892. EXPORT_SYMBOL(edma_set_dest_index);
  893. /**
  894. * edma_set_transfer_params - configure DMA transfer parameters
  895. * @slot: parameter RAM slot being configured
  896. * @acnt: how many bytes per array (at least one)
  897. * @bcnt: how many arrays per frame (at least one)
  898. * @ccnt: how many frames per block (at least one)
  899. * @bcnt_rld: used only for A-Synchronized transfers; this specifies
  900. * the value to reload into bcnt when it decrements to zero
  901. * @sync_mode: ASYNC or ABSYNC
  902. *
  903. * See the EDMA3 documentation to understand how to configure and link
  904. * transfers using the fields in PaRAM slots. If you are not doing it
  905. * all at once with edma_write_slot(), you will use this routine
  906. * plus two calls each for source and destination, setting the initial
  907. * address and saying how to index that address.
  908. *
  909. * An example of an A-Synchronized transfer is a serial link using a
  910. * single word shift register. In that case, @acnt would be equal to
  911. * that word size; the serial controller issues a DMA synchronization
  912. * event to transfer each word, and memory access by the DMA transfer
  913. * controller will be word-at-a-time.
  914. *
  915. * An example of an AB-Synchronized transfer is a device using a FIFO.
  916. * In that case, @acnt equals the FIFO width and @bcnt equals its depth.
  917. * The controller with the FIFO issues DMA synchronization events when
  918. * the FIFO threshold is reached, and the DMA transfer controller will
  919. * transfer one frame to (or from) the FIFO. It will probably use
  920. * efficient burst modes to access memory.
  921. */
  922. void edma_set_transfer_params(unsigned slot,
  923. u16 acnt, u16 bcnt, u16 ccnt,
  924. u16 bcnt_rld, enum sync_dimension sync_mode)
  925. {
  926. unsigned ctlr;
  927. ctlr = EDMA_CTLR(slot);
  928. slot = EDMA_CHAN_SLOT(slot);
  929. if (slot < edma_info[ctlr]->num_slots) {
  930. edma_parm_modify(ctlr, PARM_LINK_BCNTRLD, slot,
  931. 0x0000ffff, bcnt_rld << 16);
  932. if (sync_mode == ASYNC)
  933. edma_parm_and(ctlr, PARM_OPT, slot, ~SYNCDIM);
  934. else
  935. edma_parm_or(ctlr, PARM_OPT, slot, SYNCDIM);
  936. /* Set the acount, bcount, ccount registers */
  937. edma_parm_write(ctlr, PARM_A_B_CNT, slot, (bcnt << 16) | acnt);
  938. edma_parm_write(ctlr, PARM_CCNT, slot, ccnt);
  939. }
  940. }
  941. EXPORT_SYMBOL(edma_set_transfer_params);
  942. /**
  943. * edma_link - link one parameter RAM slot to another
  944. * @from: parameter RAM slot originating the link
  945. * @to: parameter RAM slot which is the link target
  946. *
  947. * The originating slot should not be part of any active DMA transfer.
  948. */
  949. void edma_link(unsigned from, unsigned to)
  950. {
  951. unsigned ctlr_from, ctlr_to;
  952. ctlr_from = EDMA_CTLR(from);
  953. from = EDMA_CHAN_SLOT(from);
  954. ctlr_to = EDMA_CTLR(to);
  955. to = EDMA_CHAN_SLOT(to);
  956. if (from >= edma_info[ctlr_from]->num_slots)
  957. return;
  958. if (to >= edma_info[ctlr_to]->num_slots)
  959. return;
  960. edma_parm_modify(ctlr_from, PARM_LINK_BCNTRLD, from, 0xffff0000,
  961. PARM_OFFSET(to));
  962. }
  963. EXPORT_SYMBOL(edma_link);
  964. /**
  965. * edma_unlink - cut link from one parameter RAM slot
  966. * @from: parameter RAM slot originating the link
  967. *
  968. * The originating slot should not be part of any active DMA transfer.
  969. * Its link is set to 0xffff.
  970. */
  971. void edma_unlink(unsigned from)
  972. {
  973. unsigned ctlr;
  974. ctlr = EDMA_CTLR(from);
  975. from = EDMA_CHAN_SLOT(from);
  976. if (from >= edma_info[ctlr]->num_slots)
  977. return;
  978. edma_parm_or(ctlr, PARM_LINK_BCNTRLD, from, 0xffff);
  979. }
  980. EXPORT_SYMBOL(edma_unlink);
  981. /*-----------------------------------------------------------------------*/
  982. /* Parameter RAM operations (ii) -- read/write whole parameter sets */
  983. /**
  984. * edma_write_slot - write parameter RAM data for slot
  985. * @slot: number of parameter RAM slot being modified
  986. * @param: data to be written into parameter RAM slot
  987. *
  988. * Use this to assign all parameters of a transfer at once. This
  989. * allows more efficient setup of transfers than issuing multiple
  990. * calls to set up those parameters in small pieces, and provides
  991. * complete control over all transfer options.
  992. */
  993. void edma_write_slot(unsigned slot, const struct edmacc_param *param)
  994. {
  995. unsigned ctlr;
  996. ctlr = EDMA_CTLR(slot);
  997. slot = EDMA_CHAN_SLOT(slot);
  998. if (slot >= edma_info[ctlr]->num_slots)
  999. return;
  1000. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot), param,
  1001. PARM_SIZE);
  1002. }
  1003. EXPORT_SYMBOL(edma_write_slot);
  1004. /**
  1005. * edma_read_slot - read parameter RAM data from slot
  1006. * @slot: number of parameter RAM slot being copied
  1007. * @param: where to store copy of parameter RAM data
  1008. *
  1009. * Use this to read data from a parameter RAM slot, perhaps to
  1010. * save them as a template for later reuse.
  1011. */
  1012. void edma_read_slot(unsigned slot, struct edmacc_param *param)
  1013. {
  1014. unsigned ctlr;
  1015. ctlr = EDMA_CTLR(slot);
  1016. slot = EDMA_CHAN_SLOT(slot);
  1017. if (slot >= edma_info[ctlr]->num_slots)
  1018. return;
  1019. memcpy_fromio(param, edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  1020. PARM_SIZE);
  1021. }
  1022. EXPORT_SYMBOL(edma_read_slot);
  1023. /*-----------------------------------------------------------------------*/
  1024. /* Various EDMA channel control operations */
  1025. /**
  1026. * edma_pause - pause dma on a channel
  1027. * @channel: on which edma_start() has been called
  1028. *
  1029. * This temporarily disables EDMA hardware events on the specified channel,
  1030. * preventing them from triggering new transfers on its behalf
  1031. */
  1032. void edma_pause(unsigned channel)
  1033. {
  1034. unsigned ctlr;
  1035. ctlr = EDMA_CTLR(channel);
  1036. channel = EDMA_CHAN_SLOT(channel);
  1037. if (channel < edma_info[ctlr]->num_channels) {
  1038. unsigned int mask = (1 << (channel & 0x1f));
  1039. edma_shadow0_write_array(ctlr, SH_EECR, channel >> 5, mask);
  1040. }
  1041. }
  1042. EXPORT_SYMBOL(edma_pause);
  1043. /**
  1044. * edma_resume - resumes dma on a paused channel
  1045. * @channel: on which edma_pause() has been called
  1046. *
  1047. * This re-enables EDMA hardware events on the specified channel.
  1048. */
  1049. void edma_resume(unsigned channel)
  1050. {
  1051. unsigned ctlr;
  1052. ctlr = EDMA_CTLR(channel);
  1053. channel = EDMA_CHAN_SLOT(channel);
  1054. if (channel < edma_info[ctlr]->num_channels) {
  1055. unsigned int mask = (1 << (channel & 0x1f));
  1056. edma_shadow0_write_array(ctlr, SH_EESR, channel >> 5, mask);
  1057. }
  1058. }
  1059. EXPORT_SYMBOL(edma_resume);
  1060. /**
  1061. * edma_start - start dma on a channel
  1062. * @channel: channel being activated
  1063. *
  1064. * Channels with event associations will be triggered by their hardware
  1065. * events, and channels without such associations will be triggered by
  1066. * software. (At this writing there is no interface for using software
  1067. * triggers except with channels that don't support hardware triggers.)
  1068. *
  1069. * Returns zero on success, else negative errno.
  1070. */
  1071. int edma_start(unsigned channel)
  1072. {
  1073. unsigned ctlr;
  1074. ctlr = EDMA_CTLR(channel);
  1075. channel = EDMA_CHAN_SLOT(channel);
  1076. if (channel < edma_info[ctlr]->num_channels) {
  1077. int j = channel >> 5;
  1078. unsigned int mask = (1 << (channel & 0x1f));
  1079. /* EDMA channels without event association */
  1080. if (test_bit(channel, edma_info[ctlr]->edma_noevent)) {
  1081. pr_debug("EDMA: ESR%d %08x\n", j,
  1082. edma_shadow0_read_array(ctlr, SH_ESR, j));
  1083. edma_shadow0_write_array(ctlr, SH_ESR, j, mask);
  1084. return 0;
  1085. }
  1086. /* EDMA channel with event association */
  1087. pr_debug("EDMA: ER%d %08x\n", j,
  1088. edma_shadow0_read_array(ctlr, SH_ER, j));
  1089. /* Clear any pending error */
  1090. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1091. /* Clear any SER */
  1092. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1093. edma_shadow0_write_array(ctlr, SH_EESR, j, mask);
  1094. pr_debug("EDMA: EER%d %08x\n", j,
  1095. edma_shadow0_read_array(ctlr, SH_EER, j));
  1096. return 0;
  1097. }
  1098. return -EINVAL;
  1099. }
  1100. EXPORT_SYMBOL(edma_start);
  1101. /**
  1102. * edma_stop - stops dma on the channel passed
  1103. * @channel: channel being deactivated
  1104. *
  1105. * When @lch is a channel, any active transfer is paused and
  1106. * all pending hardware events are cleared. The current transfer
  1107. * may not be resumed, and the channel's Parameter RAM should be
  1108. * reinitialized before being reused.
  1109. */
  1110. void edma_stop(unsigned channel)
  1111. {
  1112. unsigned ctlr;
  1113. ctlr = EDMA_CTLR(channel);
  1114. channel = EDMA_CHAN_SLOT(channel);
  1115. if (channel < edma_info[ctlr]->num_channels) {
  1116. int j = channel >> 5;
  1117. unsigned int mask = (1 << (channel & 0x1f));
  1118. edma_shadow0_write_array(ctlr, SH_EECR, j, mask);
  1119. edma_shadow0_write_array(ctlr, SH_ECR, j, mask);
  1120. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1121. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1122. pr_debug("EDMA: EER%d %08x\n", j,
  1123. edma_shadow0_read_array(ctlr, SH_EER, j));
  1124. /* REVISIT: consider guarding against inappropriate event
  1125. * chaining by overwriting with dummy_paramset.
  1126. */
  1127. }
  1128. }
  1129. EXPORT_SYMBOL(edma_stop);
  1130. /******************************************************************************
  1131. *
  1132. * It cleans ParamEntry qand bring back EDMA to initial state if media has
  1133. * been removed before EDMA has finished.It is usedful for removable media.
  1134. * Arguments:
  1135. * ch_no - channel no
  1136. *
  1137. * Return: zero on success, or corresponding error no on failure
  1138. *
  1139. * FIXME this should not be needed ... edma_stop() should suffice.
  1140. *
  1141. *****************************************************************************/
  1142. void edma_clean_channel(unsigned channel)
  1143. {
  1144. unsigned ctlr;
  1145. ctlr = EDMA_CTLR(channel);
  1146. channel = EDMA_CHAN_SLOT(channel);
  1147. if (channel < edma_info[ctlr]->num_channels) {
  1148. int j = (channel >> 5);
  1149. unsigned int mask = 1 << (channel & 0x1f);
  1150. pr_debug("EDMA: EMR%d %08x\n", j,
  1151. edma_read_array(ctlr, EDMA_EMR, j));
  1152. edma_shadow0_write_array(ctlr, SH_ECR, j, mask);
  1153. /* Clear the corresponding EMR bits */
  1154. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1155. /* Clear any SER */
  1156. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1157. edma_write(ctlr, EDMA_CCERRCLR, (1 << 16) | 0x3);
  1158. }
  1159. }
  1160. EXPORT_SYMBOL(edma_clean_channel);
  1161. /*
  1162. * edma_clear_event - clear an outstanding event on the DMA channel
  1163. * Arguments:
  1164. * channel - channel number
  1165. */
  1166. void edma_clear_event(unsigned channel)
  1167. {
  1168. unsigned ctlr;
  1169. ctlr = EDMA_CTLR(channel);
  1170. channel = EDMA_CHAN_SLOT(channel);
  1171. if (channel >= edma_info[ctlr]->num_channels)
  1172. return;
  1173. if (channel < 32)
  1174. edma_write(ctlr, EDMA_ECR, 1 << channel);
  1175. else
  1176. edma_write(ctlr, EDMA_ECRH, 1 << (channel - 32));
  1177. }
  1178. EXPORT_SYMBOL(edma_clear_event);
  1179. /*-----------------------------------------------------------------------*/
  1180. static int __init edma_probe(struct platform_device *pdev)
  1181. {
  1182. struct edma_soc_info *info = pdev->dev.platform_data;
  1183. const s8 (*queue_priority_mapping)[2];
  1184. const s8 (*queue_tc_mapping)[2];
  1185. int i, j, found = 0;
  1186. int status = -1;
  1187. const s8 *noevent;
  1188. int irq[EDMA_MAX_CC] = {0, 0};
  1189. int err_irq[EDMA_MAX_CC] = {0, 0};
  1190. struct resource *r[EDMA_MAX_CC] = {NULL};
  1191. resource_size_t len[EDMA_MAX_CC];
  1192. char res_name[10];
  1193. char irq_name[10];
  1194. if (!info)
  1195. return -ENODEV;
  1196. for (j = 0; j < EDMA_MAX_CC; j++) {
  1197. sprintf(res_name, "edma_cc%d", j);
  1198. r[j] = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1199. res_name);
  1200. if (!r[j]) {
  1201. if (found)
  1202. break;
  1203. else
  1204. return -ENODEV;
  1205. } else
  1206. found = 1;
  1207. len[j] = resource_size(r[j]);
  1208. r[j] = request_mem_region(r[j]->start, len[j],
  1209. dev_name(&pdev->dev));
  1210. if (!r[j]) {
  1211. status = -EBUSY;
  1212. goto fail1;
  1213. }
  1214. edmacc_regs_base[j] = ioremap(r[j]->start, len[j]);
  1215. if (!edmacc_regs_base[j]) {
  1216. status = -EBUSY;
  1217. goto fail1;
  1218. }
  1219. edma_info[j] = kmalloc(sizeof(struct edma), GFP_KERNEL);
  1220. if (!edma_info[j]) {
  1221. status = -ENOMEM;
  1222. goto fail1;
  1223. }
  1224. memset(edma_info[j], 0, sizeof(struct edma));
  1225. edma_info[j]->num_channels = min_t(unsigned, info[j].n_channel,
  1226. EDMA_MAX_DMACH);
  1227. edma_info[j]->num_slots = min_t(unsigned, info[j].n_slot,
  1228. EDMA_MAX_PARAMENTRY);
  1229. edma_info[j]->num_cc = min_t(unsigned, info[j].n_cc,
  1230. EDMA_MAX_CC);
  1231. edma_info[j]->default_queue = info[j].default_queue;
  1232. if (!edma_info[j]->default_queue)
  1233. edma_info[j]->default_queue = EVENTQ_1;
  1234. dev_dbg(&pdev->dev, "DMA REG BASE ADDR=%p\n",
  1235. edmacc_regs_base[j]);
  1236. for (i = 0; i < edma_info[j]->num_slots; i++)
  1237. memcpy_toio(edmacc_regs_base[j] + PARM_OFFSET(i),
  1238. &dummy_paramset, PARM_SIZE);
  1239. noevent = info[j].noevent;
  1240. if (noevent) {
  1241. while (*noevent != -1)
  1242. set_bit(*noevent++, edma_info[j]->edma_noevent);
  1243. }
  1244. sprintf(irq_name, "edma%d", j);
  1245. irq[j] = platform_get_irq_byname(pdev, irq_name);
  1246. edma_info[j]->irq_res_start = irq[j];
  1247. status = request_irq(irq[j], dma_irq_handler, 0, "edma",
  1248. &pdev->dev);
  1249. if (status < 0) {
  1250. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  1251. irq[j], status);
  1252. goto fail;
  1253. }
  1254. sprintf(irq_name, "edma%d_err", j);
  1255. err_irq[j] = platform_get_irq_byname(pdev, irq_name);
  1256. edma_info[j]->irq_res_end = err_irq[j];
  1257. status = request_irq(err_irq[j], dma_ccerr_handler, 0,
  1258. "edma_error", &pdev->dev);
  1259. if (status < 0) {
  1260. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  1261. err_irq[j], status);
  1262. goto fail;
  1263. }
  1264. /* Everything lives on transfer controller 1 until otherwise
  1265. * specified. This way, long transfers on the low priority queue
  1266. * started by the codec engine will not cause audio defects.
  1267. */
  1268. for (i = 0; i < edma_info[j]->num_channels; i++)
  1269. map_dmach_queue(j, i, EVENTQ_1);
  1270. queue_tc_mapping = info[j].queue_tc_mapping;
  1271. queue_priority_mapping = info[j].queue_priority_mapping;
  1272. /* Event queue to TC mapping */
  1273. for (i = 0; queue_tc_mapping[i][0] != -1; i++)
  1274. map_queue_tc(j, queue_tc_mapping[i][0],
  1275. queue_tc_mapping[i][1]);
  1276. /* Event queue priority mapping */
  1277. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  1278. assign_priority_to_queue(j,
  1279. queue_priority_mapping[i][0],
  1280. queue_priority_mapping[i][1]);
  1281. /* Map the channel to param entry if channel mapping logic
  1282. * exist
  1283. */
  1284. if (edma_read(j, EDMA_CCCFG) & CHMAP_EXIST)
  1285. map_dmach_param(j);
  1286. for (i = 0; i < info[j].n_region; i++) {
  1287. edma_write_array2(j, EDMA_DRAE, i, 0, 0x0);
  1288. edma_write_array2(j, EDMA_DRAE, i, 1, 0x0);
  1289. edma_write_array(j, EDMA_QRAE, i, 0x0);
  1290. }
  1291. }
  1292. if (tc_errs_handled) {
  1293. status = request_irq(IRQ_TCERRINT0, dma_tc0err_handler, 0,
  1294. "edma_tc0", &pdev->dev);
  1295. if (status < 0) {
  1296. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  1297. IRQ_TCERRINT0, status);
  1298. return status;
  1299. }
  1300. status = request_irq(IRQ_TCERRINT, dma_tc1err_handler, 0,
  1301. "edma_tc1", &pdev->dev);
  1302. if (status < 0) {
  1303. dev_dbg(&pdev->dev, "request_irq %d --> %d\n",
  1304. IRQ_TCERRINT, status);
  1305. return status;
  1306. }
  1307. }
  1308. return 0;
  1309. fail:
  1310. for (i = 0; i < EDMA_MAX_CC; i++) {
  1311. if (err_irq[i])
  1312. free_irq(err_irq[i], &pdev->dev);
  1313. if (irq[i])
  1314. free_irq(irq[i], &pdev->dev);
  1315. }
  1316. fail1:
  1317. for (i = 0; i < EDMA_MAX_CC; i++) {
  1318. if (r[i])
  1319. release_mem_region(r[i]->start, len[i]);
  1320. if (edmacc_regs_base[i])
  1321. iounmap(edmacc_regs_base[i]);
  1322. kfree(edma_info[i]);
  1323. }
  1324. return status;
  1325. }
  1326. static struct platform_driver edma_driver = {
  1327. .driver.name = "edma",
  1328. };
  1329. static int __init edma_init(void)
  1330. {
  1331. return platform_driver_probe(&edma_driver, edma_probe);
  1332. }
  1333. arch_initcall(edma_init);