dma.c 52 KB

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