dma.c 52 KB

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