dma.c 53 KB

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