| 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> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 20 | #include <mach/dma.h> | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 21 |  | 
 | 22 | #define MSM_DMOV_CHANNEL_COUNT 16 | 
 | 23 |  | 
 | 24 | enum { | 
 | 25 | 	MSM_DMOV_PRINT_ERRORS = 1, | 
 | 26 | 	MSM_DMOV_PRINT_IO = 2, | 
 | 27 | 	MSM_DMOV_PRINT_FLOW = 4 | 
 | 28 | }; | 
 | 29 |  | 
 | 30 | static DEFINE_SPINLOCK(msm_dmov_lock); | 
| Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 31 | static struct clk *msm_dmov_clk; | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 32 | static unsigned int channel_active; | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 33 | static struct list_head ready_commands[MSM_DMOV_CHANNEL_COUNT]; | 
 | 34 | static struct list_head active_commands[MSM_DMOV_CHANNEL_COUNT]; | 
 | 35 | unsigned int msm_dmov_print_mask = MSM_DMOV_PRINT_ERRORS; | 
 | 36 |  | 
 | 37 | #define MSM_DMOV_DPRINTF(mask, format, args...) \ | 
 | 38 | 	do { \ | 
 | 39 | 		if ((mask) & msm_dmov_print_mask) \ | 
 | 40 | 			printk(KERN_ERR format, args); \ | 
 | 41 | 	} while (0) | 
 | 42 | #define PRINT_ERROR(format, args...) \ | 
 | 43 | 	MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_ERRORS, format, args); | 
 | 44 | #define PRINT_IO(format, args...) \ | 
 | 45 | 	MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_IO, format, args); | 
 | 46 | #define PRINT_FLOW(format, args...) \ | 
 | 47 | 	MSM_DMOV_DPRINTF(MSM_DMOV_PRINT_FLOW, format, args); | 
 | 48 |  | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 49 | void msm_dmov_stop_cmd(unsigned id, struct msm_dmov_cmd *cmd, int graceful) | 
 | 50 | { | 
 | 51 | 	writel((graceful << 31), DMOV_FLUSH0(id)); | 
 | 52 | } | 
 | 53 |  | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 54 | void msm_dmov_enqueue_cmd(unsigned id, struct msm_dmov_cmd *cmd) | 
 | 55 | { | 
 | 56 | 	unsigned long irq_flags; | 
 | 57 | 	unsigned int status; | 
 | 58 |  | 
 | 59 | 	spin_lock_irqsave(&msm_dmov_lock, irq_flags); | 
| Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 60 | 	if (!channel_active) | 
 | 61 | 		clk_enable(msm_dmov_clk); | 
| Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 62 | 	dsb(); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 63 | 	status = readl(DMOV_STATUS(id)); | 
 | 64 | 	if (list_empty(&ready_commands[id]) && | 
 | 65 | 		(status & DMOV_STATUS_CMD_PTR_RDY)) { | 
 | 66 | #if 0 | 
 | 67 | 		if (list_empty(&active_commands[id])) { | 
 | 68 | 			PRINT_FLOW("msm_dmov_enqueue_cmd(%d), enable interrupt\n", id); | 
 | 69 | 			writel(DMOV_CONFIG_IRQ_EN, DMOV_CONFIG(id)); | 
 | 70 | 		} | 
 | 71 | #endif | 
| San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame] | 72 | 		if (cmd->execute_func) | 
 | 73 | 			cmd->execute_func(cmd); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 74 | 		PRINT_IO("msm_dmov_enqueue_cmd(%d), start command, status %x\n", id, status); | 
 | 75 | 		list_add_tail(&cmd->list, &active_commands[id]); | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 76 | 		if (!channel_active) | 
 | 77 | 			enable_irq(INT_ADM_AARM); | 
 | 78 | 		channel_active |= 1U << id; | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 79 | 		writel(cmd->cmdptr, DMOV_CMD_PTR(id)); | 
 | 80 | 	} else { | 
| Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 81 | 		if (!channel_active) | 
 | 82 | 			clk_disable(msm_dmov_clk); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 83 | 		if (list_empty(&active_commands[id])) | 
 | 84 | 			PRINT_ERROR("msm_dmov_enqueue_cmd(%d), error datamover stalled, status %x\n", id, status); | 
 | 85 |  | 
 | 86 | 		PRINT_IO("msm_dmov_enqueue_cmd(%d), enqueue command, status %x\n", id, status); | 
 | 87 | 		list_add_tail(&cmd->list, &ready_commands[id]); | 
 | 88 | 	} | 
 | 89 | 	spin_unlock_irqrestore(&msm_dmov_lock, irq_flags); | 
 | 90 | } | 
 | 91 |  | 
 | 92 | struct msm_dmov_exec_cmdptr_cmd { | 
 | 93 | 	struct msm_dmov_cmd dmov_cmd; | 
 | 94 | 	struct completion complete; | 
 | 95 | 	unsigned id; | 
 | 96 | 	unsigned int result; | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 97 | 	struct msm_dmov_errdata err; | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 98 | }; | 
 | 99 |  | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 100 | static void | 
 | 101 | dmov_exec_cmdptr_complete_func(struct msm_dmov_cmd *_cmd, | 
 | 102 | 			       unsigned int result, | 
 | 103 | 			       struct msm_dmov_errdata *err) | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 104 | { | 
 | 105 | 	struct msm_dmov_exec_cmdptr_cmd *cmd = container_of(_cmd, struct msm_dmov_exec_cmdptr_cmd, dmov_cmd); | 
 | 106 | 	cmd->result = result; | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 107 | 	if (result != 0x80000002 && err) | 
 | 108 | 		memcpy(&cmd->err, err, sizeof(struct msm_dmov_errdata)); | 
 | 109 |  | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 110 | 	complete(&cmd->complete); | 
 | 111 | } | 
 | 112 |  | 
 | 113 | int msm_dmov_exec_cmd(unsigned id, unsigned int cmdptr) | 
 | 114 | { | 
 | 115 | 	struct msm_dmov_exec_cmdptr_cmd cmd; | 
 | 116 |  | 
 | 117 | 	PRINT_FLOW("dmov_exec_cmdptr(%d, %x)\n", id, cmdptr); | 
 | 118 |  | 
 | 119 | 	cmd.dmov_cmd.cmdptr = cmdptr; | 
 | 120 | 	cmd.dmov_cmd.complete_func = dmov_exec_cmdptr_complete_func; | 
| San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame] | 121 | 	cmd.dmov_cmd.execute_func = NULL; | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 122 | 	cmd.id = id; | 
 | 123 | 	init_completion(&cmd.complete); | 
 | 124 |  | 
 | 125 | 	msm_dmov_enqueue_cmd(id, &cmd.dmov_cmd); | 
 | 126 | 	wait_for_completion(&cmd.complete); | 
 | 127 |  | 
 | 128 | 	if (cmd.result != 0x80000002) { | 
 | 129 | 		PRINT_ERROR("dmov_exec_cmdptr(%d): ERROR, result: %x\n", id, cmd.result); | 
 | 130 | 		PRINT_ERROR("dmov_exec_cmdptr(%d):  flush: %x %x %x %x\n", | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 131 | 			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] | 132 | 		return -EIO; | 
 | 133 | 	} | 
 | 134 | 	PRINT_FLOW("dmov_exec_cmdptr(%d, %x) done\n", id, cmdptr); | 
 | 135 | 	return 0; | 
 | 136 | } | 
 | 137 |  | 
 | 138 |  | 
 | 139 | static irqreturn_t msm_datamover_irq_handler(int irq, void *dev_id) | 
 | 140 | { | 
 | 141 | 	unsigned int int_status, mask, id; | 
 | 142 | 	unsigned long irq_flags; | 
 | 143 | 	unsigned int ch_status; | 
 | 144 | 	unsigned int ch_result; | 
 | 145 | 	struct msm_dmov_cmd *cmd; | 
 | 146 |  | 
 | 147 | 	spin_lock_irqsave(&msm_dmov_lock, irq_flags); | 
 | 148 |  | 
 | 149 | 	int_status = readl(DMOV_ISR); /* read and clear interrupt */ | 
 | 150 | 	PRINT_FLOW("msm_datamover_irq_handler: DMOV_ISR %x\n", int_status); | 
 | 151 |  | 
 | 152 | 	while (int_status) { | 
 | 153 | 		mask = int_status & -int_status; | 
 | 154 | 		id = fls(mask) - 1; | 
 | 155 | 		PRINT_FLOW("msm_datamover_irq_handler %08x %08x id %d\n", int_status, mask, id); | 
 | 156 | 		int_status &= ~mask; | 
 | 157 | 		ch_status = readl(DMOV_STATUS(id)); | 
 | 158 | 		if (!(ch_status & DMOV_STATUS_RSLT_VALID)) { | 
 | 159 | 			PRINT_FLOW("msm_datamover_irq_handler id %d, result not valid %x\n", id, ch_status); | 
 | 160 | 			continue; | 
 | 161 | 		} | 
 | 162 | 		do { | 
 | 163 | 			ch_result = readl(DMOV_RSLT(id)); | 
 | 164 | 			if (list_empty(&active_commands[id])) { | 
 | 165 | 				PRINT_ERROR("msm_datamover_irq_handler id %d, got result " | 
 | 166 | 					"with no active command, status %x, result %x\n", | 
 | 167 | 					id, ch_status, ch_result); | 
 | 168 | 				cmd = NULL; | 
 | 169 | 			} else | 
 | 170 | 				cmd = list_entry(active_commands[id].next, typeof(*cmd), list); | 
 | 171 | 			PRINT_FLOW("msm_datamover_irq_handler id %d, status %x, result %x\n", id, ch_status, ch_result); | 
 | 172 | 			if (ch_result & DMOV_RSLT_DONE) { | 
 | 173 | 				PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", | 
 | 174 | 					id, ch_status); | 
 | 175 | 				PRINT_IO("msm_datamover_irq_handler id %d, got result " | 
 | 176 | 					"for %p, result %x\n", id, cmd, ch_result); | 
 | 177 | 				if (cmd) { | 
 | 178 | 					list_del(&cmd->list); | 
| Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 179 | 					dsb(); | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 180 | 					cmd->complete_func(cmd, ch_result, NULL); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 181 | 				} | 
 | 182 | 			} | 
 | 183 | 			if (ch_result & DMOV_RSLT_FLUSH) { | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 184 | 				struct msm_dmov_errdata errdata; | 
 | 185 |  | 
 | 186 | 				errdata.flush[0] = readl(DMOV_FLUSH0(id)); | 
 | 187 | 				errdata.flush[1] = readl(DMOV_FLUSH1(id)); | 
 | 188 | 				errdata.flush[2] = readl(DMOV_FLUSH2(id)); | 
 | 189 | 				errdata.flush[3] = readl(DMOV_FLUSH3(id)); | 
 | 190 | 				errdata.flush[4] = readl(DMOV_FLUSH4(id)); | 
 | 191 | 				errdata.flush[5] = readl(DMOV_FLUSH5(id)); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 192 | 				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] | 193 | 				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] | 194 | 				if (cmd) { | 
 | 195 | 					list_del(&cmd->list); | 
| Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 196 | 					dsb(); | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 197 | 					cmd->complete_func(cmd, ch_result, &errdata); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 198 | 				} | 
 | 199 | 			} | 
 | 200 | 			if (ch_result & DMOV_RSLT_ERROR) { | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 201 | 				struct msm_dmov_errdata errdata; | 
 | 202 |  | 
 | 203 | 				errdata.flush[0] = readl(DMOV_FLUSH0(id)); | 
 | 204 | 				errdata.flush[1] = readl(DMOV_FLUSH1(id)); | 
 | 205 | 				errdata.flush[2] = readl(DMOV_FLUSH2(id)); | 
 | 206 | 				errdata.flush[3] = readl(DMOV_FLUSH3(id)); | 
 | 207 | 				errdata.flush[4] = readl(DMOV_FLUSH4(id)); | 
 | 208 | 				errdata.flush[5] = readl(DMOV_FLUSH5(id)); | 
 | 209 |  | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 210 | 				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] | 211 | 				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] | 212 | 				if (cmd) { | 
 | 213 | 					list_del(&cmd->list); | 
| Brian Swetland | 9f68fcd | 2009-08-19 18:49:45 -0700 | [diff] [blame] | 214 | 					dsb(); | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 215 | 					cmd->complete_func(cmd, ch_result, &errdata); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 216 | 				} | 
 | 217 | 				/* this does not seem to work, once we get an error */ | 
 | 218 | 				/* the datamover will no longer accept commands */ | 
 | 219 | 				writel(0, DMOV_FLUSH0(id)); | 
 | 220 | 			} | 
 | 221 | 			ch_status = readl(DMOV_STATUS(id)); | 
 | 222 | 			PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); | 
 | 223 | 			if ((ch_status & DMOV_STATUS_CMD_PTR_RDY) && !list_empty(&ready_commands[id])) { | 
 | 224 | 				cmd = list_entry(ready_commands[id].next, typeof(*cmd), list); | 
 | 225 | 				list_del(&cmd->list); | 
 | 226 | 				list_add_tail(&cmd->list, &active_commands[id]); | 
| San Mehat | 5b00f40 | 2009-11-21 09:22:14 -0800 | [diff] [blame] | 227 | 				if (cmd->execute_func) | 
 | 228 | 					cmd->execute_func(cmd); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 229 | 				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 Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 233 | 		if (list_empty(&active_commands[id]) && list_empty(&ready_commands[id])) | 
 | 234 | 			channel_active &= ~(1U << id); | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 235 | 		PRINT_FLOW("msm_datamover_irq_handler id %d, status %x\n", id, ch_status); | 
 | 236 | 	} | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 237 |  | 
| Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 238 | 	if (!channel_active) { | 
| Arve Hjønnevåg | a6407dd | 2009-04-17 18:21:34 -0700 | [diff] [blame] | 239 | 		disable_irq_nosync(INT_ADM_AARM); | 
| Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 240 | 		clk_disable(msm_dmov_clk); | 
 | 241 | 	} | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 242 |  | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 243 | 	spin_unlock_irqrestore(&msm_dmov_lock, irq_flags); | 
 | 244 | 	return IRQ_HANDLED; | 
 | 245 | } | 
 | 246 |  | 
 | 247 | static int __init msm_init_datamover(void) | 
 | 248 | { | 
 | 249 | 	int i; | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 250 | 	int ret; | 
| Arve Hjønnevåg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 251 | 	struct clk *clk; | 
 | 252 |  | 
| Arve Hjønnevåg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 253 | 	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åg | c554107 | 2009-06-25 17:03:14 -0700 | [diff] [blame] | 258 | 	clk = clk_get(NULL, "adm_clk"); | 
 | 259 | 	if (IS_ERR(clk)) | 
 | 260 | 		return PTR_ERR(clk); | 
 | 261 | 	msm_dmov_clk = clk; | 
| Brian Swetland | 8a0f6f1 | 2008-09-10 14:58:25 -0700 | [diff] [blame] | 262 | 	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åg | bfe645a | 2007-11-26 04:12:29 -0800 | [diff] [blame] | 267 | } | 
 | 268 |  | 
 | 269 | arch_initcall(msm_init_datamover); | 
 | 270 |  |