dma.c 51 KB

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