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> 211ec1e82fSSascha Hauer #include <linux/types.h> 221ec1e82fSSascha Hauer #include <linux/mm.h> 231ec1e82fSSascha Hauer #include <linux/interrupt.h> 241ec1e82fSSascha Hauer #include <linux/clk.h> 251ec1e82fSSascha Hauer #include <linux/wait.h> 261ec1e82fSSascha Hauer #include <linux/sched.h> 271ec1e82fSSascha Hauer #include <linux/semaphore.h> 281ec1e82fSSascha Hauer #include <linux/spinlock.h> 291ec1e82fSSascha Hauer #include <linux/device.h> 301ec1e82fSSascha Hauer #include <linux/dma-mapping.h> 311ec1e82fSSascha Hauer #include <linux/firmware.h> 321ec1e82fSSascha Hauer #include <linux/slab.h> 331ec1e82fSSascha Hauer #include <linux/platform_device.h> 341ec1e82fSSascha Hauer #include <linux/dmaengine.h> 351ec1e82fSSascha Hauer 361ec1e82fSSascha Hauer #include <asm/irq.h> 371ec1e82fSSascha Hauer #include <mach/sdma.h> 381ec1e82fSSascha Hauer #include <mach/dma.h> 391ec1e82fSSascha Hauer #include <mach/hardware.h> 401ec1e82fSSascha Hauer 411ec1e82fSSascha Hauer /* SDMA registers */ 421ec1e82fSSascha Hauer #define SDMA_H_C0PTR 0x000 431ec1e82fSSascha Hauer #define SDMA_H_INTR 0x004 441ec1e82fSSascha Hauer #define SDMA_H_STATSTOP 0x008 451ec1e82fSSascha Hauer #define SDMA_H_START 0x00c 461ec1e82fSSascha Hauer #define SDMA_H_EVTOVR 0x010 471ec1e82fSSascha Hauer #define SDMA_H_DSPOVR 0x014 481ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR 0x018 491ec1e82fSSascha Hauer #define SDMA_H_EVTPEND 0x01c 501ec1e82fSSascha Hauer #define SDMA_H_DSPENBL 0x020 511ec1e82fSSascha Hauer #define SDMA_H_RESET 0x024 521ec1e82fSSascha Hauer #define SDMA_H_EVTERR 0x028 531ec1e82fSSascha Hauer #define SDMA_H_INTRMSK 0x02c 541ec1e82fSSascha Hauer #define SDMA_H_PSW 0x030 551ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG 0x034 561ec1e82fSSascha Hauer #define SDMA_H_CONFIG 0x038 571ec1e82fSSascha Hauer #define SDMA_ONCE_ENB 0x040 581ec1e82fSSascha Hauer #define SDMA_ONCE_DATA 0x044 591ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR 0x048 601ec1e82fSSascha Hauer #define SDMA_ONCE_STAT 0x04c 611ec1e82fSSascha Hauer #define SDMA_ONCE_CMD 0x050 621ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR 0x054 631ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR 0x058 641ec1e82fSSascha Hauer #define SDMA_CHN0ADDR 0x05c 651ec1e82fSSascha Hauer #define SDMA_ONCE_RTB 0x060 661ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1 0x070 671ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2 0x074 681ec1e82fSSascha Hauer #define SDMA_CHNENBL0_V2 0x200 691ec1e82fSSascha Hauer #define SDMA_CHNENBL0_V1 0x080 701ec1e82fSSascha Hauer #define SDMA_CHNPRI_0 0x100 711ec1e82fSSascha Hauer 721ec1e82fSSascha Hauer /* 731ec1e82fSSascha Hauer * Buffer descriptor status values. 741ec1e82fSSascha Hauer */ 751ec1e82fSSascha Hauer #define BD_DONE 0x01 761ec1e82fSSascha Hauer #define BD_WRAP 0x02 771ec1e82fSSascha Hauer #define BD_CONT 0x04 781ec1e82fSSascha Hauer #define BD_INTR 0x08 791ec1e82fSSascha Hauer #define BD_RROR 0x10 801ec1e82fSSascha Hauer #define BD_LAST 0x20 811ec1e82fSSascha Hauer #define BD_EXTD 0x80 821ec1e82fSSascha Hauer 831ec1e82fSSascha Hauer /* 841ec1e82fSSascha Hauer * Data Node descriptor status values. 851ec1e82fSSascha Hauer */ 861ec1e82fSSascha Hauer #define DND_END_OF_FRAME 0x80 871ec1e82fSSascha Hauer #define DND_END_OF_XFER 0x40 881ec1e82fSSascha Hauer #define DND_DONE 0x20 891ec1e82fSSascha Hauer #define DND_UNUSED 0x01 901ec1e82fSSascha Hauer 911ec1e82fSSascha Hauer /* 921ec1e82fSSascha Hauer * IPCV2 descriptor status values. 931ec1e82fSSascha Hauer */ 941ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME 0x40 951ec1e82fSSascha Hauer 961ec1e82fSSascha Hauer #define IPCV2_MAX_NODES 50 971ec1e82fSSascha Hauer /* 981ec1e82fSSascha Hauer * Error bit set in the CCB status field by the SDMA, 991ec1e82fSSascha Hauer * in setbd routine, in case of a transfer error 1001ec1e82fSSascha Hauer */ 1011ec1e82fSSascha Hauer #define DATA_ERROR 0x10000000 1021ec1e82fSSascha Hauer 1031ec1e82fSSascha Hauer /* 1041ec1e82fSSascha Hauer * Buffer descriptor commands. 1051ec1e82fSSascha Hauer */ 1061ec1e82fSSascha Hauer #define C0_ADDR 0x01 1071ec1e82fSSascha Hauer #define C0_LOAD 0x02 1081ec1e82fSSascha Hauer #define C0_DUMP 0x03 1091ec1e82fSSascha Hauer #define C0_SETCTX 0x07 1101ec1e82fSSascha Hauer #define C0_GETCTX 0x03 1111ec1e82fSSascha Hauer #define C0_SETDM 0x01 1121ec1e82fSSascha Hauer #define C0_SETPM 0x04 1131ec1e82fSSascha Hauer #define C0_GETDM 0x02 1141ec1e82fSSascha Hauer #define C0_GETPM 0x08 1151ec1e82fSSascha Hauer /* 1161ec1e82fSSascha Hauer * Change endianness indicator in the BD command field 1171ec1e82fSSascha Hauer */ 1181ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS 0x80 1191ec1e82fSSascha Hauer 1201ec1e82fSSascha Hauer /* 1211ec1e82fSSascha Hauer * Mode/Count of data node descriptors - IPCv2 1221ec1e82fSSascha Hauer */ 1231ec1e82fSSascha Hauer struct sdma_mode_count { 1241ec1e82fSSascha Hauer u32 count : 16; /* size of the buffer pointed by this BD */ 1251ec1e82fSSascha Hauer u32 status : 8; /* E,R,I,C,W,D status bits stored here */ 1261ec1e82fSSascha Hauer u32 command : 8; /* command mostlky used for channel 0 */ 1271ec1e82fSSascha Hauer }; 1281ec1e82fSSascha Hauer 1291ec1e82fSSascha Hauer /* 1301ec1e82fSSascha Hauer * Buffer descriptor 1311ec1e82fSSascha Hauer */ 1321ec1e82fSSascha Hauer struct sdma_buffer_descriptor { 1331ec1e82fSSascha Hauer struct sdma_mode_count mode; 1341ec1e82fSSascha Hauer u32 buffer_addr; /* address of the buffer described */ 1351ec1e82fSSascha Hauer u32 ext_buffer_addr; /* extended buffer address */ 1361ec1e82fSSascha Hauer } __attribute__ ((packed)); 1371ec1e82fSSascha Hauer 1381ec1e82fSSascha Hauer /** 1391ec1e82fSSascha Hauer * struct sdma_channel_control - Channel control Block 1401ec1e82fSSascha Hauer * 1411ec1e82fSSascha Hauer * @current_bd_ptr current buffer descriptor processed 1421ec1e82fSSascha Hauer * @base_bd_ptr first element of buffer descriptor array 1431ec1e82fSSascha Hauer * @unused padding. The SDMA engine expects an array of 128 byte 1441ec1e82fSSascha Hauer * control blocks 1451ec1e82fSSascha Hauer */ 1461ec1e82fSSascha Hauer struct sdma_channel_control { 1471ec1e82fSSascha Hauer u32 current_bd_ptr; 1481ec1e82fSSascha Hauer u32 base_bd_ptr; 1491ec1e82fSSascha Hauer u32 unused[2]; 1501ec1e82fSSascha Hauer } __attribute__ ((packed)); 1511ec1e82fSSascha Hauer 1521ec1e82fSSascha Hauer /** 1531ec1e82fSSascha Hauer * struct sdma_state_registers - SDMA context for a channel 1541ec1e82fSSascha Hauer * 1551ec1e82fSSascha Hauer * @pc: program counter 1561ec1e82fSSascha Hauer * @t: test bit: status of arithmetic & test instruction 1571ec1e82fSSascha Hauer * @rpc: return program counter 1581ec1e82fSSascha Hauer * @sf: source fault while loading data 1591ec1e82fSSascha Hauer * @spc: loop start program counter 1601ec1e82fSSascha Hauer * @df: destination fault while storing data 1611ec1e82fSSascha Hauer * @epc: loop end program counter 1621ec1e82fSSascha Hauer * @lm: loop mode 1631ec1e82fSSascha Hauer */ 1641ec1e82fSSascha Hauer struct sdma_state_registers { 1651ec1e82fSSascha Hauer u32 pc :14; 1661ec1e82fSSascha Hauer u32 unused1: 1; 1671ec1e82fSSascha Hauer u32 t : 1; 1681ec1e82fSSascha Hauer u32 rpc :14; 1691ec1e82fSSascha Hauer u32 unused0: 1; 1701ec1e82fSSascha Hauer u32 sf : 1; 1711ec1e82fSSascha Hauer u32 spc :14; 1721ec1e82fSSascha Hauer u32 unused2: 1; 1731ec1e82fSSascha Hauer u32 df : 1; 1741ec1e82fSSascha Hauer u32 epc :14; 1751ec1e82fSSascha Hauer u32 lm : 2; 1761ec1e82fSSascha Hauer } __attribute__ ((packed)); 1771ec1e82fSSascha Hauer 1781ec1e82fSSascha Hauer /** 1791ec1e82fSSascha Hauer * struct sdma_context_data - sdma context specific to a channel 1801ec1e82fSSascha Hauer * 1811ec1e82fSSascha Hauer * @channel_state: channel state bits 1821ec1e82fSSascha Hauer * @gReg: general registers 1831ec1e82fSSascha Hauer * @mda: burst dma destination address register 1841ec1e82fSSascha Hauer * @msa: burst dma source address register 1851ec1e82fSSascha Hauer * @ms: burst dma status register 1861ec1e82fSSascha Hauer * @md: burst dma data register 1871ec1e82fSSascha Hauer * @pda: peripheral dma destination address register 1881ec1e82fSSascha Hauer * @psa: peripheral dma source address register 1891ec1e82fSSascha Hauer * @ps: peripheral dma status register 1901ec1e82fSSascha Hauer * @pd: peripheral dma data register 1911ec1e82fSSascha Hauer * @ca: CRC polynomial register 1921ec1e82fSSascha Hauer * @cs: CRC accumulator register 1931ec1e82fSSascha Hauer * @dda: dedicated core destination address register 1941ec1e82fSSascha Hauer * @dsa: dedicated core source address register 1951ec1e82fSSascha Hauer * @ds: dedicated core status register 1961ec1e82fSSascha Hauer * @dd: dedicated core data register 1971ec1e82fSSascha Hauer */ 1981ec1e82fSSascha Hauer struct sdma_context_data { 1991ec1e82fSSascha Hauer struct sdma_state_registers channel_state; 2001ec1e82fSSascha Hauer u32 gReg[8]; 2011ec1e82fSSascha Hauer u32 mda; 2021ec1e82fSSascha Hauer u32 msa; 2031ec1e82fSSascha Hauer u32 ms; 2041ec1e82fSSascha Hauer u32 md; 2051ec1e82fSSascha Hauer u32 pda; 2061ec1e82fSSascha Hauer u32 psa; 2071ec1e82fSSascha Hauer u32 ps; 2081ec1e82fSSascha Hauer u32 pd; 2091ec1e82fSSascha Hauer u32 ca; 2101ec1e82fSSascha Hauer u32 cs; 2111ec1e82fSSascha Hauer u32 dda; 2121ec1e82fSSascha Hauer u32 dsa; 2131ec1e82fSSascha Hauer u32 ds; 2141ec1e82fSSascha Hauer u32 dd; 2151ec1e82fSSascha Hauer u32 scratch0; 2161ec1e82fSSascha Hauer u32 scratch1; 2171ec1e82fSSascha Hauer u32 scratch2; 2181ec1e82fSSascha Hauer u32 scratch3; 2191ec1e82fSSascha Hauer u32 scratch4; 2201ec1e82fSSascha Hauer u32 scratch5; 2211ec1e82fSSascha Hauer u32 scratch6; 2221ec1e82fSSascha Hauer u32 scratch7; 2231ec1e82fSSascha Hauer } __attribute__ ((packed)); 2241ec1e82fSSascha Hauer 2251ec1e82fSSascha Hauer #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor)) 2261ec1e82fSSascha Hauer 2271ec1e82fSSascha Hauer struct sdma_engine; 2281ec1e82fSSascha Hauer 2291ec1e82fSSascha Hauer /** 2301ec1e82fSSascha Hauer * struct sdma_channel - housekeeping for a SDMA channel 2311ec1e82fSSascha Hauer * 2321ec1e82fSSascha Hauer * @sdma pointer to the SDMA engine for this channel 23323889c63SSascha Hauer * @channel the channel number, matches dmaengine chan_id + 1 2341ec1e82fSSascha Hauer * @direction transfer type. Needed for setting SDMA script 2351ec1e82fSSascha Hauer * @peripheral_type Peripheral type. Needed for setting SDMA script 2361ec1e82fSSascha Hauer * @event_id0 aka dma request line 2371ec1e82fSSascha Hauer * @event_id1 for channels that use 2 events 2381ec1e82fSSascha Hauer * @word_size peripheral access size 2391ec1e82fSSascha Hauer * @buf_tail ID of the buffer that was processed 2401ec1e82fSSascha Hauer * @done channel completion 2411ec1e82fSSascha Hauer * @num_bd max NUM_BD. number of descriptors currently handling 2421ec1e82fSSascha Hauer */ 2431ec1e82fSSascha Hauer struct sdma_channel { 2441ec1e82fSSascha Hauer struct sdma_engine *sdma; 2451ec1e82fSSascha Hauer unsigned int channel; 2461ec1e82fSSascha Hauer enum dma_data_direction direction; 2471ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type; 2481ec1e82fSSascha Hauer unsigned int event_id0; 2491ec1e82fSSascha Hauer unsigned int event_id1; 2501ec1e82fSSascha Hauer enum dma_slave_buswidth word_size; 2511ec1e82fSSascha Hauer unsigned int buf_tail; 2521ec1e82fSSascha Hauer struct completion done; 2531ec1e82fSSascha Hauer unsigned int num_bd; 2541ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 2551ec1e82fSSascha Hauer dma_addr_t bd_phys; 2561ec1e82fSSascha Hauer unsigned int pc_from_device, pc_to_device; 2571ec1e82fSSascha Hauer unsigned long flags; 2581ec1e82fSSascha Hauer dma_addr_t per_address; 2591ec1e82fSSascha Hauer u32 event_mask0, event_mask1; 2601ec1e82fSSascha Hauer u32 watermark_level; 2611ec1e82fSSascha Hauer u32 shp_addr, per_addr; 2621ec1e82fSSascha Hauer struct dma_chan chan; 2631ec1e82fSSascha Hauer spinlock_t lock; 2641ec1e82fSSascha Hauer struct dma_async_tx_descriptor desc; 2651ec1e82fSSascha Hauer dma_cookie_t last_completed; 2661ec1e82fSSascha Hauer enum dma_status status; 2671ec1e82fSSascha Hauer }; 2681ec1e82fSSascha Hauer 2691ec1e82fSSascha Hauer #define IMX_DMA_SG_LOOP (1 << 0) 2701ec1e82fSSascha Hauer 2711ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32 2721ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1 2731ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1 2741ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7 2751ec1e82fSSascha Hauer 2761ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453 2771ec1e82fSSascha Hauer 2781ec1e82fSSascha Hauer /** 2791ec1e82fSSascha Hauer * struct sdma_firmware_header - Layout of the firmware image 2801ec1e82fSSascha Hauer * 2811ec1e82fSSascha Hauer * @magic "SDMA" 2821ec1e82fSSascha Hauer * @version_major increased whenever layout of struct sdma_script_start_addrs 2831ec1e82fSSascha Hauer * changes. 2841ec1e82fSSascha Hauer * @version_minor firmware minor version (for binary compatible changes) 2851ec1e82fSSascha Hauer * @script_addrs_start offset of struct sdma_script_start_addrs in this image 2861ec1e82fSSascha Hauer * @num_script_addrs Number of script addresses in this image 2871ec1e82fSSascha Hauer * @ram_code_start offset of SDMA ram image in this firmware image 2881ec1e82fSSascha Hauer * @ram_code_size size of SDMA ram image 2891ec1e82fSSascha Hauer * @script_addrs Stores the start address of the SDMA scripts 2901ec1e82fSSascha Hauer * (in SDMA memory space) 2911ec1e82fSSascha Hauer */ 2921ec1e82fSSascha Hauer struct sdma_firmware_header { 2931ec1e82fSSascha Hauer u32 magic; 2941ec1e82fSSascha Hauer u32 version_major; 2951ec1e82fSSascha Hauer u32 version_minor; 2961ec1e82fSSascha Hauer u32 script_addrs_start; 2971ec1e82fSSascha Hauer u32 num_script_addrs; 2981ec1e82fSSascha Hauer u32 ram_code_start; 2991ec1e82fSSascha Hauer u32 ram_code_size; 3001ec1e82fSSascha Hauer }; 3011ec1e82fSSascha Hauer 3021ec1e82fSSascha Hauer struct sdma_engine { 3031ec1e82fSSascha Hauer struct device *dev; 304b9b3f82fSSascha Hauer struct device_dma_parameters dma_parms; 3051ec1e82fSSascha Hauer struct sdma_channel channel[MAX_DMA_CHANNELS]; 3061ec1e82fSSascha Hauer struct sdma_channel_control *channel_control; 3071ec1e82fSSascha Hauer void __iomem *regs; 3081ec1e82fSSascha Hauer unsigned int version; 3091ec1e82fSSascha Hauer unsigned int num_events; 3101ec1e82fSSascha Hauer struct sdma_context_data *context; 3111ec1e82fSSascha Hauer dma_addr_t context_phys; 3121ec1e82fSSascha Hauer struct dma_device dma_device; 3131ec1e82fSSascha Hauer struct clk *clk; 3141ec1e82fSSascha Hauer struct sdma_script_start_addrs *script_addrs; 3151ec1e82fSSascha Hauer }; 3161ec1e82fSSascha Hauer 3171ec1e82fSSascha Hauer #define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */ 3181ec1e82fSSascha Hauer #define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */ 3191ec1e82fSSascha Hauer #define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */ 3201ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/ 3211ec1e82fSSascha Hauer 3221ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event) 3231ec1e82fSSascha Hauer { 3241ec1e82fSSascha Hauer u32 chnenbl0 = (sdma->version == 2 ? SDMA_CHNENBL0_V2 : SDMA_CHNENBL0_V1); 3251ec1e82fSSascha Hauer 3261ec1e82fSSascha Hauer return chnenbl0 + event * 4; 3271ec1e82fSSascha Hauer } 3281ec1e82fSSascha Hauer 3291ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac, 3301ec1e82fSSascha Hauer bool event_override, bool mcu_override, bool dsp_override) 3311ec1e82fSSascha Hauer { 3321ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 3331ec1e82fSSascha Hauer int channel = sdmac->channel; 3341ec1e82fSSascha Hauer u32 evt, mcu, dsp; 3351ec1e82fSSascha Hauer 3361ec1e82fSSascha Hauer if (event_override && mcu_override && dsp_override) 3371ec1e82fSSascha Hauer return -EINVAL; 3381ec1e82fSSascha Hauer 3391ec1e82fSSascha Hauer evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR); 3401ec1e82fSSascha Hauer mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR); 3411ec1e82fSSascha Hauer dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR); 3421ec1e82fSSascha Hauer 3431ec1e82fSSascha Hauer if (dsp_override) 3441ec1e82fSSascha Hauer dsp &= ~(1 << channel); 3451ec1e82fSSascha Hauer else 3461ec1e82fSSascha Hauer dsp |= (1 << channel); 3471ec1e82fSSascha Hauer 3481ec1e82fSSascha Hauer if (event_override) 3491ec1e82fSSascha Hauer evt &= ~(1 << channel); 3501ec1e82fSSascha Hauer else 3511ec1e82fSSascha Hauer evt |= (1 << channel); 3521ec1e82fSSascha Hauer 3531ec1e82fSSascha Hauer if (mcu_override) 3541ec1e82fSSascha Hauer mcu &= ~(1 << channel); 3551ec1e82fSSascha Hauer else 3561ec1e82fSSascha Hauer mcu |= (1 << channel); 3571ec1e82fSSascha Hauer 3581ec1e82fSSascha Hauer __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR); 3591ec1e82fSSascha Hauer __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR); 3601ec1e82fSSascha Hauer __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR); 3611ec1e82fSSascha Hauer 3621ec1e82fSSascha Hauer return 0; 3631ec1e82fSSascha Hauer } 3641ec1e82fSSascha Hauer 3651ec1e82fSSascha Hauer /* 3661ec1e82fSSascha Hauer * sdma_run_channel - run a channel and wait till it's done 3671ec1e82fSSascha Hauer */ 3681ec1e82fSSascha Hauer static int sdma_run_channel(struct sdma_channel *sdmac) 3691ec1e82fSSascha Hauer { 3701ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 3711ec1e82fSSascha Hauer int channel = sdmac->channel; 3721ec1e82fSSascha Hauer int ret; 3731ec1e82fSSascha Hauer 3741ec1e82fSSascha Hauer init_completion(&sdmac->done); 3751ec1e82fSSascha Hauer 3761ec1e82fSSascha Hauer __raw_writel(1 << channel, sdma->regs + SDMA_H_START); 3771ec1e82fSSascha Hauer 3781ec1e82fSSascha Hauer ret = wait_for_completion_timeout(&sdmac->done, HZ); 3791ec1e82fSSascha Hauer 3801ec1e82fSSascha Hauer return ret ? 0 : -ETIMEDOUT; 3811ec1e82fSSascha Hauer } 3821ec1e82fSSascha Hauer 3831ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size, 3841ec1e82fSSascha Hauer u32 address) 3851ec1e82fSSascha Hauer { 3861ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 3871ec1e82fSSascha Hauer void *buf_virt; 3881ec1e82fSSascha Hauer dma_addr_t buf_phys; 3891ec1e82fSSascha Hauer int ret; 3901ec1e82fSSascha Hauer 3911ec1e82fSSascha Hauer buf_virt = dma_alloc_coherent(NULL, 3921ec1e82fSSascha Hauer size, 3931ec1e82fSSascha Hauer &buf_phys, GFP_KERNEL); 3941ec1e82fSSascha Hauer if (!buf_virt) 3951ec1e82fSSascha Hauer return -ENOMEM; 3961ec1e82fSSascha Hauer 3971ec1e82fSSascha Hauer bd0->mode.command = C0_SETPM; 3981ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 3991ec1e82fSSascha Hauer bd0->mode.count = size / 2; 4001ec1e82fSSascha Hauer bd0->buffer_addr = buf_phys; 4011ec1e82fSSascha Hauer bd0->ext_buffer_addr = address; 4021ec1e82fSSascha Hauer 4031ec1e82fSSascha Hauer memcpy(buf_virt, buf, size); 4041ec1e82fSSascha Hauer 4051ec1e82fSSascha Hauer ret = sdma_run_channel(&sdma->channel[0]); 4061ec1e82fSSascha Hauer 4071ec1e82fSSascha Hauer dma_free_coherent(NULL, size, buf_virt, buf_phys); 4081ec1e82fSSascha Hauer 4091ec1e82fSSascha Hauer return ret; 4101ec1e82fSSascha Hauer } 4111ec1e82fSSascha Hauer 4121ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event) 4131ec1e82fSSascha Hauer { 4141ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 4151ec1e82fSSascha Hauer int channel = sdmac->channel; 4161ec1e82fSSascha Hauer u32 val; 4171ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 4181ec1e82fSSascha Hauer 4191ec1e82fSSascha Hauer val = __raw_readl(sdma->regs + chnenbl); 4201ec1e82fSSascha Hauer val |= (1 << channel); 4211ec1e82fSSascha Hauer __raw_writel(val, sdma->regs + chnenbl); 4221ec1e82fSSascha Hauer } 4231ec1e82fSSascha Hauer 4241ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event) 4251ec1e82fSSascha Hauer { 4261ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 4271ec1e82fSSascha Hauer int channel = sdmac->channel; 4281ec1e82fSSascha Hauer u32 chnenbl = chnenbl_ofs(sdma, event); 4291ec1e82fSSascha Hauer u32 val; 4301ec1e82fSSascha Hauer 4311ec1e82fSSascha Hauer val = __raw_readl(sdma->regs + chnenbl); 4321ec1e82fSSascha Hauer val &= ~(1 << channel); 4331ec1e82fSSascha Hauer __raw_writel(val, sdma->regs + chnenbl); 4341ec1e82fSSascha Hauer } 4351ec1e82fSSascha Hauer 4361ec1e82fSSascha Hauer static void sdma_handle_channel_loop(struct sdma_channel *sdmac) 4371ec1e82fSSascha Hauer { 4381ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 4391ec1e82fSSascha Hauer 4401ec1e82fSSascha Hauer /* 4411ec1e82fSSascha Hauer * loop mode. Iterate over descriptors, re-setup them and 4421ec1e82fSSascha Hauer * call callback function. 4431ec1e82fSSascha Hauer */ 4441ec1e82fSSascha Hauer while (1) { 4451ec1e82fSSascha Hauer bd = &sdmac->bd[sdmac->buf_tail]; 4461ec1e82fSSascha Hauer 4471ec1e82fSSascha Hauer if (bd->mode.status & BD_DONE) 4481ec1e82fSSascha Hauer break; 4491ec1e82fSSascha Hauer 4501ec1e82fSSascha Hauer if (bd->mode.status & BD_RROR) 4511ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 4521ec1e82fSSascha Hauer else 4531e9cebb4SShawn Guo sdmac->status = DMA_IN_PROGRESS; 4541ec1e82fSSascha Hauer 4551ec1e82fSSascha Hauer bd->mode.status |= BD_DONE; 4561ec1e82fSSascha Hauer sdmac->buf_tail++; 4571ec1e82fSSascha Hauer sdmac->buf_tail %= sdmac->num_bd; 4581ec1e82fSSascha Hauer 4591ec1e82fSSascha Hauer if (sdmac->desc.callback) 4601ec1e82fSSascha Hauer sdmac->desc.callback(sdmac->desc.callback_param); 4611ec1e82fSSascha Hauer } 4621ec1e82fSSascha Hauer } 4631ec1e82fSSascha Hauer 4641ec1e82fSSascha Hauer static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac) 4651ec1e82fSSascha Hauer { 4661ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd; 4671ec1e82fSSascha Hauer int i, error = 0; 4681ec1e82fSSascha Hauer 4691ec1e82fSSascha Hauer /* 4701ec1e82fSSascha Hauer * non loop mode. Iterate over all descriptors, collect 4711ec1e82fSSascha Hauer * errors and call callback function 4721ec1e82fSSascha Hauer */ 4731ec1e82fSSascha Hauer for (i = 0; i < sdmac->num_bd; i++) { 4741ec1e82fSSascha Hauer bd = &sdmac->bd[i]; 4751ec1e82fSSascha Hauer 4761ec1e82fSSascha Hauer if (bd->mode.status & (BD_DONE | BD_RROR)) 4771ec1e82fSSascha Hauer error = -EIO; 4781ec1e82fSSascha Hauer } 4791ec1e82fSSascha Hauer 4801ec1e82fSSascha Hauer if (error) 4811ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 4821ec1e82fSSascha Hauer else 4831ec1e82fSSascha Hauer sdmac->status = DMA_SUCCESS; 4841ec1e82fSSascha Hauer 4851ec1e82fSSascha Hauer if (sdmac->desc.callback) 4861ec1e82fSSascha Hauer sdmac->desc.callback(sdmac->desc.callback_param); 4871ec1e82fSSascha Hauer sdmac->last_completed = sdmac->desc.cookie; 4881ec1e82fSSascha Hauer } 4891ec1e82fSSascha Hauer 4901ec1e82fSSascha Hauer static void mxc_sdma_handle_channel(struct sdma_channel *sdmac) 4911ec1e82fSSascha Hauer { 4921ec1e82fSSascha Hauer complete(&sdmac->done); 4931ec1e82fSSascha Hauer 4941ec1e82fSSascha Hauer /* not interested in channel 0 interrupts */ 4951ec1e82fSSascha Hauer if (sdmac->channel == 0) 4961ec1e82fSSascha Hauer return; 4971ec1e82fSSascha Hauer 4981ec1e82fSSascha Hauer if (sdmac->flags & IMX_DMA_SG_LOOP) 4991ec1e82fSSascha Hauer sdma_handle_channel_loop(sdmac); 5001ec1e82fSSascha Hauer else 5011ec1e82fSSascha Hauer mxc_sdma_handle_channel_normal(sdmac); 5021ec1e82fSSascha Hauer } 5031ec1e82fSSascha Hauer 5041ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id) 5051ec1e82fSSascha Hauer { 5061ec1e82fSSascha Hauer struct sdma_engine *sdma = dev_id; 5071ec1e82fSSascha Hauer u32 stat; 5081ec1e82fSSascha Hauer 5091ec1e82fSSascha Hauer stat = __raw_readl(sdma->regs + SDMA_H_INTR); 5101ec1e82fSSascha Hauer __raw_writel(stat, sdma->regs + SDMA_H_INTR); 5111ec1e82fSSascha Hauer 5121ec1e82fSSascha Hauer while (stat) { 5131ec1e82fSSascha Hauer int channel = fls(stat) - 1; 5141ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[channel]; 5151ec1e82fSSascha Hauer 5161ec1e82fSSascha Hauer mxc_sdma_handle_channel(sdmac); 5171ec1e82fSSascha Hauer 5181ec1e82fSSascha Hauer stat &= ~(1 << channel); 5191ec1e82fSSascha Hauer } 5201ec1e82fSSascha Hauer 5211ec1e82fSSascha Hauer return IRQ_HANDLED; 5221ec1e82fSSascha Hauer } 5231ec1e82fSSascha Hauer 5241ec1e82fSSascha Hauer /* 5251ec1e82fSSascha Hauer * sets the pc of SDMA script according to the peripheral type 5261ec1e82fSSascha Hauer */ 5271ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac, 5281ec1e82fSSascha Hauer enum sdma_peripheral_type peripheral_type) 5291ec1e82fSSascha Hauer { 5301ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 5311ec1e82fSSascha Hauer int per_2_emi = 0, emi_2_per = 0; 5321ec1e82fSSascha Hauer /* 5331ec1e82fSSascha Hauer * These are needed once we start to support transfers between 5341ec1e82fSSascha Hauer * two peripherals or memory-to-memory transfers 5351ec1e82fSSascha Hauer */ 5361ec1e82fSSascha Hauer int per_2_per = 0, emi_2_emi = 0; 5371ec1e82fSSascha Hauer 5381ec1e82fSSascha Hauer sdmac->pc_from_device = 0; 5391ec1e82fSSascha Hauer sdmac->pc_to_device = 0; 5401ec1e82fSSascha Hauer 5411ec1e82fSSascha Hauer switch (peripheral_type) { 5421ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 5431ec1e82fSSascha Hauer emi_2_emi = sdma->script_addrs->ap_2_ap_addr; 5441ec1e82fSSascha Hauer break; 5451ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 5461ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->bp_2_ap_addr; 5471ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ap_2_bp_addr; 5481ec1e82fSSascha Hauer break; 5491ec1e82fSSascha Hauer case IMX_DMATYPE_FIRI: 5501ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->firi_2_mcu_addr; 5511ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_firi_addr; 5521ec1e82fSSascha Hauer break; 5531ec1e82fSSascha Hauer case IMX_DMATYPE_UART: 5541ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uart_2_mcu_addr; 5551ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 5561ec1e82fSSascha Hauer break; 5571ec1e82fSSascha Hauer case IMX_DMATYPE_UART_SP: 5581ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr; 5591ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 5601ec1e82fSSascha Hauer break; 5611ec1e82fSSascha Hauer case IMX_DMATYPE_ATA: 5621ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->ata_2_mcu_addr; 5631ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_ata_addr; 5641ec1e82fSSascha Hauer break; 5651ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI: 5661ec1e82fSSascha Hauer case IMX_DMATYPE_EXT: 5671ec1e82fSSascha Hauer case IMX_DMATYPE_SSI: 5681ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->app_2_mcu_addr; 5691ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_app_addr; 5701ec1e82fSSascha Hauer break; 5711ec1e82fSSascha Hauer case IMX_DMATYPE_SSI_SP: 5721ec1e82fSSascha Hauer case IMX_DMATYPE_MMC: 5731ec1e82fSSascha Hauer case IMX_DMATYPE_SDHC: 5741ec1e82fSSascha Hauer case IMX_DMATYPE_CSPI_SP: 5751ec1e82fSSascha Hauer case IMX_DMATYPE_ESAI: 5761ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC_SP: 5771ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->shp_2_mcu_addr; 5781ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_shp_addr; 5791ec1e82fSSascha Hauer break; 5801ec1e82fSSascha Hauer case IMX_DMATYPE_ASRC: 5811ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->asrc_2_mcu_addr; 5821ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->asrc_2_mcu_addr; 5831ec1e82fSSascha Hauer per_2_per = sdma->script_addrs->per_2_per_addr; 5841ec1e82fSSascha Hauer break; 5851ec1e82fSSascha Hauer case IMX_DMATYPE_MSHC: 5861ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->mshc_2_mcu_addr; 5871ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_mshc_addr; 5881ec1e82fSSascha Hauer break; 5891ec1e82fSSascha Hauer case IMX_DMATYPE_CCM: 5901ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->dptc_dvfs_addr; 5911ec1e82fSSascha Hauer break; 5921ec1e82fSSascha Hauer case IMX_DMATYPE_SPDIF: 5931ec1e82fSSascha Hauer per_2_emi = sdma->script_addrs->spdif_2_mcu_addr; 5941ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->mcu_2_spdif_addr; 5951ec1e82fSSascha Hauer break; 5961ec1e82fSSascha Hauer case IMX_DMATYPE_IPU_MEMORY: 5971ec1e82fSSascha Hauer emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr; 5981ec1e82fSSascha Hauer break; 5991ec1e82fSSascha Hauer default: 6001ec1e82fSSascha Hauer break; 6011ec1e82fSSascha Hauer } 6021ec1e82fSSascha Hauer 6031ec1e82fSSascha Hauer sdmac->pc_from_device = per_2_emi; 6041ec1e82fSSascha Hauer sdmac->pc_to_device = emi_2_per; 6051ec1e82fSSascha Hauer } 6061ec1e82fSSascha Hauer 6071ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac) 6081ec1e82fSSascha Hauer { 6091ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6101ec1e82fSSascha Hauer int channel = sdmac->channel; 6111ec1e82fSSascha Hauer int load_address; 6121ec1e82fSSascha Hauer struct sdma_context_data *context = sdma->context; 6131ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd; 6141ec1e82fSSascha Hauer int ret; 6151ec1e82fSSascha Hauer 6161ec1e82fSSascha Hauer if (sdmac->direction == DMA_FROM_DEVICE) { 6171ec1e82fSSascha Hauer load_address = sdmac->pc_from_device; 6181ec1e82fSSascha Hauer } else { 6191ec1e82fSSascha Hauer load_address = sdmac->pc_to_device; 6201ec1e82fSSascha Hauer } 6211ec1e82fSSascha Hauer 6221ec1e82fSSascha Hauer if (load_address < 0) 6231ec1e82fSSascha Hauer return load_address; 6241ec1e82fSSascha Hauer 6251ec1e82fSSascha Hauer dev_dbg(sdma->dev, "load_address = %d\n", load_address); 6261ec1e82fSSascha Hauer dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level); 6271ec1e82fSSascha Hauer dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr); 6281ec1e82fSSascha Hauer dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr); 6291ec1e82fSSascha Hauer dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0); 6301ec1e82fSSascha Hauer dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1); 6311ec1e82fSSascha Hauer 6321ec1e82fSSascha Hauer memset(context, 0, sizeof(*context)); 6331ec1e82fSSascha Hauer context->channel_state.pc = load_address; 6341ec1e82fSSascha Hauer 6351ec1e82fSSascha Hauer /* Send by context the event mask,base address for peripheral 6361ec1e82fSSascha Hauer * and watermark level 6371ec1e82fSSascha Hauer */ 6381ec1e82fSSascha Hauer context->gReg[0] = sdmac->event_mask1; 6391ec1e82fSSascha Hauer context->gReg[1] = sdmac->event_mask0; 6401ec1e82fSSascha Hauer context->gReg[2] = sdmac->per_addr; 6411ec1e82fSSascha Hauer context->gReg[6] = sdmac->shp_addr; 6421ec1e82fSSascha Hauer context->gReg[7] = sdmac->watermark_level; 6431ec1e82fSSascha Hauer 6441ec1e82fSSascha Hauer bd0->mode.command = C0_SETDM; 6451ec1e82fSSascha Hauer bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD; 6461ec1e82fSSascha Hauer bd0->mode.count = sizeof(*context) / 4; 6471ec1e82fSSascha Hauer bd0->buffer_addr = sdma->context_phys; 6481ec1e82fSSascha Hauer bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel; 6491ec1e82fSSascha Hauer 6501ec1e82fSSascha Hauer ret = sdma_run_channel(&sdma->channel[0]); 6511ec1e82fSSascha Hauer 6521ec1e82fSSascha Hauer return ret; 6531ec1e82fSSascha Hauer } 6541ec1e82fSSascha Hauer 6551ec1e82fSSascha Hauer static void sdma_disable_channel(struct sdma_channel *sdmac) 6561ec1e82fSSascha Hauer { 6571ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 6581ec1e82fSSascha Hauer int channel = sdmac->channel; 6591ec1e82fSSascha Hauer 6601ec1e82fSSascha Hauer __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP); 6611ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 6621ec1e82fSSascha Hauer } 6631ec1e82fSSascha Hauer 6641ec1e82fSSascha Hauer static int sdma_config_channel(struct sdma_channel *sdmac) 6651ec1e82fSSascha Hauer { 6661ec1e82fSSascha Hauer int ret; 6671ec1e82fSSascha Hauer 6681ec1e82fSSascha Hauer sdma_disable_channel(sdmac); 6691ec1e82fSSascha Hauer 6701ec1e82fSSascha Hauer sdmac->event_mask0 = 0; 6711ec1e82fSSascha Hauer sdmac->event_mask1 = 0; 6721ec1e82fSSascha Hauer sdmac->shp_addr = 0; 6731ec1e82fSSascha Hauer sdmac->per_addr = 0; 6741ec1e82fSSascha Hauer 6751ec1e82fSSascha Hauer if (sdmac->event_id0) { 6761ec1e82fSSascha Hauer if (sdmac->event_id0 > 32) 6771ec1e82fSSascha Hauer return -EINVAL; 6781ec1e82fSSascha Hauer sdma_event_enable(sdmac, sdmac->event_id0); 6791ec1e82fSSascha Hauer } 6801ec1e82fSSascha Hauer 6811ec1e82fSSascha Hauer switch (sdmac->peripheral_type) { 6821ec1e82fSSascha Hauer case IMX_DMATYPE_DSP: 6831ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, true); 6841ec1e82fSSascha Hauer break; 6851ec1e82fSSascha Hauer case IMX_DMATYPE_MEMORY: 6861ec1e82fSSascha Hauer sdma_config_ownership(sdmac, false, true, false); 6871ec1e82fSSascha Hauer break; 6881ec1e82fSSascha Hauer default: 6891ec1e82fSSascha Hauer sdma_config_ownership(sdmac, true, true, false); 6901ec1e82fSSascha Hauer break; 6911ec1e82fSSascha Hauer } 6921ec1e82fSSascha Hauer 6931ec1e82fSSascha Hauer sdma_get_pc(sdmac, sdmac->peripheral_type); 6941ec1e82fSSascha Hauer 6951ec1e82fSSascha Hauer if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) && 6961ec1e82fSSascha Hauer (sdmac->peripheral_type != IMX_DMATYPE_DSP)) { 6971ec1e82fSSascha Hauer /* Handle multiple event channels differently */ 6981ec1e82fSSascha Hauer if (sdmac->event_id1) { 6991ec1e82fSSascha Hauer sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32); 7001ec1e82fSSascha Hauer if (sdmac->event_id1 > 31) 7011ec1e82fSSascha Hauer sdmac->watermark_level |= 1 << 31; 7021ec1e82fSSascha Hauer sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32); 7031ec1e82fSSascha Hauer if (sdmac->event_id0 > 31) 7041ec1e82fSSascha Hauer sdmac->watermark_level |= 1 << 30; 7051ec1e82fSSascha Hauer } else { 7061ec1e82fSSascha Hauer sdmac->event_mask0 = 1 << sdmac->event_id0; 7071ec1e82fSSascha Hauer sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32); 7081ec1e82fSSascha Hauer } 7091ec1e82fSSascha Hauer /* Watermark Level */ 7101ec1e82fSSascha Hauer sdmac->watermark_level |= sdmac->watermark_level; 7111ec1e82fSSascha Hauer /* Address */ 7121ec1e82fSSascha Hauer sdmac->shp_addr = sdmac->per_address; 7131ec1e82fSSascha Hauer } else { 7141ec1e82fSSascha Hauer sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */ 7151ec1e82fSSascha Hauer } 7161ec1e82fSSascha Hauer 7171ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 7181ec1e82fSSascha Hauer 7191ec1e82fSSascha Hauer return ret; 7201ec1e82fSSascha Hauer } 7211ec1e82fSSascha Hauer 7221ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac, 7231ec1e82fSSascha Hauer unsigned int priority) 7241ec1e82fSSascha Hauer { 7251ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7261ec1e82fSSascha Hauer int channel = sdmac->channel; 7271ec1e82fSSascha Hauer 7281ec1e82fSSascha Hauer if (priority < MXC_SDMA_MIN_PRIORITY 7291ec1e82fSSascha Hauer || priority > MXC_SDMA_MAX_PRIORITY) { 7301ec1e82fSSascha Hauer return -EINVAL; 7311ec1e82fSSascha Hauer } 7321ec1e82fSSascha Hauer 7331ec1e82fSSascha Hauer __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel); 7341ec1e82fSSascha Hauer 7351ec1e82fSSascha Hauer return 0; 7361ec1e82fSSascha Hauer } 7371ec1e82fSSascha Hauer 7381ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac) 7391ec1e82fSSascha Hauer { 7401ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7411ec1e82fSSascha Hauer int channel = sdmac->channel; 7421ec1e82fSSascha Hauer int ret = -EBUSY; 7431ec1e82fSSascha Hauer 7441ec1e82fSSascha Hauer sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL); 7451ec1e82fSSascha Hauer if (!sdmac->bd) { 7461ec1e82fSSascha Hauer ret = -ENOMEM; 7471ec1e82fSSascha Hauer goto out; 7481ec1e82fSSascha Hauer } 7491ec1e82fSSascha Hauer 7501ec1e82fSSascha Hauer memset(sdmac->bd, 0, PAGE_SIZE); 7511ec1e82fSSascha Hauer 7521ec1e82fSSascha Hauer sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys; 7531ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 7541ec1e82fSSascha Hauer 7551ec1e82fSSascha Hauer clk_enable(sdma->clk); 7561ec1e82fSSascha Hauer 7571ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY); 7581ec1e82fSSascha Hauer 7591ec1e82fSSascha Hauer init_completion(&sdmac->done); 7601ec1e82fSSascha Hauer 7611ec1e82fSSascha Hauer sdmac->buf_tail = 0; 7621ec1e82fSSascha Hauer 7631ec1e82fSSascha Hauer return 0; 7641ec1e82fSSascha Hauer out: 7651ec1e82fSSascha Hauer 7661ec1e82fSSascha Hauer return ret; 7671ec1e82fSSascha Hauer } 7681ec1e82fSSascha Hauer 7691ec1e82fSSascha Hauer static void sdma_enable_channel(struct sdma_engine *sdma, int channel) 7701ec1e82fSSascha Hauer { 7711ec1e82fSSascha Hauer __raw_writel(1 << channel, sdma->regs + SDMA_H_START); 7721ec1e82fSSascha Hauer } 7731ec1e82fSSascha Hauer 774d718f4ebSShawn Guo static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdmac) 7751ec1e82fSSascha Hauer { 776d718f4ebSShawn Guo dma_cookie_t cookie = sdmac->chan.cookie; 7771ec1e82fSSascha Hauer 7781ec1e82fSSascha Hauer if (++cookie < 0) 7791ec1e82fSSascha Hauer cookie = 1; 7801ec1e82fSSascha Hauer 781d718f4ebSShawn Guo sdmac->chan.cookie = cookie; 782d718f4ebSShawn Guo sdmac->desc.cookie = cookie; 7831ec1e82fSSascha Hauer 7841ec1e82fSSascha Hauer return cookie; 7851ec1e82fSSascha Hauer } 7861ec1e82fSSascha Hauer 7871ec1e82fSSascha Hauer static struct sdma_channel *to_sdma_chan(struct dma_chan *chan) 7881ec1e82fSSascha Hauer { 7891ec1e82fSSascha Hauer return container_of(chan, struct sdma_channel, chan); 7901ec1e82fSSascha Hauer } 7911ec1e82fSSascha Hauer 7921ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx) 7931ec1e82fSSascha Hauer { 7941ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(tx->chan); 7951ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 7961ec1e82fSSascha Hauer dma_cookie_t cookie; 7971ec1e82fSSascha Hauer 7981ec1e82fSSascha Hauer spin_lock_irq(&sdmac->lock); 7991ec1e82fSSascha Hauer 8001ec1e82fSSascha Hauer cookie = sdma_assign_cookie(sdmac); 8011ec1e82fSSascha Hauer 80223889c63SSascha Hauer sdma_enable_channel(sdma, sdmac->channel); 8031ec1e82fSSascha Hauer 8041ec1e82fSSascha Hauer spin_unlock_irq(&sdmac->lock); 8051ec1e82fSSascha Hauer 8061ec1e82fSSascha Hauer return cookie; 8071ec1e82fSSascha Hauer } 8081ec1e82fSSascha Hauer 8091ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan) 8101ec1e82fSSascha Hauer { 8111ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 8121ec1e82fSSascha Hauer struct imx_dma_data *data = chan->private; 8131ec1e82fSSascha Hauer int prio, ret; 8141ec1e82fSSascha Hauer 8151ec1e82fSSascha Hauer if (!data) 8161ec1e82fSSascha Hauer return -EINVAL; 8171ec1e82fSSascha Hauer 8181ec1e82fSSascha Hauer switch (data->priority) { 8191ec1e82fSSascha Hauer case DMA_PRIO_HIGH: 8201ec1e82fSSascha Hauer prio = 3; 8211ec1e82fSSascha Hauer break; 8221ec1e82fSSascha Hauer case DMA_PRIO_MEDIUM: 8231ec1e82fSSascha Hauer prio = 2; 8241ec1e82fSSascha Hauer break; 8251ec1e82fSSascha Hauer case DMA_PRIO_LOW: 8261ec1e82fSSascha Hauer default: 8271ec1e82fSSascha Hauer prio = 1; 8281ec1e82fSSascha Hauer break; 8291ec1e82fSSascha Hauer } 8301ec1e82fSSascha Hauer 8311ec1e82fSSascha Hauer sdmac->peripheral_type = data->peripheral_type; 8321ec1e82fSSascha Hauer sdmac->event_id0 = data->dma_request; 8331ec1e82fSSascha Hauer ret = sdma_set_channel_priority(sdmac, prio); 8341ec1e82fSSascha Hauer if (ret) 8351ec1e82fSSascha Hauer return ret; 8361ec1e82fSSascha Hauer 8371ec1e82fSSascha Hauer ret = sdma_request_channel(sdmac); 8381ec1e82fSSascha Hauer if (ret) 8391ec1e82fSSascha Hauer return ret; 8401ec1e82fSSascha Hauer 8411ec1e82fSSascha Hauer dma_async_tx_descriptor_init(&sdmac->desc, chan); 8421ec1e82fSSascha Hauer sdmac->desc.tx_submit = sdma_tx_submit; 8431ec1e82fSSascha Hauer /* txd.flags will be overwritten in prep funcs */ 8441ec1e82fSSascha Hauer sdmac->desc.flags = DMA_CTRL_ACK; 8451ec1e82fSSascha Hauer 8461ec1e82fSSascha Hauer return 0; 8471ec1e82fSSascha Hauer } 8481ec1e82fSSascha Hauer 8491ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan) 8501ec1e82fSSascha Hauer { 8511ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 8521ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8531ec1e82fSSascha Hauer 8541ec1e82fSSascha Hauer sdma_disable_channel(sdmac); 8551ec1e82fSSascha Hauer 8561ec1e82fSSascha Hauer if (sdmac->event_id0) 8571ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id0); 8581ec1e82fSSascha Hauer if (sdmac->event_id1) 8591ec1e82fSSascha Hauer sdma_event_disable(sdmac, sdmac->event_id1); 8601ec1e82fSSascha Hauer 8611ec1e82fSSascha Hauer sdmac->event_id0 = 0; 8621ec1e82fSSascha Hauer sdmac->event_id1 = 0; 8631ec1e82fSSascha Hauer 8641ec1e82fSSascha Hauer sdma_set_channel_priority(sdmac, 0); 8651ec1e82fSSascha Hauer 8661ec1e82fSSascha Hauer dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys); 8671ec1e82fSSascha Hauer 8681ec1e82fSSascha Hauer clk_disable(sdma->clk); 8691ec1e82fSSascha Hauer } 8701ec1e82fSSascha Hauer 8711ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg( 8721ec1e82fSSascha Hauer struct dma_chan *chan, struct scatterlist *sgl, 8731ec1e82fSSascha Hauer unsigned int sg_len, enum dma_data_direction direction, 8741ec1e82fSSascha Hauer unsigned long flags) 8751ec1e82fSSascha Hauer { 8761ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 8771ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 8781ec1e82fSSascha Hauer int ret, i, count; 87923889c63SSascha Hauer int channel = sdmac->channel; 8801ec1e82fSSascha Hauer struct scatterlist *sg; 8811ec1e82fSSascha Hauer 8821ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 8831ec1e82fSSascha Hauer return NULL; 8841ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 8851ec1e82fSSascha Hauer 8861ec1e82fSSascha Hauer sdmac->flags = 0; 8871ec1e82fSSascha Hauer 8881ec1e82fSSascha Hauer dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", 8891ec1e82fSSascha Hauer sg_len, channel); 8901ec1e82fSSascha Hauer 8911ec1e82fSSascha Hauer sdmac->direction = direction; 8921ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 8931ec1e82fSSascha Hauer if (ret) 8941ec1e82fSSascha Hauer goto err_out; 8951ec1e82fSSascha Hauer 8961ec1e82fSSascha Hauer if (sg_len > NUM_BD) { 8971ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 8981ec1e82fSSascha Hauer channel, sg_len, NUM_BD); 8991ec1e82fSSascha Hauer ret = -EINVAL; 9001ec1e82fSSascha Hauer goto err_out; 9011ec1e82fSSascha Hauer } 9021ec1e82fSSascha Hauer 9031ec1e82fSSascha Hauer for_each_sg(sgl, sg, sg_len, i) { 9041ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 9051ec1e82fSSascha Hauer int param; 9061ec1e82fSSascha Hauer 907d2f5c276SAnatolij Gustschin bd->buffer_addr = sg->dma_address; 9081ec1e82fSSascha Hauer 9091ec1e82fSSascha Hauer count = sg->length; 9101ec1e82fSSascha Hauer 9111ec1e82fSSascha Hauer if (count > 0xffff) { 9121ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n", 9131ec1e82fSSascha Hauer channel, count, 0xffff); 9141ec1e82fSSascha Hauer ret = -EINVAL; 9151ec1e82fSSascha Hauer goto err_out; 9161ec1e82fSSascha Hauer } 9171ec1e82fSSascha Hauer 9181ec1e82fSSascha Hauer bd->mode.count = count; 9191ec1e82fSSascha Hauer 9201ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { 9211ec1e82fSSascha Hauer ret = -EINVAL; 9221ec1e82fSSascha Hauer goto err_out; 9231ec1e82fSSascha Hauer } 9241fa81c27SSascha Hauer 9251fa81c27SSascha Hauer switch (sdmac->word_size) { 9261fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_4_BYTES: 9271ec1e82fSSascha Hauer bd->mode.command = 0; 9281fa81c27SSascha Hauer if (count & 3 || sg->dma_address & 3) 9291fa81c27SSascha Hauer return NULL; 9301fa81c27SSascha Hauer break; 9311fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_2_BYTES: 9321fa81c27SSascha Hauer bd->mode.command = 2; 9331fa81c27SSascha Hauer if (count & 1 || sg->dma_address & 1) 9341fa81c27SSascha Hauer return NULL; 9351fa81c27SSascha Hauer break; 9361fa81c27SSascha Hauer case DMA_SLAVE_BUSWIDTH_1_BYTE: 9371fa81c27SSascha Hauer bd->mode.command = 1; 9381fa81c27SSascha Hauer break; 9391fa81c27SSascha Hauer default: 9401fa81c27SSascha Hauer return NULL; 9411fa81c27SSascha Hauer } 9421ec1e82fSSascha Hauer 9431ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT; 9441ec1e82fSSascha Hauer 945341b9419SShawn Guo if (i + 1 == sg_len) { 9461ec1e82fSSascha Hauer param |= BD_INTR; 947341b9419SShawn Guo param |= BD_LAST; 948341b9419SShawn Guo param &= ~BD_CONT; 9491ec1e82fSSascha Hauer } 9501ec1e82fSSascha Hauer 9511ec1e82fSSascha Hauer dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n", 9521ec1e82fSSascha Hauer i, count, sg->dma_address, 9531ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 9541ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 9551ec1e82fSSascha Hauer 9561ec1e82fSSascha Hauer bd->mode.status = param; 9571ec1e82fSSascha Hauer } 9581ec1e82fSSascha Hauer 9591ec1e82fSSascha Hauer sdmac->num_bd = sg_len; 9601ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 9611ec1e82fSSascha Hauer 9621ec1e82fSSascha Hauer return &sdmac->desc; 9631ec1e82fSSascha Hauer err_out: 9644b2ce9ddSShawn Guo sdmac->status = DMA_ERROR; 9651ec1e82fSSascha Hauer return NULL; 9661ec1e82fSSascha Hauer } 9671ec1e82fSSascha Hauer 9681ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic( 9691ec1e82fSSascha Hauer struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, 9701ec1e82fSSascha Hauer size_t period_len, enum dma_data_direction direction) 9711ec1e82fSSascha Hauer { 9721ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 9731ec1e82fSSascha Hauer struct sdma_engine *sdma = sdmac->sdma; 9741ec1e82fSSascha Hauer int num_periods = buf_len / period_len; 97523889c63SSascha Hauer int channel = sdmac->channel; 9761ec1e82fSSascha Hauer int ret, i = 0, buf = 0; 9771ec1e82fSSascha Hauer 9781ec1e82fSSascha Hauer dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel); 9791ec1e82fSSascha Hauer 9801ec1e82fSSascha Hauer if (sdmac->status == DMA_IN_PROGRESS) 9811ec1e82fSSascha Hauer return NULL; 9821ec1e82fSSascha Hauer 9831ec1e82fSSascha Hauer sdmac->status = DMA_IN_PROGRESS; 9841ec1e82fSSascha Hauer 9851ec1e82fSSascha Hauer sdmac->flags |= IMX_DMA_SG_LOOP; 9861ec1e82fSSascha Hauer sdmac->direction = direction; 9871ec1e82fSSascha Hauer ret = sdma_load_context(sdmac); 9881ec1e82fSSascha Hauer if (ret) 9891ec1e82fSSascha Hauer goto err_out; 9901ec1e82fSSascha Hauer 9911ec1e82fSSascha Hauer if (num_periods > NUM_BD) { 9921ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", 9931ec1e82fSSascha Hauer channel, num_periods, NUM_BD); 9941ec1e82fSSascha Hauer goto err_out; 9951ec1e82fSSascha Hauer } 9961ec1e82fSSascha Hauer 9971ec1e82fSSascha Hauer if (period_len > 0xffff) { 9981ec1e82fSSascha Hauer dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n", 9991ec1e82fSSascha Hauer channel, period_len, 0xffff); 10001ec1e82fSSascha Hauer goto err_out; 10011ec1e82fSSascha Hauer } 10021ec1e82fSSascha Hauer 10031ec1e82fSSascha Hauer while (buf < buf_len) { 10041ec1e82fSSascha Hauer struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; 10051ec1e82fSSascha Hauer int param; 10061ec1e82fSSascha Hauer 10071ec1e82fSSascha Hauer bd->buffer_addr = dma_addr; 10081ec1e82fSSascha Hauer 10091ec1e82fSSascha Hauer bd->mode.count = period_len; 10101ec1e82fSSascha Hauer 10111ec1e82fSSascha Hauer if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) 10121ec1e82fSSascha Hauer goto err_out; 10131ec1e82fSSascha Hauer if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES) 10141ec1e82fSSascha Hauer bd->mode.command = 0; 10151ec1e82fSSascha Hauer else 10161ec1e82fSSascha Hauer bd->mode.command = sdmac->word_size; 10171ec1e82fSSascha Hauer 10181ec1e82fSSascha Hauer param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR; 10191ec1e82fSSascha Hauer if (i + 1 == num_periods) 10201ec1e82fSSascha Hauer param |= BD_WRAP; 10211ec1e82fSSascha Hauer 10221ec1e82fSSascha Hauer dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n", 10231ec1e82fSSascha Hauer i, period_len, dma_addr, 10241ec1e82fSSascha Hauer param & BD_WRAP ? "wrap" : "", 10251ec1e82fSSascha Hauer param & BD_INTR ? " intr" : ""); 10261ec1e82fSSascha Hauer 10271ec1e82fSSascha Hauer bd->mode.status = param; 10281ec1e82fSSascha Hauer 10291ec1e82fSSascha Hauer dma_addr += period_len; 10301ec1e82fSSascha Hauer buf += period_len; 10311ec1e82fSSascha Hauer 10321ec1e82fSSascha Hauer i++; 10331ec1e82fSSascha Hauer } 10341ec1e82fSSascha Hauer 10351ec1e82fSSascha Hauer sdmac->num_bd = num_periods; 10361ec1e82fSSascha Hauer sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys; 10371ec1e82fSSascha Hauer 10381ec1e82fSSascha Hauer return &sdmac->desc; 10391ec1e82fSSascha Hauer err_out: 10401ec1e82fSSascha Hauer sdmac->status = DMA_ERROR; 10411ec1e82fSSascha Hauer return NULL; 10421ec1e82fSSascha Hauer } 10431ec1e82fSSascha Hauer 10441ec1e82fSSascha Hauer static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 10451ec1e82fSSascha Hauer unsigned long arg) 10461ec1e82fSSascha Hauer { 10471ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 10481ec1e82fSSascha Hauer struct dma_slave_config *dmaengine_cfg = (void *)arg; 10491ec1e82fSSascha Hauer 10501ec1e82fSSascha Hauer switch (cmd) { 10511ec1e82fSSascha Hauer case DMA_TERMINATE_ALL: 10521ec1e82fSSascha Hauer sdma_disable_channel(sdmac); 10531ec1e82fSSascha Hauer return 0; 10541ec1e82fSSascha Hauer case DMA_SLAVE_CONFIG: 10551ec1e82fSSascha Hauer if (dmaengine_cfg->direction == DMA_FROM_DEVICE) { 10561ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->src_addr; 10571ec1e82fSSascha Hauer sdmac->watermark_level = dmaengine_cfg->src_maxburst; 10581ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->src_addr_width; 10591ec1e82fSSascha Hauer } else { 10601ec1e82fSSascha Hauer sdmac->per_address = dmaengine_cfg->dst_addr; 10611ec1e82fSSascha Hauer sdmac->watermark_level = dmaengine_cfg->dst_maxburst; 10621ec1e82fSSascha Hauer sdmac->word_size = dmaengine_cfg->dst_addr_width; 10631ec1e82fSSascha Hauer } 10641ec1e82fSSascha Hauer return sdma_config_channel(sdmac); 10651ec1e82fSSascha Hauer default: 10661ec1e82fSSascha Hauer return -ENOSYS; 10671ec1e82fSSascha Hauer } 10681ec1e82fSSascha Hauer 10691ec1e82fSSascha Hauer return -EINVAL; 10701ec1e82fSSascha Hauer } 10711ec1e82fSSascha Hauer 10721ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan, 10731ec1e82fSSascha Hauer dma_cookie_t cookie, 10741ec1e82fSSascha Hauer struct dma_tx_state *txstate) 10751ec1e82fSSascha Hauer { 10761ec1e82fSSascha Hauer struct sdma_channel *sdmac = to_sdma_chan(chan); 10771ec1e82fSSascha Hauer dma_cookie_t last_used; 10781ec1e82fSSascha Hauer 10791ec1e82fSSascha Hauer last_used = chan->cookie; 10801ec1e82fSSascha Hauer 10811ec1e82fSSascha Hauer dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0); 10821ec1e82fSSascha Hauer 10838a965911SShawn Guo return sdmac->status; 10841ec1e82fSSascha Hauer } 10851ec1e82fSSascha Hauer 10861ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan) 10871ec1e82fSSascha Hauer { 10881ec1e82fSSascha Hauer /* 10891ec1e82fSSascha Hauer * Nothing to do. We only have a single descriptor 10901ec1e82fSSascha Hauer */ 10911ec1e82fSSascha Hauer } 10921ec1e82fSSascha Hauer 10935b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34 10945b28aa31SSascha Hauer 10955b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma, 10965b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr) 10975b28aa31SSascha Hauer { 10985b28aa31SSascha Hauer s32 *addr_arr = (u32 *)addr; 10995b28aa31SSascha Hauer s32 *saddr_arr = (u32 *)sdma->script_addrs; 11005b28aa31SSascha Hauer int i; 11015b28aa31SSascha Hauer 11025b28aa31SSascha Hauer for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++) 11035b28aa31SSascha Hauer if (addr_arr[i] > 0) 11045b28aa31SSascha Hauer saddr_arr[i] = addr_arr[i]; 11055b28aa31SSascha Hauer } 11065b28aa31SSascha Hauer 11075b28aa31SSascha Hauer static int __init sdma_get_firmware(struct sdma_engine *sdma, 11085b28aa31SSascha Hauer const char *cpu_name, int to_version) 11095b28aa31SSascha Hauer { 11105b28aa31SSascha Hauer const struct firmware *fw; 11115b28aa31SSascha Hauer char *fwname; 11125b28aa31SSascha Hauer const struct sdma_firmware_header *header; 11135b28aa31SSascha Hauer int ret; 11145b28aa31SSascha Hauer const struct sdma_script_start_addrs *addr; 11155b28aa31SSascha Hauer unsigned short *ram_code; 11165b28aa31SSascha Hauer 11175b28aa31SSascha Hauer fwname = kasprintf(GFP_KERNEL, "sdma-%s-to%d.bin", cpu_name, to_version); 11185b28aa31SSascha Hauer if (!fwname) 11195b28aa31SSascha Hauer return -ENOMEM; 11205b28aa31SSascha Hauer 11215b28aa31SSascha Hauer ret = request_firmware(&fw, fwname, sdma->dev); 11225b28aa31SSascha Hauer if (ret) { 11235b28aa31SSascha Hauer kfree(fwname); 11245b28aa31SSascha Hauer return ret; 11255b28aa31SSascha Hauer } 11265b28aa31SSascha Hauer kfree(fwname); 11275b28aa31SSascha Hauer 11285b28aa31SSascha Hauer if (fw->size < sizeof(*header)) 11295b28aa31SSascha Hauer goto err_firmware; 11305b28aa31SSascha Hauer 11315b28aa31SSascha Hauer header = (struct sdma_firmware_header *)fw->data; 11325b28aa31SSascha Hauer 11335b28aa31SSascha Hauer if (header->magic != SDMA_FIRMWARE_MAGIC) 11345b28aa31SSascha Hauer goto err_firmware; 11355b28aa31SSascha Hauer if (header->ram_code_start + header->ram_code_size > fw->size) 11365b28aa31SSascha Hauer goto err_firmware; 11375b28aa31SSascha Hauer 11385b28aa31SSascha Hauer addr = (void *)header + header->script_addrs_start; 11395b28aa31SSascha Hauer ram_code = (void *)header + header->ram_code_start; 11405b28aa31SSascha Hauer 11415b28aa31SSascha Hauer clk_enable(sdma->clk); 11425b28aa31SSascha Hauer /* download the RAM image for SDMA */ 11435b28aa31SSascha Hauer sdma_load_script(sdma, ram_code, 11445b28aa31SSascha Hauer header->ram_code_size, 11456866fd3bSSascha Hauer addr->ram_code_start_addr); 11465b28aa31SSascha Hauer clk_disable(sdma->clk); 11475b28aa31SSascha Hauer 11485b28aa31SSascha Hauer sdma_add_scripts(sdma, addr); 11495b28aa31SSascha Hauer 11505b28aa31SSascha Hauer dev_info(sdma->dev, "loaded firmware %d.%d\n", 11515b28aa31SSascha Hauer header->version_major, 11525b28aa31SSascha Hauer header->version_minor); 11535b28aa31SSascha Hauer 11545b28aa31SSascha Hauer err_firmware: 11555b28aa31SSascha Hauer release_firmware(fw); 11565b28aa31SSascha Hauer 11575b28aa31SSascha Hauer return ret; 11585b28aa31SSascha Hauer } 11595b28aa31SSascha Hauer 11605b28aa31SSascha Hauer static int __init sdma_init(struct sdma_engine *sdma) 11611ec1e82fSSascha Hauer { 11621ec1e82fSSascha Hauer int i, ret; 11631ec1e82fSSascha Hauer dma_addr_t ccb_phys; 11641ec1e82fSSascha Hauer 11651ec1e82fSSascha Hauer switch (sdma->version) { 11661ec1e82fSSascha Hauer case 1: 11671ec1e82fSSascha Hauer sdma->num_events = 32; 11681ec1e82fSSascha Hauer break; 11691ec1e82fSSascha Hauer case 2: 11701ec1e82fSSascha Hauer sdma->num_events = 48; 11711ec1e82fSSascha Hauer break; 11721ec1e82fSSascha Hauer default: 11731ec1e82fSSascha Hauer dev_err(sdma->dev, "Unknown version %d. aborting\n", sdma->version); 11741ec1e82fSSascha Hauer return -ENODEV; 11751ec1e82fSSascha Hauer } 11761ec1e82fSSascha Hauer 11771ec1e82fSSascha Hauer clk_enable(sdma->clk); 11781ec1e82fSSascha Hauer 11791ec1e82fSSascha Hauer /* Be sure SDMA has not started yet */ 11801ec1e82fSSascha Hauer __raw_writel(0, sdma->regs + SDMA_H_C0PTR); 11811ec1e82fSSascha Hauer 11821ec1e82fSSascha Hauer sdma->channel_control = dma_alloc_coherent(NULL, 11831ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) + 11841ec1e82fSSascha Hauer sizeof(struct sdma_context_data), 11851ec1e82fSSascha Hauer &ccb_phys, GFP_KERNEL); 11861ec1e82fSSascha Hauer 11871ec1e82fSSascha Hauer if (!sdma->channel_control) { 11881ec1e82fSSascha Hauer ret = -ENOMEM; 11891ec1e82fSSascha Hauer goto err_dma_alloc; 11901ec1e82fSSascha Hauer } 11911ec1e82fSSascha Hauer 11921ec1e82fSSascha Hauer sdma->context = (void *)sdma->channel_control + 11931ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 11941ec1e82fSSascha Hauer sdma->context_phys = ccb_phys + 11951ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control); 11961ec1e82fSSascha Hauer 11971ec1e82fSSascha Hauer /* Zero-out the CCB structures array just allocated */ 11981ec1e82fSSascha Hauer memset(sdma->channel_control, 0, 11991ec1e82fSSascha Hauer MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control)); 12001ec1e82fSSascha Hauer 12011ec1e82fSSascha Hauer /* disable all channels */ 12021ec1e82fSSascha Hauer for (i = 0; i < sdma->num_events; i++) 12031ec1e82fSSascha Hauer __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i)); 12041ec1e82fSSascha Hauer 12051ec1e82fSSascha Hauer /* All channels have priority 0 */ 12061ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) 12071ec1e82fSSascha Hauer __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4); 12081ec1e82fSSascha Hauer 12091ec1e82fSSascha Hauer ret = sdma_request_channel(&sdma->channel[0]); 12101ec1e82fSSascha Hauer if (ret) 12111ec1e82fSSascha Hauer goto err_dma_alloc; 12121ec1e82fSSascha Hauer 12131ec1e82fSSascha Hauer sdma_config_ownership(&sdma->channel[0], false, true, false); 12141ec1e82fSSascha Hauer 12151ec1e82fSSascha Hauer /* Set Command Channel (Channel Zero) */ 12161ec1e82fSSascha Hauer __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR); 12171ec1e82fSSascha Hauer 12181ec1e82fSSascha Hauer /* Set bits of CONFIG register but with static context switching */ 12191ec1e82fSSascha Hauer /* FIXME: Check whether to set ACR bit depending on clock ratios */ 12201ec1e82fSSascha Hauer __raw_writel(0, sdma->regs + SDMA_H_CONFIG); 12211ec1e82fSSascha Hauer 12221ec1e82fSSascha Hauer __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR); 12231ec1e82fSSascha Hauer 12241ec1e82fSSascha Hauer /* Set bits of CONFIG register with given context switching mode */ 12251ec1e82fSSascha Hauer __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG); 12261ec1e82fSSascha Hauer 12271ec1e82fSSascha Hauer /* Initializes channel's priorities */ 12281ec1e82fSSascha Hauer sdma_set_channel_priority(&sdma->channel[0], 7); 12291ec1e82fSSascha Hauer 12301ec1e82fSSascha Hauer clk_disable(sdma->clk); 12311ec1e82fSSascha Hauer 12321ec1e82fSSascha Hauer return 0; 12331ec1e82fSSascha Hauer 12341ec1e82fSSascha Hauer err_dma_alloc: 12351ec1e82fSSascha Hauer clk_disable(sdma->clk); 12361ec1e82fSSascha Hauer dev_err(sdma->dev, "initialisation failed with %d\n", ret); 12371ec1e82fSSascha Hauer return ret; 12381ec1e82fSSascha Hauer } 12391ec1e82fSSascha Hauer 12401ec1e82fSSascha Hauer static int __init sdma_probe(struct platform_device *pdev) 12411ec1e82fSSascha Hauer { 12421ec1e82fSSascha Hauer int ret; 12431ec1e82fSSascha Hauer int irq; 12441ec1e82fSSascha Hauer struct resource *iores; 12451ec1e82fSSascha Hauer struct sdma_platform_data *pdata = pdev->dev.platform_data; 12461ec1e82fSSascha Hauer int i; 12471ec1e82fSSascha Hauer struct sdma_engine *sdma; 12481ec1e82fSSascha Hauer 12491ec1e82fSSascha Hauer sdma = kzalloc(sizeof(*sdma), GFP_KERNEL); 12501ec1e82fSSascha Hauer if (!sdma) 12511ec1e82fSSascha Hauer return -ENOMEM; 12521ec1e82fSSascha Hauer 12531ec1e82fSSascha Hauer sdma->dev = &pdev->dev; 12541ec1e82fSSascha Hauer 12551ec1e82fSSascha Hauer iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); 12561ec1e82fSSascha Hauer irq = platform_get_irq(pdev, 0); 12571ec1e82fSSascha Hauer if (!iores || irq < 0 || !pdata) { 12581ec1e82fSSascha Hauer ret = -EINVAL; 12591ec1e82fSSascha Hauer goto err_irq; 12601ec1e82fSSascha Hauer } 12611ec1e82fSSascha Hauer 12621ec1e82fSSascha Hauer if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) { 12631ec1e82fSSascha Hauer ret = -EBUSY; 12641ec1e82fSSascha Hauer goto err_request_region; 12651ec1e82fSSascha Hauer } 12661ec1e82fSSascha Hauer 12671ec1e82fSSascha Hauer sdma->clk = clk_get(&pdev->dev, NULL); 12681ec1e82fSSascha Hauer if (IS_ERR(sdma->clk)) { 12691ec1e82fSSascha Hauer ret = PTR_ERR(sdma->clk); 12701ec1e82fSSascha Hauer goto err_clk; 12711ec1e82fSSascha Hauer } 12721ec1e82fSSascha Hauer 12731ec1e82fSSascha Hauer sdma->regs = ioremap(iores->start, resource_size(iores)); 12741ec1e82fSSascha Hauer if (!sdma->regs) { 12751ec1e82fSSascha Hauer ret = -ENOMEM; 12761ec1e82fSSascha Hauer goto err_ioremap; 12771ec1e82fSSascha Hauer } 12781ec1e82fSSascha Hauer 12791ec1e82fSSascha Hauer ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma); 12801ec1e82fSSascha Hauer if (ret) 12811ec1e82fSSascha Hauer goto err_request_irq; 12821ec1e82fSSascha Hauer 12835b28aa31SSascha Hauer sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL); 1284*1c1d9547SAxel Lin if (!sdma->script_addrs) { 1285*1c1d9547SAxel Lin ret = -ENOMEM; 12865b28aa31SSascha Hauer goto err_alloc; 1287*1c1d9547SAxel Lin } 12881ec1e82fSSascha Hauer 12891ec1e82fSSascha Hauer sdma->version = pdata->sdma_version; 12901ec1e82fSSascha Hauer 12917214a8b1SSascha Hauer dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask); 12927214a8b1SSascha Hauer dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask); 12937214a8b1SSascha Hauer 12941ec1e82fSSascha Hauer INIT_LIST_HEAD(&sdma->dma_device.channels); 12951ec1e82fSSascha Hauer /* Initialize channel parameters */ 12961ec1e82fSSascha Hauer for (i = 0; i < MAX_DMA_CHANNELS; i++) { 12971ec1e82fSSascha Hauer struct sdma_channel *sdmac = &sdma->channel[i]; 12981ec1e82fSSascha Hauer 12991ec1e82fSSascha Hauer sdmac->sdma = sdma; 13001ec1e82fSSascha Hauer spin_lock_init(&sdmac->lock); 13011ec1e82fSSascha Hauer 13021ec1e82fSSascha Hauer sdmac->chan.device = &sdma->dma_device; 13031ec1e82fSSascha Hauer sdmac->channel = i; 13041ec1e82fSSascha Hauer 130523889c63SSascha Hauer /* 130623889c63SSascha Hauer * Add the channel to the DMAC list. Do not add channel 0 though 130723889c63SSascha Hauer * because we need it internally in the SDMA driver. This also means 130823889c63SSascha Hauer * that channel 0 in dmaengine counting matches sdma channel 1. 130923889c63SSascha Hauer */ 131023889c63SSascha Hauer if (i) 131123889c63SSascha Hauer list_add_tail(&sdmac->chan.device_node, 131223889c63SSascha Hauer &sdma->dma_device.channels); 13131ec1e82fSSascha Hauer } 13141ec1e82fSSascha Hauer 13155b28aa31SSascha Hauer ret = sdma_init(sdma); 13161ec1e82fSSascha Hauer if (ret) 13171ec1e82fSSascha Hauer goto err_init; 13181ec1e82fSSascha Hauer 13195b28aa31SSascha Hauer if (pdata->script_addrs) 13205b28aa31SSascha Hauer sdma_add_scripts(sdma, pdata->script_addrs); 13215b28aa31SSascha Hauer 13225b28aa31SSascha Hauer sdma_get_firmware(sdma, pdata->cpu_name, pdata->to_version); 13235b28aa31SSascha Hauer 13241ec1e82fSSascha Hauer sdma->dma_device.dev = &pdev->dev; 13251ec1e82fSSascha Hauer 13261ec1e82fSSascha Hauer sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources; 13271ec1e82fSSascha Hauer sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources; 13281ec1e82fSSascha Hauer sdma->dma_device.device_tx_status = sdma_tx_status; 13291ec1e82fSSascha Hauer sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg; 13301ec1e82fSSascha Hauer sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic; 13311ec1e82fSSascha Hauer sdma->dma_device.device_control = sdma_control; 13321ec1e82fSSascha Hauer sdma->dma_device.device_issue_pending = sdma_issue_pending; 1333b9b3f82fSSascha Hauer sdma->dma_device.dev->dma_parms = &sdma->dma_parms; 1334b9b3f82fSSascha Hauer dma_set_max_seg_size(sdma->dma_device.dev, 65535); 13351ec1e82fSSascha Hauer 13361ec1e82fSSascha Hauer ret = dma_async_device_register(&sdma->dma_device); 13371ec1e82fSSascha Hauer if (ret) { 13381ec1e82fSSascha Hauer dev_err(&pdev->dev, "unable to register\n"); 13391ec1e82fSSascha Hauer goto err_init; 13401ec1e82fSSascha Hauer } 13411ec1e82fSSascha Hauer 13425b28aa31SSascha Hauer dev_info(sdma->dev, "initialized\n"); 13431ec1e82fSSascha Hauer 13441ec1e82fSSascha Hauer return 0; 13451ec1e82fSSascha Hauer 13461ec1e82fSSascha Hauer err_init: 13471ec1e82fSSascha Hauer kfree(sdma->script_addrs); 13485b28aa31SSascha Hauer err_alloc: 13491ec1e82fSSascha Hauer free_irq(irq, sdma); 13501ec1e82fSSascha Hauer err_request_irq: 13511ec1e82fSSascha Hauer iounmap(sdma->regs); 13521ec1e82fSSascha Hauer err_ioremap: 13531ec1e82fSSascha Hauer clk_put(sdma->clk); 13541ec1e82fSSascha Hauer err_clk: 13551ec1e82fSSascha Hauer release_mem_region(iores->start, resource_size(iores)); 13561ec1e82fSSascha Hauer err_request_region: 13571ec1e82fSSascha Hauer err_irq: 13581ec1e82fSSascha Hauer kfree(sdma); 1359939fd4f0SShawn Guo return ret; 13601ec1e82fSSascha Hauer } 13611ec1e82fSSascha Hauer 13621ec1e82fSSascha Hauer static int __exit sdma_remove(struct platform_device *pdev) 13631ec1e82fSSascha Hauer { 13641ec1e82fSSascha Hauer return -EBUSY; 13651ec1e82fSSascha Hauer } 13661ec1e82fSSascha Hauer 13671ec1e82fSSascha Hauer static struct platform_driver sdma_driver = { 13681ec1e82fSSascha Hauer .driver = { 13691ec1e82fSSascha Hauer .name = "imx-sdma", 13701ec1e82fSSascha Hauer }, 13711ec1e82fSSascha Hauer .remove = __exit_p(sdma_remove), 13721ec1e82fSSascha Hauer }; 13731ec1e82fSSascha Hauer 13741ec1e82fSSascha Hauer static int __init sdma_module_init(void) 13751ec1e82fSSascha Hauer { 13761ec1e82fSSascha Hauer return platform_driver_probe(&sdma_driver, sdma_probe); 13771ec1e82fSSascha Hauer } 1378c989a7fcSSascha Hauer module_init(sdma_module_init); 13791ec1e82fSSascha Hauer 13801ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>"); 13811ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver"); 13821ec1e82fSSascha Hauer MODULE_LICENSE("GPL"); 1383