bfin_dma_5xx.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. static 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. /* Mark MEMDMA Channel 0 as requested since we're using it internally */
  125. dma_ch[CH_MEM_STREAM0_DEST].chan_status = DMA_CHANNEL_REQUESTED;
  126. dma_ch[CH_MEM_STREAM0_SRC].chan_status = DMA_CHANNEL_REQUESTED;
  127. return 0;
  128. }
  129. arch_initcall(blackfin_dma_init);
  130. /*
  131. * Form the channel find the irq number for that channel.
  132. */
  133. #if !defined(CONFIG_BF561)
  134. static int bf533_channel2irq(unsigned int channel)
  135. {
  136. int ret_irq = -1;
  137. switch (channel) {
  138. case CH_PPI:
  139. ret_irq = IRQ_PPI;
  140. break;
  141. #if (defined(CONFIG_BF537) || defined(CONFIG_BF534) || defined(CONFIG_BF536))
  142. case CH_EMAC_RX:
  143. ret_irq = IRQ_MAC_RX;
  144. break;
  145. case CH_EMAC_TX:
  146. ret_irq = IRQ_MAC_TX;
  147. break;
  148. case CH_UART1_RX:
  149. ret_irq = IRQ_UART1_RX;
  150. break;
  151. case CH_UART1_TX:
  152. ret_irq = IRQ_UART1_TX;
  153. break;
  154. #endif
  155. case CH_SPORT0_RX:
  156. ret_irq = IRQ_SPORT0_RX;
  157. break;
  158. case CH_SPORT0_TX:
  159. ret_irq = IRQ_SPORT0_TX;
  160. break;
  161. case CH_SPORT1_RX:
  162. ret_irq = IRQ_SPORT1_RX;
  163. break;
  164. case CH_SPORT1_TX:
  165. ret_irq = IRQ_SPORT1_TX;
  166. break;
  167. case CH_SPI:
  168. ret_irq = IRQ_SPI;
  169. break;
  170. case CH_UART_RX:
  171. ret_irq = IRQ_UART_RX;
  172. break;
  173. case CH_UART_TX:
  174. ret_irq = IRQ_UART_TX;
  175. break;
  176. case CH_MEM_STREAM0_SRC:
  177. case CH_MEM_STREAM0_DEST:
  178. ret_irq = IRQ_MEM_DMA0;
  179. break;
  180. case CH_MEM_STREAM1_SRC:
  181. case CH_MEM_STREAM1_DEST:
  182. ret_irq = IRQ_MEM_DMA1;
  183. break;
  184. }
  185. return ret_irq;
  186. }
  187. # define channel2irq(channel) bf533_channel2irq(channel)
  188. #else
  189. static int bf561_channel2irq(unsigned int channel)
  190. {
  191. int ret_irq = -1;
  192. switch (channel) {
  193. case CH_PPI0:
  194. ret_irq = IRQ_PPI0;
  195. break;
  196. case CH_PPI1:
  197. ret_irq = IRQ_PPI1;
  198. break;
  199. case CH_SPORT0_RX:
  200. ret_irq = IRQ_SPORT0_RX;
  201. break;
  202. case CH_SPORT0_TX:
  203. ret_irq = IRQ_SPORT0_TX;
  204. break;
  205. case CH_SPORT1_RX:
  206. ret_irq = IRQ_SPORT1_RX;
  207. break;
  208. case CH_SPORT1_TX:
  209. ret_irq = IRQ_SPORT1_TX;
  210. break;
  211. case CH_SPI:
  212. ret_irq = IRQ_SPI;
  213. break;
  214. case CH_UART_RX:
  215. ret_irq = IRQ_UART_RX;
  216. break;
  217. case CH_UART_TX:
  218. ret_irq = IRQ_UART_TX;
  219. break;
  220. case CH_MEM_STREAM0_SRC:
  221. case CH_MEM_STREAM0_DEST:
  222. ret_irq = IRQ_MEM_DMA0;
  223. break;
  224. case CH_MEM_STREAM1_SRC:
  225. case CH_MEM_STREAM1_DEST:
  226. ret_irq = IRQ_MEM_DMA1;
  227. break;
  228. case CH_MEM_STREAM2_SRC:
  229. case CH_MEM_STREAM2_DEST:
  230. ret_irq = IRQ_MEM_DMA2;
  231. break;
  232. case CH_MEM_STREAM3_SRC:
  233. case CH_MEM_STREAM3_DEST:
  234. ret_irq = IRQ_MEM_DMA3;
  235. break;
  236. case CH_IMEM_STREAM0_SRC:
  237. case CH_IMEM_STREAM0_DEST:
  238. ret_irq = IRQ_IMEM_DMA0;
  239. break;
  240. case CH_IMEM_STREAM1_SRC:
  241. case CH_IMEM_STREAM1_DEST:
  242. ret_irq = IRQ_IMEM_DMA1;
  243. break;
  244. }
  245. return ret_irq;
  246. }
  247. # define channel2irq(channel) bf561_channel2irq(channel)
  248. #endif
  249. /*------------------------------------------------------------------------------
  250. * Request the specific DMA channel from the system.
  251. *-----------------------------------------------------------------------------*/
  252. int request_dma(unsigned int channel, char *device_id)
  253. {
  254. pr_debug("request_dma() : BEGIN \n");
  255. mutex_lock(&(dma_ch[channel].dmalock));
  256. if ((dma_ch[channel].chan_status == DMA_CHANNEL_REQUESTED)
  257. || (dma_ch[channel].chan_status == DMA_CHANNEL_ENABLED)) {
  258. mutex_unlock(&(dma_ch[channel].dmalock));
  259. pr_debug("DMA CHANNEL IN USE \n");
  260. return -EBUSY;
  261. } else {
  262. dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
  263. pr_debug("DMA CHANNEL IS ALLOCATED \n");
  264. }
  265. mutex_unlock(&(dma_ch[channel].dmalock));
  266. dma_ch[channel].device_id = device_id;
  267. dma_ch[channel].irq_callback = NULL;
  268. /* This is to be enabled by putting a restriction -
  269. * you have to request DMA, before doing any operations on
  270. * descriptor/channel
  271. */
  272. pr_debug("request_dma() : END \n");
  273. return channel;
  274. }
  275. EXPORT_SYMBOL(request_dma);
  276. int set_dma_callback(unsigned int channel, dma_interrupt_t callback, void *data)
  277. {
  278. int ret_irq = 0;
  279. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  280. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  281. if (callback != NULL) {
  282. int ret_val;
  283. ret_irq = channel2irq(channel);
  284. dma_ch[channel].data = data;
  285. ret_val =
  286. request_irq(ret_irq, (void *)callback, IRQF_DISABLED,
  287. dma_ch[channel].device_id, data);
  288. if (ret_val) {
  289. printk(KERN_NOTICE
  290. "Request irq in DMA engine failed.\n");
  291. return -EPERM;
  292. }
  293. dma_ch[channel].irq_callback = callback;
  294. }
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(set_dma_callback);
  298. void free_dma(unsigned int channel)
  299. {
  300. int ret_irq;
  301. pr_debug("freedma() : BEGIN \n");
  302. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  303. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  304. /* Halt the DMA */
  305. disable_dma(channel);
  306. clear_dma_buffer(channel);
  307. if (dma_ch[channel].irq_callback != NULL) {
  308. ret_irq = channel2irq(channel);
  309. free_irq(ret_irq, dma_ch[channel].data);
  310. }
  311. /* Clear the DMA Variable in the Channel */
  312. mutex_lock(&(dma_ch[channel].dmalock));
  313. dma_ch[channel].chan_status = DMA_CHANNEL_FREE;
  314. mutex_unlock(&(dma_ch[channel].dmalock));
  315. pr_debug("freedma() : END \n");
  316. }
  317. EXPORT_SYMBOL(free_dma);
  318. void dma_enable_irq(unsigned int channel)
  319. {
  320. int ret_irq;
  321. pr_debug("dma_enable_irq() : BEGIN \n");
  322. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  323. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  324. ret_irq = channel2irq(channel);
  325. enable_irq(ret_irq);
  326. }
  327. EXPORT_SYMBOL(dma_enable_irq);
  328. void dma_disable_irq(unsigned int channel)
  329. {
  330. int ret_irq;
  331. pr_debug("dma_disable_irq() : BEGIN \n");
  332. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  333. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  334. ret_irq = channel2irq(channel);
  335. disable_irq(ret_irq);
  336. }
  337. EXPORT_SYMBOL(dma_disable_irq);
  338. int dma_channel_active(unsigned int channel)
  339. {
  340. if (dma_ch[channel].chan_status == DMA_CHANNEL_FREE) {
  341. return 0;
  342. } else {
  343. return 1;
  344. }
  345. }
  346. EXPORT_SYMBOL(dma_channel_active);
  347. /*------------------------------------------------------------------------------
  348. * stop the specific DMA channel.
  349. *-----------------------------------------------------------------------------*/
  350. void disable_dma(unsigned int channel)
  351. {
  352. pr_debug("stop_dma() : BEGIN \n");
  353. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  354. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  355. dma_ch[channel].regs->cfg &= ~DMAEN; /* Clean the enable bit */
  356. SSYNC();
  357. dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
  358. /* Needs to be enabled Later */
  359. pr_debug("stop_dma() : END \n");
  360. return;
  361. }
  362. EXPORT_SYMBOL(disable_dma);
  363. void enable_dma(unsigned int channel)
  364. {
  365. pr_debug("enable_dma() : BEGIN \n");
  366. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  367. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  368. dma_ch[channel].chan_status = DMA_CHANNEL_ENABLED;
  369. dma_ch[channel].regs->curr_x_count = 0;
  370. dma_ch[channel].regs->curr_y_count = 0;
  371. dma_ch[channel].regs->cfg |= DMAEN; /* Set the enable bit */
  372. SSYNC();
  373. pr_debug("enable_dma() : END \n");
  374. return;
  375. }
  376. EXPORT_SYMBOL(enable_dma);
  377. /*------------------------------------------------------------------------------
  378. * Set the Start Address register for the specific DMA channel
  379. * This function can be used for register based DMA,
  380. * to setup the start address
  381. * addr: Starting address of the DMA Data to be transferred.
  382. *-----------------------------------------------------------------------------*/
  383. void set_dma_start_addr(unsigned int channel, unsigned long addr)
  384. {
  385. pr_debug("set_dma_start_addr() : BEGIN \n");
  386. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  387. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  388. dma_ch[channel].regs->start_addr = addr;
  389. SSYNC();
  390. pr_debug("set_dma_start_addr() : END\n");
  391. }
  392. EXPORT_SYMBOL(set_dma_start_addr);
  393. void set_dma_next_desc_addr(unsigned int channel, unsigned long addr)
  394. {
  395. pr_debug("set_dma_next_desc_addr() : BEGIN \n");
  396. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  397. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  398. dma_ch[channel].regs->next_desc_ptr = addr;
  399. SSYNC();
  400. pr_debug("set_dma_start_addr() : END\n");
  401. }
  402. EXPORT_SYMBOL(set_dma_next_desc_addr);
  403. void set_dma_x_count(unsigned int channel, unsigned short x_count)
  404. {
  405. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  406. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  407. dma_ch[channel].regs->x_count = x_count;
  408. SSYNC();
  409. }
  410. EXPORT_SYMBOL(set_dma_x_count);
  411. void set_dma_y_count(unsigned int channel, unsigned short y_count)
  412. {
  413. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  414. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  415. dma_ch[channel].regs->y_count = y_count;
  416. SSYNC();
  417. }
  418. EXPORT_SYMBOL(set_dma_y_count);
  419. void set_dma_x_modify(unsigned int channel, short x_modify)
  420. {
  421. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  422. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  423. dma_ch[channel].regs->x_modify = x_modify;
  424. SSYNC();
  425. }
  426. EXPORT_SYMBOL(set_dma_x_modify);
  427. void set_dma_y_modify(unsigned int channel, short y_modify)
  428. {
  429. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  430. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  431. dma_ch[channel].regs->y_modify = y_modify;
  432. SSYNC();
  433. }
  434. EXPORT_SYMBOL(set_dma_y_modify);
  435. void set_dma_config(unsigned int channel, unsigned short config)
  436. {
  437. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  438. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  439. dma_ch[channel].regs->cfg = config;
  440. SSYNC();
  441. }
  442. EXPORT_SYMBOL(set_dma_config);
  443. unsigned short
  444. set_bfin_dma_config(char direction, char flow_mode,
  445. char intr_mode, char dma_mode, char width)
  446. {
  447. unsigned short config;
  448. config =
  449. ((direction << 1) | (width << 2) | (dma_mode << 4) |
  450. (intr_mode << 6) | (flow_mode << 12) | RESTART);
  451. return config;
  452. }
  453. EXPORT_SYMBOL(set_bfin_dma_config);
  454. void set_dma_sg(unsigned int channel, struct dmasg * sg, int nr_sg)
  455. {
  456. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  457. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  458. dma_ch[channel].regs->cfg |= ((nr_sg & 0x0F) << 8);
  459. dma_ch[channel].regs->next_desc_ptr = (unsigned int)sg;
  460. SSYNC();
  461. }
  462. EXPORT_SYMBOL(set_dma_sg);
  463. /*------------------------------------------------------------------------------
  464. * Get the DMA status of a specific DMA channel from the system.
  465. *-----------------------------------------------------------------------------*/
  466. unsigned short get_dma_curr_irqstat(unsigned int channel)
  467. {
  468. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  469. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  470. return dma_ch[channel].regs->irq_status;
  471. }
  472. EXPORT_SYMBOL(get_dma_curr_irqstat);
  473. /*------------------------------------------------------------------------------
  474. * Clear the DMA_DONE bit in DMA status. Stop the DMA completion interrupt.
  475. *-----------------------------------------------------------------------------*/
  476. void clear_dma_irqstat(unsigned int channel)
  477. {
  478. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  479. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  480. dma_ch[channel].regs->irq_status |= 3;
  481. }
  482. EXPORT_SYMBOL(clear_dma_irqstat);
  483. /*------------------------------------------------------------------------------
  484. * Get current DMA xcount of a specific DMA channel from the system.
  485. *-----------------------------------------------------------------------------*/
  486. unsigned short get_dma_curr_xcount(unsigned int channel)
  487. {
  488. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  489. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  490. return dma_ch[channel].regs->curr_x_count;
  491. }
  492. EXPORT_SYMBOL(get_dma_curr_xcount);
  493. /*------------------------------------------------------------------------------
  494. * Get current DMA ycount of a specific DMA channel from the system.
  495. *-----------------------------------------------------------------------------*/
  496. unsigned short get_dma_curr_ycount(unsigned int channel)
  497. {
  498. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  499. && channel < MAX_BLACKFIN_DMA_CHANNEL));
  500. return dma_ch[channel].regs->curr_y_count;
  501. }
  502. EXPORT_SYMBOL(get_dma_curr_ycount);
  503. void *_dma_memcpy(void *dest, const void *src, size_t size)
  504. {
  505. int direction; /* 1 - address decrease, 0 - address increase */
  506. int flag_align; /* 1 - address aligned, 0 - address unaligned */
  507. int flag_2D; /* 1 - 2D DMA needed, 0 - 1D DMA needed */
  508. unsigned long flags;
  509. if (size <= 0)
  510. return NULL;
  511. local_irq_save(flags);
  512. if ((unsigned long)src < memory_end)
  513. blackfin_dcache_flush_range((unsigned int)src,
  514. (unsigned int)(src + size));
  515. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  516. if ((unsigned long)src < (unsigned long)dest)
  517. direction = 1;
  518. else
  519. direction = 0;
  520. if ((((unsigned long)dest % 2) == 0) && (((unsigned long)src % 2) == 0)
  521. && ((size % 2) == 0))
  522. flag_align = 1;
  523. else
  524. flag_align = 0;
  525. if (size > 0x10000) /* size > 64K */
  526. flag_2D = 1;
  527. else
  528. flag_2D = 0;
  529. /* Setup destination and source start address */
  530. if (direction) {
  531. if (flag_align) {
  532. bfin_write_MDMA_D0_START_ADDR(dest + size - 2);
  533. bfin_write_MDMA_S0_START_ADDR(src + size - 2);
  534. } else {
  535. bfin_write_MDMA_D0_START_ADDR(dest + size - 1);
  536. bfin_write_MDMA_S0_START_ADDR(src + size - 1);
  537. }
  538. } else {
  539. bfin_write_MDMA_D0_START_ADDR(dest);
  540. bfin_write_MDMA_S0_START_ADDR(src);
  541. }
  542. /* Setup destination and source xcount */
  543. if (flag_2D) {
  544. if (flag_align) {
  545. bfin_write_MDMA_D0_X_COUNT(1024 / 2);
  546. bfin_write_MDMA_S0_X_COUNT(1024 / 2);
  547. } else {
  548. bfin_write_MDMA_D0_X_COUNT(1024);
  549. bfin_write_MDMA_S0_X_COUNT(1024);
  550. }
  551. bfin_write_MDMA_D0_Y_COUNT(size >> 10);
  552. bfin_write_MDMA_S0_Y_COUNT(size >> 10);
  553. } else {
  554. if (flag_align) {
  555. bfin_write_MDMA_D0_X_COUNT(size / 2);
  556. bfin_write_MDMA_S0_X_COUNT(size / 2);
  557. } else {
  558. bfin_write_MDMA_D0_X_COUNT(size);
  559. bfin_write_MDMA_S0_X_COUNT(size);
  560. }
  561. }
  562. /* Setup destination and source xmodify and ymodify */
  563. if (direction) {
  564. if (flag_align) {
  565. bfin_write_MDMA_D0_X_MODIFY(-2);
  566. bfin_write_MDMA_S0_X_MODIFY(-2);
  567. if (flag_2D) {
  568. bfin_write_MDMA_D0_Y_MODIFY(-2);
  569. bfin_write_MDMA_S0_Y_MODIFY(-2);
  570. }
  571. } else {
  572. bfin_write_MDMA_D0_X_MODIFY(-1);
  573. bfin_write_MDMA_S0_X_MODIFY(-1);
  574. if (flag_2D) {
  575. bfin_write_MDMA_D0_Y_MODIFY(-1);
  576. bfin_write_MDMA_S0_Y_MODIFY(-1);
  577. }
  578. }
  579. } else {
  580. if (flag_align) {
  581. bfin_write_MDMA_D0_X_MODIFY(2);
  582. bfin_write_MDMA_S0_X_MODIFY(2);
  583. if (flag_2D) {
  584. bfin_write_MDMA_D0_Y_MODIFY(2);
  585. bfin_write_MDMA_S0_Y_MODIFY(2);
  586. }
  587. } else {
  588. bfin_write_MDMA_D0_X_MODIFY(1);
  589. bfin_write_MDMA_S0_X_MODIFY(1);
  590. if (flag_2D) {
  591. bfin_write_MDMA_D0_Y_MODIFY(1);
  592. bfin_write_MDMA_S0_Y_MODIFY(1);
  593. }
  594. }
  595. }
  596. /* Enable source DMA */
  597. if (flag_2D) {
  598. if (flag_align) {
  599. bfin_write_MDMA_S0_CONFIG(DMAEN | DMA2D | WDSIZE_16);
  600. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | DMA2D | WDSIZE_16);
  601. } else {
  602. bfin_write_MDMA_S0_CONFIG(DMAEN | DMA2D);
  603. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | DMA2D);
  604. }
  605. } else {
  606. if (flag_align) {
  607. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_16);
  608. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_16);
  609. } else {
  610. bfin_write_MDMA_S0_CONFIG(DMAEN);
  611. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN);
  612. }
  613. }
  614. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  615. ;
  616. bfin_write_MDMA_D0_IRQ_STATUS(bfin_read_MDMA_D0_IRQ_STATUS() |
  617. (DMA_DONE | DMA_ERR));
  618. bfin_write_MDMA_S0_CONFIG(0);
  619. bfin_write_MDMA_D0_CONFIG(0);
  620. if ((unsigned long)dest < memory_end)
  621. blackfin_dcache_invalidate_range((unsigned int)dest,
  622. (unsigned int)(dest + size));
  623. local_irq_restore(flags);
  624. return dest;
  625. }
  626. void *dma_memcpy(void *dest, const void *src, size_t size)
  627. {
  628. size_t bulk;
  629. size_t rest;
  630. void * addr;
  631. bulk = (size >> 16) << 16;
  632. rest = size - bulk;
  633. if (bulk)
  634. _dma_memcpy(dest, src, bulk);
  635. addr = _dma_memcpy(dest+bulk, src+bulk, rest);
  636. return addr;
  637. }
  638. EXPORT_SYMBOL(dma_memcpy);
  639. void *safe_dma_memcpy(void *dest, const void *src, size_t size)
  640. {
  641. void *addr;
  642. addr = dma_memcpy(dest, src, size);
  643. return addr;
  644. }
  645. EXPORT_SYMBOL(safe_dma_memcpy);
  646. void dma_outsb(void __iomem *addr, const void *buf, unsigned short len)
  647. {
  648. unsigned long flags;
  649. local_irq_save(flags);
  650. blackfin_dcache_flush_range((unsigned int)buf,(unsigned int)(buf) + len);
  651. bfin_write_MDMA_D0_START_ADDR(addr);
  652. bfin_write_MDMA_D0_X_COUNT(len);
  653. bfin_write_MDMA_D0_X_MODIFY(0);
  654. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  655. bfin_write_MDMA_S0_START_ADDR(buf);
  656. bfin_write_MDMA_S0_X_COUNT(len);
  657. bfin_write_MDMA_S0_X_MODIFY(1);
  658. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  659. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_8);
  660. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_8);
  661. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE));
  662. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  663. bfin_write_MDMA_S0_CONFIG(0);
  664. bfin_write_MDMA_D0_CONFIG(0);
  665. local_irq_restore(flags);
  666. }
  667. EXPORT_SYMBOL(dma_outsb);
  668. void dma_insb(const void __iomem *addr, void *buf, unsigned short len)
  669. {
  670. unsigned long flags;
  671. local_irq_save(flags);
  672. bfin_write_MDMA_D0_START_ADDR(buf);
  673. bfin_write_MDMA_D0_X_COUNT(len);
  674. bfin_write_MDMA_D0_X_MODIFY(1);
  675. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  676. bfin_write_MDMA_S0_START_ADDR(addr);
  677. bfin_write_MDMA_S0_X_COUNT(len);
  678. bfin_write_MDMA_S0_X_MODIFY(0);
  679. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  680. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_8);
  681. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_8);
  682. blackfin_dcache_invalidate_range((unsigned int)buf, (unsigned int)(buf) + len);
  683. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE));
  684. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  685. bfin_write_MDMA_S0_CONFIG(0);
  686. bfin_write_MDMA_D0_CONFIG(0);
  687. local_irq_restore(flags);
  688. }
  689. EXPORT_SYMBOL(dma_insb);
  690. void dma_outsw(void __iomem *addr, const void *buf, unsigned short len)
  691. {
  692. unsigned long flags;
  693. local_irq_save(flags);
  694. blackfin_dcache_flush_range((unsigned int)buf,(unsigned int)(buf) + len);
  695. bfin_write_MDMA_D0_START_ADDR(addr);
  696. bfin_write_MDMA_D0_X_COUNT(len);
  697. bfin_write_MDMA_D0_X_MODIFY(0);
  698. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  699. bfin_write_MDMA_S0_START_ADDR(buf);
  700. bfin_write_MDMA_S0_X_COUNT(len);
  701. bfin_write_MDMA_S0_X_MODIFY(2);
  702. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  703. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_16);
  704. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_16);
  705. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE));
  706. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  707. bfin_write_MDMA_S0_CONFIG(0);
  708. bfin_write_MDMA_D0_CONFIG(0);
  709. local_irq_restore(flags);
  710. }
  711. EXPORT_SYMBOL(dma_outsw);
  712. void dma_insw(const void __iomem *addr, void *buf, unsigned short len)
  713. {
  714. unsigned long flags;
  715. local_irq_save(flags);
  716. bfin_write_MDMA_D0_START_ADDR(buf);
  717. bfin_write_MDMA_D0_X_COUNT(len);
  718. bfin_write_MDMA_D0_X_MODIFY(2);
  719. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  720. bfin_write_MDMA_S0_START_ADDR(addr);
  721. bfin_write_MDMA_S0_X_COUNT(len);
  722. bfin_write_MDMA_S0_X_MODIFY(0);
  723. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  724. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_16);
  725. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_16);
  726. blackfin_dcache_invalidate_range((unsigned int)buf, (unsigned int)(buf) + len);
  727. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE));
  728. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  729. bfin_write_MDMA_S0_CONFIG(0);
  730. bfin_write_MDMA_D0_CONFIG(0);
  731. local_irq_restore(flags);
  732. }
  733. EXPORT_SYMBOL(dma_insw);
  734. void dma_outsl(void __iomem *addr, const void *buf, unsigned short len)
  735. {
  736. unsigned long flags;
  737. local_irq_save(flags);
  738. blackfin_dcache_flush_range((unsigned int)buf,(unsigned int)(buf) + len);
  739. bfin_write_MDMA_D0_START_ADDR(addr);
  740. bfin_write_MDMA_D0_X_COUNT(len);
  741. bfin_write_MDMA_D0_X_MODIFY(0);
  742. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  743. bfin_write_MDMA_S0_START_ADDR(buf);
  744. bfin_write_MDMA_S0_X_COUNT(len);
  745. bfin_write_MDMA_S0_X_MODIFY(4);
  746. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  747. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_32);
  748. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_32);
  749. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE));
  750. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  751. bfin_write_MDMA_S0_CONFIG(0);
  752. bfin_write_MDMA_D0_CONFIG(0);
  753. local_irq_restore(flags);
  754. }
  755. EXPORT_SYMBOL(dma_outsl);
  756. void dma_insl(const void __iomem *addr, void *buf, unsigned short len)
  757. {
  758. unsigned long flags;
  759. local_irq_save(flags);
  760. bfin_write_MDMA_D0_START_ADDR(buf);
  761. bfin_write_MDMA_D0_X_COUNT(len);
  762. bfin_write_MDMA_D0_X_MODIFY(4);
  763. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  764. bfin_write_MDMA_S0_START_ADDR(addr);
  765. bfin_write_MDMA_S0_X_COUNT(len);
  766. bfin_write_MDMA_S0_X_MODIFY(0);
  767. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  768. bfin_write_MDMA_S0_CONFIG(DMAEN | WDSIZE_32);
  769. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | WDSIZE_32);
  770. blackfin_dcache_invalidate_range((unsigned int)buf, (unsigned int)(buf) + len);
  771. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE));
  772. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  773. bfin_write_MDMA_S0_CONFIG(0);
  774. bfin_write_MDMA_D0_CONFIG(0);
  775. local_irq_restore(flags);
  776. }
  777. EXPORT_SYMBOL(dma_insl);