blob: 8df798ac70c7692886a7a137701c8e8b8bbc0ae2 [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
Russell Kingfced80c2008-09-06 12:10:45 +010016#include <linux/io.h>
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080017#include <linux/interrupt.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010018#include <mach/dma.h>
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080019
20#define MSM_DMOV_CHANNEL_COUNT 16
21
22enum {
23 MSM_DMOV_PRINT_ERRORS = 1,
24 MSM_DMOV_PRINT_IO = 2,
25 MSM_DMOV_PRINT_FLOW = 4
26};
27
28static DEFINE_SPINLOCK(msm_dmov_lock);
Brian Swetland8a0f6f12008-09-10 14:58:25 -070029static unsigned int channel_active;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080030static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT];
31static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT];
32unsigned 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
Brian Swetland8a0f6f12008-09-10 14:58:25 -070046void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful)
47{
48 writel((graceful << 31), DMOV_FLUSH0(id));
49}
50
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080051void 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
San Mehat5b00f402009-11-21 09:22:14 -080066 if (cmd->execute_func)
67 cmd->execute_func(cmd);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080068 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status);
69 list_add_tail(&cmd->list, &active_commands[id]);
Brian Swetland8a0f6f12008-09-10 14:58:25 -070070 if (!channel_active)
71 enable_irq(INT_ADM_AARM);
72 channel_active |= 1U << id;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080073 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
74 } else {
75 if (list_empty(&active_commands[id]))
76 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status);
77
78 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status);
79 list_add_tail(&cmd->list, &ready_commands[id]);
80 }
81 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
82}
83
84struct msm_dmov_exec_cmdptr_cmd {
85 struct msm_dmov_cmd dmov_cmd;
86 struct completion complete;
87 unsigned id;
88 unsigned int result;
Brian Swetland8a0f6f12008-09-10 14:58:25 -070089 struct msm_dmov_errdata err;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080090};
91
Brian Swetland8a0f6f12008-09-10 14:58:25 -070092static void
93dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd,
94 unsigned int result,
95 struct msm_dmov_errdata *err)
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -080096{
97 struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd);
98 cmd->result = result;
Brian Swetland8a0f6f12008-09-10 14:58:25 -070099 if (result != 0x80000002 && err)
100 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata));
101
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800102 complete(&cmd->complete);
103}
104
105int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr)
106{
107 struct msm_dmov_exec_cmdptr_cmd cmd;
108
109 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr);
110
111 cmd.dmov_cmd.cmdptr = cmdptr;
112 cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func;
San Mehat5b00f402009-11-21 09:22:14 -0800113 cmd.dmov_cmd.execute_func = NULL;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800114 cmd.id = id;
115 init_completion(&cmd.complete);
116
117 msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd);
118 wait_for_completion(&cmd.complete);
119
120 if (cmd.result != 0x80000002) {
121 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result);
122 PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n",
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700123 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 -0800124 return -EIO;
125 }
126 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr);
127 return 0;
128}
129
130
131static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id)
132{
133 unsigned int int_status, mask, id;
134 unsigned long irq_flags;
135 unsigned int ch_status;
136 unsigned int ch_result;
137 struct msm_dmov_cmd *cmd;
138
139 spin_lock_irqsave(&msm_dmov_lock, irq_flags);
140
141 int_status = readl(DMOV_ISR); /* read and clear interrupt */
142 PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status);
143
144 while (int_status) {
145 mask = int_status & -int_status;
146 id = fls(mask) - 1;
147 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id);
148 int_status &= ~mask;
149 ch_status = readl(DMOV_STATUS(id));
150 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) {
151 PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status);
152 continue;
153 }
154 do {
155 ch_result = readl(DMOV_RSLT(id));
156 if (list_empty(&active_commands[id])) {
157 PRINT_ERROR("msm_datamover_irq_handler id %d, got result "
158 "with no active command, status %x, result %x\n",
159 id, ch_status, ch_result);
160 cmd = NULL;
161 } else
162 cmd = list_entry(active_commands[id].next, typeof(*cmd), list);
163 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result);
164 if (ch_result & DMOV_RSLT_DONE) {
165 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n",
166 id, ch_status);
167 PRINT_IO("msm_datamover_irq_handler id %d, got result "
168 "for %p, result %x\n", id, cmd, ch_result);
169 if (cmd) {
170 list_del(&cmd->list);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700171 cmd->complete_func(cmd, ch_result, NULL);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800172 }
173 }
174 if (ch_result & DMOV_RSLT_FLUSH) {
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700175 struct msm_dmov_errdata errdata;
176
177 errdata.flush[0] = readl(DMOV_FLUSH0(id));
178 errdata.flush[1] = readl(DMOV_FLUSH1(id));
179 errdata.flush[2] = readl(DMOV_FLUSH2(id));
180 errdata.flush[3] = readl(DMOV_FLUSH3(id));
181 errdata.flush[4] = readl(DMOV_FLUSH4(id));
182 errdata.flush[5] = readl(DMOV_FLUSH5(id));
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800183 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700184 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 -0800185 if (cmd) {
186 list_del(&cmd->list);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700187 cmd->complete_func(cmd, ch_result, &errdata);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800188 }
189 }
190 if (ch_result & DMOV_RSLT_ERROR) {
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700191 struct msm_dmov_errdata errdata;
192
193 errdata.flush[0] = readl(DMOV_FLUSH0(id));
194 errdata.flush[1] = readl(DMOV_FLUSH1(id));
195 errdata.flush[2] = readl(DMOV_FLUSH2(id));
196 errdata.flush[3] = readl(DMOV_FLUSH3(id));
197 errdata.flush[4] = readl(DMOV_FLUSH4(id));
198 errdata.flush[5] = readl(DMOV_FLUSH5(id));
199
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800200 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700201 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 -0800202 if (cmd) {
203 list_del(&cmd->list);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700204 cmd->complete_func(cmd, ch_result, &errdata);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800205 }
206 /* this does not seem to work, once we get an error */
207 /* the datamover will no longer accept commands */
208 writel(0, DMOV_FLUSH0(id));
209 }
210 ch_status = readl(DMOV_STATUS(id));
211 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
212 if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) {
213 cmd = list_entry(ready_commands[id].next, typeof(*cmd), list);
214 list_del(&cmd->list);
215 list_add_tail(&cmd->list, &active_commands[id]);
San Mehat5b00f402009-11-21 09:22:14 -0800216 if (cmd->execute_func)
217 cmd->execute_func(cmd);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800218 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id);
219 writel(cmd->cmdptr, DMOV_CMD_PTR(id));
220 }
221 } while (ch_status & DMOV_STATUS_RSLT_VALID);
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700222 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id]))
223 channel_active &= ~(1U << id);
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800224 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status);
225 }
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700226
227 if (!channel_active)
228 disable_irq(INT_ADM_AARM);
229
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800230 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags);
231 return IRQ_HANDLED;
232}
233
234static int __init msm_init_datamover(void)
235{
236 int i;
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700237 int ret;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800238 for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) {
239 INIT_LIST_HEAD(&ready_commands[i]);
240 INIT_LIST_HEAD(&active_commands[i]);
241 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i));
242 }
Brian Swetland8a0f6f12008-09-10 14:58:25 -0700243 ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL);
244 if (ret)
245 return ret;
246 disable_irq(INT_ADM_AARM);
247 return 0;
Arve Hjønnevågbfe645a2007-11-26 04:12:29 -0800248}
249
250arch_initcall(msm_init_datamover);
251