dma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * linux/arch/arm/omap/dma.c
  3. *
  4. * Copyright (C) 2003 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. * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
  10. *
  11. * Support functions for the OMAP internal DMA channels.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/errno.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/system.h>
  25. #include <asm/irq.h>
  26. #include <asm/hardware.h>
  27. #include <asm/dma.h>
  28. #include <asm/io.h>
  29. #include <asm/arch/tc.h>
  30. #define OMAP_DMA_ACTIVE 0x01
  31. #define OMAP_DMA_CCR_EN (1 << 7)
  32. #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
  33. static int enable_1510_mode = 0;
  34. struct omap_dma_lch {
  35. int next_lch;
  36. int dev_id;
  37. u16 saved_csr;
  38. u16 enabled_irqs;
  39. const char *dev_name;
  40. void (* callback)(int lch, u16 ch_status, void *data);
  41. void *data;
  42. long flags;
  43. };
  44. static int dma_chan_count;
  45. static spinlock_t dma_chan_lock;
  46. static struct omap_dma_lch dma_chan[OMAP_LOGICAL_DMA_CH_COUNT];
  47. const static u8 dma_irq[OMAP_LOGICAL_DMA_CH_COUNT] = {
  48. INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
  49. INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
  50. INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
  51. INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
  52. INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
  53. };
  54. static inline int get_gdma_dev(int req)
  55. {
  56. u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
  57. int shift = ((req - 1) % 5) * 6;
  58. return ((omap_readl(reg) >> shift) & 0x3f) + 1;
  59. }
  60. static inline void set_gdma_dev(int req, int dev)
  61. {
  62. u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
  63. int shift = ((req - 1) % 5) * 6;
  64. u32 l;
  65. l = omap_readl(reg);
  66. l &= ~(0x3f << shift);
  67. l |= (dev - 1) << shift;
  68. omap_writel(l, reg);
  69. }
  70. static void clear_lch_regs(int lch)
  71. {
  72. int i;
  73. u32 lch_base = OMAP_DMA_BASE + lch * 0x40;
  74. for (i = 0; i < 0x2c; i += 2)
  75. omap_writew(0, lch_base + i);
  76. }
  77. void omap_set_dma_priority(int dst_port, int priority)
  78. {
  79. unsigned long reg;
  80. u32 l;
  81. switch (dst_port) {
  82. case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
  83. reg = OMAP_TC_OCPT1_PRIOR;
  84. break;
  85. case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
  86. reg = OMAP_TC_OCPT2_PRIOR;
  87. break;
  88. case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
  89. reg = OMAP_TC_EMIFF_PRIOR;
  90. break;
  91. case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
  92. reg = OMAP_TC_EMIFS_PRIOR;
  93. break;
  94. default:
  95. BUG();
  96. return;
  97. }
  98. l = omap_readl(reg);
  99. l &= ~(0xf << 8);
  100. l |= (priority & 0xf) << 8;
  101. omap_writel(l, reg);
  102. }
  103. void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
  104. int frame_count, int sync_mode)
  105. {
  106. u16 w;
  107. w = omap_readw(OMAP_DMA_CSDP(lch));
  108. w &= ~0x03;
  109. w |= data_type;
  110. omap_writew(w, OMAP_DMA_CSDP(lch));
  111. w = omap_readw(OMAP_DMA_CCR(lch));
  112. w &= ~(1 << 5);
  113. if (sync_mode == OMAP_DMA_SYNC_FRAME)
  114. w |= 1 << 5;
  115. omap_writew(w, OMAP_DMA_CCR(lch));
  116. w = omap_readw(OMAP_DMA_CCR2(lch));
  117. w &= ~(1 << 2);
  118. if (sync_mode == OMAP_DMA_SYNC_BLOCK)
  119. w |= 1 << 2;
  120. omap_writew(w, OMAP_DMA_CCR2(lch));
  121. omap_writew(elem_count, OMAP_DMA_CEN(lch));
  122. omap_writew(frame_count, OMAP_DMA_CFN(lch));
  123. }
  124. void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
  125. {
  126. u16 w;
  127. BUG_ON(omap_dma_in_1510_mode());
  128. w = omap_readw(OMAP_DMA_CCR2(lch)) & ~0x03;
  129. switch (mode) {
  130. case OMAP_DMA_CONSTANT_FILL:
  131. w |= 0x01;
  132. break;
  133. case OMAP_DMA_TRANSPARENT_COPY:
  134. w |= 0x02;
  135. break;
  136. case OMAP_DMA_COLOR_DIS:
  137. break;
  138. default:
  139. BUG();
  140. }
  141. omap_writew(w, OMAP_DMA_CCR2(lch));
  142. w = omap_readw(OMAP_DMA_LCH_CTRL(lch)) & ~0x0f;
  143. /* Default is channel type 2D */
  144. if (mode) {
  145. omap_writew((u16)color, OMAP_DMA_COLOR_L(lch));
  146. omap_writew((u16)(color >> 16), OMAP_DMA_COLOR_U(lch));
  147. w |= 1; /* Channel type G */
  148. }
  149. omap_writew(w, OMAP_DMA_LCH_CTRL(lch));
  150. }
  151. void omap_set_dma_src_params(int lch, int src_port, int src_amode,
  152. unsigned long src_start)
  153. {
  154. u16 w;
  155. w = omap_readw(OMAP_DMA_CSDP(lch));
  156. w &= ~(0x1f << 2);
  157. w |= src_port << 2;
  158. omap_writew(w, OMAP_DMA_CSDP(lch));
  159. w = omap_readw(OMAP_DMA_CCR(lch));
  160. w &= ~(0x03 << 12);
  161. w |= src_amode << 12;
  162. omap_writew(w, OMAP_DMA_CCR(lch));
  163. omap_writew(src_start >> 16, OMAP_DMA_CSSA_U(lch));
  164. omap_writew(src_start, OMAP_DMA_CSSA_L(lch));
  165. }
  166. void omap_set_dma_src_index(int lch, int eidx, int fidx)
  167. {
  168. omap_writew(eidx, OMAP_DMA_CSEI(lch));
  169. omap_writew(fidx, OMAP_DMA_CSFI(lch));
  170. }
  171. void omap_set_dma_src_data_pack(int lch, int enable)
  172. {
  173. u16 w;
  174. w = omap_readw(OMAP_DMA_CSDP(lch)) & ~(1 << 6);
  175. w |= enable ? (1 << 6) : 0;
  176. omap_writew(w, OMAP_DMA_CSDP(lch));
  177. }
  178. void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
  179. {
  180. u16 w;
  181. w = omap_readw(OMAP_DMA_CSDP(lch)) & ~(0x03 << 7);
  182. switch (burst_mode) {
  183. case OMAP_DMA_DATA_BURST_DIS:
  184. break;
  185. case OMAP_DMA_DATA_BURST_4:
  186. w |= (0x01 << 7);
  187. break;
  188. case OMAP_DMA_DATA_BURST_8:
  189. /* not supported by current hardware
  190. * w |= (0x03 << 7);
  191. * fall through
  192. */
  193. default:
  194. BUG();
  195. }
  196. omap_writew(w, OMAP_DMA_CSDP(lch));
  197. }
  198. void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
  199. unsigned long dest_start)
  200. {
  201. u16 w;
  202. w = omap_readw(OMAP_DMA_CSDP(lch));
  203. w &= ~(0x1f << 9);
  204. w |= dest_port << 9;
  205. omap_writew(w, OMAP_DMA_CSDP(lch));
  206. w = omap_readw(OMAP_DMA_CCR(lch));
  207. w &= ~(0x03 << 14);
  208. w |= dest_amode << 14;
  209. omap_writew(w, OMAP_DMA_CCR(lch));
  210. omap_writew(dest_start >> 16, OMAP_DMA_CDSA_U(lch));
  211. omap_writew(dest_start, OMAP_DMA_CDSA_L(lch));
  212. }
  213. void omap_set_dma_dest_index(int lch, int eidx, int fidx)
  214. {
  215. omap_writew(eidx, OMAP_DMA_CDEI(lch));
  216. omap_writew(fidx, OMAP_DMA_CDFI(lch));
  217. }
  218. void omap_set_dma_dest_data_pack(int lch, int enable)
  219. {
  220. u16 w;
  221. w = omap_readw(OMAP_DMA_CSDP(lch)) & ~(1 << 13);
  222. w |= enable ? (1 << 13) : 0;
  223. omap_writew(w, OMAP_DMA_CSDP(lch));
  224. }
  225. void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
  226. {
  227. u16 w;
  228. w = omap_readw(OMAP_DMA_CSDP(lch)) & ~(0x03 << 14);
  229. switch (burst_mode) {
  230. case OMAP_DMA_DATA_BURST_DIS:
  231. break;
  232. case OMAP_DMA_DATA_BURST_4:
  233. w |= (0x01 << 14);
  234. break;
  235. case OMAP_DMA_DATA_BURST_8:
  236. w |= (0x03 << 14);
  237. break;
  238. default:
  239. printk(KERN_ERR "Invalid DMA burst mode\n");
  240. BUG();
  241. return;
  242. }
  243. omap_writew(w, OMAP_DMA_CSDP(lch));
  244. }
  245. static inline void init_intr(int lch)
  246. {
  247. u16 w;
  248. /* Read CSR to make sure it's cleared. */
  249. w = omap_readw(OMAP_DMA_CSR(lch));
  250. /* Enable some nice interrupts. */
  251. omap_writew(dma_chan[lch].enabled_irqs, OMAP_DMA_CICR(lch));
  252. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  253. }
  254. static inline void enable_lnk(int lch)
  255. {
  256. u16 w;
  257. /* Clear the STOP_LNK bits */
  258. w = omap_readw(OMAP_DMA_CLNK_CTRL(lch));
  259. w &= ~(1 << 14);
  260. omap_writew(w, OMAP_DMA_CLNK_CTRL(lch));
  261. /* And set the ENABLE_LNK bits */
  262. if (dma_chan[lch].next_lch != -1)
  263. omap_writew(dma_chan[lch].next_lch | (1 << 15),
  264. OMAP_DMA_CLNK_CTRL(lch));
  265. }
  266. static inline void disable_lnk(int lch)
  267. {
  268. u16 w;
  269. /* Disable interrupts */
  270. omap_writew(0, OMAP_DMA_CICR(lch));
  271. /* Set the STOP_LNK bit */
  272. w = omap_readw(OMAP_DMA_CLNK_CTRL(lch));
  273. w |= (1 << 14);
  274. w = omap_writew(w, OMAP_DMA_CLNK_CTRL(lch));
  275. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  276. }
  277. void omap_start_dma(int lch)
  278. {
  279. u16 w;
  280. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  281. int next_lch, cur_lch;
  282. char dma_chan_link_map[OMAP_LOGICAL_DMA_CH_COUNT];
  283. dma_chan_link_map[lch] = 1;
  284. /* Set the link register of the first channel */
  285. enable_lnk(lch);
  286. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  287. cur_lch = dma_chan[lch].next_lch;
  288. do {
  289. next_lch = dma_chan[cur_lch].next_lch;
  290. /* The loop case: we've been here already */
  291. if (dma_chan_link_map[cur_lch])
  292. break;
  293. /* Mark the current channel */
  294. dma_chan_link_map[cur_lch] = 1;
  295. enable_lnk(cur_lch);
  296. init_intr(cur_lch);
  297. cur_lch = next_lch;
  298. } while (next_lch != -1);
  299. }
  300. init_intr(lch);
  301. w = omap_readw(OMAP_DMA_CCR(lch));
  302. w |= OMAP_DMA_CCR_EN;
  303. omap_writew(w, OMAP_DMA_CCR(lch));
  304. dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
  305. }
  306. void omap_stop_dma(int lch)
  307. {
  308. u16 w;
  309. if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
  310. int next_lch, cur_lch = lch;
  311. char dma_chan_link_map[OMAP_LOGICAL_DMA_CH_COUNT];
  312. memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
  313. do {
  314. /* The loop case: we've been here already */
  315. if (dma_chan_link_map[cur_lch])
  316. break;
  317. /* Mark the current channel */
  318. dma_chan_link_map[cur_lch] = 1;
  319. disable_lnk(cur_lch);
  320. next_lch = dma_chan[cur_lch].next_lch;
  321. cur_lch = next_lch;
  322. } while (next_lch != -1);
  323. return;
  324. }
  325. /* Disable all interrupts on the channel */
  326. omap_writew(0, OMAP_DMA_CICR(lch));
  327. w = omap_readw(OMAP_DMA_CCR(lch));
  328. w &= ~OMAP_DMA_CCR_EN;
  329. omap_writew(w, OMAP_DMA_CCR(lch));
  330. dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
  331. }
  332. void omap_enable_dma_irq(int lch, u16 bits)
  333. {
  334. dma_chan[lch].enabled_irqs |= bits;
  335. }
  336. void omap_disable_dma_irq(int lch, u16 bits)
  337. {
  338. dma_chan[lch].enabled_irqs &= ~bits;
  339. }
  340. static int dma_handle_ch(int ch)
  341. {
  342. u16 csr;
  343. if (enable_1510_mode && ch >= 6) {
  344. csr = dma_chan[ch].saved_csr;
  345. dma_chan[ch].saved_csr = 0;
  346. } else
  347. csr = omap_readw(OMAP_DMA_CSR(ch));
  348. if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
  349. dma_chan[ch + 6].saved_csr = csr >> 7;
  350. csr &= 0x7f;
  351. }
  352. if (!csr)
  353. return 0;
  354. if (unlikely(dma_chan[ch].dev_id == -1)) {
  355. printk(KERN_WARNING "Spurious interrupt from DMA channel %d (CSR %04x)\n",
  356. ch, csr);
  357. return 0;
  358. }
  359. if (unlikely(csr & OMAP_DMA_TOUT_IRQ))
  360. printk(KERN_WARNING "DMA timeout with device %d\n", dma_chan[ch].dev_id);
  361. if (unlikely(csr & OMAP_DMA_DROP_IRQ))
  362. printk(KERN_WARNING "DMA synchronization event drop occurred with device %d\n",
  363. dma_chan[ch].dev_id);
  364. if (likely(csr & OMAP_DMA_BLOCK_IRQ))
  365. dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
  366. if (likely(dma_chan[ch].callback != NULL))
  367. dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
  368. return 1;
  369. }
  370. static irqreturn_t dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  371. {
  372. int ch = ((int) dev_id) - 1;
  373. int handled = 0;
  374. for (;;) {
  375. int handled_now = 0;
  376. handled_now += dma_handle_ch(ch);
  377. if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
  378. handled_now += dma_handle_ch(ch + 6);
  379. if (!handled_now)
  380. break;
  381. handled += handled_now;
  382. }
  383. return handled ? IRQ_HANDLED : IRQ_NONE;
  384. }
  385. int omap_request_dma(int dev_id, const char *dev_name,
  386. void (* callback)(int lch, u16 ch_status, void *data),
  387. void *data, int *dma_ch_out)
  388. {
  389. int ch, free_ch = -1;
  390. unsigned long flags;
  391. struct omap_dma_lch *chan;
  392. spin_lock_irqsave(&dma_chan_lock, flags);
  393. for (ch = 0; ch < dma_chan_count; ch++) {
  394. if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
  395. free_ch = ch;
  396. if (dev_id == 0)
  397. break;
  398. }
  399. }
  400. if (free_ch == -1) {
  401. spin_unlock_irqrestore(&dma_chan_lock, flags);
  402. return -EBUSY;
  403. }
  404. chan = dma_chan + free_ch;
  405. chan->dev_id = dev_id;
  406. clear_lch_regs(free_ch);
  407. spin_unlock_irqrestore(&dma_chan_lock, flags);
  408. chan->dev_id = dev_id;
  409. chan->dev_name = dev_name;
  410. chan->callback = callback;
  411. chan->data = data;
  412. chan->enabled_irqs = OMAP_DMA_TOUT_IRQ | OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
  413. if (cpu_is_omap16xx()) {
  414. /* If the sync device is set, configure it dynamically. */
  415. if (dev_id != 0) {
  416. set_gdma_dev(free_ch + 1, dev_id);
  417. dev_id = free_ch + 1;
  418. }
  419. /* Disable the 1510 compatibility mode and set the sync device
  420. * id. */
  421. omap_writew(dev_id | (1 << 10), OMAP_DMA_CCR(free_ch));
  422. } else {
  423. omap_writew(dev_id, OMAP_DMA_CCR(free_ch));
  424. }
  425. *dma_ch_out = free_ch;
  426. return 0;
  427. }
  428. void omap_free_dma(int ch)
  429. {
  430. unsigned long flags;
  431. spin_lock_irqsave(&dma_chan_lock, flags);
  432. if (dma_chan[ch].dev_id == -1) {
  433. printk("omap_dma: trying to free nonallocated DMA channel %d\n", ch);
  434. spin_unlock_irqrestore(&dma_chan_lock, flags);
  435. return;
  436. }
  437. dma_chan[ch].dev_id = -1;
  438. spin_unlock_irqrestore(&dma_chan_lock, flags);
  439. /* Disable all DMA interrupts for the channel. */
  440. omap_writew(0, OMAP_DMA_CICR(ch));
  441. /* Make sure the DMA transfer is stopped. */
  442. omap_writew(0, OMAP_DMA_CCR(ch));
  443. }
  444. int omap_dma_in_1510_mode(void)
  445. {
  446. return enable_1510_mode;
  447. }
  448. /*
  449. * lch_queue DMA will start right after lch_head one is finished.
  450. * For this DMA link to start, you still need to start (see omap_start_dma)
  451. * the first one. That will fire up the entire queue.
  452. */
  453. void omap_dma_link_lch (int lch_head, int lch_queue)
  454. {
  455. if (omap_dma_in_1510_mode()) {
  456. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  457. BUG();
  458. return;
  459. }
  460. if ((dma_chan[lch_head].dev_id == -1) ||
  461. (dma_chan[lch_queue].dev_id == -1)) {
  462. printk(KERN_ERR "omap_dma: trying to link non requested channels\n");
  463. dump_stack();
  464. }
  465. dma_chan[lch_head].next_lch = lch_queue;
  466. }
  467. /*
  468. * Once the DMA queue is stopped, we can destroy it.
  469. */
  470. void omap_dma_unlink_lch (int lch_head, int lch_queue)
  471. {
  472. if (omap_dma_in_1510_mode()) {
  473. printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
  474. BUG();
  475. return;
  476. }
  477. if (dma_chan[lch_head].next_lch != lch_queue ||
  478. dma_chan[lch_head].next_lch == -1) {
  479. printk(KERN_ERR "omap_dma: trying to unlink non linked channels\n");
  480. dump_stack();
  481. }
  482. if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
  483. (dma_chan[lch_head].flags & OMAP_DMA_ACTIVE)) {
  484. printk(KERN_ERR "omap_dma: You need to stop the DMA channels before unlinking\n");
  485. dump_stack();
  486. }
  487. dma_chan[lch_head].next_lch = -1;
  488. }
  489. static struct lcd_dma_info {
  490. spinlock_t lock;
  491. int reserved;
  492. void (* callback)(u16 status, void *data);
  493. void *cb_data;
  494. int active;
  495. unsigned long addr, size;
  496. int rotate, data_type, xres, yres;
  497. int vxres;
  498. int mirror;
  499. int xscale, yscale;
  500. int ext_ctrl;
  501. int src_port;
  502. int single_transfer;
  503. } lcd_dma;
  504. void omap_set_lcd_dma_b1(unsigned long addr, u16 fb_xres, u16 fb_yres,
  505. int data_type)
  506. {
  507. lcd_dma.addr = addr;
  508. lcd_dma.data_type = data_type;
  509. lcd_dma.xres = fb_xres;
  510. lcd_dma.yres = fb_yres;
  511. }
  512. void omap_set_lcd_dma_src_port(int port)
  513. {
  514. lcd_dma.src_port = port;
  515. }
  516. void omap_set_lcd_dma_ext_controller(int external)
  517. {
  518. lcd_dma.ext_ctrl = external;
  519. }
  520. void omap_set_lcd_dma_single_transfer(int single)
  521. {
  522. lcd_dma.single_transfer = single;
  523. }
  524. void omap_set_lcd_dma_b1_rotation(int rotate)
  525. {
  526. if (omap_dma_in_1510_mode()) {
  527. printk(KERN_ERR "DMA rotation is not supported in 1510 mode\n");
  528. BUG();
  529. return;
  530. }
  531. lcd_dma.rotate = rotate;
  532. }
  533. void omap_set_lcd_dma_b1_mirror(int mirror)
  534. {
  535. if (omap_dma_in_1510_mode()) {
  536. printk(KERN_ERR "DMA mirror is not supported in 1510 mode\n");
  537. BUG();
  538. }
  539. lcd_dma.mirror = mirror;
  540. }
  541. void omap_set_lcd_dma_b1_vxres(unsigned long vxres)
  542. {
  543. if (omap_dma_in_1510_mode()) {
  544. printk(KERN_ERR "DMA virtual resulotion is not supported "
  545. "in 1510 mode\n");
  546. BUG();
  547. }
  548. lcd_dma.vxres = vxres;
  549. }
  550. void omap_set_lcd_dma_b1_scale(unsigned int xscale, unsigned int yscale)
  551. {
  552. if (omap_dma_in_1510_mode()) {
  553. printk(KERN_ERR "DMA scale is not supported in 1510 mode\n");
  554. BUG();
  555. }
  556. lcd_dma.xscale = xscale;
  557. lcd_dma.yscale = yscale;
  558. }
  559. static void set_b1_regs(void)
  560. {
  561. unsigned long top, bottom;
  562. int es;
  563. u16 w;
  564. unsigned long en, fn;
  565. long ei, fi;
  566. unsigned long vxres;
  567. unsigned int xscale, yscale;
  568. switch (lcd_dma.data_type) {
  569. case OMAP_DMA_DATA_TYPE_S8:
  570. es = 1;
  571. break;
  572. case OMAP_DMA_DATA_TYPE_S16:
  573. es = 2;
  574. break;
  575. case OMAP_DMA_DATA_TYPE_S32:
  576. es = 4;
  577. break;
  578. default:
  579. BUG();
  580. return;
  581. }
  582. vxres = lcd_dma.vxres ? lcd_dma.vxres : lcd_dma.xres;
  583. xscale = lcd_dma.xscale ? lcd_dma.xscale : 1;
  584. yscale = lcd_dma.yscale ? lcd_dma.yscale : 1;
  585. BUG_ON(vxres < lcd_dma.xres);
  586. #define PIXADDR(x,y) (lcd_dma.addr + ((y) * vxres * yscale + (x) * xscale) * es)
  587. #define PIXSTEP(sx, sy, dx, dy) (PIXADDR(dx, dy) - PIXADDR(sx, sy) - es + 1)
  588. switch (lcd_dma.rotate) {
  589. case 0:
  590. if (!lcd_dma.mirror) {
  591. top = PIXADDR(0, 0);
  592. bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
  593. /* 1510 DMA requires the bottom address to be 2 more
  594. * than the actual last memory access location. */
  595. if (omap_dma_in_1510_mode() &&
  596. lcd_dma.data_type == OMAP_DMA_DATA_TYPE_S32)
  597. bottom += 2;
  598. ei = PIXSTEP(0, 0, 1, 0);
  599. fi = PIXSTEP(lcd_dma.xres - 1, 0, 0, 1);
  600. } else {
  601. top = PIXADDR(lcd_dma.xres - 1, 0);
  602. bottom = PIXADDR(0, lcd_dma.yres - 1);
  603. ei = PIXSTEP(1, 0, 0, 0);
  604. fi = PIXSTEP(0, 0, lcd_dma.xres - 1, 1);
  605. }
  606. en = lcd_dma.xres;
  607. fn = lcd_dma.yres;
  608. break;
  609. case 90:
  610. if (!lcd_dma.mirror) {
  611. top = PIXADDR(0, lcd_dma.yres - 1);
  612. bottom = PIXADDR(lcd_dma.xres - 1, 0);
  613. ei = PIXSTEP(0, 1, 0, 0);
  614. fi = PIXSTEP(0, 0, 1, lcd_dma.yres - 1);
  615. } else {
  616. top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
  617. bottom = PIXADDR(0, 0);
  618. ei = PIXSTEP(0, 1, 0, 0);
  619. fi = PIXSTEP(1, 0, 0, lcd_dma.yres - 1);
  620. }
  621. en = lcd_dma.yres;
  622. fn = lcd_dma.xres;
  623. break;
  624. case 180:
  625. if (!lcd_dma.mirror) {
  626. top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
  627. bottom = PIXADDR(0, 0);
  628. ei = PIXSTEP(1, 0, 0, 0);
  629. fi = PIXSTEP(0, 1, lcd_dma.xres - 1, 0);
  630. } else {
  631. top = PIXADDR(0, lcd_dma.yres - 1);
  632. bottom = PIXADDR(lcd_dma.xres - 1, 0);
  633. ei = PIXSTEP(0, 0, 1, 0);
  634. fi = PIXSTEP(lcd_dma.xres - 1, 1, 0, 0);
  635. }
  636. en = lcd_dma.xres;
  637. fn = lcd_dma.yres;
  638. break;
  639. case 270:
  640. if (!lcd_dma.mirror) {
  641. top = PIXADDR(lcd_dma.xres - 1, 0);
  642. bottom = PIXADDR(0, lcd_dma.yres - 1);
  643. ei = PIXSTEP(0, 0, 0, 1);
  644. fi = PIXSTEP(1, lcd_dma.yres - 1, 0, 0);
  645. } else {
  646. top = PIXADDR(0, 0);
  647. bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
  648. ei = PIXSTEP(0, 0, 0, 1);
  649. fi = PIXSTEP(0, lcd_dma.yres - 1, 1, 0);
  650. }
  651. en = lcd_dma.yres;
  652. fn = lcd_dma.xres;
  653. break;
  654. default:
  655. BUG();
  656. return; /* Supress warning about uninitialized vars */
  657. }
  658. if (omap_dma_in_1510_mode()) {
  659. omap_writew(top >> 16, OMAP1510_DMA_LCD_TOP_F1_U);
  660. omap_writew(top, OMAP1510_DMA_LCD_TOP_F1_L);
  661. omap_writew(bottom >> 16, OMAP1510_DMA_LCD_BOT_F1_U);
  662. omap_writew(bottom, OMAP1510_DMA_LCD_BOT_F1_L);
  663. return;
  664. }
  665. /* 1610 regs */
  666. omap_writew(top >> 16, OMAP1610_DMA_LCD_TOP_B1_U);
  667. omap_writew(top, OMAP1610_DMA_LCD_TOP_B1_L);
  668. omap_writew(bottom >> 16, OMAP1610_DMA_LCD_BOT_B1_U);
  669. omap_writew(bottom, OMAP1610_DMA_LCD_BOT_B1_L);
  670. omap_writew(en, OMAP1610_DMA_LCD_SRC_EN_B1);
  671. omap_writew(fn, OMAP1610_DMA_LCD_SRC_FN_B1);
  672. w = omap_readw(OMAP1610_DMA_LCD_CSDP);
  673. w &= ~0x03;
  674. w |= lcd_dma.data_type;
  675. omap_writew(w, OMAP1610_DMA_LCD_CSDP);
  676. w = omap_readw(OMAP1610_DMA_LCD_CTRL);
  677. /* Always set the source port as SDRAM for now*/
  678. w &= ~(0x03 << 6);
  679. if (lcd_dma.ext_ctrl)
  680. w |= 1 << 8;
  681. else
  682. w &= ~(1 << 8);
  683. if (lcd_dma.callback != NULL)
  684. w |= 1 << 1; /* Block interrupt enable */
  685. else
  686. w &= ~(1 << 1);
  687. omap_writew(w, OMAP1610_DMA_LCD_CTRL);
  688. if (!(lcd_dma.rotate || lcd_dma.mirror ||
  689. lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
  690. return;
  691. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  692. /* Set the double-indexed addressing mode */
  693. w |= (0x03 << 12);
  694. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  695. omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
  696. omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
  697. omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
  698. }
  699. static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  700. {
  701. u16 w;
  702. w = omap_readw(OMAP1610_DMA_LCD_CTRL);
  703. if (unlikely(!(w & (1 << 3)))) {
  704. printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
  705. return IRQ_NONE;
  706. }
  707. /* Ack the IRQ */
  708. w |= (1 << 3);
  709. omap_writew(w, OMAP1610_DMA_LCD_CTRL);
  710. lcd_dma.active = 0;
  711. if (lcd_dma.callback != NULL)
  712. lcd_dma.callback(w, lcd_dma.cb_data);
  713. return IRQ_HANDLED;
  714. }
  715. int omap_request_lcd_dma(void (* callback)(u16 status, void *data),
  716. void *data)
  717. {
  718. spin_lock_irq(&lcd_dma.lock);
  719. if (lcd_dma.reserved) {
  720. spin_unlock_irq(&lcd_dma.lock);
  721. printk(KERN_ERR "LCD DMA channel already reserved\n");
  722. BUG();
  723. return -EBUSY;
  724. }
  725. lcd_dma.reserved = 1;
  726. spin_unlock_irq(&lcd_dma.lock);
  727. lcd_dma.callback = callback;
  728. lcd_dma.cb_data = data;
  729. lcd_dma.active = 0;
  730. lcd_dma.single_transfer = 0;
  731. lcd_dma.rotate = 0;
  732. lcd_dma.vxres = 0;
  733. lcd_dma.mirror = 0;
  734. lcd_dma.xscale = 0;
  735. lcd_dma.yscale = 0;
  736. lcd_dma.ext_ctrl = 0;
  737. lcd_dma.src_port = 0;
  738. return 0;
  739. }
  740. void omap_free_lcd_dma(void)
  741. {
  742. spin_lock(&lcd_dma.lock);
  743. if (!lcd_dma.reserved) {
  744. spin_unlock(&lcd_dma.lock);
  745. printk(KERN_ERR "LCD DMA is not reserved\n");
  746. BUG();
  747. return;
  748. }
  749. if (!enable_1510_mode)
  750. omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1, OMAP1610_DMA_LCD_CCR);
  751. lcd_dma.reserved = 0;
  752. spin_unlock(&lcd_dma.lock);
  753. }
  754. void omap_enable_lcd_dma(void)
  755. {
  756. u16 w;
  757. /* Set the Enable bit only if an external controller is
  758. * connected. Otherwise the OMAP internal controller will
  759. * start the transfer when it gets enabled.
  760. */
  761. if (enable_1510_mode || !lcd_dma.ext_ctrl)
  762. return;
  763. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  764. w |= 1 << 7;
  765. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  766. lcd_dma.active = 1;
  767. }
  768. void omap_setup_lcd_dma(void)
  769. {
  770. BUG_ON(lcd_dma.active);
  771. if (!enable_1510_mode) {
  772. /* Set some reasonable defaults */
  773. omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
  774. omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
  775. omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
  776. }
  777. set_b1_regs();
  778. if (!enable_1510_mode) {
  779. u16 w;
  780. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  781. /* If DMA was already active set the end_prog bit to have
  782. * the programmed register set loaded into the active
  783. * register set.
  784. */
  785. w |= 1 << 11; /* End_prog */
  786. if (!lcd_dma.single_transfer)
  787. w |= (3 << 8); /* Auto_init, repeat */
  788. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  789. }
  790. }
  791. void omap_stop_lcd_dma(void)
  792. {
  793. lcd_dma.active = 0;
  794. if (!enable_1510_mode && lcd_dma.ext_ctrl)
  795. omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~(1 << 7),
  796. OMAP1610_DMA_LCD_CCR);
  797. }
  798. /*
  799. * Clears any DMA state so the DMA engine is ready to restart with new buffers
  800. * through omap_start_dma(). Any buffers in flight are discarded.
  801. */
  802. void omap_clear_dma(int lch)
  803. {
  804. unsigned long flags;
  805. int status;
  806. local_irq_save(flags);
  807. omap_writew(omap_readw(OMAP_DMA_CCR(lch)) & ~OMAP_DMA_CCR_EN,
  808. OMAP_DMA_CCR(lch));
  809. status = OMAP_DMA_CSR(lch); /* clear pending interrupts */
  810. local_irq_restore(flags);
  811. }
  812. /*
  813. * Returns current physical source address for the given DMA channel.
  814. * If the channel is running the caller must disable interrupts prior calling
  815. * this function and process the returned value before re-enabling interrupt to
  816. * prevent races with the interrupt handler. Note that in continuous mode there
  817. * is a chance for CSSA_L register overflow inbetween the two reads resulting
  818. * in incorrect return value.
  819. */
  820. dma_addr_t omap_get_dma_src_pos(int lch)
  821. {
  822. return (dma_addr_t) (OMAP_DMA_CSSA_L(lch) |
  823. (OMAP_DMA_CSSA_U(lch) << 16));
  824. }
  825. /*
  826. * Returns current physical destination address for the given DMA channel.
  827. * If the channel is running the caller must disable interrupts prior calling
  828. * this function and process the returned value before re-enabling interrupt to
  829. * prevent races with the interrupt handler. Note that in continuous mode there
  830. * is a chance for CDSA_L register overflow inbetween the two reads resulting
  831. * in incorrect return value.
  832. */
  833. dma_addr_t omap_get_dma_dst_pos(int lch)
  834. {
  835. return (dma_addr_t) (OMAP_DMA_CDSA_L(lch) |
  836. (OMAP_DMA_CDSA_U(lch) << 16));
  837. }
  838. static int __init omap_init_dma(void)
  839. {
  840. int ch, r;
  841. if (cpu_is_omap1510()) {
  842. printk(KERN_INFO "DMA support for OMAP1510 initialized\n");
  843. dma_chan_count = 9;
  844. enable_1510_mode = 1;
  845. } else if (cpu_is_omap16xx() || cpu_is_omap730()) {
  846. printk(KERN_INFO "OMAP DMA hardware version %d\n",
  847. omap_readw(OMAP_DMA_HW_ID));
  848. printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
  849. (omap_readw(OMAP_DMA_CAPS_0_U) << 16) | omap_readw(OMAP_DMA_CAPS_0_L),
  850. (omap_readw(OMAP_DMA_CAPS_1_U) << 16) | omap_readw(OMAP_DMA_CAPS_1_L),
  851. omap_readw(OMAP_DMA_CAPS_2), omap_readw(OMAP_DMA_CAPS_3),
  852. omap_readw(OMAP_DMA_CAPS_4));
  853. if (!enable_1510_mode) {
  854. u16 w;
  855. /* Disable OMAP 3.0/3.1 compatibility mode. */
  856. w = omap_readw(OMAP_DMA_GSCR);
  857. w |= 1 << 3;
  858. omap_writew(w, OMAP_DMA_GSCR);
  859. dma_chan_count = 16;
  860. } else
  861. dma_chan_count = 9;
  862. } else {
  863. dma_chan_count = 0;
  864. return 0;
  865. }
  866. memset(&lcd_dma, 0, sizeof(lcd_dma));
  867. spin_lock_init(&lcd_dma.lock);
  868. spin_lock_init(&dma_chan_lock);
  869. memset(&dma_chan, 0, sizeof(dma_chan));
  870. for (ch = 0; ch < dma_chan_count; ch++) {
  871. dma_chan[ch].dev_id = -1;
  872. dma_chan[ch].next_lch = -1;
  873. if (ch >= 6 && enable_1510_mode)
  874. continue;
  875. /* request_irq() doesn't like dev_id (ie. ch) being zero,
  876. * so we have to kludge around this. */
  877. r = request_irq(dma_irq[ch], dma_irq_handler, 0, "DMA",
  878. (void *) (ch + 1));
  879. if (r != 0) {
  880. int i;
  881. printk(KERN_ERR "unable to request IRQ %d for DMA (error %d)\n",
  882. dma_irq[ch], r);
  883. for (i = 0; i < ch; i++)
  884. free_irq(dma_irq[i], (void *) (i + 1));
  885. return r;
  886. }
  887. }
  888. r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0, "LCD DMA", NULL);
  889. if (r != 0) {
  890. int i;
  891. printk(KERN_ERR "unable to request IRQ for LCD DMA (error %d)\n", r);
  892. for (i = 0; i < dma_chan_count; i++)
  893. free_irq(dma_irq[i], (void *) (i + 1));
  894. return r;
  895. }
  896. return 0;
  897. }
  898. arch_initcall(omap_init_dma);
  899. EXPORT_SYMBOL(omap_get_dma_src_pos);
  900. EXPORT_SYMBOL(omap_get_dma_dst_pos);
  901. EXPORT_SYMBOL(omap_clear_dma);
  902. EXPORT_SYMBOL(omap_set_dma_priority);
  903. EXPORT_SYMBOL(omap_request_dma);
  904. EXPORT_SYMBOL(omap_free_dma);
  905. EXPORT_SYMBOL(omap_start_dma);
  906. EXPORT_SYMBOL(omap_stop_dma);
  907. EXPORT_SYMBOL(omap_enable_dma_irq);
  908. EXPORT_SYMBOL(omap_disable_dma_irq);
  909. EXPORT_SYMBOL(omap_set_dma_transfer_params);
  910. EXPORT_SYMBOL(omap_set_dma_color_mode);
  911. EXPORT_SYMBOL(omap_set_dma_src_params);
  912. EXPORT_SYMBOL(omap_set_dma_src_index);
  913. EXPORT_SYMBOL(omap_set_dma_src_data_pack);
  914. EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
  915. EXPORT_SYMBOL(omap_set_dma_dest_params);
  916. EXPORT_SYMBOL(omap_set_dma_dest_index);
  917. EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
  918. EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
  919. EXPORT_SYMBOL(omap_dma_link_lch);
  920. EXPORT_SYMBOL(omap_dma_unlink_lch);
  921. EXPORT_SYMBOL(omap_request_lcd_dma);
  922. EXPORT_SYMBOL(omap_free_lcd_dma);
  923. EXPORT_SYMBOL(omap_enable_lcd_dma);
  924. EXPORT_SYMBOL(omap_setup_lcd_dma);
  925. EXPORT_SYMBOL(omap_stop_lcd_dma);
  926. EXPORT_SYMBOL(omap_set_lcd_dma_b1);
  927. EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
  928. EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
  929. EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
  930. EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
  931. EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
  932. EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);