dmaengine: imx-sdma: remove dummy assignment
[deliverable/linux.git] / drivers / dma / imx-sdma.c
CommitLineData
1ec1e82f
SH
1/*
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>
1d069bfa 21#include <linux/iopoll.h>
f8de8f4c 22#include <linux/module.h>
1ec1e82f 23#include <linux/types.h>
0bbc1413 24#include <linux/bitops.h>
1ec1e82f
SH
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <linux/clk.h>
2ccaef05 28#include <linux/delay.h>
1ec1e82f
SH
29#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>
580975d7 38#include <linux/of.h>
8391ecf4 39#include <linux/of_address.h>
580975d7 40#include <linux/of_device.h>
9479e17c 41#include <linux/of_dma.h>
1ec1e82f
SH
42
43#include <asm/irq.h>
82906b13
AB
44#include <linux/platform_data/dma-imx-sdma.h>
45#include <linux/platform_data/dma-imx.h>
d078cd1b
ZW
46#include <linux/regmap.h>
47#include <linux/mfd/syscon.h>
48#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
1ec1e82f 49
d2ebfb33
RKAL
50#include "dmaengine.h"
51
1ec1e82f
SH
52/* 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
62550cd7
SG
79#define SDMA_CHNENBL0_IMX35 0x200
80#define SDMA_CHNENBL0_IMX31 0x080
1ec1e82f
SH
81#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
8391ecf4
SW
131/*
132 * 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
1ec1e82f
SH
181/*
182 * 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
23889c63 294 * @channel the channel number, matches dmaengine chan_id + 1
1ec1e82f
SH
295 * @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
1ec1e82f
SH
301 * @num_bd max NUM_BD. number of descriptors currently handling
302 */
303struct sdma_channel {
304 struct sdma_engine *sdma;
305 unsigned int channel;
db8196df 306 enum dma_transfer_direction direction;
1ec1e82f
SH
307 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;
1ec1e82f 312 unsigned int num_bd;
d1a792f3 313 unsigned int period_len;
1ec1e82f
SH
314 struct sdma_buffer_descriptor *bd;
315 dma_addr_t bd_phys;
316 unsigned int pc_from_device, pc_to_device;
8391ecf4 317 unsigned int device_to_device;
1ec1e82f 318 unsigned long flags;
8391ecf4 319 dma_addr_t per_address, per_address2;
0bbc1413
RZ
320 unsigned long event_mask[2];
321 unsigned long watermark_level;
1ec1e82f
SH
322 u32 shp_addr, per_addr;
323 struct dma_chan chan;
324 spinlock_t lock;
325 struct dma_async_tx_descriptor desc;
1ec1e82f 326 enum dma_status status;
ab59a510
HS
327 unsigned int chn_count;
328 unsigned int chn_real_count;
abd9ccc8 329 struct tasklet_struct tasklet;
0b351865 330 struct imx_dma_data data;
1ec1e82f
SH
331};
332
0bbc1413 333#define IMX_DMA_SG_LOOP BIT(0)
1ec1e82f
SH
334
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
1ec1e82f
SH
340#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
17bba72f
SH
366struct sdma_driver_data {
367 int chnenbl0;
368 int num_events;
dcfec3c0 369 struct sdma_script_start_addrs *script_addrs;
62550cd7
SG
370};
371
1ec1e82f
SH
372struct sdma_engine {
373 struct device *dev;
b9b3f82f 374 struct device_dma_parameters dma_parms;
1ec1e82f
SH
375 struct sdma_channel channel[MAX_DMA_CHANNELS];
376 struct sdma_channel_control *channel_control;
377 void __iomem *regs;
1ec1e82f
SH
378 struct sdma_context_data *context;
379 dma_addr_t context_phys;
380 struct dma_device dma_device;
7560e3f3
SH
381 struct clk *clk_ipg;
382 struct clk *clk_ahb;
2ccaef05 383 spinlock_t channel_0_lock;
cd72b846 384 u32 script_number;
1ec1e82f 385 struct sdma_script_start_addrs *script_addrs;
17bba72f 386 const struct sdma_driver_data *drvdata;
8391ecf4
SW
387 u32 spba_start_addr;
388 u32 spba_end_addr;
5bb9dbb5 389 unsigned int irq;
17bba72f
SH
390};
391
e9fd58de 392static struct sdma_driver_data sdma_imx31 = {
17bba72f
SH
393 .chnenbl0 = SDMA_CHNENBL0_IMX31,
394 .num_events = 32,
395};
396
dcfec3c0
SH
397static 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
e9fd58de 413static struct sdma_driver_data sdma_imx25 = {
dcfec3c0
SH
414 .chnenbl0 = SDMA_CHNENBL0_IMX35,
415 .num_events = 48,
416 .script_addrs = &sdma_script_imx25,
417};
418
e9fd58de 419static struct sdma_driver_data sdma_imx35 = {
17bba72f
SH
420 .chnenbl0 = SDMA_CHNENBL0_IMX35,
421 .num_events = 48,
1ec1e82f
SH
422};
423
dcfec3c0
SH
424static 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
e9fd58de 437static struct sdma_driver_data sdma_imx51 = {
dcfec3c0
SH
438 .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
e9fd58de 457static struct sdma_driver_data sdma_imx53 = {
dcfec3c0
SH
458 .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
e9fd58de 476static struct sdma_driver_data sdma_imx6q = {
dcfec3c0
SH
477 .chnenbl0 = SDMA_CHNENBL0_IMX35,
478 .num_events = 48,
479 .script_addrs = &sdma_script_imx6q,
480};
481
afe7cded 482static const struct platform_device_id sdma_devtypes[] = {
62550cd7 483 {
dcfec3c0
SH
484 .name = "imx25-sdma",
485 .driver_data = (unsigned long)&sdma_imx25,
486 }, {
62550cd7 487 .name = "imx31-sdma",
17bba72f 488 .driver_data = (unsigned long)&sdma_imx31,
62550cd7
SG
489 }, {
490 .name = "imx35-sdma",
17bba72f 491 .driver_data = (unsigned long)&sdma_imx35,
dcfec3c0
SH
492 }, {
493 .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,
62550cd7
SG
501 }, {
502 /* sentinel */
503 }
504};
505MODULE_DEVICE_TABLE(platform, sdma_devtypes);
506
580975d7 507static const struct of_device_id sdma_dt_ids[] = {
dcfec3c0
SH
508 { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
509 { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
510 { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
17bba72f 511 { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
dcfec3c0 512 { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
63edea16 513 { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
580975d7
SG
514 { /* sentinel */ }
515};
516MODULE_DEVICE_TABLE(of, sdma_dt_ids);
517
0bbc1413
RZ
518#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 */
1ec1e82f
SH
521#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{
17bba72f 525 u32 chnenbl0 = sdma->drvdata->chnenbl0;
1ec1e82f
SH
526 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;
0bbc1413 534 unsigned long evt, mcu, dsp;
1ec1e82f
SH
535
536 if (event_override && mcu_override && dsp_override)
537 return -EINVAL;
538
c4b56857
RZ
539 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);
1ec1e82f
SH
542
543 if (dsp_override)
0bbc1413 544 __clear_bit(channel, &dsp);
1ec1e82f 545 else
0bbc1413 546 __set_bit(channel, &dsp);
1ec1e82f
SH
547
548 if (event_override)
0bbc1413 549 __clear_bit(channel, &evt);
1ec1e82f 550 else
0bbc1413 551 __set_bit(channel, &evt);
1ec1e82f
SH
552
553 if (mcu_override)
0bbc1413 554 __clear_bit(channel, &mcu);
1ec1e82f 555 else
0bbc1413 556 __set_bit(channel, &mcu);
1ec1e82f 557
c4b56857
RZ
558 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);
1ec1e82f
SH
561
562 return 0;
563}
564
b9a59166
RZ
565static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
566{
0bbc1413 567 writel(BIT(channel), sdma->regs + SDMA_H_START);
b9a59166
RZ
568}
569
1ec1e82f 570/*
2ccaef05 571 * sdma_run_channel0 - run a channel and wait till it's done
1ec1e82f 572 */
2ccaef05 573static int sdma_run_channel0(struct sdma_engine *sdma)
1ec1e82f 574{
1ec1e82f 575 int ret;
1d069bfa 576 u32 reg;
1ec1e82f 577
2ccaef05 578 sdma_enable_channel(sdma, 0);
1ec1e82f 579
1d069bfa
MO
580 ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
581 reg, !(reg & 1), 1, 500);
582 if (ret)
2ccaef05 583 dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
1ec1e82f 584
855832e4
RG
585 /* 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
1d069bfa 589 return ret;
1ec1e82f
SH
590}
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;
2ccaef05 599 unsigned long flags;
73eab978 600
1ec1e82f
SH
601 buf_virt = dma_alloc_coherent(NULL,
602 size,
603 &buf_phys, GFP_KERNEL);
73eab978 604 if (!buf_virt) {
2ccaef05 605 return -ENOMEM;
73eab978 606 }
1ec1e82f 607
2ccaef05
RZ
608 spin_lock_irqsave(&sdma->channel_0_lock, flags);
609
1ec1e82f
SH
610 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
2ccaef05 618 ret = sdma_run_channel0(sdma);
1ec1e82f 619
2ccaef05 620 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
1ec1e82f 621
2ccaef05 622 dma_free_coherent(NULL, size, buf_virt, buf_phys);
73eab978 623
1ec1e82f
SH
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;
0bbc1413 631 unsigned long val;
1ec1e82f
SH
632 u32 chnenbl = chnenbl_ofs(sdma, event);
633
c4b56857 634 val = readl_relaxed(sdma->regs + chnenbl);
0bbc1413 635 __set_bit(channel, &val);
c4b56857 636 writel_relaxed(val, sdma->regs + chnenbl);
1ec1e82f
SH
637}
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);
0bbc1413 644 unsigned long val;
1ec1e82f 645
c4b56857 646 val = readl_relaxed(sdma->regs + chnenbl);
0bbc1413 647 __clear_bit(channel, &val);
c4b56857 648 writel_relaxed(val, sdma->regs + chnenbl);
1ec1e82f
SH
649}
650
651static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
d1a792f3
RKAL
652{
653 if (sdmac->desc.callback)
654 sdmac->desc.callback(sdmac->desc.callback_param);
655}
656
657static void sdma_update_channel_loop(struct sdma_channel *sdmac)
1ec1e82f
SH
658{
659 struct sdma_buffer_descriptor *bd;
660
661 /*
662 * loop mode. Iterate over descriptors, re-setup them and
663 * call callback function.
664 */
665 while (1) {
666 bd = &sdmac->bd[sdmac->buf_tail];
667
668 if (bd->mode.status & BD_DONE)
669 break;
670
671 if (bd->mode.status & BD_RROR)
672 sdmac->status = DMA_ERROR;
1ec1e82f
SH
673
674 bd->mode.status |= BD_DONE;
675 sdmac->buf_tail++;
676 sdmac->buf_tail %= sdmac->num_bd;
1ec1e82f
SH
677 }
678}
679
680static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
681{
682 struct sdma_buffer_descriptor *bd;
683 int i, error = 0;
684
ab59a510 685 sdmac->chn_real_count = 0;
1ec1e82f
SH
686 /*
687 * non loop mode. Iterate over all descriptors, collect
688 * errors and call callback function
689 */
690 for (i = 0; i < sdmac->num_bd; i++) {
691 bd = &sdmac->bd[i];
692
693 if (bd->mode.status & (BD_DONE | BD_RROR))
694 error = -EIO;
ab59a510 695 sdmac->chn_real_count += bd->mode.count;
1ec1e82f
SH
696 }
697
698 if (error)
699 sdmac->status = DMA_ERROR;
700 else
409bff6a 701 sdmac->status = DMA_COMPLETE;
1ec1e82f 702
f7fbce07 703 dma_cookie_complete(&sdmac->desc);
1ec1e82f
SH
704 if (sdmac->desc.callback)
705 sdmac->desc.callback(sdmac->desc.callback_param);
1ec1e82f
SH
706}
707
abd9ccc8 708static void sdma_tasklet(unsigned long data)
1ec1e82f 709{
abd9ccc8
HS
710 struct sdma_channel *sdmac = (struct sdma_channel *) data;
711
1ec1e82f
SH
712 if (sdmac->flags & IMX_DMA_SG_LOOP)
713 sdma_handle_channel_loop(sdmac);
714 else
715 mxc_sdma_handle_channel_normal(sdmac);
716}
717
718static irqreturn_t sdma_int_handler(int irq, void *dev_id)
719{
720 struct sdma_engine *sdma = dev_id;
0bbc1413 721 unsigned long stat;
1ec1e82f 722
c4b56857
RZ
723 stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
724 writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
1d069bfa
MO
725 /* channel 0 is special and not handled here, see run_channel0() */
726 stat &= ~1;
1ec1e82f
SH
727
728 while (stat) {
729 int channel = fls(stat) - 1;
730 struct sdma_channel *sdmac = &sdma->channel[channel];
731
d1a792f3
RKAL
732 if (sdmac->flags & IMX_DMA_SG_LOOP)
733 sdma_update_channel_loop(sdmac);
734
abd9ccc8 735 tasklet_schedule(&sdmac->tasklet);
1ec1e82f 736
0bbc1413 737 __clear_bit(channel, &stat);
1ec1e82f
SH
738 }
739
740 return IRQ_HANDLED;
741}
742
743/*
744 * sets the pc of SDMA script according to the peripheral type
745 */
746static void sdma_get_pc(struct sdma_channel *sdmac,
747 enum sdma_peripheral_type peripheral_type)
748{
749 struct sdma_engine *sdma = sdmac->sdma;
750 int per_2_emi = 0, emi_2_per = 0;
751 /*
752 * These are needed once we start to support transfers between
753 * two peripherals or memory-to-memory transfers
754 */
755 int per_2_per = 0, emi_2_emi = 0;
756
757 sdmac->pc_from_device = 0;
758 sdmac->pc_to_device = 0;
8391ecf4 759 sdmac->device_to_device = 0;
1ec1e82f
SH
760
761 switch (peripheral_type) {
762 case IMX_DMATYPE_MEMORY:
763 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
764 break;
765 case IMX_DMATYPE_DSP:
766 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
767 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
768 break;
769 case IMX_DMATYPE_FIRI:
770 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
771 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
772 break;
773 case IMX_DMATYPE_UART:
774 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
775 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
776 break;
777 case IMX_DMATYPE_UART_SP:
778 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
779 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
780 break;
781 case IMX_DMATYPE_ATA:
782 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
783 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
784 break;
785 case IMX_DMATYPE_CSPI:
786 case IMX_DMATYPE_EXT:
787 case IMX_DMATYPE_SSI:
29aebfde 788 case IMX_DMATYPE_SAI:
1ec1e82f
SH
789 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
790 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
791 break;
1a895578
NC
792 case IMX_DMATYPE_SSI_DUAL:
793 per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
794 emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
795 break;
1ec1e82f
SH
796 case IMX_DMATYPE_SSI_SP:
797 case IMX_DMATYPE_MMC:
798 case IMX_DMATYPE_SDHC:
799 case IMX_DMATYPE_CSPI_SP:
800 case IMX_DMATYPE_ESAI:
801 case IMX_DMATYPE_MSHC_SP:
802 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
803 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
804 break;
805 case IMX_DMATYPE_ASRC:
806 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
807 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
808 per_2_per = sdma->script_addrs->per_2_per_addr;
809 break;
f892afb0
NC
810 case IMX_DMATYPE_ASRC_SP:
811 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
812 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
813 per_2_per = sdma->script_addrs->per_2_per_addr;
814 break;
1ec1e82f
SH
815 case IMX_DMATYPE_MSHC:
816 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
817 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
818 break;
819 case IMX_DMATYPE_CCM:
820 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
821 break;
822 case IMX_DMATYPE_SPDIF:
823 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
824 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
825 break;
826 case IMX_DMATYPE_IPU_MEMORY:
827 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
828 break;
829 default:
830 break;
831 }
832
833 sdmac->pc_from_device = per_2_emi;
834 sdmac->pc_to_device = emi_2_per;
8391ecf4 835 sdmac->device_to_device = per_2_per;
1ec1e82f
SH
836}
837
838static int sdma_load_context(struct sdma_channel *sdmac)
839{
840 struct sdma_engine *sdma = sdmac->sdma;
841 int channel = sdmac->channel;
842 int load_address;
843 struct sdma_context_data *context = sdma->context;
844 struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
845 int ret;
2ccaef05 846 unsigned long flags;
1ec1e82f 847
8391ecf4 848 if (sdmac->direction == DMA_DEV_TO_MEM)
1ec1e82f 849 load_address = sdmac->pc_from_device;
8391ecf4
SW
850 else if (sdmac->direction == DMA_DEV_TO_DEV)
851 load_address = sdmac->device_to_device;
852 else
1ec1e82f 853 load_address = sdmac->pc_to_device;
1ec1e82f
SH
854
855 if (load_address < 0)
856 return load_address;
857
858 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
0bbc1413 859 dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
1ec1e82f
SH
860 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
861 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
0bbc1413
RZ
862 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
863 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
1ec1e82f 864
2ccaef05 865 spin_lock_irqsave(&sdma->channel_0_lock, flags);
73eab978 866
1ec1e82f
SH
867 memset(context, 0, sizeof(*context));
868 context->channel_state.pc = load_address;
869
870 /* Send by context the event mask,base address for peripheral
871 * and watermark level
872 */
0bbc1413
RZ
873 context->gReg[0] = sdmac->event_mask[1];
874 context->gReg[1] = sdmac->event_mask[0];
1ec1e82f
SH
875 context->gReg[2] = sdmac->per_addr;
876 context->gReg[6] = sdmac->shp_addr;
877 context->gReg[7] = sdmac->watermark_level;
878
879 bd0->mode.command = C0_SETDM;
880 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
881 bd0->mode.count = sizeof(*context) / 4;
882 bd0->buffer_addr = sdma->context_phys;
883 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
2ccaef05 884 ret = sdma_run_channel0(sdma);
1ec1e82f 885
2ccaef05 886 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
73eab978 887
1ec1e82f
SH
888 return ret;
889}
890
7b350ab0
MR
891static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
892{
893 return container_of(chan, struct sdma_channel, chan);
894}
895
896static int sdma_disable_channel(struct dma_chan *chan)
1ec1e82f 897{
7b350ab0 898 struct sdma_channel *sdmac = to_sdma_chan(chan);
1ec1e82f
SH
899 struct sdma_engine *sdma = sdmac->sdma;
900 int channel = sdmac->channel;
901
0bbc1413 902 writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
1ec1e82f 903 sdmac->status = DMA_ERROR;
7b350ab0
MR
904
905 return 0;
1ec1e82f
SH
906}
907
8391ecf4
SW
908static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
909{
910 struct sdma_engine *sdma = sdmac->sdma;
911
912 int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
913 int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
914
915 set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
916 set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
917
918 if (sdmac->event_id0 > 31)
919 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
920
921 if (sdmac->event_id1 > 31)
922 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
923
924 /*
925 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
926 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
927 * r0(event_mask[1]) and r1(event_mask[0]).
928 */
929 if (lwml > hwml) {
930 sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
931 SDMA_WATERMARK_LEVEL_HWML);
932 sdmac->watermark_level |= hwml;
933 sdmac->watermark_level |= lwml << 16;
934 swap(sdmac->event_mask[0], sdmac->event_mask[1]);
935 }
936
937 if (sdmac->per_address2 >= sdma->spba_start_addr &&
938 sdmac->per_address2 <= sdma->spba_end_addr)
939 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
940
941 if (sdmac->per_address >= sdma->spba_start_addr &&
942 sdmac->per_address <= sdma->spba_end_addr)
943 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
944
945 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
946}
947
7b350ab0 948static int sdma_config_channel(struct dma_chan *chan)
1ec1e82f 949{
7b350ab0 950 struct sdma_channel *sdmac = to_sdma_chan(chan);
1ec1e82f
SH
951 int ret;
952
7b350ab0 953 sdma_disable_channel(chan);
1ec1e82f 954
0bbc1413
RZ
955 sdmac->event_mask[0] = 0;
956 sdmac->event_mask[1] = 0;
1ec1e82f
SH
957 sdmac->shp_addr = 0;
958 sdmac->per_addr = 0;
959
960 if (sdmac->event_id0) {
17bba72f 961 if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1ec1e82f
SH
962 return -EINVAL;
963 sdma_event_enable(sdmac, sdmac->event_id0);
964 }
965
8391ecf4
SW
966 if (sdmac->event_id1) {
967 if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
968 return -EINVAL;
969 sdma_event_enable(sdmac, sdmac->event_id1);
970 }
971
1ec1e82f
SH
972 switch (sdmac->peripheral_type) {
973 case IMX_DMATYPE_DSP:
974 sdma_config_ownership(sdmac, false, true, true);
975 break;
976 case IMX_DMATYPE_MEMORY:
977 sdma_config_ownership(sdmac, false, true, false);
978 break;
979 default:
980 sdma_config_ownership(sdmac, true, true, false);
981 break;
982 }
983
984 sdma_get_pc(sdmac, sdmac->peripheral_type);
985
986 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
987 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
988 /* Handle multiple event channels differently */
989 if (sdmac->event_id1) {
8391ecf4
SW
990 if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
991 sdmac->peripheral_type == IMX_DMATYPE_ASRC)
992 sdma_set_watermarklevel_for_p2p(sdmac);
993 } else
0bbc1413 994 __set_bit(sdmac->event_id0, sdmac->event_mask);
8391ecf4 995
1ec1e82f
SH
996 /* Address */
997 sdmac->shp_addr = sdmac->per_address;
8391ecf4 998 sdmac->per_addr = sdmac->per_address2;
1ec1e82f
SH
999 } else {
1000 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
1001 }
1002
1003 ret = sdma_load_context(sdmac);
1004
1005 return ret;
1006}
1007
1008static int sdma_set_channel_priority(struct sdma_channel *sdmac,
1009 unsigned int priority)
1010{
1011 struct sdma_engine *sdma = sdmac->sdma;
1012 int channel = sdmac->channel;
1013
1014 if (priority < MXC_SDMA_MIN_PRIORITY
1015 || priority > MXC_SDMA_MAX_PRIORITY) {
1016 return -EINVAL;
1017 }
1018
c4b56857 1019 writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
1ec1e82f
SH
1020
1021 return 0;
1022}
1023
1024static int sdma_request_channel(struct sdma_channel *sdmac)
1025{
1026 struct sdma_engine *sdma = sdmac->sdma;
1027 int channel = sdmac->channel;
1028 int ret = -EBUSY;
1029
9f92d223
JP
1030 sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys,
1031 GFP_KERNEL);
1ec1e82f
SH
1032 if (!sdmac->bd) {
1033 ret = -ENOMEM;
1034 goto out;
1035 }
1036
1ec1e82f
SH
1037 sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
1038 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1039
1ec1e82f 1040 sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
1ec1e82f
SH
1041 return 0;
1042out:
1043
1044 return ret;
1045}
1046
1ec1e82f
SH
1047static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
1048{
f69f2e26 1049 unsigned long flags;
1ec1e82f 1050 struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
1ec1e82f
SH
1051 dma_cookie_t cookie;
1052
f69f2e26 1053 spin_lock_irqsave(&sdmac->lock, flags);
1ec1e82f 1054
884485e1 1055 cookie = dma_cookie_assign(tx);
1ec1e82f 1056
f69f2e26 1057 spin_unlock_irqrestore(&sdmac->lock, flags);
1ec1e82f
SH
1058
1059 return cookie;
1060}
1061
1062static int sdma_alloc_chan_resources(struct dma_chan *chan)
1063{
1064 struct sdma_channel *sdmac = to_sdma_chan(chan);
1065 struct imx_dma_data *data = chan->private;
1066 int prio, ret;
1067
1ec1e82f
SH
1068 if (!data)
1069 return -EINVAL;
1070
1071 switch (data->priority) {
1072 case DMA_PRIO_HIGH:
1073 prio = 3;
1074 break;
1075 case DMA_PRIO_MEDIUM:
1076 prio = 2;
1077 break;
1078 case DMA_PRIO_LOW:
1079 default:
1080 prio = 1;
1081 break;
1082 }
1083
1084 sdmac->peripheral_type = data->peripheral_type;
1085 sdmac->event_id0 = data->dma_request;
8391ecf4 1086 sdmac->event_id1 = data->dma_request2;
c2c744d3 1087
b93edcdd
FE
1088 ret = clk_enable(sdmac->sdma->clk_ipg);
1089 if (ret)
1090 return ret;
1091 ret = clk_enable(sdmac->sdma->clk_ahb);
1092 if (ret)
1093 goto disable_clk_ipg;
c2c744d3 1094
3bb5e7ca 1095 ret = sdma_request_channel(sdmac);
1ec1e82f 1096 if (ret)
b93edcdd 1097 goto disable_clk_ahb;
1ec1e82f 1098
3bb5e7ca 1099 ret = sdma_set_channel_priority(sdmac, prio);
1ec1e82f 1100 if (ret)
b93edcdd 1101 goto disable_clk_ahb;
1ec1e82f
SH
1102
1103 dma_async_tx_descriptor_init(&sdmac->desc, chan);
1104 sdmac->desc.tx_submit = sdma_tx_submit;
1105 /* txd.flags will be overwritten in prep funcs */
1106 sdmac->desc.flags = DMA_CTRL_ACK;
1107
1108 return 0;
b93edcdd
FE
1109
1110disable_clk_ahb:
1111 clk_disable(sdmac->sdma->clk_ahb);
1112disable_clk_ipg:
1113 clk_disable(sdmac->sdma->clk_ipg);
1114 return ret;
1ec1e82f
SH
1115}
1116
1117static void sdma_free_chan_resources(struct dma_chan *chan)
1118{
1119 struct sdma_channel *sdmac = to_sdma_chan(chan);
1120 struct sdma_engine *sdma = sdmac->sdma;
1121
7b350ab0 1122 sdma_disable_channel(chan);
1ec1e82f
SH
1123
1124 if (sdmac->event_id0)
1125 sdma_event_disable(sdmac, sdmac->event_id0);
1126 if (sdmac->event_id1)
1127 sdma_event_disable(sdmac, sdmac->event_id1);
1128
1129 sdmac->event_id0 = 0;
1130 sdmac->event_id1 = 0;
1131
1132 sdma_set_channel_priority(sdmac, 0);
1133
1134 dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
1135
7560e3f3
SH
1136 clk_disable(sdma->clk_ipg);
1137 clk_disable(sdma->clk_ahb);
1ec1e82f
SH
1138}
1139
1140static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
1141 struct dma_chan *chan, struct scatterlist *sgl,
db8196df 1142 unsigned int sg_len, enum dma_transfer_direction direction,
185ecb5f 1143 unsigned long flags, void *context)
1ec1e82f
SH
1144{
1145 struct sdma_channel *sdmac = to_sdma_chan(chan);
1146 struct sdma_engine *sdma = sdmac->sdma;
1147 int ret, i, count;
23889c63 1148 int channel = sdmac->channel;
1ec1e82f
SH
1149 struct scatterlist *sg;
1150
1151 if (sdmac->status == DMA_IN_PROGRESS)
1152 return NULL;
1153 sdmac->status = DMA_IN_PROGRESS;
1154
1155 sdmac->flags = 0;
1156
8e2e27c7
RZ
1157 sdmac->buf_tail = 0;
1158
1ec1e82f
SH
1159 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
1160 sg_len, channel);
1161
1162 sdmac->direction = direction;
1163 ret = sdma_load_context(sdmac);
1164 if (ret)
1165 goto err_out;
1166
1167 if (sg_len > NUM_BD) {
1168 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1169 channel, sg_len, NUM_BD);
1170 ret = -EINVAL;
1171 goto err_out;
1172 }
1173
ab59a510 1174 sdmac->chn_count = 0;
1ec1e82f
SH
1175 for_each_sg(sgl, sg, sg_len, i) {
1176 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1177 int param;
1178
d2f5c276 1179 bd->buffer_addr = sg->dma_address;
1ec1e82f 1180
fdaf9c4b 1181 count = sg_dma_len(sg);
1ec1e82f
SH
1182
1183 if (count > 0xffff) {
1184 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
1185 channel, count, 0xffff);
1186 ret = -EINVAL;
1187 goto err_out;
1188 }
1189
1190 bd->mode.count = count;
ab59a510 1191 sdmac->chn_count += count;
1ec1e82f
SH
1192
1193 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
1194 ret = -EINVAL;
1195 goto err_out;
1196 }
1fa81c27
SH
1197
1198 switch (sdmac->word_size) {
1199 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1ec1e82f 1200 bd->mode.command = 0;
1fa81c27
SH
1201 if (count & 3 || sg->dma_address & 3)
1202 return NULL;
1203 break;
1204 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1205 bd->mode.command = 2;
1206 if (count & 1 || sg->dma_address & 1)
1207 return NULL;
1208 break;
1209 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1210 bd->mode.command = 1;
1211 break;
1212 default:
1213 return NULL;
1214 }
1ec1e82f
SH
1215
1216 param = BD_DONE | BD_EXTD | BD_CONT;
1217
341b9419 1218 if (i + 1 == sg_len) {
1ec1e82f 1219 param |= BD_INTR;
341b9419
SG
1220 param |= BD_LAST;
1221 param &= ~BD_CONT;
1ec1e82f
SH
1222 }
1223
c3cc74b2
OJ
1224 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1225 i, count, (u64)sg->dma_address,
1ec1e82f
SH
1226 param & BD_WRAP ? "wrap" : "",
1227 param & BD_INTR ? " intr" : "");
1228
1229 bd->mode.status = param;
1230 }
1231
1232 sdmac->num_bd = sg_len;
1233 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1234
1235 return &sdmac->desc;
1236err_out:
4b2ce9dd 1237 sdmac->status = DMA_ERROR;
1ec1e82f
SH
1238 return NULL;
1239}
1240
1241static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1242 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
185ecb5f 1243 size_t period_len, enum dma_transfer_direction direction,
31c1e5a1 1244 unsigned long flags)
1ec1e82f
SH
1245{
1246 struct sdma_channel *sdmac = to_sdma_chan(chan);
1247 struct sdma_engine *sdma = sdmac->sdma;
1248 int num_periods = buf_len / period_len;
23889c63 1249 int channel = sdmac->channel;
1ec1e82f
SH
1250 int ret, i = 0, buf = 0;
1251
1252 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1253
1254 if (sdmac->status == DMA_IN_PROGRESS)
1255 return NULL;
1256
1257 sdmac->status = DMA_IN_PROGRESS;
1258
8e2e27c7 1259 sdmac->buf_tail = 0;
d1a792f3 1260 sdmac->period_len = period_len;
8e2e27c7 1261
1ec1e82f
SH
1262 sdmac->flags |= IMX_DMA_SG_LOOP;
1263 sdmac->direction = direction;
1264 ret = sdma_load_context(sdmac);
1265 if (ret)
1266 goto err_out;
1267
1268 if (num_periods > NUM_BD) {
1269 dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1270 channel, num_periods, NUM_BD);
1271 goto err_out;
1272 }
1273
1274 if (period_len > 0xffff) {
1275 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
1276 channel, period_len, 0xffff);
1277 goto err_out;
1278 }
1279
1280 while (buf < buf_len) {
1281 struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
1282 int param;
1283
1284 bd->buffer_addr = dma_addr;
1285
1286 bd->mode.count = period_len;
1287
1288 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1289 goto err_out;
1290 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1291 bd->mode.command = 0;
1292 else
1293 bd->mode.command = sdmac->word_size;
1294
1295 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1296 if (i + 1 == num_periods)
1297 param |= BD_WRAP;
1298
c3cc74b2
OJ
1299 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1300 i, period_len, (u64)dma_addr,
1ec1e82f
SH
1301 param & BD_WRAP ? "wrap" : "",
1302 param & BD_INTR ? " intr" : "");
1303
1304 bd->mode.status = param;
1305
1306 dma_addr += period_len;
1307 buf += period_len;
1308
1309 i++;
1310 }
1311
1312 sdmac->num_bd = num_periods;
1313 sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
1314
1315 return &sdmac->desc;
1316err_out:
1317 sdmac->status = DMA_ERROR;
1318 return NULL;
1319}
1320
7b350ab0
MR
1321static int sdma_config(struct dma_chan *chan,
1322 struct dma_slave_config *dmaengine_cfg)
1ec1e82f
SH
1323{
1324 struct sdma_channel *sdmac = to_sdma_chan(chan);
1ec1e82f 1325
7b350ab0
MR
1326 if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
1327 sdmac->per_address = dmaengine_cfg->src_addr;
1328 sdmac->watermark_level = dmaengine_cfg->src_maxburst *
1329 dmaengine_cfg->src_addr_width;
1330 sdmac->word_size = dmaengine_cfg->src_addr_width;
8391ecf4
SW
1331 } else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) {
1332 sdmac->per_address2 = dmaengine_cfg->src_addr;
1333 sdmac->per_address = dmaengine_cfg->dst_addr;
1334 sdmac->watermark_level = dmaengine_cfg->src_maxburst &
1335 SDMA_WATERMARK_LEVEL_LWML;
1336 sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
1337 SDMA_WATERMARK_LEVEL_HWML;
1338 sdmac->word_size = dmaengine_cfg->dst_addr_width;
7b350ab0
MR
1339 } else {
1340 sdmac->per_address = dmaengine_cfg->dst_addr;
1341 sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
1342 dmaengine_cfg->dst_addr_width;
1343 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1344 }
1345 sdmac->direction = dmaengine_cfg->direction;
1346 return sdma_config_channel(chan);
1ec1e82f
SH
1347}
1348
1349static enum dma_status sdma_tx_status(struct dma_chan *chan,
e8e3a790
AS
1350 dma_cookie_t cookie,
1351 struct dma_tx_state *txstate)
1ec1e82f
SH
1352{
1353 struct sdma_channel *sdmac = to_sdma_chan(chan);
d1a792f3
RKAL
1354 u32 residue;
1355
1356 if (sdmac->flags & IMX_DMA_SG_LOOP)
1357 residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len;
1358 else
1359 residue = sdmac->chn_count - sdmac->chn_real_count;
1ec1e82f 1360
e8e3a790 1361 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
d1a792f3 1362 residue);
1ec1e82f 1363
8a965911 1364 return sdmac->status;
1ec1e82f
SH
1365}
1366
1367static void sdma_issue_pending(struct dma_chan *chan)
1368{
2b4f130e
SH
1369 struct sdma_channel *sdmac = to_sdma_chan(chan);
1370 struct sdma_engine *sdma = sdmac->sdma;
1371
1372 if (sdmac->status == DMA_IN_PROGRESS)
1373 sdma_enable_channel(sdma, sdmac->channel);
1ec1e82f
SH
1374}
1375
5b28aa31 1376#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
cd72b846 1377#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
a572460b 1378#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
5b28aa31
SH
1379
1380static void sdma_add_scripts(struct sdma_engine *sdma,
1381 const struct sdma_script_start_addrs *addr)
1382{
1383 s32 *addr_arr = (u32 *)addr;
1384 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1385 int i;
1386
70dabaed
NC
1387 /* use the default firmware in ROM if missing external firmware */
1388 if (!sdma->script_number)
1389 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1390
cd72b846 1391 for (i = 0; i < sdma->script_number; i++)
5b28aa31
SH
1392 if (addr_arr[i] > 0)
1393 saddr_arr[i] = addr_arr[i];
1394}
1395
7b4b88e0 1396static void sdma_load_firmware(const struct firmware *fw, void *context)
5b28aa31 1397{
7b4b88e0 1398 struct sdma_engine *sdma = context;
5b28aa31 1399 const struct sdma_firmware_header *header;
5b28aa31
SH
1400 const struct sdma_script_start_addrs *addr;
1401 unsigned short *ram_code;
1402
7b4b88e0 1403 if (!fw) {
0f927a11
SH
1404 dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
1405 /* In this case we just use the ROM firmware. */
7b4b88e0
SH
1406 return;
1407 }
5b28aa31
SH
1408
1409 if (fw->size < sizeof(*header))
1410 goto err_firmware;
1411
1412 header = (struct sdma_firmware_header *)fw->data;
1413
1414 if (header->magic != SDMA_FIRMWARE_MAGIC)
1415 goto err_firmware;
1416 if (header->ram_code_start + header->ram_code_size > fw->size)
1417 goto err_firmware;
cd72b846 1418 switch (header->version_major) {
681d15ec
AV
1419 case 1:
1420 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1421 break;
1422 case 2:
1423 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1424 break;
a572460b
FE
1425 case 3:
1426 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1427 break;
681d15ec
AV
1428 default:
1429 dev_err(sdma->dev, "unknown firmware version\n");
1430 goto err_firmware;
cd72b846 1431 }
5b28aa31
SH
1432
1433 addr = (void *)header + header->script_addrs_start;
1434 ram_code = (void *)header + header->ram_code_start;
1435
7560e3f3
SH
1436 clk_enable(sdma->clk_ipg);
1437 clk_enable(sdma->clk_ahb);
5b28aa31
SH
1438 /* download the RAM image for SDMA */
1439 sdma_load_script(sdma, ram_code,
1440 header->ram_code_size,
6866fd3b 1441 addr->ram_code_start_addr);
7560e3f3
SH
1442 clk_disable(sdma->clk_ipg);
1443 clk_disable(sdma->clk_ahb);
5b28aa31
SH
1444
1445 sdma_add_scripts(sdma, addr);
1446
1447 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1448 header->version_major,
1449 header->version_minor);
1450
1451err_firmware:
1452 release_firmware(fw);
7b4b88e0
SH
1453}
1454
d078cd1b
ZW
1455#define EVENT_REMAP_CELLS 3
1456
29f493da 1457static int sdma_event_remap(struct sdma_engine *sdma)
d078cd1b
ZW
1458{
1459 struct device_node *np = sdma->dev->of_node;
1460 struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1461 struct property *event_remap;
1462 struct regmap *gpr;
1463 char propname[] = "fsl,sdma-event-remap";
1464 u32 reg, val, shift, num_map, i;
1465 int ret = 0;
1466
1467 if (IS_ERR(np) || IS_ERR(gpr_np))
1468 goto out;
1469
1470 event_remap = of_find_property(np, propname, NULL);
1471 num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1472 if (!num_map) {
ce078af7 1473 dev_dbg(sdma->dev, "no event needs to be remapped\n");
d078cd1b
ZW
1474 goto out;
1475 } else if (num_map % EVENT_REMAP_CELLS) {
1476 dev_err(sdma->dev, "the property %s must modulo %d\n",
1477 propname, EVENT_REMAP_CELLS);
1478 ret = -EINVAL;
1479 goto out;
1480 }
1481
1482 gpr = syscon_node_to_regmap(gpr_np);
1483 if (IS_ERR(gpr)) {
1484 dev_err(sdma->dev, "failed to get gpr regmap\n");
1485 ret = PTR_ERR(gpr);
1486 goto out;
1487 }
1488
1489 for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1490 ret = of_property_read_u32_index(np, propname, i, &reg);
1491 if (ret) {
1492 dev_err(sdma->dev, "failed to read property %s index %d\n",
1493 propname, i);
1494 goto out;
1495 }
1496
1497 ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1498 if (ret) {
1499 dev_err(sdma->dev, "failed to read property %s index %d\n",
1500 propname, i + 1);
1501 goto out;
1502 }
1503
1504 ret = of_property_read_u32_index(np, propname, i + 2, &val);
1505 if (ret) {
1506 dev_err(sdma->dev, "failed to read property %s index %d\n",
1507 propname, i + 2);
1508 goto out;
1509 }
1510
1511 regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1512 }
1513
1514out:
1515 if (!IS_ERR(gpr_np))
1516 of_node_put(gpr_np);
1517
1518 return ret;
1519}
1520
fe6cf289 1521static int sdma_get_firmware(struct sdma_engine *sdma,
7b4b88e0
SH
1522 const char *fw_name)
1523{
1524 int ret;
1525
1526 ret = request_firmware_nowait(THIS_MODULE,
1527 FW_ACTION_HOTPLUG, fw_name, sdma->dev,
1528 GFP_KERNEL, sdma, sdma_load_firmware);
5b28aa31
SH
1529
1530 return ret;
1531}
1532
19bfc772 1533static int sdma_init(struct sdma_engine *sdma)
1ec1e82f
SH
1534{
1535 int i, ret;
1536 dma_addr_t ccb_phys;
1537
b93edcdd
FE
1538 ret = clk_enable(sdma->clk_ipg);
1539 if (ret)
1540 return ret;
1541 ret = clk_enable(sdma->clk_ahb);
1542 if (ret)
1543 goto disable_clk_ipg;
1ec1e82f
SH
1544
1545 /* Be sure SDMA has not started yet */
c4b56857 1546 writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
1ec1e82f
SH
1547
1548 sdma->channel_control = dma_alloc_coherent(NULL,
1549 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1550 sizeof(struct sdma_context_data),
1551 &ccb_phys, GFP_KERNEL);
1552
1553 if (!sdma->channel_control) {
1554 ret = -ENOMEM;
1555 goto err_dma_alloc;
1556 }
1557
1558 sdma->context = (void *)sdma->channel_control +
1559 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1560 sdma->context_phys = ccb_phys +
1561 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1562
1563 /* Zero-out the CCB structures array just allocated */
1564 memset(sdma->channel_control, 0,
1565 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1566
1567 /* disable all channels */
17bba72f 1568 for (i = 0; i < sdma->drvdata->num_events; i++)
c4b56857 1569 writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
1ec1e82f
SH
1570
1571 /* All channels have priority 0 */
1572 for (i = 0; i < MAX_DMA_CHANNELS; i++)
c4b56857 1573 writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1ec1e82f
SH
1574
1575 ret = sdma_request_channel(&sdma->channel[0]);
1576 if (ret)
1577 goto err_dma_alloc;
1578
1579 sdma_config_ownership(&sdma->channel[0], false, true, false);
1580
1581 /* Set Command Channel (Channel Zero) */
c4b56857 1582 writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
1ec1e82f
SH
1583
1584 /* Set bits of CONFIG register but with static context switching */
1585 /* FIXME: Check whether to set ACR bit depending on clock ratios */
c4b56857 1586 writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
1ec1e82f 1587
c4b56857 1588 writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1ec1e82f 1589
1ec1e82f
SH
1590 /* Initializes channel's priorities */
1591 sdma_set_channel_priority(&sdma->channel[0], 7);
1592
7560e3f3
SH
1593 clk_disable(sdma->clk_ipg);
1594 clk_disable(sdma->clk_ahb);
1ec1e82f
SH
1595
1596 return 0;
1597
1598err_dma_alloc:
7560e3f3 1599 clk_disable(sdma->clk_ahb);
b93edcdd
FE
1600disable_clk_ipg:
1601 clk_disable(sdma->clk_ipg);
1ec1e82f
SH
1602 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1603 return ret;
1604}
1605
9479e17c
SG
1606static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
1607{
0b351865 1608 struct sdma_channel *sdmac = to_sdma_chan(chan);
9479e17c
SG
1609 struct imx_dma_data *data = fn_param;
1610
1611 if (!imx_dma_is_general_purpose(chan))
1612 return false;
1613
0b351865
NC
1614 sdmac->data = *data;
1615 chan->private = &sdmac->data;
9479e17c
SG
1616
1617 return true;
1618}
1619
1620static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
1621 struct of_dma *ofdma)
1622{
1623 struct sdma_engine *sdma = ofdma->of_dma_data;
1624 dma_cap_mask_t mask = sdma->dma_device.cap_mask;
1625 struct imx_dma_data data;
1626
1627 if (dma_spec->args_count != 3)
1628 return NULL;
1629
1630 data.dma_request = dma_spec->args[0];
1631 data.peripheral_type = dma_spec->args[1];
1632 data.priority = dma_spec->args[2];
8391ecf4
SW
1633 /*
1634 * init dma_request2 to zero, which is not used by the dts.
1635 * For P2P, dma_request2 is init from dma_request_channel(),
1636 * chan->private will point to the imx_dma_data, and in
1637 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
1638 * be set to sdmac->event_id1.
1639 */
1640 data.dma_request2 = 0;
9479e17c
SG
1641
1642 return dma_request_channel(mask, sdma_filter_fn, &data);
1643}
1644
e34b731f 1645static int sdma_probe(struct platform_device *pdev)
1ec1e82f 1646{
580975d7
SG
1647 const struct of_device_id *of_id =
1648 of_match_device(sdma_dt_ids, &pdev->dev);
1649 struct device_node *np = pdev->dev.of_node;
8391ecf4 1650 struct device_node *spba_bus;
580975d7 1651 const char *fw_name;
1ec1e82f 1652 int ret;
1ec1e82f 1653 int irq;
1ec1e82f 1654 struct resource *iores;
8391ecf4 1655 struct resource spba_res;
d4adcc01 1656 struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
1ec1e82f 1657 int i;
1ec1e82f 1658 struct sdma_engine *sdma;
36e2f21a 1659 s32 *saddr_arr;
17bba72f
SH
1660 const struct sdma_driver_data *drvdata = NULL;
1661
1662 if (of_id)
1663 drvdata = of_id->data;
1664 else if (pdev->id_entry)
1665 drvdata = (void *)pdev->id_entry->driver_data;
1666
1667 if (!drvdata) {
1668 dev_err(&pdev->dev, "unable to find driver data\n");
1669 return -EINVAL;
1670 }
1ec1e82f 1671
42536b9f
PR
1672 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1673 if (ret)
1674 return ret;
1675
7f24e0ee 1676 sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
1ec1e82f
SH
1677 if (!sdma)
1678 return -ENOMEM;
1679
2ccaef05 1680 spin_lock_init(&sdma->channel_0_lock);
73eab978 1681
1ec1e82f 1682 sdma->dev = &pdev->dev;
17bba72f 1683 sdma->drvdata = drvdata;
1ec1e82f 1684
1ec1e82f 1685 irq = platform_get_irq(pdev, 0);
7f24e0ee 1686 if (irq < 0)
63c72e02 1687 return irq;
1ec1e82f 1688
7f24e0ee
FE
1689 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1690 sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
1691 if (IS_ERR(sdma->regs))
1692 return PTR_ERR(sdma->regs);
1ec1e82f 1693
7560e3f3 1694 sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
7f24e0ee
FE
1695 if (IS_ERR(sdma->clk_ipg))
1696 return PTR_ERR(sdma->clk_ipg);
1ec1e82f 1697
7560e3f3 1698 sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
7f24e0ee
FE
1699 if (IS_ERR(sdma->clk_ahb))
1700 return PTR_ERR(sdma->clk_ahb);
7560e3f3
SH
1701
1702 clk_prepare(sdma->clk_ipg);
1703 clk_prepare(sdma->clk_ahb);
1704
7f24e0ee
FE
1705 ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
1706 sdma);
1ec1e82f 1707 if (ret)
7f24e0ee 1708 return ret;
1ec1e82f 1709
5bb9dbb5
VK
1710 sdma->irq = irq;
1711
5b28aa31 1712 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
7f24e0ee
FE
1713 if (!sdma->script_addrs)
1714 return -ENOMEM;
1ec1e82f 1715
36e2f21a
SH
1716 /* initially no scripts available */
1717 saddr_arr = (s32 *)sdma->script_addrs;
1718 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
1719 saddr_arr[i] = -EINVAL;
1720
7214a8b1
SH
1721 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
1722 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
1723
1ec1e82f
SH
1724 INIT_LIST_HEAD(&sdma->dma_device.channels);
1725 /* Initialize channel parameters */
1726 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1727 struct sdma_channel *sdmac = &sdma->channel[i];
1728
1729 sdmac->sdma = sdma;
1730 spin_lock_init(&sdmac->lock);
1731
1ec1e82f 1732 sdmac->chan.device = &sdma->dma_device;
8ac69546 1733 dma_cookie_init(&sdmac->chan);
1ec1e82f
SH
1734 sdmac->channel = i;
1735
abd9ccc8
HS
1736 tasklet_init(&sdmac->tasklet, sdma_tasklet,
1737 (unsigned long) sdmac);
23889c63
SH
1738 /*
1739 * Add the channel to the DMAC list. Do not add channel 0 though
1740 * because we need it internally in the SDMA driver. This also means
1741 * that channel 0 in dmaengine counting matches sdma channel 1.
1742 */
1743 if (i)
1744 list_add_tail(&sdmac->chan.device_node,
1745 &sdma->dma_device.channels);
1ec1e82f
SH
1746 }
1747
5b28aa31 1748 ret = sdma_init(sdma);
1ec1e82f
SH
1749 if (ret)
1750 goto err_init;
1751
d078cd1b
ZW
1752 ret = sdma_event_remap(sdma);
1753 if (ret)
1754 goto err_init;
1755
dcfec3c0
SH
1756 if (sdma->drvdata->script_addrs)
1757 sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
580975d7 1758 if (pdata && pdata->script_addrs)
5b28aa31
SH
1759 sdma_add_scripts(sdma, pdata->script_addrs);
1760
580975d7 1761 if (pdata) {
6d0d7e2d
FE
1762 ret = sdma_get_firmware(sdma, pdata->fw_name);
1763 if (ret)
ad1122e5 1764 dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
580975d7
SG
1765 } else {
1766 /*
1767 * Because that device tree does not encode ROM script address,
1768 * the RAM script in firmware is mandatory for device tree
1769 * probe, otherwise it fails.
1770 */
1771 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
1772 &fw_name);
6602b0dd 1773 if (ret)
ad1122e5 1774 dev_warn(&pdev->dev, "failed to get firmware name\n");
6602b0dd
FE
1775 else {
1776 ret = sdma_get_firmware(sdma, fw_name);
1777 if (ret)
ad1122e5 1778 dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
580975d7
SG
1779 }
1780 }
5b28aa31 1781
1ec1e82f
SH
1782 sdma->dma_device.dev = &pdev->dev;
1783
1784 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1785 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
1786 sdma->dma_device.device_tx_status = sdma_tx_status;
1787 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1788 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
7b350ab0
MR
1789 sdma->dma_device.device_config = sdma_config;
1790 sdma->dma_device.device_terminate_all = sdma_disable_channel;
1e4a4f50
FE
1791 sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1792 sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1793 sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1794 sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1ec1e82f 1795 sdma->dma_device.device_issue_pending = sdma_issue_pending;
b9b3f82f
SH
1796 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1797 dma_set_max_seg_size(sdma->dma_device.dev, 65535);
1ec1e82f 1798
23e11811
VR
1799 platform_set_drvdata(pdev, sdma);
1800
1ec1e82f
SH
1801 ret = dma_async_device_register(&sdma->dma_device);
1802 if (ret) {
1803 dev_err(&pdev->dev, "unable to register\n");
1804 goto err_init;
1805 }
1806
9479e17c
SG
1807 if (np) {
1808 ret = of_dma_controller_register(np, sdma_xlate, sdma);
1809 if (ret) {
1810 dev_err(&pdev->dev, "failed to register controller\n");
1811 goto err_register;
1812 }
8391ecf4
SW
1813
1814 spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
1815 ret = of_address_to_resource(spba_bus, 0, &spba_res);
1816 if (!ret) {
1817 sdma->spba_start_addr = spba_res.start;
1818 sdma->spba_end_addr = spba_res.end;
1819 }
1820 of_node_put(spba_bus);
9479e17c
SG
1821 }
1822
1ec1e82f
SH
1823 return 0;
1824
9479e17c
SG
1825err_register:
1826 dma_async_device_unregister(&sdma->dma_device);
1ec1e82f
SH
1827err_init:
1828 kfree(sdma->script_addrs);
939fd4f0 1829 return ret;
1ec1e82f
SH
1830}
1831
1d1bbd30 1832static int sdma_remove(struct platform_device *pdev)
1ec1e82f 1833{
23e11811 1834 struct sdma_engine *sdma = platform_get_drvdata(pdev);
c12fe497 1835 int i;
23e11811 1836
5bb9dbb5 1837 devm_free_irq(&pdev->dev, sdma->irq, sdma);
23e11811
VR
1838 dma_async_device_unregister(&sdma->dma_device);
1839 kfree(sdma->script_addrs);
c12fe497
VR
1840 /* Kill the tasklet */
1841 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1842 struct sdma_channel *sdmac = &sdma->channel[i];
1843
1844 tasklet_kill(&sdmac->tasklet);
1845 }
23e11811
VR
1846
1847 platform_set_drvdata(pdev, NULL);
23e11811 1848 return 0;
1ec1e82f
SH
1849}
1850
1851static struct platform_driver sdma_driver = {
1852 .driver = {
1853 .name = "imx-sdma",
580975d7 1854 .of_match_table = sdma_dt_ids,
1ec1e82f 1855 },
62550cd7 1856 .id_table = sdma_devtypes,
1d1bbd30 1857 .remove = sdma_remove,
23e11811 1858 .probe = sdma_probe,
1ec1e82f
SH
1859};
1860
23e11811 1861module_platform_driver(sdma_driver);
1ec1e82f
SH
1862
1863MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1864MODULE_DESCRIPTION("i.MX SDMA driver");
1865MODULE_LICENSE("GPL");
This page took 0.324941 seconds and 5 git commands to generate.