blob: aa35a77773eff606edd8142c97f60eeea0c9e7ef [file] [log] [blame]
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001/*
2 * drivers/dma/imx-sdma.c
3 *
4 * This file contains a driver for the Freescale Smart DMA engine
5 *
6 * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7 *
8 * Based on code from Freescale:
9 *
10 * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
11 *
12 * The code contained herein is licensed under the GNU General Public
13 * License. You may obtain a copy of the GNU General Public License
14 * Version 2 or later at the following locations:
15 *
16 * http://www.opensource.org/licenses/gpl-license.html
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20#include <linux/init.h>
Michael Olbrich1d069bf2016-07-07 11:35:51 +020021#include <linux/iopoll.h>
Axel Linf8de8f42011-08-30 15:08:24 +080022#include <linux/module.h>
Sascha Hauer1ec1e822010-09-30 13:56:34 +000023#include <linux/types.h>
Richard Zhao0bbc1412012-01-13 11:10:01 +080024#include <linux/bitops.h>
Sascha Hauer1ec1e822010-09-30 13:56:34 +000025#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <linux/clk.h>
Richard Zhao2ccaef02012-05-11 15:14:27 +080028#include <linux/delay.h>
Sascha Hauer1ec1e822010-09-30 13:56:34 +000029#include <linux/sched.h>
30#include <linux/semaphore.h>
31#include <linux/spinlock.h>
32#include <linux/device.h>
33#include <linux/dma-mapping.h>
34#include <linux/firmware.h>
35#include <linux/slab.h>
36#include <linux/platform_device.h>
37#include <linux/dmaengine.h>
Shawn Guo580975d2011-07-14 08:35:48 +080038#include <linux/of.h>
Shengjiu Wang8391ecf2015-07-10 17:08:16 +080039#include <linux/of_address.h>
Shawn Guo580975d2011-07-14 08:35:48 +080040#include <linux/of_device.h>
Shawn Guo9479e172013-05-30 22:23:32 +080041#include <linux/of_dma.h>
Sascha Hauer1ec1e822010-09-30 13:56:34 +000042
43#include <asm/irq.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020044#include <linux/platform_data/dma-imx-sdma.h>
45#include <linux/platform_data/dma-imx.h>
Zidan Wangd078cd12015-07-23 11:40:49 +080046#include <linux/regmap.h>
47#include <linux/mfd/syscon.h>
48#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
Sascha Hauer1ec1e822010-09-30 13:56:34 +000049
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000050#include "dmaengine.h"
51
Sascha Hauer1ec1e822010-09-30 13:56:34 +000052/* SDMA registers */
53#define SDMA_H_C0PTR 0x000
54#define SDMA_H_INTR 0x004
55#define SDMA_H_STATSTOP 0x008
56#define SDMA_H_START 0x00c
57#define SDMA_H_EVTOVR 0x010
58#define SDMA_H_DSPOVR 0x014
59#define SDMA_H_HOSTOVR 0x018
60#define SDMA_H_EVTPEND 0x01c
61#define SDMA_H_DSPENBL 0x020
62#define SDMA_H_RESET 0x024
63#define SDMA_H_EVTERR 0x028
64#define SDMA_H_INTRMSK 0x02c
65#define SDMA_H_PSW 0x030
66#define SDMA_H_EVTERRDBG 0x034
67#define SDMA_H_CONFIG 0x038
68#define SDMA_ONCE_ENB 0x040
69#define SDMA_ONCE_DATA 0x044
70#define SDMA_ONCE_INSTR 0x048
71#define SDMA_ONCE_STAT 0x04c
72#define SDMA_ONCE_CMD 0x050
73#define SDMA_EVT_MIRROR 0x054
74#define SDMA_ILLINSTADDR 0x058
75#define SDMA_CHN0ADDR 0x05c
76#define SDMA_ONCE_RTB 0x060
77#define SDMA_XTRIG_CONF1 0x070
78#define SDMA_XTRIG_CONF2 0x074
Shawn Guo62550cd2011-07-13 21:33:17 +080079#define SDMA_CHNENBL0_IMX35 0x200
80#define SDMA_CHNENBL0_IMX31 0x080
Sascha Hauer1ec1e822010-09-30 13:56:34 +000081#define SDMA_CHNPRI_0 0x100
82
83/*
84 * Buffer descriptor status values.
85 */
86#define BD_DONE 0x01
87#define BD_WRAP 0x02
88#define BD_CONT 0x04
89#define BD_INTR 0x08
90#define BD_RROR 0x10
91#define BD_LAST 0x20
92#define BD_EXTD 0x80
93
94/*
95 * Data Node descriptor status values.
96 */
97#define DND_END_OF_FRAME 0x80
98#define DND_END_OF_XFER 0x40
99#define DND_DONE 0x20
100#define DND_UNUSED 0x01
101
102/*
103 * IPCV2 descriptor status values.
104 */
105#define BD_IPCV2_END_OF_FRAME 0x40
106
107#define IPCV2_MAX_NODES 50
108/*
109 * Error bit set in the CCB status field by the SDMA,
110 * in setbd routine, in case of a transfer error
111 */
112#define DATA_ERROR 0x10000000
113
114/*
115 * Buffer descriptor commands.
116 */
117#define C0_ADDR 0x01
118#define C0_LOAD 0x02
119#define C0_DUMP 0x03
120#define C0_SETCTX 0x07
121#define C0_GETCTX 0x03
122#define C0_SETDM 0x01
123#define C0_SETPM 0x04
124#define C0_GETDM 0x02
125#define C0_GETPM 0x08
126/*
127 * Change endianness indicator in the BD command field
128 */
129#define CHANGE_ENDIANNESS 0x80
130
131/*
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800132 * p_2_p watermark_level description
133 * Bits Name Description
134 * 0-7 Lower WML Lower watermark level
135 * 8 PS 1: Pad Swallowing
136 * 0: No Pad Swallowing
137 * 9 PA 1: Pad Adding
138 * 0: No Pad Adding
139 * 10 SPDIF If this bit is set both source
140 * and destination are on SPBA
141 * 11 Source Bit(SP) 1: Source on SPBA
142 * 0: Source on AIPS
143 * 12 Destination Bit(DP) 1: Destination on SPBA
144 * 0: Destination on AIPS
145 * 13-15 --------- MUST BE 0
146 * 16-23 Higher WML HWML
147 * 24-27 N Total number of samples after
148 * which Pad adding/Swallowing
149 * must be done. It must be odd.
150 * 28 Lower WML Event(LWE) SDMA events reg to check for
151 * LWML event mask
152 * 0: LWE in EVENTS register
153 * 1: LWE in EVENTS2 register
154 * 29 Higher WML Event(HWE) SDMA events reg to check for
155 * HWML event mask
156 * 0: HWE in EVENTS register
157 * 1: HWE in EVENTS2 register
158 * 30 --------- MUST BE 0
159 * 31 CONT 1: Amount of samples to be
160 * transferred is unknown and
161 * script will keep on
162 * transferring samples as long as
163 * both events are detected and
164 * script must be manually stopped
165 * by the application
166 * 0: The amount of samples to be
167 * transferred is equal to the
168 * count field of mode word
169 */
170#define SDMA_WATERMARK_LEVEL_LWML 0xFF
171#define SDMA_WATERMARK_LEVEL_PS BIT(8)
172#define SDMA_WATERMARK_LEVEL_PA BIT(9)
173#define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
174#define SDMA_WATERMARK_LEVEL_SP BIT(11)
175#define SDMA_WATERMARK_LEVEL_DP BIT(12)
176#define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
177#define SDMA_WATERMARK_LEVEL_LWE BIT(28)
178#define SDMA_WATERMARK_LEVEL_HWE BIT(29)
179#define SDMA_WATERMARK_LEVEL_CONT BIT(31)
180
181/*
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000182 * Mode/Count of data node descriptors - IPCv2
183 */
184struct sdma_mode_count {
185 u32 count : 16; /* size of the buffer pointed by this BD */
186 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
187 u32 command : 8; /* command mostlky used for channel 0 */
188};
189
190/*
191 * Buffer descriptor
192 */
193struct sdma_buffer_descriptor {
194 struct sdma_mode_count mode;
195 u32 buffer_addr; /* address of the buffer described */
196 u32 ext_buffer_addr; /* extended buffer address */
197} __attribute__ ((packed));
198
199/**
200 * struct sdma_channel_control - Channel control Block
201 *
202 * @current_bd_ptr current buffer descriptor processed
203 * @base_bd_ptr first element of buffer descriptor array
204 * @unused padding. The SDMA engine expects an array of 128 byte
205 * control blocks
206 */
207struct sdma_channel_control {
208 u32 current_bd_ptr;
209 u32 base_bd_ptr;
210 u32 unused[2];
211} __attribute__ ((packed));
212
213/**
214 * struct sdma_state_registers - SDMA context for a channel
215 *
216 * @pc: program counter
217 * @t: test bit: status of arithmetic & test instruction
218 * @rpc: return program counter
219 * @sf: source fault while loading data
220 * @spc: loop start program counter
221 * @df: destination fault while storing data
222 * @epc: loop end program counter
223 * @lm: loop mode
224 */
225struct sdma_state_registers {
226 u32 pc :14;
227 u32 unused1: 1;
228 u32 t : 1;
229 u32 rpc :14;
230 u32 unused0: 1;
231 u32 sf : 1;
232 u32 spc :14;
233 u32 unused2: 1;
234 u32 df : 1;
235 u32 epc :14;
236 u32 lm : 2;
237} __attribute__ ((packed));
238
239/**
240 * struct sdma_context_data - sdma context specific to a channel
241 *
242 * @channel_state: channel state bits
243 * @gReg: general registers
244 * @mda: burst dma destination address register
245 * @msa: burst dma source address register
246 * @ms: burst dma status register
247 * @md: burst dma data register
248 * @pda: peripheral dma destination address register
249 * @psa: peripheral dma source address register
250 * @ps: peripheral dma status register
251 * @pd: peripheral dma data register
252 * @ca: CRC polynomial register
253 * @cs: CRC accumulator register
254 * @dda: dedicated core destination address register
255 * @dsa: dedicated core source address register
256 * @ds: dedicated core status register
257 * @dd: dedicated core data register
258 */
259struct sdma_context_data {
260 struct sdma_state_registers channel_state;
261 u32 gReg[8];
262 u32 mda;
263 u32 msa;
264 u32 ms;
265 u32 md;
266 u32 pda;
267 u32 psa;
268 u32 ps;
269 u32 pd;
270 u32 ca;
271 u32 cs;
272 u32 dda;
273 u32 dsa;
274 u32 ds;
275 u32 dd;
276 u32 scratch0;
277 u32 scratch1;
278 u32 scratch2;
279 u32 scratch3;
280 u32 scratch4;
281 u32 scratch5;
282 u32 scratch6;
283 u32 scratch7;
284} __attribute__ ((packed));
285
286#define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
287
288struct sdma_engine;
289
290/**
291 * struct sdma_channel - housekeeping for a SDMA channel
292 *
293 * @sdma pointer to the SDMA engine for this channel
Sascha Hauer23889c62011-01-31 10:56:58 +0100294 * @channel the channel number, matches dmaengine chan_id + 1
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000295 * @direction transfer type. Needed for setting SDMA script
296 * @peripheral_type Peripheral type. Needed for setting SDMA script
297 * @event_id0 aka dma request line
298 * @event_id1 for channels that use 2 events
299 * @word_size peripheral access size
300 * @buf_tail ID of the buffer that was processed
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000301 * @num_bd max NUM_BD. number of descriptors currently handling
302 */
303struct sdma_channel {
304 struct sdma_engine *sdma;
305 unsigned int channel;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530306 enum dma_transfer_direction direction;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000307 enum sdma_peripheral_type peripheral_type;
308 unsigned int event_id0;
309 unsigned int event_id1;
310 enum dma_slave_buswidth word_size;
311 unsigned int buf_tail;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000312 unsigned int num_bd;
Russell King - ARM Linuxd1a792f2014-06-25 13:00:33 +0100313 unsigned int period_len;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000314 struct sdma_buffer_descriptor *bd;
315 dma_addr_t bd_phys;
316 unsigned int pc_from_device, pc_to_device;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800317 unsigned int device_to_device;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000318 unsigned long flags;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800319 dma_addr_t per_address, per_address2;
Richard Zhao0bbc1412012-01-13 11:10:01 +0800320 unsigned long event_mask[2];
321 unsigned long watermark_level;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000322 u32 shp_addr, per_addr;
323 struct dma_chan chan;
324 spinlock_t lock;
325 struct dma_async_tx_descriptor desc;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000326 enum dma_status status;
Huang Shijieab59a512011-12-02 10:16:25 +0800327 unsigned int chn_count;
328 unsigned int chn_real_count;
Huang Shijieabd9ccc2012-04-28 18:15:42 +0800329 struct tasklet_struct tasklet;
Nicolin Chen0b351862014-06-16 11:32:29 +0800330 struct imx_dma_data data;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000331};
332
Richard Zhao0bbc1412012-01-13 11:10:01 +0800333#define IMX_DMA_SG_LOOP BIT(0)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000334
335#define MAX_DMA_CHANNELS 32
336#define MXC_SDMA_DEFAULT_PRIORITY 1
337#define MXC_SDMA_MIN_PRIORITY 1
338#define MXC_SDMA_MAX_PRIORITY 7
339
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000340#define SDMA_FIRMWARE_MAGIC 0x414d4453
341
342/**
343 * struct sdma_firmware_header - Layout of the firmware image
344 *
345 * @magic "SDMA"
346 * @version_major increased whenever layout of struct sdma_script_start_addrs
347 * changes.
348 * @version_minor firmware minor version (for binary compatible changes)
349 * @script_addrs_start offset of struct sdma_script_start_addrs in this image
350 * @num_script_addrs Number of script addresses in this image
351 * @ram_code_start offset of SDMA ram image in this firmware image
352 * @ram_code_size size of SDMA ram image
353 * @script_addrs Stores the start address of the SDMA scripts
354 * (in SDMA memory space)
355 */
356struct sdma_firmware_header {
357 u32 magic;
358 u32 version_major;
359 u32 version_minor;
360 u32 script_addrs_start;
361 u32 num_script_addrs;
362 u32 ram_code_start;
363 u32 ram_code_size;
364};
365
Sascha Hauer17bba722013-08-20 10:04:31 +0200366struct sdma_driver_data {
367 int chnenbl0;
368 int num_events;
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200369 struct sdma_script_start_addrs *script_addrs;
Shawn Guo62550cd2011-07-13 21:33:17 +0800370};
371
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000372struct sdma_engine {
373 struct device *dev;
Sascha Hauerb9b3f822011-01-12 12:12:31 +0100374 struct device_dma_parameters dma_parms;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000375 struct sdma_channel channel[MAX_DMA_CHANNELS];
376 struct sdma_channel_control *channel_control;
377 void __iomem *regs;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000378 struct sdma_context_data *context;
379 dma_addr_t context_phys;
380 struct dma_device dma_device;
Sascha Hauer7560e3f2012-03-07 09:30:06 +0100381 struct clk *clk_ipg;
382 struct clk *clk_ahb;
Richard Zhao2ccaef02012-05-11 15:14:27 +0800383 spinlock_t channel_0_lock;
Nicolin Chencd72b842013-11-13 22:55:24 +0800384 u32 script_number;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000385 struct sdma_script_start_addrs *script_addrs;
Sascha Hauer17bba722013-08-20 10:04:31 +0200386 const struct sdma_driver_data *drvdata;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800387 u32 spba_start_addr;
388 u32 spba_end_addr;
Vinod Koul5bb9dbb2016-07-03 00:00:55 +0530389 unsigned int irq;
Sascha Hauer17bba722013-08-20 10:04:31 +0200390};
391
Fabio Estevame9fd58d2013-09-01 21:57:12 -0300392static struct sdma_driver_data sdma_imx31 = {
Sascha Hauer17bba722013-08-20 10:04:31 +0200393 .chnenbl0 = SDMA_CHNENBL0_IMX31,
394 .num_events = 32,
395};
396
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200397static struct sdma_script_start_addrs sdma_script_imx25 = {
398 .ap_2_ap_addr = 729,
399 .uart_2_mcu_addr = 904,
400 .per_2_app_addr = 1255,
401 .mcu_2_app_addr = 834,
402 .uartsh_2_mcu_addr = 1120,
403 .per_2_shp_addr = 1329,
404 .mcu_2_shp_addr = 1048,
405 .ata_2_mcu_addr = 1560,
406 .mcu_2_ata_addr = 1479,
407 .app_2_per_addr = 1189,
408 .app_2_mcu_addr = 770,
409 .shp_2_per_addr = 1407,
410 .shp_2_mcu_addr = 979,
411};
412
Fabio Estevame9fd58d2013-09-01 21:57:12 -0300413static struct sdma_driver_data sdma_imx25 = {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200414 .chnenbl0 = SDMA_CHNENBL0_IMX35,
415 .num_events = 48,
416 .script_addrs = &sdma_script_imx25,
417};
418
Fabio Estevame9fd58d2013-09-01 21:57:12 -0300419static struct sdma_driver_data sdma_imx35 = {
Sascha Hauer17bba722013-08-20 10:04:31 +0200420 .chnenbl0 = SDMA_CHNENBL0_IMX35,
421 .num_events = 48,
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000422};
423
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200424static struct sdma_script_start_addrs sdma_script_imx51 = {
425 .ap_2_ap_addr = 642,
426 .uart_2_mcu_addr = 817,
427 .mcu_2_app_addr = 747,
428 .mcu_2_shp_addr = 961,
429 .ata_2_mcu_addr = 1473,
430 .mcu_2_ata_addr = 1392,
431 .app_2_per_addr = 1033,
432 .app_2_mcu_addr = 683,
433 .shp_2_per_addr = 1251,
434 .shp_2_mcu_addr = 892,
435};
436
Fabio Estevame9fd58d2013-09-01 21:57:12 -0300437static struct sdma_driver_data sdma_imx51 = {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200438 .chnenbl0 = SDMA_CHNENBL0_IMX35,
439 .num_events = 48,
440 .script_addrs = &sdma_script_imx51,
441};
442
443static struct sdma_script_start_addrs sdma_script_imx53 = {
444 .ap_2_ap_addr = 642,
445 .app_2_mcu_addr = 683,
446 .mcu_2_app_addr = 747,
447 .uart_2_mcu_addr = 817,
448 .shp_2_mcu_addr = 891,
449 .mcu_2_shp_addr = 960,
450 .uartsh_2_mcu_addr = 1032,
451 .spdif_2_mcu_addr = 1100,
452 .mcu_2_spdif_addr = 1134,
453 .firi_2_mcu_addr = 1193,
454 .mcu_2_firi_addr = 1290,
455};
456
Fabio Estevame9fd58d2013-09-01 21:57:12 -0300457static struct sdma_driver_data sdma_imx53 = {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200458 .chnenbl0 = SDMA_CHNENBL0_IMX35,
459 .num_events = 48,
460 .script_addrs = &sdma_script_imx53,
461};
462
463static struct sdma_script_start_addrs sdma_script_imx6q = {
464 .ap_2_ap_addr = 642,
465 .uart_2_mcu_addr = 817,
466 .mcu_2_app_addr = 747,
467 .per_2_per_addr = 6331,
468 .uartsh_2_mcu_addr = 1032,
469 .mcu_2_shp_addr = 960,
470 .app_2_mcu_addr = 683,
471 .shp_2_mcu_addr = 891,
472 .spdif_2_mcu_addr = 1100,
473 .mcu_2_spdif_addr = 1134,
474};
475
Fabio Estevame9fd58d2013-09-01 21:57:12 -0300476static struct sdma_driver_data sdma_imx6q = {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200477 .chnenbl0 = SDMA_CHNENBL0_IMX35,
478 .num_events = 48,
479 .script_addrs = &sdma_script_imx6q,
480};
481
Krzysztof Kozlowskiafe7cde2015-05-02 00:57:46 +0900482static const struct platform_device_id sdma_devtypes[] = {
Shawn Guo62550cd2011-07-13 21:33:17 +0800483 {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200484 .name = "imx25-sdma",
485 .driver_data = (unsigned long)&sdma_imx25,
486 }, {
Shawn Guo62550cd2011-07-13 21:33:17 +0800487 .name = "imx31-sdma",
Sascha Hauer17bba722013-08-20 10:04:31 +0200488 .driver_data = (unsigned long)&sdma_imx31,
Shawn Guo62550cd2011-07-13 21:33:17 +0800489 }, {
490 .name = "imx35-sdma",
Sascha Hauer17bba722013-08-20 10:04:31 +0200491 .driver_data = (unsigned long)&sdma_imx35,
Shawn Guo62550cd2011-07-13 21:33:17 +0800492 }, {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200493 .name = "imx51-sdma",
494 .driver_data = (unsigned long)&sdma_imx51,
495 }, {
496 .name = "imx53-sdma",
497 .driver_data = (unsigned long)&sdma_imx53,
498 }, {
499 .name = "imx6q-sdma",
500 .driver_data = (unsigned long)&sdma_imx6q,
501 }, {
Shawn Guo62550cd2011-07-13 21:33:17 +0800502 /* sentinel */
503 }
504};
505MODULE_DEVICE_TABLE(platform, sdma_devtypes);
506
Shawn Guo580975d2011-07-14 08:35:48 +0800507static const struct of_device_id sdma_dt_ids[] = {
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200508 { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
509 { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
510 { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
Sascha Hauer17bba722013-08-20 10:04:31 +0200511 { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
Sascha Hauerdcfec3c2013-08-20 10:04:32 +0200512 { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
Markus Pargmann63edea12014-02-16 20:10:55 +0100513 { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
Shawn Guo580975d2011-07-14 08:35:48 +0800514 { /* sentinel */ }
515};
516MODULE_DEVICE_TABLE(of, sdma_dt_ids);
517
Richard Zhao0bbc1412012-01-13 11:10:01 +0800518#define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
519#define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
520#define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000521#define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
522
523static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
524{
Sascha Hauer17bba722013-08-20 10:04:31 +0200525 u32 chnenbl0 = sdma->drvdata->chnenbl0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000526 return chnenbl0 + event * 4;
527}
528
529static int sdma_config_ownership(struct sdma_channel *sdmac,
530 bool event_override, bool mcu_override, bool dsp_override)
531{
532 struct sdma_engine *sdma = sdmac->sdma;
533 int channel = sdmac->channel;
Richard Zhao0bbc1412012-01-13 11:10:01 +0800534 unsigned long evt, mcu, dsp;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000535
536 if (event_override && mcu_override && dsp_override)
537 return -EINVAL;
538
Richard Zhaoc4b56852012-01-13 11:09:57 +0800539 evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
540 mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
541 dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000542
543 if (dsp_override)
Richard Zhao0bbc1412012-01-13 11:10:01 +0800544 __clear_bit(channel, &dsp);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000545 else
Richard Zhao0bbc1412012-01-13 11:10:01 +0800546 __set_bit(channel, &dsp);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000547
548 if (event_override)
Richard Zhao0bbc1412012-01-13 11:10:01 +0800549 __clear_bit(channel, &evt);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000550 else
Richard Zhao0bbc1412012-01-13 11:10:01 +0800551 __set_bit(channel, &evt);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000552
553 if (mcu_override)
Richard Zhao0bbc1412012-01-13 11:10:01 +0800554 __clear_bit(channel, &mcu);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000555 else
Richard Zhao0bbc1412012-01-13 11:10:01 +0800556 __set_bit(channel, &mcu);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000557
Richard Zhaoc4b56852012-01-13 11:09:57 +0800558 writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
559 writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
560 writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000561
562 return 0;
563}
564
Richard Zhaob9a591662012-01-13 11:09:56 +0800565static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
566{
Richard Zhao0bbc1412012-01-13 11:10:01 +0800567 writel(BIT(channel), sdma->regs + SDMA_H_START);
Richard Zhaob9a591662012-01-13 11:09:56 +0800568}
569
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000570/*
Richard Zhao2ccaef02012-05-11 15:14:27 +0800571 * sdma_run_channel0 - run a channel and wait till it's done
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000572 */
Richard Zhao2ccaef02012-05-11 15:14:27 +0800573static int sdma_run_channel0(struct sdma_engine *sdma)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000574{
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000575 int ret;
Michael Olbrich1d069bf2016-07-07 11:35:51 +0200576 u32 reg;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000577
Richard Zhao2ccaef02012-05-11 15:14:27 +0800578 sdma_enable_channel(sdma, 0);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000579
Michael Olbrich1d069bf2016-07-07 11:35:51 +0200580 ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
581 reg, !(reg & 1), 1, 500);
582 if (ret)
Richard Zhao2ccaef02012-05-11 15:14:27 +0800583 dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000584
Robin Gong855832e2015-02-15 10:00:35 +0800585 /* Set bits of CONFIG register with dynamic context switching */
586 if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
587 writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
588
Michael Olbrich1d069bf2016-07-07 11:35:51 +0200589 return ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000590}
591
592static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
593 u32 address)
594{
595 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
596 void *buf_virt;
597 dma_addr_t buf_phys;
598 int ret;
Richard Zhao2ccaef02012-05-11 15:14:27 +0800599 unsigned long flags;
Sascha Hauer73eab972011-08-25 11:03:35 +0200600
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000601 buf_virt = dma_alloc_coherent(NULL,
602 size,
603 &buf_phys, GFP_KERNEL);
Sascha Hauer73eab972011-08-25 11:03:35 +0200604 if (!buf_virt) {
Richard Zhao2ccaef02012-05-11 15:14:27 +0800605 return -ENOMEM;
Sascha Hauer73eab972011-08-25 11:03:35 +0200606 }
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000607
Richard Zhao2ccaef02012-05-11 15:14:27 +0800608 spin_lock_irqsave(&sdma->channel_0_lock, flags);
609
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000610 bd0->mode.command = C0_SETPM;
611 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
612 bd0->mode.count = size / 2;
613 bd0->buffer_addr = buf_phys;
614 bd0->ext_buffer_addr = address;
615
616 memcpy(buf_virt, buf, size);
617
Richard Zhao2ccaef02012-05-11 15:14:27 +0800618 ret = sdma_run_channel0(sdma);
619
620 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000621
622 dma_free_coherent(NULL, size, buf_virt, buf_phys);
623
624 return ret;
625}
626
627static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
628{
629 struct sdma_engine *sdma = sdmac->sdma;
630 int channel = sdmac->channel;
Richard Zhao0bbc1412012-01-13 11:10:01 +0800631 unsigned long val;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000632 u32 chnenbl = chnenbl_ofs(sdma, event);
633
Richard Zhaoc4b56852012-01-13 11:09:57 +0800634 val = readl_relaxed(sdma->regs + chnenbl);
Richard Zhao0bbc1412012-01-13 11:10:01 +0800635 __set_bit(channel, &val);
Richard Zhaoc4b56852012-01-13 11:09:57 +0800636 writel_relaxed(val, sdma->regs + chnenbl);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000637}
638
639static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
640{
641 struct sdma_engine *sdma = sdmac->sdma;
642 int channel = sdmac->channel;
643 u32 chnenbl = chnenbl_ofs(sdma, event);
Richard Zhao0bbc1412012-01-13 11:10:01 +0800644 unsigned long val;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000645
Richard Zhaoc4b56852012-01-13 11:09:57 +0800646 val = readl_relaxed(sdma->regs + chnenbl);
Richard Zhao0bbc1412012-01-13 11:10:01 +0800647 __clear_bit(channel, &val);
Richard Zhaoc4b56852012-01-13 11:09:57 +0800648 writel_relaxed(val, sdma->regs + chnenbl);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000649}
650
Russell King - ARM Linuxd1a792f2014-06-25 13:00:33 +0100651static void sdma_update_channel_loop(struct sdma_channel *sdmac)
652{
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000653 struct sdma_buffer_descriptor *bd;
654
655 /*
656 * loop mode. Iterate over descriptors, re-setup them and
657 * call callback function.
658 */
659 while (1) {
660 bd = &sdmac->bd[sdmac->buf_tail];
661
662 if (bd->mode.status & BD_DONE)
663 break;
664
665 if (bd->mode.status & BD_RROR)
666 sdmac->status = DMA_ERROR;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000667
668 bd->mode.status |= BD_DONE;
Nandor Han15f30f52016-08-08 15:38:25 +0300669
670 /*
671 * The callback is called from the interrupt context in order
672 * to reduce latency and to avoid the risk of altering the
673 * SDMA transaction status by the time the client tasklet is
674 * executed.
675 */
676
677 if (sdmac->desc.callback)
678 sdmac->desc.callback(sdmac->desc.callback_param);
679
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000680 sdmac->buf_tail++;
681 sdmac->buf_tail %= sdmac->num_bd;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000682 }
683}
684
Nandor Han15f30f52016-08-08 15:38:25 +0300685static void mxc_sdma_handle_channel_normal(unsigned long data)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000686{
Nandor Han15f30f52016-08-08 15:38:25 +0300687 struct sdma_channel *sdmac = (struct sdma_channel *) data;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000688 struct sdma_buffer_descriptor *bd;
689 int i, error = 0;
690
Huang Shijieab59a512011-12-02 10:16:25 +0800691 sdmac->chn_real_count = 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000692 /*
693 * non loop mode. Iterate over all descriptors, collect
694 * errors and call callback function
695 */
696 for (i = 0; i < sdmac->num_bd; i++) {
697 bd = &sdmac->bd[i];
698
699 if (bd->mode.status & (BD_DONE | BD_RROR))
700 error = -EIO;
Huang Shijieab59a512011-12-02 10:16:25 +0800701 sdmac->chn_real_count += bd->mode.count;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000702 }
703
704 if (error)
705 sdmac->status = DMA_ERROR;
706 else
Vinod Koul409bff62013-10-16 14:07:06 +0530707 sdmac->status = DMA_COMPLETE;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000708
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000709 dma_cookie_complete(&sdmac->desc);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000710 if (sdmac->desc.callback)
711 sdmac->desc.callback(sdmac->desc.callback_param);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000712}
713
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000714static irqreturn_t sdma_int_handler(int irq, void *dev_id)
715{
716 struct sdma_engine *sdma = dev_id;
Richard Zhao0bbc1412012-01-13 11:10:01 +0800717 unsigned long stat;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000718
Richard Zhaoc4b56852012-01-13 11:09:57 +0800719 stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
720 writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
Michael Olbrich1d069bf2016-07-07 11:35:51 +0200721 /* channel 0 is special and not handled here, see run_channel0() */
722 stat &= ~1;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000723
724 while (stat) {
725 int channel = fls(stat) - 1;
726 struct sdma_channel *sdmac = &sdma->channel[channel];
727
Russell King - ARM Linuxd1a792f2014-06-25 13:00:33 +0100728 if (sdmac->flags & IMX_DMA_SG_LOOP)
729 sdma_update_channel_loop(sdmac);
Nandor Han15f30f52016-08-08 15:38:25 +0300730 else
731 tasklet_schedule(&sdmac->tasklet);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000732
Richard Zhao0bbc1412012-01-13 11:10:01 +0800733 __clear_bit(channel, &stat);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000734 }
735
736 return IRQ_HANDLED;
737}
738
739/*
740 * sets the pc of SDMA script according to the peripheral type
741 */
742static void sdma_get_pc(struct sdma_channel *sdmac,
743 enum sdma_peripheral_type peripheral_type)
744{
745 struct sdma_engine *sdma = sdmac->sdma;
746 int per_2_emi = 0, emi_2_per = 0;
747 /*
748 * These are needed once we start to support transfers between
749 * two peripherals or memory-to-memory transfers
750 */
Vinod Koul0d605ba2016-07-08 10:43:27 +0530751 int per_2_per = 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000752
753 sdmac->pc_from_device = 0;
754 sdmac->pc_to_device = 0;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800755 sdmac->device_to_device = 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000756
757 switch (peripheral_type) {
758 case IMX_DMATYPE_MEMORY:
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000759 break;
760 case IMX_DMATYPE_DSP:
761 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
762 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
763 break;
764 case IMX_DMATYPE_FIRI:
765 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
766 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
767 break;
768 case IMX_DMATYPE_UART:
769 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
770 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
771 break;
772 case IMX_DMATYPE_UART_SP:
773 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
774 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
775 break;
776 case IMX_DMATYPE_ATA:
777 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
778 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
779 break;
780 case IMX_DMATYPE_CSPI:
781 case IMX_DMATYPE_EXT:
782 case IMX_DMATYPE_SSI:
Nicolin Chen29aebfd2014-10-24 12:37:41 -0700783 case IMX_DMATYPE_SAI:
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000784 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
785 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
786 break;
Nicolin Chen1a895572013-11-13 22:55:25 +0800787 case IMX_DMATYPE_SSI_DUAL:
788 per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
789 emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
790 break;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000791 case IMX_DMATYPE_SSI_SP:
792 case IMX_DMATYPE_MMC:
793 case IMX_DMATYPE_SDHC:
794 case IMX_DMATYPE_CSPI_SP:
795 case IMX_DMATYPE_ESAI:
796 case IMX_DMATYPE_MSHC_SP:
797 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
798 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
799 break;
800 case IMX_DMATYPE_ASRC:
801 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
802 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
803 per_2_per = sdma->script_addrs->per_2_per_addr;
804 break;
Nicolin Chenf892afb2014-06-16 11:31:05 +0800805 case IMX_DMATYPE_ASRC_SP:
806 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
807 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
808 per_2_per = sdma->script_addrs->per_2_per_addr;
809 break;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000810 case IMX_DMATYPE_MSHC:
811 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
812 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
813 break;
814 case IMX_DMATYPE_CCM:
815 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
816 break;
817 case IMX_DMATYPE_SPDIF:
818 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
819 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
820 break;
821 case IMX_DMATYPE_IPU_MEMORY:
822 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
823 break;
824 default:
825 break;
826 }
827
828 sdmac->pc_from_device = per_2_emi;
829 sdmac->pc_to_device = emi_2_per;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800830 sdmac->device_to_device = per_2_per;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000831}
832
833static int sdma_load_context(struct sdma_channel *sdmac)
834{
835 struct sdma_engine *sdma = sdmac->sdma;
836 int channel = sdmac->channel;
837 int load_address;
838 struct sdma_context_data *context = sdma->context;
839 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
840 int ret;
Richard Zhao2ccaef02012-05-11 15:14:27 +0800841 unsigned long flags;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000842
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800843 if (sdmac->direction == DMA_DEV_TO_MEM)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000844 load_address = sdmac->pc_from_device;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800845 else if (sdmac->direction == DMA_DEV_TO_DEV)
846 load_address = sdmac->device_to_device;
847 else
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000848 load_address = sdmac->pc_to_device;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000849
850 if (load_address < 0)
851 return load_address;
852
853 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
Richard Zhao0bbc1412012-01-13 11:10:01 +0800854 dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000855 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
856 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
Richard Zhao0bbc1412012-01-13 11:10:01 +0800857 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
858 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000859
Richard Zhao2ccaef02012-05-11 15:14:27 +0800860 spin_lock_irqsave(&sdma->channel_0_lock, flags);
Sascha Hauer73eab972011-08-25 11:03:35 +0200861
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000862 memset(context, 0, sizeof(*context));
863 context->channel_state.pc = load_address;
864
865 /* Send by context the event mask,base address for peripheral
866 * and watermark level
867 */
Richard Zhao0bbc1412012-01-13 11:10:01 +0800868 context->gReg[0] = sdmac->event_mask[1];
869 context->gReg[1] = sdmac->event_mask[0];
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000870 context->gReg[2] = sdmac->per_addr;
871 context->gReg[6] = sdmac->shp_addr;
872 context->gReg[7] = sdmac->watermark_level;
873
874 bd0->mode.command = C0_SETDM;
875 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
876 bd0->mode.count = sizeof(*context) / 4;
877 bd0->buffer_addr = sdma->context_phys;
878 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
Richard Zhao2ccaef02012-05-11 15:14:27 +0800879 ret = sdma_run_channel0(sdma);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000880
Richard Zhao2ccaef02012-05-11 15:14:27 +0800881 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
Sascha Hauer73eab972011-08-25 11:03:35 +0200882
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000883 return ret;
884}
885
Maxime Ripard7b350ab2014-11-17 14:42:17 +0100886static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000887{
Maxime Ripard7b350ab2014-11-17 14:42:17 +0100888 return container_of(chan, struct sdma_channel, chan);
889}
890
891static int sdma_disable_channel(struct dma_chan *chan)
892{
893 struct sdma_channel *sdmac = to_sdma_chan(chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000894 struct sdma_engine *sdma = sdmac->sdma;
895 int channel = sdmac->channel;
896
Richard Zhao0bbc1412012-01-13 11:10:01 +0800897 writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000898 sdmac->status = DMA_ERROR;
Maxime Ripard7b350ab2014-11-17 14:42:17 +0100899
900 return 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000901}
902
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800903static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
904{
905 struct sdma_engine *sdma = sdmac->sdma;
906
907 int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
908 int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
909
910 set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
911 set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
912
913 if (sdmac->event_id0 > 31)
914 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
915
916 if (sdmac->event_id1 > 31)
917 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
918
919 /*
920 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
921 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
922 * r0(event_mask[1]) and r1(event_mask[0]).
923 */
924 if (lwml > hwml) {
925 sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
926 SDMA_WATERMARK_LEVEL_HWML);
927 sdmac->watermark_level |= hwml;
928 sdmac->watermark_level |= lwml << 16;
929 swap(sdmac->event_mask[0], sdmac->event_mask[1]);
930 }
931
932 if (sdmac->per_address2 >= sdma->spba_start_addr &&
933 sdmac->per_address2 <= sdma->spba_end_addr)
934 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
935
936 if (sdmac->per_address >= sdma->spba_start_addr &&
937 sdmac->per_address <= sdma->spba_end_addr)
938 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
939
940 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
941}
942
Maxime Ripard7b350ab2014-11-17 14:42:17 +0100943static int sdma_config_channel(struct dma_chan *chan)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000944{
Maxime Ripard7b350ab2014-11-17 14:42:17 +0100945 struct sdma_channel *sdmac = to_sdma_chan(chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000946 int ret;
947
Maxime Ripard7b350ab2014-11-17 14:42:17 +0100948 sdma_disable_channel(chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000949
Richard Zhao0bbc1412012-01-13 11:10:01 +0800950 sdmac->event_mask[0] = 0;
951 sdmac->event_mask[1] = 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000952 sdmac->shp_addr = 0;
953 sdmac->per_addr = 0;
954
955 if (sdmac->event_id0) {
Sascha Hauer17bba722013-08-20 10:04:31 +0200956 if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000957 return -EINVAL;
958 sdma_event_enable(sdmac, sdmac->event_id0);
959 }
960
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800961 if (sdmac->event_id1) {
962 if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
963 return -EINVAL;
964 sdma_event_enable(sdmac, sdmac->event_id1);
965 }
966
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000967 switch (sdmac->peripheral_type) {
968 case IMX_DMATYPE_DSP:
969 sdma_config_ownership(sdmac, false, true, true);
970 break;
971 case IMX_DMATYPE_MEMORY:
972 sdma_config_ownership(sdmac, false, true, false);
973 break;
974 default:
975 sdma_config_ownership(sdmac, true, true, false);
976 break;
977 }
978
979 sdma_get_pc(sdmac, sdmac->peripheral_type);
980
981 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
982 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
983 /* Handle multiple event channels differently */
984 if (sdmac->event_id1) {
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800985 if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
986 sdmac->peripheral_type == IMX_DMATYPE_ASRC)
987 sdma_set_watermarklevel_for_p2p(sdmac);
988 } else
Richard Zhao0bbc1412012-01-13 11:10:01 +0800989 __set_bit(sdmac->event_id0, sdmac->event_mask);
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800990
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000991 /* Address */
992 sdmac->shp_addr = sdmac->per_address;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +0800993 sdmac->per_addr = sdmac->per_address2;
Sascha Hauer1ec1e822010-09-30 13:56:34 +0000994 } else {
995 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
996 }
997
998 ret = sdma_load_context(sdmac);
999
1000 return ret;
1001}
1002
1003static int sdma_set_channel_priority(struct sdma_channel *sdmac,
1004 unsigned int priority)
1005{
1006 struct sdma_engine *sdma = sdmac->sdma;
1007 int channel = sdmac->channel;
1008
1009 if (priority < MXC_SDMA_MIN_PRIORITY
1010 || priority > MXC_SDMA_MAX_PRIORITY) {
1011 return -EINVAL;
1012 }
1013
Richard Zhaoc4b56852012-01-13 11:09:57 +08001014 writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001015
1016 return 0;
1017}
1018
1019static int sdma_request_channel(struct sdma_channel *sdmac)
1020{
1021 struct sdma_engine *sdma = sdmac->sdma;
1022 int channel = sdmac->channel;
1023 int ret = -EBUSY;
1024
Joe Perches9f92d222014-06-15 13:37:35 -07001025 sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys,
1026 GFP_KERNEL);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001027 if (!sdmac->bd) {
1028 ret = -ENOMEM;
1029 goto out;
1030 }
1031
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001032 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
1033 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1034
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001035 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001036 return 0;
1037out:
1038
1039 return ret;
1040}
1041
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001042static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
1043{
Haitao Zhangf69f2e22012-01-01 11:30:06 +08001044 unsigned long flags;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001045 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001046 dma_cookie_t cookie;
1047
Haitao Zhangf69f2e22012-01-01 11:30:06 +08001048 spin_lock_irqsave(&sdmac->lock, flags);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001049
Russell King - ARM Linux884485e2012-03-06 22:34:46 +00001050 cookie = dma_cookie_assign(tx);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001051
Haitao Zhangf69f2e22012-01-01 11:30:06 +08001052 spin_unlock_irqrestore(&sdmac->lock, flags);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001053
1054 return cookie;
1055}
1056
1057static int sdma_alloc_chan_resources(struct dma_chan *chan)
1058{
1059 struct sdma_channel *sdmac = to_sdma_chan(chan);
1060 struct imx_dma_data *data = chan->private;
1061 int prio, ret;
1062
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001063 if (!data)
1064 return -EINVAL;
1065
1066 switch (data->priority) {
1067 case DMA_PRIO_HIGH:
1068 prio = 3;
1069 break;
1070 case DMA_PRIO_MEDIUM:
1071 prio = 2;
1072 break;
1073 case DMA_PRIO_LOW:
1074 default:
1075 prio = 1;
1076 break;
1077 }
1078
1079 sdmac->peripheral_type = data->peripheral_type;
1080 sdmac->event_id0 = data->dma_request;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +08001081 sdmac->event_id1 = data->dma_request2;
Richard Zhaoc2c744d2012-01-13 11:09:59 +08001082
Fabio Estevamb93edcd2015-07-29 21:03:49 -03001083 ret = clk_enable(sdmac->sdma->clk_ipg);
1084 if (ret)
1085 return ret;
1086 ret = clk_enable(sdmac->sdma->clk_ahb);
1087 if (ret)
1088 goto disable_clk_ipg;
Richard Zhaoc2c744d2012-01-13 11:09:59 +08001089
Richard Zhao3bb5e7c2012-01-13 11:09:58 +08001090 ret = sdma_request_channel(sdmac);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001091 if (ret)
Fabio Estevamb93edcd2015-07-29 21:03:49 -03001092 goto disable_clk_ahb;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001093
Richard Zhao3bb5e7c2012-01-13 11:09:58 +08001094 ret = sdma_set_channel_priority(sdmac, prio);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001095 if (ret)
Fabio Estevamb93edcd2015-07-29 21:03:49 -03001096 goto disable_clk_ahb;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001097
1098 dma_async_tx_descriptor_init(&sdmac->desc, chan);
1099 sdmac->desc.tx_submit = sdma_tx_submit;
1100 /* txd.flags will be overwritten in prep funcs */
1101 sdmac->desc.flags = DMA_CTRL_ACK;
1102
1103 return 0;
Fabio Estevamb93edcd2015-07-29 21:03:49 -03001104
1105disable_clk_ahb:
1106 clk_disable(sdmac->sdma->clk_ahb);
1107disable_clk_ipg:
1108 clk_disable(sdmac->sdma->clk_ipg);
1109 return ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001110}
1111
1112static void sdma_free_chan_resources(struct dma_chan *chan)
1113{
1114 struct sdma_channel *sdmac = to_sdma_chan(chan);
1115 struct sdma_engine *sdma = sdmac->sdma;
1116
Maxime Ripard7b350ab2014-11-17 14:42:17 +01001117 sdma_disable_channel(chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001118
1119 if (sdmac->event_id0)
1120 sdma_event_disable(sdmac, sdmac->event_id0);
1121 if (sdmac->event_id1)
1122 sdma_event_disable(sdmac, sdmac->event_id1);
1123
1124 sdmac->event_id0 = 0;
1125 sdmac->event_id1 = 0;
1126
1127 sdma_set_channel_priority(sdmac, 0);
1128
1129 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
1130
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001131 clk_disable(sdma->clk_ipg);
1132 clk_disable(sdma->clk_ahb);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001133}
1134
1135static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
1136 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301137 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05001138 unsigned long flags, void *context)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001139{
1140 struct sdma_channel *sdmac = to_sdma_chan(chan);
1141 struct sdma_engine *sdma = sdmac->sdma;
1142 int ret, i, count;
Sascha Hauer23889c62011-01-31 10:56:58 +01001143 int channel = sdmac->channel;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001144 struct scatterlist *sg;
1145
1146 if (sdmac->status == DMA_IN_PROGRESS)
1147 return NULL;
1148 sdmac->status = DMA_IN_PROGRESS;
1149
1150 sdmac->flags = 0;
1151
Richard Zhao8e2e27c2012-06-04 09:17:24 +08001152 sdmac->buf_tail = 0;
1153
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001154 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
1155 sg_len, channel);
1156
1157 sdmac->direction = direction;
1158 ret = sdma_load_context(sdmac);
1159 if (ret)
1160 goto err_out;
1161
1162 if (sg_len > NUM_BD) {
1163 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1164 channel, sg_len, NUM_BD);
1165 ret = -EINVAL;
1166 goto err_out;
1167 }
1168
Huang Shijieab59a512011-12-02 10:16:25 +08001169 sdmac->chn_count = 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001170 for_each_sg(sgl, sg, sg_len, i) {
1171 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1172 int param;
1173
Anatolij Gustschind2f5c272010-11-22 18:35:18 +01001174 bd->buffer_addr = sg->dma_address;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001175
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +02001176 count = sg_dma_len(sg);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001177
1178 if (count > 0xffff) {
1179 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
1180 channel, count, 0xffff);
1181 ret = -EINVAL;
1182 goto err_out;
1183 }
1184
1185 bd->mode.count = count;
Huang Shijieab59a512011-12-02 10:16:25 +08001186 sdmac->chn_count += count;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001187
1188 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
1189 ret = -EINVAL;
1190 goto err_out;
1191 }
Sascha Hauer1fa81c22011-01-12 13:02:28 +01001192
1193 switch (sdmac->word_size) {
1194 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001195 bd->mode.command = 0;
Sascha Hauer1fa81c22011-01-12 13:02:28 +01001196 if (count & 3 || sg->dma_address & 3)
1197 return NULL;
1198 break;
1199 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1200 bd->mode.command = 2;
1201 if (count & 1 || sg->dma_address & 1)
1202 return NULL;
1203 break;
1204 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1205 bd->mode.command = 1;
1206 break;
1207 default:
1208 return NULL;
1209 }
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001210
1211 param = BD_DONE | BD_EXTD | BD_CONT;
1212
Shawn Guo341b9412011-01-20 05:50:39 +08001213 if (i + 1 == sg_len) {
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001214 param |= BD_INTR;
Shawn Guo341b9412011-01-20 05:50:39 +08001215 param |= BD_LAST;
1216 param &= ~BD_CONT;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001217 }
1218
Olof Johanssonc3cc74b2013-11-12 22:30:44 -08001219 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1220 i, count, (u64)sg->dma_address,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001221 param & BD_WRAP ? "wrap" : "",
1222 param & BD_INTR ? " intr" : "");
1223
1224 bd->mode.status = param;
1225 }
1226
1227 sdmac->num_bd = sg_len;
1228 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1229
1230 return &sdmac->desc;
1231err_out:
Shawn Guo4b2ce9d2011-01-20 05:50:36 +08001232 sdmac->status = DMA_ERROR;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001233 return NULL;
1234}
1235
1236static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1237 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -05001238 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001239 unsigned long flags)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001240{
1241 struct sdma_channel *sdmac = to_sdma_chan(chan);
1242 struct sdma_engine *sdma = sdmac->sdma;
1243 int num_periods = buf_len / period_len;
Sascha Hauer23889c62011-01-31 10:56:58 +01001244 int channel = sdmac->channel;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001245 int ret, i = 0, buf = 0;
1246
1247 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1248
1249 if (sdmac->status == DMA_IN_PROGRESS)
1250 return NULL;
1251
1252 sdmac->status = DMA_IN_PROGRESS;
1253
Richard Zhao8e2e27c2012-06-04 09:17:24 +08001254 sdmac->buf_tail = 0;
Russell King - ARM Linuxd1a792f2014-06-25 13:00:33 +01001255 sdmac->period_len = period_len;
Richard Zhao8e2e27c2012-06-04 09:17:24 +08001256
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001257 sdmac->flags |= IMX_DMA_SG_LOOP;
1258 sdmac->direction = direction;
1259 ret = sdma_load_context(sdmac);
1260 if (ret)
1261 goto err_out;
1262
1263 if (num_periods > NUM_BD) {
1264 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1265 channel, num_periods, NUM_BD);
1266 goto err_out;
1267 }
1268
1269 if (period_len > 0xffff) {
1270 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
1271 channel, period_len, 0xffff);
1272 goto err_out;
1273 }
1274
1275 while (buf < buf_len) {
1276 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1277 int param;
1278
1279 bd->buffer_addr = dma_addr;
1280
1281 bd->mode.count = period_len;
1282
1283 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1284 goto err_out;
1285 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1286 bd->mode.command = 0;
1287 else
1288 bd->mode.command = sdmac->word_size;
1289
1290 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1291 if (i + 1 == num_periods)
1292 param |= BD_WRAP;
1293
Olof Johanssonc3cc74b2013-11-12 22:30:44 -08001294 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1295 i, period_len, (u64)dma_addr,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001296 param & BD_WRAP ? "wrap" : "",
1297 param & BD_INTR ? " intr" : "");
1298
1299 bd->mode.status = param;
1300
1301 dma_addr += period_len;
1302 buf += period_len;
1303
1304 i++;
1305 }
1306
1307 sdmac->num_bd = num_periods;
1308 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1309
1310 return &sdmac->desc;
1311err_out:
1312 sdmac->status = DMA_ERROR;
1313 return NULL;
1314}
1315
Maxime Ripard7b350ab2014-11-17 14:42:17 +01001316static int sdma_config(struct dma_chan *chan,
1317 struct dma_slave_config *dmaengine_cfg)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001318{
1319 struct sdma_channel *sdmac = to_sdma_chan(chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001320
Maxime Ripard7b350ab2014-11-17 14:42:17 +01001321 if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
1322 sdmac->per_address = dmaengine_cfg->src_addr;
1323 sdmac->watermark_level = dmaengine_cfg->src_maxburst *
1324 dmaengine_cfg->src_addr_width;
1325 sdmac->word_size = dmaengine_cfg->src_addr_width;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +08001326 } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) {
1327 sdmac->per_address2 = dmaengine_cfg->src_addr;
1328 sdmac->per_address = dmaengine_cfg->dst_addr;
1329 sdmac->watermark_level = dmaengine_cfg->src_maxburst &
1330 SDMA_WATERMARK_LEVEL_LWML;
1331 sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
1332 SDMA_WATERMARK_LEVEL_HWML;
1333 sdmac->word_size = dmaengine_cfg->dst_addr_width;
Maxime Ripard7b350ab2014-11-17 14:42:17 +01001334 } else {
1335 sdmac->per_address = dmaengine_cfg->dst_addr;
1336 sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
1337 dmaengine_cfg->dst_addr_width;
1338 sdmac->word_size = dmaengine_cfg->dst_addr_width;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001339 }
Maxime Ripard7b350ab2014-11-17 14:42:17 +01001340 sdmac->direction = dmaengine_cfg->direction;
1341 return sdma_config_channel(chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001342}
1343
1344static enum dma_status sdma_tx_status(struct dma_chan *chan,
Andy Shevchenkoe8e3a792013-05-27 15:14:31 +03001345 dma_cookie_t cookie,
1346 struct dma_tx_state *txstate)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001347{
1348 struct sdma_channel *sdmac = to_sdma_chan(chan);
Russell King - ARM Linuxd1a792f2014-06-25 13:00:33 +01001349 u32 residue;
1350
1351 if (sdmac->flags & IMX_DMA_SG_LOOP)
1352 residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len;
1353 else
1354 residue = sdmac->chn_count - sdmac->chn_real_count;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001355
Andy Shevchenkoe8e3a792013-05-27 15:14:31 +03001356 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
Russell King - ARM Linuxd1a792f2014-06-25 13:00:33 +01001357 residue);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001358
Shawn Guo8a965912011-01-20 05:50:37 +08001359 return sdmac->status;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001360}
1361
1362static void sdma_issue_pending(struct dma_chan *chan)
1363{
Sascha Hauer2b4f1302012-01-09 10:32:50 +01001364 struct sdma_channel *sdmac = to_sdma_chan(chan);
1365 struct sdma_engine *sdma = sdmac->sdma;
1366
1367 if (sdmac->status == DMA_IN_PROGRESS)
1368 sdma_enable_channel(sdma, sdmac->channel);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001369}
1370
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001371#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
Nicolin Chencd72b842013-11-13 22:55:24 +08001372#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
Fabio Estevama5724602015-03-11 12:30:58 -03001373#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001374
1375static void sdma_add_scripts(struct sdma_engine *sdma,
1376 const struct sdma_script_start_addrs *addr)
1377{
1378 s32 *addr_arr = (u32 *)addr;
1379 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1380 int i;
1381
Nicolin Chen70dabaed2014-01-08 16:45:56 +08001382 /* use the default firmware in ROM if missing external firmware */
1383 if (!sdma->script_number)
1384 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1385
Nicolin Chencd72b842013-11-13 22:55:24 +08001386 for (i = 0; i < sdma->script_number; i++)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001387 if (addr_arr[i] > 0)
1388 saddr_arr[i] = addr_arr[i];
1389}
1390
Sascha Hauer7b4b88e2011-08-25 11:03:37 +02001391static void sdma_load_firmware(const struct firmware *fw, void *context)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001392{
Sascha Hauer7b4b88e2011-08-25 11:03:37 +02001393 struct sdma_engine *sdma = context;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001394 const struct sdma_firmware_header *header;
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001395 const struct sdma_script_start_addrs *addr;
1396 unsigned short *ram_code;
1397
Sascha Hauer7b4b88e2011-08-25 11:03:37 +02001398 if (!fw) {
Sascha Hauer0f927a12014-11-12 20:04:29 -02001399 dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
1400 /* In this case we just use the ROM firmware. */
Sascha Hauer7b4b88e2011-08-25 11:03:37 +02001401 return;
1402 }
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001403
1404 if (fw->size < sizeof(*header))
1405 goto err_firmware;
1406
1407 header = (struct sdma_firmware_header *)fw->data;
1408
1409 if (header->magic != SDMA_FIRMWARE_MAGIC)
1410 goto err_firmware;
1411 if (header->ram_code_start + header->ram_code_size > fw->size)
1412 goto err_firmware;
Nicolin Chencd72b842013-11-13 22:55:24 +08001413 switch (header->version_major) {
Asaf Vertz681d15e2014-12-10 10:00:36 +02001414 case 1:
1415 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1416 break;
1417 case 2:
1418 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1419 break;
Fabio Estevama5724602015-03-11 12:30:58 -03001420 case 3:
1421 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1422 break;
Asaf Vertz681d15e2014-12-10 10:00:36 +02001423 default:
1424 dev_err(sdma->dev, "unknown firmware version\n");
1425 goto err_firmware;
Nicolin Chencd72b842013-11-13 22:55:24 +08001426 }
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001427
1428 addr = (void *)header + header->script_addrs_start;
1429 ram_code = (void *)header + header->ram_code_start;
1430
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001431 clk_enable(sdma->clk_ipg);
1432 clk_enable(sdma->clk_ahb);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001433 /* download the RAM image for SDMA */
1434 sdma_load_script(sdma, ram_code,
1435 header->ram_code_size,
Sascha Hauer6866fd32011-01-12 11:18:14 +01001436 addr->ram_code_start_addr);
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001437 clk_disable(sdma->clk_ipg);
1438 clk_disable(sdma->clk_ahb);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001439
1440 sdma_add_scripts(sdma, addr);
1441
1442 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1443 header->version_major,
1444 header->version_minor);
1445
1446err_firmware:
1447 release_firmware(fw);
Sascha Hauer7b4b88e2011-08-25 11:03:37 +02001448}
1449
Zidan Wangd078cd12015-07-23 11:40:49 +08001450#define EVENT_REMAP_CELLS 3
1451
Jason Liu29f493d2015-11-11 17:20:49 +08001452static int sdma_event_remap(struct sdma_engine *sdma)
Zidan Wangd078cd12015-07-23 11:40:49 +08001453{
1454 struct device_node *np = sdma->dev->of_node;
1455 struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1456 struct property *event_remap;
1457 struct regmap *gpr;
1458 char propname[] = "fsl,sdma-event-remap";
1459 u32 reg, val, shift, num_map, i;
1460 int ret = 0;
1461
1462 if (IS_ERR(np) || IS_ERR(gpr_np))
1463 goto out;
1464
1465 event_remap = of_find_property(np, propname, NULL);
1466 num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1467 if (!num_map) {
Fabio Estevamce078af2015-10-03 19:37:58 -03001468 dev_dbg(sdma->dev, "no event needs to be remapped\n");
Zidan Wangd078cd12015-07-23 11:40:49 +08001469 goto out;
1470 } else if (num_map % EVENT_REMAP_CELLS) {
1471 dev_err(sdma->dev, "the property %s must modulo %d\n",
1472 propname, EVENT_REMAP_CELLS);
1473 ret = -EINVAL;
1474 goto out;
1475 }
1476
1477 gpr = syscon_node_to_regmap(gpr_np);
1478 if (IS_ERR(gpr)) {
1479 dev_err(sdma->dev, "failed to get gpr regmap\n");
1480 ret = PTR_ERR(gpr);
1481 goto out;
1482 }
1483
1484 for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1485 ret = of_property_read_u32_index(np, propname, i, &reg);
1486 if (ret) {
1487 dev_err(sdma->dev, "failed to read property %s index %d\n",
1488 propname, i);
1489 goto out;
1490 }
1491
1492 ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1493 if (ret) {
1494 dev_err(sdma->dev, "failed to read property %s index %d\n",
1495 propname, i + 1);
1496 goto out;
1497 }
1498
1499 ret = of_property_read_u32_index(np, propname, i + 2, &val);
1500 if (ret) {
1501 dev_err(sdma->dev, "failed to read property %s index %d\n",
1502 propname, i + 2);
1503 goto out;
1504 }
1505
1506 regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1507 }
1508
1509out:
1510 if (!IS_ERR(gpr_np))
1511 of_node_put(gpr_np);
1512
1513 return ret;
1514}
1515
Arnd Bergmannfe6cf282014-09-26 23:24:00 +02001516static int sdma_get_firmware(struct sdma_engine *sdma,
Sascha Hauer7b4b88e2011-08-25 11:03:37 +02001517 const char *fw_name)
1518{
1519 int ret;
1520
1521 ret = request_firmware_nowait(THIS_MODULE,
1522 FW_ACTION_HOTPLUG, fw_name, sdma->dev,
1523 GFP_KERNEL, sdma, sdma_load_firmware);
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001524
1525 return ret;
1526}
1527
Jingoo Han19bfc772014-11-06 10:10:09 +09001528static int sdma_init(struct sdma_engine *sdma)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001529{
1530 int i, ret;
1531 dma_addr_t ccb_phys;
1532
Fabio Estevamb93edcd2015-07-29 21:03:49 -03001533 ret = clk_enable(sdma->clk_ipg);
1534 if (ret)
1535 return ret;
1536 ret = clk_enable(sdma->clk_ahb);
1537 if (ret)
1538 goto disable_clk_ipg;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001539
1540 /* Be sure SDMA has not started yet */
Richard Zhaoc4b56852012-01-13 11:09:57 +08001541 writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001542
1543 sdma->channel_control = dma_alloc_coherent(NULL,
1544 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1545 sizeof(struct sdma_context_data),
1546 &ccb_phys, GFP_KERNEL);
1547
1548 if (!sdma->channel_control) {
1549 ret = -ENOMEM;
1550 goto err_dma_alloc;
1551 }
1552
1553 sdma->context = (void *)sdma->channel_control +
1554 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1555 sdma->context_phys = ccb_phys +
1556 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1557
1558 /* Zero-out the CCB structures array just allocated */
1559 memset(sdma->channel_control, 0,
1560 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1561
1562 /* disable all channels */
Sascha Hauer17bba722013-08-20 10:04:31 +02001563 for (i = 0; i < sdma->drvdata->num_events; i++)
Richard Zhaoc4b56852012-01-13 11:09:57 +08001564 writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001565
1566 /* All channels have priority 0 */
1567 for (i = 0; i < MAX_DMA_CHANNELS; i++)
Richard Zhaoc4b56852012-01-13 11:09:57 +08001568 writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001569
1570 ret = sdma_request_channel(&sdma->channel[0]);
1571 if (ret)
1572 goto err_dma_alloc;
1573
1574 sdma_config_ownership(&sdma->channel[0], false, true, false);
1575
1576 /* Set Command Channel (Channel Zero) */
Richard Zhaoc4b56852012-01-13 11:09:57 +08001577 writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001578
1579 /* Set bits of CONFIG register but with static context switching */
1580 /* FIXME: Check whether to set ACR bit depending on clock ratios */
Richard Zhaoc4b56852012-01-13 11:09:57 +08001581 writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001582
Richard Zhaoc4b56852012-01-13 11:09:57 +08001583 writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001584
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001585 /* Initializes channel's priorities */
1586 sdma_set_channel_priority(&sdma->channel[0], 7);
1587
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001588 clk_disable(sdma->clk_ipg);
1589 clk_disable(sdma->clk_ahb);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001590
1591 return 0;
1592
1593err_dma_alloc:
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001594 clk_disable(sdma->clk_ahb);
Fabio Estevamb93edcd2015-07-29 21:03:49 -03001595disable_clk_ipg:
1596 clk_disable(sdma->clk_ipg);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001597 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1598 return ret;
1599}
1600
Shawn Guo9479e172013-05-30 22:23:32 +08001601static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
1602{
Nicolin Chen0b351862014-06-16 11:32:29 +08001603 struct sdma_channel *sdmac = to_sdma_chan(chan);
Shawn Guo9479e172013-05-30 22:23:32 +08001604 struct imx_dma_data *data = fn_param;
1605
1606 if (!imx_dma_is_general_purpose(chan))
1607 return false;
1608
Nicolin Chen0b351862014-06-16 11:32:29 +08001609 sdmac->data = *data;
1610 chan->private = &sdmac->data;
Shawn Guo9479e172013-05-30 22:23:32 +08001611
1612 return true;
1613}
1614
1615static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
1616 struct of_dma *ofdma)
1617{
1618 struct sdma_engine *sdma = ofdma->of_dma_data;
1619 dma_cap_mask_t mask = sdma->dma_device.cap_mask;
1620 struct imx_dma_data data;
1621
1622 if (dma_spec->args_count != 3)
1623 return NULL;
1624
1625 data.dma_request = dma_spec->args[0];
1626 data.peripheral_type = dma_spec->args[1];
1627 data.priority = dma_spec->args[2];
Shengjiu Wang8391ecf2015-07-10 17:08:16 +08001628 /*
1629 * init dma_request2 to zero, which is not used by the dts.
1630 * For P2P, dma_request2 is init from dma_request_channel(),
1631 * chan->private will point to the imx_dma_data, and in
1632 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
1633 * be set to sdmac->event_id1.
1634 */
1635 data.dma_request2 = 0;
Shawn Guo9479e172013-05-30 22:23:32 +08001636
1637 return dma_request_channel(mask, sdma_filter_fn, &data);
1638}
1639
Mark Browne34b7312014-08-27 11:55:53 +01001640static int sdma_probe(struct platform_device *pdev)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001641{
Shawn Guo580975d2011-07-14 08:35:48 +08001642 const struct of_device_id *of_id =
1643 of_match_device(sdma_dt_ids, &pdev->dev);
1644 struct device_node *np = pdev->dev.of_node;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +08001645 struct device_node *spba_bus;
Shawn Guo580975d2011-07-14 08:35:48 +08001646 const char *fw_name;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001647 int ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001648 int irq;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001649 struct resource *iores;
Shengjiu Wang8391ecf2015-07-10 17:08:16 +08001650 struct resource spba_res;
Jingoo Hand4adcc02013-07-30 17:09:11 +09001651 struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001652 int i;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001653 struct sdma_engine *sdma;
Sascha Hauer36e2f212011-08-25 11:03:36 +02001654 s32 *saddr_arr;
Sascha Hauer17bba722013-08-20 10:04:31 +02001655 const struct sdma_driver_data *drvdata = NULL;
1656
1657 if (of_id)
1658 drvdata = of_id->data;
1659 else if (pdev->id_entry)
1660 drvdata = (void *)pdev->id_entry->driver_data;
1661
1662 if (!drvdata) {
1663 dev_err(&pdev->dev, "unable to find driver data\n");
1664 return -EINVAL;
1665 }
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001666
Philippe Retornaz42536b92013-10-14 09:45:17 +01001667 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1668 if (ret)
1669 return ret;
1670
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001671 sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001672 if (!sdma)
1673 return -ENOMEM;
1674
Richard Zhao2ccaef02012-05-11 15:14:27 +08001675 spin_lock_init(&sdma->channel_0_lock);
Sascha Hauer73eab972011-08-25 11:03:35 +02001676
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001677 sdma->dev = &pdev->dev;
Sascha Hauer17bba722013-08-20 10:04:31 +02001678 sdma->drvdata = drvdata;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001679
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001680 irq = platform_get_irq(pdev, 0);
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001681 if (irq < 0)
Fabio Estevam63c72e02014-12-29 15:20:53 -02001682 return irq;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001683
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001684 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1685 sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
1686 if (IS_ERR(sdma->regs))
1687 return PTR_ERR(sdma->regs);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001688
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001689 sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001690 if (IS_ERR(sdma->clk_ipg))
1691 return PTR_ERR(sdma->clk_ipg);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001692
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001693 sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001694 if (IS_ERR(sdma->clk_ahb))
1695 return PTR_ERR(sdma->clk_ahb);
Sascha Hauer7560e3f2012-03-07 09:30:06 +01001696
1697 clk_prepare(sdma->clk_ipg);
1698 clk_prepare(sdma->clk_ahb);
1699
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001700 ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
1701 sdma);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001702 if (ret)
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001703 return ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001704
Vinod Koul5bb9dbb2016-07-03 00:00:55 +05301705 sdma->irq = irq;
1706
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001707 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
Fabio Estevam7f24e0e2014-12-29 15:20:52 -02001708 if (!sdma->script_addrs)
1709 return -ENOMEM;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001710
Sascha Hauer36e2f212011-08-25 11:03:36 +02001711 /* initially no scripts available */
1712 saddr_arr = (s32 *)sdma->script_addrs;
1713 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1714 saddr_arr[i] = -EINVAL;
1715
Sascha Hauer7214a8b2011-01-31 10:21:35 +01001716 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1717 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1718
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001719 INIT_LIST_HEAD(&sdma->dma_device.channels);
1720 /* Initialize channel parameters */
1721 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1722 struct sdma_channel *sdmac = &sdma->channel[i];
1723
1724 sdmac->sdma = sdma;
1725 spin_lock_init(&sdmac->lock);
1726
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001727 sdmac->chan.device = &sdma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +00001728 dma_cookie_init(&sdmac->chan);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001729 sdmac->channel = i;
1730
Nandor Han15f30f52016-08-08 15:38:25 +03001731 tasklet_init(&sdmac->tasklet, mxc_sdma_handle_channel_normal,
Huang Shijieabd9ccc2012-04-28 18:15:42 +08001732 (unsigned long) sdmac);
Sascha Hauer23889c62011-01-31 10:56:58 +01001733 /*
1734 * Add the channel to the DMAC list. Do not add channel 0 though
1735 * because we need it internally in the SDMA driver. This also means
1736 * that channel 0 in dmaengine counting matches sdma channel 1.
1737 */
1738 if (i)
1739 list_add_tail(&sdmac->chan.device_node,
1740 &sdma->dma_device.channels);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001741 }
1742
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001743 ret = sdma_init(sdma);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001744 if (ret)
1745 goto err_init;
1746
Zidan Wangd078cd12015-07-23 11:40:49 +08001747 ret = sdma_event_remap(sdma);
1748 if (ret)
1749 goto err_init;
1750
Sascha Hauerdcfec3c2013-08-20 10:04:32 +02001751 if (sdma->drvdata->script_addrs)
1752 sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
Shawn Guo580975d2011-07-14 08:35:48 +08001753 if (pdata && pdata->script_addrs)
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001754 sdma_add_scripts(sdma, pdata->script_addrs);
1755
Shawn Guo580975d2011-07-14 08:35:48 +08001756 if (pdata) {
Fabio Estevam6d0d7e22012-02-29 11:20:38 -03001757 ret = sdma_get_firmware(sdma, pdata->fw_name);
1758 if (ret)
Fabio Estevamad1122e2012-03-08 09:26:39 -03001759 dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
Shawn Guo580975d2011-07-14 08:35:48 +08001760 } else {
1761 /*
1762 * Because that device tree does not encode ROM script address,
1763 * the RAM script in firmware is mandatory for device tree
1764 * probe, otherwise it fails.
1765 */
1766 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
1767 &fw_name);
Fabio Estevam6602b0d2012-02-29 11:20:37 -03001768 if (ret)
Fabio Estevamad1122e2012-03-08 09:26:39 -03001769 dev_warn(&pdev->dev, "failed to get firmware name\n");
Fabio Estevam6602b0d2012-02-29 11:20:37 -03001770 else {
1771 ret = sdma_get_firmware(sdma, fw_name);
1772 if (ret)
Fabio Estevamad1122e2012-03-08 09:26:39 -03001773 dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
Shawn Guo580975d2011-07-14 08:35:48 +08001774 }
1775 }
Sascha Hauer5b28aa32010-10-06 15:41:15 +02001776
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001777 sdma->dma_device.dev = &pdev->dev;
1778
1779 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1780 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1781 sdma->dma_device.device_tx_status = sdma_tx_status;
1782 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1783 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
Maxime Ripard7b350ab2014-11-17 14:42:17 +01001784 sdma->dma_device.device_config = sdma_config;
1785 sdma->dma_device.device_terminate_all = sdma_disable_channel;
Fabio Estevam1e4a4f52014-12-29 15:20:51 -02001786 sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1787 sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1788 sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1789 sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001790 sdma->dma_device.device_issue_pending = sdma_issue_pending;
Sascha Hauerb9b3f822011-01-12 12:12:31 +01001791 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1792 dma_set_max_seg_size(sdma->dma_device.dev, 65535);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001793
Vignesh Raman23e11812014-08-05 18:39:41 +05301794 platform_set_drvdata(pdev, sdma);
1795
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001796 ret = dma_async_device_register(&sdma->dma_device);
1797 if (ret) {
1798 dev_err(&pdev->dev, "unable to register\n");
1799 goto err_init;
1800 }
1801
Shawn Guo9479e172013-05-30 22:23:32 +08001802 if (np) {
1803 ret = of_dma_controller_register(np, sdma_xlate, sdma);
1804 if (ret) {
1805 dev_err(&pdev->dev, "failed to register controller\n");
1806 goto err_register;
1807 }
Shengjiu Wang8391ecf2015-07-10 17:08:16 +08001808
1809 spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
1810 ret = of_address_to_resource(spba_bus, 0, &spba_res);
1811 if (!ret) {
1812 sdma->spba_start_addr = spba_res.start;
1813 sdma->spba_end_addr = spba_res.end;
1814 }
1815 of_node_put(spba_bus);
Shawn Guo9479e172013-05-30 22:23:32 +08001816 }
1817
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001818 return 0;
1819
Shawn Guo9479e172013-05-30 22:23:32 +08001820err_register:
1821 dma_async_device_unregister(&sdma->dma_device);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001822err_init:
1823 kfree(sdma->script_addrs);
Shawn Guo939fd4f2011-01-19 19:13:06 +08001824 return ret;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001825}
1826
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001827static int sdma_remove(struct platform_device *pdev)
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001828{
Vignesh Raman23e11812014-08-05 18:39:41 +05301829 struct sdma_engine *sdma = platform_get_drvdata(pdev);
Vignesh Ramanc12fe492014-08-05 18:39:42 +05301830 int i;
Vignesh Raman23e11812014-08-05 18:39:41 +05301831
Vinod Koul5bb9dbb2016-07-03 00:00:55 +05301832 devm_free_irq(&pdev->dev, sdma->irq, sdma);
Vignesh Raman23e11812014-08-05 18:39:41 +05301833 dma_async_device_unregister(&sdma->dma_device);
1834 kfree(sdma->script_addrs);
Vignesh Ramanc12fe492014-08-05 18:39:42 +05301835 /* Kill the tasklet */
1836 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1837 struct sdma_channel *sdmac = &sdma->channel[i];
1838
1839 tasklet_kill(&sdmac->tasklet);
1840 }
Vignesh Raman23e11812014-08-05 18:39:41 +05301841
1842 platform_set_drvdata(pdev, NULL);
Vignesh Raman23e11812014-08-05 18:39:41 +05301843 return 0;
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001844}
1845
1846static struct platform_driver sdma_driver = {
1847 .driver = {
1848 .name = "imx-sdma",
Shawn Guo580975d2011-07-14 08:35:48 +08001849 .of_match_table = sdma_dt_ids,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001850 },
Shawn Guo62550cd2011-07-13 21:33:17 +08001851 .id_table = sdma_devtypes,
Maxin B. John1d1bbd32013-02-20 02:07:04 +02001852 .remove = sdma_remove,
Vignesh Raman23e11812014-08-05 18:39:41 +05301853 .probe = sdma_probe,
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001854};
1855
Vignesh Raman23e11812014-08-05 18:39:41 +05301856module_platform_driver(sdma_driver);
Sascha Hauer1ec1e822010-09-30 13:56:34 +00001857
1858MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1859MODULE_DESCRIPTION("i.MX SDMA driver");
1860MODULE_LICENSE("GPL");