dma.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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: On ES2.0 BUFFERING disable must be set.
  834. * This will always fail on ES1.0
  835. */
  836. if (cpu_is_omap24xx())
  837. l |= OMAP_DMA_CCR_EN;
  838. l |= OMAP_DMA_CCR_EN;
  839. dma_write(l, CCR(lch));
  840. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  841. }
  842. EXPORT_SYMBOL(omap_start_dma);
  843. void omap_stop_dma(int lch)
  844. {
  845. u32 l;
  846. /* Disable all interrupts on the channel */
  847. if (cpu_class_is_omap1())
  848. dma_write(0, CICR(lch));
  849. l = dma_read(CCR(lch));
  850. l &= ~OMAP_DMA_CCR_EN;
  851. dma_write(l, CCR(lch));
  852. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  853. int next_lch, cur_lch = lch;
  854. char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
  855. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  856. do {
  857. /* The loop case: we've been here already */
  858. if (dma_chan_link_map[cur_lch])
  859. break;
  860. /* Mark the current channel */
  861. dma_chan_link_map[cur_lch] = 1;
  862. disable_lnk(cur_lch);
  863. next_lch = dma_chan[cur_lch].next_lch;
  864. cur_lch = next_lch;
  865. } while (next_lch != -1);
  866. }
  867. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  868. }
  869. EXPORT_SYMBOL(omap_stop_dma);
  870. /*
  871. * Allows changing the DMA callback function or data. This may be needed if
  872. * the driver shares a single DMA channel for multiple dma triggers.
  873. */
  874. int omap_set_dma_callback(int lch,
  875. void (*callback)(int lch, u16 ch_status, void *data),
  876. void *data)
  877. {
  878. unsigned long flags;
  879. if (lch < 0)
  880. return -ENODEV;
  881. spin_lock_irqsave(&dma_chan_lock, flags);
  882. if (dma_chan[lch].dev_id == -1) {
  883. printk(KERN_ERR "DMA callback for not set for free channel\n");
  884. spin_unlock_irqrestore(&dma_chan_lock, flags);
  885. return -EINVAL;
  886. }
  887. dma_chan[lch].callback = callback;
  888. dma_chan[lch].data = data;
  889. spin_unlock_irqrestore(&dma_chan_lock, flags);
  890. return 0;
  891. }
  892. EXPORT_SYMBOL(omap_set_dma_callback);
  893. /*
  894. * Returns current physical source address for the given DMA channel.
  895. * If the channel is running the caller must disable interrupts prior calling
  896. * this function and process the returned value before re-enabling interrupt to
  897. * prevent races with the interrupt handler. Note that in continuous mode there
  898. * is a chance for CSSA_L register overflow inbetween the two reads resulting
  899. * in incorrect return value.
  900. */
  901. dma_addr_t omap_get_dma_src_pos(int lch)
  902. {
  903. dma_addr_t offset = 0;
  904. if (cpu_is_omap15xx())
  905. offset = dma_read(CPC(lch));
  906. else
  907. offset = dma_read(CSAC(lch));
  908. /*
  909. * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  910. * read before the DMA controller finished disabling the channel.
  911. */
  912. if (!cpu_is_omap15xx() && offset == 0)
  913. offset = dma_read(CSAC(lch));
  914. if (cpu_class_is_omap1())
  915. offset |= (dma_read(CSSA_U(lch)) << 16);
  916. return offset;
  917. }
  918. EXPORT_SYMBOL(omap_get_dma_src_pos);
  919. /*
  920. * Returns current physical destination address for the given DMA channel.
  921. * If the channel is running the caller must disable interrupts prior calling
  922. * this function and process the returned value before re-enabling interrupt to
  923. * prevent races with the interrupt handler. Note that in continuous mode there
  924. * is a chance for CDSA_L register overflow inbetween the two reads resulting
  925. * in incorrect return value.
  926. */
  927. dma_addr_t omap_get_dma_dst_pos(int lch)
  928. {
  929. dma_addr_t offset = 0;
  930. if (cpu_is_omap15xx())
  931. offset = dma_read(CPC(lch));
  932. else
  933. offset = dma_read(CDAC(lch));
  934. /*
  935. * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  936. * read before the DMA controller finished disabling the channel.
  937. */
  938. if (!cpu_is_omap15xx() && offset == 0)
  939. offset = dma_read(CDAC(lch));
  940. if (cpu_class_is_omap1())
  941. offset |= (dma_read(CDSA_U(lch)) << 16);
  942. return offset;
  943. }
  944. EXPORT_SYMBOL(omap_get_dma_dst_pos);
  945. int omap_get_dma_active_status(int lch)
  946. {
  947. return (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN) != 0;
  948. }
  949. EXPORT_SYMBOL(omap_get_dma_active_status);
  950. int omap_dma_running(void)
  951. {
  952. int lch;
  953. if (cpu_class_is_omap1())
  954. if (omap_lcd_dma_running())
  955. return 1;
  956. for (lch = 0; lch < dma_chan_count; lch++)
  957. if (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN)
  958. return 1;
  959. return 0;
  960. }
  961. /*
  962. * lch_queue DMA will start right after lch_head one is finished.
  963. * For this DMA link to start, you still need to start (see omap_start_dma)
  964. * the first one. That will fire up the entire queue.
  965. */
  966. void omap_dma_link_lch(int lch_head, int lch_queue)
  967. {
  968. if (omap_dma_in_1510_mode()) {
  969. if (lch_head == lch_queue) {
  970. dma_write(dma_read(CCR(lch_head)) | (3 << 8),
  971. CCR(lch_head));
  972. return;
  973. }
  974. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  975. BUG();
  976. return;
  977. }
  978. if ((dma_chan[lch_head].dev_id == -1) ||
  979. (dma_chan[lch_queue].dev_id == -1)) {
  980. printk(KERN_ERR "omap_dma: trying to link "
  981. "non requested channels\n");
  982. dump_stack();
  983. }
  984. dma_chan[lch_head].next_lch = lch_queue;
  985. }
  986. EXPORT_SYMBOL(omap_dma_link_lch);
  987. /*
  988. * Once the DMA queue is stopped, we can destroy it.
  989. */
  990. void omap_dma_unlink_lch(int lch_head, int lch_queue)
  991. {
  992. if (omap_dma_in_1510_mode()) {
  993. if (lch_head == lch_queue) {
  994. dma_write(dma_read(CCR(lch_head)) & ~(3 << 8),
  995. CCR(lch_head));
  996. return;
  997. }
  998. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  999. BUG();
  1000. return;
  1001. }
  1002. if (dma_chan[lch_head].next_lch != lch_queue ||
  1003. dma_chan[lch_head].next_lch == -1) {
  1004. printk(KERN_ERR "omap_dma: trying to unlink "
  1005. "non linked channels\n");
  1006. dump_stack();
  1007. }
  1008. if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
  1009. (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
  1010. printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
  1011. "before unlinking\n");
  1012. dump_stack();
  1013. }
  1014. dma_chan[lch_head].next_lch = -1;
  1015. }
  1016. EXPORT_SYMBOL(omap_dma_unlink_lch);
  1017. /*----------------------------------------------------------------------------*/
  1018. #ifndef CONFIG_ARCH_OMAP1
  1019. /* Create chain of DMA channesls */
  1020. static void create_dma_lch_chain(int lch_head, int lch_queue)
  1021. {
  1022. u32 l;
  1023. /* Check if this is the first link in chain */
  1024. if (dma_chan[lch_head].next_linked_ch == -1) {
  1025. dma_chan[lch_head].next_linked_ch = lch_queue;
  1026. dma_chan[lch_head].prev_linked_ch = lch_queue;
  1027. dma_chan[lch_queue].next_linked_ch = lch_head;
  1028. dma_chan[lch_queue].prev_linked_ch = lch_head;
  1029. }
  1030. /* a link exists, link the new channel in circular chain */
  1031. else {
  1032. dma_chan[lch_queue].next_linked_ch =
  1033. dma_chan[lch_head].next_linked_ch;
  1034. dma_chan[lch_queue].prev_linked_ch = lch_head;
  1035. dma_chan[lch_head].next_linked_ch = lch_queue;
  1036. dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
  1037. lch_queue;
  1038. }
  1039. l = dma_read(CLNK_CTRL(lch_head));
  1040. l &= ~(0x1f);
  1041. l |= lch_queue;
  1042. dma_write(l, CLNK_CTRL(lch_head));
  1043. l = dma_read(CLNK_CTRL(lch_queue));
  1044. l &= ~(0x1f);
  1045. l |= (dma_chan[lch_queue].next_linked_ch);
  1046. dma_write(l, CLNK_CTRL(lch_queue));
  1047. }
  1048. /**
  1049. * @brief omap_request_dma_chain : Request a chain of DMA channels
  1050. *
  1051. * @param dev_id - Device id using the dma channel
  1052. * @param dev_name - Device name
  1053. * @param callback - Call back function
  1054. * @chain_id -
  1055. * @no_of_chans - Number of channels requested
  1056. * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
  1057. * OMAP_DMA_DYNAMIC_CHAIN
  1058. * @params - Channel parameters
  1059. *
  1060. * @return - Success : 0
  1061. * Failure: -EINVAL/-ENOMEM
  1062. */
  1063. int omap_request_dma_chain(int dev_id, const char *dev_name,
  1064. void (*callback) (int lch, u16 ch_status,
  1065. void *data),
  1066. int *chain_id, int no_of_chans, int chain_mode,
  1067. struct omap_dma_channel_params params)
  1068. {
  1069. int *channels;
  1070. int i, err;
  1071. /* Is the chain mode valid ? */
  1072. if (chain_mode != OMAP_DMA_STATIC_CHAIN
  1073. && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
  1074. printk(KERN_ERR "Invalid chain mode requested\n");
  1075. return -EINVAL;
  1076. }
  1077. if (unlikely((no_of_chans < 1
  1078. || no_of_chans > dma_lch_count))) {
  1079. printk(KERN_ERR "Invalid Number of channels requested\n");
  1080. return -EINVAL;
  1081. }
  1082. /*
  1083. * Allocate a queue to maintain the status of the channels
  1084. * in the chain
  1085. */
  1086. channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
  1087. if (channels == NULL) {
  1088. printk(KERN_ERR "omap_dma: No memory for channel queue\n");
  1089. return -ENOMEM;
  1090. }
  1091. /* request and reserve DMA channels for the chain */
  1092. for (i = 0; i < no_of_chans; i++) {
  1093. err = omap_request_dma(dev_id, dev_name,
  1094. callback, NULL, &channels[i]);
  1095. if (err < 0) {
  1096. int j;
  1097. for (j = 0; j < i; j++)
  1098. omap_free_dma(channels[j]);
  1099. kfree(channels);
  1100. printk(KERN_ERR "omap_dma: Request failed %d\n", err);
  1101. return err;
  1102. }
  1103. dma_chan[channels[i]].prev_linked_ch = -1;
  1104. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1105. /*
  1106. * Allowing client drivers to set common parameters now,
  1107. * so that later only relevant (src_start, dest_start
  1108. * and element count) can be set
  1109. */
  1110. omap_set_dma_params(channels[i], &params);
  1111. }
  1112. *chain_id = channels[0];
  1113. dma_linked_lch[*chain_id].linked_dmach_q = channels;
  1114. dma_linked_lch[*chain_id].chain_mode = chain_mode;
  1115. dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
  1116. dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
  1117. for (i = 0; i < no_of_chans; i++)
  1118. dma_chan[channels[i]].chain_id = *chain_id;
  1119. /* Reset the Queue pointers */
  1120. OMAP_DMA_CHAIN_QINIT(*chain_id);
  1121. /* Set up the chain */
  1122. if (no_of_chans == 1)
  1123. create_dma_lch_chain(channels[0], channels[0]);
  1124. else {
  1125. for (i = 0; i < (no_of_chans - 1); i++)
  1126. create_dma_lch_chain(channels[i], channels[i + 1]);
  1127. }
  1128. return 0;
  1129. }
  1130. EXPORT_SYMBOL(omap_request_dma_chain);
  1131. /**
  1132. * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
  1133. * params after setting it. Dont do this while dma is running!!
  1134. *
  1135. * @param chain_id - Chained logical channel id.
  1136. * @param params
  1137. *
  1138. * @return - Success : 0
  1139. * Failure : -EINVAL
  1140. */
  1141. int omap_modify_dma_chain_params(int chain_id,
  1142. struct omap_dma_channel_params params)
  1143. {
  1144. int *channels;
  1145. u32 i;
  1146. /* Check for input params */
  1147. if (unlikely((chain_id < 0
  1148. || chain_id >= dma_lch_count))) {
  1149. printk(KERN_ERR "Invalid chain id\n");
  1150. return -EINVAL;
  1151. }
  1152. /* Check if the chain exists */
  1153. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1154. printk(KERN_ERR "Chain doesn't exists\n");
  1155. return -EINVAL;
  1156. }
  1157. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1158. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1159. /*
  1160. * Allowing client drivers to set common parameters now,
  1161. * so that later only relevant (src_start, dest_start
  1162. * and element count) can be set
  1163. */
  1164. omap_set_dma_params(channels[i], &params);
  1165. }
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL(omap_modify_dma_chain_params);
  1169. /**
  1170. * @brief omap_free_dma_chain - Free all the logical channels in a chain.
  1171. *
  1172. * @param chain_id
  1173. *
  1174. * @return - Success : 0
  1175. * Failure : -EINVAL
  1176. */
  1177. int omap_free_dma_chain(int chain_id)
  1178. {
  1179. int *channels;
  1180. u32 i;
  1181. /* Check for input params */
  1182. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1183. printk(KERN_ERR "Invalid chain id\n");
  1184. return -EINVAL;
  1185. }
  1186. /* Check if the chain exists */
  1187. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1188. printk(KERN_ERR "Chain doesn't exists\n");
  1189. return -EINVAL;
  1190. }
  1191. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1192. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1193. dma_chan[channels[i]].next_linked_ch = -1;
  1194. dma_chan[channels[i]].prev_linked_ch = -1;
  1195. dma_chan[channels[i]].chain_id = -1;
  1196. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1197. omap_free_dma(channels[i]);
  1198. }
  1199. kfree(channels);
  1200. dma_linked_lch[chain_id].linked_dmach_q = NULL;
  1201. dma_linked_lch[chain_id].chain_mode = -1;
  1202. dma_linked_lch[chain_id].chain_state = -1;
  1203. return (0);
  1204. }
  1205. EXPORT_SYMBOL(omap_free_dma_chain);
  1206. /**
  1207. * @brief omap_dma_chain_status - Check if the chain is in
  1208. * active / inactive state.
  1209. * @param chain_id
  1210. *
  1211. * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
  1212. * Failure : -EINVAL
  1213. */
  1214. int omap_dma_chain_status(int chain_id)
  1215. {
  1216. /* Check for input params */
  1217. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1218. printk(KERN_ERR "Invalid chain id\n");
  1219. return -EINVAL;
  1220. }
  1221. /* Check if the chain exists */
  1222. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1223. printk(KERN_ERR "Chain doesn't exists\n");
  1224. return -EINVAL;
  1225. }
  1226. pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
  1227. dma_linked_lch[chain_id].q_count);
  1228. if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
  1229. return OMAP_DMA_CHAIN_INACTIVE;
  1230. return OMAP_DMA_CHAIN_ACTIVE;
  1231. }
  1232. EXPORT_SYMBOL(omap_dma_chain_status);
  1233. /**
  1234. * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
  1235. * set the params and start the transfer.
  1236. *
  1237. * @param chain_id
  1238. * @param src_start - buffer start address
  1239. * @param dest_start - Dest address
  1240. * @param elem_count
  1241. * @param frame_count
  1242. * @param callbk_data - channel callback parameter data.
  1243. *
  1244. * @return - Success : 0
  1245. * Failure: -EINVAL/-EBUSY
  1246. */
  1247. int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
  1248. int elem_count, int frame_count, void *callbk_data)
  1249. {
  1250. int *channels;
  1251. u32 l, lch;
  1252. int start_dma = 0;
  1253. /*
  1254. * if buffer size is less than 1 then there is
  1255. * no use of starting the chain
  1256. */
  1257. if (elem_count < 1) {
  1258. printk(KERN_ERR "Invalid buffer size\n");
  1259. return -EINVAL;
  1260. }
  1261. /* Check for input params */
  1262. if (unlikely((chain_id < 0
  1263. || chain_id >= dma_lch_count))) {
  1264. printk(KERN_ERR "Invalid chain id\n");
  1265. return -EINVAL;
  1266. }
  1267. /* Check if the chain exists */
  1268. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1269. printk(KERN_ERR "Chain doesn't exist\n");
  1270. return -EINVAL;
  1271. }
  1272. /* Check if all the channels in chain are in use */
  1273. if (OMAP_DMA_CHAIN_QFULL(chain_id))
  1274. return -EBUSY;
  1275. /* Frame count may be negative in case of indexed transfers */
  1276. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1277. /* Get a free channel */
  1278. lch = channels[dma_linked_lch[chain_id].q_tail];
  1279. /* Store the callback data */
  1280. dma_chan[lch].data = callbk_data;
  1281. /* Increment the q_tail */
  1282. OMAP_DMA_CHAIN_INCQTAIL(chain_id);
  1283. /* Set the params to the free channel */
  1284. if (src_start != 0)
  1285. dma_write(src_start, CSSA(lch));
  1286. if (dest_start != 0)
  1287. dma_write(dest_start, CDSA(lch));
  1288. /* Write the buffer size */
  1289. dma_write(elem_count, CEN(lch));
  1290. dma_write(frame_count, CFN(lch));
  1291. /*
  1292. * If the chain is dynamically linked,
  1293. * then we may have to start the chain if its not active
  1294. */
  1295. if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
  1296. /*
  1297. * In Dynamic chain, if the chain is not started,
  1298. * queue the channel
  1299. */
  1300. if (dma_linked_lch[chain_id].chain_state ==
  1301. DMA_CHAIN_NOTSTARTED) {
  1302. /* Enable the link in previous channel */
  1303. if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
  1304. DMA_CH_QUEUED)
  1305. enable_lnk(dma_chan[lch].prev_linked_ch);
  1306. dma_chan[lch].state = DMA_CH_QUEUED;
  1307. }
  1308. /*
  1309. * Chain is already started, make sure its active,
  1310. * if not then start the chain
  1311. */
  1312. else {
  1313. start_dma = 1;
  1314. if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
  1315. DMA_CH_STARTED) {
  1316. enable_lnk(dma_chan[lch].prev_linked_ch);
  1317. dma_chan[lch].state = DMA_CH_QUEUED;
  1318. start_dma = 0;
  1319. if (0 == ((1 << 7) & dma_read(
  1320. CCR(dma_chan[lch].prev_linked_ch)))) {
  1321. disable_lnk(dma_chan[lch].
  1322. prev_linked_ch);
  1323. pr_debug("\n prev ch is stopped\n");
  1324. start_dma = 1;
  1325. }
  1326. }
  1327. else if (dma_chan[dma_chan[lch].prev_linked_ch].state
  1328. == DMA_CH_QUEUED) {
  1329. enable_lnk(dma_chan[lch].prev_linked_ch);
  1330. dma_chan[lch].state = DMA_CH_QUEUED;
  1331. start_dma = 0;
  1332. }
  1333. omap_enable_channel_irq(lch);
  1334. l = dma_read(CCR(lch));
  1335. if ((0 == (l & (1 << 24))))
  1336. l &= ~(1 << 25);
  1337. else
  1338. l |= (1 << 25);
  1339. if (start_dma == 1) {
  1340. if (0 == (l & (1 << 7))) {
  1341. l |= (1 << 7);
  1342. dma_chan[lch].state = DMA_CH_STARTED;
  1343. pr_debug("starting %d\n", lch);
  1344. dma_write(l, CCR(lch));
  1345. } else
  1346. start_dma = 0;
  1347. } else {
  1348. if (0 == (l & (1 << 7)))
  1349. dma_write(l, CCR(lch));
  1350. }
  1351. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  1352. }
  1353. }
  1354. return 0;
  1355. }
  1356. EXPORT_SYMBOL(omap_dma_chain_a_transfer);
  1357. /**
  1358. * @brief omap_start_dma_chain_transfers - Start the chain
  1359. *
  1360. * @param chain_id
  1361. *
  1362. * @return - Success : 0
  1363. * Failure : -EINVAL/-EBUSY
  1364. */
  1365. int omap_start_dma_chain_transfers(int chain_id)
  1366. {
  1367. int *channels;
  1368. u32 l, i;
  1369. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1370. printk(KERN_ERR "Invalid chain id\n");
  1371. return -EINVAL;
  1372. }
  1373. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1374. if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
  1375. printk(KERN_ERR "Chain is already started\n");
  1376. return -EBUSY;
  1377. }
  1378. if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
  1379. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
  1380. i++) {
  1381. enable_lnk(channels[i]);
  1382. omap_enable_channel_irq(channels[i]);
  1383. }
  1384. } else {
  1385. omap_enable_channel_irq(channels[0]);
  1386. }
  1387. l = dma_read(CCR(channels[0]));
  1388. l |= (1 << 7);
  1389. dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
  1390. dma_chan[channels[0]].state = DMA_CH_STARTED;
  1391. if ((0 == (l & (1 << 24))))
  1392. l &= ~(1 << 25);
  1393. else
  1394. l |= (1 << 25);
  1395. dma_write(l, CCR(channels[0]));
  1396. dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(omap_start_dma_chain_transfers);
  1400. /**
  1401. * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
  1402. *
  1403. * @param chain_id
  1404. *
  1405. * @return - Success : 0
  1406. * Failure : EINVAL
  1407. */
  1408. int omap_stop_dma_chain_transfers(int chain_id)
  1409. {
  1410. int *channels;
  1411. u32 l, i;
  1412. u32 sys_cf;
  1413. /* Check for input params */
  1414. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1415. printk(KERN_ERR "Invalid chain id\n");
  1416. return -EINVAL;
  1417. }
  1418. /* Check if the chain exists */
  1419. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1420. printk(KERN_ERR "Chain doesn't exists\n");
  1421. return -EINVAL;
  1422. }
  1423. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1424. /*
  1425. * DMA Errata:
  1426. * Special programming model needed to disable DMA before end of block
  1427. */
  1428. sys_cf = dma_read(OCP_SYSCONFIG);
  1429. l = sys_cf;
  1430. /* Middle mode reg set no Standby */
  1431. l &= ~((1 << 12)|(1 << 13));
  1432. dma_write(l, OCP_SYSCONFIG);
  1433. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1434. /* Stop the Channel transmission */
  1435. l = dma_read(CCR(channels[i]));
  1436. l &= ~(1 << 7);
  1437. dma_write(l, CCR(channels[i]));
  1438. /* Disable the link in all the channels */
  1439. disable_lnk(channels[i]);
  1440. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1441. }
  1442. dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
  1443. /* Reset the Queue pointers */
  1444. OMAP_DMA_CHAIN_QINIT(chain_id);
  1445. /* Errata - put in the old value */
  1446. dma_write(sys_cf, OCP_SYSCONFIG);
  1447. return 0;
  1448. }
  1449. EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
  1450. /* Get the index of the ongoing DMA in chain */
  1451. /**
  1452. * @brief omap_get_dma_chain_index - Get the element and frame index
  1453. * of the ongoing DMA in chain
  1454. *
  1455. * @param chain_id
  1456. * @param ei - Element index
  1457. * @param fi - Frame index
  1458. *
  1459. * @return - Success : 0
  1460. * Failure : -EINVAL
  1461. */
  1462. int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
  1463. {
  1464. int lch;
  1465. int *channels;
  1466. /* Check for input params */
  1467. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1468. printk(KERN_ERR "Invalid chain id\n");
  1469. return -EINVAL;
  1470. }
  1471. /* Check if the chain exists */
  1472. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1473. printk(KERN_ERR "Chain doesn't exists\n");
  1474. return -EINVAL;
  1475. }
  1476. if ((!ei) || (!fi))
  1477. return -EINVAL;
  1478. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1479. /* Get the current channel */
  1480. lch = channels[dma_linked_lch[chain_id].q_head];
  1481. *ei = dma_read(CCEN(lch));
  1482. *fi = dma_read(CCFN(lch));
  1483. return 0;
  1484. }
  1485. EXPORT_SYMBOL(omap_get_dma_chain_index);
  1486. /**
  1487. * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
  1488. * ongoing DMA in chain
  1489. *
  1490. * @param chain_id
  1491. *
  1492. * @return - Success : Destination position
  1493. * Failure : -EINVAL
  1494. */
  1495. int omap_get_dma_chain_dst_pos(int chain_id)
  1496. {
  1497. int lch;
  1498. int *channels;
  1499. /* Check for input params */
  1500. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1501. printk(KERN_ERR "Invalid chain id\n");
  1502. return -EINVAL;
  1503. }
  1504. /* Check if the chain exists */
  1505. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1506. printk(KERN_ERR "Chain doesn't exists\n");
  1507. return -EINVAL;
  1508. }
  1509. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1510. /* Get the current channel */
  1511. lch = channels[dma_linked_lch[chain_id].q_head];
  1512. return dma_read(CDAC(lch));
  1513. }
  1514. EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
  1515. /**
  1516. * @brief omap_get_dma_chain_src_pos - Get the source position
  1517. * of the ongoing DMA in chain
  1518. * @param chain_id
  1519. *
  1520. * @return - Success : Destination position
  1521. * Failure : -EINVAL
  1522. */
  1523. int omap_get_dma_chain_src_pos(int chain_id)
  1524. {
  1525. int lch;
  1526. int *channels;
  1527. /* Check for input params */
  1528. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1529. printk(KERN_ERR "Invalid chain id\n");
  1530. return -EINVAL;
  1531. }
  1532. /* Check if the chain exists */
  1533. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1534. printk(KERN_ERR "Chain doesn't exists\n");
  1535. return -EINVAL;
  1536. }
  1537. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1538. /* Get the current channel */
  1539. lch = channels[dma_linked_lch[chain_id].q_head];
  1540. return dma_read(CSAC(lch));
  1541. }
  1542. EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
  1543. #endif /* ifndef CONFIG_ARCH_OMAP1 */
  1544. /*----------------------------------------------------------------------------*/
  1545. #ifdef CONFIG_ARCH_OMAP1
  1546. static int omap1_dma_handle_ch(int ch)
  1547. {
  1548. u32 csr;
  1549. if (enable_1510_mode && ch >= 6) {
  1550. csr = dma_chan[ch].saved_csr;
  1551. dma_chan[ch].saved_csr = 0;
  1552. } else
  1553. csr = dma_read(CSR(ch));
  1554. if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
  1555. dma_chan[ch + 6].saved_csr = csr >> 7;
  1556. csr &= 0x7f;
  1557. }
  1558. if ((csr & 0x3f) == 0)
  1559. return 0;
  1560. if (unlikely(dma_chan[ch].dev_id == -1)) {
  1561. printk(KERN_WARNING "Spurious interrupt from DMA channel "
  1562. "%d (CSR %04x)\n", ch, csr);
  1563. return 0;
  1564. }
  1565. if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
  1566. printk(KERN_WARNING "DMA timeout with device %d\n",
  1567. dma_chan[ch].dev_id);
  1568. if (unlikely(csr & OMAP_DMA_DROP_IRQ))
  1569. printk(KERN_WARNING "DMA synchronization event drop occurred "
  1570. "with device %d\n", dma_chan[ch].dev_id);
  1571. if (likely(csr & OMAP_DMA_BLOCK_IRQ))
  1572. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  1573. if (likely(dma_chan[ch].callback != NULL))
  1574. dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
  1575. return 1;
  1576. }
  1577. static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
  1578. {
  1579. int ch = ((int) dev_id) - 1;
  1580. int handled = 0;
  1581. for (;;) {
  1582. int handled_now = 0;
  1583. handled_now += omap1_dma_handle_ch(ch);
  1584. if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
  1585. handled_now += omap1_dma_handle_ch(ch + 6);
  1586. if (!handled_now)
  1587. break;
  1588. handled += handled_now;
  1589. }
  1590. return handled ? IRQ_HANDLED : IRQ_NONE;
  1591. }
  1592. #else
  1593. #define omap1_dma_irq_handler NULL
  1594. #endif
  1595. #ifdef CONFIG_ARCH_OMAP2PLUS
  1596. static int omap2_dma_handle_ch(int ch)
  1597. {
  1598. u32 status = dma_read(CSR(ch));
  1599. if (!status) {
  1600. if (printk_ratelimit())
  1601. printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
  1602. ch);
  1603. dma_write(1 << ch, IRQSTATUS_L0);
  1604. return 0;
  1605. }
  1606. if (unlikely(dma_chan[ch].dev_id == -1)) {
  1607. if (printk_ratelimit())
  1608. printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
  1609. "channel %d\n", status, ch);
  1610. return 0;
  1611. }
  1612. if (unlikely(status & OMAP_DMA_DROP_IRQ))
  1613. printk(KERN_INFO
  1614. "DMA synchronization event drop occurred with device "
  1615. "%d\n", dma_chan[ch].dev_id);
  1616. if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
  1617. printk(KERN_INFO "DMA transaction error with device %d\n",
  1618. dma_chan[ch].dev_id);
  1619. if (cpu_class_is_omap2()) {
  1620. /*
  1621. * Errata: sDMA Channel is not disabled
  1622. * after a transaction error. So we explicitely
  1623. * disable the channel
  1624. */
  1625. u32 ccr;
  1626. ccr = dma_read(CCR(ch));
  1627. ccr &= ~OMAP_DMA_CCR_EN;
  1628. dma_write(ccr, CCR(ch));
  1629. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  1630. }
  1631. }
  1632. if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
  1633. printk(KERN_INFO "DMA secure error with device %d\n",
  1634. dma_chan[ch].dev_id);
  1635. if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
  1636. printk(KERN_INFO "DMA misaligned error with device %d\n",
  1637. dma_chan[ch].dev_id);
  1638. dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(ch));
  1639. dma_write(1 << ch, IRQSTATUS_L0);
  1640. /* If the ch is not chained then chain_id will be -1 */
  1641. if (dma_chan[ch].chain_id != -1) {
  1642. int chain_id = dma_chan[ch].chain_id;
  1643. dma_chan[ch].state = DMA_CH_NOTSTARTED;
  1644. if (dma_read(CLNK_CTRL(ch)) & (1 << 15))
  1645. dma_chan[dma_chan[ch].next_linked_ch].state =
  1646. DMA_CH_STARTED;
  1647. if (dma_linked_lch[chain_id].chain_mode ==
  1648. OMAP_DMA_DYNAMIC_CHAIN)
  1649. disable_lnk(ch);
  1650. if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
  1651. OMAP_DMA_CHAIN_INCQHEAD(chain_id);
  1652. status = dma_read(CSR(ch));
  1653. }
  1654. dma_write(status, CSR(ch));
  1655. if (likely(dma_chan[ch].callback != NULL))
  1656. dma_chan[ch].callback(ch, status, dma_chan[ch].data);
  1657. return 0;
  1658. }
  1659. /* STATUS register count is from 1-32 while our is 0-31 */
  1660. static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
  1661. {
  1662. u32 val, enable_reg;
  1663. int i;
  1664. val = dma_read(IRQSTATUS_L0);
  1665. if (val == 0) {
  1666. if (printk_ratelimit())
  1667. printk(KERN_WARNING "Spurious DMA IRQ\n");
  1668. return IRQ_HANDLED;
  1669. }
  1670. enable_reg = dma_read(IRQENABLE_L0);
  1671. val &= enable_reg; /* Dispatch only relevant interrupts */
  1672. for (i = 0; i < dma_lch_count && val != 0; i++) {
  1673. if (val & 1)
  1674. omap2_dma_handle_ch(i);
  1675. val >>= 1;
  1676. }
  1677. return IRQ_HANDLED;
  1678. }
  1679. static struct irqaction omap24xx_dma_irq = {
  1680. .name = "DMA",
  1681. .handler = omap2_dma_irq_handler,
  1682. .flags = IRQF_DISABLED
  1683. };
  1684. #else
  1685. static struct irqaction omap24xx_dma_irq;
  1686. #endif
  1687. /*----------------------------------------------------------------------------*/
  1688. void omap_dma_global_context_save(void)
  1689. {
  1690. omap_dma_global_context.dma_irqenable_l0 =
  1691. dma_read(IRQENABLE_L0);
  1692. omap_dma_global_context.dma_ocp_sysconfig =
  1693. dma_read(OCP_SYSCONFIG);
  1694. omap_dma_global_context.dma_gcr = dma_read(GCR);
  1695. }
  1696. void omap_dma_global_context_restore(void)
  1697. {
  1698. int ch;
  1699. dma_write(omap_dma_global_context.dma_gcr, GCR);
  1700. dma_write(omap_dma_global_context.dma_ocp_sysconfig,
  1701. OCP_SYSCONFIG);
  1702. dma_write(omap_dma_global_context.dma_irqenable_l0,
  1703. IRQENABLE_L0);
  1704. /*
  1705. * A bug in ROM code leaves IRQ status for channels 0 and 1 uncleared
  1706. * after secure sram context save and restore. Hence we need to
  1707. * manually clear those IRQs to avoid spurious interrupts. This
  1708. * affects only secure devices.
  1709. */
  1710. if (cpu_is_omap34xx() && (omap_type() != OMAP2_DEVICE_TYPE_GP))
  1711. dma_write(0x3 , IRQSTATUS_L0);
  1712. for (ch = 0; ch < dma_chan_count; ch++)
  1713. if (dma_chan[ch].dev_id != -1)
  1714. omap_clear_dma(ch);
  1715. }
  1716. /*----------------------------------------------------------------------------*/
  1717. static int __init omap_init_dma(void)
  1718. {
  1719. unsigned long base;
  1720. int ch, r;
  1721. if (cpu_class_is_omap1()) {
  1722. base = OMAP1_DMA_BASE;
  1723. dma_lch_count = OMAP1_LOGICAL_DMA_CH_COUNT;
  1724. } else if (cpu_is_omap24xx()) {
  1725. base = OMAP24XX_DMA4_BASE;
  1726. dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
  1727. } else if (cpu_is_omap34xx()) {
  1728. base = OMAP34XX_DMA4_BASE;
  1729. dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
  1730. } else if (cpu_is_omap44xx()) {
  1731. base = OMAP44XX_DMA4_BASE;
  1732. dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
  1733. } else {
  1734. pr_err("DMA init failed for unsupported omap\n");
  1735. return -ENODEV;
  1736. }
  1737. omap_dma_base = ioremap(base, SZ_4K);
  1738. BUG_ON(!omap_dma_base);
  1739. if (cpu_class_is_omap2() && omap_dma_reserve_channels
  1740. && (omap_dma_reserve_channels <= dma_lch_count))
  1741. dma_lch_count = omap_dma_reserve_channels;
  1742. dma_chan = kzalloc(sizeof(struct omap_dma_lch) * dma_lch_count,
  1743. GFP_KERNEL);
  1744. if (!dma_chan) {
  1745. r = -ENOMEM;
  1746. goto out_unmap;
  1747. }
  1748. if (cpu_class_is_omap2()) {
  1749. dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
  1750. dma_lch_count, GFP_KERNEL);
  1751. if (!dma_linked_lch) {
  1752. r = -ENOMEM;
  1753. goto out_free;
  1754. }
  1755. }
  1756. if (cpu_is_omap15xx()) {
  1757. printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
  1758. dma_chan_count = 9;
  1759. enable_1510_mode = 1;
  1760. } else if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
  1761. printk(KERN_INFO "OMAP DMA hardware version %d\n",
  1762. dma_read(HW_ID));
  1763. printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
  1764. (dma_read(CAPS_0_U) << 16) |
  1765. dma_read(CAPS_0_L),
  1766. (dma_read(CAPS_1_U) << 16) |
  1767. dma_read(CAPS_1_L),
  1768. dma_read(CAPS_2), dma_read(CAPS_3),
  1769. dma_read(CAPS_4));
  1770. if (!enable_1510_mode) {
  1771. u16 w;
  1772. /* Disable OMAP 3.0/3.1 compatibility mode. */
  1773. w = dma_read(GSCR);
  1774. w |= 1 << 3;
  1775. dma_write(w, GSCR);
  1776. dma_chan_count = 16;
  1777. } else
  1778. dma_chan_count = 9;
  1779. } else if (cpu_class_is_omap2()) {
  1780. u8 revision = dma_read(REVISION) & 0xff;
  1781. printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
  1782. revision >> 4, revision & 0xf);
  1783. dma_chan_count = dma_lch_count;
  1784. } else {
  1785. dma_chan_count = 0;
  1786. return 0;
  1787. }
  1788. spin_lock_init(&dma_chan_lock);
  1789. for (ch = 0; ch < dma_chan_count; ch++) {
  1790. omap_clear_dma(ch);
  1791. if (cpu_class_is_omap2())
  1792. omap2_disable_irq_lch(ch);
  1793. dma_chan[ch].dev_id = -1;
  1794. dma_chan[ch].next_lch = -1;
  1795. if (ch >= 6 && enable_1510_mode)
  1796. continue;
  1797. if (cpu_class_is_omap1()) {
  1798. /*
  1799. * request_irq() doesn't like dev_id (ie. ch) being
  1800. * zero, so we have to kludge around this.
  1801. */
  1802. r = request_irq(omap1_dma_irq[ch],
  1803. omap1_dma_irq_handler, 0, "DMA",
  1804. (void *) (ch + 1));
  1805. if (r != 0) {
  1806. int i;
  1807. printk(KERN_ERR "unable to request IRQ %d "
  1808. "for DMA (error %d)\n",
  1809. omap1_dma_irq[ch], r);
  1810. for (i = 0; i < ch; i++)
  1811. free_irq(omap1_dma_irq[i],
  1812. (void *) (i + 1));
  1813. goto out_free;
  1814. }
  1815. }
  1816. }
  1817. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
  1818. omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
  1819. DMA_DEFAULT_FIFO_DEPTH, 0);
  1820. if (cpu_class_is_omap2()) {
  1821. int irq;
  1822. if (cpu_is_omap44xx())
  1823. irq = OMAP44XX_IRQ_SDMA_0;
  1824. else
  1825. irq = INT_24XX_SDMA_IRQ0;
  1826. setup_irq(irq, &omap24xx_dma_irq);
  1827. }
  1828. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  1829. /* Enable smartidle idlemodes and autoidle */
  1830. u32 v = dma_read(OCP_SYSCONFIG);
  1831. v &= ~(DMA_SYSCONFIG_MIDLEMODE_MASK |
  1832. DMA_SYSCONFIG_SIDLEMODE_MASK |
  1833. DMA_SYSCONFIG_AUTOIDLE);
  1834. v |= (DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_SMARTIDLE) |
  1835. DMA_SYSCONFIG_SIDLEMODE(DMA_IDLEMODE_SMARTIDLE) |
  1836. DMA_SYSCONFIG_AUTOIDLE);
  1837. dma_write(v , OCP_SYSCONFIG);
  1838. /* reserve dma channels 0 and 1 in high security devices */
  1839. if (cpu_is_omap34xx() &&
  1840. (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
  1841. printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
  1842. "HS ROM code\n");
  1843. dma_chan[0].dev_id = 0;
  1844. dma_chan[1].dev_id = 1;
  1845. }
  1846. }
  1847. return 0;
  1848. out_free:
  1849. kfree(dma_chan);
  1850. out_unmap:
  1851. iounmap(omap_dma_base);
  1852. return r;
  1853. }
  1854. arch_initcall(omap_init_dma);
  1855. /*
  1856. * Reserve the omap SDMA channels using cmdline bootarg
  1857. * "omap_dma_reserve_ch=". The valid range is 1 to 32
  1858. */
  1859. static int __init omap_dma_cmdline_reserve_ch(char *str)
  1860. {
  1861. if (get_option(&str, &omap_dma_reserve_channels) != 1)
  1862. omap_dma_reserve_channels = 0;
  1863. return 1;
  1864. }
  1865. __setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);