dma.c 55 KB

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