dma.c 8.5 KB

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