dma.c 51 KB

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