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 */ 1871ec1e82fSSascha Hauer u32 command : 8; /* command mostlky 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 482afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = { 48362550cd7SShawn Guo { 484dcfec3c0SSascha Hauer .name = "imx25-sdma", 485dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx25, 486dcfec3c0SSascha Hauer }, { 48762550cd7SShawn Guo .name = "imx31-sdma", 48817bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx31, 48962550cd7SShawn Guo }, { 49062550cd7SShawn Guo .name = "imx35-sdma", 49117bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx35, 49262550cd7SShawn Guo }, { 493dcfec3c0SSascha Hauer .name = "imx51-sdma", 494dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx51, 495dcfec3c0SSascha Hauer }, { 496dcfec3c0SSascha Hauer .name = "imx53-sdma", 497dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx53, 498dcfec3c0SSascha Hauer }, { 499dcfec3c0SSascha Hauer .name = "imx6q-sdma", 500dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx6q, 501dcfec3c0SSascha Hauer }, { 50262550cd7SShawn Guo /* sentinel */ 50362550cd7SShawn Guo } 50462550cd7SShawn Guo }; 50562550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes); 50662550cd7SShawn Guo 507580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = { 508dcfec3c0SSascha Hauer { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, }, 509dcfec3c0SSascha Hauer { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, }, 510dcfec3c0SSascha Hauer { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, }, 51117bba72fSSascha Hauer { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, }, 512dcfec3c0SSascha Hauer { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, }, 51363edea16SMarkus Pargmann { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, }, 514580975d7SShawn Guo { /* sentinel */ } 515580975d7SShawn Guo }; 516580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids); 517580975d7SShawn Guo 5180bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */ 5190bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */ 5200bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */ 5211ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 5221ec1e82fSSascha Hauer 5231ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 5241ec1e82fSSascha Hauer { 52517bba72fSSascha Hauer u32 chnenbl0 = sdma->drvdata->chnenbl0; 5261ec1e82fSSascha Hauer return chnenbl0 + event * 4; 5271ec1e82fSSascha Hauer } 5281ec1e82fSSascha Hauer 5291ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 5301ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 5311ec1e82fSSascha Hauer { 5321ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 5331ec1e82fSSascha Hauer int channel = sdmac->channel; 5340bbc1413SRichard Zhao unsigned long evt, mcu, dsp; 5351ec1e82fSSascha Hauer 5361ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 5371ec1e82fSSascha Hauer return -EINVAL; 5381ec1e82fSSascha Hauer 539c4b56857SRichard Zhao evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); 540c4b56857SRichard Zhao mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); 541c4b56857SRichard Zhao dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); 5421ec1e82fSSascha Hauer 5431ec1e82fSSascha Hauer if (dsp_override) 5440bbc1413SRichard Zhao __clear_bit(channel, &dsp); 5451ec1e82fSSascha Hauer else 5460bbc1413SRichard Zhao __set_bit(channel, &dsp); 5471ec1e82fSSascha Hauer 5481ec1e82fSSascha Hauer if (event_override) 5490bbc1413SRichard Zhao __clear_bit(channel, &evt); 5501ec1e82fSSascha Hauer else 5510bbc1413SRichard Zhao __set_bit(channel, &evt); 5521ec1e82fSSascha Hauer 5531ec1e82fSSascha Hauer if (mcu_override) 5540bbc1413SRichard Zhao __clear_bit(channel, &mcu); 5551ec1e82fSSascha Hauer else 5560bbc1413SRichard Zhao __set_bit(channel, &mcu); 5571ec1e82fSSascha Hauer 558c4b56857SRichard Zhao writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); 559c4b56857SRichard Zhao writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); 560c4b56857SRichard Zhao writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); 5611ec1e82fSSascha Hauer 5621ec1e82fSSascha Hauer return 0; 5631ec1e82fSSascha Hauer } 5641ec1e82fSSascha Hauer 565b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 566b9a59166SRichard Zhao { 5670bbc1413SRichard Zhao writel(BIT(channel), sdma->regs + SDMA_H_START); 568b9a59166SRichard Zhao } 569b9a59166SRichard Zhao 5701ec1e82fSSascha Hauer /* 5712ccaef05SRichard Zhao * sdma_run_channel0 - run a channel and wait till it's done 5721ec1e82fSSascha Hauer */ 5732ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma) 5741ec1e82fSSascha Hauer { 5751ec1e82fSSascha Hauer int ret; 5761d069bfaSMichael Olbrich u32 reg; 5771ec1e82fSSascha Hauer 5782ccaef05SRichard Zhao sdma_enable_channel(sdma, 0); 5791ec1e82fSSascha Hauer 5801d069bfaSMichael Olbrich ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP, 5811d069bfaSMichael Olbrich reg, !(reg & 1), 1, 500); 5821d069bfaSMichael Olbrich if (ret) 5832ccaef05SRichard Zhao dev_err(sdma->dev, "Timeout waiting for CH0 ready\n"); 5841ec1e82fSSascha Hauer 585855832e4SRobin Gong /* Set bits of CONFIG register with dynamic context switching */ 586855832e4SRobin Gong if (readl(sdma->regs + SDMA_H_CONFIG) == 0) 587855832e4SRobin Gong writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG); 588855832e4SRobin Gong 5891d069bfaSMichael Olbrich return ret; 5901ec1e82fSSascha Hauer } 5911ec1e82fSSascha Hauer 5921ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 5931ec1e82fSSascha Hauer u32 address) 5941ec1e82fSSascha Hauer { 5951ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 5961ec1e82fSSascha Hauer void *buf_virt; 5971ec1e82fSSascha Hauer dma_addr_t buf_phys; 5981ec1e82fSSascha Hauer int ret; 5992ccaef05SRichard Zhao unsigned long flags; 60073eab978SSascha Hauer 6011ec1e82fSSascha Hauer buf_virt = dma_alloc_coherent(NULL, 6021ec1e82fSSascha Hauer size, 6031ec1e82fSSascha Hauer &buf_phys, GFP_KERNEL); 60473eab978SSascha Hauer if (!buf_virt) { 6052ccaef05SRichard Zhao return -ENOMEM; 60673eab978SSascha Hauer } 6071ec1e82fSSascha Hauer 6082ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 6092ccaef05SRichard Zhao 6101ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 6111ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 6121ec1e82fSSascha Hauer bd0->mode.count = size / 2; 6131ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 6141ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 6151ec1e82fSSascha Hauer 6161ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 6171ec1e82fSSascha Hauer 6182ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 6192ccaef05SRichard Zhao 6202ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 6211ec1e82fSSascha Hauer 6221ec1e82fSSascha Hauer dma_free_coherent(NULL, size, buf_virt, buf_phys); 6231ec1e82fSSascha Hauer 6241ec1e82fSSascha Hauer return ret; 6251ec1e82fSSascha Hauer } 6261ec1e82fSSascha Hauer 6271ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 6281ec1e82fSSascha Hauer { 6291ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6301ec1e82fSSascha Hauer int channel = sdmac->channel; 6310bbc1413SRichard Zhao unsigned long val; 6321ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 6331ec1e82fSSascha Hauer 634c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 6350bbc1413SRichard Zhao __set_bit(channel, &val); 636c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 6371ec1e82fSSascha Hauer } 6381ec1e82fSSascha Hauer 6391ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 6401ec1e82fSSascha Hauer { 6411ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6421ec1e82fSSascha Hauer int channel = sdmac->channel; 6431ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 6440bbc1413SRichard Zhao unsigned long val; 6451ec1e82fSSascha Hauer 646c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 6470bbc1413SRichard Zhao __clear_bit(channel, &val); 648c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 6491ec1e82fSSascha Hauer } 6501ec1e82fSSascha Hauer 651d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac) 652d1a792f3SRussell King - ARM Linux { 6531ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 654*5881826dSNandor Han int error = 0; 655*5881826dSNandor Han enum dma_status old_status = sdmac->status; 6561ec1e82fSSascha Hauer 6571ec1e82fSSascha Hauer /* 6581ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 6591ec1e82fSSascha Hauer * call callback function. 6601ec1e82fSSascha Hauer */ 6611ec1e82fSSascha Hauer while (1) { 6621ec1e82fSSascha Hauer bd = &sdmac->bd[sdmac->buf_tail]; 6631ec1e82fSSascha Hauer 6641ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 6651ec1e82fSSascha Hauer break; 6661ec1e82fSSascha Hauer 667*5881826dSNandor Han if (bd->mode.status & BD_RROR) { 668*5881826dSNandor Han bd->mode.status &= ~BD_RROR; 6691ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 670*5881826dSNandor Han error = -EIO; 671*5881826dSNandor Han } 6721ec1e82fSSascha Hauer 673*5881826dSNandor Han /* 674*5881826dSNandor Han * We use bd->mode.count to calculate the residue, since contains 675*5881826dSNandor Han * the number of bytes present in the current buffer descriptor. 676*5881826dSNandor Han */ 677*5881826dSNandor Han 678*5881826dSNandor Han sdmac->chn_real_count = bd->mode.count; 6791ec1e82fSSascha Hauer bd->mode.status |= BD_DONE; 680*5881826dSNandor Han bd->mode.count = sdmac->period_len; 68115f30f51SNandor Han 68215f30f51SNandor Han /* 68315f30f51SNandor Han * The callback is called from the interrupt context in order 68415f30f51SNandor Han * to reduce latency and to avoid the risk of altering the 68515f30f51SNandor Han * SDMA transaction status by the time the client tasklet is 68615f30f51SNandor Han * executed. 68715f30f51SNandor Han */ 68815f30f51SNandor Han 68915f30f51SNandor Han if (sdmac->desc.callback) 69015f30f51SNandor Han sdmac->desc.callback(sdmac->desc.callback_param); 69115f30f51SNandor Han 6921ec1e82fSSascha Hauer sdmac->buf_tail++; 6931ec1e82fSSascha Hauer sdmac->buf_tail %= sdmac->num_bd; 694*5881826dSNandor Han 695*5881826dSNandor Han if (error) 696*5881826dSNandor Han sdmac->status = old_status; 6971ec1e82fSSascha Hauer } 6981ec1e82fSSascha Hauer } 6991ec1e82fSSascha Hauer 70015f30f51SNandor Han static void mxc_sdma_handle_channel_normal(unsigned long data) 7011ec1e82fSSascha Hauer { 70215f30f51SNandor Han struct sdma_channel *sdmac = (struct sdma_channel *) data; 7031ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 7041ec1e82fSSascha Hauer int i, error = 0; 7051ec1e82fSSascha Hauer 706ab59a510SHuang Shijie sdmac->chn_real_count = 0; 7071ec1e82fSSascha Hauer /* 7081ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 7091ec1e82fSSascha Hauer * errors and call callback function 7101ec1e82fSSascha Hauer */ 7111ec1e82fSSascha Hauer for (i = 0; i < sdmac->num_bd; i++) { 7121ec1e82fSSascha Hauer bd = &sdmac->bd[i]; 7131ec1e82fSSascha Hauer 7141ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 7151ec1e82fSSascha Hauer error = -EIO; 716ab59a510SHuang Shijie sdmac->chn_real_count += bd->mode.count; 7171ec1e82fSSascha Hauer } 7181ec1e82fSSascha Hauer 7191ec1e82fSSascha Hauer if (error) 7201ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 7211ec1e82fSSascha Hauer else 722409bff6aSVinod Koul sdmac->status = DMA_COMPLETE; 7231ec1e82fSSascha Hauer 724f7fbce07SRussell King - ARM Linux dma_cookie_complete(&sdmac->desc); 7251ec1e82fSSascha Hauer if (sdmac->desc.callback) 7261ec1e82fSSascha Hauer sdmac->desc.callback(sdmac->desc.callback_param); 7271ec1e82fSSascha Hauer } 7281ec1e82fSSascha Hauer 7291ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 7301ec1e82fSSascha Hauer { 7311ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 7320bbc1413SRichard Zhao unsigned long stat; 7331ec1e82fSSascha Hauer 734c4b56857SRichard Zhao stat = readl_relaxed(sdma->regs + SDMA_H_INTR); 735c4b56857SRichard Zhao writel_relaxed(stat, sdma->regs + SDMA_H_INTR); 7361d069bfaSMichael Olbrich /* channel 0 is special and not handled here, see run_channel0() */ 7371d069bfaSMichael Olbrich stat &= ~1; 7381ec1e82fSSascha Hauer 7391ec1e82fSSascha Hauer while (stat) { 7401ec1e82fSSascha Hauer int channel = fls(stat) - 1; 7411ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 7421ec1e82fSSascha Hauer 743d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 744d1a792f3SRussell King - ARM Linux sdma_update_channel_loop(sdmac); 74515f30f51SNandor Han else 746abd9ccc8SHuang Shijie tasklet_schedule(&sdmac->tasklet); 7471ec1e82fSSascha Hauer 7480bbc1413SRichard Zhao __clear_bit(channel, &stat); 7491ec1e82fSSascha Hauer } 7501ec1e82fSSascha Hauer 7511ec1e82fSSascha Hauer return IRQ_HANDLED; 7521ec1e82fSSascha Hauer } 7531ec1e82fSSascha Hauer 7541ec1e82fSSascha Hauer /* 7551ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 7561ec1e82fSSascha Hauer */ 7571ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac, 7581ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 7591ec1e82fSSascha Hauer { 7601ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7611ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 7621ec1e82fSSascha Hauer /* 7631ec1e82fSSascha Hauer * These are needed once we start to support transfers between 7641ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 7651ec1e82fSSascha Hauer */ 7660d605ba0SVinod Koul int per_2_per = 0; 7671ec1e82fSSascha Hauer 7681ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 7691ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 7708391ecf4SShengjiu Wang sdmac->device_to_device = 0; 7711ec1e82fSSascha Hauer 7721ec1e82fSSascha Hauer switch (peripheral_type) { 7731ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 7741ec1e82fSSascha Hauer break; 7751ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 7761ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 7771ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 7781ec1e82fSSascha Hauer break; 7791ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 7801ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 7811ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 7821ec1e82fSSascha Hauer break; 7831ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 7841ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 7851ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 7861ec1e82fSSascha Hauer break; 7871ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 7881ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 7891ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 7901ec1e82fSSascha Hauer break; 7911ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 7921ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 7931ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 7941ec1e82fSSascha Hauer break; 7951ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 7961ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 7971ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 79829aebfdeSNicolin Chen case IMX_DMATYPE_SAI: 7991ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 8001ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 8011ec1e82fSSascha Hauer break; 8021a895578SNicolin Chen case IMX_DMATYPE_SSI_DUAL: 8031a895578SNicolin Chen per_2_emi = sdma->script_addrs->ssish_2_mcu_addr; 8041a895578SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_ssish_addr; 8051a895578SNicolin Chen break; 8061ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 8071ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 8081ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 8091ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 8101ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 8111ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 8121ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 8131ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 8141ec1e82fSSascha Hauer break; 8151ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 8161ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 8171ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 8181ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 8191ec1e82fSSascha Hauer break; 820f892afb0SNicolin Chen case IMX_DMATYPE_ASRC_SP: 821f892afb0SNicolin Chen per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 822f892afb0SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 823f892afb0SNicolin Chen per_2_per = sdma->script_addrs->per_2_per_addr; 824f892afb0SNicolin Chen break; 8251ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 8261ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 8271ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 8281ec1e82fSSascha Hauer break; 8291ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 8301ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 8311ec1e82fSSascha Hauer break; 8321ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 8331ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 8341ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 8351ec1e82fSSascha Hauer break; 8361ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 8371ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 8381ec1e82fSSascha Hauer break; 8391ec1e82fSSascha Hauer default: 8401ec1e82fSSascha Hauer break; 8411ec1e82fSSascha Hauer } 8421ec1e82fSSascha Hauer 8431ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 8441ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 8458391ecf4SShengjiu Wang sdmac->device_to_device = per_2_per; 8461ec1e82fSSascha Hauer } 8471ec1e82fSSascha Hauer 8481ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 8491ec1e82fSSascha Hauer { 8501ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8511ec1e82fSSascha Hauer int channel = sdmac->channel; 8521ec1e82fSSascha Hauer int load_address; 8531ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 8541ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 8551ec1e82fSSascha Hauer int ret; 8562ccaef05SRichard Zhao unsigned long flags; 8571ec1e82fSSascha Hauer 8588391ecf4SShengjiu Wang if (sdmac->direction == DMA_DEV_TO_MEM) 8591ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 8608391ecf4SShengjiu Wang else if (sdmac->direction == DMA_DEV_TO_DEV) 8618391ecf4SShengjiu Wang load_address = sdmac->device_to_device; 8628391ecf4SShengjiu Wang else 8631ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 8641ec1e82fSSascha Hauer 8651ec1e82fSSascha Hauer if (load_address < 0) 8661ec1e82fSSascha Hauer return load_address; 8671ec1e82fSSascha Hauer 8681ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 8690bbc1413SRichard Zhao dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level); 8701ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 8711ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 8720bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]); 8730bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]); 8741ec1e82fSSascha Hauer 8752ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 87673eab978SSascha Hauer 8771ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 8781ec1e82fSSascha Hauer context->channel_state.pc = load_address; 8791ec1e82fSSascha Hauer 8801ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 8811ec1e82fSSascha Hauer * and watermark level 8821ec1e82fSSascha Hauer */ 8830bbc1413SRichard Zhao context->gReg[0] = sdmac->event_mask[1]; 8840bbc1413SRichard Zhao context->gReg[1] = sdmac->event_mask[0]; 8851ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 8861ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 8871ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 8881ec1e82fSSascha Hauer 8891ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 8901ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 8911ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 8921ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 8931ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 8942ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 8951ec1e82fSSascha Hauer 8962ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 89773eab978SSascha Hauer 8981ec1e82fSSascha Hauer return ret; 8991ec1e82fSSascha Hauer } 9001ec1e82fSSascha Hauer 9017b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 9021ec1e82fSSascha Hauer { 9037b350ab0SMaxime Ripard return container_of(chan, struct sdma_channel, chan); 9047b350ab0SMaxime Ripard } 9057b350ab0SMaxime Ripard 9067b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan) 9077b350ab0SMaxime Ripard { 9087b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 9091ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9101ec1e82fSSascha Hauer int channel = sdmac->channel; 9111ec1e82fSSascha Hauer 9120bbc1413SRichard Zhao writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP); 9131ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 9147b350ab0SMaxime Ripard 9157b350ab0SMaxime Ripard return 0; 9161ec1e82fSSascha Hauer } 9171ec1e82fSSascha Hauer 9188391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac) 9198391ecf4SShengjiu Wang { 9208391ecf4SShengjiu Wang struct sdma_engine *sdma = sdmac->sdma; 9218391ecf4SShengjiu Wang 9228391ecf4SShengjiu Wang int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML; 9238391ecf4SShengjiu Wang int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16; 9248391ecf4SShengjiu Wang 9258391ecf4SShengjiu Wang set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]); 9268391ecf4SShengjiu Wang set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]); 9278391ecf4SShengjiu Wang 9288391ecf4SShengjiu Wang if (sdmac->event_id0 > 31) 9298391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE; 9308391ecf4SShengjiu Wang 9318391ecf4SShengjiu Wang if (sdmac->event_id1 > 31) 9328391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE; 9338391ecf4SShengjiu Wang 9348391ecf4SShengjiu Wang /* 9358391ecf4SShengjiu Wang * If LWML(src_maxburst) > HWML(dst_maxburst), we need 9368391ecf4SShengjiu Wang * swap LWML and HWML of INFO(A.3.2.5.1), also need swap 9378391ecf4SShengjiu Wang * r0(event_mask[1]) and r1(event_mask[0]). 9388391ecf4SShengjiu Wang */ 9398391ecf4SShengjiu Wang if (lwml > hwml) { 9408391ecf4SShengjiu Wang sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML | 9418391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML); 9428391ecf4SShengjiu Wang sdmac->watermark_level |= hwml; 9438391ecf4SShengjiu Wang sdmac->watermark_level |= lwml << 16; 9448391ecf4SShengjiu Wang swap(sdmac->event_mask[0], sdmac->event_mask[1]); 9458391ecf4SShengjiu Wang } 9468391ecf4SShengjiu Wang 9478391ecf4SShengjiu Wang if (sdmac->per_address2 >= sdma->spba_start_addr && 9488391ecf4SShengjiu Wang sdmac->per_address2 <= sdma->spba_end_addr) 9498391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP; 9508391ecf4SShengjiu Wang 9518391ecf4SShengjiu Wang if (sdmac->per_address >= sdma->spba_start_addr && 9528391ecf4SShengjiu Wang sdmac->per_address <= sdma->spba_end_addr) 9538391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP; 9548391ecf4SShengjiu Wang 9558391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT; 9568391ecf4SShengjiu Wang } 9578391ecf4SShengjiu Wang 9587b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan) 9591ec1e82fSSascha Hauer { 9607b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 9611ec1e82fSSascha Hauer int ret; 9621ec1e82fSSascha Hauer 9637b350ab0SMaxime Ripard sdma_disable_channel(chan); 9641ec1e82fSSascha Hauer 9650bbc1413SRichard Zhao sdmac->event_mask[0] = 0; 9660bbc1413SRichard Zhao sdmac->event_mask[1] = 0; 9671ec1e82fSSascha Hauer sdmac->shp_addr = 0; 9681ec1e82fSSascha Hauer sdmac->per_addr = 0; 9691ec1e82fSSascha Hauer 9701ec1e82fSSascha Hauer if (sdmac->event_id0) { 97117bba72fSSascha Hauer if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events) 9721ec1e82fSSascha Hauer return -EINVAL; 9731ec1e82fSSascha Hauer sdma_event_enable(sdmac, sdmac->event_id0); 9741ec1e82fSSascha Hauer } 9751ec1e82fSSascha Hauer 9768391ecf4SShengjiu Wang if (sdmac->event_id1) { 9778391ecf4SShengjiu Wang if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events) 9788391ecf4SShengjiu Wang return -EINVAL; 9798391ecf4SShengjiu Wang sdma_event_enable(sdmac, sdmac->event_id1); 9808391ecf4SShengjiu Wang } 9818391ecf4SShengjiu Wang 9821ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 9831ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 9841ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 9851ec1e82fSSascha Hauer break; 9861ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 9871ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 9881ec1e82fSSascha Hauer break; 9891ec1e82fSSascha Hauer default: 9901ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 9911ec1e82fSSascha Hauer break; 9921ec1e82fSSascha Hauer } 9931ec1e82fSSascha Hauer 9941ec1e82fSSascha Hauer sdma_get_pc(sdmac, sdmac->peripheral_type); 9951ec1e82fSSascha Hauer 9961ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 9971ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 9981ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 9991ec1e82fSSascha Hauer if (sdmac->event_id1) { 10008391ecf4SShengjiu Wang if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP || 10018391ecf4SShengjiu Wang sdmac->peripheral_type == IMX_DMATYPE_ASRC) 10028391ecf4SShengjiu Wang sdma_set_watermarklevel_for_p2p(sdmac); 10038391ecf4SShengjiu Wang } else 10040bbc1413SRichard Zhao __set_bit(sdmac->event_id0, sdmac->event_mask); 10058391ecf4SShengjiu Wang 10061ec1e82fSSascha Hauer /* Address */ 10071ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 10088391ecf4SShengjiu Wang sdmac->per_addr = sdmac->per_address2; 10091ec1e82fSSascha Hauer } else { 10101ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 10111ec1e82fSSascha Hauer } 10121ec1e82fSSascha Hauer 10131ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 10141ec1e82fSSascha Hauer 10151ec1e82fSSascha Hauer return ret; 10161ec1e82fSSascha Hauer } 10171ec1e82fSSascha Hauer 10181ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 10191ec1e82fSSascha Hauer unsigned int priority) 10201ec1e82fSSascha Hauer { 10211ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10221ec1e82fSSascha Hauer int channel = sdmac->channel; 10231ec1e82fSSascha Hauer 10241ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 10251ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 10261ec1e82fSSascha Hauer return -EINVAL; 10271ec1e82fSSascha Hauer } 10281ec1e82fSSascha Hauer 1029c4b56857SRichard Zhao writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 10301ec1e82fSSascha Hauer 10311ec1e82fSSascha Hauer return 0; 10321ec1e82fSSascha Hauer } 10331ec1e82fSSascha Hauer 10341ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac) 10351ec1e82fSSascha Hauer { 10361ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10371ec1e82fSSascha Hauer int channel = sdmac->channel; 10381ec1e82fSSascha Hauer int ret = -EBUSY; 10391ec1e82fSSascha Hauer 10409f92d223SJoe Perches sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, 10419f92d223SJoe Perches GFP_KERNEL); 10421ec1e82fSSascha Hauer if (!sdmac->bd) { 10431ec1e82fSSascha Hauer ret = -ENOMEM; 10441ec1e82fSSascha Hauer goto out; 10451ec1e82fSSascha Hauer } 10461ec1e82fSSascha Hauer 10471ec1e82fSSascha Hauer sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys; 10481ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 10491ec1e82fSSascha Hauer 10501ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY); 10511ec1e82fSSascha Hauer return 0; 10521ec1e82fSSascha Hauer out: 10531ec1e82fSSascha Hauer 10541ec1e82fSSascha Hauer return ret; 10551ec1e82fSSascha Hauer } 10561ec1e82fSSascha Hauer 10571ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx) 10581ec1e82fSSascha Hauer { 1059f69f2e26SHaitao Zhang unsigned long flags; 10601ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(tx->chan); 10611ec1e82fSSascha Hauer dma_cookie_t cookie; 10621ec1e82fSSascha Hauer 1063f69f2e26SHaitao Zhang spin_lock_irqsave(&sdmac->lock, flags); 10641ec1e82fSSascha Hauer 1065884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 10661ec1e82fSSascha Hauer 1067f69f2e26SHaitao Zhang spin_unlock_irqrestore(&sdmac->lock, flags); 10681ec1e82fSSascha Hauer 10691ec1e82fSSascha Hauer return cookie; 10701ec1e82fSSascha Hauer } 10711ec1e82fSSascha Hauer 10721ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 10731ec1e82fSSascha Hauer { 10741ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 10751ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 10761ec1e82fSSascha Hauer int prio, ret; 10771ec1e82fSSascha Hauer 10781ec1e82fSSascha Hauer if (!data) 10791ec1e82fSSascha Hauer return -EINVAL; 10801ec1e82fSSascha Hauer 10811ec1e82fSSascha Hauer switch (data->priority) { 10821ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 10831ec1e82fSSascha Hauer prio = 3; 10841ec1e82fSSascha Hauer break; 10851ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 10861ec1e82fSSascha Hauer prio = 2; 10871ec1e82fSSascha Hauer break; 10881ec1e82fSSascha Hauer case DMA_PRIO_LOW: 10891ec1e82fSSascha Hauer default: 10901ec1e82fSSascha Hauer prio = 1; 10911ec1e82fSSascha Hauer break; 10921ec1e82fSSascha Hauer } 10931ec1e82fSSascha Hauer 10941ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 10951ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 10968391ecf4SShengjiu Wang sdmac->event_id1 = data->dma_request2; 1097c2c744d3SRichard Zhao 1098b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ipg); 1099b93edcddSFabio Estevam if (ret) 1100b93edcddSFabio Estevam return ret; 1101b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ahb); 1102b93edcddSFabio Estevam if (ret) 1103b93edcddSFabio Estevam goto disable_clk_ipg; 1104c2c744d3SRichard Zhao 11053bb5e7caSRichard Zhao ret = sdma_request_channel(sdmac); 11061ec1e82fSSascha Hauer if (ret) 1107b93edcddSFabio Estevam goto disable_clk_ahb; 11081ec1e82fSSascha Hauer 11093bb5e7caSRichard Zhao ret = sdma_set_channel_priority(sdmac, prio); 11101ec1e82fSSascha Hauer if (ret) 1111b93edcddSFabio Estevam goto disable_clk_ahb; 11121ec1e82fSSascha Hauer 11131ec1e82fSSascha Hauer dma_async_tx_descriptor_init(&sdmac->desc, chan); 11141ec1e82fSSascha Hauer sdmac->desc.tx_submit = sdma_tx_submit; 11151ec1e82fSSascha Hauer /* txd.flags will be overwritten in prep funcs */ 11161ec1e82fSSascha Hauer sdmac->desc.flags = DMA_CTRL_ACK; 11171ec1e82fSSascha Hauer 11181ec1e82fSSascha Hauer return 0; 1119b93edcddSFabio Estevam 1120b93edcddSFabio Estevam disable_clk_ahb: 1121b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ahb); 1122b93edcddSFabio Estevam disable_clk_ipg: 1123b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ipg); 1124b93edcddSFabio Estevam return ret; 11251ec1e82fSSascha Hauer } 11261ec1e82fSSascha Hauer 11271ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 11281ec1e82fSSascha Hauer { 11291ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11301ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11311ec1e82fSSascha Hauer 11327b350ab0SMaxime Ripard sdma_disable_channel(chan); 11331ec1e82fSSascha Hauer 11341ec1e82fSSascha Hauer if (sdmac->event_id0) 11351ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 11361ec1e82fSSascha Hauer if (sdmac->event_id1) 11371ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 11381ec1e82fSSascha Hauer 11391ec1e82fSSascha Hauer sdmac->event_id0 = 0; 11401ec1e82fSSascha Hauer sdmac->event_id1 = 0; 11411ec1e82fSSascha Hauer 11421ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 11431ec1e82fSSascha Hauer 11441ec1e82fSSascha Hauer dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys); 11451ec1e82fSSascha Hauer 11467560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 11477560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 11481ec1e82fSSascha Hauer } 11491ec1e82fSSascha Hauer 11501ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 11511ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 1152db8196dfSVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 1153185ecb5fSAlexandre Bounine unsigned long flags, void *context) 11541ec1e82fSSascha Hauer { 11551ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11561ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11571ec1e82fSSascha Hauer int ret, i, count; 115823889c63SSascha Hauer int channel = sdmac->channel; 11591ec1e82fSSascha Hauer struct scatterlist *sg; 11601ec1e82fSSascha Hauer 11611ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 11621ec1e82fSSascha Hauer return NULL; 11631ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 11641ec1e82fSSascha Hauer 11651ec1e82fSSascha Hauer sdmac->flags = 0; 11661ec1e82fSSascha Hauer 11678e2e27c7SRichard Zhao sdmac->buf_tail = 0; 11688e2e27c7SRichard Zhao 11691ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 11701ec1e82fSSascha Hauer sg_len, channel); 11711ec1e82fSSascha Hauer 11721ec1e82fSSascha Hauer sdmac->direction = direction; 11731ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 11741ec1e82fSSascha Hauer if (ret) 11751ec1e82fSSascha Hauer goto err_out; 11761ec1e82fSSascha Hauer 11771ec1e82fSSascha Hauer if (sg_len > NUM_BD) { 11781ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 11791ec1e82fSSascha Hauer channel, sg_len, NUM_BD); 11801ec1e82fSSascha Hauer ret = -EINVAL; 11811ec1e82fSSascha Hauer goto err_out; 11821ec1e82fSSascha Hauer } 11831ec1e82fSSascha Hauer 1184ab59a510SHuang Shijie sdmac->chn_count = 0; 11851ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 11861ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 11871ec1e82fSSascha Hauer int param; 11881ec1e82fSSascha Hauer 1189d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 11901ec1e82fSSascha Hauer 1191fdaf9c4bSLars-Peter Clausen count = sg_dma_len(sg); 11921ec1e82fSSascha Hauer 11931ec1e82fSSascha Hauer if (count > 0xffff) { 11941ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 11951ec1e82fSSascha Hauer channel, count, 0xffff); 11961ec1e82fSSascha Hauer ret = -EINVAL; 11971ec1e82fSSascha Hauer goto err_out; 11981ec1e82fSSascha Hauer } 11991ec1e82fSSascha Hauer 12001ec1e82fSSascha Hauer bd->mode.count = count; 1201ab59a510SHuang Shijie sdmac->chn_count += count; 12021ec1e82fSSascha Hauer 12031ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { 12041ec1e82fSSascha Hauer ret = -EINVAL; 12051ec1e82fSSascha Hauer goto err_out; 12061ec1e82fSSascha Hauer } 12071fa81c27SSascha Hauer 12081fa81c27SSascha Hauer switch (sdmac->word_size) { 12091fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 12101ec1e82fSSascha Hauer bd->mode.command = 0; 12111fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 12121fa81c27SSascha Hauer return NULL; 12131fa81c27SSascha Hauer break; 12141fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 12151fa81c27SSascha Hauer bd->mode.command = 2; 12161fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 12171fa81c27SSascha Hauer return NULL; 12181fa81c27SSascha Hauer break; 12191fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 12201fa81c27SSascha Hauer bd->mode.command = 1; 12211fa81c27SSascha Hauer break; 12221fa81c27SSascha Hauer default: 12231fa81c27SSascha Hauer return NULL; 12241fa81c27SSascha Hauer } 12251ec1e82fSSascha Hauer 12261ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 12271ec1e82fSSascha Hauer 1228341b9419SShawn Guo if (i + 1 == sg_len) { 12291ec1e82fSSascha Hauer param |= BD_INTR; 1230341b9419SShawn Guo param |= BD_LAST; 1231341b9419SShawn Guo param &= ~BD_CONT; 12321ec1e82fSSascha Hauer } 12331ec1e82fSSascha Hauer 1234c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1235c3cc74b2SOlof Johansson i, count, (u64)sg->dma_address, 12361ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 12371ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 12381ec1e82fSSascha Hauer 12391ec1e82fSSascha Hauer bd->mode.status = param; 12401ec1e82fSSascha Hauer } 12411ec1e82fSSascha Hauer 12421ec1e82fSSascha Hauer sdmac->num_bd = sg_len; 12431ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 12441ec1e82fSSascha Hauer 12451ec1e82fSSascha Hauer return &sdmac->desc; 12461ec1e82fSSascha Hauer err_out: 12474b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 12481ec1e82fSSascha Hauer return NULL; 12491ec1e82fSSascha Hauer } 12501ec1e82fSSascha Hauer 12511ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 12521ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 1253185ecb5fSAlexandre Bounine size_t period_len, enum dma_transfer_direction direction, 125431c1e5a1SLaurent Pinchart unsigned long flags) 12551ec1e82fSSascha Hauer { 12561ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 12571ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 12581ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 125923889c63SSascha Hauer int channel = sdmac->channel; 12601ec1e82fSSascha Hauer int ret, i = 0, buf = 0; 12611ec1e82fSSascha Hauer 12621ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 12631ec1e82fSSascha Hauer 12641ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 12651ec1e82fSSascha Hauer return NULL; 12661ec1e82fSSascha Hauer 12671ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 12681ec1e82fSSascha Hauer 12698e2e27c7SRichard Zhao sdmac->buf_tail = 0; 1270d1a792f3SRussell King - ARM Linux sdmac->period_len = period_len; 12718e2e27c7SRichard Zhao 12721ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 12731ec1e82fSSascha Hauer sdmac->direction = direction; 12741ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 12751ec1e82fSSascha Hauer if (ret) 12761ec1e82fSSascha Hauer goto err_out; 12771ec1e82fSSascha Hauer 12781ec1e82fSSascha Hauer if (num_periods > NUM_BD) { 12791ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 12801ec1e82fSSascha Hauer channel, num_periods, NUM_BD); 12811ec1e82fSSascha Hauer goto err_out; 12821ec1e82fSSascha Hauer } 12831ec1e82fSSascha Hauer 12841ec1e82fSSascha Hauer if (period_len > 0xffff) { 12851ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n", 12861ec1e82fSSascha Hauer channel, period_len, 0xffff); 12871ec1e82fSSascha Hauer goto err_out; 12881ec1e82fSSascha Hauer } 12891ec1e82fSSascha Hauer 12901ec1e82fSSascha Hauer while (buf < buf_len) { 12911ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 12921ec1e82fSSascha Hauer int param; 12931ec1e82fSSascha Hauer 12941ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 12951ec1e82fSSascha Hauer 12961ec1e82fSSascha Hauer bd->mode.count = period_len; 12971ec1e82fSSascha Hauer 12981ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 12991ec1e82fSSascha Hauer goto err_out; 13001ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 13011ec1e82fSSascha Hauer bd->mode.command = 0; 13021ec1e82fSSascha Hauer else 13031ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 13041ec1e82fSSascha Hauer 13051ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 13061ec1e82fSSascha Hauer if (i + 1 == num_periods) 13071ec1e82fSSascha Hauer param |= BD_WRAP; 13081ec1e82fSSascha Hauer 1309c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1310c3cc74b2SOlof Johansson i, period_len, (u64)dma_addr, 13111ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 13121ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 13131ec1e82fSSascha Hauer 13141ec1e82fSSascha Hauer bd->mode.status = param; 13151ec1e82fSSascha Hauer 13161ec1e82fSSascha Hauer dma_addr += period_len; 13171ec1e82fSSascha Hauer buf += period_len; 13181ec1e82fSSascha Hauer 13191ec1e82fSSascha Hauer i++; 13201ec1e82fSSascha Hauer } 13211ec1e82fSSascha Hauer 13221ec1e82fSSascha Hauer sdmac->num_bd = num_periods; 13231ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 13241ec1e82fSSascha Hauer 13251ec1e82fSSascha Hauer return &sdmac->desc; 13261ec1e82fSSascha Hauer err_out: 13271ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 13281ec1e82fSSascha Hauer return NULL; 13291ec1e82fSSascha Hauer } 13301ec1e82fSSascha Hauer 13317b350ab0SMaxime Ripard static int sdma_config(struct dma_chan *chan, 13327b350ab0SMaxime Ripard struct dma_slave_config *dmaengine_cfg) 13331ec1e82fSSascha Hauer { 13341ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13351ec1e82fSSascha Hauer 1336db8196dfSVinod Koul if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) { 13371ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 133894ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->src_maxburst * 133994ac27a5SPhilippe Rétornaz dmaengine_cfg->src_addr_width; 13401ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 13418391ecf4SShengjiu Wang } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) { 13428391ecf4SShengjiu Wang sdmac->per_address2 = dmaengine_cfg->src_addr; 13438391ecf4SShengjiu Wang sdmac->per_address = dmaengine_cfg->dst_addr; 13448391ecf4SShengjiu Wang sdmac->watermark_level = dmaengine_cfg->src_maxburst & 13458391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_LWML; 13468391ecf4SShengjiu Wang sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) & 13478391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML; 13488391ecf4SShengjiu Wang sdmac->word_size = dmaengine_cfg->dst_addr_width; 13491ec1e82fSSascha Hauer } else { 13501ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 135194ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->dst_maxburst * 135294ac27a5SPhilippe Rétornaz dmaengine_cfg->dst_addr_width; 13531ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 13541ec1e82fSSascha Hauer } 1355e6966433SHuang Shijie sdmac->direction = dmaengine_cfg->direction; 13567b350ab0SMaxime Ripard return sdma_config_channel(chan); 13571ec1e82fSSascha Hauer } 13581ec1e82fSSascha Hauer 13591ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 13601ec1e82fSSascha Hauer dma_cookie_t cookie, 13611ec1e82fSSascha Hauer struct dma_tx_state *txstate) 13621ec1e82fSSascha Hauer { 13631ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 1364d1a792f3SRussell King - ARM Linux u32 residue; 1365d1a792f3SRussell King - ARM Linux 1366d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 1367*5881826dSNandor Han residue = (sdmac->num_bd - sdmac->buf_tail) * 1368*5881826dSNandor Han sdmac->period_len - sdmac->chn_real_count; 1369d1a792f3SRussell King - ARM Linux else 1370d1a792f3SRussell King - ARM Linux residue = sdmac->chn_count - sdmac->chn_real_count; 13711ec1e82fSSascha Hauer 1372e8e3a790SAndy Shevchenko dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, 1373d1a792f3SRussell King - ARM Linux residue); 13741ec1e82fSSascha Hauer 13758a965911SShawn Guo return sdmac->status; 13761ec1e82fSSascha Hauer } 13771ec1e82fSSascha Hauer 13781ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 13791ec1e82fSSascha Hauer { 13802b4f130eSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13812b4f130eSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 13822b4f130eSSascha Hauer 13832b4f130eSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 13842b4f130eSSascha Hauer sdma_enable_channel(sdma, sdmac->channel); 13851ec1e82fSSascha Hauer } 13861ec1e82fSSascha Hauer 13875b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 1388cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38 1389a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41 13905b28aa31SSascha Hauer 13915b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 13925b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 13935b28aa31SSascha Hauer { 13945b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 13955b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 13965b28aa31SSascha Hauer int i; 13975b28aa31SSascha Hauer 139870dabaedSNicolin Chen /* use the default firmware in ROM if missing external firmware */ 139970dabaedSNicolin Chen if (!sdma->script_number) 140070dabaedSNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 140170dabaedSNicolin Chen 1402cd72b846SNicolin Chen for (i = 0; i < sdma->script_number; i++) 14035b28aa31SSascha Hauer if (addr_arr[i] > 0) 14045b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 14055b28aa31SSascha Hauer } 14065b28aa31SSascha Hauer 14077b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context) 14085b28aa31SSascha Hauer { 14097b4b88e0SSascha Hauer struct sdma_engine *sdma = context; 14105b28aa31SSascha Hauer const struct sdma_firmware_header *header; 14115b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 14125b28aa31SSascha Hauer unsigned short *ram_code; 14135b28aa31SSascha Hauer 14147b4b88e0SSascha Hauer if (!fw) { 14150f927a11SSascha Hauer dev_info(sdma->dev, "external firmware not found, using ROM firmware\n"); 14160f927a11SSascha Hauer /* In this case we just use the ROM firmware. */ 14177b4b88e0SSascha Hauer return; 14187b4b88e0SSascha Hauer } 14195b28aa31SSascha Hauer 14205b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 14215b28aa31SSascha Hauer goto err_firmware; 14225b28aa31SSascha Hauer 14235b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 14245b28aa31SSascha Hauer 14255b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 14265b28aa31SSascha Hauer goto err_firmware; 14275b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 14285b28aa31SSascha Hauer goto err_firmware; 1429cd72b846SNicolin Chen switch (header->version_major) { 1430cd72b846SNicolin Chen case 1: 1431cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 1432cd72b846SNicolin Chen break; 1433cd72b846SNicolin Chen case 2: 1434cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2; 1435cd72b846SNicolin Chen break; 1436a572460bSFabio Estevam case 3: 1437a572460bSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3; 1438a572460bSFabio Estevam break; 1439cd72b846SNicolin Chen default: 1440cd72b846SNicolin Chen dev_err(sdma->dev, "unknown firmware version\n"); 1441cd72b846SNicolin Chen goto err_firmware; 1442cd72b846SNicolin Chen } 14435b28aa31SSascha Hauer 14445b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 14455b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 14465b28aa31SSascha Hauer 14477560e3f3SSascha Hauer clk_enable(sdma->clk_ipg); 14487560e3f3SSascha Hauer clk_enable(sdma->clk_ahb); 14495b28aa31SSascha Hauer /* download the RAM image for SDMA */ 14505b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 14515b28aa31SSascha Hauer header->ram_code_size, 14526866fd3bSSascha Hauer addr->ram_code_start_addr); 14537560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 14547560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 14555b28aa31SSascha Hauer 14565b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 14575b28aa31SSascha Hauer 14585b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 14595b28aa31SSascha Hauer header->version_major, 14605b28aa31SSascha Hauer header->version_minor); 14615b28aa31SSascha Hauer 14625b28aa31SSascha Hauer err_firmware: 14635b28aa31SSascha Hauer release_firmware(fw); 14647b4b88e0SSascha Hauer } 14657b4b88e0SSascha Hauer 1466d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3 1467d078cd1bSZidan Wang 146829f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma) 1469d078cd1bSZidan Wang { 1470d078cd1bSZidan Wang struct device_node *np = sdma->dev->of_node; 1471d078cd1bSZidan Wang struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0); 1472d078cd1bSZidan Wang struct property *event_remap; 1473d078cd1bSZidan Wang struct regmap *gpr; 1474d078cd1bSZidan Wang char propname[] = "fsl,sdma-event-remap"; 1475d078cd1bSZidan Wang u32 reg, val, shift, num_map, i; 1476d078cd1bSZidan Wang int ret = 0; 1477d078cd1bSZidan Wang 1478d078cd1bSZidan Wang if (IS_ERR(np) || IS_ERR(gpr_np)) 1479d078cd1bSZidan Wang goto out; 1480d078cd1bSZidan Wang 1481d078cd1bSZidan Wang event_remap = of_find_property(np, propname, NULL); 1482d078cd1bSZidan Wang num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0; 1483d078cd1bSZidan Wang if (!num_map) { 1484ce078af7SFabio Estevam dev_dbg(sdma->dev, "no event needs to be remapped\n"); 1485d078cd1bSZidan Wang goto out; 1486d078cd1bSZidan Wang } else if (num_map % EVENT_REMAP_CELLS) { 1487d078cd1bSZidan Wang dev_err(sdma->dev, "the property %s must modulo %d\n", 1488d078cd1bSZidan Wang propname, EVENT_REMAP_CELLS); 1489d078cd1bSZidan Wang ret = -EINVAL; 1490d078cd1bSZidan Wang goto out; 1491d078cd1bSZidan Wang } 1492d078cd1bSZidan Wang 1493d078cd1bSZidan Wang gpr = syscon_node_to_regmap(gpr_np); 1494d078cd1bSZidan Wang if (IS_ERR(gpr)) { 1495d078cd1bSZidan Wang dev_err(sdma->dev, "failed to get gpr regmap\n"); 1496d078cd1bSZidan Wang ret = PTR_ERR(gpr); 1497d078cd1bSZidan Wang goto out; 1498d078cd1bSZidan Wang } 1499d078cd1bSZidan Wang 1500d078cd1bSZidan Wang for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) { 1501d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i, ®); 1502d078cd1bSZidan Wang if (ret) { 1503d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1504d078cd1bSZidan Wang propname, i); 1505d078cd1bSZidan Wang goto out; 1506d078cd1bSZidan Wang } 1507d078cd1bSZidan Wang 1508d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 1, &shift); 1509d078cd1bSZidan Wang if (ret) { 1510d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1511d078cd1bSZidan Wang propname, i + 1); 1512d078cd1bSZidan Wang goto out; 1513d078cd1bSZidan Wang } 1514d078cd1bSZidan Wang 1515d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 2, &val); 1516d078cd1bSZidan Wang if (ret) { 1517d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1518d078cd1bSZidan Wang propname, i + 2); 1519d078cd1bSZidan Wang goto out; 1520d078cd1bSZidan Wang } 1521d078cd1bSZidan Wang 1522d078cd1bSZidan Wang regmap_update_bits(gpr, reg, BIT(shift), val << shift); 1523d078cd1bSZidan Wang } 1524d078cd1bSZidan Wang 1525d078cd1bSZidan Wang out: 1526d078cd1bSZidan Wang if (!IS_ERR(gpr_np)) 1527d078cd1bSZidan Wang of_node_put(gpr_np); 1528d078cd1bSZidan Wang 1529d078cd1bSZidan Wang return ret; 1530d078cd1bSZidan Wang } 1531d078cd1bSZidan Wang 1532fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma, 15337b4b88e0SSascha Hauer const char *fw_name) 15347b4b88e0SSascha Hauer { 15357b4b88e0SSascha Hauer int ret; 15367b4b88e0SSascha Hauer 15377b4b88e0SSascha Hauer ret = request_firmware_nowait(THIS_MODULE, 15387b4b88e0SSascha Hauer FW_ACTION_HOTPLUG, fw_name, sdma->dev, 15397b4b88e0SSascha Hauer GFP_KERNEL, sdma, sdma_load_firmware); 15405b28aa31SSascha Hauer 15415b28aa31SSascha Hauer return ret; 15425b28aa31SSascha Hauer } 15435b28aa31SSascha Hauer 154419bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma) 15451ec1e82fSSascha Hauer { 15461ec1e82fSSascha Hauer int i, ret; 15471ec1e82fSSascha Hauer dma_addr_t ccb_phys; 15481ec1e82fSSascha Hauer 1549b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ipg); 1550b93edcddSFabio Estevam if (ret) 1551b93edcddSFabio Estevam return ret; 1552b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ahb); 1553b93edcddSFabio Estevam if (ret) 1554b93edcddSFabio Estevam goto disable_clk_ipg; 15551ec1e82fSSascha Hauer 15561ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 1557c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); 15581ec1e82fSSascha Hauer 15591ec1e82fSSascha Hauer sdma->channel_control = dma_alloc_coherent(NULL, 15601ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + 15611ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 15621ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 15631ec1e82fSSascha Hauer 15641ec1e82fSSascha Hauer if (!sdma->channel_control) { 15651ec1e82fSSascha Hauer ret = -ENOMEM; 15661ec1e82fSSascha Hauer goto err_dma_alloc; 15671ec1e82fSSascha Hauer } 15681ec1e82fSSascha Hauer 15691ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 15701ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 15711ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 15721ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 15731ec1e82fSSascha Hauer 15741ec1e82fSSascha Hauer /* Zero-out the CCB structures array just allocated */ 15751ec1e82fSSascha Hauer memset(sdma->channel_control, 0, 15761ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control)); 15771ec1e82fSSascha Hauer 15781ec1e82fSSascha Hauer /* disable all channels */ 157917bba72fSSascha Hauer for (i = 0; i < sdma->drvdata->num_events; i++) 1580c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); 15811ec1e82fSSascha Hauer 15821ec1e82fSSascha Hauer /* All channels have priority 0 */ 15831ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 1584c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 15851ec1e82fSSascha Hauer 15861ec1e82fSSascha Hauer ret = sdma_request_channel(&sdma->channel[0]); 15871ec1e82fSSascha Hauer if (ret) 15881ec1e82fSSascha Hauer goto err_dma_alloc; 15891ec1e82fSSascha Hauer 15901ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 15911ec1e82fSSascha Hauer 15921ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 1593c4b56857SRichard Zhao writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); 15941ec1e82fSSascha Hauer 15951ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 15961ec1e82fSSascha Hauer /* FIXME: Check whether to set ACR bit depending on clock ratios */ 1597c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); 15981ec1e82fSSascha Hauer 1599c4b56857SRichard Zhao writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); 16001ec1e82fSSascha Hauer 16011ec1e82fSSascha Hauer /* Initializes channel's priorities */ 16021ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 16031ec1e82fSSascha Hauer 16047560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 16057560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 16061ec1e82fSSascha Hauer 16071ec1e82fSSascha Hauer return 0; 16081ec1e82fSSascha Hauer 16091ec1e82fSSascha Hauer err_dma_alloc: 16107560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 1611b93edcddSFabio Estevam disable_clk_ipg: 1612b93edcddSFabio Estevam clk_disable(sdma->clk_ipg); 16131ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 16141ec1e82fSSascha Hauer return ret; 16151ec1e82fSSascha Hauer } 16161ec1e82fSSascha Hauer 16179479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param) 16189479e17cSShawn Guo { 16190b351865SNicolin Chen struct sdma_channel *sdmac = to_sdma_chan(chan); 16209479e17cSShawn Guo struct imx_dma_data *data = fn_param; 16219479e17cSShawn Guo 16229479e17cSShawn Guo if (!imx_dma_is_general_purpose(chan)) 16239479e17cSShawn Guo return false; 16249479e17cSShawn Guo 16250b351865SNicolin Chen sdmac->data = *data; 16260b351865SNicolin Chen chan->private = &sdmac->data; 16279479e17cSShawn Guo 16289479e17cSShawn Guo return true; 16299479e17cSShawn Guo } 16309479e17cSShawn Guo 16319479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec, 16329479e17cSShawn Guo struct of_dma *ofdma) 16339479e17cSShawn Guo { 16349479e17cSShawn Guo struct sdma_engine *sdma = ofdma->of_dma_data; 16359479e17cSShawn Guo dma_cap_mask_t mask = sdma->dma_device.cap_mask; 16369479e17cSShawn Guo struct imx_dma_data data; 16379479e17cSShawn Guo 16389479e17cSShawn Guo if (dma_spec->args_count != 3) 16399479e17cSShawn Guo return NULL; 16409479e17cSShawn Guo 16419479e17cSShawn Guo data.dma_request = dma_spec->args[0]; 16429479e17cSShawn Guo data.peripheral_type = dma_spec->args[1]; 16439479e17cSShawn Guo data.priority = dma_spec->args[2]; 16448391ecf4SShengjiu Wang /* 16458391ecf4SShengjiu Wang * init dma_request2 to zero, which is not used by the dts. 16468391ecf4SShengjiu Wang * For P2P, dma_request2 is init from dma_request_channel(), 16478391ecf4SShengjiu Wang * chan->private will point to the imx_dma_data, and in 16488391ecf4SShengjiu Wang * device_alloc_chan_resources(), imx_dma_data.dma_request2 will 16498391ecf4SShengjiu Wang * be set to sdmac->event_id1. 16508391ecf4SShengjiu Wang */ 16518391ecf4SShengjiu Wang data.dma_request2 = 0; 16529479e17cSShawn Guo 16539479e17cSShawn Guo return dma_request_channel(mask, sdma_filter_fn, &data); 16549479e17cSShawn Guo } 16559479e17cSShawn Guo 1656e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev) 16571ec1e82fSSascha Hauer { 1658580975d7SShawn Guo const struct of_device_id *of_id = 1659580975d7SShawn Guo of_match_device(sdma_dt_ids, &pdev->dev); 1660580975d7SShawn Guo struct device_node *np = pdev->dev.of_node; 16618391ecf4SShengjiu Wang struct device_node *spba_bus; 1662580975d7SShawn Guo const char *fw_name; 16631ec1e82fSSascha Hauer int ret; 16641ec1e82fSSascha Hauer int irq; 16651ec1e82fSSascha Hauer struct resource *iores; 16668391ecf4SShengjiu Wang struct resource spba_res; 1667d4adcc01SJingoo Han struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev); 16681ec1e82fSSascha Hauer int i; 16691ec1e82fSSascha Hauer struct sdma_engine *sdma; 167036e2f21aSSascha Hauer s32 *saddr_arr; 167117bba72fSSascha Hauer const struct sdma_driver_data *drvdata = NULL; 167217bba72fSSascha Hauer 167317bba72fSSascha Hauer if (of_id) 167417bba72fSSascha Hauer drvdata = of_id->data; 167517bba72fSSascha Hauer else if (pdev->id_entry) 167617bba72fSSascha Hauer drvdata = (void *)pdev->id_entry->driver_data; 167717bba72fSSascha Hauer 167817bba72fSSascha Hauer if (!drvdata) { 167917bba72fSSascha Hauer dev_err(&pdev->dev, "unable to find driver data\n"); 168017bba72fSSascha Hauer return -EINVAL; 168117bba72fSSascha Hauer } 16821ec1e82fSSascha Hauer 168342536b9fSPhilippe Retornaz ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 168442536b9fSPhilippe Retornaz if (ret) 168542536b9fSPhilippe Retornaz return ret; 168642536b9fSPhilippe Retornaz 16877f24e0eeSFabio Estevam sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL); 16881ec1e82fSSascha Hauer if (!sdma) 16891ec1e82fSSascha Hauer return -ENOMEM; 16901ec1e82fSSascha Hauer 16912ccaef05SRichard Zhao spin_lock_init(&sdma->channel_0_lock); 169273eab978SSascha Hauer 16931ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 169417bba72fSSascha Hauer sdma->drvdata = drvdata; 16951ec1e82fSSascha Hauer 16961ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 16977f24e0eeSFabio Estevam if (irq < 0) 169863c72e02SFabio Estevam return irq; 16991ec1e82fSSascha Hauer 17007f24e0eeSFabio Estevam iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 17017f24e0eeSFabio Estevam sdma->regs = devm_ioremap_resource(&pdev->dev, iores); 17027f24e0eeSFabio Estevam if (IS_ERR(sdma->regs)) 17037f24e0eeSFabio Estevam return PTR_ERR(sdma->regs); 17041ec1e82fSSascha Hauer 17057560e3f3SSascha Hauer sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 17067f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ipg)) 17077f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ipg); 17081ec1e82fSSascha Hauer 17097560e3f3SSascha Hauer sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 17107f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ahb)) 17117f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ahb); 17127560e3f3SSascha Hauer 17137560e3f3SSascha Hauer clk_prepare(sdma->clk_ipg); 17147560e3f3SSascha Hauer clk_prepare(sdma->clk_ahb); 17157560e3f3SSascha Hauer 17167f24e0eeSFabio Estevam ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma", 17177f24e0eeSFabio Estevam sdma); 17181ec1e82fSSascha Hauer if (ret) 17197f24e0eeSFabio Estevam return ret; 17201ec1e82fSSascha Hauer 17215bb9dbb5SVinod Koul sdma->irq = irq; 17225bb9dbb5SVinod Koul 17235b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 17247f24e0eeSFabio Estevam if (!sdma->script_addrs) 17257f24e0eeSFabio Estevam return -ENOMEM; 17261ec1e82fSSascha Hauer 172736e2f21aSSascha Hauer /* initially no scripts available */ 172836e2f21aSSascha Hauer saddr_arr = (s32 *)sdma->script_addrs; 172936e2f21aSSascha Hauer for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) 173036e2f21aSSascha Hauer saddr_arr[i] = -EINVAL; 173136e2f21aSSascha Hauer 17327214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 17337214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 17347214a8b1SSascha Hauer 17351ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 17361ec1e82fSSascha Hauer /* Initialize channel parameters */ 17371ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 17381ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 17391ec1e82fSSascha Hauer 17401ec1e82fSSascha Hauer sdmac->sdma = sdma; 17411ec1e82fSSascha Hauer spin_lock_init(&sdmac->lock); 17421ec1e82fSSascha Hauer 17431ec1e82fSSascha Hauer sdmac->chan.device = &sdma->dma_device; 17448ac69546SRussell King - ARM Linux dma_cookie_init(&sdmac->chan); 17451ec1e82fSSascha Hauer sdmac->channel = i; 17461ec1e82fSSascha Hauer 174715f30f51SNandor Han tasklet_init(&sdmac->tasklet, mxc_sdma_handle_channel_normal, 1748abd9ccc8SHuang Shijie (unsigned long) sdmac); 174923889c63SSascha Hauer /* 175023889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 175123889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 175223889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 175323889c63SSascha Hauer */ 175423889c63SSascha Hauer if (i) 175523889c63SSascha Hauer list_add_tail(&sdmac->chan.device_node, 175623889c63SSascha Hauer &sdma->dma_device.channels); 17571ec1e82fSSascha Hauer } 17581ec1e82fSSascha Hauer 17595b28aa31SSascha Hauer ret = sdma_init(sdma); 17601ec1e82fSSascha Hauer if (ret) 17611ec1e82fSSascha Hauer goto err_init; 17621ec1e82fSSascha Hauer 1763d078cd1bSZidan Wang ret = sdma_event_remap(sdma); 1764d078cd1bSZidan Wang if (ret) 1765d078cd1bSZidan Wang goto err_init; 1766d078cd1bSZidan Wang 1767dcfec3c0SSascha Hauer if (sdma->drvdata->script_addrs) 1768dcfec3c0SSascha Hauer sdma_add_scripts(sdma, sdma->drvdata->script_addrs); 1769580975d7SShawn Guo if (pdata && pdata->script_addrs) 17705b28aa31SSascha Hauer sdma_add_scripts(sdma, pdata->script_addrs); 17715b28aa31SSascha Hauer 1772580975d7SShawn Guo if (pdata) { 17736d0d7e2dSFabio Estevam ret = sdma_get_firmware(sdma, pdata->fw_name); 17746d0d7e2dSFabio Estevam if (ret) 1775ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); 1776580975d7SShawn Guo } else { 1777580975d7SShawn Guo /* 1778580975d7SShawn Guo * Because that device tree does not encode ROM script address, 1779580975d7SShawn Guo * the RAM script in firmware is mandatory for device tree 1780580975d7SShawn Guo * probe, otherwise it fails. 1781580975d7SShawn Guo */ 1782580975d7SShawn Guo ret = of_property_read_string(np, "fsl,sdma-ram-script-name", 1783580975d7SShawn Guo &fw_name); 17846602b0ddSFabio Estevam if (ret) 1785ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware name\n"); 17866602b0ddSFabio Estevam else { 1787580975d7SShawn Guo ret = sdma_get_firmware(sdma, fw_name); 17886602b0ddSFabio Estevam if (ret) 1789ad1122e5SFabio Estevam dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); 1790580975d7SShawn Guo } 1791580975d7SShawn Guo } 17925b28aa31SSascha Hauer 17931ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 17941ec1e82fSSascha Hauer 17951ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 17961ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 17971ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 17981ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 17991ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 18007b350ab0SMaxime Ripard sdma->dma_device.device_config = sdma_config; 18017b350ab0SMaxime Ripard sdma->dma_device.device_terminate_all = sdma_disable_channel; 18021e4a4f50SFabio Estevam sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 18031e4a4f50SFabio Estevam sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES); 18041e4a4f50SFabio Estevam sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); 18051e4a4f50SFabio Estevam sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 18061ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 1807b9b3f82fSSascha Hauer sdma->dma_device.dev->dma_parms = &sdma->dma_parms; 1808b9b3f82fSSascha Hauer dma_set_max_seg_size(sdma->dma_device.dev, 65535); 18091ec1e82fSSascha Hauer 181023e11811SVignesh Raman platform_set_drvdata(pdev, sdma); 181123e11811SVignesh Raman 18121ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 18131ec1e82fSSascha Hauer if (ret) { 18141ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 18151ec1e82fSSascha Hauer goto err_init; 18161ec1e82fSSascha Hauer } 18171ec1e82fSSascha Hauer 18189479e17cSShawn Guo if (np) { 18199479e17cSShawn Guo ret = of_dma_controller_register(np, sdma_xlate, sdma); 18209479e17cSShawn Guo if (ret) { 18219479e17cSShawn Guo dev_err(&pdev->dev, "failed to register controller\n"); 18229479e17cSShawn Guo goto err_register; 18239479e17cSShawn Guo } 18248391ecf4SShengjiu Wang 18258391ecf4SShengjiu Wang spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus"); 18268391ecf4SShengjiu Wang ret = of_address_to_resource(spba_bus, 0, &spba_res); 18278391ecf4SShengjiu Wang if (!ret) { 18288391ecf4SShengjiu Wang sdma->spba_start_addr = spba_res.start; 18298391ecf4SShengjiu Wang sdma->spba_end_addr = spba_res.end; 18308391ecf4SShengjiu Wang } 18318391ecf4SShengjiu Wang of_node_put(spba_bus); 18329479e17cSShawn Guo } 18339479e17cSShawn Guo 18341ec1e82fSSascha Hauer return 0; 18351ec1e82fSSascha Hauer 18369479e17cSShawn Guo err_register: 18379479e17cSShawn Guo dma_async_device_unregister(&sdma->dma_device); 18381ec1e82fSSascha Hauer err_init: 18391ec1e82fSSascha Hauer kfree(sdma->script_addrs); 1840939fd4f0SShawn Guo return ret; 18411ec1e82fSSascha Hauer } 18421ec1e82fSSascha Hauer 18431d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev) 18441ec1e82fSSascha Hauer { 184523e11811SVignesh Raman struct sdma_engine *sdma = platform_get_drvdata(pdev); 1846c12fe497SVignesh Raman int i; 184723e11811SVignesh Raman 18485bb9dbb5SVinod Koul devm_free_irq(&pdev->dev, sdma->irq, sdma); 184923e11811SVignesh Raman dma_async_device_unregister(&sdma->dma_device); 185023e11811SVignesh Raman kfree(sdma->script_addrs); 1851c12fe497SVignesh Raman /* Kill the tasklet */ 1852c12fe497SVignesh Raman for (i = 0; i < MAX_DMA_CHANNELS; i++) { 1853c12fe497SVignesh Raman struct sdma_channel *sdmac = &sdma->channel[i]; 1854c12fe497SVignesh Raman 1855c12fe497SVignesh Raman tasklet_kill(&sdmac->tasklet); 1856c12fe497SVignesh Raman } 185723e11811SVignesh Raman 185823e11811SVignesh Raman platform_set_drvdata(pdev, NULL); 185923e11811SVignesh Raman return 0; 18601ec1e82fSSascha Hauer } 18611ec1e82fSSascha Hauer 18621ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 18631ec1e82fSSascha Hauer .driver = { 18641ec1e82fSSascha Hauer .name = "imx-sdma", 1865580975d7SShawn Guo .of_match_table = sdma_dt_ids, 18661ec1e82fSSascha Hauer }, 186762550cd7SShawn Guo .id_table = sdma_devtypes, 18681d1bbd30SMaxin B. John .remove = sdma_remove, 186923e11811SVignesh Raman .probe = sdma_probe, 18701ec1e82fSSascha Hauer }; 18711ec1e82fSSascha Hauer 187223e11811SVignesh Raman module_platform_driver(sdma_driver); 18731ec1e82fSSascha Hauer 18741ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 18751ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 18761ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 1877