11ec1e82fSSascha Hauer /* 21ec1e82fSSascha Hauer * drivers/dma/imx-sdma.c 31ec1e82fSSascha Hauer * 41ec1e82fSSascha Hauer * This file contains a driver for the Freescale Smart DMA engine 51ec1e82fSSascha Hauer * 61ec1e82fSSascha Hauer * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> 71ec1e82fSSascha Hauer * 81ec1e82fSSascha Hauer * Based on code from Freescale: 91ec1e82fSSascha Hauer * 101ec1e82fSSascha Hauer * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved. 111ec1e82fSSascha Hauer * 121ec1e82fSSascha Hauer * The code contained herein is licensed under the GNU General Public 131ec1e82fSSascha Hauer * License. You may obtain a copy of the GNU General Public License 141ec1e82fSSascha Hauer * Version 2 or later at the following locations: 151ec1e82fSSascha Hauer * 161ec1e82fSSascha Hauer * http://www.opensource.org/licenses/gpl-license.html 171ec1e82fSSascha Hauer * http://www.gnu.org/copyleft/gpl.html 181ec1e82fSSascha Hauer */ 191ec1e82fSSascha Hauer 201ec1e82fSSascha Hauer #include <linux/init.h> 21f8de8f4cSAxel Lin #include <linux/module.h> 221ec1e82fSSascha Hauer #include <linux/types.h> 231ec1e82fSSascha Hauer #include <linux/mm.h> 241ec1e82fSSascha Hauer #include <linux/interrupt.h> 251ec1e82fSSascha Hauer #include <linux/clk.h> 261ec1e82fSSascha Hauer #include <linux/wait.h> 271ec1e82fSSascha Hauer #include <linux/sched.h> 281ec1e82fSSascha Hauer #include <linux/semaphore.h> 291ec1e82fSSascha Hauer #include <linux/spinlock.h> 301ec1e82fSSascha Hauer #include <linux/device.h> 311ec1e82fSSascha Hauer #include <linux/dma-mapping.h> 321ec1e82fSSascha Hauer #include <linux/firmware.h> 331ec1e82fSSascha Hauer #include <linux/slab.h> 341ec1e82fSSascha Hauer #include <linux/platform_device.h> 351ec1e82fSSascha Hauer #include <linux/dmaengine.h> 36580975d7SShawn Guo #include <linux/of.h> 37580975d7SShawn Guo #include <linux/of_device.h> 385c45ad77SPaul Gortmaker #include <linux/module.h> 391ec1e82fSSascha Hauer 401ec1e82fSSascha Hauer #include <asm/irq.h> 411ec1e82fSSascha Hauer #include <mach/sdma.h> 421ec1e82fSSascha Hauer #include <mach/dma.h> 431ec1e82fSSascha Hauer #include <mach/hardware.h> 441ec1e82fSSascha Hauer 451ec1e82fSSascha Hauer /* SDMA registers */ 461ec1e82fSSascha Hauer #define SDMA_H_C0PTR 0x000 471ec1e82fSSascha Hauer #define SDMA_H_INTR 0x004 481ec1e82fSSascha Hauer #define SDMA_H_STATSTOP 0x008 491ec1e82fSSascha Hauer #define SDMA_H_START 0x00c 501ec1e82fSSascha Hauer #define SDMA_H_EVTOVR 0x010 511ec1e82fSSascha Hauer #define SDMA_H_DSPOVR 0x014 521ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR 0x018 531ec1e82fSSascha Hauer #define SDMA_H_EVTPEND 0x01c 541ec1e82fSSascha Hauer #define SDMA_H_DSPENBL 0x020 551ec1e82fSSascha Hauer #define SDMA_H_RESET 0x024 561ec1e82fSSascha Hauer #define SDMA_H_EVTERR 0x028 571ec1e82fSSascha Hauer #define SDMA_H_INTRMSK 0x02c 581ec1e82fSSascha Hauer #define SDMA_H_PSW 0x030 591ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG 0x034 601ec1e82fSSascha Hauer #define SDMA_H_CONFIG 0x038 611ec1e82fSSascha Hauer #define SDMA_ONCE_ENB 0x040 621ec1e82fSSascha Hauer #define SDMA_ONCE_DATA 0x044 631ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR 0x048 641ec1e82fSSascha Hauer #define SDMA_ONCE_STAT 0x04c 651ec1e82fSSascha Hauer #define SDMA_ONCE_CMD 0x050 661ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR 0x054 671ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR 0x058 681ec1e82fSSascha Hauer #define SDMA_CHN0ADDR 0x05c 691ec1e82fSSascha Hauer #define SDMA_ONCE_RTB 0x060 701ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1 0x070 711ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2 0x074 7262550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35 0x200 7362550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31 0x080 741ec1e82fSSascha Hauer #define SDMA_CHNPRI_0 0x100 751ec1e82fSSascha Hauer 761ec1e82fSSascha Hauer /* 771ec1e82fSSascha Hauer * Buffer descriptor status values. 781ec1e82fSSascha Hauer */ 791ec1e82fSSascha Hauer #define BD_DONE 0x01 801ec1e82fSSascha Hauer #define BD_WRAP 0x02 811ec1e82fSSascha Hauer #define BD_CONT 0x04 821ec1e82fSSascha Hauer #define BD_INTR 0x08 831ec1e82fSSascha Hauer #define BD_RROR 0x10 841ec1e82fSSascha Hauer #define BD_LAST 0x20 851ec1e82fSSascha Hauer #define BD_EXTD 0x80 861ec1e82fSSascha Hauer 871ec1e82fSSascha Hauer /* 881ec1e82fSSascha Hauer * Data Node descriptor status values. 891ec1e82fSSascha Hauer */ 901ec1e82fSSascha Hauer #define DND_END_OF_FRAME 0x80 911ec1e82fSSascha Hauer #define DND_END_OF_XFER 0x40 921ec1e82fSSascha Hauer #define DND_DONE 0x20 931ec1e82fSSascha Hauer #define DND_UNUSED 0x01 941ec1e82fSSascha Hauer 951ec1e82fSSascha Hauer /* 961ec1e82fSSascha Hauer * IPCV2 descriptor status values. 971ec1e82fSSascha Hauer */ 981ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME 0x40 991ec1e82fSSascha Hauer 1001ec1e82fSSascha Hauer #define IPCV2_MAX_NODES 50 1011ec1e82fSSascha Hauer /* 1021ec1e82fSSascha Hauer * Error bit set in the CCB status field by the SDMA, 1031ec1e82fSSascha Hauer * in setbd routine, in case of a transfer error 1041ec1e82fSSascha Hauer */ 1051ec1e82fSSascha Hauer #define DATA_ERROR 0x10000000 1061ec1e82fSSascha Hauer 1071ec1e82fSSascha Hauer /* 1081ec1e82fSSascha Hauer * Buffer descriptor commands. 1091ec1e82fSSascha Hauer */ 1101ec1e82fSSascha Hauer #define C0_ADDR 0x01 1111ec1e82fSSascha Hauer #define C0_LOAD 0x02 1121ec1e82fSSascha Hauer #define C0_DUMP 0x03 1131ec1e82fSSascha Hauer #define C0_SETCTX 0x07 1141ec1e82fSSascha Hauer #define C0_GETCTX 0x03 1151ec1e82fSSascha Hauer #define C0_SETDM 0x01 1161ec1e82fSSascha Hauer #define C0_SETPM 0x04 1171ec1e82fSSascha Hauer #define C0_GETDM 0x02 1181ec1e82fSSascha Hauer #define C0_GETPM 0x08 1191ec1e82fSSascha Hauer /* 1201ec1e82fSSascha Hauer * Change endianness indicator in the BD command field 1211ec1e82fSSascha Hauer */ 1221ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS 0x80 1231ec1e82fSSascha Hauer 1241ec1e82fSSascha Hauer /* 1251ec1e82fSSascha Hauer * Mode/Count of data node descriptors - IPCv2 1261ec1e82fSSascha Hauer */ 1271ec1e82fSSascha Hauer struct sdma_mode_count { 1281ec1e82fSSascha Hauer u32 count : 16; /* size of the buffer pointed by this BD */ 1291ec1e82fSSascha Hauer u32 status : 8; /* E,R,I,C,W,D status bits stored here */ 1301ec1e82fSSascha Hauer u32 command : 8; /* command mostlky used for channel 0 */ 1311ec1e82fSSascha Hauer }; 1321ec1e82fSSascha Hauer 1331ec1e82fSSascha Hauer /* 1341ec1e82fSSascha Hauer * Buffer descriptor 1351ec1e82fSSascha Hauer */ 1361ec1e82fSSascha Hauer struct sdma_buffer_descriptor { 1371ec1e82fSSascha Hauer struct sdma_mode_count mode; 1381ec1e82fSSascha Hauer u32 buffer_addr; /* address of the buffer described */ 1391ec1e82fSSascha Hauer u32 ext_buffer_addr; /* extended buffer address */ 1401ec1e82fSSascha Hauer } __attribute__ ((packed)); 1411ec1e82fSSascha Hauer 1421ec1e82fSSascha Hauer /** 1431ec1e82fSSascha Hauer * struct sdma_channel_control - Channel control Block 1441ec1e82fSSascha Hauer * 1451ec1e82fSSascha Hauer * @current_bd_ptr current buffer descriptor processed 1461ec1e82fSSascha Hauer * @base_bd_ptr first element of buffer descriptor array 1471ec1e82fSSascha Hauer * @unused padding. The SDMA engine expects an array of 128 byte 1481ec1e82fSSascha Hauer * control blocks 1491ec1e82fSSascha Hauer */ 1501ec1e82fSSascha Hauer struct sdma_channel_control { 1511ec1e82fSSascha Hauer u32 current_bd_ptr; 1521ec1e82fSSascha Hauer u32 base_bd_ptr; 1531ec1e82fSSascha Hauer u32 unused[2]; 1541ec1e82fSSascha Hauer } __attribute__ ((packed)); 1551ec1e82fSSascha Hauer 1561ec1e82fSSascha Hauer /** 1571ec1e82fSSascha Hauer * struct sdma_state_registers - SDMA context for a channel 1581ec1e82fSSascha Hauer * 1591ec1e82fSSascha Hauer * @pc: program counter 1601ec1e82fSSascha Hauer * @t: test bit: status of arithmetic & test instruction 1611ec1e82fSSascha Hauer * @rpc: return program counter 1621ec1e82fSSascha Hauer * @sf: source fault while loading data 1631ec1e82fSSascha Hauer * @spc: loop start program counter 1641ec1e82fSSascha Hauer * @df: destination fault while storing data 1651ec1e82fSSascha Hauer * @epc: loop end program counter 1661ec1e82fSSascha Hauer * @lm: loop mode 1671ec1e82fSSascha Hauer */ 1681ec1e82fSSascha Hauer struct sdma_state_registers { 1691ec1e82fSSascha Hauer u32 pc :14; 1701ec1e82fSSascha Hauer u32 unused1: 1; 1711ec1e82fSSascha Hauer u32 t : 1; 1721ec1e82fSSascha Hauer u32 rpc :14; 1731ec1e82fSSascha Hauer u32 unused0: 1; 1741ec1e82fSSascha Hauer u32 sf : 1; 1751ec1e82fSSascha Hauer u32 spc :14; 1761ec1e82fSSascha Hauer u32 unused2: 1; 1771ec1e82fSSascha Hauer u32 df : 1; 1781ec1e82fSSascha Hauer u32 epc :14; 1791ec1e82fSSascha Hauer u32 lm : 2; 1801ec1e82fSSascha Hauer } __attribute__ ((packed)); 1811ec1e82fSSascha Hauer 1821ec1e82fSSascha Hauer /** 1831ec1e82fSSascha Hauer * struct sdma_context_data - sdma context specific to a channel 1841ec1e82fSSascha Hauer * 1851ec1e82fSSascha Hauer * @channel_state: channel state bits 1861ec1e82fSSascha Hauer * @gReg: general registers 1871ec1e82fSSascha Hauer * @mda: burst dma destination address register 1881ec1e82fSSascha Hauer * @msa: burst dma source address register 1891ec1e82fSSascha Hauer * @ms: burst dma status register 1901ec1e82fSSascha Hauer * @md: burst dma data register 1911ec1e82fSSascha Hauer * @pda: peripheral dma destination address register 1921ec1e82fSSascha Hauer * @psa: peripheral dma source address register 1931ec1e82fSSascha Hauer * @ps: peripheral dma status register 1941ec1e82fSSascha Hauer * @pd: peripheral dma data register 1951ec1e82fSSascha Hauer * @ca: CRC polynomial register 1961ec1e82fSSascha Hauer * @cs: CRC accumulator register 1971ec1e82fSSascha Hauer * @dda: dedicated core destination address register 1981ec1e82fSSascha Hauer * @dsa: dedicated core source address register 1991ec1e82fSSascha Hauer * @ds: dedicated core status register 2001ec1e82fSSascha Hauer * @dd: dedicated core data register 2011ec1e82fSSascha Hauer */ 2021ec1e82fSSascha Hauer struct sdma_context_data { 2031ec1e82fSSascha Hauer struct sdma_state_registers channel_state; 2041ec1e82fSSascha Hauer u32 gReg[8]; 2051ec1e82fSSascha Hauer u32 mda; 2061ec1e82fSSascha Hauer u32 msa; 2071ec1e82fSSascha Hauer u32 ms; 2081ec1e82fSSascha Hauer u32 md; 2091ec1e82fSSascha Hauer u32 pda; 2101ec1e82fSSascha Hauer u32 psa; 2111ec1e82fSSascha Hauer u32 ps; 2121ec1e82fSSascha Hauer u32 pd; 2131ec1e82fSSascha Hauer u32 ca; 2141ec1e82fSSascha Hauer u32 cs; 2151ec1e82fSSascha Hauer u32 dda; 2161ec1e82fSSascha Hauer u32 dsa; 2171ec1e82fSSascha Hauer u32 ds; 2181ec1e82fSSascha Hauer u32 dd; 2191ec1e82fSSascha Hauer u32 scratch0; 2201ec1e82fSSascha Hauer u32 scratch1; 2211ec1e82fSSascha Hauer u32 scratch2; 2221ec1e82fSSascha Hauer u32 scratch3; 2231ec1e82fSSascha Hauer u32 scratch4; 2241ec1e82fSSascha Hauer u32 scratch5; 2251ec1e82fSSascha Hauer u32 scratch6; 2261ec1e82fSSascha Hauer u32 scratch7; 2271ec1e82fSSascha Hauer } __attribute__ ((packed)); 2281ec1e82fSSascha Hauer 2291ec1e82fSSascha Hauer #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor)) 2301ec1e82fSSascha Hauer 2311ec1e82fSSascha Hauer struct sdma_engine; 2321ec1e82fSSascha Hauer 2331ec1e82fSSascha Hauer /** 2341ec1e82fSSascha Hauer * struct sdma_channel - housekeeping for a SDMA channel 2351ec1e82fSSascha Hauer * 2361ec1e82fSSascha Hauer * @sdma pointer to the SDMA engine for this channel 23723889c63SSascha Hauer * @channel the channel number, matches dmaengine chan_id + 1 2381ec1e82fSSascha Hauer * @direction transfer type. Needed for setting SDMA script 2391ec1e82fSSascha Hauer * @peripheral_type Peripheral type. Needed for setting SDMA script 2401ec1e82fSSascha Hauer * @event_id0 aka dma request line 2411ec1e82fSSascha Hauer * @event_id1 for channels that use 2 events 2421ec1e82fSSascha Hauer * @word_size peripheral access size 2431ec1e82fSSascha Hauer * @buf_tail ID of the buffer that was processed 2441ec1e82fSSascha Hauer * @done channel completion 2451ec1e82fSSascha Hauer * @num_bd max NUM_BD. number of descriptors currently handling 2461ec1e82fSSascha Hauer */ 2471ec1e82fSSascha Hauer struct sdma_channel { 2481ec1e82fSSascha Hauer struct sdma_engine *sdma; 2491ec1e82fSSascha Hauer unsigned int channel; 250db8196dfSVinod Koul enum dma_transfer_direction direction; 2511ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type; 2521ec1e82fSSascha Hauer unsigned int event_id0; 2531ec1e82fSSascha Hauer unsigned int event_id1; 2541ec1e82fSSascha Hauer enum dma_slave_buswidth word_size; 2551ec1e82fSSascha Hauer unsigned int buf_tail; 2561ec1e82fSSascha Hauer struct completion done; 2571ec1e82fSSascha Hauer unsigned int num_bd; 2581ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 2591ec1e82fSSascha Hauer dma_addr_t bd_phys; 2601ec1e82fSSascha Hauer unsigned int pc_from_device, pc_to_device; 2611ec1e82fSSascha Hauer unsigned long flags; 2621ec1e82fSSascha Hauer dma_addr_t per_address; 2631ec1e82fSSascha Hauer u32 event_mask0, event_mask1; 2641ec1e82fSSascha Hauer u32 watermark_level; 2651ec1e82fSSascha Hauer u32 shp_addr, per_addr; 2661ec1e82fSSascha Hauer struct dma_chan chan; 2671ec1e82fSSascha Hauer spinlock_t lock; 2681ec1e82fSSascha Hauer struct dma_async_tx_descriptor desc; 2691ec1e82fSSascha Hauer dma_cookie_t last_completed; 2701ec1e82fSSascha Hauer enum dma_status status; 271ab59a510SHuang Shijie unsigned int chn_count; 272ab59a510SHuang Shijie unsigned int chn_real_count; 2731ec1e82fSSascha Hauer }; 2741ec1e82fSSascha Hauer 2751ec1e82fSSascha Hauer #define IMX_DMA_SG_LOOP (1 << 0) 2761ec1e82fSSascha Hauer 2771ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32 2781ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1 2791ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1 2801ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7 2811ec1e82fSSascha Hauer 2821ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453 2831ec1e82fSSascha Hauer 2841ec1e82fSSascha Hauer /** 2851ec1e82fSSascha Hauer * struct sdma_firmware_header - Layout of the firmware image 2861ec1e82fSSascha Hauer * 2871ec1e82fSSascha Hauer * @magic "SDMA" 2881ec1e82fSSascha Hauer * @version_major increased whenever layout of struct sdma_script_start_addrs 2891ec1e82fSSascha Hauer * changes. 2901ec1e82fSSascha Hauer * @version_minor firmware minor version (for binary compatible changes) 2911ec1e82fSSascha Hauer * @script_addrs_start offset of struct sdma_script_start_addrs in this image 2921ec1e82fSSascha Hauer * @num_script_addrs Number of script addresses in this image 2931ec1e82fSSascha Hauer * @ram_code_start offset of SDMA ram image in this firmware image 2941ec1e82fSSascha Hauer * @ram_code_size size of SDMA ram image 2951ec1e82fSSascha Hauer * @script_addrs Stores the start address of the SDMA scripts 2961ec1e82fSSascha Hauer * (in SDMA memory space) 2971ec1e82fSSascha Hauer */ 2981ec1e82fSSascha Hauer struct sdma_firmware_header { 2991ec1e82fSSascha Hauer u32 magic; 3001ec1e82fSSascha Hauer u32 version_major; 3011ec1e82fSSascha Hauer u32 version_minor; 3021ec1e82fSSascha Hauer u32 script_addrs_start; 3031ec1e82fSSascha Hauer u32 num_script_addrs; 3041ec1e82fSSascha Hauer u32 ram_code_start; 3051ec1e82fSSascha Hauer u32 ram_code_size; 3061ec1e82fSSascha Hauer }; 3071ec1e82fSSascha Hauer 30862550cd7SShawn Guo enum sdma_devtype { 30962550cd7SShawn Guo IMX31_SDMA, /* runs on i.mx31 */ 31062550cd7SShawn Guo IMX35_SDMA, /* runs on i.mx35 and later */ 31162550cd7SShawn Guo }; 31262550cd7SShawn Guo 3131ec1e82fSSascha Hauer struct sdma_engine { 3141ec1e82fSSascha Hauer struct device *dev; 315b9b3f82fSSascha Hauer struct device_dma_parameters dma_parms; 3161ec1e82fSSascha Hauer struct sdma_channel channel[MAX_DMA_CHANNELS]; 3171ec1e82fSSascha Hauer struct sdma_channel_control *channel_control; 3181ec1e82fSSascha Hauer void __iomem *regs; 31962550cd7SShawn Guo enum sdma_devtype devtype; 3201ec1e82fSSascha Hauer unsigned int num_events; 3211ec1e82fSSascha Hauer struct sdma_context_data *context; 3221ec1e82fSSascha Hauer dma_addr_t context_phys; 3231ec1e82fSSascha Hauer struct dma_device dma_device; 3241ec1e82fSSascha Hauer struct clk *clk; 32573eab978SSascha Hauer struct mutex channel_0_lock; 3261ec1e82fSSascha Hauer struct sdma_script_start_addrs *script_addrs; 3271ec1e82fSSascha Hauer }; 3281ec1e82fSSascha Hauer 32962550cd7SShawn Guo static struct platform_device_id sdma_devtypes[] = { 33062550cd7SShawn Guo { 33162550cd7SShawn Guo .name = "imx31-sdma", 33262550cd7SShawn Guo .driver_data = IMX31_SDMA, 33362550cd7SShawn Guo }, { 33462550cd7SShawn Guo .name = "imx35-sdma", 33562550cd7SShawn Guo .driver_data = IMX35_SDMA, 33662550cd7SShawn Guo }, { 33762550cd7SShawn Guo /* sentinel */ 33862550cd7SShawn Guo } 33962550cd7SShawn Guo }; 34062550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes); 34162550cd7SShawn Guo 342580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = { 343580975d7SShawn Guo { .compatible = "fsl,imx31-sdma", .data = &sdma_devtypes[IMX31_SDMA], }, 344580975d7SShawn Guo { .compatible = "fsl,imx35-sdma", .data = &sdma_devtypes[IMX35_SDMA], }, 345580975d7SShawn Guo { /* sentinel */ } 346580975d7SShawn Guo }; 347580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids); 348580975d7SShawn Guo 3491ec1e82fSSascha Hauer #define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */ 3501ec1e82fSSascha Hauer #define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */ 3511ec1e82fSSascha Hauer #define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */ 3521ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 3531ec1e82fSSascha Hauer 3541ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 3551ec1e82fSSascha Hauer { 35662550cd7SShawn Guo u32 chnenbl0 = (sdma->devtype == IMX31_SDMA ? SDMA_CHNENBL0_IMX31 : 35762550cd7SShawn Guo SDMA_CHNENBL0_IMX35); 3581ec1e82fSSascha Hauer return chnenbl0 + event * 4; 3591ec1e82fSSascha Hauer } 3601ec1e82fSSascha Hauer 3611ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 3621ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 3631ec1e82fSSascha Hauer { 3641ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 3651ec1e82fSSascha Hauer int channel = sdmac->channel; 3661ec1e82fSSascha Hauer u32 evt, mcu, dsp; 3671ec1e82fSSascha Hauer 3681ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 3691ec1e82fSSascha Hauer return -EINVAL; 3701ec1e82fSSascha Hauer 371*c4b56857SRichard Zhao evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR); 372*c4b56857SRichard Zhao mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR); 373*c4b56857SRichard Zhao dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR); 3741ec1e82fSSascha Hauer 3751ec1e82fSSascha Hauer if (dsp_override) 3761ec1e82fSSascha Hauer dsp &= ~(1 << channel); 3771ec1e82fSSascha Hauer else 3781ec1e82fSSascha Hauer dsp |= (1 << channel); 3791ec1e82fSSascha Hauer 3801ec1e82fSSascha Hauer if (event_override) 3811ec1e82fSSascha Hauer evt &= ~(1 << channel); 3821ec1e82fSSascha Hauer else 3831ec1e82fSSascha Hauer evt |= (1 << channel); 3841ec1e82fSSascha Hauer 3851ec1e82fSSascha Hauer if (mcu_override) 3861ec1e82fSSascha Hauer mcu &= ~(1 << channel); 3871ec1e82fSSascha Hauer else 3881ec1e82fSSascha Hauer mcu |= (1 << channel); 3891ec1e82fSSascha Hauer 390*c4b56857SRichard Zhao writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR); 391*c4b56857SRichard Zhao writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR); 392*c4b56857SRichard Zhao writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR); 3931ec1e82fSSascha Hauer 3941ec1e82fSSascha Hauer return 0; 3951ec1e82fSSascha Hauer } 3961ec1e82fSSascha Hauer 397b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 398b9a59166SRichard Zhao { 399*c4b56857SRichard Zhao writel(1 << channel, sdma->regs + SDMA_H_START); 400b9a59166SRichard Zhao } 401b9a59166SRichard Zhao 4021ec1e82fSSascha Hauer /* 4031ec1e82fSSascha Hauer * sdma_run_channel - run a channel and wait till it's done 4041ec1e82fSSascha Hauer */ 4051ec1e82fSSascha Hauer static int sdma_run_channel(struct sdma_channel *sdmac) 4061ec1e82fSSascha Hauer { 4071ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 4081ec1e82fSSascha Hauer int channel = sdmac->channel; 4091ec1e82fSSascha Hauer int ret; 4101ec1e82fSSascha Hauer 4111ec1e82fSSascha Hauer init_completion(&sdmac->done); 4121ec1e82fSSascha Hauer 413b9a59166SRichard Zhao sdma_enable_channel(sdma, channel); 4141ec1e82fSSascha Hauer 4151ec1e82fSSascha Hauer ret = wait_for_completion_timeout(&sdmac->done, HZ); 4161ec1e82fSSascha Hauer 4171ec1e82fSSascha Hauer return ret ? 0 : -ETIMEDOUT; 4181ec1e82fSSascha Hauer } 4191ec1e82fSSascha Hauer 4201ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 4211ec1e82fSSascha Hauer u32 address) 4221ec1e82fSSascha Hauer { 4231ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 4241ec1e82fSSascha Hauer void *buf_virt; 4251ec1e82fSSascha Hauer dma_addr_t buf_phys; 4261ec1e82fSSascha Hauer int ret; 4271ec1e82fSSascha Hauer 42873eab978SSascha Hauer mutex_lock(&sdma->channel_0_lock); 42973eab978SSascha Hauer 4301ec1e82fSSascha Hauer buf_virt = dma_alloc_coherent(NULL, 4311ec1e82fSSascha Hauer size, 4321ec1e82fSSascha Hauer &buf_phys, GFP_KERNEL); 43373eab978SSascha Hauer if (!buf_virt) { 43473eab978SSascha Hauer ret = -ENOMEM; 43573eab978SSascha Hauer goto err_out; 43673eab978SSascha Hauer } 4371ec1e82fSSascha Hauer 4381ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 4391ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 4401ec1e82fSSascha Hauer bd0->mode.count = size / 2; 4411ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 4421ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 4431ec1e82fSSascha Hauer 4441ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 4451ec1e82fSSascha Hauer 4461ec1e82fSSascha Hauer ret = sdma_run_channel(&sdma->channel[0]); 4471ec1e82fSSascha Hauer 4481ec1e82fSSascha Hauer dma_free_coherent(NULL, size, buf_virt, buf_phys); 4491ec1e82fSSascha Hauer 45073eab978SSascha Hauer err_out: 45173eab978SSascha Hauer mutex_unlock(&sdma->channel_0_lock); 45273eab978SSascha Hauer 4531ec1e82fSSascha Hauer return ret; 4541ec1e82fSSascha Hauer } 4551ec1e82fSSascha Hauer 4561ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 4571ec1e82fSSascha Hauer { 4581ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 4591ec1e82fSSascha Hauer int channel = sdmac->channel; 4601ec1e82fSSascha Hauer u32 val; 4611ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 4621ec1e82fSSascha Hauer 463*c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 4641ec1e82fSSascha Hauer val |= (1 << channel); 465*c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 4661ec1e82fSSascha Hauer } 4671ec1e82fSSascha Hauer 4681ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 4691ec1e82fSSascha Hauer { 4701ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 4711ec1e82fSSascha Hauer int channel = sdmac->channel; 4721ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 4731ec1e82fSSascha Hauer u32 val; 4741ec1e82fSSascha Hauer 475*c4b56857SRichard Zhao val = readl_relaxed(sdma->regs + chnenbl); 4761ec1e82fSSascha Hauer val &= ~(1 << channel); 477*c4b56857SRichard Zhao writel_relaxed(val, sdma->regs + chnenbl); 4781ec1e82fSSascha Hauer } 4791ec1e82fSSascha Hauer 4801ec1e82fSSascha Hauer static void sdma_handle_channel_loop(struct sdma_channel *sdmac) 4811ec1e82fSSascha Hauer { 4821ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 4831ec1e82fSSascha Hauer 4841ec1e82fSSascha Hauer /* 4851ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 4861ec1e82fSSascha Hauer * call callback function. 4871ec1e82fSSascha Hauer */ 4881ec1e82fSSascha Hauer while (1) { 4891ec1e82fSSascha Hauer bd = &sdmac->bd[sdmac->buf_tail]; 4901ec1e82fSSascha Hauer 4911ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 4921ec1e82fSSascha Hauer break; 4931ec1e82fSSascha Hauer 4941ec1e82fSSascha Hauer if (bd->mode.status & BD_RROR) 4951ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 4961ec1e82fSSascha Hauer else 4971e9cebb4SShawn Guo sdmac->status = DMA_IN_PROGRESS; 4981ec1e82fSSascha Hauer 4991ec1e82fSSascha Hauer bd->mode.status |= BD_DONE; 5001ec1e82fSSascha Hauer sdmac->buf_tail++; 5011ec1e82fSSascha Hauer sdmac->buf_tail %= sdmac->num_bd; 5021ec1e82fSSascha Hauer 5031ec1e82fSSascha Hauer if (sdmac->desc.callback) 5041ec1e82fSSascha Hauer sdmac->desc.callback(sdmac->desc.callback_param); 5051ec1e82fSSascha Hauer } 5061ec1e82fSSascha Hauer } 5071ec1e82fSSascha Hauer 5081ec1e82fSSascha Hauer static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac) 5091ec1e82fSSascha Hauer { 5101ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 5111ec1e82fSSascha Hauer int i, error = 0; 5121ec1e82fSSascha Hauer 513ab59a510SHuang Shijie sdmac->chn_real_count = 0; 5141ec1e82fSSascha Hauer /* 5151ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 5161ec1e82fSSascha Hauer * errors and call callback function 5171ec1e82fSSascha Hauer */ 5181ec1e82fSSascha Hauer for (i = 0; i < sdmac->num_bd; i++) { 5191ec1e82fSSascha Hauer bd = &sdmac->bd[i]; 5201ec1e82fSSascha Hauer 5211ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 5221ec1e82fSSascha Hauer error = -EIO; 523ab59a510SHuang Shijie sdmac->chn_real_count += bd->mode.count; 5241ec1e82fSSascha Hauer } 5251ec1e82fSSascha Hauer 5261ec1e82fSSascha Hauer if (error) 5271ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 5281ec1e82fSSascha Hauer else 5291ec1e82fSSascha Hauer sdmac->status = DMA_SUCCESS; 5301ec1e82fSSascha Hauer 531ab59a510SHuang Shijie sdmac->last_completed = sdmac->desc.cookie; 5321ec1e82fSSascha Hauer if (sdmac->desc.callback) 5331ec1e82fSSascha Hauer sdmac->desc.callback(sdmac->desc.callback_param); 5341ec1e82fSSascha Hauer } 5351ec1e82fSSascha Hauer 5361ec1e82fSSascha Hauer static void mxc_sdma_handle_channel(struct sdma_channel *sdmac) 5371ec1e82fSSascha Hauer { 5381ec1e82fSSascha Hauer complete(&sdmac->done); 5391ec1e82fSSascha Hauer 5401ec1e82fSSascha Hauer /* not interested in channel 0 interrupts */ 5411ec1e82fSSascha Hauer if (sdmac->channel == 0) 5421ec1e82fSSascha Hauer return; 5431ec1e82fSSascha Hauer 5441ec1e82fSSascha Hauer if (sdmac->flags & IMX_DMA_SG_LOOP) 5451ec1e82fSSascha Hauer sdma_handle_channel_loop(sdmac); 5461ec1e82fSSascha Hauer else 5471ec1e82fSSascha Hauer mxc_sdma_handle_channel_normal(sdmac); 5481ec1e82fSSascha Hauer } 5491ec1e82fSSascha Hauer 5501ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 5511ec1e82fSSascha Hauer { 5521ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 5531ec1e82fSSascha Hauer u32 stat; 5541ec1e82fSSascha Hauer 555*c4b56857SRichard Zhao stat = readl_relaxed(sdma->regs + SDMA_H_INTR); 556*c4b56857SRichard Zhao writel_relaxed(stat, sdma->regs + SDMA_H_INTR); 5571ec1e82fSSascha Hauer 5581ec1e82fSSascha Hauer while (stat) { 5591ec1e82fSSascha Hauer int channel = fls(stat) - 1; 5601ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 5611ec1e82fSSascha Hauer 5621ec1e82fSSascha Hauer mxc_sdma_handle_channel(sdmac); 5631ec1e82fSSascha Hauer 5641ec1e82fSSascha Hauer stat &= ~(1 << channel); 5651ec1e82fSSascha Hauer } 5661ec1e82fSSascha Hauer 5671ec1e82fSSascha Hauer return IRQ_HANDLED; 5681ec1e82fSSascha Hauer } 5691ec1e82fSSascha Hauer 5701ec1e82fSSascha Hauer /* 5711ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 5721ec1e82fSSascha Hauer */ 5731ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac, 5741ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 5751ec1e82fSSascha Hauer { 5761ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 5771ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 5781ec1e82fSSascha Hauer /* 5791ec1e82fSSascha Hauer * These are needed once we start to support transfers between 5801ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 5811ec1e82fSSascha Hauer */ 5821ec1e82fSSascha Hauer int per_2_per = 0, emi_2_emi = 0; 5831ec1e82fSSascha Hauer 5841ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 5851ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 5861ec1e82fSSascha Hauer 5871ec1e82fSSascha Hauer switch (peripheral_type) { 5881ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 5891ec1e82fSSascha Hauer emi_2_emi = sdma->script_addrs->ap_2_ap_addr; 5901ec1e82fSSascha Hauer break; 5911ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 5921ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 5931ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 5941ec1e82fSSascha Hauer break; 5951ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 5961ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 5971ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 5981ec1e82fSSascha Hauer break; 5991ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 6001ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 6011ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 6021ec1e82fSSascha Hauer break; 6031ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 6041ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 6051ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 6061ec1e82fSSascha Hauer break; 6071ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 6081ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 6091ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 6101ec1e82fSSascha Hauer break; 6111ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 6121ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 6131ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 6141ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 6151ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 6161ec1e82fSSascha Hauer break; 6171ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 6181ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 6191ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 6201ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 6211ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 6221ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 6231ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 6241ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 6251ec1e82fSSascha Hauer break; 6261ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 6271ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 6281ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 6291ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 6301ec1e82fSSascha Hauer break; 6311ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 6321ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 6331ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 6341ec1e82fSSascha Hauer break; 6351ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 6361ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 6371ec1e82fSSascha Hauer break; 6381ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 6391ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 6401ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 6411ec1e82fSSascha Hauer break; 6421ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 6431ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 6441ec1e82fSSascha Hauer break; 6451ec1e82fSSascha Hauer default: 6461ec1e82fSSascha Hauer break; 6471ec1e82fSSascha Hauer } 6481ec1e82fSSascha Hauer 6491ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 6501ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 6511ec1e82fSSascha Hauer } 6521ec1e82fSSascha Hauer 6531ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 6541ec1e82fSSascha Hauer { 6551ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6561ec1e82fSSascha Hauer int channel = sdmac->channel; 6571ec1e82fSSascha Hauer int load_address; 6581ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 6591ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 6601ec1e82fSSascha Hauer int ret; 6611ec1e82fSSascha Hauer 662db8196dfSVinod Koul if (sdmac->direction == DMA_DEV_TO_MEM) { 6631ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 6641ec1e82fSSascha Hauer } else { 6651ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 6661ec1e82fSSascha Hauer } 6671ec1e82fSSascha Hauer 6681ec1e82fSSascha Hauer if (load_address < 0) 6691ec1e82fSSascha Hauer return load_address; 6701ec1e82fSSascha Hauer 6711ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 6721ec1e82fSSascha Hauer dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level); 6731ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 6741ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 6751ec1e82fSSascha Hauer dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0); 6761ec1e82fSSascha Hauer dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1); 6771ec1e82fSSascha Hauer 67873eab978SSascha Hauer mutex_lock(&sdma->channel_0_lock); 67973eab978SSascha Hauer 6801ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 6811ec1e82fSSascha Hauer context->channel_state.pc = load_address; 6821ec1e82fSSascha Hauer 6831ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 6841ec1e82fSSascha Hauer * and watermark level 6851ec1e82fSSascha Hauer */ 6861ec1e82fSSascha Hauer context->gReg[0] = sdmac->event_mask1; 6871ec1e82fSSascha Hauer context->gReg[1] = sdmac->event_mask0; 6881ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 6891ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 6901ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 6911ec1e82fSSascha Hauer 6921ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 6931ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 6941ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 6951ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 6961ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 6971ec1e82fSSascha Hauer 6981ec1e82fSSascha Hauer ret = sdma_run_channel(&sdma->channel[0]); 6991ec1e82fSSascha Hauer 70073eab978SSascha Hauer mutex_unlock(&sdma->channel_0_lock); 70173eab978SSascha Hauer 7021ec1e82fSSascha Hauer return ret; 7031ec1e82fSSascha Hauer } 7041ec1e82fSSascha Hauer 7051ec1e82fSSascha Hauer static void sdma_disable_channel(struct sdma_channel *sdmac) 7061ec1e82fSSascha Hauer { 7071ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7081ec1e82fSSascha Hauer int channel = sdmac->channel; 7091ec1e82fSSascha Hauer 710*c4b56857SRichard Zhao writel_relaxed(1 << channel, sdma->regs + SDMA_H_STATSTOP); 7111ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 7121ec1e82fSSascha Hauer } 7131ec1e82fSSascha Hauer 7141ec1e82fSSascha Hauer static int sdma_config_channel(struct sdma_channel *sdmac) 7151ec1e82fSSascha Hauer { 7161ec1e82fSSascha Hauer int ret; 7171ec1e82fSSascha Hauer 7181ec1e82fSSascha Hauer sdma_disable_channel(sdmac); 7191ec1e82fSSascha Hauer 7201ec1e82fSSascha Hauer sdmac->event_mask0 = 0; 7211ec1e82fSSascha Hauer sdmac->event_mask1 = 0; 7221ec1e82fSSascha Hauer sdmac->shp_addr = 0; 7231ec1e82fSSascha Hauer sdmac->per_addr = 0; 7241ec1e82fSSascha Hauer 7251ec1e82fSSascha Hauer if (sdmac->event_id0) { 7261ec1e82fSSascha Hauer if (sdmac->event_id0 > 32) 7271ec1e82fSSascha Hauer return -EINVAL; 7281ec1e82fSSascha Hauer sdma_event_enable(sdmac, sdmac->event_id0); 7291ec1e82fSSascha Hauer } 7301ec1e82fSSascha Hauer 7311ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 7321ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 7331ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 7341ec1e82fSSascha Hauer break; 7351ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 7361ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 7371ec1e82fSSascha Hauer break; 7381ec1e82fSSascha Hauer default: 7391ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 7401ec1e82fSSascha Hauer break; 7411ec1e82fSSascha Hauer } 7421ec1e82fSSascha Hauer 7431ec1e82fSSascha Hauer sdma_get_pc(sdmac, sdmac->peripheral_type); 7441ec1e82fSSascha Hauer 7451ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 7461ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 7471ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 7481ec1e82fSSascha Hauer if (sdmac->event_id1) { 7491ec1e82fSSascha Hauer sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32); 7501ec1e82fSSascha Hauer if (sdmac->event_id1 > 31) 7511ec1e82fSSascha Hauer sdmac->watermark_level |= 1 << 31; 7521ec1e82fSSascha Hauer sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32); 7531ec1e82fSSascha Hauer if (sdmac->event_id0 > 31) 7541ec1e82fSSascha Hauer sdmac->watermark_level |= 1 << 30; 7551ec1e82fSSascha Hauer } else { 7561ec1e82fSSascha Hauer sdmac->event_mask0 = 1 << sdmac->event_id0; 7571ec1e82fSSascha Hauer sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32); 7581ec1e82fSSascha Hauer } 7591ec1e82fSSascha Hauer /* Watermark Level */ 7601ec1e82fSSascha Hauer sdmac->watermark_level |= sdmac->watermark_level; 7611ec1e82fSSascha Hauer /* Address */ 7621ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 7631ec1e82fSSascha Hauer } else { 7641ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 7651ec1e82fSSascha Hauer } 7661ec1e82fSSascha Hauer 7671ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 7681ec1e82fSSascha Hauer 7691ec1e82fSSascha Hauer return ret; 7701ec1e82fSSascha Hauer } 7711ec1e82fSSascha Hauer 7721ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 7731ec1e82fSSascha Hauer unsigned int priority) 7741ec1e82fSSascha Hauer { 7751ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7761ec1e82fSSascha Hauer int channel = sdmac->channel; 7771ec1e82fSSascha Hauer 7781ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 7791ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 7801ec1e82fSSascha Hauer return -EINVAL; 7811ec1e82fSSascha Hauer } 7821ec1e82fSSascha Hauer 783*c4b56857SRichard Zhao writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 7841ec1e82fSSascha Hauer 7851ec1e82fSSascha Hauer return 0; 7861ec1e82fSSascha Hauer } 7871ec1e82fSSascha Hauer 7881ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac) 7891ec1e82fSSascha Hauer { 7901ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7911ec1e82fSSascha Hauer int channel = sdmac->channel; 7921ec1e82fSSascha Hauer int ret = -EBUSY; 7931ec1e82fSSascha Hauer 7941ec1e82fSSascha Hauer sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL); 7951ec1e82fSSascha Hauer if (!sdmac->bd) { 7961ec1e82fSSascha Hauer ret = -ENOMEM; 7971ec1e82fSSascha Hauer goto out; 7981ec1e82fSSascha Hauer } 7991ec1e82fSSascha Hauer 8001ec1e82fSSascha Hauer memset(sdmac->bd, 0, PAGE_SIZE); 8011ec1e82fSSascha Hauer 8021ec1e82fSSascha Hauer sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys; 8031ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 8041ec1e82fSSascha Hauer 8051ec1e82fSSascha Hauer clk_enable(sdma->clk); 8061ec1e82fSSascha Hauer 8071ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY); 8081ec1e82fSSascha Hauer 8091ec1e82fSSascha Hauer init_completion(&sdmac->done); 8101ec1e82fSSascha Hauer 8111ec1e82fSSascha Hauer sdmac->buf_tail = 0; 8121ec1e82fSSascha Hauer 8131ec1e82fSSascha Hauer return 0; 8141ec1e82fSSascha Hauer out: 8151ec1e82fSSascha Hauer 8161ec1e82fSSascha Hauer return ret; 8171ec1e82fSSascha Hauer } 8181ec1e82fSSascha Hauer 819d718f4ebSShawn Guo static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac) 8201ec1e82fSSascha Hauer { 821d718f4ebSShawn Guo dma_cookie_t cookie = sdmac->chan.cookie; 8221ec1e82fSSascha Hauer 8231ec1e82fSSascha Hauer if (++cookie < 0) 8241ec1e82fSSascha Hauer cookie = 1; 8251ec1e82fSSascha Hauer 826d718f4ebSShawn Guo sdmac->chan.cookie = cookie; 827d718f4ebSShawn Guo sdmac->desc.cookie = cookie; 8281ec1e82fSSascha Hauer 8291ec1e82fSSascha Hauer return cookie; 8301ec1e82fSSascha Hauer } 8311ec1e82fSSascha Hauer 8321ec1e82fSSascha Hauer static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 8331ec1e82fSSascha Hauer { 8341ec1e82fSSascha Hauer return container_of(chan, struct sdma_channel, chan); 8351ec1e82fSSascha Hauer } 8361ec1e82fSSascha Hauer 8371ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx) 8381ec1e82fSSascha Hauer { 839f69f2e26SHaitao Zhang unsigned long flags; 8401ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(tx->chan); 8411ec1e82fSSascha Hauer dma_cookie_t cookie; 8421ec1e82fSSascha Hauer 843f69f2e26SHaitao Zhang spin_lock_irqsave(&sdmac->lock, flags); 8441ec1e82fSSascha Hauer 8451ec1e82fSSascha Hauer cookie = sdma_assign_cookie(sdmac); 8461ec1e82fSSascha Hauer 847f69f2e26SHaitao Zhang spin_unlock_irqrestore(&sdmac->lock, flags); 8481ec1e82fSSascha Hauer 8491ec1e82fSSascha Hauer return cookie; 8501ec1e82fSSascha Hauer } 8511ec1e82fSSascha Hauer 8521ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 8531ec1e82fSSascha Hauer { 8541ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 8551ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 8561ec1e82fSSascha Hauer int prio, ret; 8571ec1e82fSSascha Hauer 8581ec1e82fSSascha Hauer if (!data) 8591ec1e82fSSascha Hauer return -EINVAL; 8601ec1e82fSSascha Hauer 8611ec1e82fSSascha Hauer switch (data->priority) { 8621ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 8631ec1e82fSSascha Hauer prio = 3; 8641ec1e82fSSascha Hauer break; 8651ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 8661ec1e82fSSascha Hauer prio = 2; 8671ec1e82fSSascha Hauer break; 8681ec1e82fSSascha Hauer case DMA_PRIO_LOW: 8691ec1e82fSSascha Hauer default: 8701ec1e82fSSascha Hauer prio = 1; 8711ec1e82fSSascha Hauer break; 8721ec1e82fSSascha Hauer } 8731ec1e82fSSascha Hauer 8741ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 8751ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 8761ec1e82fSSascha Hauer ret = sdma_set_channel_priority(sdmac, prio); 8771ec1e82fSSascha Hauer if (ret) 8781ec1e82fSSascha Hauer return ret; 8791ec1e82fSSascha Hauer 8801ec1e82fSSascha Hauer ret = sdma_request_channel(sdmac); 8811ec1e82fSSascha Hauer if (ret) 8821ec1e82fSSascha Hauer return ret; 8831ec1e82fSSascha Hauer 8841ec1e82fSSascha Hauer dma_async_tx_descriptor_init(&sdmac->desc, chan); 8851ec1e82fSSascha Hauer sdmac->desc.tx_submit = sdma_tx_submit; 8861ec1e82fSSascha Hauer /* txd.flags will be overwritten in prep funcs */ 8871ec1e82fSSascha Hauer sdmac->desc.flags = DMA_CTRL_ACK; 8881ec1e82fSSascha Hauer 8891ec1e82fSSascha Hauer return 0; 8901ec1e82fSSascha Hauer } 8911ec1e82fSSascha Hauer 8921ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 8931ec1e82fSSascha Hauer { 8941ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 8951ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8961ec1e82fSSascha Hauer 8971ec1e82fSSascha Hauer sdma_disable_channel(sdmac); 8981ec1e82fSSascha Hauer 8991ec1e82fSSascha Hauer if (sdmac->event_id0) 9001ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 9011ec1e82fSSascha Hauer if (sdmac->event_id1) 9021ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 9031ec1e82fSSascha Hauer 9041ec1e82fSSascha Hauer sdmac->event_id0 = 0; 9051ec1e82fSSascha Hauer sdmac->event_id1 = 0; 9061ec1e82fSSascha Hauer 9071ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 9081ec1e82fSSascha Hauer 9091ec1e82fSSascha Hauer dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys); 9101ec1e82fSSascha Hauer 9111ec1e82fSSascha Hauer clk_disable(sdma->clk); 9121ec1e82fSSascha Hauer } 9131ec1e82fSSascha Hauer 9141ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 9151ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 916db8196dfSVinod Koul unsigned int sg_len, enum dma_transfer_direction direction, 9171ec1e82fSSascha Hauer unsigned long flags) 9181ec1e82fSSascha Hauer { 9191ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 9201ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9211ec1e82fSSascha Hauer int ret, i, count; 92223889c63SSascha Hauer int channel = sdmac->channel; 9231ec1e82fSSascha Hauer struct scatterlist *sg; 9241ec1e82fSSascha Hauer 9251ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 9261ec1e82fSSascha Hauer return NULL; 9271ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 9281ec1e82fSSascha Hauer 9291ec1e82fSSascha Hauer sdmac->flags = 0; 9301ec1e82fSSascha Hauer 9311ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 9321ec1e82fSSascha Hauer sg_len, channel); 9331ec1e82fSSascha Hauer 9341ec1e82fSSascha Hauer sdmac->direction = direction; 9351ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 9361ec1e82fSSascha Hauer if (ret) 9371ec1e82fSSascha Hauer goto err_out; 9381ec1e82fSSascha Hauer 9391ec1e82fSSascha Hauer if (sg_len > NUM_BD) { 9401ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 9411ec1e82fSSascha Hauer channel, sg_len, NUM_BD); 9421ec1e82fSSascha Hauer ret = -EINVAL; 9431ec1e82fSSascha Hauer goto err_out; 9441ec1e82fSSascha Hauer } 9451ec1e82fSSascha Hauer 946ab59a510SHuang Shijie sdmac->chn_count = 0; 9471ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 9481ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 9491ec1e82fSSascha Hauer int param; 9501ec1e82fSSascha Hauer 951d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 9521ec1e82fSSascha Hauer 9531ec1e82fSSascha Hauer count = sg->length; 9541ec1e82fSSascha Hauer 9551ec1e82fSSascha Hauer if (count > 0xffff) { 9561ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 9571ec1e82fSSascha Hauer channel, count, 0xffff); 9581ec1e82fSSascha Hauer ret = -EINVAL; 9591ec1e82fSSascha Hauer goto err_out; 9601ec1e82fSSascha Hauer } 9611ec1e82fSSascha Hauer 9621ec1e82fSSascha Hauer bd->mode.count = count; 963ab59a510SHuang Shijie sdmac->chn_count += count; 9641ec1e82fSSascha Hauer 9651ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { 9661ec1e82fSSascha Hauer ret = -EINVAL; 9671ec1e82fSSascha Hauer goto err_out; 9681ec1e82fSSascha Hauer } 9691fa81c27SSascha Hauer 9701fa81c27SSascha Hauer switch (sdmac->word_size) { 9711fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 9721ec1e82fSSascha Hauer bd->mode.command = 0; 9731fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 9741fa81c27SSascha Hauer return NULL; 9751fa81c27SSascha Hauer break; 9761fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 9771fa81c27SSascha Hauer bd->mode.command = 2; 9781fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 9791fa81c27SSascha Hauer return NULL; 9801fa81c27SSascha Hauer break; 9811fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 9821fa81c27SSascha Hauer bd->mode.command = 1; 9831fa81c27SSascha Hauer break; 9841fa81c27SSascha Hauer default: 9851fa81c27SSascha Hauer return NULL; 9861fa81c27SSascha Hauer } 9871ec1e82fSSascha Hauer 9881ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 9891ec1e82fSSascha Hauer 990341b9419SShawn Guo if (i + 1 == sg_len) { 9911ec1e82fSSascha Hauer param |= BD_INTR; 992341b9419SShawn Guo param |= BD_LAST; 993341b9419SShawn Guo param &= ~BD_CONT; 9941ec1e82fSSascha Hauer } 9951ec1e82fSSascha Hauer 9961ec1e82fSSascha Hauer dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n", 9971ec1e82fSSascha Hauer i, count, sg->dma_address, 9981ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 9991ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 10001ec1e82fSSascha Hauer 10011ec1e82fSSascha Hauer bd->mode.status = param; 10021ec1e82fSSascha Hauer } 10031ec1e82fSSascha Hauer 10041ec1e82fSSascha Hauer sdmac->num_bd = sg_len; 10051ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 10061ec1e82fSSascha Hauer 10071ec1e82fSSascha Hauer return &sdmac->desc; 10081ec1e82fSSascha Hauer err_out: 10094b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 10101ec1e82fSSascha Hauer return NULL; 10111ec1e82fSSascha Hauer } 10121ec1e82fSSascha Hauer 10131ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 10141ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 1015db8196dfSVinod Koul size_t period_len, enum dma_transfer_direction direction) 10161ec1e82fSSascha Hauer { 10171ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 10181ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 10191ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 102023889c63SSascha Hauer int channel = sdmac->channel; 10211ec1e82fSSascha Hauer int ret, i = 0, buf = 0; 10221ec1e82fSSascha Hauer 10231ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 10241ec1e82fSSascha Hauer 10251ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 10261ec1e82fSSascha Hauer return NULL; 10271ec1e82fSSascha Hauer 10281ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 10291ec1e82fSSascha Hauer 10301ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 10311ec1e82fSSascha Hauer sdmac->direction = direction; 10321ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 10331ec1e82fSSascha Hauer if (ret) 10341ec1e82fSSascha Hauer goto err_out; 10351ec1e82fSSascha Hauer 10361ec1e82fSSascha Hauer if (num_periods > NUM_BD) { 10371ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 10381ec1e82fSSascha Hauer channel, num_periods, NUM_BD); 10391ec1e82fSSascha Hauer goto err_out; 10401ec1e82fSSascha Hauer } 10411ec1e82fSSascha Hauer 10421ec1e82fSSascha Hauer if (period_len > 0xffff) { 10431ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n", 10441ec1e82fSSascha Hauer channel, period_len, 0xffff); 10451ec1e82fSSascha Hauer goto err_out; 10461ec1e82fSSascha Hauer } 10471ec1e82fSSascha Hauer 10481ec1e82fSSascha Hauer while (buf < buf_len) { 10491ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 10501ec1e82fSSascha Hauer int param; 10511ec1e82fSSascha Hauer 10521ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 10531ec1e82fSSascha Hauer 10541ec1e82fSSascha Hauer bd->mode.count = period_len; 10551ec1e82fSSascha Hauer 10561ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 10571ec1e82fSSascha Hauer goto err_out; 10581ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 10591ec1e82fSSascha Hauer bd->mode.command = 0; 10601ec1e82fSSascha Hauer else 10611ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 10621ec1e82fSSascha Hauer 10631ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 10641ec1e82fSSascha Hauer if (i + 1 == num_periods) 10651ec1e82fSSascha Hauer param |= BD_WRAP; 10661ec1e82fSSascha Hauer 10671ec1e82fSSascha Hauer dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n", 10681ec1e82fSSascha Hauer i, period_len, dma_addr, 10691ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 10701ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 10711ec1e82fSSascha Hauer 10721ec1e82fSSascha Hauer bd->mode.status = param; 10731ec1e82fSSascha Hauer 10741ec1e82fSSascha Hauer dma_addr += period_len; 10751ec1e82fSSascha Hauer buf += period_len; 10761ec1e82fSSascha Hauer 10771ec1e82fSSascha Hauer i++; 10781ec1e82fSSascha Hauer } 10791ec1e82fSSascha Hauer 10801ec1e82fSSascha Hauer sdmac->num_bd = num_periods; 10811ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 10821ec1e82fSSascha Hauer 10831ec1e82fSSascha Hauer return &sdmac->desc; 10841ec1e82fSSascha Hauer err_out: 10851ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 10861ec1e82fSSascha Hauer return NULL; 10871ec1e82fSSascha Hauer } 10881ec1e82fSSascha Hauer 10891ec1e82fSSascha Hauer static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 10901ec1e82fSSascha Hauer unsigned long arg) 10911ec1e82fSSascha Hauer { 10921ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 10931ec1e82fSSascha Hauer struct dma_slave_config *dmaengine_cfg = (void *)arg; 10941ec1e82fSSascha Hauer 10951ec1e82fSSascha Hauer switch (cmd) { 10961ec1e82fSSascha Hauer case DMA_TERMINATE_ALL: 10971ec1e82fSSascha Hauer sdma_disable_channel(sdmac); 10981ec1e82fSSascha Hauer return 0; 10991ec1e82fSSascha Hauer case DMA_SLAVE_CONFIG: 1100db8196dfSVinod Koul if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) { 11011ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 11021ec1e82fSSascha Hauer sdmac->watermark_level = dmaengine_cfg->src_maxburst; 11031ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 11041ec1e82fSSascha Hauer } else { 11051ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 11061ec1e82fSSascha Hauer sdmac->watermark_level = dmaengine_cfg->dst_maxburst; 11071ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 11081ec1e82fSSascha Hauer } 1109e6966433SHuang Shijie sdmac->direction = dmaengine_cfg->direction; 11101ec1e82fSSascha Hauer return sdma_config_channel(sdmac); 11111ec1e82fSSascha Hauer default: 11121ec1e82fSSascha Hauer return -ENOSYS; 11131ec1e82fSSascha Hauer } 11141ec1e82fSSascha Hauer 11151ec1e82fSSascha Hauer return -EINVAL; 11161ec1e82fSSascha Hauer } 11171ec1e82fSSascha Hauer 11181ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 11191ec1e82fSSascha Hauer dma_cookie_t cookie, 11201ec1e82fSSascha Hauer struct dma_tx_state *txstate) 11211ec1e82fSSascha Hauer { 11221ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11231ec1e82fSSascha Hauer dma_cookie_t last_used; 11241ec1e82fSSascha Hauer 11251ec1e82fSSascha Hauer last_used = chan->cookie; 11261ec1e82fSSascha Hauer 1127ab59a510SHuang Shijie dma_set_tx_state(txstate, sdmac->last_completed, last_used, 1128ab59a510SHuang Shijie sdmac->chn_count - sdmac->chn_real_count); 11291ec1e82fSSascha Hauer 11308a965911SShawn Guo return sdmac->status; 11311ec1e82fSSascha Hauer } 11321ec1e82fSSascha Hauer 11331ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 11341ec1e82fSSascha Hauer { 11352b4f130eSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 11362b4f130eSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 11372b4f130eSSascha Hauer 11382b4f130eSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 11392b4f130eSSascha Hauer sdma_enable_channel(sdma, sdmac->channel); 11401ec1e82fSSascha Hauer } 11411ec1e82fSSascha Hauer 11425b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 11435b28aa31SSascha Hauer 11445b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 11455b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 11465b28aa31SSascha Hauer { 11475b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 11485b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 11495b28aa31SSascha Hauer int i; 11505b28aa31SSascha Hauer 11515b28aa31SSascha Hauer for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) 11525b28aa31SSascha Hauer if (addr_arr[i] > 0) 11535b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 11545b28aa31SSascha Hauer } 11555b28aa31SSascha Hauer 11567b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context) 11575b28aa31SSascha Hauer { 11587b4b88e0SSascha Hauer struct sdma_engine *sdma = context; 11595b28aa31SSascha Hauer const struct sdma_firmware_header *header; 11605b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 11615b28aa31SSascha Hauer unsigned short *ram_code; 11625b28aa31SSascha Hauer 11637b4b88e0SSascha Hauer if (!fw) { 11647b4b88e0SSascha Hauer dev_err(sdma->dev, "firmware not found\n"); 11657b4b88e0SSascha Hauer return; 11667b4b88e0SSascha Hauer } 11675b28aa31SSascha Hauer 11685b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 11695b28aa31SSascha Hauer goto err_firmware; 11705b28aa31SSascha Hauer 11715b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 11725b28aa31SSascha Hauer 11735b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 11745b28aa31SSascha Hauer goto err_firmware; 11755b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 11765b28aa31SSascha Hauer goto err_firmware; 11775b28aa31SSascha Hauer 11785b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 11795b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 11805b28aa31SSascha Hauer 11815b28aa31SSascha Hauer clk_enable(sdma->clk); 11825b28aa31SSascha Hauer /* download the RAM image for SDMA */ 11835b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 11845b28aa31SSascha Hauer header->ram_code_size, 11856866fd3bSSascha Hauer addr->ram_code_start_addr); 11865b28aa31SSascha Hauer clk_disable(sdma->clk); 11875b28aa31SSascha Hauer 11885b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 11895b28aa31SSascha Hauer 11905b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 11915b28aa31SSascha Hauer header->version_major, 11925b28aa31SSascha Hauer header->version_minor); 11935b28aa31SSascha Hauer 11945b28aa31SSascha Hauer err_firmware: 11955b28aa31SSascha Hauer release_firmware(fw); 11967b4b88e0SSascha Hauer } 11977b4b88e0SSascha Hauer 11987b4b88e0SSascha Hauer static int __init sdma_get_firmware(struct sdma_engine *sdma, 11997b4b88e0SSascha Hauer const char *fw_name) 12007b4b88e0SSascha Hauer { 12017b4b88e0SSascha Hauer int ret; 12027b4b88e0SSascha Hauer 12037b4b88e0SSascha Hauer ret = request_firmware_nowait(THIS_MODULE, 12047b4b88e0SSascha Hauer FW_ACTION_HOTPLUG, fw_name, sdma->dev, 12057b4b88e0SSascha Hauer GFP_KERNEL, sdma, sdma_load_firmware); 12065b28aa31SSascha Hauer 12075b28aa31SSascha Hauer return ret; 12085b28aa31SSascha Hauer } 12095b28aa31SSascha Hauer 12105b28aa31SSascha Hauer static int __init sdma_init(struct sdma_engine *sdma) 12111ec1e82fSSascha Hauer { 12121ec1e82fSSascha Hauer int i, ret; 12131ec1e82fSSascha Hauer dma_addr_t ccb_phys; 12141ec1e82fSSascha Hauer 121562550cd7SShawn Guo switch (sdma->devtype) { 121662550cd7SShawn Guo case IMX31_SDMA: 12171ec1e82fSSascha Hauer sdma->num_events = 32; 12181ec1e82fSSascha Hauer break; 121962550cd7SShawn Guo case IMX35_SDMA: 12201ec1e82fSSascha Hauer sdma->num_events = 48; 12211ec1e82fSSascha Hauer break; 12221ec1e82fSSascha Hauer default: 122362550cd7SShawn Guo dev_err(sdma->dev, "Unknown sdma type %d. aborting\n", 122462550cd7SShawn Guo sdma->devtype); 12251ec1e82fSSascha Hauer return -ENODEV; 12261ec1e82fSSascha Hauer } 12271ec1e82fSSascha Hauer 12281ec1e82fSSascha Hauer clk_enable(sdma->clk); 12291ec1e82fSSascha Hauer 12301ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 1231*c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_C0PTR); 12321ec1e82fSSascha Hauer 12331ec1e82fSSascha Hauer sdma->channel_control = dma_alloc_coherent(NULL, 12341ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + 12351ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 12361ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 12371ec1e82fSSascha Hauer 12381ec1e82fSSascha Hauer if (!sdma->channel_control) { 12391ec1e82fSSascha Hauer ret = -ENOMEM; 12401ec1e82fSSascha Hauer goto err_dma_alloc; 12411ec1e82fSSascha Hauer } 12421ec1e82fSSascha Hauer 12431ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 12441ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 12451ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 12461ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 12471ec1e82fSSascha Hauer 12481ec1e82fSSascha Hauer /* Zero-out the CCB structures array just allocated */ 12491ec1e82fSSascha Hauer memset(sdma->channel_control, 0, 12501ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control)); 12511ec1e82fSSascha Hauer 12521ec1e82fSSascha Hauer /* disable all channels */ 12531ec1e82fSSascha Hauer for (i = 0; i < sdma->num_events; i++) 1254*c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i)); 12551ec1e82fSSascha Hauer 12561ec1e82fSSascha Hauer /* All channels have priority 0 */ 12571ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 1258*c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 12591ec1e82fSSascha Hauer 12601ec1e82fSSascha Hauer ret = sdma_request_channel(&sdma->channel[0]); 12611ec1e82fSSascha Hauer if (ret) 12621ec1e82fSSascha Hauer goto err_dma_alloc; 12631ec1e82fSSascha Hauer 12641ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 12651ec1e82fSSascha Hauer 12661ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 1267*c4b56857SRichard Zhao writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR); 12681ec1e82fSSascha Hauer 12691ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 12701ec1e82fSSascha Hauer /* FIXME: Check whether to set ACR bit depending on clock ratios */ 1271*c4b56857SRichard Zhao writel_relaxed(0, sdma->regs + SDMA_H_CONFIG); 12721ec1e82fSSascha Hauer 1273*c4b56857SRichard Zhao writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR); 12741ec1e82fSSascha Hauer 12751ec1e82fSSascha Hauer /* Set bits of CONFIG register with given context switching mode */ 1276*c4b56857SRichard Zhao writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG); 12771ec1e82fSSascha Hauer 12781ec1e82fSSascha Hauer /* Initializes channel's priorities */ 12791ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 12801ec1e82fSSascha Hauer 12811ec1e82fSSascha Hauer clk_disable(sdma->clk); 12821ec1e82fSSascha Hauer 12831ec1e82fSSascha Hauer return 0; 12841ec1e82fSSascha Hauer 12851ec1e82fSSascha Hauer err_dma_alloc: 12861ec1e82fSSascha Hauer clk_disable(sdma->clk); 12871ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 12881ec1e82fSSascha Hauer return ret; 12891ec1e82fSSascha Hauer } 12901ec1e82fSSascha Hauer 12911ec1e82fSSascha Hauer static int __init sdma_probe(struct platform_device *pdev) 12921ec1e82fSSascha Hauer { 1293580975d7SShawn Guo const struct of_device_id *of_id = 1294580975d7SShawn Guo of_match_device(sdma_dt_ids, &pdev->dev); 1295580975d7SShawn Guo struct device_node *np = pdev->dev.of_node; 1296580975d7SShawn Guo const char *fw_name; 12971ec1e82fSSascha Hauer int ret; 12981ec1e82fSSascha Hauer int irq; 12991ec1e82fSSascha Hauer struct resource *iores; 13001ec1e82fSSascha Hauer struct sdma_platform_data *pdata = pdev->dev.platform_data; 13011ec1e82fSSascha Hauer int i; 13021ec1e82fSSascha Hauer struct sdma_engine *sdma; 130336e2f21aSSascha Hauer s32 *saddr_arr; 13041ec1e82fSSascha Hauer 13051ec1e82fSSascha Hauer sdma = kzalloc(sizeof(*sdma), GFP_KERNEL); 13061ec1e82fSSascha Hauer if (!sdma) 13071ec1e82fSSascha Hauer return -ENOMEM; 13081ec1e82fSSascha Hauer 130973eab978SSascha Hauer mutex_init(&sdma->channel_0_lock); 131073eab978SSascha Hauer 13111ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 13121ec1e82fSSascha Hauer 13131ec1e82fSSascha Hauer iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 13141ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 1315580975d7SShawn Guo if (!iores || irq < 0) { 13161ec1e82fSSascha Hauer ret = -EINVAL; 13171ec1e82fSSascha Hauer goto err_irq; 13181ec1e82fSSascha Hauer } 13191ec1e82fSSascha Hauer 13201ec1e82fSSascha Hauer if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) { 13211ec1e82fSSascha Hauer ret = -EBUSY; 13221ec1e82fSSascha Hauer goto err_request_region; 13231ec1e82fSSascha Hauer } 13241ec1e82fSSascha Hauer 13251ec1e82fSSascha Hauer sdma->clk = clk_get(&pdev->dev, NULL); 13261ec1e82fSSascha Hauer if (IS_ERR(sdma->clk)) { 13271ec1e82fSSascha Hauer ret = PTR_ERR(sdma->clk); 13281ec1e82fSSascha Hauer goto err_clk; 13291ec1e82fSSascha Hauer } 13301ec1e82fSSascha Hauer 13311ec1e82fSSascha Hauer sdma->regs = ioremap(iores->start, resource_size(iores)); 13321ec1e82fSSascha Hauer if (!sdma->regs) { 13331ec1e82fSSascha Hauer ret = -ENOMEM; 13341ec1e82fSSascha Hauer goto err_ioremap; 13351ec1e82fSSascha Hauer } 13361ec1e82fSSascha Hauer 13371ec1e82fSSascha Hauer ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma); 13381ec1e82fSSascha Hauer if (ret) 13391ec1e82fSSascha Hauer goto err_request_irq; 13401ec1e82fSSascha Hauer 13415b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 13421c1d9547SAxel Lin if (!sdma->script_addrs) { 13431c1d9547SAxel Lin ret = -ENOMEM; 13445b28aa31SSascha Hauer goto err_alloc; 13451c1d9547SAxel Lin } 13461ec1e82fSSascha Hauer 134736e2f21aSSascha Hauer /* initially no scripts available */ 134836e2f21aSSascha Hauer saddr_arr = (s32 *)sdma->script_addrs; 134936e2f21aSSascha Hauer for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) 135036e2f21aSSascha Hauer saddr_arr[i] = -EINVAL; 135136e2f21aSSascha Hauer 1352580975d7SShawn Guo if (of_id) 1353580975d7SShawn Guo pdev->id_entry = of_id->data; 135462550cd7SShawn Guo sdma->devtype = pdev->id_entry->driver_data; 13551ec1e82fSSascha Hauer 13567214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 13577214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 13587214a8b1SSascha Hauer 13591ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 13601ec1e82fSSascha Hauer /* Initialize channel parameters */ 13611ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 13621ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 13631ec1e82fSSascha Hauer 13641ec1e82fSSascha Hauer sdmac->sdma = sdma; 13651ec1e82fSSascha Hauer spin_lock_init(&sdmac->lock); 13661ec1e82fSSascha Hauer 13671ec1e82fSSascha Hauer sdmac->chan.device = &sdma->dma_device; 13681ec1e82fSSascha Hauer sdmac->channel = i; 13691ec1e82fSSascha Hauer 137023889c63SSascha Hauer /* 137123889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 137223889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 137323889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 137423889c63SSascha Hauer */ 137523889c63SSascha Hauer if (i) 137623889c63SSascha Hauer list_add_tail(&sdmac->chan.device_node, 137723889c63SSascha Hauer &sdma->dma_device.channels); 13781ec1e82fSSascha Hauer } 13791ec1e82fSSascha Hauer 13805b28aa31SSascha Hauer ret = sdma_init(sdma); 13811ec1e82fSSascha Hauer if (ret) 13821ec1e82fSSascha Hauer goto err_init; 13831ec1e82fSSascha Hauer 1384580975d7SShawn Guo if (pdata && pdata->script_addrs) 13855b28aa31SSascha Hauer sdma_add_scripts(sdma, pdata->script_addrs); 13865b28aa31SSascha Hauer 1387580975d7SShawn Guo if (pdata) { 13882e534b21SShawn Guo sdma_get_firmware(sdma, pdata->fw_name); 1389580975d7SShawn Guo } else { 1390580975d7SShawn Guo /* 1391580975d7SShawn Guo * Because that device tree does not encode ROM script address, 1392580975d7SShawn Guo * the RAM script in firmware is mandatory for device tree 1393580975d7SShawn Guo * probe, otherwise it fails. 1394580975d7SShawn Guo */ 1395580975d7SShawn Guo ret = of_property_read_string(np, "fsl,sdma-ram-script-name", 1396580975d7SShawn Guo &fw_name); 1397580975d7SShawn Guo if (ret) { 1398580975d7SShawn Guo dev_err(&pdev->dev, "failed to get firmware name\n"); 1399580975d7SShawn Guo goto err_init; 1400580975d7SShawn Guo } 1401580975d7SShawn Guo 1402580975d7SShawn Guo ret = sdma_get_firmware(sdma, fw_name); 1403580975d7SShawn Guo if (ret) { 1404580975d7SShawn Guo dev_err(&pdev->dev, "failed to get firmware\n"); 1405580975d7SShawn Guo goto err_init; 1406580975d7SShawn Guo } 1407580975d7SShawn Guo } 14085b28aa31SSascha Hauer 14091ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 14101ec1e82fSSascha Hauer 14111ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 14121ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 14131ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 14141ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 14151ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 14161ec1e82fSSascha Hauer sdma->dma_device.device_control = sdma_control; 14171ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 1418b9b3f82fSSascha Hauer sdma->dma_device.dev->dma_parms = &sdma->dma_parms; 1419b9b3f82fSSascha Hauer dma_set_max_seg_size(sdma->dma_device.dev, 65535); 14201ec1e82fSSascha Hauer 14211ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 14221ec1e82fSSascha Hauer if (ret) { 14231ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 14241ec1e82fSSascha Hauer goto err_init; 14251ec1e82fSSascha Hauer } 14261ec1e82fSSascha Hauer 14275b28aa31SSascha Hauer dev_info(sdma->dev, "initialized\n"); 14281ec1e82fSSascha Hauer 14291ec1e82fSSascha Hauer return 0; 14301ec1e82fSSascha Hauer 14311ec1e82fSSascha Hauer err_init: 14321ec1e82fSSascha Hauer kfree(sdma->script_addrs); 14335b28aa31SSascha Hauer err_alloc: 14341ec1e82fSSascha Hauer free_irq(irq, sdma); 14351ec1e82fSSascha Hauer err_request_irq: 14361ec1e82fSSascha Hauer iounmap(sdma->regs); 14371ec1e82fSSascha Hauer err_ioremap: 14381ec1e82fSSascha Hauer clk_put(sdma->clk); 14391ec1e82fSSascha Hauer err_clk: 14401ec1e82fSSascha Hauer release_mem_region(iores->start, resource_size(iores)); 14411ec1e82fSSascha Hauer err_request_region: 14421ec1e82fSSascha Hauer err_irq: 14431ec1e82fSSascha Hauer kfree(sdma); 1444939fd4f0SShawn Guo return ret; 14451ec1e82fSSascha Hauer } 14461ec1e82fSSascha Hauer 14471ec1e82fSSascha Hauer static int __exit sdma_remove(struct platform_device *pdev) 14481ec1e82fSSascha Hauer { 14491ec1e82fSSascha Hauer return -EBUSY; 14501ec1e82fSSascha Hauer } 14511ec1e82fSSascha Hauer 14521ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 14531ec1e82fSSascha Hauer .driver = { 14541ec1e82fSSascha Hauer .name = "imx-sdma", 1455580975d7SShawn Guo .of_match_table = sdma_dt_ids, 14561ec1e82fSSascha Hauer }, 145762550cd7SShawn Guo .id_table = sdma_devtypes, 14581ec1e82fSSascha Hauer .remove = __exit_p(sdma_remove), 14591ec1e82fSSascha Hauer }; 14601ec1e82fSSascha Hauer 14611ec1e82fSSascha Hauer static int __init sdma_module_init(void) 14621ec1e82fSSascha Hauer { 14631ec1e82fSSascha Hauer return platform_driver_probe(&sdma_driver, sdma_probe); 14641ec1e82fSSascha Hauer } 1465c989a7fcSSascha Hauer module_init(sdma_module_init); 14661ec1e82fSSascha Hauer 14671ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 14681ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 14691ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 1470