dma.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  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. /*
  768. * As dma_write() uses IO accessors which are weakly ordered, there
  769. * is no guarantee that data in coherent DMA memory will be visible
  770. * to the DMA device. Add a memory barrier here to ensure that any
  771. * such data is visible prior to enabling DMA.
  772. */
  773. mb();
  774. p->dma_write(l, CCR, lch);
  775. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  776. }
  777. EXPORT_SYMBOL(omap_start_dma);
  778. void omap_stop_dma(int lch)
  779. {
  780. u32 l;
  781. /* Disable all interrupts on the channel */
  782. if (cpu_class_is_omap1())
  783. p->dma_write(0, CICR, lch);
  784. l = p->dma_read(CCR, lch);
  785. if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
  786. (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
  787. int i = 0;
  788. u32 sys_cf;
  789. /* Configure No-Standby */
  790. l = p->dma_read(OCP_SYSCONFIG, lch);
  791. sys_cf = l;
  792. l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
  793. l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
  794. p->dma_write(l , OCP_SYSCONFIG, 0);
  795. l = p->dma_read(CCR, lch);
  796. l &= ~OMAP_DMA_CCR_EN;
  797. p->dma_write(l, CCR, lch);
  798. /* Wait for sDMA FIFO drain */
  799. l = p->dma_read(CCR, lch);
  800. while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
  801. OMAP_DMA_CCR_WR_ACTIVE))) {
  802. udelay(5);
  803. i++;
  804. l = p->dma_read(CCR, lch);
  805. }
  806. if (i >= 100)
  807. printk(KERN_ERR "DMA drain did not complete on "
  808. "lch %d\n", lch);
  809. /* Restore OCP_SYSCONFIG */
  810. p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
  811. } else {
  812. l &= ~OMAP_DMA_CCR_EN;
  813. p->dma_write(l, CCR, lch);
  814. }
  815. /*
  816. * Ensure that data transferred by DMA is visible to any access
  817. * after DMA has been disabled. This is important for coherent
  818. * DMA regions.
  819. */
  820. mb();
  821. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  822. int next_lch, cur_lch = lch;
  823. char dma_chan_link_map[dma_lch_count];
  824. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  825. do {
  826. /* The loop case: we've been here already */
  827. if (dma_chan_link_map[cur_lch])
  828. break;
  829. /* Mark the current channel */
  830. dma_chan_link_map[cur_lch] = 1;
  831. disable_lnk(cur_lch);
  832. next_lch = dma_chan[cur_lch].next_lch;
  833. cur_lch = next_lch;
  834. } while (next_lch != -1);
  835. }
  836. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  837. }
  838. EXPORT_SYMBOL(omap_stop_dma);
  839. /*
  840. * Allows changing the DMA callback function or data. This may be needed if
  841. * the driver shares a single DMA channel for multiple dma triggers.
  842. */
  843. int omap_set_dma_callback(int lch,
  844. void (*callback)(int lch, u16 ch_status, void *data),
  845. void *data)
  846. {
  847. unsigned long flags;
  848. if (lch < 0)
  849. return -ENODEV;
  850. spin_lock_irqsave(&dma_chan_lock, flags);
  851. if (dma_chan[lch].dev_id == -1) {
  852. printk(KERN_ERR "DMA callback for not set for free channel\n");
  853. spin_unlock_irqrestore(&dma_chan_lock, flags);
  854. return -EINVAL;
  855. }
  856. dma_chan[lch].callback = callback;
  857. dma_chan[lch].data = data;
  858. spin_unlock_irqrestore(&dma_chan_lock, flags);
  859. return 0;
  860. }
  861. EXPORT_SYMBOL(omap_set_dma_callback);
  862. /*
  863. * Returns current physical source address for the given DMA channel.
  864. * If the channel is running the caller must disable interrupts prior calling
  865. * this function and process the returned value before re-enabling interrupt to
  866. * prevent races with the interrupt handler. Note that in continuous mode there
  867. * is a chance for CSSA_L register overflow between the two reads resulting
  868. * in incorrect return value.
  869. */
  870. dma_addr_t omap_get_dma_src_pos(int lch)
  871. {
  872. dma_addr_t offset = 0;
  873. if (cpu_is_omap15xx())
  874. offset = p->dma_read(CPC, lch);
  875. else
  876. offset = p->dma_read(CSAC, lch);
  877. if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
  878. offset = p->dma_read(CSAC, lch);
  879. if (!cpu_is_omap15xx()) {
  880. /*
  881. * CDAC == 0 indicates that the DMA transfer on the channel has
  882. * not been started (no data has been transferred so far).
  883. * Return the programmed source start address in this case.
  884. */
  885. if (likely(p->dma_read(CDAC, lch)))
  886. offset = p->dma_read(CSAC, lch);
  887. else
  888. offset = p->dma_read(CSSA, lch);
  889. }
  890. if (cpu_class_is_omap1())
  891. offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
  892. return offset;
  893. }
  894. EXPORT_SYMBOL(omap_get_dma_src_pos);
  895. /*
  896. * Returns current physical destination address for the given DMA channel.
  897. * If the channel is running the caller must disable interrupts prior calling
  898. * this function and process the returned value before re-enabling interrupt to
  899. * prevent races with the interrupt handler. Note that in continuous mode there
  900. * is a chance for CDSA_L register overflow between the two reads resulting
  901. * in incorrect return value.
  902. */
  903. dma_addr_t omap_get_dma_dst_pos(int lch)
  904. {
  905. dma_addr_t offset = 0;
  906. if (cpu_is_omap15xx())
  907. offset = p->dma_read(CPC, lch);
  908. else
  909. offset = p->dma_read(CDAC, lch);
  910. /*
  911. * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  912. * read before the DMA controller finished disabling the channel.
  913. */
  914. if (!cpu_is_omap15xx() && offset == 0) {
  915. offset = p->dma_read(CDAC, lch);
  916. /*
  917. * CDAC == 0 indicates that the DMA transfer on the channel has
  918. * not been started (no data has been transferred so far).
  919. * Return the programmed destination start address in this case.
  920. */
  921. if (unlikely(!offset))
  922. offset = p->dma_read(CDSA, lch);
  923. }
  924. if (cpu_class_is_omap1())
  925. offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
  926. return offset;
  927. }
  928. EXPORT_SYMBOL(omap_get_dma_dst_pos);
  929. int omap_get_dma_active_status(int lch)
  930. {
  931. return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
  932. }
  933. EXPORT_SYMBOL(omap_get_dma_active_status);
  934. int omap_dma_running(void)
  935. {
  936. int lch;
  937. if (cpu_class_is_omap1())
  938. if (omap_lcd_dma_running())
  939. return 1;
  940. for (lch = 0; lch < dma_chan_count; lch++)
  941. if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
  942. return 1;
  943. return 0;
  944. }
  945. /*
  946. * lch_queue DMA will start right after lch_head one is finished.
  947. * For this DMA link to start, you still need to start (see omap_start_dma)
  948. * the first one. That will fire up the entire queue.
  949. */
  950. void omap_dma_link_lch(int lch_head, int lch_queue)
  951. {
  952. if (omap_dma_in_1510_mode()) {
  953. if (lch_head == lch_queue) {
  954. p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
  955. CCR, lch_head);
  956. return;
  957. }
  958. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  959. BUG();
  960. return;
  961. }
  962. if ((dma_chan[lch_head].dev_id == -1) ||
  963. (dma_chan[lch_queue].dev_id == -1)) {
  964. printk(KERN_ERR "omap_dma: trying to link "
  965. "non requested channels\n");
  966. dump_stack();
  967. }
  968. dma_chan[lch_head].next_lch = lch_queue;
  969. }
  970. EXPORT_SYMBOL(omap_dma_link_lch);
  971. /*
  972. * Once the DMA queue is stopped, we can destroy it.
  973. */
  974. void omap_dma_unlink_lch(int lch_head, int lch_queue)
  975. {
  976. if (omap_dma_in_1510_mode()) {
  977. if (lch_head == lch_queue) {
  978. p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
  979. CCR, lch_head);
  980. return;
  981. }
  982. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  983. BUG();
  984. return;
  985. }
  986. if (dma_chan[lch_head].next_lch != lch_queue ||
  987. dma_chan[lch_head].next_lch == -1) {
  988. printk(KERN_ERR "omap_dma: trying to unlink "
  989. "non linked channels\n");
  990. dump_stack();
  991. }
  992. if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
  993. (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
  994. printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
  995. "before unlinking\n");
  996. dump_stack();
  997. }
  998. dma_chan[lch_head].next_lch = -1;
  999. }
  1000. EXPORT_SYMBOL(omap_dma_unlink_lch);
  1001. #ifndef CONFIG_ARCH_OMAP1
  1002. /* Create chain of DMA channesls */
  1003. static void create_dma_lch_chain(int lch_head, int lch_queue)
  1004. {
  1005. u32 l;
  1006. /* Check if this is the first link in chain */
  1007. if (dma_chan[lch_head].next_linked_ch == -1) {
  1008. dma_chan[lch_head].next_linked_ch = lch_queue;
  1009. dma_chan[lch_head].prev_linked_ch = lch_queue;
  1010. dma_chan[lch_queue].next_linked_ch = lch_head;
  1011. dma_chan[lch_queue].prev_linked_ch = lch_head;
  1012. }
  1013. /* a link exists, link the new channel in circular chain */
  1014. else {
  1015. dma_chan[lch_queue].next_linked_ch =
  1016. dma_chan[lch_head].next_linked_ch;
  1017. dma_chan[lch_queue].prev_linked_ch = lch_head;
  1018. dma_chan[lch_head].next_linked_ch = lch_queue;
  1019. dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
  1020. lch_queue;
  1021. }
  1022. l = p->dma_read(CLNK_CTRL, lch_head);
  1023. l &= ~(0x1f);
  1024. l |= lch_queue;
  1025. p->dma_write(l, CLNK_CTRL, lch_head);
  1026. l = p->dma_read(CLNK_CTRL, lch_queue);
  1027. l &= ~(0x1f);
  1028. l |= (dma_chan[lch_queue].next_linked_ch);
  1029. p->dma_write(l, CLNK_CTRL, lch_queue);
  1030. }
  1031. /**
  1032. * @brief omap_request_dma_chain : Request a chain of DMA channels
  1033. *
  1034. * @param dev_id - Device id using the dma channel
  1035. * @param dev_name - Device name
  1036. * @param callback - Call back function
  1037. * @chain_id -
  1038. * @no_of_chans - Number of channels requested
  1039. * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
  1040. * OMAP_DMA_DYNAMIC_CHAIN
  1041. * @params - Channel parameters
  1042. *
  1043. * @return - Success : 0
  1044. * Failure: -EINVAL/-ENOMEM
  1045. */
  1046. int omap_request_dma_chain(int dev_id, const char *dev_name,
  1047. void (*callback) (int lch, u16 ch_status,
  1048. void *data),
  1049. int *chain_id, int no_of_chans, int chain_mode,
  1050. struct omap_dma_channel_params params)
  1051. {
  1052. int *channels;
  1053. int i, err;
  1054. /* Is the chain mode valid ? */
  1055. if (chain_mode != OMAP_DMA_STATIC_CHAIN
  1056. && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
  1057. printk(KERN_ERR "Invalid chain mode requested\n");
  1058. return -EINVAL;
  1059. }
  1060. if (unlikely((no_of_chans < 1
  1061. || no_of_chans > dma_lch_count))) {
  1062. printk(KERN_ERR "Invalid Number of channels requested\n");
  1063. return -EINVAL;
  1064. }
  1065. /*
  1066. * Allocate a queue to maintain the status of the channels
  1067. * in the chain
  1068. */
  1069. channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
  1070. if (channels == NULL) {
  1071. printk(KERN_ERR "omap_dma: No memory for channel queue\n");
  1072. return -ENOMEM;
  1073. }
  1074. /* request and reserve DMA channels for the chain */
  1075. for (i = 0; i < no_of_chans; i++) {
  1076. err = omap_request_dma(dev_id, dev_name,
  1077. callback, NULL, &channels[i]);
  1078. if (err < 0) {
  1079. int j;
  1080. for (j = 0; j < i; j++)
  1081. omap_free_dma(channels[j]);
  1082. kfree(channels);
  1083. printk(KERN_ERR "omap_dma: Request failed %d\n", err);
  1084. return err;
  1085. }
  1086. dma_chan[channels[i]].prev_linked_ch = -1;
  1087. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1088. /*
  1089. * Allowing client drivers to set common parameters now,
  1090. * so that later only relevant (src_start, dest_start
  1091. * and element count) can be set
  1092. */
  1093. omap_set_dma_params(channels[i], &params);
  1094. }
  1095. *chain_id = channels[0];
  1096. dma_linked_lch[*chain_id].linked_dmach_q = channels;
  1097. dma_linked_lch[*chain_id].chain_mode = chain_mode;
  1098. dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
  1099. dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
  1100. for (i = 0; i < no_of_chans; i++)
  1101. dma_chan[channels[i]].chain_id = *chain_id;
  1102. /* Reset the Queue pointers */
  1103. OMAP_DMA_CHAIN_QINIT(*chain_id);
  1104. /* Set up the chain */
  1105. if (no_of_chans == 1)
  1106. create_dma_lch_chain(channels[0], channels[0]);
  1107. else {
  1108. for (i = 0; i < (no_of_chans - 1); i++)
  1109. create_dma_lch_chain(channels[i], channels[i + 1]);
  1110. }
  1111. return 0;
  1112. }
  1113. EXPORT_SYMBOL(omap_request_dma_chain);
  1114. /**
  1115. * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
  1116. * params after setting it. Dont do this while dma is running!!
  1117. *
  1118. * @param chain_id - Chained logical channel id.
  1119. * @param params
  1120. *
  1121. * @return - Success : 0
  1122. * Failure : -EINVAL
  1123. */
  1124. int omap_modify_dma_chain_params(int chain_id,
  1125. struct omap_dma_channel_params params)
  1126. {
  1127. int *channels;
  1128. u32 i;
  1129. /* Check for input params */
  1130. if (unlikely((chain_id < 0
  1131. || chain_id >= dma_lch_count))) {
  1132. printk(KERN_ERR "Invalid chain id\n");
  1133. return -EINVAL;
  1134. }
  1135. /* Check if the chain exists */
  1136. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1137. printk(KERN_ERR "Chain doesn't exists\n");
  1138. return -EINVAL;
  1139. }
  1140. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1141. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1142. /*
  1143. * Allowing client drivers to set common parameters now,
  1144. * so that later only relevant (src_start, dest_start
  1145. * and element count) can be set
  1146. */
  1147. omap_set_dma_params(channels[i], &params);
  1148. }
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL(omap_modify_dma_chain_params);
  1152. /**
  1153. * @brief omap_free_dma_chain - Free all the logical channels in a chain.
  1154. *
  1155. * @param chain_id
  1156. *
  1157. * @return - Success : 0
  1158. * Failure : -EINVAL
  1159. */
  1160. int omap_free_dma_chain(int chain_id)
  1161. {
  1162. int *channels;
  1163. u32 i;
  1164. /* Check for input params */
  1165. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1166. printk(KERN_ERR "Invalid chain id\n");
  1167. return -EINVAL;
  1168. }
  1169. /* Check if the chain exists */
  1170. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1171. printk(KERN_ERR "Chain doesn't exists\n");
  1172. return -EINVAL;
  1173. }
  1174. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1175. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1176. dma_chan[channels[i]].next_linked_ch = -1;
  1177. dma_chan[channels[i]].prev_linked_ch = -1;
  1178. dma_chan[channels[i]].chain_id = -1;
  1179. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1180. omap_free_dma(channels[i]);
  1181. }
  1182. kfree(channels);
  1183. dma_linked_lch[chain_id].linked_dmach_q = NULL;
  1184. dma_linked_lch[chain_id].chain_mode = -1;
  1185. dma_linked_lch[chain_id].chain_state = -1;
  1186. return (0);
  1187. }
  1188. EXPORT_SYMBOL(omap_free_dma_chain);
  1189. /**
  1190. * @brief omap_dma_chain_status - Check if the chain is in
  1191. * active / inactive state.
  1192. * @param chain_id
  1193. *
  1194. * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
  1195. * Failure : -EINVAL
  1196. */
  1197. int omap_dma_chain_status(int chain_id)
  1198. {
  1199. /* Check for input params */
  1200. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1201. printk(KERN_ERR "Invalid chain id\n");
  1202. return -EINVAL;
  1203. }
  1204. /* Check if the chain exists */
  1205. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1206. printk(KERN_ERR "Chain doesn't exists\n");
  1207. return -EINVAL;
  1208. }
  1209. pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
  1210. dma_linked_lch[chain_id].q_count);
  1211. if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
  1212. return OMAP_DMA_CHAIN_INACTIVE;
  1213. return OMAP_DMA_CHAIN_ACTIVE;
  1214. }
  1215. EXPORT_SYMBOL(omap_dma_chain_status);
  1216. /**
  1217. * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
  1218. * set the params and start the transfer.
  1219. *
  1220. * @param chain_id
  1221. * @param src_start - buffer start address
  1222. * @param dest_start - Dest address
  1223. * @param elem_count
  1224. * @param frame_count
  1225. * @param callbk_data - channel callback parameter data.
  1226. *
  1227. * @return - Success : 0
  1228. * Failure: -EINVAL/-EBUSY
  1229. */
  1230. int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
  1231. int elem_count, int frame_count, void *callbk_data)
  1232. {
  1233. int *channels;
  1234. u32 l, lch;
  1235. int start_dma = 0;
  1236. /*
  1237. * if buffer size is less than 1 then there is
  1238. * no use of starting the chain
  1239. */
  1240. if (elem_count < 1) {
  1241. printk(KERN_ERR "Invalid buffer size\n");
  1242. return -EINVAL;
  1243. }
  1244. /* Check for input params */
  1245. if (unlikely((chain_id < 0
  1246. || chain_id >= dma_lch_count))) {
  1247. printk(KERN_ERR "Invalid chain id\n");
  1248. return -EINVAL;
  1249. }
  1250. /* Check if the chain exists */
  1251. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1252. printk(KERN_ERR "Chain doesn't exist\n");
  1253. return -EINVAL;
  1254. }
  1255. /* Check if all the channels in chain are in use */
  1256. if (OMAP_DMA_CHAIN_QFULL(chain_id))
  1257. return -EBUSY;
  1258. /* Frame count may be negative in case of indexed transfers */
  1259. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1260. /* Get a free channel */
  1261. lch = channels[dma_linked_lch[chain_id].q_tail];
  1262. /* Store the callback data */
  1263. dma_chan[lch].data = callbk_data;
  1264. /* Increment the q_tail */
  1265. OMAP_DMA_CHAIN_INCQTAIL(chain_id);
  1266. /* Set the params to the free channel */
  1267. if (src_start != 0)
  1268. p->dma_write(src_start, CSSA, lch);
  1269. if (dest_start != 0)
  1270. p->dma_write(dest_start, CDSA, lch);
  1271. /* Write the buffer size */
  1272. p->dma_write(elem_count, CEN, lch);
  1273. p->dma_write(frame_count, CFN, lch);
  1274. /*
  1275. * If the chain is dynamically linked,
  1276. * then we may have to start the chain if its not active
  1277. */
  1278. if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
  1279. /*
  1280. * In Dynamic chain, if the chain is not started,
  1281. * queue the channel
  1282. */
  1283. if (dma_linked_lch[chain_id].chain_state ==
  1284. DMA_CHAIN_NOTSTARTED) {
  1285. /* Enable the link in previous channel */
  1286. if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
  1287. DMA_CH_QUEUED)
  1288. enable_lnk(dma_chan[lch].prev_linked_ch);
  1289. dma_chan[lch].state = DMA_CH_QUEUED;
  1290. }
  1291. /*
  1292. * Chain is already started, make sure its active,
  1293. * if not then start the chain
  1294. */
  1295. else {
  1296. start_dma = 1;
  1297. if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
  1298. DMA_CH_STARTED) {
  1299. enable_lnk(dma_chan[lch].prev_linked_ch);
  1300. dma_chan[lch].state = DMA_CH_QUEUED;
  1301. start_dma = 0;
  1302. if (0 == ((1 << 7) & p->dma_read(
  1303. CCR, dma_chan[lch].prev_linked_ch))) {
  1304. disable_lnk(dma_chan[lch].
  1305. prev_linked_ch);
  1306. pr_debug("\n prev ch is stopped\n");
  1307. start_dma = 1;
  1308. }
  1309. }
  1310. else if (dma_chan[dma_chan[lch].prev_linked_ch].state
  1311. == DMA_CH_QUEUED) {
  1312. enable_lnk(dma_chan[lch].prev_linked_ch);
  1313. dma_chan[lch].state = DMA_CH_QUEUED;
  1314. start_dma = 0;
  1315. }
  1316. omap_enable_channel_irq(lch);
  1317. l = p->dma_read(CCR, lch);
  1318. if ((0 == (l & (1 << 24))))
  1319. l &= ~(1 << 25);
  1320. else
  1321. l |= (1 << 25);
  1322. if (start_dma == 1) {
  1323. if (0 == (l & (1 << 7))) {
  1324. l |= (1 << 7);
  1325. dma_chan[lch].state = DMA_CH_STARTED;
  1326. pr_debug("starting %d\n", lch);
  1327. p->dma_write(l, CCR, lch);
  1328. } else
  1329. start_dma = 0;
  1330. } else {
  1331. if (0 == (l & (1 << 7)))
  1332. p->dma_write(l, CCR, lch);
  1333. }
  1334. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  1335. }
  1336. }
  1337. return 0;
  1338. }
  1339. EXPORT_SYMBOL(omap_dma_chain_a_transfer);
  1340. /**
  1341. * @brief omap_start_dma_chain_transfers - Start the chain
  1342. *
  1343. * @param chain_id
  1344. *
  1345. * @return - Success : 0
  1346. * Failure : -EINVAL/-EBUSY
  1347. */
  1348. int omap_start_dma_chain_transfers(int chain_id)
  1349. {
  1350. int *channels;
  1351. u32 l, i;
  1352. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1353. printk(KERN_ERR "Invalid chain id\n");
  1354. return -EINVAL;
  1355. }
  1356. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1357. if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
  1358. printk(KERN_ERR "Chain is already started\n");
  1359. return -EBUSY;
  1360. }
  1361. if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
  1362. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
  1363. i++) {
  1364. enable_lnk(channels[i]);
  1365. omap_enable_channel_irq(channels[i]);
  1366. }
  1367. } else {
  1368. omap_enable_channel_irq(channels[0]);
  1369. }
  1370. l = p->dma_read(CCR, channels[0]);
  1371. l |= (1 << 7);
  1372. dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
  1373. dma_chan[channels[0]].state = DMA_CH_STARTED;
  1374. if ((0 == (l & (1 << 24))))
  1375. l &= ~(1 << 25);
  1376. else
  1377. l |= (1 << 25);
  1378. p->dma_write(l, CCR, channels[0]);
  1379. dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
  1380. return 0;
  1381. }
  1382. EXPORT_SYMBOL(omap_start_dma_chain_transfers);
  1383. /**
  1384. * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
  1385. *
  1386. * @param chain_id
  1387. *
  1388. * @return - Success : 0
  1389. * Failure : EINVAL
  1390. */
  1391. int omap_stop_dma_chain_transfers(int chain_id)
  1392. {
  1393. int *channels;
  1394. u32 l, i;
  1395. u32 sys_cf = 0;
  1396. /* Check for input params */
  1397. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1398. printk(KERN_ERR "Invalid chain id\n");
  1399. return -EINVAL;
  1400. }
  1401. /* Check if the chain exists */
  1402. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1403. printk(KERN_ERR "Chain doesn't exists\n");
  1404. return -EINVAL;
  1405. }
  1406. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1407. if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
  1408. sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
  1409. l = sys_cf;
  1410. /* Middle mode reg set no Standby */
  1411. l &= ~((1 << 12)|(1 << 13));
  1412. p->dma_write(l, OCP_SYSCONFIG, 0);
  1413. }
  1414. for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
  1415. /* Stop the Channel transmission */
  1416. l = p->dma_read(CCR, channels[i]);
  1417. l &= ~(1 << 7);
  1418. p->dma_write(l, CCR, channels[i]);
  1419. /* Disable the link in all the channels */
  1420. disable_lnk(channels[i]);
  1421. dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
  1422. }
  1423. dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
  1424. /* Reset the Queue pointers */
  1425. OMAP_DMA_CHAIN_QINIT(chain_id);
  1426. if (IS_DMA_ERRATA(DMA_ERRATA_i88))
  1427. p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
  1428. return 0;
  1429. }
  1430. EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
  1431. /* Get the index of the ongoing DMA in chain */
  1432. /**
  1433. * @brief omap_get_dma_chain_index - Get the element and frame index
  1434. * of the ongoing DMA in chain
  1435. *
  1436. * @param chain_id
  1437. * @param ei - Element index
  1438. * @param fi - Frame index
  1439. *
  1440. * @return - Success : 0
  1441. * Failure : -EINVAL
  1442. */
  1443. int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
  1444. {
  1445. int lch;
  1446. int *channels;
  1447. /* Check for input params */
  1448. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1449. printk(KERN_ERR "Invalid chain id\n");
  1450. return -EINVAL;
  1451. }
  1452. /* Check if the chain exists */
  1453. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1454. printk(KERN_ERR "Chain doesn't exists\n");
  1455. return -EINVAL;
  1456. }
  1457. if ((!ei) || (!fi))
  1458. return -EINVAL;
  1459. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1460. /* Get the current channel */
  1461. lch = channels[dma_linked_lch[chain_id].q_head];
  1462. *ei = p->dma_read(CCEN, lch);
  1463. *fi = p->dma_read(CCFN, lch);
  1464. return 0;
  1465. }
  1466. EXPORT_SYMBOL(omap_get_dma_chain_index);
  1467. /**
  1468. * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
  1469. * ongoing DMA in chain
  1470. *
  1471. * @param chain_id
  1472. *
  1473. * @return - Success : Destination position
  1474. * Failure : -EINVAL
  1475. */
  1476. int omap_get_dma_chain_dst_pos(int chain_id)
  1477. {
  1478. int lch;
  1479. int *channels;
  1480. /* Check for input params */
  1481. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1482. printk(KERN_ERR "Invalid chain id\n");
  1483. return -EINVAL;
  1484. }
  1485. /* Check if the chain exists */
  1486. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1487. printk(KERN_ERR "Chain doesn't exists\n");
  1488. return -EINVAL;
  1489. }
  1490. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1491. /* Get the current channel */
  1492. lch = channels[dma_linked_lch[chain_id].q_head];
  1493. return p->dma_read(CDAC, lch);
  1494. }
  1495. EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
  1496. /**
  1497. * @brief omap_get_dma_chain_src_pos - Get the source position
  1498. * of the ongoing DMA in chain
  1499. * @param chain_id
  1500. *
  1501. * @return - Success : Destination position
  1502. * Failure : -EINVAL
  1503. */
  1504. int omap_get_dma_chain_src_pos(int chain_id)
  1505. {
  1506. int lch;
  1507. int *channels;
  1508. /* Check for input params */
  1509. if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
  1510. printk(KERN_ERR "Invalid chain id\n");
  1511. return -EINVAL;
  1512. }
  1513. /* Check if the chain exists */
  1514. if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
  1515. printk(KERN_ERR "Chain doesn't exists\n");
  1516. return -EINVAL;
  1517. }
  1518. channels = dma_linked_lch[chain_id].linked_dmach_q;
  1519. /* Get the current channel */
  1520. lch = channels[dma_linked_lch[chain_id].q_head];
  1521. return p->dma_read(CSAC, lch);
  1522. }
  1523. EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
  1524. #endif /* ifndef CONFIG_ARCH_OMAP1 */
  1525. /*----------------------------------------------------------------------------*/
  1526. #ifdef CONFIG_ARCH_OMAP1
  1527. static int omap1_dma_handle_ch(int ch)
  1528. {
  1529. u32 csr;
  1530. if (enable_1510_mode && ch >= 6) {
  1531. csr = dma_chan[ch].saved_csr;
  1532. dma_chan[ch].saved_csr = 0;
  1533. } else
  1534. csr = p->dma_read(CSR, ch);
  1535. if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
  1536. dma_chan[ch + 6].saved_csr = csr >> 7;
  1537. csr &= 0x7f;
  1538. }
  1539. if ((csr & 0x3f) == 0)
  1540. return 0;
  1541. if (unlikely(dma_chan[ch].dev_id == -1)) {
  1542. printk(KERN_WARNING "Spurious interrupt from DMA channel "
  1543. "%d (CSR %04x)\n", ch, csr);
  1544. return 0;
  1545. }
  1546. if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
  1547. printk(KERN_WARNING "DMA timeout with device %d\n",
  1548. dma_chan[ch].dev_id);
  1549. if (unlikely(csr & OMAP_DMA_DROP_IRQ))
  1550. printk(KERN_WARNING "DMA synchronization event drop occurred "
  1551. "with device %d\n", dma_chan[ch].dev_id);
  1552. if (likely(csr & OMAP_DMA_BLOCK_IRQ))
  1553. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  1554. if (likely(dma_chan[ch].callback != NULL))
  1555. dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
  1556. return 1;
  1557. }
  1558. static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
  1559. {
  1560. int ch = ((int) dev_id) - 1;
  1561. int handled = 0;
  1562. for (;;) {
  1563. int handled_now = 0;
  1564. handled_now += omap1_dma_handle_ch(ch);
  1565. if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
  1566. handled_now += omap1_dma_handle_ch(ch + 6);
  1567. if (!handled_now)
  1568. break;
  1569. handled += handled_now;
  1570. }
  1571. return handled ? IRQ_HANDLED : IRQ_NONE;
  1572. }
  1573. #else
  1574. #define omap1_dma_irq_handler NULL
  1575. #endif
  1576. #ifdef CONFIG_ARCH_OMAP2PLUS
  1577. static int omap2_dma_handle_ch(int ch)
  1578. {
  1579. u32 status = p->dma_read(CSR, ch);
  1580. if (!status) {
  1581. if (printk_ratelimit())
  1582. printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
  1583. ch);
  1584. p->dma_write(1 << ch, IRQSTATUS_L0, ch);
  1585. return 0;
  1586. }
  1587. if (unlikely(dma_chan[ch].dev_id == -1)) {
  1588. if (printk_ratelimit())
  1589. printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
  1590. "channel %d\n", status, ch);
  1591. return 0;
  1592. }
  1593. if (unlikely(status & OMAP_DMA_DROP_IRQ))
  1594. printk(KERN_INFO
  1595. "DMA synchronization event drop occurred with device "
  1596. "%d\n", dma_chan[ch].dev_id);
  1597. if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
  1598. printk(KERN_INFO "DMA transaction error with device %d\n",
  1599. dma_chan[ch].dev_id);
  1600. if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
  1601. u32 ccr;
  1602. ccr = p->dma_read(CCR, ch);
  1603. ccr &= ~OMAP_DMA_CCR_EN;
  1604. p->dma_write(ccr, CCR, ch);
  1605. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  1606. }
  1607. }
  1608. if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
  1609. printk(KERN_INFO "DMA secure error with device %d\n",
  1610. dma_chan[ch].dev_id);
  1611. if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
  1612. printk(KERN_INFO "DMA misaligned error with device %d\n",
  1613. dma_chan[ch].dev_id);
  1614. p->dma_write(status, CSR, ch);
  1615. p->dma_write(1 << ch, IRQSTATUS_L0, ch);
  1616. /* read back the register to flush the write */
  1617. p->dma_read(IRQSTATUS_L0, ch);
  1618. /* If the ch is not chained then chain_id will be -1 */
  1619. if (dma_chan[ch].chain_id != -1) {
  1620. int chain_id = dma_chan[ch].chain_id;
  1621. dma_chan[ch].state = DMA_CH_NOTSTARTED;
  1622. if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
  1623. dma_chan[dma_chan[ch].next_linked_ch].state =
  1624. DMA_CH_STARTED;
  1625. if (dma_linked_lch[chain_id].chain_mode ==
  1626. OMAP_DMA_DYNAMIC_CHAIN)
  1627. disable_lnk(ch);
  1628. if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
  1629. OMAP_DMA_CHAIN_INCQHEAD(chain_id);
  1630. status = p->dma_read(CSR, ch);
  1631. p->dma_write(status, CSR, ch);
  1632. }
  1633. if (likely(dma_chan[ch].callback != NULL))
  1634. dma_chan[ch].callback(ch, status, dma_chan[ch].data);
  1635. return 0;
  1636. }
  1637. /* STATUS register count is from 1-32 while our is 0-31 */
  1638. static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
  1639. {
  1640. u32 val, enable_reg;
  1641. int i;
  1642. val = p->dma_read(IRQSTATUS_L0, 0);
  1643. if (val == 0) {
  1644. if (printk_ratelimit())
  1645. printk(KERN_WARNING "Spurious DMA IRQ\n");
  1646. return IRQ_HANDLED;
  1647. }
  1648. enable_reg = p->dma_read(IRQENABLE_L0, 0);
  1649. val &= enable_reg; /* Dispatch only relevant interrupts */
  1650. for (i = 0; i < dma_lch_count && val != 0; i++) {
  1651. if (val & 1)
  1652. omap2_dma_handle_ch(i);
  1653. val >>= 1;
  1654. }
  1655. return IRQ_HANDLED;
  1656. }
  1657. static struct irqaction omap24xx_dma_irq = {
  1658. .name = "DMA",
  1659. .handler = omap2_dma_irq_handler,
  1660. .flags = IRQF_DISABLED
  1661. };
  1662. #else
  1663. static struct irqaction omap24xx_dma_irq;
  1664. #endif
  1665. /*----------------------------------------------------------------------------*/
  1666. void omap_dma_global_context_save(void)
  1667. {
  1668. omap_dma_global_context.dma_irqenable_l0 =
  1669. p->dma_read(IRQENABLE_L0, 0);
  1670. omap_dma_global_context.dma_ocp_sysconfig =
  1671. p->dma_read(OCP_SYSCONFIG, 0);
  1672. omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
  1673. }
  1674. void omap_dma_global_context_restore(void)
  1675. {
  1676. int ch;
  1677. p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
  1678. p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
  1679. OCP_SYSCONFIG, 0);
  1680. p->dma_write(omap_dma_global_context.dma_irqenable_l0,
  1681. IRQENABLE_L0, 0);
  1682. if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
  1683. p->dma_write(0x3 , IRQSTATUS_L0, 0);
  1684. for (ch = 0; ch < dma_chan_count; ch++)
  1685. if (dma_chan[ch].dev_id != -1)
  1686. omap_clear_dma(ch);
  1687. }
  1688. static int __devinit omap_system_dma_probe(struct platform_device *pdev)
  1689. {
  1690. int ch, ret = 0;
  1691. int dma_irq;
  1692. char irq_name[4];
  1693. int irq_rel;
  1694. p = pdev->dev.platform_data;
  1695. if (!p) {
  1696. dev_err(&pdev->dev, "%s: System DMA initialized without"
  1697. "platform data\n", __func__);
  1698. return -EINVAL;
  1699. }
  1700. d = p->dma_attr;
  1701. errata = p->errata;
  1702. if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
  1703. && (omap_dma_reserve_channels <= dma_lch_count))
  1704. d->lch_count = omap_dma_reserve_channels;
  1705. dma_lch_count = d->lch_count;
  1706. dma_chan_count = dma_lch_count;
  1707. dma_chan = d->chan;
  1708. enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
  1709. if (cpu_class_is_omap2()) {
  1710. dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
  1711. dma_lch_count, GFP_KERNEL);
  1712. if (!dma_linked_lch) {
  1713. ret = -ENOMEM;
  1714. goto exit_dma_lch_fail;
  1715. }
  1716. }
  1717. spin_lock_init(&dma_chan_lock);
  1718. for (ch = 0; ch < dma_chan_count; ch++) {
  1719. omap_clear_dma(ch);
  1720. if (cpu_class_is_omap2())
  1721. omap2_disable_irq_lch(ch);
  1722. dma_chan[ch].dev_id = -1;
  1723. dma_chan[ch].next_lch = -1;
  1724. if (ch >= 6 && enable_1510_mode)
  1725. continue;
  1726. if (cpu_class_is_omap1()) {
  1727. /*
  1728. * request_irq() doesn't like dev_id (ie. ch) being
  1729. * zero, so we have to kludge around this.
  1730. */
  1731. sprintf(&irq_name[0], "%d", ch);
  1732. dma_irq = platform_get_irq_byname(pdev, irq_name);
  1733. if (dma_irq < 0) {
  1734. ret = dma_irq;
  1735. goto exit_dma_irq_fail;
  1736. }
  1737. /* INT_DMA_LCD is handled in lcd_dma.c */
  1738. if (dma_irq == INT_DMA_LCD)
  1739. continue;
  1740. ret = request_irq(dma_irq,
  1741. omap1_dma_irq_handler, 0, "DMA",
  1742. (void *) (ch + 1));
  1743. if (ret != 0)
  1744. goto exit_dma_irq_fail;
  1745. }
  1746. }
  1747. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
  1748. omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
  1749. DMA_DEFAULT_FIFO_DEPTH, 0);
  1750. if (cpu_class_is_omap2()) {
  1751. strcpy(irq_name, "0");
  1752. dma_irq = platform_get_irq_byname(pdev, irq_name);
  1753. if (dma_irq < 0) {
  1754. dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
  1755. goto exit_dma_lch_fail;
  1756. }
  1757. ret = setup_irq(dma_irq, &omap24xx_dma_irq);
  1758. if (ret) {
  1759. dev_err(&pdev->dev, "set_up failed for IRQ %d"
  1760. "for DMA (error %d)\n", dma_irq, ret);
  1761. goto exit_dma_lch_fail;
  1762. }
  1763. }
  1764. /* reserve dma channels 0 and 1 in high security devices */
  1765. if (cpu_is_omap34xx() &&
  1766. (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
  1767. printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
  1768. "HS ROM code\n");
  1769. dma_chan[0].dev_id = 0;
  1770. dma_chan[1].dev_id = 1;
  1771. }
  1772. p->show_dma_caps();
  1773. return 0;
  1774. exit_dma_irq_fail:
  1775. dev_err(&pdev->dev, "unable to request IRQ %d"
  1776. "for DMA (error %d)\n", dma_irq, ret);
  1777. for (irq_rel = 0; irq_rel < ch; irq_rel++) {
  1778. dma_irq = platform_get_irq(pdev, irq_rel);
  1779. free_irq(dma_irq, (void *)(irq_rel + 1));
  1780. }
  1781. exit_dma_lch_fail:
  1782. kfree(p);
  1783. kfree(d);
  1784. kfree(dma_chan);
  1785. return ret;
  1786. }
  1787. static int __devexit omap_system_dma_remove(struct platform_device *pdev)
  1788. {
  1789. int dma_irq;
  1790. if (cpu_class_is_omap2()) {
  1791. char irq_name[4];
  1792. strcpy(irq_name, "0");
  1793. dma_irq = platform_get_irq_byname(pdev, irq_name);
  1794. remove_irq(dma_irq, &omap24xx_dma_irq);
  1795. } else {
  1796. int irq_rel = 0;
  1797. for ( ; irq_rel < dma_chan_count; irq_rel++) {
  1798. dma_irq = platform_get_irq(pdev, irq_rel);
  1799. free_irq(dma_irq, (void *)(irq_rel + 1));
  1800. }
  1801. }
  1802. kfree(p);
  1803. kfree(d);
  1804. kfree(dma_chan);
  1805. return 0;
  1806. }
  1807. static struct platform_driver omap_system_dma_driver = {
  1808. .probe = omap_system_dma_probe,
  1809. .remove = __devexit_p(omap_system_dma_remove),
  1810. .driver = {
  1811. .name = "omap_dma_system"
  1812. },
  1813. };
  1814. static int __init omap_system_dma_init(void)
  1815. {
  1816. return platform_driver_register(&omap_system_dma_driver);
  1817. }
  1818. arch_initcall(omap_system_dma_init);
  1819. static void __exit omap_system_dma_exit(void)
  1820. {
  1821. platform_driver_unregister(&omap_system_dma_driver);
  1822. }
  1823. MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
  1824. MODULE_LICENSE("GPL");
  1825. MODULE_ALIAS("platform:" DRIVER_NAME);
  1826. MODULE_AUTHOR("Texas Instruments Inc");
  1827. /*
  1828. * Reserve the omap SDMA channels using cmdline bootarg
  1829. * "omap_dma_reserve_ch=". The valid range is 1 to 32
  1830. */
  1831. static int __init omap_dma_cmdline_reserve_ch(char *str)
  1832. {
  1833. if (get_option(&str, &omap_dma_reserve_channels) != 1)
  1834. omap_dma_reserve_channels = 0;
  1835. return 1;
  1836. }
  1837. __setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);