dma.c 9.5 KB

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