1 /* linux/arch/arm/mach-msm/dma.c
3 * Copyright (C) 2007 Google, Inc.
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.
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.
16 #include <linux/clk.h>
17 #include <linux/err.h>
19 #include <linux/interrupt.h>
22 #define MSM_DMOV_CHANNEL_COUNT 16
25 MSM_DMOV_PRINT_ERRORS = 1,
26 MSM_DMOV_PRINT_IO = 2,
27 MSM_DMOV_PRINT_FLOW = 4
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;
37 #define MSM_DMOV_DPRINTF(mask, format, args...) \
39 if ((mask) & msm_dmov_print_mask) \
40 printk(KERN_ERR format, args); \
42 #define PRINT_ERROR(format, args...) \
43 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args);
44 #define PRINT_IO(format, args...) \
45 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args);
46 #define PRINT_FLOW(format, args...) \
47 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args);
49 void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
51 writel((graceful << 31), DMOV_FLUSH0(id));
54 void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
56 unsigned long irq_flags;
59 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
61 clk_enable(msm_dmov_clk);
63 status = readl(DMOV_STATUS(id));
64 if (list_empty(&ready_commands[id]) &&
65 (status & DMOV_STATUS_CMD_PTR_RDY)) {
67 if (list_empty(&active_commands[id])) {
68 PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
69 writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
72 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
73 list_add_tail(&cmd->list, &active_commands[id]);
75 enable_irq(INT_ADM_AARM);
76 channel_active |= 1U << id;
77 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
80 clk_disable(msm_dmov_clk);
81 if (list_empty(&active_commands[id]))
82 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
84 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
85 list_add_tail(&cmd->list, &ready_commands[id]);
87 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
90 struct msm_dmov_exec_cmdptr_cmd {
91 struct msm_dmov_cmd dmov_cmd;
92 struct completion complete;
95 struct msm_dmov_errdata err;
99 dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
101 struct msm_dmov_errdata *err)
103 struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
104 cmd->result = result;
105 if (result != 0x80000002 && err)
106 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
108 complete(&cmd->complete);
111 int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
113 struct msm_dmov_exec_cmdptr_cmd cmd;
115 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
117 cmd.dmov_cmd.cmdptr = cmdptr;
118 cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
120 init_completion(&cmd.complete);
122 msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
123 wait_for_completion(&cmd.complete);
125 if (cmd.result != 0x80000002) {
126 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
127 PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
128 id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]);
131 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
136 static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
138 unsigned int int_status, mask, id;
139 unsigned long irq_flags;
140 unsigned int ch_status;
141 unsigned int ch_result;
142 struct msm_dmov_cmd *cmd;
144 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);
150 mask = int_status & -int_status;
152 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
154 ch_status = readl(DMOV_STATUS(id));
155 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
156 PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
160 ch_result = readl(DMOV_RSLT(id));
161 if (list_empty(&active_commands[id])) {
162 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
163 "with no active command, status %x, result %x\n",
164 id, ch_status, ch_result);
167 cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
168 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
169 if (ch_result & DMOV_RSLT_DONE) {
170 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
172 PRINT_IO("msm_datamover_irq_handler id %d, got result "
173 "for %p, result %x\n", id, cmd, ch_result);
175 list_del(&cmd->list);
177 cmd->complete_func(cmd, ch_result, NULL);
180 if (ch_result & DMOV_RSLT_FLUSH) {
181 struct msm_dmov_errdata errdata;
183 errdata.flush[0] = readl(DMOV_FLUSH0(id));
184 errdata.flush[1] = readl(DMOV_FLUSH1(id));
185 errdata.flush[2] = readl(DMOV_FLUSH2(id));
186 errdata.flush[3] = readl(DMOV_FLUSH3(id));
187 errdata.flush[4] = readl(DMOV_FLUSH4(id));
188 errdata.flush[5] = readl(DMOV_FLUSH5(id));
189 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
190 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
192 list_del(&cmd->list);
194 cmd->complete_func(cmd, ch_result, &errdata);
197 if (ch_result & DMOV_RSLT_ERROR) {
198 struct msm_dmov_errdata errdata;
200 errdata.flush[0] = readl(DMOV_FLUSH0(id));
201 errdata.flush[1] = readl(DMOV_FLUSH1(id));
202 errdata.flush[2] = readl(DMOV_FLUSH2(id));
203 errdata.flush[3] = readl(DMOV_FLUSH3(id));
204 errdata.flush[4] = readl(DMOV_FLUSH4(id));
205 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]);
210 list_del(&cmd->list);
212 cmd->complete_func(cmd, ch_result, &errdata);
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));
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_del(&cmd->list);
223 list_add_tail(&cmd->list, &active_commands[id]);
224 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
225 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
227 } while (ch_status & DMOV_STATUS_RSLT_VALID);
228 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
229 channel_active &= ~(1U << id);
230 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
233 if (!channel_active) {
234 disable_irq_nosync(INT_ADM_AARM);
235 clk_disable(msm_dmov_clk);
238 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
242 static int __init msm_init_datamover(void)
248 for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
249 INIT_LIST_HEAD(&ready_commands[i]);
250 INIT_LIST_HEAD(&active_commands[i]);
251 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
253 clk = clk_get(NULL, "adm_clk");
257 ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
260 disable_irq(INT_ADM_AARM);
264 arch_initcall(msm_init_datamover);