dma.c 52 KB

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