Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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 | |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 16 | #include <linux/clk.h> |
| 17 | #include <linux/err.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Daniel Walker | 6d7b7d5 | 2010-05-20 15:39:33 -0700 | [diff] [blame] | 20 | #include <linux/completion.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 21 | #include <mach/dma.h> |
Stephen Boyd | 430f1ee | 2013-03-04 15:08:25 -0800 | [diff] [blame] | 22 | #include <mach/msm_iomap.h> |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 23 | |
| 24 | #define MSM_DMOV_CHANNEL_COUNT 16 |
| 25 | |
Stephen Boyd | 430f1ee | 2013-03-04 15:08:25 -0800 | [diff] [blame] | 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 | |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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); |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 58 | static struct clk *msm_dmov_clk; |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 59 | static unsigned int channel_active; |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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 | |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 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 | |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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); |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 87 | if (!channel_active) |
| 88 | clk_enable(msm_dmov_clk); |
Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 89 | dsb(); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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 |
San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame] | 99 | if (cmd->execute_func) |
| 100 | cmd->execute_func(cmd); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 101 | PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status); |
| 102 | list_add_tail(&cmd->list, &active_commands[id]); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 103 | if (!channel_active) |
| 104 | enable_irq(INT_ADM_AARM); |
| 105 | channel_active |= 1U << id; |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 106 | writel(cmd->cmdptr, DMOV_CMD_PTR(id)); |
| 107 | } else { |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 108 | if (!channel_active) |
| 109 | clk_disable(msm_dmov_clk); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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; |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 124 | struct msm_dmov_errdata err; |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 127 | static void |
| 128 | dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd, |
| 129 | unsigned int result, |
| 130 | struct msm_dmov_errdata *err) |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 131 | { |
| 132 | struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd); |
| 133 | cmd->result = result; |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 134 | if (result != 0x80000002 && err) |
| 135 | memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata)); |
| 136 | |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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; |
San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame] | 148 | cmd.dmov_cmd.execute_func = NULL; |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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", |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 158 | id, cmd.err.flush[0], cmd.err.flush[1], cmd.err.flush[2], cmd.err.flush[3]); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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); |
Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 206 | dsb(); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 207 | cmd->complete_func(cmd, ch_result, NULL); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | if (ch_result & DMOV_RSLT_FLUSH) { |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 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)); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 219 | PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 220 | PRINT_FLOW("msm_datamover_irq_handler id %d, flush, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 221 | if (cmd) { |
| 222 | list_del(&cmd->list); |
Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 223 | dsb(); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 224 | cmd->complete_func(cmd, ch_result, &errdata); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | if (ch_result & DMOV_RSLT_ERROR) { |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 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 | |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 237 | PRINT_ERROR("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 238 | PRINT_ERROR("msm_datamover_irq_handler id %d, error, result %x, flush0 %x\n", id, ch_result, errdata.flush[0]); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 239 | if (cmd) { |
| 240 | list_del(&cmd->list); |
Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 241 | dsb(); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 242 | cmd->complete_func(cmd, ch_result, &errdata); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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); |
Wei Yongjun | 6eebf2d | 2012-09-05 14:53:46 +0800 | [diff] [blame] | 252 | list_move_tail(&cmd->list, &active_commands[id]); |
San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame] | 253 | if (cmd->execute_func) |
| 254 | cmd->execute_func(cmd); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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); |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 259 | if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id])) |
| 260 | channel_active &= ~(1U << id); |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 261 | PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); |
| 262 | } |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 263 | |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 264 | if (!channel_active) { |
Arve Hjønnevåg | a6407dd | 2009-04-17 18:21:34 -0700 | [diff] [blame] | 265 | disable_irq_nosync(INT_ADM_AARM); |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 266 | clk_disable(msm_dmov_clk); |
| 267 | } |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 268 | |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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; |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 276 | int ret; |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 277 | struct clk *clk; |
| 278 | |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 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 | } |
Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 284 | clk = clk_get(NULL, "adm_clk"); |
| 285 | if (IS_ERR(clk)) |
| 286 | return PTR_ERR(clk); |
| 287 | msm_dmov_clk = clk; |
Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 288 | ret = request_irq(INT_ADM_AARM, msm_datamover_irq_handler, 0, "msmdatamover", NULL); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | disable_irq(INT_ADM_AARM); |
| 292 | return 0; |
Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | arch_initcall(msm_init_datamover); |
| 296 | |