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/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-sdma.h>
39 #include <linux/platform_data/dma-imx.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
79 * Buffer descriptor status values.
90 * Data Node descriptor status values.
92 #define DND_END_OF_FRAME 0x80
93 #define DND_END_OF_XFER 0x40
95 #define DND_UNUSED 0x01
98 * IPCV2 descriptor status values.
100 #define BD_IPCV2_END_OF_FRAME 0x40
102 #define IPCV2_MAX_NODES 50
104 * Error bit set in the CCB status field by the SDMA,
105 * in setbd routine, in case of a transfer error
107 #define DATA_ERROR 0x10000000
110 * Buffer descriptor commands.
115 #define C0_SETCTX 0x07
116 #define C0_GETCTX 0x03
117 #define C0_SETDM 0x01
118 #define C0_SETPM 0x04
119 #define C0_GETDM 0x02
120 #define C0_GETPM 0x08
122 * Change endianness indicator in the BD command field
124 #define CHANGE_ENDIANNESS 0x80
127 * p_2_p watermark_level description
128 * Bits Name Description
129 * 0-7 Lower WML Lower watermark level
130 * 8 PS 1: Pad Swallowing
131 * 0: No Pad Swallowing
134 * 10 SPDIF If this bit is set both source
135 * and destination are on SPBA
136 * 11 Source Bit(SP) 1: Source on SPBA
138 * 12 Destination Bit(DP) 1: Destination on SPBA
139 * 0: Destination on AIPS
140 * 13-15 --------- MUST BE 0
141 * 16-23 Higher WML HWML
142 * 24-27 N Total number of samples after
143 * which Pad adding/Swallowing
144 * must be done. It must be odd.
145 * 28 Lower WML Event(LWE) SDMA events reg to check for
147 * 0: LWE in EVENTS register
148 * 1: LWE in EVENTS2 register
149 * 29 Higher WML Event(HWE) SDMA events reg to check for
151 * 0: HWE in EVENTS register
152 * 1: HWE in EVENTS2 register
153 * 30 --------- MUST BE 0
154 * 31 CONT 1: Amount of samples to be
155 * transferred is unknown and
156 * script will keep on
157 * transferring samples as long as
158 * both events are detected and
159 * script must be manually stopped
161 * 0: The amount of samples to be
162 * transferred is equal to the
163 * count field of mode word
165 #define SDMA_WATERMARK_LEVEL_LWML 0xFF
166 #define SDMA_WATERMARK_LEVEL_PS BIT(8)
167 #define SDMA_WATERMARK_LEVEL_PA BIT(9)
168 #define SDMA_WATERMARK_LEVEL_SPDIF BIT(10)
169 #define SDMA_WATERMARK_LEVEL_SP BIT(11)
170 #define SDMA_WATERMARK_LEVEL_DP BIT(12)
171 #define SDMA_WATERMARK_LEVEL_HWML (0xFF << 16)
172 #define SDMA_WATERMARK_LEVEL_LWE BIT(28)
173 #define SDMA_WATERMARK_LEVEL_HWE BIT(29)
174 #define SDMA_WATERMARK_LEVEL_CONT BIT(31)
176 #define SDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
177 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
178 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
180 #define SDMA_DMA_DIRECTIONS (BIT(DMA_DEV_TO_MEM) | \
181 BIT(DMA_MEM_TO_DEV) | \
185 * Mode/Count of data node descriptors - IPCv2
187 struct sdma_mode_count {
188 #define SDMA_BD_MAX_CNT 0xffff
189 u32 count : 16; /* size of the buffer pointed by this BD */
190 u32 status : 8; /* E,R,I,C,W,D status bits stored here */
191 u32 command : 8; /* command mostly used for channel 0 */
197 struct sdma_buffer_descriptor {
198 struct sdma_mode_count mode;
199 u32 buffer_addr; /* address of the buffer described */
200 u32 ext_buffer_addr; /* extended buffer address */
201 } __attribute__ ((packed));
204 * struct sdma_channel_control - Channel control Block
206 * @current_bd_ptr: current buffer descriptor processed
207 * @base_bd_ptr: first element of buffer descriptor array
208 * @unused: padding. The SDMA engine expects an array of 128 byte
211 struct sdma_channel_control {
215 } __attribute__ ((packed));
218 * struct sdma_state_registers - SDMA context for a channel
220 * @pc: program counter
222 * @t: test bit: status of arithmetic & test instruction
223 * @rpc: return program counter
225 * @sf: source fault while loading data
226 * @spc: loop start program counter
228 * @df: destination fault while storing data
229 * @epc: loop end program counter
232 struct sdma_state_registers {
244 } __attribute__ ((packed));
247 * struct sdma_context_data - sdma context specific to a channel
249 * @channel_state: channel state bits
250 * @gReg: general registers
251 * @mda: burst dma destination address register
252 * @msa: burst dma source address register
253 * @ms: burst dma status register
254 * @md: burst dma data register
255 * @pda: peripheral dma destination address register
256 * @psa: peripheral dma source address register
257 * @ps: peripheral dma status register
258 * @pd: peripheral dma data register
259 * @ca: CRC polynomial register
260 * @cs: CRC accumulator register
261 * @dda: dedicated core destination address register
262 * @dsa: dedicated core source address register
263 * @ds: dedicated core status register
264 * @dd: dedicated core data register
265 * @scratch0: 1st word of dedicated ram for context switch
266 * @scratch1: 2nd word of dedicated ram for context switch
267 * @scratch2: 3rd word of dedicated ram for context switch
268 * @scratch3: 4th word of dedicated ram for context switch
269 * @scratch4: 5th word of dedicated ram for context switch
270 * @scratch5: 6th word of dedicated ram for context switch
271 * @scratch6: 7th word of dedicated ram for context switch
272 * @scratch7: 8th word of dedicated ram for context switch
274 struct sdma_context_data {
275 struct sdma_state_registers channel_state;
299 } __attribute__ ((packed));
305 * struct sdma_desc - descriptor structor for one transfer
306 * @vd: descriptor for virt dma
307 * @num_bd: number of descriptors currently handling
308 * @bd_phys: physical address of bd
309 * @buf_tail: ID of the buffer that was processed
310 * @buf_ptail: ID of the previous buffer that was processed
311 * @period_len: period length, used in cyclic.
312 * @chn_real_count: the real count updated from bd->mode.count
313 * @chn_count: the transfer count set
314 * @sdmac: sdma_channel pointer
315 * @bd: pointer of allocate bd
318 struct virt_dma_desc vd;
321 unsigned int buf_tail;
322 unsigned int buf_ptail;
323 unsigned int period_len;
324 unsigned int chn_real_count;
325 unsigned int chn_count;
326 struct sdma_channel *sdmac;
327 struct sdma_buffer_descriptor *bd;
331 * struct sdma_channel - housekeeping for a SDMA channel
333 * @vc: virt_dma base structure
334 * @desc: sdma description including vd and other special member
335 * @sdma: pointer to the SDMA engine for this channel
336 * @channel: the channel number, matches dmaengine chan_id + 1
337 * @direction: transfer type. Needed for setting SDMA script
338 * @slave_config Slave configuration
339 * @peripheral_type: Peripheral type. Needed for setting SDMA script
340 * @event_id0: aka dma request line
341 * @event_id1: for channels that use 2 events
342 * @word_size: peripheral access size
343 * @pc_from_device: script address for those device_2_memory
344 * @pc_to_device: script address for those memory_2_device
345 * @device_to_device: script address for those device_2_device
346 * @pc_to_pc: script address for those memory_2_memory
347 * @flags: loop mode or not
348 * @per_address: peripheral source or destination address in common case
349 * destination address in p_2_p case
350 * @per_address2: peripheral source address in p_2_p case
351 * @event_mask: event mask used in p_2_p script
352 * @watermark_level: value for gReg[7], some script will extend it from
353 * basic watermark such as p_2_p
354 * @shp_addr: value for gReg[6]
355 * @per_addr: value for gReg[2]
356 * @status: status of dma channel
357 * @data: specific sdma interface structure
358 * @bd_pool: dma_pool for bd
360 struct sdma_channel {
361 struct virt_dma_chan vc;
362 struct sdma_desc *desc;
363 struct sdma_engine *sdma;
364 unsigned int channel;
365 enum dma_transfer_direction direction;
366 struct dma_slave_config slave_config;
367 enum sdma_peripheral_type peripheral_type;
368 unsigned int event_id0;
369 unsigned int event_id1;
370 enum dma_slave_buswidth word_size;
371 unsigned int pc_from_device, pc_to_device;
372 unsigned int device_to_device;
373 unsigned int pc_to_pc;
375 dma_addr_t per_address, per_address2;
376 unsigned long event_mask[2];
377 unsigned long watermark_level;
378 u32 shp_addr, per_addr;
379 enum dma_status status;
380 struct imx_dma_data data;
381 struct work_struct terminate_worker;
384 #define IMX_DMA_SG_LOOP BIT(0)
386 #define MAX_DMA_CHANNELS 32
387 #define MXC_SDMA_DEFAULT_PRIORITY 1
388 #define MXC_SDMA_MIN_PRIORITY 1
389 #define MXC_SDMA_MAX_PRIORITY 7
391 #define SDMA_FIRMWARE_MAGIC 0x414d4453
394 * struct sdma_firmware_header - Layout of the firmware image
397 * @version_major: increased whenever layout of struct
398 * sdma_script_start_addrs changes.
399 * @version_minor: firmware minor version (for binary compatible changes)
400 * @script_addrs_start: offset of struct sdma_script_start_addrs in this image
401 * @num_script_addrs: Number of script addresses in this image
402 * @ram_code_start: offset of SDMA ram image in this firmware image
403 * @ram_code_size: size of SDMA ram image
404 * @script_addrs: Stores the start address of the SDMA scripts
405 * (in SDMA memory space)
407 struct sdma_firmware_header {
411 u32 script_addrs_start;
412 u32 num_script_addrs;
417 struct sdma_driver_data {
420 struct sdma_script_start_addrs *script_addrs;
425 struct device_dma_parameters dma_parms;
426 struct sdma_channel channel[MAX_DMA_CHANNELS];
427 struct sdma_channel_control *channel_control;
429 struct sdma_context_data *context;
430 dma_addr_t context_phys;
431 struct dma_device dma_device;
434 spinlock_t channel_0_lock;
436 struct sdma_script_start_addrs *script_addrs;
437 const struct sdma_driver_data *drvdata;
442 struct sdma_buffer_descriptor *bd0;
445 static int sdma_config_write(struct dma_chan *chan,
446 struct dma_slave_config *dmaengine_cfg,
447 enum dma_transfer_direction direction);
449 static struct sdma_driver_data sdma_imx31 = {
450 .chnenbl0 = SDMA_CHNENBL0_IMX31,
454 static struct sdma_script_start_addrs sdma_script_imx25 = {
456 .uart_2_mcu_addr = 904,
457 .per_2_app_addr = 1255,
458 .mcu_2_app_addr = 834,
459 .uartsh_2_mcu_addr = 1120,
460 .per_2_shp_addr = 1329,
461 .mcu_2_shp_addr = 1048,
462 .ata_2_mcu_addr = 1560,
463 .mcu_2_ata_addr = 1479,
464 .app_2_per_addr = 1189,
465 .app_2_mcu_addr = 770,
466 .shp_2_per_addr = 1407,
467 .shp_2_mcu_addr = 979,
470 static struct sdma_driver_data sdma_imx25 = {
471 .chnenbl0 = SDMA_CHNENBL0_IMX35,
473 .script_addrs = &sdma_script_imx25,
476 static struct sdma_driver_data sdma_imx35 = {
477 .chnenbl0 = SDMA_CHNENBL0_IMX35,
481 static struct sdma_script_start_addrs sdma_script_imx51 = {
483 .uart_2_mcu_addr = 817,
484 .mcu_2_app_addr = 747,
485 .mcu_2_shp_addr = 961,
486 .ata_2_mcu_addr = 1473,
487 .mcu_2_ata_addr = 1392,
488 .app_2_per_addr = 1033,
489 .app_2_mcu_addr = 683,
490 .shp_2_per_addr = 1251,
491 .shp_2_mcu_addr = 892,
494 static struct sdma_driver_data sdma_imx51 = {
495 .chnenbl0 = SDMA_CHNENBL0_IMX35,
497 .script_addrs = &sdma_script_imx51,
500 static struct sdma_script_start_addrs sdma_script_imx53 = {
502 .app_2_mcu_addr = 683,
503 .mcu_2_app_addr = 747,
504 .uart_2_mcu_addr = 817,
505 .shp_2_mcu_addr = 891,
506 .mcu_2_shp_addr = 960,
507 .uartsh_2_mcu_addr = 1032,
508 .spdif_2_mcu_addr = 1100,
509 .mcu_2_spdif_addr = 1134,
510 .firi_2_mcu_addr = 1193,
511 .mcu_2_firi_addr = 1290,
514 static struct sdma_driver_data sdma_imx53 = {
515 .chnenbl0 = SDMA_CHNENBL0_IMX35,
517 .script_addrs = &sdma_script_imx53,
520 static struct sdma_script_start_addrs sdma_script_imx6q = {
522 .uart_2_mcu_addr = 817,
523 .mcu_2_app_addr = 747,
524 .per_2_per_addr = 6331,
525 .uartsh_2_mcu_addr = 1032,
526 .mcu_2_shp_addr = 960,
527 .app_2_mcu_addr = 683,
528 .shp_2_mcu_addr = 891,
529 .spdif_2_mcu_addr = 1100,
530 .mcu_2_spdif_addr = 1134,
533 static struct sdma_driver_data sdma_imx6q = {
534 .chnenbl0 = SDMA_CHNENBL0_IMX35,
536 .script_addrs = &sdma_script_imx6q,
539 static struct sdma_script_start_addrs sdma_script_imx7d = {
541 .uart_2_mcu_addr = 819,
542 .mcu_2_app_addr = 749,
543 .uartsh_2_mcu_addr = 1034,
544 .mcu_2_shp_addr = 962,
545 .app_2_mcu_addr = 685,
546 .shp_2_mcu_addr = 893,
547 .spdif_2_mcu_addr = 1102,
548 .mcu_2_spdif_addr = 1136,
551 static struct sdma_driver_data sdma_imx7d = {
552 .chnenbl0 = SDMA_CHNENBL0_IMX35,
554 .script_addrs = &sdma_script_imx7d,
557 static const struct platform_device_id sdma_devtypes[] = {
559 .name = "imx25-sdma",
560 .driver_data = (unsigned long)&sdma_imx25,
562 .name = "imx31-sdma",
563 .driver_data = (unsigned long)&sdma_imx31,
565 .name = "imx35-sdma",
566 .driver_data = (unsigned long)&sdma_imx35,
568 .name = "imx51-sdma",
569 .driver_data = (unsigned long)&sdma_imx51,
571 .name = "imx53-sdma",
572 .driver_data = (unsigned long)&sdma_imx53,
574 .name = "imx6q-sdma",
575 .driver_data = (unsigned long)&sdma_imx6q,
577 .name = "imx7d-sdma",
578 .driver_data = (unsigned long)&sdma_imx7d,
583 MODULE_DEVICE_TABLE(platform, sdma_devtypes);
585 static const struct of_device_id sdma_dt_ids[] = {
586 { .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
587 { .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
588 { .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
589 { .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
590 { .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
591 { .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
592 { .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
595 MODULE_DEVICE_TABLE(of, sdma_dt_ids);
597 #define SDMA_H_CONFIG_DSPDMA BIT(12) /* indicates if the DSPDMA is used */
598 #define SDMA_H_CONFIG_RTD_PINS BIT(11) /* indicates if Real-Time Debug pins are enabled */
599 #define SDMA_H_CONFIG_ACR BIT(4) /* indicates if AHB freq /core freq = 2 or 1 */
600 #define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/
602 static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
604 u32 chnenbl0 = sdma->drvdata->chnenbl0;
605 return chnenbl0 + event * 4;
608 static int sdma_config_ownership(struct sdma_channel *sdmac,
609 bool event_override, bool mcu_override, bool dsp_override)
611 struct sdma_engine *sdma = sdmac->sdma;
612 int channel = sdmac->channel;
613 unsigned long evt, mcu, dsp;
615 if (event_override && mcu_override && dsp_override)
618 evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
619 mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
620 dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
623 __clear_bit(channel, &dsp);
625 __set_bit(channel, &dsp);
628 __clear_bit(channel, &evt);
630 __set_bit(channel, &evt);
633 __clear_bit(channel, &mcu);
635 __set_bit(channel, &mcu);
637 writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
638 writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
639 writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
644 static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
646 writel(BIT(channel), sdma->regs + SDMA_H_START);
650 * sdma_run_channel0 - run a channel and wait till it's done
652 static int sdma_run_channel0(struct sdma_engine *sdma)
657 sdma_enable_channel(sdma, 0);
659 ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
660 reg, !(reg & 1), 1, 500);
662 dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
664 /* Set bits of CONFIG register with dynamic context switching */
665 if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
666 writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
671 static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
674 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
680 buf_virt = dma_alloc_coherent(NULL,
682 &buf_phys, GFP_KERNEL);
687 spin_lock_irqsave(&sdma->channel_0_lock, flags);
689 bd0->mode.command = C0_SETPM;
690 bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
691 bd0->mode.count = size / 2;
692 bd0->buffer_addr = buf_phys;
693 bd0->ext_buffer_addr = address;
695 memcpy(buf_virt, buf, size);
697 ret = sdma_run_channel0(sdma);
699 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
701 dma_free_coherent(NULL, size, buf_virt, buf_phys);
706 static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
708 struct sdma_engine *sdma = sdmac->sdma;
709 int channel = sdmac->channel;
711 u32 chnenbl = chnenbl_ofs(sdma, event);
713 val = readl_relaxed(sdma->regs + chnenbl);
714 __set_bit(channel, &val);
715 writel_relaxed(val, sdma->regs + chnenbl);
718 static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
720 struct sdma_engine *sdma = sdmac->sdma;
721 int channel = sdmac->channel;
722 u32 chnenbl = chnenbl_ofs(sdma, event);
725 val = readl_relaxed(sdma->regs + chnenbl);
726 __clear_bit(channel, &val);
727 writel_relaxed(val, sdma->regs + chnenbl);
730 static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
732 return container_of(t, struct sdma_desc, vd.tx);
735 static void sdma_start_desc(struct sdma_channel *sdmac)
737 struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
738 struct sdma_desc *desc;
739 struct sdma_engine *sdma = sdmac->sdma;
740 int channel = sdmac->channel;
746 sdmac->desc = desc = to_sdma_desc(&vd->tx);
748 * Do not delete the node in desc_issued list in cyclic mode, otherwise
749 * the desc allocated will never be freed in vchan_dma_desc_free_list
751 if (!(sdmac->flags & IMX_DMA_SG_LOOP))
754 sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
755 sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
756 sdma_enable_channel(sdma, sdmac->channel);
759 static void sdma_update_channel_loop(struct sdma_channel *sdmac)
761 struct sdma_buffer_descriptor *bd;
763 enum dma_status old_status = sdmac->status;
766 * loop mode. Iterate over descriptors, re-setup them and
767 * call callback function.
769 while (sdmac->desc) {
770 struct sdma_desc *desc = sdmac->desc;
772 bd = &desc->bd[desc->buf_tail];
774 if (bd->mode.status & BD_DONE)
777 if (bd->mode.status & BD_RROR) {
778 bd->mode.status &= ~BD_RROR;
779 sdmac->status = DMA_ERROR;
784 * We use bd->mode.count to calculate the residue, since contains
785 * the number of bytes present in the current buffer descriptor.
788 desc->chn_real_count = bd->mode.count;
789 bd->mode.status |= BD_DONE;
790 bd->mode.count = desc->period_len;
791 desc->buf_ptail = desc->buf_tail;
792 desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
795 * The callback is called from the interrupt context in order
796 * to reduce latency and to avoid the risk of altering the
797 * SDMA transaction status by the time the client tasklet is
800 spin_unlock(&sdmac->vc.lock);
801 dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
802 spin_lock(&sdmac->vc.lock);
805 sdmac->status = old_status;
809 static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
811 struct sdma_channel *sdmac = (struct sdma_channel *) data;
812 struct sdma_buffer_descriptor *bd;
815 sdmac->desc->chn_real_count = 0;
817 * non loop mode. Iterate over all descriptors, collect
818 * errors and call callback function
820 for (i = 0; i < sdmac->desc->num_bd; i++) {
821 bd = &sdmac->desc->bd[i];
823 if (bd->mode.status & (BD_DONE | BD_RROR))
825 sdmac->desc->chn_real_count += bd->mode.count;
829 sdmac->status = DMA_ERROR;
831 sdmac->status = DMA_COMPLETE;
834 static irqreturn_t sdma_int_handler(int irq, void *dev_id)
836 struct sdma_engine *sdma = dev_id;
839 stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
840 writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
841 /* channel 0 is special and not handled here, see run_channel0() */
845 int channel = fls(stat) - 1;
846 struct sdma_channel *sdmac = &sdma->channel[channel];
847 struct sdma_desc *desc;
849 spin_lock(&sdmac->vc.lock);
852 if (sdmac->flags & IMX_DMA_SG_LOOP) {
853 sdma_update_channel_loop(sdmac);
855 mxc_sdma_handle_channel_normal(sdmac);
856 vchan_cookie_complete(&desc->vd);
857 sdma_start_desc(sdmac);
861 spin_unlock(&sdmac->vc.lock);
862 __clear_bit(channel, &stat);
869 * sets the pc of SDMA script according to the peripheral type
871 static void sdma_get_pc(struct sdma_channel *sdmac,
872 enum sdma_peripheral_type peripheral_type)
874 struct sdma_engine *sdma = sdmac->sdma;
875 int per_2_emi = 0, emi_2_per = 0;
877 * These are needed once we start to support transfers between
878 * two peripherals or memory-to-memory transfers
880 int per_2_per = 0, emi_2_emi = 0;
882 sdmac->pc_from_device = 0;
883 sdmac->pc_to_device = 0;
884 sdmac->device_to_device = 0;
887 switch (peripheral_type) {
888 case IMX_DMATYPE_MEMORY:
889 emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
891 case IMX_DMATYPE_DSP:
892 emi_2_per = sdma->script_addrs->bp_2_ap_addr;
893 per_2_emi = sdma->script_addrs->ap_2_bp_addr;
895 case IMX_DMATYPE_FIRI:
896 per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
897 emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
899 case IMX_DMATYPE_UART:
900 per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
901 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
903 case IMX_DMATYPE_UART_SP:
904 per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
905 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
907 case IMX_DMATYPE_ATA:
908 per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
909 emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
911 case IMX_DMATYPE_CSPI:
912 case IMX_DMATYPE_EXT:
913 case IMX_DMATYPE_SSI:
914 case IMX_DMATYPE_SAI:
915 per_2_emi = sdma->script_addrs->app_2_mcu_addr;
916 emi_2_per = sdma->script_addrs->mcu_2_app_addr;
918 case IMX_DMATYPE_SSI_DUAL:
919 per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
920 emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
922 case IMX_DMATYPE_SSI_SP:
923 case IMX_DMATYPE_MMC:
924 case IMX_DMATYPE_SDHC:
925 case IMX_DMATYPE_CSPI_SP:
926 case IMX_DMATYPE_ESAI:
927 case IMX_DMATYPE_MSHC_SP:
928 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
929 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
931 case IMX_DMATYPE_ASRC:
932 per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
933 emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
934 per_2_per = sdma->script_addrs->per_2_per_addr;
936 case IMX_DMATYPE_ASRC_SP:
937 per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
938 emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
939 per_2_per = sdma->script_addrs->per_2_per_addr;
941 case IMX_DMATYPE_MSHC:
942 per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
943 emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
945 case IMX_DMATYPE_CCM:
946 per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
948 case IMX_DMATYPE_SPDIF:
949 per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
950 emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
952 case IMX_DMATYPE_IPU_MEMORY:
953 emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
959 sdmac->pc_from_device = per_2_emi;
960 sdmac->pc_to_device = emi_2_per;
961 sdmac->device_to_device = per_2_per;
962 sdmac->pc_to_pc = emi_2_emi;
965 static int sdma_load_context(struct sdma_channel *sdmac)
967 struct sdma_engine *sdma = sdmac->sdma;
968 int channel = sdmac->channel;
970 struct sdma_context_data *context = sdma->context;
971 struct sdma_buffer_descriptor *bd0 = sdma->bd0;
975 if (sdmac->direction == DMA_DEV_TO_MEM)
976 load_address = sdmac->pc_from_device;
977 else if (sdmac->direction == DMA_DEV_TO_DEV)
978 load_address = sdmac->device_to_device;
979 else if (sdmac->direction == DMA_MEM_TO_MEM)
980 load_address = sdmac->pc_to_pc;
982 load_address = sdmac->pc_to_device;
984 if (load_address < 0)
987 dev_dbg(sdma->dev, "load_address = %d\n", load_address);
988 dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
989 dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
990 dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
991 dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
992 dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
994 spin_lock_irqsave(&sdma->channel_0_lock, flags);
996 memset(context, 0, sizeof(*context));
997 context->channel_state.pc = load_address;
999 /* Send by context the event mask,base address for peripheral
1000 * and watermark level
1002 context->gReg[0] = sdmac->event_mask[1];
1003 context->gReg[1] = sdmac->event_mask[0];
1004 context->gReg[2] = sdmac->per_addr;
1005 context->gReg[6] = sdmac->shp_addr;
1006 context->gReg[7] = sdmac->watermark_level;
1008 bd0->mode.command = C0_SETDM;
1009 bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
1010 bd0->mode.count = sizeof(*context) / 4;
1011 bd0->buffer_addr = sdma->context_phys;
1012 bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
1013 ret = sdma_run_channel0(sdma);
1015 spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
1020 static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
1022 return container_of(chan, struct sdma_channel, vc.chan);
1025 static int sdma_disable_channel(struct dma_chan *chan)
1027 struct sdma_channel *sdmac = to_sdma_chan(chan);
1028 struct sdma_engine *sdma = sdmac->sdma;
1029 int channel = sdmac->channel;
1031 writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
1032 sdmac->status = DMA_ERROR;
1036 static void sdma_channel_terminate_work(struct work_struct *work)
1038 struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1040 unsigned long flags;
1044 * According to NXP R&D team a delay of one BD SDMA cost time
1045 * (maximum is 1ms) should be added after disable of the channel
1046 * bit, to ensure SDMA core has really been stopped after SDMA
1047 * clients call .device_terminate_all.
1049 usleep_range(1000, 2000);
1051 spin_lock_irqsave(&sdmac->vc.lock, flags);
1052 vchan_get_all_descriptors(&sdmac->vc, &head);
1054 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1055 vchan_dma_desc_free_list(&sdmac->vc, &head);
1058 static int sdma_disable_channel_async(struct dma_chan *chan)
1060 struct sdma_channel *sdmac = to_sdma_chan(chan);
1062 sdma_disable_channel(chan);
1065 schedule_work(&sdmac->terminate_worker);
1070 static void sdma_channel_synchronize(struct dma_chan *chan)
1072 struct sdma_channel *sdmac = to_sdma_chan(chan);
1074 vchan_synchronize(&sdmac->vc);
1076 flush_work(&sdmac->terminate_worker);
1079 static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
1081 struct sdma_engine *sdma = sdmac->sdma;
1083 int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
1084 int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
1086 set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
1087 set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
1089 if (sdmac->event_id0 > 31)
1090 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
1092 if (sdmac->event_id1 > 31)
1093 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
1096 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
1097 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
1098 * r0(event_mask[1]) and r1(event_mask[0]).
1101 sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
1102 SDMA_WATERMARK_LEVEL_HWML);
1103 sdmac->watermark_level |= hwml;
1104 sdmac->watermark_level |= lwml << 16;
1105 swap(sdmac->event_mask[0], sdmac->event_mask[1]);
1108 if (sdmac->per_address2 >= sdma->spba_start_addr &&
1109 sdmac->per_address2 <= sdma->spba_end_addr)
1110 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
1112 if (sdmac->per_address >= sdma->spba_start_addr &&
1113 sdmac->per_address <= sdma->spba_end_addr)
1114 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
1116 sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
1119 static int sdma_config_channel(struct dma_chan *chan)
1121 struct sdma_channel *sdmac = to_sdma_chan(chan);
1124 sdma_disable_channel(chan);
1126 sdmac->event_mask[0] = 0;
1127 sdmac->event_mask[1] = 0;
1128 sdmac->shp_addr = 0;
1129 sdmac->per_addr = 0;
1131 switch (sdmac->peripheral_type) {
1132 case IMX_DMATYPE_DSP:
1133 sdma_config_ownership(sdmac, false, true, true);
1135 case IMX_DMATYPE_MEMORY:
1136 sdma_config_ownership(sdmac, false, true, false);
1139 sdma_config_ownership(sdmac, true, true, false);
1143 sdma_get_pc(sdmac, sdmac->peripheral_type);
1145 if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
1146 (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
1147 /* Handle multiple event channels differently */
1148 if (sdmac->event_id1) {
1149 if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
1150 sdmac->peripheral_type == IMX_DMATYPE_ASRC)
1151 sdma_set_watermarklevel_for_p2p(sdmac);
1153 __set_bit(sdmac->event_id0, sdmac->event_mask);
1156 sdmac->shp_addr = sdmac->per_address;
1157 sdmac->per_addr = sdmac->per_address2;
1159 sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
1162 ret = sdma_load_context(sdmac);
1167 static int sdma_set_channel_priority(struct sdma_channel *sdmac,
1168 unsigned int priority)
1170 struct sdma_engine *sdma = sdmac->sdma;
1171 int channel = sdmac->channel;
1173 if (priority < MXC_SDMA_MIN_PRIORITY
1174 || priority > MXC_SDMA_MAX_PRIORITY) {
1178 writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
1183 static int sdma_request_channel0(struct sdma_engine *sdma)
1187 sdma->bd0 = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdma->bd0_phys,
1194 sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
1195 sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
1197 sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
1205 static int sdma_alloc_bd(struct sdma_desc *desc)
1207 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1210 desc->bd = dma_zalloc_coherent(NULL, bd_size, &desc->bd_phys,
1220 static void sdma_free_bd(struct sdma_desc *desc)
1222 u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1224 dma_free_coherent(NULL, bd_size, desc->bd, desc->bd_phys);
1227 static void sdma_desc_free(struct virt_dma_desc *vd)
1229 struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
1235 static int sdma_alloc_chan_resources(struct dma_chan *chan)
1237 struct sdma_channel *sdmac = to_sdma_chan(chan);
1238 struct imx_dma_data *data = chan->private;
1239 struct imx_dma_data mem_data;
1243 * MEMCPY may never setup chan->private by filter function such as
1244 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
1245 * Please note in any other slave case, you have to setup chan->private
1246 * with 'struct imx_dma_data' in your own filter function if you want to
1247 * request dma channel by dma_request_channel() rather than
1248 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
1249 * to warn you to correct your filter function.
1252 dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
1253 mem_data.priority = 2;
1254 mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
1255 mem_data.dma_request = 0;
1256 mem_data.dma_request2 = 0;
1259 sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
1262 switch (data->priority) {
1266 case DMA_PRIO_MEDIUM:
1275 sdmac->peripheral_type = data->peripheral_type;
1276 sdmac->event_id0 = data->dma_request;
1277 sdmac->event_id1 = data->dma_request2;
1279 ret = clk_enable(sdmac->sdma->clk_ipg);
1282 ret = clk_enable(sdmac->sdma->clk_ahb);
1284 goto disable_clk_ipg;
1286 ret = sdma_set_channel_priority(sdmac, prio);
1288 goto disable_clk_ahb;
1293 clk_disable(sdmac->sdma->clk_ahb);
1295 clk_disable(sdmac->sdma->clk_ipg);
1299 static void sdma_free_chan_resources(struct dma_chan *chan)
1301 struct sdma_channel *sdmac = to_sdma_chan(chan);
1302 struct sdma_engine *sdma = sdmac->sdma;
1304 sdma_disable_channel_async(chan);
1306 sdma_channel_synchronize(chan);
1308 if (sdmac->event_id0)
1309 sdma_event_disable(sdmac, sdmac->event_id0);
1310 if (sdmac->event_id1)
1311 sdma_event_disable(sdmac, sdmac->event_id1);
1313 sdmac->event_id0 = 0;
1314 sdmac->event_id1 = 0;
1316 sdma_set_channel_priority(sdmac, 0);
1318 clk_disable(sdma->clk_ipg);
1319 clk_disable(sdma->clk_ahb);
1322 static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
1323 enum dma_transfer_direction direction, u32 bds)
1325 struct sdma_desc *desc;
1327 desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
1331 sdmac->status = DMA_IN_PROGRESS;
1332 sdmac->direction = direction;
1335 desc->chn_count = 0;
1336 desc->chn_real_count = 0;
1338 desc->buf_ptail = 0;
1339 desc->sdmac = sdmac;
1342 if (sdma_alloc_bd(desc))
1345 /* No slave_config called in MEMCPY case, so do here */
1346 if (direction == DMA_MEM_TO_MEM)
1347 sdma_config_ownership(sdmac, false, true, false);
1349 if (sdma_load_context(sdmac))
1360 static struct dma_async_tx_descriptor *sdma_prep_memcpy(
1361 struct dma_chan *chan, dma_addr_t dma_dst,
1362 dma_addr_t dma_src, size_t len, unsigned long flags)
1364 struct sdma_channel *sdmac = to_sdma_chan(chan);
1365 struct sdma_engine *sdma = sdmac->sdma;
1366 int channel = sdmac->channel;
1369 struct sdma_buffer_descriptor *bd;
1370 struct sdma_desc *desc;
1375 dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
1376 &dma_src, &dma_dst, len, channel);
1378 desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
1379 len / SDMA_BD_MAX_CNT + 1);
1384 count = min_t(size_t, len, SDMA_BD_MAX_CNT);
1386 bd->buffer_addr = dma_src;
1387 bd->ext_buffer_addr = dma_dst;
1388 bd->mode.count = count;
1389 desc->chn_count += count;
1390 bd->mode.command = 0;
1397 param = BD_DONE | BD_EXTD | BD_CONT;
1405 dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
1406 i, count, bd->buffer_addr,
1407 param & BD_WRAP ? "wrap" : "",
1408 param & BD_INTR ? " intr" : "");
1410 bd->mode.status = param;
1413 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1416 static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
1417 struct dma_chan *chan, struct scatterlist *sgl,
1418 unsigned int sg_len, enum dma_transfer_direction direction,
1419 unsigned long flags, void *context)
1421 struct sdma_channel *sdmac = to_sdma_chan(chan);
1422 struct sdma_engine *sdma = sdmac->sdma;
1424 int channel = sdmac->channel;
1425 struct scatterlist *sg;
1426 struct sdma_desc *desc;
1428 sdma_config_write(chan, &sdmac->slave_config, direction);
1430 desc = sdma_transfer_init(sdmac, direction, sg_len);
1434 dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
1437 for_each_sg(sgl, sg, sg_len, i) {
1438 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1441 bd->buffer_addr = sg->dma_address;
1443 count = sg_dma_len(sg);
1445 if (count > SDMA_BD_MAX_CNT) {
1446 dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
1447 channel, count, SDMA_BD_MAX_CNT);
1451 bd->mode.count = count;
1452 desc->chn_count += count;
1454 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1457 switch (sdmac->word_size) {
1458 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1459 bd->mode.command = 0;
1460 if (count & 3 || sg->dma_address & 3)
1463 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1464 bd->mode.command = 2;
1465 if (count & 1 || sg->dma_address & 1)
1468 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1469 bd->mode.command = 1;
1475 param = BD_DONE | BD_EXTD | BD_CONT;
1477 if (i + 1 == sg_len) {
1483 dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1484 i, count, (u64)sg->dma_address,
1485 param & BD_WRAP ? "wrap" : "",
1486 param & BD_INTR ? " intr" : "");
1488 bd->mode.status = param;
1491 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1496 sdmac->status = DMA_ERROR;
1500 static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
1501 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1502 size_t period_len, enum dma_transfer_direction direction,
1503 unsigned long flags)
1505 struct sdma_channel *sdmac = to_sdma_chan(chan);
1506 struct sdma_engine *sdma = sdmac->sdma;
1507 int num_periods = buf_len / period_len;
1508 int channel = sdmac->channel;
1510 struct sdma_desc *desc;
1512 dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
1514 sdma_config_write(chan, &sdmac->slave_config, direction);
1516 desc = sdma_transfer_init(sdmac, direction, num_periods);
1520 desc->period_len = period_len;
1522 sdmac->flags |= IMX_DMA_SG_LOOP;
1524 if (period_len > SDMA_BD_MAX_CNT) {
1525 dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
1526 channel, period_len, SDMA_BD_MAX_CNT);
1530 while (buf < buf_len) {
1531 struct sdma_buffer_descriptor *bd = &desc->bd[i];
1534 bd->buffer_addr = dma_addr;
1536 bd->mode.count = period_len;
1538 if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
1540 if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
1541 bd->mode.command = 0;
1543 bd->mode.command = sdmac->word_size;
1545 param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
1546 if (i + 1 == num_periods)
1549 dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1550 i, period_len, (u64)dma_addr,
1551 param & BD_WRAP ? "wrap" : "",
1552 param & BD_INTR ? " intr" : "");
1554 bd->mode.status = param;
1556 dma_addr += period_len;
1562 return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1567 sdmac->status = DMA_ERROR;
1571 static int sdma_config_write(struct dma_chan *chan,
1572 struct dma_slave_config *dmaengine_cfg,
1573 enum dma_transfer_direction direction)
1575 struct sdma_channel *sdmac = to_sdma_chan(chan);
1577 if (direction == DMA_DEV_TO_MEM) {
1578 sdmac->per_address = dmaengine_cfg->src_addr;
1579 sdmac->watermark_level = dmaengine_cfg->src_maxburst *
1580 dmaengine_cfg->src_addr_width;
1581 sdmac->word_size = dmaengine_cfg->src_addr_width;
1582 } else if (direction == DMA_DEV_TO_DEV) {
1583 sdmac->per_address2 = dmaengine_cfg->src_addr;
1584 sdmac->per_address = dmaengine_cfg->dst_addr;
1585 sdmac->watermark_level = dmaengine_cfg->src_maxburst &
1586 SDMA_WATERMARK_LEVEL_LWML;
1587 sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
1588 SDMA_WATERMARK_LEVEL_HWML;
1589 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1591 sdmac->per_address = dmaengine_cfg->dst_addr;
1592 sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
1593 dmaengine_cfg->dst_addr_width;
1594 sdmac->word_size = dmaengine_cfg->dst_addr_width;
1596 sdmac->direction = direction;
1597 return sdma_config_channel(chan);
1600 static int sdma_config(struct dma_chan *chan,
1601 struct dma_slave_config *dmaengine_cfg)
1603 struct sdma_channel *sdmac = to_sdma_chan(chan);
1605 memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1607 /* Set ENBLn earlier to make sure dma request triggered after that */
1608 if (sdmac->event_id0) {
1609 if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1611 sdma_event_enable(sdmac, sdmac->event_id0);
1614 if (sdmac->event_id1) {
1615 if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1617 sdma_event_enable(sdmac, sdmac->event_id1);
1623 static enum dma_status sdma_tx_status(struct dma_chan *chan,
1624 dma_cookie_t cookie,
1625 struct dma_tx_state *txstate)
1627 struct sdma_channel *sdmac = to_sdma_chan(chan);
1628 struct sdma_desc *desc;
1630 struct virt_dma_desc *vd;
1631 enum dma_status ret;
1632 unsigned long flags;
1634 ret = dma_cookie_status(chan, cookie, txstate);
1635 if (ret == DMA_COMPLETE || !txstate)
1638 spin_lock_irqsave(&sdmac->vc.lock, flags);
1639 vd = vchan_find_desc(&sdmac->vc, cookie);
1641 desc = to_sdma_desc(&vd->tx);
1642 if (sdmac->flags & IMX_DMA_SG_LOOP)
1643 residue = (desc->num_bd - desc->buf_ptail) *
1644 desc->period_len - desc->chn_real_count;
1646 residue = desc->chn_count - desc->chn_real_count;
1647 } else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) {
1648 residue = sdmac->desc->chn_count - sdmac->desc->chn_real_count;
1652 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1654 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1657 return sdmac->status;
1660 static void sdma_issue_pending(struct dma_chan *chan)
1662 struct sdma_channel *sdmac = to_sdma_chan(chan);
1663 unsigned long flags;
1665 spin_lock_irqsave(&sdmac->vc.lock, flags);
1666 if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
1667 sdma_start_desc(sdmac);
1668 spin_unlock_irqrestore(&sdmac->vc.lock, flags);
1671 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1672 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2 38
1673 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3 41
1674 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4 42
1676 static void sdma_add_scripts(struct sdma_engine *sdma,
1677 const struct sdma_script_start_addrs *addr)
1679 s32 *addr_arr = (u32 *)addr;
1680 s32 *saddr_arr = (u32 *)sdma->script_addrs;
1683 /* use the default firmware in ROM if missing external firmware */
1684 if (!sdma->script_number)
1685 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1687 if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
1690 "SDMA script number %d not match with firmware.\n",
1691 sdma->script_number);
1695 for (i = 0; i < sdma->script_number; i++)
1696 if (addr_arr[i] > 0)
1697 saddr_arr[i] = addr_arr[i];
1700 static void sdma_load_firmware(const struct firmware *fw, void *context)
1702 struct sdma_engine *sdma = context;
1703 const struct sdma_firmware_header *header;
1704 const struct sdma_script_start_addrs *addr;
1705 unsigned short *ram_code;
1708 dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
1709 /* In this case we just use the ROM firmware. */
1713 if (fw->size < sizeof(*header))
1716 header = (struct sdma_firmware_header *)fw->data;
1718 if (header->magic != SDMA_FIRMWARE_MAGIC)
1720 if (header->ram_code_start + header->ram_code_size > fw->size)
1722 switch (header->version_major) {
1724 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1727 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1730 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1733 sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1736 dev_err(sdma->dev, "unknown firmware version\n");
1740 addr = (void *)header + header->script_addrs_start;
1741 ram_code = (void *)header + header->ram_code_start;
1743 clk_enable(sdma->clk_ipg);
1744 clk_enable(sdma->clk_ahb);
1745 /* download the RAM image for SDMA */
1746 sdma_load_script(sdma, ram_code,
1747 header->ram_code_size,
1748 addr->ram_code_start_addr);
1749 clk_disable(sdma->clk_ipg);
1750 clk_disable(sdma->clk_ahb);
1752 sdma_add_scripts(sdma, addr);
1754 dev_info(sdma->dev, "loaded firmware %d.%d\n",
1755 header->version_major,
1756 header->version_minor);
1759 release_firmware(fw);
1762 #define EVENT_REMAP_CELLS 3
1764 static int sdma_event_remap(struct sdma_engine *sdma)
1766 struct device_node *np = sdma->dev->of_node;
1767 struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1768 struct property *event_remap;
1770 char propname[] = "fsl,sdma-event-remap";
1771 u32 reg, val, shift, num_map, i;
1774 if (IS_ERR(np) || !gpr_np)
1777 event_remap = of_find_property(np, propname, NULL);
1778 num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1780 dev_dbg(sdma->dev, "no event needs to be remapped\n");
1782 } else if (num_map % EVENT_REMAP_CELLS) {
1783 dev_err(sdma->dev, "the property %s must modulo %d\n",
1784 propname, EVENT_REMAP_CELLS);
1789 gpr = syscon_node_to_regmap(gpr_np);
1791 dev_err(sdma->dev, "failed to get gpr regmap\n");
1796 for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1797 ret = of_property_read_u32_index(np, propname, i, ®);
1799 dev_err(sdma->dev, "failed to read property %s index %d\n",
1804 ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1806 dev_err(sdma->dev, "failed to read property %s index %d\n",
1811 ret = of_property_read_u32_index(np, propname, i + 2, &val);
1813 dev_err(sdma->dev, "failed to read property %s index %d\n",
1818 regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1823 of_node_put(gpr_np);
1828 static int sdma_get_firmware(struct sdma_engine *sdma,
1829 const char *fw_name)
1833 ret = reject_firmware_nowait(THIS_MODULE,
1834 FW_ACTION_HOTPLUG, fw_name, sdma->dev,
1835 GFP_KERNEL, sdma, sdma_load_firmware);
1840 static int sdma_init(struct sdma_engine *sdma)
1843 dma_addr_t ccb_phys;
1845 ret = clk_enable(sdma->clk_ipg);
1848 ret = clk_enable(sdma->clk_ahb);
1850 goto disable_clk_ipg;
1852 /* Be sure SDMA has not started yet */
1853 writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
1855 sdma->channel_control = dma_alloc_coherent(NULL,
1856 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
1857 sizeof(struct sdma_context_data),
1858 &ccb_phys, GFP_KERNEL);
1860 if (!sdma->channel_control) {
1865 sdma->context = (void *)sdma->channel_control +
1866 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1867 sdma->context_phys = ccb_phys +
1868 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
1870 /* Zero-out the CCB structures array just allocated */
1871 memset(sdma->channel_control, 0,
1872 MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
1874 /* disable all channels */
1875 for (i = 0; i < sdma->drvdata->num_events; i++)
1876 writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
1878 /* All channels have priority 0 */
1879 for (i = 0; i < MAX_DMA_CHANNELS; i++)
1880 writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
1882 ret = sdma_request_channel0(sdma);
1886 sdma_config_ownership(&sdma->channel[0], false, true, false);
1888 /* Set Command Channel (Channel Zero) */
1889 writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
1891 /* Set bits of CONFIG register but with static context switching */
1892 /* FIXME: Check whether to set ACR bit depending on clock ratios */
1893 writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
1895 writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
1897 /* Initializes channel's priorities */
1898 sdma_set_channel_priority(&sdma->channel[0], 7);
1900 clk_disable(sdma->clk_ipg);
1901 clk_disable(sdma->clk_ahb);
1906 clk_disable(sdma->clk_ahb);
1908 clk_disable(sdma->clk_ipg);
1909 dev_err(sdma->dev, "initialisation failed with %d\n", ret);
1913 static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
1915 struct sdma_channel *sdmac = to_sdma_chan(chan);
1916 struct imx_dma_data *data = fn_param;
1918 if (!imx_dma_is_general_purpose(chan))
1921 sdmac->data = *data;
1922 chan->private = &sdmac->data;
1927 static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
1928 struct of_dma *ofdma)
1930 struct sdma_engine *sdma = ofdma->of_dma_data;
1931 dma_cap_mask_t mask = sdma->dma_device.cap_mask;
1932 struct imx_dma_data data;
1934 if (dma_spec->args_count != 3)
1937 data.dma_request = dma_spec->args[0];
1938 data.peripheral_type = dma_spec->args[1];
1939 data.priority = dma_spec->args[2];
1941 * init dma_request2 to zero, which is not used by the dts.
1942 * For P2P, dma_request2 is init from dma_request_channel(),
1943 * chan->private will point to the imx_dma_data, and in
1944 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
1945 * be set to sdmac->event_id1.
1947 data.dma_request2 = 0;
1949 return dma_request_channel(mask, sdma_filter_fn, &data);
1952 static int sdma_probe(struct platform_device *pdev)
1954 const struct of_device_id *of_id =
1955 of_match_device(sdma_dt_ids, &pdev->dev);
1956 struct device_node *np = pdev->dev.of_node;
1957 struct device_node *spba_bus;
1958 const char *fw_name;
1961 struct resource *iores;
1962 struct resource spba_res;
1963 struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
1965 struct sdma_engine *sdma;
1967 const struct sdma_driver_data *drvdata = NULL;
1970 drvdata = of_id->data;
1971 else if (pdev->id_entry)
1972 drvdata = (void *)pdev->id_entry->driver_data;
1975 dev_err(&pdev->dev, "unable to find driver data\n");
1979 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1983 sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
1987 spin_lock_init(&sdma->channel_0_lock);
1989 sdma->dev = &pdev->dev;
1990 sdma->drvdata = drvdata;
1992 irq = platform_get_irq(pdev, 0);
1996 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1997 sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
1998 if (IS_ERR(sdma->regs))
1999 return PTR_ERR(sdma->regs);
2001 sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2002 if (IS_ERR(sdma->clk_ipg))
2003 return PTR_ERR(sdma->clk_ipg);
2005 sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2006 if (IS_ERR(sdma->clk_ahb))
2007 return PTR_ERR(sdma->clk_ahb);
2009 ret = clk_prepare(sdma->clk_ipg);
2013 ret = clk_prepare(sdma->clk_ahb);
2017 ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
2024 sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2025 if (!sdma->script_addrs) {
2030 /* initially no scripts available */
2031 saddr_arr = (s32 *)sdma->script_addrs;
2032 for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
2033 saddr_arr[i] = -EINVAL;
2035 dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
2036 dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
2037 dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
2039 INIT_LIST_HEAD(&sdma->dma_device.channels);
2040 /* Initialize channel parameters */
2041 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2042 struct sdma_channel *sdmac = &sdma->channel[i];
2047 sdmac->vc.desc_free = sdma_desc_free;
2048 INIT_WORK(&sdmac->terminate_worker,
2049 sdma_channel_terminate_work);
2051 * Add the channel to the DMAC list. Do not add channel 0 though
2052 * because we need it internally in the SDMA driver. This also means
2053 * that channel 0 in dmaengine counting matches sdma channel 1.
2056 vchan_init(&sdmac->vc, &sdma->dma_device);
2059 ret = sdma_init(sdma);
2063 ret = sdma_event_remap(sdma);
2067 if (sdma->drvdata->script_addrs)
2068 sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
2069 if (pdata && pdata->script_addrs)
2070 sdma_add_scripts(sdma, pdata->script_addrs);
2072 sdma->dma_device.dev = &pdev->dev;
2074 sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
2075 sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
2076 sdma->dma_device.device_tx_status = sdma_tx_status;
2077 sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
2078 sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
2079 sdma->dma_device.device_config = sdma_config;
2080 sdma->dma_device.device_terminate_all = sdma_disable_channel_async;
2081 sdma->dma_device.device_synchronize = sdma_channel_synchronize;
2082 sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2083 sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2084 sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
2085 sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
2086 sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
2087 sdma->dma_device.device_issue_pending = sdma_issue_pending;
2088 sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
2089 dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
2091 platform_set_drvdata(pdev, sdma);
2093 ret = dma_async_device_register(&sdma->dma_device);
2095 dev_err(&pdev->dev, "unable to register\n");
2100 ret = of_dma_controller_register(np, sdma_xlate, sdma);
2102 dev_err(&pdev->dev, "failed to register controller\n");
2106 spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
2107 ret = of_address_to_resource(spba_bus, 0, &spba_res);
2109 sdma->spba_start_addr = spba_res.start;
2110 sdma->spba_end_addr = spba_res.end;
2112 of_node_put(spba_bus);
2116 * Kick off firmware loading as the very last step:
2117 * attempt to load firmware only if we're not on the error path, because
2118 * the firmware callback requires a fully functional and allocated sdma
2122 ret = sdma_get_firmware(sdma, pdata->fw_name);
2124 dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
2127 * Because that device tree does not encode ROM script address,
2128 * the RAM script in firmware is mandatory for device tree
2129 * probe, otherwise it fails.
2131 ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
2134 dev_warn(&pdev->dev, "failed to get firmware name\n");
2136 ret = sdma_get_firmware(sdma, fw_name);
2138 dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
2145 dma_async_device_unregister(&sdma->dma_device);
2147 kfree(sdma->script_addrs);
2149 clk_unprepare(sdma->clk_ahb);
2151 clk_unprepare(sdma->clk_ipg);
2155 static int sdma_remove(struct platform_device *pdev)
2157 struct sdma_engine *sdma = platform_get_drvdata(pdev);
2160 devm_free_irq(&pdev->dev, sdma->irq, sdma);
2161 dma_async_device_unregister(&sdma->dma_device);
2162 kfree(sdma->script_addrs);
2163 clk_unprepare(sdma->clk_ahb);
2164 clk_unprepare(sdma->clk_ipg);
2165 /* Kill the tasklet */
2166 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2167 struct sdma_channel *sdmac = &sdma->channel[i];
2169 tasklet_kill(&sdmac->vc.task);
2170 sdma_free_chan_resources(&sdmac->vc.chan);
2173 platform_set_drvdata(pdev, NULL);
2177 static struct platform_driver sdma_driver = {
2180 .of_match_table = sdma_dt_ids,
2182 .id_table = sdma_devtypes,
2183 .remove = sdma_remove,
2184 .probe = sdma_probe,
2187 module_platform_driver(sdma_driver);
2189 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
2190 MODULE_DESCRIPTION("i.MX SDMA driver");
2191 #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2194 #if IS_ENABLED(CONFIG_SOC_IMX7D)
2197 MODULE_LICENSE("GPL");