dma.c 45 KB

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