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> 170bbc1413SRichard Zhao #include <linux/bitops.h> 181ec1e82fSSascha Hauer #include <linux/mm.h> 191ec1e82fSSascha Hauer #include <linux/interrupt.h> 201ec1e82fSSascha Hauer #include <linux/clk.h> 212ccaef05SRichard Zhao #include <linux/delay.h> 221ec1e82fSSascha Hauer #include <linux/sched.h> 231ec1e82fSSascha Hauer #include <linux/semaphore.h> 241ec1e82fSSascha Hauer #include <linux/spinlock.h> 251ec1e82fSSascha Hauer #include <linux/device.h> 261ec1e82fSSascha Hauer #include <linux/dma-mapping.h> 271ec1e82fSSascha Hauer #include <linux/firmware.h> 281ec1e82fSSascha Hauer #include <linux/slab.h> 291ec1e82fSSascha Hauer #include <linux/platform_device.h> 301ec1e82fSSascha Hauer #include <linux/dmaengine.h> 31580975d7SShawn Guo #include <linux/of.h> 328391ecf4SShengjiu Wang #include <linux/of_address.h> 33580975d7SShawn Guo #include <linux/of_device.h> 349479e17cSShawn Guo #include <linux/of_dma.h> 35b8603d2aSLucas Stach #include <linux/workqueue.h> 361ec1e82fSSascha Hauer 371ec1e82fSSascha Hauer #include <asm/irq.h> 3882906b13SArnd Bergmann #include <linux/platform_data/dma-imx-sdma.h> 3982906b13SArnd Bergmann #include <linux/platform_data/dma-imx.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 771ec1e82fSSascha Hauer 781ec1e82fSSascha Hauer /* 791ec1e82fSSascha Hauer * Buffer descriptor status values. 801ec1e82fSSascha Hauer */ 811ec1e82fSSascha Hauer #define BD_DONE 0x01 821ec1e82fSSascha Hauer #define BD_WRAP 0x02 831ec1e82fSSascha Hauer #define BD_CONT 0x04 841ec1e82fSSascha Hauer #define BD_INTR 0x08 851ec1e82fSSascha Hauer #define BD_RROR 0x10 861ec1e82fSSascha Hauer #define BD_LAST 0x20 871ec1e82fSSascha Hauer #define BD_EXTD 0x80 881ec1e82fSSascha Hauer 891ec1e82fSSascha Hauer /* 901ec1e82fSSascha Hauer * Data Node descriptor status values. 911ec1e82fSSascha Hauer */ 921ec1e82fSSascha Hauer #define DND_END_OF_FRAME 0x80 931ec1e82fSSascha Hauer #define DND_END_OF_XFER 0x40 941ec1e82fSSascha Hauer #define DND_DONE 0x20 951ec1e82fSSascha Hauer #define DND_UNUSED 0x01 961ec1e82fSSascha Hauer 971ec1e82fSSascha Hauer /* 981ec1e82fSSascha Hauer * IPCV2 descriptor status values. 991ec1e82fSSascha Hauer */ 1001ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME 0x40 1011ec1e82fSSascha Hauer 1021ec1e82fSSascha Hauer #define IPCV2_MAX_NODES 50 1031ec1e82fSSascha Hauer /* 1041ec1e82fSSascha Hauer * Error bit set in the CCB status field by the SDMA, 1051ec1e82fSSascha Hauer * in setbd routine, in case of a transfer error 1061ec1e82fSSascha Hauer */ 1071ec1e82fSSascha Hauer #define DATA_ERROR 0x10000000 1081ec1e82fSSascha Hauer 1091ec1e82fSSascha Hauer /* 1101ec1e82fSSascha Hauer * Buffer descriptor commands. 1111ec1e82fSSascha Hauer */ 1121ec1e82fSSascha Hauer #define C0_ADDR 0x01 1131ec1e82fSSascha Hauer #define C0_LOAD 0x02 1141ec1e82fSSascha Hauer #define C0_DUMP 0x03 1151ec1e82fSSascha Hauer #define C0_SETCTX 0x07 1161ec1e82fSSascha Hauer #define C0_GETCTX 0x03 1171ec1e82fSSascha Hauer #define C0_SETDM 0x01 1181ec1e82fSSascha Hauer #define C0_SETPM 0x04 1191ec1e82fSSascha Hauer #define C0_GETDM 0x02 1201ec1e82fSSascha Hauer #define C0_GETPM 0x08 1211ec1e82fSSascha Hauer /* 1221ec1e82fSSascha Hauer * Change endianness indicator in the BD command field 1231ec1e82fSSascha Hauer */ 1241ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS 0x80 1251ec1e82fSSascha Hauer 1261ec1e82fSSascha Hauer /* 1278391ecf4SShengjiu Wang * p_2_p watermark_level description 1288391ecf4SShengjiu Wang * Bits Name Description 1298391ecf4SShengjiu Wang * 0-7 Lower WML Lower watermark level 1308391ecf4SShengjiu Wang * 8 PS 1: Pad Swallowing 1318391ecf4SShengjiu Wang * 0: No Pad Swallowing 1328391ecf4SShengjiu Wang * 9 PA 1: Pad Adding 1338391ecf4SShengjiu Wang * 0: No Pad Adding 1348391ecf4SShengjiu Wang * 10 SPDIF If this bit is set both source 1358391ecf4SShengjiu Wang * and destination are on SPBA 1368391ecf4SShengjiu Wang * 11 Source Bit(SP) 1: Source on SPBA 1378391ecf4SShengjiu Wang * 0: Source on AIPS 1388391ecf4SShengjiu Wang * 12 Destination Bit(DP) 1: Destination on SPBA 1398391ecf4SShengjiu Wang * 0: Destination on AIPS 1408391ecf4SShengjiu Wang * 13-15 --------- MUST BE 0 1418391ecf4SShengjiu Wang * 16-23 Higher WML HWML 1428391ecf4SShengjiu Wang * 24-27 N Total number of samples after 1438391ecf4SShengjiu Wang * which Pad adding/Swallowing 1448391ecf4SShengjiu Wang * must be done. It must be odd. 1458391ecf4SShengjiu Wang * 28 Lower WML Event(LWE) SDMA events reg to check for 1468391ecf4SShengjiu Wang * LWML event mask 1478391ecf4SShengjiu Wang * 0: LWE in EVENTS register 1488391ecf4SShengjiu Wang * 1: LWE in EVENTS2 register 1498391ecf4SShengjiu Wang * 29 Higher WML Event(HWE) SDMA events reg to check for 1508391ecf4SShengjiu Wang * HWML event mask 1518391ecf4SShengjiu Wang * 0: HWE in EVENTS register 1528391ecf4SShengjiu Wang * 1: HWE in EVENTS2 register 1538391ecf4SShengjiu Wang * 30 --------- MUST BE 0 1548391ecf4SShengjiu Wang * 31 CONT 1: Amount of samples to be 1558391ecf4SShengjiu Wang * transferred is unknown and 1568391ecf4SShengjiu Wang * script will keep on 1578391ecf4SShengjiu Wang * transferring samples as long as 1588391ecf4SShengjiu Wang * both events are detected and 1598391ecf4SShengjiu Wang * script must be manually stopped 1608391ecf4SShengjiu Wang * by the application 1618391ecf4SShengjiu Wang * 0: The amount of samples to be 1628391ecf4SShengjiu Wang * transferred is equal to the 1638391ecf4SShengjiu Wang * count field of mode word 1648391ecf4SShengjiu Wang */ 1658391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML 0xFF 1668391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS BIT(8) 1678391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA BIT(9) 1688391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10) 1698391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP BIT(11) 1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP BIT(12) 1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16) 1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE BIT(28) 1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE BIT(29) 1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT BIT(31) 1758391ecf4SShengjiu Wang 176f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ 177f9d4a398SNicolin Chen BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ 178f9d4a398SNicolin Chen BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)) 179f9d4a398SNicolin Chen 180f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \ 181f9d4a398SNicolin Chen BIT(DMA_MEM_TO_DEV) | \ 182f9d4a398SNicolin Chen BIT(DMA_DEV_TO_DEV)) 183f9d4a398SNicolin Chen 1848391ecf4SShengjiu Wang /* 1851ec1e82fSSascha Hauer * Mode/Count of data node descriptors - IPCv2 1861ec1e82fSSascha Hauer */ 1871ec1e82fSSascha Hauer struct sdma_mode_count { 1884a6b2e8aSRobin Gong #define SDMA_BD_MAX_CNT 0xffff 1891ec1e82fSSascha Hauer u32 count : 16; /* size of the buffer pointed by this BD */ 1901ec1e82fSSascha Hauer u32 status : 8; /* E,R,I,C,W,D status bits stored here */ 191e4b75760SMartin Kaiser u32 command : 8; /* command mostly used for channel 0 */ 1921ec1e82fSSascha Hauer }; 1931ec1e82fSSascha Hauer 1941ec1e82fSSascha Hauer /* 1951ec1e82fSSascha Hauer * Buffer descriptor 1961ec1e82fSSascha Hauer */ 1971ec1e82fSSascha Hauer struct sdma_buffer_descriptor { 1981ec1e82fSSascha Hauer struct sdma_mode_count mode; 1991ec1e82fSSascha Hauer u32 buffer_addr; /* address of the buffer described */ 2001ec1e82fSSascha Hauer u32 ext_buffer_addr; /* extended buffer address */ 2011ec1e82fSSascha Hauer } __attribute__ ((packed)); 2021ec1e82fSSascha Hauer 2031ec1e82fSSascha Hauer /** 2041ec1e82fSSascha Hauer * struct sdma_channel_control - Channel control Block 2051ec1e82fSSascha Hauer * 20624ca312dSRobin Gong * @current_bd_ptr: current buffer descriptor processed 20724ca312dSRobin Gong * @base_bd_ptr: first element of buffer descriptor array 20824ca312dSRobin Gong * @unused: padding. The SDMA engine expects an array of 128 byte 2091ec1e82fSSascha Hauer * control blocks 2101ec1e82fSSascha Hauer */ 2111ec1e82fSSascha Hauer struct sdma_channel_control { 2121ec1e82fSSascha Hauer u32 current_bd_ptr; 2131ec1e82fSSascha Hauer u32 base_bd_ptr; 2141ec1e82fSSascha Hauer u32 unused[2]; 2151ec1e82fSSascha Hauer } __attribute__ ((packed)); 2161ec1e82fSSascha Hauer 2171ec1e82fSSascha Hauer /** 2181ec1e82fSSascha Hauer * struct sdma_state_registers - SDMA context for a channel 2191ec1e82fSSascha Hauer * 2201ec1e82fSSascha Hauer * @pc: program counter 22124ca312dSRobin Gong * @unused1: unused 2221ec1e82fSSascha Hauer * @t: test bit: status of arithmetic & test instruction 2231ec1e82fSSascha Hauer * @rpc: return program counter 22424ca312dSRobin Gong * @unused0: unused 2251ec1e82fSSascha Hauer * @sf: source fault while loading data 2261ec1e82fSSascha Hauer * @spc: loop start program counter 22724ca312dSRobin Gong * @unused2: unused 2281ec1e82fSSascha Hauer * @df: destination fault while storing data 2291ec1e82fSSascha Hauer * @epc: loop end program counter 2301ec1e82fSSascha Hauer * @lm: loop mode 2311ec1e82fSSascha Hauer */ 2321ec1e82fSSascha Hauer struct sdma_state_registers { 2331ec1e82fSSascha Hauer u32 pc :14; 2341ec1e82fSSascha Hauer u32 unused1: 1; 2351ec1e82fSSascha Hauer u32 t : 1; 2361ec1e82fSSascha Hauer u32 rpc :14; 2371ec1e82fSSascha Hauer u32 unused0: 1; 2381ec1e82fSSascha Hauer u32 sf : 1; 2391ec1e82fSSascha Hauer u32 spc :14; 2401ec1e82fSSascha Hauer u32 unused2: 1; 2411ec1e82fSSascha Hauer u32 df : 1; 2421ec1e82fSSascha Hauer u32 epc :14; 2431ec1e82fSSascha Hauer u32 lm : 2; 2441ec1e82fSSascha Hauer } __attribute__ ((packed)); 2451ec1e82fSSascha Hauer 2461ec1e82fSSascha Hauer /** 2471ec1e82fSSascha Hauer * struct sdma_context_data - sdma context specific to a channel 2481ec1e82fSSascha Hauer * 2491ec1e82fSSascha Hauer * @channel_state: channel state bits 2501ec1e82fSSascha Hauer * @gReg: general registers 2511ec1e82fSSascha Hauer * @mda: burst dma destination address register 2521ec1e82fSSascha Hauer * @msa: burst dma source address register 2531ec1e82fSSascha Hauer * @ms: burst dma status register 2541ec1e82fSSascha Hauer * @md: burst dma data register 2551ec1e82fSSascha Hauer * @pda: peripheral dma destination address register 2561ec1e82fSSascha Hauer * @psa: peripheral dma source address register 2571ec1e82fSSascha Hauer * @ps: peripheral dma status register 2581ec1e82fSSascha Hauer * @pd: peripheral dma data register 2591ec1e82fSSascha Hauer * @ca: CRC polynomial register 2601ec1e82fSSascha Hauer * @cs: CRC accumulator register 2611ec1e82fSSascha Hauer * @dda: dedicated core destination address register 2621ec1e82fSSascha Hauer * @dsa: dedicated core source address register 2631ec1e82fSSascha Hauer * @ds: dedicated core status register 2641ec1e82fSSascha Hauer * @dd: dedicated core data register 26524ca312dSRobin Gong * @scratch0: 1st word of dedicated ram for context switch 26624ca312dSRobin Gong * @scratch1: 2nd word of dedicated ram for context switch 26724ca312dSRobin Gong * @scratch2: 3rd word of dedicated ram for context switch 26824ca312dSRobin Gong * @scratch3: 4th word of dedicated ram for context switch 26924ca312dSRobin Gong * @scratch4: 5th word of dedicated ram for context switch 27024ca312dSRobin Gong * @scratch5: 6th word of dedicated ram for context switch 27124ca312dSRobin Gong * @scratch6: 7th word of dedicated ram for context switch 27224ca312dSRobin Gong * @scratch7: 8th word of dedicated ram for context switch 2731ec1e82fSSascha Hauer */ 2741ec1e82fSSascha Hauer struct sdma_context_data { 2751ec1e82fSSascha Hauer struct sdma_state_registers channel_state; 2761ec1e82fSSascha Hauer u32 gReg[8]; 2771ec1e82fSSascha Hauer u32 mda; 2781ec1e82fSSascha Hauer u32 msa; 2791ec1e82fSSascha Hauer u32 ms; 2801ec1e82fSSascha Hauer u32 md; 2811ec1e82fSSascha Hauer u32 pda; 2821ec1e82fSSascha Hauer u32 psa; 2831ec1e82fSSascha Hauer u32 ps; 2841ec1e82fSSascha Hauer u32 pd; 2851ec1e82fSSascha Hauer u32 ca; 2861ec1e82fSSascha Hauer u32 cs; 2871ec1e82fSSascha Hauer u32 dda; 2881ec1e82fSSascha Hauer u32 dsa; 2891ec1e82fSSascha Hauer u32 ds; 2901ec1e82fSSascha Hauer u32 dd; 2911ec1e82fSSascha Hauer u32 scratch0; 2921ec1e82fSSascha Hauer u32 scratch1; 2931ec1e82fSSascha Hauer u32 scratch2; 2941ec1e82fSSascha Hauer u32 scratch3; 2951ec1e82fSSascha Hauer u32 scratch4; 2961ec1e82fSSascha Hauer u32 scratch5; 2971ec1e82fSSascha Hauer u32 scratch6; 2981ec1e82fSSascha Hauer u32 scratch7; 2991ec1e82fSSascha Hauer } __attribute__ ((packed)); 3001ec1e82fSSascha Hauer 3011ec1e82fSSascha Hauer 3021ec1e82fSSascha Hauer struct sdma_engine; 3031ec1e82fSSascha Hauer 3041ec1e82fSSascha Hauer /** 30576c33d27SSascha Hauer * struct sdma_desc - descriptor structor for one transfer 30624ca312dSRobin Gong * @vd: descriptor for virt dma 30724ca312dSRobin Gong * @num_bd: number of descriptors currently handling 30824ca312dSRobin Gong * @bd_phys: physical address of bd 30924ca312dSRobin Gong * @buf_tail: ID of the buffer that was processed 31024ca312dSRobin Gong * @buf_ptail: ID of the previous buffer that was processed 31124ca312dSRobin Gong * @period_len: period length, used in cyclic. 31224ca312dSRobin Gong * @chn_real_count: the real count updated from bd->mode.count 31324ca312dSRobin Gong * @chn_count: the transfer count set 31424ca312dSRobin Gong * @sdmac: sdma_channel pointer 31524ca312dSRobin Gong * @bd: pointer of allocate bd 31676c33d27SSascha Hauer */ 31776c33d27SSascha Hauer struct sdma_desc { 31857b772b8SRobin Gong struct virt_dma_desc vd; 31976c33d27SSascha Hauer unsigned int num_bd; 32076c33d27SSascha Hauer dma_addr_t bd_phys; 32176c33d27SSascha Hauer unsigned int buf_tail; 32276c33d27SSascha Hauer unsigned int buf_ptail; 32376c33d27SSascha Hauer unsigned int period_len; 32476c33d27SSascha Hauer unsigned int chn_real_count; 32576c33d27SSascha Hauer unsigned int chn_count; 32676c33d27SSascha Hauer struct sdma_channel *sdmac; 32776c33d27SSascha Hauer struct sdma_buffer_descriptor *bd; 32876c33d27SSascha Hauer }; 32976c33d27SSascha Hauer 33076c33d27SSascha Hauer /** 3311ec1e82fSSascha Hauer * struct sdma_channel - housekeeping for a SDMA channel 3321ec1e82fSSascha Hauer * 33324ca312dSRobin Gong * @vc: virt_dma base structure 33424ca312dSRobin Gong * @desc: sdma description including vd and other special member 33524ca312dSRobin Gong * @sdma: pointer to the SDMA engine for this channel 33624ca312dSRobin Gong * @channel: the channel number, matches dmaengine chan_id + 1 33724ca312dSRobin Gong * @direction: transfer type. Needed for setting SDMA script 338107d0644SVinod Koul * @slave_config Slave configuration 33924ca312dSRobin Gong * @peripheral_type: Peripheral type. Needed for setting SDMA script 34024ca312dSRobin Gong * @event_id0: aka dma request line 34124ca312dSRobin Gong * @event_id1: for channels that use 2 events 34224ca312dSRobin Gong * @word_size: peripheral access size 34324ca312dSRobin Gong * @pc_from_device: script address for those device_2_memory 34424ca312dSRobin Gong * @pc_to_device: script address for those memory_2_device 34524ca312dSRobin Gong * @device_to_device: script address for those device_2_device 3460f06c027SRobin Gong * @pc_to_pc: script address for those memory_2_memory 34724ca312dSRobin Gong * @flags: loop mode or not 34824ca312dSRobin Gong * @per_address: peripheral source or destination address in common case 34924ca312dSRobin Gong * destination address in p_2_p case 35024ca312dSRobin Gong * @per_address2: peripheral source address in p_2_p case 35124ca312dSRobin Gong * @event_mask: event mask used in p_2_p script 35224ca312dSRobin Gong * @watermark_level: value for gReg[7], some script will extend it from 35324ca312dSRobin Gong * basic watermark such as p_2_p 35424ca312dSRobin Gong * @shp_addr: value for gReg[6] 35524ca312dSRobin Gong * @per_addr: value for gReg[2] 35624ca312dSRobin Gong * @status: status of dma channel 35724ca312dSRobin Gong * @data: specific sdma interface structure 35824ca312dSRobin Gong * @bd_pool: dma_pool for bd 3591ec1e82fSSascha Hauer */ 3601ec1e82fSSascha Hauer struct sdma_channel { 36157b772b8SRobin Gong struct virt_dma_chan vc; 36276c33d27SSascha Hauer struct sdma_desc *desc; 3631ec1e82fSSascha Hauer struct sdma_engine *sdma; 3641ec1e82fSSascha Hauer unsigned int channel; 365db8196dfSVinod Koul enum dma_transfer_direction direction; 366107d0644SVinod Koul struct dma_slave_config slave_config; 3671ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type; 3681ec1e82fSSascha Hauer unsigned int event_id0; 3691ec1e82fSSascha Hauer unsigned int event_id1; 3701ec1e82fSSascha Hauer enum dma_slave_buswidth word_size; 3711ec1e82fSSascha Hauer unsigned int pc_from_device, pc_to_device; 3728391ecf4SShengjiu Wang unsigned int device_to_device; 3730f06c027SRobin Gong unsigned int pc_to_pc; 3741ec1e82fSSascha Hauer unsigned long flags; 3758391ecf4SShengjiu Wang dma_addr_t per_address, per_address2; 3760bbc1413SRichard Zhao unsigned long event_mask[2]; 3770bbc1413SRichard Zhao unsigned long watermark_level; 3781ec1e82fSSascha Hauer u32 shp_addr, per_addr; 3791ec1e82fSSascha Hauer enum dma_status status; 380ad0d92d7SRobin Gong bool context_loaded; 3810b351865SNicolin Chen struct imx_dma_data data; 382b8603d2aSLucas Stach struct work_struct terminate_worker; 3831ec1e82fSSascha Hauer }; 3841ec1e82fSSascha Hauer 3850bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP BIT(0) 3861ec1e82fSSascha Hauer 3871ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32 3881ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1 3891ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1 3901ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7 3911ec1e82fSSascha Hauer 3921ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453 3931ec1e82fSSascha Hauer 3941ec1e82fSSascha Hauer /** 3951ec1e82fSSascha Hauer * struct sdma_firmware_header - Layout of the firmware image 3961ec1e82fSSascha Hauer * 39724ca312dSRobin Gong * @magic: "SDMA" 39824ca312dSRobin Gong * @version_major: increased whenever layout of struct 39924ca312dSRobin Gong * sdma_script_start_addrs changes. 40024ca312dSRobin Gong * @version_minor: firmware minor version (for binary compatible changes) 40124ca312dSRobin Gong * @script_addrs_start: offset of struct sdma_script_start_addrs in this image 40224ca312dSRobin Gong * @num_script_addrs: Number of script addresses in this image 40324ca312dSRobin Gong * @ram_code_start: offset of SDMA ram image in this firmware image 40424ca312dSRobin Gong * @ram_code_size: size of SDMA ram image 40524ca312dSRobin Gong * @script_addrs: Stores the start address of the SDMA scripts 4061ec1e82fSSascha Hauer * (in SDMA memory space) 4071ec1e82fSSascha Hauer */ 4081ec1e82fSSascha Hauer struct sdma_firmware_header { 4091ec1e82fSSascha Hauer u32 magic; 4101ec1e82fSSascha Hauer u32 version_major; 4111ec1e82fSSascha Hauer u32 version_minor; 4121ec1e82fSSascha Hauer u32 script_addrs_start; 4131ec1e82fSSascha Hauer u32 num_script_addrs; 4141ec1e82fSSascha Hauer u32 ram_code_start; 4151ec1e82fSSascha Hauer u32 ram_code_size; 4161ec1e82fSSascha Hauer }; 4171ec1e82fSSascha Hauer 41817bba72fSSascha Hauer struct sdma_driver_data { 41917bba72fSSascha Hauer int chnenbl0; 42017bba72fSSascha Hauer int num_events; 421dcfec3c0SSascha Hauer struct sdma_script_start_addrs *script_addrs; 422941acd56SAngus Ainslie (Purism) bool check_ratio; 42362550cd7SShawn Guo }; 42462550cd7SShawn Guo 4251ec1e82fSSascha Hauer struct sdma_engine { 4261ec1e82fSSascha Hauer struct device *dev; 427b9b3f82fSSascha Hauer struct device_dma_parameters dma_parms; 4281ec1e82fSSascha Hauer struct sdma_channel channel[MAX_DMA_CHANNELS]; 4291ec1e82fSSascha Hauer struct sdma_channel_control *channel_control; 4301ec1e82fSSascha Hauer void __iomem *regs; 4311ec1e82fSSascha Hauer struct sdma_context_data *context; 4321ec1e82fSSascha Hauer dma_addr_t context_phys; 4331ec1e82fSSascha Hauer struct dma_device dma_device; 4347560e3f3SSascha Hauer struct clk *clk_ipg; 4357560e3f3SSascha Hauer struct clk *clk_ahb; 4362ccaef05SRichard Zhao spinlock_t channel_0_lock; 437cd72b846SNicolin Chen u32 script_number; 4381ec1e82fSSascha Hauer struct sdma_script_start_addrs *script_addrs; 43917bba72fSSascha Hauer const struct sdma_driver_data *drvdata; 4408391ecf4SShengjiu Wang u32 spba_start_addr; 4418391ecf4SShengjiu Wang u32 spba_end_addr; 4425bb9dbb5SVinod Koul unsigned int irq; 44376c33d27SSascha Hauer dma_addr_t bd0_phys; 44476c33d27SSascha Hauer struct sdma_buffer_descriptor *bd0; 44525aaa75dSAngus Ainslie (Purism) /* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/ 44625aaa75dSAngus Ainslie (Purism) bool clk_ratio; 44717bba72fSSascha Hauer }; 44817bba72fSSascha Hauer 449107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan, 450107d0644SVinod Koul struct dma_slave_config *dmaengine_cfg, 451107d0644SVinod Koul enum dma_transfer_direction direction); 452107d0644SVinod Koul 453e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = { 45417bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX31, 45517bba72fSSascha Hauer .num_events = 32, 45617bba72fSSascha Hauer }; 45717bba72fSSascha Hauer 458dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = { 459dcfec3c0SSascha Hauer .ap_2_ap_addr = 729, 460dcfec3c0SSascha Hauer .uart_2_mcu_addr = 904, 461dcfec3c0SSascha Hauer .per_2_app_addr = 1255, 462dcfec3c0SSascha Hauer .mcu_2_app_addr = 834, 463dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1120, 464dcfec3c0SSascha Hauer .per_2_shp_addr = 1329, 465dcfec3c0SSascha Hauer .mcu_2_shp_addr = 1048, 466dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1560, 467dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1479, 468dcfec3c0SSascha Hauer .app_2_per_addr = 1189, 469dcfec3c0SSascha Hauer .app_2_mcu_addr = 770, 470dcfec3c0SSascha Hauer .shp_2_per_addr = 1407, 471dcfec3c0SSascha Hauer .shp_2_mcu_addr = 979, 472dcfec3c0SSascha Hauer }; 473dcfec3c0SSascha Hauer 474e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = { 475dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 476dcfec3c0SSascha Hauer .num_events = 48, 477dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx25, 478dcfec3c0SSascha Hauer }; 479dcfec3c0SSascha Hauer 480e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = { 48117bba72fSSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 48217bba72fSSascha Hauer .num_events = 48, 4831ec1e82fSSascha Hauer }; 4841ec1e82fSSascha Hauer 485dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = { 486dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 487dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 488dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 489dcfec3c0SSascha Hauer .mcu_2_shp_addr = 961, 490dcfec3c0SSascha Hauer .ata_2_mcu_addr = 1473, 491dcfec3c0SSascha Hauer .mcu_2_ata_addr = 1392, 492dcfec3c0SSascha Hauer .app_2_per_addr = 1033, 493dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 494dcfec3c0SSascha Hauer .shp_2_per_addr = 1251, 495dcfec3c0SSascha Hauer .shp_2_mcu_addr = 892, 496dcfec3c0SSascha Hauer }; 497dcfec3c0SSascha Hauer 498e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = { 499dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 500dcfec3c0SSascha Hauer .num_events = 48, 501dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx51, 502dcfec3c0SSascha Hauer }; 503dcfec3c0SSascha Hauer 504dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = { 505dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 506dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 507dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 508dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 509dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 510dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 511dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 512dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 513dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 514dcfec3c0SSascha Hauer .firi_2_mcu_addr = 1193, 515dcfec3c0SSascha Hauer .mcu_2_firi_addr = 1290, 516dcfec3c0SSascha Hauer }; 517dcfec3c0SSascha Hauer 518e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = { 519dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 520dcfec3c0SSascha Hauer .num_events = 48, 521dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx53, 522dcfec3c0SSascha Hauer }; 523dcfec3c0SSascha Hauer 524dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = { 525dcfec3c0SSascha Hauer .ap_2_ap_addr = 642, 526dcfec3c0SSascha Hauer .uart_2_mcu_addr = 817, 527dcfec3c0SSascha Hauer .mcu_2_app_addr = 747, 528dcfec3c0SSascha Hauer .per_2_per_addr = 6331, 529dcfec3c0SSascha Hauer .uartsh_2_mcu_addr = 1032, 530dcfec3c0SSascha Hauer .mcu_2_shp_addr = 960, 531dcfec3c0SSascha Hauer .app_2_mcu_addr = 683, 532dcfec3c0SSascha Hauer .shp_2_mcu_addr = 891, 533dcfec3c0SSascha Hauer .spdif_2_mcu_addr = 1100, 534dcfec3c0SSascha Hauer .mcu_2_spdif_addr = 1134, 535dcfec3c0SSascha Hauer }; 536dcfec3c0SSascha Hauer 537e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = { 538dcfec3c0SSascha Hauer .chnenbl0 = SDMA_CHNENBL0_IMX35, 539dcfec3c0SSascha Hauer .num_events = 48, 540dcfec3c0SSascha Hauer .script_addrs = &sdma_script_imx6q, 541dcfec3c0SSascha Hauer }; 542dcfec3c0SSascha Hauer 543b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = { 544b7d2648aSFabio Estevam .ap_2_ap_addr = 644, 545b7d2648aSFabio Estevam .uart_2_mcu_addr = 819, 546b7d2648aSFabio Estevam .mcu_2_app_addr = 749, 547b7d2648aSFabio Estevam .uartsh_2_mcu_addr = 1034, 548b7d2648aSFabio Estevam .mcu_2_shp_addr = 962, 549b7d2648aSFabio Estevam .app_2_mcu_addr = 685, 550b7d2648aSFabio Estevam .shp_2_mcu_addr = 893, 551b7d2648aSFabio Estevam .spdif_2_mcu_addr = 1102, 552b7d2648aSFabio Estevam .mcu_2_spdif_addr = 1136, 553b7d2648aSFabio Estevam }; 554b7d2648aSFabio Estevam 555b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = { 556b7d2648aSFabio Estevam .chnenbl0 = SDMA_CHNENBL0_IMX35, 557b7d2648aSFabio Estevam .num_events = 48, 558b7d2648aSFabio Estevam .script_addrs = &sdma_script_imx7d, 559b7d2648aSFabio Estevam }; 560b7d2648aSFabio Estevam 561941acd56SAngus Ainslie (Purism) static struct sdma_driver_data sdma_imx8mq = { 562941acd56SAngus Ainslie (Purism) .chnenbl0 = SDMA_CHNENBL0_IMX35, 563941acd56SAngus Ainslie (Purism) .num_events = 48, 564941acd56SAngus Ainslie (Purism) .script_addrs = &sdma_script_imx7d, 565941acd56SAngus Ainslie (Purism) .check_ratio = 1, 566941acd56SAngus Ainslie (Purism) }; 567941acd56SAngus Ainslie (Purism) 568afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = { 56962550cd7SShawn Guo { 570dcfec3c0SSascha Hauer .name = "imx25-sdma", 571dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx25, 572dcfec3c0SSascha Hauer }, { 57362550cd7SShawn Guo .name = "imx31-sdma", 57417bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx31, 57562550cd7SShawn Guo }, { 57662550cd7SShawn Guo .name = "imx35-sdma", 57717bba72fSSascha Hauer .driver_data = (unsigned long)&sdma_imx35, 57862550cd7SShawn Guo }, { 579dcfec3c0SSascha Hauer .name = "imx51-sdma", 580dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx51, 581dcfec3c0SSascha Hauer }, { 582dcfec3c0SSascha Hauer .name = "imx53-sdma", 583dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx53, 584dcfec3c0SSascha Hauer }, { 585dcfec3c0SSascha Hauer .name = "imx6q-sdma", 586dcfec3c0SSascha Hauer .driver_data = (unsigned long)&sdma_imx6q, 587dcfec3c0SSascha Hauer }, { 588b7d2648aSFabio Estevam .name = "imx7d-sdma", 589b7d2648aSFabio Estevam .driver_data = (unsigned long)&sdma_imx7d, 590b7d2648aSFabio Estevam }, { 591941acd56SAngus Ainslie (Purism) .name = "imx8mq-sdma", 592941acd56SAngus Ainslie (Purism) .driver_data = (unsigned long)&sdma_imx8mq, 593941acd56SAngus Ainslie (Purism) }, { 59462550cd7SShawn Guo /* sentinel */ 59562550cd7SShawn Guo } 59662550cd7SShawn Guo }; 59762550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes); 59862550cd7SShawn Guo 599580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = { 600dcfec3c0SSascha Hauer { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, }, 601dcfec3c0SSascha Hauer { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, }, 602dcfec3c0SSascha Hauer { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, }, 60317bba72fSSascha Hauer { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, }, 604dcfec3c0SSascha Hauer { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, }, 60563edea16SMarkus Pargmann { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, }, 606b7d2648aSFabio Estevam { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, }, 607941acd56SAngus Ainslie (Purism) { .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, }, 608580975d7SShawn Guo { /* sentinel */ } 609580975d7SShawn Guo }; 610580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids); 611580975d7SShawn Guo 6120bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */ 6130bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */ 6140bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */ 6151ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 6161ec1e82fSSascha Hauer 6171ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 6181ec1e82fSSascha Hauer { 61917bba72fSSascha Hauer u32 chnenbl0 = sdma->drvdata->chnenbl0; 6201ec1e82fSSascha Hauer return chnenbl0 + event * 4; 6211ec1e82fSSascha Hauer } 6221ec1e82fSSascha Hauer 6231ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 6241ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 6251ec1e82fSSascha Hauer { 6261ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6271ec1e82fSSascha Hauer int channel = sdmac->channel; 6280bbc1413SRichard Zhao unsigned long evt, mcu, dsp; 6291ec1e82fSSascha Hauer 6301ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 6311ec1e82fSSascha Hauer return -EINVAL; 6321ec1e82fSSascha Hauer 633c4b56857SRichard Zhao evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); 634c4b56857SRichard Zhao mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); 635c4b56857SRichard Zhao dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); 6361ec1e82fSSascha Hauer 6371ec1e82fSSascha Hauer if (dsp_override) 6380bbc1413SRichard Zhao __clear_bit(channel, &dsp); 6391ec1e82fSSascha Hauer else 6400bbc1413SRichard Zhao __set_bit(channel, &dsp); 6411ec1e82fSSascha Hauer 6421ec1e82fSSascha Hauer if (event_override) 6430bbc1413SRichard Zhao __clear_bit(channel, &evt); 6441ec1e82fSSascha Hauer else 6450bbc1413SRichard Zhao __set_bit(channel, &evt); 6461ec1e82fSSascha Hauer 6471ec1e82fSSascha Hauer if (mcu_override) 6480bbc1413SRichard Zhao __clear_bit(channel, &mcu); 6491ec1e82fSSascha Hauer else 6500bbc1413SRichard Zhao __set_bit(channel, &mcu); 6511ec1e82fSSascha Hauer 652c4b56857SRichard Zhao writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); 653c4b56857SRichard Zhao writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); 654c4b56857SRichard Zhao writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); 6551ec1e82fSSascha Hauer 6561ec1e82fSSascha Hauer return 0; 6571ec1e82fSSascha Hauer } 6581ec1e82fSSascha Hauer 659b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 660b9a59166SRichard Zhao { 6610bbc1413SRichard Zhao writel(BIT(channel), sdma->regs + SDMA_H_START); 662b9a59166SRichard Zhao } 663b9a59166SRichard Zhao 6641ec1e82fSSascha Hauer /* 6652ccaef05SRichard Zhao * sdma_run_channel0 - run a channel and wait till it's done 6661ec1e82fSSascha Hauer */ 6672ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma) 6681ec1e82fSSascha Hauer { 6691ec1e82fSSascha Hauer int ret; 6701d069bfaSMichael Olbrich u32 reg; 6711ec1e82fSSascha Hauer 6722ccaef05SRichard Zhao sdma_enable_channel(sdma, 0); 6731ec1e82fSSascha Hauer 6741d069bfaSMichael Olbrich ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP, 6751d069bfaSMichael Olbrich reg, !(reg & 1), 1, 500); 6761d069bfaSMichael Olbrich if (ret) 6772ccaef05SRichard Zhao dev_err(sdma->dev, "Timeout waiting for CH0 ready\n"); 6781ec1e82fSSascha Hauer 679855832e4SRobin Gong /* Set bits of CONFIG register with dynamic context switching */ 68025aaa75dSAngus Ainslie (Purism) reg = readl(sdma->regs + SDMA_H_CONFIG); 68125aaa75dSAngus Ainslie (Purism) if ((reg & SDMA_H_CONFIG_CSM) == 0) { 68225aaa75dSAngus Ainslie (Purism) reg |= SDMA_H_CONFIG_CSM; 68325aaa75dSAngus Ainslie (Purism) writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG); 68425aaa75dSAngus Ainslie (Purism) } 685855832e4SRobin Gong 6861d069bfaSMichael Olbrich return ret; 6871ec1e82fSSascha Hauer } 6881ec1e82fSSascha Hauer 6891ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 6901ec1e82fSSascha Hauer u32 address) 6911ec1e82fSSascha Hauer { 69276c33d27SSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->bd0; 6931ec1e82fSSascha Hauer void *buf_virt; 6941ec1e82fSSascha Hauer dma_addr_t buf_phys; 6951ec1e82fSSascha Hauer int ret; 6962ccaef05SRichard Zhao unsigned long flags; 69773eab978SSascha Hauer 698ceaf5226SAndy Duan buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL); 69973eab978SSascha Hauer if (!buf_virt) { 7002ccaef05SRichard Zhao return -ENOMEM; 70173eab978SSascha Hauer } 7021ec1e82fSSascha Hauer 7032ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 7042ccaef05SRichard Zhao 7051ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 7063f93a4f2SRobin Gong bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD; 7071ec1e82fSSascha Hauer bd0->mode.count = size / 2; 7081ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 7091ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 7101ec1e82fSSascha Hauer 7111ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 7121ec1e82fSSascha Hauer 7132ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 7142ccaef05SRichard Zhao 7152ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 7161ec1e82fSSascha Hauer 717ceaf5226SAndy Duan dma_free_coherent(sdma->dev, size, buf_virt, buf_phys); 7181ec1e82fSSascha Hauer 7191ec1e82fSSascha Hauer return ret; 7201ec1e82fSSascha Hauer } 7211ec1e82fSSascha Hauer 7221ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 7231ec1e82fSSascha Hauer { 7241ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7251ec1e82fSSascha Hauer int channel = sdmac->channel; 7260bbc1413SRichard Zhao unsigned long val; 7271ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 7281ec1e82fSSascha Hauer 729c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 7300bbc1413SRichard Zhao __set_bit(channel, &val); 731c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 7321ec1e82fSSascha Hauer } 7331ec1e82fSSascha Hauer 7341ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 7351ec1e82fSSascha Hauer { 7361ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7371ec1e82fSSascha Hauer int channel = sdmac->channel; 7381ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 7390bbc1413SRichard Zhao unsigned long val; 7401ec1e82fSSascha Hauer 741c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 7420bbc1413SRichard Zhao __clear_bit(channel, &val); 743c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 7441ec1e82fSSascha Hauer } 7451ec1e82fSSascha Hauer 74657b772b8SRobin Gong static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t) 74757b772b8SRobin Gong { 74857b772b8SRobin Gong return container_of(t, struct sdma_desc, vd.tx); 74957b772b8SRobin Gong } 75057b772b8SRobin Gong 75157b772b8SRobin Gong static void sdma_start_desc(struct sdma_channel *sdmac) 75257b772b8SRobin Gong { 75357b772b8SRobin Gong struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc); 75457b772b8SRobin Gong struct sdma_desc *desc; 75557b772b8SRobin Gong struct sdma_engine *sdma = sdmac->sdma; 75657b772b8SRobin Gong int channel = sdmac->channel; 75757b772b8SRobin Gong 75857b772b8SRobin Gong if (!vd) { 75957b772b8SRobin Gong sdmac->desc = NULL; 76057b772b8SRobin Gong return; 76157b772b8SRobin Gong } 76257b772b8SRobin Gong sdmac->desc = desc = to_sdma_desc(&vd->tx); 76302939cd1SSascha Hauer 76457b772b8SRobin Gong list_del(&vd->node); 76557b772b8SRobin Gong 76657b772b8SRobin Gong sdma->channel_control[channel].base_bd_ptr = desc->bd_phys; 76757b772b8SRobin Gong sdma->channel_control[channel].current_bd_ptr = desc->bd_phys; 76857b772b8SRobin Gong sdma_enable_channel(sdma, sdmac->channel); 76957b772b8SRobin Gong } 77057b772b8SRobin Gong 771d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac) 772d1a792f3SRussell King - ARM Linux { 7731ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 7745881826dSNandor Han int error = 0; 7755881826dSNandor Han enum dma_status old_status = sdmac->status; 7761ec1e82fSSascha Hauer 7771ec1e82fSSascha Hauer /* 7781ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 7791ec1e82fSSascha Hauer * call callback function. 7801ec1e82fSSascha Hauer */ 78157b772b8SRobin Gong while (sdmac->desc) { 78276c33d27SSascha Hauer struct sdma_desc *desc = sdmac->desc; 78376c33d27SSascha Hauer 78476c33d27SSascha Hauer bd = &desc->bd[desc->buf_tail]; 7851ec1e82fSSascha Hauer 7861ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 7871ec1e82fSSascha Hauer break; 7881ec1e82fSSascha Hauer 7895881826dSNandor Han if (bd->mode.status & BD_RROR) { 7905881826dSNandor Han bd->mode.status &= ~BD_RROR; 7911ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 7925881826dSNandor Han error = -EIO; 7935881826dSNandor Han } 7941ec1e82fSSascha Hauer 7955881826dSNandor Han /* 7965881826dSNandor Han * We use bd->mode.count to calculate the residue, since contains 7975881826dSNandor Han * the number of bytes present in the current buffer descriptor. 7985881826dSNandor Han */ 7995881826dSNandor Han 80076c33d27SSascha Hauer desc->chn_real_count = bd->mode.count; 8011ec1e82fSSascha Hauer bd->mode.status |= BD_DONE; 80276c33d27SSascha Hauer bd->mode.count = desc->period_len; 80376c33d27SSascha Hauer desc->buf_ptail = desc->buf_tail; 80476c33d27SSascha Hauer desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd; 80515f30f51SNandor Han 80615f30f51SNandor Han /* 80715f30f51SNandor Han * The callback is called from the interrupt context in order 80815f30f51SNandor Han * to reduce latency and to avoid the risk of altering the 80915f30f51SNandor Han * SDMA transaction status by the time the client tasklet is 81015f30f51SNandor Han * executed. 81115f30f51SNandor Han */ 81257b772b8SRobin Gong spin_unlock(&sdmac->vc.lock); 81357b772b8SRobin Gong dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL); 81457b772b8SRobin Gong spin_lock(&sdmac->vc.lock); 81515f30f51SNandor Han 8165881826dSNandor Han if (error) 8175881826dSNandor Han sdmac->status = old_status; 8181ec1e82fSSascha Hauer } 8191ec1e82fSSascha Hauer } 8201ec1e82fSSascha Hauer 82157b772b8SRobin Gong static void mxc_sdma_handle_channel_normal(struct sdma_channel *data) 8221ec1e82fSSascha Hauer { 82315f30f51SNandor Han struct sdma_channel *sdmac = (struct sdma_channel *) data; 8241ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 8251ec1e82fSSascha Hauer int i, error = 0; 8261ec1e82fSSascha Hauer 82776c33d27SSascha Hauer sdmac->desc->chn_real_count = 0; 8281ec1e82fSSascha Hauer /* 8291ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 8301ec1e82fSSascha Hauer * errors and call callback function 8311ec1e82fSSascha Hauer */ 83276c33d27SSascha Hauer for (i = 0; i < sdmac->desc->num_bd; i++) { 83376c33d27SSascha Hauer bd = &sdmac->desc->bd[i]; 8341ec1e82fSSascha Hauer 8351ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 8361ec1e82fSSascha Hauer error = -EIO; 83776c33d27SSascha Hauer sdmac->desc->chn_real_count += bd->mode.count; 8381ec1e82fSSascha Hauer } 8391ec1e82fSSascha Hauer 8401ec1e82fSSascha Hauer if (error) 8411ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 8421ec1e82fSSascha Hauer else 843409bff6aSVinod Koul sdmac->status = DMA_COMPLETE; 8441ec1e82fSSascha Hauer } 8451ec1e82fSSascha Hauer 8461ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 8471ec1e82fSSascha Hauer { 8481ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 8490bbc1413SRichard Zhao unsigned long stat; 8501ec1e82fSSascha Hauer 851c4b56857SRichard Zhao stat = readl_relaxed(sdma->regs + SDMA_H_INTR); 852c4b56857SRichard Zhao writel_relaxed(stat, sdma->regs + SDMA_H_INTR); 8531d069bfaSMichael Olbrich /* channel 0 is special and not handled here, see run_channel0() */ 8541d069bfaSMichael Olbrich stat &= ~1; 8551ec1e82fSSascha Hauer 8561ec1e82fSSascha Hauer while (stat) { 8571ec1e82fSSascha Hauer int channel = fls(stat) - 1; 8581ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 85957b772b8SRobin Gong struct sdma_desc *desc; 8601ec1e82fSSascha Hauer 86157b772b8SRobin Gong spin_lock(&sdmac->vc.lock); 86257b772b8SRobin Gong desc = sdmac->desc; 86357b772b8SRobin Gong if (desc) { 86457b772b8SRobin Gong if (sdmac->flags & IMX_DMA_SG_LOOP) { 865d1a792f3SRussell King - ARM Linux sdma_update_channel_loop(sdmac); 86657b772b8SRobin Gong } else { 86757b772b8SRobin Gong mxc_sdma_handle_channel_normal(sdmac); 86857b772b8SRobin Gong vchan_cookie_complete(&desc->vd); 86957b772b8SRobin Gong sdma_start_desc(sdmac); 87057b772b8SRobin Gong } 87157b772b8SRobin Gong } 8721ec1e82fSSascha Hauer 87357b772b8SRobin Gong spin_unlock(&sdmac->vc.lock); 8740bbc1413SRichard Zhao __clear_bit(channel, &stat); 8751ec1e82fSSascha Hauer } 8761ec1e82fSSascha Hauer 8771ec1e82fSSascha Hauer return IRQ_HANDLED; 8781ec1e82fSSascha Hauer } 8791ec1e82fSSascha Hauer 8801ec1e82fSSascha Hauer /* 8811ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 8821ec1e82fSSascha Hauer */ 8831ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac, 8841ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 8851ec1e82fSSascha Hauer { 8861ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8871ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 8881ec1e82fSSascha Hauer /* 8891ec1e82fSSascha Hauer * These are needed once we start to support transfers between 8901ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 8911ec1e82fSSascha Hauer */ 8920f06c027SRobin Gong int per_2_per = 0, emi_2_emi = 0; 8931ec1e82fSSascha Hauer 8941ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 8951ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 8968391ecf4SShengjiu Wang sdmac->device_to_device = 0; 8970f06c027SRobin Gong sdmac->pc_to_pc = 0; 8981ec1e82fSSascha Hauer 8991ec1e82fSSascha Hauer switch (peripheral_type) { 9001ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 9010f06c027SRobin Gong emi_2_emi = sdma->script_addrs->ap_2_ap_addr; 9021ec1e82fSSascha Hauer break; 9031ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 9041ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 9051ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 9061ec1e82fSSascha Hauer break; 9071ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 9081ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 9091ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 9101ec1e82fSSascha Hauer break; 9111ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 9121ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 9131ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 9141ec1e82fSSascha Hauer break; 9151ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 9161ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 9171ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 9181ec1e82fSSascha Hauer break; 9191ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 9201ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 9211ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 9221ec1e82fSSascha Hauer break; 9231ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 9241ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 9251ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 92629aebfdeSNicolin Chen case IMX_DMATYPE_SAI: 9271ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 9281ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 9291ec1e82fSSascha Hauer break; 9301a895578SNicolin Chen case IMX_DMATYPE_SSI_DUAL: 9311a895578SNicolin Chen per_2_emi = sdma->script_addrs->ssish_2_mcu_addr; 9321a895578SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_ssish_addr; 9331a895578SNicolin Chen break; 9341ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 9351ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 9361ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 9371ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 9381ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 9391ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 9401ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 9411ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 9421ec1e82fSSascha Hauer break; 9431ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 9441ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 9451ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 9461ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 9471ec1e82fSSascha Hauer break; 948f892afb0SNicolin Chen case IMX_DMATYPE_ASRC_SP: 949f892afb0SNicolin Chen per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 950f892afb0SNicolin Chen emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 951f892afb0SNicolin Chen per_2_per = sdma->script_addrs->per_2_per_addr; 952f892afb0SNicolin Chen break; 9531ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 9541ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 9551ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 9561ec1e82fSSascha Hauer break; 9571ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 9581ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 9591ec1e82fSSascha Hauer break; 9601ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 9611ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 9621ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 9631ec1e82fSSascha Hauer break; 9641ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 9651ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 9661ec1e82fSSascha Hauer break; 9671ec1e82fSSascha Hauer default: 9681ec1e82fSSascha Hauer break; 9691ec1e82fSSascha Hauer } 9701ec1e82fSSascha Hauer 9711ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 9721ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 9738391ecf4SShengjiu Wang sdmac->device_to_device = per_2_per; 9740f06c027SRobin Gong sdmac->pc_to_pc = emi_2_emi; 9751ec1e82fSSascha Hauer } 9761ec1e82fSSascha Hauer 9771ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 9781ec1e82fSSascha Hauer { 9791ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9801ec1e82fSSascha Hauer int channel = sdmac->channel; 9811ec1e82fSSascha Hauer int load_address; 9821ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 98376c33d27SSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->bd0; 9841ec1e82fSSascha Hauer int ret; 9852ccaef05SRichard Zhao unsigned long flags; 9861ec1e82fSSascha Hauer 987ad0d92d7SRobin Gong if (sdmac->context_loaded) 988ad0d92d7SRobin Gong return 0; 989ad0d92d7SRobin Gong 9908391ecf4SShengjiu Wang if (sdmac->direction == DMA_DEV_TO_MEM) 9911ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 9928391ecf4SShengjiu Wang else if (sdmac->direction == DMA_DEV_TO_DEV) 9938391ecf4SShengjiu Wang load_address = sdmac->device_to_device; 9940f06c027SRobin Gong else if (sdmac->direction == DMA_MEM_TO_MEM) 9950f06c027SRobin Gong load_address = sdmac->pc_to_pc; 9968391ecf4SShengjiu Wang else 9971ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 9981ec1e82fSSascha Hauer 9991ec1e82fSSascha Hauer if (load_address < 0) 10001ec1e82fSSascha Hauer return load_address; 10011ec1e82fSSascha Hauer 10021ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 10030bbc1413SRichard Zhao dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level); 10041ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 10051ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 10060bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]); 10070bbc1413SRichard Zhao dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]); 10081ec1e82fSSascha Hauer 10092ccaef05SRichard Zhao spin_lock_irqsave(&sdma->channel_0_lock, flags); 101073eab978SSascha Hauer 10111ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 10121ec1e82fSSascha Hauer context->channel_state.pc = load_address; 10131ec1e82fSSascha Hauer 10141ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 10151ec1e82fSSascha Hauer * and watermark level 10161ec1e82fSSascha Hauer */ 10170bbc1413SRichard Zhao context->gReg[0] = sdmac->event_mask[1]; 10180bbc1413SRichard Zhao context->gReg[1] = sdmac->event_mask[0]; 10191ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 10201ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 10211ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 10221ec1e82fSSascha Hauer 10231ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 10243f93a4f2SRobin Gong bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD; 10251ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 10261ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 10271ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 10282ccaef05SRichard Zhao ret = sdma_run_channel0(sdma); 10291ec1e82fSSascha Hauer 10302ccaef05SRichard Zhao spin_unlock_irqrestore(&sdma->channel_0_lock, flags); 103173eab978SSascha Hauer 1032ad0d92d7SRobin Gong sdmac->context_loaded = true; 1033ad0d92d7SRobin Gong 10341ec1e82fSSascha Hauer return ret; 10351ec1e82fSSascha Hauer } 10361ec1e82fSSascha Hauer 10377b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 10381ec1e82fSSascha Hauer { 103957b772b8SRobin Gong return container_of(chan, struct sdma_channel, vc.chan); 10407b350ab0SMaxime Ripard } 10417b350ab0SMaxime Ripard 10427b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan) 10437b350ab0SMaxime Ripard { 10447b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 10451ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10461ec1e82fSSascha Hauer int channel = sdmac->channel; 10471ec1e82fSSascha Hauer 10480bbc1413SRichard Zhao writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP); 10491ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 10507b350ab0SMaxime Ripard 10517b350ab0SMaxime Ripard return 0; 10521ec1e82fSSascha Hauer } 1053b8603d2aSLucas Stach static void sdma_channel_terminate_work(struct work_struct *work) 10547f3ff14bSJiada Wang { 1055b8603d2aSLucas Stach struct sdma_channel *sdmac = container_of(work, struct sdma_channel, 1056b8603d2aSLucas Stach terminate_worker); 105757b772b8SRobin Gong unsigned long flags; 105857b772b8SRobin Gong LIST_HEAD(head); 105957b772b8SRobin Gong 10607f3ff14bSJiada Wang /* 10617f3ff14bSJiada Wang * According to NXP R&D team a delay of one BD SDMA cost time 10627f3ff14bSJiada Wang * (maximum is 1ms) should be added after disable of the channel 10637f3ff14bSJiada Wang * bit, to ensure SDMA core has really been stopped after SDMA 10647f3ff14bSJiada Wang * clients call .device_terminate_all. 10657f3ff14bSJiada Wang */ 1066b8603d2aSLucas Stach usleep_range(1000, 2000); 1067b8603d2aSLucas Stach 1068b8603d2aSLucas Stach spin_lock_irqsave(&sdmac->vc.lock, flags); 1069b8603d2aSLucas Stach vchan_get_all_descriptors(&sdmac->vc, &head); 1070b8603d2aSLucas Stach spin_unlock_irqrestore(&sdmac->vc.lock, flags); 1071b8603d2aSLucas Stach vchan_dma_desc_free_list(&sdmac->vc, &head); 1072ad0d92d7SRobin Gong sdmac->context_loaded = false; 1073b8603d2aSLucas Stach } 1074b8603d2aSLucas Stach 1075a80f2787SSascha Hauer static int sdma_terminate_all(struct dma_chan *chan) 1076b8603d2aSLucas Stach { 1077b8603d2aSLucas Stach struct sdma_channel *sdmac = to_sdma_chan(chan); 107802939cd1SSascha Hauer unsigned long flags; 107902939cd1SSascha Hauer 108002939cd1SSascha Hauer spin_lock_irqsave(&sdmac->vc.lock, flags); 1081b8603d2aSLucas Stach 1082b8603d2aSLucas Stach sdma_disable_channel(chan); 1083b8603d2aSLucas Stach 108402939cd1SSascha Hauer if (sdmac->desc) { 108502939cd1SSascha Hauer vchan_terminate_vdesc(&sdmac->desc->vd); 108602939cd1SSascha Hauer sdmac->desc = NULL; 1087b8603d2aSLucas Stach schedule_work(&sdmac->terminate_worker); 108802939cd1SSascha Hauer } 108902939cd1SSascha Hauer 109002939cd1SSascha Hauer spin_unlock_irqrestore(&sdmac->vc.lock, flags); 10917f3ff14bSJiada Wang 10927f3ff14bSJiada Wang return 0; 10937f3ff14bSJiada Wang } 10947f3ff14bSJiada Wang 1095b8603d2aSLucas Stach static void sdma_channel_synchronize(struct dma_chan *chan) 1096b8603d2aSLucas Stach { 1097b8603d2aSLucas Stach struct sdma_channel *sdmac = to_sdma_chan(chan); 1098b8603d2aSLucas Stach 1099b8603d2aSLucas Stach vchan_synchronize(&sdmac->vc); 1100b8603d2aSLucas Stach 1101b8603d2aSLucas Stach flush_work(&sdmac->terminate_worker); 1102b8603d2aSLucas Stach } 1103b8603d2aSLucas Stach 11048391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac) 11058391ecf4SShengjiu Wang { 11068391ecf4SShengjiu Wang struct sdma_engine *sdma = sdmac->sdma; 11078391ecf4SShengjiu Wang 11088391ecf4SShengjiu Wang int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML; 11098391ecf4SShengjiu Wang int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16; 11108391ecf4SShengjiu Wang 11118391ecf4SShengjiu Wang set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]); 11128391ecf4SShengjiu Wang set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]); 11138391ecf4SShengjiu Wang 11148391ecf4SShengjiu Wang if (sdmac->event_id0 > 31) 11158391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE; 11168391ecf4SShengjiu Wang 11178391ecf4SShengjiu Wang if (sdmac->event_id1 > 31) 11188391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE; 11198391ecf4SShengjiu Wang 11208391ecf4SShengjiu Wang /* 11218391ecf4SShengjiu Wang * If LWML(src_maxburst) > HWML(dst_maxburst), we need 11228391ecf4SShengjiu Wang * swap LWML and HWML of INFO(A.3.2.5.1), also need swap 11238391ecf4SShengjiu Wang * r0(event_mask[1]) and r1(event_mask[0]). 11248391ecf4SShengjiu Wang */ 11258391ecf4SShengjiu Wang if (lwml > hwml) { 11268391ecf4SShengjiu Wang sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML | 11278391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML); 11288391ecf4SShengjiu Wang sdmac->watermark_level |= hwml; 11298391ecf4SShengjiu Wang sdmac->watermark_level |= lwml << 16; 11308391ecf4SShengjiu Wang swap(sdmac->event_mask[0], sdmac->event_mask[1]); 11318391ecf4SShengjiu Wang } 11328391ecf4SShengjiu Wang 11338391ecf4SShengjiu Wang if (sdmac->per_address2 >= sdma->spba_start_addr && 11348391ecf4SShengjiu Wang sdmac->per_address2 <= sdma->spba_end_addr) 11358391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP; 11368391ecf4SShengjiu Wang 11378391ecf4SShengjiu Wang if (sdmac->per_address >= sdma->spba_start_addr && 11388391ecf4SShengjiu Wang sdmac->per_address <= sdma->spba_end_addr) 11398391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP; 11408391ecf4SShengjiu Wang 11418391ecf4SShengjiu Wang sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT; 11428391ecf4SShengjiu Wang } 11438391ecf4SShengjiu Wang 11447b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan) 11451ec1e82fSSascha Hauer { 11467b350ab0SMaxime Ripard struct sdma_channel *sdmac = to_sdma_chan(chan); 11471ec1e82fSSascha Hauer int ret; 11481ec1e82fSSascha Hauer 11497b350ab0SMaxime Ripard sdma_disable_channel(chan); 11501ec1e82fSSascha Hauer 11510bbc1413SRichard Zhao sdmac->event_mask[0] = 0; 11520bbc1413SRichard Zhao sdmac->event_mask[1] = 0; 11531ec1e82fSSascha Hauer sdmac->shp_addr = 0; 11541ec1e82fSSascha Hauer sdmac->per_addr = 0; 11551ec1e82fSSascha Hauer 11561ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 11571ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 11581ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 11591ec1e82fSSascha Hauer break; 11601ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 11611ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 11621ec1e82fSSascha Hauer break; 11631ec1e82fSSascha Hauer default: 11641ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 11651ec1e82fSSascha Hauer break; 11661ec1e82fSSascha Hauer } 11671ec1e82fSSascha Hauer 11681ec1e82fSSascha Hauer sdma_get_pc(sdmac, sdmac->peripheral_type); 11691ec1e82fSSascha Hauer 11701ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 11711ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 11721ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 11731ec1e82fSSascha Hauer if (sdmac->event_id1) { 11748391ecf4SShengjiu Wang if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP || 11758391ecf4SShengjiu Wang sdmac->peripheral_type == IMX_DMATYPE_ASRC) 11768391ecf4SShengjiu Wang sdma_set_watermarklevel_for_p2p(sdmac); 11778391ecf4SShengjiu Wang } else 11780bbc1413SRichard Zhao __set_bit(sdmac->event_id0, sdmac->event_mask); 11798391ecf4SShengjiu Wang 11801ec1e82fSSascha Hauer /* Address */ 11811ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 11828391ecf4SShengjiu Wang sdmac->per_addr = sdmac->per_address2; 11831ec1e82fSSascha Hauer } else { 11841ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 11851ec1e82fSSascha Hauer } 11861ec1e82fSSascha Hauer 11871ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 11881ec1e82fSSascha Hauer 11891ec1e82fSSascha Hauer return ret; 11901ec1e82fSSascha Hauer } 11911ec1e82fSSascha Hauer 11921ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 11931ec1e82fSSascha Hauer unsigned int priority) 11941ec1e82fSSascha Hauer { 11951ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11961ec1e82fSSascha Hauer int channel = sdmac->channel; 11971ec1e82fSSascha Hauer 11981ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 11991ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 12001ec1e82fSSascha Hauer return -EINVAL; 12011ec1e82fSSascha Hauer } 12021ec1e82fSSascha Hauer 1203c4b56857SRichard Zhao writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 12041ec1e82fSSascha Hauer 12051ec1e82fSSascha Hauer return 0; 12061ec1e82fSSascha Hauer } 12071ec1e82fSSascha Hauer 120857b772b8SRobin Gong static int sdma_request_channel0(struct sdma_engine *sdma) 12091ec1e82fSSascha Hauer { 12101ec1e82fSSascha Hauer int ret = -EBUSY; 12111ec1e82fSSascha Hauer 121231ef489aSLinus Torvalds sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys, 121357b772b8SRobin Gong GFP_NOWAIT); 121457b772b8SRobin Gong if (!sdma->bd0) { 12151ec1e82fSSascha Hauer ret = -ENOMEM; 12161ec1e82fSSascha Hauer goto out; 12171ec1e82fSSascha Hauer } 12181ec1e82fSSascha Hauer 121957b772b8SRobin Gong sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys; 122057b772b8SRobin Gong sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys; 12211ec1e82fSSascha Hauer 122257b772b8SRobin Gong sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY); 12231ec1e82fSSascha Hauer return 0; 12241ec1e82fSSascha Hauer out: 12251ec1e82fSSascha Hauer 12261ec1e82fSSascha Hauer return ret; 12271ec1e82fSSascha Hauer } 12281ec1e82fSSascha Hauer 122957b772b8SRobin Gong 123057b772b8SRobin Gong static int sdma_alloc_bd(struct sdma_desc *desc) 12311ec1e82fSSascha Hauer { 1232ebb853b1SLucas Stach u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor); 123357b772b8SRobin Gong int ret = 0; 12341ec1e82fSSascha Hauer 123531ef489aSLinus Torvalds desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size, 1236ceaf5226SAndy Duan &desc->bd_phys, GFP_NOWAIT); 123757b772b8SRobin Gong if (!desc->bd) { 123857b772b8SRobin Gong ret = -ENOMEM; 123957b772b8SRobin Gong goto out; 124057b772b8SRobin Gong } 124157b772b8SRobin Gong out: 124257b772b8SRobin Gong return ret; 124357b772b8SRobin Gong } 12441ec1e82fSSascha Hauer 124557b772b8SRobin Gong static void sdma_free_bd(struct sdma_desc *desc) 124657b772b8SRobin Gong { 1247ebb853b1SLucas Stach u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor); 1248ebb853b1SLucas Stach 1249ceaf5226SAndy Duan dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd, 1250ceaf5226SAndy Duan desc->bd_phys); 125157b772b8SRobin Gong } 12521ec1e82fSSascha Hauer 125357b772b8SRobin Gong static void sdma_desc_free(struct virt_dma_desc *vd) 125457b772b8SRobin Gong { 125557b772b8SRobin Gong struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd); 125657b772b8SRobin Gong 125757b772b8SRobin Gong sdma_free_bd(desc); 125857b772b8SRobin Gong kfree(desc); 12591ec1e82fSSascha Hauer } 12601ec1e82fSSascha Hauer 12611ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 12621ec1e82fSSascha Hauer { 12631ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 12641ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 12650f06c027SRobin Gong struct imx_dma_data mem_data; 12661ec1e82fSSascha Hauer int prio, ret; 12671ec1e82fSSascha Hauer 12680f06c027SRobin Gong /* 12690f06c027SRobin Gong * MEMCPY may never setup chan->private by filter function such as 12700f06c027SRobin Gong * dmatest, thus create 'struct imx_dma_data mem_data' for this case. 12710f06c027SRobin Gong * Please note in any other slave case, you have to setup chan->private 12720f06c027SRobin Gong * with 'struct imx_dma_data' in your own filter function if you want to 12730f06c027SRobin Gong * request dma channel by dma_request_channel() rather than 12740f06c027SRobin Gong * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear 12750f06c027SRobin Gong * to warn you to correct your filter function. 12760f06c027SRobin Gong */ 12770f06c027SRobin Gong if (!data) { 12780f06c027SRobin Gong dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n"); 12790f06c027SRobin Gong mem_data.priority = 2; 12800f06c027SRobin Gong mem_data.peripheral_type = IMX_DMATYPE_MEMORY; 12810f06c027SRobin Gong mem_data.dma_request = 0; 12820f06c027SRobin Gong mem_data.dma_request2 = 0; 12830f06c027SRobin Gong data = &mem_data; 12840f06c027SRobin Gong 12850f06c027SRobin Gong sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY); 12860f06c027SRobin Gong } 12871ec1e82fSSascha Hauer 12881ec1e82fSSascha Hauer switch (data->priority) { 12891ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 12901ec1e82fSSascha Hauer prio = 3; 12911ec1e82fSSascha Hauer break; 12921ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 12931ec1e82fSSascha Hauer prio = 2; 12941ec1e82fSSascha Hauer break; 12951ec1e82fSSascha Hauer case DMA_PRIO_LOW: 12961ec1e82fSSascha Hauer default: 12971ec1e82fSSascha Hauer prio = 1; 12981ec1e82fSSascha Hauer break; 12991ec1e82fSSascha Hauer } 13001ec1e82fSSascha Hauer 13011ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 13021ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 13038391ecf4SShengjiu Wang sdmac->event_id1 = data->dma_request2; 1304c2c744d3SRichard Zhao 1305b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ipg); 1306b93edcddSFabio Estevam if (ret) 1307b93edcddSFabio Estevam return ret; 1308b93edcddSFabio Estevam ret = clk_enable(sdmac->sdma->clk_ahb); 1309b93edcddSFabio Estevam if (ret) 1310b93edcddSFabio Estevam goto disable_clk_ipg; 1311c2c744d3SRichard Zhao 13123bb5e7caSRichard Zhao ret = sdma_set_channel_priority(sdmac, prio); 13131ec1e82fSSascha Hauer if (ret) 1314b93edcddSFabio Estevam goto disable_clk_ahb; 13151ec1e82fSSascha Hauer 13161ec1e82fSSascha Hauer return 0; 1317b93edcddSFabio Estevam 1318b93edcddSFabio Estevam disable_clk_ahb: 1319b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ahb); 1320b93edcddSFabio Estevam disable_clk_ipg: 1321b93edcddSFabio Estevam clk_disable(sdmac->sdma->clk_ipg); 1322b93edcddSFabio Estevam return ret; 13231ec1e82fSSascha Hauer } 13241ec1e82fSSascha Hauer 13251ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 13261ec1e82fSSascha Hauer { 13271ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 13281ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 13291ec1e82fSSascha Hauer 1330a80f2787SSascha Hauer sdma_terminate_all(chan); 1331b8603d2aSLucas Stach 1332b8603d2aSLucas Stach sdma_channel_synchronize(chan); 13331ec1e82fSSascha Hauer 133425962e1aSFrieder Schrempf if (sdmac->event_id0 >= 0) 13351ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 13361ec1e82fSSascha Hauer if (sdmac->event_id1) 13371ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 13381ec1e82fSSascha Hauer 13391ec1e82fSSascha Hauer sdmac->event_id0 = 0; 13401ec1e82fSSascha Hauer sdmac->event_id1 = 0; 1341d288bdddSMartin Fuzzey sdmac->context_loaded = false; 13421ec1e82fSSascha Hauer 13431ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 13441ec1e82fSSascha Hauer 13457560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 13467560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 13471ec1e82fSSascha Hauer } 13481ec1e82fSSascha Hauer 134921420841SRobin Gong static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac, 135021420841SRobin Gong enum dma_transfer_direction direction, u32 bds) 135121420841SRobin Gong { 135221420841SRobin Gong struct sdma_desc *desc; 135321420841SRobin Gong 135421420841SRobin Gong desc = kzalloc((sizeof(*desc)), GFP_NOWAIT); 135521420841SRobin Gong if (!desc) 135621420841SRobin Gong goto err_out; 135721420841SRobin Gong 135821420841SRobin Gong sdmac->status = DMA_IN_PROGRESS; 135921420841SRobin Gong sdmac->direction = direction; 136021420841SRobin Gong sdmac->flags = 0; 136121420841SRobin Gong 136221420841SRobin Gong desc->chn_count = 0; 136321420841SRobin Gong desc->chn_real_count = 0; 136421420841SRobin Gong desc->buf_tail = 0; 136521420841SRobin Gong desc->buf_ptail = 0; 136621420841SRobin Gong desc->sdmac = sdmac; 136721420841SRobin Gong desc->num_bd = bds; 136821420841SRobin Gong 136921420841SRobin Gong if (sdma_alloc_bd(desc)) 137021420841SRobin Gong goto err_desc_out; 137121420841SRobin Gong 13720f06c027SRobin Gong /* No slave_config called in MEMCPY case, so do here */ 13730f06c027SRobin Gong if (direction == DMA_MEM_TO_MEM) 13740f06c027SRobin Gong sdma_config_ownership(sdmac, false, true, false); 13750f06c027SRobin Gong 137621420841SRobin Gong if (sdma_load_context(sdmac)) 137721420841SRobin Gong goto err_desc_out; 137821420841SRobin Gong 137921420841SRobin Gong return desc; 138021420841SRobin Gong 138121420841SRobin Gong err_desc_out: 138221420841SRobin Gong kfree(desc); 138321420841SRobin Gong err_out: 138421420841SRobin Gong return NULL; 138521420841SRobin Gong } 138621420841SRobin Gong 13870f06c027SRobin Gong static struct dma_async_tx_descriptor *sdma_prep_memcpy( 13880f06c027SRobin Gong struct dma_chan *chan, dma_addr_t dma_dst, 13890f06c027SRobin Gong dma_addr_t dma_src, size_t len, unsigned long flags) 13900f06c027SRobin Gong { 13910f06c027SRobin Gong struct sdma_channel *sdmac = to_sdma_chan(chan); 13920f06c027SRobin Gong struct sdma_engine *sdma = sdmac->sdma; 13930f06c027SRobin Gong int channel = sdmac->channel; 13940f06c027SRobin Gong size_t count; 13950f06c027SRobin Gong int i = 0, param; 13960f06c027SRobin Gong struct sdma_buffer_descriptor *bd; 13970f06c027SRobin Gong struct sdma_desc *desc; 13980f06c027SRobin Gong 13990f06c027SRobin Gong if (!chan || !len) 14000f06c027SRobin Gong return NULL; 14010f06c027SRobin Gong 14020f06c027SRobin Gong dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n", 14030f06c027SRobin Gong &dma_src, &dma_dst, len, channel); 14040f06c027SRobin Gong 14050f06c027SRobin Gong desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM, 14060f06c027SRobin Gong len / SDMA_BD_MAX_CNT + 1); 14070f06c027SRobin Gong if (!desc) 14080f06c027SRobin Gong return NULL; 14090f06c027SRobin Gong 14100f06c027SRobin Gong do { 14110f06c027SRobin Gong count = min_t(size_t, len, SDMA_BD_MAX_CNT); 14120f06c027SRobin Gong bd = &desc->bd[i]; 14130f06c027SRobin Gong bd->buffer_addr = dma_src; 14140f06c027SRobin Gong bd->ext_buffer_addr = dma_dst; 14150f06c027SRobin Gong bd->mode.count = count; 14160f06c027SRobin Gong desc->chn_count += count; 14170f06c027SRobin Gong bd->mode.command = 0; 14180f06c027SRobin Gong 14190f06c027SRobin Gong dma_src += count; 14200f06c027SRobin Gong dma_dst += count; 14210f06c027SRobin Gong len -= count; 14220f06c027SRobin Gong i++; 14230f06c027SRobin Gong 14240f06c027SRobin Gong param = BD_DONE | BD_EXTD | BD_CONT; 14250f06c027SRobin Gong /* last bd */ 14260f06c027SRobin Gong if (!len) { 14270f06c027SRobin Gong param |= BD_INTR; 14280f06c027SRobin Gong param |= BD_LAST; 14290f06c027SRobin Gong param &= ~BD_CONT; 14300f06c027SRobin Gong } 14310f06c027SRobin Gong 14320f06c027SRobin Gong dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n", 14330f06c027SRobin Gong i, count, bd->buffer_addr, 14340f06c027SRobin Gong param & BD_WRAP ? "wrap" : "", 14350f06c027SRobin Gong param & BD_INTR ? " intr" : ""); 14360f06c027SRobin Gong 14370f06c027SRobin Gong bd->mode.status = param; 14380f06c027SRobin Gong } while (len); 14390f06c027SRobin Gong 14400f06c027SRobin Gong return vchan_tx_prep(&sdmac->vc, &desc->vd, flags); 14410f06c027SRobin Gong } 14420f06c027SRobin Gong 14431ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 14441ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 1445db8196dfSVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 1446185ecb5fSAlexandre Bounine unsigned long flags, void *context) 14471ec1e82fSSascha Hauer { 14481ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 14491ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 1450ad78b000SVinod Koul int i, count; 145123889c63SSascha Hauer int channel = sdmac->channel; 14521ec1e82fSSascha Hauer struct scatterlist *sg; 145357b772b8SRobin Gong struct sdma_desc *desc; 14541ec1e82fSSascha Hauer 1455107d0644SVinod Koul sdma_config_write(chan, &sdmac->slave_config, direction); 1456107d0644SVinod Koul 145721420841SRobin Gong desc = sdma_transfer_init(sdmac, direction, sg_len); 145857b772b8SRobin Gong if (!desc) 145957b772b8SRobin Gong goto err_out; 146057b772b8SRobin Gong 14611ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 14621ec1e82fSSascha Hauer sg_len, channel); 14631ec1e82fSSascha Hauer 14641ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 146576c33d27SSascha Hauer struct sdma_buffer_descriptor *bd = &desc->bd[i]; 14661ec1e82fSSascha Hauer int param; 14671ec1e82fSSascha Hauer 1468d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 14691ec1e82fSSascha Hauer 1470fdaf9c4bSLars-Peter Clausen count = sg_dma_len(sg); 14711ec1e82fSSascha Hauer 14724a6b2e8aSRobin Gong if (count > SDMA_BD_MAX_CNT) { 14731ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 14744a6b2e8aSRobin Gong channel, count, SDMA_BD_MAX_CNT); 147557b772b8SRobin Gong goto err_bd_out; 14761ec1e82fSSascha Hauer } 14771ec1e82fSSascha Hauer 14781ec1e82fSSascha Hauer bd->mode.count = count; 147976c33d27SSascha Hauer desc->chn_count += count; 14801ec1e82fSSascha Hauer 1481ad78b000SVinod Koul if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 148257b772b8SRobin Gong goto err_bd_out; 14831fa81c27SSascha Hauer 14841fa81c27SSascha Hauer switch (sdmac->word_size) { 14851fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 14861ec1e82fSSascha Hauer bd->mode.command = 0; 14871fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 148857b772b8SRobin Gong goto err_bd_out; 14891fa81c27SSascha Hauer break; 14901fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 14911fa81c27SSascha Hauer bd->mode.command = 2; 14921fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 149357b772b8SRobin Gong goto err_bd_out; 14941fa81c27SSascha Hauer break; 14951fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 14961fa81c27SSascha Hauer bd->mode.command = 1; 14971fa81c27SSascha Hauer break; 14981fa81c27SSascha Hauer default: 149957b772b8SRobin Gong goto err_bd_out; 15001fa81c27SSascha Hauer } 15011ec1e82fSSascha Hauer 15021ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 15031ec1e82fSSascha Hauer 1504341b9419SShawn Guo if (i + 1 == sg_len) { 15051ec1e82fSSascha Hauer param |= BD_INTR; 1506341b9419SShawn Guo param |= BD_LAST; 1507341b9419SShawn Guo param &= ~BD_CONT; 15081ec1e82fSSascha Hauer } 15091ec1e82fSSascha Hauer 1510c3cc74b2SOlof Johansson dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n", 1511c3cc74b2SOlof Johansson i, count, (u64)sg->dma_address, 15121ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 15131ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 15141ec1e82fSSascha Hauer 15151ec1e82fSSascha Hauer bd->mode.status = param; 15161ec1e82fSSascha Hauer } 15171ec1e82fSSascha Hauer 151857b772b8SRobin Gong return vchan_tx_prep(&sdmac->vc, &desc->vd, flags); 151957b772b8SRobin Gong err_bd_out: 152057b772b8SRobin Gong sdma_free_bd(desc); 152157b772b8SRobin Gong kfree(desc); 15221ec1e82fSSascha Hauer err_out: 15234b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 15241ec1e82fSSascha Hauer return NULL; 15251ec1e82fSSascha Hauer } 15261ec1e82fSSascha Hauer 15271ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 15281ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 1529185ecb5fSAlexandre Bounine size_t period_len, enum dma_transfer_direction direction, 153031c1e5a1SLaurent Pinchart unsigned long flags) 15311ec1e82fSSascha Hauer { 15321ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 15331ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 15341ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 153523889c63SSascha Hauer int channel = sdmac->channel; 153621420841SRobin Gong int i = 0, buf = 0; 153757b772b8SRobin Gong struct sdma_desc *desc; 15381ec1e82fSSascha Hauer 15391ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 15401ec1e82fSSascha Hauer 1541107d0644SVinod Koul sdma_config_write(chan, &sdmac->slave_config, direction); 1542107d0644SVinod Koul 154321420841SRobin Gong desc = sdma_transfer_init(sdmac, direction, num_periods); 154457b772b8SRobin Gong if (!desc) 154557b772b8SRobin Gong goto err_out; 154657b772b8SRobin Gong 154776c33d27SSascha Hauer desc->period_len = period_len; 15488e2e27c7SRichard Zhao 15491ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 15501ec1e82fSSascha Hauer 15514a6b2e8aSRobin Gong if (period_len > SDMA_BD_MAX_CNT) { 1552ba6ab3b3SArvind Yadav dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n", 15534a6b2e8aSRobin Gong channel, period_len, SDMA_BD_MAX_CNT); 155457b772b8SRobin Gong goto err_bd_out; 15551ec1e82fSSascha Hauer } 15561ec1e82fSSascha Hauer 15571ec1e82fSSascha Hauer while (buf < buf_len) { 155876c33d27SSascha Hauer struct sdma_buffer_descriptor *bd = &desc->bd[i]; 15591ec1e82fSSascha Hauer int param; 15601ec1e82fSSascha Hauer 15611ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 15621ec1e82fSSascha Hauer 15631ec1e82fSSascha Hauer bd->mode.count = period_len; 15641ec1e82fSSascha Hauer 15651ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 156657b772b8SRobin Gong goto err_bd_out; 15671ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 15681ec1e82fSSascha Hauer bd->mode.command = 0; 15691ec1e82fSSascha Hauer else 15701ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 15711ec1e82fSSascha Hauer 15721ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 15731ec1e82fSSascha Hauer if (i + 1 == num_periods) 15741ec1e82fSSascha Hauer param |= BD_WRAP; 15751ec1e82fSSascha Hauer 1576ba6ab3b3SArvind Yadav dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n", 1577c3cc74b2SOlof Johansson i, period_len, (u64)dma_addr, 15781ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 15791ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 15801ec1e82fSSascha Hauer 15811ec1e82fSSascha Hauer bd->mode.status = param; 15821ec1e82fSSascha Hauer 15831ec1e82fSSascha Hauer dma_addr += period_len; 15841ec1e82fSSascha Hauer buf += period_len; 15851ec1e82fSSascha Hauer 15861ec1e82fSSascha Hauer i++; 15871ec1e82fSSascha Hauer } 15881ec1e82fSSascha Hauer 158957b772b8SRobin Gong return vchan_tx_prep(&sdmac->vc, &desc->vd, flags); 159057b772b8SRobin Gong err_bd_out: 159157b772b8SRobin Gong sdma_free_bd(desc); 159257b772b8SRobin Gong kfree(desc); 15931ec1e82fSSascha Hauer err_out: 15941ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 15951ec1e82fSSascha Hauer return NULL; 15961ec1e82fSSascha Hauer } 15971ec1e82fSSascha Hauer 1598107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan, 1599107d0644SVinod Koul struct dma_slave_config *dmaengine_cfg, 1600107d0644SVinod Koul enum dma_transfer_direction direction) 16011ec1e82fSSascha Hauer { 16021ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 16031ec1e82fSSascha Hauer 1604107d0644SVinod Koul if (direction == DMA_DEV_TO_MEM) { 16051ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 160694ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->src_maxburst * 160794ac27a5SPhilippe Rétornaz dmaengine_cfg->src_addr_width; 16081ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 1609107d0644SVinod Koul } else if (direction == DMA_DEV_TO_DEV) { 16108391ecf4SShengjiu Wang sdmac->per_address2 = dmaengine_cfg->src_addr; 16118391ecf4SShengjiu Wang sdmac->per_address = dmaengine_cfg->dst_addr; 16128391ecf4SShengjiu Wang sdmac->watermark_level = dmaengine_cfg->src_maxburst & 16138391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_LWML; 16148391ecf4SShengjiu Wang sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) & 16158391ecf4SShengjiu Wang SDMA_WATERMARK_LEVEL_HWML; 16168391ecf4SShengjiu Wang sdmac->word_size = dmaengine_cfg->dst_addr_width; 16171ec1e82fSSascha Hauer } else { 16181ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 161994ac27a5SPhilippe Rétornaz sdmac->watermark_level = dmaengine_cfg->dst_maxburst * 162094ac27a5SPhilippe Rétornaz dmaengine_cfg->dst_addr_width; 16211ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 16221ec1e82fSSascha Hauer } 1623107d0644SVinod Koul sdmac->direction = direction; 16247b350ab0SMaxime Ripard return sdma_config_channel(chan); 16251ec1e82fSSascha Hauer } 16261ec1e82fSSascha Hauer 1627107d0644SVinod Koul static int sdma_config(struct dma_chan *chan, 1628107d0644SVinod Koul struct dma_slave_config *dmaengine_cfg) 1629107d0644SVinod Koul { 1630107d0644SVinod Koul struct sdma_channel *sdmac = to_sdma_chan(chan); 1631107d0644SVinod Koul 1632107d0644SVinod Koul memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg)); 1633107d0644SVinod Koul 1634107d0644SVinod Koul /* Set ENBLn earlier to make sure dma request triggered after that */ 163525962e1aSFrieder Schrempf if (sdmac->event_id0 >= 0) { 1636107d0644SVinod Koul if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events) 1637107d0644SVinod Koul return -EINVAL; 1638107d0644SVinod Koul sdma_event_enable(sdmac, sdmac->event_id0); 1639107d0644SVinod Koul } 1640107d0644SVinod Koul 1641107d0644SVinod Koul if (sdmac->event_id1) { 1642107d0644SVinod Koul if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events) 1643107d0644SVinod Koul return -EINVAL; 1644107d0644SVinod Koul sdma_event_enable(sdmac, sdmac->event_id1); 1645107d0644SVinod Koul } 1646107d0644SVinod Koul 1647107d0644SVinod Koul return 0; 1648107d0644SVinod Koul } 1649107d0644SVinod Koul 16501ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 16511ec1e82fSSascha Hauer dma_cookie_t cookie, 16521ec1e82fSSascha Hauer struct dma_tx_state *txstate) 16531ec1e82fSSascha Hauer { 16541ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 1655a1ff6a07SSascha Hauer struct sdma_desc *desc = NULL; 1656d1a792f3SRussell King - ARM Linux u32 residue; 165757b772b8SRobin Gong struct virt_dma_desc *vd; 165857b772b8SRobin Gong enum dma_status ret; 165957b772b8SRobin Gong unsigned long flags; 1660d1a792f3SRussell King - ARM Linux 166157b772b8SRobin Gong ret = dma_cookie_status(chan, cookie, txstate); 166257b772b8SRobin Gong if (ret == DMA_COMPLETE || !txstate) 166357b772b8SRobin Gong return ret; 166457b772b8SRobin Gong 166557b772b8SRobin Gong spin_lock_irqsave(&sdmac->vc.lock, flags); 1666a1ff6a07SSascha Hauer 166757b772b8SRobin Gong vd = vchan_find_desc(&sdmac->vc, cookie); 1668a1ff6a07SSascha Hauer if (vd) 166957b772b8SRobin Gong desc = to_sdma_desc(&vd->tx); 1670a1ff6a07SSascha Hauer else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) 1671a1ff6a07SSascha Hauer desc = sdmac->desc; 1672a1ff6a07SSascha Hauer 1673a1ff6a07SSascha Hauer if (desc) { 1674d1a792f3SRussell King - ARM Linux if (sdmac->flags & IMX_DMA_SG_LOOP) 167576c33d27SSascha Hauer residue = (desc->num_bd - desc->buf_ptail) * 167676c33d27SSascha Hauer desc->period_len - desc->chn_real_count; 1677d1a792f3SRussell King - ARM Linux else 167876c33d27SSascha Hauer residue = desc->chn_count - desc->chn_real_count; 167957b772b8SRobin Gong } else { 168057b772b8SRobin Gong residue = 0; 168157b772b8SRobin Gong } 1682a1ff6a07SSascha Hauer 168357b772b8SRobin Gong spin_unlock_irqrestore(&sdmac->vc.lock, flags); 16841ec1e82fSSascha Hauer 1685e8e3a790SAndy Shevchenko dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, 1686d1a792f3SRussell King - ARM Linux residue); 16871ec1e82fSSascha Hauer 16888a965911SShawn Guo return sdmac->status; 16891ec1e82fSSascha Hauer } 16901ec1e82fSSascha Hauer 16911ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 16921ec1e82fSSascha Hauer { 16932b4f130eSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 169457b772b8SRobin Gong unsigned long flags; 16952b4f130eSSascha Hauer 169657b772b8SRobin Gong spin_lock_irqsave(&sdmac->vc.lock, flags); 169757b772b8SRobin Gong if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc) 169857b772b8SRobin Gong sdma_start_desc(sdmac); 169957b772b8SRobin Gong spin_unlock_irqrestore(&sdmac->vc.lock, flags); 17001ec1e82fSSascha Hauer } 17011ec1e82fSSascha Hauer 17025b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 1703cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38 1704a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41 1705b7d2648aSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42 17065b28aa31SSascha Hauer 17075b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 17085b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 17095b28aa31SSascha Hauer { 17105b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 17115b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 17125b28aa31SSascha Hauer int i; 17135b28aa31SSascha Hauer 171470dabaedSNicolin Chen /* use the default firmware in ROM if missing external firmware */ 171570dabaedSNicolin Chen if (!sdma->script_number) 171670dabaedSNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 171770dabaedSNicolin Chen 1718bd73dfabSRobin Gong if (sdma->script_number > sizeof(struct sdma_script_start_addrs) 1719bd73dfabSRobin Gong / sizeof(s32)) { 1720bd73dfabSRobin Gong dev_err(sdma->dev, 1721bd73dfabSRobin Gong "SDMA script number %d not match with firmware.\n", 1722bd73dfabSRobin Gong sdma->script_number); 1723bd73dfabSRobin Gong return; 1724bd73dfabSRobin Gong } 1725bd73dfabSRobin Gong 1726cd72b846SNicolin Chen for (i = 0; i < sdma->script_number; i++) 17275b28aa31SSascha Hauer if (addr_arr[i] > 0) 17285b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 17295b28aa31SSascha Hauer } 17305b28aa31SSascha Hauer 17317b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context) 17325b28aa31SSascha Hauer { 17337b4b88e0SSascha Hauer struct sdma_engine *sdma = context; 17345b28aa31SSascha Hauer const struct sdma_firmware_header *header; 17355b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 17365b28aa31SSascha Hauer unsigned short *ram_code; 17375b28aa31SSascha Hauer 17387b4b88e0SSascha Hauer if (!fw) { 17390f927a11SSascha Hauer dev_info(sdma->dev, "external firmware not found, using ROM firmware\n"); 17400f927a11SSascha Hauer /* In this case we just use the ROM firmware. */ 17417b4b88e0SSascha Hauer return; 17427b4b88e0SSascha Hauer } 17435b28aa31SSascha Hauer 17445b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 17455b28aa31SSascha Hauer goto err_firmware; 17465b28aa31SSascha Hauer 17475b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 17485b28aa31SSascha Hauer 17495b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 17505b28aa31SSascha Hauer goto err_firmware; 17515b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 17525b28aa31SSascha Hauer goto err_firmware; 1753cd72b846SNicolin Chen switch (header->version_major) { 1754cd72b846SNicolin Chen case 1: 1755cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; 1756cd72b846SNicolin Chen break; 1757cd72b846SNicolin Chen case 2: 1758cd72b846SNicolin Chen sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2; 1759cd72b846SNicolin Chen break; 1760a572460bSFabio Estevam case 3: 1761a572460bSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3; 1762a572460bSFabio Estevam break; 1763b7d2648aSFabio Estevam case 4: 1764b7d2648aSFabio Estevam sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4; 1765b7d2648aSFabio Estevam break; 1766cd72b846SNicolin Chen default: 1767cd72b846SNicolin Chen dev_err(sdma->dev, "unknown firmware version\n"); 1768cd72b846SNicolin Chen goto err_firmware; 1769cd72b846SNicolin Chen } 17705b28aa31SSascha Hauer 17715b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 17725b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 17735b28aa31SSascha Hauer 17747560e3f3SSascha Hauer clk_enable(sdma->clk_ipg); 17757560e3f3SSascha Hauer clk_enable(sdma->clk_ahb); 17765b28aa31SSascha Hauer /* download the RAM image for SDMA */ 17775b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 17785b28aa31SSascha Hauer header->ram_code_size, 17796866fd3bSSascha Hauer addr->ram_code_start_addr); 17807560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 17817560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 17825b28aa31SSascha Hauer 17835b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 17845b28aa31SSascha Hauer 17855b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 17865b28aa31SSascha Hauer header->version_major, 17875b28aa31SSascha Hauer header->version_minor); 17885b28aa31SSascha Hauer 17895b28aa31SSascha Hauer err_firmware: 17905b28aa31SSascha Hauer release_firmware(fw); 17917b4b88e0SSascha Hauer } 17927b4b88e0SSascha Hauer 1793d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3 1794d078cd1bSZidan Wang 179529f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma) 1796d078cd1bSZidan Wang { 1797d078cd1bSZidan Wang struct device_node *np = sdma->dev->of_node; 1798d078cd1bSZidan Wang struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0); 1799d078cd1bSZidan Wang struct property *event_remap; 1800d078cd1bSZidan Wang struct regmap *gpr; 1801d078cd1bSZidan Wang char propname[] = "fsl,sdma-event-remap"; 1802d078cd1bSZidan Wang u32 reg, val, shift, num_map, i; 1803d078cd1bSZidan Wang int ret = 0; 1804d078cd1bSZidan Wang 1805d078cd1bSZidan Wang if (IS_ERR(np) || IS_ERR(gpr_np)) 1806d078cd1bSZidan Wang goto out; 1807d078cd1bSZidan Wang 1808d078cd1bSZidan Wang event_remap = of_find_property(np, propname, NULL); 1809d078cd1bSZidan Wang num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0; 1810d078cd1bSZidan Wang if (!num_map) { 1811ce078af7SFabio Estevam dev_dbg(sdma->dev, "no event needs to be remapped\n"); 1812d078cd1bSZidan Wang goto out; 1813d078cd1bSZidan Wang } else if (num_map % EVENT_REMAP_CELLS) { 1814d078cd1bSZidan Wang dev_err(sdma->dev, "the property %s must modulo %d\n", 1815d078cd1bSZidan Wang propname, EVENT_REMAP_CELLS); 1816d078cd1bSZidan Wang ret = -EINVAL; 1817d078cd1bSZidan Wang goto out; 1818d078cd1bSZidan Wang } 1819d078cd1bSZidan Wang 1820d078cd1bSZidan Wang gpr = syscon_node_to_regmap(gpr_np); 1821d078cd1bSZidan Wang if (IS_ERR(gpr)) { 1822d078cd1bSZidan Wang dev_err(sdma->dev, "failed to get gpr regmap\n"); 1823d078cd1bSZidan Wang ret = PTR_ERR(gpr); 1824d078cd1bSZidan Wang goto out; 1825d078cd1bSZidan Wang } 1826d078cd1bSZidan Wang 1827d078cd1bSZidan Wang for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) { 1828d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i, ®); 1829d078cd1bSZidan Wang if (ret) { 1830d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1831d078cd1bSZidan Wang propname, i); 1832d078cd1bSZidan Wang goto out; 1833d078cd1bSZidan Wang } 1834d078cd1bSZidan Wang 1835d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 1, &shift); 1836d078cd1bSZidan Wang if (ret) { 1837d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1838d078cd1bSZidan Wang propname, i + 1); 1839d078cd1bSZidan Wang goto out; 1840d078cd1bSZidan Wang } 1841d078cd1bSZidan Wang 1842d078cd1bSZidan Wang ret = of_property_read_u32_index(np, propname, i + 2, &val); 1843d078cd1bSZidan Wang if (ret) { 1844d078cd1bSZidan Wang dev_err(sdma->dev, "failed to read property %s index %d\n", 1845d078cd1bSZidan Wang propname, i + 2); 1846d078cd1bSZidan Wang goto out; 1847d078cd1bSZidan Wang } 1848d078cd1bSZidan Wang 1849d078cd1bSZidan Wang regmap_update_bits(gpr, reg, BIT(shift), val << shift); 1850d078cd1bSZidan Wang } 1851d078cd1bSZidan Wang 1852d078cd1bSZidan Wang out: 1853d078cd1bSZidan Wang if (!IS_ERR(gpr_np)) 1854d078cd1bSZidan Wang of_node_put(gpr_np); 1855d078cd1bSZidan Wang 1856d078cd1bSZidan Wang return ret; 1857d078cd1bSZidan Wang } 1858d078cd1bSZidan Wang 1859fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma, 18607b4b88e0SSascha Hauer const char *fw_name) 18617b4b88e0SSascha Hauer { 18627b4b88e0SSascha Hauer int ret; 18637b4b88e0SSascha Hauer 18647b4b88e0SSascha Hauer ret = request_firmware_nowait(THIS_MODULE, 18657b4b88e0SSascha Hauer FW_ACTION_HOTPLUG, fw_name, sdma->dev, 18667b4b88e0SSascha Hauer GFP_KERNEL, sdma, sdma_load_firmware); 18675b28aa31SSascha Hauer 18685b28aa31SSascha Hauer return ret; 18695b28aa31SSascha Hauer } 18705b28aa31SSascha Hauer 187119bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma) 18721ec1e82fSSascha Hauer { 18731ec1e82fSSascha Hauer int i, ret; 18741ec1e82fSSascha Hauer dma_addr_t ccb_phys; 18751ec1e82fSSascha Hauer 1876b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ipg); 1877b93edcddSFabio Estevam if (ret) 1878b93edcddSFabio Estevam return ret; 1879b93edcddSFabio Estevam ret = clk_enable(sdma->clk_ahb); 1880b93edcddSFabio Estevam if (ret) 1881b93edcddSFabio Estevam goto disable_clk_ipg; 18821ec1e82fSSascha Hauer 1883941acd56SAngus Ainslie (Purism) if (sdma->drvdata->check_ratio && 1884941acd56SAngus Ainslie (Purism) (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg))) 188525aaa75dSAngus Ainslie (Purism) sdma->clk_ratio = 1; 188625aaa75dSAngus Ainslie (Purism) 18871ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 1888c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); 18891ec1e82fSSascha Hauer 1890ceaf5226SAndy Duan sdma->channel_control = dma_alloc_coherent(sdma->dev, 18911ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + 18921ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 18931ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 18941ec1e82fSSascha Hauer 18951ec1e82fSSascha Hauer if (!sdma->channel_control) { 18961ec1e82fSSascha Hauer ret = -ENOMEM; 18971ec1e82fSSascha Hauer goto err_dma_alloc; 18981ec1e82fSSascha Hauer } 18991ec1e82fSSascha Hauer 19001ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 19011ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 19021ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 19031ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 19041ec1e82fSSascha Hauer 19051ec1e82fSSascha Hauer /* disable all channels */ 190617bba72fSSascha Hauer for (i = 0; i < sdma->drvdata->num_events; i++) 1907c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); 19081ec1e82fSSascha Hauer 19091ec1e82fSSascha Hauer /* All channels have priority 0 */ 19101ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 1911c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 19121ec1e82fSSascha Hauer 191357b772b8SRobin Gong ret = sdma_request_channel0(sdma); 19141ec1e82fSSascha Hauer if (ret) 19151ec1e82fSSascha Hauer goto err_dma_alloc; 19161ec1e82fSSascha Hauer 19171ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 19181ec1e82fSSascha Hauer 19191ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 1920c4b56857SRichard Zhao writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); 19211ec1e82fSSascha Hauer 19221ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 192325aaa75dSAngus Ainslie (Purism) if (sdma->clk_ratio) 192425aaa75dSAngus Ainslie (Purism) writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG); 192525aaa75dSAngus Ainslie (Purism) else 1926c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); 19271ec1e82fSSascha Hauer 1928c4b56857SRichard Zhao writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); 19291ec1e82fSSascha Hauer 19301ec1e82fSSascha Hauer /* Initializes channel's priorities */ 19311ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 19321ec1e82fSSascha Hauer 19337560e3f3SSascha Hauer clk_disable(sdma->clk_ipg); 19347560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 19351ec1e82fSSascha Hauer 19361ec1e82fSSascha Hauer return 0; 19371ec1e82fSSascha Hauer 19381ec1e82fSSascha Hauer err_dma_alloc: 19397560e3f3SSascha Hauer clk_disable(sdma->clk_ahb); 1940b93edcddSFabio Estevam disable_clk_ipg: 1941b93edcddSFabio Estevam clk_disable(sdma->clk_ipg); 19421ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 19431ec1e82fSSascha Hauer return ret; 19441ec1e82fSSascha Hauer } 19451ec1e82fSSascha Hauer 19469479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param) 19479479e17cSShawn Guo { 19480b351865SNicolin Chen struct sdma_channel *sdmac = to_sdma_chan(chan); 19499479e17cSShawn Guo struct imx_dma_data *data = fn_param; 19509479e17cSShawn Guo 19519479e17cSShawn Guo if (!imx_dma_is_general_purpose(chan)) 19529479e17cSShawn Guo return false; 19539479e17cSShawn Guo 19540b351865SNicolin Chen sdmac->data = *data; 19550b351865SNicolin Chen chan->private = &sdmac->data; 19569479e17cSShawn Guo 19579479e17cSShawn Guo return true; 19589479e17cSShawn Guo } 19599479e17cSShawn Guo 19609479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec, 19619479e17cSShawn Guo struct of_dma *ofdma) 19629479e17cSShawn Guo { 19639479e17cSShawn Guo struct sdma_engine *sdma = ofdma->of_dma_data; 19649479e17cSShawn Guo dma_cap_mask_t mask = sdma->dma_device.cap_mask; 19659479e17cSShawn Guo struct imx_dma_data data; 19669479e17cSShawn Guo 19679479e17cSShawn Guo if (dma_spec->args_count != 3) 19689479e17cSShawn Guo return NULL; 19699479e17cSShawn Guo 19709479e17cSShawn Guo data.dma_request = dma_spec->args[0]; 19719479e17cSShawn Guo data.peripheral_type = dma_spec->args[1]; 19729479e17cSShawn Guo data.priority = dma_spec->args[2]; 19738391ecf4SShengjiu Wang /* 19748391ecf4SShengjiu Wang * init dma_request2 to zero, which is not used by the dts. 19758391ecf4SShengjiu Wang * For P2P, dma_request2 is init from dma_request_channel(), 19768391ecf4SShengjiu Wang * chan->private will point to the imx_dma_data, and in 19778391ecf4SShengjiu Wang * device_alloc_chan_resources(), imx_dma_data.dma_request2 will 19788391ecf4SShengjiu Wang * be set to sdmac->event_id1. 19798391ecf4SShengjiu Wang */ 19808391ecf4SShengjiu Wang data.dma_request2 = 0; 19819479e17cSShawn Guo 1982990c0b53SBaolin Wang return __dma_request_channel(&mask, sdma_filter_fn, &data, 1983990c0b53SBaolin Wang ofdma->of_node); 19849479e17cSShawn Guo } 19859479e17cSShawn Guo 1986e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev) 19871ec1e82fSSascha Hauer { 1988580975d7SShawn Guo const struct of_device_id *of_id = 1989580975d7SShawn Guo of_match_device(sdma_dt_ids, &pdev->dev); 1990580975d7SShawn Guo struct device_node *np = pdev->dev.of_node; 19918391ecf4SShengjiu Wang struct device_node *spba_bus; 1992580975d7SShawn Guo const char *fw_name; 19931ec1e82fSSascha Hauer int ret; 19941ec1e82fSSascha Hauer int irq; 19951ec1e82fSSascha Hauer struct resource *iores; 19968391ecf4SShengjiu Wang struct resource spba_res; 1997d4adcc01SJingoo Han struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev); 19981ec1e82fSSascha Hauer int i; 19991ec1e82fSSascha Hauer struct sdma_engine *sdma; 200036e2f21aSSascha Hauer s32 *saddr_arr; 200117bba72fSSascha Hauer const struct sdma_driver_data *drvdata = NULL; 200217bba72fSSascha Hauer 200317bba72fSSascha Hauer if (of_id) 200417bba72fSSascha Hauer drvdata = of_id->data; 200517bba72fSSascha Hauer else if (pdev->id_entry) 200617bba72fSSascha Hauer drvdata = (void *)pdev->id_entry->driver_data; 200717bba72fSSascha Hauer 200817bba72fSSascha Hauer if (!drvdata) { 200917bba72fSSascha Hauer dev_err(&pdev->dev, "unable to find driver data\n"); 201017bba72fSSascha Hauer return -EINVAL; 201117bba72fSSascha Hauer } 20121ec1e82fSSascha Hauer 201342536b9fSPhilippe Retornaz ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 201442536b9fSPhilippe Retornaz if (ret) 201542536b9fSPhilippe Retornaz return ret; 201642536b9fSPhilippe Retornaz 20177f24e0eeSFabio Estevam sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL); 20181ec1e82fSSascha Hauer if (!sdma) 20191ec1e82fSSascha Hauer return -ENOMEM; 20201ec1e82fSSascha Hauer 20212ccaef05SRichard Zhao spin_lock_init(&sdma->channel_0_lock); 202273eab978SSascha Hauer 20231ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 202417bba72fSSascha Hauer sdma->drvdata = drvdata; 20251ec1e82fSSascha Hauer 20261ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 20277f24e0eeSFabio Estevam if (irq < 0) 202863c72e02SFabio Estevam return irq; 20291ec1e82fSSascha Hauer 20307f24e0eeSFabio Estevam iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 20317f24e0eeSFabio Estevam sdma->regs = devm_ioremap_resource(&pdev->dev, iores); 20327f24e0eeSFabio Estevam if (IS_ERR(sdma->regs)) 20337f24e0eeSFabio Estevam return PTR_ERR(sdma->regs); 20341ec1e82fSSascha Hauer 20357560e3f3SSascha Hauer sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 20367f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ipg)) 20377f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ipg); 20381ec1e82fSSascha Hauer 20397560e3f3SSascha Hauer sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 20407f24e0eeSFabio Estevam if (IS_ERR(sdma->clk_ahb)) 20417f24e0eeSFabio Estevam return PTR_ERR(sdma->clk_ahb); 20427560e3f3SSascha Hauer 2043fb9caf37SArvind Yadav ret = clk_prepare(sdma->clk_ipg); 2044fb9caf37SArvind Yadav if (ret) 2045fb9caf37SArvind Yadav return ret; 2046fb9caf37SArvind Yadav 2047fb9caf37SArvind Yadav ret = clk_prepare(sdma->clk_ahb); 2048fb9caf37SArvind Yadav if (ret) 2049fb9caf37SArvind Yadav goto err_clk; 20507560e3f3SSascha Hauer 20517f24e0eeSFabio Estevam ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma", 20527f24e0eeSFabio Estevam sdma); 20531ec1e82fSSascha Hauer if (ret) 2054fb9caf37SArvind Yadav goto err_irq; 20551ec1e82fSSascha Hauer 20565bb9dbb5SVinod Koul sdma->irq = irq; 20575bb9dbb5SVinod Koul 20585b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 2059fb9caf37SArvind Yadav if (!sdma->script_addrs) { 2060fb9caf37SArvind Yadav ret = -ENOMEM; 2061fb9caf37SArvind Yadav goto err_irq; 2062fb9caf37SArvind Yadav } 20631ec1e82fSSascha Hauer 206436e2f21aSSascha Hauer /* initially no scripts available */ 206536e2f21aSSascha Hauer saddr_arr = (s32 *)sdma->script_addrs; 2066*be4cf718SSascha Hauer for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++) 206736e2f21aSSascha Hauer saddr_arr[i] = -EINVAL; 206836e2f21aSSascha Hauer 20697214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 20707214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 20710f06c027SRobin Gong dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask); 20727214a8b1SSascha Hauer 20731ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 20741ec1e82fSSascha Hauer /* Initialize channel parameters */ 20751ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 20761ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 20771ec1e82fSSascha Hauer 20781ec1e82fSSascha Hauer sdmac->sdma = sdma; 20791ec1e82fSSascha Hauer 20801ec1e82fSSascha Hauer sdmac->channel = i; 208157b772b8SRobin Gong sdmac->vc.desc_free = sdma_desc_free; 2082b8603d2aSLucas Stach INIT_WORK(&sdmac->terminate_worker, 2083b8603d2aSLucas Stach sdma_channel_terminate_work); 208423889c63SSascha Hauer /* 208523889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 208623889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 208723889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 208823889c63SSascha Hauer */ 208923889c63SSascha Hauer if (i) 209057b772b8SRobin Gong vchan_init(&sdmac->vc, &sdma->dma_device); 20911ec1e82fSSascha Hauer } 20921ec1e82fSSascha Hauer 20935b28aa31SSascha Hauer ret = sdma_init(sdma); 20941ec1e82fSSascha Hauer if (ret) 20951ec1e82fSSascha Hauer goto err_init; 20961ec1e82fSSascha Hauer 2097d078cd1bSZidan Wang ret = sdma_event_remap(sdma); 2098d078cd1bSZidan Wang if (ret) 2099d078cd1bSZidan Wang goto err_init; 2100d078cd1bSZidan Wang 2101dcfec3c0SSascha Hauer if (sdma->drvdata->script_addrs) 2102dcfec3c0SSascha Hauer sdma_add_scripts(sdma, sdma->drvdata->script_addrs); 2103580975d7SShawn Guo if (pdata && pdata->script_addrs) 21045b28aa31SSascha Hauer sdma_add_scripts(sdma, pdata->script_addrs); 21055b28aa31SSascha Hauer 21061ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 21071ec1e82fSSascha Hauer 21081ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 21091ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 21101ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 21111ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 21121ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 21137b350ab0SMaxime Ripard sdma->dma_device.device_config = sdma_config; 2114a80f2787SSascha Hauer sdma->dma_device.device_terminate_all = sdma_terminate_all; 2115b8603d2aSLucas Stach sdma->dma_device.device_synchronize = sdma_channel_synchronize; 2116f9d4a398SNicolin Chen sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS; 2117f9d4a398SNicolin Chen sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS; 2118f9d4a398SNicolin Chen sdma->dma_device.directions = SDMA_DMA_DIRECTIONS; 21196f3125ceSLucas Stach sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT; 21200f06c027SRobin Gong sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy; 21211ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 2122b9b3f82fSSascha Hauer sdma->dma_device.dev->dma_parms = &sdma->dma_parms; 2123a3711d49SAngus Ainslie (Purism) sdma->dma_device.copy_align = 2; 21244a6b2e8aSRobin Gong dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT); 21251ec1e82fSSascha Hauer 212623e11811SVignesh Raman platform_set_drvdata(pdev, sdma); 212723e11811SVignesh Raman 21281ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 21291ec1e82fSSascha Hauer if (ret) { 21301ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 21311ec1e82fSSascha Hauer goto err_init; 21321ec1e82fSSascha Hauer } 21331ec1e82fSSascha Hauer 21349479e17cSShawn Guo if (np) { 21359479e17cSShawn Guo ret = of_dma_controller_register(np, sdma_xlate, sdma); 21369479e17cSShawn Guo if (ret) { 21379479e17cSShawn Guo dev_err(&pdev->dev, "failed to register controller\n"); 21389479e17cSShawn Guo goto err_register; 21399479e17cSShawn Guo } 21408391ecf4SShengjiu Wang 21418391ecf4SShengjiu Wang spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus"); 21428391ecf4SShengjiu Wang ret = of_address_to_resource(spba_bus, 0, &spba_res); 21438391ecf4SShengjiu Wang if (!ret) { 21448391ecf4SShengjiu Wang sdma->spba_start_addr = spba_res.start; 21458391ecf4SShengjiu Wang sdma->spba_end_addr = spba_res.end; 21468391ecf4SShengjiu Wang } 21478391ecf4SShengjiu Wang of_node_put(spba_bus); 21489479e17cSShawn Guo } 21499479e17cSShawn Guo 21502b8066c3SSven Van Asbroeck /* 21512b8066c3SSven Van Asbroeck * Kick off firmware loading as the very last step: 21522b8066c3SSven Van Asbroeck * attempt to load firmware only if we're not on the error path, because 21532b8066c3SSven Van Asbroeck * the firmware callback requires a fully functional and allocated sdma 21542b8066c3SSven Van Asbroeck * instance. 21552b8066c3SSven Van Asbroeck */ 21562b8066c3SSven Van Asbroeck if (pdata) { 21572b8066c3SSven Van Asbroeck ret = sdma_get_firmware(sdma, pdata->fw_name); 21582b8066c3SSven Van Asbroeck if (ret) 21592b8066c3SSven Van Asbroeck dev_warn(&pdev->dev, "failed to get firmware from platform data\n"); 21602b8066c3SSven Van Asbroeck } else { 21612b8066c3SSven Van Asbroeck /* 21622b8066c3SSven Van Asbroeck * Because that device tree does not encode ROM script address, 21632b8066c3SSven Van Asbroeck * the RAM script in firmware is mandatory for device tree 21642b8066c3SSven Van Asbroeck * probe, otherwise it fails. 21652b8066c3SSven Van Asbroeck */ 21662b8066c3SSven Van Asbroeck ret = of_property_read_string(np, "fsl,sdma-ram-script-name", 21672b8066c3SSven Van Asbroeck &fw_name); 21682b8066c3SSven Van Asbroeck if (ret) { 21692b8066c3SSven Van Asbroeck dev_warn(&pdev->dev, "failed to get firmware name\n"); 21702b8066c3SSven Van Asbroeck } else { 21712b8066c3SSven Van Asbroeck ret = sdma_get_firmware(sdma, fw_name); 21722b8066c3SSven Van Asbroeck if (ret) 21732b8066c3SSven Van Asbroeck dev_warn(&pdev->dev, "failed to get firmware from device tree\n"); 21742b8066c3SSven Van Asbroeck } 21752b8066c3SSven Van Asbroeck } 21762b8066c3SSven Van Asbroeck 21771ec1e82fSSascha Hauer return 0; 21781ec1e82fSSascha Hauer 21799479e17cSShawn Guo err_register: 21809479e17cSShawn Guo dma_async_device_unregister(&sdma->dma_device); 21811ec1e82fSSascha Hauer err_init: 21821ec1e82fSSascha Hauer kfree(sdma->script_addrs); 2183fb9caf37SArvind Yadav err_irq: 2184fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ahb); 2185fb9caf37SArvind Yadav err_clk: 2186fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ipg); 2187939fd4f0SShawn Guo return ret; 21881ec1e82fSSascha Hauer } 21891ec1e82fSSascha Hauer 21901d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev) 21911ec1e82fSSascha Hauer { 219223e11811SVignesh Raman struct sdma_engine *sdma = platform_get_drvdata(pdev); 2193c12fe497SVignesh Raman int i; 219423e11811SVignesh Raman 21955bb9dbb5SVinod Koul devm_free_irq(&pdev->dev, sdma->irq, sdma); 219623e11811SVignesh Raman dma_async_device_unregister(&sdma->dma_device); 219723e11811SVignesh Raman kfree(sdma->script_addrs); 2198fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ahb); 2199fb9caf37SArvind Yadav clk_unprepare(sdma->clk_ipg); 2200c12fe497SVignesh Raman /* Kill the tasklet */ 2201c12fe497SVignesh Raman for (i = 0; i < MAX_DMA_CHANNELS; i++) { 2202c12fe497SVignesh Raman struct sdma_channel *sdmac = &sdma->channel[i]; 2203c12fe497SVignesh Raman 220457b772b8SRobin Gong tasklet_kill(&sdmac->vc.task); 220557b772b8SRobin Gong sdma_free_chan_resources(&sdmac->vc.chan); 2206c12fe497SVignesh Raman } 220723e11811SVignesh Raman 220823e11811SVignesh Raman platform_set_drvdata(pdev, NULL); 220923e11811SVignesh Raman return 0; 22101ec1e82fSSascha Hauer } 22111ec1e82fSSascha Hauer 22121ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 22131ec1e82fSSascha Hauer .driver = { 22141ec1e82fSSascha Hauer .name = "imx-sdma", 2215580975d7SShawn Guo .of_match_table = sdma_dt_ids, 22161ec1e82fSSascha Hauer }, 221762550cd7SShawn Guo .id_table = sdma_devtypes, 22181d1bbd30SMaxin B. John .remove = sdma_remove, 221923e11811SVignesh Raman .probe = sdma_probe, 22201ec1e82fSSascha Hauer }; 22211ec1e82fSSascha Hauer 222223e11811SVignesh Raman module_platform_driver(sdma_driver); 22231ec1e82fSSascha Hauer 22241ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 22251ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 2226c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q) 2227c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin"); 2228c0879342SNicolas Chauvet #endif 2229c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX7D) 2230c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin"); 2231c0879342SNicolas Chauvet #endif 22321ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 2233