dma.c 52 KB

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