dma.c 50 KB

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