dma.c 52 KB

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