dma.c 52 KB

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