dma.c 51 KB

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