dma.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * linux/arch/arm/plat-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 & 0x3f) == 0)
  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.callback != NULL)
  680. w |= 1 << 1; /* Block interrupt enable */
  681. else
  682. w &= ~(1 << 1);
  683. omap_writew(w, OMAP1610_DMA_LCD_CTRL);
  684. if (!(lcd_dma.rotate || lcd_dma.mirror ||
  685. lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
  686. return;
  687. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  688. /* Set the double-indexed addressing mode */
  689. w |= (0x03 << 12);
  690. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  691. omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
  692. omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
  693. omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
  694. }
  695. static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  696. {
  697. u16 w;
  698. w = omap_readw(OMAP1610_DMA_LCD_CTRL);
  699. if (unlikely(!(w & (1 << 3)))) {
  700. printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
  701. return IRQ_NONE;
  702. }
  703. /* Ack the IRQ */
  704. w |= (1 << 3);
  705. omap_writew(w, OMAP1610_DMA_LCD_CTRL);
  706. lcd_dma.active = 0;
  707. if (lcd_dma.callback != NULL)
  708. lcd_dma.callback(w, lcd_dma.cb_data);
  709. return IRQ_HANDLED;
  710. }
  711. int omap_request_lcd_dma(void (* callback)(u16 status, void *data),
  712. void *data)
  713. {
  714. spin_lock_irq(&lcd_dma.lock);
  715. if (lcd_dma.reserved) {
  716. spin_unlock_irq(&lcd_dma.lock);
  717. printk(KERN_ERR "LCD DMA channel already reserved\n");
  718. BUG();
  719. return -EBUSY;
  720. }
  721. lcd_dma.reserved = 1;
  722. spin_unlock_irq(&lcd_dma.lock);
  723. lcd_dma.callback = callback;
  724. lcd_dma.cb_data = data;
  725. lcd_dma.active = 0;
  726. lcd_dma.single_transfer = 0;
  727. lcd_dma.rotate = 0;
  728. lcd_dma.vxres = 0;
  729. lcd_dma.mirror = 0;
  730. lcd_dma.xscale = 0;
  731. lcd_dma.yscale = 0;
  732. lcd_dma.ext_ctrl = 0;
  733. lcd_dma.src_port = 0;
  734. return 0;
  735. }
  736. void omap_free_lcd_dma(void)
  737. {
  738. spin_lock(&lcd_dma.lock);
  739. if (!lcd_dma.reserved) {
  740. spin_unlock(&lcd_dma.lock);
  741. printk(KERN_ERR "LCD DMA is not reserved\n");
  742. BUG();
  743. return;
  744. }
  745. if (!enable_1510_mode)
  746. omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1, OMAP1610_DMA_LCD_CCR);
  747. lcd_dma.reserved = 0;
  748. spin_unlock(&lcd_dma.lock);
  749. }
  750. void omap_enable_lcd_dma(void)
  751. {
  752. u16 w;
  753. /* Set the Enable bit only if an external controller is
  754. * connected. Otherwise the OMAP internal controller will
  755. * start the transfer when it gets enabled.
  756. */
  757. if (enable_1510_mode || !lcd_dma.ext_ctrl)
  758. return;
  759. w = omap_readw(OMAP1610_DMA_LCD_CTRL);
  760. w |= 1 << 8;
  761. omap_writew(w, OMAP1610_DMA_LCD_CTRL);
  762. lcd_dma.active = 1;
  763. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  764. w |= 1 << 7;
  765. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  766. }
  767. void omap_setup_lcd_dma(void)
  768. {
  769. BUG_ON(lcd_dma.active);
  770. if (!enable_1510_mode) {
  771. /* Set some reasonable defaults */
  772. omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
  773. omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
  774. omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
  775. }
  776. set_b1_regs();
  777. if (!enable_1510_mode) {
  778. u16 w;
  779. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  780. /* If DMA was already active set the end_prog bit to have
  781. * the programmed register set loaded into the active
  782. * register set.
  783. */
  784. w |= 1 << 11; /* End_prog */
  785. if (!lcd_dma.single_transfer)
  786. w |= (3 << 8); /* Auto_init, repeat */
  787. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  788. }
  789. }
  790. void omap_stop_lcd_dma(void)
  791. {
  792. u16 w;
  793. lcd_dma.active = 0;
  794. if (enable_1510_mode || !lcd_dma.ext_ctrl)
  795. return;
  796. w = omap_readw(OMAP1610_DMA_LCD_CCR);
  797. w &= ~(1 << 7);
  798. omap_writew(w, OMAP1610_DMA_LCD_CCR);
  799. w = omap_readw(OMAP1610_DMA_LCD_CTRL);
  800. w &= ~(1 << 8);
  801. omap_writew(w, OMAP1610_DMA_LCD_CTRL);
  802. }
  803. /*
  804. * Clears any DMA state so the DMA engine is ready to restart with new buffers
  805. * through omap_start_dma(). Any buffers in flight are discarded.
  806. */
  807. void omap_clear_dma(int lch)
  808. {
  809. unsigned long flags;
  810. int status;
  811. local_irq_save(flags);
  812. omap_writew(omap_readw(OMAP_DMA_CCR(lch)) & ~OMAP_DMA_CCR_EN,
  813. OMAP_DMA_CCR(lch));
  814. status = OMAP_DMA_CSR(lch); /* clear pending interrupts */
  815. local_irq_restore(flags);
  816. }
  817. /*
  818. * Returns current physical source address for the given DMA channel.
  819. * If the channel is running the caller must disable interrupts prior calling
  820. * this function and process the returned value before re-enabling interrupt to
  821. * prevent races with the interrupt handler. Note that in continuous mode there
  822. * is a chance for CSSA_L register overflow inbetween the two reads resulting
  823. * in incorrect return value.
  824. */
  825. dma_addr_t omap_get_dma_src_pos(int lch)
  826. {
  827. return (dma_addr_t) (omap_readw(OMAP_DMA_CSSA_L(lch)) |
  828. (omap_readw(OMAP_DMA_CSSA_U(lch)) << 16));
  829. }
  830. /*
  831. * Returns current physical destination address for the given DMA channel.
  832. * If the channel is running the caller must disable interrupts prior calling
  833. * this function and process the returned value before re-enabling interrupt to
  834. * prevent races with the interrupt handler. Note that in continuous mode there
  835. * is a chance for CDSA_L register overflow inbetween the two reads resulting
  836. * in incorrect return value.
  837. */
  838. dma_addr_t omap_get_dma_dst_pos(int lch)
  839. {
  840. return (dma_addr_t) (omap_readw(OMAP_DMA_CDSA_L(lch)) |
  841. (omap_readw(OMAP_DMA_CDSA_U(lch)) << 16));
  842. }
  843. /*
  844. * Returns current source transfer counting for the given DMA channel.
  845. * Can be used to monitor the progress of a transfer inside a block.
  846. * It must be called with disabled interrupts.
  847. */
  848. int omap_get_dma_src_addr_counter(int lch)
  849. {
  850. return (dma_addr_t) omap_readw(OMAP_DMA_CSAC(lch));
  851. }
  852. int omap_dma_running(void)
  853. {
  854. int lch;
  855. /* Check if LCD DMA is running */
  856. if (cpu_is_omap16xx())
  857. if (omap_readw(OMAP1610_DMA_LCD_CCR) & OMAP_DMA_CCR_EN)
  858. return 1;
  859. for (lch = 0; lch < dma_chan_count; lch++) {
  860. u16 w;
  861. w = omap_readw(OMAP_DMA_CCR(lch));
  862. if (w & OMAP_DMA_CCR_EN)
  863. return 1;
  864. }
  865. return 0;
  866. }
  867. static int __init omap_init_dma(void)
  868. {
  869. int ch, r;
  870. if (cpu_is_omap1510()) {
  871. printk(KERN_INFO "DMA support for OMAP1510 initialized\n");
  872. dma_chan_count = 9;
  873. enable_1510_mode = 1;
  874. } else if (cpu_is_omap16xx() || cpu_is_omap730()) {
  875. printk(KERN_INFO "OMAP DMA hardware version %d\n",
  876. omap_readw(OMAP_DMA_HW_ID));
  877. printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
  878. (omap_readw(OMAP_DMA_CAPS_0_U) << 16) | omap_readw(OMAP_DMA_CAPS_0_L),
  879. (omap_readw(OMAP_DMA_CAPS_1_U) << 16) | omap_readw(OMAP_DMA_CAPS_1_L),
  880. omap_readw(OMAP_DMA_CAPS_2), omap_readw(OMAP_DMA_CAPS_3),
  881. omap_readw(OMAP_DMA_CAPS_4));
  882. if (!enable_1510_mode) {
  883. u16 w;
  884. /* Disable OMAP 3.0/3.1 compatibility mode. */
  885. w = omap_readw(OMAP_DMA_GSCR);
  886. w |= 1 << 3;
  887. omap_writew(w, OMAP_DMA_GSCR);
  888. dma_chan_count = 16;
  889. } else
  890. dma_chan_count = 9;
  891. } else {
  892. dma_chan_count = 0;
  893. return 0;
  894. }
  895. memset(&lcd_dma, 0, sizeof(lcd_dma));
  896. spin_lock_init(&lcd_dma.lock);
  897. spin_lock_init(&dma_chan_lock);
  898. memset(&dma_chan, 0, sizeof(dma_chan));
  899. for (ch = 0; ch < dma_chan_count; ch++) {
  900. dma_chan[ch].dev_id = -1;
  901. dma_chan[ch].next_lch = -1;
  902. if (ch >= 6 && enable_1510_mode)
  903. continue;
  904. /* request_irq() doesn't like dev_id (ie. ch) being zero,
  905. * so we have to kludge around this. */
  906. r = request_irq(dma_irq[ch], dma_irq_handler, 0, "DMA",
  907. (void *) (ch + 1));
  908. if (r != 0) {
  909. int i;
  910. printk(KERN_ERR "unable to request IRQ %d for DMA (error %d)\n",
  911. dma_irq[ch], r);
  912. for (i = 0; i < ch; i++)
  913. free_irq(dma_irq[i], (void *) (i + 1));
  914. return r;
  915. }
  916. }
  917. r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0, "LCD DMA", NULL);
  918. if (r != 0) {
  919. int i;
  920. printk(KERN_ERR "unable to request IRQ for LCD DMA (error %d)\n", r);
  921. for (i = 0; i < dma_chan_count; i++)
  922. free_irq(dma_irq[i], (void *) (i + 1));
  923. return r;
  924. }
  925. return 0;
  926. }
  927. arch_initcall(omap_init_dma);
  928. EXPORT_SYMBOL(omap_get_dma_src_pos);
  929. EXPORT_SYMBOL(omap_get_dma_dst_pos);
  930. EXPORT_SYMBOL(omap_get_dma_src_addr_counter);
  931. EXPORT_SYMBOL(omap_clear_dma);
  932. EXPORT_SYMBOL(omap_set_dma_priority);
  933. EXPORT_SYMBOL(omap_request_dma);
  934. EXPORT_SYMBOL(omap_free_dma);
  935. EXPORT_SYMBOL(omap_start_dma);
  936. EXPORT_SYMBOL(omap_stop_dma);
  937. EXPORT_SYMBOL(omap_enable_dma_irq);
  938. EXPORT_SYMBOL(omap_disable_dma_irq);
  939. EXPORT_SYMBOL(omap_set_dma_transfer_params);
  940. EXPORT_SYMBOL(omap_set_dma_color_mode);
  941. EXPORT_SYMBOL(omap_set_dma_src_params);
  942. EXPORT_SYMBOL(omap_set_dma_src_index);
  943. EXPORT_SYMBOL(omap_set_dma_src_data_pack);
  944. EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
  945. EXPORT_SYMBOL(omap_set_dma_dest_params);
  946. EXPORT_SYMBOL(omap_set_dma_dest_index);
  947. EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
  948. EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
  949. EXPORT_SYMBOL(omap_dma_link_lch);
  950. EXPORT_SYMBOL(omap_dma_unlink_lch);
  951. EXPORT_SYMBOL(omap_request_lcd_dma);
  952. EXPORT_SYMBOL(omap_free_lcd_dma);
  953. EXPORT_SYMBOL(omap_enable_lcd_dma);
  954. EXPORT_SYMBOL(omap_setup_lcd_dma);
  955. EXPORT_SYMBOL(omap_stop_lcd_dma);
  956. EXPORT_SYMBOL(omap_set_lcd_dma_b1);
  957. EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
  958. EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
  959. EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
  960. EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
  961. EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
  962. EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);