dma.c 51 KB

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