dma.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Wrapper for DMA channel allocator that starts clocks etc */
  2. #include <linux/kernel.h>
  3. #include <linux/spinlock.h>
  4. #include <asm/dma.h>
  5. #include <hwregs/reg_map.h>
  6. #include <hwregs/reg_rdwr.h>
  7. #include <hwregs/marb_defs.h>
  8. #include <hwregs/config_defs.h>
  9. #include <hwregs/strmux_defs.h>
  10. #include <linux/errno.h>
  11. #include <mach/arbiter.h>
  12. static char used_dma_channels[MAX_DMA_CHANNELS];
  13. static const char *used_dma_channels_users[MAX_DMA_CHANNELS];
  14. static DEFINE_SPINLOCK(dma_lock);
  15. int crisv32_request_dma(unsigned int dmanr, const char *device_id,
  16. unsigned options, unsigned int bandwidth,
  17. enum dma_owner owner)
  18. {
  19. unsigned long flags;
  20. reg_config_rw_clk_ctrl clk_ctrl;
  21. reg_strmux_rw_cfg strmux_cfg;
  22. if (crisv32_arbiter_allocate_bandwidth(dmanr,
  23. options & DMA_INT_MEM ?
  24. INT_REGION : EXT_REGION,
  25. bandwidth))
  26. return -ENOMEM;
  27. spin_lock_irqsave(&dma_lock, flags);
  28. if (used_dma_channels[dmanr]) {
  29. spin_unlock_irqrestore(&dma_lock, flags);
  30. if (options & DMA_VERBOSE_ON_ERROR) {
  31. printk(KERN_ERR "Failed to request DMA %i for %s, "
  32. "already allocated by %s\n",
  33. dmanr,
  34. device_id,
  35. used_dma_channels_users[dmanr]);
  36. }
  37. if (options & DMA_PANIC_ON_ERROR)
  38. panic("request_dma error!");
  39. spin_unlock_irqrestore(&dma_lock, flags);
  40. return -EBUSY;
  41. }
  42. clk_ctrl = REG_RD(config, regi_config, rw_clk_ctrl);
  43. strmux_cfg = REG_RD(strmux, regi_strmux, rw_cfg);
  44. switch (dmanr) {
  45. case 0:
  46. case 1:
  47. clk_ctrl.dma01_eth0 = 1;
  48. break;
  49. case 2:
  50. case 3:
  51. clk_ctrl.dma23 = 1;
  52. break;
  53. case 4:
  54. case 5:
  55. clk_ctrl.dma45 = 1;
  56. break;
  57. case 6:
  58. case 7:
  59. clk_ctrl.dma67 = 1;
  60. break;
  61. case 8:
  62. case 9:
  63. clk_ctrl.dma89_strcop = 1;
  64. break;
  65. #if MAX_DMA_CHANNELS-1 != 9
  66. #error Check dma.c
  67. #endif
  68. default:
  69. spin_unlock_irqrestore(&dma_lock, flags);
  70. if (options & DMA_VERBOSE_ON_ERROR) {
  71. printk(KERN_ERR "Failed to request DMA %i for %s, "
  72. "only 0-%i valid)\n",
  73. dmanr, device_id, MAX_DMA_CHANNELS - 1);
  74. }
  75. if (options & DMA_PANIC_ON_ERROR)
  76. panic("request_dma error!");
  77. return -EINVAL;
  78. }
  79. switch (owner) {
  80. case dma_eth0:
  81. if (dmanr == 0)
  82. strmux_cfg.dma0 = regk_strmux_eth0;
  83. else if (dmanr == 1)
  84. strmux_cfg.dma1 = regk_strmux_eth0;
  85. else
  86. panic("Invalid DMA channel for eth0\n");
  87. break;
  88. case dma_eth1:
  89. if (dmanr == 6)
  90. strmux_cfg.dma6 = regk_strmux_eth1;
  91. else if (dmanr == 7)
  92. strmux_cfg.dma7 = regk_strmux_eth1;
  93. else
  94. panic("Invalid DMA channel for eth1\n");
  95. break;
  96. case dma_iop0:
  97. if (dmanr == 2)
  98. strmux_cfg.dma2 = regk_strmux_iop0;
  99. else if (dmanr == 3)
  100. strmux_cfg.dma3 = regk_strmux_iop0;
  101. else
  102. panic("Invalid DMA channel for iop0\n");
  103. break;
  104. case dma_iop1:
  105. if (dmanr == 4)
  106. strmux_cfg.dma4 = regk_strmux_iop1;
  107. else if (dmanr == 5)
  108. strmux_cfg.dma5 = regk_strmux_iop1;
  109. else
  110. panic("Invalid DMA channel for iop1\n");
  111. break;
  112. case dma_ser0:
  113. if (dmanr == 6)
  114. strmux_cfg.dma6 = regk_strmux_ser0;
  115. else if (dmanr == 7)
  116. strmux_cfg.dma7 = regk_strmux_ser0;
  117. else
  118. panic("Invalid DMA channel for ser0\n");
  119. break;
  120. case dma_ser1:
  121. if (dmanr == 4)
  122. strmux_cfg.dma4 = regk_strmux_ser1;
  123. else if (dmanr == 5)
  124. strmux_cfg.dma5 = regk_strmux_ser1;
  125. else
  126. panic("Invalid DMA channel for ser1\n");
  127. break;
  128. case dma_ser2:
  129. if (dmanr == 2)
  130. strmux_cfg.dma2 = regk_strmux_ser2;
  131. else if (dmanr == 3)
  132. strmux_cfg.dma3 = regk_strmux_ser2;
  133. else
  134. panic("Invalid DMA channel for ser2\n");
  135. break;
  136. case dma_ser3:
  137. if (dmanr == 8)
  138. strmux_cfg.dma8 = regk_strmux_ser3;
  139. else if (dmanr == 9)
  140. strmux_cfg.dma9 = regk_strmux_ser3;
  141. else
  142. panic("Invalid DMA channel for ser3\n");
  143. break;
  144. case dma_sser0:
  145. if (dmanr == 4)
  146. strmux_cfg.dma4 = regk_strmux_sser0;
  147. else if (dmanr == 5)
  148. strmux_cfg.dma5 = regk_strmux_sser0;
  149. else
  150. panic("Invalid DMA channel for sser0\n");
  151. break;
  152. case dma_sser1:
  153. if (dmanr == 6)
  154. strmux_cfg.dma6 = regk_strmux_sser1;
  155. else if (dmanr == 7)
  156. strmux_cfg.dma7 = regk_strmux_sser1;
  157. else
  158. panic("Invalid DMA channel for sser1\n");
  159. break;
  160. case dma_ata:
  161. if (dmanr == 2)
  162. strmux_cfg.dma2 = regk_strmux_ata;
  163. else if (dmanr == 3)
  164. strmux_cfg.dma3 = regk_strmux_ata;
  165. else
  166. panic("Invalid DMA channel for ata\n");
  167. break;
  168. case dma_strp:
  169. if (dmanr == 8)
  170. strmux_cfg.dma8 = regk_strmux_strcop;
  171. else if (dmanr == 9)
  172. strmux_cfg.dma9 = regk_strmux_strcop;
  173. else
  174. panic("Invalid DMA channel for strp\n");
  175. break;
  176. case dma_ext0:
  177. if (dmanr == 6)
  178. strmux_cfg.dma6 = regk_strmux_ext0;
  179. else
  180. panic("Invalid DMA channel for ext0\n");
  181. break;
  182. case dma_ext1:
  183. if (dmanr == 7)
  184. strmux_cfg.dma7 = regk_strmux_ext1;
  185. else
  186. panic("Invalid DMA channel for ext1\n");
  187. break;
  188. case dma_ext2:
  189. if (dmanr == 2)
  190. strmux_cfg.dma2 = regk_strmux_ext2;
  191. else if (dmanr == 8)
  192. strmux_cfg.dma8 = regk_strmux_ext2;
  193. else
  194. panic("Invalid DMA channel for ext2\n");
  195. break;
  196. case dma_ext3:
  197. if (dmanr == 3)
  198. strmux_cfg.dma3 = regk_strmux_ext3;
  199. else if (dmanr == 9)
  200. strmux_cfg.dma9 = regk_strmux_ext2;
  201. else
  202. panic("Invalid DMA channel for ext2\n");
  203. break;
  204. }
  205. used_dma_channels[dmanr] = 1;
  206. used_dma_channels_users[dmanr] = device_id;
  207. REG_WR(config, regi_config, rw_clk_ctrl, clk_ctrl);
  208. REG_WR(strmux, regi_strmux, rw_cfg, strmux_cfg);
  209. spin_unlock_irqrestore(&dma_lock, flags);
  210. return 0;
  211. }
  212. void crisv32_free_dma(unsigned int dmanr)
  213. {
  214. spin_lock(&dma_lock);
  215. used_dma_channels[dmanr] = 0;
  216. spin_unlock(&dma_lock);
  217. }