11ec1e82fSSascha Hauer /* 21ec1e82fSSascha Hauer * drivers/dma/imx-sdma.c 31ec1e82fSSascha Hauer * 41ec1e82fSSascha Hauer * This file contains a driver for the Freescale Smart DMA engine 51ec1e82fSSascha Hauer * 61ec1e82fSSascha Hauer * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 71ec1e82fSSascha Hauer * 81ec1e82fSSascha Hauer * Based on code from Freescale: 91ec1e82fSSascha Hauer * 101ec1e82fSSascha Hauer * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved. 111ec1e82fSSascha Hauer * 121ec1e82fSSascha Hauer * The code contained herein is licensed under the GNU General Public 131ec1e82fSSascha Hauer * License. You may obtain a copy of the GNU General Public License 141ec1e82fSSascha Hauer * Version 2 or later at the following locations: 151ec1e82fSSascha Hauer * 161ec1e82fSSascha Hauer * http://www.opensource.org/licenses/gpl-license.html 171ec1e82fSSascha Hauer * http://www.gnu.org/copyleft/gpl.html 181ec1e82fSSascha Hauer */ 191ec1e82fSSascha Hauer 201ec1e82fSSascha Hauer #include <linux/init.h> 211d069bfaSMichael Olbrich #include <linux/iopoll.h> 22f8de8f4cSAxel Lin #include <linux/module.h> 231ec1e82fSSascha Hauer #include <linux/types.h> 240bbc1413SRichard Zhao #include <linux/bitops.h> 251ec1e82fSSascha Hauer #include <linux/mm.h> 261ec1e82fSSascha Hauer #include <linux/interrupt.h> 271ec1e82fSSascha Hauer #include <linux/clk.h> 282ccaef05SRichard Zhao #include <linux/delay.h> 291ec1e82fSSascha Hauer #include <linux/sched.h> 301ec1e82fSSascha Hauer #include <linux/semaphore.h> 311ec1e82fSSascha Hauer #include <linux/spinlock.h> 321ec1e82fSSascha Hauer #include <linux/device.h> 331ec1e82fSSascha Hauer #include <linux/dma-mapping.h> 341ec1e82fSSascha Hauer #include <linux/firmware.h> 351ec1e82fSSascha Hauer #include <linux/slab.h> 361ec1e82fSSascha Hauer #include <linux/platform_device.h> 371ec1e82fSSascha Hauer #include <linux/dmaengine.h> 38580975d7SShawn Guo #include <linux/of.h> 398391ecf4SShengjiu Wang #include <linux/of_address.h> 40580975d7SShawn Guo #include <linux/of_device.h> 419479e17cSShawn Guo #include <linux/of_dma.h> 421ec1e82fSSascha Hauer 431ec1e82fSSascha Hauer #include <asm/irq.h> 4482906b13SArnd Bergmann #include <linux/platform_data/dma-imx-sdma.h> 4582906b13SArnd Bergmann #include <linux/platform_data/dma-imx.h> 46d078cd1bSZidan Wang #include <linux/regmap.h> 47d078cd1bSZidan Wang #include <linux/mfd/syscon.h> 48d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> 491ec1e82fSSascha Hauer 50d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 51d2ebfb33SRussell King - ARM Linux 521ec1e82fSSascha Hauer /* SDMA registers */ 531ec1e82fSSascha Hauer #define SDMA_H_C0PTR 0x000 541ec1e82fSSascha Hauer #define SDMA_H_INTR 0x004 551ec1e82fSSascha Hauer #define SDMA_H_STATSTOP 0x008 561ec1e82fSSascha Hauer #define SDMA_H_START 0x00c 571ec1e82fSSascha Hauer #define SDMA_H_EVTOVR 0x010 581ec1e82fSSascha Hauer #define SDMA_H_DSPOVR 0x014 591ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR 0x018 601ec1e82fSSascha Hauer #define SDMA_H_EVTPEND 0x01c 611ec1e82fSSascha Hauer #define SDMA_H_DSPENBL 0x020 621ec1e82fSSascha Hauer #define SDMA_H_RESET 0x024 631ec1e82fSSascha Hauer #define SDMA_H_EVTERR 0x028 641ec1e82fSSascha Hauer #define SDMA_H_INTRMSK 0x02c 651ec1e82fSSascha Hauer #define SDMA_H_PSW 0x030 661ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG 0x034 671ec1e82fSSascha Hauer #define SDMA_H_CONFIG 0x038 681ec1e82fSSascha Hauer #define SDMA_ONCE_ENB 0x040 691ec1e82fSSascha Hauer #define SDMA_ONCE_DATA 0x044 701ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR 0x048 711ec1e82fSSascha Hauer #define SDMA_ONCE_STAT 0x04c 721ec1e82fSSascha Hauer #define SDMA_ONCE_CMD 0x050 731ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR 0x054 741ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR 0x058 751ec1e82fSSascha Hauer #define SDMA_CHN0ADDR 0x05c 761ec1e82fSSascha Hauer #define SDMA_ONCE_RTB 0x060 771ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1 0x070 781ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2 0x074 7962550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35 0x200 8062550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31 0x080 811ec1e82fSSascha Hauer #define SDMA_CHNPRI_0 0x100 821ec1e82fSSascha Hauer 831ec1e82fSSascha Hauer /* 841ec1e82fSSascha Hauer * Buffer descriptor status values. 851ec1e82fSSascha Hauer */ 861ec1e82fSSascha Hauer #define BD_DONE 0x01 871ec1e82fSSascha Hauer #define BD_WRAP 0x02 881ec1e82fSSascha Hauer #define BD_CONT 0x04 891ec1e82fSSascha Hauer #define BD_INTR 0x08 901ec1e82fSSascha Hauer #define BD_RROR 0x10 911ec1e82fSSascha Hauer #define BD_LAST 0x20 921ec1e82fSSascha Hauer #define BD_EXTD 0x80 931ec1e82fSSascha Hauer 941ec1e82fSSascha Hauer /* 951ec1e82fSSascha Hauer * Data Node descriptor status values. 961ec1e82fSSascha Hauer */ 971ec1e82fSSascha Hauer #define DND_END_OF_FRAME 0x80 981ec1e82fSSascha Hauer #define DND_END_OF_XFER 0x40 991ec1e82fSSascha Hauer #define DND_DONE 0x20 1001ec1e82fSSascha Hauer #define DND_UNUSED 0x01 1011ec1e82fSSascha Hauer 1021ec1e82fSSascha Hauer /* 1031ec1e82fSSascha Hauer * IPCV2 descriptor status values. 1041ec1e82fSSascha Hauer */ 1051ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME 0x40 1061ec1e82fSSascha Hauer 1071ec1e82fSSascha Hauer #define IPCV2_MAX_NODES 50 1081ec1e82fSSascha Hauer /* 1091ec1e82fSSascha Hauer * Error bit set in the CCB status field by the SDMA, 1101ec1e82fSSascha Hauer * in setbd routine, in case of a transfer error 1111ec1e82fSSascha Hauer */ 1121ec1e82fSSascha Hauer #define DATA_ERROR 0x10000000 1131ec1e82fSSascha Hauer 1141ec1e82fSSascha Hauer /* 1151ec1e82fSSascha Hauer * Buffer descriptor commands. 1161ec1e82fSSascha Hauer */ 1171ec1e82fSSascha Hauer #define C0_ADDR 0x01 1181ec1e82fSSascha Hauer #define C0_LOAD 0x02 1191ec1e82fSSascha Hauer #define C0_DUMP 0x03 1201ec1e82fSSascha Hauer #define C0_SETCTX 0x07 1211ec1e82fSSascha Hauer #define C0_GETCTX 0x03 1221ec1e82fSSascha Hauer #define C0_SETDM 0x01 1231ec1e82fSSascha Hauer #define C0_SETPM 0x04 1241ec1e82fSSascha Hauer #define C0_GETDM 0x02 1251ec1e82fSSascha Hauer #define C0_GETPM 0x08 1261ec1e82fSSascha Hauer /* 1271ec1e82fSSascha Hauer * Change endianness indicator in the BD command field 1281ec1e82fSSascha Hauer */ 1291ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS 0x80 1301ec1e82fSSascha Hauer 1311ec1e82fSSascha Hauer /* 1328391ecf4SShengjiu Wang * p_2_p watermark_level description 1338391ecf4SShengjiu Wang * Bits Name Description 1348391ecf4SShengjiu Wang * 0-7 Lower WML Lower watermark level 1358391ecf4SShengjiu Wang * 8 PS 1: Pad Swallowing 1368391ecf4SShengjiu Wang * 0: No Pad Swallowing 1378391ecf4SShengjiu Wang * 9 PA 1: Pad Adding 1388391ecf4SShengjiu Wang * 0: No Pad Adding 1398391ecf4SShengjiu Wang * 10 SPDIF If this bit is set both source 1408391ecf4SShengjiu Wang * and destination are on SPBA 1418391ecf4SShengjiu Wang * 11 Source Bit(SP) 1: Source on SPBA 1428391ecf4SShengjiu Wang * 0: Source on AIPS 1438391ecf4SShengjiu Wang * 12 Destination Bit(DP) 1: Destination on SPBA 1448391ecf4SShengjiu Wang * 0: Destination on AIPS 1458391ecf4SShengjiu Wang * 13-15 --------- MUST BE 0 1468391ecf4SShengjiu Wang * 16-23 Higher WML HWML 1478391ecf4SShengjiu Wang * 24-27 N Total number of samples after 1488391ecf4SShengjiu Wang * which Pad adding/Swallowing 1498391ecf4SShengjiu Wang * must be done. It must be odd. 1508391ecf4SShengjiu Wang * 28 Lower WML Event(LWE) SDMA events reg to check for 1518391ecf4SShengjiu Wang * LWML event mask 1528391ecf4SShengjiu Wang * 0: LWE in EVENTS register 1538391ecf4SShengjiu Wang * 1: LWE in EVENTS2 register 1548391ecf4SShengjiu Wang * 29 Higher WML Event(HWE) SDMA events reg to check for 1558391ecf4SShengjiu Wang * HWML event mask 1568391ecf4SShengjiu Wang * 0: HWE in EVENTS register 1578391ecf4SShengjiu Wang * 1: HWE in EVENTS2 register 1588391ecf4SShengjiu Wang * 30 --------- MUST BE 0 1598391ecf4SShengjiu Wang * 31 CONT 1: Amount of samples to be 1608391ecf4SShengjiu Wang * transferred is unknown and 1618391ecf4SShengjiu Wang * script will keep on 1628391ecf4SShengjiu Wang * transferring samples as long as 1638391ecf4SShengjiu Wang * both events are detected and 1648391ecf4SShengjiu Wang * script must be manually stopped 1658391ecf4SShengjiu Wang * by the application 1668391ecf4SShengjiu Wang * 0: The amount of samples to be 1678391ecf4SShengjiu Wang * transferred is equal to the 1688391ecf4SShengjiu Wang * count field of mode word 1698391ecf4SShengjiu Wang */ 1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML 0xFF 1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS BIT(8) 1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA BIT(9) 1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10) 1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP BIT(11) 1758391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP BIT(12) 1768391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16) 1778391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE BIT(28) 1788391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE BIT(29) 1798391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT BIT(31) 1808391ecf4SShengjiu Wang 181f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ 182f9d4a398SNicolin Chen BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ 183f9d4a398SNicolin Chen BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)) 184f9d4a398SNicolin Chen 185f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \ 186f9d4a398SNicolin Chen BIT(DMA_MEM_TO_DEV) | \ 187f9d4a398SNicolin Chen BIT(DMA_DEV_TO_DEV)) 188f9d4a398SNicolin Chen 1898391ecf4SShengjiu Wang /* 1901ec1e82fSSascha Hauer * Mode/Count of data node descriptors - IPCv2 1911ec1e82fSSascha Hauer */ 1921ec1e82fSSascha Hauer struct sdma_mode_count { 1931ec1e82fSSascha Hauer u32 count : 16; /* size of the buffer pointed by this BD */ 1941ec1e82fSSascha Hauer u32 status : 8; /* E,R,I,C,W,D status bits stored here */ 195e4b75760SMartin Kaiser u32 command : 8; /* command mostly used for channel 0 */ 1961ec1e82fSSascha Hauer }; 1971ec1e82fSSascha Hauer 1981ec1e82fSSascha Hauer /* 1991ec1e82fSSascha Hauer * Buffer descriptor 2001ec1e82fSSascha Hauer */ 2011ec1e82fSSascha Hauer struct sdma_buffer_descriptor { 2021ec1e82fSSascha Hauer struct sdma_mode_count mode; 2031ec1e82fSSascha Hauer u32 buffer_addr; /* address of the buffer described */ 2041ec1e82fSSascha Hauer u32 ext_buffer_addr; /* extended buffer address */ 2051ec1e82fSSascha Hauer } __attribute__ ((packed)); 2061ec1e82fSSascha Hauer 2071ec1e82fSSascha Hauer /** 2081ec1e82fSSascha Hauer * struct sdma_channel_control - Channel control Block 2091ec1e82fSSascha Hauer * 2101ec1e82fSSascha Hauer * @current_bd_ptr current buffer descriptor processed 2111ec1e82fSSascha Hauer * @base_bd_ptr first element of buffer descriptor array 2121ec1e82fSSascha Hauer * @unused padding. The SDMA engine expects an array of 128 byte 2131ec1e82fSSascha Hauer * control blocks 2141ec1e82fSSascha Hauer */ 2151ec1e82fSSascha Hauer struct sdma_channel_control { 2161ec1e82fSSascha Hauer u32 current_bd_ptr; 2171ec1e82fSSascha Hauer u32 base_bd_ptr; 2181ec1e82fSSascha Hauer u32 unused[2]; 2191ec1e82fSSascha Hauer } __attribute__ ((packed)); 2201ec1e82fSSascha Hauer 2211ec1e82fSSascha Hauer /** 2221ec1e82fSSascha Hauer * struct sdma_state_registers - SDMA context for a channel 2231ec1e82fSSascha Hauer * 2241ec1e82fSSascha Hauer * @pc: program counter 2251ec1e82fSSascha Hauer * @t: test bit: status of arithmetic & test instruction 2261ec1e82fSSascha Hauer * @rpc: return program counter 2271ec1e82fSSascha Hauer * @sf: source fault while loading data 2281ec1e82fSSascha Hauer * @spc: loop start program counter 2291ec1e82fSSascha Hauer * @df: destination fault while storing data 2301ec1e82fSSascha Hauer * @epc: loop end program counter 2311ec1e82fSSascha Hauer * @lm: loop mode 2321ec1e82fSSascha Hauer */ 2331ec1e82fSSascha Hauer struct sdma_state_registers { 2341ec1e82fSSascha Hauer u32 pc :14; 2351ec1e82fSSascha Hauer u32 unused1: 1; 2361ec1e82fSSascha Hauer u32 t : 1; 2371ec1e82fSSascha Hauer u32 rpc :14; 2381ec1e82fSSascha Hauer u32 unused0: 1; 2391ec1e82fSSascha Hauer u32 sf : 1; 2401ec1e82fSSascha Hauer u32 spc :14; 2411ec1e82fSSascha Hauer u32 unused2: 1; 2421ec1e82fSSascha Hauer u32 df : 1; 2431ec1e82fSSascha Hauer u32 epc :14; 2441ec1e82fSSascha Hauer u32 lm : 2; 2451ec1e82fSSascha Hauer } __attribute__ ((packed)); 2461ec1e82fSSascha Hauer 2471ec1e82fSSascha Hauer /** 2481ec1e82fSSascha Hauer * struct sdma_context_data - sdma context specific to a channel 2491ec1e82fSSascha Hauer * 2501ec1e82fSSascha Hauer * @channel_state: channel state bits 2511ec1e82fSSascha Hauer * @gReg: general registers 2521ec1e82fSSascha Hauer * @mda: burst dma destination address register 2531ec1e82fSSascha Hauer * @msa: burst dma source address register 2541ec1e82fSSascha Hauer * @ms: burst dma status register 2551ec1e82fSSascha Hauer * @md: burst dma data register 2561ec1e82fSSascha Hauer * @pda: peripheral dma destination address register 2571ec1e82fSSascha Hauer * @psa: peripheral dma source address register 2581ec1e82fSSascha Hauer * @ps: peripheral dma status register 2591ec1e82fSSascha Hauer * @pd: peripheral dma data register 2601ec1e82fSSascha Hauer * @ca: CRC polynomial register 2611ec1e82fSSascha Hauer * @cs: CRC accumulator register 2621ec1e82fSSascha Hauer * @dda: dedicated core destination address register 2631ec1e82fSSascha Hauer * @dsa: dedicated core source address register 2641ec1e82fSSascha Hauer * @ds: dedicated core status register 2651ec1e82fSSascha Hauer * @dd: dedicated core data register 2661ec1e82fSSascha Hauer */ 2671ec1e82fSSascha Hauer struct sdma_context_data { 2681ec1e82fSSascha Hauer struct sdma_state_registers channel_state; 2691ec1e82fSSascha Hauer u32 gReg[8]; 2701ec1e82fSSascha Hauer u32 mda; 2711ec1e82fSSascha Hauer u32 msa; 2721ec1e82fSSascha Hauer u32 ms; 2731ec1e82fSSascha Hauer u32 md; 2741ec1e82fSSascha Hauer u32 pda; 2751ec1e82fSSascha Hauer u32 psa; 2761ec1e82fSSascha Hauer u32 ps; 2771ec1e82fSSascha Hauer u32 pd; 2781ec1e82fSSascha Hauer u32 ca; 2791ec1e82fSSascha Hauer u32 cs; 2801ec1e82fSSascha Hauer u32 dda; 2811ec1e82fSSascha Hauer u32 dsa; 2821ec1e82fSSascha Hauer u32 ds; 2831ec1e82fSSascha Hauer u32 dd; 2841ec1e82fSSascha Hauer u32 scratch0; 2851ec1e82fSSascha Hauer u32 scratch1; 2861ec1e82fSSascha Hauer u32 scratch2; 2871ec1e82fSSascha Hauer u32 scratch3; 2881ec1e82fSSascha Hauer u32 scratch4; 2891ec1e82fSSascha Hauer u32 scratch5; 2901ec1e82fSSascha Hauer u32 scratch6; 2911ec1e82fSSascha Hauer u32 scratch7; 2921ec1e82fSSascha Hauer } __attribute__ ((packed)); 2931ec1e82fSSascha Hauer 2941ec1e82fSSascha Hauer #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor)) 2951ec1e82fSSascha Hauer 2961ec1e82fSSascha Hauer struct sdma_engine; 2971ec1e82fSSascha Hauer 2981ec1e82fSSascha Hauer /** 2991ec1e82fSSascha Hauer * struct sdma_channel - housekeeping for a SDMA channel 3001ec1e82fSSascha Hauer * 3011ec1e82fSSascha Hauer * @sdma pointer to the SDMA engine for this channel 30223889c63SSascha Hauer * @channel the channel number, matches dmaengine chan_id + 1 3031ec1e82fSSascha Hauer * @direction transfer type. Needed for setting SDMA script 3041ec1e82fSSascha Hauer * @peripheral_type Peripheral type. Needed for setting SDMA script 3051ec1e82fSSascha Hauer * @event_id0 aka dma request line 3061ec1e82fSSascha Hauer * @event_id1 for channels that use 2 events 3071ec1e82fSSascha Hauer * @word_size peripheral access size 3081ec1e82fSSascha Hauer * @buf_tail ID of the buffer that was processed 30985f57752SNandor Han * @buf_ptail ID of the previous buffer that was processed 3101ec1e82fSSascha Hauer * @num_bd max NUM_BD. number of descriptors currently handling 3111ec1e82fSSascha Hauer */ 3121ec1e82fSSascha Hauer struct sdma_channel { 3131ec1e82fSSascha Hauer struct sdma_engine *sdma; 3141ec1e82fSSascha Hauer unsigned int channel; 315db8196dfSVinod Koul enum dma_transfer_direction direction; 3161ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type; 3171ec1e82fSSascha Hauer unsigned int event_id0; 3181ec1e82fSSascha Hauer unsigned int event_id1; 3191ec1e82fSSascha Hauer enum dma_slave_buswidth word_size; 3201ec1e82fSSascha Hauer unsigned int buf_tail; 32185f57752SNandor Han unsigned int buf_ptail; 3221ec1e82fSSascha Hauer unsigned int num_bd; 323d1a792f3SRussell King - ARM Linux unsigned int period_len; 3241ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 3251ec1e82fSSascha Hauer dma_addr_t bd_phys; 3261ec1e82fSSascha Hauer unsigned int pc_from_device, pc_to_device; 3278391ecf4SShengjiu Wang unsigned int device_to_device; 3281ec1e82fSSascha Hauer unsigned long flags; 3298391ecf4SShengjiu Wang dma_addr_t per_address, per_address2; 3300bbc1413SRichard Zhao unsigned long event_mask[2]; 3310bbc1413SRichard Zhao unsigned long watermark_level; 3321ec1e82fSSascha Hauer u32 shp_addr, per_addr; 3331ec1e82fSSascha Hauer struct dma_chan chan; 3341ec1e82fSSascha Hauer spinlock_t lock; 3351ec1e82fSSascha Hauer struct dma_async_tx_descriptor desc; 3361ec1e82fSSascha Hauer enum dma_status status; 337ab59a510SHuang Shijie unsigned int chn_count; 338ab59a510SHuang Shijie unsigned int chn_real_count; 339abd9ccc8SHuang Shijie struct tasklet_struct tasklet; 3400b351865SNicolin Chen struct imx_dma_data data; 341*2746e2c3SThierry Bultel bool enabled; 3421ec1e82fSSascha Hauer }; 3431ec1e82fSSascha Hauer 3440bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP BIT(0) 3451ec1e82fSSascha Hauer 3461ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32 3471ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1 3481ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1 3491ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7 3501ec1e82fSSascha Hauer 3511ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453 3521ec1e82fSSascha Hauer 3531ec1e82fSSascha Hauer /** 3541ec1e82fSSascha Hauer * struct sdma_firmware_header - Layout of the firmware image 3551ec1e82fSSascha Hauer * 3561ec1e82fSSascha Hauer * @magic "SDMA" 3571ec1e82fSSascha Hauer * @version_major increased whenever layout of struct sdma_script_start_addrs 3581ec1e82fSSascha Hauer * changes. 3591ec1e82fSSascha Hauer * @version_minor firmware minor version (for binary compatible changes) 3601ec1e82fSSascha Hauer * @script_addrs_start offset of struct sdma_script_start_addrs in this image 3611ec1e82fSSascha Hauer * @num_script_addrs Number of script addresses in this image 3621ec1e82fSSascha Hauer * @ram_code_start offset of SDMA ram image in this firmware image 3631ec1e82fSSascha Hauer * @ram_code_size size of SDMA ram image 3641ec1e82fSSascha Hauer * @script_addrs Stores the start address of the SDMA scripts 3651ec1e82fSSascha Hauer * (in SDMA memory space) 3661ec1e82fSSascha Hauer */ 3671ec1e82fSSascha Hauer struct sdma_firmware_header { 3681ec1e82fSSascha Hauer u32 magic; 3691ec1e82fSSascha Hauer u32 version_major; 3701ec1e82fSSascha Hauer u32 version_minor; 3711ec1e82fSSascha Hauer u32 script_addrs_start; 3721ec1e82fSSascha Hauer u32 num_script_addrs; 3731ec1e82fSSascha Hauer u32 ram_code_start; 3741ec1e82fSSascha Hauer u32 ram_code_size; 3751ec1e82fSSascha Hauer }; 3761ec1e82fSSascha Hauer 37717bba72fSSascha Hauer struct sdma_driver_data { 37817bba72fSSascha Hauer int chnenbl0; 37917bba72fSSascha Hauer int num_events; 380dcfec3c0SSascha Hauer struct sdma_script_start_addrs *script_addrs; 38162550cd7SShawn Guo }; 38262550cd7SShawn Guo 3831ec1e82fSSascha Hauer struct sdma_engine { 3841ec1e82fSSascha Hauer struct device *dev; 385b9b3f82fSSascha Hauer struct device_dma_parameters dma_parms; 3861ec1e82fSSascha Hauer struct sdma_channel channel[MAX_DMA_CHANNELS]; 3871ec1e82fSSascha Hauer struct sdma_channel_control *channel_control; 3881ec1e82fSSascha Hauer void __iomem *regs; 3891ec1e82fSSascha Hauer struct sdma_context_data *context; 3901ec1e82fSSascha Hauer dma_addr_t context_phys; 3911ec1e82fSSascha Hauer struct dma_device dma_device; 3927560e3f3SSascha Hauer struct clk *clk_ipg; 3937560e3f3SSascha Hauer struct clk *clk_ahb; 3942ccaef05SRichard Zhao spinlock_t channel_0_lock; 395cd72b846SNicolin Chen u32 script_number; 3961ec1e82fSSascha Hauer struct sdma_script_start_addrs *script_addrs; 39717bba72fSSascha Hauer const struct sdma_driver_data *drvdata; 3988391ecf4SShengjiu Wang u32 spba_start_addr; 3998391ecf4SShengjiu Wang u32 spba_end_addr; 4005bb9dbb5SVinod Koul unsigned int irq; 40117bba72fSSascha Hauer }; 40217bba72fSSascha Hauer 403e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = { 40417bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX31, 40517bba72fSSascha Hauer .num_events = 32, 40617bba72fSSascha Hauer }; 40717bba72fSSascha Hauer 408dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = { 409dcfec3c0SSascha Hauer .ap_2_ap_addr = 729, 410dcfec3c0SSascha Hauer .uart_2_mcu_addr = 904, 411dcfec3c0SSascha Hauer .per_2_app_addr = 1255, 412dcfec3c0SSascha Hauer .mcu_2_app_addr = 834, 413dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1120, 414dcfec3c0SSascha Hauer .per_2_shp_addr = 1329, 415dcfec3c0SSascha Hauer .mcu_2_shp_addr = 1048, 416dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1560, 417dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1479, 418dcfec3c0SSascha Hauer .app_2_per_addr = 1189, 419dcfec3c0SSascha Hauer .app_2_mcu_addr = 770, 420dcfec3c0SSascha Hauer .shp_2_per_addr = 1407, 421dcfec3c0SSascha Hauer .shp_2_mcu_addr = 979, 422dcfec3c0SSascha Hauer }; 423dcfec3c0SSascha Hauer 424e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = { 425dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 426dcfec3c0SSascha Hauer .num_events = 48, 427dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx25, 428dcfec3c0SSascha Hauer }; 429dcfec3c0SSascha Hauer 430e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = { 43117bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 43217bba72fSSascha Hauer .num_events = 48, 4331ec1e82fSSascha Hauer }; 4341ec1e82fSSascha Hauer 435dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = { 436dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 437dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 438dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 439dcfec3c0SSascha Hauer .mcu_2_shp_addr = 961, 440dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1473, 441dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1392, 442dcfec3c0SSascha Hauer .app_2_per_addr = 1033, 443dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 444dcfec3c0SSascha Hauer .shp_2_per_addr = 1251, 445dcfec3c0SSascha Hauer .shp_2_mcu_addr = 892, 446dcfec3c0SSascha Hauer }; 447dcfec3c0SSascha Hauer 448e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = { 449dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 450dcfec3c0SSascha Hauer .num_events = 48, 451dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx51, 452dcfec3c0SSascha Hauer }; 453dcfec3c0SSascha Hauer 454dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = { 455dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 456dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 457dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 458dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 459dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 460dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 461dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 462dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 463dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 464dcfec3c0SSascha Hauer .firi_2_mcu_addr = 1193, 465dcfec3c0SSascha Hauer .mcu_2_firi_addr = 1290, 466dcfec3c0SSascha Hauer }; 467dcfec3c0SSascha Hauer 468e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = { 469dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 470dcfec3c0SSascha Hauer .num_events = 48, 471dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx53, 472dcfec3c0SSascha Hauer }; 473dcfec3c0SSascha Hauer 474dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = { 475dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 476dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 477dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 478dcfec3c0SSascha Hauer .per_2_per_addr = 6331, 479dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 480dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 481dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 482dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 483dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 484dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 485dcfec3c0SSascha Hauer }; 486dcfec3c0SSascha Hauer 487e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = { 488dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 489dcfec3c0SSascha Hauer .num_events = 48, 490dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx6q, 491dcfec3c0SSascha Hauer }; 492dcfec3c0SSascha Hauer 493b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = { 494b7d2648aSFabio Estevam .ap_2_ap_addr = 644, 495b7d2648aSFabio Estevam .uart_2_mcu_addr = 819, 496b7d2648aSFabio Estevam .mcu_2_app_addr = 749, 497b7d2648aSFabio Estevam .uartsh_2_mcu_addr = 1034, 498b7d2648aSFabio Estevam .mcu_2_shp_addr = 962, 499b7d2648aSFabio Estevam .app_2_mcu_addr = 685, 500b7d2648aSFabio Estevam .shp_2_mcu_addr = 893, 501b7d2648aSFabio Estevam .spdif_2_mcu_addr = 1102, 502b7d2648aSFabio Estevam .mcu_2_spdif_addr = 1136, 503b7d2648aSFabio Estevam }; 504b7d2648aSFabio Estevam 505b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = { 506b7d2648aSFabio Estevam .chnenbl0 = SDMA_CHNENBL0_IMX35, 507b7d2648aSFabio Estevam .num_events = 48, 508b7d2648aSFabio Estevam .script_addrs = &sdma_script_imx7d, 509b7d2648aSFabio Estevam }; 510b7d2648aSFabio Estevam 511afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = { 51262550cd7SShawn Guo { 513dcfec3c0SSascha Hauer .name = "imx25-sdma", 514dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx25, 515dcfec3c0SSascha Hauer }, { 51662550cd7SShawn Guo .name = "imx31-sdma", 51717bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx31, 51862550cd7SShawn Guo }, { 51962550cd7SShawn Guo .name = "imx35-sdma", 52017bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx35, 52162550cd7SShawn Guo }, { 522dcfec3c0SSascha Hauer .name = "imx51-sdma", 523dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx51, 524dcfec3c0SSascha Hauer }, { 525dcfec3c0SSascha Hauer .name = "imx53-sdma", 526dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx53, 527dcfec3c0SSascha Hauer }, { 528dcfec3c0SSascha Hauer .name = "imx6q-sdma", 529dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx6q, 530dcfec3c0SSascha Hauer }, { 531b7d2648aSFabio Estevam .name = "imx7d-sdma", 532b7d2648aSFabio Estevam .driver_data = (unsigned long)&sdma_imx7d, 533b7d2648aSFabio Estevam }, { 53462550cd7SShawn Guo /* sentinel */ 53562550cd7SShawn Guo } 53662550cd7SShawn Guo }; 53762550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes); 53862550cd7SShawn Guo 539580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = { 540dcfec3c0SSascha Hauer { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, }, 541dcfec3c0SSascha Hauer { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, }, 542dcfec3c0SSascha Hauer { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, }, 54317bba72fSSascha Hauer { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, }, 544dcfec3c0SSascha Hauer { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, }, 54563edea16SMarkus Pargmann { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, }, 546b7d2648aSFabio Estevam { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, }, 547580975d7SShawn Guo { /* sentinel */ } 548580975d7SShawn Guo }; 549580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids); 550580975d7SShawn Guo 5510bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */ 5520bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */ 5530bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */ 5541ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 5551ec1e82fSSascha Hauer 5561ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 5571ec1e82fSSascha Hauer { 55817bba72fSSascha Hauer u32 chnenbl0 = sdma->drvdata->chnenbl0; 5591ec1e82fSSascha Hauer return chnenbl0 + event * 4; 5601ec1e82fSSascha Hauer } 5611ec1e82fSSascha Hauer 5621ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 5631ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 5641ec1e82fSSascha Hauer { 5651ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 5661ec1e82fSSascha Hauer int channel = sdmac->channel; 5670bbc1413SRichard Zhao unsigned long evt, mcu, dsp; 5681ec1e82fSSascha Hauer 5691ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 5701ec1e82fSSascha Hauer return -EINVAL; 5711ec1e82fSSascha Hauer 572c4b56857SRichard Zhao evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); 573c4b56857SRichard Zhao mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); 574c4b56857SRichard Zhao dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); 5751ec1e82fSSascha Hauer 5761ec1e82fSSascha Hauer if (dsp_override) 5770bbc1413SRichard Zhao __clear_bit(channel, &dsp); 5781ec1e82fSSascha Hauer else 5790bbc1413SRichard Zhao __set_bit(channel, &dsp); 5801ec1e82fSSascha Hauer 5811ec1e82fSSascha Hauer if (event_override) 5820bbc1413SRichard Zhao __clear_bit(channel, &evt); 5831ec1e82fSSascha Hauer else 5840bbc1413SRichard Zhao __set_bit(channel, &evt); 5851ec1e82fSSascha Hauer 5861ec1e82fSSascha Hauer if (mcu_override) 5870bbc1413SRichard Zhao __clear_bit(channel, &mcu); 5881ec1e82fSSascha Hauer else 5890bbc1413SRichard Zhao __set_bit(channel, &mcu); 5901ec1e82fSSascha Hauer 591c4b56857SRichard Zhao writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); 592c4b56857SRichard Zhao writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); 593c4b56857SRichard Zhao writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); 5941ec1e82fSSascha Hauer 5951ec1e82fSSascha Hauer return 0; 5961ec1e82fSSascha Hauer } 5971ec1e82fSSascha Hauer 598b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 599b9a59166SRichard Zhao { 600*2746e2c3SThierry Bultel unsigned long flags; 601*2746e2c3SThierry Bultel struct sdma_channel *sdmac = &sdma->channel[channel]; 602*2746e2c3SThierry Bultel 6030bbc1413SRichard Zhao writel(BIT(channel), sdma->regs + SDMA_H_START); 604*2746e2c3SThierry Bultel 605*2746e2c3SThierry Bultel spin_lock_irqsave(&sdmac->lock, flags); 606*2746e2c3SThierry Bultel sdmac->enabled = true; 607*2746e2c3SThierry Bultel spin_unlock_irqrestore(&sdmac->lock, flags); 608b9a59166SRichard Zhao } 609b9a59166SRichard Zhao 6101ec1e82fSSascha Hauer /* 6112ccaef05SRichard Zhao * sdma_run_channel0 - run a channel and wait till it's done 6121ec1e82fSSascha Hauer */ 6132ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma) 6141ec1e82fSSascha Hauer { 6151ec1e82fSSascha Hauer int ret; 6161d069bfaSMichael Olbrich u32 reg; 6171ec1e82fSSascha Hauer 6182ccaef05SRichard Zhao sdma_enable_channel(sdma, 0); 6191ec1e82fSSascha Hauer 6201d069bfaSMichael Olbrich ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP, 6211d069bfaSMichael Olbrich reg, !(reg & 1), 1, 500); 6221d069bfaSMichael Olbrich if (ret) 6232ccaef05SRichard Zhao dev_err(sdma->dev, "Timeout waiting for CH0 ready\n"); 6241ec1e82fSSascha Hauer 625855832e4SRobin Gong /* Set bits of CONFIG register with dynamic context switching */ 626855832e4SRobin Gong if (readl(sdma->regs + SDMA_H_CONFIG) == 0) 627855832e4SRobin Gong writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG); 628855832e4SRobin Gong 6291d069bfaSMichael Olbrich return ret; 6301ec1e82fSSascha Hauer } 6311ec1e82fSSascha Hauer 6321ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 6331ec1e82fSSascha Hauer u32 address) 6341ec1e82fSSascha Hauer { 6351ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 6361ec1e82fSSascha Hauer void *buf_virt; 6371ec1e82fSSascha Hauer dma_addr_t buf_phys; 6381ec1e82fSSascha Hauer int ret; 6392ccaef05SRichard Zhao unsigned long flags; 64073eab978SSascha Hauer 6411ec1e82fSSascha Hauer buf_virt = dma_alloc_coherent(NULL, 6421ec1e82fSSascha Hauer size, 6431ec1e82fSSascha Hauer &buf_phys, GFP_KERNEL); 64473eab978SSascha Hauer if (!buf_virt) { 6452ccaef05SRichard Zhao return -ENOMEM; 64673eab978SSascha Hauer } 6471ec1e82fSSascha Hauer 6482ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 6492ccaef05SRichard Zhao 6501ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 6511ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 6521ec1e82fSSascha Hauer bd0->mode.count = size / 2; 6531ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 6541ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 6551ec1e82fSSascha Hauer 6561ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 6571ec1e82fSSascha Hauer 6582ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 6592ccaef05SRichard Zhao 6602ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 6611ec1e82fSSascha Hauer 6621ec1e82fSSascha Hauer dma_free_coherent(NULL, size, buf_virt, buf_phys); 6631ec1e82fSSascha Hauer 6641ec1e82fSSascha Hauer return ret; 6651ec1e82fSSascha Hauer } 6661ec1e82fSSascha Hauer 6671ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 6681ec1e82fSSascha Hauer { 6691ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6701ec1e82fSSascha Hauer int channel = sdmac->channel; 6710bbc1413SRichard Zhao unsigned long val; 6721ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 6731ec1e82fSSascha Hauer 674c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 6750bbc1413SRichard Zhao __set_bit(channel, &val); 676c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 6771ec1e82fSSascha Hauer } 6781ec1e82fSSascha Hauer 6791ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 6801ec1e82fSSascha Hauer { 6811ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6821ec1e82fSSascha Hauer int channel = sdmac->channel; 6831ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 6840bbc1413SRichard Zhao unsigned long val; 6851ec1e82fSSascha Hauer 686c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 6870bbc1413SRichard Zhao __clear_bit(channel, &val); 688c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 6891ec1e82fSSascha Hauer } 6901ec1e82fSSascha Hauer 691d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac) 692d1a792f3SRussell King - ARM Linux { 6931ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 6945881826dSNandor Han int error = 0; 6955881826dSNandor Han enum dma_status old_status = sdmac->status; 696*2746e2c3SThierry Bultel unsigned long flags; 697*2746e2c3SThierry Bultel 698*2746e2c3SThierry Bultel spin_lock_irqsave(&sdmac->lock, flags); 699*2746e2c3SThierry Bultel if (!sdmac->enabled) { 700*2746e2c3SThierry Bultel spin_unlock_irqrestore(&sdmac->lock, flags); 701*2746e2c3SThierry Bultel return; 702*2746e2c3SThierry Bultel } 703*2746e2c3SThierry Bultel spin_unlock_irqrestore(&sdmac->lock, flags); 7041ec1e82fSSascha Hauer 7051ec1e82fSSascha Hauer /* 7061ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 7071ec1e82fSSascha Hauer * call callback function. 7081ec1e82fSSascha Hauer */ 7091ec1e82fSSascha Hauer while (1) { 7101ec1e82fSSascha Hauer bd = &sdmac->bd[sdmac->buf_tail]; 7111ec1e82fSSascha Hauer 7121ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 7131ec1e82fSSascha Hauer break; 7141ec1e82fSSascha Hauer 7155881826dSNandor Han if (bd->mode.status & BD_RROR) { 7165881826dSNandor Han bd->mode.status &= ~BD_RROR; 7171ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 7185881826dSNandor Han error = -EIO; 7195881826dSNandor Han } 7201ec1e82fSSascha Hauer 7215881826dSNandor Han /* 7225881826dSNandor Han * We use bd->mode.count to calculate the residue, since contains 7235881826dSNandor Han * the number of bytes present in the current buffer descriptor. 7245881826dSNandor Han */ 7255881826dSNandor Han 7265881826dSNandor Han sdmac->chn_real_count = bd->mode.count; 7271ec1e82fSSascha Hauer bd->mode.status |= BD_DONE; 7285881826dSNandor Han bd->mode.count = sdmac->period_len; 72985f57752SNandor Han sdmac->buf_ptail = sdmac->buf_tail; 73085f57752SNandor Han sdmac->buf_tail = (sdmac->buf_tail + 1) % sdmac->num_bd; 73115f30f51SNandor Han 73215f30f51SNandor Han /* 73315f30f51SNandor Han * The callback is called from the interrupt context in order 73415f30f51SNandor Han * to reduce latency and to avoid the risk of altering the 73515f30f51SNandor Han * SDMA transaction status by the time the client tasklet is 73615f30f51SNandor Han * executed. 73715f30f51SNandor Han */ 73815f30f51SNandor Han 739553911c6SLinus Torvalds dmaengine_desc_get_callback_invoke(&sdmac->desc, NULL); 74015f30f51SNandor Han 7415881826dSNandor Han if (error) 7425881826dSNandor Han sdmac->status = old_status; 7431ec1e82fSSascha Hauer } 7441ec1e82fSSascha Hauer } 7451ec1e82fSSascha Hauer 74615f30f51SNandor Han static void mxc_sdma_handle_channel_normal(unsigned long data) 7471ec1e82fSSascha Hauer { 74815f30f51SNandor Han struct sdma_channel *sdmac = (struct sdma_channel *) data; 7491ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 7501ec1e82fSSascha Hauer int i, error = 0; 7511ec1e82fSSascha Hauer 752ab59a510SHuang Shijie sdmac->chn_real_count = 0; 7531ec1e82fSSascha Hauer /* 7541ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 7551ec1e82fSSascha Hauer * errors and call callback function 7561ec1e82fSSascha Hauer */ 7571ec1e82fSSascha Hauer for (i = 0; i < sdmac->num_bd; i++) { 7581ec1e82fSSascha Hauer bd = &sdmac->bd[i]; 7591ec1e82fSSascha Hauer 7601ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 7611ec1e82fSSascha Hauer error = -EIO; 762ab59a510SHuang Shijie sdmac->chn_real_count += bd->mode.count; 7631ec1e82fSSascha Hauer } 7641ec1e82fSSascha Hauer 7651ec1e82fSSascha Hauer if (error) 7661ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 7671ec1e82fSSascha Hauer else 768409bff6aSVinod Koul sdmac->status = DMA_COMPLETE; 7691ec1e82fSSascha Hauer 770f7fbce07SRussell King - ARM Linux dma_cookie_complete(&sdmac->desc); 77148dc77e2SDave Jiang 77248dc77e2SDave Jiang dmaengine_desc_get_callback_invoke(&sdmac->desc, NULL); 7731ec1e82fSSascha Hauer } 7741ec1e82fSSascha Hauer 7751ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 7761ec1e82fSSascha Hauer { 7771ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 7780bbc1413SRichard Zhao unsigned long stat; 7791ec1e82fSSascha Hauer 780c4b56857SRichard Zhao stat = readl_relaxed(sdma->regs + SDMA_H_INTR); 781c4b56857SRichard Zhao writel_relaxed(stat, sdma->regs + SDMA_H_INTR); 7821d069bfaSMichael Olbrich /* channel 0 is special and not handled here, see run_channel0() */ 7831d069bfaSMichael Olbrich stat &= ~1; 7841ec1e82fSSascha Hauer 7851ec1e82fSSascha Hauer while (stat) { 7861ec1e82fSSascha Hauer int channel = fls(stat) - 1; 7871ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 7881ec1e82fSSascha Hauer 789d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 790d1a792f3SRussell King - ARM Linux sdma_update_channel_loop(sdmac); 79115f30f51SNandor Han else 792abd9ccc8SHuang Shijie tasklet_schedule(&sdmac->tasklet); 7931ec1e82fSSascha Hauer 7940bbc1413SRichard Zhao __clear_bit(channel, &stat); 7951ec1e82fSSascha Hauer } 7961ec1e82fSSascha Hauer 7971ec1e82fSSascha Hauer return IRQ_HANDLED; 7981ec1e82fSSascha Hauer } 7991ec1e82fSSascha Hauer 8001ec1e82fSSascha Hauer /* 8011ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 8021ec1e82fSSascha Hauer */ 8031ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac, 8041ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 8051ec1e82fSSascha Hauer { 8061ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8071ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 8081ec1e82fSSascha Hauer /* 8091ec1e82fSSascha Hauer * These are needed once we start to support transfers between 8101ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 8111ec1e82fSSascha Hauer */ 8120d605ba0SVinod Koul int per_2_per = 0; 8131ec1e82fSSascha Hauer 8141ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 8151ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 8168391ecf4SShengjiu Wang sdmac->device_to_device = 0; 8171ec1e82fSSascha Hauer 8181ec1e82fSSascha Hauer switch (peripheral_type) { 8191ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 8201ec1e82fSSascha Hauer break; 8211ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 8221ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 8231ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 8241ec1e82fSSascha Hauer break; 8251ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 8261ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 8271ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 8281ec1e82fSSascha Hauer break; 8291ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 8301ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 8311ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 8321ec1e82fSSascha Hauer break; 8331ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 8341ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 8351ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 8361ec1e82fSSascha Hauer break; 8371ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 8381ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 8391ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 8401ec1e82fSSascha Hauer break; 8411ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 8421ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 8431ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 84429aebfdeSNicolin Chen case IMX_DMATYPE_SAI: 8451ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 8461ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 8471ec1e82fSSascha Hauer break; 8481a895578SNicolin Chen case IMX_DMATYPE_SSI_DUAL: 8491a895578SNicolin Chen per_2_emi = sdma->script_addrs->ssish_2_mcu_addr; 8501a895578SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_ssish_addr; 8511a895578SNicolin Chen break; 8521ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 8531ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 8541ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 8551ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 8561ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 8571ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 8581ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 8591ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 8601ec1e82fSSascha Hauer break; 8611ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 8621ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 8631ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 8641ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 8651ec1e82fSSascha Hauer break; 866f892afb0SNicolin Chen case IMX_DMATYPE_ASRC_SP: 867f892afb0SNicolin Chen per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 868f892afb0SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 869f892afb0SNicolin Chen per_2_per = sdma->script_addrs->per_2_per_addr; 870f892afb0SNicolin Chen break; 8711ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 8721ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 8731ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 8741ec1e82fSSascha Hauer break; 8751ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 8761ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 8771ec1e82fSSascha Hauer break; 8781ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 8791ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 8801ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 8811ec1e82fSSascha Hauer break; 8821ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 8831ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 8841ec1e82fSSascha Hauer break; 8851ec1e82fSSascha Hauer default: 8861ec1e82fSSascha Hauer break; 8871ec1e82fSSascha Hauer } 8881ec1e82fSSascha Hauer 8891ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 8901ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 8918391ecf4SShengjiu Wang sdmac->device_to_device = per_2_per; 8921ec1e82fSSascha Hauer } 8931ec1e82fSSascha Hauer 8941ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 8951ec1e82fSSascha Hauer { 8961ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8971ec1e82fSSascha Hauer int channel = sdmac->channel; 8981ec1e82fSSascha Hauer int load_address; 8991ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 9001ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 9011ec1e82fSSascha Hauer int ret; 9022ccaef05SRichard Zhao unsigned long flags; 9031ec1e82fSSascha Hauer 9048391ecf4SShengjiu Wang if (sdmac->direction == DMA_DEV_TO_MEM) 9051ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 9068391ecf4SShengjiu Wang else if (sdmac->direction == DMA_DEV_TO_DEV) 9078391ecf4SShengjiu Wang load_address = sdmac->device_to_device; 9088391ecf4SShengjiu Wang else 9091ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 9101ec1e82fSSascha Hauer 9111ec1e82fSSascha Hauer if (load_address < 0) 9121ec1e82fSSascha Hauer return load_address; 9131ec1e82fSSascha Hauer 9141ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 9150bbc1413SRichard Zhao dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level); 9161ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 9171ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 9180bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]); 9190bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]); 9201ec1e82fSSascha Hauer 9212ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 92273eab978SSascha Hauer 9231ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 9241ec1e82fSSascha Hauer context->channel_state.pc = load_address; 9251ec1e82fSSascha Hauer 9261ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 9271ec1e82fSSascha Hauer * and watermark level 9281ec1e82fSSascha Hauer */ 9290bbc1413SRichard Zhao context->gReg[0] = sdmac->event_mask[1]; 9300bbc1413SRichard Zhao context->gReg[1] = sdmac->event_mask[0]; 9311ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 9321ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 9331ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 9341ec1e82fSSascha Hauer 9351ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 9361ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 9371ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 9381ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 9391ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 9402ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 9411ec1e82fSSascha Hauer 9422ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 94373eab978SSascha Hauer 9441ec1e82fSSascha Hauer return ret; 9451ec1e82fSSascha Hauer } 9461ec1e82fSSascha Hauer 9477b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 9481ec1e82fSSascha Hauer { 9497b350ab0SMaxime Ripard return container_of(chan, struct sdma_channel, chan); 9507b350ab0SMaxime Ripard } 9517b350ab0SMaxime Ripard 9527b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan) 9537b350ab0SMaxime Ripard { 9547b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 9551ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9561ec1e82fSSascha Hauer int channel = sdmac->channel; 957*2746e2c3SThierry Bultel unsigned long flags; 9581ec1e82fSSascha Hauer 9590bbc1413SRichard Zhao writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP); 9601ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 9617b350ab0SMaxime Ripard 962*2746e2c3SThierry Bultel spin_lock_irqsave(&sdmac->lock, flags); 963*2746e2c3SThierry Bultel sdmac->enabled = false; 964*2746e2c3SThierry Bultel spin_unlock_irqrestore(&sdmac->lock, flags); 965*2746e2c3SThierry Bultel 9667b350ab0SMaxime Ripard return 0; 9671ec1e82fSSascha Hauer } 9681ec1e82fSSascha Hauer 9697f3ff14bSJiada Wang static int sdma_disable_channel_with_delay(struct dma_chan *chan) 9707f3ff14bSJiada Wang { 9717f3ff14bSJiada Wang sdma_disable_channel(chan); 9727f3ff14bSJiada Wang 9737f3ff14bSJiada Wang /* 9747f3ff14bSJiada Wang * According to NXP R&D team a delay of one BD SDMA cost time 9757f3ff14bSJiada Wang * (maximum is 1ms) should be added after disable of the channel 9767f3ff14bSJiada Wang * bit, to ensure SDMA core has really been stopped after SDMA 9777f3ff14bSJiada Wang * clients call .device_terminate_all. 9787f3ff14bSJiada Wang */ 9797f3ff14bSJiada Wang mdelay(1); 9807f3ff14bSJiada Wang 9817f3ff14bSJiada Wang return 0; 9827f3ff14bSJiada Wang } 9837f3ff14bSJiada Wang 9848391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac) 9858391ecf4SShengjiu Wang { 9868391ecf4SShengjiu Wang struct sdma_engine *sdma = sdmac->sdma; 9878391ecf4SShengjiu Wang 9888391ecf4SShengjiu Wang int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML; 9898391ecf4SShengjiu Wang int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16; 9908391ecf4SShengjiu Wang 9918391ecf4SShengjiu Wang set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]); 9928391ecf4SShengjiu Wang set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]); 9938391ecf4SShengjiu Wang 9948391ecf4SShengjiu Wang if (sdmac->event_id0 > 31) 9958391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE; 9968391ecf4SShengjiu Wang 9978391ecf4SShengjiu Wang if (sdmac->event_id1 > 31) 9988391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE; 9998391ecf4SShengjiu Wang 10008391ecf4SShengjiu Wang /* 10018391ecf4SShengjiu Wang * If LWML(src_maxburst) > HWML(dst_maxburst), we need 10028391ecf4SShengjiu Wang * swap LWML and HWML of INFO(A.3.2.5.1), also need swap 10038391ecf4SShengjiu Wang * r0(event_mask[1]) and r1(event_mask[0]). 10048391ecf4SShengjiu Wang */ 10058391ecf4SShengjiu Wang if (lwml > hwml) { 10068391ecf4SShengjiu Wang sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML | 10078391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML); 10088391ecf4SShengjiu Wang sdmac->watermark_level |= hwml; 10098391ecf4SShengjiu Wang sdmac->watermark_level |= lwml << 16; 10108391ecf4SShengjiu Wang swap(sdmac->event_mask[0], sdmac->event_mask[1]); 10118391ecf4SShengjiu Wang } 10128391ecf4SShengjiu Wang 10138391ecf4SShengjiu Wang if (sdmac->per_address2 >= sdma->spba_start_addr && 10148391ecf4SShengjiu Wang sdmac->per_address2 <= sdma->spba_end_addr) 10158391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP; 10168391ecf4SShengjiu Wang 10178391ecf4SShengjiu Wang if (sdmac->per_address >= sdma->spba_start_addr && 10188391ecf4SShengjiu Wang sdmac->per_address <= sdma->spba_end_addr) 10198391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP; 10208391ecf4SShengjiu Wang 10218391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT; 10228391ecf4SShengjiu Wang } 10238391ecf4SShengjiu Wang 10247b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan) 10251ec1e82fSSascha Hauer { 10267b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 10271ec1e82fSSascha Hauer int ret; 10281ec1e82fSSascha Hauer 10297b350ab0SMaxime Ripard sdma_disable_channel(chan); 10301ec1e82fSSascha Hauer 10310bbc1413SRichard Zhao sdmac->event_mask[0] = 0; 10320bbc1413SRichard Zhao sdmac->event_mask[1] = 0; 10331ec1e82fSSascha Hauer sdmac->shp_addr = 0; 10341ec1e82fSSascha Hauer sdmac->per_addr = 0; 10351ec1e82fSSascha Hauer 10361ec1e82fSSascha Hauer if (sdmac->event_id0) { 103717bba72fSSascha Hauer if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events) 10381ec1e82fSSascha Hauer return -EINVAL; 10391ec1e82fSSascha Hauer sdma_event_enable(sdmac, sdmac->event_id0); 10401ec1e82fSSascha Hauer } 10411ec1e82fSSascha Hauer 10428391ecf4SShengjiu Wang if (sdmac->event_id1) { 10438391ecf4SShengjiu Wang if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events) 10448391ecf4SShengjiu Wang return -EINVAL; 10458391ecf4SShengjiu Wang sdma_event_enable(sdmac, sdmac->event_id1); 10468391ecf4SShengjiu Wang } 10478391ecf4SShengjiu Wang 10481ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 10491ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 10501ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 10511ec1e82fSSascha Hauer break; 10521ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 10531ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 10541ec1e82fSSascha Hauer break; 10551ec1e82fSSascha Hauer default: 10561ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 10571ec1e82fSSascha Hauer break; 10581ec1e82fSSascha Hauer } 10591ec1e82fSSascha Hauer 10601ec1e82fSSascha Hauer sdma_get_pc(sdmac, sdmac->peripheral_type); 10611ec1e82fSSascha Hauer 10621ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 10631ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 10641ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 10651ec1e82fSSascha Hauer if (sdmac->event_id1) { 10668391ecf4SShengjiu Wang if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP || 10678391ecf4SShengjiu Wang sdmac->peripheral_type == IMX_DMATYPE_ASRC) 10688391ecf4SShengjiu Wang sdma_set_watermarklevel_for_p2p(sdmac); 10698391ecf4SShengjiu Wang } else 10700bbc1413SRichard Zhao __set_bit(sdmac->event_id0, sdmac->event_mask); 10718391ecf4SShengjiu Wang 10721ec1e82fSSascha Hauer /* Address */ 10731ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 10748391ecf4SShengjiu Wang sdmac->per_addr = sdmac->per_address2; 10751ec1e82fSSascha Hauer } else { 10761ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 10771ec1e82fSSascha Hauer } 10781ec1e82fSSascha Hauer 10791ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 10801ec1e82fSSascha Hauer 10811ec1e82fSSascha Hauer return ret; 10821ec1e82fSSascha Hauer } 10831ec1e82fSSascha Hauer 10841ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 10851ec1e82fSSascha Hauer unsigned int priority) 10861ec1e82fSSascha Hauer { 10871ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10881ec1e82fSSascha Hauer int channel = sdmac->channel; 10891ec1e82fSSascha Hauer 10901ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 10911ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 10921ec1e82fSSascha Hauer return -EINVAL; 10931ec1e82fSSascha Hauer } 10941ec1e82fSSascha Hauer 1095c4b56857SRichard Zhao writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 10961ec1e82fSSascha Hauer 10971ec1e82fSSascha Hauer return 0; 10981ec1e82fSSascha Hauer } 10991ec1e82fSSascha Hauer 11001ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac) 11011ec1e82fSSascha Hauer { 11021ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11031ec1e82fSSascha Hauer int channel = sdmac->channel; 11041ec1e82fSSascha Hauer int ret = -EBUSY; 11051ec1e82fSSascha Hauer 11069f92d223SJoe Perches sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, 11079f92d223SJoe Perches GFP_KERNEL); 11081ec1e82fSSascha Hauer if (!sdmac->bd) { 11091ec1e82fSSascha Hauer ret = -ENOMEM; 11101ec1e82fSSascha Hauer goto out; 11111ec1e82fSSascha Hauer } 11121ec1e82fSSascha Hauer 11131ec1e82fSSascha Hauer sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys; 11141ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 11151ec1e82fSSascha Hauer 11161ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY); 11171ec1e82fSSascha Hauer return 0; 11181ec1e82fSSascha Hauer out: 11191ec1e82fSSascha Hauer 11201ec1e82fSSascha Hauer return ret; 11211ec1e82fSSascha Hauer } 11221ec1e82fSSascha Hauer 11231ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx) 11241ec1e82fSSascha Hauer { 1125f69f2e26SHaitao Zhang unsigned long flags; 11261ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(tx->chan); 11271ec1e82fSSascha Hauer dma_cookie_t cookie; 11281ec1e82fSSascha Hauer 1129f69f2e26SHaitao Zhang spin_lock_irqsave(&sdmac->lock, flags); 11301ec1e82fSSascha Hauer 1131884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 11321ec1e82fSSascha Hauer 1133f69f2e26SHaitao Zhang spin_unlock_irqrestore(&sdmac->lock, flags); 11341ec1e82fSSascha Hauer 11351ec1e82fSSascha Hauer return cookie; 11361ec1e82fSSascha Hauer } 11371ec1e82fSSascha Hauer 11381ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 11391ec1e82fSSascha Hauer { 11401ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11411ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 11421ec1e82fSSascha Hauer int prio, ret; 11431ec1e82fSSascha Hauer 11441ec1e82fSSascha Hauer if (!data) 11451ec1e82fSSascha Hauer return -EINVAL; 11461ec1e82fSSascha Hauer 11471ec1e82fSSascha Hauer switch (data->priority) { 11481ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 11491ec1e82fSSascha Hauer prio = 3; 11501ec1e82fSSascha Hauer break; 11511ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 11521ec1e82fSSascha Hauer prio = 2; 11531ec1e82fSSascha Hauer break; 11541ec1e82fSSascha Hauer case DMA_PRIO_LOW: 11551ec1e82fSSascha Hauer default: 11561ec1e82fSSascha Hauer prio = 1; 11571ec1e82fSSascha Hauer break; 11581ec1e82fSSascha Hauer } 11591ec1e82fSSascha Hauer 11601ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 11611ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 11628391ecf4SShengjiu Wang sdmac->event_id1 = data->dma_request2; 1163c2c744d3SRichard Zhao 1164b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ipg); 1165b93edcddSFabio Estevam if (ret) 1166b93edcddSFabio Estevam return ret; 1167b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ahb); 1168b93edcddSFabio Estevam if (ret) 1169b93edcddSFabio Estevam goto disable_clk_ipg; 1170c2c744d3SRichard Zhao 11713bb5e7caSRichard Zhao ret = sdma_request_channel(sdmac); 11721ec1e82fSSascha Hauer if (ret) 1173b93edcddSFabio Estevam goto disable_clk_ahb; 11741ec1e82fSSascha Hauer 11753bb5e7caSRichard Zhao ret = sdma_set_channel_priority(sdmac, prio); 11761ec1e82fSSascha Hauer if (ret) 1177b93edcddSFabio Estevam goto disable_clk_ahb; 11781ec1e82fSSascha Hauer 11791ec1e82fSSascha Hauer dma_async_tx_descriptor_init(&sdmac->desc, chan); 11801ec1e82fSSascha Hauer sdmac->desc.tx_submit = sdma_tx_submit; 11811ec1e82fSSascha Hauer /* txd.flags will be overwritten in prep funcs */ 11821ec1e82fSSascha Hauer sdmac->desc.flags = DMA_CTRL_ACK; 11831ec1e82fSSascha Hauer 11841ec1e82fSSascha Hauer return 0; 1185b93edcddSFabio Estevam 1186b93edcddSFabio Estevam disable_clk_ahb: 1187b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ahb); 1188b93edcddSFabio Estevam disable_clk_ipg: 1189b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ipg); 1190b93edcddSFabio Estevam return ret; 11911ec1e82fSSascha Hauer } 11921ec1e82fSSascha Hauer 11931ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 11941ec1e82fSSascha Hauer { 11951ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11961ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11971ec1e82fSSascha Hauer 11987b350ab0SMaxime Ripard sdma_disable_channel(chan); 11991ec1e82fSSascha Hauer 12001ec1e82fSSascha Hauer if (sdmac->event_id0) 12011ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 12021ec1e82fSSascha Hauer if (sdmac->event_id1) 12031ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 12041ec1e82fSSascha Hauer 12051ec1e82fSSascha Hauer sdmac->event_id0 = 0; 12061ec1e82fSSascha Hauer sdmac->event_id1 = 0; 12071ec1e82fSSascha Hauer 12081ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 12091ec1e82fSSascha Hauer 12101ec1e82fSSascha Hauer dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys); 12111ec1e82fSSascha Hauer 12127560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 12137560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 12141ec1e82fSSascha Hauer } 12151ec1e82fSSascha Hauer 12161ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 12171ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 1218db8196dfSVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 1219185ecb5fSAlexandre Bounine unsigned long flags, void *context) 12201ec1e82fSSascha Hauer { 12211ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 12221ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 12231ec1e82fSSascha Hauer int ret, i, count; 122423889c63SSascha Hauer int channel = sdmac->channel; 12251ec1e82fSSascha Hauer struct scatterlist *sg; 12261ec1e82fSSascha Hauer 12271ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 12281ec1e82fSSascha Hauer return NULL; 12291ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 12301ec1e82fSSascha Hauer 12311ec1e82fSSascha Hauer sdmac->flags = 0; 12321ec1e82fSSascha Hauer 12338e2e27c7SRichard Zhao sdmac->buf_tail = 0; 123485f57752SNandor Han sdmac->buf_ptail = 0; 123585f57752SNandor Han sdmac->chn_real_count = 0; 12368e2e27c7SRichard Zhao 12371ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 12381ec1e82fSSascha Hauer sg_len, channel); 12391ec1e82fSSascha Hauer 12401ec1e82fSSascha Hauer sdmac->direction = direction; 12411ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 12421ec1e82fSSascha Hauer if (ret) 12431ec1e82fSSascha Hauer goto err_out; 12441ec1e82fSSascha Hauer 12451ec1e82fSSascha Hauer if (sg_len > NUM_BD) { 12461ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 12471ec1e82fSSascha Hauer channel, sg_len, NUM_BD); 12481ec1e82fSSascha Hauer ret = -EINVAL; 12491ec1e82fSSascha Hauer goto err_out; 12501ec1e82fSSascha Hauer } 12511ec1e82fSSascha Hauer 1252ab59a510SHuang Shijie sdmac->chn_count = 0; 12531ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 12541ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 12551ec1e82fSSascha Hauer int param; 12561ec1e82fSSascha Hauer 1257d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 12581ec1e82fSSascha Hauer 1259fdaf9c4bSLars-Peter Clausen count = sg_dma_len(sg); 12601ec1e82fSSascha Hauer 12611ec1e82fSSascha Hauer if (count > 0xffff) { 12621ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 12631ec1e82fSSascha Hauer channel, count, 0xffff); 12641ec1e82fSSascha Hauer ret = -EINVAL; 12651ec1e82fSSascha Hauer goto err_out; 12661ec1e82fSSascha Hauer } 12671ec1e82fSSascha Hauer 12681ec1e82fSSascha Hauer bd->mode.count = count; 1269ab59a510SHuang Shijie sdmac->chn_count += count; 12701ec1e82fSSascha Hauer 12711ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { 12721ec1e82fSSascha Hauer ret = -EINVAL; 12731ec1e82fSSascha Hauer goto err_out; 12741ec1e82fSSascha Hauer } 12751fa81c27SSascha Hauer 12761fa81c27SSascha Hauer switch (sdmac->word_size) { 12771fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 12781ec1e82fSSascha Hauer bd->mode.command = 0; 12791fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 12801fa81c27SSascha Hauer return NULL; 12811fa81c27SSascha Hauer break; 12821fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 12831fa81c27SSascha Hauer bd->mode.command = 2; 12841fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 12851fa81c27SSascha Hauer return NULL; 12861fa81c27SSascha Hauer break; 12871fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 12881fa81c27SSascha Hauer bd->mode.command = 1; 12891fa81c27SSascha Hauer break; 12901fa81c27SSascha Hauer default: 12911fa81c27SSascha Hauer return NULL; 12921fa81c27SSascha Hauer } 12931ec1e82fSSascha Hauer 12941ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 12951ec1e82fSSascha Hauer 1296341b9419SShawn Guo if (i + 1 == sg_len) { 12971ec1e82fSSascha Hauer param |= BD_INTR; 1298341b9419SShawn Guo param |= BD_LAST; 1299341b9419SShawn Guo param &= ~BD_CONT; 13001ec1e82fSSascha Hauer } 13011ec1e82fSSascha Hauer 1302c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1303c3cc74b2SOlof Johansson i, count, (u64)sg->dma_address, 13041ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 13051ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 13061ec1e82fSSascha Hauer 13071ec1e82fSSascha Hauer bd->mode.status = param; 13081ec1e82fSSascha Hauer } 13091ec1e82fSSascha Hauer 13101ec1e82fSSascha Hauer sdmac->num_bd = sg_len; 13111ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 13121ec1e82fSSascha Hauer 13131ec1e82fSSascha Hauer return &sdmac->desc; 13141ec1e82fSSascha Hauer err_out: 13154b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 13161ec1e82fSSascha Hauer return NULL; 13171ec1e82fSSascha Hauer } 13181ec1e82fSSascha Hauer 13191ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 13201ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 1321185ecb5fSAlexandre Bounine size_t period_len, enum dma_transfer_direction direction, 132231c1e5a1SLaurent Pinchart unsigned long flags) 13231ec1e82fSSascha Hauer { 13241ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13251ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 13261ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 132723889c63SSascha Hauer int channel = sdmac->channel; 13281ec1e82fSSascha Hauer int ret, i = 0, buf = 0; 13291ec1e82fSSascha Hauer 13301ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 13311ec1e82fSSascha Hauer 13321ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 13331ec1e82fSSascha Hauer return NULL; 13341ec1e82fSSascha Hauer 13351ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 13361ec1e82fSSascha Hauer 13378e2e27c7SRichard Zhao sdmac->buf_tail = 0; 133885f57752SNandor Han sdmac->buf_ptail = 0; 133985f57752SNandor Han sdmac->chn_real_count = 0; 1340d1a792f3SRussell King - ARM Linux sdmac->period_len = period_len; 13418e2e27c7SRichard Zhao 13421ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 13431ec1e82fSSascha Hauer sdmac->direction = direction; 13441ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 13451ec1e82fSSascha Hauer if (ret) 13461ec1e82fSSascha Hauer goto err_out; 13471ec1e82fSSascha Hauer 13481ec1e82fSSascha Hauer if (num_periods > NUM_BD) { 13491ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 13501ec1e82fSSascha Hauer channel, num_periods, NUM_BD); 13511ec1e82fSSascha Hauer goto err_out; 13521ec1e82fSSascha Hauer } 13531ec1e82fSSascha Hauer 13541ec1e82fSSascha Hauer if (period_len > 0xffff) { 1355ba6ab3b3SArvind Yadav dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n", 13561ec1e82fSSascha Hauer channel, period_len, 0xffff); 13571ec1e82fSSascha Hauer goto err_out; 13581ec1e82fSSascha Hauer } 13591ec1e82fSSascha Hauer 13601ec1e82fSSascha Hauer while (buf < buf_len) { 13611ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 13621ec1e82fSSascha Hauer int param; 13631ec1e82fSSascha Hauer 13641ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 13651ec1e82fSSascha Hauer 13661ec1e82fSSascha Hauer bd->mode.count = period_len; 13671ec1e82fSSascha Hauer 13681ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 13691ec1e82fSSascha Hauer goto err_out; 13701ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 13711ec1e82fSSascha Hauer bd->mode.command = 0; 13721ec1e82fSSascha Hauer else 13731ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 13741ec1e82fSSascha Hauer 13751ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 13761ec1e82fSSascha Hauer if (i + 1 == num_periods) 13771ec1e82fSSascha Hauer param |= BD_WRAP; 13781ec1e82fSSascha Hauer 1379ba6ab3b3SArvind Yadav dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n", 1380c3cc74b2SOlof Johansson i, period_len, (u64)dma_addr, 13811ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 13821ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 13831ec1e82fSSascha Hauer 13841ec1e82fSSascha Hauer bd->mode.status = param; 13851ec1e82fSSascha Hauer 13861ec1e82fSSascha Hauer dma_addr += period_len; 13871ec1e82fSSascha Hauer buf += period_len; 13881ec1e82fSSascha Hauer 13891ec1e82fSSascha Hauer i++; 13901ec1e82fSSascha Hauer } 13911ec1e82fSSascha Hauer 13921ec1e82fSSascha Hauer sdmac->num_bd = num_periods; 13931ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 13941ec1e82fSSascha Hauer 13951ec1e82fSSascha Hauer return &sdmac->desc; 13961ec1e82fSSascha Hauer err_out: 13971ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 13981ec1e82fSSascha Hauer return NULL; 13991ec1e82fSSascha Hauer } 14001ec1e82fSSascha Hauer 14017b350ab0SMaxime Ripard static int sdma_config(struct dma_chan *chan, 14027b350ab0SMaxime Ripard struct dma_slave_config *dmaengine_cfg) 14031ec1e82fSSascha Hauer { 14041ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 14051ec1e82fSSascha Hauer 1406db8196dfSVinod Koul if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) { 14071ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 140894ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->src_maxburst * 140994ac27a5SPhilippe Rétornaz dmaengine_cfg->src_addr_width; 14101ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 14118391ecf4SShengjiu Wang } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) { 14128391ecf4SShengjiu Wang sdmac->per_address2 = dmaengine_cfg->src_addr; 14138391ecf4SShengjiu Wang sdmac->per_address = dmaengine_cfg->dst_addr; 14148391ecf4SShengjiu Wang sdmac->watermark_level = dmaengine_cfg->src_maxburst & 14158391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_LWML; 14168391ecf4SShengjiu Wang sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) & 14178391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML; 14188391ecf4SShengjiu Wang sdmac->word_size = dmaengine_cfg->dst_addr_width; 14191ec1e82fSSascha Hauer } else { 14201ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 142194ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->dst_maxburst * 142294ac27a5SPhilippe Rétornaz dmaengine_cfg->dst_addr_width; 14231ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 14241ec1e82fSSascha Hauer } 1425e6966433SHuang Shijie sdmac->direction = dmaengine_cfg->direction; 14267b350ab0SMaxime Ripard return sdma_config_channel(chan); 14271ec1e82fSSascha Hauer } 14281ec1e82fSSascha Hauer 14291ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 14301ec1e82fSSascha Hauer dma_cookie_t cookie, 14311ec1e82fSSascha Hauer struct dma_tx_state *txstate) 14321ec1e82fSSascha Hauer { 14331ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 1434d1a792f3SRussell King - ARM Linux u32 residue; 1435d1a792f3SRussell King - ARM Linux 1436d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 143785f57752SNandor Han residue = (sdmac->num_bd - sdmac->buf_ptail) * 14385881826dSNandor Han sdmac->period_len - sdmac->chn_real_count; 1439d1a792f3SRussell King - ARM Linux else 1440d1a792f3SRussell King - ARM Linux residue = sdmac->chn_count - sdmac->chn_real_count; 14411ec1e82fSSascha Hauer 1442e8e3a790SAndy Shevchenko dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, 1443d1a792f3SRussell King - ARM Linux residue); 14441ec1e82fSSascha Hauer 14458a965911SShawn Guo return sdmac->status; 14461ec1e82fSSascha Hauer } 14471ec1e82fSSascha Hauer 14481ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 14491ec1e82fSSascha Hauer { 14502b4f130eSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 14512b4f130eSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 14522b4f130eSSascha Hauer 14532b4f130eSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 14542b4f130eSSascha Hauer sdma_enable_channel(sdma, sdmac->channel); 14551ec1e82fSSascha Hauer } 14561ec1e82fSSascha Hauer 14575b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 1458cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38 1459a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41 1460b7d2648aSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42 14615b28aa31SSascha Hauer 14625b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 14635b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 14645b28aa31SSascha Hauer { 14655b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 14665b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 14675b28aa31SSascha Hauer int i; 14685b28aa31SSascha Hauer 146970dabaedSNicolin Chen /* use the default firmware in ROM if missing external firmware */ 147070dabaedSNicolin Chen if (!sdma->script_number) 147170dabaedSNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 147270dabaedSNicolin Chen 1473cd72b846SNicolin Chen for (i = 0; i < sdma->script_number; i++) 14745b28aa31SSascha Hauer if (addr_arr[i] > 0) 14755b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 14765b28aa31SSascha Hauer } 14775b28aa31SSascha Hauer 14787b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context) 14795b28aa31SSascha Hauer { 14807b4b88e0SSascha Hauer struct sdma_engine *sdma = context; 14815b28aa31SSascha Hauer const struct sdma_firmware_header *header; 14825b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 14835b28aa31SSascha Hauer unsigned short *ram_code; 14845b28aa31SSascha Hauer 14857b4b88e0SSascha Hauer if (!fw) { 14860f927a11SSascha Hauer dev_info(sdma->dev, "external firmware not found, using ROM firmware\n"); 14870f927a11SSascha Hauer /* In this case we just use the ROM firmware. */ 14887b4b88e0SSascha Hauer return; 14897b4b88e0SSascha Hauer } 14905b28aa31SSascha Hauer 14915b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 14925b28aa31SSascha Hauer goto err_firmware; 14935b28aa31SSascha Hauer 14945b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 14955b28aa31SSascha Hauer 14965b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 14975b28aa31SSascha Hauer goto err_firmware; 14985b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 14995b28aa31SSascha Hauer goto err_firmware; 1500cd72b846SNicolin Chen switch (header->version_major) { 1501cd72b846SNicolin Chen case 1: 1502cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 1503cd72b846SNicolin Chen break; 1504cd72b846SNicolin Chen case 2: 1505cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2; 1506cd72b846SNicolin Chen break; 1507a572460bSFabio Estevam case 3: 1508a572460bSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3; 1509a572460bSFabio Estevam break; 1510b7d2648aSFabio Estevam case 4: 1511b7d2648aSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4; 1512b7d2648aSFabio Estevam break; 1513cd72b846SNicolin Chen default: 1514cd72b846SNicolin Chen dev_err(sdma->dev, "unknown firmware version\n"); 1515cd72b846SNicolin Chen goto err_firmware; 1516cd72b846SNicolin Chen } 15175b28aa31SSascha Hauer 15185b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 15195b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 15205b28aa31SSascha Hauer 15217560e3f3SSascha Hauer clk_enable(sdma->clk_ipg); 15227560e3f3SSascha Hauer clk_enable(sdma->clk_ahb); 15235b28aa31SSascha Hauer /* download the RAM image for SDMA */ 15245b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 15255b28aa31SSascha Hauer header->ram_code_size, 15266866fd3bSSascha Hauer addr->ram_code_start_addr); 15277560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 15287560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 15295b28aa31SSascha Hauer 15305b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 15315b28aa31SSascha Hauer 15325b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 15335b28aa31SSascha Hauer header->version_major, 15345b28aa31SSascha Hauer header->version_minor); 15355b28aa31SSascha Hauer 15365b28aa31SSascha Hauer err_firmware: 15375b28aa31SSascha Hauer release_firmware(fw); 15387b4b88e0SSascha Hauer } 15397b4b88e0SSascha Hauer 1540d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3 1541d078cd1bSZidan Wang 154229f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma) 1543d078cd1bSZidan Wang { 1544d078cd1bSZidan Wang struct device_node *np = sdma->dev->of_node; 1545d078cd1bSZidan Wang struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0); 1546d078cd1bSZidan Wang struct property *event_remap; 1547d078cd1bSZidan Wang struct regmap *gpr; 1548d078cd1bSZidan Wang char propname[] = "fsl,sdma-event-remap"; 1549d078cd1bSZidan Wang u32 reg, val, shift, num_map, i; 1550d078cd1bSZidan Wang int ret = 0; 1551d078cd1bSZidan Wang 1552d078cd1bSZidan Wang if (IS_ERR(np) || IS_ERR(gpr_np)) 1553d078cd1bSZidan Wang goto out; 1554d078cd1bSZidan Wang 1555d078cd1bSZidan Wang event_remap = of_find_property(np, propname, NULL); 1556d078cd1bSZidan Wang num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0; 1557d078cd1bSZidan Wang if (!num_map) { 1558ce078af7SFabio Estevam dev_dbg(sdma->dev, "no event needs to be remapped\n"); 1559d078cd1bSZidan Wang goto out; 1560d078cd1bSZidan Wang } else if (num_map % EVENT_REMAP_CELLS) { 1561d078cd1bSZidan Wang dev_err(sdma->dev, "the property %s must modulo %d\n", 1562d078cd1bSZidan Wang propname, EVENT_REMAP_CELLS); 1563d078cd1bSZidan Wang ret = -EINVAL; 1564d078cd1bSZidan Wang goto out; 1565d078cd1bSZidan Wang } 1566d078cd1bSZidan Wang 1567d078cd1bSZidan Wang gpr = syscon_node_to_regmap(gpr_np); 1568d078cd1bSZidan Wang if (IS_ERR(gpr)) { 1569d078cd1bSZidan Wang dev_err(sdma->dev, "failed to get gpr regmap\n"); 1570d078cd1bSZidan Wang ret = PTR_ERR(gpr); 1571d078cd1bSZidan Wang goto out; 1572d078cd1bSZidan Wang } 1573d078cd1bSZidan Wang 1574d078cd1bSZidan Wang for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) { 1575d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i, ®); 1576d078cd1bSZidan Wang if (ret) { 1577d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1578d078cd1bSZidan Wang propname, i); 1579d078cd1bSZidan Wang goto out; 1580d078cd1bSZidan Wang } 1581d078cd1bSZidan Wang 1582d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 1, &shift); 1583d078cd1bSZidan Wang if (ret) { 1584d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1585d078cd1bSZidan Wang propname, i + 1); 1586d078cd1bSZidan Wang goto out; 1587d078cd1bSZidan Wang } 1588d078cd1bSZidan Wang 1589d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 2, &val); 1590d078cd1bSZidan Wang if (ret) { 1591d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1592d078cd1bSZidan Wang propname, i + 2); 1593d078cd1bSZidan Wang goto out; 1594d078cd1bSZidan Wang } 1595d078cd1bSZidan Wang 1596d078cd1bSZidan Wang regmap_update_bits(gpr, reg, BIT(shift), val << shift); 1597d078cd1bSZidan Wang } 1598d078cd1bSZidan Wang 1599d078cd1bSZidan Wang out: 1600d078cd1bSZidan Wang if (!IS_ERR(gpr_np)) 1601d078cd1bSZidan Wang of_node_put(gpr_np); 1602d078cd1bSZidan Wang 1603d078cd1bSZidan Wang return ret; 1604d078cd1bSZidan Wang } 1605d078cd1bSZidan Wang 1606fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma, 16077b4b88e0SSascha Hauer const char *fw_name) 16087b4b88e0SSascha Hauer { 16097b4b88e0SSascha Hauer int ret; 16107b4b88e0SSascha Hauer 16117b4b88e0SSascha Hauer ret = request_firmware_nowait(THIS_MODULE, 16127b4b88e0SSascha Hauer FW_ACTION_HOTPLUG, fw_name, sdma->dev, 16137b4b88e0SSascha Hauer GFP_KERNEL, sdma, sdma_load_firmware); 16145b28aa31SSascha Hauer 16155b28aa31SSascha Hauer return ret; 16165b28aa31SSascha Hauer } 16175b28aa31SSascha Hauer 161819bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma) 16191ec1e82fSSascha Hauer { 16201ec1e82fSSascha Hauer int i, ret; 16211ec1e82fSSascha Hauer dma_addr_t ccb_phys; 16221ec1e82fSSascha Hauer 1623b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ipg); 1624b93edcddSFabio Estevam if (ret) 1625b93edcddSFabio Estevam return ret; 1626b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ahb); 1627b93edcddSFabio Estevam if (ret) 1628b93edcddSFabio Estevam goto disable_clk_ipg; 16291ec1e82fSSascha Hauer 16301ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 1631c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); 16321ec1e82fSSascha Hauer 16331ec1e82fSSascha Hauer sdma->channel_control = dma_alloc_coherent(NULL, 16341ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + 16351ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 16361ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 16371ec1e82fSSascha Hauer 16381ec1e82fSSascha Hauer if (!sdma->channel_control) { 16391ec1e82fSSascha Hauer ret = -ENOMEM; 16401ec1e82fSSascha Hauer goto err_dma_alloc; 16411ec1e82fSSascha Hauer } 16421ec1e82fSSascha Hauer 16431ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 16441ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 16451ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 16461ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 16471ec1e82fSSascha Hauer 16481ec1e82fSSascha Hauer /* Zero-out the CCB structures array just allocated */ 16491ec1e82fSSascha Hauer memset(sdma->channel_control, 0, 16501ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control)); 16511ec1e82fSSascha Hauer 16521ec1e82fSSascha Hauer /* disable all channels */ 165317bba72fSSascha Hauer for (i = 0; i < sdma->drvdata->num_events; i++) 1654c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); 16551ec1e82fSSascha Hauer 16561ec1e82fSSascha Hauer /* All channels have priority 0 */ 16571ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 1658c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 16591ec1e82fSSascha Hauer 16601ec1e82fSSascha Hauer ret = sdma_request_channel(&sdma->channel[0]); 16611ec1e82fSSascha Hauer if (ret) 16621ec1e82fSSascha Hauer goto err_dma_alloc; 16631ec1e82fSSascha Hauer 16641ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 16651ec1e82fSSascha Hauer 16661ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 1667c4b56857SRichard Zhao writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); 16681ec1e82fSSascha Hauer 16691ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 16701ec1e82fSSascha Hauer /* FIXME: Check whether to set ACR bit depending on clock ratios */ 1671c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); 16721ec1e82fSSascha Hauer 1673c4b56857SRichard Zhao writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); 16741ec1e82fSSascha Hauer 16751ec1e82fSSascha Hauer /* Initializes channel's priorities */ 16761ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 16771ec1e82fSSascha Hauer 16787560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 16797560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 16801ec1e82fSSascha Hauer 16811ec1e82fSSascha Hauer return 0; 16821ec1e82fSSascha Hauer 16831ec1e82fSSascha Hauer err_dma_alloc: 16847560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 1685b93edcddSFabio Estevam disable_clk_ipg: 1686b93edcddSFabio Estevam clk_disable(sdma->clk_ipg); 16871ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 16881ec1e82fSSascha Hauer return ret; 16891ec1e82fSSascha Hauer } 16901ec1e82fSSascha Hauer 16919479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param) 16929479e17cSShawn Guo { 16930b351865SNicolin Chen struct sdma_channel *sdmac = to_sdma_chan(chan); 16949479e17cSShawn Guo struct imx_dma_data *data = fn_param; 16959479e17cSShawn Guo 16969479e17cSShawn Guo if (!imx_dma_is_general_purpose(chan)) 16979479e17cSShawn Guo return false; 16989479e17cSShawn Guo 16990b351865SNicolin Chen sdmac->data = *data; 17000b351865SNicolin Chen chan->private = &sdmac->data; 17019479e17cSShawn Guo 17029479e17cSShawn Guo return true; 17039479e17cSShawn Guo } 17049479e17cSShawn Guo 17059479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec, 17069479e17cSShawn Guo struct of_dma *ofdma) 17079479e17cSShawn Guo { 17089479e17cSShawn Guo struct sdma_engine *sdma = ofdma->of_dma_data; 17099479e17cSShawn Guo dma_cap_mask_t mask = sdma->dma_device.cap_mask; 17109479e17cSShawn Guo struct imx_dma_data data; 17119479e17cSShawn Guo 17129479e17cSShawn Guo if (dma_spec->args_count != 3) 17139479e17cSShawn Guo return NULL; 17149479e17cSShawn Guo 17159479e17cSShawn Guo data.dma_request = dma_spec->args[0]; 17169479e17cSShawn Guo data.peripheral_type = dma_spec->args[1]; 17179479e17cSShawn Guo data.priority = dma_spec->args[2]; 17188391ecf4SShengjiu Wang /* 17198391ecf4SShengjiu Wang * init dma_request2 to zero, which is not used by the dts. 17208391ecf4SShengjiu Wang * For P2P, dma_request2 is init from dma_request_channel(), 17218391ecf4SShengjiu Wang * chan->private will point to the imx_dma_data, and in 17228391ecf4SShengjiu Wang * device_alloc_chan_resources(), imx_dma_data.dma_request2 will 17238391ecf4SShengjiu Wang * be set to sdmac->event_id1. 17248391ecf4SShengjiu Wang */ 17258391ecf4SShengjiu Wang data.dma_request2 = 0; 17269479e17cSShawn Guo 17279479e17cSShawn Guo return dma_request_channel(mask, sdma_filter_fn, &data); 17289479e17cSShawn Guo } 17299479e17cSShawn Guo 1730e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev) 17311ec1e82fSSascha Hauer { 1732580975d7SShawn Guo const struct of_device_id *of_id = 1733580975d7SShawn Guo of_match_device(sdma_dt_ids, &pdev->dev); 1734580975d7SShawn Guo struct device_node *np = pdev->dev.of_node; 17358391ecf4SShengjiu Wang struct device_node *spba_bus; 1736580975d7SShawn Guo const char *fw_name; 17371ec1e82fSSascha Hauer int ret; 17381ec1e82fSSascha Hauer int irq; 17391ec1e82fSSascha Hauer struct resource *iores; 17408391ecf4SShengjiu Wang struct resource spba_res; 1741d4adcc01SJingoo Han struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev); 17421ec1e82fSSascha Hauer int i; 17431ec1e82fSSascha Hauer struct sdma_engine *sdma; 174436e2f21aSSascha Hauer s32 *saddr_arr; 174517bba72fSSascha Hauer const struct sdma_driver_data *drvdata = NULL; 174617bba72fSSascha Hauer 174717bba72fSSascha Hauer if (of_id) 174817bba72fSSascha Hauer drvdata = of_id->data; 174917bba72fSSascha Hauer else if (pdev->id_entry) 175017bba72fSSascha Hauer drvdata = (void *)pdev->id_entry->driver_data; 175117bba72fSSascha Hauer 175217bba72fSSascha Hauer if (!drvdata) { 175317bba72fSSascha Hauer dev_err(&pdev->dev, "unable to find driver data\n"); 175417bba72fSSascha Hauer return -EINVAL; 175517bba72fSSascha Hauer } 17561ec1e82fSSascha Hauer 175742536b9fSPhilippe Retornaz ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 175842536b9fSPhilippe Retornaz if (ret) 175942536b9fSPhilippe Retornaz return ret; 176042536b9fSPhilippe Retornaz 17617f24e0eeSFabio Estevam sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL); 17621ec1e82fSSascha Hauer if (!sdma) 17631ec1e82fSSascha Hauer return -ENOMEM; 17641ec1e82fSSascha Hauer 17652ccaef05SRichard Zhao spin_lock_init(&sdma->channel_0_lock); 176673eab978SSascha Hauer 17671ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 176817bba72fSSascha Hauer sdma->drvdata = drvdata; 17691ec1e82fSSascha Hauer 17701ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 17717f24e0eeSFabio Estevam if (irq < 0) 177263c72e02SFabio Estevam return irq; 17731ec1e82fSSascha Hauer 17747f24e0eeSFabio Estevam iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 17757f24e0eeSFabio Estevam sdma->regs = devm_ioremap_resource(&pdev->dev, iores); 17767f24e0eeSFabio Estevam if (IS_ERR(sdma->regs)) 17777f24e0eeSFabio Estevam return PTR_ERR(sdma->regs); 17781ec1e82fSSascha Hauer 17797560e3f3SSascha Hauer sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 17807f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ipg)) 17817f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ipg); 17821ec1e82fSSascha Hauer 17837560e3f3SSascha Hauer sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 17847f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ahb)) 17857f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ahb); 17867560e3f3SSascha Hauer 1787fb9caf37SArvind Yadav ret = clk_prepare(sdma->clk_ipg); 1788fb9caf37SArvind Yadav if (ret) 1789fb9caf37SArvind Yadav return ret; 1790fb9caf37SArvind Yadav 1791fb9caf37SArvind Yadav ret = clk_prepare(sdma->clk_ahb); 1792fb9caf37SArvind Yadav if (ret) 1793fb9caf37SArvind Yadav goto err_clk; 17947560e3f3SSascha Hauer 17957f24e0eeSFabio Estevam ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma", 17967f24e0eeSFabio Estevam sdma); 17971ec1e82fSSascha Hauer if (ret) 1798fb9caf37SArvind Yadav goto err_irq; 17991ec1e82fSSascha Hauer 18005bb9dbb5SVinod Koul sdma->irq = irq; 18015bb9dbb5SVinod Koul 18025b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 1803fb9caf37SArvind Yadav if (!sdma->script_addrs) { 1804fb9caf37SArvind Yadav ret = -ENOMEM; 1805fb9caf37SArvind Yadav goto err_irq; 1806fb9caf37SArvind Yadav } 18071ec1e82fSSascha Hauer 180836e2f21aSSascha Hauer /* initially no scripts available */ 180936e2f21aSSascha Hauer saddr_arr = (s32 *)sdma->script_addrs; 181036e2f21aSSascha Hauer for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) 181136e2f21aSSascha Hauer saddr_arr[i] = -EINVAL; 181236e2f21aSSascha Hauer 18137214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 18147214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 18157214a8b1SSascha Hauer 18161ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 18171ec1e82fSSascha Hauer /* Initialize channel parameters */ 18181ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 18191ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 18201ec1e82fSSascha Hauer 18211ec1e82fSSascha Hauer sdmac->sdma = sdma; 18221ec1e82fSSascha Hauer spin_lock_init(&sdmac->lock); 18231ec1e82fSSascha Hauer 18241ec1e82fSSascha Hauer sdmac->chan.device = &sdma->dma_device; 18258ac69546SRussell King - ARM Linux dma_cookie_init(&sdmac->chan); 18261ec1e82fSSascha Hauer sdmac->channel = i; 18271ec1e82fSSascha Hauer 182815f30f51SNandor Han tasklet_init(&sdmac->tasklet, mxc_sdma_handle_channel_normal, 1829abd9ccc8SHuang Shijie (unsigned long) sdmac); 183023889c63SSascha Hauer /* 183123889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 183223889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 183323889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 183423889c63SSascha Hauer */ 183523889c63SSascha Hauer if (i) 183623889c63SSascha Hauer list_add_tail(&sdmac->chan.device_node, 183723889c63SSascha Hauer &sdma->dma_device.channels); 18381ec1e82fSSascha Hauer } 18391ec1e82fSSascha Hauer 18405b28aa31SSascha Hauer ret = sdma_init(sdma); 18411ec1e82fSSascha Hauer if (ret) 18421ec1e82fSSascha Hauer goto err_init; 18431ec1e82fSSascha Hauer 1844d078cd1bSZidan Wang ret = sdma_event_remap(sdma); 1845d078cd1bSZidan Wang if (ret) 1846d078cd1bSZidan Wang goto err_init; 1847d078cd1bSZidan Wang 1848dcfec3c0SSascha Hauer if (sdma->drvdata->script_addrs) 1849dcfec3c0SSascha Hauer sdma_add_scripts(sdma, sdma->drvdata->script_addrs); 1850580975d7SShawn Guo if (pdata && pdata->script_addrs) 18515b28aa31SSascha Hauer sdma_add_scripts(sdma, pdata->script_addrs); 18525b28aa31SSascha Hauer 1853580975d7SShawn Guo if (pdata) { 18546d0d7e2dSFabio Estevam ret = sdma_get_firmware(sdma, pdata->fw_name); 18556d0d7e2dSFabio Estevam if (ret) 1856ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); 1857580975d7SShawn Guo } else { 1858580975d7SShawn Guo /* 1859580975d7SShawn Guo * Because that device tree does not encode ROM script address, 1860580975d7SShawn Guo * the RAM script in firmware is mandatory for device tree 1861580975d7SShawn Guo * probe, otherwise it fails. 1862580975d7SShawn Guo */ 1863580975d7SShawn Guo ret = of_property_read_string(np, "fsl,sdma-ram-script-name", 1864580975d7SShawn Guo &fw_name); 18656602b0ddSFabio Estevam if (ret) 1866ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware name\n"); 18676602b0ddSFabio Estevam else { 1868580975d7SShawn Guo ret = sdma_get_firmware(sdma, fw_name); 18696602b0ddSFabio Estevam if (ret) 1870ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); 1871580975d7SShawn Guo } 1872580975d7SShawn Guo } 18735b28aa31SSascha Hauer 18741ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 18751ec1e82fSSascha Hauer 18761ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 18771ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 18781ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 18791ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 18801ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 18817b350ab0SMaxime Ripard sdma->dma_device.device_config = sdma_config; 18827f3ff14bSJiada Wang sdma->dma_device.device_terminate_all = sdma_disable_channel_with_delay; 1883f9d4a398SNicolin Chen sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS; 1884f9d4a398SNicolin Chen sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS; 1885f9d4a398SNicolin Chen sdma->dma_device.directions = SDMA_DMA_DIRECTIONS; 18866f3125ceSLucas Stach sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT; 18871ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 1888b9b3f82fSSascha Hauer sdma->dma_device.dev->dma_parms = &sdma->dma_parms; 1889b9b3f82fSSascha Hauer dma_set_max_seg_size(sdma->dma_device.dev, 65535); 18901ec1e82fSSascha Hauer 189123e11811SVignesh Raman platform_set_drvdata(pdev, sdma); 189223e11811SVignesh Raman 18931ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 18941ec1e82fSSascha Hauer if (ret) { 18951ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 18961ec1e82fSSascha Hauer goto err_init; 18971ec1e82fSSascha Hauer } 18981ec1e82fSSascha Hauer 18999479e17cSShawn Guo if (np) { 19009479e17cSShawn Guo ret = of_dma_controller_register(np, sdma_xlate, sdma); 19019479e17cSShawn Guo if (ret) { 19029479e17cSShawn Guo dev_err(&pdev->dev, "failed to register controller\n"); 19039479e17cSShawn Guo goto err_register; 19049479e17cSShawn Guo } 19058391ecf4SShengjiu Wang 19068391ecf4SShengjiu Wang spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus"); 19078391ecf4SShengjiu Wang ret = of_address_to_resource(spba_bus, 0, &spba_res); 19088391ecf4SShengjiu Wang if (!ret) { 19098391ecf4SShengjiu Wang sdma->spba_start_addr = spba_res.start; 19108391ecf4SShengjiu Wang sdma->spba_end_addr = spba_res.end; 19118391ecf4SShengjiu Wang } 19128391ecf4SShengjiu Wang of_node_put(spba_bus); 19139479e17cSShawn Guo } 19149479e17cSShawn Guo 19151ec1e82fSSascha Hauer return 0; 19161ec1e82fSSascha Hauer 19179479e17cSShawn Guo err_register: 19189479e17cSShawn Guo dma_async_device_unregister(&sdma->dma_device); 19191ec1e82fSSascha Hauer err_init: 19201ec1e82fSSascha Hauer kfree(sdma->script_addrs); 1921fb9caf37SArvind Yadav err_irq: 1922fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ahb); 1923fb9caf37SArvind Yadav err_clk: 1924fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ipg); 1925939fd4f0SShawn Guo return ret; 19261ec1e82fSSascha Hauer } 19271ec1e82fSSascha Hauer 19281d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev) 19291ec1e82fSSascha Hauer { 193023e11811SVignesh Raman struct sdma_engine *sdma = platform_get_drvdata(pdev); 1931c12fe497SVignesh Raman int i; 193223e11811SVignesh Raman 19335bb9dbb5SVinod Koul devm_free_irq(&pdev->dev, sdma->irq, sdma); 193423e11811SVignesh Raman dma_async_device_unregister(&sdma->dma_device); 193523e11811SVignesh Raman kfree(sdma->script_addrs); 1936fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ahb); 1937fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ipg); 1938c12fe497SVignesh Raman /* Kill the tasklet */ 1939c12fe497SVignesh Raman for (i = 0; i < MAX_DMA_CHANNELS; i++) { 1940c12fe497SVignesh Raman struct sdma_channel *sdmac = &sdma->channel[i]; 1941c12fe497SVignesh Raman 1942c12fe497SVignesh Raman tasklet_kill(&sdmac->tasklet); 1943c12fe497SVignesh Raman } 194423e11811SVignesh Raman 194523e11811SVignesh Raman platform_set_drvdata(pdev, NULL); 194623e11811SVignesh Raman return 0; 19471ec1e82fSSascha Hauer } 19481ec1e82fSSascha Hauer 19491ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 19501ec1e82fSSascha Hauer .driver = { 19511ec1e82fSSascha Hauer .name = "imx-sdma", 1952580975d7SShawn Guo .of_match_table = sdma_dt_ids, 19531ec1e82fSSascha Hauer }, 195462550cd7SShawn Guo .id_table = sdma_devtypes, 19551d1bbd30SMaxin B. John .remove = sdma_remove, 195623e11811SVignesh Raman .probe = sdma_probe, 19571ec1e82fSSascha Hauer }; 19581ec1e82fSSascha Hauer 195923e11811SVignesh Raman module_platform_driver(sdma_driver); 19601ec1e82fSSascha Hauer 19611ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 19621ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 1963c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q) 1964c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin"); 1965c0879342SNicolas Chauvet #endif 1966c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX7D) 1967c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin"); 1968c0879342SNicolas Chauvet #endif 19691ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 1970