Merge branch 'drm-nouveau-next' of git://anongit.freedesktop.org/git/nouveau/linux...
[deliverable/linux.git] / arch / arm / mach-msm / dma.c
CommitLineData
bfe645ad
AH
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
c5541079
AH
16#include <linux/clk.h>
17#include <linux/err.h>
fced80c7 18#include <linux/io.h>
bfe645ad 19#include <linux/interrupt.h>
6d7b7d57 20#include <linux/completion.h>
404ed596 21#include <linux/module.h>
a09e64fb 22#include <mach/dma.h>
430f1eef 23#include <mach/msm_iomap.h>
bfe645ad
AH
24
25#define MSM_DMOV_CHANNEL_COUNT 16
26
430f1eef
SB
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))
31
32#if defined(CONFIG_ARCH_MSM7X30)
33#define DMOV_SD_AARM DMOV_SD2
34#else
35#define DMOV_SD_AARM DMOV_SD3
36#endif
37
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)
46
47#define DMOV_STATUS(ch) DMOV_SD_AARM(0x200, ch)
48#define DMOV_ISR DMOV_SD_AARM(0x380, 0)
49
50#define DMOV_CONFIG(ch) DMOV_SD_AARM(0x300, ch)
51
bfe645ad
AH
52enum {
53 MSM_DMOV_PRINT_ERRORS = 1,
54 MSM_DMOV_PRINT_IO = 2,
55 MSM_DMOV_PRINT_FLOW = 4
56};
57
58static DEFINE_SPINLOCK(msm_dmov_lock);
c5541079 59static struct clk *msm_dmov_clk;
8a0f6f17 60static unsigned int channel_active;
bfe645ad
AH
61static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
62static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
63unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS;
64
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);
76
8a0f6f17
BS
77void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
78{
79 writel((graceful << 31), DMOV_FLUSH0(id));
80}
b25a7912 81EXPORT_SYMBOL_GPL(msm_dmov_stop_cmd);
8a0f6f17 82
bfe645ad
AH
83void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
84{
85 unsigned long irq_flags;
86 unsigned int status;
87
88 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
c5541079
AH
89 if (!channel_active)
90 clk_enable(msm_dmov_clk);
9f68fcdb 91 dsb();
bfe645ad
AH
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));
99 }
100#endif
5b00f40f
SM
101 if (cmd->execute_func)
102 cmd->execute_func(cmd);
bfe645ad
AH
103 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
104 list_add_tail(&cmd->list, &active_commands[id]);
8a0f6f17
BS
105 if (!channel_active)
106 enable_irq(INT_ADM_AARM);
107 channel_active |= 1U << id;
bfe645ad
AH
108 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
109 } else {
c5541079
AH
110 if (!channel_active)
111 clk_disable(msm_dmov_clk);
bfe645ad
AH
112 if (list_empty(&active_commands[id]))
113 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
114
115 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
116 list_add_tail(&cmd->list, &ready_commands[id]);
117 }
118 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
119}
b25a7912 120EXPORT_SYMBOL_GPL(msm_dmov_enqueue_cmd);
bfe645ad
AH
121
122struct msm_dmov_exec_cmdptr_cmd {
123 struct msm_dmov_cmd dmov_cmd;
124 struct completion complete;
125 unsigned id;
126 unsigned int result;
8a0f6f17 127 struct msm_dmov_errdata err;
bfe645ad
AH
128};
129
8a0f6f17
BS
130static void
131dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
132 unsigned int result,
133 struct msm_dmov_errdata *err)
bfe645ad
AH
134{
135 struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
136 cmd->result = result;
8a0f6f17
BS
137 if (result != 0x80000002 && err)
138 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
139
bfe645ad
AH
140 complete(&cmd->complete);
141}
142
143int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
144{
145 struct msm_dmov_exec_cmdptr_cmd cmd;
146
147 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
148
149 cmd.dmov_cmd.cmdptr = cmdptr;
150 cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
5b00f40f 151 cmd.dmov_cmd.execute_func = NULL;
bfe645ad
AH
152 cmd.id = id;
153 init_completion(&cmd.complete);
154
155 msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
156 wait_for_completion(&cmd.complete);
157
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",
8a0f6f17 161 id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]);
bfe645ad
AH
162 return -EIO;
163 }
164 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
165 return 0;
166}
167
168
169static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
170{
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;
176
177 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
178
179 int_status = readl(DMOV_ISR); /* read and clear interrupt */
180 PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
181
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;
191 }
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);
9f68fcdb 209 dsb();
8a0f6f17 210 cmd->complete_func(cmd, ch_result, NULL);
bfe645ad
AH
211 }
212 }
213 if (ch_result & DMOV_RSLT_FLUSH) {
8a0f6f17
BS
214 struct msm_dmov_errdata errdata;
215
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));
bfe645ad 222 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
8a0f6f17 223 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
bfe645ad
AH
224 if (cmd) {
225 list_del(&cmd->list);
9f68fcdb 226 dsb();
8a0f6f17 227 cmd->complete_func(cmd, ch_result, &errdata);
bfe645ad
AH
228 }
229 }
230 if (ch_result & DMOV_RSLT_ERROR) {
8a0f6f17
BS
231 struct msm_dmov_errdata errdata;
232
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));
239
bfe645ad 240 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
8a0f6f17 241 PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
bfe645ad
AH
242 if (cmd) {
243 list_del(&cmd->list);
9f68fcdb 244 dsb();
8a0f6f17 245 cmd->complete_func(cmd, ch_result, &errdata);
bfe645ad
AH
246 }
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));
250 }
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);
6eebf2de 255 list_move_tail(&cmd->list, &active_commands[id]);
5b00f40f
SM
256 if (cmd->execute_func)
257 cmd->execute_func(cmd);
bfe645ad
AH
258 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
259 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
260 }
261 } while (ch_status & DMOV_STATUS_RSLT_VALID);
8a0f6f17
BS
262 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
263 channel_active &= ~(1U << id);
bfe645ad
AH
264 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
265 }
8a0f6f17 266
c5541079 267 if (!channel_active) {
a6407dd7 268 disable_irq_nosync(INT_ADM_AARM);
c5541079
AH
269 clk_disable(msm_dmov_clk);
270 }
8a0f6f17 271
bfe645ad
AH
272 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
273 return IRQ_HANDLED;
274}
275
276static int __init msm_init_datamover(void)
277{
278 int i;
8a0f6f17 279 int ret;
c5541079
AH
280 struct clk *clk;
281
bfe645ad
AH
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));
286 }
c5541079
AH
287 clk = clk_get(NULL, "adm_clk");
288 if (IS_ERR(clk))
289 return PTR_ERR(clk);
2dfd9c1f 290 clk_prepare(clk);
c5541079 291 msm_dmov_clk = clk;
8a0f6f17
BS
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;
bfe645ad 297}
2dfd9c1f 298module_init(msm_init_datamover);
This page took 0.437788 seconds and 5 git commands to generate.