omap-dma.c 20 KB

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