dma.c 50 KB

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