dma.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Wrapper for DMA channel allocator that updates DMA client muxing.
  2. * Copyright 2004-2007, Axis Communications AB
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/errno.h>
  7. #include <asm/dma.h>
  8. #include <arch/svinto.h>
  9. /* Macro to access ETRAX 100 registers */
  10. #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  11. IO_STATE_(reg##_, field##_, _##val)
  12. static char used_dma_channels[MAX_DMA_CHANNELS];
  13. static const char * used_dma_channels_users[MAX_DMA_CHANNELS];
  14. int cris_request_dma(unsigned int dmanr, const char * device_id,
  15. unsigned options, enum dma_owner owner)
  16. {
  17. unsigned long flags;
  18. unsigned long int gens;
  19. int fail = -EINVAL;
  20. if (dmanr >= MAX_DMA_CHANNELS) {
  21. printk(KERN_CRIT "cris_request_dma: invalid DMA channel %u\n", dmanr);
  22. return -EINVAL;
  23. }
  24. local_irq_save(flags);
  25. if (used_dma_channels[dmanr]) {
  26. local_irq_restore(flags);
  27. if (options & DMA_VERBOSE_ON_ERROR) {
  28. printk(KERN_CRIT "Failed to request DMA %i for %s, already allocated by %s\n", dmanr, device_id, used_dma_channels_users[dmanr]);
  29. }
  30. if (options & DMA_PANIC_ON_ERROR) {
  31. panic("request_dma error!");
  32. }
  33. return -EBUSY;
  34. }
  35. gens = genconfig_shadow;
  36. switch(owner)
  37. {
  38. case dma_eth:
  39. if ((dmanr != NETWORK_TX_DMA_NBR) &&
  40. (dmanr != NETWORK_RX_DMA_NBR)) {
  41. printk(KERN_CRIT "Invalid DMA channel for eth\n");
  42. goto bail;
  43. }
  44. break;
  45. case dma_ser0:
  46. if (dmanr == SER0_TX_DMA_NBR) {
  47. SETS(gens, R_GEN_CONFIG, dma6, serial0);
  48. } else if (dmanr == SER0_RX_DMA_NBR) {
  49. SETS(gens, R_GEN_CONFIG, dma7, serial0);
  50. } else {
  51. printk(KERN_CRIT "Invalid DMA channel for ser0\n");
  52. goto bail;
  53. }
  54. break;
  55. case dma_ser1:
  56. if (dmanr == SER1_TX_DMA_NBR) {
  57. SETS(gens, R_GEN_CONFIG, dma8, serial1);
  58. } else if (dmanr == SER1_RX_DMA_NBR) {
  59. SETS(gens, R_GEN_CONFIG, dma9, serial1);
  60. } else {
  61. printk(KERN_CRIT "Invalid DMA channel for ser1\n");
  62. goto bail;
  63. }
  64. break;
  65. case dma_ser2:
  66. if (dmanr == SER2_TX_DMA_NBR) {
  67. SETS(gens, R_GEN_CONFIG, dma2, serial2);
  68. } else if (dmanr == SER2_RX_DMA_NBR) {
  69. SETS(gens, R_GEN_CONFIG, dma3, serial2);
  70. } else {
  71. printk(KERN_CRIT "Invalid DMA channel for ser2\n");
  72. goto bail;
  73. }
  74. break;
  75. case dma_ser3:
  76. if (dmanr == SER3_TX_DMA_NBR) {
  77. SETS(gens, R_GEN_CONFIG, dma4, serial3);
  78. } else if (dmanr == SER3_RX_DMA_NBR) {
  79. SETS(gens, R_GEN_CONFIG, dma5, serial3);
  80. } else {
  81. printk(KERN_CRIT "Invalid DMA channel for ser3\n");
  82. goto bail;
  83. }
  84. break;
  85. case dma_ata:
  86. if (dmanr == ATA_TX_DMA_NBR) {
  87. SETS(gens, R_GEN_CONFIG, dma2, ata);
  88. } else if (dmanr == ATA_RX_DMA_NBR) {
  89. SETS(gens, R_GEN_CONFIG, dma3, ata);
  90. } else {
  91. printk(KERN_CRIT "Invalid DMA channel for ata\n");
  92. goto bail;
  93. }
  94. break;
  95. case dma_ext0:
  96. if (dmanr == EXTDMA0_TX_DMA_NBR) {
  97. SETS(gens, R_GEN_CONFIG, dma4, extdma0);
  98. } else if (dmanr == EXTDMA0_RX_DMA_NBR) {
  99. SETS(gens, R_GEN_CONFIG, dma5, extdma0);
  100. } else {
  101. printk(KERN_CRIT "Invalid DMA channel for ext0\n");
  102. goto bail;
  103. }
  104. break;
  105. case dma_ext1:
  106. if (dmanr == EXTDMA1_TX_DMA_NBR) {
  107. SETS(gens, R_GEN_CONFIG, dma6, extdma1);
  108. } else if (dmanr == EXTDMA1_RX_DMA_NBR) {
  109. SETS(gens, R_GEN_CONFIG, dma7, extdma1);
  110. } else {
  111. printk(KERN_CRIT "Invalid DMA channel for ext1\n");
  112. goto bail;
  113. }
  114. break;
  115. case dma_int6:
  116. if (dmanr == MEM2MEM_RX_DMA_NBR) {
  117. SETS(gens, R_GEN_CONFIG, dma7, intdma6);
  118. } else {
  119. printk(KERN_CRIT "Invalid DMA channel for int6\n");
  120. goto bail;
  121. }
  122. break;
  123. case dma_int7:
  124. if (dmanr == MEM2MEM_TX_DMA_NBR) {
  125. SETS(gens, R_GEN_CONFIG, dma6, intdma7);
  126. } else {
  127. printk(KERN_CRIT "Invalid DMA channel for int7\n");
  128. goto bail;
  129. }
  130. break;
  131. case dma_usb:
  132. if (dmanr == USB_TX_DMA_NBR) {
  133. SETS(gens, R_GEN_CONFIG, dma8, usb);
  134. } else if (dmanr == USB_RX_DMA_NBR) {
  135. SETS(gens, R_GEN_CONFIG, dma9, usb);
  136. } else {
  137. printk(KERN_CRIT "Invalid DMA channel for usb\n");
  138. goto bail;
  139. }
  140. break;
  141. case dma_scsi0:
  142. if (dmanr == SCSI0_TX_DMA_NBR) {
  143. SETS(gens, R_GEN_CONFIG, dma2, scsi0);
  144. } else if (dmanr == SCSI0_RX_DMA_NBR) {
  145. SETS(gens, R_GEN_CONFIG, dma3, scsi0);
  146. } else {
  147. printk(KERN_CRIT "Invalid DMA channel for scsi0\n");
  148. goto bail;
  149. }
  150. break;
  151. case dma_scsi1:
  152. if (dmanr == SCSI1_TX_DMA_NBR) {
  153. SETS(gens, R_GEN_CONFIG, dma4, scsi1);
  154. } else if (dmanr == SCSI1_RX_DMA_NBR) {
  155. SETS(gens, R_GEN_CONFIG, dma5, scsi1);
  156. } else {
  157. printk(KERN_CRIT "Invalid DMA channel for scsi1\n");
  158. goto bail;
  159. }
  160. break;
  161. case dma_par0:
  162. if (dmanr == PAR0_TX_DMA_NBR) {
  163. SETS(gens, R_GEN_CONFIG, dma2, par0);
  164. } else if (dmanr == PAR0_RX_DMA_NBR) {
  165. SETS(gens, R_GEN_CONFIG, dma3, par0);
  166. } else {
  167. printk(KERN_CRIT "Invalid DMA channel for par0\n");
  168. goto bail;
  169. }
  170. break;
  171. case dma_par1:
  172. if (dmanr == PAR1_TX_DMA_NBR) {
  173. SETS(gens, R_GEN_CONFIG, dma4, par1);
  174. } else if (dmanr == PAR1_RX_DMA_NBR) {
  175. SETS(gens, R_GEN_CONFIG, dma5, par1);
  176. } else {
  177. printk(KERN_CRIT "Invalid DMA channel for par1\n");
  178. goto bail;
  179. }
  180. break;
  181. default:
  182. printk(KERN_CRIT "Invalid DMA owner.\n");
  183. goto bail;
  184. }
  185. used_dma_channels[dmanr] = 1;
  186. used_dma_channels_users[dmanr] = device_id;
  187. {
  188. volatile int i;
  189. genconfig_shadow = gens;
  190. *R_GEN_CONFIG = genconfig_shadow;
  191. /* Wait 12 cycles before doing any DMA command */
  192. for(i = 6; i > 0; i--)
  193. nop();
  194. }
  195. fail = 0;
  196. bail:
  197. local_irq_restore(flags);
  198. return fail;
  199. }
  200. void cris_free_dma(unsigned int dmanr, const char * device_id)
  201. {
  202. unsigned long flags;
  203. if (dmanr >= MAX_DMA_CHANNELS) {
  204. printk(KERN_CRIT "cris_free_dma: invalid DMA channel %u\n", dmanr);
  205. return;
  206. }
  207. local_irq_save(flags);
  208. if (!used_dma_channels[dmanr]) {
  209. printk(KERN_CRIT "cris_free_dma: DMA channel %u not allocated\n", dmanr);
  210. } else if (device_id != used_dma_channels_users[dmanr]) {
  211. printk(KERN_CRIT "cris_free_dma: DMA channel %u not allocated by device\n", dmanr);
  212. } else {
  213. switch(dmanr)
  214. {
  215. case 0:
  216. *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, reset);
  217. while (IO_EXTRACT(R_DMA_CH0_CMD, cmd, *R_DMA_CH0_CMD) ==
  218. IO_STATE_VALUE(R_DMA_CH0_CMD, cmd, reset));
  219. break;
  220. case 1:
  221. *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, reset);
  222. while (IO_EXTRACT(R_DMA_CH1_CMD, cmd, *R_DMA_CH1_CMD) ==
  223. IO_STATE_VALUE(R_DMA_CH1_CMD, cmd, reset));
  224. break;
  225. case 2:
  226. *R_DMA_CH2_CMD = IO_STATE(R_DMA_CH2_CMD, cmd, reset);
  227. while (IO_EXTRACT(R_DMA_CH2_CMD, cmd, *R_DMA_CH2_CMD) ==
  228. IO_STATE_VALUE(R_DMA_CH2_CMD, cmd, reset));
  229. break;
  230. case 3:
  231. *R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, reset);
  232. while (IO_EXTRACT(R_DMA_CH3_CMD, cmd, *R_DMA_CH3_CMD) ==
  233. IO_STATE_VALUE(R_DMA_CH3_CMD, cmd, reset));
  234. break;
  235. case 4:
  236. *R_DMA_CH4_CMD = IO_STATE(R_DMA_CH4_CMD, cmd, reset);
  237. while (IO_EXTRACT(R_DMA_CH4_CMD, cmd, *R_DMA_CH4_CMD) ==
  238. IO_STATE_VALUE(R_DMA_CH4_CMD, cmd, reset));
  239. break;
  240. case 5:
  241. *R_DMA_CH5_CMD = IO_STATE(R_DMA_CH5_CMD, cmd, reset);
  242. while (IO_EXTRACT(R_DMA_CH5_CMD, cmd, *R_DMA_CH5_CMD) ==
  243. IO_STATE_VALUE(R_DMA_CH5_CMD, cmd, reset));
  244. break;
  245. case 6:
  246. *R_DMA_CH6_CMD = IO_STATE(R_DMA_CH6_CMD, cmd, reset);
  247. while (IO_EXTRACT(R_DMA_CH6_CMD, cmd, *R_DMA_CH6_CMD) ==
  248. IO_STATE_VALUE(R_DMA_CH6_CMD, cmd, reset));
  249. break;
  250. case 7:
  251. *R_DMA_CH7_CMD = IO_STATE(R_DMA_CH7_CMD, cmd, reset);
  252. while (IO_EXTRACT(R_DMA_CH7_CMD, cmd, *R_DMA_CH7_CMD) ==
  253. IO_STATE_VALUE(R_DMA_CH7_CMD, cmd, reset));
  254. break;
  255. case 8:
  256. *R_DMA_CH8_CMD = IO_STATE(R_DMA_CH8_CMD, cmd, reset);
  257. while (IO_EXTRACT(R_DMA_CH8_CMD, cmd, *R_DMA_CH8_CMD) ==
  258. IO_STATE_VALUE(R_DMA_CH8_CMD, cmd, reset));
  259. break;
  260. case 9:
  261. *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, reset);
  262. while (IO_EXTRACT(R_DMA_CH9_CMD, cmd, *R_DMA_CH9_CMD) ==
  263. IO_STATE_VALUE(R_DMA_CH9_CMD, cmd, reset));
  264. break;
  265. }
  266. used_dma_channels[dmanr] = 0;
  267. }
  268. local_irq_restore(flags);
  269. }
  270. EXPORT_SYMBOL(cris_request_dma);
  271. EXPORT_SYMBOL(cris_free_dma);