Merge remote-tracking branch 'regulator/topic/lp8755' into regulator-next
[linux.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/clk.h>
17 #include <linux/err.h>
18 #include <linux/io.h>
19 #include <linux/interrupt.h>
20 #include <linux/completion.h>
21 #include <mach/dma.h>
22 #include <mach/msm_iomap.h>
23
24 #define MSM_DMOV_CHANNEL_COUNT 16
25
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))
30
31 #if defined(CONFIG_ARCH_MSM7X30)
32 #define DMOV_SD_AARM DMOV_SD2
33 #else
34 #define DMOV_SD_AARM DMOV_SD3
35 #endif
36
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)
45
46 #define DMOV_STATUS(ch)       DMOV_SD_AARM(0x200, ch)
47 #define DMOV_ISR              DMOV_SD_AARM(0x380, 0)
48
49 #define DMOV_CONFIG(ch)       DMOV_SD_AARM(0x300, ch)
50
51 enum {
52         MSM_DMOV_PRINT_ERRORS = 1,
53         MSM_DMOV_PRINT_IO = 2,
54         MSM_DMOV_PRINT_FLOW = 4
55 };
56
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;
63
64 #define MSM_DMOV_DPRINTF(mask, format, args...) \
65         do { \
66                 if ((mask) & msm_dmov_print_mask) \
67                         printk(KERN_ERR format, args); \
68         } while (0)
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);
75
76 void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
77 {
78         writel((graceful << 31), DMOV_FLUSH0(id));
79 }
80
81 void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
82 {
83         unsigned long irq_flags;
84         unsigned int status;
85
86         spin_lock_irqsave(&msm_dmov_lock, irq_flags);
87         if (!channel_active)
88                 clk_enable(msm_dmov_clk);
89         dsb();
90         status = readl(DMOV_STATUS(id));
91         if (list_empty(&ready_commands[id]) &&
92                 (status & DMOV_STATUS_CMD_PTR_RDY)) {
93 #if 0
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));
97                 }
98 #endif
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]);
103                 if (!channel_active)
104                         enable_irq(INT_ADM_AARM);
105                 channel_active |= 1U << id;
106                 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
107         } else {
108                 if (!channel_active)
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);
112
113                 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
114                 list_add_tail(&cmd->list, &ready_commands[id]);
115         }
116         spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
117 }
118
119 struct msm_dmov_exec_cmdptr_cmd {
120         struct msm_dmov_cmd dmov_cmd;
121         struct completion complete;
122         unsigned id;
123         unsigned int result;
124         struct msm_dmov_errdata err;
125 };
126
127 static void
128 dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
129                                unsigned int result,
130                                struct msm_dmov_errdata *err)
131 {
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));
136
137         complete(&cmd->complete);
138 }
139
140 int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
141 {
142         struct msm_dmov_exec_cmdptr_cmd cmd;
143
144         PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
145
146         cmd.dmov_cmd.cmdptr = cmdptr;
147         cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
148         cmd.dmov_cmd.execute_func = NULL;
149         cmd.id = id;
150         init_completion(&cmd.complete);
151
152         msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
153         wait_for_completion(&cmd.complete);
154
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]);
159                 return -EIO;
160         }
161         PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
162         return 0;
163 }
164
165
166 static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
167 {
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;
173
174         spin_lock_irqsave(&msm_dmov_lock, irq_flags);
175
176         int_status = readl(DMOV_ISR); /* read and clear interrupt */
177         PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
178
179         while (int_status) {
180                 mask = int_status & -int_status;
181                 id = fls(mask) - 1;
182                 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
183                 int_status &= ~mask;
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);
187                         continue;
188                 }
189                 do {
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);
195                                 cmd = NULL;
196                         } else
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",
201                                         id, ch_status);
202                                 PRINT_IO("msm_datamover_irq_handler id %d, got result "
203                                         "for %p, result %x\n", id, cmd, ch_result);
204                                 if (cmd) {
205                                         list_del(&cmd->list);
206                                         dsb();
207                                         cmd->complete_func(cmd, ch_result, NULL);
208                                 }
209                         }
210                         if (ch_result & DMOV_RSLT_FLUSH) {
211                                 struct msm_dmov_errdata errdata;
212
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]);
221                                 if (cmd) {
222                                         list_del(&cmd->list);
223                                         dsb();
224                                         cmd->complete_func(cmd, ch_result, &errdata);
225                                 }
226                         }
227                         if (ch_result & DMOV_RSLT_ERROR) {
228                                 struct msm_dmov_errdata errdata;
229
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));
236
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]);
239                                 if (cmd) {
240                                         list_del(&cmd->list);
241                                         dsb();
242                                         cmd->complete_func(cmd, ch_result, &errdata);
243                                 }
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));
247                         }
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));
257                         }
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);
262         }
263
264         if (!channel_active) {
265                 disable_irq_nosync(INT_ADM_AARM);
266                 clk_disable(msm_dmov_clk);
267         }
268
269         spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
270         return IRQ_HANDLED;
271 }
272
273 static int __init msm_init_datamover(void)
274 {
275         int i;
276         int ret;
277         struct clk *clk;
278
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));
283         }
284         clk = clk_get(NULL, "adm_clk");
285         if (IS_ERR(clk))
286                 return PTR_ERR(clk);
287         clk_prepare(clk);
288         msm_dmov_clk = clk;
289         ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
290         if (ret)
291                 return ret;
292         disable_irq(INT_ADM_AARM);
293         return 0;
294 }
295 module_init(msm_init_datamover);