dma.c 51 KB

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