dma.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* linux/arch/arm/mach-msm/dma.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/io.h>
  16. #include <linux/interrupt.h>
  17. #include <mach/dma.h>
  18. #define MSM_DMOV_CHANNEL_COUNT 16
  19. enum {
  20. MSM_DMOV_PRINT_ERRORS = 1,
  21. MSM_DMOV_PRINT_IO = 2,
  22. MSM_DMOV_PRINT_FLOW = 4
  23. };
  24. static DEFINE_SPINLOCK(msm_dmov_lock);
  25. static struct msm_dmov_cmd active_command;
  26. static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
  27. static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
  28. unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS;
  29. #define MSM_DMOV_DPRINTF(mask, format, args...) \
  30. do { \
  31. if ((mask) & msm_dmov_print_mask) \
  32. printk(KERN_ERR format, args); \
  33. } while (0)
  34. #define PRINT_ERROR(format, args...) \
  35. MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args);
  36. #define PRINT_IO(format, args...) \
  37. MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args);
  38. #define PRINT_FLOW(format, args...) \
  39. MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args);
  40. void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
  41. {
  42. unsigned long irq_flags;
  43. unsigned int status;
  44. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  45. status = readl(DMOV_STATUS(id));
  46. if (list_empty(&ready_commands[id]) &&
  47. (status & DMOV_STATUS_CMD_PTR_RDY)) {
  48. #if 0
  49. if (list_empty(&active_commands[id])) {
  50. PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
  51. writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
  52. }
  53. #endif
  54. PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
  55. list_add_tail(&cmd->list, &active_commands[id]);
  56. writel(cmd->cmdptr, DMOV_CMD_PTR(id));
  57. } else {
  58. if (list_empty(&active_commands[id]))
  59. PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
  60. PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
  61. list_add_tail(&cmd->list, &ready_commands[id]);
  62. }
  63. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  64. }
  65. struct msm_dmov_exec_cmdptr_cmd {
  66. struct msm_dmov_cmd dmov_cmd;
  67. struct completion complete;
  68. unsigned id;
  69. unsigned int result;
  70. unsigned int flush[6];
  71. };
  72. static void dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd, unsigned int result)
  73. {
  74. struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
  75. cmd->result = result;
  76. if (result != 0x80000002) {
  77. cmd->flush[0] = readl(DMOV_FLUSH0(cmd->id));
  78. cmd->flush[1] = readl(DMOV_FLUSH1(cmd->id));
  79. cmd->flush[2] = readl(DMOV_FLUSH2(cmd->id));
  80. cmd->flush[3] = readl(DMOV_FLUSH3(cmd->id));
  81. cmd->flush[4] = readl(DMOV_FLUSH4(cmd->id));
  82. cmd->flush[5] = readl(DMOV_FLUSH5(cmd->id));
  83. }
  84. complete(&cmd->complete);
  85. }
  86. int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
  87. {
  88. struct msm_dmov_exec_cmdptr_cmd cmd;
  89. PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
  90. cmd.dmov_cmd.cmdptr = cmdptr;
  91. cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
  92. cmd.id = id;
  93. init_completion(&cmd.complete);
  94. msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
  95. wait_for_completion(&cmd.complete);
  96. if (cmd.result != 0x80000002) {
  97. PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
  98. PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
  99. id, cmd.flush[0], cmd.flush[1], cmd.flush[2], cmd.flush[3]);
  100. return -EIO;
  101. }
  102. PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
  103. return 0;
  104. }
  105. static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
  106. {
  107. unsigned int int_status, mask, id;
  108. unsigned long irq_flags;
  109. unsigned int ch_status;
  110. unsigned int ch_result;
  111. struct msm_dmov_cmd *cmd;
  112. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  113. int_status = readl(DMOV_ISR); /* read and clear interrupt */
  114. PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
  115. while (int_status) {
  116. mask = int_status & -int_status;
  117. id = fls(mask) - 1;
  118. PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
  119. int_status &= ~mask;
  120. ch_status = readl(DMOV_STATUS(id));
  121. if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
  122. PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
  123. continue;
  124. }
  125. do {
  126. ch_result = readl(DMOV_RSLT(id));
  127. if (list_empty(&active_commands[id])) {
  128. PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
  129. "with no active command, status %x, result %x\n",
  130. id, ch_status, ch_result);
  131. cmd = NULL;
  132. } else
  133. cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
  134. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
  135. if (ch_result & DMOV_RSLT_DONE) {
  136. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
  137. id, ch_status);
  138. PRINT_IO("msm_datamover_irq_handler id %d, got result "
  139. "for %p, result %x\n", id, cmd, ch_result);
  140. if (cmd) {
  141. list_del(&cmd->list);
  142. cmd->complete_func(cmd, ch_result);
  143. }
  144. }
  145. if (ch_result & DMOV_RSLT_FLUSH) {
  146. unsigned int flush0 = readl(DMOV_FLUSH0(id));
  147. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  148. PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, flush0);
  149. if (cmd) {
  150. list_del(&cmd->list);
  151. cmd->complete_func(cmd, ch_result);
  152. }
  153. }
  154. if (ch_result & DMOV_RSLT_ERROR) {
  155. unsigned int flush0 = readl(DMOV_FLUSH0(id));
  156. PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  157. PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, flush0);
  158. if (cmd) {
  159. list_del(&cmd->list);
  160. cmd->complete_func(cmd, ch_result);
  161. }
  162. /* this does not seem to work, once we get an error */
  163. /* the datamover will no longer accept commands */
  164. writel(0, DMOV_FLUSH0(id));
  165. }
  166. ch_status = readl(DMOV_STATUS(id));
  167. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  168. if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
  169. cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
  170. list_del(&cmd->list);
  171. list_add_tail(&cmd->list, &active_commands[id]);
  172. PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
  173. writel(cmd->cmdptr, DMOV_CMD_PTR(id));
  174. }
  175. } while (ch_status & DMOV_STATUS_RSLT_VALID);
  176. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  177. }
  178. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  179. return IRQ_HANDLED;
  180. }
  181. static int __init msm_init_datamover(void)
  182. {
  183. int i;
  184. for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
  185. INIT_LIST_HEAD(&ready_commands[i]);
  186. INIT_LIST_HEAD(&active_commands[i]);
  187. writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
  188. }
  189. return request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
  190. }
  191. arch_initcall(msm_init_datamover);