dma.c 48 KB

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