Merge commit 'v2.6.28-rc6' into core/debug
[sfrench/cifs-2.6.git] / arch / arm / mach-msm / dma.c
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
16 #include <linux/io.h>
17 #include <linux/interrupt.h>
18 #include <mach/dma.h>
19
20 #define MSM_DMOV_CHANNEL_COUNT 16
21
22 enum {
23         MSM_DMOV_PRINT_ERRORS = 1,
24         MSM_DMOV_PRINT_IO = 2,
25         MSM_DMOV_PRINT_FLOW = 4
26 };
27
28 static DEFINE_SPINLOCK(msm_dmov_lock);
29 static unsigned int channel_active;
30 static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
31 static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
32 unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS;
33
34 #define MSM_DMOV_DPRINTF(mask, format, args...) \
35         do { \
36                 if ((mask) & msm_dmov_print_mask) \
37                         printk(KERN_ERR format, args); \
38         } while (0)
39 #define PRINT_ERROR(format, args...) \
40         MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args);
41 #define PRINT_IO(format, args...) \
42         MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args);
43 #define PRINT_FLOW(format, args...) \
44         MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args);
45
46 void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
47 {
48         writel((graceful << 31), DMOV_FLUSH0(id));
49 }
50
51 void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
52 {
53         unsigned long irq_flags;
54         unsigned int status;
55
56         spin_lock_irqsave(&msm_dmov_lock, irq_flags);
57         status = readl(DMOV_STATUS(id));
58         if (list_empty(&ready_commands[id]) &&
59                 (status & DMOV_STATUS_CMD_PTR_RDY)) {
60 #if 0
61                 if (list_empty(&active_commands[id])) {
62                         PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
63                         writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
64                 }
65 #endif
66                 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
67                 list_add_tail(&cmd->list, &active_commands[id]);
68                 if (!channel_active)
69                         enable_irq(INT_ADM_AARM);
70                 channel_active |= 1U << id;
71                 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
72         } else {
73                 if (list_empty(&active_commands[id]))
74                         PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
75
76                 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
77                 list_add_tail(&cmd->list, &ready_commands[id]);
78         }
79         spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
80 }
81
82 struct msm_dmov_exec_cmdptr_cmd {
83         struct msm_dmov_cmd dmov_cmd;
84         struct completion complete;
85         unsigned id;
86         unsigned int result;
87         struct msm_dmov_errdata err;
88 };
89
90 static void
91 dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
92                                unsigned int result,
93                                struct msm_dmov_errdata *err)
94 {
95         struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
96         cmd->result = result;
97         if (result != 0x80000002 && err)
98                 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
99
100         complete(&cmd->complete);
101 }
102
103 int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
104 {
105         struct msm_dmov_exec_cmdptr_cmd cmd;
106
107         PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
108
109         cmd.dmov_cmd.cmdptr = cmdptr;
110         cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
111         cmd.id = id;
112         init_completion(&cmd.complete);
113
114         msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
115         wait_for_completion(&cmd.complete);
116
117         if (cmd.result != 0x80000002) {
118                 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
119                 PRINT_ERROR("dmov_exec_cmdptr(%d):  flush: %x %x %x %x\n",
120                         id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]);
121                 return -EIO;
122         }
123         PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
124         return 0;
125 }
126
127
128 static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
129 {
130         unsigned int int_status, mask, id;
131         unsigned long irq_flags;
132         unsigned int ch_status;
133         unsigned int ch_result;
134         struct msm_dmov_cmd *cmd;
135
136         spin_lock_irqsave(&msm_dmov_lock, irq_flags);
137
138         int_status = readl(DMOV_ISR); /* read and clear interrupt */
139         PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
140
141         while (int_status) {
142                 mask = int_status & -int_status;
143                 id = fls(mask) - 1;
144                 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
145                 int_status &= ~mask;
146                 ch_status = readl(DMOV_STATUS(id));
147                 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
148                         PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
149                         continue;
150                 }
151                 do {
152                         ch_result = readl(DMOV_RSLT(id));
153                         if (list_empty(&active_commands[id])) {
154                                 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
155                                         "with no active command, status %x, result %x\n",
156                                         id, ch_status, ch_result);
157                                 cmd = NULL;
158                         } else
159                                 cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
160                         PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
161                         if (ch_result & DMOV_RSLT_DONE) {
162                                 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
163                                         id, ch_status);
164                                 PRINT_IO("msm_datamover_irq_handler id %d, got result "
165                                         "for %p, result %x\n", id, cmd, ch_result);
166                                 if (cmd) {
167                                         list_del(&cmd->list);
168                                         cmd->complete_func(cmd, ch_result, NULL);
169                                 }
170                         }
171                         if (ch_result & DMOV_RSLT_FLUSH) {
172                                 struct msm_dmov_errdata errdata;
173
174                                 errdata.flush[0] = readl(DMOV_FLUSH0(id));
175                                 errdata.flush[1] = readl(DMOV_FLUSH1(id));
176                                 errdata.flush[2] = readl(DMOV_FLUSH2(id));
177                                 errdata.flush[3] = readl(DMOV_FLUSH3(id));
178                                 errdata.flush[4] = readl(DMOV_FLUSH4(id));
179                                 errdata.flush[5] = readl(DMOV_FLUSH5(id));
180                                 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
181                                 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
182                                 if (cmd) {
183                                         list_del(&cmd->list);
184                                         cmd->complete_func(cmd, ch_result, &errdata);
185                                 }
186                         }
187                         if (ch_result & DMOV_RSLT_ERROR) {
188                                 struct msm_dmov_errdata errdata;
189
190                                 errdata.flush[0] = readl(DMOV_FLUSH0(id));
191                                 errdata.flush[1] = readl(DMOV_FLUSH1(id));
192                                 errdata.flush[2] = readl(DMOV_FLUSH2(id));
193                                 errdata.flush[3] = readl(DMOV_FLUSH3(id));
194                                 errdata.flush[4] = readl(DMOV_FLUSH4(id));
195                                 errdata.flush[5] = readl(DMOV_FLUSH5(id));
196
197                                 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
198                                 PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]);
199                                 if (cmd) {
200                                         list_del(&cmd->list);
201                                         cmd->complete_func(cmd, ch_result, &errdata);
202                                 }
203                                 /* this does not seem to work, once we get an error */
204                                 /* the datamover will no longer accept commands */
205                                 writel(0, DMOV_FLUSH0(id));
206                         }
207                         ch_status = readl(DMOV_STATUS(id));
208                         PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
209                         if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
210                                 cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
211                                 list_del(&cmd->list);
212                                 list_add_tail(&cmd->list, &active_commands[id]);
213                                 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
214                                 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
215                         }
216                 } while (ch_status & DMOV_STATUS_RSLT_VALID);
217                 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
218                         channel_active &= ~(1U << id);
219                 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
220         }
221
222         if (!channel_active)
223                 disable_irq(INT_ADM_AARM);
224
225         spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
226         return IRQ_HANDLED;
227 }
228
229 static int __init msm_init_datamover(void)
230 {
231         int i;
232         int ret;
233         for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
234                 INIT_LIST_HEAD(&ready_commands[i]);
235                 INIT_LIST_HEAD(&active_commands[i]);
236                 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
237         }
238         ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
239         if (ret)
240                 return ret;
241         disable_irq(INT_ADM_AARM);
242         return 0;
243 }
244
245 arch_initcall(msm_init_datamover);
246