dma.c 44 KB

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