au1000_dma.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * BRIEF MODULE DESCRIPTION
  3. * Defines for using and allocating dma channels on the Alchemy
  4. * Au1000 mips processor.
  5. *
  6. * Copyright 2000 MontaVista Software Inc.
  7. * Author: MontaVista Software, Inc.
  8. * stevel@mvista.com or source@mvista.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  18. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  21. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. */
  31. #ifndef __ASM_AU1000_DMA_H
  32. #define __ASM_AU1000_DMA_H
  33. #include <asm/io.h> /* need byte IO */
  34. #include <linux/spinlock.h> /* And spinlocks */
  35. #include <linux/delay.h>
  36. #include <asm/system.h>
  37. #define NUM_AU1000_DMA_CHANNELS 8
  38. /* DMA Channel Base Addresses */
  39. #define DMA_CHANNEL_BASE 0xB4002000
  40. #define DMA_CHANNEL_LEN 0x00000100
  41. /* DMA Channel Register Offsets */
  42. #define DMA_MODE_SET 0x00000000
  43. #define DMA_MODE_READ DMA_MODE_SET
  44. #define DMA_MODE_CLEAR 0x00000004
  45. /* DMA Mode register bits follow */
  46. #define DMA_DAH_MASK (0x0f << 20)
  47. #define DMA_DID_BIT 16
  48. #define DMA_DID_MASK (0x0f << DMA_DID_BIT)
  49. #define DMA_DS (1<<15)
  50. #define DMA_BE (1<<13)
  51. #define DMA_DR (1<<12)
  52. #define DMA_TS8 (1<<11)
  53. #define DMA_DW_BIT 9
  54. #define DMA_DW_MASK (0x03 << DMA_DW_BIT)
  55. #define DMA_DW8 (0 << DMA_DW_BIT)
  56. #define DMA_DW16 (1 << DMA_DW_BIT)
  57. #define DMA_DW32 (2 << DMA_DW_BIT)
  58. #define DMA_NC (1<<8)
  59. #define DMA_IE (1<<7)
  60. #define DMA_HALT (1<<6)
  61. #define DMA_GO (1<<5)
  62. #define DMA_AB (1<<4)
  63. #define DMA_D1 (1<<3)
  64. #define DMA_BE1 (1<<2)
  65. #define DMA_D0 (1<<1)
  66. #define DMA_BE0 (1<<0)
  67. #define DMA_PERIPHERAL_ADDR 0x00000008
  68. #define DMA_BUFFER0_START 0x0000000C
  69. #define DMA_BUFFER1_START 0x00000014
  70. #define DMA_BUFFER0_COUNT 0x00000010
  71. #define DMA_BUFFER1_COUNT 0x00000018
  72. #define DMA_BAH_BIT 16
  73. #define DMA_BAH_MASK (0x0f << DMA_BAH_BIT)
  74. #define DMA_COUNT_BIT 0
  75. #define DMA_COUNT_MASK (0xffff << DMA_COUNT_BIT)
  76. /* DMA Device ID's follow */
  77. enum {
  78. DMA_ID_UART0_TX = 0,
  79. DMA_ID_UART0_RX,
  80. DMA_ID_GP04,
  81. DMA_ID_GP05,
  82. DMA_ID_AC97C_TX,
  83. DMA_ID_AC97C_RX,
  84. DMA_ID_UART3_TX,
  85. DMA_ID_UART3_RX,
  86. DMA_ID_USBDEV_EP0_RX,
  87. DMA_ID_USBDEV_EP0_TX,
  88. DMA_ID_USBDEV_EP2_TX,
  89. DMA_ID_USBDEV_EP3_TX,
  90. DMA_ID_USBDEV_EP4_RX,
  91. DMA_ID_USBDEV_EP5_RX,
  92. DMA_ID_I2S_TX,
  93. DMA_ID_I2S_RX,
  94. DMA_NUM_DEV
  95. };
  96. /* DMA Device ID's for 2nd bank (AU1100) follow */
  97. enum {
  98. DMA_ID_SD0_TX = 0,
  99. DMA_ID_SD0_RX,
  100. DMA_ID_SD1_TX,
  101. DMA_ID_SD1_RX,
  102. DMA_NUM_DEV_BANK2
  103. };
  104. struct dma_chan {
  105. int dev_id; // this channel is allocated if >=0, free otherwise
  106. unsigned int io;
  107. const char *dev_str;
  108. int irq;
  109. void *irq_dev;
  110. unsigned int fifo_addr;
  111. unsigned int mode;
  112. };
  113. /* These are in arch/mips/au1000/common/dma.c */
  114. extern struct dma_chan au1000_dma_table[];
  115. extern int request_au1000_dma(int dev_id,
  116. const char *dev_str,
  117. irq_handler_t irqhandler,
  118. unsigned long irqflags,
  119. void *irq_dev_id);
  120. extern void free_au1000_dma(unsigned int dmanr);
  121. extern int au1000_dma_read_proc(char *buf, char **start, off_t fpos,
  122. int length, int *eof, void *data);
  123. extern void dump_au1000_dma_channel(unsigned int dmanr);
  124. extern spinlock_t au1000_dma_spin_lock;
  125. static __inline__ struct dma_chan *get_dma_chan(unsigned int dmanr)
  126. {
  127. if (dmanr >= NUM_AU1000_DMA_CHANNELS
  128. || au1000_dma_table[dmanr].dev_id < 0)
  129. return NULL;
  130. return &au1000_dma_table[dmanr];
  131. }
  132. static __inline__ unsigned long claim_dma_lock(void)
  133. {
  134. unsigned long flags;
  135. spin_lock_irqsave(&au1000_dma_spin_lock, flags);
  136. return flags;
  137. }
  138. static __inline__ void release_dma_lock(unsigned long flags)
  139. {
  140. spin_unlock_irqrestore(&au1000_dma_spin_lock, flags);
  141. }
  142. /*
  143. * Set the DMA buffer enable bits in the mode register.
  144. */
  145. static __inline__ void enable_dma_buffer0(unsigned int dmanr)
  146. {
  147. struct dma_chan *chan = get_dma_chan(dmanr);
  148. if (!chan)
  149. return;
  150. au_writel(DMA_BE0, chan->io + DMA_MODE_SET);
  151. }
  152. static __inline__ void enable_dma_buffer1(unsigned int dmanr)
  153. {
  154. struct dma_chan *chan = get_dma_chan(dmanr);
  155. if (!chan)
  156. return;
  157. au_writel(DMA_BE1, chan->io + DMA_MODE_SET);
  158. }
  159. static __inline__ void enable_dma_buffers(unsigned int dmanr)
  160. {
  161. struct dma_chan *chan = get_dma_chan(dmanr);
  162. if (!chan)
  163. return;
  164. au_writel(DMA_BE0 | DMA_BE1, chan->io + DMA_MODE_SET);
  165. }
  166. static __inline__ void start_dma(unsigned int dmanr)
  167. {
  168. struct dma_chan *chan = get_dma_chan(dmanr);
  169. if (!chan)
  170. return;
  171. au_writel(DMA_GO, chan->io + DMA_MODE_SET);
  172. }
  173. #define DMA_HALT_POLL 0x5000
  174. static __inline__ void halt_dma(unsigned int dmanr)
  175. {
  176. struct dma_chan *chan = get_dma_chan(dmanr);
  177. int i;
  178. if (!chan)
  179. return;
  180. au_writel(DMA_GO, chan->io + DMA_MODE_CLEAR);
  181. // poll the halt bit
  182. for (i = 0; i < DMA_HALT_POLL; i++)
  183. if (au_readl(chan->io + DMA_MODE_READ) & DMA_HALT)
  184. break;
  185. if (i == DMA_HALT_POLL)
  186. printk(KERN_INFO "halt_dma: HALT poll expired!\n");
  187. }
  188. static __inline__ void disable_dma(unsigned int dmanr)
  189. {
  190. struct dma_chan *chan = get_dma_chan(dmanr);
  191. if (!chan)
  192. return;
  193. halt_dma(dmanr);
  194. // now we can disable the buffers
  195. au_writel(~DMA_GO, chan->io + DMA_MODE_CLEAR);
  196. }
  197. static __inline__ int dma_halted(unsigned int dmanr)
  198. {
  199. struct dma_chan *chan = get_dma_chan(dmanr);
  200. if (!chan)
  201. return 1;
  202. return (au_readl(chan->io + DMA_MODE_READ) & DMA_HALT) ? 1 : 0;
  203. }
  204. /* initialize a DMA channel */
  205. static __inline__ void init_dma(unsigned int dmanr)
  206. {
  207. struct dma_chan *chan = get_dma_chan(dmanr);
  208. u32 mode;
  209. if (!chan)
  210. return;
  211. disable_dma(dmanr);
  212. // set device FIFO address
  213. au_writel(CPHYSADDR(chan->fifo_addr),
  214. chan->io + DMA_PERIPHERAL_ADDR);
  215. mode = chan->mode | (chan->dev_id << DMA_DID_BIT);
  216. if (chan->irq)
  217. mode |= DMA_IE;
  218. au_writel(~mode, chan->io + DMA_MODE_CLEAR);
  219. au_writel(mode, chan->io + DMA_MODE_SET);
  220. }
  221. /*
  222. * set mode for a specific DMA channel
  223. */
  224. static __inline__ void set_dma_mode(unsigned int dmanr, unsigned int mode)
  225. {
  226. struct dma_chan *chan = get_dma_chan(dmanr);
  227. if (!chan)
  228. return;
  229. /*
  230. * set_dma_mode is only allowed to change endianess, direction,
  231. * transfer size, device FIFO width, and coherency settings.
  232. * Make sure anything else is masked off.
  233. */
  234. mode &= (DMA_BE | DMA_DR | DMA_TS8 | DMA_DW_MASK | DMA_NC);
  235. chan->mode &= ~(DMA_BE | DMA_DR | DMA_TS8 | DMA_DW_MASK | DMA_NC);
  236. chan->mode |= mode;
  237. }
  238. static __inline__ unsigned int get_dma_mode(unsigned int dmanr)
  239. {
  240. struct dma_chan *chan = get_dma_chan(dmanr);
  241. if (!chan)
  242. return 0;
  243. return chan->mode;
  244. }
  245. static __inline__ int get_dma_active_buffer(unsigned int dmanr)
  246. {
  247. struct dma_chan *chan = get_dma_chan(dmanr);
  248. if (!chan)
  249. return -1;
  250. return (au_readl(chan->io + DMA_MODE_READ) & DMA_AB) ? 1 : 0;
  251. }
  252. /*
  253. * set the device FIFO address for a specific DMA channel - only
  254. * applicable to GPO4 and GPO5. All the other devices have fixed
  255. * FIFO addresses.
  256. */
  257. static __inline__ void set_dma_fifo_addr(unsigned int dmanr,
  258. unsigned int a)
  259. {
  260. struct dma_chan *chan = get_dma_chan(dmanr);
  261. if (!chan)
  262. return;
  263. if (chan->mode & DMA_DS) /* second bank of device ids */
  264. return;
  265. if (chan->dev_id != DMA_ID_GP04 && chan->dev_id != DMA_ID_GP05)
  266. return;
  267. au_writel(CPHYSADDR(a), chan->io + DMA_PERIPHERAL_ADDR);
  268. }
  269. /*
  270. * Clear the DMA buffer done bits in the mode register.
  271. */
  272. static __inline__ void clear_dma_done0(unsigned int dmanr)
  273. {
  274. struct dma_chan *chan = get_dma_chan(dmanr);
  275. if (!chan)
  276. return;
  277. au_writel(DMA_D0, chan->io + DMA_MODE_CLEAR);
  278. }
  279. static __inline__ void clear_dma_done1(unsigned int dmanr)
  280. {
  281. struct dma_chan *chan = get_dma_chan(dmanr);
  282. if (!chan)
  283. return;
  284. au_writel(DMA_D1, chan->io + DMA_MODE_CLEAR);
  285. }
  286. /*
  287. * This does nothing - not applicable to Au1000 DMA.
  288. */
  289. static __inline__ void set_dma_page(unsigned int dmanr, char pagenr)
  290. {
  291. }
  292. /*
  293. * Set Buffer 0 transfer address for specific DMA channel.
  294. */
  295. static __inline__ void set_dma_addr0(unsigned int dmanr, unsigned int a)
  296. {
  297. struct dma_chan *chan = get_dma_chan(dmanr);
  298. if (!chan)
  299. return;
  300. au_writel(a, chan->io + DMA_BUFFER0_START);
  301. }
  302. /*
  303. * Set Buffer 1 transfer address for specific DMA channel.
  304. */
  305. static __inline__ void set_dma_addr1(unsigned int dmanr, unsigned int a)
  306. {
  307. struct dma_chan *chan = get_dma_chan(dmanr);
  308. if (!chan)
  309. return;
  310. au_writel(a, chan->io + DMA_BUFFER1_START);
  311. }
  312. /*
  313. * Set Buffer 0 transfer size (max 64k) for a specific DMA channel.
  314. */
  315. static __inline__ void set_dma_count0(unsigned int dmanr,
  316. unsigned int count)
  317. {
  318. struct dma_chan *chan = get_dma_chan(dmanr);
  319. if (!chan)
  320. return;
  321. count &= DMA_COUNT_MASK;
  322. au_writel(count, chan->io + DMA_BUFFER0_COUNT);
  323. }
  324. /*
  325. * Set Buffer 1 transfer size (max 64k) for a specific DMA channel.
  326. */
  327. static __inline__ void set_dma_count1(unsigned int dmanr,
  328. unsigned int count)
  329. {
  330. struct dma_chan *chan = get_dma_chan(dmanr);
  331. if (!chan)
  332. return;
  333. count &= DMA_COUNT_MASK;
  334. au_writel(count, chan->io + DMA_BUFFER1_COUNT);
  335. }
  336. /*
  337. * Set both buffer transfer sizes (max 64k) for a specific DMA channel.
  338. */
  339. static __inline__ void set_dma_count(unsigned int dmanr,
  340. unsigned int count)
  341. {
  342. struct dma_chan *chan = get_dma_chan(dmanr);
  343. if (!chan)
  344. return;
  345. count &= DMA_COUNT_MASK;
  346. au_writel(count, chan->io + DMA_BUFFER0_COUNT);
  347. au_writel(count, chan->io + DMA_BUFFER1_COUNT);
  348. }
  349. /*
  350. * Returns which buffer has its done bit set in the mode register.
  351. * Returns -1 if neither or both done bits set.
  352. */
  353. static __inline__ unsigned int get_dma_buffer_done(unsigned int dmanr)
  354. {
  355. struct dma_chan *chan = get_dma_chan(dmanr);
  356. if (!chan)
  357. return 0;
  358. return au_readl(chan->io + DMA_MODE_READ) & (DMA_D0 | DMA_D1);
  359. }
  360. /*
  361. * Returns the DMA channel's Buffer Done IRQ number.
  362. */
  363. static __inline__ int get_dma_done_irq(unsigned int dmanr)
  364. {
  365. struct dma_chan *chan = get_dma_chan(dmanr);
  366. if (!chan)
  367. return -1;
  368. return chan->irq;
  369. }
  370. /*
  371. * Get DMA residue count. Returns the number of _bytes_ left to transfer.
  372. */
  373. static __inline__ int get_dma_residue(unsigned int dmanr)
  374. {
  375. int curBufCntReg, count;
  376. struct dma_chan *chan = get_dma_chan(dmanr);
  377. if (!chan)
  378. return 0;
  379. curBufCntReg = (au_readl(chan->io + DMA_MODE_READ) & DMA_AB) ?
  380. DMA_BUFFER1_COUNT : DMA_BUFFER0_COUNT;
  381. count = au_readl(chan->io + curBufCntReg) & DMA_COUNT_MASK;
  382. if ((chan->mode & DMA_DW_MASK) == DMA_DW16)
  383. count <<= 1;
  384. else if ((chan->mode & DMA_DW_MASK) == DMA_DW32)
  385. count <<= 2;
  386. return count;
  387. }
  388. #endif /* __ASM_AU1000_DMA_H */