dma.c 55 KB

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