dma.c 52 KB

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