dma.c 8.3 KB

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