dma.c 51 KB

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