dma.c 51 KB

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