omap-dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/plat-omap/dma.c
  4. *
  5. * Copyright (C) 2003 - 2008 Nokia Corporation
  6. * Author: Juha Yrjölä <juha.yrjola@nokia.com>
  7. * DMA channel linking for 1610 by Samuel Ortiz <samuel.ortiz@nokia.com>
  8. * Graphics DMA and LCD DMA graphics tranformations
  9. * by Imre Deak <imre.deak@nokia.com>
  10. * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
  11. * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
  12. * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
  13. *
  14. * Copyright (C) 2009 Texas Instruments
  15. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  16. *
  17. * Support functions for the OMAP internal DMA channels.
  18. *
  19. * Copyright (C) 2010 Texas Instruments Incorporated - https://www.ti.com/
  20. * Converted DMA library into DMA platform driver.
  21. * - G, Manjunath Kondaiah <manjugk@ti.com>
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/sched.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/errno.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/irq.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <linux/delay.h>
  33. #include <linux/omap-dma.h>
  34. #include <linux/soc/ti/omap1-io.h>
  35. #include <linux/soc/ti/omap1-soc.h>
  36. #include "tc.h"
  37. /*
  38. * MAX_LOGICAL_DMA_CH_COUNT: the maximum number of logical DMA
  39. * channels that an instance of the SDMA IP block can support. Used
  40. * to size arrays. (The actual maximum on a particular SoC may be less
  41. * than this -- for example, OMAP1 SDMA instances only support 17 logical
  42. * DMA channels.)
  43. */
  44. #define MAX_LOGICAL_DMA_CH_COUNT 32
  45. #undef DEBUG
  46. #define OMAP_DMA_ACTIVE 0x01
  47. #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
  48. static struct omap_system_dma_plat_info *p;
  49. static struct omap_dma_dev_attr *d;
  50. static int enable_1510_mode;
  51. static u32 errata;
  52. static int dma_lch_count;
  53. static int dma_chan_count;
  54. static int omap_dma_reserve_channels;
  55. static DEFINE_SPINLOCK(dma_chan_lock);
  56. static struct omap_dma_lch *dma_chan;
  57. static inline void omap_disable_channel_irq(int lch)
  58. {
  59. /* disable channel interrupts */
  60. p->dma_write(0, CICR, lch);
  61. /* Clear CSR */
  62. p->dma_read(CSR, lch);
  63. }
  64. static inline void set_gdma_dev(int req, int dev)
  65. {
  66. u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
  67. int shift = ((req - 1) % 5) * 6;
  68. u32 l;
  69. l = omap_readl(reg);
  70. l &= ~(0x3f << shift);
  71. l |= (dev - 1) << shift;
  72. omap_writel(l, reg);
  73. }
  74. #if IS_ENABLED(CONFIG_FB_OMAP)
  75. void omap_set_dma_priority(int lch, int dst_port, int priority)
  76. {
  77. unsigned long reg;
  78. u32 l;
  79. if (dma_omap1()) {
  80. switch (dst_port) {
  81. case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
  82. reg = OMAP_TC_OCPT1_PRIOR;
  83. break;
  84. case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
  85. reg = OMAP_TC_OCPT2_PRIOR;
  86. break;
  87. case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
  88. reg = OMAP_TC_EMIFF_PRIOR;
  89. break;
  90. case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
  91. reg = OMAP_TC_EMIFS_PRIOR;
  92. break;
  93. default:
  94. BUG();
  95. return;
  96. }
  97. l = omap_readl(reg);
  98. l &= ~(0xf << 8);
  99. l |= (priority & 0xf) << 8;
  100. omap_writel(l, reg);
  101. }
  102. }
  103. EXPORT_SYMBOL(omap_set_dma_priority);
  104. #endif
  105. #if IS_ENABLED(CONFIG_USB_OMAP)
  106. #ifdef CONFIG_ARCH_OMAP15XX
  107. /* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
  108. static int omap_dma_in_1510_mode(void)
  109. {
  110. return enable_1510_mode;
  111. }
  112. #else
  113. #define omap_dma_in_1510_mode() 0
  114. #endif
  115. void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
  116. int frame_count, int sync_mode,
  117. int dma_trigger, int src_or_dst_synch)
  118. {
  119. u32 l;
  120. u16 ccr;
  121. l = p->dma_read(CSDP, lch);
  122. l &= ~0x03;
  123. l |= data_type;
  124. p->dma_write(l, CSDP, lch);
  125. ccr = p->dma_read(CCR, lch);
  126. ccr &= ~(1 << 5);
  127. if (sync_mode == OMAP_DMA_SYNC_FRAME)
  128. ccr |= 1 << 5;
  129. p->dma_write(ccr, CCR, lch);
  130. ccr = p->dma_read(CCR2, lch);
  131. ccr &= ~(1 << 2);
  132. if (sync_mode == OMAP_DMA_SYNC_BLOCK)
  133. ccr |= 1 << 2;
  134. p->dma_write(ccr, CCR2, lch);
  135. p->dma_write(elem_count, CEN, lch);
  136. p->dma_write(frame_count, CFN, lch);
  137. }
  138. EXPORT_SYMBOL(omap_set_dma_transfer_params);
  139. void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
  140. {
  141. if (!dma_omap15xx()) {
  142. u32 l;
  143. l = p->dma_read(LCH_CTRL, lch);
  144. l &= ~0x7;
  145. l |= mode;
  146. p->dma_write(l, LCH_CTRL, lch);
  147. }
  148. }
  149. EXPORT_SYMBOL(omap_set_dma_channel_mode);
  150. /* Note that src_port is only for omap1 */
  151. void omap_set_dma_src_params(int lch, int src_port, int src_amode,
  152. unsigned long src_start,
  153. int src_ei, int src_fi)
  154. {
  155. u32 l;
  156. u16 w;
  157. w = p->dma_read(CSDP, lch);
  158. w &= ~(0x1f << 2);
  159. w |= src_port << 2;
  160. p->dma_write(w, CSDP, lch);
  161. l = p->dma_read(CCR, lch);
  162. l &= ~(0x03 << 12);
  163. l |= src_amode << 12;
  164. p->dma_write(l, CCR, lch);
  165. p->dma_write(src_start, CSSA, lch);
  166. p->dma_write(src_ei, CSEI, lch);
  167. p->dma_write(src_fi, CSFI, lch);
  168. }
  169. EXPORT_SYMBOL(omap_set_dma_src_params);
  170. void omap_set_dma_src_data_pack(int lch, int enable)
  171. {
  172. u32 l;
  173. l = p->dma_read(CSDP, lch);
  174. l &= ~(1 << 6);
  175. if (enable)
  176. l |= (1 << 6);
  177. p->dma_write(l, CSDP, lch);
  178. }
  179. EXPORT_SYMBOL(omap_set_dma_src_data_pack);
  180. void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
  181. {
  182. unsigned int burst = 0;
  183. u32 l;
  184. l = p->dma_read(CSDP, lch);
  185. l &= ~(0x03 << 7);
  186. switch (burst_mode) {
  187. case OMAP_DMA_DATA_BURST_DIS:
  188. break;
  189. case OMAP_DMA_DATA_BURST_4:
  190. burst = 0x2;
  191. break;
  192. case OMAP_DMA_DATA_BURST_8:
  193. /*
  194. * not supported by current hardware on OMAP1
  195. * w |= (0x03 << 7);
  196. */
  197. fallthrough;
  198. case OMAP_DMA_DATA_BURST_16:
  199. /* OMAP1 don't support burst 16 */
  200. fallthrough;
  201. default:
  202. BUG();
  203. }
  204. l |= (burst << 7);
  205. p->dma_write(l, CSDP, lch);
  206. }
  207. EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
  208. /* Note that dest_port is only for OMAP1 */
  209. void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
  210. unsigned long dest_start,
  211. int dst_ei, int dst_fi)
  212. {
  213. u32 l;
  214. l = p->dma_read(CSDP, lch);
  215. l &= ~(0x1f << 9);
  216. l |= dest_port << 9;
  217. p->dma_write(l, CSDP, lch);
  218. l = p->dma_read(CCR, lch);
  219. l &= ~(0x03 << 14);
  220. l |= dest_amode << 14;
  221. p->dma_write(l, CCR, lch);
  222. p->dma_write(dest_start, CDSA, lch);
  223. p->dma_write(dst_ei, CDEI, lch);
  224. p->dma_write(dst_fi, CDFI, lch);
  225. }
  226. EXPORT_SYMBOL(omap_set_dma_dest_params);
  227. void omap_set_dma_dest_data_pack(int lch, int enable)
  228. {
  229. u32 l;
  230. l = p->dma_read(CSDP, lch);
  231. l &= ~(1 << 13);
  232. if (enable)
  233. l |= 1 << 13;
  234. p->dma_write(l, CSDP, lch);
  235. }
  236. EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
  237. void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
  238. {
  239. unsigned int burst = 0;
  240. u32 l;
  241. l = p->dma_read(CSDP, lch);
  242. l &= ~(0x03 << 14);
  243. switch (burst_mode) {
  244. case OMAP_DMA_DATA_BURST_DIS:
  245. break;
  246. case OMAP_DMA_DATA_BURST_4:
  247. burst = 0x2;
  248. break;
  249. case OMAP_DMA_DATA_BURST_8:
  250. burst = 0x3;
  251. break;
  252. case OMAP_DMA_DATA_BURST_16:
  253. /* OMAP1 don't support burst 16 */
  254. fallthrough;
  255. default:
  256. printk(KERN_ERR "Invalid DMA burst mode\n");
  257. BUG();
  258. return;
  259. }
  260. l |= (burst << 14);
  261. p->dma_write(l, CSDP, lch);
  262. }
  263. EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
  264. static inline void omap_enable_channel_irq(int lch)
  265. {
  266. /* Clear CSR */
  267. p->dma_read(CSR, lch);
  268. /* Enable some nice interrupts. */
  269. p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
  270. }
  271. void omap_disable_dma_irq(int lch, u16 bits)
  272. {
  273. dma_chan[lch].enabled_irqs &= ~bits;
  274. }
  275. EXPORT_SYMBOL(omap_disable_dma_irq);
  276. static inline void enable_lnk(int lch)
  277. {
  278. u32 l;
  279. l = p->dma_read(CLNK_CTRL, lch);
  280. l &= ~(1 << 14);
  281. /* Set the ENABLE_LNK bits */
  282. if (dma_chan[lch].next_lch != -1)
  283. l = dma_chan[lch].next_lch | (1 << 15);
  284. p->dma_write(l, CLNK_CTRL, lch);
  285. }
  286. static inline void disable_lnk(int lch)
  287. {
  288. u32 l;
  289. l = p->dma_read(CLNK_CTRL, lch);
  290. /* Disable interrupts */
  291. omap_disable_channel_irq(lch);
  292. /* Set the STOP_LNK bit */
  293. l |= 1 << 14;
  294. p->dma_write(l, CLNK_CTRL, lch);
  295. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  296. }
  297. #endif
  298. int omap_request_dma(int dev_id, const char *dev_name,
  299. void (*callback)(int lch, u16 ch_status, void *data),
  300. void *data, int *dma_ch_out)
  301. {
  302. int ch, free_ch = -1;
  303. unsigned long flags;
  304. struct omap_dma_lch *chan;
  305. WARN(strcmp(dev_name, "DMA engine"), "Using deprecated platform DMA API - please update to DMA engine");
  306. spin_lock_irqsave(&dma_chan_lock, flags);
  307. for (ch = 0; ch < dma_chan_count; ch++) {
  308. if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
  309. free_ch = ch;
  310. /* Exit after first free channel found */
  311. break;
  312. }
  313. }
  314. if (free_ch == -1) {
  315. spin_unlock_irqrestore(&dma_chan_lock, flags);
  316. return -EBUSY;
  317. }
  318. chan = dma_chan + free_ch;
  319. chan->dev_id = dev_id;
  320. if (p->clear_lch_regs)
  321. p->clear_lch_regs(free_ch);
  322. spin_unlock_irqrestore(&dma_chan_lock, flags);
  323. chan->dev_name = dev_name;
  324. chan->callback = callback;
  325. chan->data = data;
  326. chan->flags = 0;
  327. chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
  328. chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
  329. if (dma_omap16xx()) {
  330. /* If the sync device is set, configure it dynamically. */
  331. if (dev_id != 0) {
  332. set_gdma_dev(free_ch + 1, dev_id);
  333. dev_id = free_ch + 1;
  334. }
  335. /*
  336. * Disable the 1510 compatibility mode and set the sync device
  337. * id.
  338. */
  339. p->dma_write(dev_id | (1 << 10), CCR, free_ch);
  340. } else {
  341. p->dma_write(dev_id, CCR, free_ch);
  342. }
  343. *dma_ch_out = free_ch;
  344. return 0;
  345. }
  346. EXPORT_SYMBOL(omap_request_dma);
  347. void omap_free_dma(int lch)
  348. {
  349. unsigned long flags;
  350. if (dma_chan[lch].dev_id == -1) {
  351. pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
  352. lch);
  353. return;
  354. }
  355. /* Disable all DMA interrupts for the channel. */
  356. omap_disable_channel_irq(lch);
  357. /* Make sure the DMA transfer is stopped. */
  358. p->dma_write(0, CCR, lch);
  359. spin_lock_irqsave(&dma_chan_lock, flags);
  360. dma_chan[lch].dev_id = -1;
  361. dma_chan[lch].next_lch = -1;
  362. dma_chan[lch].callback = NULL;
  363. spin_unlock_irqrestore(&dma_chan_lock, flags);
  364. }
  365. EXPORT_SYMBOL(omap_free_dma);
  366. /*
  367. * Clears any DMA state so the DMA engine is ready to restart with new buffers
  368. * through omap_start_dma(). Any buffers in flight are discarded.
  369. */
  370. static void omap_clear_dma(int lch)
  371. {
  372. unsigned long flags;
  373. local_irq_save(flags);
  374. p->clear_dma(lch);
  375. local_irq_restore(flags);
  376. }
  377. #if IS_ENABLED(CONFIG_USB_OMAP)
  378. void omap_start_dma(int lch)
  379. {
  380. u32 l;
  381. /*
  382. * The CPC/CDAC register needs to be initialized to zero
  383. * before starting dma transfer.
  384. */
  385. if (dma_omap15xx())
  386. p->dma_write(0, CPC, lch);
  387. else
  388. p->dma_write(0, CDAC, lch);
  389. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  390. int next_lch, cur_lch;
  391. char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
  392. /* Set the link register of the first channel */
  393. enable_lnk(lch);
  394. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  395. dma_chan_link_map[lch] = 1;
  396. cur_lch = dma_chan[lch].next_lch;
  397. do {
  398. next_lch = dma_chan[cur_lch].next_lch;
  399. /* The loop case: we've been here already */
  400. if (dma_chan_link_map[cur_lch])
  401. break;
  402. /* Mark the current channel */
  403. dma_chan_link_map[cur_lch] = 1;
  404. enable_lnk(cur_lch);
  405. omap_enable_channel_irq(cur_lch);
  406. cur_lch = next_lch;
  407. } while (next_lch != -1);
  408. } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
  409. p->dma_write(lch, CLNK_CTRL, lch);
  410. omap_enable_channel_irq(lch);
  411. l = p->dma_read(CCR, lch);
  412. if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
  413. l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
  414. l |= OMAP_DMA_CCR_EN;
  415. /*
  416. * As dma_write() uses IO accessors which are weakly ordered, there
  417. * is no guarantee that data in coherent DMA memory will be visible
  418. * to the DMA device. Add a memory barrier here to ensure that any
  419. * such data is visible prior to enabling DMA.
  420. */
  421. mb();
  422. p->dma_write(l, CCR, lch);
  423. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  424. }
  425. EXPORT_SYMBOL(omap_start_dma);
  426. void omap_stop_dma(int lch)
  427. {
  428. u32 l;
  429. /* Disable all interrupts on the channel */
  430. omap_disable_channel_irq(lch);
  431. l = p->dma_read(CCR, lch);
  432. if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
  433. (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
  434. int i = 0;
  435. u32 sys_cf;
  436. /* Configure No-Standby */
  437. l = p->dma_read(OCP_SYSCONFIG, lch);
  438. sys_cf = l;
  439. l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
  440. l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
  441. p->dma_write(l , OCP_SYSCONFIG, 0);
  442. l = p->dma_read(CCR, lch);
  443. l &= ~OMAP_DMA_CCR_EN;
  444. p->dma_write(l, CCR, lch);
  445. /* Wait for sDMA FIFO drain */
  446. l = p->dma_read(CCR, lch);
  447. while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
  448. OMAP_DMA_CCR_WR_ACTIVE))) {
  449. udelay(5);
  450. i++;
  451. l = p->dma_read(CCR, lch);
  452. }
  453. if (i >= 100)
  454. pr_err("DMA drain did not complete on lch %d\n", lch);
  455. /* Restore OCP_SYSCONFIG */
  456. p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
  457. } else {
  458. l &= ~OMAP_DMA_CCR_EN;
  459. p->dma_write(l, CCR, lch);
  460. }
  461. /*
  462. * Ensure that data transferred by DMA is visible to any access
  463. * after DMA has been disabled. This is important for coherent
  464. * DMA regions.
  465. */
  466. mb();
  467. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  468. int next_lch, cur_lch = lch;
  469. char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
  470. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  471. do {
  472. /* The loop case: we've been here already */
  473. if (dma_chan_link_map[cur_lch])
  474. break;
  475. /* Mark the current channel */
  476. dma_chan_link_map[cur_lch] = 1;
  477. disable_lnk(cur_lch);
  478. next_lch = dma_chan[cur_lch].next_lch;
  479. cur_lch = next_lch;
  480. } while (next_lch != -1);
  481. }
  482. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  483. }
  484. EXPORT_SYMBOL(omap_stop_dma);
  485. /*
  486. * Allows changing the DMA callback function or data. This may be needed if
  487. * the driver shares a single DMA channel for multiple dma triggers.
  488. */
  489. /*
  490. * Returns current physical source address for the given DMA channel.
  491. * If the channel is running the caller must disable interrupts prior calling
  492. * this function and process the returned value before re-enabling interrupt to
  493. * prevent races with the interrupt handler. Note that in continuous mode there
  494. * is a chance for CSSA_L register overflow between the two reads resulting
  495. * in incorrect return value.
  496. */
  497. dma_addr_t omap_get_dma_src_pos(int lch)
  498. {
  499. dma_addr_t offset = 0;
  500. if (dma_omap15xx())
  501. offset = p->dma_read(CPC, lch);
  502. else
  503. offset = p->dma_read(CSAC, lch);
  504. if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
  505. offset = p->dma_read(CSAC, lch);
  506. if (!dma_omap15xx()) {
  507. /*
  508. * CDAC == 0 indicates that the DMA transfer on the channel has
  509. * not been started (no data has been transferred so far).
  510. * Return the programmed source start address in this case.
  511. */
  512. if (likely(p->dma_read(CDAC, lch)))
  513. offset = p->dma_read(CSAC, lch);
  514. else
  515. offset = p->dma_read(CSSA, lch);
  516. }
  517. offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
  518. return offset;
  519. }
  520. EXPORT_SYMBOL(omap_get_dma_src_pos);
  521. /*
  522. * Returns current physical destination address for the given DMA channel.
  523. * If the channel is running the caller must disable interrupts prior calling
  524. * this function and process the returned value before re-enabling interrupt to
  525. * prevent races with the interrupt handler. Note that in continuous mode there
  526. * is a chance for CDSA_L register overflow between the two reads resulting
  527. * in incorrect return value.
  528. */
  529. dma_addr_t omap_get_dma_dst_pos(int lch)
  530. {
  531. dma_addr_t offset = 0;
  532. if (dma_omap15xx())
  533. offset = p->dma_read(CPC, lch);
  534. else
  535. offset = p->dma_read(CDAC, lch);
  536. /*
  537. * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  538. * read before the DMA controller finished disabling the channel.
  539. */
  540. if (!dma_omap15xx() && offset == 0) {
  541. offset = p->dma_read(CDAC, lch);
  542. /*
  543. * CDAC == 0 indicates that the DMA transfer on the channel has
  544. * not been started (no data has been transferred so far).
  545. * Return the programmed destination start address in this case.
  546. */
  547. if (unlikely(!offset))
  548. offset = p->dma_read(CDSA, lch);
  549. }
  550. offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
  551. return offset;
  552. }
  553. EXPORT_SYMBOL(omap_get_dma_dst_pos);
  554. int omap_get_dma_active_status(int lch)
  555. {
  556. return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
  557. }
  558. EXPORT_SYMBOL(omap_get_dma_active_status);
  559. #endif
  560. int omap_dma_running(void)
  561. {
  562. int lch;
  563. if (omap_lcd_dma_running())
  564. return 1;
  565. for (lch = 0; lch < dma_chan_count; lch++)
  566. if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
  567. return 1;
  568. return 0;
  569. }
  570. /*----------------------------------------------------------------------------*/
  571. static int omap1_dma_handle_ch(int ch)
  572. {
  573. u32 csr;
  574. if (enable_1510_mode && ch >= 6) {
  575. csr = dma_chan[ch].saved_csr;
  576. dma_chan[ch].saved_csr = 0;
  577. } else
  578. csr = p->dma_read(CSR, ch);
  579. if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
  580. dma_chan[ch + 6].saved_csr = csr >> 7;
  581. csr &= 0x7f;
  582. }
  583. if ((csr & 0x3f) == 0)
  584. return 0;
  585. if (unlikely(dma_chan[ch].dev_id == -1)) {
  586. pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
  587. ch, csr);
  588. return 0;
  589. }
  590. if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
  591. pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
  592. if (unlikely(csr & OMAP_DMA_DROP_IRQ))
  593. pr_warn("DMA synchronization event drop occurred with device %d\n",
  594. dma_chan[ch].dev_id);
  595. if (likely(csr & OMAP_DMA_BLOCK_IRQ))
  596. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  597. if (likely(dma_chan[ch].callback != NULL))
  598. dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
  599. return 1;
  600. }
  601. static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
  602. {
  603. int ch = ((int) dev_id) - 1;
  604. int handled = 0;
  605. for (;;) {
  606. int handled_now = 0;
  607. handled_now += omap1_dma_handle_ch(ch);
  608. if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
  609. handled_now += omap1_dma_handle_ch(ch + 6);
  610. if (!handled_now)
  611. break;
  612. handled += handled_now;
  613. }
  614. return handled ? IRQ_HANDLED : IRQ_NONE;
  615. }
  616. struct omap_system_dma_plat_info *omap_get_plat_info(void)
  617. {
  618. return p;
  619. }
  620. EXPORT_SYMBOL_GPL(omap_get_plat_info);
  621. static int omap_system_dma_probe(struct platform_device *pdev)
  622. {
  623. int ch, ret = 0;
  624. int dma_irq;
  625. char irq_name[4];
  626. p = pdev->dev.platform_data;
  627. if (!p) {
  628. dev_err(&pdev->dev,
  629. "%s: System DMA initialized without platform data\n",
  630. __func__);
  631. return -EINVAL;
  632. }
  633. d = p->dma_attr;
  634. errata = p->errata;
  635. if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
  636. && (omap_dma_reserve_channels < d->lch_count))
  637. d->lch_count = omap_dma_reserve_channels;
  638. dma_lch_count = d->lch_count;
  639. dma_chan_count = dma_lch_count;
  640. enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
  641. dma_chan = devm_kcalloc(&pdev->dev, dma_lch_count,
  642. sizeof(*dma_chan), GFP_KERNEL);
  643. if (!dma_chan)
  644. return -ENOMEM;
  645. for (ch = 0; ch < dma_chan_count; ch++) {
  646. omap_clear_dma(ch);
  647. dma_chan[ch].dev_id = -1;
  648. dma_chan[ch].next_lch = -1;
  649. if (ch >= 6 && enable_1510_mode)
  650. continue;
  651. /*
  652. * request_irq() doesn't like dev_id (ie. ch) being
  653. * zero, so we have to kludge around this.
  654. */
  655. sprintf(&irq_name[0], "%d", ch);
  656. dma_irq = platform_get_irq_byname(pdev, irq_name);
  657. if (dma_irq < 0) {
  658. ret = dma_irq;
  659. goto exit_dma_irq_fail;
  660. }
  661. /* INT_DMA_LCD is handled in lcd_dma.c */
  662. if (dma_irq == INT_DMA_LCD)
  663. continue;
  664. ret = request_irq(dma_irq,
  665. omap1_dma_irq_handler, 0, "DMA",
  666. (void *) (ch + 1));
  667. if (ret != 0)
  668. goto exit_dma_irq_fail;
  669. }
  670. /* reserve dma channels 0 and 1 in high security devices on 34xx */
  671. if (d->dev_caps & HS_CHANNELS_RESERVED) {
  672. pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
  673. dma_chan[0].dev_id = 0;
  674. dma_chan[1].dev_id = 1;
  675. }
  676. p->show_dma_caps();
  677. return 0;
  678. exit_dma_irq_fail:
  679. return ret;
  680. }
  681. static void omap_system_dma_remove(struct platform_device *pdev)
  682. {
  683. int dma_irq, irq_rel = 0;
  684. for ( ; irq_rel < dma_chan_count; irq_rel++) {
  685. dma_irq = platform_get_irq(pdev, irq_rel);
  686. free_irq(dma_irq, (void *)(irq_rel + 1));
  687. }
  688. }
  689. static struct platform_driver omap_system_dma_driver = {
  690. .probe = omap_system_dma_probe,
  691. .remove_new = omap_system_dma_remove,
  692. .driver = {
  693. .name = "omap_dma_system"
  694. },
  695. };
  696. static int __init omap_system_dma_init(void)
  697. {
  698. return platform_driver_register(&omap_system_dma_driver);
  699. }
  700. arch_initcall(omap_system_dma_init);
  701. static void __exit omap_system_dma_exit(void)
  702. {
  703. platform_driver_unregister(&omap_system_dma_driver);
  704. }
  705. MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
  706. MODULE_LICENSE("GPL");
  707. MODULE_AUTHOR("Texas Instruments Inc");
  708. /*
  709. * Reserve the omap SDMA channels using cmdline bootarg
  710. * "omap_dma_reserve_ch=". The valid range is 1 to 32
  711. */
  712. static int __init omap_dma_cmdline_reserve_ch(char *str)
  713. {
  714. if (get_option(&str, &omap_dma_reserve_channels) != 1)
  715. omap_dma_reserve_channels = 0;
  716. return 1;
  717. }
  718. __setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);