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
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/bitops.h>
19 #include <linux/interrupt.h>
20 #include <linux/clk.h>
21 #include <linux/delay.h>
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>
32 #include <linux/of_address.h>
33 #include <linux/of_device.h>
34 #include <linux/of_dma.h>
35 #include <linux/workqueue.h>
38 #include <linux/platform_data/dma-imx.h>
39 #include <linux/regmap.h>
40 #include <linux/mfd/syscon.h>
41 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
43 #include "dmaengine.h"
47 #define SDMA_H_C0PTR 0x000
48 #define SDMA_H_INTR 0x004
49 #define SDMA_H_STATSTOP 0x008
50 #define SDMA_H_START 0x00c
51 #define SDMA_H_EVTOVR 0x010
52 #define SDMA_H_DSPOVR 0x014
53 #define SDMA_H_HOSTOVR 0x018
54 #define SDMA_H_EVTPEND 0x01c
55 #define SDMA_H_DSPENBL 0x020
56 #define SDMA_H_RESET 0x024
57 #define SDMA_H_EVTERR 0x028
58 #define SDMA_H_INTRMSK 0x02c
59 #define SDMA_H_PSW 0x030
60 #define SDMA_H_EVTERRDBG 0x034
61 #define SDMA_H_CONFIG 0x038
62 #define SDMA_ONCE_ENB 0x040
63 #define SDMA_ONCE_DATA 0x044
64 #define SDMA_ONCE_INSTR 0x048
65 #define SDMA_ONCE_STAT 0x04c
66 #define SDMA_ONCE_CMD 0x050
67 #define SDMA_EVT_MIRROR 0x054
68 #define SDMA_ILLINSTADDR 0x058
69 #define SDMA_CHN0ADDR 0x05c
70 #define SDMA_ONCE_RTB 0x060
71 #define SDMA_XTRIG_CONF1 0x070
72 #define SDMA_XTRIG_CONF2 0x074
73 #define SDMA_CHNENBL0_IMX35 0x200
74 #define SDMA_CHNENBL0_IMX31 0x080
75 #define SDMA_CHNPRI_0 0x100
78 * Buffer descriptor status values.
89 * Data Node descriptor status values.
91 #define DND_END_OF_FRAME 0x80
92 #define DND_END_OF_XFER 0x40
94 #define DND_UNUSED 0x01
97 * IPCV2 descriptor status values.
99 #define BD_IPCV2_END_OF_FRAME 0x40
101 #define IPCV2_MAX_NODES 50
103 * Error bit set in the CCB status field by the SDMA,
104 * in setbd routine, in case of a transfer error
106 #define DATA_ERROR 0x10000000
109 * Buffer descriptor commands.
114 #define C0_SETCTX 0x07
115 #define C0_GETCTX 0x03
116 #define C0_SETDM 0x01
117 #define C0_SETPM 0x04
118 #define C0_GETDM 0x02
119 #define C0_GETPM 0x08
121 * Change endianness indicator in the BD command field
123 #define CHANGE_ENDIANNESS 0x80
126 * p_2_p watermark_level description
127 * Bits Name Description
128 * 0-7 Lower WML Lower watermark level
129 * 8 PS 1: Pad Swallowing
130 * 0: No Pad Swallowing
133 * 10 SPDIF If this bit is set both source
134 * and destination are on SPBA
135 * 11 Source Bit(SP) 1: Source on SPBA
137 * 12 Destination Bit(DP) 1: Destination on SPBA
138 * 0: Destination on AIPS
139 * 13-15 --------- MUST BE 0
140 * 16-23 Higher WML HWML
141 * 24-27 N Total number of samples after
142 * which Pad adding/Swallowing
143 * must be done. It must be odd.
144 * 28 Lower WML Event(LWE) SDMA events reg to check for
146 * 0: LWE in EVENTS register
147 * 1: LWE in EVENTS2 register
148 * 29 Higher WML Event(HWE) SDMA events reg to check for
150 * 0: HWE in EVENTS register
151 * 1: HWE in EVENTS2 register
152 * 30 --------- MUST BE 0
153 * 31 CONT 1: Amount of samples to be
154 * transferred is unknown and
155 * script will keep on
156 * transferring samples as long as
157 * both events are detected and
158 * script must be manually stopped
160 * 0: The amount of samples to be
161 * transferred is equal to the
162 * count field of mode word
164 #define SDMA_WATERMARK_LEVEL_LWML 0xFF
165 #define SDMA_WATERMARK_LEVEL_PS BIT(8)
166 #define SDMA_WATERMARK_LEVEL_PA BIT(9)
167 #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
168 #define SDMA_WATERMARK_LEVEL_SP BIT(11)
169 #define SDMA_WATERMARK_LEVEL_DP BIT(12)
170 #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
171 #define SDMA_WATERMARK_LEVEL_LWE BIT(28)
172 #define SDMA_WATERMARK_LEVEL_HWE BIT(29)
173 #define SDMA_WATERMARK_LEVEL_CONT BIT(31)
175 #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
176 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
177 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
179 #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \
180 BIT(DMA_MEM_TO_DEV) | \
184 * struct sdma_script_start_addrs - SDMA script start pointers
186 * start addresses of the different functions in the physical
187 * address space of the SDMA engine.
189 struct sdma_script_start_addrs {
192 s32 ap_2_ap_fixed_addr;
194 s32 loopback_on_dsp_side_addr;
195 s32 mcu_interrupt_only_addr;
201 s32 uart_2_mcu_ram_addr;
205 s32 uartsh_2_per_addr;
206 s32 uartsh_2_mcu_ram_addr;
217 s32 spdif_2_mcu_addr;
218 s32 mcu_2_spdif_addr;
220 s32 ext_mem_2_ipu_addr;
221 s32 descrambler_addr;
224 s32 ram_code_start_addr;
225 /* End of v1 array */
226 s32 mcu_2_ssish_addr;
227 s32 ssish_2_mcu_addr;
229 /* End of v2 array */
230 s32 zcanfd_2_mcu_addr;
231 s32 zqspi_2_mcu_addr;
232 s32 mcu_2_ecspi_addr;
236 s32 uartsh_2_mcu_addr;
237 /* End of v3 array */
238 s32 mcu_2_zqspi_addr;
239 /* End of v4 array */
243 * Mode/Count of data node descriptors - IPCv2
245 struct sdma_mode_count {
246 #define SDMA_BD_MAX_CNT 0xffff
247 u32 count : 16; /* size of the buffer pointed by this BD */
248 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
249 u32 command : 8; /* command mostly used for channel 0 */
255 struct sdma_buffer_descriptor {
256 struct sdma_mode_count mode;
257 u32 buffer_addr; /* address of the buffer described */
258 u32 ext_buffer_addr; /* extended buffer address */
259 } __attribute__ ((packed));
262 * struct sdma_channel_control - Channel control Block
264 * @current_bd_ptr: current buffer descriptor processed
265 * @base_bd_ptr: first element of buffer descriptor array
266 * @unused: padding. The SDMA engine expects an array of 128 byte
269 struct sdma_channel_control {
273 } __attribute__ ((packed));
276 * struct sdma_state_registers - SDMA context for a channel
278 * @pc: program counter
280 * @t: test bit: status of arithmetic & test instruction
281 * @rpc: return program counter
283 * @sf: source fault while loading data
284 * @spc: loop start program counter
286 * @df: destination fault while storing data
287 * @epc: loop end program counter
290 struct sdma_state_registers {
302 } __attribute__ ((packed));
305 * struct sdma_context_data - sdma context specific to a channel
307 * @channel_state: channel state bits
308 * @gReg: general registers
309 * @mda: burst dma destination address register
310 * @msa: burst dma source address register
311 * @ms: burst dma status register
312 * @md: burst dma data register
313 * @pda: peripheral dma destination address register
314 * @psa: peripheral dma source address register
315 * @ps: peripheral dma status register
316 * @pd: peripheral dma data register
317 * @ca: CRC polynomial register
318 * @cs: CRC accumulator register
319 * @dda: dedicated core destination address register
320 * @dsa: dedicated core source address register
321 * @ds: dedicated core status register
322 * @dd: dedicated core data register
323 * @scratch0: 1st word of dedicated ram for context switch
324 * @scratch1: 2nd word of dedicated ram for context switch
325 * @scratch2: 3rd word of dedicated ram for context switch
326 * @scratch3: 4th word of dedicated ram for context switch
327 * @scratch4: 5th word of dedicated ram for context switch
328 * @scratch5: 6th word of dedicated ram for context switch
329 * @scratch6: 7th word of dedicated ram for context switch
330 * @scratch7: 8th word of dedicated ram for context switch
332 struct sdma_context_data {
333 struct sdma_state_registers channel_state;
357 } __attribute__ ((packed));
363 * struct sdma_desc - descriptor structor for one transfer
364 * @vd: descriptor for virt dma
365 * @num_bd: number of descriptors currently handling
366 * @bd_phys: physical address of bd
367 * @buf_tail: ID of the buffer that was processed
368 * @buf_ptail: ID of the previous buffer that was processed
369 * @period_len: period length, used in cyclic.
370 * @chn_real_count: the real count updated from bd->mode.count
371 * @chn_count: the transfer count set
372 * @sdmac: sdma_channel pointer
373 * @bd: pointer of allocate bd
376 struct virt_dma_desc vd;
379 unsigned int buf_tail;
380 unsigned int buf_ptail;
381 unsigned int period_len;
382 unsigned int chn_real_count;
383 unsigned int chn_count;
384 struct sdma_channel *sdmac;
385 struct sdma_buffer_descriptor *bd;
389 * struct sdma_channel - housekeeping for a SDMA channel
391 * @vc: virt_dma base structure
392 * @desc: sdma description including vd and other special member
393 * @sdma: pointer to the SDMA engine for this channel
394 * @channel: the channel number, matches dmaengine chan_id + 1
395 * @direction: transfer type. Needed for setting SDMA script
396 * @slave_config: Slave configuration
397 * @peripheral_type: Peripheral type. Needed for setting SDMA script
398 * @event_id0: aka dma request line
399 * @event_id1: for channels that use 2 events
400 * @word_size: peripheral access size
401 * @pc_from_device: script address for those device_2_memory
402 * @pc_to_device: script address for those memory_2_device
403 * @device_to_device: script address for those device_2_device
404 * @pc_to_pc: script address for those memory_2_memory
405 * @flags: loop mode or not
406 * @per_address: peripheral source or destination address in common case
407 * destination address in p_2_p case
408 * @per_address2: peripheral source address in p_2_p case
409 * @event_mask: event mask used in p_2_p script
410 * @watermark_level: value for gReg[7], some script will extend it from
411 * basic watermark such as p_2_p
412 * @shp_addr: value for gReg[6]
413 * @per_addr: value for gReg[2]
414 * @status: status of dma channel
415 * @context_loaded: ensure context is only loaded once
416 * @data: specific sdma interface structure
417 * @bd_pool: dma_pool for bd
418 * @terminate_worker: used to call back into terminate work function
420 struct sdma_channel {
421 struct virt_dma_chan vc;
422 struct sdma_desc *desc;
423 struct sdma_engine *sdma;
424 unsigned int channel;
425 enum dma_transfer_direction direction;
426 struct dma_slave_config slave_config;
427 enum sdma_peripheral_type peripheral_type;
428 unsigned int event_id0;
429 unsigned int event_id1;
430 enum dma_slave_buswidth word_size;
431 unsigned int pc_from_device, pc_to_device;
432 unsigned int device_to_device;
433 unsigned int pc_to_pc;
435 dma_addr_t per_address, per_address2;
436 unsigned long event_mask[2];
437 unsigned long watermark_level;
438 u32 shp_addr, per_addr;
439 enum dma_status status;
440 struct imx_dma_data data;
441 struct work_struct terminate_worker;
442 struct list_head terminated;
446 #define IMX_DMA_SG_LOOP BIT(0)
448 #define MAX_DMA_CHANNELS 32
449 #define MXC_SDMA_DEFAULT_PRIORITY 1
450 #define MXC_SDMA_MIN_PRIORITY 1
451 #define MXC_SDMA_MAX_PRIORITY 7
453 #define SDMA_FIRMWARE_MAGIC 0x414d4453
456 * struct sdma_firmware_header - Layout of the firmware image
459 * @version_major: increased whenever layout of struct
460 * sdma_script_start_addrs changes.
461 * @version_minor: firmware minor version (for binary compatible changes)
462 * @script_addrs_start: offset of struct sdma_script_start_addrs in this image
463 * @num_script_addrs: Number of script addresses in this image
464 * @ram_code_start: offset of SDMA ram image in this firmware image
465 * @ram_code_size: size of SDMA ram image
466 * @script_addrs: Stores the start address of the SDMA scripts
467 * (in SDMA memory space)
469 struct sdma_firmware_header {
473 u32 script_addrs_start;
474 u32 num_script_addrs;
479 struct sdma_driver_data {
482 struct sdma_script_start_addrs *script_addrs;
485 * ecspi ERR009165 fixed should be done in sdma script
486 * and it has been fixed in soc from i.mx6ul.
487 * please get more information from the below link:
488 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
495 struct sdma_channel channel[MAX_DMA_CHANNELS];
496 struct sdma_channel_control *channel_control;
498 struct sdma_context_data *context;
499 dma_addr_t context_phys;
500 struct dma_device dma_device;
503 spinlock_t channel_0_lock;
505 struct sdma_script_start_addrs *script_addrs;
506 const struct sdma_driver_data *drvdata;
511 struct sdma_buffer_descriptor *bd0;
512 /* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/
517 static int sdma_config_write(struct dma_chan *chan,
518 struct dma_slave_config *dmaengine_cfg,
519 enum dma_transfer_direction direction);
521 static struct sdma_driver_data sdma_imx31 = {
522 .chnenbl0 = SDMA_CHNENBL0_IMX31,
526 static struct sdma_script_start_addrs sdma_script_imx25 = {
528 .uart_2_mcu_addr = 904,
529 .per_2_app_addr = 1255,
530 .mcu_2_app_addr = 834,
531 .uartsh_2_mcu_addr = 1120,
532 .per_2_shp_addr = 1329,
533 .mcu_2_shp_addr = 1048,
534 .ata_2_mcu_addr = 1560,
535 .mcu_2_ata_addr = 1479,
536 .app_2_per_addr = 1189,
537 .app_2_mcu_addr = 770,
538 .shp_2_per_addr = 1407,
539 .shp_2_mcu_addr = 979,
542 static struct sdma_driver_data sdma_imx25 = {
543 .chnenbl0 = SDMA_CHNENBL0_IMX35,
545 .script_addrs = &sdma_script_imx25,
548 static struct sdma_driver_data sdma_imx35 = {
549 .chnenbl0 = SDMA_CHNENBL0_IMX35,
553 static struct sdma_script_start_addrs sdma_script_imx51 = {
555 .uart_2_mcu_addr = 817,
556 .mcu_2_app_addr = 747,
557 .mcu_2_shp_addr = 961,
558 .ata_2_mcu_addr = 1473,
559 .mcu_2_ata_addr = 1392,
560 .app_2_per_addr = 1033,
561 .app_2_mcu_addr = 683,
562 .shp_2_per_addr = 1251,
563 .shp_2_mcu_addr = 892,
566 static struct sdma_driver_data sdma_imx51 = {
567 .chnenbl0 = SDMA_CHNENBL0_IMX35,
569 .script_addrs = &sdma_script_imx51,
572 static struct sdma_script_start_addrs sdma_script_imx53 = {
574 .app_2_mcu_addr = 683,
575 .mcu_2_app_addr = 747,
576 .uart_2_mcu_addr = 817,
577 .shp_2_mcu_addr = 891,
578 .mcu_2_shp_addr = 960,
579 .uartsh_2_mcu_addr = 1032,
580 .spdif_2_mcu_addr = 1100,
581 .mcu_2_spdif_addr = 1134,
582 .firi_2_mcu_addr = 1193,
583 .mcu_2_firi_addr = 1290,
586 static struct sdma_driver_data sdma_imx53 = {
587 .chnenbl0 = SDMA_CHNENBL0_IMX35,
589 .script_addrs = &sdma_script_imx53,
592 static struct sdma_script_start_addrs sdma_script_imx6q = {
594 .uart_2_mcu_addr = 817,
595 .mcu_2_app_addr = 747,
596 .per_2_per_addr = 6331,
597 .uartsh_2_mcu_addr = 1032,
598 .mcu_2_shp_addr = 960,
599 .app_2_mcu_addr = 683,
600 .shp_2_mcu_addr = 891,
601 .spdif_2_mcu_addr = 1100,
602 .mcu_2_spdif_addr = 1134,
605 static struct sdma_driver_data sdma_imx6q = {
606 .chnenbl0 = SDMA_CHNENBL0_IMX35,
608 .script_addrs = &sdma_script_imx6q,
611 static struct sdma_driver_data sdma_imx6ul = {
612 .chnenbl0 = SDMA_CHNENBL0_IMX35,
614 .script_addrs = &sdma_script_imx6q,
618 static struct sdma_script_start_addrs sdma_script_imx7d = {
620 .uart_2_mcu_addr = 819,
621 .mcu_2_app_addr = 749,
622 .uartsh_2_mcu_addr = 1034,
623 .mcu_2_shp_addr = 962,
624 .app_2_mcu_addr = 685,
625 .shp_2_mcu_addr = 893,
626 .spdif_2_mcu_addr = 1102,
627 .mcu_2_spdif_addr = 1136,
630 static struct sdma_driver_data sdma_imx7d = {
631 .chnenbl0 = SDMA_CHNENBL0_IMX35,
633 .script_addrs = &sdma_script_imx7d,
636 static struct sdma_driver_data sdma_imx8mq = {
637 .chnenbl0 = SDMA_CHNENBL0_IMX35,
639 .script_addrs = &sdma_script_imx7d,
643 static const struct of_device_id sdma_dt_ids[] = {
644 { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
645 { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
646 { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
647 { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
648 { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
649 { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
650 { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
651 { .compatible = "fsl,imx6ul-sdma", .data = &sdma_imx6ul, },
652 { .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, },
655 MODULE_DEVICE_TABLE(of, sdma_dt_ids);
657 #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
658 #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
659 #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
660 #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
662 static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
664 u32 chnenbl0 = sdma->drvdata->chnenbl0;
665 return chnenbl0 + event * 4;
668 static int sdma_config_ownership(struct sdma_channel *sdmac,
669 bool event_override, bool mcu_override, bool dsp_override)
671 struct sdma_engine *sdma = sdmac->sdma;
672 int channel = sdmac->channel;
673 unsigned long evt, mcu, dsp;
675 if (event_override && mcu_override && dsp_override)
678 evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
679 mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
680 dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
683 __clear_bit(channel, &dsp);
685 __set_bit(channel, &dsp);
688 __clear_bit(channel, &evt);
690 __set_bit(channel, &evt);
693 __clear_bit(channel, &mcu);
695 __set_bit(channel, &mcu);
697 writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
698 writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
699 writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
704 static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
706 writel(BIT(channel), sdma->regs + SDMA_H_START);
710 * sdma_run_channel0 - run a channel and wait till it's done
712 static int sdma_run_channel0(struct sdma_engine *sdma)
717 sdma_enable_channel(sdma, 0);
719 ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
720 reg, !(reg & 1), 1, 500);
722 dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
724 /* Set bits of CONFIG register with dynamic context switching */
725 reg = readl(sdma->regs + SDMA_H_CONFIG);
726 if ((reg & SDMA_H_CONFIG_CSM) == 0) {
727 reg |= SDMA_H_CONFIG_CSM;
728 writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG);
734 static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
737 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
743 buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL);
747 spin_lock_irqsave(&sdma->channel_0_lock, flags);
749 bd0->mode.command = C0_SETPM;
750 bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
751 bd0->mode.count = size / 2;
752 bd0->buffer_addr = buf_phys;
753 bd0->ext_buffer_addr = address;
755 memcpy(buf_virt, buf, size);
757 ret = sdma_run_channel0(sdma);
759 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
761 dma_free_coherent(sdma->dev, size, buf_virt, buf_phys);
766 static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
768 struct sdma_engine *sdma = sdmac->sdma;
769 int channel = sdmac->channel;
771 u32 chnenbl = chnenbl_ofs(sdma, event);
773 val = readl_relaxed(sdma->regs + chnenbl);
774 __set_bit(channel, &val);
775 writel_relaxed(val, sdma->regs + chnenbl);
778 static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
780 struct sdma_engine *sdma = sdmac->sdma;
781 int channel = sdmac->channel;
782 u32 chnenbl = chnenbl_ofs(sdma, event);
785 val = readl_relaxed(sdma->regs + chnenbl);
786 __clear_bit(channel, &val);
787 writel_relaxed(val, sdma->regs + chnenbl);
790 static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
792 return container_of(t, struct sdma_desc, vd.tx);
795 static void sdma_start_desc(struct sdma_channel *sdmac)
797 struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
798 struct sdma_desc *desc;
799 struct sdma_engine *sdma = sdmac->sdma;
800 int channel = sdmac->channel;
806 sdmac->desc = desc = to_sdma_desc(&vd->tx);
810 sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
811 sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
812 sdma_enable_channel(sdma, sdmac->channel);
815 static void sdma_update_channel_loop(struct sdma_channel *sdmac)
817 struct sdma_buffer_descriptor *bd;
819 enum dma_status old_status = sdmac->status;
822 * loop mode. Iterate over descriptors, re-setup them and
823 * call callback function.
825 while (sdmac->desc) {
826 struct sdma_desc *desc = sdmac->desc;
828 bd = &desc->bd[desc->buf_tail];
830 if (bd->mode.status & BD_DONE)
833 if (bd->mode.status & BD_RROR) {
834 bd->mode.status &= ~BD_RROR;
835 sdmac->status = DMA_ERROR;
840 * We use bd->mode.count to calculate the residue, since contains
841 * the number of bytes present in the current buffer descriptor.
844 desc->chn_real_count = bd->mode.count;
845 bd->mode.status |= BD_DONE;
846 bd->mode.count = desc->period_len;
847 desc->buf_ptail = desc->buf_tail;
848 desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
851 * The callback is called from the interrupt context in order
852 * to reduce latency and to avoid the risk of altering the
853 * SDMA transaction status by the time the client tasklet is
856 spin_unlock(&sdmac->vc.lock);
857 dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
858 spin_lock(&sdmac->vc.lock);
861 sdmac->status = old_status;
865 static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
867 struct sdma_channel *sdmac = (struct sdma_channel *) data;
868 struct sdma_buffer_descriptor *bd;
871 sdmac->desc->chn_real_count = 0;
873 * non loop mode. Iterate over all descriptors, collect
874 * errors and call callback function
876 for (i = 0; i < sdmac->desc->num_bd; i++) {
877 bd = &sdmac->desc->bd[i];
879 if (bd->mode.status & (BD_DONE | BD_RROR))
881 sdmac->desc->chn_real_count += bd->mode.count;
885 sdmac->status = DMA_ERROR;
887 sdmac->status = DMA_COMPLETE;
890 static irqreturn_t sdma_int_handler(int irq, void *dev_id)
892 struct sdma_engine *sdma = dev_id;
895 stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
896 writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
897 /* channel 0 is special and not handled here, see run_channel0() */
901 int channel = fls(stat) - 1;
902 struct sdma_channel *sdmac = &sdma->channel[channel];
903 struct sdma_desc *desc;
905 spin_lock(&sdmac->vc.lock);
908 if (sdmac->flags & IMX_DMA_SG_LOOP) {
909 sdma_update_channel_loop(sdmac);
911 mxc_sdma_handle_channel_normal(sdmac);
912 vchan_cookie_complete(&desc->vd);
913 sdma_start_desc(sdmac);
917 spin_unlock(&sdmac->vc.lock);
918 __clear_bit(channel, &stat);
925 * sets the pc of SDMA script according to the peripheral type
927 static void sdma_get_pc(struct sdma_channel *sdmac,
928 enum sdma_peripheral_type peripheral_type)
930 struct sdma_engine *sdma = sdmac->sdma;
931 int per_2_emi = 0, emi_2_per = 0;
933 * These are needed once we start to support transfers between
934 * two peripherals or memory-to-memory transfers
936 int per_2_per = 0, emi_2_emi = 0;
938 sdmac->pc_from_device = 0;
939 sdmac->pc_to_device = 0;
940 sdmac->device_to_device = 0;
942 sdmac->is_ram_script = false;
944 switch (peripheral_type) {
945 case IMX_DMATYPE_MEMORY:
946 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
948 case IMX_DMATYPE_DSP:
949 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
950 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
952 case IMX_DMATYPE_FIRI:
953 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
954 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
956 case IMX_DMATYPE_UART:
957 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
958 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
960 case IMX_DMATYPE_UART_SP:
961 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
962 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
964 case IMX_DMATYPE_ATA:
965 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
966 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
968 case IMX_DMATYPE_CSPI:
969 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
971 /* Use rom script mcu_2_app if ERR009165 fixed */
972 if (sdmac->sdma->drvdata->ecspi_fixed) {
973 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
975 emi_2_per = sdma->script_addrs->mcu_2_ecspi_addr;
976 sdmac->is_ram_script = true;
980 case IMX_DMATYPE_EXT:
981 case IMX_DMATYPE_SSI:
982 case IMX_DMATYPE_SAI:
983 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
984 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
986 case IMX_DMATYPE_SSI_DUAL:
987 per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
988 emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
989 sdmac->is_ram_script = true;
991 case IMX_DMATYPE_SSI_SP:
992 case IMX_DMATYPE_MMC:
993 case IMX_DMATYPE_SDHC:
994 case IMX_DMATYPE_CSPI_SP:
995 case IMX_DMATYPE_ESAI:
996 case IMX_DMATYPE_MSHC_SP:
997 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
998 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1000 case IMX_DMATYPE_ASRC:
1001 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
1002 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
1003 per_2_per = sdma->script_addrs->per_2_per_addr;
1004 sdmac->is_ram_script = true;
1006 case IMX_DMATYPE_ASRC_SP:
1007 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
1008 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1009 per_2_per = sdma->script_addrs->per_2_per_addr;
1011 case IMX_DMATYPE_MSHC:
1012 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
1013 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
1015 case IMX_DMATYPE_CCM:
1016 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
1018 case IMX_DMATYPE_SPDIF:
1019 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
1020 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
1022 case IMX_DMATYPE_IPU_MEMORY:
1023 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
1029 sdmac->pc_from_device = per_2_emi;
1030 sdmac->pc_to_device = emi_2_per;
1031 sdmac->device_to_device = per_2_per;
1032 sdmac->pc_to_pc = emi_2_emi;
1035 static int sdma_load_context(struct sdma_channel *sdmac)
1037 struct sdma_engine *sdma = sdmac->sdma;
1038 int channel = sdmac->channel;
1040 struct sdma_context_data *context = sdma->context;
1041 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
1043 unsigned long flags;
1045 if (sdmac->direction == DMA_DEV_TO_MEM)
1046 load_address = sdmac->pc_from_device;
1047 else if (sdmac->direction == DMA_DEV_TO_DEV)
1048 load_address = sdmac->device_to_device;
1049 else if (sdmac->direction == DMA_MEM_TO_MEM)
1050 load_address = sdmac->pc_to_pc;
1052 load_address = sdmac->pc_to_device;
1054 if (load_address < 0)
1055 return load_address;
1057 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
1058 dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
1059 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
1060 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
1061 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
1062 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
1064 spin_lock_irqsave(&sdma->channel_0_lock, flags);
1066 memset(context, 0, sizeof(*context));
1067 context->channel_state.pc = load_address;
1069 /* Send by context the event mask,base address for peripheral
1070 * and watermark level
1072 context->gReg[0] = sdmac->event_mask[1];
1073 context->gReg[1] = sdmac->event_mask[0];
1074 context->gReg[2] = sdmac->per_addr;
1075 context->gReg[6] = sdmac->shp_addr;
1076 context->gReg[7] = sdmac->watermark_level;
1078 bd0->mode.command = C0_SETDM;
1079 bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
1080 bd0->mode.count = sizeof(*context) / 4;
1081 bd0->buffer_addr = sdma->context_phys;
1082 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
1083 ret = sdma_run_channel0(sdma);
1085 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
1090 static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
1092 return container_of(chan, struct sdma_channel, vc.chan);
1095 static int sdma_disable_channel(struct dma_chan *chan)
1097 struct sdma_channel *sdmac = to_sdma_chan(chan);
1098 struct sdma_engine *sdma = sdmac->sdma;
1099 int channel = sdmac->channel;
1101 writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
1102 sdmac->status = DMA_ERROR;
1106 static void sdma_channel_terminate_work(struct work_struct *work)
1108 struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1111 * According to NXP R&D team a delay of one BD SDMA cost time
1112 * (maximum is 1ms) should be added after disable of the channel
1113 * bit, to ensure SDMA core has really been stopped after SDMA
1114 * clients call .device_terminate_all.
1116 usleep_range(1000, 2000);
1118 vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
1121 static int sdma_terminate_all(struct dma_chan *chan)
1123 struct sdma_channel *sdmac = to_sdma_chan(chan);
1124 unsigned long flags;
1126 spin_lock_irqsave(&sdmac->vc.lock, flags);
1128 sdma_disable_channel(chan);
1131 vchan_terminate_vdesc(&sdmac->desc->vd);
1133 * move out current descriptor into terminated list so that
1134 * it could be free in sdma_channel_terminate_work alone
1135 * later without potential involving next descriptor raised
1136 * up before the last descriptor terminated.
1138 vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
1140 schedule_work(&sdmac->terminate_worker);
1143 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1148 static void sdma_channel_synchronize(struct dma_chan *chan)
1150 struct sdma_channel *sdmac = to_sdma_chan(chan);
1152 vchan_synchronize(&sdmac->vc);
1154 flush_work(&sdmac->terminate_worker);
1157 static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
1159 struct sdma_engine *sdma = sdmac->sdma;
1161 int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
1162 int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
1164 set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
1165 set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
1167 if (sdmac->event_id0 > 31)
1168 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
1170 if (sdmac->event_id1 > 31)
1171 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
1174 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
1175 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
1176 * r0(event_mask[1]) and r1(event_mask[0]).
1179 sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
1180 SDMA_WATERMARK_LEVEL_HWML);
1181 sdmac->watermark_level |= hwml;
1182 sdmac->watermark_level |= lwml << 16;
1183 swap(sdmac->event_mask[0], sdmac->event_mask[1]);
1186 if (sdmac->per_address2 >= sdma->spba_start_addr &&
1187 sdmac->per_address2 <= sdma->spba_end_addr)
1188 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
1190 if (sdmac->per_address >= sdma->spba_start_addr &&
1191 sdmac->per_address <= sdma->spba_end_addr)
1192 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
1194 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
1197 static int sdma_config_channel(struct dma_chan *chan)
1199 struct sdma_channel *sdmac = to_sdma_chan(chan);
1201 sdma_disable_channel(chan);
1203 sdmac->event_mask[0] = 0;
1204 sdmac->event_mask[1] = 0;
1205 sdmac->shp_addr = 0;
1206 sdmac->per_addr = 0;
1208 switch (sdmac->peripheral_type) {
1209 case IMX_DMATYPE_DSP:
1210 sdma_config_ownership(sdmac, false, true, true);
1212 case IMX_DMATYPE_MEMORY:
1213 sdma_config_ownership(sdmac, false, true, false);
1216 sdma_config_ownership(sdmac, true, true, false);
1220 sdma_get_pc(sdmac, sdmac->peripheral_type);
1222 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
1223 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
1224 /* Handle multiple event channels differently */
1225 if (sdmac->event_id1) {
1226 if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
1227 sdmac->peripheral_type == IMX_DMATYPE_ASRC)
1228 sdma_set_watermarklevel_for_p2p(sdmac);
1230 __set_bit(sdmac->event_id0, sdmac->event_mask);
1234 sdmac->shp_addr = sdmac->per_address;
1235 sdmac->per_addr = sdmac->per_address2;
1237 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
1243 static int sdma_set_channel_priority(struct sdma_channel *sdmac,
1244 unsigned int priority)
1246 struct sdma_engine *sdma = sdmac->sdma;
1247 int channel = sdmac->channel;
1249 if (priority < MXC_SDMA_MIN_PRIORITY
1250 || priority > MXC_SDMA_MAX_PRIORITY) {
1254 writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
1259 static int sdma_request_channel0(struct sdma_engine *sdma)
1263 sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys,
1270 sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
1271 sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
1273 sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
1281 static int sdma_alloc_bd(struct sdma_desc *desc)
1283 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1286 desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size,
1287 &desc->bd_phys, GFP_NOWAIT);
1296 static void sdma_free_bd(struct sdma_desc *desc)
1298 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1300 dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd,
1304 static void sdma_desc_free(struct virt_dma_desc *vd)
1306 struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
1312 static int sdma_alloc_chan_resources(struct dma_chan *chan)
1314 struct sdma_channel *sdmac = to_sdma_chan(chan);
1315 struct imx_dma_data *data = chan->private;
1316 struct imx_dma_data mem_data;
1320 * MEMCPY may never setup chan->private by filter function such as
1321 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
1322 * Please note in any other slave case, you have to setup chan->private
1323 * with 'struct imx_dma_data' in your own filter function if you want to
1324 * request dma channel by dma_request_channel() rather than
1325 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
1326 * to warn you to correct your filter function.
1329 dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
1330 mem_data.priority = 2;
1331 mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
1332 mem_data.dma_request = 0;
1333 mem_data.dma_request2 = 0;
1336 sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
1339 switch (data->priority) {
1343 case DMA_PRIO_MEDIUM:
1352 sdmac->peripheral_type = data->peripheral_type;
1353 sdmac->event_id0 = data->dma_request;
1354 sdmac->event_id1 = data->dma_request2;
1356 ret = clk_enable(sdmac->sdma->clk_ipg);
1359 ret = clk_enable(sdmac->sdma->clk_ahb);
1361 goto disable_clk_ipg;
1363 ret = sdma_set_channel_priority(sdmac, prio);
1365 goto disable_clk_ahb;
1370 clk_disable(sdmac->sdma->clk_ahb);
1372 clk_disable(sdmac->sdma->clk_ipg);
1376 static void sdma_free_chan_resources(struct dma_chan *chan)
1378 struct sdma_channel *sdmac = to_sdma_chan(chan);
1379 struct sdma_engine *sdma = sdmac->sdma;
1381 sdma_terminate_all(chan);
1383 sdma_channel_synchronize(chan);
1385 sdma_event_disable(sdmac, sdmac->event_id0);
1386 if (sdmac->event_id1)
1387 sdma_event_disable(sdmac, sdmac->event_id1);
1389 sdmac->event_id0 = 0;
1390 sdmac->event_id1 = 0;
1392 sdma_set_channel_priority(sdmac, 0);
1394 clk_disable(sdma->clk_ipg);
1395 clk_disable(sdma->clk_ahb);
1398 static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
1399 enum dma_transfer_direction direction, u32 bds)
1401 struct sdma_desc *desc;
1403 if (!sdmac->sdma->fw_loaded && sdmac->is_ram_script) {
1404 dev_warn_once(sdmac->sdma->dev, "sdma firmware not ready!\n");
1408 desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
1412 sdmac->status = DMA_IN_PROGRESS;
1413 sdmac->direction = direction;
1416 desc->chn_count = 0;
1417 desc->chn_real_count = 0;
1419 desc->buf_ptail = 0;
1420 desc->sdmac = sdmac;
1423 if (sdma_alloc_bd(desc))
1426 /* No slave_config called in MEMCPY case, so do here */
1427 if (direction == DMA_MEM_TO_MEM)
1428 sdma_config_ownership(sdmac, false, true, false);
1430 if (sdma_load_context(sdmac))
1441 static struct dma_async_tx_descriptor *sdma_prep_memcpy(
1442 struct dma_chan *chan, dma_addr_t dma_dst,
1443 dma_addr_t dma_src, size_t len, unsigned long flags)
1445 struct sdma_channel *sdmac = to_sdma_chan(chan);
1446 struct sdma_engine *sdma = sdmac->sdma;
1447 int channel = sdmac->channel;
1450 struct sdma_buffer_descriptor *bd;
1451 struct sdma_desc *desc;
1456 dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
1457 &dma_src, &dma_dst, len, channel);
1459 desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
1460 len / SDMA_BD_MAX_CNT + 1);
1465 count = min_t(size_t, len, SDMA_BD_MAX_CNT);
1467 bd->buffer_addr = dma_src;
1468 bd->ext_buffer_addr = dma_dst;
1469 bd->mode.count = count;
1470 desc->chn_count += count;
1471 bd->mode.command = 0;
1478 param = BD_DONE | BD_EXTD | BD_CONT;
1486 dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
1487 i, count, bd->buffer_addr,
1488 param & BD_WRAP ? "wrap" : "",
1489 param & BD_INTR ? " intr" : "");
1491 bd->mode.status = param;
1494 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1497 static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
1498 struct dma_chan *chan, struct scatterlist *sgl,
1499 unsigned int sg_len, enum dma_transfer_direction direction,
1500 unsigned long flags, void *context)
1502 struct sdma_channel *sdmac = to_sdma_chan(chan);
1503 struct sdma_engine *sdma = sdmac->sdma;
1505 int channel = sdmac->channel;
1506 struct scatterlist *sg;
1507 struct sdma_desc *desc;
1509 sdma_config_write(chan, &sdmac->slave_config, direction);
1511 desc = sdma_transfer_init(sdmac, direction, sg_len);
1515 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
1518 for_each_sg(sgl, sg, sg_len, i) {
1519 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1522 bd->buffer_addr = sg->dma_address;
1524 count = sg_dma_len(sg);
1526 if (count > SDMA_BD_MAX_CNT) {
1527 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
1528 channel, count, SDMA_BD_MAX_CNT);
1532 bd->mode.count = count;
1533 desc->chn_count += count;
1535 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1538 switch (sdmac->word_size) {
1539 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1540 bd->mode.command = 0;
1541 if (count & 3 || sg->dma_address & 3)
1544 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1545 bd->mode.command = 2;
1546 if (count & 1 || sg->dma_address & 1)
1549 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1550 bd->mode.command = 1;
1556 param = BD_DONE | BD_EXTD | BD_CONT;
1558 if (i + 1 == sg_len) {
1564 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1565 i, count, (u64)sg->dma_address,
1566 param & BD_WRAP ? "wrap" : "",
1567 param & BD_INTR ? " intr" : "");
1569 bd->mode.status = param;
1572 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1577 sdmac->status = DMA_ERROR;
1581 static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1582 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1583 size_t period_len, enum dma_transfer_direction direction,
1584 unsigned long flags)
1586 struct sdma_channel *sdmac = to_sdma_chan(chan);
1587 struct sdma_engine *sdma = sdmac->sdma;
1588 int num_periods = buf_len / period_len;
1589 int channel = sdmac->channel;
1591 struct sdma_desc *desc;
1593 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1595 sdma_config_write(chan, &sdmac->slave_config, direction);
1597 desc = sdma_transfer_init(sdmac, direction, num_periods);
1601 desc->period_len = period_len;
1603 sdmac->flags |= IMX_DMA_SG_LOOP;
1605 if (period_len > SDMA_BD_MAX_CNT) {
1606 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
1607 channel, period_len, SDMA_BD_MAX_CNT);
1611 while (buf < buf_len) {
1612 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1615 bd->buffer_addr = dma_addr;
1617 bd->mode.count = period_len;
1619 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1621 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1622 bd->mode.command = 0;
1624 bd->mode.command = sdmac->word_size;
1626 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1627 if (i + 1 == num_periods)
1630 dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1631 i, period_len, (u64)dma_addr,
1632 param & BD_WRAP ? "wrap" : "",
1633 param & BD_INTR ? " intr" : "");
1635 bd->mode.status = param;
1637 dma_addr += period_len;
1643 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1648 sdmac->status = DMA_ERROR;
1652 static int sdma_config_write(struct dma_chan *chan,
1653 struct dma_slave_config *dmaengine_cfg,
1654 enum dma_transfer_direction direction)
1656 struct sdma_channel *sdmac = to_sdma_chan(chan);
1658 if (direction == DMA_DEV_TO_MEM) {
1659 sdmac->per_address = dmaengine_cfg->src_addr;
1660 sdmac->watermark_level = dmaengine_cfg->src_maxburst *
1661 dmaengine_cfg->src_addr_width;
1662 sdmac->word_size = dmaengine_cfg->src_addr_width;
1663 } else if (direction == DMA_DEV_TO_DEV) {
1664 sdmac->per_address2 = dmaengine_cfg->src_addr;
1665 sdmac->per_address = dmaengine_cfg->dst_addr;
1666 sdmac->watermark_level = dmaengine_cfg->src_maxburst &
1667 SDMA_WATERMARK_LEVEL_LWML;
1668 sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
1669 SDMA_WATERMARK_LEVEL_HWML;
1670 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1672 sdmac->per_address = dmaengine_cfg->dst_addr;
1673 sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
1674 dmaengine_cfg->dst_addr_width;
1675 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1677 sdmac->direction = direction;
1678 return sdma_config_channel(chan);
1681 static int sdma_config(struct dma_chan *chan,
1682 struct dma_slave_config *dmaengine_cfg)
1684 struct sdma_channel *sdmac = to_sdma_chan(chan);
1686 memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1688 /* Set ENBLn earlier to make sure dma request triggered after that */
1689 if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1691 sdma_event_enable(sdmac, sdmac->event_id0);
1693 if (sdmac->event_id1) {
1694 if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1696 sdma_event_enable(sdmac, sdmac->event_id1);
1702 static enum dma_status sdma_tx_status(struct dma_chan *chan,
1703 dma_cookie_t cookie,
1704 struct dma_tx_state *txstate)
1706 struct sdma_channel *sdmac = to_sdma_chan(chan);
1707 struct sdma_desc *desc = NULL;
1709 struct virt_dma_desc *vd;
1710 enum dma_status ret;
1711 unsigned long flags;
1713 ret = dma_cookie_status(chan, cookie, txstate);
1714 if (ret == DMA_COMPLETE || !txstate)
1717 spin_lock_irqsave(&sdmac->vc.lock, flags);
1719 vd = vchan_find_desc(&sdmac->vc, cookie);
1721 desc = to_sdma_desc(&vd->tx);
1722 else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie)
1726 if (sdmac->flags & IMX_DMA_SG_LOOP)
1727 residue = (desc->num_bd - desc->buf_ptail) *
1728 desc->period_len - desc->chn_real_count;
1730 residue = desc->chn_count - desc->chn_real_count;
1735 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1737 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1740 return sdmac->status;
1743 static void sdma_issue_pending(struct dma_chan *chan)
1745 struct sdma_channel *sdmac = to_sdma_chan(chan);
1746 unsigned long flags;
1748 spin_lock_irqsave(&sdmac->vc.lock, flags);
1749 if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
1750 sdma_start_desc(sdmac);
1751 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1754 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1755 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
1756 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 45
1757 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 46
1759 static void sdma_add_scripts(struct sdma_engine *sdma,
1760 const struct sdma_script_start_addrs *addr)
1762 s32 *addr_arr = (u32 *)addr;
1763 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1766 /* use the default firmware in ROM if missing external firmware */
1767 if (!sdma->script_number)
1768 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1770 if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
1773 "SDMA script number %d not match with firmware.\n",
1774 sdma->script_number);
1778 for (i = 0; i < sdma->script_number; i++)
1779 if (addr_arr[i] > 0)
1780 saddr_arr[i] = addr_arr[i];
1783 * get uart_2_mcu_addr/uartsh_2_mcu_addr rom script specially because
1784 * they are now replaced by uart_2_mcu_ram_addr/uartsh_2_mcu_ram_addr
1785 * to be compatible with legacy freescale/nxp sdma firmware, and they
1786 * are located in the bottom part of sdma_script_start_addrs which are
1787 * beyond the SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1.
1789 if (addr->uart_2_mcu_addr)
1790 sdma->script_addrs->uart_2_mcu_addr = addr->uart_2_mcu_addr;
1791 if (addr->uartsh_2_mcu_addr)
1792 sdma->script_addrs->uartsh_2_mcu_addr = addr->uartsh_2_mcu_addr;
1796 static void sdma_load_firmware(const struct firmware *fw, void *context)
1798 struct sdma_engine *sdma = context;
1799 const struct sdma_firmware_header *header;
1800 const struct sdma_script_start_addrs *addr;
1801 unsigned short *ram_code;
1804 dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
1805 /* In this case we just use the ROM firmware. */
1809 if (fw->size < sizeof(*header))
1812 header = (struct sdma_firmware_header *)fw->data;
1814 if (header->magic != SDMA_FIRMWARE_MAGIC)
1816 if (header->ram_code_start + header->ram_code_size > fw->size)
1818 switch (header->version_major) {
1820 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1823 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1826 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1829 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1832 dev_err(sdma->dev, "unknown firmware version\n");
1836 addr = (void *)header + header->script_addrs_start;
1837 ram_code = (void *)header + header->ram_code_start;
1839 clk_enable(sdma->clk_ipg);
1840 clk_enable(sdma->clk_ahb);
1841 /* download the RAM image for SDMA */
1842 sdma_load_script(sdma, ram_code,
1843 header->ram_code_size,
1844 addr->ram_code_start_addr);
1845 clk_disable(sdma->clk_ipg);
1846 clk_disable(sdma->clk_ahb);
1848 sdma_add_scripts(sdma, addr);
1850 sdma->fw_loaded = true;
1852 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1853 header->version_major,
1854 header->version_minor);
1857 release_firmware(fw);
1860 #define EVENT_REMAP_CELLS 3
1862 static int sdma_event_remap(struct sdma_engine *sdma)
1864 struct device_node *np = sdma->dev->of_node;
1865 struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1866 struct property *event_remap;
1868 char propname[] = "fsl,sdma-event-remap";
1869 u32 reg, val, shift, num_map, i;
1872 if (IS_ERR(np) || IS_ERR(gpr_np))
1875 event_remap = of_find_property(np, propname, NULL);
1876 num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1878 dev_dbg(sdma->dev, "no event needs to be remapped\n");
1880 } else if (num_map % EVENT_REMAP_CELLS) {
1881 dev_err(sdma->dev, "the property %s must modulo %d\n",
1882 propname, EVENT_REMAP_CELLS);
1887 gpr = syscon_node_to_regmap(gpr_np);
1889 dev_err(sdma->dev, "failed to get gpr regmap\n");
1894 for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1895 ret = of_property_read_u32_index(np, propname, i, ®);
1897 dev_err(sdma->dev, "failed to read property %s index %d\n",
1902 ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1904 dev_err(sdma->dev, "failed to read property %s index %d\n",
1909 ret = of_property_read_u32_index(np, propname, i + 2, &val);
1911 dev_err(sdma->dev, "failed to read property %s index %d\n",
1916 regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1920 if (!IS_ERR(gpr_np))
1921 of_node_put(gpr_np);
1926 static int sdma_get_firmware(struct sdma_engine *sdma,
1927 const char *fw_name)
1931 ret = request_firmware_nowait(THIS_MODULE,
1932 FW_ACTION_UEVENT, fw_name, sdma->dev,
1933 GFP_KERNEL, sdma, sdma_load_firmware);
1938 static int sdma_init(struct sdma_engine *sdma)
1941 dma_addr_t ccb_phys;
1943 ret = clk_enable(sdma->clk_ipg);
1946 ret = clk_enable(sdma->clk_ahb);
1948 goto disable_clk_ipg;
1950 if (sdma->drvdata->check_ratio &&
1951 (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg)))
1952 sdma->clk_ratio = 1;
1954 /* Be sure SDMA has not started yet */
1955 writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
1957 sdma->channel_control = dma_alloc_coherent(sdma->dev,
1958 MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control) +
1959 sizeof(struct sdma_context_data),
1960 &ccb_phys, GFP_KERNEL);
1962 if (!sdma->channel_control) {
1967 sdma->context = (void *)sdma->channel_control +
1968 MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
1969 sdma->context_phys = ccb_phys +
1970 MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
1972 /* disable all channels */
1973 for (i = 0; i < sdma->drvdata->num_events; i++)
1974 writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
1976 /* All channels have priority 0 */
1977 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1978 writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1980 ret = sdma_request_channel0(sdma);
1984 sdma_config_ownership(&sdma->channel[0], false, true, false);
1986 /* Set Command Channel (Channel Zero) */
1987 writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
1989 /* Set bits of CONFIG register but with static context switching */
1990 if (sdma->clk_ratio)
1991 writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG);
1993 writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
1995 writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1997 /* Initializes channel's priorities */
1998 sdma_set_channel_priority(&sdma->channel[0], 7);
2000 clk_disable(sdma->clk_ipg);
2001 clk_disable(sdma->clk_ahb);
2006 clk_disable(sdma->clk_ahb);
2008 clk_disable(sdma->clk_ipg);
2009 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
2013 static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
2015 struct sdma_channel *sdmac = to_sdma_chan(chan);
2016 struct imx_dma_data *data = fn_param;
2018 if (!imx_dma_is_general_purpose(chan))
2021 sdmac->data = *data;
2022 chan->private = &sdmac->data;
2027 static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
2028 struct of_dma *ofdma)
2030 struct sdma_engine *sdma = ofdma->of_dma_data;
2031 dma_cap_mask_t mask = sdma->dma_device.cap_mask;
2032 struct imx_dma_data data;
2034 if (dma_spec->args_count != 3)
2037 data.dma_request = dma_spec->args[0];
2038 data.peripheral_type = dma_spec->args[1];
2039 data.priority = dma_spec->args[2];
2041 * init dma_request2 to zero, which is not used by the dts.
2042 * For P2P, dma_request2 is init from dma_request_channel(),
2043 * chan->private will point to the imx_dma_data, and in
2044 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
2045 * be set to sdmac->event_id1.
2047 data.dma_request2 = 0;
2049 return __dma_request_channel(&mask, sdma_filter_fn, &data,
2053 static int sdma_probe(struct platform_device *pdev)
2055 struct device_node *np = pdev->dev.of_node;
2056 struct device_node *spba_bus;
2057 const char *fw_name;
2060 struct resource *iores;
2061 struct resource spba_res;
2063 struct sdma_engine *sdma;
2066 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2070 sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
2074 spin_lock_init(&sdma->channel_0_lock);
2076 sdma->dev = &pdev->dev;
2077 sdma->drvdata = of_device_get_match_data(sdma->dev);
2079 irq = platform_get_irq(pdev, 0);
2083 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2084 sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
2085 if (IS_ERR(sdma->regs))
2086 return PTR_ERR(sdma->regs);
2088 sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2089 if (IS_ERR(sdma->clk_ipg))
2090 return PTR_ERR(sdma->clk_ipg);
2092 sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2093 if (IS_ERR(sdma->clk_ahb))
2094 return PTR_ERR(sdma->clk_ahb);
2096 ret = clk_prepare(sdma->clk_ipg);
2100 ret = clk_prepare(sdma->clk_ahb);
2104 ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
2111 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2112 if (!sdma->script_addrs) {
2117 /* initially no scripts available */
2118 saddr_arr = (s32 *)sdma->script_addrs;
2119 for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++)
2120 saddr_arr[i] = -EINVAL;
2122 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
2123 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
2124 dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
2126 INIT_LIST_HEAD(&sdma->dma_device.channels);
2127 /* Initialize channel parameters */
2128 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2129 struct sdma_channel *sdmac = &sdma->channel[i];
2134 sdmac->vc.desc_free = sdma_desc_free;
2135 INIT_LIST_HEAD(&sdmac->terminated);
2136 INIT_WORK(&sdmac->terminate_worker,
2137 sdma_channel_terminate_work);
2139 * Add the channel to the DMAC list. Do not add channel 0 though
2140 * because we need it internally in the SDMA driver. This also means
2141 * that channel 0 in dmaengine counting matches sdma channel 1.
2144 vchan_init(&sdmac->vc, &sdma->dma_device);
2147 ret = sdma_init(sdma);
2151 ret = sdma_event_remap(sdma);
2155 if (sdma->drvdata->script_addrs)
2156 sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
2158 sdma->dma_device.dev = &pdev->dev;
2160 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
2161 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
2162 sdma->dma_device.device_tx_status = sdma_tx_status;
2163 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
2164 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
2165 sdma->dma_device.device_config = sdma_config;
2166 sdma->dma_device.device_terminate_all = sdma_terminate_all;
2167 sdma->dma_device.device_synchronize = sdma_channel_synchronize;
2168 sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2169 sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2170 sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
2171 sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
2172 sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
2173 sdma->dma_device.device_issue_pending = sdma_issue_pending;
2174 sdma->dma_device.copy_align = 2;
2175 dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
2177 platform_set_drvdata(pdev, sdma);
2179 ret = dma_async_device_register(&sdma->dma_device);
2181 dev_err(&pdev->dev, "unable to register\n");
2186 ret = of_dma_controller_register(np, sdma_xlate, sdma);
2188 dev_err(&pdev->dev, "failed to register controller\n");
2192 spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
2193 ret = of_address_to_resource(spba_bus, 0, &spba_res);
2195 sdma->spba_start_addr = spba_res.start;
2196 sdma->spba_end_addr = spba_res.end;
2198 of_node_put(spba_bus);
2202 * Because that device tree does not encode ROM script address,
2203 * the RAM script in firmware is mandatory for device tree
2204 * probe, otherwise it fails.
2206 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
2209 dev_warn(&pdev->dev, "failed to get firmware name\n");
2211 ret = sdma_get_firmware(sdma, fw_name);
2213 dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
2219 dma_async_device_unregister(&sdma->dma_device);
2221 kfree(sdma->script_addrs);
2223 clk_unprepare(sdma->clk_ahb);
2225 clk_unprepare(sdma->clk_ipg);
2229 static int sdma_remove(struct platform_device *pdev)
2231 struct sdma_engine *sdma = platform_get_drvdata(pdev);
2234 devm_free_irq(&pdev->dev, sdma->irq, sdma);
2235 dma_async_device_unregister(&sdma->dma_device);
2236 kfree(sdma->script_addrs);
2237 clk_unprepare(sdma->clk_ahb);
2238 clk_unprepare(sdma->clk_ipg);
2239 /* Kill the tasklet */
2240 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2241 struct sdma_channel *sdmac = &sdma->channel[i];
2243 tasklet_kill(&sdmac->vc.task);
2244 sdma_free_chan_resources(&sdmac->vc.chan);
2247 platform_set_drvdata(pdev, NULL);
2251 static struct platform_driver sdma_driver = {
2254 .of_match_table = sdma_dt_ids,
2256 .remove = sdma_remove,
2257 .probe = sdma_probe,
2260 module_platform_driver(sdma_driver);
2263 MODULE_DESCRIPTION("i.MX SDMA driver");
2264 #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2265 MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2267 #if IS_ENABLED(CONFIG_SOC_IMX7D)
2268 MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2270 MODULE_LICENSE("GPL");