blob: 354b91d4c3ac190da702970f6eac58bce3a74720 [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>
Daniel Walker6d7b7d52010-05-20 15:39:33 -070020#include <linux/completion.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010021#include <mach/dma.h>
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080022
23#define MSM_DMOV_CHANNEL_COUNT 16
24
25enum {
26 MSM_DMOV_PRINT_ERRORS = 1,
27 MSM_DMOV_PRINT_IO = 2,
28 MSM_DMOV_PRINT_FLOW = 4
29};
30
31static DEFINE_SPINLOCK(msm_dmov_lock);
Arve Hjønnevågc5541072009-06-25 17:03:14 -070032static struct clk *msm_dmov_clk;
Brian Swetland8a0f6f12008-09-10 14:58:25 -070033static unsigned int channel_active;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080034static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
35static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
36unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS;
37
38#define MSM_DMOV_DPRINTF(mask, format, args...) \
39 do { \
40 if ((mask) & msm_dmov_print_mask) \
41 printk(KERN_ERR format, args); \
42 } while (0)
43#define PRINT_ERROR(format, args...) \
44 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args);
45#define PRINT_IO(format, args...) \
46 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args);
47#define PRINT_FLOW(format, args...) \
48 MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args);
49
Brian Swetland8a0f6f12008-09-10 14:58:25 -070050void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
51{
52 writel((graceful << 31), DMOV_FLUSH0(id));
53}
54
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080055void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd)
56{
57 unsigned long irq_flags;
58 unsigned int status;
59
60 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
Arve Hjønnevågc5541072009-06-25 17:03:14 -070061 if (!channel_active)
62 clk_enable(msm_dmov_clk);
Brian Swetland9f68fcd2009-08-19 18:49:45 -070063 dsb();
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080064 status = readl(DMOV_STATUS(id));
65 if (list_empty(&ready_commands[id]) &&
66 (status & DMOV_STATUS_CMD_PTR_RDY)) {
67#if 0
68 if (list_empty(&active_commands[id])) {
69 PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id);
70 writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id));
71 }
72#endif
San Mehat5b00f402009-11-21 09:22:14 -080073 if (cmd->execute_func)
74 cmd->execute_func(cmd);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080075 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
76 list_add_tail(&cmd->list, &active_commands[id]);
Brian Swetland8a0f6f12008-09-10 14:58:25 -070077 if (!channel_active)
78 enable_irq(INT_ADM_AARM);
79 channel_active |= 1U << id;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080080 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
81 } else {
Arve Hjønnevågc5541072009-06-25 17:03:14 -070082 if (!channel_active)
83 clk_disable(msm_dmov_clk);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080084 if (list_empty(&active_commands[id]))
85 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
86
87 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
88 list_add_tail(&cmd->list, &ready_commands[id]);
89 }
90 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
91}
92
93struct msm_dmov_exec_cmdptr_cmd {
94 struct msm_dmov_cmd dmov_cmd;
95 struct completion complete;
96 unsigned id;
97 unsigned int result;
Brian Swetland8a0f6f12008-09-10 14:58:25 -070098 struct msm_dmov_errdata err;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080099};
100
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700101static void
102dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
103 unsigned int result,
104 struct msm_dmov_errdata *err)
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800105{
106 struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
107 cmd->result = result;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700108 if (result != 0x80000002 && err)
109 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
110
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800111 complete(&cmd->complete);
112}
113
114int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
115{
116 struct msm_dmov_exec_cmdptr_cmd cmd;
117
118 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
119
120 cmd.dmov_cmd.cmdptr = cmdptr;
121 cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
San Mehat5b00f402009-11-21 09:22:14 -0800122 cmd.dmov_cmd.execute_func = NULL;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800123 cmd.id = id;
124 init_completion(&cmd.complete);
125
126 msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
127 wait_for_completion(&cmd.complete);
128
129 if (cmd.result != 0x80000002) {
130 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
131 PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700132 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 -0800133 return -EIO;
134 }
135 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
136 return 0;
137}
138
139
140static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
141{
142 unsigned int int_status, mask, id;
143 unsigned long irq_flags;
144 unsigned int ch_status;
145 unsigned int ch_result;
146 struct msm_dmov_cmd *cmd;
147
148 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
149
150 int_status = readl(DMOV_ISR); /* read and clear interrupt */
151 PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
152
153 while (int_status) {
154 mask = int_status & -int_status;
155 id = fls(mask) - 1;
156 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
157 int_status &= ~mask;
158 ch_status = readl(DMOV_STATUS(id));
159 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
160 PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
161 continue;
162 }
163 do {
164 ch_result = readl(DMOV_RSLT(id));
165 if (list_empty(&active_commands[id])) {
166 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
167 "with no active command, status %x, result %x\n",
168 id, ch_status, ch_result);
169 cmd = NULL;
170 } else
171 cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
172 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
173 if (ch_result & DMOV_RSLT_DONE) {
174 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
175 id, ch_status);
176 PRINT_IO("msm_datamover_irq_handler id %d, got result "
177 "for %p, result %x\n", id, cmd, ch_result);
178 if (cmd) {
179 list_del(&cmd->list);
Brian Swetland9f68fcd2009-08-19 18:49:45 -0700180 dsb();
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700181 cmd->complete_func(cmd, ch_result, NULL);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800182 }
183 }
184 if (ch_result & DMOV_RSLT_FLUSH) {
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700185 struct msm_dmov_errdata errdata;
186
187 errdata.flush[0] = readl(DMOV_FLUSH0(id));
188 errdata.flush[1] = readl(DMOV_FLUSH1(id));
189 errdata.flush[2] = readl(DMOV_FLUSH2(id));
190 errdata.flush[3] = readl(DMOV_FLUSH3(id));
191 errdata.flush[4] = readl(DMOV_FLUSH4(id));
192 errdata.flush[5] = readl(DMOV_FLUSH5(id));
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800193 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700194 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 -0800195 if (cmd) {
196 list_del(&cmd->list);
Brian Swetland9f68fcd2009-08-19 18:49:45 -0700197 dsb();
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700198 cmd->complete_func(cmd, ch_result, &errdata);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800199 }
200 }
201 if (ch_result & DMOV_RSLT_ERROR) {
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700202 struct msm_dmov_errdata errdata;
203
204 errdata.flush[0] = readl(DMOV_FLUSH0(id));
205 errdata.flush[1] = readl(DMOV_FLUSH1(id));
206 errdata.flush[2] = readl(DMOV_FLUSH2(id));
207 errdata.flush[3] = readl(DMOV_FLUSH3(id));
208 errdata.flush[4] = readl(DMOV_FLUSH4(id));
209 errdata.flush[5] = readl(DMOV_FLUSH5(id));
210
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800211 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700212 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 -0800213 if (cmd) {
214 list_del(&cmd->list);
Brian Swetland9f68fcd2009-08-19 18:49:45 -0700215 dsb();
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700216 cmd->complete_func(cmd, ch_result, &errdata);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800217 }
218 /* this does not seem to work, once we get an error */
219 /* the datamover will no longer accept commands */
220 writel(0, DMOV_FLUSH0(id));
221 }
222 ch_status = readl(DMOV_STATUS(id));
223 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
224 if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
225 cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
Wei Yongjun6eebf2d2012-09-05 14:53:46 +0800226 list_move_tail(&cmd->list, &active_commands[id]);
San Mehat5b00f402009-11-21 09:22:14 -0800227 if (cmd->execute_func)
228 cmd->execute_func(cmd);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800229 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
230 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
231 }
232 } while (ch_status & DMOV_STATUS_RSLT_VALID);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700233 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
234 channel_active &= ~(1U << id);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800235 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
236 }
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700237
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700238 if (!channel_active) {
Arve Hjønnevåga6407dd2009-04-17 18:21:34 -0700239 disable_irq_nosync(INT_ADM_AARM);
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700240 clk_disable(msm_dmov_clk);
241 }
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700242
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800243 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
244 return IRQ_HANDLED;
245}
246
247static int __init msm_init_datamover(void)
248{
249 int i;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700250 int ret;
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700251 struct clk *clk;
252
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800253 for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
254 INIT_LIST_HEAD(&ready_commands[i]);
255 INIT_LIST_HEAD(&active_commands[i]);
256 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
257 }
Arve Hjønnevågc5541072009-06-25 17:03:14 -0700258 clk = clk_get(NULL, "adm_clk");
259 if (IS_ERR(clk))
260 return PTR_ERR(clk);
261 msm_dmov_clk = clk;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700262 ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
263 if (ret)
264 return ret;
265 disable_irq(INT_ADM_AARM);
266 return 0;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800267}
268
269arch_initcall(msm_init_datamover);
270