dma.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  1. /*
  2. * linux/arch/arm/plat-omap/dma.c
  3. *
  4. * Copyright (C) 2003 - 2008 Nokia Corporation
  5. * Author: Juha Yrjölä <juha.yrjola@nokia.com>
  6. * DMA channel linking for 1610 by Samuel Ortiz <samuel.ortiz@nokia.com>
  7. * Graphics DMA and LCD DMA graphics tranformations
  8. * by Imre Deak <imre.deak@nokia.com>
  9. * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
  10. * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
  11. * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
  12. *
  13. * Copyright (C) 2009 Texas Instruments
  14. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  15. *
  16. * Support functions for the OMAP internal DMA channels.
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/sched.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/errno.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/irq.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <asm/system.h>
  34. #include <mach/hardware.h>
  35. #include <plat/dma.h>
  36. #include <plat/tc.h>
  37. #undef DEBUG
  38. #ifndef CONFIG_ARCH_OMAP1
  39. enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
  40. DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
  41. };
  42. enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
  43. #endif
  44. #define OMAP_DMA_ACTIVE 0x01
  45. #define OMAP2_DMA_CSR_CLEAR_MASK 0xffe
  46. #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
  47. static int enable_1510_mode;
  48. static struct omap_dma_global_context_registers {
  49. u32 dma_irqenable_l0;
  50. u32 dma_ocp_sysconfig;
  51. u32 dma_gcr;
  52. } omap_dma_global_context;
  53. struct omap_dma_lch {
  54. int next_lch;
  55. int dev_id;
  56. u16 saved_csr;
  57. u16 enabled_irqs;
  58. const char *dev_name;
  59. void (*callback)(int lch, u16 ch_status, void *data);
  60. void *data;
  61. #ifndef CONFIG_ARCH_OMAP1
  62. /* required for Dynamic chaining */
  63. int prev_linked_ch;
  64. int next_linked_ch;
  65. int state;
  66. int chain_id;
  67. int status;
  68. #endif
  69. long flags;
  70. };
  71. struct dma_link_info {
  72. int *linked_dmach_q;
  73. int no_of_lchs_linked;
  74. int q_count;
  75. int q_tail;
  76. int q_head;
  77. int chain_state;
  78. int chain_mode;
  79. };
  80. static struct dma_link_info *dma_linked_lch;
  81. #ifndef CONFIG_ARCH_OMAP1
  82. /* Chain handling macros */
  83. #define OMAP_DMA_CHAIN_QINIT(chain_id) \
  84. do { \
  85. dma_linked_lch[chain_id].q_head = \
  86. dma_linked_lch[chain_id].q_tail = \
  87. dma_linked_lch[chain_id].q_count = 0; \
  88. } while (0)
  89. #define OMAP_DMA_CHAIN_QFULL(chain_id) \
  90. (dma_linked_lch[chain_id].no_of_lchs_linked == \
  91. dma_linked_lch[chain_id].q_count)
  92. #define OMAP_DMA_CHAIN_QLAST(chain_id) \
  93. do { \
  94. ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
  95. dma_linked_lch[chain_id].q_count) \
  96. } while (0)
  97. #define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
  98. (0 == dma_linked_lch[chain_id].q_count)
  99. #define __OMAP_DMA_CHAIN_INCQ(end) \
  100. ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
  101. #define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
  102. do { \
  103. __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
  104. dma_linked_lch[chain_id].q_count--; \
  105. } while (0)
  106. #define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
  107. do { \
  108. __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
  109. dma_linked_lch[chain_id].q_count++; \
  110. } while (0)
  111. #endif
  112. static int dma_lch_count;
  113. static int dma_chan_count;
  114. static int omap_dma_reserve_channels;
  115. static spinlock_t dma_chan_lock;
  116. static struct omap_dma_lch *dma_chan;
  117. static void __iomem *omap_dma_base;
  118. static const u8 omap1_dma_irq[OMAP1_LOGICAL_DMA_CH_COUNT] = {
  119. INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
  120. INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
  121. INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
  122. INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
  123. INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
  124. };
  125. static inline void disable_lnk(int lch);
  126. static void omap_disable_channel_irq(int lch);
  127. static inline void omap_enable_channel_irq(int lch);
  128. #define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
  129. __func__);
  130. #define dma_read(reg) \
  131. ({ \
  132. u32 __val; \
  133. if (cpu_class_is_omap1()) \
  134. __val = __raw_readw(omap_dma_base + OMAP1_DMA_##reg); \
  135. else \
  136. __val = __raw_readl(omap_dma_base + OMAP_DMA4_##reg); \
  137. __val; \
  138. })
  139. #define dma_write(val, reg) \
  140. ({ \
  141. if (cpu_class_is_omap1()) \
  142. __raw_writew((u16)(val), omap_dma_base + OMAP1_DMA_##reg); \
  143. else \
  144. __raw_writel((val), omap_dma_base + OMAP_DMA4_##reg); \
  145. })
  146. #ifdef CONFIG_ARCH_OMAP15XX
  147. /* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
  148. int omap_dma_in_1510_mode(void)
  149. {
  150. return enable_1510_mode;
  151. }
  152. #else
  153. #define omap_dma_in_1510_mode() 0
  154. #endif
  155. #ifdef CONFIG_ARCH_OMAP1
  156. static inline int get_gdma_dev(int req)
  157. {
  158. u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
  159. int shift = ((req - 1) % 5) * 6;
  160. return ((omap_readl(reg) >> shift) & 0x3f) + 1;
  161. }
  162. static inline void set_gdma_dev(int req, int dev)
  163. {
  164. u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
  165. int shift = ((req - 1) % 5) * 6;
  166. u32 l;
  167. l = omap_readl(reg);
  168. l &= ~(0x3f << shift);
  169. l |= (dev - 1) << shift;
  170. omap_writel(l, reg);
  171. }
  172. #else
  173. #define set_gdma_dev(req, dev) do {} while (0)
  174. #endif
  175. /* Omap1 only */
  176. static void clear_lch_regs(int lch)
  177. {
  178. int i;
  179. void __iomem *lch_base = omap_dma_base + OMAP1_DMA_CH_BASE(lch);
  180. for (i = 0; i < 0x2c; i += 2)
  181. __raw_writew(0, lch_base + i);
  182. }
  183. void omap_set_dma_priority(int lch, int dst_port, int priority)
  184. {
  185. unsigned long reg;
  186. u32 l;
  187. if (cpu_class_is_omap1()) {
  188. switch (dst_port) {
  189. case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
  190. reg = OMAP_TC_OCPT1_PRIOR;
  191. break;
  192. case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
  193. reg = OMAP_TC_OCPT2_PRIOR;
  194. break;
  195. case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
  196. reg = OMAP_TC_EMIFF_PRIOR;
  197. break;
  198. case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
  199. reg = OMAP_TC_EMIFS_PRIOR;
  200. break;
  201. default:
  202. BUG();
  203. return;
  204. }
  205. l = omap_readl(reg);
  206. l &= ~(0xf << 8);
  207. l |= (priority & 0xf) << 8;
  208. omap_writel(l, reg);
  209. }
  210. if (cpu_class_is_omap2()) {
  211. u32 ccr;
  212. ccr = dma_read(CCR(lch));
  213. if (priority)
  214. ccr |= (1 << 6);
  215. else
  216. ccr &= ~(1 << 6);
  217. dma_write(ccr, CCR(lch));
  218. }
  219. }
  220. EXPORT_SYMBOL(omap_set_dma_priority);
  221. void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
  222. int frame_count, int sync_mode,
  223. int dma_trigger, int src_or_dst_synch)
  224. {
  225. u32 l;
  226. l = dma_read(CSDP(lch));
  227. l &= ~0x03;
  228. l |= data_type;
  229. dma_write(l, CSDP(lch));
  230. if (cpu_class_is_omap1()) {
  231. u16 ccr;
  232. ccr = dma_read(CCR(lch));
  233. ccr &= ~(1 << 5);
  234. if (sync_mode == OMAP_DMA_SYNC_FRAME)
  235. ccr |= 1 << 5;
  236. dma_write(ccr, CCR(lch));
  237. ccr = dma_read(CCR2(lch));
  238. ccr &= ~(1 << 2);
  239. if (sync_mode == OMAP_DMA_SYNC_BLOCK)
  240. ccr |= 1 << 2;
  241. dma_write(ccr, CCR2(lch));
  242. }
  243. if (cpu_class_is_omap2() && dma_trigger) {
  244. u32 val;
  245. val = dma_read(CCR(lch));
  246. /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
  247. val &= ~((1 << 23) | (3 << 19) | 0x1f);
  248. val |= (dma_trigger & ~0x1f) << 14;
  249. val |= dma_trigger & 0x1f;
  250. if (sync_mode & OMAP_DMA_SYNC_FRAME)
  251. val |= 1 << 5;
  252. else
  253. val &= ~(1 << 5);
  254. if (sync_mode & OMAP_DMA_SYNC_BLOCK)
  255. val |= 1 << 18;
  256. else
  257. val &= ~(1 << 18);
  258. if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
  259. val &= ~(1 << 24); /* dest synch */
  260. val |= (1 << 23); /* Prefetch */
  261. } else if (src_or_dst_synch) {
  262. val |= 1 << 24; /* source synch */
  263. } else {
  264. val &= ~(1 << 24); /* dest synch */
  265. }
  266. dma_write(val, CCR(lch));
  267. }
  268. dma_write(elem_count, CEN(lch));
  269. dma_write(frame_count, CFN(lch));
  270. }
  271. EXPORT_SYMBOL(omap_set_dma_transfer_params);
  272. void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
  273. {
  274. BUG_ON(omap_dma_in_1510_mode());
  275. if (cpu_class_is_omap1()) {
  276. u16 w;
  277. w = dma_read(CCR2(lch));
  278. w &= ~0x03;
  279. switch (mode) {
  280. case OMAP_DMA_CONSTANT_FILL:
  281. w |= 0x01;
  282. break;
  283. case OMAP_DMA_TRANSPARENT_COPY:
  284. w |= 0x02;
  285. break;
  286. case OMAP_DMA_COLOR_DIS:
  287. break;
  288. default:
  289. BUG();
  290. }
  291. dma_write(w, CCR2(lch));
  292. w = dma_read(LCH_CTRL(lch));
  293. w &= ~0x0f;
  294. /* Default is channel type 2D */
  295. if (mode) {
  296. dma_write((u16)color, COLOR_L(lch));
  297. dma_write((u16)(color >> 16), COLOR_U(lch));
  298. w |= 1; /* Channel type G */
  299. }
  300. dma_write(w, LCH_CTRL(lch));
  301. }
  302. if (cpu_class_is_omap2()) {
  303. u32 val;
  304. val = dma_read(CCR(lch));
  305. val &= ~((1 << 17) | (1 << 16));
  306. switch (mode) {
  307. case OMAP_DMA_CONSTANT_FILL:
  308. val |= 1 << 16;
  309. break;
  310. case OMAP_DMA_TRANSPARENT_COPY:
  311. val |= 1 << 17;
  312. break;
  313. case OMAP_DMA_COLOR_DIS:
  314. break;
  315. default:
  316. BUG();
  317. }
  318. dma_write(val, CCR(lch));
  319. color &= 0xffffff;
  320. dma_write(color, COLOR(lch));
  321. }
  322. }
  323. EXPORT_SYMBOL(omap_set_dma_color_mode);
  324. void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
  325. {
  326. if (cpu_class_is_omap2()) {
  327. u32 csdp;
  328. csdp = dma_read(CSDP(lch));
  329. csdp &= ~(0x3 << 16);
  330. csdp |= (mode << 16);
  331. dma_write(csdp, CSDP(lch));
  332. }
  333. }
  334. EXPORT_SYMBOL(omap_set_dma_write_mode);
  335. void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
  336. {
  337. if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
  338. u32 l;
  339. l = dma_read(LCH_CTRL(lch));
  340. l &= ~0x7;
  341. l |= mode;
  342. dma_write(l, LCH_CTRL(lch));
  343. }
  344. }
  345. EXPORT_SYMBOL(omap_set_dma_channel_mode);
  346. /* Note that src_port is only for omap1 */
  347. void omap_set_dma_src_params(int lch, int src_port, int src_amode,
  348. unsigned long src_start,
  349. int src_ei, int src_fi)
  350. {
  351. u32 l;
  352. if (cpu_class_is_omap1()) {
  353. u16 w;
  354. w = dma_read(CSDP(lch));
  355. w &= ~(0x1f << 2);
  356. w |= src_port << 2;
  357. dma_write(w, CSDP(lch));
  358. }
  359. l = dma_read(CCR(lch));
  360. l &= ~(0x03 << 12);
  361. l |= src_amode << 12;
  362. dma_write(l, CCR(lch));
  363. if (cpu_class_is_omap1()) {
  364. dma_write(src_start >> 16, CSSA_U(lch));
  365. dma_write((u16)src_start, CSSA_L(lch));
  366. }
  367. if (cpu_class_is_omap2())
  368. dma_write(src_start, CSSA(lch));
  369. dma_write(src_ei, CSEI(lch));
  370. dma_write(src_fi, CSFI(lch));
  371. }
  372. EXPORT_SYMBOL(omap_set_dma_src_params);
  373. void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
  374. {
  375. omap_set_dma_transfer_params(lch, params->data_type,
  376. params->elem_count, params->frame_count,
  377. params->sync_mode, params->trigger,
  378. params->src_or_dst_synch);
  379. omap_set_dma_src_params(lch, params->src_port,
  380. params->src_amode, params->src_start,
  381. params->src_ei, params->src_fi);
  382. omap_set_dma_dest_params(lch, params->dst_port,
  383. params->dst_amode, params->dst_start,
  384. params->dst_ei, params->dst_fi);
  385. if (params->read_prio || params->write_prio)
  386. omap_dma_set_prio_lch(lch, params->read_prio,
  387. params->write_prio);
  388. }
  389. EXPORT_SYMBOL(omap_set_dma_params);
  390. void omap_set_dma_src_index(int lch, int eidx, int fidx)
  391. {
  392. if (cpu_class_is_omap2())
  393. return;
  394. dma_write(eidx, CSEI(lch));
  395. dma_write(fidx, CSFI(lch));
  396. }
  397. EXPORT_SYMBOL(omap_set_dma_src_index);
  398. void omap_set_dma_src_data_pack(int lch, int enable)
  399. {
  400. u32 l;
  401. l = dma_read(CSDP(lch));
  402. l &= ~(1 << 6);
  403. if (enable)
  404. l |= (1 << 6);
  405. dma_write(l, CSDP(lch));
  406. }
  407. EXPORT_SYMBOL(omap_set_dma_src_data_pack);
  408. void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
  409. {
  410. unsigned int burst = 0;
  411. u32 l;
  412. l = dma_read(CSDP(lch));
  413. l &= ~(0x03 << 7);
  414. switch (burst_mode) {
  415. case OMAP_DMA_DATA_BURST_DIS:
  416. break;
  417. case OMAP_DMA_DATA_BURST_4:
  418. if (cpu_class_is_omap2())
  419. burst = 0x1;
  420. else
  421. burst = 0x2;
  422. break;
  423. case OMAP_DMA_DATA_BURST_8:
  424. if (cpu_class_is_omap2()) {
  425. burst = 0x2;
  426. break;
  427. }
  428. /*
  429. * not supported by current hardware on OMAP1
  430. * w |= (0x03 << 7);
  431. * fall through
  432. */
  433. case OMAP_DMA_DATA_BURST_16:
  434. if (cpu_class_is_omap2()) {
  435. burst = 0x3;
  436. break;
  437. }
  438. /*
  439. * OMAP1 don't support burst 16
  440. * fall through
  441. */
  442. default:
  443. BUG();
  444. }
  445. l |= (burst << 7);
  446. dma_write(l, CSDP(lch));
  447. }
  448. EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
  449. /* Note that dest_port is only for OMAP1 */
  450. void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
  451. unsigned long dest_start,
  452. int dst_ei, int dst_fi)
  453. {
  454. u32 l;
  455. if (cpu_class_is_omap1()) {
  456. l = dma_read(CSDP(lch));
  457. l &= ~(0x1f << 9);
  458. l |= dest_port << 9;
  459. dma_write(l, CSDP(lch));
  460. }
  461. l = dma_read(CCR(lch));
  462. l &= ~(0x03 << 14);
  463. l |= dest_amode << 14;
  464. dma_write(l, CCR(lch));
  465. if (cpu_class_is_omap1()) {
  466. dma_write(dest_start >> 16, CDSA_U(lch));
  467. dma_write(dest_start, CDSA_L(lch));
  468. }
  469. if (cpu_class_is_omap2())
  470. dma_write(dest_start, CDSA(lch));
  471. dma_write(dst_ei, CDEI(lch));
  472. dma_write(dst_fi, CDFI(lch));
  473. }
  474. EXPORT_SYMBOL(omap_set_dma_dest_params);
  475. void omap_set_dma_dest_index(int lch, int eidx, int fidx)
  476. {
  477. if (cpu_class_is_omap2())
  478. return;
  479. dma_write(eidx, CDEI(lch));
  480. dma_write(fidx, CDFI(lch));
  481. }
  482. EXPORT_SYMBOL(omap_set_dma_dest_index);
  483. void omap_set_dma_dest_data_pack(int lch, int enable)
  484. {
  485. u32 l;
  486. l = dma_read(CSDP(lch));
  487. l &= ~(1 << 13);
  488. if (enable)
  489. l |= 1 << 13;
  490. dma_write(l, CSDP(lch));
  491. }
  492. EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
  493. void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
  494. {
  495. unsigned int burst = 0;
  496. u32 l;
  497. l = dma_read(CSDP(lch));
  498. l &= ~(0x03 << 14);
  499. switch (burst_mode) {
  500. case OMAP_DMA_DATA_BURST_DIS:
  501. break;
  502. case OMAP_DMA_DATA_BURST_4:
  503. if (cpu_class_is_omap2())
  504. burst = 0x1;
  505. else
  506. burst = 0x2;
  507. break;
  508. case OMAP_DMA_DATA_BURST_8:
  509. if (cpu_class_is_omap2())
  510. burst = 0x2;
  511. else
  512. burst = 0x3;
  513. break;
  514. case OMAP_DMA_DATA_BURST_16:
  515. if (cpu_class_is_omap2()) {
  516. burst = 0x3;
  517. break;
  518. }
  519. /*
  520. * OMAP1 don't support burst 16
  521. * fall through
  522. */
  523. default:
  524. printk(KERN_ERR "Invalid DMA burst mode\n");
  525. BUG();
  526. return;
  527. }
  528. l |= (burst << 14);
  529. dma_write(l, CSDP(lch));
  530. }
  531. EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
  532. static inline void omap_enable_channel_irq(int lch)
  533. {
  534. u32 status;
  535. /* Clear CSR */
  536. if (cpu_class_is_omap1())
  537. status = dma_read(CSR(lch));
  538. else if (cpu_class_is_omap2())
  539. dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
  540. /* Enable some nice interrupts. */
  541. dma_write(dma_chan[lch].enabled_irqs, CICR(lch));
  542. }
  543. static void omap_disable_channel_irq(int lch)
  544. {
  545. if (cpu_class_is_omap2())
  546. dma_write(0, CICR(lch));
  547. }
  548. void omap_enable_dma_irq(int lch, u16 bits)
  549. {
  550. dma_chan[lch].enabled_irqs |= bits;
  551. }
  552. EXPORT_SYMBOL(omap_enable_dma_irq);
  553. void omap_disable_dma_irq(int lch, u16 bits)
  554. {
  555. dma_chan[lch].enabled_irqs &= ~bits;
  556. }
  557. EXPORT_SYMBOL(omap_disable_dma_irq);
  558. static inline void enable_lnk(int lch)
  559. {
  560. u32 l;
  561. l = dma_read(CLNK_CTRL(lch));
  562. if (cpu_class_is_omap1())
  563. l &= ~(1 << 14);
  564. /* Set the ENABLE_LNK bits */
  565. if (dma_chan[lch].next_lch != -1)
  566. l = dma_chan[lch].next_lch | (1 << 15);
  567. #ifndef CONFIG_ARCH_OMAP1
  568. if (cpu_class_is_omap2())
  569. if (dma_chan[lch].next_linked_ch != -1)
  570. l = dma_chan[lch].next_linked_ch | (1 << 15);
  571. #endif
  572. dma_write(l, CLNK_CTRL(lch));
  573. }
  574. static inline void disable_lnk(int lch)
  575. {
  576. u32 l;
  577. l = dma_read(CLNK_CTRL(lch));
  578. /* Disable interrupts */
  579. if (cpu_class_is_omap1()) {
  580. dma_write(0, CICR(lch));
  581. /* Set the STOP_LNK bit */
  582. l |= 1 << 14;
  583. }
  584. if (cpu_class_is_omap2()) {
  585. omap_disable_channel_irq(lch);
  586. /* Clear the ENABLE_LNK bit */
  587. l &= ~(1 << 15);
  588. }
  589. dma_write(l, CLNK_CTRL(lch));
  590. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  591. }
  592. static inline void omap2_enable_irq_lch(int lch)
  593. {
  594. u32 val;
  595. unsigned long flags;
  596. if (!cpu_class_is_omap2())
  597. return;
  598. spin_lock_irqsave(&dma_chan_lock, flags);
  599. val = dma_read(IRQENABLE_L0);
  600. val |= 1 << lch;
  601. dma_write(val, IRQENABLE_L0);
  602. spin_unlock_irqrestore(&dma_chan_lock, flags);
  603. }
  604. static inline void omap2_disable_irq_lch(int lch)
  605. {
  606. u32 val;
  607. unsigned long flags;
  608. if (!cpu_class_is_omap2())
  609. return;
  610. spin_lock_irqsave(&dma_chan_lock, flags);
  611. val = dma_read(IRQENABLE_L0);
  612. val &= ~(1 << lch);
  613. dma_write(val, IRQENABLE_L0);
  614. spin_unlock_irqrestore(&dma_chan_lock, flags);
  615. }
  616. int omap_request_dma(int dev_id, const char *dev_name,
  617. void (*callback)(int lch, u16 ch_status, void *data),
  618. void *data, int *dma_ch_out)
  619. {
  620. int ch, free_ch = -1;
  621. unsigned long flags;
  622. struct omap_dma_lch *chan;
  623. spin_lock_irqsave(&dma_chan_lock, flags);
  624. for (ch = 0; ch < dma_chan_count; ch++) {
  625. if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
  626. free_ch = ch;
  627. if (dev_id == 0)
  628. break;
  629. }
  630. }
  631. if (free_ch == -1) {
  632. spin_unlock_irqrestore(&dma_chan_lock, flags);
  633. return -EBUSY;
  634. }
  635. chan = dma_chan + free_ch;
  636. chan->dev_id = dev_id;
  637. if (cpu_class_is_omap1())
  638. clear_lch_regs(free_ch);
  639. if (cpu_class_is_omap2())
  640. omap_clear_dma(free_ch);
  641. spin_unlock_irqrestore(&dma_chan_lock, flags);
  642. chan->dev_name = dev_name;
  643. chan->callback = callback;
  644. chan->data = data;
  645. chan->flags = 0;
  646. #ifndef CONFIG_ARCH_OMAP1
  647. if (cpu_class_is_omap2()) {
  648. chan->chain_id = -1;
  649. chan->next_linked_ch = -1;
  650. }
  651. #endif
  652. chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
  653. if (cpu_class_is_omap1())
  654. chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
  655. else if (cpu_class_is_omap2())
  656. chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
  657. OMAP2_DMA_TRANS_ERR_IRQ;
  658. if (cpu_is_omap16xx()) {
  659. /* If the sync device is set, configure it dynamically. */
  660. if (dev_id != 0) {
  661. set_gdma_dev(free_ch + 1, dev_id);
  662. dev_id = free_ch + 1;
  663. }
  664. /*
  665. * Disable the 1510 compatibility mode and set the sync device
  666. * id.
  667. */
  668. dma_write(dev_id | (1 << 10), CCR(free_ch));
  669. } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
  670. dma_write(dev_id, CCR(free_ch));
  671. }
  672. if (cpu_class_is_omap2()) {
  673. omap2_enable_irq_lch(free_ch);
  674. omap_enable_channel_irq(free_ch);
  675. /* Clear the CSR register and IRQ status register */
  676. dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(free_ch));
  677. dma_write(1 << free_ch, IRQSTATUS_L0);
  678. }
  679. *dma_ch_out = free_ch;
  680. return 0;
  681. }
  682. EXPORT_SYMBOL(omap_request_dma);
  683. void omap_free_dma(int lch)
  684. {
  685. unsigned long flags;
  686. if (dma_chan[lch].dev_id == -1) {
  687. pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
  688. lch);
  689. return;
  690. }
  691. if (cpu_class_is_omap1()) {
  692. /* Disable all DMA interrupts for the channel. */
  693. dma_write(0, CICR(lch));
  694. /* Make sure the DMA transfer is stopped. */
  695. dma_write(0, CCR(lch));
  696. }
  697. if (cpu_class_is_omap2()) {
  698. omap2_disable_irq_lch(lch);
  699. /* Clear the CSR register and IRQ status register */
  700. dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
  701. dma_write(1 << lch, IRQSTATUS_L0);
  702. /* Disable all DMA interrupts for the channel. */
  703. dma_write(0, CICR(lch));
  704. /* Make sure the DMA transfer is stopped. */
  705. dma_write(0, CCR(lch));
  706. omap_clear_dma(lch);
  707. }
  708. spin_lock_irqsave(&dma_chan_lock, flags);
  709. dma_chan[lch].dev_id = -1;
  710. dma_chan[lch].next_lch = -1;
  711. dma_chan[lch].callback = NULL;
  712. spin_unlock_irqrestore(&dma_chan_lock, flags);
  713. }
  714. EXPORT_SYMBOL(omap_free_dma);
  715. /**
  716. * @brief omap_dma_set_global_params : Set global priority settings for dma
  717. *
  718. * @param arb_rate
  719. * @param max_fifo_depth
  720. * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
  721. * DMA_THREAD_RESERVE_ONET
  722. * DMA_THREAD_RESERVE_TWOT
  723. * DMA_THREAD_RESERVE_THREET
  724. */
  725. void
  726. omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
  727. {
  728. u32 reg;
  729. if (!cpu_class_is_omap2()) {
  730. printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
  731. return;
  732. }
  733. if (max_fifo_depth == 0)
  734. max_fifo_depth = 1;
  735. if (arb_rate == 0)
  736. arb_rate = 1;
  737. reg = 0xff & max_fifo_depth;
  738. reg |= (0x3 & tparams) << 12;
  739. reg |= (arb_rate & 0xff) << 16;
  740. dma_write(reg, GCR);
  741. }
  742. EXPORT_SYMBOL(omap_dma_set_global_params);
  743. /**
  744. * @brief omap_dma_set_prio_lch : Set channel wise priority settings
  745. *
  746. * @param lch
  747. * @param read_prio - Read priority
  748. * @param write_prio - Write priority
  749. * Both of the above can be set with one of the following values :
  750. * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
  751. */
  752. int
  753. omap_dma_set_prio_lch(int lch, unsigned char read_prio,
  754. unsigned char write_prio)
  755. {
  756. u32 l;
  757. if (unlikely((lch < 0 || lch >= dma_lch_count))) {
  758. printk(KERN_ERR "Invalid channel id\n");
  759. return -EINVAL;
  760. }
  761. l = dma_read(CCR(lch));
  762. l &= ~((1 << 6) | (1 << 26));
  763. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
  764. l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
  765. else
  766. l |= ((read_prio & 0x1) << 6);
  767. dma_write(l, CCR(lch));
  768. return 0;
  769. }
  770. EXPORT_SYMBOL(omap_dma_set_prio_lch);
  771. /*
  772. * Clears any DMA state so the DMA engine is ready to restart with new buffers
  773. * through omap_start_dma(). Any buffers in flight are discarded.
  774. */
  775. void omap_clear_dma(int lch)
  776. {
  777. unsigned long flags;
  778. local_irq_save(flags);
  779. if (cpu_class_is_omap1()) {
  780. u32 l;
  781. l = dma_read(CCR(lch));
  782. l &= ~OMAP_DMA_CCR_EN;
  783. dma_write(l, CCR(lch));
  784. /* Clear pending interrupts */
  785. l = dma_read(CSR(lch));
  786. }
  787. if (cpu_class_is_omap2()) {
  788. int i;
  789. void __iomem *lch_base = omap_dma_base + OMAP_DMA4_CH_BASE(lch);
  790. for (i = 0; i < 0x44; i += 4)
  791. __raw_writel(0, lch_base + i);
  792. }
  793. local_irq_restore(flags);
  794. }
  795. EXPORT_SYMBOL(omap_clear_dma);
  796. void omap_start_dma(int lch)
  797. {
  798. u32 l;
  799. /*
  800. * The CPC/CDAC register needs to be initialized to zero
  801. * before starting dma transfer.
  802. */
  803. if (cpu_is_omap15xx())
  804. dma_write(0, CPC(lch));
  805. else
  806. dma_write(0, CDAC(lch));
  807. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  808. int next_lch, cur_lch;
  809. char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
  810. dma_chan_link_map[lch] = 1;
  811. /* Set the link register of the first channel */
  812. enable_lnk(lch);
  813. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  814. cur_lch = dma_chan[lch].next_lch;
  815. do {
  816. next_lch = dma_chan[cur_lch].next_lch;
  817. /* The loop case: we've been here already */
  818. if (dma_chan_link_map[cur_lch])
  819. break;
  820. /* Mark the current channel */
  821. dma_chan_link_map[cur_lch] = 1;
  822. enable_lnk(cur_lch);
  823. omap_enable_channel_irq(cur_lch);
  824. cur_lch = next_lch;
  825. } while (next_lch != -1);
  826. } else if (cpu_is_omap242x() ||
  827. (cpu_is_omap243x() && omap_type() <= OMAP2430_REV_ES1_0)) {
  828. /* Errata: Need to write lch even if not using chaining */
  829. dma_write(lch, CLNK_CTRL(lch));
  830. }
  831. omap_enable_channel_irq(lch);
  832. l = dma_read(CCR(lch));
  833. /*
  834. * Errata: Inter Frame DMA buffering issue (All OMAP2420 and
  835. * OMAP2430ES1.0): DMA will wrongly buffer elements if packing and
  836. * bursting is enabled. This might result in data gets stalled in
  837. * FIFO at the end of the block.
  838. * Workaround: DMA channels must have BUFFERING_DISABLED bit set to
  839. * guarantee no data will stay in the DMA FIFO in case inter frame
  840. * buffering occurs.
  841. */
  842. if (cpu_is_omap2420() ||
  843. (cpu_is_omap2430() && (omap_type() == OMAP2430_REV_ES1_0)))
  844. l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
  845. l |= OMAP_DMA_CCR_EN;
  846. dma_write(l, CCR(lch));
  847. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  848. }
  849. EXPORT_SYMBOL(omap_start_dma);
  850. void omap_stop_dma(int lch)
  851. {
  852. u32 l;
  853. /* Disable all interrupts on the channel */
  854. if (cpu_class_is_omap1())
  855. dma_write(0, CICR(lch));
  856. l = dma_read(CCR(lch));
  857. /* OMAP3 Errata i541: sDMA FIFO draining does not finish */
  858. if (cpu_is_omap34xx() && (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
  859. int i = 0;
  860. u32 sys_cf;
  861. /* Configure No-Standby */
  862. l = dma_read(OCP_SYSCONFIG);
  863. sys_cf = l;
  864. l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
  865. l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
  866. dma_write(l , OCP_SYSCONFIG);
  867. l = dma_read(CCR(lch));
  868. l &= ~OMAP_DMA_CCR_EN;
  869. dma_write(l, CCR(lch));
  870. /* Wait for sDMA FIFO drain */
  871. l = dma_read(CCR(lch));
  872. while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
  873. OMAP_DMA_CCR_WR_ACTIVE))) {
  874. udelay(5);
  875. i++;
  876. l = dma_read(CCR(lch));
  877. }
  878. if (i >= 100)
  879. printk(KERN_ERR "DMA drain did not complete on "
  880. "lch %d\n", lch);
  881. /* Restore OCP_SYSCONFIG */
  882. dma_write(sys_cf, OCP_SYSCONFIG);
  883. } else {
  884. l &= ~OMAP_DMA_CCR_EN;
  885. dma_write(l, CCR(lch));
  886. }
  887. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  888. int next_lch, cur_lch = lch;
  889. char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
  890. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  891. do {
  892. /* The loop case: we've been here already */
  893. if (dma_chan_link_map[cur_lch])
  894. break;
  895. /* Mark the current channel */
  896. dma_chan_link_map[cur_lch] = 1;
  897. disable_lnk(cur_lch);
  898. next_lch = dma_chan[cur_lch].next_lch;
  899. cur_lch = next_lch;
  900. } while (next_lch != -1);
  901. }
  902. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  903. }
  904. EXPORT_SYMBOL(omap_stop_dma);
  905. /*
  906. * Allows changing the DMA callback function or data. This may be needed if
  907. * the driver shares a single DMA channel for multiple dma triggers.
  908. */
  909. int omap_set_dma_callback(int lch,
  910. void (*callback)(int lch, u16 ch_status, void *data),
  911. void *data)
  912. {
  913. unsigned long flags;
  914. if (lch < 0)
  915. return -ENODEV;
  916. spin_lock_irqsave(&dma_chan_lock, flags);
  917. if (dma_chan[lch].dev_id == -1) {
  918. printk(KERN_ERR "DMA callback for not set for free channel\n");
  919. spin_unlock_irqrestore(&dma_chan_lock, flags);
  920. return -EINVAL;
  921. }
  922. dma_chan[lch].callback = callback;
  923. dma_chan[lch].data = data;
  924. spin_unlock_irqrestore(&dma_chan_lock, flags);
  925. return 0;
  926. }
  927. EXPORT_SYMBOL(omap_set_dma_callback);
  928. /*
  929. * Returns current physical source address for the given DMA channel.
  930. * If the channel is running the caller must disable interrupts prior calling
  931. * this function and process the returned value before re-enabling interrupt to
  932. * prevent races with the interrupt handler. Note that in continuous mode there
  933. * is a chance for CSSA_L register overflow inbetween the two reads resulting
  934. * in incorrect return value.
  935. */
  936. dma_addr_t omap_get_dma_src_pos(int lch)
  937. {
  938. dma_addr_t offset = 0;
  939. if (cpu_is_omap15xx())
  940. offset = dma_read(CPC(lch));
  941. else
  942. offset = dma_read(CSAC(lch));
  943. /*
  944. * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  945. * read before the DMA controller finished disabling the channel.
  946. */
  947. if (!cpu_is_omap15xx() && offset == 0)
  948. offset = dma_read(CSAC(lch));
  949. if (cpu_class_is_omap1())
  950. offset |= (dma_read(CSSA_U(lch)) << 16);
  951. return offset;
  952. }
  953. EXPORT_SYMBOL(omap_get_dma_src_pos);
  954. /*
  955. * Returns current physical destination address for the given DMA channel.
  956. * If the channel is running the caller must disable interrupts prior calling
  957. * this function and process the returned value before re-enabling interrupt to
  958. * prevent races with the interrupt handler. Note that in continuous mode there
  959. * is a chance for CDSA_L register overflow inbetween the two reads resulting
  960. * in incorrect return value.
  961. */
  962. dma_addr_t omap_get_dma_dst_pos(int lch)
  963. {
  964. dma_addr_t offset = 0;
  965. if (cpu_is_omap15xx())
  966. offset = dma_read(CPC(lch));
  967. else
  968. offset = dma_read(CDAC(lch));
  969. /*
  970. * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  971. * read before the DMA controller finished disabling the channel.
  972. */
  973. if (!cpu_is_omap15xx() && offset == 0)
  974. offset = dma_read(CDAC(lch));
  975. if (cpu_class_is_omap1())
  976. offset |= (dma_read(CDSA_U(lch)) << 16);
  977. return offset;
  978. }
  979. EXPORT_SYMBOL(omap_get_dma_dst_pos);
  980. int omap_get_dma_active_status(int lch)
  981. {
  982. return (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN) != 0;
  983. }
  984. EXPORT_SYMBOL(omap_get_dma_active_status);
  985. int omap_dma_running(void)
  986. {
  987. int lch;
  988. if (cpu_class_is_omap1())
  989. if (omap_lcd_dma_running())
  990. return 1;
  991. for (lch = 0; lch < dma_chan_count; lch++)
  992. if (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN)
  993. return 1;
  994. return 0;
  995. }
  996. /*
  997. * lch_queue DMA will start right after lch_head one is finished.
  998. * For this DMA link to start, you still need to start (see omap_start_dma)
  999. * the first one. That will fire up the entire queue.
  1000. */
  1001. void omap_dma_link_lch(int lch_head, int lch_queue)
  1002. {
  1003. if (omap_dma_in_1510_mode()) {
  1004. if (lch_head == lch_queue) {
  1005. dma_write(dma_read(CCR(lch_head)) | (3 << 8),
  1006. CCR(lch_head));
  1007. return;
  1008. }
  1009. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  1010. BUG();
  1011. return;
  1012. }
  1013. if ((dma_chan[lch_head].dev_id == -1) ||
  1014. (dma_chan[lch_queue].dev_id == -1)) {
  1015. printk(KERN_ERR "omap_dma: trying to link "
  1016. "non requested channels\n");
  1017. dump_stack();
  1018. }
  1019. dma_chan[lch_head].next_lch = lch_queue;
  1020. }
  1021. EXPORT_SYMBOL(omap_dma_link_lch);
  1022. /*
  1023. * Once the DMA queue is stopped, we can destroy it.
  1024. */
  1025. void omap_dma_unlink_lch(int lch_head, int lch_queue)
  1026. {
  1027. if (omap_dma_in_1510_mode()) {
  1028. if (lch_head == lch_queue) {
  1029. dma_write(dma_read(CCR(lch_head)) & ~(3 << 8),
  1030. CCR(lch_head));
  1031. return;
  1032. }
  1033. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  1034. BUG();
  1035. return;
  1036. }
  1037. if (dma_chan[lch_head].next_lch != lch_queue ||
  1038. dma_chan[lch_head].next_lch == -1) {
  1039. printk(KERN_ERR "omap_dma: trying to unlink "
  1040. "non linked channels\n");
  1041. dump_stack();
  1042. }
  1043. if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
  1044. (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
  1045. printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
  1046. "before unlinking\n");
  1047. dump_stack();
  1048. }
  1049. dma_chan[lch_head].next_lch = -1;
  1050. }
  1051. EXPORT_SYMBOL(omap_dma_unlink_lch);
  1052. /*----------------------------------------------------------------------------*/
  1053. #ifndef CONFIG_ARCH_OMAP1
  1054. /* Create chain of DMA channesls */
  1055. static void create_dma_lch_chain(int lch_head, int lch_queue)
  1056. {
  1057. u32 l;
  1058. /* Check if this is the first link in chain */
  1059. if (dma_chan[lch_head].next_linked_ch == -1) {
  1060. dma_chan[lch_head].next_linked_ch = lch_queue;
  1061. dma_chan[lch_head].prev_linked_ch = lch_queue;
  1062. dma_chan[lch_queue].next_linked_ch = lch_head;
  1063. dma_chan[lch_queue].prev_linked_ch = lch_head;
  1064. }
  1065. /* a link exists, link the new channel in circular chain */
  1066. else {
  1067. dma_chan[lch_queue].next_linked_ch =
  1068. dma_chan[lch_head].next_linked_ch;
  1069. dma_chan[lch_queue].prev_linked_ch = lch_head;
  1070. dma_chan[lch_head].next_linked_ch = lch_queue;
  1071. dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
  1072. lch_queue;
  1073. }
  1074. l = dma_read(CLNK_CTRL(lch_head));
  1075. l &= ~(0x1f);
  1076. l |= lch_queue;
  1077. dma_write(l, CLNK_CTRL(lch_head));
  1078. l = dma_read(CLNK_CTRL(lch_queue));
  1079. l &= ~(0x1f);
  1080. l |= (dma_chan[lch_queue].next_linked_ch);
  1081. dma_write(l, CLNK_CTRL(lch_queue));
  1082. }
  1083. /**
  1084. * @brief omap_request_dma_chain : Request a chain of DMA channels
  1085. *
  1086. * @param dev_id - Device id using the dma channel
  1087. * @param dev_name - Device name
  1088. * @param callback - Call back function
  1089. * @chain_id -
  1090. * @no_of_chans - Number of channels requested
  1091. * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
  1092. * OMAP_DMA_DYNAMIC_CHAIN
  1093. * @params - Channel parameters
  1094. *
  1095. * @return - Success : 0
  1096. * Failure: -EINVAL/-ENOMEM
  1097. */
  1098. int omap_request_dma_chain(int dev_id, const char *dev_name,
  1099. void (*callback) (int lch, u16 ch_status,
  1100. void *data),
  1101. int *chain_id, int no_of_chans, int chain_mode,
  1102. struct omap_dma_channel_params params)
  1103. {
  1104. int *channels;
  1105. int i, err;
  1106. /* Is the chain mode valid ? */
  1107. if (chain_mode != OMAP_DMA_STATIC_CHAIN
  1108. && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
  1109. printk(KERN_ERR "Invalid chain mode requested\n");
  1110. return -EINVAL;
  1111. }
  1112. if (unlikely((no_of_chans < 1
  1113. || no_of_chans > dma_lch_count))) {
  1114. printk(KERN_ERR "Invalid Number of channels requested\n");
  1115. return -EINVAL;
  1116. }
  1117. /*
  1118. * Allocate a queue to maintain the status of the channels
  1119. * in the chain
  1120. */
  1121. channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
  1122. if (channels == NULL) {
  1123. printk(KERN_ERR "omap_dma: No memory for channel queue\n");
  1124. return -ENOMEM;
  1125. }
  1126. /* request and reserve DMA channels for the chain */
  1127. for (i = 0; i < no_of_chans; i++) {
  1128. err = omap_request_dma(dev_id, dev_name,
  1129. callback, NULL, &channels[i]);
  1130. if (err < 0) {
  1131. int j;
  1132. for (j = 0; j < i; j++)
  1133. omap_free_dma(channels[j]);
  1134. kfree(channels);
  1135. printk(KERN_ERR "omap_dma: Request failed %d\n", err);
  1136. return err;
  1137. }
  1138. dma_chan[channels[i]].prev_linked_ch = -1;
  1139. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1140. /*
  1141. * Allowing client drivers to set common parameters now,
  1142. * so that later only relevant (src_start, dest_start
  1143. * and element count) can be set
  1144. */
  1145. omap_set_dma_params(channels[i], &params);
  1146. }
  1147. *chain_id = channels[0];
  1148. dma_linked_lch[*chain_id].linked_dmach_q = channels;
  1149. dma_linked_lch[*chain_id].chain_mode = chain_mode;
  1150. dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
  1151. dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
  1152. for (i = 0; i < no_of_chans; i++)
  1153. dma_chan[channels[i]].chain_id = *chain_id;
  1154. /* Reset the Queue pointers */
  1155. OMAP_DMA_CHAIN_QINIT(*chain_id);
  1156. /* Set up the chain */
  1157. if (no_of_chans == 1)
  1158. create_dma_lch_chain(channels[0], channels[0]);
  1159. else {
  1160. for (i = 0; i < (no_of_chans - 1); i++)
  1161. create_dma_lch_chain(channels[i], channels[i + 1]);
  1162. }
  1163. return 0;
  1164. }
  1165. EXPORT_SYMBOL(omap_request_dma_chain);
  1166. /**
  1167. * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
  1168. * params after setting it. Dont do this while dma is running!!
  1169. *
  1170. * @param chain_id - Chained logical channel id.
  1171. * @param params
  1172. *
  1173. * @return - Success : 0
  1174. * Failure : -EINVAL
  1175. */
  1176. int omap_modify_dma_chain_params(int chain_id,
  1177. struct omap_dma_channel_params params)
  1178. {
  1179. int *channels;
  1180. u32 i;
  1181. /* Check for input params */
  1182. if (unlikely((chain_id < 0
  1183. || chain_id >= dma_lch_count))) {
  1184. printk(KERN_ERR "Invalid chain id\n");
  1185. return -EINVAL;
  1186. }
  1187. /* Check if the chain exists */
  1188. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1189. printk(KERN_ERR "Chain doesn't exists\n");
  1190. return -EINVAL;
  1191. }
  1192. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1193. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1194. /*
  1195. * Allowing client drivers to set common parameters now,
  1196. * so that later only relevant (src_start, dest_start
  1197. * and element count) can be set
  1198. */
  1199. omap_set_dma_params(channels[i], &params);
  1200. }
  1201. return 0;
  1202. }
  1203. EXPORT_SYMBOL(omap_modify_dma_chain_params);
  1204. /**
  1205. * @brief omap_free_dma_chain - Free all the logical channels in a chain.
  1206. *
  1207. * @param chain_id
  1208. *
  1209. * @return - Success : 0
  1210. * Failure : -EINVAL
  1211. */
  1212. int omap_free_dma_chain(int chain_id)
  1213. {
  1214. int *channels;
  1215. u32 i;
  1216. /* Check for input params */
  1217. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1218. printk(KERN_ERR "Invalid chain id\n");
  1219. return -EINVAL;
  1220. }
  1221. /* Check if the chain exists */
  1222. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1223. printk(KERN_ERR "Chain doesn't exists\n");
  1224. return -EINVAL;
  1225. }
  1226. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1227. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1228. dma_chan[channels[i]].next_linked_ch = -1;
  1229. dma_chan[channels[i]].prev_linked_ch = -1;
  1230. dma_chan[channels[i]].chain_id = -1;
  1231. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1232. omap_free_dma(channels[i]);
  1233. }
  1234. kfree(channels);
  1235. dma_linked_lch[chain_id].linked_dmach_q = NULL;
  1236. dma_linked_lch[chain_id].chain_mode = -1;
  1237. dma_linked_lch[chain_id].chain_state = -1;
  1238. return (0);
  1239. }
  1240. EXPORT_SYMBOL(omap_free_dma_chain);
  1241. /**
  1242. * @brief omap_dma_chain_status - Check if the chain is in
  1243. * active / inactive state.
  1244. * @param chain_id
  1245. *
  1246. * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
  1247. * Failure : -EINVAL
  1248. */
  1249. int omap_dma_chain_status(int chain_id)
  1250. {
  1251. /* Check for input params */
  1252. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1253. printk(KERN_ERR "Invalid chain id\n");
  1254. return -EINVAL;
  1255. }
  1256. /* Check if the chain exists */
  1257. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1258. printk(KERN_ERR "Chain doesn't exists\n");
  1259. return -EINVAL;
  1260. }
  1261. pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
  1262. dma_linked_lch[chain_id].q_count);
  1263. if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
  1264. return OMAP_DMA_CHAIN_INACTIVE;
  1265. return OMAP_DMA_CHAIN_ACTIVE;
  1266. }
  1267. EXPORT_SYMBOL(omap_dma_chain_status);
  1268. /**
  1269. * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
  1270. * set the params and start the transfer.
  1271. *
  1272. * @param chain_id
  1273. * @param src_start - buffer start address
  1274. * @param dest_start - Dest address
  1275. * @param elem_count
  1276. * @param frame_count
  1277. * @param callbk_data - channel callback parameter data.
  1278. *
  1279. * @return - Success : 0
  1280. * Failure: -EINVAL/-EBUSY
  1281. */
  1282. int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
  1283. int elem_count, int frame_count, void *callbk_data)
  1284. {
  1285. int *channels;
  1286. u32 l, lch;
  1287. int start_dma = 0;
  1288. /*
  1289. * if buffer size is less than 1 then there is
  1290. * no use of starting the chain
  1291. */
  1292. if (elem_count < 1) {
  1293. printk(KERN_ERR "Invalid buffer size\n");
  1294. return -EINVAL;
  1295. }
  1296. /* Check for input params */
  1297. if (unlikely((chain_id < 0
  1298. || chain_id >= dma_lch_count))) {
  1299. printk(KERN_ERR "Invalid chain id\n");
  1300. return -EINVAL;
  1301. }
  1302. /* Check if the chain exists */
  1303. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1304. printk(KERN_ERR "Chain doesn't exist\n");
  1305. return -EINVAL;
  1306. }
  1307. /* Check if all the channels in chain are in use */
  1308. if (OMAP_DMA_CHAIN_QFULL(chain_id))
  1309. return -EBUSY;
  1310. /* Frame count may be negative in case of indexed transfers */
  1311. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1312. /* Get a free channel */
  1313. lch = channels[dma_linked_lch[chain_id].q_tail];
  1314. /* Store the callback data */
  1315. dma_chan[lch].data = callbk_data;
  1316. /* Increment the q_tail */
  1317. OMAP_DMA_CHAIN_INCQTAIL(chain_id);
  1318. /* Set the params to the free channel */
  1319. if (src_start != 0)
  1320. dma_write(src_start, CSSA(lch));
  1321. if (dest_start != 0)
  1322. dma_write(dest_start, CDSA(lch));
  1323. /* Write the buffer size */
  1324. dma_write(elem_count, CEN(lch));
  1325. dma_write(frame_count, CFN(lch));
  1326. /*
  1327. * If the chain is dynamically linked,
  1328. * then we may have to start the chain if its not active
  1329. */
  1330. if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
  1331. /*
  1332. * In Dynamic chain, if the chain is not started,
  1333. * queue the channel
  1334. */
  1335. if (dma_linked_lch[chain_id].chain_state ==
  1336. DMA_CHAIN_NOTSTARTED) {
  1337. /* Enable the link in previous channel */
  1338. if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
  1339. DMA_CH_QUEUED)
  1340. enable_lnk(dma_chan[lch].prev_linked_ch);
  1341. dma_chan[lch].state = DMA_CH_QUEUED;
  1342. }
  1343. /*
  1344. * Chain is already started, make sure its active,
  1345. * if not then start the chain
  1346. */
  1347. else {
  1348. start_dma = 1;
  1349. if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
  1350. DMA_CH_STARTED) {
  1351. enable_lnk(dma_chan[lch].prev_linked_ch);
  1352. dma_chan[lch].state = DMA_CH_QUEUED;
  1353. start_dma = 0;
  1354. if (0 == ((1 << 7) & dma_read(
  1355. CCR(dma_chan[lch].prev_linked_ch)))) {
  1356. disable_lnk(dma_chan[lch].
  1357. prev_linked_ch);
  1358. pr_debug("\n prev ch is stopped\n");
  1359. start_dma = 1;
  1360. }
  1361. }
  1362. else if (dma_chan[dma_chan[lch].prev_linked_ch].state
  1363. == DMA_CH_QUEUED) {
  1364. enable_lnk(dma_chan[lch].prev_linked_ch);
  1365. dma_chan[lch].state = DMA_CH_QUEUED;
  1366. start_dma = 0;
  1367. }
  1368. omap_enable_channel_irq(lch);
  1369. l = dma_read(CCR(lch));
  1370. if ((0 == (l & (1 << 24))))
  1371. l &= ~(1 << 25);
  1372. else
  1373. l |= (1 << 25);
  1374. if (start_dma == 1) {
  1375. if (0 == (l & (1 << 7))) {
  1376. l |= (1 << 7);
  1377. dma_chan[lch].state = DMA_CH_STARTED;
  1378. pr_debug("starting %d\n", lch);
  1379. dma_write(l, CCR(lch));
  1380. } else
  1381. start_dma = 0;
  1382. } else {
  1383. if (0 == (l & (1 << 7)))
  1384. dma_write(l, CCR(lch));
  1385. }
  1386. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  1387. }
  1388. }
  1389. return 0;
  1390. }
  1391. EXPORT_SYMBOL(omap_dma_chain_a_transfer);
  1392. /**
  1393. * @brief omap_start_dma_chain_transfers - Start the chain
  1394. *
  1395. * @param chain_id
  1396. *
  1397. * @return - Success : 0
  1398. * Failure : -EINVAL/-EBUSY
  1399. */
  1400. int omap_start_dma_chain_transfers(int chain_id)
  1401. {
  1402. int *channels;
  1403. u32 l, i;
  1404. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1405. printk(KERN_ERR "Invalid chain id\n");
  1406. return -EINVAL;
  1407. }
  1408. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1409. if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
  1410. printk(KERN_ERR "Chain is already started\n");
  1411. return -EBUSY;
  1412. }
  1413. if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
  1414. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
  1415. i++) {
  1416. enable_lnk(channels[i]);
  1417. omap_enable_channel_irq(channels[i]);
  1418. }
  1419. } else {
  1420. omap_enable_channel_irq(channels[0]);
  1421. }
  1422. l = dma_read(CCR(channels[0]));
  1423. l |= (1 << 7);
  1424. dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
  1425. dma_chan[channels[0]].state = DMA_CH_STARTED;
  1426. if ((0 == (l & (1 << 24))))
  1427. l &= ~(1 << 25);
  1428. else
  1429. l |= (1 << 25);
  1430. dma_write(l, CCR(channels[0]));
  1431. dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
  1432. return 0;
  1433. }
  1434. EXPORT_SYMBOL(omap_start_dma_chain_transfers);
  1435. /**
  1436. * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
  1437. *
  1438. * @param chain_id
  1439. *
  1440. * @return - Success : 0
  1441. * Failure : EINVAL
  1442. */
  1443. int omap_stop_dma_chain_transfers(int chain_id)
  1444. {
  1445. int *channels;
  1446. u32 l, i;
  1447. u32 sys_cf;
  1448. /* Check for input params */
  1449. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1450. printk(KERN_ERR "Invalid chain id\n");
  1451. return -EINVAL;
  1452. }
  1453. /* Check if the chain exists */
  1454. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1455. printk(KERN_ERR "Chain doesn't exists\n");
  1456. return -EINVAL;
  1457. }
  1458. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1459. /*
  1460. * DMA Errata:
  1461. * Special programming model needed to disable DMA before end of block
  1462. */
  1463. sys_cf = dma_read(OCP_SYSCONFIG);
  1464. l = sys_cf;
  1465. /* Middle mode reg set no Standby */
  1466. l &= ~((1 << 12)|(1 << 13));
  1467. dma_write(l, OCP_SYSCONFIG);
  1468. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1469. /* Stop the Channel transmission */
  1470. l = dma_read(CCR(channels[i]));
  1471. l &= ~(1 << 7);
  1472. dma_write(l, CCR(channels[i]));
  1473. /* Disable the link in all the channels */
  1474. disable_lnk(channels[i]);
  1475. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1476. }
  1477. dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
  1478. /* Reset the Queue pointers */
  1479. OMAP_DMA_CHAIN_QINIT(chain_id);
  1480. /* Errata - put in the old value */
  1481. dma_write(sys_cf, OCP_SYSCONFIG);
  1482. return 0;
  1483. }
  1484. EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
  1485. /* Get the index of the ongoing DMA in chain */
  1486. /**
  1487. * @brief omap_get_dma_chain_index - Get the element and frame index
  1488. * of the ongoing DMA in chain
  1489. *
  1490. * @param chain_id
  1491. * @param ei - Element index
  1492. * @param fi - Frame index
  1493. *
  1494. * @return - Success : 0
  1495. * Failure : -EINVAL
  1496. */
  1497. int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
  1498. {
  1499. int lch;
  1500. int *channels;
  1501. /* Check for input params */
  1502. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1503. printk(KERN_ERR "Invalid chain id\n");
  1504. return -EINVAL;
  1505. }
  1506. /* Check if the chain exists */
  1507. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1508. printk(KERN_ERR "Chain doesn't exists\n");
  1509. return -EINVAL;
  1510. }
  1511. if ((!ei) || (!fi))
  1512. return -EINVAL;
  1513. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1514. /* Get the current channel */
  1515. lch = channels[dma_linked_lch[chain_id].q_head];
  1516. *ei = dma_read(CCEN(lch));
  1517. *fi = dma_read(CCFN(lch));
  1518. return 0;
  1519. }
  1520. EXPORT_SYMBOL(omap_get_dma_chain_index);
  1521. /**
  1522. * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
  1523. * ongoing DMA in chain
  1524. *
  1525. * @param chain_id
  1526. *
  1527. * @return - Success : Destination position
  1528. * Failure : -EINVAL
  1529. */
  1530. int omap_get_dma_chain_dst_pos(int chain_id)
  1531. {
  1532. int lch;
  1533. int *channels;
  1534. /* Check for input params */
  1535. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1536. printk(KERN_ERR "Invalid chain id\n");
  1537. return -EINVAL;
  1538. }
  1539. /* Check if the chain exists */
  1540. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1541. printk(KERN_ERR "Chain doesn't exists\n");
  1542. return -EINVAL;
  1543. }
  1544. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1545. /* Get the current channel */
  1546. lch = channels[dma_linked_lch[chain_id].q_head];
  1547. return dma_read(CDAC(lch));
  1548. }
  1549. EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
  1550. /**
  1551. * @brief omap_get_dma_chain_src_pos - Get the source position
  1552. * of the ongoing DMA in chain
  1553. * @param chain_id
  1554. *
  1555. * @return - Success : Destination position
  1556. * Failure : -EINVAL
  1557. */
  1558. int omap_get_dma_chain_src_pos(int chain_id)
  1559. {
  1560. int lch;
  1561. int *channels;
  1562. /* Check for input params */
  1563. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1564. printk(KERN_ERR "Invalid chain id\n");
  1565. return -EINVAL;
  1566. }
  1567. /* Check if the chain exists */
  1568. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1569. printk(KERN_ERR "Chain doesn't exists\n");
  1570. return -EINVAL;
  1571. }
  1572. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1573. /* Get the current channel */
  1574. lch = channels[dma_linked_lch[chain_id].q_head];
  1575. return dma_read(CSAC(lch));
  1576. }
  1577. EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
  1578. #endif /* ifndef CONFIG_ARCH_OMAP1 */
  1579. /*----------------------------------------------------------------------------*/
  1580. #ifdef CONFIG_ARCH_OMAP1
  1581. static int omap1_dma_handle_ch(int ch)
  1582. {
  1583. u32 csr;
  1584. if (enable_1510_mode && ch >= 6) {
  1585. csr = dma_chan[ch].saved_csr;
  1586. dma_chan[ch].saved_csr = 0;
  1587. } else
  1588. csr = dma_read(CSR(ch));
  1589. if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
  1590. dma_chan[ch + 6].saved_csr = csr >> 7;
  1591. csr &= 0x7f;
  1592. }
  1593. if ((csr & 0x3f) == 0)
  1594. return 0;
  1595. if (unlikely(dma_chan[ch].dev_id == -1)) {
  1596. printk(KERN_WARNING "Spurious interrupt from DMA channel "
  1597. "%d (CSR %04x)\n", ch, csr);
  1598. return 0;
  1599. }
  1600. if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
  1601. printk(KERN_WARNING "DMA timeout with device %d\n",
  1602. dma_chan[ch].dev_id);
  1603. if (unlikely(csr & OMAP_DMA_DROP_IRQ))
  1604. printk(KERN_WARNING "DMA synchronization event drop occurred "
  1605. "with device %d\n", dma_chan[ch].dev_id);
  1606. if (likely(csr & OMAP_DMA_BLOCK_IRQ))
  1607. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  1608. if (likely(dma_chan[ch].callback != NULL))
  1609. dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
  1610. return 1;
  1611. }
  1612. static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
  1613. {
  1614. int ch = ((int) dev_id) - 1;
  1615. int handled = 0;
  1616. for (;;) {
  1617. int handled_now = 0;
  1618. handled_now += omap1_dma_handle_ch(ch);
  1619. if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
  1620. handled_now += omap1_dma_handle_ch(ch + 6);
  1621. if (!handled_now)
  1622. break;
  1623. handled += handled_now;
  1624. }
  1625. return handled ? IRQ_HANDLED : IRQ_NONE;
  1626. }
  1627. #else
  1628. #define omap1_dma_irq_handler NULL
  1629. #endif
  1630. #ifdef CONFIG_ARCH_OMAP2PLUS
  1631. static int omap2_dma_handle_ch(int ch)
  1632. {
  1633. u32 status = dma_read(CSR(ch));
  1634. if (!status) {
  1635. if (printk_ratelimit())
  1636. printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
  1637. ch);
  1638. dma_write(1 << ch, IRQSTATUS_L0);
  1639. return 0;
  1640. }
  1641. if (unlikely(dma_chan[ch].dev_id == -1)) {
  1642. if (printk_ratelimit())
  1643. printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
  1644. "channel %d\n", status, ch);
  1645. return 0;
  1646. }
  1647. if (unlikely(status & OMAP_DMA_DROP_IRQ))
  1648. printk(KERN_INFO
  1649. "DMA synchronization event drop occurred with device "
  1650. "%d\n", dma_chan[ch].dev_id);
  1651. if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
  1652. printk(KERN_INFO "DMA transaction error with device %d\n",
  1653. dma_chan[ch].dev_id);
  1654. if (cpu_class_is_omap2()) {
  1655. /*
  1656. * Errata: sDMA Channel is not disabled
  1657. * after a transaction error. So we explicitely
  1658. * disable the channel
  1659. */
  1660. u32 ccr;
  1661. ccr = dma_read(CCR(ch));
  1662. ccr &= ~OMAP_DMA_CCR_EN;
  1663. dma_write(ccr, CCR(ch));
  1664. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  1665. }
  1666. }
  1667. if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
  1668. printk(KERN_INFO "DMA secure error with device %d\n",
  1669. dma_chan[ch].dev_id);
  1670. if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
  1671. printk(KERN_INFO "DMA misaligned error with device %d\n",
  1672. dma_chan[ch].dev_id);
  1673. dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(ch));
  1674. dma_write(1 << ch, IRQSTATUS_L0);
  1675. /* If the ch is not chained then chain_id will be -1 */
  1676. if (dma_chan[ch].chain_id != -1) {
  1677. int chain_id = dma_chan[ch].chain_id;
  1678. dma_chan[ch].state = DMA_CH_NOTSTARTED;
  1679. if (dma_read(CLNK_CTRL(ch)) & (1 << 15))
  1680. dma_chan[dma_chan[ch].next_linked_ch].state =
  1681. DMA_CH_STARTED;
  1682. if (dma_linked_lch[chain_id].chain_mode ==
  1683. OMAP_DMA_DYNAMIC_CHAIN)
  1684. disable_lnk(ch);
  1685. if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
  1686. OMAP_DMA_CHAIN_INCQHEAD(chain_id);
  1687. status = dma_read(CSR(ch));
  1688. }
  1689. dma_write(status, CSR(ch));
  1690. if (likely(dma_chan[ch].callback != NULL))
  1691. dma_chan[ch].callback(ch, status, dma_chan[ch].data);
  1692. return 0;
  1693. }
  1694. /* STATUS register count is from 1-32 while our is 0-31 */
  1695. static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
  1696. {
  1697. u32 val, enable_reg;
  1698. int i;
  1699. val = dma_read(IRQSTATUS_L0);
  1700. if (val == 0) {
  1701. if (printk_ratelimit())
  1702. printk(KERN_WARNING "Spurious DMA IRQ\n");
  1703. return IRQ_HANDLED;
  1704. }
  1705. enable_reg = dma_read(IRQENABLE_L0);
  1706. val &= enable_reg; /* Dispatch only relevant interrupts */
  1707. for (i = 0; i < dma_lch_count && val != 0; i++) {
  1708. if (val & 1)
  1709. omap2_dma_handle_ch(i);
  1710. val >>= 1;
  1711. }
  1712. return IRQ_HANDLED;
  1713. }
  1714. static struct irqaction omap24xx_dma_irq = {
  1715. .name = "DMA",
  1716. .handler = omap2_dma_irq_handler,
  1717. .flags = IRQF_DISABLED
  1718. };
  1719. #else
  1720. static struct irqaction omap24xx_dma_irq;
  1721. #endif
  1722. /*----------------------------------------------------------------------------*/
  1723. void omap_dma_global_context_save(void)
  1724. {
  1725. omap_dma_global_context.dma_irqenable_l0 =
  1726. dma_read(IRQENABLE_L0);
  1727. omap_dma_global_context.dma_ocp_sysconfig =
  1728. dma_read(OCP_SYSCONFIG);
  1729. omap_dma_global_context.dma_gcr = dma_read(GCR);
  1730. }
  1731. void omap_dma_global_context_restore(void)
  1732. {
  1733. int ch;
  1734. dma_write(omap_dma_global_context.dma_gcr, GCR);
  1735. dma_write(omap_dma_global_context.dma_ocp_sysconfig,
  1736. OCP_SYSCONFIG);
  1737. dma_write(omap_dma_global_context.dma_irqenable_l0,
  1738. IRQENABLE_L0);
  1739. /*
  1740. * A bug in ROM code leaves IRQ status for channels 0 and 1 uncleared
  1741. * after secure sram context save and restore. Hence we need to
  1742. * manually clear those IRQs to avoid spurious interrupts. This
  1743. * affects only secure devices.
  1744. */
  1745. if (cpu_is_omap34xx() && (omap_type() != OMAP2_DEVICE_TYPE_GP))
  1746. dma_write(0x3 , IRQSTATUS_L0);
  1747. for (ch = 0; ch < dma_chan_count; ch++)
  1748. if (dma_chan[ch].dev_id != -1)
  1749. omap_clear_dma(ch);
  1750. }
  1751. /*----------------------------------------------------------------------------*/
  1752. static int __init omap_init_dma(void)
  1753. {
  1754. unsigned long base;
  1755. int ch, r;
  1756. if (cpu_class_is_omap1()) {
  1757. base = OMAP1_DMA_BASE;
  1758. dma_lch_count = OMAP1_LOGICAL_DMA_CH_COUNT;
  1759. } else if (cpu_is_omap24xx()) {
  1760. base = OMAP24XX_DMA4_BASE;
  1761. dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
  1762. } else if (cpu_is_omap34xx()) {
  1763. base = OMAP34XX_DMA4_BASE;
  1764. dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
  1765. } else if (cpu_is_omap44xx()) {
  1766. base = OMAP44XX_DMA4_BASE;
  1767. dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
  1768. } else {
  1769. pr_err("DMA init failed for unsupported omap\n");
  1770. return -ENODEV;
  1771. }
  1772. omap_dma_base = ioremap(base, SZ_4K);
  1773. BUG_ON(!omap_dma_base);
  1774. if (cpu_class_is_omap2() && omap_dma_reserve_channels
  1775. && (omap_dma_reserve_channels <= dma_lch_count))
  1776. dma_lch_count = omap_dma_reserve_channels;
  1777. dma_chan = kzalloc(sizeof(struct omap_dma_lch) * dma_lch_count,
  1778. GFP_KERNEL);
  1779. if (!dma_chan) {
  1780. r = -ENOMEM;
  1781. goto out_unmap;
  1782. }
  1783. if (cpu_class_is_omap2()) {
  1784. dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
  1785. dma_lch_count, GFP_KERNEL);
  1786. if (!dma_linked_lch) {
  1787. r = -ENOMEM;
  1788. goto out_free;
  1789. }
  1790. }
  1791. if (cpu_is_omap15xx()) {
  1792. printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
  1793. dma_chan_count = 9;
  1794. enable_1510_mode = 1;
  1795. } else if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
  1796. printk(KERN_INFO "OMAP DMA hardware version %d\n",
  1797. dma_read(HW_ID));
  1798. printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
  1799. (dma_read(CAPS_0_U) << 16) |
  1800. dma_read(CAPS_0_L),
  1801. (dma_read(CAPS_1_U) << 16) |
  1802. dma_read(CAPS_1_L),
  1803. dma_read(CAPS_2), dma_read(CAPS_3),
  1804. dma_read(CAPS_4));
  1805. if (!enable_1510_mode) {
  1806. u16 w;
  1807. /* Disable OMAP 3.0/3.1 compatibility mode. */
  1808. w = dma_read(GSCR);
  1809. w |= 1 << 3;
  1810. dma_write(w, GSCR);
  1811. dma_chan_count = 16;
  1812. } else
  1813. dma_chan_count = 9;
  1814. } else if (cpu_class_is_omap2()) {
  1815. u8 revision = dma_read(REVISION) & 0xff;
  1816. printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
  1817. revision >> 4, revision & 0xf);
  1818. dma_chan_count = dma_lch_count;
  1819. } else {
  1820. dma_chan_count = 0;
  1821. return 0;
  1822. }
  1823. spin_lock_init(&dma_chan_lock);
  1824. for (ch = 0; ch < dma_chan_count; ch++) {
  1825. omap_clear_dma(ch);
  1826. if (cpu_class_is_omap2())
  1827. omap2_disable_irq_lch(ch);
  1828. dma_chan[ch].dev_id = -1;
  1829. dma_chan[ch].next_lch = -1;
  1830. if (ch >= 6 && enable_1510_mode)
  1831. continue;
  1832. if (cpu_class_is_omap1()) {
  1833. /*
  1834. * request_irq() doesn't like dev_id (ie. ch) being
  1835. * zero, so we have to kludge around this.
  1836. */
  1837. r = request_irq(omap1_dma_irq[ch],
  1838. omap1_dma_irq_handler, 0, "DMA",
  1839. (void *) (ch + 1));
  1840. if (r != 0) {
  1841. int i;
  1842. printk(KERN_ERR "unable to request IRQ %d "
  1843. "for DMA (error %d)\n",
  1844. omap1_dma_irq[ch], r);
  1845. for (i = 0; i < ch; i++)
  1846. free_irq(omap1_dma_irq[i],
  1847. (void *) (i + 1));
  1848. goto out_free;
  1849. }
  1850. }
  1851. }
  1852. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
  1853. omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
  1854. DMA_DEFAULT_FIFO_DEPTH, 0);
  1855. if (cpu_class_is_omap2()) {
  1856. int irq;
  1857. if (cpu_is_omap44xx())
  1858. irq = OMAP44XX_IRQ_SDMA_0;
  1859. else
  1860. irq = INT_24XX_SDMA_IRQ0;
  1861. setup_irq(irq, &omap24xx_dma_irq);
  1862. }
  1863. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  1864. /* Enable smartidle idlemodes and autoidle */
  1865. u32 v = dma_read(OCP_SYSCONFIG);
  1866. v &= ~(DMA_SYSCONFIG_MIDLEMODE_MASK |
  1867. DMA_SYSCONFIG_SIDLEMODE_MASK |
  1868. DMA_SYSCONFIG_AUTOIDLE);
  1869. v |= (DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_SMARTIDLE) |
  1870. DMA_SYSCONFIG_SIDLEMODE(DMA_IDLEMODE_SMARTIDLE) |
  1871. DMA_SYSCONFIG_AUTOIDLE);
  1872. dma_write(v , OCP_SYSCONFIG);
  1873. /* reserve dma channels 0 and 1 in high security devices */
  1874. if (cpu_is_omap34xx() &&
  1875. (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
  1876. printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
  1877. "HS ROM code\n");
  1878. dma_chan[0].dev_id = 0;
  1879. dma_chan[1].dev_id = 1;
  1880. }
  1881. }
  1882. return 0;
  1883. out_free:
  1884. kfree(dma_chan);
  1885. out_unmap:
  1886. iounmap(omap_dma_base);
  1887. return r;
  1888. }
  1889. arch_initcall(omap_init_dma);
  1890. /*
  1891. * Reserve the omap SDMA channels using cmdline bootarg
  1892. * "omap_dma_reserve_ch=". The valid range is 1 to 32
  1893. */
  1894. static int __init omap_dma_cmdline_reserve_ch(char *str)
  1895. {
  1896. if (get_option(&str, &omap_dma_reserve_channels) != 1)
  1897. omap_dma_reserve_channels = 0;
  1898. return 1;
  1899. }
  1900. __setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);