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>
20 #include <linux/completion.h>
22 #include <mach/msm_iomap.h>
24 #define MSM_DMOV_CHANNEL_COUNT 16
26 #define DMOV_SD0(off, ch) (MSM_DMOV_BASE + 0x0000 + (off) + ((ch) << 2))
27 #define DMOV_SD1(off, ch) (MSM_DMOV_BASE + 0x0400 + (off) + ((ch) << 2))
28 #define DMOV_SD2(off, ch) (MSM_DMOV_BASE + 0x0800 + (off) + ((ch) << 2))
29 #define DMOV_SD3(off, ch) (MSM_DMOV_BASE + 0x0C00 + (off) + ((ch) << 2))
31 #if defined(CONFIG_ARCH_MSM7X30)
32 #define DMOV_SD_AARM DMOV_SD2
34 #define DMOV_SD_AARM DMOV_SD3
37 #define DMOV_CMD_PTR(ch) DMOV_SD_AARM(0x000, ch)
38 #define DMOV_RSLT(ch) DMOV_SD_AARM(0x040, ch)
39 #define DMOV_FLUSH0(ch) DMOV_SD_AARM(0x080, ch)
40 #define DMOV_FLUSH1(ch) DMOV_SD_AARM(0x0C0, ch)
41 #define DMOV_FLUSH2(ch) DMOV_SD_AARM(0x100, ch)
42 #define DMOV_FLUSH3(ch) DMOV_SD_AARM(0x140, ch)
43 #define DMOV_FLUSH4(ch) DMOV_SD_AARM(0x180, ch)
44 #define DMOV_FLUSH5(ch) DMOV_SD_AARM(0x1C0, ch)
46 #define DMOV_STATUS(ch) DMOV_SD_AARM(0x200, ch)
47 #define DMOV_ISR DMOV_SD_AARM(0x380, 0)
49 #define DMOV_CONFIG(ch) DMOV_SD_AARM(0x300, ch)
52 MSM_DMOV_PRINT_ERRORS
= 1,
53 MSM_DMOV_PRINT_IO
= 2,
54 MSM_DMOV_PRINT_FLOW
= 4
57 static DEFINE_SPINLOCK(msm_dmov_lock
);
58 static struct clk
*msm_dmov_clk
;
59 static unsigned int channel_active
;
60 static struct list_head ready_commands
[MSM_DMOV_CHANNEL_COUNT
];
61 static struct list_head active_commands
[MSM_DMOV_CHANNEL_COUNT
];
62 unsigned int msm_dmov_print_mask
= MSM_DMOV_PRINT_ERRORS
;
64 #define MSM_DMOV_DPRINTF(mask, format, args...) \
66 if ((mask) & msm_dmov_print_mask) \
67 printk(KERN_ERR format, args); \
69 #define PRINT_ERROR(format, args...) \
70 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args);
71 #define PRINT_IO(format, args...) \
72 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args);
73 #define PRINT_FLOW(format, args...) \
74 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args);
76 void msm_dmov_stop_cmd(unsigned id
, struct msm_dmov_cmd
*cmd
, int graceful
)
78 writel((graceful
<< 31), DMOV_FLUSH0(id
));
81 void msm_dmov_enqueue_cmd(unsigned id
, struct msm_dmov_cmd
*cmd
)
83 unsigned long irq_flags
;
86 spin_lock_irqsave(&msm_dmov_lock
, irq_flags
);
88 clk_enable(msm_dmov_clk
);
90 status
= readl(DMOV_STATUS(id
));
91 if (list_empty(&ready_commands
[id
]) &&
92 (status
& DMOV_STATUS_CMD_PTR_RDY
)) {
94 if (list_empty(&active_commands
[id
])) {
95 PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id
);
96 writel(DMOV_CONFIG_IRQ_EN
, DMOV_CONFIG(id
));
99 if (cmd
->execute_func
)
100 cmd
->execute_func(cmd
);
101 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id
, status
);
102 list_add_tail(&cmd
->list
, &active_commands
[id
]);
104 enable_irq(INT_ADM_AARM
);
105 channel_active
|= 1U << id
;
106 writel(cmd
->cmdptr
, DMOV_CMD_PTR(id
));
109 clk_disable(msm_dmov_clk
);
110 if (list_empty(&active_commands
[id
]))
111 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id
, status
);
113 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id
, status
);
114 list_add_tail(&cmd
->list
, &ready_commands
[id
]);
116 spin_unlock_irqrestore(&msm_dmov_lock
, irq_flags
);
119 struct msm_dmov_exec_cmdptr_cmd
{
120 struct msm_dmov_cmd dmov_cmd
;
121 struct completion complete
;
124 struct msm_dmov_errdata err
;
128 dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd
*_cmd
,
130 struct msm_dmov_errdata
*err
)
132 struct msm_dmov_exec_cmdptr_cmd
*cmd
= container_of(_cmd
, struct msm_dmov_exec_cmdptr_cmd
, dmov_cmd
);
133 cmd
->result
= result
;
134 if (result
!= 0x80000002 && err
)
135 memcpy(&cmd
->err
, err
, sizeof(struct msm_dmov_errdata
));
137 complete(&cmd
->complete
);
140 int msm_dmov_exec_cmd(unsigned id
, unsigned int cmdptr
)
142 struct msm_dmov_exec_cmdptr_cmd cmd
;
144 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id
, cmdptr
);
146 cmd
.dmov_cmd
.cmdptr
= cmdptr
;
147 cmd
.dmov_cmd
.complete_func
= dmov_exec_cmdptr_complete_func
;
148 cmd
.dmov_cmd
.execute_func
= NULL
;
150 init_completion(&cmd
.complete
);
152 msm_dmov_enqueue_cmd(id
, &cmd
.dmov_cmd
);
153 wait_for_completion(&cmd
.complete
);
155 if (cmd
.result
!= 0x80000002) {
156 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id
, cmd
.result
);
157 PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
158 id
, cmd
.err
.flush
[0], cmd
.err
.flush
[1], cmd
.err
.flush
[2], cmd
.err
.flush
[3]);
161 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id
, cmdptr
);
166 static irqreturn_t
msm_datamover_irq_handler(int irq
, void *dev_id
)
168 unsigned int int_status
, mask
, id
;
169 unsigned long irq_flags
;
170 unsigned int ch_status
;
171 unsigned int ch_result
;
172 struct msm_dmov_cmd
*cmd
;
174 spin_lock_irqsave(&msm_dmov_lock
, irq_flags
);
176 int_status
= readl(DMOV_ISR
); /* read and clear interrupt */
177 PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status
);
180 mask
= int_status
& -int_status
;
182 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status
, mask
, id
);
184 ch_status
= readl(DMOV_STATUS(id
));
185 if (!(ch_status
& DMOV_STATUS_RSLT_VALID
)) {
186 PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id
, ch_status
);
190 ch_result
= readl(DMOV_RSLT(id
));
191 if (list_empty(&active_commands
[id
])) {
192 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
193 "with no active command, status %x, result %x\n",
194 id
, ch_status
, ch_result
);
197 cmd
= list_entry(active_commands
[id
].next
, typeof(*cmd
), list
);
198 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id
, ch_status
, ch_result
);
199 if (ch_result
& DMOV_RSLT_DONE
) {
200 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
202 PRINT_IO("msm_datamover_irq_handler id %d, got result "
203 "for %p, result %x\n", id
, cmd
, ch_result
);
205 list_del(&cmd
->list
);
207 cmd
->complete_func(cmd
, ch_result
, NULL
);
210 if (ch_result
& DMOV_RSLT_FLUSH
) {
211 struct msm_dmov_errdata errdata
;
213 errdata
.flush
[0] = readl(DMOV_FLUSH0(id
));
214 errdata
.flush
[1] = readl(DMOV_FLUSH1(id
));
215 errdata
.flush
[2] = readl(DMOV_FLUSH2(id
));
216 errdata
.flush
[3] = readl(DMOV_FLUSH3(id
));
217 errdata
.flush
[4] = readl(DMOV_FLUSH4(id
));
218 errdata
.flush
[5] = readl(DMOV_FLUSH5(id
));
219 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id
, ch_status
);
220 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id
, ch_result
, errdata
.flush
[0]);
222 list_del(&cmd
->list
);
224 cmd
->complete_func(cmd
, ch_result
, &errdata
);
227 if (ch_result
& DMOV_RSLT_ERROR
) {
228 struct msm_dmov_errdata errdata
;
230 errdata
.flush
[0] = readl(DMOV_FLUSH0(id
));
231 errdata
.flush
[1] = readl(DMOV_FLUSH1(id
));
232 errdata
.flush
[2] = readl(DMOV_FLUSH2(id
));
233 errdata
.flush
[3] = readl(DMOV_FLUSH3(id
));
234 errdata
.flush
[4] = readl(DMOV_FLUSH4(id
));
235 errdata
.flush
[5] = readl(DMOV_FLUSH5(id
));
237 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id
, ch_status
);
238 PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id
, ch_result
, errdata
.flush
[0]);
240 list_del(&cmd
->list
);
242 cmd
->complete_func(cmd
, ch_result
, &errdata
);
244 /* this does not seem to work, once we get an error */
245 /* the datamover will no longer accept commands */
246 writel(0, DMOV_FLUSH0(id
));
248 ch_status
= readl(DMOV_STATUS(id
));
249 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id
, ch_status
);
250 if ((ch_status
& DMOV_STATUS_CMD_PTR_RDY
) && !list_empty(&ready_commands
[id
])) {
251 cmd
= list_entry(ready_commands
[id
].next
, typeof(*cmd
), list
);
252 list_move_tail(&cmd
->list
, &active_commands
[id
]);
253 if (cmd
->execute_func
)
254 cmd
->execute_func(cmd
);
255 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id
);
256 writel(cmd
->cmdptr
, DMOV_CMD_PTR(id
));
258 } while (ch_status
& DMOV_STATUS_RSLT_VALID
);
259 if (list_empty(&active_commands
[id
]) && list_empty(&ready_commands
[id
]))
260 channel_active
&= ~(1U << id
);
261 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id
, ch_status
);
264 if (!channel_active
) {
265 disable_irq_nosync(INT_ADM_AARM
);
266 clk_disable(msm_dmov_clk
);
269 spin_unlock_irqrestore(&msm_dmov_lock
, irq_flags
);
273 static int __init
msm_init_datamover(void)
279 for (i
= 0; i
< MSM_DMOV_CHANNEL_COUNT
; i
++) {
280 INIT_LIST_HEAD(&ready_commands
[i
]);
281 INIT_LIST_HEAD(&active_commands
[i
]);
282 writel(DMOV_CONFIG_IRQ_EN
| DMOV_CONFIG_FORCE_TOP_PTR_RSLT
| DMOV_CONFIG_FORCE_FLUSH_RSLT
, DMOV_CONFIG(i
));
284 clk
= clk_get(NULL
, "adm_clk");
289 ret
= request_irq(INT_ADM_AARM
, msm_datamover_irq_handler
, 0, "msmdatamover", NULL
);
292 disable_irq(INT_ADM_AARM
);
295 module_init(msm_init_datamover
);