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