Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.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 <asm/io.h>
17 #include <linux/interrupt.h>
18 #include <asm/arch/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 struct msm_dmov_cmd active_command;
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_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
47 {
48         unsigned long irq_flags;
49         unsigned int status;
50
51         spin_lock_irqsave(&msm_dmov_lock, irq_flags);
52         status = readl(DMOV_STATUS(id));
53         if (list_empty(&ready_commands[id]) &&
54                 (status & DMOV_STATUS_CMD_PTR_RDY)) {
55 #if 0
56                 if (list_empty(&active_commands[id])) {
57                         PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
58                         writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
59                 }
60 #endif
61                 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
62                 list_add_tail(&cmd->list, &active_commands[id]);
63                 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
64         } else {
65                 if (list_empty(&active_commands[id]))
66                         PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
67
68                 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
69                 list_add_tail(&cmd->list, &ready_commands[id]);
70         }
71         spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
72 }
73
74 struct msm_dmov_exec_cmdptr_cmd {
75         struct msm_dmov_cmd dmov_cmd;
76         struct completion complete;
77         unsigned id;
78         unsigned int result;
79         unsigned int flush[6];
80 };
81
82 static void dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd, unsigned int result)
83 {
84         struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
85         cmd->result = result;
86         if (result != 0x80000002) {
87                 cmd->flush[0] = readl(DMOV_FLUSH0(cmd->id));
88                 cmd->flush[1] = readl(DMOV_FLUSH1(cmd->id));
89                 cmd->flush[2] = readl(DMOV_FLUSH2(cmd->id));
90                 cmd->flush[3] = readl(DMOV_FLUSH3(cmd->id));
91                 cmd->flush[4] = readl(DMOV_FLUSH4(cmd->id));
92                 cmd->flush[5] = readl(DMOV_FLUSH5(cmd->id));
93         }
94         complete(&cmd->complete);
95 }
96
97 int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
98 {
99         struct msm_dmov_exec_cmdptr_cmd cmd;
100
101         PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
102
103         cmd.dmov_cmd.cmdptr = cmdptr;
104         cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
105         cmd.id = id;
106         init_completion(&cmd.complete);
107
108         msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
109         wait_for_completion(&cmd.complete);
110
111         if (cmd.result != 0x80000002) {
112                 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
113                 PRINT_ERROR("dmov_exec_cmdptr(%d):  flush: %x %x %x %x\n",
114                         id, cmd.flush[0], cmd.flush[1], cmd.flush[2], cmd.flush[3]);
115                 return -EIO;
116         }
117         PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
118         return 0;
119 }
120
121
122 static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
123 {
124         unsigned int int_status, mask, id;
125         unsigned long irq_flags;
126         unsigned int ch_status;
127         unsigned int ch_result;
128         struct msm_dmov_cmd *cmd;
129
130         spin_lock_irqsave(&msm_dmov_lock, irq_flags);
131
132         int_status = readl(DMOV_ISR); /* read and clear interrupt */
133         PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
134
135         while (int_status) {
136                 mask = int_status & -int_status;
137                 id = fls(mask) - 1;
138                 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
139                 int_status &= ~mask;
140                 ch_status = readl(DMOV_STATUS(id));
141                 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
142                         PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
143                         continue;
144                 }
145                 do {
146                         ch_result = readl(DMOV_RSLT(id));
147                         if (list_empty(&active_commands[id])) {
148                                 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
149                                         "with no active command, status %x, result %x\n",
150                                         id, ch_status, ch_result);
151                                 cmd = NULL;
152                         } else
153                                 cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
154                         PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
155                         if (ch_result & DMOV_RSLT_DONE) {
156                                 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
157                                         id, ch_status);
158                                 PRINT_IO("msm_datamover_irq_handler id %d, got result "
159                                         "for %p, result %x\n", id, cmd, ch_result);
160                                 if (cmd) {
161                                         list_del(&cmd->list);
162                                         cmd->complete_func(cmd, ch_result);
163                                 }
164                         }
165                         if (ch_result & DMOV_RSLT_FLUSH) {
166                                 unsigned int flush0 = readl(DMOV_FLUSH0(id));
167                                 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
168                                 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, flush0);
169                                 if (cmd) {
170                                         list_del(&cmd->list);
171                                         cmd->complete_func(cmd, ch_result);
172                                 }
173                         }
174                         if (ch_result & DMOV_RSLT_ERROR) {
175                                 unsigned int flush0 = readl(DMOV_FLUSH0(id));
176                                 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
177                                 PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, flush0);
178                                 if (cmd) {
179                                         list_del(&cmd->list);
180                                         cmd->complete_func(cmd, ch_result);
181                                 }
182                                 /* this does not seem to work, once we get an error */
183                                 /* the datamover will no longer accept commands */
184                                 writel(0, DMOV_FLUSH0(id));
185                         }
186                         ch_status = readl(DMOV_STATUS(id));
187                         PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
188                         if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
189                                 cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
190                                 list_del(&cmd->list);
191                                 list_add_tail(&cmd->list, &active_commands[id]);
192                                 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
193                                 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
194                         }
195                 } while (ch_status & DMOV_STATUS_RSLT_VALID);
196                 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
197         }
198         spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
199         return IRQ_HANDLED;
200 }
201
202 static int __init msm_init_datamover(void)
203 {
204         int i;
205         for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
206                 INIT_LIST_HEAD(&ready_commands[i]);
207                 INIT_LIST_HEAD(&active_commands[i]);
208                 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
209         }
210         return request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
211 }
212
213 arch_initcall(msm_init_datamover);
214