dma.c 55 KB

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