dma.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 unsigned int channel_active;
  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_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
  41. {
  42. writel((graceful << 31), DMOV_FLUSH0(id));
  43. }
  44. void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
  45. {
  46. unsigned long irq_flags;
  47. unsigned int status;
  48. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  49. status = readl(DMOV_STATUS(id));
  50. if (list_empty(&ready_commands[id]) &&
  51. (status & DMOV_STATUS_CMD_PTR_RDY)) {
  52. #if 0
  53. if (list_empty(&active_commands[id])) {
  54. PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
  55. writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
  56. }
  57. #endif
  58. PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
  59. list_add_tail(&cmd->list, &active_commands[id]);
  60. if (!channel_active)
  61. enable_irq(INT_ADM_AARM);
  62. channel_active |= 1U << id;
  63. writel(cmd->cmdptr, DMOV_CMD_PTR(id));
  64. } else {
  65. if (list_empty(&active_commands[id]))
  66. PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
  67. PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
  68. list_add_tail(&cmd->list, &ready_commands[id]);
  69. }
  70. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  71. }
  72. struct msm_dmov_exec_cmdptr_cmd {
  73. struct msm_dmov_cmd dmov_cmd;
  74. struct completion complete;
  75. unsigned id;
  76. unsigned int result;
  77. struct msm_dmov_errdata err;
  78. };
  79. static void
  80. dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
  81. unsigned int result,
  82. struct msm_dmov_errdata *err)
  83. {
  84. struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
  85. cmd->result = result;
  86. if (result != 0x80000002 && err)
  87. memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
  88. complete(&cmd->complete);
  89. }
  90. int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
  91. {
  92. struct msm_dmov_exec_cmdptr_cmd cmd;
  93. PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
  94. cmd.dmov_cmd.cmdptr = cmdptr;
  95. cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
  96. cmd.id = id;
  97. init_completion(&cmd.complete);
  98. msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
  99. wait_for_completion(&cmd.complete);
  100. if (cmd.result != 0x80000002) {
  101. PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
  102. PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
  103. id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]);
  104. return -EIO;
  105. }
  106. PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
  107. return 0;
  108. }
  109. static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
  110. {
  111. unsigned int int_status, mask, id;
  112. unsigned long irq_flags;
  113. unsigned int ch_status;
  114. unsigned int ch_result;
  115. struct msm_dmov_cmd *cmd;
  116. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  117. int_status = readl(DMOV_ISR); /* read and clear interrupt */
  118. PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
  119. while (int_status) {
  120. mask = int_status & -int_status;
  121. id = fls(mask) - 1;
  122. PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
  123. int_status &= ~mask;
  124. ch_status = readl(DMOV_STATUS(id));
  125. if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
  126. PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
  127. continue;
  128. }
  129. do {
  130. ch_result = readl(DMOV_RSLT(id));
  131. if (list_empty(&active_commands[id])) {
  132. PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
  133. "with no active command, status %x, result %x\n",
  134. id, ch_status, ch_result);
  135. cmd = NULL;
  136. } else
  137. cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
  138. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
  139. if (ch_result & DMOV_RSLT_DONE) {
  140. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
  141. id, ch_status);
  142. PRINT_IO("msm_datamover_irq_handler id %d, got result "
  143. "for %p, result %x\n", id, cmd, ch_result);
  144. if (cmd) {
  145. list_del(&cmd->list);
  146. cmd->complete_func(cmd, ch_result, NULL);
  147. }
  148. }
  149. if (ch_result & DMOV_RSLT_FLUSH) {
  150. struct msm_dmov_errdata errdata;
  151. errdata.flush[0] = readl(DMOV_FLUSH0(id));
  152. errdata.flush[1] = readl(DMOV_FLUSH1(id));
  153. errdata.flush[2] = readl(DMOV_FLUSH2(id));
  154. errdata.flush[3] = readl(DMOV_FLUSH3(id));
  155. errdata.flush[4] = readl(DMOV_FLUSH4(id));
  156. errdata.flush[5] = readl(DMOV_FLUSH5(id));
  157. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  158. PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
  159. if (cmd) {
  160. list_del(&cmd->list);
  161. cmd->complete_func(cmd, ch_result, &errdata);
  162. }
  163. }
  164. if (ch_result & DMOV_RSLT_ERROR) {
  165. struct msm_dmov_errdata errdata;
  166. errdata.flush[0] = readl(DMOV_FLUSH0(id));
  167. errdata.flush[1] = readl(DMOV_FLUSH1(id));
  168. errdata.flush[2] = readl(DMOV_FLUSH2(id));
  169. errdata.flush[3] = readl(DMOV_FLUSH3(id));
  170. errdata.flush[4] = readl(DMOV_FLUSH4(id));
  171. errdata.flush[5] = readl(DMOV_FLUSH5(id));
  172. PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  173. PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
  174. if (cmd) {
  175. list_del(&cmd->list);
  176. cmd->complete_func(cmd, ch_result, &errdata);
  177. }
  178. /* this does not seem to work, once we get an error */
  179. /* the datamover will no longer accept commands */
  180. writel(0, DMOV_FLUSH0(id));
  181. }
  182. ch_status = readl(DMOV_STATUS(id));
  183. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  184. if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
  185. cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
  186. list_del(&cmd->list);
  187. list_add_tail(&cmd->list, &active_commands[id]);
  188. PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
  189. writel(cmd->cmdptr, DMOV_CMD_PTR(id));
  190. }
  191. } while (ch_status & DMOV_STATUS_RSLT_VALID);
  192. if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
  193. channel_active &= ~(1U << id);
  194. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  195. }
  196. if (!channel_active)
  197. disable_irq(INT_ADM_AARM);
  198. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  199. return IRQ_HANDLED;
  200. }
  201. static int __init msm_init_datamover(void)
  202. {
  203. int i;
  204. int ret;
  205. for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
  206. INIT_LIST_HEAD(&ready_commands[i]);
  207. INIT_LIST_HEAD(&active_commands[i]);
  208. writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
  209. }
  210. ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
  211. if (ret)
  212. return ret;
  213. disable_irq(INT_ADM_AARM);
  214. return 0;
  215. }
  216. arch_initcall(msm_init_datamover);