1 // SPDX-License-Identifier: GPL-2.0+
3 // drivers/dma/imx-sdma.c
5 // This file contains a driver for the Freescale Smart DMA engine
7 // Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
9 // Based on code from Freescale:
11 // Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
13 #include <linux/init.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/bitfield.h>
18 #include <linux/bitops.h>
20 #include <linux/interrupt.h>
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/sched.h>
24 #include <linux/semaphore.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/firmware.h>
29 #include <linux/slab.h>
30 #include <linux/platform_device.h>
31 #include <linux/dmaengine.h>
33 #include <linux/of_address.h>
34 #include <linux/of_device.h>
35 #include <linux/of_dma.h>
36 #include <linux/workqueue.h>
39 #include <linux/dma/imx-dma.h>
40 #include <linux/regmap.h>
41 #include <linux/mfd/syscon.h>
42 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
44 #include "dmaengine.h"
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
74 #define SDMA_CHNENBL0_IMX35 0x200
75 #define SDMA_CHNENBL0_IMX31 0x080
76 #define SDMA_CHNPRI_0 0x100
77 #define SDMA_DONE0_CONFIG 0x1000
80 * Buffer descriptor status values.
91 * Data Node descriptor status values.
93 #define DND_END_OF_FRAME 0x80
94 #define DND_END_OF_XFER 0x40
96 #define DND_UNUSED 0x01
99 * IPCV2 descriptor status values.
101 #define BD_IPCV2_END_OF_FRAME 0x40
103 #define IPCV2_MAX_NODES 50
105 * Error bit set in the CCB status field by the SDMA,
106 * in setbd routine, in case of a transfer error
108 #define DATA_ERROR 0x10000000
111 * Buffer descriptor commands.
116 #define C0_SETCTX 0x07
117 #define C0_GETCTX 0x03
118 #define C0_SETDM 0x01
119 #define C0_SETPM 0x04
120 #define C0_GETDM 0x02
121 #define C0_GETPM 0x08
123 * Change endianness indicator in the BD command field
125 #define CHANGE_ENDIANNESS 0x80
128 * p_2_p watermark_level description
129 * Bits Name Description
130 * 0-7 Lower WML Lower watermark level
131 * 8 PS 1: Pad Swallowing
132 * 0: No Pad Swallowing
135 * 10 SPDIF If this bit is set both source
136 * and destination are on SPBA
137 * 11 Source Bit(SP) 1: Source on SPBA
139 * 12 Destination Bit(DP) 1: Destination on SPBA
140 * 0: Destination on AIPS
141 * 13-15 --------- MUST BE 0
142 * 16-23 Higher WML HWML
143 * 24-27 N Total number of samples after
144 * which Pad adding/Swallowing
145 * must be done. It must be odd.
146 * 28 Lower WML Event(LWE) SDMA events reg to check for
148 * 0: LWE in EVENTS register
149 * 1: LWE in EVENTS2 register
150 * 29 Higher WML Event(HWE) SDMA events reg to check for
152 * 0: HWE in EVENTS register
153 * 1: HWE in EVENTS2 register
154 * 30 --------- MUST BE 0
155 * 31 CONT 1: Amount of samples to be
156 * transferred is unknown and
157 * script will keep on
158 * transferring samples as long as
159 * both events are detected and
160 * script must be manually stopped
162 * 0: The amount of samples to be
163 * transferred is equal to the
164 * count field of mode word
166 #define SDMA_WATERMARK_LEVEL_LWML 0xFF
167 #define SDMA_WATERMARK_LEVEL_PS BIT(8)
168 #define SDMA_WATERMARK_LEVEL_PA BIT(9)
169 #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
170 #define SDMA_WATERMARK_LEVEL_SP BIT(11)
171 #define SDMA_WATERMARK_LEVEL_DP BIT(12)
172 #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
173 #define SDMA_WATERMARK_LEVEL_LWE BIT(28)
174 #define SDMA_WATERMARK_LEVEL_HWE BIT(29)
175 #define SDMA_WATERMARK_LEVEL_CONT BIT(31)
177 #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
178 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
179 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
181 #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \
182 BIT(DMA_MEM_TO_DEV) | \
185 #define SDMA_WATERMARK_LEVEL_N_FIFOS GENMASK(15, 12)
186 #define SDMA_WATERMARK_LEVEL_SW_DONE BIT(23)
188 #define SDMA_DONE0_CONFIG_DONE_SEL BIT(7)
189 #define SDMA_DONE0_CONFIG_DONE_DIS BIT(6)
192 * struct sdma_script_start_addrs - SDMA script start pointers
194 * start addresses of the different functions in the physical
195 * address space of the SDMA engine.
197 struct sdma_script_start_addrs {
200 s32 ap_2_ap_fixed_addr;
202 s32 loopback_on_dsp_side_addr;
203 s32 mcu_interrupt_only_addr;
213 s32 uartsh_2_per_addr;
214 s32 uartsh_2_mcu_addr;
225 s32 spdif_2_mcu_addr;
226 s32 mcu_2_spdif_addr;
228 s32 ext_mem_2_ipu_addr;
229 s32 descrambler_addr;
232 s32 ram_code_start_addr;
233 /* End of v1 array */
234 s32 mcu_2_ssish_addr;
235 s32 ssish_2_mcu_addr;
237 /* End of v2 array */
238 s32 zcanfd_2_mcu_addr;
239 s32 zqspi_2_mcu_addr;
240 s32 mcu_2_ecspi_addr;
243 s32 uart_2_mcu_rom_addr;
244 s32 uartsh_2_mcu_rom_addr;
245 /* End of v3 array */
246 s32 mcu_2_zqspi_addr;
247 /* End of v4 array */
251 * Mode/Count of data node descriptors - IPCv2
253 struct sdma_mode_count {
254 #define SDMA_BD_MAX_CNT 0xffff
255 u32 count : 16; /* size of the buffer pointed by this BD */
256 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
257 u32 command : 8; /* command mostly used for channel 0 */
263 struct sdma_buffer_descriptor {
264 struct sdma_mode_count mode;
265 u32 buffer_addr; /* address of the buffer described */
266 u32 ext_buffer_addr; /* extended buffer address */
267 } __attribute__ ((packed));
270 * struct sdma_channel_control - Channel control Block
272 * @current_bd_ptr: current buffer descriptor processed
273 * @base_bd_ptr: first element of buffer descriptor array
274 * @unused: padding. The SDMA engine expects an array of 128 byte
277 struct sdma_channel_control {
281 } __attribute__ ((packed));
284 * struct sdma_state_registers - SDMA context for a channel
286 * @pc: program counter
288 * @t: test bit: status of arithmetic & test instruction
289 * @rpc: return program counter
291 * @sf: source fault while loading data
292 * @spc: loop start program counter
294 * @df: destination fault while storing data
295 * @epc: loop end program counter
298 struct sdma_state_registers {
310 } __attribute__ ((packed));
313 * struct sdma_context_data - sdma context specific to a channel
315 * @channel_state: channel state bits
316 * @gReg: general registers
317 * @mda: burst dma destination address register
318 * @msa: burst dma source address register
319 * @ms: burst dma status register
320 * @md: burst dma data register
321 * @pda: peripheral dma destination address register
322 * @psa: peripheral dma source address register
323 * @ps: peripheral dma status register
324 * @pd: peripheral dma data register
325 * @ca: CRC polynomial register
326 * @cs: CRC accumulator register
327 * @dda: dedicated core destination address register
328 * @dsa: dedicated core source address register
329 * @ds: dedicated core status register
330 * @dd: dedicated core data register
331 * @scratch0: 1st word of dedicated ram for context switch
332 * @scratch1: 2nd word of dedicated ram for context switch
333 * @scratch2: 3rd word of dedicated ram for context switch
334 * @scratch3: 4th word of dedicated ram for context switch
335 * @scratch4: 5th word of dedicated ram for context switch
336 * @scratch5: 6th word of dedicated ram for context switch
337 * @scratch6: 7th word of dedicated ram for context switch
338 * @scratch7: 8th word of dedicated ram for context switch
340 struct sdma_context_data {
341 struct sdma_state_registers channel_state;
365 } __attribute__ ((packed));
371 * struct sdma_desc - descriptor structor for one transfer
372 * @vd: descriptor for virt dma
373 * @num_bd: number of descriptors currently handling
374 * @bd_phys: physical address of bd
375 * @buf_tail: ID of the buffer that was processed
376 * @buf_ptail: ID of the previous buffer that was processed
377 * @period_len: period length, used in cyclic.
378 * @chn_real_count: the real count updated from bd->mode.count
379 * @chn_count: the transfer count set
380 * @sdmac: sdma_channel pointer
381 * @bd: pointer of allocate bd
384 struct virt_dma_desc vd;
387 unsigned int buf_tail;
388 unsigned int buf_ptail;
389 unsigned int period_len;
390 unsigned int chn_real_count;
391 unsigned int chn_count;
392 struct sdma_channel *sdmac;
393 struct sdma_buffer_descriptor *bd;
397 * struct sdma_channel - housekeeping for a SDMA channel
399 * @vc: virt_dma base structure
400 * @desc: sdma description including vd and other special member
401 * @sdma: pointer to the SDMA engine for this channel
402 * @channel: the channel number, matches dmaengine chan_id + 1
403 * @direction: transfer type. Needed for setting SDMA script
404 * @slave_config: Slave configuration
405 * @peripheral_type: Peripheral type. Needed for setting SDMA script
406 * @event_id0: aka dma request line
407 * @event_id1: for channels that use 2 events
408 * @word_size: peripheral access size
409 * @pc_from_device: script address for those device_2_memory
410 * @pc_to_device: script address for those memory_2_device
411 * @device_to_device: script address for those device_2_device
412 * @pc_to_pc: script address for those memory_2_memory
413 * @flags: loop mode or not
414 * @per_address: peripheral source or destination address in common case
415 * destination address in p_2_p case
416 * @per_address2: peripheral source address in p_2_p case
417 * @event_mask: event mask used in p_2_p script
418 * @watermark_level: value for gReg[7], some script will extend it from
419 * basic watermark such as p_2_p
420 * @shp_addr: value for gReg[6]
421 * @per_addr: value for gReg[2]
422 * @status: status of dma channel
423 * @context_loaded: ensure context is only loaded once
424 * @data: specific sdma interface structure
425 * @bd_pool: dma_pool for bd
426 * @terminate_worker: used to call back into terminate work function
428 struct sdma_channel {
429 struct virt_dma_chan vc;
430 struct sdma_desc *desc;
431 struct sdma_engine *sdma;
432 unsigned int channel;
433 enum dma_transfer_direction direction;
434 struct dma_slave_config slave_config;
435 enum sdma_peripheral_type peripheral_type;
436 unsigned int event_id0;
437 unsigned int event_id1;
438 enum dma_slave_buswidth word_size;
439 unsigned int pc_from_device, pc_to_device;
440 unsigned int device_to_device;
441 unsigned int pc_to_pc;
443 dma_addr_t per_address, per_address2;
444 unsigned long event_mask[2];
445 unsigned long watermark_level;
446 u32 shp_addr, per_addr;
447 enum dma_status status;
448 struct imx_dma_data data;
449 struct work_struct terminate_worker;
450 struct list_head terminated;
452 unsigned int n_fifos_src;
453 unsigned int n_fifos_dst;
457 #define IMX_DMA_SG_LOOP BIT(0)
459 #define MAX_DMA_CHANNELS 32
460 #define MXC_SDMA_DEFAULT_PRIORITY 1
461 #define MXC_SDMA_MIN_PRIORITY 1
462 #define MXC_SDMA_MAX_PRIORITY 7
464 #define SDMA_FIRMWARE_MAGIC 0x414d4453
467 * struct sdma_firmware_header - Layout of the firmware image
470 * @version_major: increased whenever layout of struct
471 * sdma_script_start_addrs changes.
472 * @version_minor: firmware minor version (for binary compatible changes)
473 * @script_addrs_start: offset of struct sdma_script_start_addrs in this image
474 * @num_script_addrs: Number of script addresses in this image
475 * @ram_code_start: offset of SDMA ram image in this firmware image
476 * @ram_code_size: size of SDMA ram image
477 * @script_addrs: Stores the start address of the SDMA scripts
478 * (in SDMA memory space)
480 struct sdma_firmware_header {
484 u32 script_addrs_start;
485 u32 num_script_addrs;
490 struct sdma_driver_data {
493 struct sdma_script_start_addrs *script_addrs;
496 * ecspi ERR009165 fixed should be done in sdma script
497 * and it has been fixed in soc from i.mx6ul.
498 * please get more information from the below link:
499 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
506 struct sdma_channel channel[MAX_DMA_CHANNELS];
507 struct sdma_channel_control *channel_control;
509 struct sdma_context_data *context;
510 dma_addr_t context_phys;
511 struct dma_device dma_device;
514 spinlock_t channel_0_lock;
516 struct sdma_script_start_addrs *script_addrs;
517 const struct sdma_driver_data *drvdata;
522 struct sdma_buffer_descriptor *bd0;
523 /* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/
528 static int sdma_config_write(struct dma_chan *chan,
529 struct dma_slave_config *dmaengine_cfg,
530 enum dma_transfer_direction direction);
532 static struct sdma_driver_data sdma_imx31 = {
533 .chnenbl0 = SDMA_CHNENBL0_IMX31,
537 static struct sdma_script_start_addrs sdma_script_imx25 = {
539 .uart_2_mcu_addr = 904,
540 .per_2_app_addr = 1255,
541 .mcu_2_app_addr = 834,
542 .uartsh_2_mcu_addr = 1120,
543 .per_2_shp_addr = 1329,
544 .mcu_2_shp_addr = 1048,
545 .ata_2_mcu_addr = 1560,
546 .mcu_2_ata_addr = 1479,
547 .app_2_per_addr = 1189,
548 .app_2_mcu_addr = 770,
549 .shp_2_per_addr = 1407,
550 .shp_2_mcu_addr = 979,
553 static struct sdma_driver_data sdma_imx25 = {
554 .chnenbl0 = SDMA_CHNENBL0_IMX35,
556 .script_addrs = &sdma_script_imx25,
559 static struct sdma_driver_data sdma_imx35 = {
560 .chnenbl0 = SDMA_CHNENBL0_IMX35,
564 static struct sdma_script_start_addrs sdma_script_imx51 = {
566 .uart_2_mcu_addr = 817,
567 .mcu_2_app_addr = 747,
568 .mcu_2_shp_addr = 961,
569 .ata_2_mcu_addr = 1473,
570 .mcu_2_ata_addr = 1392,
571 .app_2_per_addr = 1033,
572 .app_2_mcu_addr = 683,
573 .shp_2_per_addr = 1251,
574 .shp_2_mcu_addr = 892,
577 static struct sdma_driver_data sdma_imx51 = {
578 .chnenbl0 = SDMA_CHNENBL0_IMX35,
580 .script_addrs = &sdma_script_imx51,
583 static struct sdma_script_start_addrs sdma_script_imx53 = {
585 .app_2_mcu_addr = 683,
586 .mcu_2_app_addr = 747,
587 .uart_2_mcu_addr = 817,
588 .shp_2_mcu_addr = 891,
589 .mcu_2_shp_addr = 960,
590 .uartsh_2_mcu_addr = 1032,
591 .spdif_2_mcu_addr = 1100,
592 .mcu_2_spdif_addr = 1134,
593 .firi_2_mcu_addr = 1193,
594 .mcu_2_firi_addr = 1290,
597 static struct sdma_driver_data sdma_imx53 = {
598 .chnenbl0 = SDMA_CHNENBL0_IMX35,
600 .script_addrs = &sdma_script_imx53,
603 static struct sdma_script_start_addrs sdma_script_imx6q = {
605 .uart_2_mcu_addr = 817,
606 .mcu_2_app_addr = 747,
607 .per_2_per_addr = 6331,
608 .uartsh_2_mcu_addr = 1032,
609 .mcu_2_shp_addr = 960,
610 .app_2_mcu_addr = 683,
611 .shp_2_mcu_addr = 891,
612 .spdif_2_mcu_addr = 1100,
613 .mcu_2_spdif_addr = 1134,
616 static struct sdma_driver_data sdma_imx6q = {
617 .chnenbl0 = SDMA_CHNENBL0_IMX35,
619 .script_addrs = &sdma_script_imx6q,
622 static struct sdma_driver_data sdma_imx6ul = {
623 .chnenbl0 = SDMA_CHNENBL0_IMX35,
625 .script_addrs = &sdma_script_imx6q,
629 static struct sdma_script_start_addrs sdma_script_imx7d = {
631 .uart_2_mcu_addr = 819,
632 .mcu_2_app_addr = 749,
633 .uartsh_2_mcu_addr = 1034,
634 .mcu_2_shp_addr = 962,
635 .app_2_mcu_addr = 685,
636 .shp_2_mcu_addr = 893,
637 .spdif_2_mcu_addr = 1102,
638 .mcu_2_spdif_addr = 1136,
641 static struct sdma_driver_data sdma_imx7d = {
642 .chnenbl0 = SDMA_CHNENBL0_IMX35,
644 .script_addrs = &sdma_script_imx7d,
647 static struct sdma_driver_data sdma_imx8mq = {
648 .chnenbl0 = SDMA_CHNENBL0_IMX35,
650 .script_addrs = &sdma_script_imx7d,
654 static const struct of_device_id sdma_dt_ids[] = {
655 { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
656 { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
657 { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
658 { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
659 { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
660 { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
661 { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
662 { .compatible = "fsl,imx6ul-sdma", .data = &sdma_imx6ul, },
663 { .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, },
666 MODULE_DEVICE_TABLE(of, sdma_dt_ids);
668 #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
669 #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
670 #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
671 #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
673 static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
675 u32 chnenbl0 = sdma->drvdata->chnenbl0;
676 return chnenbl0 + event * 4;
679 static int sdma_config_ownership(struct sdma_channel *sdmac,
680 bool event_override, bool mcu_override, bool dsp_override)
682 struct sdma_engine *sdma = sdmac->sdma;
683 int channel = sdmac->channel;
684 unsigned long evt, mcu, dsp;
686 if (event_override && mcu_override && dsp_override)
689 evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
690 mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
691 dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
694 __clear_bit(channel, &dsp);
696 __set_bit(channel, &dsp);
699 __clear_bit(channel, &evt);
701 __set_bit(channel, &evt);
704 __clear_bit(channel, &mcu);
706 __set_bit(channel, &mcu);
708 writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
709 writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
710 writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
715 static int is_sdma_channel_enabled(struct sdma_engine *sdma, int channel)
717 return !!(readl(sdma->regs + SDMA_H_STATSTOP) & BIT(channel));
720 static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
722 writel(BIT(channel), sdma->regs + SDMA_H_START);
726 * sdma_run_channel0 - run a channel and wait till it's done
728 static int sdma_run_channel0(struct sdma_engine *sdma)
733 sdma_enable_channel(sdma, 0);
735 ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
736 reg, !(reg & 1), 1, 500);
738 dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
740 /* Set bits of CONFIG register with dynamic context switching */
741 reg = readl(sdma->regs + SDMA_H_CONFIG);
742 if ((reg & SDMA_H_CONFIG_CSM) == 0) {
743 reg |= SDMA_H_CONFIG_CSM;
744 writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG);
750 static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
753 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
759 buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL);
763 spin_lock_irqsave(&sdma->channel_0_lock, flags);
765 bd0->mode.command = C0_SETPM;
766 bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
767 bd0->mode.count = size / 2;
768 bd0->buffer_addr = buf_phys;
769 bd0->ext_buffer_addr = address;
771 memcpy(buf_virt, buf, size);
773 ret = sdma_run_channel0(sdma);
775 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
777 dma_free_coherent(sdma->dev, size, buf_virt, buf_phys);
782 static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
784 struct sdma_engine *sdma = sdmac->sdma;
785 int channel = sdmac->channel;
787 u32 chnenbl = chnenbl_ofs(sdma, event);
789 val = readl_relaxed(sdma->regs + chnenbl);
790 __set_bit(channel, &val);
791 writel_relaxed(val, sdma->regs + chnenbl);
793 /* Set SDMA_DONEx_CONFIG is sw_done enabled */
794 if (sdmac->sw_done) {
795 val = readl_relaxed(sdma->regs + SDMA_DONE0_CONFIG);
796 val |= SDMA_DONE0_CONFIG_DONE_SEL;
797 val &= ~SDMA_DONE0_CONFIG_DONE_DIS;
798 writel_relaxed(val, sdma->regs + SDMA_DONE0_CONFIG);
802 static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
804 struct sdma_engine *sdma = sdmac->sdma;
805 int channel = sdmac->channel;
806 u32 chnenbl = chnenbl_ofs(sdma, event);
809 val = readl_relaxed(sdma->regs + chnenbl);
810 __clear_bit(channel, &val);
811 writel_relaxed(val, sdma->regs + chnenbl);
814 static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
816 return container_of(t, struct sdma_desc, vd.tx);
819 static void sdma_start_desc(struct sdma_channel *sdmac)
821 struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
822 struct sdma_desc *desc;
823 struct sdma_engine *sdma = sdmac->sdma;
824 int channel = sdmac->channel;
830 sdmac->desc = desc = to_sdma_desc(&vd->tx);
834 sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
835 sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
836 sdma_enable_channel(sdma, sdmac->channel);
839 static void sdma_update_channel_loop(struct sdma_channel *sdmac)
841 struct sdma_buffer_descriptor *bd;
843 enum dma_status old_status = sdmac->status;
846 * loop mode. Iterate over descriptors, re-setup them and
847 * call callback function.
849 while (sdmac->desc) {
850 struct sdma_desc *desc = sdmac->desc;
852 bd = &desc->bd[desc->buf_tail];
854 if (bd->mode.status & BD_DONE)
857 if (bd->mode.status & BD_RROR) {
858 bd->mode.status &= ~BD_RROR;
859 sdmac->status = DMA_ERROR;
864 * We use bd->mode.count to calculate the residue, since contains
865 * the number of bytes present in the current buffer descriptor.
868 desc->chn_real_count = bd->mode.count;
869 bd->mode.count = desc->period_len;
870 desc->buf_ptail = desc->buf_tail;
871 desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
874 * The callback is called from the interrupt context in order
875 * to reduce latency and to avoid the risk of altering the
876 * SDMA transaction status by the time the client tasklet is
879 spin_unlock(&sdmac->vc.lock);
880 dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
881 spin_lock(&sdmac->vc.lock);
883 /* Assign buffer ownership to SDMA */
884 bd->mode.status |= BD_DONE;
887 sdmac->status = old_status;
891 * SDMA stops cyclic channel when DMA request triggers a channel and no SDMA
892 * owned buffer is available (i.e. BD_DONE was set too late).
894 if (sdmac->desc && !is_sdma_channel_enabled(sdmac->sdma, sdmac->channel)) {
895 dev_warn(sdmac->sdma->dev, "restart cyclic channel %d\n", sdmac->channel);
896 sdma_enable_channel(sdmac->sdma, sdmac->channel);
900 static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
902 struct sdma_channel *sdmac = (struct sdma_channel *) data;
903 struct sdma_buffer_descriptor *bd;
906 sdmac->desc->chn_real_count = 0;
908 * non loop mode. Iterate over all descriptors, collect
909 * errors and call callback function
911 for (i = 0; i < sdmac->desc->num_bd; i++) {
912 bd = &sdmac->desc->bd[i];
914 if (bd->mode.status & (BD_DONE | BD_RROR))
916 sdmac->desc->chn_real_count += bd->mode.count;
920 sdmac->status = DMA_ERROR;
922 sdmac->status = DMA_COMPLETE;
925 static irqreturn_t sdma_int_handler(int irq, void *dev_id)
927 struct sdma_engine *sdma = dev_id;
930 stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
931 writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
932 /* channel 0 is special and not handled here, see run_channel0() */
936 int channel = fls(stat) - 1;
937 struct sdma_channel *sdmac = &sdma->channel[channel];
938 struct sdma_desc *desc;
940 spin_lock(&sdmac->vc.lock);
943 if (sdmac->flags & IMX_DMA_SG_LOOP) {
944 sdma_update_channel_loop(sdmac);
946 mxc_sdma_handle_channel_normal(sdmac);
947 vchan_cookie_complete(&desc->vd);
948 sdma_start_desc(sdmac);
952 spin_unlock(&sdmac->vc.lock);
953 __clear_bit(channel, &stat);
960 * sets the pc of SDMA script according to the peripheral type
962 static int sdma_get_pc(struct sdma_channel *sdmac,
963 enum sdma_peripheral_type peripheral_type)
965 struct sdma_engine *sdma = sdmac->sdma;
966 int per_2_emi = 0, emi_2_per = 0;
968 * These are needed once we start to support transfers between
969 * two peripherals or memory-to-memory transfers
971 int per_2_per = 0, emi_2_emi = 0;
973 sdmac->pc_from_device = 0;
974 sdmac->pc_to_device = 0;
975 sdmac->device_to_device = 0;
977 sdmac->is_ram_script = false;
979 switch (peripheral_type) {
980 case IMX_DMATYPE_MEMORY:
981 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
983 case IMX_DMATYPE_DSP:
984 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
985 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
987 case IMX_DMATYPE_FIRI:
988 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
989 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
991 case IMX_DMATYPE_UART:
992 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
993 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
995 case IMX_DMATYPE_UART_SP:
996 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
997 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
999 case IMX_DMATYPE_ATA:
1000 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
1001 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
1003 case IMX_DMATYPE_CSPI:
1004 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
1006 /* Use rom script mcu_2_app if ERR009165 fixed */
1007 if (sdmac->sdma->drvdata->ecspi_fixed) {
1008 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
1010 emi_2_per = sdma->script_addrs->mcu_2_ecspi_addr;
1011 sdmac->is_ram_script = true;
1015 case IMX_DMATYPE_EXT:
1016 case IMX_DMATYPE_SSI:
1017 case IMX_DMATYPE_SAI:
1018 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
1019 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
1021 case IMX_DMATYPE_SSI_DUAL:
1022 per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
1023 emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
1024 sdmac->is_ram_script = true;
1026 case IMX_DMATYPE_SSI_SP:
1027 case IMX_DMATYPE_MMC:
1028 case IMX_DMATYPE_SDHC:
1029 case IMX_DMATYPE_CSPI_SP:
1030 case IMX_DMATYPE_ESAI:
1031 case IMX_DMATYPE_MSHC_SP:
1032 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
1033 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1035 case IMX_DMATYPE_ASRC:
1036 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
1037 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
1038 per_2_per = sdma->script_addrs->per_2_per_addr;
1039 sdmac->is_ram_script = true;
1041 case IMX_DMATYPE_ASRC_SP:
1042 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
1043 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1044 per_2_per = sdma->script_addrs->per_2_per_addr;
1046 case IMX_DMATYPE_MSHC:
1047 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
1048 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
1050 case IMX_DMATYPE_CCM:
1051 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
1053 case IMX_DMATYPE_SPDIF:
1054 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
1055 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
1057 case IMX_DMATYPE_IPU_MEMORY:
1058 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
1060 case IMX_DMATYPE_MULTI_SAI:
1061 per_2_emi = sdma->script_addrs->sai_2_mcu_addr;
1062 emi_2_per = sdma->script_addrs->mcu_2_sai_addr;
1065 dev_err(sdma->dev, "Unsupported transfer type %d\n",
1070 sdmac->pc_from_device = per_2_emi;
1071 sdmac->pc_to_device = emi_2_per;
1072 sdmac->device_to_device = per_2_per;
1073 sdmac->pc_to_pc = emi_2_emi;
1078 static int sdma_load_context(struct sdma_channel *sdmac)
1080 struct sdma_engine *sdma = sdmac->sdma;
1081 int channel = sdmac->channel;
1083 struct sdma_context_data *context = sdma->context;
1084 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
1086 unsigned long flags;
1088 if (sdmac->direction == DMA_DEV_TO_MEM)
1089 load_address = sdmac->pc_from_device;
1090 else if (sdmac->direction == DMA_DEV_TO_DEV)
1091 load_address = sdmac->device_to_device;
1092 else if (sdmac->direction == DMA_MEM_TO_MEM)
1093 load_address = sdmac->pc_to_pc;
1095 load_address = sdmac->pc_to_device;
1097 if (load_address < 0)
1098 return load_address;
1100 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
1101 dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
1102 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
1103 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
1104 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
1105 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
1107 spin_lock_irqsave(&sdma->channel_0_lock, flags);
1109 memset(context, 0, sizeof(*context));
1110 context->channel_state.pc = load_address;
1112 /* Send by context the event mask,base address for peripheral
1113 * and watermark level
1115 context->gReg[0] = sdmac->event_mask[1];
1116 context->gReg[1] = sdmac->event_mask[0];
1117 context->gReg[2] = sdmac->per_addr;
1118 context->gReg[6] = sdmac->shp_addr;
1119 context->gReg[7] = sdmac->watermark_level;
1121 bd0->mode.command = C0_SETDM;
1122 bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
1123 bd0->mode.count = sizeof(*context) / 4;
1124 bd0->buffer_addr = sdma->context_phys;
1125 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
1126 ret = sdma_run_channel0(sdma);
1128 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
1133 static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
1135 return container_of(chan, struct sdma_channel, vc.chan);
1138 static int sdma_disable_channel(struct dma_chan *chan)
1140 struct sdma_channel *sdmac = to_sdma_chan(chan);
1141 struct sdma_engine *sdma = sdmac->sdma;
1142 int channel = sdmac->channel;
1144 writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
1145 sdmac->status = DMA_ERROR;
1149 static void sdma_channel_terminate_work(struct work_struct *work)
1151 struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1154 * According to NXP R&D team a delay of one BD SDMA cost time
1155 * (maximum is 1ms) should be added after disable of the channel
1156 * bit, to ensure SDMA core has really been stopped after SDMA
1157 * clients call .device_terminate_all.
1159 usleep_range(1000, 2000);
1161 vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
1164 static int sdma_terminate_all(struct dma_chan *chan)
1166 struct sdma_channel *sdmac = to_sdma_chan(chan);
1167 unsigned long flags;
1169 spin_lock_irqsave(&sdmac->vc.lock, flags);
1171 sdma_disable_channel(chan);
1174 vchan_terminate_vdesc(&sdmac->desc->vd);
1176 * move out current descriptor into terminated list so that
1177 * it could be free in sdma_channel_terminate_work alone
1178 * later without potential involving next descriptor raised
1179 * up before the last descriptor terminated.
1181 vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
1183 schedule_work(&sdmac->terminate_worker);
1186 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1191 static void sdma_channel_synchronize(struct dma_chan *chan)
1193 struct sdma_channel *sdmac = to_sdma_chan(chan);
1195 vchan_synchronize(&sdmac->vc);
1197 flush_work(&sdmac->terminate_worker);
1200 static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
1202 struct sdma_engine *sdma = sdmac->sdma;
1204 int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
1205 int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
1207 set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
1208 set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
1210 if (sdmac->event_id0 > 31)
1211 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
1213 if (sdmac->event_id1 > 31)
1214 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
1217 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
1218 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
1219 * r0(event_mask[1]) and r1(event_mask[0]).
1222 sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
1223 SDMA_WATERMARK_LEVEL_HWML);
1224 sdmac->watermark_level |= hwml;
1225 sdmac->watermark_level |= lwml << 16;
1226 swap(sdmac->event_mask[0], sdmac->event_mask[1]);
1229 if (sdmac->per_address2 >= sdma->spba_start_addr &&
1230 sdmac->per_address2 <= sdma->spba_end_addr)
1231 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
1233 if (sdmac->per_address >= sdma->spba_start_addr &&
1234 sdmac->per_address <= sdma->spba_end_addr)
1235 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
1237 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
1240 static void sdma_set_watermarklevel_for_sais(struct sdma_channel *sdmac)
1242 unsigned int n_fifos;
1245 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SW_DONE;
1247 if (sdmac->direction == DMA_DEV_TO_MEM)
1248 n_fifos = sdmac->n_fifos_src;
1250 n_fifos = sdmac->n_fifos_dst;
1252 sdmac->watermark_level |=
1253 FIELD_PREP(SDMA_WATERMARK_LEVEL_N_FIFOS, n_fifos);
1256 static int sdma_config_channel(struct dma_chan *chan)
1258 struct sdma_channel *sdmac = to_sdma_chan(chan);
1261 sdma_disable_channel(chan);
1263 sdmac->event_mask[0] = 0;
1264 sdmac->event_mask[1] = 0;
1265 sdmac->shp_addr = 0;
1266 sdmac->per_addr = 0;
1268 switch (sdmac->peripheral_type) {
1269 case IMX_DMATYPE_DSP:
1270 sdma_config_ownership(sdmac, false, true, true);
1272 case IMX_DMATYPE_MEMORY:
1273 sdma_config_ownership(sdmac, false, true, false);
1276 sdma_config_ownership(sdmac, true, true, false);
1280 ret = sdma_get_pc(sdmac, sdmac->peripheral_type);
1284 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
1285 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
1286 /* Handle multiple event channels differently */
1287 if (sdmac->event_id1) {
1288 if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
1289 sdmac->peripheral_type == IMX_DMATYPE_ASRC)
1290 sdma_set_watermarklevel_for_p2p(sdmac);
1292 if (sdmac->peripheral_type ==
1293 IMX_DMATYPE_MULTI_SAI)
1294 sdma_set_watermarklevel_for_sais(sdmac);
1296 __set_bit(sdmac->event_id0, sdmac->event_mask);
1300 sdmac->shp_addr = sdmac->per_address;
1301 sdmac->per_addr = sdmac->per_address2;
1303 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
1309 static int sdma_set_channel_priority(struct sdma_channel *sdmac,
1310 unsigned int priority)
1312 struct sdma_engine *sdma = sdmac->sdma;
1313 int channel = sdmac->channel;
1315 if (priority < MXC_SDMA_MIN_PRIORITY
1316 || priority > MXC_SDMA_MAX_PRIORITY) {
1320 writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
1325 static int sdma_request_channel0(struct sdma_engine *sdma)
1329 sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys,
1336 sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
1337 sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
1339 sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
1347 static int sdma_alloc_bd(struct sdma_desc *desc)
1349 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1352 desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size,
1353 &desc->bd_phys, GFP_NOWAIT);
1362 static void sdma_free_bd(struct sdma_desc *desc)
1364 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1366 dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd,
1370 static void sdma_desc_free(struct virt_dma_desc *vd)
1372 struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
1378 static int sdma_alloc_chan_resources(struct dma_chan *chan)
1380 struct sdma_channel *sdmac = to_sdma_chan(chan);
1381 struct imx_dma_data *data = chan->private;
1382 struct imx_dma_data mem_data;
1386 * MEMCPY may never setup chan->private by filter function such as
1387 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
1388 * Please note in any other slave case, you have to setup chan->private
1389 * with 'struct imx_dma_data' in your own filter function if you want to
1390 * request dma channel by dma_request_channel() rather than
1391 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
1392 * to warn you to correct your filter function.
1395 dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
1396 mem_data.priority = 2;
1397 mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
1398 mem_data.dma_request = 0;
1399 mem_data.dma_request2 = 0;
1402 ret = sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
1407 switch (data->priority) {
1411 case DMA_PRIO_MEDIUM:
1420 sdmac->peripheral_type = data->peripheral_type;
1421 sdmac->event_id0 = data->dma_request;
1422 sdmac->event_id1 = data->dma_request2;
1424 ret = clk_enable(sdmac->sdma->clk_ipg);
1427 ret = clk_enable(sdmac->sdma->clk_ahb);
1429 goto disable_clk_ipg;
1431 ret = sdma_set_channel_priority(sdmac, prio);
1433 goto disable_clk_ahb;
1438 clk_disable(sdmac->sdma->clk_ahb);
1440 clk_disable(sdmac->sdma->clk_ipg);
1444 static void sdma_free_chan_resources(struct dma_chan *chan)
1446 struct sdma_channel *sdmac = to_sdma_chan(chan);
1447 struct sdma_engine *sdma = sdmac->sdma;
1449 sdma_terminate_all(chan);
1451 sdma_channel_synchronize(chan);
1453 sdma_event_disable(sdmac, sdmac->event_id0);
1454 if (sdmac->event_id1)
1455 sdma_event_disable(sdmac, sdmac->event_id1);
1457 sdmac->event_id0 = 0;
1458 sdmac->event_id1 = 0;
1460 sdma_set_channel_priority(sdmac, 0);
1462 clk_disable(sdma->clk_ipg);
1463 clk_disable(sdma->clk_ahb);
1466 static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
1467 enum dma_transfer_direction direction, u32 bds)
1469 struct sdma_desc *desc;
1471 if (!sdmac->sdma->fw_loaded && sdmac->is_ram_script) {
1472 dev_warn_once(sdmac->sdma->dev, "sdma firmware not ready!\n");
1476 desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
1480 sdmac->status = DMA_IN_PROGRESS;
1481 sdmac->direction = direction;
1484 desc->chn_count = 0;
1485 desc->chn_real_count = 0;
1487 desc->buf_ptail = 0;
1488 desc->sdmac = sdmac;
1491 if (sdma_alloc_bd(desc))
1494 /* No slave_config called in MEMCPY case, so do here */
1495 if (direction == DMA_MEM_TO_MEM)
1496 sdma_config_ownership(sdmac, false, true, false);
1498 if (sdma_load_context(sdmac))
1509 static struct dma_async_tx_descriptor *sdma_prep_memcpy(
1510 struct dma_chan *chan, dma_addr_t dma_dst,
1511 dma_addr_t dma_src, size_t len, unsigned long flags)
1513 struct sdma_channel *sdmac = to_sdma_chan(chan);
1514 struct sdma_engine *sdma = sdmac->sdma;
1515 int channel = sdmac->channel;
1518 struct sdma_buffer_descriptor *bd;
1519 struct sdma_desc *desc;
1524 dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
1525 &dma_src, &dma_dst, len, channel);
1527 desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
1528 len / SDMA_BD_MAX_CNT + 1);
1533 count = min_t(size_t, len, SDMA_BD_MAX_CNT);
1535 bd->buffer_addr = dma_src;
1536 bd->ext_buffer_addr = dma_dst;
1537 bd->mode.count = count;
1538 desc->chn_count += count;
1539 bd->mode.command = 0;
1546 param = BD_DONE | BD_EXTD | BD_CONT;
1554 dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
1555 i, count, bd->buffer_addr,
1556 param & BD_WRAP ? "wrap" : "",
1557 param & BD_INTR ? " intr" : "");
1559 bd->mode.status = param;
1562 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1565 static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
1566 struct dma_chan *chan, struct scatterlist *sgl,
1567 unsigned int sg_len, enum dma_transfer_direction direction,
1568 unsigned long flags, void *context)
1570 struct sdma_channel *sdmac = to_sdma_chan(chan);
1571 struct sdma_engine *sdma = sdmac->sdma;
1573 int channel = sdmac->channel;
1574 struct scatterlist *sg;
1575 struct sdma_desc *desc;
1577 sdma_config_write(chan, &sdmac->slave_config, direction);
1579 desc = sdma_transfer_init(sdmac, direction, sg_len);
1583 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
1586 for_each_sg(sgl, sg, sg_len, i) {
1587 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1590 bd->buffer_addr = sg->dma_address;
1592 count = sg_dma_len(sg);
1594 if (count > SDMA_BD_MAX_CNT) {
1595 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
1596 channel, count, SDMA_BD_MAX_CNT);
1600 bd->mode.count = count;
1601 desc->chn_count += count;
1603 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1606 switch (sdmac->word_size) {
1607 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1608 bd->mode.command = 0;
1609 if (count & 3 || sg->dma_address & 3)
1612 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1613 bd->mode.command = 2;
1614 if (count & 1 || sg->dma_address & 1)
1617 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1618 bd->mode.command = 1;
1624 param = BD_DONE | BD_EXTD | BD_CONT;
1626 if (i + 1 == sg_len) {
1632 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1633 i, count, (u64)sg->dma_address,
1634 param & BD_WRAP ? "wrap" : "",
1635 param & BD_INTR ? " intr" : "");
1637 bd->mode.status = param;
1640 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1645 sdmac->status = DMA_ERROR;
1649 static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1650 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1651 size_t period_len, enum dma_transfer_direction direction,
1652 unsigned long flags)
1654 struct sdma_channel *sdmac = to_sdma_chan(chan);
1655 struct sdma_engine *sdma = sdmac->sdma;
1656 int num_periods = buf_len / period_len;
1657 int channel = sdmac->channel;
1659 struct sdma_desc *desc;
1661 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1663 sdma_config_write(chan, &sdmac->slave_config, direction);
1665 desc = sdma_transfer_init(sdmac, direction, num_periods);
1669 desc->period_len = period_len;
1671 sdmac->flags |= IMX_DMA_SG_LOOP;
1673 if (period_len > SDMA_BD_MAX_CNT) {
1674 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
1675 channel, period_len, SDMA_BD_MAX_CNT);
1679 while (buf < buf_len) {
1680 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1683 bd->buffer_addr = dma_addr;
1685 bd->mode.count = period_len;
1687 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1689 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1690 bd->mode.command = 0;
1692 bd->mode.command = sdmac->word_size;
1694 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1695 if (i + 1 == num_periods)
1698 dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1699 i, period_len, (u64)dma_addr,
1700 param & BD_WRAP ? "wrap" : "",
1701 param & BD_INTR ? " intr" : "");
1703 bd->mode.status = param;
1705 dma_addr += period_len;
1711 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1716 sdmac->status = DMA_ERROR;
1720 static int sdma_config_write(struct dma_chan *chan,
1721 struct dma_slave_config *dmaengine_cfg,
1722 enum dma_transfer_direction direction)
1724 struct sdma_channel *sdmac = to_sdma_chan(chan);
1726 if (direction == DMA_DEV_TO_MEM) {
1727 sdmac->per_address = dmaengine_cfg->src_addr;
1728 sdmac->watermark_level = dmaengine_cfg->src_maxburst *
1729 dmaengine_cfg->src_addr_width;
1730 sdmac->word_size = dmaengine_cfg->src_addr_width;
1731 } else if (direction == DMA_DEV_TO_DEV) {
1732 sdmac->per_address2 = dmaengine_cfg->src_addr;
1733 sdmac->per_address = dmaengine_cfg->dst_addr;
1734 sdmac->watermark_level = dmaengine_cfg->src_maxburst &
1735 SDMA_WATERMARK_LEVEL_LWML;
1736 sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
1737 SDMA_WATERMARK_LEVEL_HWML;
1738 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1740 sdmac->per_address = dmaengine_cfg->dst_addr;
1741 sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
1742 dmaengine_cfg->dst_addr_width;
1743 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1745 sdmac->direction = direction;
1746 return sdma_config_channel(chan);
1749 static int sdma_config(struct dma_chan *chan,
1750 struct dma_slave_config *dmaengine_cfg)
1752 struct sdma_channel *sdmac = to_sdma_chan(chan);
1753 struct sdma_engine *sdma = sdmac->sdma;
1755 memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1757 if (dmaengine_cfg->peripheral_config) {
1758 struct sdma_peripheral_config *sdmacfg = dmaengine_cfg->peripheral_config;
1759 if (dmaengine_cfg->peripheral_size != sizeof(struct sdma_peripheral_config)) {
1760 dev_err(sdma->dev, "Invalid peripheral size %zu, expected %zu\n",
1761 dmaengine_cfg->peripheral_size,
1762 sizeof(struct sdma_peripheral_config));
1765 sdmac->n_fifos_src = sdmacfg->n_fifos_src;
1766 sdmac->n_fifos_dst = sdmacfg->n_fifos_dst;
1767 sdmac->sw_done = sdmacfg->sw_done;
1770 /* Set ENBLn earlier to make sure dma request triggered after that */
1771 if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1773 sdma_event_enable(sdmac, sdmac->event_id0);
1775 if (sdmac->event_id1) {
1776 if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1778 sdma_event_enable(sdmac, sdmac->event_id1);
1784 static enum dma_status sdma_tx_status(struct dma_chan *chan,
1785 dma_cookie_t cookie,
1786 struct dma_tx_state *txstate)
1788 struct sdma_channel *sdmac = to_sdma_chan(chan);
1789 struct sdma_desc *desc = NULL;
1791 struct virt_dma_desc *vd;
1792 enum dma_status ret;
1793 unsigned long flags;
1795 ret = dma_cookie_status(chan, cookie, txstate);
1796 if (ret == DMA_COMPLETE || !txstate)
1799 spin_lock_irqsave(&sdmac->vc.lock, flags);
1801 vd = vchan_find_desc(&sdmac->vc, cookie);
1803 desc = to_sdma_desc(&vd->tx);
1804 else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie)
1808 if (sdmac->flags & IMX_DMA_SG_LOOP)
1809 residue = (desc->num_bd - desc->buf_ptail) *
1810 desc->period_len - desc->chn_real_count;
1812 residue = desc->chn_count - desc->chn_real_count;
1817 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1819 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1822 return sdmac->status;
1825 static void sdma_issue_pending(struct dma_chan *chan)
1827 struct sdma_channel *sdmac = to_sdma_chan(chan);
1828 unsigned long flags;
1830 spin_lock_irqsave(&sdmac->vc.lock, flags);
1831 if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
1832 sdma_start_desc(sdmac);
1833 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1836 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1837 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
1838 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 45
1839 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 46
1841 static void sdma_add_scripts(struct sdma_engine *sdma,
1842 const struct sdma_script_start_addrs *addr)
1844 s32 *addr_arr = (u32 *)addr;
1845 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1848 /* use the default firmware in ROM if missing external firmware */
1849 if (!sdma->script_number)
1850 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1852 if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
1855 "SDMA script number %d not match with firmware.\n",
1856 sdma->script_number);
1860 for (i = 0; i < sdma->script_number; i++)
1861 if (addr_arr[i] > 0)
1862 saddr_arr[i] = addr_arr[i];
1865 * For compatibility with NXP internal legacy kernel before 4.19 which
1866 * is based on uart ram script and mainline kernel based on uart rom
1867 * script, both uart ram/rom scripts are present in newer sdma
1868 * firmware. Use the rom versions if they are present (V3 or newer).
1870 if (sdma->script_number >= SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3) {
1871 if (addr->uart_2_mcu_rom_addr)
1872 sdma->script_addrs->uart_2_mcu_addr = addr->uart_2_mcu_rom_addr;
1873 if (addr->uartsh_2_mcu_rom_addr)
1874 sdma->script_addrs->uartsh_2_mcu_addr = addr->uartsh_2_mcu_rom_addr;
1878 static void sdma_load_firmware(const struct firmware *fw, void *context)
1880 struct sdma_engine *sdma = context;
1881 const struct sdma_firmware_header *header;
1882 const struct sdma_script_start_addrs *addr;
1883 unsigned short *ram_code;
1886 dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
1887 /* In this case we just use the ROM firmware. */
1891 if (fw->size < sizeof(*header))
1894 header = (struct sdma_firmware_header *)fw->data;
1896 if (header->magic != SDMA_FIRMWARE_MAGIC)
1898 if (header->ram_code_start + header->ram_code_size > fw->size)
1900 switch (header->version_major) {
1902 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1905 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1908 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1911 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1914 dev_err(sdma->dev, "unknown firmware version\n");
1918 addr = (void *)header + header->script_addrs_start;
1919 ram_code = (void *)header + header->ram_code_start;
1921 clk_enable(sdma->clk_ipg);
1922 clk_enable(sdma->clk_ahb);
1923 /* download the RAM image for SDMA */
1924 sdma_load_script(sdma, ram_code,
1925 header->ram_code_size,
1926 addr->ram_code_start_addr);
1927 clk_disable(sdma->clk_ipg);
1928 clk_disable(sdma->clk_ahb);
1930 sdma_add_scripts(sdma, addr);
1932 sdma->fw_loaded = true;
1934 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1935 header->version_major,
1936 header->version_minor);
1939 release_firmware(fw);
1942 #define EVENT_REMAP_CELLS 3
1944 static int sdma_event_remap(struct sdma_engine *sdma)
1946 struct device_node *np = sdma->dev->of_node;
1947 struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1948 struct property *event_remap;
1950 char propname[] = "fsl,sdma-event-remap";
1951 u32 reg, val, shift, num_map, i;
1954 if (IS_ERR(np) || !gpr_np)
1957 event_remap = of_find_property(np, propname, NULL);
1958 num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1960 dev_dbg(sdma->dev, "no event needs to be remapped\n");
1962 } else if (num_map % EVENT_REMAP_CELLS) {
1963 dev_err(sdma->dev, "the property %s must modulo %d\n",
1964 propname, EVENT_REMAP_CELLS);
1969 gpr = syscon_node_to_regmap(gpr_np);
1971 dev_err(sdma->dev, "failed to get gpr regmap\n");
1976 for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1977 ret = of_property_read_u32_index(np, propname, i, ®);
1979 dev_err(sdma->dev, "failed to read property %s index %d\n",
1984 ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1986 dev_err(sdma->dev, "failed to read property %s index %d\n",
1991 ret = of_property_read_u32_index(np, propname, i + 2, &val);
1993 dev_err(sdma->dev, "failed to read property %s index %d\n",
1998 regmap_update_bits(gpr, reg, BIT(shift), val << shift);
2003 of_node_put(gpr_np);
2008 static int sdma_get_firmware(struct sdma_engine *sdma,
2009 const char *fw_name)
2013 ret = request_firmware_nowait(THIS_MODULE,
2014 FW_ACTION_UEVENT, fw_name, sdma->dev,
2015 GFP_KERNEL, sdma, sdma_load_firmware);
2020 static int sdma_init(struct sdma_engine *sdma)
2023 dma_addr_t ccb_phys;
2025 ret = clk_enable(sdma->clk_ipg);
2028 ret = clk_enable(sdma->clk_ahb);
2030 goto disable_clk_ipg;
2032 if (sdma->drvdata->check_ratio &&
2033 (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg)))
2034 sdma->clk_ratio = 1;
2036 /* Be sure SDMA has not started yet */
2037 writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
2039 sdma->channel_control = dma_alloc_coherent(sdma->dev,
2040 MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control) +
2041 sizeof(struct sdma_context_data),
2042 &ccb_phys, GFP_KERNEL);
2044 if (!sdma->channel_control) {
2049 sdma->context = (void *)sdma->channel_control +
2050 MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
2051 sdma->context_phys = ccb_phys +
2052 MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
2054 /* disable all channels */
2055 for (i = 0; i < sdma->drvdata->num_events; i++)
2056 writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
2058 /* All channels have priority 0 */
2059 for (i = 0; i < MAX_DMA_CHANNELS; i++)
2060 writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
2062 ret = sdma_request_channel0(sdma);
2066 sdma_config_ownership(&sdma->channel[0], false, true, false);
2068 /* Set Command Channel (Channel Zero) */
2069 writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
2071 /* Set bits of CONFIG register but with static context switching */
2072 if (sdma->clk_ratio)
2073 writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG);
2075 writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
2077 writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
2079 /* Initializes channel's priorities */
2080 sdma_set_channel_priority(&sdma->channel[0], 7);
2082 clk_disable(sdma->clk_ipg);
2083 clk_disable(sdma->clk_ahb);
2088 clk_disable(sdma->clk_ahb);
2090 clk_disable(sdma->clk_ipg);
2091 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
2095 static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
2097 struct sdma_channel *sdmac = to_sdma_chan(chan);
2098 struct imx_dma_data *data = fn_param;
2100 if (!imx_dma_is_general_purpose(chan))
2103 sdmac->data = *data;
2104 chan->private = &sdmac->data;
2109 static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
2110 struct of_dma *ofdma)
2112 struct sdma_engine *sdma = ofdma->of_dma_data;
2113 dma_cap_mask_t mask = sdma->dma_device.cap_mask;
2114 struct imx_dma_data data;
2116 if (dma_spec->args_count != 3)
2119 data.dma_request = dma_spec->args[0];
2120 data.peripheral_type = dma_spec->args[1];
2121 data.priority = dma_spec->args[2];
2123 * init dma_request2 to zero, which is not used by the dts.
2124 * For P2P, dma_request2 is init from dma_request_channel(),
2125 * chan->private will point to the imx_dma_data, and in
2126 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
2127 * be set to sdmac->event_id1.
2129 data.dma_request2 = 0;
2131 return __dma_request_channel(&mask, sdma_filter_fn, &data,
2135 static int sdma_probe(struct platform_device *pdev)
2137 struct device_node *np = pdev->dev.of_node;
2138 struct device_node *spba_bus;
2139 const char *fw_name;
2142 struct resource *iores;
2143 struct resource spba_res;
2145 struct sdma_engine *sdma;
2148 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2152 sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
2156 spin_lock_init(&sdma->channel_0_lock);
2158 sdma->dev = &pdev->dev;
2159 sdma->drvdata = of_device_get_match_data(sdma->dev);
2161 irq = platform_get_irq(pdev, 0);
2165 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2166 sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
2167 if (IS_ERR(sdma->regs))
2168 return PTR_ERR(sdma->regs);
2170 sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2171 if (IS_ERR(sdma->clk_ipg))
2172 return PTR_ERR(sdma->clk_ipg);
2174 sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2175 if (IS_ERR(sdma->clk_ahb))
2176 return PTR_ERR(sdma->clk_ahb);
2178 ret = clk_prepare(sdma->clk_ipg);
2182 ret = clk_prepare(sdma->clk_ahb);
2186 ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
2193 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2194 if (!sdma->script_addrs) {
2199 /* initially no scripts available */
2200 saddr_arr = (s32 *)sdma->script_addrs;
2201 for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++)
2202 saddr_arr[i] = -EINVAL;
2204 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
2205 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
2206 dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
2208 INIT_LIST_HEAD(&sdma->dma_device.channels);
2209 /* Initialize channel parameters */
2210 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2211 struct sdma_channel *sdmac = &sdma->channel[i];
2216 sdmac->vc.desc_free = sdma_desc_free;
2217 INIT_LIST_HEAD(&sdmac->terminated);
2218 INIT_WORK(&sdmac->terminate_worker,
2219 sdma_channel_terminate_work);
2221 * Add the channel to the DMAC list. Do not add channel 0 though
2222 * because we need it internally in the SDMA driver. This also means
2223 * that channel 0 in dmaengine counting matches sdma channel 1.
2226 vchan_init(&sdmac->vc, &sdma->dma_device);
2229 ret = sdma_init(sdma);
2233 ret = sdma_event_remap(sdma);
2237 if (sdma->drvdata->script_addrs)
2238 sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
2240 sdma->dma_device.dev = &pdev->dev;
2242 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
2243 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
2244 sdma->dma_device.device_tx_status = sdma_tx_status;
2245 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
2246 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
2247 sdma->dma_device.device_config = sdma_config;
2248 sdma->dma_device.device_terminate_all = sdma_terminate_all;
2249 sdma->dma_device.device_synchronize = sdma_channel_synchronize;
2250 sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2251 sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2252 sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
2253 sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
2254 sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
2255 sdma->dma_device.device_issue_pending = sdma_issue_pending;
2256 sdma->dma_device.copy_align = 2;
2257 dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
2259 platform_set_drvdata(pdev, sdma);
2261 ret = dma_async_device_register(&sdma->dma_device);
2263 dev_err(&pdev->dev, "unable to register\n");
2268 ret = of_dma_controller_register(np, sdma_xlate, sdma);
2270 dev_err(&pdev->dev, "failed to register controller\n");
2274 spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
2275 ret = of_address_to_resource(spba_bus, 0, &spba_res);
2277 sdma->spba_start_addr = spba_res.start;
2278 sdma->spba_end_addr = spba_res.end;
2280 of_node_put(spba_bus);
2284 * Because that device tree does not encode ROM script address,
2285 * the RAM script in firmware is mandatory for device tree
2286 * probe, otherwise it fails.
2288 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
2291 dev_warn(&pdev->dev, "failed to get firmware name\n");
2293 ret = sdma_get_firmware(sdma, fw_name);
2295 dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
2301 dma_async_device_unregister(&sdma->dma_device);
2303 kfree(sdma->script_addrs);
2305 clk_unprepare(sdma->clk_ahb);
2307 clk_unprepare(sdma->clk_ipg);
2311 static int sdma_remove(struct platform_device *pdev)
2313 struct sdma_engine *sdma = platform_get_drvdata(pdev);
2316 devm_free_irq(&pdev->dev, sdma->irq, sdma);
2317 dma_async_device_unregister(&sdma->dma_device);
2318 kfree(sdma->script_addrs);
2319 clk_unprepare(sdma->clk_ahb);
2320 clk_unprepare(sdma->clk_ipg);
2321 /* Kill the tasklet */
2322 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2323 struct sdma_channel *sdmac = &sdma->channel[i];
2325 tasklet_kill(&sdmac->vc.task);
2326 sdma_free_chan_resources(&sdmac->vc.chan);
2329 platform_set_drvdata(pdev, NULL);
2333 static struct platform_driver sdma_driver = {
2336 .of_match_table = sdma_dt_ids,
2338 .remove = sdma_remove,
2339 .probe = sdma_probe,
2342 module_platform_driver(sdma_driver);
2344 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
2345 MODULE_DESCRIPTION("i.MX SDMA driver");
2346 #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2347 MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2349 #if IS_ENABLED(CONFIG_SOC_IMX7D) || IS_ENABLED(CONFIG_SOC_IMX8M)
2350 MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2352 MODULE_LICENSE("GPL");