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 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); 32static struct clk *msm_dmov_clk; 33static unsigned int channel_active; 34static 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 50void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful) 51{ 52 writel((graceful << 31), DMOV_FLUSH0(id)); 53} 54 55void 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); 61 if (!channel_active) 62 clk_enable(msm_dmov_clk); 63 dsb(); 64 status = readl(DMOV_STATUS(id)); 65 if (list_empty(&ready_commands[id]) && 66 (status & DMOV_STATUS_CMD_PTR_RDY)) { 67 if (cmd->execute_func) 68 cmd->execute_func(cmd); 69 PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status); 70 list_add_tail(&cmd->list, &active_commands[id]); 71 if (!channel_active) 72 enable_irq(INT_ADM_AARM); 73 channel_active |= 1U << id; 74 writel(cmd->cmdptr, DMOV_CMD_PTR(id)); 75 } else { 76 if (!channel_active) 77 clk_disable(msm_dmov_clk); 78 if (list_empty(&active_commands[id])) 79 PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status); 80 81 PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status); 82 list_add_tail(&cmd->list, &ready_commands[id]); 83 } 84 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags); 85} 86 87struct msm_dmov_exec_cmdptr_cmd { 88 struct msm_dmov_cmd dmov_cmd; 89 struct completion complete; 90 unsigned id; 91 unsigned int result; 92 struct msm_dmov_errdata err; 93}; 94 95static void 96dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd, 97 unsigned int result, 98 struct msm_dmov_errdata *err) 99{ 100 struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd); 101 cmd->result = result; 102 if (result != 0x80000002 && err) 103 memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata)); 104 105 complete(&cmd->complete); 106} 107 108int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr) 109{ 110 struct msm_dmov_exec_cmdptr_cmd cmd; 111 112 PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr); 113 114 cmd.dmov_cmd.cmdptr = cmdptr; 115 cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func; 116 cmd.dmov_cmd.execute_func = NULL; 117 cmd.id = id; 118 init_completion(&cmd.complete); 119 120 msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd); 121 wait_for_completion(&cmd.complete); 122 123 if (cmd.result != 0x80000002) { 124 PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result); 125 PRINT_ERROR("dmov_exec_cmdptr(%d): flush: %x %x %x %x\n", 126 id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]); 127 return -EIO; 128 } 129 PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr); 130 return 0; 131} 132 133 134static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id) 135{ 136 unsigned int int_status, mask, id; 137 unsigned long irq_flags; 138 unsigned int ch_status; 139 unsigned int ch_result; 140 struct msm_dmov_cmd *cmd; 141 142 spin_lock_irqsave(&msm_dmov_lock, irq_flags); 143 144 int_status = readl(DMOV_ISR); /* read and clear interrupt */ 145 PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status); 146 147 while (int_status) { 148 mask = int_status & -int_status; 149 id = fls(mask) - 1; 150 PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id); 151 int_status &= ~mask; 152 ch_status = readl(DMOV_STATUS(id)); 153 if (!(ch_status & DMOV_STATUS_RSLT_VALID)) { 154 PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status); 155 continue; 156 } 157 do { 158 ch_result = readl(DMOV_RSLT(id)); 159 if (list_empty(&active_commands[id])) { 160 PRINT_ERROR("msm_datamover_irq_handler id %d, got result " 161 "with no active command, status %x, result %x\n", 162 id, ch_status, ch_result); 163 cmd = NULL; 164 } else 165 cmd = list_entry(active_commands[id].next, typeof(*cmd), list); 166 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result); 167 if (ch_result & DMOV_RSLT_DONE) { 168 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", 169 id, ch_status); 170 PRINT_IO("msm_datamover_irq_handler id %d, got result " 171 "for %p, result %x\n", id, cmd, ch_result); 172 if (cmd) { 173 list_del(&cmd->list); 174 dsb(); 175 cmd->complete_func(cmd, ch_result, NULL); 176 } 177 } 178 if (ch_result & DMOV_RSLT_FLUSH) { 179 struct msm_dmov_errdata errdata; 180 181 errdata.flush[0] = readl(DMOV_FLUSH0(id)); 182 errdata.flush[1] = readl(DMOV_FLUSH1(id)); 183 errdata.flush[2] = readl(DMOV_FLUSH2(id)); 184 errdata.flush[3] = readl(DMOV_FLUSH3(id)); 185 errdata.flush[4] = readl(DMOV_FLUSH4(id)); 186 errdata.flush[5] = readl(DMOV_FLUSH5(id)); 187 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); 188 PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]); 189 if (cmd) { 190 list_del(&cmd->list); 191 dsb(); 192 cmd->complete_func(cmd, ch_result, &errdata); 193 } 194 } 195 if (ch_result & DMOV_RSLT_ERROR) { 196 struct msm_dmov_errdata errdata; 197 198 errdata.flush[0] = readl(DMOV_FLUSH0(id)); 199 errdata.flush[1] = readl(DMOV_FLUSH1(id)); 200 errdata.flush[2] = readl(DMOV_FLUSH2(id)); 201 errdata.flush[3] = readl(DMOV_FLUSH3(id)); 202 errdata.flush[4] = readl(DMOV_FLUSH4(id)); 203 errdata.flush[5] = readl(DMOV_FLUSH5(id)); 204 205 PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); 206 PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]); 207 if (cmd) { 208 list_del(&cmd->list); 209 dsb(); 210 cmd->complete_func(cmd, ch_result, &errdata); 211 } 212 /* this does not seem to work, once we get an error */ 213 /* the datamover will no longer accept commands */ 214 writel(0, DMOV_FLUSH0(id)); 215 } 216 ch_status = readl(DMOV_STATUS(id)); 217 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); 218 if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) { 219 cmd = list_entry(ready_commands[id].next, typeof(*cmd), list); 220 list_del(&cmd->list); 221 list_add_tail(&cmd->list, &active_commands[id]); 222 if (cmd->execute_func) 223 cmd->execute_func(cmd); 224 PRINT_FLOW("msm_datamover_irq_handler id %d, start command\n", id); 225 writel(cmd->cmdptr, DMOV_CMD_PTR(id)); 226 } 227 } while (ch_status & DMOV_STATUS_RSLT_VALID); 228 if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id])) 229 channel_active &= ~(1U << id); 230 PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); 231 } 232 233 if (!channel_active) { 234 disable_irq_nosync(INT_ADM_AARM); 235 clk_disable(msm_dmov_clk); 236 } 237 238 spin_unlock_irqrestore(&msm_dmov_lock, irq_flags); 239 return IRQ_HANDLED; 240} 241 242static int __init msm_init_datamover(void) 243{ 244 int i; 245 int ret; 246 struct clk *clk; 247 248 for (i = 0; i < MSM_DMOV_CHANNEL_COUNT; i++) { 249 INIT_LIST_HEAD(&ready_commands[i]); 250 INIT_LIST_HEAD(&active_commands[i]); 251 writel(DMOV_CONFIG_IRQ_EN | DMOV_CONFIG_FORCE_TOP_PTR_RSLT | DMOV_CONFIG_FORCE_FLUSH_RSLT, DMOV_CONFIG(i)); 252 } 253 clk = clk_get(NULL, "adm_clk"); 254 if (IS_ERR(clk)) 255 return PTR_ERR(clk); 256 msm_dmov_clk = clk; 257 ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL); 258 if (ret) 259 return ret; 260 disable_irq(INT_ADM_AARM); 261 return 0; 262} 263 264arch_initcall(msm_init_datamover); 265