dma.c 8.1 KB

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