dma.c 51 KB

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