1c01faacaSFabio Estevam // SPDX-License-Identifier: GPL-2.0+ 2c01faacaSFabio Estevam // 3c01faacaSFabio Estevam // drivers/dma/imx-sdma.c 4c01faacaSFabio Estevam // 5c01faacaSFabio Estevam // This file contains a driver for the Freescale Smart DMA engine 6c01faacaSFabio Estevam // 7c01faacaSFabio Estevam // Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 8c01faacaSFabio Estevam // 9c01faacaSFabio Estevam // Based on code from Freescale: 10c01faacaSFabio Estevam // 11c01faacaSFabio Estevam // Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved. 121ec1e82fSSascha Hauer 131ec1e82fSSascha Hauer #include <linux/init.h> 141d069bfaSMichael Olbrich #include <linux/iopoll.h> 15f8de8f4cSAxel Lin #include <linux/module.h> 161ec1e82fSSascha Hauer #include <linux/types.h> 17824a0a02SSascha Hauer #include <linux/bitfield.h> 180bbc1413SRichard Zhao #include <linux/bitops.h> 191ec1e82fSSascha Hauer #include <linux/mm.h> 201ec1e82fSSascha Hauer #include <linux/interrupt.h> 211ec1e82fSSascha Hauer #include <linux/clk.h> 222ccaef05SRichard Zhao #include <linux/delay.h> 231ec1e82fSSascha Hauer #include <linux/sched.h> 241ec1e82fSSascha Hauer #include <linux/semaphore.h> 251ec1e82fSSascha Hauer #include <linux/spinlock.h> 261ec1e82fSSascha Hauer #include <linux/device.h> 271ec1e82fSSascha Hauer #include <linux/dma-mapping.h> 281ec1e82fSSascha Hauer #include <linux/firmware.h> 291ec1e82fSSascha Hauer #include <linux/slab.h> 301ec1e82fSSascha Hauer #include <linux/platform_device.h> 311ec1e82fSSascha Hauer #include <linux/dmaengine.h> 32580975d7SShawn Guo #include <linux/of.h> 338391ecf4SShengjiu Wang #include <linux/of_address.h> 34580975d7SShawn Guo #include <linux/of_device.h> 359479e17cSShawn Guo #include <linux/of_dma.h> 36b8603d2aSLucas Stach #include <linux/workqueue.h> 371ec1e82fSSascha Hauer 381ec1e82fSSascha Hauer #include <asm/irq.h> 39c6547c2eSSascha Hauer #include <linux/dma/imx-dma.h> 40d078cd1bSZidan Wang #include <linux/regmap.h> 41d078cd1bSZidan Wang #include <linux/mfd/syscon.h> 42d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> 431ec1e82fSSascha Hauer 44d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 4557b772b8SRobin Gong #include "virt-dma.h" 46d2ebfb33SRussell King - ARM Linux 471ec1e82fSSascha Hauer /* SDMA registers */ 481ec1e82fSSascha Hauer #define SDMA_H_C0PTR 0x000 491ec1e82fSSascha Hauer #define SDMA_H_INTR 0x004 501ec1e82fSSascha Hauer #define SDMA_H_STATSTOP 0x008 511ec1e82fSSascha Hauer #define SDMA_H_START 0x00c 521ec1e82fSSascha Hauer #define SDMA_H_EVTOVR 0x010 531ec1e82fSSascha Hauer #define SDMA_H_DSPOVR 0x014 541ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR 0x018 551ec1e82fSSascha Hauer #define SDMA_H_EVTPEND 0x01c 561ec1e82fSSascha Hauer #define SDMA_H_DSPENBL 0x020 571ec1e82fSSascha Hauer #define SDMA_H_RESET 0x024 581ec1e82fSSascha Hauer #define SDMA_H_EVTERR 0x028 591ec1e82fSSascha Hauer #define SDMA_H_INTRMSK 0x02c 601ec1e82fSSascha Hauer #define SDMA_H_PSW 0x030 611ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG 0x034 621ec1e82fSSascha Hauer #define SDMA_H_CONFIG 0x038 631ec1e82fSSascha Hauer #define SDMA_ONCE_ENB 0x040 641ec1e82fSSascha Hauer #define SDMA_ONCE_DATA 0x044 651ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR 0x048 661ec1e82fSSascha Hauer #define SDMA_ONCE_STAT 0x04c 671ec1e82fSSascha Hauer #define SDMA_ONCE_CMD 0x050 681ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR 0x054 691ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR 0x058 701ec1e82fSSascha Hauer #define SDMA_CHN0ADDR 0x05c 711ec1e82fSSascha Hauer #define SDMA_ONCE_RTB 0x060 721ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1 0x070 731ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2 0x074 7462550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35 0x200 7562550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31 0x080 761ec1e82fSSascha Hauer #define SDMA_CHNPRI_0 0x100 77824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG 0x1000 781ec1e82fSSascha Hauer 791ec1e82fSSascha Hauer /* 801ec1e82fSSascha Hauer * Buffer descriptor status values. 811ec1e82fSSascha Hauer */ 821ec1e82fSSascha Hauer #define BD_DONE 0x01 831ec1e82fSSascha Hauer #define BD_WRAP 0x02 841ec1e82fSSascha Hauer #define BD_CONT 0x04 851ec1e82fSSascha Hauer #define BD_INTR 0x08 861ec1e82fSSascha Hauer #define BD_RROR 0x10 871ec1e82fSSascha Hauer #define BD_LAST 0x20 881ec1e82fSSascha Hauer #define BD_EXTD 0x80 891ec1e82fSSascha Hauer 901ec1e82fSSascha Hauer /* 911ec1e82fSSascha Hauer * Data Node descriptor status values. 921ec1e82fSSascha Hauer */ 931ec1e82fSSascha Hauer #define DND_END_OF_FRAME 0x80 941ec1e82fSSascha Hauer #define DND_END_OF_XFER 0x40 951ec1e82fSSascha Hauer #define DND_DONE 0x20 961ec1e82fSSascha Hauer #define DND_UNUSED 0x01 971ec1e82fSSascha Hauer 981ec1e82fSSascha Hauer /* 991ec1e82fSSascha Hauer * IPCV2 descriptor status values. 1001ec1e82fSSascha Hauer */ 1011ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME 0x40 1021ec1e82fSSascha Hauer 1031ec1e82fSSascha Hauer #define IPCV2_MAX_NODES 50 1041ec1e82fSSascha Hauer /* 1051ec1e82fSSascha Hauer * Error bit set in the CCB status field by the SDMA, 1061ec1e82fSSascha Hauer * in setbd routine, in case of a transfer error 1071ec1e82fSSascha Hauer */ 1081ec1e82fSSascha Hauer #define DATA_ERROR 0x10000000 1091ec1e82fSSascha Hauer 1101ec1e82fSSascha Hauer /* 1111ec1e82fSSascha Hauer * Buffer descriptor commands. 1121ec1e82fSSascha Hauer */ 1131ec1e82fSSascha Hauer #define C0_ADDR 0x01 1141ec1e82fSSascha Hauer #define C0_LOAD 0x02 1151ec1e82fSSascha Hauer #define C0_DUMP 0x03 1161ec1e82fSSascha Hauer #define C0_SETCTX 0x07 1171ec1e82fSSascha Hauer #define C0_GETCTX 0x03 1181ec1e82fSSascha Hauer #define C0_SETDM 0x01 1191ec1e82fSSascha Hauer #define C0_SETPM 0x04 1201ec1e82fSSascha Hauer #define C0_GETDM 0x02 1211ec1e82fSSascha Hauer #define C0_GETPM 0x08 1221ec1e82fSSascha Hauer /* 1231ec1e82fSSascha Hauer * Change endianness indicator in the BD command field 1241ec1e82fSSascha Hauer */ 1251ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS 0x80 1261ec1e82fSSascha Hauer 1271ec1e82fSSascha Hauer /* 1288391ecf4SShengjiu Wang * p_2_p watermark_level description 1298391ecf4SShengjiu Wang * Bits Name Description 1308391ecf4SShengjiu Wang * 0-7 Lower WML Lower watermark level 1318391ecf4SShengjiu Wang * 8 PS 1: Pad Swallowing 1328391ecf4SShengjiu Wang * 0: No Pad Swallowing 1338391ecf4SShengjiu Wang * 9 PA 1: Pad Adding 1348391ecf4SShengjiu Wang * 0: No Pad Adding 1358391ecf4SShengjiu Wang * 10 SPDIF If this bit is set both source 1368391ecf4SShengjiu Wang * and destination are on SPBA 1378391ecf4SShengjiu Wang * 11 Source Bit(SP) 1: Source on SPBA 1388391ecf4SShengjiu Wang * 0: Source on AIPS 1398391ecf4SShengjiu Wang * 12 Destination Bit(DP) 1: Destination on SPBA 1408391ecf4SShengjiu Wang * 0: Destination on AIPS 1418391ecf4SShengjiu Wang * 13-15 --------- MUST BE 0 1428391ecf4SShengjiu Wang * 16-23 Higher WML HWML 1438391ecf4SShengjiu Wang * 24-27 N Total number of samples after 1448391ecf4SShengjiu Wang * which Pad adding/Swallowing 1458391ecf4SShengjiu Wang * must be done. It must be odd. 1468391ecf4SShengjiu Wang * 28 Lower WML Event(LWE) SDMA events reg to check for 1478391ecf4SShengjiu Wang * LWML event mask 1488391ecf4SShengjiu Wang * 0: LWE in EVENTS register 1498391ecf4SShengjiu Wang * 1: LWE in EVENTS2 register 1508391ecf4SShengjiu Wang * 29 Higher WML Event(HWE) SDMA events reg to check for 1518391ecf4SShengjiu Wang * HWML event mask 1528391ecf4SShengjiu Wang * 0: HWE in EVENTS register 1538391ecf4SShengjiu Wang * 1: HWE in EVENTS2 register 1548391ecf4SShengjiu Wang * 30 --------- MUST BE 0 1558391ecf4SShengjiu Wang * 31 CONT 1: Amount of samples to be 1568391ecf4SShengjiu Wang * transferred is unknown and 1578391ecf4SShengjiu Wang * script will keep on 1588391ecf4SShengjiu Wang * transferring samples as long as 1598391ecf4SShengjiu Wang * both events are detected and 1608391ecf4SShengjiu Wang * script must be manually stopped 1618391ecf4SShengjiu Wang * by the application 1628391ecf4SShengjiu Wang * 0: The amount of samples to be 1638391ecf4SShengjiu Wang * transferred is equal to the 1648391ecf4SShengjiu Wang * count field of mode word 1658391ecf4SShengjiu Wang */ 1668391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML 0xFF 1678391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS BIT(8) 1688391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA BIT(9) 1698391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10) 1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP BIT(11) 1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP BIT(12) 1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16) 1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE BIT(28) 1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE BIT(29) 1758391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT BIT(31) 1768391ecf4SShengjiu Wang 177f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ 178f9d4a398SNicolin Chen BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ 179f9d4a398SNicolin Chen BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)) 180f9d4a398SNicolin Chen 181f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \ 182f9d4a398SNicolin Chen BIT(DMA_MEM_TO_DEV) | \ 183f9d4a398SNicolin Chen BIT(DMA_DEV_TO_DEV)) 184f9d4a398SNicolin Chen 185824a0a02SSascha Hauer #define SDMA_WATERMARK_LEVEL_N_FIFOS GENMASK(15, 12) 186824a0a02SSascha Hauer #define SDMA_WATERMARK_LEVEL_SW_DONE BIT(23) 187824a0a02SSascha Hauer 188824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG_DONE_SEL BIT(7) 189824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG_DONE_DIS BIT(6) 190824a0a02SSascha Hauer 191*01eafd4bSShengjiu Wang /* 1928d11cfb0SVladimir Zapolskiy * struct sdma_script_start_addrs - SDMA script start pointers 1938d11cfb0SVladimir Zapolskiy * 1948d11cfb0SVladimir Zapolskiy * start addresses of the different functions in the physical 1958d11cfb0SVladimir Zapolskiy * address space of the SDMA engine. 1968d11cfb0SVladimir Zapolskiy */ 1978d11cfb0SVladimir Zapolskiy struct sdma_script_start_addrs { 1988d11cfb0SVladimir Zapolskiy s32 ap_2_ap_addr; 1998d11cfb0SVladimir Zapolskiy s32 ap_2_bp_addr; 2008d11cfb0SVladimir Zapolskiy s32 ap_2_ap_fixed_addr; 2018d11cfb0SVladimir Zapolskiy s32 bp_2_ap_addr; 2028d11cfb0SVladimir Zapolskiy s32 loopback_on_dsp_side_addr; 2038d11cfb0SVladimir Zapolskiy s32 mcu_interrupt_only_addr; 2048d11cfb0SVladimir Zapolskiy s32 firi_2_per_addr; 2058d11cfb0SVladimir Zapolskiy s32 firi_2_mcu_addr; 2068d11cfb0SVladimir Zapolskiy s32 per_2_firi_addr; 2078d11cfb0SVladimir Zapolskiy s32 mcu_2_firi_addr; 2088d11cfb0SVladimir Zapolskiy s32 uart_2_per_addr; 209a3ae97f4SKevin Groeneveld s32 uart_2_mcu_addr; 2108d11cfb0SVladimir Zapolskiy s32 per_2_app_addr; 2118d11cfb0SVladimir Zapolskiy s32 mcu_2_app_addr; 2128d11cfb0SVladimir Zapolskiy s32 per_2_per_addr; 2138d11cfb0SVladimir Zapolskiy s32 uartsh_2_per_addr; 214a3ae97f4SKevin Groeneveld s32 uartsh_2_mcu_addr; 2158d11cfb0SVladimir Zapolskiy s32 per_2_shp_addr; 2168d11cfb0SVladimir Zapolskiy s32 mcu_2_shp_addr; 2178d11cfb0SVladimir Zapolskiy s32 ata_2_mcu_addr; 2188d11cfb0SVladimir Zapolskiy s32 mcu_2_ata_addr; 2198d11cfb0SVladimir Zapolskiy s32 app_2_per_addr; 2208d11cfb0SVladimir Zapolskiy s32 app_2_mcu_addr; 2218d11cfb0SVladimir Zapolskiy s32 shp_2_per_addr; 2228d11cfb0SVladimir Zapolskiy s32 shp_2_mcu_addr; 2238d11cfb0SVladimir Zapolskiy s32 mshc_2_mcu_addr; 2248d11cfb0SVladimir Zapolskiy s32 mcu_2_mshc_addr; 2258d11cfb0SVladimir Zapolskiy s32 spdif_2_mcu_addr; 2268d11cfb0SVladimir Zapolskiy s32 mcu_2_spdif_addr; 2278d11cfb0SVladimir Zapolskiy s32 asrc_2_mcu_addr; 2288d11cfb0SVladimir Zapolskiy s32 ext_mem_2_ipu_addr; 2298d11cfb0SVladimir Zapolskiy s32 descrambler_addr; 2308d11cfb0SVladimir Zapolskiy s32 dptc_dvfs_addr; 2318d11cfb0SVladimir Zapolskiy s32 utra_addr; 2328d11cfb0SVladimir Zapolskiy s32 ram_code_start_addr; 2338d11cfb0SVladimir Zapolskiy /* End of v1 array */ 2348d11cfb0SVladimir Zapolskiy s32 mcu_2_ssish_addr; 2358d11cfb0SVladimir Zapolskiy s32 ssish_2_mcu_addr; 2368d11cfb0SVladimir Zapolskiy s32 hdmi_dma_addr; 2378d11cfb0SVladimir Zapolskiy /* End of v2 array */ 2388d11cfb0SVladimir Zapolskiy s32 zcanfd_2_mcu_addr; 2398d11cfb0SVladimir Zapolskiy s32 zqspi_2_mcu_addr; 2408d11cfb0SVladimir Zapolskiy s32 mcu_2_ecspi_addr; 241b98ce2f4SRobin Gong s32 mcu_2_sai_addr; 242b98ce2f4SRobin Gong s32 sai_2_mcu_addr; 243a3ae97f4SKevin Groeneveld s32 uart_2_mcu_rom_addr; 244a3ae97f4SKevin Groeneveld s32 uartsh_2_mcu_rom_addr; 2458d11cfb0SVladimir Zapolskiy /* End of v3 array */ 2468d11cfb0SVladimir Zapolskiy s32 mcu_2_zqspi_addr; 2478d11cfb0SVladimir Zapolskiy /* End of v4 array */ 2488d11cfb0SVladimir Zapolskiy }; 2498d11cfb0SVladimir Zapolskiy 2508391ecf4SShengjiu Wang /* 2511ec1e82fSSascha Hauer * Mode/Count of data node descriptors - IPCv2 2521ec1e82fSSascha Hauer */ 2531ec1e82fSSascha Hauer struct sdma_mode_count { 2544a6b2e8aSRobin Gong #define SDMA_BD_MAX_CNT 0xffff 2551ec1e82fSSascha Hauer u32 count : 16; /* size of the buffer pointed by this BD */ 2561ec1e82fSSascha Hauer u32 status : 8; /* E,R,I,C,W,D status bits stored here */ 257e4b75760SMartin Kaiser u32 command : 8; /* command mostly used for channel 0 */ 2581ec1e82fSSascha Hauer }; 2591ec1e82fSSascha Hauer 2601ec1e82fSSascha Hauer /* 2611ec1e82fSSascha Hauer * Buffer descriptor 2621ec1e82fSSascha Hauer */ 2631ec1e82fSSascha Hauer struct sdma_buffer_descriptor { 2641ec1e82fSSascha Hauer struct sdma_mode_count mode; 2651ec1e82fSSascha Hauer u32 buffer_addr; /* address of the buffer described */ 2661ec1e82fSSascha Hauer u32 ext_buffer_addr; /* extended buffer address */ 2671ec1e82fSSascha Hauer } __attribute__ ((packed)); 2681ec1e82fSSascha Hauer 2691ec1e82fSSascha Hauer /** 2701ec1e82fSSascha Hauer * struct sdma_channel_control - Channel control Block 2711ec1e82fSSascha Hauer * 27224ca312dSRobin Gong * @current_bd_ptr: current buffer descriptor processed 27324ca312dSRobin Gong * @base_bd_ptr: first element of buffer descriptor array 27424ca312dSRobin Gong * @unused: padding. The SDMA engine expects an array of 128 byte 2751ec1e82fSSascha Hauer * control blocks 2761ec1e82fSSascha Hauer */ 2771ec1e82fSSascha Hauer struct sdma_channel_control { 2781ec1e82fSSascha Hauer u32 current_bd_ptr; 2791ec1e82fSSascha Hauer u32 base_bd_ptr; 2801ec1e82fSSascha Hauer u32 unused[2]; 2811ec1e82fSSascha Hauer } __attribute__ ((packed)); 2821ec1e82fSSascha Hauer 2831ec1e82fSSascha Hauer /** 2841ec1e82fSSascha Hauer * struct sdma_state_registers - SDMA context for a channel 2851ec1e82fSSascha Hauer * 2861ec1e82fSSascha Hauer * @pc: program counter 28724ca312dSRobin Gong * @unused1: unused 2881ec1e82fSSascha Hauer * @t: test bit: status of arithmetic & test instruction 2891ec1e82fSSascha Hauer * @rpc: return program counter 29024ca312dSRobin Gong * @unused0: unused 2911ec1e82fSSascha Hauer * @sf: source fault while loading data 2921ec1e82fSSascha Hauer * @spc: loop start program counter 29324ca312dSRobin Gong * @unused2: unused 2941ec1e82fSSascha Hauer * @df: destination fault while storing data 2951ec1e82fSSascha Hauer * @epc: loop end program counter 2961ec1e82fSSascha Hauer * @lm: loop mode 2971ec1e82fSSascha Hauer */ 2981ec1e82fSSascha Hauer struct sdma_state_registers { 2991ec1e82fSSascha Hauer u32 pc :14; 3001ec1e82fSSascha Hauer u32 unused1: 1; 3011ec1e82fSSascha Hauer u32 t : 1; 3021ec1e82fSSascha Hauer u32 rpc :14; 3031ec1e82fSSascha Hauer u32 unused0: 1; 3041ec1e82fSSascha Hauer u32 sf : 1; 3051ec1e82fSSascha Hauer u32 spc :14; 3061ec1e82fSSascha Hauer u32 unused2: 1; 3071ec1e82fSSascha Hauer u32 df : 1; 3081ec1e82fSSascha Hauer u32 epc :14; 3091ec1e82fSSascha Hauer u32 lm : 2; 3101ec1e82fSSascha Hauer } __attribute__ ((packed)); 3111ec1e82fSSascha Hauer 3121ec1e82fSSascha Hauer /** 3131ec1e82fSSascha Hauer * struct sdma_context_data - sdma context specific to a channel 3141ec1e82fSSascha Hauer * 3151ec1e82fSSascha Hauer * @channel_state: channel state bits 3161ec1e82fSSascha Hauer * @gReg: general registers 3171ec1e82fSSascha Hauer * @mda: burst dma destination address register 3181ec1e82fSSascha Hauer * @msa: burst dma source address register 3191ec1e82fSSascha Hauer * @ms: burst dma status register 3201ec1e82fSSascha Hauer * @md: burst dma data register 3211ec1e82fSSascha Hauer * @pda: peripheral dma destination address register 3221ec1e82fSSascha Hauer * @psa: peripheral dma source address register 3231ec1e82fSSascha Hauer * @ps: peripheral dma status register 3241ec1e82fSSascha Hauer * @pd: peripheral dma data register 3251ec1e82fSSascha Hauer * @ca: CRC polynomial register 3261ec1e82fSSascha Hauer * @cs: CRC accumulator register 3271ec1e82fSSascha Hauer * @dda: dedicated core destination address register 3281ec1e82fSSascha Hauer * @dsa: dedicated core source address register 3291ec1e82fSSascha Hauer * @ds: dedicated core status register 3301ec1e82fSSascha Hauer * @dd: dedicated core data register 33124ca312dSRobin Gong * @scratch0: 1st word of dedicated ram for context switch 33224ca312dSRobin Gong * @scratch1: 2nd word of dedicated ram for context switch 33324ca312dSRobin Gong * @scratch2: 3rd word of dedicated ram for context switch 33424ca312dSRobin Gong * @scratch3: 4th word of dedicated ram for context switch 33524ca312dSRobin Gong * @scratch4: 5th word of dedicated ram for context switch 33624ca312dSRobin Gong * @scratch5: 6th word of dedicated ram for context switch 33724ca312dSRobin Gong * @scratch6: 7th word of dedicated ram for context switch 33824ca312dSRobin Gong * @scratch7: 8th word of dedicated ram for context switch 3391ec1e82fSSascha Hauer */ 3401ec1e82fSSascha Hauer struct sdma_context_data { 3411ec1e82fSSascha Hauer struct sdma_state_registers channel_state; 3421ec1e82fSSascha Hauer u32 gReg[8]; 3431ec1e82fSSascha Hauer u32 mda; 3441ec1e82fSSascha Hauer u32 msa; 3451ec1e82fSSascha Hauer u32 ms; 3461ec1e82fSSascha Hauer u32 md; 3471ec1e82fSSascha Hauer u32 pda; 3481ec1e82fSSascha Hauer u32 psa; 3491ec1e82fSSascha Hauer u32 ps; 3501ec1e82fSSascha Hauer u32 pd; 3511ec1e82fSSascha Hauer u32 ca; 3521ec1e82fSSascha Hauer u32 cs; 3531ec1e82fSSascha Hauer u32 dda; 3541ec1e82fSSascha Hauer u32 dsa; 3551ec1e82fSSascha Hauer u32 ds; 3561ec1e82fSSascha Hauer u32 dd; 3571ec1e82fSSascha Hauer u32 scratch0; 3581ec1e82fSSascha Hauer u32 scratch1; 3591ec1e82fSSascha Hauer u32 scratch2; 3601ec1e82fSSascha Hauer u32 scratch3; 3611ec1e82fSSascha Hauer u32 scratch4; 3621ec1e82fSSascha Hauer u32 scratch5; 3631ec1e82fSSascha Hauer u32 scratch6; 3641ec1e82fSSascha Hauer u32 scratch7; 3651ec1e82fSSascha Hauer } __attribute__ ((packed)); 3661ec1e82fSSascha Hauer 3671ec1e82fSSascha Hauer 3681ec1e82fSSascha Hauer struct sdma_engine; 3691ec1e82fSSascha Hauer 3701ec1e82fSSascha Hauer /** 37176c33d27SSascha Hauer * struct sdma_desc - descriptor structor for one transfer 37224ca312dSRobin Gong * @vd: descriptor for virt dma 37324ca312dSRobin Gong * @num_bd: number of descriptors currently handling 37424ca312dSRobin Gong * @bd_phys: physical address of bd 37524ca312dSRobin Gong * @buf_tail: ID of the buffer that was processed 37624ca312dSRobin Gong * @buf_ptail: ID of the previous buffer that was processed 37724ca312dSRobin Gong * @period_len: period length, used in cyclic. 37824ca312dSRobin Gong * @chn_real_count: the real count updated from bd->mode.count 37924ca312dSRobin Gong * @chn_count: the transfer count set 38024ca312dSRobin Gong * @sdmac: sdma_channel pointer 38124ca312dSRobin Gong * @bd: pointer of allocate bd 38276c33d27SSascha Hauer */ 38376c33d27SSascha Hauer struct sdma_desc { 38457b772b8SRobin Gong struct virt_dma_desc vd; 38576c33d27SSascha Hauer unsigned int num_bd; 38676c33d27SSascha Hauer dma_addr_t bd_phys; 38776c33d27SSascha Hauer unsigned int buf_tail; 38876c33d27SSascha Hauer unsigned int buf_ptail; 38976c33d27SSascha Hauer unsigned int period_len; 39076c33d27SSascha Hauer unsigned int chn_real_count; 39176c33d27SSascha Hauer unsigned int chn_count; 39276c33d27SSascha Hauer struct sdma_channel *sdmac; 39376c33d27SSascha Hauer struct sdma_buffer_descriptor *bd; 39476c33d27SSascha Hauer }; 39576c33d27SSascha Hauer 39676c33d27SSascha Hauer /** 3971ec1e82fSSascha Hauer * struct sdma_channel - housekeeping for a SDMA channel 3981ec1e82fSSascha Hauer * 39924ca312dSRobin Gong * @vc: virt_dma base structure 40024ca312dSRobin Gong * @desc: sdma description including vd and other special member 40124ca312dSRobin Gong * @sdma: pointer to the SDMA engine for this channel 40224ca312dSRobin Gong * @channel: the channel number, matches dmaengine chan_id + 1 40324ca312dSRobin Gong * @direction: transfer type. Needed for setting SDMA script 404d0c4a149SLee Jones * @slave_config: Slave configuration 40524ca312dSRobin Gong * @peripheral_type: Peripheral type. Needed for setting SDMA script 40624ca312dSRobin Gong * @event_id0: aka dma request line 40724ca312dSRobin Gong * @event_id1: for channels that use 2 events 40824ca312dSRobin Gong * @word_size: peripheral access size 40924ca312dSRobin Gong * @pc_from_device: script address for those device_2_memory 41024ca312dSRobin Gong * @pc_to_device: script address for those memory_2_device 41124ca312dSRobin Gong * @device_to_device: script address for those device_2_device 4120f06c027SRobin Gong * @pc_to_pc: script address for those memory_2_memory 41324ca312dSRobin Gong * @flags: loop mode or not 41424ca312dSRobin Gong * @per_address: peripheral source or destination address in common case 41524ca312dSRobin Gong * destination address in p_2_p case 41624ca312dSRobin Gong * @per_address2: peripheral source address in p_2_p case 41724ca312dSRobin Gong * @event_mask: event mask used in p_2_p script 41824ca312dSRobin Gong * @watermark_level: value for gReg[7], some script will extend it from 41924ca312dSRobin Gong * basic watermark such as p_2_p 42024ca312dSRobin Gong * @shp_addr: value for gReg[6] 42124ca312dSRobin Gong * @per_addr: value for gReg[2] 42224ca312dSRobin Gong * @status: status of dma channel 423d0c4a149SLee Jones * @context_loaded: ensure context is only loaded once 42424ca312dSRobin Gong * @data: specific sdma interface structure 42524ca312dSRobin Gong * @bd_pool: dma_pool for bd 426d0c4a149SLee Jones * @terminate_worker: used to call back into terminate work function 427*01eafd4bSShengjiu Wang * @terminated: terminated list 428*01eafd4bSShengjiu Wang * @is_ram_script: flag for script in ram 429*01eafd4bSShengjiu Wang * @n_fifos_src: number of source device fifos 430*01eafd4bSShengjiu Wang * @n_fifos_dst: number of destination device fifos 431*01eafd4bSShengjiu Wang * @sw_done: software done flag 4321ec1e82fSSascha Hauer */ 4331ec1e82fSSascha Hauer struct sdma_channel { 43457b772b8SRobin Gong struct virt_dma_chan vc; 43576c33d27SSascha Hauer struct sdma_desc *desc; 4361ec1e82fSSascha Hauer struct sdma_engine *sdma; 4371ec1e82fSSascha Hauer unsigned int channel; 438db8196dfSVinod Koul enum dma_transfer_direction direction; 439107d0644SVinod Koul struct dma_slave_config slave_config; 4401ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type; 4411ec1e82fSSascha Hauer unsigned int event_id0; 4421ec1e82fSSascha Hauer unsigned int event_id1; 4431ec1e82fSSascha Hauer enum dma_slave_buswidth word_size; 4441ec1e82fSSascha Hauer unsigned int pc_from_device, pc_to_device; 4458391ecf4SShengjiu Wang unsigned int device_to_device; 4460f06c027SRobin Gong unsigned int pc_to_pc; 4471ec1e82fSSascha Hauer unsigned long flags; 4488391ecf4SShengjiu Wang dma_addr_t per_address, per_address2; 4490bbc1413SRichard Zhao unsigned long event_mask[2]; 4500bbc1413SRichard Zhao unsigned long watermark_level; 4511ec1e82fSSascha Hauer u32 shp_addr, per_addr; 4521ec1e82fSSascha Hauer enum dma_status status; 4530b351865SNicolin Chen struct imx_dma_data data; 454b8603d2aSLucas Stach struct work_struct terminate_worker; 4554e2b10beSRobin Gong struct list_head terminated; 456e8fafa50SRobin Gong bool is_ram_script; 457824a0a02SSascha Hauer unsigned int n_fifos_src; 458824a0a02SSascha Hauer unsigned int n_fifos_dst; 459824a0a02SSascha Hauer bool sw_done; 4601ec1e82fSSascha Hauer }; 4611ec1e82fSSascha Hauer 4620bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP BIT(0) 4631ec1e82fSSascha Hauer 4641ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32 4651ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1 4661ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1 4671ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7 4681ec1e82fSSascha Hauer 4691ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453 4701ec1e82fSSascha Hauer 4711ec1e82fSSascha Hauer /** 4721ec1e82fSSascha Hauer * struct sdma_firmware_header - Layout of the firmware image 4731ec1e82fSSascha Hauer * 47424ca312dSRobin Gong * @magic: "SDMA" 47524ca312dSRobin Gong * @version_major: increased whenever layout of struct 47624ca312dSRobin Gong * sdma_script_start_addrs changes. 47724ca312dSRobin Gong * @version_minor: firmware minor version (for binary compatible changes) 47824ca312dSRobin Gong * @script_addrs_start: offset of struct sdma_script_start_addrs in this image 47924ca312dSRobin Gong * @num_script_addrs: Number of script addresses in this image 48024ca312dSRobin Gong * @ram_code_start: offset of SDMA ram image in this firmware image 48124ca312dSRobin Gong * @ram_code_size: size of SDMA ram image 48224ca312dSRobin Gong * @script_addrs: Stores the start address of the SDMA scripts 4831ec1e82fSSascha Hauer * (in SDMA memory space) 4841ec1e82fSSascha Hauer */ 4851ec1e82fSSascha Hauer struct sdma_firmware_header { 4861ec1e82fSSascha Hauer u32 magic; 4871ec1e82fSSascha Hauer u32 version_major; 4881ec1e82fSSascha Hauer u32 version_minor; 4891ec1e82fSSascha Hauer u32 script_addrs_start; 4901ec1e82fSSascha Hauer u32 num_script_addrs; 4911ec1e82fSSascha Hauer u32 ram_code_start; 4921ec1e82fSSascha Hauer u32 ram_code_size; 4931ec1e82fSSascha Hauer }; 4941ec1e82fSSascha Hauer 49517bba72fSSascha Hauer struct sdma_driver_data { 49617bba72fSSascha Hauer int chnenbl0; 49717bba72fSSascha Hauer int num_events; 498dcfec3c0SSascha Hauer struct sdma_script_start_addrs *script_addrs; 499941acd56SAngus Ainslie (Purism) bool check_ratio; 5004852e9a2SRobin Gong /* 5014852e9a2SRobin Gong * ecspi ERR009165 fixed should be done in sdma script 5024852e9a2SRobin Gong * and it has been fixed in soc from i.mx6ul. 5034852e9a2SRobin Gong * please get more information from the below link: 5044852e9a2SRobin Gong * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf 5054852e9a2SRobin Gong */ 5064852e9a2SRobin Gong bool ecspi_fixed; 50762550cd7SShawn Guo }; 50862550cd7SShawn Guo 5091ec1e82fSSascha Hauer struct sdma_engine { 5101ec1e82fSSascha Hauer struct device *dev; 5111ec1e82fSSascha Hauer struct sdma_channel channel[MAX_DMA_CHANNELS]; 5121ec1e82fSSascha Hauer struct sdma_channel_control *channel_control; 5131ec1e82fSSascha Hauer void __iomem *regs; 5141ec1e82fSSascha Hauer struct sdma_context_data *context; 5151ec1e82fSSascha Hauer dma_addr_t context_phys; 5161ec1e82fSSascha Hauer struct dma_device dma_device; 5177560e3f3SSascha Hauer struct clk *clk_ipg; 5187560e3f3SSascha Hauer struct clk *clk_ahb; 5192ccaef05SRichard Zhao spinlock_t channel_0_lock; 520cd72b846SNicolin Chen u32 script_number; 5211ec1e82fSSascha Hauer struct sdma_script_start_addrs *script_addrs; 52217bba72fSSascha Hauer const struct sdma_driver_data *drvdata; 5238391ecf4SShengjiu Wang u32 spba_start_addr; 5248391ecf4SShengjiu Wang u32 spba_end_addr; 5255bb9dbb5SVinod Koul unsigned int irq; 52676c33d27SSascha Hauer dma_addr_t bd0_phys; 52776c33d27SSascha Hauer struct sdma_buffer_descriptor *bd0; 52825aaa75dSAngus Ainslie (Purism) /* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/ 52925aaa75dSAngus Ainslie (Purism) bool clk_ratio; 530e8fafa50SRobin Gong bool fw_loaded; 53117bba72fSSascha Hauer }; 53217bba72fSSascha Hauer 533107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan, 534107d0644SVinod Koul struct dma_slave_config *dmaengine_cfg, 535107d0644SVinod Koul enum dma_transfer_direction direction); 536107d0644SVinod Koul 537e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = { 53817bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX31, 53917bba72fSSascha Hauer .num_events = 32, 54017bba72fSSascha Hauer }; 54117bba72fSSascha Hauer 542dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = { 543dcfec3c0SSascha Hauer .ap_2_ap_addr = 729, 544dcfec3c0SSascha Hauer .uart_2_mcu_addr = 904, 545dcfec3c0SSascha Hauer .per_2_app_addr = 1255, 546dcfec3c0SSascha Hauer .mcu_2_app_addr = 834, 547dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1120, 548dcfec3c0SSascha Hauer .per_2_shp_addr = 1329, 549dcfec3c0SSascha Hauer .mcu_2_shp_addr = 1048, 550dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1560, 551dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1479, 552dcfec3c0SSascha Hauer .app_2_per_addr = 1189, 553dcfec3c0SSascha Hauer .app_2_mcu_addr = 770, 554dcfec3c0SSascha Hauer .shp_2_per_addr = 1407, 555dcfec3c0SSascha Hauer .shp_2_mcu_addr = 979, 556dcfec3c0SSascha Hauer }; 557dcfec3c0SSascha Hauer 558e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = { 559dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 560dcfec3c0SSascha Hauer .num_events = 48, 561dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx25, 562dcfec3c0SSascha Hauer }; 563dcfec3c0SSascha Hauer 564e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = { 56517bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 56617bba72fSSascha Hauer .num_events = 48, 5671ec1e82fSSascha Hauer }; 5681ec1e82fSSascha Hauer 569dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = { 570dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 571dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 572dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 573dcfec3c0SSascha Hauer .mcu_2_shp_addr = 961, 574dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1473, 575dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1392, 576dcfec3c0SSascha Hauer .app_2_per_addr = 1033, 577dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 578dcfec3c0SSascha Hauer .shp_2_per_addr = 1251, 579dcfec3c0SSascha Hauer .shp_2_mcu_addr = 892, 580dcfec3c0SSascha Hauer }; 581dcfec3c0SSascha Hauer 582e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = { 583dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 584dcfec3c0SSascha Hauer .num_events = 48, 585dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx51, 586dcfec3c0SSascha Hauer }; 587dcfec3c0SSascha Hauer 588dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = { 589dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 590dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 591dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 592dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 593dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 594dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 595dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 596dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 597dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 598dcfec3c0SSascha Hauer .firi_2_mcu_addr = 1193, 599dcfec3c0SSascha Hauer .mcu_2_firi_addr = 1290, 600dcfec3c0SSascha Hauer }; 601dcfec3c0SSascha Hauer 602e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = { 603dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 604dcfec3c0SSascha Hauer .num_events = 48, 605dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx53, 606dcfec3c0SSascha Hauer }; 607dcfec3c0SSascha Hauer 608dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = { 609dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 610dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 611dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 612dcfec3c0SSascha Hauer .per_2_per_addr = 6331, 613dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 614dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 615dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 616dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 617dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 618dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 619dcfec3c0SSascha Hauer }; 620dcfec3c0SSascha Hauer 621e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = { 622dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 623dcfec3c0SSascha Hauer .num_events = 48, 624dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx6q, 625dcfec3c0SSascha Hauer }; 626dcfec3c0SSascha Hauer 6274852e9a2SRobin Gong static struct sdma_driver_data sdma_imx6ul = { 6284852e9a2SRobin Gong .chnenbl0 = SDMA_CHNENBL0_IMX35, 6294852e9a2SRobin Gong .num_events = 48, 6304852e9a2SRobin Gong .script_addrs = &sdma_script_imx6q, 6314852e9a2SRobin Gong .ecspi_fixed = true, 6324852e9a2SRobin Gong }; 6334852e9a2SRobin Gong 634b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = { 635b7d2648aSFabio Estevam .ap_2_ap_addr = 644, 636b7d2648aSFabio Estevam .uart_2_mcu_addr = 819, 637b7d2648aSFabio Estevam .mcu_2_app_addr = 749, 638b7d2648aSFabio Estevam .uartsh_2_mcu_addr = 1034, 639b7d2648aSFabio Estevam .mcu_2_shp_addr = 962, 640b7d2648aSFabio Estevam .app_2_mcu_addr = 685, 641b7d2648aSFabio Estevam .shp_2_mcu_addr = 893, 642b7d2648aSFabio Estevam .spdif_2_mcu_addr = 1102, 643b7d2648aSFabio Estevam .mcu_2_spdif_addr = 1136, 644b7d2648aSFabio Estevam }; 645b7d2648aSFabio Estevam 646b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = { 647b7d2648aSFabio Estevam .chnenbl0 = SDMA_CHNENBL0_IMX35, 648b7d2648aSFabio Estevam .num_events = 48, 649b7d2648aSFabio Estevam .script_addrs = &sdma_script_imx7d, 650b7d2648aSFabio Estevam }; 651b7d2648aSFabio Estevam 652941acd56SAngus Ainslie (Purism) static struct sdma_driver_data sdma_imx8mq = { 653941acd56SAngus Ainslie (Purism) .chnenbl0 = SDMA_CHNENBL0_IMX35, 654941acd56SAngus Ainslie (Purism) .num_events = 48, 655941acd56SAngus Ainslie (Purism) .script_addrs = &sdma_script_imx7d, 656941acd56SAngus Ainslie (Purism) .check_ratio = 1, 657941acd56SAngus Ainslie (Purism) }; 658941acd56SAngus Ainslie (Purism) 659580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = { 660dcfec3c0SSascha Hauer { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, }, 661dcfec3c0SSascha Hauer { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, }, 662dcfec3c0SSascha Hauer { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, }, 66317bba72fSSascha Hauer { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, }, 664dcfec3c0SSascha Hauer { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, }, 66563edea16SMarkus Pargmann { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, }, 666b7d2648aSFabio Estevam { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, }, 6674852e9a2SRobin Gong { .compatible = "fsl,imx6ul-sdma", .data = &sdma_imx6ul, }, 668941acd56SAngus Ainslie (Purism) { .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, }, 669580975d7SShawn Guo { /* sentinel */ } 670580975d7SShawn Guo }; 671580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids); 672580975d7SShawn Guo 6730bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */ 6740bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */ 6750bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */ 6761ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 6771ec1e82fSSascha Hauer 6781ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 6791ec1e82fSSascha Hauer { 68017bba72fSSascha Hauer u32 chnenbl0 = sdma->drvdata->chnenbl0; 6811ec1e82fSSascha Hauer return chnenbl0 + event * 4; 6821ec1e82fSSascha Hauer } 6831ec1e82fSSascha Hauer 6841ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 6851ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 6861ec1e82fSSascha Hauer { 6871ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6881ec1e82fSSascha Hauer int channel = sdmac->channel; 6890bbc1413SRichard Zhao unsigned long evt, mcu, dsp; 6901ec1e82fSSascha Hauer 6911ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 6921ec1e82fSSascha Hauer return -EINVAL; 6931ec1e82fSSascha Hauer 694c4b56857SRichard Zhao evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); 695c4b56857SRichard Zhao mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); 696c4b56857SRichard Zhao dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); 6971ec1e82fSSascha Hauer 6981ec1e82fSSascha Hauer if (dsp_override) 6990bbc1413SRichard Zhao __clear_bit(channel, &dsp); 7001ec1e82fSSascha Hauer else 7010bbc1413SRichard Zhao __set_bit(channel, &dsp); 7021ec1e82fSSascha Hauer 7031ec1e82fSSascha Hauer if (event_override) 7040bbc1413SRichard Zhao __clear_bit(channel, &evt); 7051ec1e82fSSascha Hauer else 7060bbc1413SRichard Zhao __set_bit(channel, &evt); 7071ec1e82fSSascha Hauer 7081ec1e82fSSascha Hauer if (mcu_override) 7090bbc1413SRichard Zhao __clear_bit(channel, &mcu); 7101ec1e82fSSascha Hauer else 7110bbc1413SRichard Zhao __set_bit(channel, &mcu); 7121ec1e82fSSascha Hauer 713c4b56857SRichard Zhao writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); 714c4b56857SRichard Zhao writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); 715c4b56857SRichard Zhao writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); 7161ec1e82fSSascha Hauer 7171ec1e82fSSascha Hauer return 0; 7181ec1e82fSSascha Hauer } 7191ec1e82fSSascha Hauer 7205b215c28STomasz Moń static int is_sdma_channel_enabled(struct sdma_engine *sdma, int channel) 7215b215c28STomasz Moń { 7225b215c28STomasz Moń return !!(readl(sdma->regs + SDMA_H_STATSTOP) & BIT(channel)); 7235b215c28STomasz Moń } 7245b215c28STomasz Moń 725b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 726b9a59166SRichard Zhao { 7270bbc1413SRichard Zhao writel(BIT(channel), sdma->regs + SDMA_H_START); 728b9a59166SRichard Zhao } 729b9a59166SRichard Zhao 7301ec1e82fSSascha Hauer /* 7312ccaef05SRichard Zhao * sdma_run_channel0 - run a channel and wait till it's done 7321ec1e82fSSascha Hauer */ 7332ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma) 7341ec1e82fSSascha Hauer { 7351ec1e82fSSascha Hauer int ret; 7361d069bfaSMichael Olbrich u32 reg; 7371ec1e82fSSascha Hauer 7382ccaef05SRichard Zhao sdma_enable_channel(sdma, 0); 7391ec1e82fSSascha Hauer 7401d069bfaSMichael Olbrich ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP, 7411d069bfaSMichael Olbrich reg, !(reg & 1), 1, 500); 7421d069bfaSMichael Olbrich if (ret) 7432ccaef05SRichard Zhao dev_err(sdma->dev, "Timeout waiting for CH0 ready\n"); 7441ec1e82fSSascha Hauer 745855832e4SRobin Gong /* Set bits of CONFIG register with dynamic context switching */ 74625aaa75dSAngus Ainslie (Purism) reg = readl(sdma->regs + SDMA_H_CONFIG); 74725aaa75dSAngus Ainslie (Purism) if ((reg & SDMA_H_CONFIG_CSM) == 0) { 74825aaa75dSAngus Ainslie (Purism) reg |= SDMA_H_CONFIG_CSM; 74925aaa75dSAngus Ainslie (Purism) writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG); 75025aaa75dSAngus Ainslie (Purism) } 751855832e4SRobin Gong 7521d069bfaSMichael Olbrich return ret; 7531ec1e82fSSascha Hauer } 7541ec1e82fSSascha Hauer 7551ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 7561ec1e82fSSascha Hauer u32 address) 7571ec1e82fSSascha Hauer { 75876c33d27SSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->bd0; 7591ec1e82fSSascha Hauer void *buf_virt; 7601ec1e82fSSascha Hauer dma_addr_t buf_phys; 7611ec1e82fSSascha Hauer int ret; 7622ccaef05SRichard Zhao unsigned long flags; 76373eab978SSascha Hauer 764ceaf5226SAndy Duan buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL); 765ef6c1dadSFlavio Suligoi if (!buf_virt) 7662ccaef05SRichard Zhao return -ENOMEM; 7671ec1e82fSSascha Hauer 7682ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 7692ccaef05SRichard Zhao 7701ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 7713f93a4f2SRobin Gong bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD; 7721ec1e82fSSascha Hauer bd0->mode.count = size / 2; 7731ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 7741ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 7751ec1e82fSSascha Hauer 7761ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 7771ec1e82fSSascha Hauer 7782ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 7792ccaef05SRichard Zhao 7802ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 7811ec1e82fSSascha Hauer 782ceaf5226SAndy Duan dma_free_coherent(sdma->dev, size, buf_virt, buf_phys); 7831ec1e82fSSascha Hauer 7841ec1e82fSSascha Hauer return ret; 7851ec1e82fSSascha Hauer } 7861ec1e82fSSascha Hauer 7871ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 7881ec1e82fSSascha Hauer { 7891ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7901ec1e82fSSascha Hauer int channel = sdmac->channel; 7910bbc1413SRichard Zhao unsigned long val; 7921ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 7931ec1e82fSSascha Hauer 794c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 7950bbc1413SRichard Zhao __set_bit(channel, &val); 796c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 797824a0a02SSascha Hauer 798824a0a02SSascha Hauer /* Set SDMA_DONEx_CONFIG is sw_done enabled */ 799824a0a02SSascha Hauer if (sdmac->sw_done) { 800824a0a02SSascha Hauer val = readl_relaxed(sdma->regs + SDMA_DONE0_CONFIG); 801824a0a02SSascha Hauer val |= SDMA_DONE0_CONFIG_DONE_SEL; 802824a0a02SSascha Hauer val &= ~SDMA_DONE0_CONFIG_DONE_DIS; 803824a0a02SSascha Hauer writel_relaxed(val, sdma->regs + SDMA_DONE0_CONFIG); 804824a0a02SSascha Hauer } 8051ec1e82fSSascha Hauer } 8061ec1e82fSSascha Hauer 8071ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 8081ec1e82fSSascha Hauer { 8091ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8101ec1e82fSSascha Hauer int channel = sdmac->channel; 8111ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 8120bbc1413SRichard Zhao unsigned long val; 8131ec1e82fSSascha Hauer 814c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 8150bbc1413SRichard Zhao __clear_bit(channel, &val); 816c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 8171ec1e82fSSascha Hauer } 8181ec1e82fSSascha Hauer 81957b772b8SRobin Gong static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t) 82057b772b8SRobin Gong { 82157b772b8SRobin Gong return container_of(t, struct sdma_desc, vd.tx); 82257b772b8SRobin Gong } 82357b772b8SRobin Gong 82457b772b8SRobin Gong static void sdma_start_desc(struct sdma_channel *sdmac) 82557b772b8SRobin Gong { 82657b772b8SRobin Gong struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc); 82757b772b8SRobin Gong struct sdma_desc *desc; 82857b772b8SRobin Gong struct sdma_engine *sdma = sdmac->sdma; 82957b772b8SRobin Gong int channel = sdmac->channel; 83057b772b8SRobin Gong 83157b772b8SRobin Gong if (!vd) { 83257b772b8SRobin Gong sdmac->desc = NULL; 83357b772b8SRobin Gong return; 83457b772b8SRobin Gong } 83557b772b8SRobin Gong sdmac->desc = desc = to_sdma_desc(&vd->tx); 83602939cd1SSascha Hauer 83757b772b8SRobin Gong list_del(&vd->node); 83857b772b8SRobin Gong 83957b772b8SRobin Gong sdma->channel_control[channel].base_bd_ptr = desc->bd_phys; 84057b772b8SRobin Gong sdma->channel_control[channel].current_bd_ptr = desc->bd_phys; 84157b772b8SRobin Gong sdma_enable_channel(sdma, sdmac->channel); 84257b772b8SRobin Gong } 84357b772b8SRobin Gong 844d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac) 845d1a792f3SRussell King - ARM Linux { 8461ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 8475881826dSNandor Han int error = 0; 8485881826dSNandor Han enum dma_status old_status = sdmac->status; 8491ec1e82fSSascha Hauer 8501ec1e82fSSascha Hauer /* 8511ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 8521ec1e82fSSascha Hauer * call callback function. 8531ec1e82fSSascha Hauer */ 85457b772b8SRobin Gong while (sdmac->desc) { 85576c33d27SSascha Hauer struct sdma_desc *desc = sdmac->desc; 85676c33d27SSascha Hauer 85776c33d27SSascha Hauer bd = &desc->bd[desc->buf_tail]; 8581ec1e82fSSascha Hauer 8591ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 8601ec1e82fSSascha Hauer break; 8611ec1e82fSSascha Hauer 8625881826dSNandor Han if (bd->mode.status & BD_RROR) { 8635881826dSNandor Han bd->mode.status &= ~BD_RROR; 8641ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 8655881826dSNandor Han error = -EIO; 8665881826dSNandor Han } 8671ec1e82fSSascha Hauer 8685881826dSNandor Han /* 8695881826dSNandor Han * We use bd->mode.count to calculate the residue, since contains 8705881826dSNandor Han * the number of bytes present in the current buffer descriptor. 8715881826dSNandor Han */ 8725881826dSNandor Han 87376c33d27SSascha Hauer desc->chn_real_count = bd->mode.count; 87476c33d27SSascha Hauer bd->mode.count = desc->period_len; 87576c33d27SSascha Hauer desc->buf_ptail = desc->buf_tail; 87676c33d27SSascha Hauer desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd; 87715f30f51SNandor Han 87815f30f51SNandor Han /* 87915f30f51SNandor Han * The callback is called from the interrupt context in order 88015f30f51SNandor Han * to reduce latency and to avoid the risk of altering the 88115f30f51SNandor Han * SDMA transaction status by the time the client tasklet is 88215f30f51SNandor Han * executed. 88315f30f51SNandor Han */ 88457b772b8SRobin Gong spin_unlock(&sdmac->vc.lock); 88557b772b8SRobin Gong dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL); 88657b772b8SRobin Gong spin_lock(&sdmac->vc.lock); 88715f30f51SNandor Han 888177360e0STomasz Moń /* Assign buffer ownership to SDMA */ 889177360e0STomasz Moń bd->mode.status |= BD_DONE; 890177360e0STomasz Moń 8915881826dSNandor Han if (error) 8925881826dSNandor Han sdmac->status = old_status; 8931ec1e82fSSascha Hauer } 8945b215c28STomasz Moń 8955b215c28STomasz Moń /* 8965b215c28STomasz Moń * SDMA stops cyclic channel when DMA request triggers a channel and no SDMA 8975b215c28STomasz Moń * owned buffer is available (i.e. BD_DONE was set too late). 8985b215c28STomasz Moń */ 8995b215c28STomasz Moń if (!is_sdma_channel_enabled(sdmac->sdma, sdmac->channel)) { 9005b215c28STomasz Moń dev_warn(sdmac->sdma->dev, "restart cyclic channel %d\n", sdmac->channel); 9015b215c28STomasz Moń sdma_enable_channel(sdmac->sdma, sdmac->channel); 9025b215c28STomasz Moń } 9031ec1e82fSSascha Hauer } 9041ec1e82fSSascha Hauer 90557b772b8SRobin Gong static void mxc_sdma_handle_channel_normal(struct sdma_channel *data) 9061ec1e82fSSascha Hauer { 90715f30f51SNandor Han struct sdma_channel *sdmac = (struct sdma_channel *) data; 9081ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 9091ec1e82fSSascha Hauer int i, error = 0; 9101ec1e82fSSascha Hauer 91176c33d27SSascha Hauer sdmac->desc->chn_real_count = 0; 9121ec1e82fSSascha Hauer /* 9131ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 9141ec1e82fSSascha Hauer * errors and call callback function 9151ec1e82fSSascha Hauer */ 91676c33d27SSascha Hauer for (i = 0; i < sdmac->desc->num_bd; i++) { 91776c33d27SSascha Hauer bd = &sdmac->desc->bd[i]; 9181ec1e82fSSascha Hauer 9191ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 9201ec1e82fSSascha Hauer error = -EIO; 92176c33d27SSascha Hauer sdmac->desc->chn_real_count += bd->mode.count; 9221ec1e82fSSascha Hauer } 9231ec1e82fSSascha Hauer 9241ec1e82fSSascha Hauer if (error) 9251ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 9261ec1e82fSSascha Hauer else 927409bff6aSVinod Koul sdmac->status = DMA_COMPLETE; 9281ec1e82fSSascha Hauer } 9291ec1e82fSSascha Hauer 9301ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 9311ec1e82fSSascha Hauer { 9321ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 9330bbc1413SRichard Zhao unsigned long stat; 9341ec1e82fSSascha Hauer 935c4b56857SRichard Zhao stat = readl_relaxed(sdma->regs + SDMA_H_INTR); 936c4b56857SRichard Zhao writel_relaxed(stat, sdma->regs + SDMA_H_INTR); 9371d069bfaSMichael Olbrich /* channel 0 is special and not handled here, see run_channel0() */ 9381d069bfaSMichael Olbrich stat &= ~1; 9391ec1e82fSSascha Hauer 9401ec1e82fSSascha Hauer while (stat) { 9411ec1e82fSSascha Hauer int channel = fls(stat) - 1; 9421ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 94357b772b8SRobin Gong struct sdma_desc *desc; 9441ec1e82fSSascha Hauer 94557b772b8SRobin Gong spin_lock(&sdmac->vc.lock); 94657b772b8SRobin Gong desc = sdmac->desc; 94757b772b8SRobin Gong if (desc) { 94857b772b8SRobin Gong if (sdmac->flags & IMX_DMA_SG_LOOP) { 949d1a792f3SRussell King - ARM Linux sdma_update_channel_loop(sdmac); 95057b772b8SRobin Gong } else { 95157b772b8SRobin Gong mxc_sdma_handle_channel_normal(sdmac); 95257b772b8SRobin Gong vchan_cookie_complete(&desc->vd); 95357b772b8SRobin Gong sdma_start_desc(sdmac); 95457b772b8SRobin Gong } 95557b772b8SRobin Gong } 9561ec1e82fSSascha Hauer 95757b772b8SRobin Gong spin_unlock(&sdmac->vc.lock); 9580bbc1413SRichard Zhao __clear_bit(channel, &stat); 9591ec1e82fSSascha Hauer } 9601ec1e82fSSascha Hauer 9611ec1e82fSSascha Hauer return IRQ_HANDLED; 9621ec1e82fSSascha Hauer } 9631ec1e82fSSascha Hauer 9641ec1e82fSSascha Hauer /* 9651ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 9661ec1e82fSSascha Hauer */ 967625d8936SSascha Hauer static int sdma_get_pc(struct sdma_channel *sdmac, 9681ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 9691ec1e82fSSascha Hauer { 9701ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9711ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 9721ec1e82fSSascha Hauer /* 9731ec1e82fSSascha Hauer * These are needed once we start to support transfers between 9741ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 9751ec1e82fSSascha Hauer */ 9760f06c027SRobin Gong int per_2_per = 0, emi_2_emi = 0; 9771ec1e82fSSascha Hauer 9781ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 9791ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 9808391ecf4SShengjiu Wang sdmac->device_to_device = 0; 9810f06c027SRobin Gong sdmac->pc_to_pc = 0; 982e8fafa50SRobin Gong sdmac->is_ram_script = false; 9831ec1e82fSSascha Hauer 9841ec1e82fSSascha Hauer switch (peripheral_type) { 9851ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 9860f06c027SRobin Gong emi_2_emi = sdma->script_addrs->ap_2_ap_addr; 9871ec1e82fSSascha Hauer break; 9881ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 9891ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 9901ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 9911ec1e82fSSascha Hauer break; 9921ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 9931ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 9941ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 9951ec1e82fSSascha Hauer break; 9961ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 9971ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 9981ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 9991ec1e82fSSascha Hauer break; 10001ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 10011ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 10021ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 10031ec1e82fSSascha Hauer break; 10041ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 10051ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 10061ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 10071ec1e82fSSascha Hauer break; 10081ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 1009a4965888SRobin Gong per_2_emi = sdma->script_addrs->app_2_mcu_addr; 10104852e9a2SRobin Gong 10114852e9a2SRobin Gong /* Use rom script mcu_2_app if ERR009165 fixed */ 10124852e9a2SRobin Gong if (sdmac->sdma->drvdata->ecspi_fixed) { 10134852e9a2SRobin Gong emi_2_per = sdma->script_addrs->mcu_2_app_addr; 10144852e9a2SRobin Gong } else { 1015a4965888SRobin Gong emi_2_per = sdma->script_addrs->mcu_2_ecspi_addr; 1016a4965888SRobin Gong sdmac->is_ram_script = true; 10174852e9a2SRobin Gong } 10184852e9a2SRobin Gong 1019a4965888SRobin Gong break; 10201ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 10211ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 102229aebfdeSNicolin Chen case IMX_DMATYPE_SAI: 10231ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 10241ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 10251ec1e82fSSascha Hauer break; 10261a895578SNicolin Chen case IMX_DMATYPE_SSI_DUAL: 10271a895578SNicolin Chen per_2_emi = sdma->script_addrs->ssish_2_mcu_addr; 10281a895578SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_ssish_addr; 1029e8fafa50SRobin Gong sdmac->is_ram_script = true; 10301a895578SNicolin Chen break; 10311ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 10321ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 10331ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 10341ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 10351ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 10361ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 10371ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 10381ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 10391ec1e82fSSascha Hauer break; 10401ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 10411ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 10421ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 10431ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 1044e8fafa50SRobin Gong sdmac->is_ram_script = true; 10451ec1e82fSSascha Hauer break; 1046f892afb0SNicolin Chen case IMX_DMATYPE_ASRC_SP: 1047f892afb0SNicolin Chen per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 1048f892afb0SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 1049f892afb0SNicolin Chen per_2_per = sdma->script_addrs->per_2_per_addr; 1050f892afb0SNicolin Chen break; 10511ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 10521ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 10531ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 10541ec1e82fSSascha Hauer break; 10551ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 10561ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 10571ec1e82fSSascha Hauer break; 10581ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 10591ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 10601ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 10611ec1e82fSSascha Hauer break; 10621ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 10631ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 10641ec1e82fSSascha Hauer break; 1065824a0a02SSascha Hauer case IMX_DMATYPE_MULTI_SAI: 1066824a0a02SSascha Hauer per_2_emi = sdma->script_addrs->sai_2_mcu_addr; 1067824a0a02SSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_sai_addr; 10681ec1e82fSSascha Hauer break; 10691ec1e82fSSascha Hauer default: 1070625d8936SSascha Hauer dev_err(sdma->dev, "Unsupported transfer type %d\n", 1071625d8936SSascha Hauer peripheral_type); 1072625d8936SSascha Hauer return -EINVAL; 10731ec1e82fSSascha Hauer } 10741ec1e82fSSascha Hauer 10751ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 10761ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 10778391ecf4SShengjiu Wang sdmac->device_to_device = per_2_per; 10780f06c027SRobin Gong sdmac->pc_to_pc = emi_2_emi; 1079625d8936SSascha Hauer 1080625d8936SSascha Hauer return 0; 10811ec1e82fSSascha Hauer } 10821ec1e82fSSascha Hauer 10831ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 10841ec1e82fSSascha Hauer { 10851ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10861ec1e82fSSascha Hauer int channel = sdmac->channel; 10871ec1e82fSSascha Hauer int load_address; 10881ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 108976c33d27SSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->bd0; 10901ec1e82fSSascha Hauer int ret; 10912ccaef05SRichard Zhao unsigned long flags; 10921ec1e82fSSascha Hauer 10938391ecf4SShengjiu Wang if (sdmac->direction == DMA_DEV_TO_MEM) 10941ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 10958391ecf4SShengjiu Wang else if (sdmac->direction == DMA_DEV_TO_DEV) 10968391ecf4SShengjiu Wang load_address = sdmac->device_to_device; 10970f06c027SRobin Gong else if (sdmac->direction == DMA_MEM_TO_MEM) 10980f06c027SRobin Gong load_address = sdmac->pc_to_pc; 10998391ecf4SShengjiu Wang else 11001ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 11011ec1e82fSSascha Hauer 11021ec1e82fSSascha Hauer if (load_address < 0) 11031ec1e82fSSascha Hauer return load_address; 11041ec1e82fSSascha Hauer 11051ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 11060bbc1413SRichard Zhao dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level); 11071ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 11081ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 11090bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]); 11100bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]); 11111ec1e82fSSascha Hauer 11122ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 111373eab978SSascha Hauer 11141ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 11151ec1e82fSSascha Hauer context->channel_state.pc = load_address; 11161ec1e82fSSascha Hauer 11171ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 11181ec1e82fSSascha Hauer * and watermark level 11191ec1e82fSSascha Hauer */ 11200bbc1413SRichard Zhao context->gReg[0] = sdmac->event_mask[1]; 11210bbc1413SRichard Zhao context->gReg[1] = sdmac->event_mask[0]; 11221ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 11231ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 11241ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 11251ec1e82fSSascha Hauer 11261ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 11273f93a4f2SRobin Gong bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD; 11281ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 11291ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 11301ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 11312ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 11321ec1e82fSSascha Hauer 11332ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 113473eab978SSascha Hauer 11351ec1e82fSSascha Hauer return ret; 11361ec1e82fSSascha Hauer } 11371ec1e82fSSascha Hauer 11387b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 11391ec1e82fSSascha Hauer { 114057b772b8SRobin Gong return container_of(chan, struct sdma_channel, vc.chan); 11417b350ab0SMaxime Ripard } 11427b350ab0SMaxime Ripard 11437b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan) 11447b350ab0SMaxime Ripard { 11457b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 11461ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11471ec1e82fSSascha Hauer int channel = sdmac->channel; 11481ec1e82fSSascha Hauer 11490bbc1413SRichard Zhao writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP); 11501ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 11517b350ab0SMaxime Ripard 11527b350ab0SMaxime Ripard return 0; 11531ec1e82fSSascha Hauer } 1154b8603d2aSLucas Stach static void sdma_channel_terminate_work(struct work_struct *work) 11557f3ff14bSJiada Wang { 1156b8603d2aSLucas Stach struct sdma_channel *sdmac = container_of(work, struct sdma_channel, 1157b8603d2aSLucas Stach terminate_worker); 11587f3ff14bSJiada Wang /* 11597f3ff14bSJiada Wang * According to NXP R&D team a delay of one BD SDMA cost time 11607f3ff14bSJiada Wang * (maximum is 1ms) should be added after disable of the channel 11617f3ff14bSJiada Wang * bit, to ensure SDMA core has really been stopped after SDMA 11627f3ff14bSJiada Wang * clients call .device_terminate_all. 11637f3ff14bSJiada Wang */ 1164b8603d2aSLucas Stach usleep_range(1000, 2000); 1165b8603d2aSLucas Stach 11664e2b10beSRobin Gong vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated); 1167b8603d2aSLucas Stach } 1168b8603d2aSLucas Stach 1169a80f2787SSascha Hauer static int sdma_terminate_all(struct dma_chan *chan) 1170b8603d2aSLucas Stach { 1171b8603d2aSLucas Stach struct sdma_channel *sdmac = to_sdma_chan(chan); 117202939cd1SSascha Hauer unsigned long flags; 117302939cd1SSascha Hauer 117402939cd1SSascha Hauer spin_lock_irqsave(&sdmac->vc.lock, flags); 1175b8603d2aSLucas Stach 1176b8603d2aSLucas Stach sdma_disable_channel(chan); 1177b8603d2aSLucas Stach 117802939cd1SSascha Hauer if (sdmac->desc) { 117902939cd1SSascha Hauer vchan_terminate_vdesc(&sdmac->desc->vd); 11804e2b10beSRobin Gong /* 11814e2b10beSRobin Gong * move out current descriptor into terminated list so that 11824e2b10beSRobin Gong * it could be free in sdma_channel_terminate_work alone 11834e2b10beSRobin Gong * later without potential involving next descriptor raised 11844e2b10beSRobin Gong * up before the last descriptor terminated. 11854e2b10beSRobin Gong */ 11864e2b10beSRobin Gong vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated); 118702939cd1SSascha Hauer sdmac->desc = NULL; 1188b8603d2aSLucas Stach schedule_work(&sdmac->terminate_worker); 118902939cd1SSascha Hauer } 119002939cd1SSascha Hauer 119102939cd1SSascha Hauer spin_unlock_irqrestore(&sdmac->vc.lock, flags); 11927f3ff14bSJiada Wang 11937f3ff14bSJiada Wang return 0; 11947f3ff14bSJiada Wang } 11957f3ff14bSJiada Wang 1196b8603d2aSLucas Stach static void sdma_channel_synchronize(struct dma_chan *chan) 1197b8603d2aSLucas Stach { 1198b8603d2aSLucas Stach struct sdma_channel *sdmac = to_sdma_chan(chan); 1199b8603d2aSLucas Stach 1200b8603d2aSLucas Stach vchan_synchronize(&sdmac->vc); 1201b8603d2aSLucas Stach 1202b8603d2aSLucas Stach flush_work(&sdmac->terminate_worker); 1203b8603d2aSLucas Stach } 1204b8603d2aSLucas Stach 12058391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac) 12068391ecf4SShengjiu Wang { 12078391ecf4SShengjiu Wang struct sdma_engine *sdma = sdmac->sdma; 12088391ecf4SShengjiu Wang 12098391ecf4SShengjiu Wang int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML; 12108391ecf4SShengjiu Wang int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16; 12118391ecf4SShengjiu Wang 12128391ecf4SShengjiu Wang set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]); 12138391ecf4SShengjiu Wang set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]); 12148391ecf4SShengjiu Wang 12158391ecf4SShengjiu Wang if (sdmac->event_id0 > 31) 12168391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE; 12178391ecf4SShengjiu Wang 12188391ecf4SShengjiu Wang if (sdmac->event_id1 > 31) 12198391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE; 12208391ecf4SShengjiu Wang 12218391ecf4SShengjiu Wang /* 12228391ecf4SShengjiu Wang * If LWML(src_maxburst) > HWML(dst_maxburst), we need 12238391ecf4SShengjiu Wang * swap LWML and HWML of INFO(A.3.2.5.1), also need swap 12248391ecf4SShengjiu Wang * r0(event_mask[1]) and r1(event_mask[0]). 12258391ecf4SShengjiu Wang */ 12268391ecf4SShengjiu Wang if (lwml > hwml) { 12278391ecf4SShengjiu Wang sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML | 12288391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML); 12298391ecf4SShengjiu Wang sdmac->watermark_level |= hwml; 12308391ecf4SShengjiu Wang sdmac->watermark_level |= lwml << 16; 12318391ecf4SShengjiu Wang swap(sdmac->event_mask[0], sdmac->event_mask[1]); 12328391ecf4SShengjiu Wang } 12338391ecf4SShengjiu Wang 12348391ecf4SShengjiu Wang if (sdmac->per_address2 >= sdma->spba_start_addr && 12358391ecf4SShengjiu Wang sdmac->per_address2 <= sdma->spba_end_addr) 12368391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP; 12378391ecf4SShengjiu Wang 12388391ecf4SShengjiu Wang if (sdmac->per_address >= sdma->spba_start_addr && 12398391ecf4SShengjiu Wang sdmac->per_address <= sdma->spba_end_addr) 12408391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP; 12418391ecf4SShengjiu Wang 12428391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT; 12438391ecf4SShengjiu Wang } 12448391ecf4SShengjiu Wang 1245824a0a02SSascha Hauer static void sdma_set_watermarklevel_for_sais(struct sdma_channel *sdmac) 1246824a0a02SSascha Hauer { 1247824a0a02SSascha Hauer unsigned int n_fifos; 1248824a0a02SSascha Hauer 1249824a0a02SSascha Hauer if (sdmac->sw_done) 1250824a0a02SSascha Hauer sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SW_DONE; 1251824a0a02SSascha Hauer 1252824a0a02SSascha Hauer if (sdmac->direction == DMA_DEV_TO_MEM) 1253824a0a02SSascha Hauer n_fifos = sdmac->n_fifos_src; 1254824a0a02SSascha Hauer else 1255824a0a02SSascha Hauer n_fifos = sdmac->n_fifos_dst; 1256824a0a02SSascha Hauer 1257824a0a02SSascha Hauer sdmac->watermark_level |= 1258824a0a02SSascha Hauer FIELD_PREP(SDMA_WATERMARK_LEVEL_N_FIFOS, n_fifos); 1259824a0a02SSascha Hauer } 1260824a0a02SSascha Hauer 12617b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan) 12621ec1e82fSSascha Hauer { 12637b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 1264625d8936SSascha Hauer int ret; 12651ec1e82fSSascha Hauer 12667b350ab0SMaxime Ripard sdma_disable_channel(chan); 12671ec1e82fSSascha Hauer 12680bbc1413SRichard Zhao sdmac->event_mask[0] = 0; 12690bbc1413SRichard Zhao sdmac->event_mask[1] = 0; 12701ec1e82fSSascha Hauer sdmac->shp_addr = 0; 12711ec1e82fSSascha Hauer sdmac->per_addr = 0; 12721ec1e82fSSascha Hauer 12731ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 12741ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 12751ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 12761ec1e82fSSascha Hauer break; 12771ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 12781ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 12791ec1e82fSSascha Hauer break; 12801ec1e82fSSascha Hauer default: 12811ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 12821ec1e82fSSascha Hauer break; 12831ec1e82fSSascha Hauer } 12841ec1e82fSSascha Hauer 1285625d8936SSascha Hauer ret = sdma_get_pc(sdmac, sdmac->peripheral_type); 1286625d8936SSascha Hauer if (ret) 1287625d8936SSascha Hauer return ret; 12881ec1e82fSSascha Hauer 12891ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 12901ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 12911ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 12921ec1e82fSSascha Hauer if (sdmac->event_id1) { 12938391ecf4SShengjiu Wang if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP || 12948391ecf4SShengjiu Wang sdmac->peripheral_type == IMX_DMATYPE_ASRC) 12958391ecf4SShengjiu Wang sdma_set_watermarklevel_for_p2p(sdmac); 12961f8595efSFlavio Suligoi } else { 1297824a0a02SSascha Hauer if (sdmac->peripheral_type == 1298824a0a02SSascha Hauer IMX_DMATYPE_MULTI_SAI) 1299824a0a02SSascha Hauer sdma_set_watermarklevel_for_sais(sdmac); 1300824a0a02SSascha Hauer 13010bbc1413SRichard Zhao __set_bit(sdmac->event_id0, sdmac->event_mask); 13021f8595efSFlavio Suligoi } 13038391ecf4SShengjiu Wang 13041ec1e82fSSascha Hauer /* Address */ 13051ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 13068391ecf4SShengjiu Wang sdmac->per_addr = sdmac->per_address2; 13071ec1e82fSSascha Hauer } else { 13081ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 13091ec1e82fSSascha Hauer } 13101ec1e82fSSascha Hauer 1311e555a03bSRobin Gong return 0; 13121ec1e82fSSascha Hauer } 13131ec1e82fSSascha Hauer 13141ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 13151ec1e82fSSascha Hauer unsigned int priority) 13161ec1e82fSSascha Hauer { 13171ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 13181ec1e82fSSascha Hauer int channel = sdmac->channel; 13191ec1e82fSSascha Hauer 13201ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 13211ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 13221ec1e82fSSascha Hauer return -EINVAL; 13231ec1e82fSSascha Hauer } 13241ec1e82fSSascha Hauer 1325c4b56857SRichard Zhao writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 13261ec1e82fSSascha Hauer 13271ec1e82fSSascha Hauer return 0; 13281ec1e82fSSascha Hauer } 13291ec1e82fSSascha Hauer 133057b772b8SRobin Gong static int sdma_request_channel0(struct sdma_engine *sdma) 13311ec1e82fSSascha Hauer { 13321ec1e82fSSascha Hauer int ret = -EBUSY; 13331ec1e82fSSascha Hauer 133431ef489aSLinus Torvalds sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys, 133557b772b8SRobin Gong GFP_NOWAIT); 133657b772b8SRobin Gong if (!sdma->bd0) { 13371ec1e82fSSascha Hauer ret = -ENOMEM; 13381ec1e82fSSascha Hauer goto out; 13391ec1e82fSSascha Hauer } 13401ec1e82fSSascha Hauer 134157b772b8SRobin Gong sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys; 134257b772b8SRobin Gong sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys; 13431ec1e82fSSascha Hauer 134457b772b8SRobin Gong sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY); 13451ec1e82fSSascha Hauer return 0; 13461ec1e82fSSascha Hauer out: 13471ec1e82fSSascha Hauer 13481ec1e82fSSascha Hauer return ret; 13491ec1e82fSSascha Hauer } 13501ec1e82fSSascha Hauer 135157b772b8SRobin Gong 135257b772b8SRobin Gong static int sdma_alloc_bd(struct sdma_desc *desc) 13531ec1e82fSSascha Hauer { 1354ebb853b1SLucas Stach u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor); 135557b772b8SRobin Gong int ret = 0; 13561ec1e82fSSascha Hauer 135731ef489aSLinus Torvalds desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size, 1358ceaf5226SAndy Duan &desc->bd_phys, GFP_NOWAIT); 135957b772b8SRobin Gong if (!desc->bd) { 136057b772b8SRobin Gong ret = -ENOMEM; 136157b772b8SRobin Gong goto out; 136257b772b8SRobin Gong } 136357b772b8SRobin Gong out: 136457b772b8SRobin Gong return ret; 136557b772b8SRobin Gong } 13661ec1e82fSSascha Hauer 136757b772b8SRobin Gong static void sdma_free_bd(struct sdma_desc *desc) 136857b772b8SRobin Gong { 1369ebb853b1SLucas Stach u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor); 1370ebb853b1SLucas Stach 1371ceaf5226SAndy Duan dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd, 1372ceaf5226SAndy Duan desc->bd_phys); 137357b772b8SRobin Gong } 13741ec1e82fSSascha Hauer 137557b772b8SRobin Gong static void sdma_desc_free(struct virt_dma_desc *vd) 137657b772b8SRobin Gong { 137757b772b8SRobin Gong struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd); 137857b772b8SRobin Gong 137957b772b8SRobin Gong sdma_free_bd(desc); 138057b772b8SRobin Gong kfree(desc); 13811ec1e82fSSascha Hauer } 13821ec1e82fSSascha Hauer 13831ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 13841ec1e82fSSascha Hauer { 13851ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13861ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 13870f06c027SRobin Gong struct imx_dma_data mem_data; 13881ec1e82fSSascha Hauer int prio, ret; 13891ec1e82fSSascha Hauer 13900f06c027SRobin Gong /* 13910f06c027SRobin Gong * MEMCPY may never setup chan->private by filter function such as 13920f06c027SRobin Gong * dmatest, thus create 'struct imx_dma_data mem_data' for this case. 13930f06c027SRobin Gong * Please note in any other slave case, you have to setup chan->private 13940f06c027SRobin Gong * with 'struct imx_dma_data' in your own filter function if you want to 13950f06c027SRobin Gong * request dma channel by dma_request_channel() rather than 13960f06c027SRobin Gong * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear 13970f06c027SRobin Gong * to warn you to correct your filter function. 13980f06c027SRobin Gong */ 13990f06c027SRobin Gong if (!data) { 14000f06c027SRobin Gong dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n"); 14010f06c027SRobin Gong mem_data.priority = 2; 14020f06c027SRobin Gong mem_data.peripheral_type = IMX_DMATYPE_MEMORY; 14030f06c027SRobin Gong mem_data.dma_request = 0; 14040f06c027SRobin Gong mem_data.dma_request2 = 0; 14050f06c027SRobin Gong data = &mem_data; 14060f06c027SRobin Gong 1407625d8936SSascha Hauer ret = sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY); 1408625d8936SSascha Hauer if (ret) 1409625d8936SSascha Hauer return ret; 14100f06c027SRobin Gong } 14111ec1e82fSSascha Hauer 14121ec1e82fSSascha Hauer switch (data->priority) { 14131ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 14141ec1e82fSSascha Hauer prio = 3; 14151ec1e82fSSascha Hauer break; 14161ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 14171ec1e82fSSascha Hauer prio = 2; 14181ec1e82fSSascha Hauer break; 14191ec1e82fSSascha Hauer case DMA_PRIO_LOW: 14201ec1e82fSSascha Hauer default: 14211ec1e82fSSascha Hauer prio = 1; 14221ec1e82fSSascha Hauer break; 14231ec1e82fSSascha Hauer } 14241ec1e82fSSascha Hauer 14251ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 14261ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 14278391ecf4SShengjiu Wang sdmac->event_id1 = data->dma_request2; 1428c2c744d3SRichard Zhao 1429b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ipg); 1430b93edcddSFabio Estevam if (ret) 1431b93edcddSFabio Estevam return ret; 1432b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ahb); 1433b93edcddSFabio Estevam if (ret) 1434b93edcddSFabio Estevam goto disable_clk_ipg; 1435c2c744d3SRichard Zhao 14363bb5e7caSRichard Zhao ret = sdma_set_channel_priority(sdmac, prio); 14371ec1e82fSSascha Hauer if (ret) 1438b93edcddSFabio Estevam goto disable_clk_ahb; 14391ec1e82fSSascha Hauer 14401ec1e82fSSascha Hauer return 0; 1441b93edcddSFabio Estevam 1442b93edcddSFabio Estevam disable_clk_ahb: 1443b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ahb); 1444b93edcddSFabio Estevam disable_clk_ipg: 1445b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ipg); 1446b93edcddSFabio Estevam return ret; 14471ec1e82fSSascha Hauer } 14481ec1e82fSSascha Hauer 14491ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 14501ec1e82fSSascha Hauer { 14511ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 14521ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 14531ec1e82fSSascha Hauer 1454a80f2787SSascha Hauer sdma_terminate_all(chan); 1455b8603d2aSLucas Stach 1456b8603d2aSLucas Stach sdma_channel_synchronize(chan); 14571ec1e82fSSascha Hauer 14581ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 14591ec1e82fSSascha Hauer if (sdmac->event_id1) 14601ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 14611ec1e82fSSascha Hauer 14621ec1e82fSSascha Hauer sdmac->event_id0 = 0; 14631ec1e82fSSascha Hauer sdmac->event_id1 = 0; 14641ec1e82fSSascha Hauer 14651ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 14661ec1e82fSSascha Hauer 14677560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 14687560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 14691ec1e82fSSascha Hauer } 14701ec1e82fSSascha Hauer 147121420841SRobin Gong static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac, 147221420841SRobin Gong enum dma_transfer_direction direction, u32 bds) 147321420841SRobin Gong { 147421420841SRobin Gong struct sdma_desc *desc; 147521420841SRobin Gong 1476e8fafa50SRobin Gong if (!sdmac->sdma->fw_loaded && sdmac->is_ram_script) { 1477e8fafa50SRobin Gong dev_warn_once(sdmac->sdma->dev, "sdma firmware not ready!\n"); 1478e8fafa50SRobin Gong goto err_out; 1479e8fafa50SRobin Gong } 1480e8fafa50SRobin Gong 148121420841SRobin Gong desc = kzalloc((sizeof(*desc)), GFP_NOWAIT); 148221420841SRobin Gong if (!desc) 148321420841SRobin Gong goto err_out; 148421420841SRobin Gong 148521420841SRobin Gong sdmac->status = DMA_IN_PROGRESS; 148621420841SRobin Gong sdmac->direction = direction; 148721420841SRobin Gong sdmac->flags = 0; 148821420841SRobin Gong 148921420841SRobin Gong desc->chn_count = 0; 149021420841SRobin Gong desc->chn_real_count = 0; 149121420841SRobin Gong desc->buf_tail = 0; 149221420841SRobin Gong desc->buf_ptail = 0; 149321420841SRobin Gong desc->sdmac = sdmac; 149421420841SRobin Gong desc->num_bd = bds; 149521420841SRobin Gong 149621420841SRobin Gong if (sdma_alloc_bd(desc)) 149721420841SRobin Gong goto err_desc_out; 149821420841SRobin Gong 14990f06c027SRobin Gong /* No slave_config called in MEMCPY case, so do here */ 15000f06c027SRobin Gong if (direction == DMA_MEM_TO_MEM) 15010f06c027SRobin Gong sdma_config_ownership(sdmac, false, true, false); 15020f06c027SRobin Gong 150321420841SRobin Gong if (sdma_load_context(sdmac)) 150421420841SRobin Gong goto err_desc_out; 150521420841SRobin Gong 150621420841SRobin Gong return desc; 150721420841SRobin Gong 150821420841SRobin Gong err_desc_out: 150921420841SRobin Gong kfree(desc); 151021420841SRobin Gong err_out: 151121420841SRobin Gong return NULL; 151221420841SRobin Gong } 151321420841SRobin Gong 15140f06c027SRobin Gong static struct dma_async_tx_descriptor *sdma_prep_memcpy( 15150f06c027SRobin Gong struct dma_chan *chan, dma_addr_t dma_dst, 15160f06c027SRobin Gong dma_addr_t dma_src, size_t len, unsigned long flags) 15170f06c027SRobin Gong { 15180f06c027SRobin Gong struct sdma_channel *sdmac = to_sdma_chan(chan); 15190f06c027SRobin Gong struct sdma_engine *sdma = sdmac->sdma; 15200f06c027SRobin Gong int channel = sdmac->channel; 15210f06c027SRobin Gong size_t count; 15220f06c027SRobin Gong int i = 0, param; 15230f06c027SRobin Gong struct sdma_buffer_descriptor *bd; 15240f06c027SRobin Gong struct sdma_desc *desc; 15250f06c027SRobin Gong 15260f06c027SRobin Gong if (!chan || !len) 15270f06c027SRobin Gong return NULL; 15280f06c027SRobin Gong 15290f06c027SRobin Gong dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n", 15300f06c027SRobin Gong &dma_src, &dma_dst, len, channel); 15310f06c027SRobin Gong 15320f06c027SRobin Gong desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM, 15330f06c027SRobin Gong len / SDMA_BD_MAX_CNT + 1); 15340f06c027SRobin Gong if (!desc) 15350f06c027SRobin Gong return NULL; 15360f06c027SRobin Gong 15370f06c027SRobin Gong do { 15380f06c027SRobin Gong count = min_t(size_t, len, SDMA_BD_MAX_CNT); 15390f06c027SRobin Gong bd = &desc->bd[i]; 15400f06c027SRobin Gong bd->buffer_addr = dma_src; 15410f06c027SRobin Gong bd->ext_buffer_addr = dma_dst; 15420f06c027SRobin Gong bd->mode.count = count; 15430f06c027SRobin Gong desc->chn_count += count; 15440f06c027SRobin Gong bd->mode.command = 0; 15450f06c027SRobin Gong 15460f06c027SRobin Gong dma_src += count; 15470f06c027SRobin Gong dma_dst += count; 15480f06c027SRobin Gong len -= count; 15490f06c027SRobin Gong i++; 15500f06c027SRobin Gong 15510f06c027SRobin Gong param = BD_DONE | BD_EXTD | BD_CONT; 15520f06c027SRobin Gong /* last bd */ 15530f06c027SRobin Gong if (!len) { 15540f06c027SRobin Gong param |= BD_INTR; 15550f06c027SRobin Gong param |= BD_LAST; 15560f06c027SRobin Gong param &= ~BD_CONT; 15570f06c027SRobin Gong } 15580f06c027SRobin Gong 15590f06c027SRobin Gong dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n", 15600f06c027SRobin Gong i, count, bd->buffer_addr, 15610f06c027SRobin Gong param & BD_WRAP ? "wrap" : "", 15620f06c027SRobin Gong param & BD_INTR ? " intr" : ""); 15630f06c027SRobin Gong 15640f06c027SRobin Gong bd->mode.status = param; 15650f06c027SRobin Gong } while (len); 15660f06c027SRobin Gong 15670f06c027SRobin Gong return vchan_tx_prep(&sdmac->vc, &desc->vd, flags); 15680f06c027SRobin Gong } 15690f06c027SRobin Gong 15701ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 15711ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 1572db8196dfSVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 1573185ecb5fSAlexandre Bounine unsigned long flags, void *context) 15741ec1e82fSSascha Hauer { 15751ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 15761ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 1577ad78b000SVinod Koul int i, count; 157823889c63SSascha Hauer int channel = sdmac->channel; 15791ec1e82fSSascha Hauer struct scatterlist *sg; 158057b772b8SRobin Gong struct sdma_desc *desc; 15811ec1e82fSSascha Hauer 1582107d0644SVinod Koul sdma_config_write(chan, &sdmac->slave_config, direction); 1583107d0644SVinod Koul 158421420841SRobin Gong desc = sdma_transfer_init(sdmac, direction, sg_len); 158557b772b8SRobin Gong if (!desc) 158657b772b8SRobin Gong goto err_out; 158757b772b8SRobin Gong 15881ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 15891ec1e82fSSascha Hauer sg_len, channel); 15901ec1e82fSSascha Hauer 15911ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 159276c33d27SSascha Hauer struct sdma_buffer_descriptor *bd = &desc->bd[i]; 15931ec1e82fSSascha Hauer int param; 15941ec1e82fSSascha Hauer 1595d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 15961ec1e82fSSascha Hauer 1597fdaf9c4bSLars-Peter Clausen count = sg_dma_len(sg); 15981ec1e82fSSascha Hauer 15994a6b2e8aSRobin Gong if (count > SDMA_BD_MAX_CNT) { 16001ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 16014a6b2e8aSRobin Gong channel, count, SDMA_BD_MAX_CNT); 160257b772b8SRobin Gong goto err_bd_out; 16031ec1e82fSSascha Hauer } 16041ec1e82fSSascha Hauer 16051ec1e82fSSascha Hauer bd->mode.count = count; 160676c33d27SSascha Hauer desc->chn_count += count; 16071ec1e82fSSascha Hauer 1608ad78b000SVinod Koul if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 160957b772b8SRobin Gong goto err_bd_out; 16101fa81c27SSascha Hauer 16111fa81c27SSascha Hauer switch (sdmac->word_size) { 16121fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 16131ec1e82fSSascha Hauer bd->mode.command = 0; 16141fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 161557b772b8SRobin Gong goto err_bd_out; 16161fa81c27SSascha Hauer break; 16171fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 16181fa81c27SSascha Hauer bd->mode.command = 2; 16191fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 162057b772b8SRobin Gong goto err_bd_out; 16211fa81c27SSascha Hauer break; 16221fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 16231fa81c27SSascha Hauer bd->mode.command = 1; 16241fa81c27SSascha Hauer break; 16251fa81c27SSascha Hauer default: 162657b772b8SRobin Gong goto err_bd_out; 16271fa81c27SSascha Hauer } 16281ec1e82fSSascha Hauer 16291ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 16301ec1e82fSSascha Hauer 1631341b9419SShawn Guo if (i + 1 == sg_len) { 16321ec1e82fSSascha Hauer param |= BD_INTR; 1633341b9419SShawn Guo param |= BD_LAST; 1634341b9419SShawn Guo param &= ~BD_CONT; 16351ec1e82fSSascha Hauer } 16361ec1e82fSSascha Hauer 1637c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1638c3cc74b2SOlof Johansson i, count, (u64)sg->dma_address, 16391ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 16401ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 16411ec1e82fSSascha Hauer 16421ec1e82fSSascha Hauer bd->mode.status = param; 16431ec1e82fSSascha Hauer } 16441ec1e82fSSascha Hauer 164557b772b8SRobin Gong return vchan_tx_prep(&sdmac->vc, &desc->vd, flags); 164657b772b8SRobin Gong err_bd_out: 164757b772b8SRobin Gong sdma_free_bd(desc); 164857b772b8SRobin Gong kfree(desc); 16491ec1e82fSSascha Hauer err_out: 16504b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 16511ec1e82fSSascha Hauer return NULL; 16521ec1e82fSSascha Hauer } 16531ec1e82fSSascha Hauer 16541ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 16551ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 1656185ecb5fSAlexandre Bounine size_t period_len, enum dma_transfer_direction direction, 165731c1e5a1SLaurent Pinchart unsigned long flags) 16581ec1e82fSSascha Hauer { 16591ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 16601ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 16611ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 166223889c63SSascha Hauer int channel = sdmac->channel; 166321420841SRobin Gong int i = 0, buf = 0; 166457b772b8SRobin Gong struct sdma_desc *desc; 16651ec1e82fSSascha Hauer 16661ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 16671ec1e82fSSascha Hauer 1668107d0644SVinod Koul sdma_config_write(chan, &sdmac->slave_config, direction); 1669107d0644SVinod Koul 167021420841SRobin Gong desc = sdma_transfer_init(sdmac, direction, num_periods); 167157b772b8SRobin Gong if (!desc) 167257b772b8SRobin Gong goto err_out; 167357b772b8SRobin Gong 167476c33d27SSascha Hauer desc->period_len = period_len; 16758e2e27c7SRichard Zhao 16761ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 16771ec1e82fSSascha Hauer 16784a6b2e8aSRobin Gong if (period_len > SDMA_BD_MAX_CNT) { 1679ba6ab3b3SArvind Yadav dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n", 16804a6b2e8aSRobin Gong channel, period_len, SDMA_BD_MAX_CNT); 168157b772b8SRobin Gong goto err_bd_out; 16821ec1e82fSSascha Hauer } 16831ec1e82fSSascha Hauer 16841ec1e82fSSascha Hauer while (buf < buf_len) { 168576c33d27SSascha Hauer struct sdma_buffer_descriptor *bd = &desc->bd[i]; 16861ec1e82fSSascha Hauer int param; 16871ec1e82fSSascha Hauer 16881ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 16891ec1e82fSSascha Hauer 16901ec1e82fSSascha Hauer bd->mode.count = period_len; 16911ec1e82fSSascha Hauer 16921ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 169357b772b8SRobin Gong goto err_bd_out; 16941ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 16951ec1e82fSSascha Hauer bd->mode.command = 0; 16961ec1e82fSSascha Hauer else 16971ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 16981ec1e82fSSascha Hauer 16991ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 17001ec1e82fSSascha Hauer if (i + 1 == num_periods) 17011ec1e82fSSascha Hauer param |= BD_WRAP; 17021ec1e82fSSascha Hauer 1703ba6ab3b3SArvind Yadav dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n", 1704c3cc74b2SOlof Johansson i, period_len, (u64)dma_addr, 17051ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 17061ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 17071ec1e82fSSascha Hauer 17081ec1e82fSSascha Hauer bd->mode.status = param; 17091ec1e82fSSascha Hauer 17101ec1e82fSSascha Hauer dma_addr += period_len; 17111ec1e82fSSascha Hauer buf += period_len; 17121ec1e82fSSascha Hauer 17131ec1e82fSSascha Hauer i++; 17141ec1e82fSSascha Hauer } 17151ec1e82fSSascha Hauer 171657b772b8SRobin Gong return vchan_tx_prep(&sdmac->vc, &desc->vd, flags); 171757b772b8SRobin Gong err_bd_out: 171857b772b8SRobin Gong sdma_free_bd(desc); 171957b772b8SRobin Gong kfree(desc); 17201ec1e82fSSascha Hauer err_out: 17211ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 17221ec1e82fSSascha Hauer return NULL; 17231ec1e82fSSascha Hauer } 17241ec1e82fSSascha Hauer 1725107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan, 1726107d0644SVinod Koul struct dma_slave_config *dmaengine_cfg, 1727107d0644SVinod Koul enum dma_transfer_direction direction) 17281ec1e82fSSascha Hauer { 17291ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 17301ec1e82fSSascha Hauer 1731107d0644SVinod Koul if (direction == DMA_DEV_TO_MEM) { 17321ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 173394ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->src_maxburst * 173494ac27a5SPhilippe Rétornaz dmaengine_cfg->src_addr_width; 17351ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 1736107d0644SVinod Koul } else if (direction == DMA_DEV_TO_DEV) { 17378391ecf4SShengjiu Wang sdmac->per_address2 = dmaengine_cfg->src_addr; 17388391ecf4SShengjiu Wang sdmac->per_address = dmaengine_cfg->dst_addr; 17398391ecf4SShengjiu Wang sdmac->watermark_level = dmaengine_cfg->src_maxburst & 17408391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_LWML; 17418391ecf4SShengjiu Wang sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) & 17428391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML; 17438391ecf4SShengjiu Wang sdmac->word_size = dmaengine_cfg->dst_addr_width; 17441ec1e82fSSascha Hauer } else { 17451ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 174694ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->dst_maxburst * 174794ac27a5SPhilippe Rétornaz dmaengine_cfg->dst_addr_width; 17481ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 17491ec1e82fSSascha Hauer } 1750107d0644SVinod Koul sdmac->direction = direction; 17517b350ab0SMaxime Ripard return sdma_config_channel(chan); 17521ec1e82fSSascha Hauer } 17531ec1e82fSSascha Hauer 1754107d0644SVinod Koul static int sdma_config(struct dma_chan *chan, 1755107d0644SVinod Koul struct dma_slave_config *dmaengine_cfg) 1756107d0644SVinod Koul { 1757107d0644SVinod Koul struct sdma_channel *sdmac = to_sdma_chan(chan); 1758824a0a02SSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 1759107d0644SVinod Koul 1760107d0644SVinod Koul memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg)); 1761107d0644SVinod Koul 1762824a0a02SSascha Hauer if (dmaengine_cfg->peripheral_config) { 1763824a0a02SSascha Hauer struct sdma_peripheral_config *sdmacfg = dmaengine_cfg->peripheral_config; 1764824a0a02SSascha Hauer if (dmaengine_cfg->peripheral_size != sizeof(struct sdma_peripheral_config)) { 1765824a0a02SSascha Hauer dev_err(sdma->dev, "Invalid peripheral size %zu, expected %zu\n", 1766824a0a02SSascha Hauer dmaengine_cfg->peripheral_size, 1767824a0a02SSascha Hauer sizeof(struct sdma_peripheral_config)); 1768824a0a02SSascha Hauer return -EINVAL; 1769824a0a02SSascha Hauer } 1770824a0a02SSascha Hauer sdmac->n_fifos_src = sdmacfg->n_fifos_src; 1771824a0a02SSascha Hauer sdmac->n_fifos_dst = sdmacfg->n_fifos_dst; 1772824a0a02SSascha Hauer sdmac->sw_done = sdmacfg->sw_done; 1773824a0a02SSascha Hauer } 1774824a0a02SSascha Hauer 1775107d0644SVinod Koul /* Set ENBLn earlier to make sure dma request triggered after that */ 1776107d0644SVinod Koul if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events) 1777107d0644SVinod Koul return -EINVAL; 1778107d0644SVinod Koul sdma_event_enable(sdmac, sdmac->event_id0); 1779107d0644SVinod Koul 1780107d0644SVinod Koul if (sdmac->event_id1) { 1781107d0644SVinod Koul if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events) 1782107d0644SVinod Koul return -EINVAL; 1783107d0644SVinod Koul sdma_event_enable(sdmac, sdmac->event_id1); 1784107d0644SVinod Koul } 1785107d0644SVinod Koul 1786107d0644SVinod Koul return 0; 1787107d0644SVinod Koul } 1788107d0644SVinod Koul 17891ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 17901ec1e82fSSascha Hauer dma_cookie_t cookie, 17911ec1e82fSSascha Hauer struct dma_tx_state *txstate) 17921ec1e82fSSascha Hauer { 17931ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 1794a1ff6a07SSascha Hauer struct sdma_desc *desc = NULL; 1795d1a792f3SRussell King - ARM Linux u32 residue; 179657b772b8SRobin Gong struct virt_dma_desc *vd; 179757b772b8SRobin Gong enum dma_status ret; 179857b772b8SRobin Gong unsigned long flags; 1799d1a792f3SRussell King - ARM Linux 180057b772b8SRobin Gong ret = dma_cookie_status(chan, cookie, txstate); 180157b772b8SRobin Gong if (ret == DMA_COMPLETE || !txstate) 180257b772b8SRobin Gong return ret; 180357b772b8SRobin Gong 180457b772b8SRobin Gong spin_lock_irqsave(&sdmac->vc.lock, flags); 1805a1ff6a07SSascha Hauer 180657b772b8SRobin Gong vd = vchan_find_desc(&sdmac->vc, cookie); 1807a1ff6a07SSascha Hauer if (vd) 180857b772b8SRobin Gong desc = to_sdma_desc(&vd->tx); 1809a1ff6a07SSascha Hauer else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) 1810a1ff6a07SSascha Hauer desc = sdmac->desc; 1811a1ff6a07SSascha Hauer 1812a1ff6a07SSascha Hauer if (desc) { 1813d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 181476c33d27SSascha Hauer residue = (desc->num_bd - desc->buf_ptail) * 181576c33d27SSascha Hauer desc->period_len - desc->chn_real_count; 1816d1a792f3SRussell King - ARM Linux else 181776c33d27SSascha Hauer residue = desc->chn_count - desc->chn_real_count; 181857b772b8SRobin Gong } else { 181957b772b8SRobin Gong residue = 0; 182057b772b8SRobin Gong } 1821a1ff6a07SSascha Hauer 182257b772b8SRobin Gong spin_unlock_irqrestore(&sdmac->vc.lock, flags); 18231ec1e82fSSascha Hauer 1824e8e3a790SAndy Shevchenko dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, 1825d1a792f3SRussell King - ARM Linux residue); 18261ec1e82fSSascha Hauer 18278a965911SShawn Guo return sdmac->status; 18281ec1e82fSSascha Hauer } 18291ec1e82fSSascha Hauer 18301ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 18311ec1e82fSSascha Hauer { 18322b4f130eSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 183357b772b8SRobin Gong unsigned long flags; 18342b4f130eSSascha Hauer 183557b772b8SRobin Gong spin_lock_irqsave(&sdmac->vc.lock, flags); 183657b772b8SRobin Gong if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc) 183757b772b8SRobin Gong sdma_start_desc(sdmac); 183857b772b8SRobin Gong spin_unlock_irqrestore(&sdmac->vc.lock, flags); 18391ec1e82fSSascha Hauer } 18401ec1e82fSSascha Hauer 18415b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 1842cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38 1843b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 45 1844b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 46 18455b28aa31SSascha Hauer 18465b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 18475b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 18485b28aa31SSascha Hauer { 18495b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 18505b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 18515b28aa31SSascha Hauer int i; 18525b28aa31SSascha Hauer 185370dabaedSNicolin Chen /* use the default firmware in ROM if missing external firmware */ 185470dabaedSNicolin Chen if (!sdma->script_number) 185570dabaedSNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 185670dabaedSNicolin Chen 1857bd73dfabSRobin Gong if (sdma->script_number > sizeof(struct sdma_script_start_addrs) 1858bd73dfabSRobin Gong / sizeof(s32)) { 1859bd73dfabSRobin Gong dev_err(sdma->dev, 1860bd73dfabSRobin Gong "SDMA script number %d not match with firmware.\n", 1861bd73dfabSRobin Gong sdma->script_number); 1862bd73dfabSRobin Gong return; 1863bd73dfabSRobin Gong } 1864bd73dfabSRobin Gong 1865cd72b846SNicolin Chen for (i = 0; i < sdma->script_number; i++) 18665b28aa31SSascha Hauer if (addr_arr[i] > 0) 18675b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 1868b98ce2f4SRobin Gong 1869b98ce2f4SRobin Gong /* 1870a3ae97f4SKevin Groeneveld * For compatibility with NXP internal legacy kernel before 4.19 which 1871a3ae97f4SKevin Groeneveld * is based on uart ram script and mainline kernel based on uart rom 1872a3ae97f4SKevin Groeneveld * script, both uart ram/rom scripts are present in newer sdma 1873a3ae97f4SKevin Groeneveld * firmware. Use the rom versions if they are present (V3 or newer). 1874b98ce2f4SRobin Gong */ 1875a3ae97f4SKevin Groeneveld if (sdma->script_number >= SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3) { 1876a3ae97f4SKevin Groeneveld if (addr->uart_2_mcu_rom_addr) 1877a3ae97f4SKevin Groeneveld sdma->script_addrs->uart_2_mcu_addr = addr->uart_2_mcu_rom_addr; 1878a3ae97f4SKevin Groeneveld if (addr->uartsh_2_mcu_rom_addr) 1879a3ae97f4SKevin Groeneveld sdma->script_addrs->uartsh_2_mcu_addr = addr->uartsh_2_mcu_rom_addr; 1880a3ae97f4SKevin Groeneveld } 18815b28aa31SSascha Hauer } 18825b28aa31SSascha Hauer 18837b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context) 18845b28aa31SSascha Hauer { 18857b4b88e0SSascha Hauer struct sdma_engine *sdma = context; 18865b28aa31SSascha Hauer const struct sdma_firmware_header *header; 18875b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 18885b28aa31SSascha Hauer unsigned short *ram_code; 18895b28aa31SSascha Hauer 18907b4b88e0SSascha Hauer if (!fw) { 18910f927a11SSascha Hauer dev_info(sdma->dev, "external firmware not found, using ROM firmware\n"); 18920f927a11SSascha Hauer /* In this case we just use the ROM firmware. */ 18937b4b88e0SSascha Hauer return; 18947b4b88e0SSascha Hauer } 18955b28aa31SSascha Hauer 18965b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 18975b28aa31SSascha Hauer goto err_firmware; 18985b28aa31SSascha Hauer 18995b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 19005b28aa31SSascha Hauer 19015b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 19025b28aa31SSascha Hauer goto err_firmware; 19035b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 19045b28aa31SSascha Hauer goto err_firmware; 1905cd72b846SNicolin Chen switch (header->version_major) { 1906cd72b846SNicolin Chen case 1: 1907cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 1908cd72b846SNicolin Chen break; 1909cd72b846SNicolin Chen case 2: 1910cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2; 1911cd72b846SNicolin Chen break; 1912a572460bSFabio Estevam case 3: 1913a572460bSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3; 1914a572460bSFabio Estevam break; 1915b7d2648aSFabio Estevam case 4: 1916b7d2648aSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4; 1917b7d2648aSFabio Estevam break; 1918cd72b846SNicolin Chen default: 1919cd72b846SNicolin Chen dev_err(sdma->dev, "unknown firmware version\n"); 1920cd72b846SNicolin Chen goto err_firmware; 1921cd72b846SNicolin Chen } 19225b28aa31SSascha Hauer 19235b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 19245b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 19255b28aa31SSascha Hauer 19267560e3f3SSascha Hauer clk_enable(sdma->clk_ipg); 19277560e3f3SSascha Hauer clk_enable(sdma->clk_ahb); 19285b28aa31SSascha Hauer /* download the RAM image for SDMA */ 19295b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 19305b28aa31SSascha Hauer header->ram_code_size, 19316866fd3bSSascha Hauer addr->ram_code_start_addr); 19327560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 19337560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 19345b28aa31SSascha Hauer 19355b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 19365b28aa31SSascha Hauer 1937e8fafa50SRobin Gong sdma->fw_loaded = true; 1938e8fafa50SRobin Gong 19395b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 19405b28aa31SSascha Hauer header->version_major, 19415b28aa31SSascha Hauer header->version_minor); 19425b28aa31SSascha Hauer 19435b28aa31SSascha Hauer err_firmware: 19445b28aa31SSascha Hauer release_firmware(fw); 19457b4b88e0SSascha Hauer } 19467b4b88e0SSascha Hauer 1947d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3 1948d078cd1bSZidan Wang 194929f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma) 1950d078cd1bSZidan Wang { 1951d078cd1bSZidan Wang struct device_node *np = sdma->dev->of_node; 1952d078cd1bSZidan Wang struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0); 1953d078cd1bSZidan Wang struct property *event_remap; 1954d078cd1bSZidan Wang struct regmap *gpr; 1955d078cd1bSZidan Wang char propname[] = "fsl,sdma-event-remap"; 1956d078cd1bSZidan Wang u32 reg, val, shift, num_map, i; 1957d078cd1bSZidan Wang int ret = 0; 1958d078cd1bSZidan Wang 19597104b9cbSMiaoqian Lin if (IS_ERR(np) || !gpr_np) 1960d078cd1bSZidan Wang goto out; 1961d078cd1bSZidan Wang 1962d078cd1bSZidan Wang event_remap = of_find_property(np, propname, NULL); 1963d078cd1bSZidan Wang num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0; 1964d078cd1bSZidan Wang if (!num_map) { 1965ce078af7SFabio Estevam dev_dbg(sdma->dev, "no event needs to be remapped\n"); 1966d078cd1bSZidan Wang goto out; 1967d078cd1bSZidan Wang } else if (num_map % EVENT_REMAP_CELLS) { 1968d078cd1bSZidan Wang dev_err(sdma->dev, "the property %s must modulo %d\n", 1969d078cd1bSZidan Wang propname, EVENT_REMAP_CELLS); 1970d078cd1bSZidan Wang ret = -EINVAL; 1971d078cd1bSZidan Wang goto out; 1972d078cd1bSZidan Wang } 1973d078cd1bSZidan Wang 1974d078cd1bSZidan Wang gpr = syscon_node_to_regmap(gpr_np); 1975d078cd1bSZidan Wang if (IS_ERR(gpr)) { 1976d078cd1bSZidan Wang dev_err(sdma->dev, "failed to get gpr regmap\n"); 1977d078cd1bSZidan Wang ret = PTR_ERR(gpr); 1978d078cd1bSZidan Wang goto out; 1979d078cd1bSZidan Wang } 1980d078cd1bSZidan Wang 1981d078cd1bSZidan Wang for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) { 1982d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i, ®); 1983d078cd1bSZidan Wang if (ret) { 1984d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1985d078cd1bSZidan Wang propname, i); 1986d078cd1bSZidan Wang goto out; 1987d078cd1bSZidan Wang } 1988d078cd1bSZidan Wang 1989d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 1, &shift); 1990d078cd1bSZidan Wang if (ret) { 1991d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1992d078cd1bSZidan Wang propname, i + 1); 1993d078cd1bSZidan Wang goto out; 1994d078cd1bSZidan Wang } 1995d078cd1bSZidan Wang 1996d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 2, &val); 1997d078cd1bSZidan Wang if (ret) { 1998d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1999d078cd1bSZidan Wang propname, i + 2); 2000d078cd1bSZidan Wang goto out; 2001d078cd1bSZidan Wang } 2002d078cd1bSZidan Wang 2003d078cd1bSZidan Wang regmap_update_bits(gpr, reg, BIT(shift), val << shift); 2004d078cd1bSZidan Wang } 2005d078cd1bSZidan Wang 2006d078cd1bSZidan Wang out: 20077104b9cbSMiaoqian Lin if (gpr_np) 2008d078cd1bSZidan Wang of_node_put(gpr_np); 2009d078cd1bSZidan Wang 2010d078cd1bSZidan Wang return ret; 2011d078cd1bSZidan Wang } 2012d078cd1bSZidan Wang 2013fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma, 20147b4b88e0SSascha Hauer const char *fw_name) 20157b4b88e0SSascha Hauer { 20167b4b88e0SSascha Hauer int ret; 20177b4b88e0SSascha Hauer 20187b4b88e0SSascha Hauer ret = request_firmware_nowait(THIS_MODULE, 20190733d839SShawn Guo FW_ACTION_UEVENT, fw_name, sdma->dev, 20207b4b88e0SSascha Hauer GFP_KERNEL, sdma, sdma_load_firmware); 20215b28aa31SSascha Hauer 20225b28aa31SSascha Hauer return ret; 20235b28aa31SSascha Hauer } 20245b28aa31SSascha Hauer 202519bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma) 20261ec1e82fSSascha Hauer { 20271ec1e82fSSascha Hauer int i, ret; 20281ec1e82fSSascha Hauer dma_addr_t ccb_phys; 20291ec1e82fSSascha Hauer 2030b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ipg); 2031b93edcddSFabio Estevam if (ret) 2032b93edcddSFabio Estevam return ret; 2033b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ahb); 2034b93edcddSFabio Estevam if (ret) 2035b93edcddSFabio Estevam goto disable_clk_ipg; 20361ec1e82fSSascha Hauer 2037941acd56SAngus Ainslie (Purism) if (sdma->drvdata->check_ratio && 2038941acd56SAngus Ainslie (Purism) (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg))) 203925aaa75dSAngus Ainslie (Purism) sdma->clk_ratio = 1; 204025aaa75dSAngus Ainslie (Purism) 20411ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 2042c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); 20431ec1e82fSSascha Hauer 2044ceaf5226SAndy Duan sdma->channel_control = dma_alloc_coherent(sdma->dev, 20451ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control) + 20461ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 20471ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 20481ec1e82fSSascha Hauer 20491ec1e82fSSascha Hauer if (!sdma->channel_control) { 20501ec1e82fSSascha Hauer ret = -ENOMEM; 20511ec1e82fSSascha Hauer goto err_dma_alloc; 20521ec1e82fSSascha Hauer } 20531ec1e82fSSascha Hauer 20541ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 20551ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control); 20561ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 20571ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control); 20581ec1e82fSSascha Hauer 20591ec1e82fSSascha Hauer /* disable all channels */ 206017bba72fSSascha Hauer for (i = 0; i < sdma->drvdata->num_events; i++) 2061c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); 20621ec1e82fSSascha Hauer 20631ec1e82fSSascha Hauer /* All channels have priority 0 */ 20641ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 2065c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 20661ec1e82fSSascha Hauer 206757b772b8SRobin Gong ret = sdma_request_channel0(sdma); 20681ec1e82fSSascha Hauer if (ret) 20691ec1e82fSSascha Hauer goto err_dma_alloc; 20701ec1e82fSSascha Hauer 20711ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 20721ec1e82fSSascha Hauer 20731ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 2074c4b56857SRichard Zhao writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); 20751ec1e82fSSascha Hauer 20761ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 207725aaa75dSAngus Ainslie (Purism) if (sdma->clk_ratio) 207825aaa75dSAngus Ainslie (Purism) writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG); 207925aaa75dSAngus Ainslie (Purism) else 2080c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); 20811ec1e82fSSascha Hauer 2082c4b56857SRichard Zhao writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); 20831ec1e82fSSascha Hauer 20841ec1e82fSSascha Hauer /* Initializes channel's priorities */ 20851ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 20861ec1e82fSSascha Hauer 20877560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 20887560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 20891ec1e82fSSascha Hauer 20901ec1e82fSSascha Hauer return 0; 20911ec1e82fSSascha Hauer 20921ec1e82fSSascha Hauer err_dma_alloc: 20937560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 2094b93edcddSFabio Estevam disable_clk_ipg: 2095b93edcddSFabio Estevam clk_disable(sdma->clk_ipg); 20961ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 20971ec1e82fSSascha Hauer return ret; 20981ec1e82fSSascha Hauer } 20991ec1e82fSSascha Hauer 21009479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param) 21019479e17cSShawn Guo { 21020b351865SNicolin Chen struct sdma_channel *sdmac = to_sdma_chan(chan); 21039479e17cSShawn Guo struct imx_dma_data *data = fn_param; 21049479e17cSShawn Guo 21059479e17cSShawn Guo if (!imx_dma_is_general_purpose(chan)) 21069479e17cSShawn Guo return false; 21079479e17cSShawn Guo 21080b351865SNicolin Chen sdmac->data = *data; 21090b351865SNicolin Chen chan->private = &sdmac->data; 21109479e17cSShawn Guo 21119479e17cSShawn Guo return true; 21129479e17cSShawn Guo } 21139479e17cSShawn Guo 21149479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec, 21159479e17cSShawn Guo struct of_dma *ofdma) 21169479e17cSShawn Guo { 21179479e17cSShawn Guo struct sdma_engine *sdma = ofdma->of_dma_data; 21189479e17cSShawn Guo dma_cap_mask_t mask = sdma->dma_device.cap_mask; 21199479e17cSShawn Guo struct imx_dma_data data; 21209479e17cSShawn Guo 21219479e17cSShawn Guo if (dma_spec->args_count != 3) 21229479e17cSShawn Guo return NULL; 21239479e17cSShawn Guo 21249479e17cSShawn Guo data.dma_request = dma_spec->args[0]; 21259479e17cSShawn Guo data.peripheral_type = dma_spec->args[1]; 21269479e17cSShawn Guo data.priority = dma_spec->args[2]; 21278391ecf4SShengjiu Wang /* 21288391ecf4SShengjiu Wang * init dma_request2 to zero, which is not used by the dts. 21298391ecf4SShengjiu Wang * For P2P, dma_request2 is init from dma_request_channel(), 21308391ecf4SShengjiu Wang * chan->private will point to the imx_dma_data, and in 21318391ecf4SShengjiu Wang * device_alloc_chan_resources(), imx_dma_data.dma_request2 will 21328391ecf4SShengjiu Wang * be set to sdmac->event_id1. 21338391ecf4SShengjiu Wang */ 21348391ecf4SShengjiu Wang data.dma_request2 = 0; 21359479e17cSShawn Guo 2136990c0b53SBaolin Wang return __dma_request_channel(&mask, sdma_filter_fn, &data, 2137990c0b53SBaolin Wang ofdma->of_node); 21389479e17cSShawn Guo } 21399479e17cSShawn Guo 2140e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev) 21411ec1e82fSSascha Hauer { 2142580975d7SShawn Guo struct device_node *np = pdev->dev.of_node; 21438391ecf4SShengjiu Wang struct device_node *spba_bus; 2144580975d7SShawn Guo const char *fw_name; 21451ec1e82fSSascha Hauer int ret; 21461ec1e82fSSascha Hauer int irq; 21471ec1e82fSSascha Hauer struct resource *iores; 21488391ecf4SShengjiu Wang struct resource spba_res; 21491ec1e82fSSascha Hauer int i; 21501ec1e82fSSascha Hauer struct sdma_engine *sdma; 215136e2f21aSSascha Hauer s32 *saddr_arr; 21521ec1e82fSSascha Hauer 215342536b9fSPhilippe Retornaz ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 215442536b9fSPhilippe Retornaz if (ret) 215542536b9fSPhilippe Retornaz return ret; 215642536b9fSPhilippe Retornaz 21577f24e0eeSFabio Estevam sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL); 21581ec1e82fSSascha Hauer if (!sdma) 21591ec1e82fSSascha Hauer return -ENOMEM; 21601ec1e82fSSascha Hauer 21612ccaef05SRichard Zhao spin_lock_init(&sdma->channel_0_lock); 216273eab978SSascha Hauer 21631ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 216432996419SFabio Estevam sdma->drvdata = of_device_get_match_data(sdma->dev); 21651ec1e82fSSascha Hauer 21661ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 21677f24e0eeSFabio Estevam if (irq < 0) 216863c72e02SFabio Estevam return irq; 21691ec1e82fSSascha Hauer 21707f24e0eeSFabio Estevam iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 21717f24e0eeSFabio Estevam sdma->regs = devm_ioremap_resource(&pdev->dev, iores); 21727f24e0eeSFabio Estevam if (IS_ERR(sdma->regs)) 21737f24e0eeSFabio Estevam return PTR_ERR(sdma->regs); 21741ec1e82fSSascha Hauer 21757560e3f3SSascha Hauer sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 21767f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ipg)) 21777f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ipg); 21781ec1e82fSSascha Hauer 21797560e3f3SSascha Hauer sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 21807f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ahb)) 21817f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ahb); 21827560e3f3SSascha Hauer 2183fb9caf37SArvind Yadav ret = clk_prepare(sdma->clk_ipg); 2184fb9caf37SArvind Yadav if (ret) 2185fb9caf37SArvind Yadav return ret; 2186fb9caf37SArvind Yadav 2187fb9caf37SArvind Yadav ret = clk_prepare(sdma->clk_ahb); 2188fb9caf37SArvind Yadav if (ret) 2189fb9caf37SArvind Yadav goto err_clk; 21907560e3f3SSascha Hauer 21910951a90eSFabio Estevam ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, 21920951a90eSFabio Estevam dev_name(&pdev->dev), sdma); 21931ec1e82fSSascha Hauer if (ret) 2194fb9caf37SArvind Yadav goto err_irq; 21951ec1e82fSSascha Hauer 21965bb9dbb5SVinod Koul sdma->irq = irq; 21975bb9dbb5SVinod Koul 21985b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 2199fb9caf37SArvind Yadav if (!sdma->script_addrs) { 2200fb9caf37SArvind Yadav ret = -ENOMEM; 2201fb9caf37SArvind Yadav goto err_irq; 2202fb9caf37SArvind Yadav } 22031ec1e82fSSascha Hauer 220436e2f21aSSascha Hauer /* initially no scripts available */ 220536e2f21aSSascha Hauer saddr_arr = (s32 *)sdma->script_addrs; 2206be4cf718SSascha Hauer for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++) 220736e2f21aSSascha Hauer saddr_arr[i] = -EINVAL; 220836e2f21aSSascha Hauer 22097214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 22107214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 22110f06c027SRobin Gong dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask); 22127214a8b1SSascha Hauer 22131ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 22141ec1e82fSSascha Hauer /* Initialize channel parameters */ 22151ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 22161ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 22171ec1e82fSSascha Hauer 22181ec1e82fSSascha Hauer sdmac->sdma = sdma; 22191ec1e82fSSascha Hauer 22201ec1e82fSSascha Hauer sdmac->channel = i; 222157b772b8SRobin Gong sdmac->vc.desc_free = sdma_desc_free; 22224e2b10beSRobin Gong INIT_LIST_HEAD(&sdmac->terminated); 2223b8603d2aSLucas Stach INIT_WORK(&sdmac->terminate_worker, 2224b8603d2aSLucas Stach sdma_channel_terminate_work); 222523889c63SSascha Hauer /* 222623889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 222723889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 222823889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 222923889c63SSascha Hauer */ 223023889c63SSascha Hauer if (i) 223157b772b8SRobin Gong vchan_init(&sdmac->vc, &sdma->dma_device); 22321ec1e82fSSascha Hauer } 22331ec1e82fSSascha Hauer 22345b28aa31SSascha Hauer ret = sdma_init(sdma); 22351ec1e82fSSascha Hauer if (ret) 22361ec1e82fSSascha Hauer goto err_init; 22371ec1e82fSSascha Hauer 2238d078cd1bSZidan Wang ret = sdma_event_remap(sdma); 2239d078cd1bSZidan Wang if (ret) 2240d078cd1bSZidan Wang goto err_init; 2241d078cd1bSZidan Wang 2242dcfec3c0SSascha Hauer if (sdma->drvdata->script_addrs) 2243dcfec3c0SSascha Hauer sdma_add_scripts(sdma, sdma->drvdata->script_addrs); 22445b28aa31SSascha Hauer 22451ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 22461ec1e82fSSascha Hauer 22471ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 22481ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 22491ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 22501ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 22511ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 22527b350ab0SMaxime Ripard sdma->dma_device.device_config = sdma_config; 2253a80f2787SSascha Hauer sdma->dma_device.device_terminate_all = sdma_terminate_all; 2254b8603d2aSLucas Stach sdma->dma_device.device_synchronize = sdma_channel_synchronize; 2255f9d4a398SNicolin Chen sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS; 2256f9d4a398SNicolin Chen sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS; 2257f9d4a398SNicolin Chen sdma->dma_device.directions = SDMA_DMA_DIRECTIONS; 22586f3125ceSLucas Stach sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT; 22590f06c027SRobin Gong sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy; 22601ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 2261a3711d49SAngus Ainslie (Purism) sdma->dma_device.copy_align = 2; 22624a6b2e8aSRobin Gong dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT); 22631ec1e82fSSascha Hauer 226423e11811SVignesh Raman platform_set_drvdata(pdev, sdma); 226523e11811SVignesh Raman 22661ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 22671ec1e82fSSascha Hauer if (ret) { 22681ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 22691ec1e82fSSascha Hauer goto err_init; 22701ec1e82fSSascha Hauer } 22711ec1e82fSSascha Hauer 22729479e17cSShawn Guo if (np) { 22739479e17cSShawn Guo ret = of_dma_controller_register(np, sdma_xlate, sdma); 22749479e17cSShawn Guo if (ret) { 22759479e17cSShawn Guo dev_err(&pdev->dev, "failed to register controller\n"); 22769479e17cSShawn Guo goto err_register; 22779479e17cSShawn Guo } 22788391ecf4SShengjiu Wang 22798391ecf4SShengjiu Wang spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus"); 22808391ecf4SShengjiu Wang ret = of_address_to_resource(spba_bus, 0, &spba_res); 22818391ecf4SShengjiu Wang if (!ret) { 22828391ecf4SShengjiu Wang sdma->spba_start_addr = spba_res.start; 22838391ecf4SShengjiu Wang sdma->spba_end_addr = spba_res.end; 22848391ecf4SShengjiu Wang } 22858391ecf4SShengjiu Wang of_node_put(spba_bus); 22869479e17cSShawn Guo } 22879479e17cSShawn Guo 22882b8066c3SSven Van Asbroeck /* 22892b8066c3SSven Van Asbroeck * Because that device tree does not encode ROM script address, 22902b8066c3SSven Van Asbroeck * the RAM script in firmware is mandatory for device tree 22912b8066c3SSven Van Asbroeck * probe, otherwise it fails. 22922b8066c3SSven Van Asbroeck */ 22932b8066c3SSven Van Asbroeck ret = of_property_read_string(np, "fsl,sdma-ram-script-name", 22942b8066c3SSven Van Asbroeck &fw_name); 22952b8066c3SSven Van Asbroeck if (ret) { 22962b8066c3SSven Van Asbroeck dev_warn(&pdev->dev, "failed to get firmware name\n"); 22972b8066c3SSven Van Asbroeck } else { 22982b8066c3SSven Van Asbroeck ret = sdma_get_firmware(sdma, fw_name); 22992b8066c3SSven Van Asbroeck if (ret) 23002b8066c3SSven Van Asbroeck dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); 23012b8066c3SSven Van Asbroeck } 23022b8066c3SSven Van Asbroeck 23031ec1e82fSSascha Hauer return 0; 23041ec1e82fSSascha Hauer 23059479e17cSShawn Guo err_register: 23069479e17cSShawn Guo dma_async_device_unregister(&sdma->dma_device); 23071ec1e82fSSascha Hauer err_init: 23081ec1e82fSSascha Hauer kfree(sdma->script_addrs); 2309fb9caf37SArvind Yadav err_irq: 2310fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ahb); 2311fb9caf37SArvind Yadav err_clk: 2312fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ipg); 2313939fd4f0SShawn Guo return ret; 23141ec1e82fSSascha Hauer } 23151ec1e82fSSascha Hauer 23161d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev) 23171ec1e82fSSascha Hauer { 231823e11811SVignesh Raman struct sdma_engine *sdma = platform_get_drvdata(pdev); 2319c12fe497SVignesh Raman int i; 232023e11811SVignesh Raman 23215bb9dbb5SVinod Koul devm_free_irq(&pdev->dev, sdma->irq, sdma); 232223e11811SVignesh Raman dma_async_device_unregister(&sdma->dma_device); 232323e11811SVignesh Raman kfree(sdma->script_addrs); 2324fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ahb); 2325fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ipg); 2326c12fe497SVignesh Raman /* Kill the tasklet */ 2327c12fe497SVignesh Raman for (i = 0; i < MAX_DMA_CHANNELS; i++) { 2328c12fe497SVignesh Raman struct sdma_channel *sdmac = &sdma->channel[i]; 2329c12fe497SVignesh Raman 233057b772b8SRobin Gong tasklet_kill(&sdmac->vc.task); 233157b772b8SRobin Gong sdma_free_chan_resources(&sdmac->vc.chan); 2332c12fe497SVignesh Raman } 233323e11811SVignesh Raman 233423e11811SVignesh Raman platform_set_drvdata(pdev, NULL); 233523e11811SVignesh Raman return 0; 23361ec1e82fSSascha Hauer } 23371ec1e82fSSascha Hauer 23381ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 23391ec1e82fSSascha Hauer .driver = { 23401ec1e82fSSascha Hauer .name = "imx-sdma", 2341580975d7SShawn Guo .of_match_table = sdma_dt_ids, 23421ec1e82fSSascha Hauer }, 23431d1bbd30SMaxin B. John .remove = sdma_remove, 234423e11811SVignesh Raman .probe = sdma_probe, 23451ec1e82fSSascha Hauer }; 23461ec1e82fSSascha Hauer 234723e11811SVignesh Raman module_platform_driver(sdma_driver); 23481ec1e82fSSascha Hauer 23491ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 23501ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 2351c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q) 2352c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin"); 2353c0879342SNicolas Chauvet #endif 2354c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX7D) 2355c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin"); 2356c0879342SNicolas Chauvet #endif 23571ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 2358