PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/arm/mach-msm/dma.c

https://github.com/darth-llamah/kernel
C | 288 lines | 238 code | 31 blank | 19 comment | 35 complexity | 56de97a31499dabdc3e81a295db80408 MD5 | raw file
  1. /* linux/arch/arm/mach-msm/dma.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2008 QUALCOMM Incorporated.
  5. * Copyright (c) 2008 QUALCOMM USA, INC.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/completion.h>
  22. #include <linux/module.h>
  23. #include <mach/dma.h>
  24. #define MSM_DMOV_CHANNEL_COUNT 16
  25. enum {
  26. MSM_DMOV_PRINT_ERRORS = 1,
  27. MSM_DMOV_PRINT_IO = 2,
  28. MSM_DMOV_PRINT_FLOW = 4
  29. };
  30. static DEFINE_SPINLOCK(msm_dmov_lock);
  31. static struct clk *msm_dmov_clk;
  32. static unsigned int channel_active;
  33. static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
  34. static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
  35. unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS;
  36. #define MSM_DMOV_DPRINTF(mask, format, args...) \
  37. do { \
  38. if ((mask) & msm_dmov_print_mask) \
  39. printk(KERN_ERR format, args); \
  40. } while (0)
  41. #define PRINT_ERROR(format, args...) \
  42. MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args);
  43. #define PRINT_IO(format, args...) \
  44. MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args);
  45. #define PRINT_FLOW(format, args...) \
  46. MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args);
  47. void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
  48. {
  49. writel((graceful << 31), DMOV_FLUSH0(id));
  50. }
  51. EXPORT_SYMBOL(msm_dmov_stop_cmd);
  52. void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
  53. {
  54. unsigned long irq_flags;
  55. unsigned int status;
  56. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  57. if (!channel_active)
  58. clk_enable(msm_dmov_clk);
  59. dsb();
  60. status = readl(DMOV_STATUS(id));
  61. if (list_empty(&ready_commands[id]) &&
  62. (status & DMOV_STATUS_CMD_PTR_RDY)) {
  63. #if 0
  64. if (list_empty(&active_commands[id])) {
  65. PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
  66. writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
  67. }
  68. #endif
  69. if (cmd->execute_func)
  70. cmd->execute_func(cmd);
  71. PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
  72. list_add_tail(&cmd->list, &active_commands[id]);
  73. if (!channel_active)
  74. enable_irq(INT_ADM_AARM);
  75. channel_active |= 1U << id;
  76. writel(cmd->cmdptr, DMOV_CMD_PTR(id));
  77. } else {
  78. if (!channel_active)
  79. clk_disable(msm_dmov_clk);
  80. if (list_empty(&active_commands[id]))
  81. PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
  82. PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
  83. list_add_tail(&cmd->list, &ready_commands[id]);
  84. }
  85. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  86. }
  87. EXPORT_SYMBOL(msm_dmov_enqueue_cmd);
  88. void msm_dmov_flush(unsigned int id)
  89. {
  90. unsigned long irq_flags;
  91. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  92. /* XXX not checking if flush cmd sent already */
  93. if (!list_empty(&active_commands[id])) {
  94. PRINT_IO("msm_dmov_flush(%d), send flush cmd\n", id);
  95. writel(DMOV_FLUSH_TYPE, DMOV_FLUSH0(id));
  96. }
  97. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  98. }
  99. EXPORT_SYMBOL(msm_dmov_flush);
  100. struct msm_dmov_exec_cmdptr_cmd {
  101. struct msm_dmov_cmd dmov_cmd;
  102. struct completion complete;
  103. unsigned id;
  104. unsigned int result;
  105. struct msm_dmov_errdata err;
  106. };
  107. static void
  108. dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
  109. unsigned int result,
  110. struct msm_dmov_errdata *err)
  111. {
  112. struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
  113. cmd->result = result;
  114. if (result != 0x80000002 && err)
  115. memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
  116. complete(&cmd->complete);
  117. }
  118. int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
  119. {
  120. struct msm_dmov_exec_cmdptr_cmd cmd;
  121. PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
  122. cmd.dmov_cmd.cmdptr = cmdptr;
  123. cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
  124. cmd.dmov_cmd.execute_func = NULL;
  125. cmd.id = id;
  126. init_completion(&cmd.complete);
  127. msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
  128. wait_for_completion(&cmd.complete);
  129. if (cmd.result != 0x80000002) {
  130. PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
  131. PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
  132. id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]);
  133. return -EIO;
  134. }
  135. PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
  136. return 0;
  137. }
  138. static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
  139. {
  140. unsigned int int_status, mask, id;
  141. unsigned long irq_flags;
  142. unsigned int ch_status;
  143. unsigned int ch_result;
  144. struct msm_dmov_cmd *cmd;
  145. spin_lock_irqsave(&msm_dmov_lock, irq_flags);
  146. int_status = readl(DMOV_ISR); /* read and clear interrupt */
  147. PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
  148. while (int_status) {
  149. mask = int_status & -int_status;
  150. id = fls(mask) - 1;
  151. PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
  152. int_status &= ~mask;
  153. ch_status = readl(DMOV_STATUS(id));
  154. if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
  155. PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
  156. continue;
  157. }
  158. do {
  159. ch_result = readl(DMOV_RSLT(id));
  160. if (list_empty(&active_commands[id])) {
  161. PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
  162. "with no active command, status %x, result %x\n",
  163. id, ch_status, ch_result);
  164. cmd = NULL;
  165. } else
  166. cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
  167. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
  168. if (ch_result & DMOV_RSLT_DONE) {
  169. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
  170. id, ch_status);
  171. PRINT_IO("msm_datamover_irq_handler id %d, got result "
  172. "for %p, result %x\n", id, cmd, ch_result);
  173. if (cmd) {
  174. list_del(&cmd->list);
  175. dsb();
  176. cmd->complete_func(cmd, ch_result, NULL);
  177. }
  178. }
  179. if (ch_result & DMOV_RSLT_FLUSH) {
  180. struct msm_dmov_errdata errdata;
  181. errdata.flush[0] = readl(DMOV_FLUSH0(id));
  182. errdata.flush[1] = readl(DMOV_FLUSH1(id));
  183. errdata.flush[2] = readl(DMOV_FLUSH2(id));
  184. errdata.flush[3] = readl(DMOV_FLUSH3(id));
  185. errdata.flush[4] = readl(DMOV_FLUSH4(id));
  186. errdata.flush[5] = readl(DMOV_FLUSH5(id));
  187. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  188. PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
  189. if (cmd) {
  190. list_del(&cmd->list);
  191. dsb();
  192. cmd->complete_func(cmd, ch_result, &errdata);
  193. }
  194. }
  195. if (ch_result & DMOV_RSLT_ERROR) {
  196. struct msm_dmov_errdata errdata;
  197. errdata.flush[0] = readl(DMOV_FLUSH0(id));
  198. errdata.flush[1] = readl(DMOV_FLUSH1(id));
  199. errdata.flush[2] = readl(DMOV_FLUSH2(id));
  200. errdata.flush[3] = readl(DMOV_FLUSH3(id));
  201. errdata.flush[4] = readl(DMOV_FLUSH4(id));
  202. errdata.flush[5] = readl(DMOV_FLUSH5(id));
  203. PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  204. PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
  205. if (cmd) {
  206. list_del(&cmd->list);
  207. dsb();
  208. cmd->complete_func(cmd, ch_result, &errdata);
  209. }
  210. /* this does not seem to work, once we get an error */
  211. /* the datamover will no longer accept commands */
  212. writel(0, DMOV_FLUSH0(id));
  213. }
  214. ch_status = readl(DMOV_STATUS(id));
  215. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  216. if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
  217. cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
  218. list_del(&cmd->list);
  219. list_add_tail(&cmd->list, &active_commands[id]);
  220. if (cmd->execute_func)
  221. cmd->execute_func(cmd);
  222. PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
  223. writel(cmd->cmdptr, DMOV_CMD_PTR(id));
  224. }
  225. } while (ch_status & DMOV_STATUS_RSLT_VALID);
  226. if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
  227. channel_active &= ~(1U << id);
  228. PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
  229. }
  230. if (!channel_active) {
  231. disable_irq_nosync(INT_ADM_AARM);
  232. clk_disable(msm_dmov_clk);
  233. }
  234. spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
  235. return IRQ_HANDLED;
  236. }
  237. static int __init msm_init_datamover(void)
  238. {
  239. int i;
  240. int ret;
  241. struct clk *clk;
  242. for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
  243. INIT_LIST_HEAD(&ready_commands[i]);
  244. INIT_LIST_HEAD(&active_commands[i]);
  245. writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
  246. }
  247. clk = clk_get(NULL, "adm_clk");
  248. if (IS_ERR(clk))
  249. return PTR_ERR(clk);
  250. msm_dmov_clk = clk;
  251. ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
  252. if (ret)
  253. return ret;
  254. disable_irq(INT_ADM_AARM);
  255. return 0;
  256. }
  257. arch_initcall(msm_init_datamover);