dmaengine: imx-dma: change return of 'imxdma_sg_next' to void
[linux-2.6-block.git] / drivers / dma / imx-sdma.c
CommitLineData
c01faaca
FE
1// SPDX-License-Identifier: GPL-2.0+
2//
3// drivers/dma/imx-sdma.c
4//
5// This file contains a driver for the Freescale Smart DMA engine
6//
7// Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8//
9// Based on code from Freescale:
10//
11// Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
1ec1e82f
SH
12
13#include <linux/init.h>
1d069bfa 14#include <linux/iopoll.h>
f8de8f4c 15#include <linux/module.h>
1ec1e82f 16#include <linux/types.h>
0bbc1413 17#include <linux/bitops.h>
1ec1e82f
SH
18#include <linux/mm.h>
19#include <linux/interrupt.h>
20#include <linux/clk.h>
2ccaef05 21#include <linux/delay.h>
1ec1e82f
SH
22#include <linux/sched.h>
23#include <linux/semaphore.h>
24#include <linux/spinlock.h>
25#include <linux/device.h>
26#include <linux/dma-mapping.h>
27#include <linux/firmware.h>
28#include <linux/slab.h>
29#include <linux/platform_device.h>
30#include <linux/dmaengine.h>
580975d7 31#include <linux/of.h>
8391ecf4 32#include <linux/of_address.h>
580975d7 33#include <linux/of_device.h>
9479e17c 34#include <linux/of_dma.h>
b8603d2a 35#include <linux/workqueue.h>
1ec1e82f
SH
36
37#include <asm/irq.h>
82906b13
AB
38#include <linux/platform_data/dma-imx-sdma.h>
39#include <linux/platform_data/dma-imx.h>
d078cd1b
ZW
40#include <linux/regmap.h>
41#include <linux/mfd/syscon.h>
42#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
1ec1e82f 43
d2ebfb33 44#include "dmaengine.h"
57b772b8 45#include "virt-dma.h"
d2ebfb33 46
1ec1e82f
SH
47/* SDMA registers */
48#define SDMA_H_C0PTR 0x000
49#define SDMA_H_INTR 0x004
50#define SDMA_H_STATSTOP 0x008
51#define SDMA_H_START 0x00c
52#define SDMA_H_EVTOVR 0x010
53#define SDMA_H_DSPOVR 0x014
54#define SDMA_H_HOSTOVR 0x018
55#define SDMA_H_EVTPEND 0x01c
56#define SDMA_H_DSPENBL 0x020
57#define SDMA_H_RESET 0x024
58#define SDMA_H_EVTERR 0x028
59#define SDMA_H_INTRMSK 0x02c
60#define SDMA_H_PSW 0x030
61#define SDMA_H_EVTERRDBG 0x034
62#define SDMA_H_CONFIG 0x038
63#define SDMA_ONCE_ENB 0x040
64#define SDMA_ONCE_DATA 0x044
65#define SDMA_ONCE_INSTR 0x048
66#define SDMA_ONCE_STAT 0x04c
67#define SDMA_ONCE_CMD 0x050
68#define SDMA_EVT_MIRROR 0x054
69#define SDMA_ILLINSTADDR 0x058
70#define SDMA_CHN0ADDR 0x05c
71#define SDMA_ONCE_RTB 0x060
72#define SDMA_XTRIG_CONF1 0x070
73#define SDMA_XTRIG_CONF2 0x074
62550cd7
SG
74#define SDMA_CHNENBL0_IMX35 0x200
75#define SDMA_CHNENBL0_IMX31 0x080
1ec1e82f
SH
76#define SDMA_CHNPRI_0 0x100
77
78/*
79 * Buffer descriptor status values.
80 */
81#define BD_DONE 0x01
82#define BD_WRAP 0x02
83#define BD_CONT 0x04
84#define BD_INTR 0x08
85#define BD_RROR 0x10
86#define BD_LAST 0x20
87#define BD_EXTD 0x80
88
89/*
90 * Data Node descriptor status values.
91 */
92#define DND_END_OF_FRAME 0x80
93#define DND_END_OF_XFER 0x40
94#define DND_DONE 0x20
95#define DND_UNUSED 0x01
96
97/*
98 * IPCV2 descriptor status values.
99 */
100#define BD_IPCV2_END_OF_FRAME 0x40
101
102#define IPCV2_MAX_NODES 50
103/*
104 * Error bit set in the CCB status field by the SDMA,
105 * in setbd routine, in case of a transfer error
106 */
107#define DATA_ERROR 0x10000000
108
109/*
110 * Buffer descriptor commands.
111 */
112#define C0_ADDR 0x01
113#define C0_LOAD 0x02
114#define C0_DUMP 0x03
115#define C0_SETCTX 0x07
116#define C0_GETCTX 0x03
117#define C0_SETDM 0x01
118#define C0_SETPM 0x04
119#define C0_GETDM 0x02
120#define C0_GETPM 0x08
121/*
122 * Change endianness indicator in the BD command field
123 */
124#define CHANGE_ENDIANNESS 0x80
125
8391ecf4
SW
126/*
127 * p_2_p watermark_level description
128 * Bits Name Description
129 * 0-7 Lower WML Lower watermark level
130 * 8 PS 1: Pad Swallowing
131 * 0: No Pad Swallowing
132 * 9 PA 1: Pad Adding
133 * 0: No Pad Adding
134 * 10 SPDIF If this bit is set both source
135 * and destination are on SPBA
136 * 11 Source Bit(SP) 1: Source on SPBA
137 * 0: Source on AIPS
138 * 12 Destination Bit(DP) 1: Destination on SPBA
139 * 0: Destination on AIPS
140 * 13-15 --------- MUST BE 0
141 * 16-23 Higher WML HWML
142 * 24-27 N Total number of samples after
143 * which Pad adding/Swallowing
144 * must be done. It must be odd.
145 * 28 Lower WML Event(LWE) SDMA events reg to check for
146 * LWML event mask
147 * 0: LWE in EVENTS register
148 * 1: LWE in EVENTS2 register
149 * 29 Higher WML Event(HWE) SDMA events reg to check for
150 * HWML event mask
151 * 0: HWE in EVENTS register
152 * 1: HWE in EVENTS2 register
153 * 30 --------- MUST BE 0
154 * 31 CONT 1: Amount of samples to be
155 * transferred is unknown and
156 * script will keep on
157 * transferring samples as long as
158 * both events are detected and
159 * script must be manually stopped
160 * by the application
161 * 0: The amount of samples to be
162 * transferred is equal to the
163 * count field of mode word
164 */
165#define SDMA_WATERMARK_LEVEL_LWML 0xFF
166#define SDMA_WATERMARK_LEVEL_PS BIT(8)
167#define SDMA_WATERMARK_LEVEL_PA BIT(9)
168#define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
169#define SDMA_WATERMARK_LEVEL_SP BIT(11)
170#define SDMA_WATERMARK_LEVEL_DP BIT(12)
171#define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
172#define SDMA_WATERMARK_LEVEL_LWE BIT(28)
173#define SDMA_WATERMARK_LEVEL_HWE BIT(29)
174#define SDMA_WATERMARK_LEVEL_CONT BIT(31)
175
f9d4a398
NC
176#define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
177 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
178 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
179
180#define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \
181 BIT(DMA_MEM_TO_DEV) | \
182 BIT(DMA_DEV_TO_DEV))
183
1ec1e82f
SH
184/*
185 * Mode/Count of data node descriptors - IPCv2
186 */
187struct sdma_mode_count {
4a6b2e8a 188#define SDMA_BD_MAX_CNT 0xffff
1ec1e82f
SH
189 u32 count : 16; /* size of the buffer pointed by this BD */
190 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
e4b75760 191 u32 command : 8; /* command mostly used for channel 0 */
1ec1e82f
SH
192};
193
194/*
195 * Buffer descriptor
196 */
197struct sdma_buffer_descriptor {
198 struct sdma_mode_count mode;
199 u32 buffer_addr; /* address of the buffer described */
200 u32 ext_buffer_addr; /* extended buffer address */
201} __attribute__ ((packed));
202
203/**
204 * struct sdma_channel_control - Channel control Block
205 *
24ca312d
RG
206 * @current_bd_ptr: current buffer descriptor processed
207 * @base_bd_ptr: first element of buffer descriptor array
208 * @unused: padding. The SDMA engine expects an array of 128 byte
1ec1e82f
SH
209 * control blocks
210 */
211struct sdma_channel_control {
212 u32 current_bd_ptr;
213 u32 base_bd_ptr;
214 u32 unused[2];
215} __attribute__ ((packed));
216
217/**
218 * struct sdma_state_registers - SDMA context for a channel
219 *
220 * @pc: program counter
24ca312d 221 * @unused1: unused
1ec1e82f
SH
222 * @t: test bit: status of arithmetic & test instruction
223 * @rpc: return program counter
24ca312d 224 * @unused0: unused
1ec1e82f
SH
225 * @sf: source fault while loading data
226 * @spc: loop start program counter
24ca312d 227 * @unused2: unused
1ec1e82f
SH
228 * @df: destination fault while storing data
229 * @epc: loop end program counter
230 * @lm: loop mode
231 */
232struct sdma_state_registers {
233 u32 pc :14;
234 u32 unused1: 1;
235 u32 t : 1;
236 u32 rpc :14;
237 u32 unused0: 1;
238 u32 sf : 1;
239 u32 spc :14;
240 u32 unused2: 1;
241 u32 df : 1;
242 u32 epc :14;
243 u32 lm : 2;
244} __attribute__ ((packed));
245
246/**
247 * struct sdma_context_data - sdma context specific to a channel
248 *
249 * @channel_state: channel state bits
250 * @gReg: general registers
251 * @mda: burst dma destination address register
252 * @msa: burst dma source address register
253 * @ms: burst dma status register
254 * @md: burst dma data register
255 * @pda: peripheral dma destination address register
256 * @psa: peripheral dma source address register
257 * @ps: peripheral dma status register
258 * @pd: peripheral dma data register
259 * @ca: CRC polynomial register
260 * @cs: CRC accumulator register
261 * @dda: dedicated core destination address register
262 * @dsa: dedicated core source address register
263 * @ds: dedicated core status register
264 * @dd: dedicated core data register
24ca312d
RG
265 * @scratch0: 1st word of dedicated ram for context switch
266 * @scratch1: 2nd word of dedicated ram for context switch
267 * @scratch2: 3rd word of dedicated ram for context switch
268 * @scratch3: 4th word of dedicated ram for context switch
269 * @scratch4: 5th word of dedicated ram for context switch
270 * @scratch5: 6th word of dedicated ram for context switch
271 * @scratch6: 7th word of dedicated ram for context switch
272 * @scratch7: 8th word of dedicated ram for context switch
1ec1e82f
SH
273 */
274struct sdma_context_data {
275 struct sdma_state_registers channel_state;
276 u32 gReg[8];
277 u32 mda;
278 u32 msa;
279 u32 ms;
280 u32 md;
281 u32 pda;
282 u32 psa;
283 u32 ps;
284 u32 pd;
285 u32 ca;
286 u32 cs;
287 u32 dda;
288 u32 dsa;
289 u32 ds;
290 u32 dd;
291 u32 scratch0;
292 u32 scratch1;
293 u32 scratch2;
294 u32 scratch3;
295 u32 scratch4;
296 u32 scratch5;
297 u32 scratch6;
298 u32 scratch7;
299} __attribute__ ((packed));
300
1ec1e82f
SH
301
302struct sdma_engine;
303
76c33d27
SH
304/**
305 * struct sdma_desc - descriptor structor for one transfer
24ca312d
RG
306 * @vd: descriptor for virt dma
307 * @num_bd: number of descriptors currently handling
308 * @bd_phys: physical address of bd
309 * @buf_tail: ID of the buffer that was processed
310 * @buf_ptail: ID of the previous buffer that was processed
311 * @period_len: period length, used in cyclic.
312 * @chn_real_count: the real count updated from bd->mode.count
313 * @chn_count: the transfer count set
314 * @sdmac: sdma_channel pointer
315 * @bd: pointer of allocate bd
76c33d27
SH
316 */
317struct sdma_desc {
57b772b8 318 struct virt_dma_desc vd;
76c33d27
SH
319 unsigned int num_bd;
320 dma_addr_t bd_phys;
321 unsigned int buf_tail;
322 unsigned int buf_ptail;
323 unsigned int period_len;
324 unsigned int chn_real_count;
325 unsigned int chn_count;
326 struct sdma_channel *sdmac;
327 struct sdma_buffer_descriptor *bd;
328};
329
1ec1e82f
SH
330/**
331 * struct sdma_channel - housekeeping for a SDMA channel
332 *
24ca312d
RG
333 * @vc: virt_dma base structure
334 * @desc: sdma description including vd and other special member
335 * @sdma: pointer to the SDMA engine for this channel
336 * @channel: the channel number, matches dmaengine chan_id + 1
337 * @direction: transfer type. Needed for setting SDMA script
107d0644 338 * @slave_config Slave configuration
24ca312d
RG
339 * @peripheral_type: Peripheral type. Needed for setting SDMA script
340 * @event_id0: aka dma request line
341 * @event_id1: for channels that use 2 events
342 * @word_size: peripheral access size
343 * @pc_from_device: script address for those device_2_memory
344 * @pc_to_device: script address for those memory_2_device
345 * @device_to_device: script address for those device_2_device
0f06c027 346 * @pc_to_pc: script address for those memory_2_memory
24ca312d
RG
347 * @flags: loop mode or not
348 * @per_address: peripheral source or destination address in common case
349 * destination address in p_2_p case
350 * @per_address2: peripheral source address in p_2_p case
351 * @event_mask: event mask used in p_2_p script
352 * @watermark_level: value for gReg[7], some script will extend it from
353 * basic watermark such as p_2_p
354 * @shp_addr: value for gReg[6]
355 * @per_addr: value for gReg[2]
356 * @status: status of dma channel
357 * @data: specific sdma interface structure
358 * @bd_pool: dma_pool for bd
1ec1e82f
SH
359 */
360struct sdma_channel {
57b772b8 361 struct virt_dma_chan vc;
76c33d27 362 struct sdma_desc *desc;
1ec1e82f
SH
363 struct sdma_engine *sdma;
364 unsigned int channel;
db8196df 365 enum dma_transfer_direction direction;
107d0644 366 struct dma_slave_config slave_config;
1ec1e82f
SH
367 enum sdma_peripheral_type peripheral_type;
368 unsigned int event_id0;
369 unsigned int event_id1;
370 enum dma_slave_buswidth word_size;
1ec1e82f 371 unsigned int pc_from_device, pc_to_device;
8391ecf4 372 unsigned int device_to_device;
0f06c027 373 unsigned int pc_to_pc;
1ec1e82f 374 unsigned long flags;
8391ecf4 375 dma_addr_t per_address, per_address2;
0bbc1413
RZ
376 unsigned long event_mask[2];
377 unsigned long watermark_level;
1ec1e82f 378 u32 shp_addr, per_addr;
1ec1e82f 379 enum dma_status status;
ad0d92d7 380 bool context_loaded;
0b351865 381 struct imx_dma_data data;
b8603d2a 382 struct work_struct terminate_worker;
1ec1e82f
SH
383};
384
0bbc1413 385#define IMX_DMA_SG_LOOP BIT(0)
1ec1e82f
SH
386
387#define MAX_DMA_CHANNELS 32
388#define MXC_SDMA_DEFAULT_PRIORITY 1
389#define MXC_SDMA_MIN_PRIORITY 1
390#define MXC_SDMA_MAX_PRIORITY 7
391
1ec1e82f
SH
392#define SDMA_FIRMWARE_MAGIC 0x414d4453
393
394/**
395 * struct sdma_firmware_header - Layout of the firmware image
396 *
24ca312d
RG
397 * @magic: "SDMA"
398 * @version_major: increased whenever layout of struct
399 * sdma_script_start_addrs changes.
400 * @version_minor: firmware minor version (for binary compatible changes)
401 * @script_addrs_start: offset of struct sdma_script_start_addrs in this image
402 * @num_script_addrs: Number of script addresses in this image
403 * @ram_code_start: offset of SDMA ram image in this firmware image
404 * @ram_code_size: size of SDMA ram image
405 * @script_addrs: Stores the start address of the SDMA scripts
1ec1e82f
SH
406 * (in SDMA memory space)
407 */
408struct sdma_firmware_header {
409 u32 magic;
410 u32 version_major;
411 u32 version_minor;
412 u32 script_addrs_start;
413 u32 num_script_addrs;
414 u32 ram_code_start;
415 u32 ram_code_size;
416};
417
17bba72f
SH
418struct sdma_driver_data {
419 int chnenbl0;
420 int num_events;
dcfec3c0 421 struct sdma_script_start_addrs *script_addrs;
62550cd7
SG
422};
423
1ec1e82f
SH
424struct sdma_engine {
425 struct device *dev;
b9b3f82f 426 struct device_dma_parameters dma_parms;
1ec1e82f
SH
427 struct sdma_channel channel[MAX_DMA_CHANNELS];
428 struct sdma_channel_control *channel_control;
429 void __iomem *regs;
1ec1e82f
SH
430 struct sdma_context_data *context;
431 dma_addr_t context_phys;
432 struct dma_device dma_device;
7560e3f3
SH
433 struct clk *clk_ipg;
434 struct clk *clk_ahb;
2ccaef05 435 spinlock_t channel_0_lock;
cd72b846 436 u32 script_number;
1ec1e82f 437 struct sdma_script_start_addrs *script_addrs;
17bba72f 438 const struct sdma_driver_data *drvdata;
8391ecf4
SW
439 u32 spba_start_addr;
440 u32 spba_end_addr;
5bb9dbb5 441 unsigned int irq;
76c33d27
SH
442 dma_addr_t bd0_phys;
443 struct sdma_buffer_descriptor *bd0;
17bba72f
SH
444};
445
107d0644
VK
446static int sdma_config_write(struct dma_chan *chan,
447 struct dma_slave_config *dmaengine_cfg,
448 enum dma_transfer_direction direction);
449
e9fd58de 450static struct sdma_driver_data sdma_imx31 = {
17bba72f
SH
451 .chnenbl0 = SDMA_CHNENBL0_IMX31,
452 .num_events = 32,
453};
454
dcfec3c0
SH
455static struct sdma_script_start_addrs sdma_script_imx25 = {
456 .ap_2_ap_addr = 729,
457 .uart_2_mcu_addr = 904,
458 .per_2_app_addr = 1255,
459 .mcu_2_app_addr = 834,
460 .uartsh_2_mcu_addr = 1120,
461 .per_2_shp_addr = 1329,
462 .mcu_2_shp_addr = 1048,
463 .ata_2_mcu_addr = 1560,
464 .mcu_2_ata_addr = 1479,
465 .app_2_per_addr = 1189,
466 .app_2_mcu_addr = 770,
467 .shp_2_per_addr = 1407,
468 .shp_2_mcu_addr = 979,
469};
470
e9fd58de 471static struct sdma_driver_data sdma_imx25 = {
dcfec3c0
SH
472 .chnenbl0 = SDMA_CHNENBL0_IMX35,
473 .num_events = 48,
474 .script_addrs = &sdma_script_imx25,
475};
476
e9fd58de 477static struct sdma_driver_data sdma_imx35 = {
17bba72f
SH
478 .chnenbl0 = SDMA_CHNENBL0_IMX35,
479 .num_events = 48,
1ec1e82f
SH
480};
481
dcfec3c0
SH
482static struct sdma_script_start_addrs sdma_script_imx51 = {
483 .ap_2_ap_addr = 642,
484 .uart_2_mcu_addr = 817,
485 .mcu_2_app_addr = 747,
486 .mcu_2_shp_addr = 961,
487 .ata_2_mcu_addr = 1473,
488 .mcu_2_ata_addr = 1392,
489 .app_2_per_addr = 1033,
490 .app_2_mcu_addr = 683,
491 .shp_2_per_addr = 1251,
492 .shp_2_mcu_addr = 892,
493};
494
e9fd58de 495static struct sdma_driver_data sdma_imx51 = {
dcfec3c0
SH
496 .chnenbl0 = SDMA_CHNENBL0_IMX35,
497 .num_events = 48,
498 .script_addrs = &sdma_script_imx51,
499};
500
501static struct sdma_script_start_addrs sdma_script_imx53 = {
502 .ap_2_ap_addr = 642,
503 .app_2_mcu_addr = 683,
504 .mcu_2_app_addr = 747,
505 .uart_2_mcu_addr = 817,
506 .shp_2_mcu_addr = 891,
507 .mcu_2_shp_addr = 960,
508 .uartsh_2_mcu_addr = 1032,
509 .spdif_2_mcu_addr = 1100,
510 .mcu_2_spdif_addr = 1134,
511 .firi_2_mcu_addr = 1193,
512 .mcu_2_firi_addr = 1290,
513};
514
e9fd58de 515static struct sdma_driver_data sdma_imx53 = {
dcfec3c0
SH
516 .chnenbl0 = SDMA_CHNENBL0_IMX35,
517 .num_events = 48,
518 .script_addrs = &sdma_script_imx53,
519};
520
521static struct sdma_script_start_addrs sdma_script_imx6q = {
522 .ap_2_ap_addr = 642,
523 .uart_2_mcu_addr = 817,
524 .mcu_2_app_addr = 747,
525 .per_2_per_addr = 6331,
526 .uartsh_2_mcu_addr = 1032,
527 .mcu_2_shp_addr = 960,
528 .app_2_mcu_addr = 683,
529 .shp_2_mcu_addr = 891,
530 .spdif_2_mcu_addr = 1100,
531 .mcu_2_spdif_addr = 1134,
532};
533
e9fd58de 534static struct sdma_driver_data sdma_imx6q = {
dcfec3c0
SH
535 .chnenbl0 = SDMA_CHNENBL0_IMX35,
536 .num_events = 48,
537 .script_addrs = &sdma_script_imx6q,
538};
539
b7d2648a
FE
540static struct sdma_script_start_addrs sdma_script_imx7d = {
541 .ap_2_ap_addr = 644,
542 .uart_2_mcu_addr = 819,
543 .mcu_2_app_addr = 749,
544 .uartsh_2_mcu_addr = 1034,
545 .mcu_2_shp_addr = 962,
546 .app_2_mcu_addr = 685,
547 .shp_2_mcu_addr = 893,
548 .spdif_2_mcu_addr = 1102,
549 .mcu_2_spdif_addr = 1136,
550};
551
552static struct sdma_driver_data sdma_imx7d = {
553 .chnenbl0 = SDMA_CHNENBL0_IMX35,
554 .num_events = 48,
555 .script_addrs = &sdma_script_imx7d,
556};
557
afe7cded 558static const struct platform_device_id sdma_devtypes[] = {
62550cd7 559 {
dcfec3c0
SH
560 .name = "imx25-sdma",
561 .driver_data = (unsigned long)&sdma_imx25,
562 }, {
62550cd7 563 .name = "imx31-sdma",
17bba72f 564 .driver_data = (unsigned long)&sdma_imx31,
62550cd7
SG
565 }, {
566 .name = "imx35-sdma",
17bba72f 567 .driver_data = (unsigned long)&sdma_imx35,
dcfec3c0
SH
568 }, {
569 .name = "imx51-sdma",
570 .driver_data = (unsigned long)&sdma_imx51,
571 }, {
572 .name = "imx53-sdma",
573 .driver_data = (unsigned long)&sdma_imx53,
574 }, {
575 .name = "imx6q-sdma",
576 .driver_data = (unsigned long)&sdma_imx6q,
b7d2648a
FE
577 }, {
578 .name = "imx7d-sdma",
579 .driver_data = (unsigned long)&sdma_imx7d,
62550cd7
SG
580 }, {
581 /* sentinel */
582 }
583};
584MODULE_DEVICE_TABLE(platform, sdma_devtypes);
585
580975d7 586static const struct of_device_id sdma_dt_ids[] = {
dcfec3c0
SH
587 { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
588 { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
589 { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
17bba72f 590 { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
dcfec3c0 591 { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
63edea16 592 { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
b7d2648a 593 { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
580975d7
SG
594 { /* sentinel */ }
595};
596MODULE_DEVICE_TABLE(of, sdma_dt_ids);
597
0bbc1413
RZ
598#define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
599#define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
600#define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
1ec1e82f
SH
601#define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
602
603static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
604{
17bba72f 605 u32 chnenbl0 = sdma->drvdata->chnenbl0;
1ec1e82f
SH
606 return chnenbl0 + event * 4;
607}
608
609static int sdma_config_ownership(struct sdma_channel *sdmac,
610 bool event_override, bool mcu_override, bool dsp_override)
611{
612 struct sdma_engine *sdma = sdmac->sdma;
613 int channel = sdmac->channel;
0bbc1413 614 unsigned long evt, mcu, dsp;
1ec1e82f
SH
615
616 if (event_override && mcu_override && dsp_override)
617 return -EINVAL;
618
c4b56857
RZ
619 evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
620 mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
621 dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
1ec1e82f
SH
622
623 if (dsp_override)
0bbc1413 624 __clear_bit(channel, &dsp);
1ec1e82f 625 else
0bbc1413 626 __set_bit(channel, &dsp);
1ec1e82f
SH
627
628 if (event_override)
0bbc1413 629 __clear_bit(channel, &evt);
1ec1e82f 630 else
0bbc1413 631 __set_bit(channel, &evt);
1ec1e82f
SH
632
633 if (mcu_override)
0bbc1413 634 __clear_bit(channel, &mcu);
1ec1e82f 635 else
0bbc1413 636 __set_bit(channel, &mcu);
1ec1e82f 637
c4b56857
RZ
638 writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
639 writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
640 writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
1ec1e82f
SH
641
642 return 0;
643}
644
b9a59166
RZ
645static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
646{
0bbc1413 647 writel(BIT(channel), sdma->regs + SDMA_H_START);
b9a59166
RZ
648}
649
1ec1e82f 650/*
2ccaef05 651 * sdma_run_channel0 - run a channel and wait till it's done
1ec1e82f 652 */
2ccaef05 653static int sdma_run_channel0(struct sdma_engine *sdma)
1ec1e82f 654{
1ec1e82f 655 int ret;
1d069bfa 656 u32 reg;
1ec1e82f 657
2ccaef05 658 sdma_enable_channel(sdma, 0);
1ec1e82f 659
1d069bfa
MO
660 ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
661 reg, !(reg & 1), 1, 500);
662 if (ret)
2ccaef05 663 dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
1ec1e82f 664
855832e4
RG
665 /* Set bits of CONFIG register with dynamic context switching */
666 if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
667 writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
668
1d069bfa 669 return ret;
1ec1e82f
SH
670}
671
672static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
673 u32 address)
674{
76c33d27 675 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
1ec1e82f
SH
676 void *buf_virt;
677 dma_addr_t buf_phys;
678 int ret;
2ccaef05 679 unsigned long flags;
73eab978 680
af8bf89a 681 buf_virt = dma_alloc_coherent(NULL, size, &buf_phys, GFP_KERNEL);
73eab978 682 if (!buf_virt) {
2ccaef05 683 return -ENOMEM;
73eab978 684 }
1ec1e82f 685
2ccaef05
RZ
686 spin_lock_irqsave(&sdma->channel_0_lock, flags);
687
1ec1e82f
SH
688 bd0->mode.command = C0_SETPM;
689 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
690 bd0->mode.count = size / 2;
691 bd0->buffer_addr = buf_phys;
692 bd0->ext_buffer_addr = address;
693
694 memcpy(buf_virt, buf, size);
695
2ccaef05 696 ret = sdma_run_channel0(sdma);
1ec1e82f 697
2ccaef05 698 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
1ec1e82f 699
2ccaef05 700 dma_free_coherent(NULL, size, buf_virt, buf_phys);
73eab978 701
1ec1e82f
SH
702 return ret;
703}
704
705static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
706{
707 struct sdma_engine *sdma = sdmac->sdma;
708 int channel = sdmac->channel;
0bbc1413 709 unsigned long val;
1ec1e82f
SH
710 u32 chnenbl = chnenbl_ofs(sdma, event);
711
c4b56857 712 val = readl_relaxed(sdma->regs + chnenbl);
0bbc1413 713 __set_bit(channel, &val);
c4b56857 714 writel_relaxed(val, sdma->regs + chnenbl);
1ec1e82f
SH
715}
716
717static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
718{
719 struct sdma_engine *sdma = sdmac->sdma;
720 int channel = sdmac->channel;
721 u32 chnenbl = chnenbl_ofs(sdma, event);
0bbc1413 722 unsigned long val;
1ec1e82f 723
c4b56857 724 val = readl_relaxed(sdma->regs + chnenbl);
0bbc1413 725 __clear_bit(channel, &val);
c4b56857 726 writel_relaxed(val, sdma->regs + chnenbl);
1ec1e82f
SH
727}
728
57b772b8
RG
729static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
730{
731 return container_of(t, struct sdma_desc, vd.tx);
732}
733
734static void sdma_start_desc(struct sdma_channel *sdmac)
735{
736 struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
737 struct sdma_desc *desc;
738 struct sdma_engine *sdma = sdmac->sdma;
739 int channel = sdmac->channel;
740
741 if (!vd) {
742 sdmac->desc = NULL;
743 return;
744 }
745 sdmac->desc = desc = to_sdma_desc(&vd->tx);
746 /*
747 * Do not delete the node in desc_issued list in cyclic mode, otherwise
680302c4 748 * the desc allocated will never be freed in vchan_dma_desc_free_list
57b772b8
RG
749 */
750 if (!(sdmac->flags & IMX_DMA_SG_LOOP))
751 list_del(&vd->node);
752
753 sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
754 sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
755 sdma_enable_channel(sdma, sdmac->channel);
756}
757
d1a792f3 758static void sdma_update_channel_loop(struct sdma_channel *sdmac)
1ec1e82f
SH
759{
760 struct sdma_buffer_descriptor *bd;
5881826d
NH
761 int error = 0;
762 enum dma_status old_status = sdmac->status;
1ec1e82f
SH
763
764 /*
765 * loop mode. Iterate over descriptors, re-setup them and
766 * call callback function.
767 */
57b772b8 768 while (sdmac->desc) {
76c33d27
SH
769 struct sdma_desc *desc = sdmac->desc;
770
771 bd = &desc->bd[desc->buf_tail];
1ec1e82f
SH
772
773 if (bd->mode.status & BD_DONE)
774 break;
775
5881826d
NH
776 if (bd->mode.status & BD_RROR) {
777 bd->mode.status &= ~BD_RROR;
1ec1e82f 778 sdmac->status = DMA_ERROR;
5881826d
NH
779 error = -EIO;
780 }
1ec1e82f 781
5881826d
NH
782 /*
783 * We use bd->mode.count to calculate the residue, since contains
784 * the number of bytes present in the current buffer descriptor.
785 */
786
76c33d27 787 desc->chn_real_count = bd->mode.count;
1ec1e82f 788 bd->mode.status |= BD_DONE;
76c33d27
SH
789 bd->mode.count = desc->period_len;
790 desc->buf_ptail = desc->buf_tail;
791 desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
15f30f51
NH
792
793 /*
794 * The callback is called from the interrupt context in order
795 * to reduce latency and to avoid the risk of altering the
796 * SDMA transaction status by the time the client tasklet is
797 * executed.
798 */
57b772b8
RG
799 spin_unlock(&sdmac->vc.lock);
800 dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
801 spin_lock(&sdmac->vc.lock);
15f30f51 802
5881826d
NH
803 if (error)
804 sdmac->status = old_status;
1ec1e82f
SH
805 }
806}
807
57b772b8 808static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
1ec1e82f 809{
15f30f51 810 struct sdma_channel *sdmac = (struct sdma_channel *) data;
1ec1e82f
SH
811 struct sdma_buffer_descriptor *bd;
812 int i, error = 0;
813
76c33d27 814 sdmac->desc->chn_real_count = 0;
1ec1e82f
SH
815 /*
816 * non loop mode. Iterate over all descriptors, collect
817 * errors and call callback function
818 */
76c33d27
SH
819 for (i = 0; i < sdmac->desc->num_bd; i++) {
820 bd = &sdmac->desc->bd[i];
1ec1e82f
SH
821
822 if (bd->mode.status & (BD_DONE | BD_RROR))
823 error = -EIO;
76c33d27 824 sdmac->desc->chn_real_count += bd->mode.count;
1ec1e82f
SH
825 }
826
827 if (error)
828 sdmac->status = DMA_ERROR;
829 else
409bff6a 830 sdmac->status = DMA_COMPLETE;
1ec1e82f
SH
831}
832
1ec1e82f
SH
833static irqreturn_t sdma_int_handler(int irq, void *dev_id)
834{
835 struct sdma_engine *sdma = dev_id;
0bbc1413 836 unsigned long stat;
1ec1e82f 837
c4b56857
RZ
838 stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
839 writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
1d069bfa
MO
840 /* channel 0 is special and not handled here, see run_channel0() */
841 stat &= ~1;
1ec1e82f
SH
842
843 while (stat) {
844 int channel = fls(stat) - 1;
845 struct sdma_channel *sdmac = &sdma->channel[channel];
57b772b8
RG
846 struct sdma_desc *desc;
847
848 spin_lock(&sdmac->vc.lock);
849 desc = sdmac->desc;
850 if (desc) {
851 if (sdmac->flags & IMX_DMA_SG_LOOP) {
852 sdma_update_channel_loop(sdmac);
853 } else {
854 mxc_sdma_handle_channel_normal(sdmac);
855 vchan_cookie_complete(&desc->vd);
856 sdma_start_desc(sdmac);
857 }
858 }
1ec1e82f 859
57b772b8 860 spin_unlock(&sdmac->vc.lock);
0bbc1413 861 __clear_bit(channel, &stat);
1ec1e82f
SH
862 }
863
864 return IRQ_HANDLED;
865}
866
867/*
868 * sets the pc of SDMA script according to the peripheral type
869 */
870static void sdma_get_pc(struct sdma_channel *sdmac,
871 enum sdma_peripheral_type peripheral_type)
872{
873 struct sdma_engine *sdma = sdmac->sdma;
874 int per_2_emi = 0, emi_2_per = 0;
875 /*
876 * These are needed once we start to support transfers between
877 * two peripherals or memory-to-memory transfers
878 */
0f06c027 879 int per_2_per = 0, emi_2_emi = 0;
1ec1e82f
SH
880
881 sdmac->pc_from_device = 0;
882 sdmac->pc_to_device = 0;
8391ecf4 883 sdmac->device_to_device = 0;
0f06c027 884 sdmac->pc_to_pc = 0;
1ec1e82f
SH
885
886 switch (peripheral_type) {
887 case IMX_DMATYPE_MEMORY:
0f06c027 888 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
1ec1e82f
SH
889 break;
890 case IMX_DMATYPE_DSP:
891 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
892 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
893 break;
894 case IMX_DMATYPE_FIRI:
895 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
896 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
897 break;
898 case IMX_DMATYPE_UART:
899 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
900 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
901 break;
902 case IMX_DMATYPE_UART_SP:
903 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
904 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
905 break;
906 case IMX_DMATYPE_ATA:
907 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
908 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
909 break;
910 case IMX_DMATYPE_CSPI:
911 case IMX_DMATYPE_EXT:
912 case IMX_DMATYPE_SSI:
29aebfde 913 case IMX_DMATYPE_SAI:
1ec1e82f
SH
914 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
915 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
916 break;
1a895578
NC
917 case IMX_DMATYPE_SSI_DUAL:
918 per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
919 emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
920 break;
1ec1e82f
SH
921 case IMX_DMATYPE_SSI_SP:
922 case IMX_DMATYPE_MMC:
923 case IMX_DMATYPE_SDHC:
924 case IMX_DMATYPE_CSPI_SP:
925 case IMX_DMATYPE_ESAI:
926 case IMX_DMATYPE_MSHC_SP:
927 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
928 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
929 break;
930 case IMX_DMATYPE_ASRC:
931 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
932 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
933 per_2_per = sdma->script_addrs->per_2_per_addr;
934 break;
f892afb0
NC
935 case IMX_DMATYPE_ASRC_SP:
936 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
937 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
938 per_2_per = sdma->script_addrs->per_2_per_addr;
939 break;
1ec1e82f
SH
940 case IMX_DMATYPE_MSHC:
941 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
942 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
943 break;
944 case IMX_DMATYPE_CCM:
945 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
946 break;
947 case IMX_DMATYPE_SPDIF:
948 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
949 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
950 break;
951 case IMX_DMATYPE_IPU_MEMORY:
952 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
953 break;
954 default:
955 break;
956 }
957
958 sdmac->pc_from_device = per_2_emi;
959 sdmac->pc_to_device = emi_2_per;
8391ecf4 960 sdmac->device_to_device = per_2_per;
0f06c027 961 sdmac->pc_to_pc = emi_2_emi;
1ec1e82f
SH
962}
963
964static int sdma_load_context(struct sdma_channel *sdmac)
965{
966 struct sdma_engine *sdma = sdmac->sdma;
967 int channel = sdmac->channel;
968 int load_address;
969 struct sdma_context_data *context = sdma->context;
76c33d27 970 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
1ec1e82f 971 int ret;
2ccaef05 972 unsigned long flags;
1ec1e82f 973
ad0d92d7
RG
974 if (sdmac->context_loaded)
975 return 0;
976
8391ecf4 977 if (sdmac->direction == DMA_DEV_TO_MEM)
1ec1e82f 978 load_address = sdmac->pc_from_device;
8391ecf4
SW
979 else if (sdmac->direction == DMA_DEV_TO_DEV)
980 load_address = sdmac->device_to_device;
0f06c027
RG
981 else if (sdmac->direction == DMA_MEM_TO_MEM)
982 load_address = sdmac->pc_to_pc;
8391ecf4 983 else
1ec1e82f 984 load_address = sdmac->pc_to_device;
1ec1e82f
SH
985
986 if (load_address < 0)
987 return load_address;
988
989 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
0bbc1413 990 dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
1ec1e82f
SH
991 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
992 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
0bbc1413
RZ
993 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
994 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
1ec1e82f 995
2ccaef05 996 spin_lock_irqsave(&sdma->channel_0_lock, flags);
73eab978 997
1ec1e82f
SH
998 memset(context, 0, sizeof(*context));
999 context->channel_state.pc = load_address;
1000
1001 /* Send by context the event mask,base address for peripheral
1002 * and watermark level
1003 */
0bbc1413
RZ
1004 context->gReg[0] = sdmac->event_mask[1];
1005 context->gReg[1] = sdmac->event_mask[0];
1ec1e82f
SH
1006 context->gReg[2] = sdmac->per_addr;
1007 context->gReg[6] = sdmac->shp_addr;
1008 context->gReg[7] = sdmac->watermark_level;
1009
1010 bd0->mode.command = C0_SETDM;
1011 bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
1012 bd0->mode.count = sizeof(*context) / 4;
1013 bd0->buffer_addr = sdma->context_phys;
1014 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
2ccaef05 1015 ret = sdma_run_channel0(sdma);
1ec1e82f 1016
2ccaef05 1017 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
73eab978 1018
ad0d92d7
RG
1019 sdmac->context_loaded = true;
1020
1ec1e82f
SH
1021 return ret;
1022}
1023
7b350ab0
MR
1024static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
1025{
57b772b8 1026 return container_of(chan, struct sdma_channel, vc.chan);
7b350ab0
MR
1027}
1028
1029static int sdma_disable_channel(struct dma_chan *chan)
1ec1e82f 1030{
7b350ab0 1031 struct sdma_channel *sdmac = to_sdma_chan(chan);
1ec1e82f
SH
1032 struct sdma_engine *sdma = sdmac->sdma;
1033 int channel = sdmac->channel;
1034
0bbc1413 1035 writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
1ec1e82f 1036 sdmac->status = DMA_ERROR;
7b350ab0
MR
1037
1038 return 0;
1ec1e82f 1039}
b8603d2a 1040static void sdma_channel_terminate_work(struct work_struct *work)
7f3ff14b 1041{
b8603d2a
LS
1042 struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1043 terminate_worker);
57b772b8
RG
1044 unsigned long flags;
1045 LIST_HEAD(head);
1046
7f3ff14b
JW
1047 /*
1048 * According to NXP R&D team a delay of one BD SDMA cost time
1049 * (maximum is 1ms) should be added after disable of the channel
1050 * bit, to ensure SDMA core has really been stopped after SDMA
1051 * clients call .device_terminate_all.
1052 */
b8603d2a
LS
1053 usleep_range(1000, 2000);
1054
1055 spin_lock_irqsave(&sdmac->vc.lock, flags);
1056 vchan_get_all_descriptors(&sdmac->vc, &head);
1057 sdmac->desc = NULL;
1058 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1059 vchan_dma_desc_free_list(&sdmac->vc, &head);
ad0d92d7 1060 sdmac->context_loaded = false;
b8603d2a
LS
1061}
1062
1063static int sdma_disable_channel_async(struct dma_chan *chan)
1064{
1065 struct sdma_channel *sdmac = to_sdma_chan(chan);
1066
1067 sdma_disable_channel(chan);
1068
1069 if (sdmac->desc)
1070 schedule_work(&sdmac->terminate_worker);
7f3ff14b
JW
1071
1072 return 0;
1073}
1074
b8603d2a
LS
1075static void sdma_channel_synchronize(struct dma_chan *chan)
1076{
1077 struct sdma_channel *sdmac = to_sdma_chan(chan);
1078
1079 vchan_synchronize(&sdmac->vc);
1080
1081 flush_work(&sdmac->terminate_worker);
1082}
1083
8391ecf4
SW
1084static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
1085{
1086 struct sdma_engine *sdma = sdmac->sdma;
1087
1088 int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
1089 int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
1090
1091 set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
1092 set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
1093
1094 if (sdmac->event_id0 > 31)
1095 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
1096
1097 if (sdmac->event_id1 > 31)
1098 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
1099
1100 /*
1101 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
1102 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
1103 * r0(event_mask[1]) and r1(event_mask[0]).
1104 */
1105 if (lwml > hwml) {
1106 sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
1107 SDMA_WATERMARK_LEVEL_HWML);
1108 sdmac->watermark_level |= hwml;
1109 sdmac->watermark_level |= lwml << 16;
1110 swap(sdmac->event_mask[0], sdmac->event_mask[1]);
1111 }
1112
1113 if (sdmac->per_address2 >= sdma->spba_start_addr &&
1114 sdmac->per_address2 <= sdma->spba_end_addr)
1115 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
1116
1117 if (sdmac->per_address >= sdma->spba_start_addr &&
1118 sdmac->per_address <= sdma->spba_end_addr)
1119 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
1120
1121 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
1122}
1123
7b350ab0 1124static int sdma_config_channel(struct dma_chan *chan)
1ec1e82f 1125{
7b350ab0 1126 struct sdma_channel *sdmac = to_sdma_chan(chan);
1ec1e82f
SH
1127 int ret;
1128
7b350ab0 1129 sdma_disable_channel(chan);
1ec1e82f 1130
0bbc1413
RZ
1131 sdmac->event_mask[0] = 0;
1132 sdmac->event_mask[1] = 0;
1ec1e82f
SH
1133 sdmac->shp_addr = 0;
1134 sdmac->per_addr = 0;
1135
1ec1e82f
SH
1136 switch (sdmac->peripheral_type) {
1137 case IMX_DMATYPE_DSP:
1138 sdma_config_ownership(sdmac, false, true, true);
1139 break;
1140 case IMX_DMATYPE_MEMORY:
1141 sdma_config_ownership(sdmac, false, true, false);
1142 break;
1143 default:
1144 sdma_config_ownership(sdmac, true, true, false);
1145 break;
1146 }
1147
1148 sdma_get_pc(sdmac, sdmac->peripheral_type);
1149
1150 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
1151 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
1152 /* Handle multiple event channels differently */
1153 if (sdmac->event_id1) {
8391ecf4
SW
1154 if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
1155 sdmac->peripheral_type == IMX_DMATYPE_ASRC)
1156 sdma_set_watermarklevel_for_p2p(sdmac);
1157 } else
0bbc1413 1158 __set_bit(sdmac->event_id0, sdmac->event_mask);
8391ecf4 1159
1ec1e82f
SH
1160 /* Address */
1161 sdmac->shp_addr = sdmac->per_address;
8391ecf4 1162 sdmac->per_addr = sdmac->per_address2;
1ec1e82f
SH
1163 } else {
1164 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
1165 }
1166
1167 ret = sdma_load_context(sdmac);
1168
1169 return ret;
1170}
1171
1172static int sdma_set_channel_priority(struct sdma_channel *sdmac,
1173 unsigned int priority)
1174{
1175 struct sdma_engine *sdma = sdmac->sdma;
1176 int channel = sdmac->channel;
1177
1178 if (priority < MXC_SDMA_MIN_PRIORITY
1179 || priority > MXC_SDMA_MAX_PRIORITY) {
1180 return -EINVAL;
1181 }
1182
c4b56857 1183 writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
1ec1e82f
SH
1184
1185 return 0;
1186}
1187
57b772b8 1188static int sdma_request_channel0(struct sdma_engine *sdma)
1ec1e82f 1189{
1ec1e82f
SH
1190 int ret = -EBUSY;
1191
57b772b8
RG
1192 sdma->bd0 = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdma->bd0_phys,
1193 GFP_NOWAIT);
1194 if (!sdma->bd0) {
1ec1e82f
SH
1195 ret = -ENOMEM;
1196 goto out;
1197 }
1198
57b772b8
RG
1199 sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
1200 sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
1ec1e82f 1201
57b772b8 1202 sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
1ec1e82f
SH
1203 return 0;
1204out:
1205
1206 return ret;
1207}
1208
57b772b8
RG
1209
1210static int sdma_alloc_bd(struct sdma_desc *desc)
1ec1e82f 1211{
ebb853b1 1212 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
57b772b8 1213 int ret = 0;
1ec1e82f 1214
ebb853b1 1215 desc->bd = dma_zalloc_coherent(NULL, bd_size, &desc->bd_phys,
64068853 1216 GFP_NOWAIT);
57b772b8
RG
1217 if (!desc->bd) {
1218 ret = -ENOMEM;
1219 goto out;
1220 }
1221out:
1222 return ret;
1223}
1ec1e82f 1224
57b772b8
RG
1225static void sdma_free_bd(struct sdma_desc *desc)
1226{
ebb853b1
LS
1227 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1228
1229 dma_free_coherent(NULL, bd_size, desc->bd, desc->bd_phys);
57b772b8 1230}
1ec1e82f 1231
57b772b8
RG
1232static void sdma_desc_free(struct virt_dma_desc *vd)
1233{
1234 struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
1235
1236 sdma_free_bd(desc);
1237 kfree(desc);
1ec1e82f
SH
1238}
1239
1240static int sdma_alloc_chan_resources(struct dma_chan *chan)
1241{
1242 struct sdma_channel *sdmac = to_sdma_chan(chan);
1243 struct imx_dma_data *data = chan->private;
0f06c027 1244 struct imx_dma_data mem_data;
1ec1e82f
SH
1245 int prio, ret;
1246
0f06c027
RG
1247 /*
1248 * MEMCPY may never setup chan->private by filter function such as
1249 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
1250 * Please note in any other slave case, you have to setup chan->private
1251 * with 'struct imx_dma_data' in your own filter function if you want to
1252 * request dma channel by dma_request_channel() rather than
1253 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
1254 * to warn you to correct your filter function.
1255 */
1256 if (!data) {
1257 dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
1258 mem_data.priority = 2;
1259 mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
1260 mem_data.dma_request = 0;
1261 mem_data.dma_request2 = 0;
1262 data = &mem_data;
1263
1264 sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
1265 }
1ec1e82f
SH
1266
1267 switch (data->priority) {
1268 case DMA_PRIO_HIGH:
1269 prio = 3;
1270 break;
1271 case DMA_PRIO_MEDIUM:
1272 prio = 2;
1273 break;
1274 case DMA_PRIO_LOW:
1275 default:
1276 prio = 1;
1277 break;
1278 }
1279
1280 sdmac->peripheral_type = data->peripheral_type;
1281 sdmac->event_id0 = data->dma_request;
8391ecf4 1282 sdmac->event_id1 = data->dma_request2;
c2c744d3 1283
b93edcdd
FE
1284 ret = clk_enable(sdmac->sdma->clk_ipg);
1285 if (ret)
1286 return ret;
1287 ret = clk_enable(sdmac->sdma->clk_ahb);
1288 if (ret)
1289 goto disable_clk_ipg;
c2c744d3 1290
3bb5e7ca 1291 ret = sdma_set_channel_priority(sdmac, prio);
1ec1e82f 1292 if (ret)
b93edcdd 1293 goto disable_clk_ahb;
1ec1e82f 1294
1ec1e82f 1295 return 0;
b93edcdd
FE
1296
1297disable_clk_ahb:
1298 clk_disable(sdmac->sdma->clk_ahb);
1299disable_clk_ipg:
1300 clk_disable(sdmac->sdma->clk_ipg);
1301 return ret;
1ec1e82f
SH
1302}
1303
1304static void sdma_free_chan_resources(struct dma_chan *chan)
1305{
1306 struct sdma_channel *sdmac = to_sdma_chan(chan);
1307 struct sdma_engine *sdma = sdmac->sdma;
1308
b8603d2a
LS
1309 sdma_disable_channel_async(chan);
1310
1311 sdma_channel_synchronize(chan);
1ec1e82f
SH
1312
1313 if (sdmac->event_id0)
1314 sdma_event_disable(sdmac, sdmac->event_id0);
1315 if (sdmac->event_id1)
1316 sdma_event_disable(sdmac, sdmac->event_id1);
1317
1318 sdmac->event_id0 = 0;
1319 sdmac->event_id1 = 0;
1320
1321 sdma_set_channel_priority(sdmac, 0);
1322
7560e3f3
SH
1323 clk_disable(sdma->clk_ipg);
1324 clk_disable(sdma->clk_ahb);
1ec1e82f
SH
1325}
1326
21420841
RG
1327static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
1328 enum dma_transfer_direction direction, u32 bds)
1329{
1330 struct sdma_desc *desc;
1331
1332 desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
1333 if (!desc)
1334 goto err_out;
1335
1336 sdmac->status = DMA_IN_PROGRESS;
1337 sdmac->direction = direction;
1338 sdmac->flags = 0;
1339
1340 desc->chn_count = 0;
1341 desc->chn_real_count = 0;
1342 desc->buf_tail = 0;
1343 desc->buf_ptail = 0;
1344 desc->sdmac = sdmac;
1345 desc->num_bd = bds;
1346
1347 if (sdma_alloc_bd(desc))
1348 goto err_desc_out;
1349
0f06c027
RG
1350 /* No slave_config called in MEMCPY case, so do here */
1351 if (direction == DMA_MEM_TO_MEM)
1352 sdma_config_ownership(sdmac, false, true, false);
1353
21420841
RG
1354 if (sdma_load_context(sdmac))
1355 goto err_desc_out;
1356
1357 return desc;
1358
1359err_desc_out:
1360 kfree(desc);
1361err_out:
1362 return NULL;
1363}
1364
0f06c027
RG
1365static struct dma_async_tx_descriptor *sdma_prep_memcpy(
1366 struct dma_chan *chan, dma_addr_t dma_dst,
1367 dma_addr_t dma_src, size_t len, unsigned long flags)
1368{
1369 struct sdma_channel *sdmac = to_sdma_chan(chan);
1370 struct sdma_engine *sdma = sdmac->sdma;
1371 int channel = sdmac->channel;
1372 size_t count;
1373 int i = 0, param;
1374 struct sdma_buffer_descriptor *bd;
1375 struct sdma_desc *desc;
1376
1377 if (!chan || !len)
1378 return NULL;
1379
1380 dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
1381 &dma_src, &dma_dst, len, channel);
1382
1383 desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
1384 len / SDMA_BD_MAX_CNT + 1);
1385 if (!desc)
1386 return NULL;
1387
1388 do {
1389 count = min_t(size_t, len, SDMA_BD_MAX_CNT);
1390 bd = &desc->bd[i];
1391 bd->buffer_addr = dma_src;
1392 bd->ext_buffer_addr = dma_dst;
1393 bd->mode.count = count;
1394 desc->chn_count += count;
1395 bd->mode.command = 0;
1396
1397 dma_src += count;
1398 dma_dst += count;
1399 len -= count;
1400 i++;
1401
1402 param = BD_DONE | BD_EXTD | BD_CONT;
1403 /* last bd */
1404 if (!len) {
1405 param |= BD_INTR;
1406 param |= BD_LAST;
1407 param &= ~BD_CONT;
1408 }
1409
1410 dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
1411 i, count, bd->buffer_addr,
1412 param & BD_WRAP ? "wrap" : "",
1413 param & BD_INTR ? " intr" : "");
1414
1415 bd->mode.status = param;
1416 } while (len);
1417
1418 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1419}
1420
1ec1e82f
SH
1421static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
1422 struct dma_chan *chan, struct scatterlist *sgl,
db8196df 1423 unsigned int sg_len, enum dma_transfer_direction direction,
185ecb5f 1424 unsigned long flags, void *context)
1ec1e82f
SH
1425{
1426 struct sdma_channel *sdmac = to_sdma_chan(chan);
1427 struct sdma_engine *sdma = sdmac->sdma;
ad78b000 1428 int i, count;
23889c63 1429 int channel = sdmac->channel;
1ec1e82f 1430 struct scatterlist *sg;
57b772b8 1431 struct sdma_desc *desc;
1ec1e82f 1432
107d0644
VK
1433 sdma_config_write(chan, &sdmac->slave_config, direction);
1434
21420841 1435 desc = sdma_transfer_init(sdmac, direction, sg_len);
57b772b8
RG
1436 if (!desc)
1437 goto err_out;
1438
1ec1e82f
SH
1439 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
1440 sg_len, channel);
1441
1ec1e82f 1442 for_each_sg(sgl, sg, sg_len, i) {
76c33d27 1443 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1ec1e82f
SH
1444 int param;
1445
d2f5c276 1446 bd->buffer_addr = sg->dma_address;
1ec1e82f 1447
fdaf9c4b 1448 count = sg_dma_len(sg);
1ec1e82f 1449
4a6b2e8a 1450 if (count > SDMA_BD_MAX_CNT) {
1ec1e82f 1451 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
4a6b2e8a 1452 channel, count, SDMA_BD_MAX_CNT);
57b772b8 1453 goto err_bd_out;
1ec1e82f
SH
1454 }
1455
1456 bd->mode.count = count;
76c33d27 1457 desc->chn_count += count;
1ec1e82f 1458
ad78b000 1459 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
57b772b8 1460 goto err_bd_out;
1fa81c27
SH
1461
1462 switch (sdmac->word_size) {
1463 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1ec1e82f 1464 bd->mode.command = 0;
1fa81c27 1465 if (count & 3 || sg->dma_address & 3)
57b772b8 1466 goto err_bd_out;
1fa81c27
SH
1467 break;
1468 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1469 bd->mode.command = 2;
1470 if (count & 1 || sg->dma_address & 1)
57b772b8 1471 goto err_bd_out;
1fa81c27
SH
1472 break;
1473 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1474 bd->mode.command = 1;
1475 break;
1476 default:
57b772b8 1477 goto err_bd_out;
1fa81c27 1478 }
1ec1e82f
SH
1479
1480 param = BD_DONE | BD_EXTD | BD_CONT;
1481
341b9419 1482 if (i + 1 == sg_len) {
1ec1e82f 1483 param |= BD_INTR;
341b9419
SG
1484 param |= BD_LAST;
1485 param &= ~BD_CONT;
1ec1e82f
SH
1486 }
1487
c3cc74b2
OJ
1488 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1489 i, count, (u64)sg->dma_address,
1ec1e82f
SH
1490 param & BD_WRAP ? "wrap" : "",
1491 param & BD_INTR ? " intr" : "");
1492
1493 bd->mode.status = param;
1494 }
1495
57b772b8
RG
1496 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1497err_bd_out:
1498 sdma_free_bd(desc);
1499 kfree(desc);
1ec1e82f 1500err_out:
4b2ce9dd 1501 sdmac->status = DMA_ERROR;
1ec1e82f
SH
1502 return NULL;
1503}
1504
1505static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1506 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
185ecb5f 1507 size_t period_len, enum dma_transfer_direction direction,
31c1e5a1 1508 unsigned long flags)
1ec1e82f
SH
1509{
1510 struct sdma_channel *sdmac = to_sdma_chan(chan);
1511 struct sdma_engine *sdma = sdmac->sdma;
1512 int num_periods = buf_len / period_len;
23889c63 1513 int channel = sdmac->channel;
21420841 1514 int i = 0, buf = 0;
57b772b8 1515 struct sdma_desc *desc;
1ec1e82f
SH
1516
1517 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1518
107d0644
VK
1519 sdma_config_write(chan, &sdmac->slave_config, direction);
1520
21420841 1521 desc = sdma_transfer_init(sdmac, direction, num_periods);
57b772b8
RG
1522 if (!desc)
1523 goto err_out;
1524
76c33d27 1525 desc->period_len = period_len;
8e2e27c7 1526
1ec1e82f 1527 sdmac->flags |= IMX_DMA_SG_LOOP;
1ec1e82f 1528
4a6b2e8a 1529 if (period_len > SDMA_BD_MAX_CNT) {
ba6ab3b3 1530 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
4a6b2e8a 1531 channel, period_len, SDMA_BD_MAX_CNT);
57b772b8 1532 goto err_bd_out;
1ec1e82f
SH
1533 }
1534
1535 while (buf < buf_len) {
76c33d27 1536 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1ec1e82f
SH
1537 int param;
1538
1539 bd->buffer_addr = dma_addr;
1540
1541 bd->mode.count = period_len;
1542
1543 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
57b772b8 1544 goto err_bd_out;
1ec1e82f
SH
1545 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1546 bd->mode.command = 0;
1547 else
1548 bd->mode.command = sdmac->word_size;
1549
1550 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1551 if (i + 1 == num_periods)
1552 param |= BD_WRAP;
1553
ba6ab3b3 1554 dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
c3cc74b2 1555 i, period_len, (u64)dma_addr,
1ec1e82f
SH
1556 param & BD_WRAP ? "wrap" : "",
1557 param & BD_INTR ? " intr" : "");
1558
1559 bd->mode.status = param;
1560
1561 dma_addr += period_len;
1562 buf += period_len;
1563
1564 i++;
1565 }
1566
57b772b8
RG
1567 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1568err_bd_out:
1569 sdma_free_bd(desc);
1570 kfree(desc);
1ec1e82f
SH
1571err_out:
1572 sdmac->status = DMA_ERROR;
1573 return NULL;
1574}
1575
107d0644
VK
1576static int sdma_config_write(struct dma_chan *chan,
1577 struct dma_slave_config *dmaengine_cfg,
1578 enum dma_transfer_direction direction)
1ec1e82f
SH
1579{
1580 struct sdma_channel *sdmac = to_sdma_chan(chan);
1ec1e82f 1581
107d0644 1582 if (direction == DMA_DEV_TO_MEM) {
7b350ab0
MR
1583 sdmac->per_address = dmaengine_cfg->src_addr;
1584 sdmac->watermark_level = dmaengine_cfg->src_maxburst *
1585 dmaengine_cfg->src_addr_width;
1586 sdmac->word_size = dmaengine_cfg->src_addr_width;
107d0644 1587 } else if (direction == DMA_DEV_TO_DEV) {
8391ecf4
SW
1588 sdmac->per_address2 = dmaengine_cfg->src_addr;
1589 sdmac->per_address = dmaengine_cfg->dst_addr;
1590 sdmac->watermark_level = dmaengine_cfg->src_maxburst &
1591 SDMA_WATERMARK_LEVEL_LWML;
1592 sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
1593 SDMA_WATERMARK_LEVEL_HWML;
1594 sdmac->word_size = dmaengine_cfg->dst_addr_width;
7b350ab0
MR
1595 } else {
1596 sdmac->per_address = dmaengine_cfg->dst_addr;
1597 sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
1598 dmaengine_cfg->dst_addr_width;
1599 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1600 }
107d0644 1601 sdmac->direction = direction;
7b350ab0 1602 return sdma_config_channel(chan);
1ec1e82f
SH
1603}
1604
107d0644
VK
1605static int sdma_config(struct dma_chan *chan,
1606 struct dma_slave_config *dmaengine_cfg)
1607{
1608 struct sdma_channel *sdmac = to_sdma_chan(chan);
1609
1610 memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1611
1612 /* Set ENBLn earlier to make sure dma request triggered after that */
1613 if (sdmac->event_id0) {
1614 if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1615 return -EINVAL;
1616 sdma_event_enable(sdmac, sdmac->event_id0);
1617 }
1618
1619 if (sdmac->event_id1) {
1620 if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1621 return -EINVAL;
1622 sdma_event_enable(sdmac, sdmac->event_id1);
1623 }
1624
1625 return 0;
1626}
1627
1ec1e82f 1628static enum dma_status sdma_tx_status(struct dma_chan *chan,
e8e3a790
AS
1629 dma_cookie_t cookie,
1630 struct dma_tx_state *txstate)
1ec1e82f
SH
1631{
1632 struct sdma_channel *sdmac = to_sdma_chan(chan);
57b772b8 1633 struct sdma_desc *desc;
d1a792f3 1634 u32 residue;
57b772b8
RG
1635 struct virt_dma_desc *vd;
1636 enum dma_status ret;
1637 unsigned long flags;
d1a792f3 1638
57b772b8
RG
1639 ret = dma_cookie_status(chan, cookie, txstate);
1640 if (ret == DMA_COMPLETE || !txstate)
1641 return ret;
1642
1643 spin_lock_irqsave(&sdmac->vc.lock, flags);
1644 vd = vchan_find_desc(&sdmac->vc, cookie);
1645 if (vd) {
1646 desc = to_sdma_desc(&vd->tx);
1647 if (sdmac->flags & IMX_DMA_SG_LOOP)
1648 residue = (desc->num_bd - desc->buf_ptail) *
1649 desc->period_len - desc->chn_real_count;
1650 else
1651 residue = desc->chn_count - desc->chn_real_count;
1652 } else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) {
1653 residue = sdmac->desc->chn_count - sdmac->desc->chn_real_count;
1654 } else {
1655 residue = 0;
1656 }
1657 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1ec1e82f 1658
e8e3a790 1659 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
d1a792f3 1660 residue);
1ec1e82f 1661
8a965911 1662 return sdmac->status;
1ec1e82f
SH
1663}
1664
1665static void sdma_issue_pending(struct dma_chan *chan)
1666{
2b4f130e 1667 struct sdma_channel *sdmac = to_sdma_chan(chan);
57b772b8 1668 unsigned long flags;
2b4f130e 1669
57b772b8
RG
1670 spin_lock_irqsave(&sdmac->vc.lock, flags);
1671 if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
1672 sdma_start_desc(sdmac);
1673 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1ec1e82f
SH
1674}
1675
5b28aa31 1676#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
cd72b846 1677#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
a572460b 1678#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
b7d2648a 1679#define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42
5b28aa31
SH
1680
1681static void sdma_add_scripts(struct sdma_engine *sdma,
1682 const struct sdma_script_start_addrs *addr)
1683{
1684 s32 *addr_arr = (u32 *)addr;
1685 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1686 int i;
1687
70dabaed
NC
1688 /* use the default firmware in ROM if missing external firmware */
1689 if (!sdma->script_number)
1690 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1691
cd72b846 1692 for (i = 0; i < sdma->script_number; i++)
5b28aa31
SH
1693 if (addr_arr[i] > 0)
1694 saddr_arr[i] = addr_arr[i];
1695}
1696
7b4b88e0 1697static void sdma_load_firmware(const struct firmware *fw, void *context)
5b28aa31 1698{
7b4b88e0 1699 struct sdma_engine *sdma = context;
5b28aa31 1700 const struct sdma_firmware_header *header;
5b28aa31
SH
1701 const struct sdma_script_start_addrs *addr;
1702 unsigned short *ram_code;
1703
7b4b88e0 1704 if (!fw) {
0f927a11
SH
1705 dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
1706 /* In this case we just use the ROM firmware. */
7b4b88e0
SH
1707 return;
1708 }
5b28aa31
SH
1709
1710 if (fw->size < sizeof(*header))
1711 goto err_firmware;
1712
1713 header = (struct sdma_firmware_header *)fw->data;
1714
1715 if (header->magic != SDMA_FIRMWARE_MAGIC)
1716 goto err_firmware;
1717 if (header->ram_code_start + header->ram_code_size > fw->size)
1718 goto err_firmware;
cd72b846 1719 switch (header->version_major) {
681d15ec
AV
1720 case 1:
1721 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1722 break;
1723 case 2:
1724 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1725 break;
a572460b
FE
1726 case 3:
1727 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1728 break;
b7d2648a
FE
1729 case 4:
1730 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1731 break;
681d15ec
AV
1732 default:
1733 dev_err(sdma->dev, "unknown firmware version\n");
1734 goto err_firmware;
cd72b846 1735 }
5b28aa31
SH
1736
1737 addr = (void *)header + header->script_addrs_start;
1738 ram_code = (void *)header + header->ram_code_start;
1739
7560e3f3
SH
1740 clk_enable(sdma->clk_ipg);
1741 clk_enable(sdma->clk_ahb);
5b28aa31
SH
1742 /* download the RAM image for SDMA */
1743 sdma_load_script(sdma, ram_code,
1744 header->ram_code_size,
6866fd3b 1745 addr->ram_code_start_addr);
7560e3f3
SH
1746 clk_disable(sdma->clk_ipg);
1747 clk_disable(sdma->clk_ahb);
5b28aa31
SH
1748
1749 sdma_add_scripts(sdma, addr);
1750
1751 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1752 header->version_major,
1753 header->version_minor);
1754
1755err_firmware:
1756 release_firmware(fw);
7b4b88e0
SH
1757}
1758
d078cd1b
ZW
1759#define EVENT_REMAP_CELLS 3
1760
29f493da 1761static int sdma_event_remap(struct sdma_engine *sdma)
d078cd1b
ZW
1762{
1763 struct device_node *np = sdma->dev->of_node;
1764 struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1765 struct property *event_remap;
1766 struct regmap *gpr;
1767 char propname[] = "fsl,sdma-event-remap";
1768 u32 reg, val, shift, num_map, i;
1769 int ret = 0;
1770
1771 if (IS_ERR(np) || IS_ERR(gpr_np))
1772 goto out;
1773
1774 event_remap = of_find_property(np, propname, NULL);
1775 num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1776 if (!num_map) {
ce078af7 1777 dev_dbg(sdma->dev, "no event needs to be remapped\n");
d078cd1b
ZW
1778 goto out;
1779 } else if (num_map % EVENT_REMAP_CELLS) {
1780 dev_err(sdma->dev, "the property %s must modulo %d\n",
1781 propname, EVENT_REMAP_CELLS);
1782 ret = -EINVAL;
1783 goto out;
1784 }
1785
1786 gpr = syscon_node_to_regmap(gpr_np);
1787 if (IS_ERR(gpr)) {
1788 dev_err(sdma->dev, "failed to get gpr regmap\n");
1789 ret = PTR_ERR(gpr);
1790 goto out;
1791 }
1792
1793 for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1794 ret = of_property_read_u32_index(np, propname, i, &reg);
1795 if (ret) {
1796 dev_err(sdma->dev, "failed to read property %s index %d\n",
1797 propname, i);
1798 goto out;
1799 }
1800
1801 ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1802 if (ret) {
1803 dev_err(sdma->dev, "failed to read property %s index %d\n",
1804 propname, i + 1);
1805 goto out;
1806 }
1807
1808 ret = of_property_read_u32_index(np, propname, i + 2, &val);
1809 if (ret) {
1810 dev_err(sdma->dev, "failed to read property %s index %d\n",
1811 propname, i + 2);
1812 goto out;
1813 }
1814
1815 regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1816 }
1817
1818out:
1819 if (!IS_ERR(gpr_np))
1820 of_node_put(gpr_np);
1821
1822 return ret;
1823}
1824
fe6cf289 1825static int sdma_get_firmware(struct sdma_engine *sdma,
7b4b88e0
SH
1826 const char *fw_name)
1827{
1828 int ret;
1829
1830 ret = request_firmware_nowait(THIS_MODULE,
1831 FW_ACTION_HOTPLUG, fw_name, sdma->dev,
1832 GFP_KERNEL, sdma, sdma_load_firmware);
5b28aa31
SH
1833
1834 return ret;
1835}
1836
19bfc772 1837static int sdma_init(struct sdma_engine *sdma)
1ec1e82f
SH
1838{
1839 int i, ret;
1840 dma_addr_t ccb_phys;
1841
b93edcdd
FE
1842 ret = clk_enable(sdma->clk_ipg);
1843 if (ret)
1844 return ret;
1845 ret = clk_enable(sdma->clk_ahb);
1846 if (ret)
1847 goto disable_clk_ipg;
1ec1e82f
SH
1848
1849 /* Be sure SDMA has not started yet */
c4b56857 1850 writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
1ec1e82f
SH
1851
1852 sdma->channel_control = dma_alloc_coherent(NULL,
1853 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1854 sizeof(struct sdma_context_data),
1855 &ccb_phys, GFP_KERNEL);
1856
1857 if (!sdma->channel_control) {
1858 ret = -ENOMEM;
1859 goto err_dma_alloc;
1860 }
1861
1862 sdma->context = (void *)sdma->channel_control +
1863 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1864 sdma->context_phys = ccb_phys +
1865 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1866
1867 /* Zero-out the CCB structures array just allocated */
1868 memset(sdma->channel_control, 0,
1869 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1870
1871 /* disable all channels */
17bba72f 1872 for (i = 0; i < sdma->drvdata->num_events; i++)
c4b56857 1873 writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
1ec1e82f
SH
1874
1875 /* All channels have priority 0 */
1876 for (i = 0; i < MAX_DMA_CHANNELS; i++)
c4b56857 1877 writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1ec1e82f 1878
57b772b8 1879 ret = sdma_request_channel0(sdma);
1ec1e82f
SH
1880 if (ret)
1881 goto err_dma_alloc;
1882
1883 sdma_config_ownership(&sdma->channel[0], false, true, false);
1884
1885 /* Set Command Channel (Channel Zero) */
c4b56857 1886 writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
1ec1e82f
SH
1887
1888 /* Set bits of CONFIG register but with static context switching */
1889 /* FIXME: Check whether to set ACR bit depending on clock ratios */
c4b56857 1890 writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
1ec1e82f 1891
c4b56857 1892 writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1ec1e82f 1893
1ec1e82f
SH
1894 /* Initializes channel's priorities */
1895 sdma_set_channel_priority(&sdma->channel[0], 7);
1896
7560e3f3
SH
1897 clk_disable(sdma->clk_ipg);
1898 clk_disable(sdma->clk_ahb);
1ec1e82f
SH
1899
1900 return 0;
1901
1902err_dma_alloc:
7560e3f3 1903 clk_disable(sdma->clk_ahb);
b93edcdd
FE
1904disable_clk_ipg:
1905 clk_disable(sdma->clk_ipg);
1ec1e82f
SH
1906 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1907 return ret;
1908}
1909
9479e17c
SG
1910static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
1911{
0b351865 1912 struct sdma_channel *sdmac = to_sdma_chan(chan);
9479e17c
SG
1913 struct imx_dma_data *data = fn_param;
1914
1915 if (!imx_dma_is_general_purpose(chan))
1916 return false;
1917
0b351865
NC
1918 sdmac->data = *data;
1919 chan->private = &sdmac->data;
9479e17c
SG
1920
1921 return true;
1922}
1923
1924static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
1925 struct of_dma *ofdma)
1926{
1927 struct sdma_engine *sdma = ofdma->of_dma_data;
1928 dma_cap_mask_t mask = sdma->dma_device.cap_mask;
1929 struct imx_dma_data data;
1930
1931 if (dma_spec->args_count != 3)
1932 return NULL;
1933
1934 data.dma_request = dma_spec->args[0];
1935 data.peripheral_type = dma_spec->args[1];
1936 data.priority = dma_spec->args[2];
8391ecf4
SW
1937 /*
1938 * init dma_request2 to zero, which is not used by the dts.
1939 * For P2P, dma_request2 is init from dma_request_channel(),
1940 * chan->private will point to the imx_dma_data, and in
1941 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
1942 * be set to sdmac->event_id1.
1943 */
1944 data.dma_request2 = 0;
9479e17c
SG
1945
1946 return dma_request_channel(mask, sdma_filter_fn, &data);
1947}
1948
e34b731f 1949static int sdma_probe(struct platform_device *pdev)
1ec1e82f 1950{
580975d7
SG
1951 const struct of_device_id *of_id =
1952 of_match_device(sdma_dt_ids, &pdev->dev);
1953 struct device_node *np = pdev->dev.of_node;
8391ecf4 1954 struct device_node *spba_bus;
580975d7 1955 const char *fw_name;
1ec1e82f 1956 int ret;
1ec1e82f 1957 int irq;
1ec1e82f 1958 struct resource *iores;
8391ecf4 1959 struct resource spba_res;
d4adcc01 1960 struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
1ec1e82f 1961 int i;
1ec1e82f 1962 struct sdma_engine *sdma;
36e2f21a 1963 s32 *saddr_arr;
17bba72f
SH
1964 const struct sdma_driver_data *drvdata = NULL;
1965
1966 if (of_id)
1967 drvdata = of_id->data;
1968 else if (pdev->id_entry)
1969 drvdata = (void *)pdev->id_entry->driver_data;
1970
1971 if (!drvdata) {
1972 dev_err(&pdev->dev, "unable to find driver data\n");
1973 return -EINVAL;
1974 }
1ec1e82f 1975
42536b9f
PR
1976 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1977 if (ret)
1978 return ret;
1979
7f24e0ee 1980 sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
1ec1e82f
SH
1981 if (!sdma)
1982 return -ENOMEM;
1983
2ccaef05 1984 spin_lock_init(&sdma->channel_0_lock);
73eab978 1985
1ec1e82f 1986 sdma->dev = &pdev->dev;
17bba72f 1987 sdma->drvdata = drvdata;
1ec1e82f 1988
1ec1e82f 1989 irq = platform_get_irq(pdev, 0);
7f24e0ee 1990 if (irq < 0)
63c72e02 1991 return irq;
1ec1e82f 1992
7f24e0ee
FE
1993 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1994 sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
1995 if (IS_ERR(sdma->regs))
1996 return PTR_ERR(sdma->regs);
1ec1e82f 1997
7560e3f3 1998 sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
7f24e0ee
FE
1999 if (IS_ERR(sdma->clk_ipg))
2000 return PTR_ERR(sdma->clk_ipg);
1ec1e82f 2001
7560e3f3 2002 sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
7f24e0ee
FE
2003 if (IS_ERR(sdma->clk_ahb))
2004 return PTR_ERR(sdma->clk_ahb);
7560e3f3 2005
fb9caf37
AY
2006 ret = clk_prepare(sdma->clk_ipg);
2007 if (ret)
2008 return ret;
2009
2010 ret = clk_prepare(sdma->clk_ahb);
2011 if (ret)
2012 goto err_clk;
7560e3f3 2013
7f24e0ee
FE
2014 ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
2015 sdma);
1ec1e82f 2016 if (ret)
fb9caf37 2017 goto err_irq;
1ec1e82f 2018
5bb9dbb5
VK
2019 sdma->irq = irq;
2020
5b28aa31 2021 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
fb9caf37
AY
2022 if (!sdma->script_addrs) {
2023 ret = -ENOMEM;
2024 goto err_irq;
2025 }
1ec1e82f 2026
36e2f21a
SH
2027 /* initially no scripts available */
2028 saddr_arr = (s32 *)sdma->script_addrs;
2029 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
2030 saddr_arr[i] = -EINVAL;
2031
7214a8b1
SH
2032 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
2033 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
0f06c027 2034 dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
7214a8b1 2035
1ec1e82f
SH
2036 INIT_LIST_HEAD(&sdma->dma_device.channels);
2037 /* Initialize channel parameters */
2038 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2039 struct sdma_channel *sdmac = &sdma->channel[i];
2040
2041 sdmac->sdma = sdma;
1ec1e82f 2042
1ec1e82f 2043 sdmac->channel = i;
57b772b8 2044 sdmac->vc.desc_free = sdma_desc_free;
b8603d2a
LS
2045 INIT_WORK(&sdmac->terminate_worker,
2046 sdma_channel_terminate_work);
23889c63
SH
2047 /*
2048 * Add the channel to the DMAC list. Do not add channel 0 though
2049 * because we need it internally in the SDMA driver. This also means
2050 * that channel 0 in dmaengine counting matches sdma channel 1.
2051 */
2052 if (i)
57b772b8 2053 vchan_init(&sdmac->vc, &sdma->dma_device);
1ec1e82f
SH
2054 }
2055
5b28aa31 2056 ret = sdma_init(sdma);
1ec1e82f
SH
2057 if (ret)
2058 goto err_init;
2059
d078cd1b
ZW
2060 ret = sdma_event_remap(sdma);
2061 if (ret)
2062 goto err_init;
2063
dcfec3c0
SH
2064 if (sdma->drvdata->script_addrs)
2065 sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
580975d7 2066 if (pdata && pdata->script_addrs)
5b28aa31
SH
2067 sdma_add_scripts(sdma, pdata->script_addrs);
2068
580975d7 2069 if (pdata) {
6d0d7e2d
FE
2070 ret = sdma_get_firmware(sdma, pdata->fw_name);
2071 if (ret)
ad1122e5 2072 dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
580975d7
SG
2073 } else {
2074 /*
2075 * Because that device tree does not encode ROM script address,
2076 * the RAM script in firmware is mandatory for device tree
2077 * probe, otherwise it fails.
2078 */
2079 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
2080 &fw_name);
6602b0dd 2081 if (ret)
ad1122e5 2082 dev_warn(&pdev->dev, "failed to get firmware name\n");
6602b0dd
FE
2083 else {
2084 ret = sdma_get_firmware(sdma, fw_name);
2085 if (ret)
ad1122e5 2086 dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
580975d7
SG
2087 }
2088 }
5b28aa31 2089
1ec1e82f
SH
2090 sdma->dma_device.dev = &pdev->dev;
2091
2092 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
2093 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
2094 sdma->dma_device.device_tx_status = sdma_tx_status;
2095 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
2096 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
7b350ab0 2097 sdma->dma_device.device_config = sdma_config;
b8603d2a
LS
2098 sdma->dma_device.device_terminate_all = sdma_disable_channel_async;
2099 sdma->dma_device.device_synchronize = sdma_channel_synchronize;
f9d4a398
NC
2100 sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2101 sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2102 sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
6f3125ce 2103 sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
0f06c027 2104 sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
1ec1e82f 2105 sdma->dma_device.device_issue_pending = sdma_issue_pending;
b9b3f82f 2106 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
4a6b2e8a 2107 dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
1ec1e82f 2108
23e11811
VR
2109 platform_set_drvdata(pdev, sdma);
2110
1ec1e82f
SH
2111 ret = dma_async_device_register(&sdma->dma_device);
2112 if (ret) {
2113 dev_err(&pdev->dev, "unable to register\n");
2114 goto err_init;
2115 }
2116
9479e17c
SG
2117 if (np) {
2118 ret = of_dma_controller_register(np, sdma_xlate, sdma);
2119 if (ret) {
2120 dev_err(&pdev->dev, "failed to register controller\n");
2121 goto err_register;
2122 }
8391ecf4
SW
2123
2124 spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
2125 ret = of_address_to_resource(spba_bus, 0, &spba_res);
2126 if (!ret) {
2127 sdma->spba_start_addr = spba_res.start;
2128 sdma->spba_end_addr = spba_res.end;
2129 }
2130 of_node_put(spba_bus);
9479e17c
SG
2131 }
2132
1ec1e82f
SH
2133 return 0;
2134
9479e17c
SG
2135err_register:
2136 dma_async_device_unregister(&sdma->dma_device);
1ec1e82f
SH
2137err_init:
2138 kfree(sdma->script_addrs);
fb9caf37
AY
2139err_irq:
2140 clk_unprepare(sdma->clk_ahb);
2141err_clk:
2142 clk_unprepare(sdma->clk_ipg);
939fd4f0 2143 return ret;
1ec1e82f
SH
2144}
2145
1d1bbd30 2146static int sdma_remove(struct platform_device *pdev)
1ec1e82f 2147{
23e11811 2148 struct sdma_engine *sdma = platform_get_drvdata(pdev);
c12fe497 2149 int i;
23e11811 2150
5bb9dbb5 2151 devm_free_irq(&pdev->dev, sdma->irq, sdma);
23e11811
VR
2152 dma_async_device_unregister(&sdma->dma_device);
2153 kfree(sdma->script_addrs);
fb9caf37
AY
2154 clk_unprepare(sdma->clk_ahb);
2155 clk_unprepare(sdma->clk_ipg);
c12fe497
VR
2156 /* Kill the tasklet */
2157 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2158 struct sdma_channel *sdmac = &sdma->channel[i];
2159
57b772b8
RG
2160 tasklet_kill(&sdmac->vc.task);
2161 sdma_free_chan_resources(&sdmac->vc.chan);
c12fe497 2162 }
23e11811
VR
2163
2164 platform_set_drvdata(pdev, NULL);
23e11811 2165 return 0;
1ec1e82f
SH
2166}
2167
2168static struct platform_driver sdma_driver = {
2169 .driver = {
2170 .name = "imx-sdma",
580975d7 2171 .of_match_table = sdma_dt_ids,
1ec1e82f 2172 },
62550cd7 2173 .id_table = sdma_devtypes,
1d1bbd30 2174 .remove = sdma_remove,
23e11811 2175 .probe = sdma_probe,
1ec1e82f
SH
2176};
2177
23e11811 2178module_platform_driver(sdma_driver);
1ec1e82f
SH
2179
2180MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
2181MODULE_DESCRIPTION("i.MX SDMA driver");
c0879342
NC
2182#if IS_ENABLED(CONFIG_SOC_IMX6Q)
2183MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2184#endif
2185#if IS_ENABLED(CONFIG_SOC_IMX7D)
2186MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2187#endif
1ec1e82f 2188MODULE_LICENSE("GPL");