blob: e80cad468925ac138200e9490838f4d96e401ace [file] [log] [blame]
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -08001/* 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
Arve Hjønnevågc5541072009-06-25 17:03:14 -070016#include <linux/clk.h>
17#include <linux/err.h>
Russell Kingfced80c2008-09-06 12:10:45 +010018#include <linux/io.h>
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080019#include <linux/interrupt.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/dma.h>
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080021
22#define MSM_DMOV_CHANNEL_COUNT 16
23
24enum {
25 MSM_DMOV_PRINT_ERRORS = 1,
26 MSM_DMOV_PRINT_IO = 2,
27 MSM_DMOV_PRINT_FLOW = 4
28};
29
30static DEFINE_SPINLOCK(msm_dmov_lock);
Arve Hjønnevågc5541072009-06-25 17:03:14 -070031static struct clk *msm_dmov_clk;
Brian Swetland8a0f6f12008-09-10 14:58:25 -070032static unsigned int channel_active;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080033static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
34static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
35unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS;
36
37#define MSM_DMOV_DPRINTF(mask, format, args...) \
38 do { \
39 if ((mask) & msm_dmov_print_mask) \
40 printk(KERN_ERR format, args); \
41 } while (0)
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);
48
Brian Swetland8a0f6f12008-09-10 14:58:25 -070049void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
50{
51 writel((graceful << 31), DMOV_FLUSH0(id));
52}
53
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080054void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
55{
56 unsigned long irq_flags;
57 unsigned int status;
58
59 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
Arve Hjønnevågc5541072009-06-25 17:03:14 -070060 if (!channel_active)
61 clk_enable(msm_dmov_clk);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080062 status = readl(DMOV_STATUS(id));
63 if (list_empty(&ready_commands[id]) &&
64 (status & DMOV_STATUS_CMD_PTR_RDY)) {
65#if 0
66 if (list_empty(&active_commands[id])) {
67 PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
68 writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
69 }
70#endif
71 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
72 list_add_tail(&cmd->list, &active_commands[id]);
Brian Swetland8a0f6f12008-09-10 14:58:25 -070073 if (!channel_active)
74 enable_irq(INT_ADM_AARM);
75 channel_active |= 1U << id;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080076 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
77 } else {
Arve Hjønnevågc5541072009-06-25 17:03:14 -070078 if (!channel_active)
79 clk_disable(msm_dmov_clk);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080080 if (list_empty(&active_commands[id]))
81 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
82
83 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
84 list_add_tail(&cmd->list, &ready_commands[id]);
85 }
86 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
87}
88
89struct msm_dmov_exec_cmdptr_cmd {
90 struct msm_dmov_cmd dmov_cmd;
91 struct completion complete;
92 unsigned id;
93 unsigned int result;
Brian Swetland8a0f6f12008-09-10 14:58:25 -070094 struct msm_dmov_errdata err;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080095};
96
Brian Swetland8a0f6f12008-09-10 14:58:25 -070097static void
98dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
99 unsigned int result,
100 struct msm_dmov_errdata *err)
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800101{
102 struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
103 cmd->result = result;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700104 if (result != 0x80000002 && err)
105 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
106
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800107 complete(&cmd->complete);
108}
109
110int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
111{
112 struct msm_dmov_exec_cmdptr_cmd cmd;
113
114 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
115
116 cmd.dmov_cmd.cmdptr = cmdptr;
117 cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
118 cmd.id = id;
119 init_completion(&cmd.complete);
120
121 msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
122 wait_for_completion(&cmd.complete);
123
124 if (cmd.result != 0x80000002) {
125 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
126 PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700127 id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800128 return -EIO;
129 }
130 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
131 return 0;
132}
133
134
135static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
136{
137 unsigned int int_status, mask, id;
138 unsigned long irq_flags;
139 unsigned int ch_status;
140 unsigned int ch_result;
141 struct msm_dmov_cmd *cmd;
142
143 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
144
145 int_status = readl(DMOV_ISR); /* read and clear interrupt */
146 PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
147
148 while (int_status) {
149 mask = int_status & -int_status;
150 id = fls(mask) - 1;
151 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
152 int_status &= ~mask;
153 ch_status = readl(DMOV_STATUS(id));
154 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
155 PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
156 continue;
157 }
158 do {
159 ch_result = readl(DMOV_RSLT(id));
160 if (list_empty(&active_commands[id])) {
161 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
162 "with no active command, status %x, result %x\n",
163 id, ch_status, ch_result);
164 cmd = NULL;
165 } else
166 cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
167 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
168 if (ch_result & DMOV_RSLT_DONE) {
169 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
170 id, ch_status);
171 PRINT_IO("msm_datamover_irq_handler id %d, got result "
172 "for %p, result %x\n", id, cmd, ch_result);
173 if (cmd) {
174 list_del(&cmd->list);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700175 cmd->complete_func(cmd, ch_result, NULL);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800176 }
177 }
178 if (ch_result & DMOV_RSLT_FLUSH) {
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700179 struct msm_dmov_errdata errdata;
180
181 errdata.flush[0] = readl(DMOV_FLUSH0(id));
182 errdata.flush[1] = readl(DMOV_FLUSH1(id));
183 errdata.flush[2] = readl(DMOV_FLUSH2(id));
184 errdata.flush[3] = readl(DMOV_FLUSH3(id));
185 errdata.flush[4] = readl(DMOV_FLUSH4(id));
186 errdata.flush[5] = readl(DMOV_FLUSH5(id));
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800187 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700188 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800189 if (cmd) {
190 list_del(&cmd->list);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700191 cmd->complete_func(cmd, ch_result, &errdata);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800192 }
193 }
194 if (ch_result & DMOV_RSLT_ERROR) {
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700195 struct msm_dmov_errdata errdata;
196
197 errdata.flush[0] = readl(DMOV_FLUSH0(id));
198 errdata.flush[1] = readl(DMOV_FLUSH1(id));
199 errdata.flush[2] = readl(DMOV_FLUSH2(id));
200 errdata.flush[3] = readl(DMOV_FLUSH3(id));
201 errdata.flush[4] = readl(DMOV_FLUSH4(id));
202 errdata.flush[5] = readl(DMOV_FLUSH5(id));
203
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800204 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700205 PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800206 if (cmd) {
207 list_del(&cmd->list);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700208 cmd->complete_func(cmd, ch_result, &errdata);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800209 }
210 /* this does not seem to work, once we get an error */
211 /* the datamover will no longer accept commands */
212 writel(0, DMOV_FLUSH0(id));
213 }
214 ch_status = readl(DMOV_STATUS(id));
215 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
216 if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
217 cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
218 list_del(&cmd->list);
219 list_add_tail(&cmd->list, &active_commands[id]);
220 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
221 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
222 }
223 } while (ch_status & DMOV_STATUS_RSLT_VALID);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700224 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
225 channel_active &= ~(1U << id);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800226 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
227 }
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700228
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700229 if (!channel_active) {
Arve Hjønnevåga6407dd2009-04-17 18:21:34 -0700230 disable_irq_nosync(INT_ADM_AARM);
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700231 clk_disable(msm_dmov_clk);
232 }
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700233
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800234 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
235 return IRQ_HANDLED;
236}
237
238static int __init msm_init_datamover(void)
239{
240 int i;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700241 int ret;
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700242 struct clk *clk;
243
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800244 for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
245 INIT_LIST_HEAD(&ready_commands[i]);
246 INIT_LIST_HEAD(&active_commands[i]);
247 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
248 }
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700249 clk = clk_get(NULL, "adm_clk");
250 if (IS_ERR(clk))
251 return PTR_ERR(clk);
252 msm_dmov_clk = clk;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700253 ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
254 if (ret)
255 return ret;
256 disable_irq(INT_ADM_AARM);
257 return 0;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800258}
259
260arch_initcall(msm_init_datamover);
261