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 1818391ecf4SShengjiu Wang /* 1821ec1e82fSSascha Hauer * Mode/Count of data node descriptors - IPCv2 1831ec1e82fSSascha Hauer */ 1841ec1e82fSSascha Hauer struct sdma_mode_count { 1851ec1e82fSSascha Hauer u32 count : 16; /* size of the buffer pointed by this BD */ 1861ec1e82fSSascha Hauer u32 status : 8; /* E,R,I,C,W,D status bits stored here */ 187e4b75760SMartin Kaiser u32 command : 8; /* command mostly used for channel 0 */ 1881ec1e82fSSascha Hauer }; 1891ec1e82fSSascha Hauer 1901ec1e82fSSascha Hauer /* 1911ec1e82fSSascha Hauer * Buffer descriptor 1921ec1e82fSSascha Hauer */ 1931ec1e82fSSascha Hauer struct sdma_buffer_descriptor { 1941ec1e82fSSascha Hauer struct sdma_mode_count mode; 1951ec1e82fSSascha Hauer u32 buffer_addr; /* address of the buffer described */ 1961ec1e82fSSascha Hauer u32 ext_buffer_addr; /* extended buffer address */ 1971ec1e82fSSascha Hauer } __attribute__ ((packed)); 1981ec1e82fSSascha Hauer 1991ec1e82fSSascha Hauer /** 2001ec1e82fSSascha Hauer * struct sdma_channel_control - Channel control Block 2011ec1e82fSSascha Hauer * 2021ec1e82fSSascha Hauer * @current_bd_ptr current buffer descriptor processed 2031ec1e82fSSascha Hauer * @base_bd_ptr first element of buffer descriptor array 2041ec1e82fSSascha Hauer * @unused padding. The SDMA engine expects an array of 128 byte 2051ec1e82fSSascha Hauer * control blocks 2061ec1e82fSSascha Hauer */ 2071ec1e82fSSascha Hauer struct sdma_channel_control { 2081ec1e82fSSascha Hauer u32 current_bd_ptr; 2091ec1e82fSSascha Hauer u32 base_bd_ptr; 2101ec1e82fSSascha Hauer u32 unused[2]; 2111ec1e82fSSascha Hauer } __attribute__ ((packed)); 2121ec1e82fSSascha Hauer 2131ec1e82fSSascha Hauer /** 2141ec1e82fSSascha Hauer * struct sdma_state_registers - SDMA context for a channel 2151ec1e82fSSascha Hauer * 2161ec1e82fSSascha Hauer * @pc: program counter 2171ec1e82fSSascha Hauer * @t: test bit: status of arithmetic & test instruction 2181ec1e82fSSascha Hauer * @rpc: return program counter 2191ec1e82fSSascha Hauer * @sf: source fault while loading data 2201ec1e82fSSascha Hauer * @spc: loop start program counter 2211ec1e82fSSascha Hauer * @df: destination fault while storing data 2221ec1e82fSSascha Hauer * @epc: loop end program counter 2231ec1e82fSSascha Hauer * @lm: loop mode 2241ec1e82fSSascha Hauer */ 2251ec1e82fSSascha Hauer struct sdma_state_registers { 2261ec1e82fSSascha Hauer u32 pc :14; 2271ec1e82fSSascha Hauer u32 unused1: 1; 2281ec1e82fSSascha Hauer u32 t : 1; 2291ec1e82fSSascha Hauer u32 rpc :14; 2301ec1e82fSSascha Hauer u32 unused0: 1; 2311ec1e82fSSascha Hauer u32 sf : 1; 2321ec1e82fSSascha Hauer u32 spc :14; 2331ec1e82fSSascha Hauer u32 unused2: 1; 2341ec1e82fSSascha Hauer u32 df : 1; 2351ec1e82fSSascha Hauer u32 epc :14; 2361ec1e82fSSascha Hauer u32 lm : 2; 2371ec1e82fSSascha Hauer } __attribute__ ((packed)); 2381ec1e82fSSascha Hauer 2391ec1e82fSSascha Hauer /** 2401ec1e82fSSascha Hauer * struct sdma_context_data - sdma context specific to a channel 2411ec1e82fSSascha Hauer * 2421ec1e82fSSascha Hauer * @channel_state: channel state bits 2431ec1e82fSSascha Hauer * @gReg: general registers 2441ec1e82fSSascha Hauer * @mda: burst dma destination address register 2451ec1e82fSSascha Hauer * @msa: burst dma source address register 2461ec1e82fSSascha Hauer * @ms: burst dma status register 2471ec1e82fSSascha Hauer * @md: burst dma data register 2481ec1e82fSSascha Hauer * @pda: peripheral dma destination address register 2491ec1e82fSSascha Hauer * @psa: peripheral dma source address register 2501ec1e82fSSascha Hauer * @ps: peripheral dma status register 2511ec1e82fSSascha Hauer * @pd: peripheral dma data register 2521ec1e82fSSascha Hauer * @ca: CRC polynomial register 2531ec1e82fSSascha Hauer * @cs: CRC accumulator register 2541ec1e82fSSascha Hauer * @dda: dedicated core destination address register 2551ec1e82fSSascha Hauer * @dsa: dedicated core source address register 2561ec1e82fSSascha Hauer * @ds: dedicated core status register 2571ec1e82fSSascha Hauer * @dd: dedicated core data register 2581ec1e82fSSascha Hauer */ 2591ec1e82fSSascha Hauer struct sdma_context_data { 2601ec1e82fSSascha Hauer struct sdma_state_registers channel_state; 2611ec1e82fSSascha Hauer u32 gReg[8]; 2621ec1e82fSSascha Hauer u32 mda; 2631ec1e82fSSascha Hauer u32 msa; 2641ec1e82fSSascha Hauer u32 ms; 2651ec1e82fSSascha Hauer u32 md; 2661ec1e82fSSascha Hauer u32 pda; 2671ec1e82fSSascha Hauer u32 psa; 2681ec1e82fSSascha Hauer u32 ps; 2691ec1e82fSSascha Hauer u32 pd; 2701ec1e82fSSascha Hauer u32 ca; 2711ec1e82fSSascha Hauer u32 cs; 2721ec1e82fSSascha Hauer u32 dda; 2731ec1e82fSSascha Hauer u32 dsa; 2741ec1e82fSSascha Hauer u32 ds; 2751ec1e82fSSascha Hauer u32 dd; 2761ec1e82fSSascha Hauer u32 scratch0; 2771ec1e82fSSascha Hauer u32 scratch1; 2781ec1e82fSSascha Hauer u32 scratch2; 2791ec1e82fSSascha Hauer u32 scratch3; 2801ec1e82fSSascha Hauer u32 scratch4; 2811ec1e82fSSascha Hauer u32 scratch5; 2821ec1e82fSSascha Hauer u32 scratch6; 2831ec1e82fSSascha Hauer u32 scratch7; 2841ec1e82fSSascha Hauer } __attribute__ ((packed)); 2851ec1e82fSSascha Hauer 2861ec1e82fSSascha Hauer #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor)) 2871ec1e82fSSascha Hauer 2881ec1e82fSSascha Hauer struct sdma_engine; 2891ec1e82fSSascha Hauer 2901ec1e82fSSascha Hauer /** 2911ec1e82fSSascha Hauer * struct sdma_channel - housekeeping for a SDMA channel 2921ec1e82fSSascha Hauer * 2931ec1e82fSSascha Hauer * @sdma pointer to the SDMA engine for this channel 29423889c63SSascha Hauer * @channel the channel number, matches dmaengine chan_id + 1 2951ec1e82fSSascha Hauer * @direction transfer type. Needed for setting SDMA script 2961ec1e82fSSascha Hauer * @peripheral_type Peripheral type. Needed for setting SDMA script 2971ec1e82fSSascha Hauer * @event_id0 aka dma request line 2981ec1e82fSSascha Hauer * @event_id1 for channels that use 2 events 2991ec1e82fSSascha Hauer * @word_size peripheral access size 3001ec1e82fSSascha Hauer * @buf_tail ID of the buffer that was processed 3011ec1e82fSSascha Hauer * @num_bd max NUM_BD. number of descriptors currently handling 3021ec1e82fSSascha Hauer */ 3031ec1e82fSSascha Hauer struct sdma_channel { 3041ec1e82fSSascha Hauer struct sdma_engine *sdma; 3051ec1e82fSSascha Hauer unsigned int channel; 306db8196dfSVinod Koul enum dma_transfer_direction direction; 3071ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type; 3081ec1e82fSSascha Hauer unsigned int event_id0; 3091ec1e82fSSascha Hauer unsigned int event_id1; 3101ec1e82fSSascha Hauer enum dma_slave_buswidth word_size; 3111ec1e82fSSascha Hauer unsigned int buf_tail; 3121ec1e82fSSascha Hauer unsigned int num_bd; 313d1a792f3SRussell King - ARM Linux unsigned int period_len; 3141ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 3151ec1e82fSSascha Hauer dma_addr_t bd_phys; 3161ec1e82fSSascha Hauer unsigned int pc_from_device, pc_to_device; 3178391ecf4SShengjiu Wang unsigned int device_to_device; 3181ec1e82fSSascha Hauer unsigned long flags; 3198391ecf4SShengjiu Wang dma_addr_t per_address, per_address2; 3200bbc1413SRichard Zhao unsigned long event_mask[2]; 3210bbc1413SRichard Zhao unsigned long watermark_level; 3221ec1e82fSSascha Hauer u32 shp_addr, per_addr; 3231ec1e82fSSascha Hauer struct dma_chan chan; 3241ec1e82fSSascha Hauer spinlock_t lock; 3251ec1e82fSSascha Hauer struct dma_async_tx_descriptor desc; 3261ec1e82fSSascha Hauer enum dma_status status; 327ab59a510SHuang Shijie unsigned int chn_count; 328ab59a510SHuang Shijie unsigned int chn_real_count; 329abd9ccc8SHuang Shijie struct tasklet_struct tasklet; 3300b351865SNicolin Chen struct imx_dma_data data; 3311ec1e82fSSascha Hauer }; 3321ec1e82fSSascha Hauer 3330bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP BIT(0) 3341ec1e82fSSascha Hauer 3351ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32 3361ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1 3371ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1 3381ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7 3391ec1e82fSSascha Hauer 3401ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453 3411ec1e82fSSascha Hauer 3421ec1e82fSSascha Hauer /** 3431ec1e82fSSascha Hauer * struct sdma_firmware_header - Layout of the firmware image 3441ec1e82fSSascha Hauer * 3451ec1e82fSSascha Hauer * @magic "SDMA" 3461ec1e82fSSascha Hauer * @version_major increased whenever layout of struct sdma_script_start_addrs 3471ec1e82fSSascha Hauer * changes. 3481ec1e82fSSascha Hauer * @version_minor firmware minor version (for binary compatible changes) 3491ec1e82fSSascha Hauer * @script_addrs_start offset of struct sdma_script_start_addrs in this image 3501ec1e82fSSascha Hauer * @num_script_addrs Number of script addresses in this image 3511ec1e82fSSascha Hauer * @ram_code_start offset of SDMA ram image in this firmware image 3521ec1e82fSSascha Hauer * @ram_code_size size of SDMA ram image 3531ec1e82fSSascha Hauer * @script_addrs Stores the start address of the SDMA scripts 3541ec1e82fSSascha Hauer * (in SDMA memory space) 3551ec1e82fSSascha Hauer */ 3561ec1e82fSSascha Hauer struct sdma_firmware_header { 3571ec1e82fSSascha Hauer u32 magic; 3581ec1e82fSSascha Hauer u32 version_major; 3591ec1e82fSSascha Hauer u32 version_minor; 3601ec1e82fSSascha Hauer u32 script_addrs_start; 3611ec1e82fSSascha Hauer u32 num_script_addrs; 3621ec1e82fSSascha Hauer u32 ram_code_start; 3631ec1e82fSSascha Hauer u32 ram_code_size; 3641ec1e82fSSascha Hauer }; 3651ec1e82fSSascha Hauer 36617bba72fSSascha Hauer struct sdma_driver_data { 36717bba72fSSascha Hauer int chnenbl0; 36817bba72fSSascha Hauer int num_events; 369dcfec3c0SSascha Hauer struct sdma_script_start_addrs *script_addrs; 37062550cd7SShawn Guo }; 37162550cd7SShawn Guo 3721ec1e82fSSascha Hauer struct sdma_engine { 3731ec1e82fSSascha Hauer struct device *dev; 374b9b3f82fSSascha Hauer struct device_dma_parameters dma_parms; 3751ec1e82fSSascha Hauer struct sdma_channel channel[MAX_DMA_CHANNELS]; 3761ec1e82fSSascha Hauer struct sdma_channel_control *channel_control; 3771ec1e82fSSascha Hauer void __iomem *regs; 3781ec1e82fSSascha Hauer struct sdma_context_data *context; 3791ec1e82fSSascha Hauer dma_addr_t context_phys; 3801ec1e82fSSascha Hauer struct dma_device dma_device; 3817560e3f3SSascha Hauer struct clk *clk_ipg; 3827560e3f3SSascha Hauer struct clk *clk_ahb; 3832ccaef05SRichard Zhao spinlock_t channel_0_lock; 384cd72b846SNicolin Chen u32 script_number; 3851ec1e82fSSascha Hauer struct sdma_script_start_addrs *script_addrs; 38617bba72fSSascha Hauer const struct sdma_driver_data *drvdata; 3878391ecf4SShengjiu Wang u32 spba_start_addr; 3888391ecf4SShengjiu Wang u32 spba_end_addr; 3895bb9dbb5SVinod Koul unsigned int irq; 39017bba72fSSascha Hauer }; 39117bba72fSSascha Hauer 392e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = { 39317bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX31, 39417bba72fSSascha Hauer .num_events = 32, 39517bba72fSSascha Hauer }; 39617bba72fSSascha Hauer 397dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = { 398dcfec3c0SSascha Hauer .ap_2_ap_addr = 729, 399dcfec3c0SSascha Hauer .uart_2_mcu_addr = 904, 400dcfec3c0SSascha Hauer .per_2_app_addr = 1255, 401dcfec3c0SSascha Hauer .mcu_2_app_addr = 834, 402dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1120, 403dcfec3c0SSascha Hauer .per_2_shp_addr = 1329, 404dcfec3c0SSascha Hauer .mcu_2_shp_addr = 1048, 405dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1560, 406dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1479, 407dcfec3c0SSascha Hauer .app_2_per_addr = 1189, 408dcfec3c0SSascha Hauer .app_2_mcu_addr = 770, 409dcfec3c0SSascha Hauer .shp_2_per_addr = 1407, 410dcfec3c0SSascha Hauer .shp_2_mcu_addr = 979, 411dcfec3c0SSascha Hauer }; 412dcfec3c0SSascha Hauer 413e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = { 414dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 415dcfec3c0SSascha Hauer .num_events = 48, 416dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx25, 417dcfec3c0SSascha Hauer }; 418dcfec3c0SSascha Hauer 419e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = { 42017bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 42117bba72fSSascha Hauer .num_events = 48, 4221ec1e82fSSascha Hauer }; 4231ec1e82fSSascha Hauer 424dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = { 425dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 426dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 427dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 428dcfec3c0SSascha Hauer .mcu_2_shp_addr = 961, 429dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1473, 430dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1392, 431dcfec3c0SSascha Hauer .app_2_per_addr = 1033, 432dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 433dcfec3c0SSascha Hauer .shp_2_per_addr = 1251, 434dcfec3c0SSascha Hauer .shp_2_mcu_addr = 892, 435dcfec3c0SSascha Hauer }; 436dcfec3c0SSascha Hauer 437e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = { 438dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 439dcfec3c0SSascha Hauer .num_events = 48, 440dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx51, 441dcfec3c0SSascha Hauer }; 442dcfec3c0SSascha Hauer 443dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = { 444dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 445dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 446dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 447dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 448dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 449dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 450dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 451dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 452dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 453dcfec3c0SSascha Hauer .firi_2_mcu_addr = 1193, 454dcfec3c0SSascha Hauer .mcu_2_firi_addr = 1290, 455dcfec3c0SSascha Hauer }; 456dcfec3c0SSascha Hauer 457e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = { 458dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 459dcfec3c0SSascha Hauer .num_events = 48, 460dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx53, 461dcfec3c0SSascha Hauer }; 462dcfec3c0SSascha Hauer 463dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = { 464dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 465dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 466dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 467dcfec3c0SSascha Hauer .per_2_per_addr = 6331, 468dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 469dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 470dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 471dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 472dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 473dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 474dcfec3c0SSascha Hauer }; 475dcfec3c0SSascha Hauer 476e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = { 477dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 478dcfec3c0SSascha Hauer .num_events = 48, 479dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx6q, 480dcfec3c0SSascha Hauer }; 481dcfec3c0SSascha Hauer 482*b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = { 483*b7d2648aSFabio Estevam .ap_2_ap_addr = 644, 484*b7d2648aSFabio Estevam .uart_2_mcu_addr = 819, 485*b7d2648aSFabio Estevam .mcu_2_app_addr = 749, 486*b7d2648aSFabio Estevam .uartsh_2_mcu_addr = 1034, 487*b7d2648aSFabio Estevam .mcu_2_shp_addr = 962, 488*b7d2648aSFabio Estevam .app_2_mcu_addr = 685, 489*b7d2648aSFabio Estevam .shp_2_mcu_addr = 893, 490*b7d2648aSFabio Estevam .spdif_2_mcu_addr = 1102, 491*b7d2648aSFabio Estevam .mcu_2_spdif_addr = 1136, 492*b7d2648aSFabio Estevam }; 493*b7d2648aSFabio Estevam 494*b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = { 495*b7d2648aSFabio Estevam .chnenbl0 = SDMA_CHNENBL0_IMX35, 496*b7d2648aSFabio Estevam .num_events = 48, 497*b7d2648aSFabio Estevam .script_addrs = &sdma_script_imx7d, 498*b7d2648aSFabio Estevam }; 499*b7d2648aSFabio Estevam 500afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = { 50162550cd7SShawn Guo { 502dcfec3c0SSascha Hauer .name = "imx25-sdma", 503dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx25, 504dcfec3c0SSascha Hauer }, { 50562550cd7SShawn Guo .name = "imx31-sdma", 50617bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx31, 50762550cd7SShawn Guo }, { 50862550cd7SShawn Guo .name = "imx35-sdma", 50917bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx35, 51062550cd7SShawn Guo }, { 511dcfec3c0SSascha Hauer .name = "imx51-sdma", 512dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx51, 513dcfec3c0SSascha Hauer }, { 514dcfec3c0SSascha Hauer .name = "imx53-sdma", 515dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx53, 516dcfec3c0SSascha Hauer }, { 517dcfec3c0SSascha Hauer .name = "imx6q-sdma", 518dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx6q, 519dcfec3c0SSascha Hauer }, { 520*b7d2648aSFabio Estevam .name = "imx7d-sdma", 521*b7d2648aSFabio Estevam .driver_data = (unsigned long)&sdma_imx7d, 522*b7d2648aSFabio Estevam }, { 52362550cd7SShawn Guo /* sentinel */ 52462550cd7SShawn Guo } 52562550cd7SShawn Guo }; 52662550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes); 52762550cd7SShawn Guo 528580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = { 529dcfec3c0SSascha Hauer { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, }, 530dcfec3c0SSascha Hauer { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, }, 531dcfec3c0SSascha Hauer { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, }, 53217bba72fSSascha Hauer { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, }, 533dcfec3c0SSascha Hauer { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, }, 53463edea16SMarkus Pargmann { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, }, 535*b7d2648aSFabio Estevam { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, }, 536580975d7SShawn Guo { /* sentinel */ } 537580975d7SShawn Guo }; 538580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids); 539580975d7SShawn Guo 5400bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */ 5410bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */ 5420bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */ 5431ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 5441ec1e82fSSascha Hauer 5451ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 5461ec1e82fSSascha Hauer { 54717bba72fSSascha Hauer u32 chnenbl0 = sdma->drvdata->chnenbl0; 5481ec1e82fSSascha Hauer return chnenbl0 + event * 4; 5491ec1e82fSSascha Hauer } 5501ec1e82fSSascha Hauer 5511ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 5521ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 5531ec1e82fSSascha Hauer { 5541ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 5551ec1e82fSSascha Hauer int channel = sdmac->channel; 5560bbc1413SRichard Zhao unsigned long evt, mcu, dsp; 5571ec1e82fSSascha Hauer 5581ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 5591ec1e82fSSascha Hauer return -EINVAL; 5601ec1e82fSSascha Hauer 561c4b56857SRichard Zhao evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); 562c4b56857SRichard Zhao mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); 563c4b56857SRichard Zhao dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); 5641ec1e82fSSascha Hauer 5651ec1e82fSSascha Hauer if (dsp_override) 5660bbc1413SRichard Zhao __clear_bit(channel, &dsp); 5671ec1e82fSSascha Hauer else 5680bbc1413SRichard Zhao __set_bit(channel, &dsp); 5691ec1e82fSSascha Hauer 5701ec1e82fSSascha Hauer if (event_override) 5710bbc1413SRichard Zhao __clear_bit(channel, &evt); 5721ec1e82fSSascha Hauer else 5730bbc1413SRichard Zhao __set_bit(channel, &evt); 5741ec1e82fSSascha Hauer 5751ec1e82fSSascha Hauer if (mcu_override) 5760bbc1413SRichard Zhao __clear_bit(channel, &mcu); 5771ec1e82fSSascha Hauer else 5780bbc1413SRichard Zhao __set_bit(channel, &mcu); 5791ec1e82fSSascha Hauer 580c4b56857SRichard Zhao writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); 581c4b56857SRichard Zhao writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); 582c4b56857SRichard Zhao writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); 5831ec1e82fSSascha Hauer 5841ec1e82fSSascha Hauer return 0; 5851ec1e82fSSascha Hauer } 5861ec1e82fSSascha Hauer 587b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 588b9a59166SRichard Zhao { 5890bbc1413SRichard Zhao writel(BIT(channel), sdma->regs + SDMA_H_START); 590b9a59166SRichard Zhao } 591b9a59166SRichard Zhao 5921ec1e82fSSascha Hauer /* 5932ccaef05SRichard Zhao * sdma_run_channel0 - run a channel and wait till it's done 5941ec1e82fSSascha Hauer */ 5952ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma) 5961ec1e82fSSascha Hauer { 5971ec1e82fSSascha Hauer int ret; 5981d069bfaSMichael Olbrich u32 reg; 5991ec1e82fSSascha Hauer 6002ccaef05SRichard Zhao sdma_enable_channel(sdma, 0); 6011ec1e82fSSascha Hauer 6021d069bfaSMichael Olbrich ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP, 6031d069bfaSMichael Olbrich reg, !(reg & 1), 1, 500); 6041d069bfaSMichael Olbrich if (ret) 6052ccaef05SRichard Zhao dev_err(sdma->dev, "Timeout waiting for CH0 ready\n"); 6061ec1e82fSSascha Hauer 607855832e4SRobin Gong /* Set bits of CONFIG register with dynamic context switching */ 608855832e4SRobin Gong if (readl(sdma->regs + SDMA_H_CONFIG) == 0) 609855832e4SRobin Gong writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG); 610855832e4SRobin Gong 6111d069bfaSMichael Olbrich return ret; 6121ec1e82fSSascha Hauer } 6131ec1e82fSSascha Hauer 6141ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 6151ec1e82fSSascha Hauer u32 address) 6161ec1e82fSSascha Hauer { 6171ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 6181ec1e82fSSascha Hauer void *buf_virt; 6191ec1e82fSSascha Hauer dma_addr_t buf_phys; 6201ec1e82fSSascha Hauer int ret; 6212ccaef05SRichard Zhao unsigned long flags; 62273eab978SSascha Hauer 6231ec1e82fSSascha Hauer buf_virt = dma_alloc_coherent(NULL, 6241ec1e82fSSascha Hauer size, 6251ec1e82fSSascha Hauer &buf_phys, GFP_KERNEL); 62673eab978SSascha Hauer if (!buf_virt) { 6272ccaef05SRichard Zhao return -ENOMEM; 62873eab978SSascha Hauer } 6291ec1e82fSSascha Hauer 6302ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 6312ccaef05SRichard Zhao 6321ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 6331ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 6341ec1e82fSSascha Hauer bd0->mode.count = size / 2; 6351ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 6361ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 6371ec1e82fSSascha Hauer 6381ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 6391ec1e82fSSascha Hauer 6402ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 6412ccaef05SRichard Zhao 6422ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 6431ec1e82fSSascha Hauer 6441ec1e82fSSascha Hauer dma_free_coherent(NULL, size, buf_virt, buf_phys); 6451ec1e82fSSascha Hauer 6461ec1e82fSSascha Hauer return ret; 6471ec1e82fSSascha Hauer } 6481ec1e82fSSascha Hauer 6491ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 6501ec1e82fSSascha Hauer { 6511ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6521ec1e82fSSascha Hauer int channel = sdmac->channel; 6530bbc1413SRichard Zhao unsigned long val; 6541ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 6551ec1e82fSSascha Hauer 656c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 6570bbc1413SRichard Zhao __set_bit(channel, &val); 658c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 6591ec1e82fSSascha Hauer } 6601ec1e82fSSascha Hauer 6611ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 6621ec1e82fSSascha Hauer { 6631ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6641ec1e82fSSascha Hauer int channel = sdmac->channel; 6651ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 6660bbc1413SRichard Zhao unsigned long val; 6671ec1e82fSSascha Hauer 668c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 6690bbc1413SRichard Zhao __clear_bit(channel, &val); 670c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 6711ec1e82fSSascha Hauer } 6721ec1e82fSSascha Hauer 6731ec1e82fSSascha Hauer static void sdma_handle_channel_loop(struct sdma_channel *sdmac) 6741ec1e82fSSascha Hauer { 675d1a792f3SRussell King - ARM Linux if (sdmac->desc.callback) 676d1a792f3SRussell King - ARM Linux sdmac->desc.callback(sdmac->desc.callback_param); 677d1a792f3SRussell King - ARM Linux } 678d1a792f3SRussell King - ARM Linux 679d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac) 680d1a792f3SRussell King - ARM Linux { 6811ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 6821ec1e82fSSascha Hauer 6831ec1e82fSSascha Hauer /* 6841ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 6851ec1e82fSSascha Hauer * call callback function. 6861ec1e82fSSascha Hauer */ 6871ec1e82fSSascha Hauer while (1) { 6881ec1e82fSSascha Hauer bd = &sdmac->bd[sdmac->buf_tail]; 6891ec1e82fSSascha Hauer 6901ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 6911ec1e82fSSascha Hauer break; 6921ec1e82fSSascha Hauer 6931ec1e82fSSascha Hauer if (bd->mode.status & BD_RROR) 6941ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 6951ec1e82fSSascha Hauer 6961ec1e82fSSascha Hauer bd->mode.status |= BD_DONE; 6971ec1e82fSSascha Hauer sdmac->buf_tail++; 6981ec1e82fSSascha Hauer sdmac->buf_tail %= sdmac->num_bd; 6991ec1e82fSSascha Hauer } 7001ec1e82fSSascha Hauer } 7011ec1e82fSSascha Hauer 7021ec1e82fSSascha Hauer static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac) 7031ec1e82fSSascha Hauer { 7041ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 7051ec1e82fSSascha Hauer int i, error = 0; 7061ec1e82fSSascha Hauer 707ab59a510SHuang Shijie sdmac->chn_real_count = 0; 7081ec1e82fSSascha Hauer /* 7091ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 7101ec1e82fSSascha Hauer * errors and call callback function 7111ec1e82fSSascha Hauer */ 7121ec1e82fSSascha Hauer for (i = 0; i < sdmac->num_bd; i++) { 7131ec1e82fSSascha Hauer bd = &sdmac->bd[i]; 7141ec1e82fSSascha Hauer 7151ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 7161ec1e82fSSascha Hauer error = -EIO; 717ab59a510SHuang Shijie sdmac->chn_real_count += bd->mode.count; 7181ec1e82fSSascha Hauer } 7191ec1e82fSSascha Hauer 7201ec1e82fSSascha Hauer if (error) 7211ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 7221ec1e82fSSascha Hauer else 723409bff6aSVinod Koul sdmac->status = DMA_COMPLETE; 7241ec1e82fSSascha Hauer 725f7fbce07SRussell King - ARM Linux dma_cookie_complete(&sdmac->desc); 7261ec1e82fSSascha Hauer if (sdmac->desc.callback) 7271ec1e82fSSascha Hauer sdmac->desc.callback(sdmac->desc.callback_param); 7281ec1e82fSSascha Hauer } 7291ec1e82fSSascha Hauer 730abd9ccc8SHuang Shijie static void sdma_tasklet(unsigned long data) 7311ec1e82fSSascha Hauer { 732abd9ccc8SHuang Shijie struct sdma_channel *sdmac = (struct sdma_channel *) data; 733abd9ccc8SHuang Shijie 7341ec1e82fSSascha Hauer if (sdmac->flags & IMX_DMA_SG_LOOP) 7351ec1e82fSSascha Hauer sdma_handle_channel_loop(sdmac); 7361ec1e82fSSascha Hauer else 7371ec1e82fSSascha Hauer mxc_sdma_handle_channel_normal(sdmac); 7381ec1e82fSSascha Hauer } 7391ec1e82fSSascha Hauer 7401ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 7411ec1e82fSSascha Hauer { 7421ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 7430bbc1413SRichard Zhao unsigned long stat; 7441ec1e82fSSascha Hauer 745c4b56857SRichard Zhao stat = readl_relaxed(sdma->regs + SDMA_H_INTR); 746c4b56857SRichard Zhao writel_relaxed(stat, sdma->regs + SDMA_H_INTR); 7471d069bfaSMichael Olbrich /* channel 0 is special and not handled here, see run_channel0() */ 7481d069bfaSMichael Olbrich stat &= ~1; 7491ec1e82fSSascha Hauer 7501ec1e82fSSascha Hauer while (stat) { 7511ec1e82fSSascha Hauer int channel = fls(stat) - 1; 7521ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 7531ec1e82fSSascha Hauer 754d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 755d1a792f3SRussell King - ARM Linux sdma_update_channel_loop(sdmac); 756d1a792f3SRussell King - ARM Linux 757abd9ccc8SHuang Shijie tasklet_schedule(&sdmac->tasklet); 7581ec1e82fSSascha Hauer 7590bbc1413SRichard Zhao __clear_bit(channel, &stat); 7601ec1e82fSSascha Hauer } 7611ec1e82fSSascha Hauer 7621ec1e82fSSascha Hauer return IRQ_HANDLED; 7631ec1e82fSSascha Hauer } 7641ec1e82fSSascha Hauer 7651ec1e82fSSascha Hauer /* 7661ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 7671ec1e82fSSascha Hauer */ 7681ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac, 7691ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 7701ec1e82fSSascha Hauer { 7711ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7721ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 7731ec1e82fSSascha Hauer /* 7741ec1e82fSSascha Hauer * These are needed once we start to support transfers between 7751ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 7761ec1e82fSSascha Hauer */ 7770d605ba0SVinod Koul int per_2_per = 0; 7781ec1e82fSSascha Hauer 7791ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 7801ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 7818391ecf4SShengjiu Wang sdmac->device_to_device = 0; 7821ec1e82fSSascha Hauer 7831ec1e82fSSascha Hauer switch (peripheral_type) { 7841ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 7851ec1e82fSSascha Hauer break; 7861ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 7871ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 7881ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 7891ec1e82fSSascha Hauer break; 7901ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 7911ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 7921ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 7931ec1e82fSSascha Hauer break; 7941ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 7951ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 7961ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 7971ec1e82fSSascha Hauer break; 7981ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 7991ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 8001ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 8011ec1e82fSSascha Hauer break; 8021ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 8031ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 8041ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 8051ec1e82fSSascha Hauer break; 8061ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 8071ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 8081ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 80929aebfdeSNicolin Chen case IMX_DMATYPE_SAI: 8101ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 8111ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 8121ec1e82fSSascha Hauer break; 8131a895578SNicolin Chen case IMX_DMATYPE_SSI_DUAL: 8141a895578SNicolin Chen per_2_emi = sdma->script_addrs->ssish_2_mcu_addr; 8151a895578SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_ssish_addr; 8161a895578SNicolin Chen break; 8171ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 8181ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 8191ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 8201ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 8211ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 8221ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 8231ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 8241ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 8251ec1e82fSSascha Hauer break; 8261ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 8271ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 8281ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 8291ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 8301ec1e82fSSascha Hauer break; 831f892afb0SNicolin Chen case IMX_DMATYPE_ASRC_SP: 832f892afb0SNicolin Chen per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 833f892afb0SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 834f892afb0SNicolin Chen per_2_per = sdma->script_addrs->per_2_per_addr; 835f892afb0SNicolin Chen break; 8361ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 8371ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 8381ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 8391ec1e82fSSascha Hauer break; 8401ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 8411ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 8421ec1e82fSSascha Hauer break; 8431ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 8441ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 8451ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 8461ec1e82fSSascha Hauer break; 8471ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 8481ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 8491ec1e82fSSascha Hauer break; 8501ec1e82fSSascha Hauer default: 8511ec1e82fSSascha Hauer break; 8521ec1e82fSSascha Hauer } 8531ec1e82fSSascha Hauer 8541ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 8551ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 8568391ecf4SShengjiu Wang sdmac->device_to_device = per_2_per; 8571ec1e82fSSascha Hauer } 8581ec1e82fSSascha Hauer 8591ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 8601ec1e82fSSascha Hauer { 8611ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8621ec1e82fSSascha Hauer int channel = sdmac->channel; 8631ec1e82fSSascha Hauer int load_address; 8641ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 8651ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 8661ec1e82fSSascha Hauer int ret; 8672ccaef05SRichard Zhao unsigned long flags; 8681ec1e82fSSascha Hauer 8698391ecf4SShengjiu Wang if (sdmac->direction == DMA_DEV_TO_MEM) 8701ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 8718391ecf4SShengjiu Wang else if (sdmac->direction == DMA_DEV_TO_DEV) 8728391ecf4SShengjiu Wang load_address = sdmac->device_to_device; 8738391ecf4SShengjiu Wang else 8741ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 8751ec1e82fSSascha Hauer 8761ec1e82fSSascha Hauer if (load_address < 0) 8771ec1e82fSSascha Hauer return load_address; 8781ec1e82fSSascha Hauer 8791ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 8800bbc1413SRichard Zhao dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level); 8811ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 8821ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 8830bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]); 8840bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]); 8851ec1e82fSSascha Hauer 8862ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 88773eab978SSascha Hauer 8881ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 8891ec1e82fSSascha Hauer context->channel_state.pc = load_address; 8901ec1e82fSSascha Hauer 8911ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 8921ec1e82fSSascha Hauer * and watermark level 8931ec1e82fSSascha Hauer */ 8940bbc1413SRichard Zhao context->gReg[0] = sdmac->event_mask[1]; 8950bbc1413SRichard Zhao context->gReg[1] = sdmac->event_mask[0]; 8961ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 8971ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 8981ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 8991ec1e82fSSascha Hauer 9001ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 9011ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 9021ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 9031ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 9041ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 9052ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 9061ec1e82fSSascha Hauer 9072ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 90873eab978SSascha Hauer 9091ec1e82fSSascha Hauer return ret; 9101ec1e82fSSascha Hauer } 9111ec1e82fSSascha Hauer 9127b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 9131ec1e82fSSascha Hauer { 9147b350ab0SMaxime Ripard return container_of(chan, struct sdma_channel, chan); 9157b350ab0SMaxime Ripard } 9167b350ab0SMaxime Ripard 9177b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan) 9187b350ab0SMaxime Ripard { 9197b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 9201ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9211ec1e82fSSascha Hauer int channel = sdmac->channel; 9221ec1e82fSSascha Hauer 9230bbc1413SRichard Zhao writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP); 9241ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 9257b350ab0SMaxime Ripard 9267b350ab0SMaxime Ripard return 0; 9271ec1e82fSSascha Hauer } 9281ec1e82fSSascha Hauer 9298391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac) 9308391ecf4SShengjiu Wang { 9318391ecf4SShengjiu Wang struct sdma_engine *sdma = sdmac->sdma; 9328391ecf4SShengjiu Wang 9338391ecf4SShengjiu Wang int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML; 9348391ecf4SShengjiu Wang int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16; 9358391ecf4SShengjiu Wang 9368391ecf4SShengjiu Wang set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]); 9378391ecf4SShengjiu Wang set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]); 9388391ecf4SShengjiu Wang 9398391ecf4SShengjiu Wang if (sdmac->event_id0 > 31) 9408391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE; 9418391ecf4SShengjiu Wang 9428391ecf4SShengjiu Wang if (sdmac->event_id1 > 31) 9438391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE; 9448391ecf4SShengjiu Wang 9458391ecf4SShengjiu Wang /* 9468391ecf4SShengjiu Wang * If LWML(src_maxburst) > HWML(dst_maxburst), we need 9478391ecf4SShengjiu Wang * swap LWML and HWML of INFO(A.3.2.5.1), also need swap 9488391ecf4SShengjiu Wang * r0(event_mask[1]) and r1(event_mask[0]). 9498391ecf4SShengjiu Wang */ 9508391ecf4SShengjiu Wang if (lwml > hwml) { 9518391ecf4SShengjiu Wang sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML | 9528391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML); 9538391ecf4SShengjiu Wang sdmac->watermark_level |= hwml; 9548391ecf4SShengjiu Wang sdmac->watermark_level |= lwml << 16; 9558391ecf4SShengjiu Wang swap(sdmac->event_mask[0], sdmac->event_mask[1]); 9568391ecf4SShengjiu Wang } 9578391ecf4SShengjiu Wang 9588391ecf4SShengjiu Wang if (sdmac->per_address2 >= sdma->spba_start_addr && 9598391ecf4SShengjiu Wang sdmac->per_address2 <= sdma->spba_end_addr) 9608391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP; 9618391ecf4SShengjiu Wang 9628391ecf4SShengjiu Wang if (sdmac->per_address >= sdma->spba_start_addr && 9638391ecf4SShengjiu Wang sdmac->per_address <= sdma->spba_end_addr) 9648391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP; 9658391ecf4SShengjiu Wang 9668391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT; 9678391ecf4SShengjiu Wang } 9688391ecf4SShengjiu Wang 9697b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan) 9701ec1e82fSSascha Hauer { 9717b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 9721ec1e82fSSascha Hauer int ret; 9731ec1e82fSSascha Hauer 9747b350ab0SMaxime Ripard sdma_disable_channel(chan); 9751ec1e82fSSascha Hauer 9760bbc1413SRichard Zhao sdmac->event_mask[0] = 0; 9770bbc1413SRichard Zhao sdmac->event_mask[1] = 0; 9781ec1e82fSSascha Hauer sdmac->shp_addr = 0; 9791ec1e82fSSascha Hauer sdmac->per_addr = 0; 9801ec1e82fSSascha Hauer 9811ec1e82fSSascha Hauer if (sdmac->event_id0) { 98217bba72fSSascha Hauer if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events) 9831ec1e82fSSascha Hauer return -EINVAL; 9841ec1e82fSSascha Hauer sdma_event_enable(sdmac, sdmac->event_id0); 9851ec1e82fSSascha Hauer } 9861ec1e82fSSascha Hauer 9878391ecf4SShengjiu Wang if (sdmac->event_id1) { 9888391ecf4SShengjiu Wang if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events) 9898391ecf4SShengjiu Wang return -EINVAL; 9908391ecf4SShengjiu Wang sdma_event_enable(sdmac, sdmac->event_id1); 9918391ecf4SShengjiu Wang } 9928391ecf4SShengjiu Wang 9931ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 9941ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 9951ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 9961ec1e82fSSascha Hauer break; 9971ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 9981ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 9991ec1e82fSSascha Hauer break; 10001ec1e82fSSascha Hauer default: 10011ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 10021ec1e82fSSascha Hauer break; 10031ec1e82fSSascha Hauer } 10041ec1e82fSSascha Hauer 10051ec1e82fSSascha Hauer sdma_get_pc(sdmac, sdmac->peripheral_type); 10061ec1e82fSSascha Hauer 10071ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 10081ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 10091ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 10101ec1e82fSSascha Hauer if (sdmac->event_id1) { 10118391ecf4SShengjiu Wang if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP || 10128391ecf4SShengjiu Wang sdmac->peripheral_type == IMX_DMATYPE_ASRC) 10138391ecf4SShengjiu Wang sdma_set_watermarklevel_for_p2p(sdmac); 10148391ecf4SShengjiu Wang } else 10150bbc1413SRichard Zhao __set_bit(sdmac->event_id0, sdmac->event_mask); 10168391ecf4SShengjiu Wang 10171ec1e82fSSascha Hauer /* Address */ 10181ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 10198391ecf4SShengjiu Wang sdmac->per_addr = sdmac->per_address2; 10201ec1e82fSSascha Hauer } else { 10211ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 10221ec1e82fSSascha Hauer } 10231ec1e82fSSascha Hauer 10241ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 10251ec1e82fSSascha Hauer 10261ec1e82fSSascha Hauer return ret; 10271ec1e82fSSascha Hauer } 10281ec1e82fSSascha Hauer 10291ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 10301ec1e82fSSascha Hauer unsigned int priority) 10311ec1e82fSSascha Hauer { 10321ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10331ec1e82fSSascha Hauer int channel = sdmac->channel; 10341ec1e82fSSascha Hauer 10351ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 10361ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 10371ec1e82fSSascha Hauer return -EINVAL; 10381ec1e82fSSascha Hauer } 10391ec1e82fSSascha Hauer 1040c4b56857SRichard Zhao writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 10411ec1e82fSSascha Hauer 10421ec1e82fSSascha Hauer return 0; 10431ec1e82fSSascha Hauer } 10441ec1e82fSSascha Hauer 10451ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac) 10461ec1e82fSSascha Hauer { 10471ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10481ec1e82fSSascha Hauer int channel = sdmac->channel; 10491ec1e82fSSascha Hauer int ret = -EBUSY; 10501ec1e82fSSascha Hauer 10519f92d223SJoe Perches sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, 10529f92d223SJoe Perches GFP_KERNEL); 10531ec1e82fSSascha Hauer if (!sdmac->bd) { 10541ec1e82fSSascha Hauer ret = -ENOMEM; 10551ec1e82fSSascha Hauer goto out; 10561ec1e82fSSascha Hauer } 10571ec1e82fSSascha Hauer 10581ec1e82fSSascha Hauer sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys; 10591ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 10601ec1e82fSSascha Hauer 10611ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY); 10621ec1e82fSSascha Hauer return 0; 10631ec1e82fSSascha Hauer out: 10641ec1e82fSSascha Hauer 10651ec1e82fSSascha Hauer return ret; 10661ec1e82fSSascha Hauer } 10671ec1e82fSSascha Hauer 10681ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx) 10691ec1e82fSSascha Hauer { 1070f69f2e26SHaitao Zhang unsigned long flags; 10711ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(tx->chan); 10721ec1e82fSSascha Hauer dma_cookie_t cookie; 10731ec1e82fSSascha Hauer 1074f69f2e26SHaitao Zhang spin_lock_irqsave(&sdmac->lock, flags); 10751ec1e82fSSascha Hauer 1076884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 10771ec1e82fSSascha Hauer 1078f69f2e26SHaitao Zhang spin_unlock_irqrestore(&sdmac->lock, flags); 10791ec1e82fSSascha Hauer 10801ec1e82fSSascha Hauer return cookie; 10811ec1e82fSSascha Hauer } 10821ec1e82fSSascha Hauer 10831ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 10841ec1e82fSSascha Hauer { 10851ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 10861ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 10871ec1e82fSSascha Hauer int prio, ret; 10881ec1e82fSSascha Hauer 10891ec1e82fSSascha Hauer if (!data) 10901ec1e82fSSascha Hauer return -EINVAL; 10911ec1e82fSSascha Hauer 10921ec1e82fSSascha Hauer switch (data->priority) { 10931ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 10941ec1e82fSSascha Hauer prio = 3; 10951ec1e82fSSascha Hauer break; 10961ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 10971ec1e82fSSascha Hauer prio = 2; 10981ec1e82fSSascha Hauer break; 10991ec1e82fSSascha Hauer case DMA_PRIO_LOW: 11001ec1e82fSSascha Hauer default: 11011ec1e82fSSascha Hauer prio = 1; 11021ec1e82fSSascha Hauer break; 11031ec1e82fSSascha Hauer } 11041ec1e82fSSascha Hauer 11051ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 11061ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 11078391ecf4SShengjiu Wang sdmac->event_id1 = data->dma_request2; 1108c2c744d3SRichard Zhao 1109b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ipg); 1110b93edcddSFabio Estevam if (ret) 1111b93edcddSFabio Estevam return ret; 1112b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ahb); 1113b93edcddSFabio Estevam if (ret) 1114b93edcddSFabio Estevam goto disable_clk_ipg; 1115c2c744d3SRichard Zhao 11163bb5e7caSRichard Zhao ret = sdma_request_channel(sdmac); 11171ec1e82fSSascha Hauer if (ret) 1118b93edcddSFabio Estevam goto disable_clk_ahb; 11191ec1e82fSSascha Hauer 11203bb5e7caSRichard Zhao ret = sdma_set_channel_priority(sdmac, prio); 11211ec1e82fSSascha Hauer if (ret) 1122b93edcddSFabio Estevam goto disable_clk_ahb; 11231ec1e82fSSascha Hauer 11241ec1e82fSSascha Hauer dma_async_tx_descriptor_init(&sdmac->desc, chan); 11251ec1e82fSSascha Hauer sdmac->desc.tx_submit = sdma_tx_submit; 11261ec1e82fSSascha Hauer /* txd.flags will be overwritten in prep funcs */ 11271ec1e82fSSascha Hauer sdmac->desc.flags = DMA_CTRL_ACK; 11281ec1e82fSSascha Hauer 11291ec1e82fSSascha Hauer return 0; 1130b93edcddSFabio Estevam 1131b93edcddSFabio Estevam disable_clk_ahb: 1132b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ahb); 1133b93edcddSFabio Estevam disable_clk_ipg: 1134b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ipg); 1135b93edcddSFabio Estevam return ret; 11361ec1e82fSSascha Hauer } 11371ec1e82fSSascha Hauer 11381ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 11391ec1e82fSSascha Hauer { 11401ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11411ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11421ec1e82fSSascha Hauer 11437b350ab0SMaxime Ripard sdma_disable_channel(chan); 11441ec1e82fSSascha Hauer 11451ec1e82fSSascha Hauer if (sdmac->event_id0) 11461ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 11471ec1e82fSSascha Hauer if (sdmac->event_id1) 11481ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 11491ec1e82fSSascha Hauer 11501ec1e82fSSascha Hauer sdmac->event_id0 = 0; 11511ec1e82fSSascha Hauer sdmac->event_id1 = 0; 11521ec1e82fSSascha Hauer 11531ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 11541ec1e82fSSascha Hauer 11551ec1e82fSSascha Hauer dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys); 11561ec1e82fSSascha Hauer 11577560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 11587560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 11591ec1e82fSSascha Hauer } 11601ec1e82fSSascha Hauer 11611ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 11621ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 1163db8196dfSVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 1164185ecb5fSAlexandre Bounine unsigned long flags, void *context) 11651ec1e82fSSascha Hauer { 11661ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11671ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11681ec1e82fSSascha Hauer int ret, i, count; 116923889c63SSascha Hauer int channel = sdmac->channel; 11701ec1e82fSSascha Hauer struct scatterlist *sg; 11711ec1e82fSSascha Hauer 11721ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 11731ec1e82fSSascha Hauer return NULL; 11741ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 11751ec1e82fSSascha Hauer 11761ec1e82fSSascha Hauer sdmac->flags = 0; 11771ec1e82fSSascha Hauer 11788e2e27c7SRichard Zhao sdmac->buf_tail = 0; 11798e2e27c7SRichard Zhao 11801ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 11811ec1e82fSSascha Hauer sg_len, channel); 11821ec1e82fSSascha Hauer 11831ec1e82fSSascha Hauer sdmac->direction = direction; 11841ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 11851ec1e82fSSascha Hauer if (ret) 11861ec1e82fSSascha Hauer goto err_out; 11871ec1e82fSSascha Hauer 11881ec1e82fSSascha Hauer if (sg_len > NUM_BD) { 11891ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 11901ec1e82fSSascha Hauer channel, sg_len, NUM_BD); 11911ec1e82fSSascha Hauer ret = -EINVAL; 11921ec1e82fSSascha Hauer goto err_out; 11931ec1e82fSSascha Hauer } 11941ec1e82fSSascha Hauer 1195ab59a510SHuang Shijie sdmac->chn_count = 0; 11961ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 11971ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 11981ec1e82fSSascha Hauer int param; 11991ec1e82fSSascha Hauer 1200d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 12011ec1e82fSSascha Hauer 1202fdaf9c4bSLars-Peter Clausen count = sg_dma_len(sg); 12031ec1e82fSSascha Hauer 12041ec1e82fSSascha Hauer if (count > 0xffff) { 12051ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 12061ec1e82fSSascha Hauer channel, count, 0xffff); 12071ec1e82fSSascha Hauer ret = -EINVAL; 12081ec1e82fSSascha Hauer goto err_out; 12091ec1e82fSSascha Hauer } 12101ec1e82fSSascha Hauer 12111ec1e82fSSascha Hauer bd->mode.count = count; 1212ab59a510SHuang Shijie sdmac->chn_count += count; 12131ec1e82fSSascha Hauer 12141ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { 12151ec1e82fSSascha Hauer ret = -EINVAL; 12161ec1e82fSSascha Hauer goto err_out; 12171ec1e82fSSascha Hauer } 12181fa81c27SSascha Hauer 12191fa81c27SSascha Hauer switch (sdmac->word_size) { 12201fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 12211ec1e82fSSascha Hauer bd->mode.command = 0; 12221fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 12231fa81c27SSascha Hauer return NULL; 12241fa81c27SSascha Hauer break; 12251fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 12261fa81c27SSascha Hauer bd->mode.command = 2; 12271fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 12281fa81c27SSascha Hauer return NULL; 12291fa81c27SSascha Hauer break; 12301fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 12311fa81c27SSascha Hauer bd->mode.command = 1; 12321fa81c27SSascha Hauer break; 12331fa81c27SSascha Hauer default: 12341fa81c27SSascha Hauer return NULL; 12351fa81c27SSascha Hauer } 12361ec1e82fSSascha Hauer 12371ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 12381ec1e82fSSascha Hauer 1239341b9419SShawn Guo if (i + 1 == sg_len) { 12401ec1e82fSSascha Hauer param |= BD_INTR; 1241341b9419SShawn Guo param |= BD_LAST; 1242341b9419SShawn Guo param &= ~BD_CONT; 12431ec1e82fSSascha Hauer } 12441ec1e82fSSascha Hauer 1245c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1246c3cc74b2SOlof Johansson i, count, (u64)sg->dma_address, 12471ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 12481ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 12491ec1e82fSSascha Hauer 12501ec1e82fSSascha Hauer bd->mode.status = param; 12511ec1e82fSSascha Hauer } 12521ec1e82fSSascha Hauer 12531ec1e82fSSascha Hauer sdmac->num_bd = sg_len; 12541ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 12551ec1e82fSSascha Hauer 12561ec1e82fSSascha Hauer return &sdmac->desc; 12571ec1e82fSSascha Hauer err_out: 12584b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 12591ec1e82fSSascha Hauer return NULL; 12601ec1e82fSSascha Hauer } 12611ec1e82fSSascha Hauer 12621ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 12631ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 1264185ecb5fSAlexandre Bounine size_t period_len, enum dma_transfer_direction direction, 126531c1e5a1SLaurent Pinchart unsigned long flags) 12661ec1e82fSSascha Hauer { 12671ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 12681ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 12691ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 127023889c63SSascha Hauer int channel = sdmac->channel; 12711ec1e82fSSascha Hauer int ret, i = 0, buf = 0; 12721ec1e82fSSascha Hauer 12731ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 12741ec1e82fSSascha Hauer 12751ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 12761ec1e82fSSascha Hauer return NULL; 12771ec1e82fSSascha Hauer 12781ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 12791ec1e82fSSascha Hauer 12808e2e27c7SRichard Zhao sdmac->buf_tail = 0; 1281d1a792f3SRussell King - ARM Linux sdmac->period_len = period_len; 12828e2e27c7SRichard Zhao 12831ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 12841ec1e82fSSascha Hauer sdmac->direction = direction; 12851ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 12861ec1e82fSSascha Hauer if (ret) 12871ec1e82fSSascha Hauer goto err_out; 12881ec1e82fSSascha Hauer 12891ec1e82fSSascha Hauer if (num_periods > NUM_BD) { 12901ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 12911ec1e82fSSascha Hauer channel, num_periods, NUM_BD); 12921ec1e82fSSascha Hauer goto err_out; 12931ec1e82fSSascha Hauer } 12941ec1e82fSSascha Hauer 12951ec1e82fSSascha Hauer if (period_len > 0xffff) { 12961ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n", 12971ec1e82fSSascha Hauer channel, period_len, 0xffff); 12981ec1e82fSSascha Hauer goto err_out; 12991ec1e82fSSascha Hauer } 13001ec1e82fSSascha Hauer 13011ec1e82fSSascha Hauer while (buf < buf_len) { 13021ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 13031ec1e82fSSascha Hauer int param; 13041ec1e82fSSascha Hauer 13051ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 13061ec1e82fSSascha Hauer 13071ec1e82fSSascha Hauer bd->mode.count = period_len; 13081ec1e82fSSascha Hauer 13091ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 13101ec1e82fSSascha Hauer goto err_out; 13111ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 13121ec1e82fSSascha Hauer bd->mode.command = 0; 13131ec1e82fSSascha Hauer else 13141ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 13151ec1e82fSSascha Hauer 13161ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 13171ec1e82fSSascha Hauer if (i + 1 == num_periods) 13181ec1e82fSSascha Hauer param |= BD_WRAP; 13191ec1e82fSSascha Hauer 1320c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1321c3cc74b2SOlof Johansson i, period_len, (u64)dma_addr, 13221ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 13231ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 13241ec1e82fSSascha Hauer 13251ec1e82fSSascha Hauer bd->mode.status = param; 13261ec1e82fSSascha Hauer 13271ec1e82fSSascha Hauer dma_addr += period_len; 13281ec1e82fSSascha Hauer buf += period_len; 13291ec1e82fSSascha Hauer 13301ec1e82fSSascha Hauer i++; 13311ec1e82fSSascha Hauer } 13321ec1e82fSSascha Hauer 13331ec1e82fSSascha Hauer sdmac->num_bd = num_periods; 13341ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 13351ec1e82fSSascha Hauer 13361ec1e82fSSascha Hauer return &sdmac->desc; 13371ec1e82fSSascha Hauer err_out: 13381ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 13391ec1e82fSSascha Hauer return NULL; 13401ec1e82fSSascha Hauer } 13411ec1e82fSSascha Hauer 13427b350ab0SMaxime Ripard static int sdma_config(struct dma_chan *chan, 13437b350ab0SMaxime Ripard struct dma_slave_config *dmaengine_cfg) 13441ec1e82fSSascha Hauer { 13451ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13461ec1e82fSSascha Hauer 1347db8196dfSVinod Koul if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) { 13481ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 134994ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->src_maxburst * 135094ac27a5SPhilippe Rétornaz dmaengine_cfg->src_addr_width; 13511ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 13528391ecf4SShengjiu Wang } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) { 13538391ecf4SShengjiu Wang sdmac->per_address2 = dmaengine_cfg->src_addr; 13548391ecf4SShengjiu Wang sdmac->per_address = dmaengine_cfg->dst_addr; 13558391ecf4SShengjiu Wang sdmac->watermark_level = dmaengine_cfg->src_maxburst & 13568391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_LWML; 13578391ecf4SShengjiu Wang sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) & 13588391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML; 13598391ecf4SShengjiu Wang sdmac->word_size = dmaengine_cfg->dst_addr_width; 13601ec1e82fSSascha Hauer } else { 13611ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 136294ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->dst_maxburst * 136394ac27a5SPhilippe Rétornaz dmaengine_cfg->dst_addr_width; 13641ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 13651ec1e82fSSascha Hauer } 1366e6966433SHuang Shijie sdmac->direction = dmaengine_cfg->direction; 13677b350ab0SMaxime Ripard return sdma_config_channel(chan); 13681ec1e82fSSascha Hauer } 13691ec1e82fSSascha Hauer 13701ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 13711ec1e82fSSascha Hauer dma_cookie_t cookie, 13721ec1e82fSSascha Hauer struct dma_tx_state *txstate) 13731ec1e82fSSascha Hauer { 13741ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 1375d1a792f3SRussell King - ARM Linux u32 residue; 1376d1a792f3SRussell King - ARM Linux 1377d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 1378d1a792f3SRussell King - ARM Linux residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len; 1379d1a792f3SRussell King - ARM Linux else 1380d1a792f3SRussell King - ARM Linux residue = sdmac->chn_count - sdmac->chn_real_count; 13811ec1e82fSSascha Hauer 1382e8e3a790SAndy Shevchenko dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, 1383d1a792f3SRussell King - ARM Linux residue); 13841ec1e82fSSascha Hauer 13858a965911SShawn Guo return sdmac->status; 13861ec1e82fSSascha Hauer } 13871ec1e82fSSascha Hauer 13881ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 13891ec1e82fSSascha Hauer { 13902b4f130eSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13912b4f130eSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 13922b4f130eSSascha Hauer 13932b4f130eSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 13942b4f130eSSascha Hauer sdma_enable_channel(sdma, sdmac->channel); 13951ec1e82fSSascha Hauer } 13961ec1e82fSSascha Hauer 13975b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 1398cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38 1399a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41 1400*b7d2648aSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42 14015b28aa31SSascha Hauer 14025b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 14035b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 14045b28aa31SSascha Hauer { 14055b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 14065b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 14075b28aa31SSascha Hauer int i; 14085b28aa31SSascha Hauer 140970dabaedSNicolin Chen /* use the default firmware in ROM if missing external firmware */ 141070dabaedSNicolin Chen if (!sdma->script_number) 141170dabaedSNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 141270dabaedSNicolin Chen 1413cd72b846SNicolin Chen for (i = 0; i < sdma->script_number; i++) 14145b28aa31SSascha Hauer if (addr_arr[i] > 0) 14155b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 14165b28aa31SSascha Hauer } 14175b28aa31SSascha Hauer 14187b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context) 14195b28aa31SSascha Hauer { 14207b4b88e0SSascha Hauer struct sdma_engine *sdma = context; 14215b28aa31SSascha Hauer const struct sdma_firmware_header *header; 14225b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 14235b28aa31SSascha Hauer unsigned short *ram_code; 14245b28aa31SSascha Hauer 14257b4b88e0SSascha Hauer if (!fw) { 14260f927a11SSascha Hauer dev_info(sdma->dev, "external firmware not found, using ROM firmware\n"); 14270f927a11SSascha Hauer /* In this case we just use the ROM firmware. */ 14287b4b88e0SSascha Hauer return; 14297b4b88e0SSascha Hauer } 14305b28aa31SSascha Hauer 14315b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 14325b28aa31SSascha Hauer goto err_firmware; 14335b28aa31SSascha Hauer 14345b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 14355b28aa31SSascha Hauer 14365b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 14375b28aa31SSascha Hauer goto err_firmware; 14385b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 14395b28aa31SSascha Hauer goto err_firmware; 1440cd72b846SNicolin Chen switch (header->version_major) { 1441cd72b846SNicolin Chen case 1: 1442cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 1443cd72b846SNicolin Chen break; 1444cd72b846SNicolin Chen case 2: 1445cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2; 1446cd72b846SNicolin Chen break; 1447a572460bSFabio Estevam case 3: 1448a572460bSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3; 1449a572460bSFabio Estevam break; 1450*b7d2648aSFabio Estevam case 4: 1451*b7d2648aSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4; 1452*b7d2648aSFabio Estevam break; 1453cd72b846SNicolin Chen default: 1454cd72b846SNicolin Chen dev_err(sdma->dev, "unknown firmware version\n"); 1455cd72b846SNicolin Chen goto err_firmware; 1456cd72b846SNicolin Chen } 14575b28aa31SSascha Hauer 14585b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 14595b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 14605b28aa31SSascha Hauer 14617560e3f3SSascha Hauer clk_enable(sdma->clk_ipg); 14627560e3f3SSascha Hauer clk_enable(sdma->clk_ahb); 14635b28aa31SSascha Hauer /* download the RAM image for SDMA */ 14645b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 14655b28aa31SSascha Hauer header->ram_code_size, 14666866fd3bSSascha Hauer addr->ram_code_start_addr); 14677560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 14687560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 14695b28aa31SSascha Hauer 14705b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 14715b28aa31SSascha Hauer 14725b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 14735b28aa31SSascha Hauer header->version_major, 14745b28aa31SSascha Hauer header->version_minor); 14755b28aa31SSascha Hauer 14765b28aa31SSascha Hauer err_firmware: 14775b28aa31SSascha Hauer release_firmware(fw); 14787b4b88e0SSascha Hauer } 14797b4b88e0SSascha Hauer 1480d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3 1481d078cd1bSZidan Wang 148229f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma) 1483d078cd1bSZidan Wang { 1484d078cd1bSZidan Wang struct device_node *np = sdma->dev->of_node; 1485d078cd1bSZidan Wang struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0); 1486d078cd1bSZidan Wang struct property *event_remap; 1487d078cd1bSZidan Wang struct regmap *gpr; 1488d078cd1bSZidan Wang char propname[] = "fsl,sdma-event-remap"; 1489d078cd1bSZidan Wang u32 reg, val, shift, num_map, i; 1490d078cd1bSZidan Wang int ret = 0; 1491d078cd1bSZidan Wang 1492d078cd1bSZidan Wang if (IS_ERR(np) || IS_ERR(gpr_np)) 1493d078cd1bSZidan Wang goto out; 1494d078cd1bSZidan Wang 1495d078cd1bSZidan Wang event_remap = of_find_property(np, propname, NULL); 1496d078cd1bSZidan Wang num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0; 1497d078cd1bSZidan Wang if (!num_map) { 1498ce078af7SFabio Estevam dev_dbg(sdma->dev, "no event needs to be remapped\n"); 1499d078cd1bSZidan Wang goto out; 1500d078cd1bSZidan Wang } else if (num_map % EVENT_REMAP_CELLS) { 1501d078cd1bSZidan Wang dev_err(sdma->dev, "the property %s must modulo %d\n", 1502d078cd1bSZidan Wang propname, EVENT_REMAP_CELLS); 1503d078cd1bSZidan Wang ret = -EINVAL; 1504d078cd1bSZidan Wang goto out; 1505d078cd1bSZidan Wang } 1506d078cd1bSZidan Wang 1507d078cd1bSZidan Wang gpr = syscon_node_to_regmap(gpr_np); 1508d078cd1bSZidan Wang if (IS_ERR(gpr)) { 1509d078cd1bSZidan Wang dev_err(sdma->dev, "failed to get gpr regmap\n"); 1510d078cd1bSZidan Wang ret = PTR_ERR(gpr); 1511d078cd1bSZidan Wang goto out; 1512d078cd1bSZidan Wang } 1513d078cd1bSZidan Wang 1514d078cd1bSZidan Wang for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) { 1515d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i, ®); 1516d078cd1bSZidan Wang if (ret) { 1517d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1518d078cd1bSZidan Wang propname, i); 1519d078cd1bSZidan Wang goto out; 1520d078cd1bSZidan Wang } 1521d078cd1bSZidan Wang 1522d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 1, &shift); 1523d078cd1bSZidan Wang if (ret) { 1524d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1525d078cd1bSZidan Wang propname, i + 1); 1526d078cd1bSZidan Wang goto out; 1527d078cd1bSZidan Wang } 1528d078cd1bSZidan Wang 1529d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 2, &val); 1530d078cd1bSZidan Wang if (ret) { 1531d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1532d078cd1bSZidan Wang propname, i + 2); 1533d078cd1bSZidan Wang goto out; 1534d078cd1bSZidan Wang } 1535d078cd1bSZidan Wang 1536d078cd1bSZidan Wang regmap_update_bits(gpr, reg, BIT(shift), val << shift); 1537d078cd1bSZidan Wang } 1538d078cd1bSZidan Wang 1539d078cd1bSZidan Wang out: 1540d078cd1bSZidan Wang if (!IS_ERR(gpr_np)) 1541d078cd1bSZidan Wang of_node_put(gpr_np); 1542d078cd1bSZidan Wang 1543d078cd1bSZidan Wang return ret; 1544d078cd1bSZidan Wang } 1545d078cd1bSZidan Wang 1546fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma, 15477b4b88e0SSascha Hauer const char *fw_name) 15487b4b88e0SSascha Hauer { 15497b4b88e0SSascha Hauer int ret; 15507b4b88e0SSascha Hauer 15517b4b88e0SSascha Hauer ret = request_firmware_nowait(THIS_MODULE, 15527b4b88e0SSascha Hauer FW_ACTION_HOTPLUG, fw_name, sdma->dev, 15537b4b88e0SSascha Hauer GFP_KERNEL, sdma, sdma_load_firmware); 15545b28aa31SSascha Hauer 15555b28aa31SSascha Hauer return ret; 15565b28aa31SSascha Hauer } 15575b28aa31SSascha Hauer 155819bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma) 15591ec1e82fSSascha Hauer { 15601ec1e82fSSascha Hauer int i, ret; 15611ec1e82fSSascha Hauer dma_addr_t ccb_phys; 15621ec1e82fSSascha Hauer 1563b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ipg); 1564b93edcddSFabio Estevam if (ret) 1565b93edcddSFabio Estevam return ret; 1566b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ahb); 1567b93edcddSFabio Estevam if (ret) 1568b93edcddSFabio Estevam goto disable_clk_ipg; 15691ec1e82fSSascha Hauer 15701ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 1571c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); 15721ec1e82fSSascha Hauer 15731ec1e82fSSascha Hauer sdma->channel_control = dma_alloc_coherent(NULL, 15741ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + 15751ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 15761ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 15771ec1e82fSSascha Hauer 15781ec1e82fSSascha Hauer if (!sdma->channel_control) { 15791ec1e82fSSascha Hauer ret = -ENOMEM; 15801ec1e82fSSascha Hauer goto err_dma_alloc; 15811ec1e82fSSascha Hauer } 15821ec1e82fSSascha Hauer 15831ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 15841ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 15851ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 15861ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 15871ec1e82fSSascha Hauer 15881ec1e82fSSascha Hauer /* Zero-out the CCB structures array just allocated */ 15891ec1e82fSSascha Hauer memset(sdma->channel_control, 0, 15901ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control)); 15911ec1e82fSSascha Hauer 15921ec1e82fSSascha Hauer /* disable all channels */ 159317bba72fSSascha Hauer for (i = 0; i < sdma->drvdata->num_events; i++) 1594c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); 15951ec1e82fSSascha Hauer 15961ec1e82fSSascha Hauer /* All channels have priority 0 */ 15971ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 1598c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 15991ec1e82fSSascha Hauer 16001ec1e82fSSascha Hauer ret = sdma_request_channel(&sdma->channel[0]); 16011ec1e82fSSascha Hauer if (ret) 16021ec1e82fSSascha Hauer goto err_dma_alloc; 16031ec1e82fSSascha Hauer 16041ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 16051ec1e82fSSascha Hauer 16061ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 1607c4b56857SRichard Zhao writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); 16081ec1e82fSSascha Hauer 16091ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 16101ec1e82fSSascha Hauer /* FIXME: Check whether to set ACR bit depending on clock ratios */ 1611c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); 16121ec1e82fSSascha Hauer 1613c4b56857SRichard Zhao writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); 16141ec1e82fSSascha Hauer 16151ec1e82fSSascha Hauer /* Initializes channel's priorities */ 16161ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 16171ec1e82fSSascha Hauer 16187560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 16197560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 16201ec1e82fSSascha Hauer 16211ec1e82fSSascha Hauer return 0; 16221ec1e82fSSascha Hauer 16231ec1e82fSSascha Hauer err_dma_alloc: 16247560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 1625b93edcddSFabio Estevam disable_clk_ipg: 1626b93edcddSFabio Estevam clk_disable(sdma->clk_ipg); 16271ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 16281ec1e82fSSascha Hauer return ret; 16291ec1e82fSSascha Hauer } 16301ec1e82fSSascha Hauer 16319479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param) 16329479e17cSShawn Guo { 16330b351865SNicolin Chen struct sdma_channel *sdmac = to_sdma_chan(chan); 16349479e17cSShawn Guo struct imx_dma_data *data = fn_param; 16359479e17cSShawn Guo 16369479e17cSShawn Guo if (!imx_dma_is_general_purpose(chan)) 16379479e17cSShawn Guo return false; 16389479e17cSShawn Guo 16390b351865SNicolin Chen sdmac->data = *data; 16400b351865SNicolin Chen chan->private = &sdmac->data; 16419479e17cSShawn Guo 16429479e17cSShawn Guo return true; 16439479e17cSShawn Guo } 16449479e17cSShawn Guo 16459479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec, 16469479e17cSShawn Guo struct of_dma *ofdma) 16479479e17cSShawn Guo { 16489479e17cSShawn Guo struct sdma_engine *sdma = ofdma->of_dma_data; 16499479e17cSShawn Guo dma_cap_mask_t mask = sdma->dma_device.cap_mask; 16509479e17cSShawn Guo struct imx_dma_data data; 16519479e17cSShawn Guo 16529479e17cSShawn Guo if (dma_spec->args_count != 3) 16539479e17cSShawn Guo return NULL; 16549479e17cSShawn Guo 16559479e17cSShawn Guo data.dma_request = dma_spec->args[0]; 16569479e17cSShawn Guo data.peripheral_type = dma_spec->args[1]; 16579479e17cSShawn Guo data.priority = dma_spec->args[2]; 16588391ecf4SShengjiu Wang /* 16598391ecf4SShengjiu Wang * init dma_request2 to zero, which is not used by the dts. 16608391ecf4SShengjiu Wang * For P2P, dma_request2 is init from dma_request_channel(), 16618391ecf4SShengjiu Wang * chan->private will point to the imx_dma_data, and in 16628391ecf4SShengjiu Wang * device_alloc_chan_resources(), imx_dma_data.dma_request2 will 16638391ecf4SShengjiu Wang * be set to sdmac->event_id1. 16648391ecf4SShengjiu Wang */ 16658391ecf4SShengjiu Wang data.dma_request2 = 0; 16669479e17cSShawn Guo 16679479e17cSShawn Guo return dma_request_channel(mask, sdma_filter_fn, &data); 16689479e17cSShawn Guo } 16699479e17cSShawn Guo 1670e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev) 16711ec1e82fSSascha Hauer { 1672580975d7SShawn Guo const struct of_device_id *of_id = 1673580975d7SShawn Guo of_match_device(sdma_dt_ids, &pdev->dev); 1674580975d7SShawn Guo struct device_node *np = pdev->dev.of_node; 16758391ecf4SShengjiu Wang struct device_node *spba_bus; 1676580975d7SShawn Guo const char *fw_name; 16771ec1e82fSSascha Hauer int ret; 16781ec1e82fSSascha Hauer int irq; 16791ec1e82fSSascha Hauer struct resource *iores; 16808391ecf4SShengjiu Wang struct resource spba_res; 1681d4adcc01SJingoo Han struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev); 16821ec1e82fSSascha Hauer int i; 16831ec1e82fSSascha Hauer struct sdma_engine *sdma; 168436e2f21aSSascha Hauer s32 *saddr_arr; 168517bba72fSSascha Hauer const struct sdma_driver_data *drvdata = NULL; 168617bba72fSSascha Hauer 168717bba72fSSascha Hauer if (of_id) 168817bba72fSSascha Hauer drvdata = of_id->data; 168917bba72fSSascha Hauer else if (pdev->id_entry) 169017bba72fSSascha Hauer drvdata = (void *)pdev->id_entry->driver_data; 169117bba72fSSascha Hauer 169217bba72fSSascha Hauer if (!drvdata) { 169317bba72fSSascha Hauer dev_err(&pdev->dev, "unable to find driver data\n"); 169417bba72fSSascha Hauer return -EINVAL; 169517bba72fSSascha Hauer } 16961ec1e82fSSascha Hauer 169742536b9fSPhilippe Retornaz ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 169842536b9fSPhilippe Retornaz if (ret) 169942536b9fSPhilippe Retornaz return ret; 170042536b9fSPhilippe Retornaz 17017f24e0eeSFabio Estevam sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL); 17021ec1e82fSSascha Hauer if (!sdma) 17031ec1e82fSSascha Hauer return -ENOMEM; 17041ec1e82fSSascha Hauer 17052ccaef05SRichard Zhao spin_lock_init(&sdma->channel_0_lock); 170673eab978SSascha Hauer 17071ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 170817bba72fSSascha Hauer sdma->drvdata = drvdata; 17091ec1e82fSSascha Hauer 17101ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 17117f24e0eeSFabio Estevam if (irq < 0) 171263c72e02SFabio Estevam return irq; 17131ec1e82fSSascha Hauer 17147f24e0eeSFabio Estevam iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 17157f24e0eeSFabio Estevam sdma->regs = devm_ioremap_resource(&pdev->dev, iores); 17167f24e0eeSFabio Estevam if (IS_ERR(sdma->regs)) 17177f24e0eeSFabio Estevam return PTR_ERR(sdma->regs); 17181ec1e82fSSascha Hauer 17197560e3f3SSascha Hauer sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 17207f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ipg)) 17217f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ipg); 17221ec1e82fSSascha Hauer 17237560e3f3SSascha Hauer sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 17247f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ahb)) 17257f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ahb); 17267560e3f3SSascha Hauer 17277560e3f3SSascha Hauer clk_prepare(sdma->clk_ipg); 17287560e3f3SSascha Hauer clk_prepare(sdma->clk_ahb); 17297560e3f3SSascha Hauer 17307f24e0eeSFabio Estevam ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma", 17317f24e0eeSFabio Estevam sdma); 17321ec1e82fSSascha Hauer if (ret) 17337f24e0eeSFabio Estevam return ret; 17341ec1e82fSSascha Hauer 17355bb9dbb5SVinod Koul sdma->irq = irq; 17365bb9dbb5SVinod Koul 17375b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 17387f24e0eeSFabio Estevam if (!sdma->script_addrs) 17397f24e0eeSFabio Estevam return -ENOMEM; 17401ec1e82fSSascha Hauer 174136e2f21aSSascha Hauer /* initially no scripts available */ 174236e2f21aSSascha Hauer saddr_arr = (s32 *)sdma->script_addrs; 174336e2f21aSSascha Hauer for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) 174436e2f21aSSascha Hauer saddr_arr[i] = -EINVAL; 174536e2f21aSSascha Hauer 17467214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 17477214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 17487214a8b1SSascha Hauer 17491ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 17501ec1e82fSSascha Hauer /* Initialize channel parameters */ 17511ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 17521ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 17531ec1e82fSSascha Hauer 17541ec1e82fSSascha Hauer sdmac->sdma = sdma; 17551ec1e82fSSascha Hauer spin_lock_init(&sdmac->lock); 17561ec1e82fSSascha Hauer 17571ec1e82fSSascha Hauer sdmac->chan.device = &sdma->dma_device; 17588ac69546SRussell King - ARM Linux dma_cookie_init(&sdmac->chan); 17591ec1e82fSSascha Hauer sdmac->channel = i; 17601ec1e82fSSascha Hauer 1761abd9ccc8SHuang Shijie tasklet_init(&sdmac->tasklet, sdma_tasklet, 1762abd9ccc8SHuang Shijie (unsigned long) sdmac); 176323889c63SSascha Hauer /* 176423889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 176523889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 176623889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 176723889c63SSascha Hauer */ 176823889c63SSascha Hauer if (i) 176923889c63SSascha Hauer list_add_tail(&sdmac->chan.device_node, 177023889c63SSascha Hauer &sdma->dma_device.channels); 17711ec1e82fSSascha Hauer } 17721ec1e82fSSascha Hauer 17735b28aa31SSascha Hauer ret = sdma_init(sdma); 17741ec1e82fSSascha Hauer if (ret) 17751ec1e82fSSascha Hauer goto err_init; 17761ec1e82fSSascha Hauer 1777d078cd1bSZidan Wang ret = sdma_event_remap(sdma); 1778d078cd1bSZidan Wang if (ret) 1779d078cd1bSZidan Wang goto err_init; 1780d078cd1bSZidan Wang 1781dcfec3c0SSascha Hauer if (sdma->drvdata->script_addrs) 1782dcfec3c0SSascha Hauer sdma_add_scripts(sdma, sdma->drvdata->script_addrs); 1783580975d7SShawn Guo if (pdata && pdata->script_addrs) 17845b28aa31SSascha Hauer sdma_add_scripts(sdma, pdata->script_addrs); 17855b28aa31SSascha Hauer 1786580975d7SShawn Guo if (pdata) { 17876d0d7e2dSFabio Estevam ret = sdma_get_firmware(sdma, pdata->fw_name); 17886d0d7e2dSFabio Estevam if (ret) 1789ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); 1790580975d7SShawn Guo } else { 1791580975d7SShawn Guo /* 1792580975d7SShawn Guo * Because that device tree does not encode ROM script address, 1793580975d7SShawn Guo * the RAM script in firmware is mandatory for device tree 1794580975d7SShawn Guo * probe, otherwise it fails. 1795580975d7SShawn Guo */ 1796580975d7SShawn Guo ret = of_property_read_string(np, "fsl,sdma-ram-script-name", 1797580975d7SShawn Guo &fw_name); 17986602b0ddSFabio Estevam if (ret) 1799ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware name\n"); 18006602b0ddSFabio Estevam else { 1801580975d7SShawn Guo ret = sdma_get_firmware(sdma, fw_name); 18026602b0ddSFabio Estevam if (ret) 1803ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); 1804580975d7SShawn Guo } 1805580975d7SShawn Guo } 18065b28aa31SSascha Hauer 18071ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 18081ec1e82fSSascha Hauer 18091ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 18101ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 18111ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 18121ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 18131ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 18147b350ab0SMaxime Ripard sdma->dma_device.device_config = sdma_config; 18157b350ab0SMaxime Ripard sdma->dma_device.device_terminate_all = sdma_disable_channel; 18161e4a4f50SFabio Estevam sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 18171e4a4f50SFabio Estevam sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 18181e4a4f50SFabio Estevam sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); 18191e4a4f50SFabio Estevam sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 18201ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 1821b9b3f82fSSascha Hauer sdma->dma_device.dev->dma_parms = &sdma->dma_parms; 1822b9b3f82fSSascha Hauer dma_set_max_seg_size(sdma->dma_device.dev, 65535); 18231ec1e82fSSascha Hauer 182423e11811SVignesh Raman platform_set_drvdata(pdev, sdma); 182523e11811SVignesh Raman 18261ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 18271ec1e82fSSascha Hauer if (ret) { 18281ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 18291ec1e82fSSascha Hauer goto err_init; 18301ec1e82fSSascha Hauer } 18311ec1e82fSSascha Hauer 18329479e17cSShawn Guo if (np) { 18339479e17cSShawn Guo ret = of_dma_controller_register(np, sdma_xlate, sdma); 18349479e17cSShawn Guo if (ret) { 18359479e17cSShawn Guo dev_err(&pdev->dev, "failed to register controller\n"); 18369479e17cSShawn Guo goto err_register; 18379479e17cSShawn Guo } 18388391ecf4SShengjiu Wang 18398391ecf4SShengjiu Wang spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus"); 18408391ecf4SShengjiu Wang ret = of_address_to_resource(spba_bus, 0, &spba_res); 18418391ecf4SShengjiu Wang if (!ret) { 18428391ecf4SShengjiu Wang sdma->spba_start_addr = spba_res.start; 18438391ecf4SShengjiu Wang sdma->spba_end_addr = spba_res.end; 18448391ecf4SShengjiu Wang } 18458391ecf4SShengjiu Wang of_node_put(spba_bus); 18469479e17cSShawn Guo } 18479479e17cSShawn Guo 18481ec1e82fSSascha Hauer return 0; 18491ec1e82fSSascha Hauer 18509479e17cSShawn Guo err_register: 18519479e17cSShawn Guo dma_async_device_unregister(&sdma->dma_device); 18521ec1e82fSSascha Hauer err_init: 18531ec1e82fSSascha Hauer kfree(sdma->script_addrs); 1854939fd4f0SShawn Guo return ret; 18551ec1e82fSSascha Hauer } 18561ec1e82fSSascha Hauer 18571d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev) 18581ec1e82fSSascha Hauer { 185923e11811SVignesh Raman struct sdma_engine *sdma = platform_get_drvdata(pdev); 1860c12fe497SVignesh Raman int i; 186123e11811SVignesh Raman 18625bb9dbb5SVinod Koul devm_free_irq(&pdev->dev, sdma->irq, sdma); 186323e11811SVignesh Raman dma_async_device_unregister(&sdma->dma_device); 186423e11811SVignesh Raman kfree(sdma->script_addrs); 1865c12fe497SVignesh Raman /* Kill the tasklet */ 1866c12fe497SVignesh Raman for (i = 0; i < MAX_DMA_CHANNELS; i++) { 1867c12fe497SVignesh Raman struct sdma_channel *sdmac = &sdma->channel[i]; 1868c12fe497SVignesh Raman 1869c12fe497SVignesh Raman tasklet_kill(&sdmac->tasklet); 1870c12fe497SVignesh Raman } 187123e11811SVignesh Raman 187223e11811SVignesh Raman platform_set_drvdata(pdev, NULL); 187323e11811SVignesh Raman return 0; 18741ec1e82fSSascha Hauer } 18751ec1e82fSSascha Hauer 18761ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 18771ec1e82fSSascha Hauer .driver = { 18781ec1e82fSSascha Hauer .name = "imx-sdma", 1879580975d7SShawn Guo .of_match_table = sdma_dt_ids, 18801ec1e82fSSascha Hauer }, 188162550cd7SShawn Guo .id_table = sdma_devtypes, 18821d1bbd30SMaxin B. John .remove = sdma_remove, 188323e11811SVignesh Raman .probe = sdma_probe, 18841ec1e82fSSascha Hauer }; 18851ec1e82fSSascha Hauer 188623e11811SVignesh Raman module_platform_driver(sdma_driver); 18871ec1e82fSSascha Hauer 18881ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 18891ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 18901ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 1891