Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * drivers/dma/imx-sdma.c |
| 3 | * |
| 4 | * This file contains a driver for the Freescale Smart DMA engine |
| 5 | * |
| 6 | * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 7 | * |
| 8 | * Based on code from Freescale: |
| 9 | * |
| 10 | * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved. |
| 11 | * |
| 12 | * The code contained herein is licensed under the GNU General Public |
| 13 | * License. You may obtain a copy of the GNU General Public License |
| 14 | * Version 2 or later at the following locations: |
| 15 | * |
| 16 | * http://www.opensource.org/licenses/gpl-license.html |
| 17 | * http://www.gnu.org/copyleft/gpl.html |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
Axel Lin | f8de8f4 | 2011-08-30 15:08:24 +0800 | [diff] [blame] | 21 | #include <linux/module.h> |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 22 | #include <linux/types.h> |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 24 | #include <linux/mm.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/clk.h> |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 27 | #include <linux/delay.h> |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 28 | #include <linux/sched.h> |
| 29 | #include <linux/semaphore.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/device.h> |
| 32 | #include <linux/dma-mapping.h> |
| 33 | #include <linux/firmware.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/platform_device.h> |
| 36 | #include <linux/dmaengine.h> |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 37 | #include <linux/of.h> |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 38 | #include <linux/of_address.h> |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 39 | #include <linux/of_device.h> |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 40 | #include <linux/of_dma.h> |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 41 | |
| 42 | #include <asm/irq.h> |
Arnd Bergmann | 82906b1 | 2012-08-24 15:14:29 +0200 | [diff] [blame] | 43 | #include <linux/platform_data/dma-imx-sdma.h> |
| 44 | #include <linux/platform_data/dma-imx.h> |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 45 | |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 46 | #include "dmaengine.h" |
| 47 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 48 | /* SDMA registers */ |
| 49 | #define SDMA_H_C0PTR 0x000 |
| 50 | #define SDMA_H_INTR 0x004 |
| 51 | #define SDMA_H_STATSTOP 0x008 |
| 52 | #define SDMA_H_START 0x00c |
| 53 | #define SDMA_H_EVTOVR 0x010 |
| 54 | #define SDMA_H_DSPOVR 0x014 |
| 55 | #define SDMA_H_HOSTOVR 0x018 |
| 56 | #define SDMA_H_EVTPEND 0x01c |
| 57 | #define SDMA_H_DSPENBL 0x020 |
| 58 | #define SDMA_H_RESET 0x024 |
| 59 | #define SDMA_H_EVTERR 0x028 |
| 60 | #define SDMA_H_INTRMSK 0x02c |
| 61 | #define SDMA_H_PSW 0x030 |
| 62 | #define SDMA_H_EVTERRDBG 0x034 |
| 63 | #define SDMA_H_CONFIG 0x038 |
| 64 | #define SDMA_ONCE_ENB 0x040 |
| 65 | #define SDMA_ONCE_DATA 0x044 |
| 66 | #define SDMA_ONCE_INSTR 0x048 |
| 67 | #define SDMA_ONCE_STAT 0x04c |
| 68 | #define SDMA_ONCE_CMD 0x050 |
| 69 | #define SDMA_EVT_MIRROR 0x054 |
| 70 | #define SDMA_ILLINSTADDR 0x058 |
| 71 | #define SDMA_CHN0ADDR 0x05c |
| 72 | #define SDMA_ONCE_RTB 0x060 |
| 73 | #define SDMA_XTRIG_CONF1 0x070 |
| 74 | #define SDMA_XTRIG_CONF2 0x074 |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 75 | #define SDMA_CHNENBL0_IMX35 0x200 |
| 76 | #define SDMA_CHNENBL0_IMX31 0x080 |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 77 | #define SDMA_CHNPRI_0 0x100 |
| 78 | |
| 79 | /* |
| 80 | * Buffer descriptor status values. |
| 81 | */ |
| 82 | #define BD_DONE 0x01 |
| 83 | #define BD_WRAP 0x02 |
| 84 | #define BD_CONT 0x04 |
| 85 | #define BD_INTR 0x08 |
| 86 | #define BD_RROR 0x10 |
| 87 | #define BD_LAST 0x20 |
| 88 | #define BD_EXTD 0x80 |
| 89 | |
| 90 | /* |
| 91 | * Data Node descriptor status values. |
| 92 | */ |
| 93 | #define DND_END_OF_FRAME 0x80 |
| 94 | #define DND_END_OF_XFER 0x40 |
| 95 | #define DND_DONE 0x20 |
| 96 | #define DND_UNUSED 0x01 |
| 97 | |
| 98 | /* |
| 99 | * IPCV2 descriptor status values. |
| 100 | */ |
| 101 | #define BD_IPCV2_END_OF_FRAME 0x40 |
| 102 | |
| 103 | #define IPCV2_MAX_NODES 50 |
| 104 | /* |
| 105 | * Error bit set in the CCB status field by the SDMA, |
| 106 | * in setbd routine, in case of a transfer error |
| 107 | */ |
| 108 | #define DATA_ERROR 0x10000000 |
| 109 | |
| 110 | /* |
| 111 | * Buffer descriptor commands. |
| 112 | */ |
| 113 | #define C0_ADDR 0x01 |
| 114 | #define C0_LOAD 0x02 |
| 115 | #define C0_DUMP 0x03 |
| 116 | #define C0_SETCTX 0x07 |
| 117 | #define C0_GETCTX 0x03 |
| 118 | #define C0_SETDM 0x01 |
| 119 | #define C0_SETPM 0x04 |
| 120 | #define C0_GETDM 0x02 |
| 121 | #define C0_GETPM 0x08 |
| 122 | /* |
| 123 | * Change endianness indicator in the BD command field |
| 124 | */ |
| 125 | #define CHANGE_ENDIANNESS 0x80 |
| 126 | |
| 127 | /* |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 128 | * p_2_p watermark_level description |
| 129 | * Bits Name Description |
| 130 | * 0-7 Lower WML Lower watermark level |
| 131 | * 8 PS 1: Pad Swallowing |
| 132 | * 0: No Pad Swallowing |
| 133 | * 9 PA 1: Pad Adding |
| 134 | * 0: No Pad Adding |
| 135 | * 10 SPDIF If this bit is set both source |
| 136 | * and destination are on SPBA |
| 137 | * 11 Source Bit(SP) 1: Source on SPBA |
| 138 | * 0: Source on AIPS |
| 139 | * 12 Destination Bit(DP) 1: Destination on SPBA |
| 140 | * 0: Destination on AIPS |
| 141 | * 13-15 --------- MUST BE 0 |
| 142 | * 16-23 Higher WML HWML |
| 143 | * 24-27 N Total number of samples after |
| 144 | * which Pad adding/Swallowing |
| 145 | * must be done. It must be odd. |
| 146 | * 28 Lower WML Event(LWE) SDMA events reg to check for |
| 147 | * LWML event mask |
| 148 | * 0: LWE in EVENTS register |
| 149 | * 1: LWE in EVENTS2 register |
| 150 | * 29 Higher WML Event(HWE) SDMA events reg to check for |
| 151 | * HWML event mask |
| 152 | * 0: HWE in EVENTS register |
| 153 | * 1: HWE in EVENTS2 register |
| 154 | * 30 --------- MUST BE 0 |
| 155 | * 31 CONT 1: Amount of samples to be |
| 156 | * transferred is unknown and |
| 157 | * script will keep on |
| 158 | * transferring samples as long as |
| 159 | * both events are detected and |
| 160 | * script must be manually stopped |
| 161 | * by the application |
| 162 | * 0: The amount of samples to be |
| 163 | * transferred is equal to the |
| 164 | * count field of mode word |
| 165 | */ |
| 166 | #define SDMA_WATERMARK_LEVEL_LWML 0xFF |
| 167 | #define SDMA_WATERMARK_LEVEL_PS BIT(8) |
| 168 | #define SDMA_WATERMARK_LEVEL_PA BIT(9) |
| 169 | #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10) |
| 170 | #define SDMA_WATERMARK_LEVEL_SP BIT(11) |
| 171 | #define SDMA_WATERMARK_LEVEL_DP BIT(12) |
| 172 | #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16) |
| 173 | #define SDMA_WATERMARK_LEVEL_LWE BIT(28) |
| 174 | #define SDMA_WATERMARK_LEVEL_HWE BIT(29) |
| 175 | #define SDMA_WATERMARK_LEVEL_CONT BIT(31) |
| 176 | |
| 177 | /* |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 178 | * Mode/Count of data node descriptors - IPCv2 |
| 179 | */ |
| 180 | struct sdma_mode_count { |
| 181 | u32 count : 16; /* size of the buffer pointed by this BD */ |
| 182 | u32 status : 8; /* E,R,I,C,W,D status bits stored here */ |
| 183 | u32 command : 8; /* command mostlky used for channel 0 */ |
| 184 | }; |
| 185 | |
| 186 | /* |
| 187 | * Buffer descriptor |
| 188 | */ |
| 189 | struct sdma_buffer_descriptor { |
| 190 | struct sdma_mode_count mode; |
| 191 | u32 buffer_addr; /* address of the buffer described */ |
| 192 | u32 ext_buffer_addr; /* extended buffer address */ |
| 193 | } __attribute__ ((packed)); |
| 194 | |
| 195 | /** |
| 196 | * struct sdma_channel_control - Channel control Block |
| 197 | * |
| 198 | * @current_bd_ptr current buffer descriptor processed |
| 199 | * @base_bd_ptr first element of buffer descriptor array |
| 200 | * @unused padding. The SDMA engine expects an array of 128 byte |
| 201 | * control blocks |
| 202 | */ |
| 203 | struct sdma_channel_control { |
| 204 | u32 current_bd_ptr; |
| 205 | u32 base_bd_ptr; |
| 206 | u32 unused[2]; |
| 207 | } __attribute__ ((packed)); |
| 208 | |
| 209 | /** |
| 210 | * struct sdma_state_registers - SDMA context for a channel |
| 211 | * |
| 212 | * @pc: program counter |
| 213 | * @t: test bit: status of arithmetic & test instruction |
| 214 | * @rpc: return program counter |
| 215 | * @sf: source fault while loading data |
| 216 | * @spc: loop start program counter |
| 217 | * @df: destination fault while storing data |
| 218 | * @epc: loop end program counter |
| 219 | * @lm: loop mode |
| 220 | */ |
| 221 | struct sdma_state_registers { |
| 222 | u32 pc :14; |
| 223 | u32 unused1: 1; |
| 224 | u32 t : 1; |
| 225 | u32 rpc :14; |
| 226 | u32 unused0: 1; |
| 227 | u32 sf : 1; |
| 228 | u32 spc :14; |
| 229 | u32 unused2: 1; |
| 230 | u32 df : 1; |
| 231 | u32 epc :14; |
| 232 | u32 lm : 2; |
| 233 | } __attribute__ ((packed)); |
| 234 | |
| 235 | /** |
| 236 | * struct sdma_context_data - sdma context specific to a channel |
| 237 | * |
| 238 | * @channel_state: channel state bits |
| 239 | * @gReg: general registers |
| 240 | * @mda: burst dma destination address register |
| 241 | * @msa: burst dma source address register |
| 242 | * @ms: burst dma status register |
| 243 | * @md: burst dma data register |
| 244 | * @pda: peripheral dma destination address register |
| 245 | * @psa: peripheral dma source address register |
| 246 | * @ps: peripheral dma status register |
| 247 | * @pd: peripheral dma data register |
| 248 | * @ca: CRC polynomial register |
| 249 | * @cs: CRC accumulator register |
| 250 | * @dda: dedicated core destination address register |
| 251 | * @dsa: dedicated core source address register |
| 252 | * @ds: dedicated core status register |
| 253 | * @dd: dedicated core data register |
| 254 | */ |
| 255 | struct sdma_context_data { |
| 256 | struct sdma_state_registers channel_state; |
| 257 | u32 gReg[8]; |
| 258 | u32 mda; |
| 259 | u32 msa; |
| 260 | u32 ms; |
| 261 | u32 md; |
| 262 | u32 pda; |
| 263 | u32 psa; |
| 264 | u32 ps; |
| 265 | u32 pd; |
| 266 | u32 ca; |
| 267 | u32 cs; |
| 268 | u32 dda; |
| 269 | u32 dsa; |
| 270 | u32 ds; |
| 271 | u32 dd; |
| 272 | u32 scratch0; |
| 273 | u32 scratch1; |
| 274 | u32 scratch2; |
| 275 | u32 scratch3; |
| 276 | u32 scratch4; |
| 277 | u32 scratch5; |
| 278 | u32 scratch6; |
| 279 | u32 scratch7; |
| 280 | } __attribute__ ((packed)); |
| 281 | |
| 282 | #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor)) |
| 283 | |
| 284 | struct sdma_engine; |
| 285 | |
| 286 | /** |
| 287 | * struct sdma_channel - housekeeping for a SDMA channel |
| 288 | * |
| 289 | * @sdma pointer to the SDMA engine for this channel |
Sascha Hauer | 23889c6 | 2011-01-31 10:56:58 +0100 | [diff] [blame] | 290 | * @channel the channel number, matches dmaengine chan_id + 1 |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 291 | * @direction transfer type. Needed for setting SDMA script |
| 292 | * @peripheral_type Peripheral type. Needed for setting SDMA script |
| 293 | * @event_id0 aka dma request line |
| 294 | * @event_id1 for channels that use 2 events |
| 295 | * @word_size peripheral access size |
| 296 | * @buf_tail ID of the buffer that was processed |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 297 | * @num_bd max NUM_BD. number of descriptors currently handling |
| 298 | */ |
| 299 | struct sdma_channel { |
| 300 | struct sdma_engine *sdma; |
| 301 | unsigned int channel; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 302 | enum dma_transfer_direction direction; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 303 | enum sdma_peripheral_type peripheral_type; |
| 304 | unsigned int event_id0; |
| 305 | unsigned int event_id1; |
| 306 | enum dma_slave_buswidth word_size; |
| 307 | unsigned int buf_tail; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 308 | unsigned int num_bd; |
Russell King - ARM Linux | d1a792f | 2014-06-25 13:00:33 +0100 | [diff] [blame] | 309 | unsigned int period_len; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 310 | struct sdma_buffer_descriptor *bd; |
| 311 | dma_addr_t bd_phys; |
| 312 | unsigned int pc_from_device, pc_to_device; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 313 | unsigned int device_to_device; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 314 | unsigned long flags; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 315 | dma_addr_t per_address, per_address2; |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 316 | unsigned long event_mask[2]; |
| 317 | unsigned long watermark_level; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 318 | u32 shp_addr, per_addr; |
| 319 | struct dma_chan chan; |
| 320 | spinlock_t lock; |
| 321 | struct dma_async_tx_descriptor desc; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 322 | enum dma_status status; |
Huang Shijie | ab59a51 | 2011-12-02 10:16:25 +0800 | [diff] [blame] | 323 | unsigned int chn_count; |
| 324 | unsigned int chn_real_count; |
Huang Shijie | abd9ccc | 2012-04-28 18:15:42 +0800 | [diff] [blame] | 325 | struct tasklet_struct tasklet; |
Nicolin Chen | 0b35186 | 2014-06-16 11:32:29 +0800 | [diff] [blame] | 326 | struct imx_dma_data data; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 329 | #define IMX_DMA_SG_LOOP BIT(0) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 330 | |
| 331 | #define MAX_DMA_CHANNELS 32 |
| 332 | #define MXC_SDMA_DEFAULT_PRIORITY 1 |
| 333 | #define MXC_SDMA_MIN_PRIORITY 1 |
| 334 | #define MXC_SDMA_MAX_PRIORITY 7 |
| 335 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 336 | #define SDMA_FIRMWARE_MAGIC 0x414d4453 |
| 337 | |
| 338 | /** |
| 339 | * struct sdma_firmware_header - Layout of the firmware image |
| 340 | * |
| 341 | * @magic "SDMA" |
| 342 | * @version_major increased whenever layout of struct sdma_script_start_addrs |
| 343 | * changes. |
| 344 | * @version_minor firmware minor version (for binary compatible changes) |
| 345 | * @script_addrs_start offset of struct sdma_script_start_addrs in this image |
| 346 | * @num_script_addrs Number of script addresses in this image |
| 347 | * @ram_code_start offset of SDMA ram image in this firmware image |
| 348 | * @ram_code_size size of SDMA ram image |
| 349 | * @script_addrs Stores the start address of the SDMA scripts |
| 350 | * (in SDMA memory space) |
| 351 | */ |
| 352 | struct sdma_firmware_header { |
| 353 | u32 magic; |
| 354 | u32 version_major; |
| 355 | u32 version_minor; |
| 356 | u32 script_addrs_start; |
| 357 | u32 num_script_addrs; |
| 358 | u32 ram_code_start; |
| 359 | u32 ram_code_size; |
| 360 | }; |
| 361 | |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 362 | struct sdma_driver_data { |
| 363 | int chnenbl0; |
| 364 | int num_events; |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 365 | struct sdma_script_start_addrs *script_addrs; |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 366 | }; |
| 367 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 368 | struct sdma_engine { |
| 369 | struct device *dev; |
Sascha Hauer | b9b3f82 | 2011-01-12 12:12:31 +0100 | [diff] [blame] | 370 | struct device_dma_parameters dma_parms; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 371 | struct sdma_channel channel[MAX_DMA_CHANNELS]; |
| 372 | struct sdma_channel_control *channel_control; |
| 373 | void __iomem *regs; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 374 | struct sdma_context_data *context; |
| 375 | dma_addr_t context_phys; |
| 376 | struct dma_device dma_device; |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 377 | struct clk *clk_ipg; |
| 378 | struct clk *clk_ahb; |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 379 | spinlock_t channel_0_lock; |
Nicolin Chen | cd72b84 | 2013-11-13 22:55:24 +0800 | [diff] [blame] | 380 | u32 script_number; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 381 | struct sdma_script_start_addrs *script_addrs; |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 382 | const struct sdma_driver_data *drvdata; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 383 | u32 spba_start_addr; |
| 384 | u32 spba_end_addr; |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 385 | }; |
| 386 | |
Fabio Estevam | e9fd58d | 2013-09-01 21:57:12 -0300 | [diff] [blame] | 387 | static struct sdma_driver_data sdma_imx31 = { |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 388 | .chnenbl0 = SDMA_CHNENBL0_IMX31, |
| 389 | .num_events = 32, |
| 390 | }; |
| 391 | |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 392 | static struct sdma_script_start_addrs sdma_script_imx25 = { |
| 393 | .ap_2_ap_addr = 729, |
| 394 | .uart_2_mcu_addr = 904, |
| 395 | .per_2_app_addr = 1255, |
| 396 | .mcu_2_app_addr = 834, |
| 397 | .uartsh_2_mcu_addr = 1120, |
| 398 | .per_2_shp_addr = 1329, |
| 399 | .mcu_2_shp_addr = 1048, |
| 400 | .ata_2_mcu_addr = 1560, |
| 401 | .mcu_2_ata_addr = 1479, |
| 402 | .app_2_per_addr = 1189, |
| 403 | .app_2_mcu_addr = 770, |
| 404 | .shp_2_per_addr = 1407, |
| 405 | .shp_2_mcu_addr = 979, |
| 406 | }; |
| 407 | |
Fabio Estevam | e9fd58d | 2013-09-01 21:57:12 -0300 | [diff] [blame] | 408 | static struct sdma_driver_data sdma_imx25 = { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 409 | .chnenbl0 = SDMA_CHNENBL0_IMX35, |
| 410 | .num_events = 48, |
| 411 | .script_addrs = &sdma_script_imx25, |
| 412 | }; |
| 413 | |
Fabio Estevam | e9fd58d | 2013-09-01 21:57:12 -0300 | [diff] [blame] | 414 | static struct sdma_driver_data sdma_imx35 = { |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 415 | .chnenbl0 = SDMA_CHNENBL0_IMX35, |
| 416 | .num_events = 48, |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 419 | static struct sdma_script_start_addrs sdma_script_imx51 = { |
| 420 | .ap_2_ap_addr = 642, |
| 421 | .uart_2_mcu_addr = 817, |
| 422 | .mcu_2_app_addr = 747, |
| 423 | .mcu_2_shp_addr = 961, |
| 424 | .ata_2_mcu_addr = 1473, |
| 425 | .mcu_2_ata_addr = 1392, |
| 426 | .app_2_per_addr = 1033, |
| 427 | .app_2_mcu_addr = 683, |
| 428 | .shp_2_per_addr = 1251, |
| 429 | .shp_2_mcu_addr = 892, |
| 430 | }; |
| 431 | |
Fabio Estevam | e9fd58d | 2013-09-01 21:57:12 -0300 | [diff] [blame] | 432 | static struct sdma_driver_data sdma_imx51 = { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 433 | .chnenbl0 = SDMA_CHNENBL0_IMX35, |
| 434 | .num_events = 48, |
| 435 | .script_addrs = &sdma_script_imx51, |
| 436 | }; |
| 437 | |
| 438 | static struct sdma_script_start_addrs sdma_script_imx53 = { |
| 439 | .ap_2_ap_addr = 642, |
| 440 | .app_2_mcu_addr = 683, |
| 441 | .mcu_2_app_addr = 747, |
| 442 | .uart_2_mcu_addr = 817, |
| 443 | .shp_2_mcu_addr = 891, |
| 444 | .mcu_2_shp_addr = 960, |
| 445 | .uartsh_2_mcu_addr = 1032, |
| 446 | .spdif_2_mcu_addr = 1100, |
| 447 | .mcu_2_spdif_addr = 1134, |
| 448 | .firi_2_mcu_addr = 1193, |
| 449 | .mcu_2_firi_addr = 1290, |
| 450 | }; |
| 451 | |
Fabio Estevam | e9fd58d | 2013-09-01 21:57:12 -0300 | [diff] [blame] | 452 | static struct sdma_driver_data sdma_imx53 = { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 453 | .chnenbl0 = SDMA_CHNENBL0_IMX35, |
| 454 | .num_events = 48, |
| 455 | .script_addrs = &sdma_script_imx53, |
| 456 | }; |
| 457 | |
| 458 | static struct sdma_script_start_addrs sdma_script_imx6q = { |
| 459 | .ap_2_ap_addr = 642, |
| 460 | .uart_2_mcu_addr = 817, |
| 461 | .mcu_2_app_addr = 747, |
| 462 | .per_2_per_addr = 6331, |
| 463 | .uartsh_2_mcu_addr = 1032, |
| 464 | .mcu_2_shp_addr = 960, |
| 465 | .app_2_mcu_addr = 683, |
| 466 | .shp_2_mcu_addr = 891, |
| 467 | .spdif_2_mcu_addr = 1100, |
| 468 | .mcu_2_spdif_addr = 1134, |
| 469 | }; |
| 470 | |
Fabio Estevam | e9fd58d | 2013-09-01 21:57:12 -0300 | [diff] [blame] | 471 | static struct sdma_driver_data sdma_imx6q = { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 472 | .chnenbl0 = SDMA_CHNENBL0_IMX35, |
| 473 | .num_events = 48, |
| 474 | .script_addrs = &sdma_script_imx6q, |
| 475 | }; |
| 476 | |
Krzysztof Kozlowski | afe7cde | 2015-05-02 00:57:46 +0900 | [diff] [blame] | 477 | static const struct platform_device_id sdma_devtypes[] = { |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 478 | { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 479 | .name = "imx25-sdma", |
| 480 | .driver_data = (unsigned long)&sdma_imx25, |
| 481 | }, { |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 482 | .name = "imx31-sdma", |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 483 | .driver_data = (unsigned long)&sdma_imx31, |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 484 | }, { |
| 485 | .name = "imx35-sdma", |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 486 | .driver_data = (unsigned long)&sdma_imx35, |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 487 | }, { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 488 | .name = "imx51-sdma", |
| 489 | .driver_data = (unsigned long)&sdma_imx51, |
| 490 | }, { |
| 491 | .name = "imx53-sdma", |
| 492 | .driver_data = (unsigned long)&sdma_imx53, |
| 493 | }, { |
| 494 | .name = "imx6q-sdma", |
| 495 | .driver_data = (unsigned long)&sdma_imx6q, |
| 496 | }, { |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 497 | /* sentinel */ |
| 498 | } |
| 499 | }; |
| 500 | MODULE_DEVICE_TABLE(platform, sdma_devtypes); |
| 501 | |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 502 | static const struct of_device_id sdma_dt_ids[] = { |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 503 | { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, }, |
| 504 | { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, }, |
| 505 | { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, }, |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 506 | { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, }, |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 507 | { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, }, |
Markus Pargmann | 63edea1 | 2014-02-16 20:10:55 +0100 | [diff] [blame] | 508 | { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, }, |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 509 | { /* sentinel */ } |
| 510 | }; |
| 511 | MODULE_DEVICE_TABLE(of, sdma_dt_ids); |
| 512 | |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 513 | #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */ |
| 514 | #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */ |
| 515 | #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */ |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 516 | #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ |
| 517 | |
| 518 | static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) |
| 519 | { |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 520 | u32 chnenbl0 = sdma->drvdata->chnenbl0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 521 | return chnenbl0 + event * 4; |
| 522 | } |
| 523 | |
| 524 | static int sdma_config_ownership(struct sdma_channel *sdmac, |
| 525 | bool event_override, bool mcu_override, bool dsp_override) |
| 526 | { |
| 527 | struct sdma_engine *sdma = sdmac->sdma; |
| 528 | int channel = sdmac->channel; |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 529 | unsigned long evt, mcu, dsp; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 530 | |
| 531 | if (event_override && mcu_override && dsp_override) |
| 532 | return -EINVAL; |
| 533 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 534 | evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); |
| 535 | mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); |
| 536 | dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 537 | |
| 538 | if (dsp_override) |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 539 | __clear_bit(channel, &dsp); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 540 | else |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 541 | __set_bit(channel, &dsp); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 542 | |
| 543 | if (event_override) |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 544 | __clear_bit(channel, &evt); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 545 | else |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 546 | __set_bit(channel, &evt); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 547 | |
| 548 | if (mcu_override) |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 549 | __clear_bit(channel, &mcu); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 550 | else |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 551 | __set_bit(channel, &mcu); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 552 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 553 | writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); |
| 554 | writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); |
| 555 | writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
Richard Zhao | b9a59166 | 2012-01-13 11:09:56 +0800 | [diff] [blame] | 560 | static void sdma_enable_channel(struct sdma_engine *sdma, int channel) |
| 561 | { |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 562 | writel(BIT(channel), sdma->regs + SDMA_H_START); |
Richard Zhao | b9a59166 | 2012-01-13 11:09:56 +0800 | [diff] [blame] | 563 | } |
| 564 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 565 | /* |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 566 | * sdma_run_channel0 - run a channel and wait till it's done |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 567 | */ |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 568 | static int sdma_run_channel0(struct sdma_engine *sdma) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 569 | { |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 570 | int ret; |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 571 | unsigned long timeout = 500; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 572 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 573 | sdma_enable_channel(sdma, 0); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 574 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 575 | while (!(ret = readl_relaxed(sdma->regs + SDMA_H_INTR) & 1)) { |
| 576 | if (timeout-- <= 0) |
| 577 | break; |
| 578 | udelay(1); |
| 579 | } |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 580 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 581 | if (ret) { |
| 582 | /* Clear the interrupt status */ |
| 583 | writel_relaxed(ret, sdma->regs + SDMA_H_INTR); |
| 584 | } else { |
| 585 | dev_err(sdma->dev, "Timeout waiting for CH0 ready\n"); |
| 586 | } |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 587 | |
Robin Gong | 855832e | 2015-02-15 10:00:35 +0800 | [diff] [blame] | 588 | /* Set bits of CONFIG register with dynamic context switching */ |
| 589 | if (readl(sdma->regs + SDMA_H_CONFIG) == 0) |
| 590 | writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG); |
| 591 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 592 | return ret ? 0 : -ETIMEDOUT; |
| 593 | } |
| 594 | |
| 595 | static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, |
| 596 | u32 address) |
| 597 | { |
| 598 | struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; |
| 599 | void *buf_virt; |
| 600 | dma_addr_t buf_phys; |
| 601 | int ret; |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 602 | unsigned long flags; |
Sascha Hauer | 73eab97 | 2011-08-25 11:03:35 +0200 | [diff] [blame] | 603 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 604 | buf_virt = dma_alloc_coherent(NULL, |
| 605 | size, |
| 606 | &buf_phys, GFP_KERNEL); |
Sascha Hauer | 73eab97 | 2011-08-25 11:03:35 +0200 | [diff] [blame] | 607 | if (!buf_virt) { |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 608 | return -ENOMEM; |
Sascha Hauer | 73eab97 | 2011-08-25 11:03:35 +0200 | [diff] [blame] | 609 | } |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 610 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 611 | spin_lock_irqsave(&sdma->channel_0_lock, flags); |
| 612 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 613 | bd0->mode.command = C0_SETPM; |
| 614 | bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; |
| 615 | bd0->mode.count = size / 2; |
| 616 | bd0->buffer_addr = buf_phys; |
| 617 | bd0->ext_buffer_addr = address; |
| 618 | |
| 619 | memcpy(buf_virt, buf, size); |
| 620 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 621 | ret = sdma_run_channel0(sdma); |
| 622 | |
| 623 | spin_unlock_irqrestore(&sdma->channel_0_lock, flags); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 624 | |
| 625 | dma_free_coherent(NULL, size, buf_virt, buf_phys); |
| 626 | |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) |
| 631 | { |
| 632 | struct sdma_engine *sdma = sdmac->sdma; |
| 633 | int channel = sdmac->channel; |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 634 | unsigned long val; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 635 | u32 chnenbl = chnenbl_ofs(sdma, event); |
| 636 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 637 | val = readl_relaxed(sdma->regs + chnenbl); |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 638 | __set_bit(channel, &val); |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 639 | writel_relaxed(val, sdma->regs + chnenbl); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) |
| 643 | { |
| 644 | struct sdma_engine *sdma = sdmac->sdma; |
| 645 | int channel = sdmac->channel; |
| 646 | u32 chnenbl = chnenbl_ofs(sdma, event); |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 647 | unsigned long val; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 648 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 649 | val = readl_relaxed(sdma->regs + chnenbl); |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 650 | __clear_bit(channel, &val); |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 651 | writel_relaxed(val, sdma->regs + chnenbl); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static void sdma_handle_channel_loop(struct sdma_channel *sdmac) |
| 655 | { |
Russell King - ARM Linux | d1a792f | 2014-06-25 13:00:33 +0100 | [diff] [blame] | 656 | if (sdmac->desc.callback) |
| 657 | sdmac->desc.callback(sdmac->desc.callback_param); |
| 658 | } |
| 659 | |
| 660 | static void sdma_update_channel_loop(struct sdma_channel *sdmac) |
| 661 | { |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 662 | struct sdma_buffer_descriptor *bd; |
| 663 | |
| 664 | /* |
| 665 | * loop mode. Iterate over descriptors, re-setup them and |
| 666 | * call callback function. |
| 667 | */ |
| 668 | while (1) { |
| 669 | bd = &sdmac->bd[sdmac->buf_tail]; |
| 670 | |
| 671 | if (bd->mode.status & BD_DONE) |
| 672 | break; |
| 673 | |
| 674 | if (bd->mode.status & BD_RROR) |
| 675 | sdmac->status = DMA_ERROR; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 676 | |
| 677 | bd->mode.status |= BD_DONE; |
| 678 | sdmac->buf_tail++; |
| 679 | sdmac->buf_tail %= sdmac->num_bd; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac) |
| 684 | { |
| 685 | struct sdma_buffer_descriptor *bd; |
| 686 | int i, error = 0; |
| 687 | |
Huang Shijie | ab59a51 | 2011-12-02 10:16:25 +0800 | [diff] [blame] | 688 | sdmac->chn_real_count = 0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 689 | /* |
| 690 | * non loop mode. Iterate over all descriptors, collect |
| 691 | * errors and call callback function |
| 692 | */ |
| 693 | for (i = 0; i < sdmac->num_bd; i++) { |
| 694 | bd = &sdmac->bd[i]; |
| 695 | |
| 696 | if (bd->mode.status & (BD_DONE | BD_RROR)) |
| 697 | error = -EIO; |
Huang Shijie | ab59a51 | 2011-12-02 10:16:25 +0800 | [diff] [blame] | 698 | sdmac->chn_real_count += bd->mode.count; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | if (error) |
| 702 | sdmac->status = DMA_ERROR; |
| 703 | else |
Vinod Koul | 409bff6 | 2013-10-16 14:07:06 +0530 | [diff] [blame] | 704 | sdmac->status = DMA_COMPLETE; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 705 | |
Russell King - ARM Linux | f7fbce0 | 2012-03-06 22:35:07 +0000 | [diff] [blame] | 706 | dma_cookie_complete(&sdmac->desc); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 707 | if (sdmac->desc.callback) |
| 708 | sdmac->desc.callback(sdmac->desc.callback_param); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Huang Shijie | abd9ccc | 2012-04-28 18:15:42 +0800 | [diff] [blame] | 711 | static void sdma_tasklet(unsigned long data) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 712 | { |
Huang Shijie | abd9ccc | 2012-04-28 18:15:42 +0800 | [diff] [blame] | 713 | struct sdma_channel *sdmac = (struct sdma_channel *) data; |
| 714 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 715 | if (sdmac->flags & IMX_DMA_SG_LOOP) |
| 716 | sdma_handle_channel_loop(sdmac); |
| 717 | else |
| 718 | mxc_sdma_handle_channel_normal(sdmac); |
| 719 | } |
| 720 | |
| 721 | static irqreturn_t sdma_int_handler(int irq, void *dev_id) |
| 722 | { |
| 723 | struct sdma_engine *sdma = dev_id; |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 724 | unsigned long stat; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 725 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 726 | stat = readl_relaxed(sdma->regs + SDMA_H_INTR); |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 727 | /* not interested in channel 0 interrupts */ |
| 728 | stat &= ~1; |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 729 | writel_relaxed(stat, sdma->regs + SDMA_H_INTR); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 730 | |
| 731 | while (stat) { |
| 732 | int channel = fls(stat) - 1; |
| 733 | struct sdma_channel *sdmac = &sdma->channel[channel]; |
| 734 | |
Russell King - ARM Linux | d1a792f | 2014-06-25 13:00:33 +0100 | [diff] [blame] | 735 | if (sdmac->flags & IMX_DMA_SG_LOOP) |
| 736 | sdma_update_channel_loop(sdmac); |
| 737 | |
Huang Shijie | abd9ccc | 2012-04-28 18:15:42 +0800 | [diff] [blame] | 738 | tasklet_schedule(&sdmac->tasklet); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 739 | |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 740 | __clear_bit(channel, &stat); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | return IRQ_HANDLED; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * sets the pc of SDMA script according to the peripheral type |
| 748 | */ |
| 749 | static void sdma_get_pc(struct sdma_channel *sdmac, |
| 750 | enum sdma_peripheral_type peripheral_type) |
| 751 | { |
| 752 | struct sdma_engine *sdma = sdmac->sdma; |
| 753 | int per_2_emi = 0, emi_2_per = 0; |
| 754 | /* |
| 755 | * These are needed once we start to support transfers between |
| 756 | * two peripherals or memory-to-memory transfers |
| 757 | */ |
| 758 | int per_2_per = 0, emi_2_emi = 0; |
| 759 | |
| 760 | sdmac->pc_from_device = 0; |
| 761 | sdmac->pc_to_device = 0; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 762 | sdmac->device_to_device = 0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 763 | |
| 764 | switch (peripheral_type) { |
| 765 | case IMX_DMATYPE_MEMORY: |
| 766 | emi_2_emi = sdma->script_addrs->ap_2_ap_addr; |
| 767 | break; |
| 768 | case IMX_DMATYPE_DSP: |
| 769 | emi_2_per = sdma->script_addrs->bp_2_ap_addr; |
| 770 | per_2_emi = sdma->script_addrs->ap_2_bp_addr; |
| 771 | break; |
| 772 | case IMX_DMATYPE_FIRI: |
| 773 | per_2_emi = sdma->script_addrs->firi_2_mcu_addr; |
| 774 | emi_2_per = sdma->script_addrs->mcu_2_firi_addr; |
| 775 | break; |
| 776 | case IMX_DMATYPE_UART: |
| 777 | per_2_emi = sdma->script_addrs->uart_2_mcu_addr; |
| 778 | emi_2_per = sdma->script_addrs->mcu_2_app_addr; |
| 779 | break; |
| 780 | case IMX_DMATYPE_UART_SP: |
| 781 | per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; |
| 782 | emi_2_per = sdma->script_addrs->mcu_2_shp_addr; |
| 783 | break; |
| 784 | case IMX_DMATYPE_ATA: |
| 785 | per_2_emi = sdma->script_addrs->ata_2_mcu_addr; |
| 786 | emi_2_per = sdma->script_addrs->mcu_2_ata_addr; |
| 787 | break; |
| 788 | case IMX_DMATYPE_CSPI: |
| 789 | case IMX_DMATYPE_EXT: |
| 790 | case IMX_DMATYPE_SSI: |
Nicolin Chen | 29aebfd | 2014-10-24 12:37:41 -0700 | [diff] [blame] | 791 | case IMX_DMATYPE_SAI: |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 792 | per_2_emi = sdma->script_addrs->app_2_mcu_addr; |
| 793 | emi_2_per = sdma->script_addrs->mcu_2_app_addr; |
| 794 | break; |
Nicolin Chen | 1a89557 | 2013-11-13 22:55:25 +0800 | [diff] [blame] | 795 | case IMX_DMATYPE_SSI_DUAL: |
| 796 | per_2_emi = sdma->script_addrs->ssish_2_mcu_addr; |
| 797 | emi_2_per = sdma->script_addrs->mcu_2_ssish_addr; |
| 798 | break; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 799 | case IMX_DMATYPE_SSI_SP: |
| 800 | case IMX_DMATYPE_MMC: |
| 801 | case IMX_DMATYPE_SDHC: |
| 802 | case IMX_DMATYPE_CSPI_SP: |
| 803 | case IMX_DMATYPE_ESAI: |
| 804 | case IMX_DMATYPE_MSHC_SP: |
| 805 | per_2_emi = sdma->script_addrs->shp_2_mcu_addr; |
| 806 | emi_2_per = sdma->script_addrs->mcu_2_shp_addr; |
| 807 | break; |
| 808 | case IMX_DMATYPE_ASRC: |
| 809 | per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; |
| 810 | emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; |
| 811 | per_2_per = sdma->script_addrs->per_2_per_addr; |
| 812 | break; |
Nicolin Chen | f892afb | 2014-06-16 11:31:05 +0800 | [diff] [blame] | 813 | case IMX_DMATYPE_ASRC_SP: |
| 814 | per_2_emi = sdma->script_addrs->shp_2_mcu_addr; |
| 815 | emi_2_per = sdma->script_addrs->mcu_2_shp_addr; |
| 816 | per_2_per = sdma->script_addrs->per_2_per_addr; |
| 817 | break; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 818 | case IMX_DMATYPE_MSHC: |
| 819 | per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; |
| 820 | emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; |
| 821 | break; |
| 822 | case IMX_DMATYPE_CCM: |
| 823 | per_2_emi = sdma->script_addrs->dptc_dvfs_addr; |
| 824 | break; |
| 825 | case IMX_DMATYPE_SPDIF: |
| 826 | per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; |
| 827 | emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; |
| 828 | break; |
| 829 | case IMX_DMATYPE_IPU_MEMORY: |
| 830 | emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; |
| 831 | break; |
| 832 | default: |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | sdmac->pc_from_device = per_2_emi; |
| 837 | sdmac->pc_to_device = emi_2_per; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 838 | sdmac->device_to_device = per_2_per; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static int sdma_load_context(struct sdma_channel *sdmac) |
| 842 | { |
| 843 | struct sdma_engine *sdma = sdmac->sdma; |
| 844 | int channel = sdmac->channel; |
| 845 | int load_address; |
| 846 | struct sdma_context_data *context = sdma->context; |
| 847 | struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; |
| 848 | int ret; |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 849 | unsigned long flags; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 850 | |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 851 | if (sdmac->direction == DMA_DEV_TO_MEM) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 852 | load_address = sdmac->pc_from_device; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 853 | else if (sdmac->direction == DMA_DEV_TO_DEV) |
| 854 | load_address = sdmac->device_to_device; |
| 855 | else |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 856 | load_address = sdmac->pc_to_device; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 857 | |
| 858 | if (load_address < 0) |
| 859 | return load_address; |
| 860 | |
| 861 | dev_dbg(sdma->dev, "load_address = %d\n", load_address); |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 862 | dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 863 | dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); |
| 864 | dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 865 | dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]); |
| 866 | dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 867 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 868 | spin_lock_irqsave(&sdma->channel_0_lock, flags); |
Sascha Hauer | 73eab97 | 2011-08-25 11:03:35 +0200 | [diff] [blame] | 869 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 870 | memset(context, 0, sizeof(*context)); |
| 871 | context->channel_state.pc = load_address; |
| 872 | |
| 873 | /* Send by context the event mask,base address for peripheral |
| 874 | * and watermark level |
| 875 | */ |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 876 | context->gReg[0] = sdmac->event_mask[1]; |
| 877 | context->gReg[1] = sdmac->event_mask[0]; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 878 | context->gReg[2] = sdmac->per_addr; |
| 879 | context->gReg[6] = sdmac->shp_addr; |
| 880 | context->gReg[7] = sdmac->watermark_level; |
| 881 | |
| 882 | bd0->mode.command = C0_SETDM; |
| 883 | bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; |
| 884 | bd0->mode.count = sizeof(*context) / 4; |
| 885 | bd0->buffer_addr = sdma->context_phys; |
| 886 | bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 887 | ret = sdma_run_channel0(sdma); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 888 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 889 | spin_unlock_irqrestore(&sdma->channel_0_lock, flags); |
Sascha Hauer | 73eab97 | 2011-08-25 11:03:35 +0200 | [diff] [blame] | 890 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 891 | return ret; |
| 892 | } |
| 893 | |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 894 | static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 895 | { |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 896 | return container_of(chan, struct sdma_channel, chan); |
| 897 | } |
| 898 | |
| 899 | static int sdma_disable_channel(struct dma_chan *chan) |
| 900 | { |
| 901 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 902 | struct sdma_engine *sdma = sdmac->sdma; |
| 903 | int channel = sdmac->channel; |
| 904 | |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 905 | writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 906 | sdmac->status = DMA_ERROR; |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 907 | |
| 908 | return 0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 911 | static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac) |
| 912 | { |
| 913 | struct sdma_engine *sdma = sdmac->sdma; |
| 914 | |
| 915 | int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML; |
| 916 | int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16; |
| 917 | |
| 918 | set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]); |
| 919 | set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]); |
| 920 | |
| 921 | if (sdmac->event_id0 > 31) |
| 922 | sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE; |
| 923 | |
| 924 | if (sdmac->event_id1 > 31) |
| 925 | sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE; |
| 926 | |
| 927 | /* |
| 928 | * If LWML(src_maxburst) > HWML(dst_maxburst), we need |
| 929 | * swap LWML and HWML of INFO(A.3.2.5.1), also need swap |
| 930 | * r0(event_mask[1]) and r1(event_mask[0]). |
| 931 | */ |
| 932 | if (lwml > hwml) { |
| 933 | sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML | |
| 934 | SDMA_WATERMARK_LEVEL_HWML); |
| 935 | sdmac->watermark_level |= hwml; |
| 936 | sdmac->watermark_level |= lwml << 16; |
| 937 | swap(sdmac->event_mask[0], sdmac->event_mask[1]); |
| 938 | } |
| 939 | |
| 940 | if (sdmac->per_address2 >= sdma->spba_start_addr && |
| 941 | sdmac->per_address2 <= sdma->spba_end_addr) |
| 942 | sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP; |
| 943 | |
| 944 | if (sdmac->per_address >= sdma->spba_start_addr && |
| 945 | sdmac->per_address <= sdma->spba_end_addr) |
| 946 | sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP; |
| 947 | |
| 948 | sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT; |
| 949 | } |
| 950 | |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 951 | static int sdma_config_channel(struct dma_chan *chan) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 952 | { |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 953 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 954 | int ret; |
| 955 | |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 956 | sdma_disable_channel(chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 957 | |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 958 | sdmac->event_mask[0] = 0; |
| 959 | sdmac->event_mask[1] = 0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 960 | sdmac->shp_addr = 0; |
| 961 | sdmac->per_addr = 0; |
| 962 | |
| 963 | if (sdmac->event_id0) { |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 964 | if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 965 | return -EINVAL; |
| 966 | sdma_event_enable(sdmac, sdmac->event_id0); |
| 967 | } |
| 968 | |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 969 | if (sdmac->event_id1) { |
| 970 | if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events) |
| 971 | return -EINVAL; |
| 972 | sdma_event_enable(sdmac, sdmac->event_id1); |
| 973 | } |
| 974 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 975 | switch (sdmac->peripheral_type) { |
| 976 | case IMX_DMATYPE_DSP: |
| 977 | sdma_config_ownership(sdmac, false, true, true); |
| 978 | break; |
| 979 | case IMX_DMATYPE_MEMORY: |
| 980 | sdma_config_ownership(sdmac, false, true, false); |
| 981 | break; |
| 982 | default: |
| 983 | sdma_config_ownership(sdmac, true, true, false); |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | sdma_get_pc(sdmac, sdmac->peripheral_type); |
| 988 | |
| 989 | if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && |
| 990 | (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { |
| 991 | /* Handle multiple event channels differently */ |
| 992 | if (sdmac->event_id1) { |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 993 | if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP || |
| 994 | sdmac->peripheral_type == IMX_DMATYPE_ASRC) |
| 995 | sdma_set_watermarklevel_for_p2p(sdmac); |
| 996 | } else |
Richard Zhao | 0bbc141 | 2012-01-13 11:10:01 +0800 | [diff] [blame] | 997 | __set_bit(sdmac->event_id0, sdmac->event_mask); |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 998 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 999 | /* Watermark Level */ |
| 1000 | sdmac->watermark_level |= sdmac->watermark_level; |
| 1001 | /* Address */ |
| 1002 | sdmac->shp_addr = sdmac->per_address; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1003 | sdmac->per_addr = sdmac->per_address2; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1004 | } else { |
| 1005 | sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ |
| 1006 | } |
| 1007 | |
| 1008 | ret = sdma_load_context(sdmac); |
| 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | static int sdma_set_channel_priority(struct sdma_channel *sdmac, |
| 1014 | unsigned int priority) |
| 1015 | { |
| 1016 | struct sdma_engine *sdma = sdmac->sdma; |
| 1017 | int channel = sdmac->channel; |
| 1018 | |
| 1019 | if (priority < MXC_SDMA_MIN_PRIORITY |
| 1020 | || priority > MXC_SDMA_MAX_PRIORITY) { |
| 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1024 | writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1025 | |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | static int sdma_request_channel(struct sdma_channel *sdmac) |
| 1030 | { |
| 1031 | struct sdma_engine *sdma = sdmac->sdma; |
| 1032 | int channel = sdmac->channel; |
| 1033 | int ret = -EBUSY; |
| 1034 | |
Joe Perches | 9f92d22 | 2014-06-15 13:37:35 -0700 | [diff] [blame] | 1035 | sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, |
| 1036 | GFP_KERNEL); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1037 | if (!sdmac->bd) { |
| 1038 | ret = -ENOMEM; |
| 1039 | goto out; |
| 1040 | } |
| 1041 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1042 | sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys; |
| 1043 | sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; |
| 1044 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1045 | sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1046 | return 0; |
| 1047 | out: |
| 1048 | |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1052 | static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx) |
| 1053 | { |
Haitao Zhang | f69f2e2 | 2012-01-01 11:30:06 +0800 | [diff] [blame] | 1054 | unsigned long flags; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1055 | struct sdma_channel *sdmac = to_sdma_chan(tx->chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1056 | dma_cookie_t cookie; |
| 1057 | |
Haitao Zhang | f69f2e2 | 2012-01-01 11:30:06 +0800 | [diff] [blame] | 1058 | spin_lock_irqsave(&sdmac->lock, flags); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1059 | |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 1060 | cookie = dma_cookie_assign(tx); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1061 | |
Haitao Zhang | f69f2e2 | 2012-01-01 11:30:06 +0800 | [diff] [blame] | 1062 | spin_unlock_irqrestore(&sdmac->lock, flags); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1063 | |
| 1064 | return cookie; |
| 1065 | } |
| 1066 | |
| 1067 | static int sdma_alloc_chan_resources(struct dma_chan *chan) |
| 1068 | { |
| 1069 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
| 1070 | struct imx_dma_data *data = chan->private; |
| 1071 | int prio, ret; |
| 1072 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1073 | if (!data) |
| 1074 | return -EINVAL; |
| 1075 | |
| 1076 | switch (data->priority) { |
| 1077 | case DMA_PRIO_HIGH: |
| 1078 | prio = 3; |
| 1079 | break; |
| 1080 | case DMA_PRIO_MEDIUM: |
| 1081 | prio = 2; |
| 1082 | break; |
| 1083 | case DMA_PRIO_LOW: |
| 1084 | default: |
| 1085 | prio = 1; |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | sdmac->peripheral_type = data->peripheral_type; |
| 1090 | sdmac->event_id0 = data->dma_request; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1091 | sdmac->event_id1 = data->dma_request2; |
Richard Zhao | c2c744d | 2012-01-13 11:09:59 +0800 | [diff] [blame] | 1092 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1093 | clk_enable(sdmac->sdma->clk_ipg); |
| 1094 | clk_enable(sdmac->sdma->clk_ahb); |
Richard Zhao | c2c744d | 2012-01-13 11:09:59 +0800 | [diff] [blame] | 1095 | |
Richard Zhao | 3bb5e7c | 2012-01-13 11:09:58 +0800 | [diff] [blame] | 1096 | ret = sdma_request_channel(sdmac); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1097 | if (ret) |
| 1098 | return ret; |
| 1099 | |
Richard Zhao | 3bb5e7c | 2012-01-13 11:09:58 +0800 | [diff] [blame] | 1100 | ret = sdma_set_channel_priority(sdmac, prio); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1101 | if (ret) |
| 1102 | return ret; |
| 1103 | |
| 1104 | dma_async_tx_descriptor_init(&sdmac->desc, chan); |
| 1105 | sdmac->desc.tx_submit = sdma_tx_submit; |
| 1106 | /* txd.flags will be overwritten in prep funcs */ |
| 1107 | sdmac->desc.flags = DMA_CTRL_ACK; |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static void sdma_free_chan_resources(struct dma_chan *chan) |
| 1113 | { |
| 1114 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
| 1115 | struct sdma_engine *sdma = sdmac->sdma; |
| 1116 | |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 1117 | sdma_disable_channel(chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (sdmac->event_id0) |
| 1120 | sdma_event_disable(sdmac, sdmac->event_id0); |
| 1121 | if (sdmac->event_id1) |
| 1122 | sdma_event_disable(sdmac, sdmac->event_id1); |
| 1123 | |
| 1124 | sdmac->event_id0 = 0; |
| 1125 | sdmac->event_id1 = 0; |
| 1126 | |
| 1127 | sdma_set_channel_priority(sdmac, 0); |
| 1128 | |
| 1129 | dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys); |
| 1130 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1131 | clk_disable(sdma->clk_ipg); |
| 1132 | clk_disable(sdma->clk_ahb); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static struct dma_async_tx_descriptor *sdma_prep_slave_sg( |
| 1136 | struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1137 | unsigned int sg_len, enum dma_transfer_direction direction, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 1138 | unsigned long flags, void *context) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1139 | { |
| 1140 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
| 1141 | struct sdma_engine *sdma = sdmac->sdma; |
| 1142 | int ret, i, count; |
Sascha Hauer | 23889c6 | 2011-01-31 10:56:58 +0100 | [diff] [blame] | 1143 | int channel = sdmac->channel; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1144 | struct scatterlist *sg; |
| 1145 | |
| 1146 | if (sdmac->status == DMA_IN_PROGRESS) |
| 1147 | return NULL; |
| 1148 | sdmac->status = DMA_IN_PROGRESS; |
| 1149 | |
| 1150 | sdmac->flags = 0; |
| 1151 | |
Richard Zhao | 8e2e27c | 2012-06-04 09:17:24 +0800 | [diff] [blame] | 1152 | sdmac->buf_tail = 0; |
| 1153 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1154 | dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", |
| 1155 | sg_len, channel); |
| 1156 | |
| 1157 | sdmac->direction = direction; |
| 1158 | ret = sdma_load_context(sdmac); |
| 1159 | if (ret) |
| 1160 | goto err_out; |
| 1161 | |
| 1162 | if (sg_len > NUM_BD) { |
| 1163 | dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", |
| 1164 | channel, sg_len, NUM_BD); |
| 1165 | ret = -EINVAL; |
| 1166 | goto err_out; |
| 1167 | } |
| 1168 | |
Huang Shijie | ab59a51 | 2011-12-02 10:16:25 +0800 | [diff] [blame] | 1169 | sdmac->chn_count = 0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1170 | for_each_sg(sgl, sg, sg_len, i) { |
| 1171 | struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; |
| 1172 | int param; |
| 1173 | |
Anatolij Gustschin | d2f5c27 | 2010-11-22 18:35:18 +0100 | [diff] [blame] | 1174 | bd->buffer_addr = sg->dma_address; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1175 | |
Lars-Peter Clausen | fdaf9c4 | 2012-04-25 20:50:52 +0200 | [diff] [blame] | 1176 | count = sg_dma_len(sg); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (count > 0xffff) { |
| 1179 | dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", |
| 1180 | channel, count, 0xffff); |
| 1181 | ret = -EINVAL; |
| 1182 | goto err_out; |
| 1183 | } |
| 1184 | |
| 1185 | bd->mode.count = count; |
Huang Shijie | ab59a51 | 2011-12-02 10:16:25 +0800 | [diff] [blame] | 1186 | sdmac->chn_count += count; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1187 | |
| 1188 | if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { |
| 1189 | ret = -EINVAL; |
| 1190 | goto err_out; |
| 1191 | } |
Sascha Hauer | 1fa81c2 | 2011-01-12 13:02:28 +0100 | [diff] [blame] | 1192 | |
| 1193 | switch (sdmac->word_size) { |
| 1194 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1195 | bd->mode.command = 0; |
Sascha Hauer | 1fa81c2 | 2011-01-12 13:02:28 +0100 | [diff] [blame] | 1196 | if (count & 3 || sg->dma_address & 3) |
| 1197 | return NULL; |
| 1198 | break; |
| 1199 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
| 1200 | bd->mode.command = 2; |
| 1201 | if (count & 1 || sg->dma_address & 1) |
| 1202 | return NULL; |
| 1203 | break; |
| 1204 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
| 1205 | bd->mode.command = 1; |
| 1206 | break; |
| 1207 | default: |
| 1208 | return NULL; |
| 1209 | } |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1210 | |
| 1211 | param = BD_DONE | BD_EXTD | BD_CONT; |
| 1212 | |
Shawn Guo | 341b941 | 2011-01-20 05:50:39 +0800 | [diff] [blame] | 1213 | if (i + 1 == sg_len) { |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1214 | param |= BD_INTR; |
Shawn Guo | 341b941 | 2011-01-20 05:50:39 +0800 | [diff] [blame] | 1215 | param |= BD_LAST; |
| 1216 | param &= ~BD_CONT; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Olof Johansson | c3cc74b | 2013-11-12 22:30:44 -0800 | [diff] [blame] | 1219 | dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", |
| 1220 | i, count, (u64)sg->dma_address, |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1221 | param & BD_WRAP ? "wrap" : "", |
| 1222 | param & BD_INTR ? " intr" : ""); |
| 1223 | |
| 1224 | bd->mode.status = param; |
| 1225 | } |
| 1226 | |
| 1227 | sdmac->num_bd = sg_len; |
| 1228 | sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; |
| 1229 | |
| 1230 | return &sdmac->desc; |
| 1231 | err_out: |
Shawn Guo | 4b2ce9d | 2011-01-20 05:50:36 +0800 | [diff] [blame] | 1232 | sdmac->status = DMA_ERROR; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1233 | return NULL; |
| 1234 | } |
| 1235 | |
| 1236 | static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( |
| 1237 | struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 1238 | size_t period_len, enum dma_transfer_direction direction, |
Laurent Pinchart | 31c1e5a | 2014-08-01 12:20:10 +0200 | [diff] [blame] | 1239 | unsigned long flags) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1240 | { |
| 1241 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
| 1242 | struct sdma_engine *sdma = sdmac->sdma; |
| 1243 | int num_periods = buf_len / period_len; |
Sascha Hauer | 23889c6 | 2011-01-31 10:56:58 +0100 | [diff] [blame] | 1244 | int channel = sdmac->channel; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1245 | int ret, i = 0, buf = 0; |
| 1246 | |
| 1247 | dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); |
| 1248 | |
| 1249 | if (sdmac->status == DMA_IN_PROGRESS) |
| 1250 | return NULL; |
| 1251 | |
| 1252 | sdmac->status = DMA_IN_PROGRESS; |
| 1253 | |
Richard Zhao | 8e2e27c | 2012-06-04 09:17:24 +0800 | [diff] [blame] | 1254 | sdmac->buf_tail = 0; |
Russell King - ARM Linux | d1a792f | 2014-06-25 13:00:33 +0100 | [diff] [blame] | 1255 | sdmac->period_len = period_len; |
Richard Zhao | 8e2e27c | 2012-06-04 09:17:24 +0800 | [diff] [blame] | 1256 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1257 | sdmac->flags |= IMX_DMA_SG_LOOP; |
| 1258 | sdmac->direction = direction; |
| 1259 | ret = sdma_load_context(sdmac); |
| 1260 | if (ret) |
| 1261 | goto err_out; |
| 1262 | |
| 1263 | if (num_periods > NUM_BD) { |
| 1264 | dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", |
| 1265 | channel, num_periods, NUM_BD); |
| 1266 | goto err_out; |
| 1267 | } |
| 1268 | |
| 1269 | if (period_len > 0xffff) { |
| 1270 | dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n", |
| 1271 | channel, period_len, 0xffff); |
| 1272 | goto err_out; |
| 1273 | } |
| 1274 | |
| 1275 | while (buf < buf_len) { |
| 1276 | struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; |
| 1277 | int param; |
| 1278 | |
| 1279 | bd->buffer_addr = dma_addr; |
| 1280 | |
| 1281 | bd->mode.count = period_len; |
| 1282 | |
| 1283 | if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 1284 | goto err_out; |
| 1285 | if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 1286 | bd->mode.command = 0; |
| 1287 | else |
| 1288 | bd->mode.command = sdmac->word_size; |
| 1289 | |
| 1290 | param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; |
| 1291 | if (i + 1 == num_periods) |
| 1292 | param |= BD_WRAP; |
| 1293 | |
Olof Johansson | c3cc74b | 2013-11-12 22:30:44 -0800 | [diff] [blame] | 1294 | dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", |
| 1295 | i, period_len, (u64)dma_addr, |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1296 | param & BD_WRAP ? "wrap" : "", |
| 1297 | param & BD_INTR ? " intr" : ""); |
| 1298 | |
| 1299 | bd->mode.status = param; |
| 1300 | |
| 1301 | dma_addr += period_len; |
| 1302 | buf += period_len; |
| 1303 | |
| 1304 | i++; |
| 1305 | } |
| 1306 | |
| 1307 | sdmac->num_bd = num_periods; |
| 1308 | sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; |
| 1309 | |
| 1310 | return &sdmac->desc; |
| 1311 | err_out: |
| 1312 | sdmac->status = DMA_ERROR; |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 1316 | static int sdma_config(struct dma_chan *chan, |
| 1317 | struct dma_slave_config *dmaengine_cfg) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1318 | { |
| 1319 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1320 | |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 1321 | if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) { |
| 1322 | sdmac->per_address = dmaengine_cfg->src_addr; |
| 1323 | sdmac->watermark_level = dmaengine_cfg->src_maxburst * |
| 1324 | dmaengine_cfg->src_addr_width; |
| 1325 | sdmac->word_size = dmaengine_cfg->src_addr_width; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1326 | } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) { |
| 1327 | sdmac->per_address2 = dmaengine_cfg->src_addr; |
| 1328 | sdmac->per_address = dmaengine_cfg->dst_addr; |
| 1329 | sdmac->watermark_level = dmaengine_cfg->src_maxburst & |
| 1330 | SDMA_WATERMARK_LEVEL_LWML; |
| 1331 | sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) & |
| 1332 | SDMA_WATERMARK_LEVEL_HWML; |
| 1333 | sdmac->word_size = dmaengine_cfg->dst_addr_width; |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 1334 | } else { |
| 1335 | sdmac->per_address = dmaengine_cfg->dst_addr; |
| 1336 | sdmac->watermark_level = dmaengine_cfg->dst_maxburst * |
| 1337 | dmaengine_cfg->dst_addr_width; |
| 1338 | sdmac->word_size = dmaengine_cfg->dst_addr_width; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1339 | } |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 1340 | sdmac->direction = dmaengine_cfg->direction; |
| 1341 | return sdma_config_channel(chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | static enum dma_status sdma_tx_status(struct dma_chan *chan, |
Andy Shevchenko | e8e3a79 | 2013-05-27 15:14:31 +0300 | [diff] [blame] | 1345 | dma_cookie_t cookie, |
| 1346 | struct dma_tx_state *txstate) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1347 | { |
| 1348 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
Russell King - ARM Linux | d1a792f | 2014-06-25 13:00:33 +0100 | [diff] [blame] | 1349 | u32 residue; |
| 1350 | |
| 1351 | if (sdmac->flags & IMX_DMA_SG_LOOP) |
| 1352 | residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len; |
| 1353 | else |
| 1354 | residue = sdmac->chn_count - sdmac->chn_real_count; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1355 | |
Andy Shevchenko | e8e3a79 | 2013-05-27 15:14:31 +0300 | [diff] [blame] | 1356 | dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, |
Russell King - ARM Linux | d1a792f | 2014-06-25 13:00:33 +0100 | [diff] [blame] | 1357 | residue); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1358 | |
Shawn Guo | 8a96591 | 2011-01-20 05:50:37 +0800 | [diff] [blame] | 1359 | return sdmac->status; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static void sdma_issue_pending(struct dma_chan *chan) |
| 1363 | { |
Sascha Hauer | 2b4f130 | 2012-01-09 10:32:50 +0100 | [diff] [blame] | 1364 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
| 1365 | struct sdma_engine *sdma = sdmac->sdma; |
| 1366 | |
| 1367 | if (sdmac->status == DMA_IN_PROGRESS) |
| 1368 | sdma_enable_channel(sdma, sdmac->channel); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1371 | #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 |
Nicolin Chen | cd72b84 | 2013-11-13 22:55:24 +0800 | [diff] [blame] | 1372 | #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38 |
Fabio Estevam | a572460 | 2015-03-11 12:30:58 -0300 | [diff] [blame] | 1373 | #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41 |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1374 | |
| 1375 | static void sdma_add_scripts(struct sdma_engine *sdma, |
| 1376 | const struct sdma_script_start_addrs *addr) |
| 1377 | { |
| 1378 | s32 *addr_arr = (u32 *)addr; |
| 1379 | s32 *saddr_arr = (u32 *)sdma->script_addrs; |
| 1380 | int i; |
| 1381 | |
Nicolin Chen | 70dabaed | 2014-01-08 16:45:56 +0800 | [diff] [blame] | 1382 | /* use the default firmware in ROM if missing external firmware */ |
| 1383 | if (!sdma->script_number) |
| 1384 | sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; |
| 1385 | |
Nicolin Chen | cd72b84 | 2013-11-13 22:55:24 +0800 | [diff] [blame] | 1386 | for (i = 0; i < sdma->script_number; i++) |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1387 | if (addr_arr[i] > 0) |
| 1388 | saddr_arr[i] = addr_arr[i]; |
| 1389 | } |
| 1390 | |
Sascha Hauer | 7b4b88e | 2011-08-25 11:03:37 +0200 | [diff] [blame] | 1391 | static void sdma_load_firmware(const struct firmware *fw, void *context) |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1392 | { |
Sascha Hauer | 7b4b88e | 2011-08-25 11:03:37 +0200 | [diff] [blame] | 1393 | struct sdma_engine *sdma = context; |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1394 | const struct sdma_firmware_header *header; |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1395 | const struct sdma_script_start_addrs *addr; |
| 1396 | unsigned short *ram_code; |
| 1397 | |
Sascha Hauer | 7b4b88e | 2011-08-25 11:03:37 +0200 | [diff] [blame] | 1398 | if (!fw) { |
Sascha Hauer | 0f927a1 | 2014-11-12 20:04:29 -0200 | [diff] [blame] | 1399 | dev_info(sdma->dev, "external firmware not found, using ROM firmware\n"); |
| 1400 | /* In this case we just use the ROM firmware. */ |
Sascha Hauer | 7b4b88e | 2011-08-25 11:03:37 +0200 | [diff] [blame] | 1401 | return; |
| 1402 | } |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1403 | |
| 1404 | if (fw->size < sizeof(*header)) |
| 1405 | goto err_firmware; |
| 1406 | |
| 1407 | header = (struct sdma_firmware_header *)fw->data; |
| 1408 | |
| 1409 | if (header->magic != SDMA_FIRMWARE_MAGIC) |
| 1410 | goto err_firmware; |
| 1411 | if (header->ram_code_start + header->ram_code_size > fw->size) |
| 1412 | goto err_firmware; |
Nicolin Chen | cd72b84 | 2013-11-13 22:55:24 +0800 | [diff] [blame] | 1413 | switch (header->version_major) { |
Asaf Vertz | 681d15e | 2014-12-10 10:00:36 +0200 | [diff] [blame] | 1414 | case 1: |
| 1415 | sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; |
| 1416 | break; |
| 1417 | case 2: |
| 1418 | sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2; |
| 1419 | break; |
Fabio Estevam | a572460 | 2015-03-11 12:30:58 -0300 | [diff] [blame] | 1420 | case 3: |
| 1421 | sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3; |
| 1422 | break; |
Asaf Vertz | 681d15e | 2014-12-10 10:00:36 +0200 | [diff] [blame] | 1423 | default: |
| 1424 | dev_err(sdma->dev, "unknown firmware version\n"); |
| 1425 | goto err_firmware; |
Nicolin Chen | cd72b84 | 2013-11-13 22:55:24 +0800 | [diff] [blame] | 1426 | } |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1427 | |
| 1428 | addr = (void *)header + header->script_addrs_start; |
| 1429 | ram_code = (void *)header + header->ram_code_start; |
| 1430 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1431 | clk_enable(sdma->clk_ipg); |
| 1432 | clk_enable(sdma->clk_ahb); |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1433 | /* download the RAM image for SDMA */ |
| 1434 | sdma_load_script(sdma, ram_code, |
| 1435 | header->ram_code_size, |
Sascha Hauer | 6866fd3 | 2011-01-12 11:18:14 +0100 | [diff] [blame] | 1436 | addr->ram_code_start_addr); |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1437 | clk_disable(sdma->clk_ipg); |
| 1438 | clk_disable(sdma->clk_ahb); |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1439 | |
| 1440 | sdma_add_scripts(sdma, addr); |
| 1441 | |
| 1442 | dev_info(sdma->dev, "loaded firmware %d.%d\n", |
| 1443 | header->version_major, |
| 1444 | header->version_minor); |
| 1445 | |
| 1446 | err_firmware: |
| 1447 | release_firmware(fw); |
Sascha Hauer | 7b4b88e | 2011-08-25 11:03:37 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
Arnd Bergmann | fe6cf28 | 2014-09-26 23:24:00 +0200 | [diff] [blame] | 1450 | static int sdma_get_firmware(struct sdma_engine *sdma, |
Sascha Hauer | 7b4b88e | 2011-08-25 11:03:37 +0200 | [diff] [blame] | 1451 | const char *fw_name) |
| 1452 | { |
| 1453 | int ret; |
| 1454 | |
| 1455 | ret = request_firmware_nowait(THIS_MODULE, |
| 1456 | FW_ACTION_HOTPLUG, fw_name, sdma->dev, |
| 1457 | GFP_KERNEL, sdma, sdma_load_firmware); |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1458 | |
| 1459 | return ret; |
| 1460 | } |
| 1461 | |
Jingoo Han | 19bfc77 | 2014-11-06 10:10:09 +0900 | [diff] [blame] | 1462 | static int sdma_init(struct sdma_engine *sdma) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1463 | { |
| 1464 | int i, ret; |
| 1465 | dma_addr_t ccb_phys; |
| 1466 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1467 | clk_enable(sdma->clk_ipg); |
| 1468 | clk_enable(sdma->clk_ahb); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1469 | |
| 1470 | /* Be sure SDMA has not started yet */ |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1471 | writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1472 | |
| 1473 | sdma->channel_control = dma_alloc_coherent(NULL, |
| 1474 | MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + |
| 1475 | sizeof(struct sdma_context_data), |
| 1476 | &ccb_phys, GFP_KERNEL); |
| 1477 | |
| 1478 | if (!sdma->channel_control) { |
| 1479 | ret = -ENOMEM; |
| 1480 | goto err_dma_alloc; |
| 1481 | } |
| 1482 | |
| 1483 | sdma->context = (void *)sdma->channel_control + |
| 1484 | MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); |
| 1485 | sdma->context_phys = ccb_phys + |
| 1486 | MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); |
| 1487 | |
| 1488 | /* Zero-out the CCB structures array just allocated */ |
| 1489 | memset(sdma->channel_control, 0, |
| 1490 | MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control)); |
| 1491 | |
| 1492 | /* disable all channels */ |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 1493 | for (i = 0; i < sdma->drvdata->num_events; i++) |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1494 | writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1495 | |
| 1496 | /* All channels have priority 0 */ |
| 1497 | for (i = 0; i < MAX_DMA_CHANNELS; i++) |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1498 | writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1499 | |
| 1500 | ret = sdma_request_channel(&sdma->channel[0]); |
| 1501 | if (ret) |
| 1502 | goto err_dma_alloc; |
| 1503 | |
| 1504 | sdma_config_ownership(&sdma->channel[0], false, true, false); |
| 1505 | |
| 1506 | /* Set Command Channel (Channel Zero) */ |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1507 | writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1508 | |
| 1509 | /* Set bits of CONFIG register but with static context switching */ |
| 1510 | /* FIXME: Check whether to set ACR bit depending on clock ratios */ |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1511 | writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1512 | |
Richard Zhao | c4b5685 | 2012-01-13 11:09:57 +0800 | [diff] [blame] | 1513 | writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1514 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1515 | /* Initializes channel's priorities */ |
| 1516 | sdma_set_channel_priority(&sdma->channel[0], 7); |
| 1517 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1518 | clk_disable(sdma->clk_ipg); |
| 1519 | clk_disable(sdma->clk_ahb); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1520 | |
| 1521 | return 0; |
| 1522 | |
| 1523 | err_dma_alloc: |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1524 | clk_disable(sdma->clk_ipg); |
| 1525 | clk_disable(sdma->clk_ahb); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1526 | dev_err(sdma->dev, "initialisation failed with %d\n", ret); |
| 1527 | return ret; |
| 1528 | } |
| 1529 | |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1530 | static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param) |
| 1531 | { |
Nicolin Chen | 0b35186 | 2014-06-16 11:32:29 +0800 | [diff] [blame] | 1532 | struct sdma_channel *sdmac = to_sdma_chan(chan); |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1533 | struct imx_dma_data *data = fn_param; |
| 1534 | |
| 1535 | if (!imx_dma_is_general_purpose(chan)) |
| 1536 | return false; |
| 1537 | |
Nicolin Chen | 0b35186 | 2014-06-16 11:32:29 +0800 | [diff] [blame] | 1538 | sdmac->data = *data; |
| 1539 | chan->private = &sdmac->data; |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1540 | |
| 1541 | return true; |
| 1542 | } |
| 1543 | |
| 1544 | static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec, |
| 1545 | struct of_dma *ofdma) |
| 1546 | { |
| 1547 | struct sdma_engine *sdma = ofdma->of_dma_data; |
| 1548 | dma_cap_mask_t mask = sdma->dma_device.cap_mask; |
| 1549 | struct imx_dma_data data; |
| 1550 | |
| 1551 | if (dma_spec->args_count != 3) |
| 1552 | return NULL; |
| 1553 | |
| 1554 | data.dma_request = dma_spec->args[0]; |
| 1555 | data.peripheral_type = dma_spec->args[1]; |
| 1556 | data.priority = dma_spec->args[2]; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1557 | /* |
| 1558 | * init dma_request2 to zero, which is not used by the dts. |
| 1559 | * For P2P, dma_request2 is init from dma_request_channel(), |
| 1560 | * chan->private will point to the imx_dma_data, and in |
| 1561 | * device_alloc_chan_resources(), imx_dma_data.dma_request2 will |
| 1562 | * be set to sdmac->event_id1. |
| 1563 | */ |
| 1564 | data.dma_request2 = 0; |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1565 | |
| 1566 | return dma_request_channel(mask, sdma_filter_fn, &data); |
| 1567 | } |
| 1568 | |
Mark Brown | e34b731 | 2014-08-27 11:55:53 +0100 | [diff] [blame] | 1569 | static int sdma_probe(struct platform_device *pdev) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1570 | { |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1571 | const struct of_device_id *of_id = |
| 1572 | of_match_device(sdma_dt_ids, &pdev->dev); |
| 1573 | struct device_node *np = pdev->dev.of_node; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1574 | struct device_node *spba_bus; |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1575 | const char *fw_name; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1576 | int ret; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1577 | int irq; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1578 | struct resource *iores; |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1579 | struct resource spba_res; |
Jingoo Han | d4adcc0 | 2013-07-30 17:09:11 +0900 | [diff] [blame] | 1580 | struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1581 | int i; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1582 | struct sdma_engine *sdma; |
Sascha Hauer | 36e2f21 | 2011-08-25 11:03:36 +0200 | [diff] [blame] | 1583 | s32 *saddr_arr; |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 1584 | const struct sdma_driver_data *drvdata = NULL; |
| 1585 | |
| 1586 | if (of_id) |
| 1587 | drvdata = of_id->data; |
| 1588 | else if (pdev->id_entry) |
| 1589 | drvdata = (void *)pdev->id_entry->driver_data; |
| 1590 | |
| 1591 | if (!drvdata) { |
| 1592 | dev_err(&pdev->dev, "unable to find driver data\n"); |
| 1593 | return -EINVAL; |
| 1594 | } |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1595 | |
Philippe Retornaz | 42536b9 | 2013-10-14 09:45:17 +0100 | [diff] [blame] | 1596 | ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 1597 | if (ret) |
| 1598 | return ret; |
| 1599 | |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1600 | sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1601 | if (!sdma) |
| 1602 | return -ENOMEM; |
| 1603 | |
Richard Zhao | 2ccaef0 | 2012-05-11 15:14:27 +0800 | [diff] [blame] | 1604 | spin_lock_init(&sdma->channel_0_lock); |
Sascha Hauer | 73eab97 | 2011-08-25 11:03:35 +0200 | [diff] [blame] | 1605 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1606 | sdma->dev = &pdev->dev; |
Sascha Hauer | 17bba72 | 2013-08-20 10:04:31 +0200 | [diff] [blame] | 1607 | sdma->drvdata = drvdata; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1608 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1609 | irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1610 | if (irq < 0) |
Fabio Estevam | 63c72e0 | 2014-12-29 15:20:53 -0200 | [diff] [blame] | 1611 | return irq; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1612 | |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1613 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1614 | sdma->regs = devm_ioremap_resource(&pdev->dev, iores); |
| 1615 | if (IS_ERR(sdma->regs)) |
| 1616 | return PTR_ERR(sdma->regs); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1617 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1618 | sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1619 | if (IS_ERR(sdma->clk_ipg)) |
| 1620 | return PTR_ERR(sdma->clk_ipg); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1621 | |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1622 | sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1623 | if (IS_ERR(sdma->clk_ahb)) |
| 1624 | return PTR_ERR(sdma->clk_ahb); |
Sascha Hauer | 7560e3f | 2012-03-07 09:30:06 +0100 | [diff] [blame] | 1625 | |
| 1626 | clk_prepare(sdma->clk_ipg); |
| 1627 | clk_prepare(sdma->clk_ahb); |
| 1628 | |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1629 | ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma", |
| 1630 | sdma); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1631 | if (ret) |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1632 | return ret; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1633 | |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1634 | sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); |
Fabio Estevam | 7f24e0e | 2014-12-29 15:20:52 -0200 | [diff] [blame] | 1635 | if (!sdma->script_addrs) |
| 1636 | return -ENOMEM; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1637 | |
Sascha Hauer | 36e2f21 | 2011-08-25 11:03:36 +0200 | [diff] [blame] | 1638 | /* initially no scripts available */ |
| 1639 | saddr_arr = (s32 *)sdma->script_addrs; |
| 1640 | for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) |
| 1641 | saddr_arr[i] = -EINVAL; |
| 1642 | |
Sascha Hauer | 7214a8b | 2011-01-31 10:21:35 +0100 | [diff] [blame] | 1643 | dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); |
| 1644 | dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); |
| 1645 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1646 | INIT_LIST_HEAD(&sdma->dma_device.channels); |
| 1647 | /* Initialize channel parameters */ |
| 1648 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { |
| 1649 | struct sdma_channel *sdmac = &sdma->channel[i]; |
| 1650 | |
| 1651 | sdmac->sdma = sdma; |
| 1652 | spin_lock_init(&sdmac->lock); |
| 1653 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1654 | sdmac->chan.device = &sdma->dma_device; |
Russell King - ARM Linux | 8ac6954 | 2012-03-06 22:36:27 +0000 | [diff] [blame] | 1655 | dma_cookie_init(&sdmac->chan); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1656 | sdmac->channel = i; |
| 1657 | |
Huang Shijie | abd9ccc | 2012-04-28 18:15:42 +0800 | [diff] [blame] | 1658 | tasklet_init(&sdmac->tasklet, sdma_tasklet, |
| 1659 | (unsigned long) sdmac); |
Sascha Hauer | 23889c6 | 2011-01-31 10:56:58 +0100 | [diff] [blame] | 1660 | /* |
| 1661 | * Add the channel to the DMAC list. Do not add channel 0 though |
| 1662 | * because we need it internally in the SDMA driver. This also means |
| 1663 | * that channel 0 in dmaengine counting matches sdma channel 1. |
| 1664 | */ |
| 1665 | if (i) |
| 1666 | list_add_tail(&sdmac->chan.device_node, |
| 1667 | &sdma->dma_device.channels); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1670 | ret = sdma_init(sdma); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1671 | if (ret) |
| 1672 | goto err_init; |
| 1673 | |
Sascha Hauer | dcfec3c | 2013-08-20 10:04:32 +0200 | [diff] [blame] | 1674 | if (sdma->drvdata->script_addrs) |
| 1675 | sdma_add_scripts(sdma, sdma->drvdata->script_addrs); |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1676 | if (pdata && pdata->script_addrs) |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1677 | sdma_add_scripts(sdma, pdata->script_addrs); |
| 1678 | |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1679 | if (pdata) { |
Fabio Estevam | 6d0d7e2 | 2012-02-29 11:20:38 -0300 | [diff] [blame] | 1680 | ret = sdma_get_firmware(sdma, pdata->fw_name); |
| 1681 | if (ret) |
Fabio Estevam | ad1122e | 2012-03-08 09:26:39 -0300 | [diff] [blame] | 1682 | dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1683 | } else { |
| 1684 | /* |
| 1685 | * Because that device tree does not encode ROM script address, |
| 1686 | * the RAM script in firmware is mandatory for device tree |
| 1687 | * probe, otherwise it fails. |
| 1688 | */ |
| 1689 | ret = of_property_read_string(np, "fsl,sdma-ram-script-name", |
| 1690 | &fw_name); |
Fabio Estevam | 6602b0d | 2012-02-29 11:20:37 -0300 | [diff] [blame] | 1691 | if (ret) |
Fabio Estevam | ad1122e | 2012-03-08 09:26:39 -0300 | [diff] [blame] | 1692 | dev_warn(&pdev->dev, "failed to get firmware name\n"); |
Fabio Estevam | 6602b0d | 2012-02-29 11:20:37 -0300 | [diff] [blame] | 1693 | else { |
| 1694 | ret = sdma_get_firmware(sdma, fw_name); |
| 1695 | if (ret) |
Fabio Estevam | ad1122e | 2012-03-08 09:26:39 -0300 | [diff] [blame] | 1696 | dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1699 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1700 | sdma->dma_device.dev = &pdev->dev; |
| 1701 | |
| 1702 | sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; |
| 1703 | sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; |
| 1704 | sdma->dma_device.device_tx_status = sdma_tx_status; |
| 1705 | sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; |
| 1706 | sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; |
Maxime Ripard | 7b350ab | 2014-11-17 14:42:17 +0100 | [diff] [blame] | 1707 | sdma->dma_device.device_config = sdma_config; |
| 1708 | sdma->dma_device.device_terminate_all = sdma_disable_channel; |
Fabio Estevam | 1e4a4f5 | 2014-12-29 15:20:51 -0200 | [diff] [blame] | 1709 | sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 1710 | sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); |
| 1711 | sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
| 1712 | sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1713 | sdma->dma_device.device_issue_pending = sdma_issue_pending; |
Sascha Hauer | b9b3f82 | 2011-01-12 12:12:31 +0100 | [diff] [blame] | 1714 | sdma->dma_device.dev->dma_parms = &sdma->dma_parms; |
| 1715 | dma_set_max_seg_size(sdma->dma_device.dev, 65535); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1716 | |
Vignesh Raman | 23e1181 | 2014-08-05 18:39:41 +0530 | [diff] [blame] | 1717 | platform_set_drvdata(pdev, sdma); |
| 1718 | |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1719 | ret = dma_async_device_register(&sdma->dma_device); |
| 1720 | if (ret) { |
| 1721 | dev_err(&pdev->dev, "unable to register\n"); |
| 1722 | goto err_init; |
| 1723 | } |
| 1724 | |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1725 | if (np) { |
| 1726 | ret = of_dma_controller_register(np, sdma_xlate, sdma); |
| 1727 | if (ret) { |
| 1728 | dev_err(&pdev->dev, "failed to register controller\n"); |
| 1729 | goto err_register; |
| 1730 | } |
Shengjiu Wang | 8391ecf | 2015-07-10 17:08:16 +0800 | [diff] [blame^] | 1731 | |
| 1732 | spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus"); |
| 1733 | ret = of_address_to_resource(spba_bus, 0, &spba_res); |
| 1734 | if (!ret) { |
| 1735 | sdma->spba_start_addr = spba_res.start; |
| 1736 | sdma->spba_end_addr = spba_res.end; |
| 1737 | } |
| 1738 | of_node_put(spba_bus); |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1739 | } |
| 1740 | |
Sascha Hauer | 5b28aa3 | 2010-10-06 15:41:15 +0200 | [diff] [blame] | 1741 | dev_info(sdma->dev, "initialized\n"); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1742 | |
| 1743 | return 0; |
| 1744 | |
Shawn Guo | 9479e17 | 2013-05-30 22:23:32 +0800 | [diff] [blame] | 1745 | err_register: |
| 1746 | dma_async_device_unregister(&sdma->dma_device); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1747 | err_init: |
| 1748 | kfree(sdma->script_addrs); |
Shawn Guo | 939fd4f | 2011-01-19 19:13:06 +0800 | [diff] [blame] | 1749 | return ret; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Maxin B. John | 1d1bbd3 | 2013-02-20 02:07:04 +0200 | [diff] [blame] | 1752 | static int sdma_remove(struct platform_device *pdev) |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1753 | { |
Vignesh Raman | 23e1181 | 2014-08-05 18:39:41 +0530 | [diff] [blame] | 1754 | struct sdma_engine *sdma = platform_get_drvdata(pdev); |
Vignesh Raman | c12fe49 | 2014-08-05 18:39:42 +0530 | [diff] [blame] | 1755 | int i; |
Vignesh Raman | 23e1181 | 2014-08-05 18:39:41 +0530 | [diff] [blame] | 1756 | |
| 1757 | dma_async_device_unregister(&sdma->dma_device); |
| 1758 | kfree(sdma->script_addrs); |
Vignesh Raman | c12fe49 | 2014-08-05 18:39:42 +0530 | [diff] [blame] | 1759 | /* Kill the tasklet */ |
| 1760 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { |
| 1761 | struct sdma_channel *sdmac = &sdma->channel[i]; |
| 1762 | |
| 1763 | tasklet_kill(&sdmac->tasklet); |
| 1764 | } |
Vignesh Raman | 23e1181 | 2014-08-05 18:39:41 +0530 | [diff] [blame] | 1765 | |
| 1766 | platform_set_drvdata(pdev, NULL); |
| 1767 | dev_info(&pdev->dev, "Removed...\n"); |
| 1768 | return 0; |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | static struct platform_driver sdma_driver = { |
| 1772 | .driver = { |
| 1773 | .name = "imx-sdma", |
Shawn Guo | 580975d | 2011-07-14 08:35:48 +0800 | [diff] [blame] | 1774 | .of_match_table = sdma_dt_ids, |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1775 | }, |
Shawn Guo | 62550cd | 2011-07-13 21:33:17 +0800 | [diff] [blame] | 1776 | .id_table = sdma_devtypes, |
Maxin B. John | 1d1bbd3 | 2013-02-20 02:07:04 +0200 | [diff] [blame] | 1777 | .remove = sdma_remove, |
Vignesh Raman | 23e1181 | 2014-08-05 18:39:41 +0530 | [diff] [blame] | 1778 | .probe = sdma_probe, |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1779 | }; |
| 1780 | |
Vignesh Raman | 23e1181 | 2014-08-05 18:39:41 +0530 | [diff] [blame] | 1781 | module_platform_driver(sdma_driver); |
Sascha Hauer | 1ec1e82 | 2010-09-30 13:56:34 +0000 | [diff] [blame] | 1782 | |
| 1783 | MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); |
| 1784 | MODULE_DESCRIPTION("i.MX SDMA driver"); |
| 1785 | MODULE_LICENSE("GPL"); |