bfin_dma_5xx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * File: arch/blackfin/kernel/bfin_dma_5xx.c
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description: This file contains the simple DMA Implementation for Blackfin
  8. *
  9. * Modified:
  10. * Copyright 2004-2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/errno.h>
  30. #include <linux/module.h>
  31. #include <linux/sched.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/kernel.h>
  34. #include <linux/param.h>
  35. #include <asm/dma.h>
  36. #include <asm/cacheflush.h>
  37. /* Remove unused code not exported by symbol or internally called */
  38. #define REMOVE_DEAD_CODE
  39. /**************************************************************************
  40. * Global Variables
  41. ***************************************************************************/
  42. static struct dma_channel dma_ch[MAX_BLACKFIN_DMA_CHANNEL];
  43. #if defined (CONFIG_BF561)
  44. static struct dma_register *base_addr[MAX_BLACKFIN_DMA_CHANNEL] = {
  45. (struct dma_register *) DMA1_0_NEXT_DESC_PTR,
  46. (struct dma_register *) DMA1_1_NEXT_DESC_PTR,
  47. (struct dma_register *) DMA1_2_NEXT_DESC_PTR,
  48. (struct dma_register *) DMA1_3_NEXT_DESC_PTR,
  49. (struct dma_register *) DMA1_4_NEXT_DESC_PTR,
  50. (struct dma_register *) DMA1_5_NEXT_DESC_PTR,
  51. (struct dma_register *) DMA1_6_NEXT_DESC_PTR,
  52. (struct dma_register *) DMA1_7_NEXT_DESC_PTR,
  53. (struct dma_register *) DMA1_8_NEXT_DESC_PTR,
  54. (struct dma_register *) DMA1_9_NEXT_DESC_PTR,
  55. (struct dma_register *) DMA1_10_NEXT_DESC_PTR,
  56. (struct dma_register *) DMA1_11_NEXT_DESC_PTR,
  57. (struct dma_register *) DMA2_0_NEXT_DESC_PTR,
  58. (struct dma_register *) DMA2_1_NEXT_DESC_PTR,
  59. (struct dma_register *) DMA2_2_NEXT_DESC_PTR,
  60. (struct dma_register *) DMA2_3_NEXT_DESC_PTR,
  61. (struct dma_register *) DMA2_4_NEXT_DESC_PTR,
  62. (struct dma_register *) DMA2_5_NEXT_DESC_PTR,
  63. (struct dma_register *) DMA2_6_NEXT_DESC_PTR,
  64. (struct dma_register *) DMA2_7_NEXT_DESC_PTR,
  65. (struct dma_register *) DMA2_8_NEXT_DESC_PTR,
  66. (struct dma_register *) DMA2_9_NEXT_DESC_PTR,
  67. (struct dma_register *) DMA2_10_NEXT_DESC_PTR,
  68. (struct dma_register *) DMA2_11_NEXT_DESC_PTR,
  69. (struct dma_register *) MDMA1_D0_NEXT_DESC_PTR,
  70. (struct dma_register *) MDMA1_S0_NEXT_DESC_PTR,
  71. (struct dma_register *) MDMA1_D1_NEXT_DESC_PTR,
  72. (struct dma_register *) MDMA1_S1_NEXT_DESC_PTR,
  73. (struct dma_register *) MDMA2_D0_NEXT_DESC_PTR,
  74. (struct dma_register *) MDMA2_S0_NEXT_DESC_PTR,
  75. (struct dma_register *) MDMA2_D1_NEXT_DESC_PTR,
  76. (struct dma_register *) MDMA2_S1_NEXT_DESC_PTR,
  77. (struct dma_register *) IMDMA_D0_NEXT_DESC_PTR,
  78. (struct dma_register *) IMDMA_S0_NEXT_DESC_PTR,
  79. (struct dma_register *) IMDMA_D1_NEXT_DESC_PTR,
  80. (struct dma_register *) IMDMA_S1_NEXT_DESC_PTR,
  81. };
  82. #else
  83. static struct dma_register *base_addr[MAX_BLACKFIN_DMA_CHANNEL] = {
  84. (struct dma_register *) DMA0_NEXT_DESC_PTR,
  85. (struct dma_register *) DMA1_NEXT_DESC_PTR,
  86. (struct dma_register *) DMA2_NEXT_DESC_PTR,
  87. (struct dma_register *) DMA3_NEXT_DESC_PTR,
  88. (struct dma_register *) DMA4_NEXT_DESC_PTR,
  89. (struct dma_register *) DMA5_NEXT_DESC_PTR,
  90. (struct dma_register *) DMA6_NEXT_DESC_PTR,
  91. (struct dma_register *) DMA7_NEXT_DESC_PTR,
  92. #if (defined(CONFIG_BF537) || defined(CONFIG_BF534) || defined(CONFIG_BF536))
  93. (struct dma_register *) DMA8_NEXT_DESC_PTR,
  94. (struct dma_register *) DMA9_NEXT_DESC_PTR,
  95. (struct dma_register *) DMA10_NEXT_DESC_PTR,
  96. (struct dma_register *) DMA11_NEXT_DESC_PTR,
  97. #endif
  98. (struct dma_register *) MDMA_D0_NEXT_DESC_PTR,
  99. (struct dma_register *) MDMA_S0_NEXT_DESC_PTR,
  100. (struct dma_register *) MDMA_D1_NEXT_DESC_PTR,
  101. (struct dma_register *) MDMA_S1_NEXT_DESC_PTR,
  102. };
  103. #endif
  104. /*------------------------------------------------------------------------------
  105. * Set the Buffer Clear bit in the Configuration register of specific DMA
  106. * channel. This will stop the descriptor based DMA operation.
  107. *-----------------------------------------------------------------------------*/
  108. static void clear_dma_buffer(unsigned int channel)
  109. {
  110. dma_ch[channel].regs->cfg |= RESTART;
  111. SSYNC();
  112. dma_ch[channel].regs->cfg &= ~RESTART;
  113. SSYNC();
  114. }
  115. int __init blackfin_dma_init(void)
  116. {
  117. int i;
  118. printk(KERN_INFO "Blackfin DMA Controller\n");
  119. for (i = 0; i < MAX_BLACKFIN_DMA_CHANNEL; i++) {
  120. dma_ch[i].chan_status = DMA_CHANNEL_FREE;
  121. dma_ch[i].regs = base_addr[i];
  122. mutex_init(&(dma_ch[i].dmalock));
  123. }
  124. return 0;
  125. }
  126. arch_initcall(blackfin_dma_init);
  127. /*
  128. * Form the channel find the irq number for that channel.
  129. */
  130. #if !defined(CONFIG_BF561)
  131. static int bf533_channel2irq(unsigned int channel)
  132. {
  133. int ret_irq = -1;
  134. switch (channel) {
  135. case CH_PPI:
  136. ret_irq = IRQ_PPI;
  137. break;
  138. #if (defined(CONFIG_BF537) || defined(CONFIG_BF534) || defined(CONFIG_BF536))
  139. case CH_EMAC_RX:
  140. ret_irq = IRQ_MAC_RX;
  141. break;
  142. case CH_EMAC_TX:
  143. ret_irq = IRQ_MAC_TX;
  144. break;
  145. case CH_UART1_RX:
  146. ret_irq = IRQ_UART1_RX;
  147. break;
  148. case CH_UART1_TX:
  149. ret_irq = IRQ_UART1_TX;
  150. break;
  151. #endif
  152. case CH_SPORT0_RX:
  153. ret_irq = IRQ_SPORT0_RX;
  154. break;
  155. case CH_SPORT0_TX:
  156. ret_irq = IRQ_SPORT0_TX;
  157. break;
  158. case CH_SPORT1_RX:
  159. ret_irq = IRQ_SPORT1_RX;
  160. break;
  161. case CH_SPORT1_TX:
  162. ret_irq = IRQ_SPORT1_TX;
  163. break;
  164. case CH_SPI:
  165. ret_irq = IRQ_SPI;
  166. break;
  167. case CH_UART_RX:
  168. ret_irq = IRQ_UART_RX;
  169. break;
  170. case CH_UART_TX:
  171. ret_irq = IRQ_UART_TX;
  172. break;
  173. case CH_MEM_STREAM0_SRC:
  174. case CH_MEM_STREAM0_DEST:
  175. ret_irq = IRQ_MEM_DMA0;
  176. break;
  177. case CH_MEM_STREAM1_SRC:
  178. case CH_MEM_STREAM1_DEST:
  179. ret_irq = IRQ_MEM_DMA1;
  180. break;
  181. }
  182. return ret_irq;
  183. }
  184. # define channel2irq(channel) bf533_channel2irq(channel)
  185. #else
  186. static int bf561_channel2irq(unsigned int channel)
  187. {
  188. int ret_irq = -1;
  189. switch (channel) {
  190. case CH_PPI0:
  191. ret_irq = IRQ_PPI0;
  192. break;
  193. case CH_PPI1:
  194. ret_irq = IRQ_PPI1;
  195. break;
  196. case CH_SPORT0_RX:
  197. ret_irq = IRQ_SPORT0_RX;
  198. break;
  199. case CH_SPORT0_TX:
  200. ret_irq = IRQ_SPORT0_TX;
  201. break;
  202. case CH_SPORT1_RX:
  203. ret_irq = IRQ_SPORT1_RX;
  204. break;
  205. case CH_SPORT1_TX:
  206. ret_irq = IRQ_SPORT1_TX;
  207. break;
  208. case CH_SPI:
  209. ret_irq = IRQ_SPI;
  210. break;
  211. case CH_UART_RX:
  212. ret_irq = IRQ_UART_RX;
  213. break;
  214. case CH_UART_TX:
  215. ret_irq = IRQ_UART_TX;
  216. break;
  217. case CH_MEM_STREAM0_SRC:
  218. case CH_MEM_STREAM0_DEST:
  219. ret_irq = IRQ_MEM_DMA0;
  220. break;
  221. case CH_MEM_STREAM1_SRC:
  222. case CH_MEM_STREAM1_DEST:
  223. ret_irq = IRQ_MEM_DMA1;
  224. break;
  225. case CH_MEM_STREAM2_SRC:
  226. case CH_MEM_STREAM2_DEST:
  227. ret_irq = IRQ_MEM_DMA2;
  228. break;
  229. case CH_MEM_STREAM3_SRC:
  230. case CH_MEM_STREAM3_DEST:
  231. ret_irq = IRQ_MEM_DMA3;
  232. break;
  233. case CH_IMEM_STREAM0_SRC:
  234. case CH_IMEM_STREAM0_DEST:
  235. ret_irq = IRQ_IMEM_DMA0;
  236. break;
  237. case CH_IMEM_STREAM1_SRC:
  238. case CH_IMEM_STREAM1_DEST:
  239. ret_irq = IRQ_IMEM_DMA1;
  240. break;
  241. }
  242. return ret_irq;
  243. }
  244. # define channel2irq(channel) bf561_channel2irq(channel)
  245. #endif
  246. /*------------------------------------------------------------------------------
  247. * Request the specific DMA channel from the system.
  248. *-----------------------------------------------------------------------------*/
  249. int request_dma(unsigned int channel, char *device_id)
  250. {
  251. pr_debug("request_dma() : BEGIN \n");
  252. mutex_lock(&(dma_ch[channel].dmalock));
  253. if ((dma_ch[channel].chan_status == DMA_CHANNEL_REQUESTED)
  254. || (dma_ch[channel].chan_status == DMA_CHANNEL_ENABLED)) {
  255. mutex_unlock(&(dma_ch[channel].dmalock));
  256. pr_debug("DMA CHANNEL IN USE \n");
  257. return -EBUSY;
  258. } else {
  259. dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
  260. pr_debug("DMA CHANNEL IS ALLOCATED \n");
  261. }
  262. mutex_unlock(&(dma_ch[channel].dmalock));
  263. dma_ch[channel].device_id = device_id;
  264. dma_ch[channel].irq_callback = NULL;
  265. /* This is to be enabled by putting a restriction -
  266. * you have to request DMA, before doing any operations on
  267. * descriptor/channel
  268. */
  269. pr_debug("request_dma() : END \n");
  270. return channel;
  271. }
  272. EXPORT_SYMBOL(request_dma);
  273. int set_dma_callback(unsigned int channel, dma_interrupt_t callback, void *data)
  274. {
  275. int ret_irq = 0;
  276. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  277. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  278. if (callback != NULL) {
  279. int ret_val;
  280. ret_irq = channel2irq(channel);
  281. dma_ch[channel].data = data;
  282. ret_val =
  283. request_irq(ret_irq, (void *)callback, IRQF_DISABLED,
  284. dma_ch[channel].device_id, data);
  285. if (ret_val) {
  286. printk(KERN_NOTICE
  287. "Request irq in DMA engine failed.\n");
  288. return -EPERM;
  289. }
  290. dma_ch[channel].irq_callback = callback;
  291. }
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(set_dma_callback);
  295. void free_dma(unsigned int channel)
  296. {
  297. int ret_irq;
  298. pr_debug("freedma() : BEGIN \n");
  299. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  300. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  301. /* Halt the DMA */
  302. disable_dma(channel);
  303. clear_dma_buffer(channel);
  304. if (dma_ch[channel].irq_callback != NULL) {
  305. ret_irq = channel2irq(channel);
  306. free_irq(ret_irq, dma_ch[channel].data);
  307. }
  308. /* Clear the DMA Variable in the Channel */
  309. mutex_lock(&(dma_ch[channel].dmalock));
  310. dma_ch[channel].chan_status = DMA_CHANNEL_FREE;
  311. mutex_unlock(&(dma_ch[channel].dmalock));
  312. pr_debug("freedma() : END \n");
  313. }
  314. EXPORT_SYMBOL(free_dma);
  315. void dma_enable_irq(unsigned int channel)
  316. {
  317. int ret_irq;
  318. pr_debug("dma_enable_irq() : BEGIN \n");
  319. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  320. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  321. ret_irq = channel2irq(channel);
  322. enable_irq(ret_irq);
  323. }
  324. EXPORT_SYMBOL(dma_enable_irq);
  325. void dma_disable_irq(unsigned int channel)
  326. {
  327. int ret_irq;
  328. pr_debug("dma_disable_irq() : BEGIN \n");
  329. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  330. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  331. ret_irq = channel2irq(channel);
  332. disable_irq(ret_irq);
  333. }
  334. EXPORT_SYMBOL(dma_disable_irq);
  335. int dma_channel_active(unsigned int channel)
  336. {
  337. if (dma_ch[channel].chan_status == DMA_CHANNEL_FREE) {
  338. return 0;
  339. } else {
  340. return 1;
  341. }
  342. }
  343. EXPORT_SYMBOL(dma_channel_active);
  344. /*------------------------------------------------------------------------------
  345. * stop the specific DMA channel.
  346. *-----------------------------------------------------------------------------*/
  347. void disable_dma(unsigned int channel)
  348. {
  349. pr_debug("stop_dma() : BEGIN \n");
  350. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  351. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  352. dma_ch[channel].regs->cfg &= ~DMAEN; /* Clean the enable bit */
  353. SSYNC();
  354. dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
  355. /* Needs to be enabled Later */
  356. pr_debug("stop_dma() : END \n");
  357. return;
  358. }
  359. EXPORT_SYMBOL(disable_dma);
  360. void enable_dma(unsigned int channel)
  361. {
  362. pr_debug("enable_dma() : BEGIN \n");
  363. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  364. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  365. dma_ch[channel].chan_status = DMA_CHANNEL_ENABLED;
  366. dma_ch[channel].regs->curr_x_count = 0;
  367. dma_ch[channel].regs->curr_y_count = 0;
  368. dma_ch[channel].regs->cfg |= DMAEN; /* Set the enable bit */
  369. SSYNC();
  370. pr_debug("enable_dma() : END \n");
  371. return;
  372. }
  373. EXPORT_SYMBOL(enable_dma);
  374. /*------------------------------------------------------------------------------
  375. * Set the Start Address register for the specific DMA channel
  376. * This function can be used for register based DMA,
  377. * to setup the start address
  378. * addr: Starting address of the DMA Data to be transferred.
  379. *-----------------------------------------------------------------------------*/
  380. void set_dma_start_addr(unsigned int channel, unsigned long addr)
  381. {
  382. pr_debug("set_dma_start_addr() : BEGIN \n");
  383. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  384. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  385. dma_ch[channel].regs->start_addr = addr;
  386. SSYNC();
  387. pr_debug("set_dma_start_addr() : END\n");
  388. }
  389. EXPORT_SYMBOL(set_dma_start_addr);
  390. void set_dma_next_desc_addr(unsigned int channel, unsigned long addr)
  391. {
  392. pr_debug("set_dma_next_desc_addr() : BEGIN \n");
  393. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  394. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  395. dma_ch[channel].regs->next_desc_ptr = addr;
  396. SSYNC();
  397. pr_debug("set_dma_start_addr() : END\n");
  398. }
  399. EXPORT_SYMBOL(set_dma_next_desc_addr);
  400. void set_dma_x_count(unsigned int channel, unsigned short x_count)
  401. {
  402. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  403. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  404. dma_ch[channel].regs->x_count = x_count;
  405. SSYNC();
  406. }
  407. EXPORT_SYMBOL(set_dma_x_count);
  408. void set_dma_y_count(unsigned int channel, unsigned short y_count)
  409. {
  410. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  411. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  412. dma_ch[channel].regs->y_count = y_count;
  413. SSYNC();
  414. }
  415. EXPORT_SYMBOL(set_dma_y_count);
  416. void set_dma_x_modify(unsigned int channel, short x_modify)
  417. {
  418. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  419. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  420. dma_ch[channel].regs->x_modify = x_modify;
  421. SSYNC();
  422. }
  423. EXPORT_SYMBOL(set_dma_x_modify);
  424. void set_dma_y_modify(unsigned int channel, short y_modify)
  425. {
  426. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  427. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  428. dma_ch[channel].regs->y_modify = y_modify;
  429. SSYNC();
  430. }
  431. EXPORT_SYMBOL(set_dma_y_modify);
  432. void set_dma_config(unsigned int channel, unsigned short config)
  433. {
  434. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  435. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  436. dma_ch[channel].regs->cfg = config;
  437. SSYNC();
  438. }
  439. EXPORT_SYMBOL(set_dma_config);
  440. unsigned short
  441. set_bfin_dma_config(char direction, char flow_mode,
  442. char intr_mode, char dma_mode, char width)
  443. {
  444. unsigned short config;
  445. config =
  446. ((direction << 1) | (width << 2) | (dma_mode << 4) |
  447. (intr_mode << 6) | (flow_mode << 12) | RESTART);
  448. return config;
  449. }
  450. EXPORT_SYMBOL(set_bfin_dma_config);
  451. void set_dma_sg(unsigned int channel, struct dmasg * sg, int nr_sg)
  452. {
  453. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  454. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  455. dma_ch[channel].regs->cfg |= ((nr_sg & 0x0F) << 8);
  456. dma_ch[channel].regs->next_desc_ptr = (unsigned int)sg;
  457. SSYNC();
  458. }
  459. EXPORT_SYMBOL(set_dma_sg);
  460. /*------------------------------------------------------------------------------
  461. * Get the DMA status of a specific DMA channel from the system.
  462. *-----------------------------------------------------------------------------*/
  463. unsigned short get_dma_curr_irqstat(unsigned int channel)
  464. {
  465. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  466. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  467. return dma_ch[channel].regs->irq_status;
  468. }
  469. EXPORT_SYMBOL(get_dma_curr_irqstat);
  470. /*------------------------------------------------------------------------------
  471. * Clear the DMA_DONE bit in DMA status. Stop the DMA completion interrupt.
  472. *-----------------------------------------------------------------------------*/
  473. void clear_dma_irqstat(unsigned int channel)
  474. {
  475. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  476. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  477. dma_ch[channel].regs->irq_status |= 3;
  478. }
  479. EXPORT_SYMBOL(clear_dma_irqstat);
  480. /*------------------------------------------------------------------------------
  481. * Get current DMA xcount of a specific DMA channel from the system.
  482. *-----------------------------------------------------------------------------*/
  483. unsigned short get_dma_curr_xcount(unsigned int channel)
  484. {
  485. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  486. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  487. return dma_ch[channel].regs->curr_x_count;
  488. }
  489. EXPORT_SYMBOL(get_dma_curr_xcount);
  490. /*------------------------------------------------------------------------------
  491. * Get current DMA ycount of a specific DMA channel from the system.
  492. *-----------------------------------------------------------------------------*/
  493. unsigned short get_dma_curr_ycount(unsigned int channel)
  494. {
  495. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  496. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  497. return dma_ch[channel].regs->curr_y_count;
  498. }
  499. EXPORT_SYMBOL(get_dma_curr_ycount);
  500. void *dma_memcpy(void *dest, const void *src, size_t size)
  501. {
  502. int direction; /* 1 - address decrease, 0 - address increase */
  503. int flag_align; /* 1 - address aligned, 0 - address unaligned */
  504. int flag_2D; /* 1 - 2D DMA needed, 0 - 1D DMA needed */
  505. if (size <= 0)
  506. return NULL;
  507. if ((unsigned long)src < memory_end)
  508. blackfin_dcache_flush_range((unsigned int)src,
  509. (unsigned int)(src + size));
  510. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  511. if ((unsigned long)src < (unsigned long)dest)
  512. direction = 1;
  513. else
  514. direction = 0;
  515. if ((((unsigned long)dest % 2) == 0) && (((unsigned long)src % 2) == 0)
  516. && ((size % 2) == 0))
  517. flag_align = 1;
  518. else
  519. flag_align = 0;
  520. if (size > 0x10000) /* size > 64K */
  521. flag_2D = 1;
  522. else
  523. flag_2D = 0;
  524. /* Setup destination and source start address */
  525. if (direction) {
  526. if (flag_align) {
  527. bfin_write_MDMA_D0_START_ADDR(dest + size - 2);
  528. bfin_write_MDMA_S0_START_ADDR(src + size - 2);
  529. } else {
  530. bfin_write_MDMA_D0_START_ADDR(dest + size - 1);
  531. bfin_write_MDMA_S0_START_ADDR(src + size - 1);
  532. }
  533. } else {
  534. bfin_write_MDMA_D0_START_ADDR(dest);
  535. bfin_write_MDMA_S0_START_ADDR(src);
  536. }
  537. /* Setup destination and source xcount */
  538. if (flag_2D) {
  539. if (flag_align) {
  540. bfin_write_MDMA_D0_X_COUNT(1024 / 2);
  541. bfin_write_MDMA_S0_X_COUNT(1024 / 2);
  542. } else {
  543. bfin_write_MDMA_D0_X_COUNT(1024);
  544. bfin_write_MDMA_S0_X_COUNT(1024);
  545. }
  546. bfin_write_MDMA_D0_Y_COUNT(size >> 10);
  547. bfin_write_MDMA_S0_Y_COUNT(size >> 10);
  548. } else {
  549. if (flag_align) {
  550. bfin_write_MDMA_D0_X_COUNT(size / 2);
  551. bfin_write_MDMA_S0_X_COUNT(size / 2);
  552. } else {
  553. bfin_write_MDMA_D0_X_COUNT(size);
  554. bfin_write_MDMA_S0_X_COUNT(size);
  555. }
  556. }
  557. /* Setup destination and source xmodify and ymodify */
  558. if (direction) {
  559. if (flag_align) {
  560. bfin_write_MDMA_D0_X_MODIFY(-2);
  561. bfin_write_MDMA_S0_X_MODIFY(-2);
  562. if (flag_2D) {
  563. bfin_write_MDMA_D0_Y_MODIFY(-2);
  564. bfin_write_MDMA_S0_Y_MODIFY(-2);
  565. }
  566. } else {
  567. bfin_write_MDMA_D0_X_MODIFY(-1);
  568. bfin_write_MDMA_S0_X_MODIFY(-1);
  569. if (flag_2D) {
  570. bfin_write_MDMA_D0_Y_MODIFY(-1);
  571. bfin_write_MDMA_S0_Y_MODIFY(-1);
  572. }
  573. }
  574. } else {
  575. if (flag_align) {
  576. bfin_write_MDMA_D0_X_MODIFY(2);
  577. bfin_write_MDMA_S0_X_MODIFY(2);
  578. if (flag_2D) {
  579. bfin_write_MDMA_D0_Y_MODIFY(2);
  580. bfin_write_MDMA_S0_Y_MODIFY(2);
  581. }
  582. } else {
  583. bfin_write_MDMA_D0_X_MODIFY(1);
  584. bfin_write_MDMA_S0_X_MODIFY(1);
  585. if (flag_2D) {
  586. bfin_write_MDMA_D0_Y_MODIFY(1);
  587. bfin_write_MDMA_S0_Y_MODIFY(1);
  588. }
  589. }
  590. }
  591. /* Enable source DMA */
  592. if (flag_2D) {
  593. if (flag_align) {
  594. bfin_write_MDMA_S0_CONFIG(DMAEN | DMA2D | WDSIZE_16);
  595. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | DMA2D | WDSIZE_16);
  596. } else {
  597. bfin_write_MDMA_S0_CONFIG(DMAEN | DMA2D);
  598. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | DMA2D);
  599. }
  600. } else {
  601. if (flag_align) {
  602. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_16);
  603. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_16);
  604. } else {
  605. bfin_write_MDMA_S0_CONFIG(DMAEN);
  606. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN);
  607. }
  608. }
  609. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  610. ;
  611. bfin_write_MDMA_D0_IRQ_STATUS(bfin_read_MDMA_D0_IRQ_STATUS() |
  612. (DMA_DONE | DMA_ERR));
  613. bfin_write_MDMA_S0_CONFIG(0);
  614. bfin_write_MDMA_D0_CONFIG(0);
  615. if ((unsigned long)dest < memory_end)
  616. blackfin_dcache_invalidate_range((unsigned int)dest,
  617. (unsigned int)(dest + size));
  618. return dest;
  619. }
  620. EXPORT_SYMBOL(dma_memcpy);
  621. void *safe_dma_memcpy(void *dest, const void *src, size_t size)
  622. {
  623. int flags = 0;
  624. void *addr;
  625. local_irq_save(flags);
  626. addr = dma_memcpy(dest, src, size);
  627. local_irq_restore(flags);
  628. return addr;
  629. }
  630. EXPORT_SYMBOL(safe_dma_memcpy);