dma.c 51 KB

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